From 794fa372c9ab45d1f3ff2e66e08a65b1213feb4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 8 Oct 2026 06:02:02 -0400 Subject: [PATCH] Q29: documents record the task that produced them Co-Authored-By: Claude Opus 5.5 --- src/main/services/agent-deliverable-tools.ts | 2 +- src/main/services/agent-document-tools.ts | 14 ++++++++-- tests/unit/agent-document-tools.test.ts | 29 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/services/agent-deliverable-tools.ts b/src/main/services/agent-deliverable-tools.ts index df3dfb90..3dcbcafa 100644 --- a/src/main/services/agent-deliverable-tools.ts +++ b/src/main/services/agent-deliverable-tools.ts @@ -42,7 +42,7 @@ export function createDocumentTool(deps: AgentToolDeps, ctx: AgentToolContext, a reviewerAgentId: null, projectId: ctx.taskProjectId, goalId: ctx.taskGoalId, - taskId: null, + taskId: ctx.taskId, }); const review = requestDeliverableReview(deps, ctx, { title: `Review ${docType}: ${title}`, diff --git a/src/main/services/agent-document-tools.ts b/src/main/services/agent-document-tools.ts index d7a742ef..02415a49 100644 --- a/src/main/services/agent-document-tools.ts +++ b/src/main/services/agent-document-tools.ts @@ -16,6 +16,8 @@ interface DocumentRow { status: string; content: string; updated_at: string; + task_id: string | null; + task_title: string | null; } export const DOCUMENT_TOOLS: ToolDefinition[] = [ @@ -65,12 +67,13 @@ export function listDocumentsTool(deps: AgentToolDeps, ctx: AgentToolContext, ar const docType = args.doc_type === undefined ? null : optionalEnum(args.doc_type, "doc_type", DOCUMENT_TYPES, ""); const limit = optionalLimit(args.limit, "limit", 10, 30); const rows = deps.db.prepare(` - select id, title, doc_type, status, content, updated_at from documents - where org_id = ? and (? is null or doc_type = ?) order by updated_at desc + select d.id, d.title, d.doc_type, d.status, d.content, d.updated_at, d.task_id, t.title as task_title + from documents d left join tasks t on t.id = d.task_id + where d.org_id = ? and (? is null or d.doc_type = ?) order by d.updated_at desc `).all(ctx.orgId, docType, docType) as unknown as DocumentRow[]; const found = query ? relevantItems(rows, (row) => `${row.title} ${row.content}`, query, limit) : rows.slice(0, limit); if (found.length === 0) return query ? `No documents match "${query}".` : "No documents yet."; - return found.map((row) => `${row.title} (doc_id=${row.id}, ${row.doc_type}, ${row.status}, ${row.content.length} chars, updated ${row.updated_at})`).join("\n"); + return found.map((row) => `${row.title} (doc_id=${row.id}, ${row.doc_type}, ${row.status}, ${row.content.length} chars, updated ${row.updated_at}${producedBy(row)})`).join("\n"); } export function readDocumentTool(deps: AgentToolDeps, ctx: AgentToolContext, args: JsonRecord): string { @@ -98,3 +101,8 @@ export function updateDocumentTool(deps: AgentToolDeps, ctx: AgentToolContext, a }); return `Updated document "${title}" (doc_id=${id}).${review ? " The update is queued for board review in Approvals." : ""}`; } + +function producedBy(row: DocumentRow): string { + if (row.task_id === null || row.task_title === null) return ""; + return `, from task "${row.task_title}" task_id=${row.task_id}`; +} diff --git a/tests/unit/agent-document-tools.test.ts b/tests/unit/agent-document-tools.test.ts index 70852b26..e0396c38 100644 --- a/tests/unit/agent-document-tools.test.ts +++ b/tests/unit/agent-document-tools.test.ts @@ -6,6 +6,7 @@ import { DatabaseConnection } from "../../src/main/database/connection"; import { OrganizationService } from "../../src/main/services/organization-service"; import { ChatService } from "../../src/main/services/chat-service"; import { dispatchAgentTool, type AgentToolContext } from "../../src/main/services/agent-tools"; +import { RISK_LEVEL, TASK_PRIORITY, TASK_STATUS } from "../../src/shared/domain"; function harness(reviewDeliverables: boolean) { const connection = new DatabaseConnection(join(tmpdir(), `ac-docs-${randomUUID()}.db`)); @@ -26,6 +27,34 @@ function harness(reviewDeliverables: boolean) { return { connection, orgId, run }; } +describe("document provenance", () => { + test("a document records the task that produced it, shown when listing", () => { + const connection = new DatabaseConnection(join(tmpdir(), `ac-docs-${randomUUID()}.db`)); + const organization = new OrganizationService(connection.db); + let orgId = ""; + let agentId = ""; + let taskId = ""; + connection.transaction(() => { + orgId = organization.createOrganization({ organizationName: "D", employeeName: "Y", mission: "m", goal: "g" }); + agentId = organization.createAgent({ orgId, name: "Ana", role: "PM", department: "Product", modelProviderId: null, modelId: null, budgetCents: 1000, managerAgentId: null }); + taskId = organization.createTask({ + orgId, title: "Write the booking PRD", description: "d", status: TASK_STATUS.IN_PROGRESS, assigneeAgentId: agentId, + risk: RISK_LEVEL.LOW, projectId: null, goalId: null, workspaceId: null, priority: TASK_PRIORITY.NORMAL, requiresFilesystem: false, + }); + }); + const ctx: AgentToolContext = { + orgId, agent: { id: agentId, modelProviderId: null, modelId: null }, + taskProjectId: null, taskGoalId: null, taskId, sourceThreadId: null, workspace: null, + }; + const run = (name: string, args: Record) => + dispatchAgentTool({ db: connection.db, organization, chat: new ChatService(connection.db) }, ctx, { id: `c_${randomUUID()}`, name, arguments: args }); + const docId = /doc_id=(doc_\w+)/.exec(run("create_document", { title: "Booking PRD", doc_type: "prd", content: "v1" }))![1]; + expect(connection.db.prepare("select task_id from documents where id = ?").get(docId)).toEqual({ task_id: taskId }); + expect(run("list_documents", {})).toContain(`from task "Write the booking PRD" task_id=${taskId})`); + connection.close(); + }); +}); + describe("agent document tools", () => { test("documents an agent wrote can be found, read in pages, and updated", () => { const h = harness(false);