diff --git a/apps/desktop/e2e/pages-agents.spec.ts b/apps/desktop/e2e/pages-agents.spec.ts index 839e9171..aa05fcb8 100644 --- a/apps/desktop/e2e/pages-agents.spec.ts +++ b/apps/desktop/e2e/pages-agents.spec.ts @@ -89,8 +89,8 @@ try { const url = fs.readFileSync(path.join(home, "pages-url"), "utf8"); await expect.poll(async () => { const r = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json, text/event-stream" }, body: JSON.stringify({ jsonrpc: "2.0", id: 99, method: "tools/call", params: { name: "search", arguments: { query: "" } } }) }); - return (await r.json()).result?.isError; - }).toBe(true); + return r.status; + }).toBe(404); await app.close(); ({ app, page } = await launch(home)); await expect(page.getByRole("button", { name: "Open note CLI note", exact: true }).last()).toBeVisible(); await page.getByRole("button", { name: "Open note CLI note", exact: true }).last().click(); diff --git a/apps/desktop/e2e/release.spec.ts b/apps/desktop/e2e/release.spec.ts index 4843de67..a8b852e8 100644 --- a/apps/desktop/e2e/release.spec.ts +++ b/apps/desktop/e2e/release.spec.ts @@ -72,6 +72,11 @@ test("minimum window keeps composer controls inside the main pane with Changes o for (const id of ["context-project", "context-kind", "context-branch"]) { expect(await tid(page, id).evaluate((el) => el.scrollWidth <= el.clientWidth + 1), `${id} text overflows its label`).toBe(true); } + const selector = page.getByRole("button", { name: "Context for next request", exact: true }); + await expect(selector).toBeVisible(); + const selectorBox = (await selector.boundingBox())!, composerBox = (await tid(page, "composer-box").boundingBox())!; + expect(selectorBox.x).toBeGreaterThanOrEqual(composerBox.x); + expect(selectorBox.x + selectorBox.width).toBeLessThanOrEqual(composerBox.x + composerBox.width); await page.screenshot({ path: test.info().outputPath("minimum-window.png") }); }); diff --git a/apps/desktop/e2e/task-context-proposals.spec.ts b/apps/desktop/e2e/task-context-proposals.spec.ts new file mode 100644 index 00000000..eaf5c15c --- /dev/null +++ b/apps/desktop/e2e/task-context-proposals.spec.ts @@ -0,0 +1,77 @@ +import { test, expect, type ElectronApplication } from "@playwright/test"; +import fs from "node:fs"; +import path from "node:path"; +import { launch, seedHome, tid, items } from "./support"; + +// Main-process integration with a synthetic Claude executable. No provider turn or native-model qualification. +function fixture(home: string): string { + const cli = path.join(home, "proposal-cli.cjs"); + fs.writeFileSync(cli, `#!${process.execPath} +const fs = require('node:fs'); +const path = require('node:path'); +if (process.argv.includes('--version')) { console.log('2.1.316 (Claude Code)'); process.exit(0); } +if (process.argv.includes('auth')) { console.log(JSON.stringify({ loggedIn: true })); process.exit(0); } +if (path.basename(process.cwd()).startsWith('modex-title-')) process.exit(1); +const emit = value => process.stdout.write(JSON.stringify(value) + '\\n'); +const prior = ${JSON.stringify(path.join(home, "fixture-private-route"))}; +const reports = ${JSON.stringify(path.join(home, "fixture-proposals.jsonl"))}; +let running = false; +require('node:readline').createInterface({ input: process.stdin }).on('line', async line => { + const input = JSON.parse(line); if (input.type !== 'user' || running) return; running = true; + try { + const index = process.argv.indexOf('--mcp-config'); + const connection = index < 0 ? undefined : JSON.parse(process.argv[index + 1]).mcpServers.modex_task_context; + if (!connection) { fs.appendFileSync(reports, JSON.stringify({ missing: true }) + '\\n'); emit({ type: 'assistant', message: { id: 'a', content: [{ type: 'text', text: 'Proposal connection missing' }] } }); return; } + const previous = fs.existsSync(prior) ? fs.readFileSync(prior, 'utf8') : null; + let oldStatus = null; + if (previous) oldStatus = await fetch(previous, { method: 'POST', redirect: 'error', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/event-stream' }, body: JSON.stringify({ jsonrpc: '2.0', id: 'old', method: 'tools/call', params: { name: 'propose_refs', arguments: { refs: [] } } }) }).then(r => r.status, () => 'closed'); + fs.writeFileSync(prior, connection.url, { mode: 0o600 }); + emit({ type: 'system', subtype: 'init', session_id: 'proposal-session', mcp_servers: [{ name: 'modex_task_context', status: 'connected' }] }); + const noteId = JSON.parse(fs.readFileSync(${JSON.stringify(path.join(home, "fixture-note.json"))}, 'utf8')).id; + const response = await fetch(connection.url, { method: 'POST', redirect: 'error', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/event-stream' }, body: JSON.stringify({ jsonrpc: '2.0', id: 'proposal', method: 'tools/call', params: { name: 'propose_refs', arguments: { refs: [{ target: { kind: 'file', path: 'README.md' }, role: 'file' }, { target: { kind: 'note', pageId: noteId }, role: 'note' }] } } }) }); + const result = await response.json(); + fs.appendFileSync(reports, JSON.stringify({ missing: false, oldStatus, fresh: previous !== connection.url, status: response.status, error: result.result?.isError === true, detail: result.result?.isError ? result.result.content : undefined }) + '\\n'); + emit({ type: 'assistant', message: { id: 'a', content: [{ type: 'text', text: 'Proposal fixture finished' }] } }); + } catch (error) { emit({ type: 'assistant', message: { id: 'a', content: [{ type: 'text', text: 'Proposal fixture failed: ' + error.message }] } }); } + finally { emit({ type: 'result', subtype: 'success', result: 'Proposal fixture finished' }); process.stdin.destroy(); setTimeout(() => process.exit(0), 10); } +}); +`, { mode: 0o755 }); + return cli; +} + +test("main owns a proposal service across fresh/resumed turns and revokes each private lease", async () => { + const seeded = seedHome({ default_backend: "claude", default_mode: "chat" }); + const home = fs.realpathSync(seeded.home), repo = fs.realpathSync(seeded.repo); + const stateFile = path.join(home, "app/state.json"), state = JSON.parse(fs.readFileSync(stateFile, "utf8")); + state.settings.claude_bin = fixture(home); state.projects[0].path = repo; + const at = new Date().toISOString(); + state.threads = [{ id: "proposal-thread", projectId: "p1", title: "Proposal fixture", titleSource: "manual", createdAt: at, updatedAt: at, cwd: repo, backend: "claude", mode: "chat", plan: false, auto: false, model: "", status: "idle" }]; + fs.writeFileSync(stateFile, JSON.stringify(state)); fs.writeFileSync(path.join(home, "app/threads/proposal-thread.json"), "[]"); + let app: ElectronApplication | undefined; + try { + const opened = await launch(home); app = opened.app; const page = opened.page; + const note = await page.evaluate(async () => { const project = await window.modex!.invoke("project:notes", { projectId: "p1" }); return window.modex!.invoke("space:create", { containerId: project.id, title: "Uninspected proposal", markdown: "private-note-body-not-inspected" }); }); + fs.writeFileSync(path.join(home, "fixture-note.json"), JSON.stringify({ id: note.id })); + const row = page.locator('[data-testid="thread-row"][data-thread-id="proposal-thread"]'); await row.click(); + for (const index of [0, 1]) { + await tid(page, "composer-input").fill(`Propose references ${index}`); await tid(page, "send").click(); + await expect(items(page, "assistant")).toHaveCount(index + 1); await expect(row).toHaveAttribute("data-status", "idle"); + const stored = await page.evaluate(() => window.modex!.invoke("thread:items", { threadId: "proposal-thread" })); + await test.info().attach(`proposal-fixture-${index}`, { body: fs.readFileSync(path.join(home, "fixture-proposals.jsonl")), contentType: "application/json" }); + const user = stored.filter(item => item.kind === "user")[index]!; expect(user.kind).toBe("user"); if (user.kind !== "user") throw new Error("Missing request"); + expect(user.contextEvidence?.refs.map(ref => ref.target)).toEqual([{ kind: "file", path: "README.md" }, { kind: "note", pageId: note.id }]); + expect(user.contextEvidence?.refs.every(ref => ref.relations.every(relation => relation.provenance === "model-proposed"))).toBe(true); + expect(user.contextEvidence?.captures).toEqual([]); expect(user.contextEvidence?.checks).toEqual([]); + expect(JSON.stringify(user.contextEvidence)).not.toContain("private-note-body-not-inspected"); + expect(user.contextEvidence?.capabilities?.map(receipt => receipt.capability)).toEqual([{ state: "available" }]); + expect(await page.evaluate(turnId => window.modex!.invoke("task:context", { threadId: "proposal-thread", turnId }), user.id)).toEqual(user.contextEvidence); + await expect(page.evaluate(({ turnId, revision, refId }) => window.modex!.invoke("task:check-confirm", { threadId: "proposal-thread", turnId, expectedRevision: revision, itemId: "foreign-check", refIds: [refId] }), { turnId: user.id, revision: user.contextEvidence!.revision, refId: user.contextEvidence!.refs[0]!.id })).rejects.toThrow(/does not belong/); + } + const reports = fs.readFileSync(path.join(home, "fixture-proposals.jsonl"), "utf8").trim().split("\n").map(line => JSON.parse(line)); + expect(reports).toEqual([{ missing: false, oldStatus: null, fresh: true, status: 200, error: false }, { missing: false, oldStatus: 404, fresh: true, status: 200, error: false }]); + const lastURL = fs.readFileSync(path.join(home, "fixture-private-route"), "utf8"); + expect(await fetch(lastURL).then(response => response.status)).toBe(404); + await app.close(); app = undefined; + expect(await fetch(lastURL).then(() => false, () => true)).toBe(true); + } finally { await app?.close(); } +}); diff --git a/apps/desktop/e2e/task-context-selection.spec.ts b/apps/desktop/e2e/task-context-selection.spec.ts new file mode 100644 index 00000000..55e2c821 --- /dev/null +++ b/apps/desktop/e2e/task-context-selection.spec.ts @@ -0,0 +1,474 @@ +import { test, expect, chromium, type Browser, type Page } from "@playwright/test"; +import { createServer } from "vite"; +import fs from "node:fs"; +import path from "node:path"; +import { createHash } from "node:crypto"; +import { launch, seedHome, tid, currentRow, items, appDir, resizeWindow } from "./support"; + +// All provider behavior below belongs to this isolated executable; no real model or credential is used. +function setup() { + const seeded = seedHome({ default_backend: "claude", default_mode: "chat" }); + const home = fs.realpathSync(seeded.home), repo = fs.realpathSync(seeded.repo); + const cli = path.join(home, "context-ui-cli.cjs"), file = path.join(home, "app/state.json"), state = JSON.parse(fs.readFileSync(file, "utf8")); + state.projects[0].path = repo; state.projects[0].pagesMaintenance = true; state.settings.claude_bin = cli; + const second = seedHome(); state.projects.push({ id: "p2", name: "Second project", path: fs.realpathSync(second.repo), addedAt: new Date().toISOString() }); + fs.writeFileSync(file, JSON.stringify(state)); + fs.writeFileSync(cli, `#!${process.execPath} +const fs=require('node:fs'),path=require('node:path'); +if(process.argv.includes('--version')){console.log('2.1.316 (Claude Code)');process.exit(0);} +if(process.argv.includes('auth')){console.log(JSON.stringify({loggedIn:true}));process.exit(0);} +const emit=x=>console.log(JSON.stringify(x)); +const idx=process.argv.indexOf('--mcp-config'); +if(idx<0||path.basename(process.cwd()).startsWith('modex-title-')){emit({type:'result',result:'Context fixture',is_error:false});process.exit(0);} +const servers=JSON.parse(process.argv[idx+1]).mcpServers; +async function rpc(url,method,params,id){const r=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json',Accept:'application/json, text/event-stream'},body:JSON.stringify({jsonrpc:'2.0',id,method,params})});const j=await r.json();if(j.error||j.result?.isError)throw Error('Fixture RPC refused');return j.result;} +require('node:readline').createInterface({input:process.stdin}).once('line',async line=>{try{ + const input=JSON.parse(line);fs.appendFileSync(${JSON.stringify(path.join(home, "context-inputs.jsonl"))},JSON.stringify(input)+'\\n'); + emit({type:'system',subtype:'init',session_id:'context-ui-session',mcp_servers:Object.keys(servers).map(name=>({name,status:'connected'}))}); + if(JSON.stringify(input).includes('Propose context')){ + const countFile=${JSON.stringify(path.join(home, "context-attempt"))};const count=fs.existsSync(countFile)?Number(fs.readFileSync(countFile,'utf8')):0;fs.writeFileSync(countFile,String(count+1)); + const note=JSON.parse(fs.readFileSync(${JSON.stringify(path.join(home, "context-note.json"))},'utf8')); + if(count===0){emit({type:'assistant',message:{content:[{type:'tool_use',id:'unrelated-check',name:'Bash',input:{command:'pnpm test'}}]}});emit({type:'user',message:{content:[{type:'tool_result',tool_use_id:'unrelated-check',content:'Checks passed',is_error:false}]}});await rpc(servers.modex_task_context.url,'tools/call',{name:'propose_refs',arguments:{refs:[{target:{kind:'file',path:'README.md'},role:'file'},{target:{kind:'note',pageId:note.id},role:'note'}]}},'proposal');} + else{ + const target=path.join(process.cwd(),'README.md');const body=fs.readFileSync(target,'utf8'); + emit({type:'assistant',message:{content:[{type:'tool_use',id:'read-context',name:'Read',input:{file_path:target}}]}}); + emit({type:'user',message:{content:[{type:'tool_result',tool_use_id:'read-context',content:body,is_error:false}]}}); + await rpc(servers.modex_pages.url,'initialize',{protocolVersion:'2025-03-26',capabilities:{},clientInfo:{name:'context-ui-fixture',version:'1'}},'init'); + await rpc(servers.modex_pages.url,'tools/call',{name:'read',arguments:{id:note.id}},'read-note'); + } + } + emit({type:'assistant',message:{id:'context-answer',content:[{type:'text',text:'Context fixture complete. All context is verified by pnpm test.'}]}}); + emit({type:'result',result:'Context fixture complete',is_error:false});process.exit(0); +}catch(error){emit({type:'result',result:String(error),is_error:true});process.exit(1);}}); +`, { mode: 0o755 }); + return { home, repo }; +} +async function openContext(page: Page) { + await page.getByRole("button", { name: "Context for next request", exact: true }).click({ timeout: 3000 }); + return page.getByRole("region", { name: "Context for next request", exact: true }); +} +async function notes(page: Page, home: string) { + const note = await page.evaluate(async () => { + const project = await window.modex!.invoke("project:notes", { projectId: "p1" }); + const own = await window.modex!.invoke("space:create", { containerId: project.id, title: "Context reference", markdown: "context-private-body-sentinel" }); + const other = await window.modex!.invoke("project:notes", { projectId: "p2" }); + await window.modex!.invoke("space:create", { containerId: other.id, title: "Foreign reference", markdown: "foreign-private-body-sentinel" }); + return own; + }); + fs.writeFileSync(path.join(home, "context-note.json"), JSON.stringify({ id: note.id })); return note; +} + +test("first request selects scoped files and Notes and reaches the native Send exactly once", async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + await resizeWindow(app, page, 900, 600); + const note = await notes(page, home); + await tid(page, "new-chat").click(); + const trigger = page.getByRole("button", { name: "Context for next request", exact: true }); + const triggerBox = (await trigger.boundingBox())!, kindBox = (await tid(page, "context-kind").boundingBox())!; + expect(triggerBox.x).toBeGreaterThan(kindBox.x + kindBox.width); + const panel = await openContext(page); + const panelBox = (await panel.boundingBox())!, viewport = await page.evaluate(() => ({ width: innerWidth, height: innerHeight })); + expect(panelBox.x).toBeGreaterThanOrEqual(0); expect(panelBox.y).toBeGreaterThanOrEqual(0); + expect(panelBox.x + panelBox.width).toBeLessThanOrEqual(viewport.width); expect(panelBox.y + panelBox.height).toBeLessThanOrEqual(viewport.height); + await expect(panel.getByRole("checkbox", { name: "README.md", exact: true })).toBeVisible(); + await expect(panel.getByRole("checkbox", { name: "Note: Context reference", exact: true })).toBeVisible(); + await expect(panel.getByText("Foreign reference", { exact: true })).toHaveCount(0); + await panel.getByRole("checkbox", { name: "README.md", exact: true }).check(); + await panel.getByRole("checkbox", { name: "Note: Context reference", exact: true }).check(); + await panel.getByRole("button", { name: "Apply context", exact: true }).click(); + await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(2); + await page.screenshot({ path: test.info().outputPath("context-composer.png"), animations: "disabled" }); + expect((await page.evaluate(() => window.modex!.invoke("state:get", undefined))).threads).toHaveLength(0); + await tid(page, "composer-input").fill("Selected request"); await tid(page, "send").click(); + await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + await expect(items(page, "assistant").last()).toContainText("Context fixture complete"); + const threadId = (await currentRow(page).getAttribute("data-thread-id"))!; + const stored = await page.evaluate(id => window.modex!.invoke("thread:items", { threadId: id }), threadId); + const requests = stored.filter(item => item.kind === "user"); expect(requests).toHaveLength(1); + expect(requests[0]!.contextEvidence!.refs.map(ref => ref.target)).toEqual([{ kind: "file", path: "README.md" }, { kind: "note", pageId: note.id }]); + const inputs = fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8").trim().split("\n"); expect(inputs).toHaveLength(1); + expect(inputs[0]).toContain("Modex selected project references"); expect(inputs[0]).not.toContain("context-private-body-sentinel"); + } finally { await app.close(); } +}); + +test("project switches revoke old draft nonces and retain selections only in their own scope", async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + await notes(page, home); + await app.evaluate(({ ipcMain }) => { + const original = (ipcMain as any)._invokeHandlers.get("task:context-draft-open"); ipcMain.removeHandler("task:context-draft-open"); + ipcMain.handle("task:context-draft-open", async (event, request) => { const binding = await original(event, request); (globalThis as any).__contextBinding = binding; return binding; }); + }); + await tid(page, "new-chat").click(); let panel = await openContext(page); + await panel.getByRole("checkbox", { name: "README.md", exact: true }).check(); await panel.getByRole("button", { name: "Apply context", exact: true }).click(); + const old = await app.evaluate(() => (globalThis as any).__contextBinding); + await tid(page, "draft-project").click(); await page.locator('[data-testid="draft-project-option"][data-project-id="p2"]').click(); + panel = await openContext(page); await expect(panel.getByRole("checkbox", { name: "Note: Foreign reference", exact: true })).toBeVisible(); + await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(0); + await expect(page.evaluate(binding => window.modex!.invoke("task:context-draft-select", { binding, targets: [] }), old)).rejects.toThrow(/draft|stale/); + await tid(page, "draft-project").click(); await page.locator('[data-testid="draft-project-option"][data-project-id="p1"]').click(); + panel = await openContext(page); await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(1); + } finally { await app.close(); } +}); + +test("a model proposal is confirmed explicitly, inspected on Retry and frozen by Save and reopen", async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + const note = await notes(page, home); await tid(page, "new-chat").click(); + await tid(page, "composer-input").fill("Propose context"); await tid(page, "send").click(); + await expect(items(page, "assistant").last()).toContainText("Context fixture complete"); await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + const threadId = (await currentRow(page).getAttribute("data-thread-id"))!; + await page.getByRole("button", { name: "Task context · 2", exact: true }).click({ timeout: 3000 }); + const panel = page.getByRole("region", { name: "Task context", exact: true }); + await expect(panel.getByText("Proposed by model", { exact: true })).toHaveCount(2); + await panel.getByText("Proposed role: file", { exact: true }).waitFor({ timeout: 3000 }); + await expect(panel.getByText("Proposed role: note", { exact: true })).toBeVisible(); + await expect(panel.getByText("Observed read", { exact: true })).toHaveCount(0); + const proposal = (await page.evaluate(id => window.modex!.invoke("thread:items", { threadId: id }), threadId)).find(item => item.kind === "user")!; + if (proposal.kind !== "user" || !proposal.contextEvidence) throw Error("Missing proposed context"); + let confirmedRevision = proposal.contextEvidence.revision; + for (const name of ["Relevant: README.md", `Relevant: Note ${note.id}`]) { + const checkbox = panel.getByRole("checkbox", { name, exact: true }); + await checkbox.click(); await expect(checkbox).toBeChecked(); await expect(checkbox).toBeEnabled(); + const context = await page.evaluate(address => window.modex!.invoke("task:context", address), { threadId, turnId: proposal.id }); + expect(context.revision).toBe(++confirmedRevision); + const confirmed = context.refs.filter(ref => ref.relations.some(relation => relation.provenance === "user-confirmed")); + expect(confirmed.map(ref => ref.target)).toEqual(name === "Relevant: README.md" ? [{ kind: "file", path: "README.md" }] : [{ kind: "file", path: "README.md" }, { kind: "note", pageId: note.id }]); + } + await expect(panel.getByText("Confirmed by you", { exact: true })).toHaveCount(2); + const initial = await page.evaluate(id => window.modex!.invoke("thread:items", { threadId: id }), threadId); + const user = initial.find(item => item.kind === "user")!; expect(user.kind).toBe("user"); if (user.kind !== "user") throw Error("Missing user"); + expect(user.contextEvidence!.captures).toEqual([]); + expect(user.contextEvidence!.checks[0]!.relevance).toBe("unestablished"); + await expect(tid(page, "task-verification").last()).toHaveAttribute("data-state", "unknown"); + const checkControls = panel.getByRole("group", { name: "Check relevance: pnpm test", exact: true }); + await checkControls.getByRole("button", { name: /^Show native item / }).click({ timeout: 3000 }); + await expect(page.locator('[data-item-kind="tool"][data-item-id]').filter({ hasText: "pnpm test" }).first()).toBeFocused(); + await checkControls.getByRole("checkbox", { name: "Check reference: README.md", exact: true }).check({ timeout: 3000 }); + await checkControls.getByRole("button", { name: "Apply check relevance", exact: true }).click(); + await expect(checkControls).toContainText("user-confirmed"); + await expect(tid(page, "task-verification").last()).toHaveAttribute("data-state", "unknown"); + await checkControls.getByRole("button", { name: "Clear check relevance", exact: true }).click(); + await expect(checkControls).toContainText("unestablished"); + const retryReceipt = await page.evaluate(id => window.modex!.invoke("thread:retry", { threadId: id }), threadId); + expect(retryReceipt.ok, retryReceipt.error).toBe(true); + await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + await test.info().attach("retry-metadata", { body: JSON.stringify({ count: fs.readFileSync(path.join(home, "context-attempt"), "utf8"), items: await page.evaluate(id => window.modex!.invoke("thread:items", { threadId: id }), threadId) }), contentType: "application/json" }); + await expect(panel.getByText("Observed read", { exact: true })).toHaveCount(2); + await panel.getByRole("button", { name: /^Show native item / }).first().click({ timeout: 3000 }); + await expect(page.locator('[data-item-kind="tool"][data-item-id]').filter({ hasText: "README.md" }).first()).toBeFocused(); + const after = await page.evaluate(({ threadId, turnId }) => window.modex!.invoke("task:context", { threadId, turnId }), { threadId, turnId: user.id }); + expect(JSON.stringify(after)).not.toContain("context-private-body-sentinel"); + expect(after.captures.some(c => c.target.kind === "note" && c.target.pageId === note.id && c.revision.state === "available" && c.revision.digest === createHash("sha256").update("context-private-body-sentinel").digest("hex"))).toBe(true); + await checkControls.getByRole("checkbox", { name: "Check reference: README.md", exact: true }).check(); + await checkControls.getByRole("button", { name: "Apply check relevance", exact: true }).click(); + await expect(checkControls).toContainText("user-confirmed"); + await tid(page, "save-task-note").last().click(); await expect(tid(page, "task-note-saved").last()).toContainText("Saved to Notes"); + const before = fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8"); + await tid(page, "task-note-saved").last().getByRole("button", { name: "Open", exact: true }).click(); await tid(page, "task-view-open").click(); + const frozen = tid(page, "task-view"); await expect(frozen).toBeVisible(); + await expect(frozen.getByText(/Files touched/)).toHaveCount(0); + await expect(tid(frozen, "task-view-file")).toHaveCount(0); + await frozen.getByRole("button", { name: "Task context · 2", exact: true }).click(); + await expect(frozen.getByText("Observed read", { exact: true })).toHaveCount(2); + await expect(frozen.getByRole("checkbox", { name: /^Relevant:/ })).toHaveCount(0); + await expect(frozen.getByRole("checkbox", { name: /^Check reference:/ })).toHaveCount(0); + await expect(frozen.getByRole("button", { name: "Apply check relevance", exact: true })).toHaveCount(0); + const mapped = frozen.getByTestId("context-reference").filter({ hasText: "README.md" }).getByTestId("context-check-mapping"); + await expect(mapped).toHaveCount(1); + await expect(mapped).toContainText("pnpm test"); + await expect(mapped).toContainText("user-confirmed"); + await expect(frozen.getByTestId("context-check-mapping")).toHaveCount(1); + await page.screenshot({ path: test.info().outputPath("context-frozen-task.png"), animations: "disabled" }); + expect(fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8")).toBe(before); + } finally { await app.close(); } +}); + +test("a scoped Note preview uses the human Notes view and grants no agent capability", async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + await notes(page, home); + await page.evaluate(() => window.modex!.invoke("project:pages", { projectId: "p1", enabled: false })); + await tid(page, "new-chat").click(); const panel = await openContext(page); + await panel.getByRole("button", { name: "Preview Note: Context reference", exact: true }).click({ timeout: 3000 }); + await expect(page.getByRole("textbox", { name: "Page title", exact: true })).toHaveValue("Context reference"); + const state = await page.evaluate(() => window.modex!.invoke("state:get", undefined)); + expect(state.threads).toHaveLength(0); expect(state.projects.find(project => project.id === "p1")!.pagesMaintenance).toBe(false); + expect(fs.existsSync(path.join(home, "context-inputs.jsonl"))).toBe(false); + } finally { await app.close(); } +}); + +test("selection errors recover, keyboard focus stays local and overflow expansion never rehashes retained references", async () => { + const { home, repo } = setup(); for (let i = 0; i < 23; i++) fs.writeFileSync(path.join(repo, `context-${String(i).padStart(2, "0")}.ts`), `// ${i}\n`); + const { app, page } = await launch(home); + try { + await app.evaluate(({ ipcMain }) => { + const original = (ipcMain as any)._invokeHandlers.get("task:context-draft-select"); let fail = true; (globalThis as any).__contextSelectCalls = 0; ipcMain.removeHandler("task:context-draft-select"); + ipcMain.handle("task:context-draft-select", async (event, request) => { (globalThis as any).__contextSelectCalls++; if (fail) { fail = false; throw Error("Synthetic context selection failure"); } const result = await original(event, request); (globalThis as any).__contextSelection = result; return result; }); + }); + await tid(page, "new-chat").click(); const trigger = page.getByRole("button", { name: "Context for next request", exact: true }); await trigger.waitFor({ timeout: 3000 }); await trigger.focus(); await page.keyboard.press("Enter"); + const panel = page.getByRole("region", { name: "Context for next request", exact: true }); + await panel.getByRole("button", { name: /Show all candidates/ }).click(); + for (let i = 0; i < 23; i++) await panel.getByRole("checkbox", { name: `context-${String(i).padStart(2, "0")}.ts`, exact: true }).check(); + await panel.getByRole("button", { name: "Apply context", exact: true }).click(); await expect(panel.getByRole("alert")).toContainText("Synthetic context selection failure"); + await panel.getByRole("button", { name: "Apply context", exact: true }).click(); await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(20); + const before = await app.evaluate(() => ({ calls: (globalThis as any).__contextSelectCalls, selection: (globalThis as any).__contextSelection })); + expect(before.calls).toBe(2); expect(before.selection.context.refs).toHaveLength(23); + fs.writeFileSync(path.join(repo, "context-00.ts"), "changed after selection\n"); + await panel.getByRole("button", { name: "Show all 23 references", exact: true }).click(); await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(23); + expect(await app.evaluate(() => ({ calls: (globalThis as any).__contextSelectCalls, selection: (globalThis as any).__contextSelection }))).toEqual(before); + const digest = before.selection.context.captures[0].revision.digest; await panel.getByTestId("context-reference").first().locator("summary").first().click(); await expect(panel.getByTestId("context-digest").first()).toHaveText(digest); + await page.keyboard.press("Escape"); await expect(trigger).toBeFocused(); await expect(panel).toHaveCount(0); + } finally { await app.close(); } +}); + +test("turn relevance controls share the retained-reference limit and reveal overflow explicitly", async () => { + const { home, repo } = setup(); for (let i = 0; i < 23; i++) fs.writeFileSync(path.join(repo, `reference-${i}.ts`), "// selected\n"); + const { app, page } = await launch(home); + try { + await tid(page, "new-chat").click(); const draft = await openContext(page); + await draft.getByRole("button", { name: /Show all candidates/ }).click(); + for (let i = 0; i < 23; i++) await draft.getByRole("checkbox", { name: `reference-${i}.ts`, exact: true }).check(); + await draft.getByRole("button", { name: "Apply context", exact: true }).click(); + await tid(page, "composer-input").fill("Selected references"); await tid(page, "send").click(); + await expect(items(page, "assistant").last()).toContainText("Context fixture complete"); await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + await page.getByRole("button", { name: "Task context · 23", exact: true }).click(); + const turn = page.getByRole("region", { name: "Task context", exact: true }); + await expect(turn.getByRole("checkbox", { name: /^Relevant:/ })).toHaveCount(20); + await turn.getByRole("button", { name: "Show all 23 references", exact: true }).click(); + await expect(turn.getByRole("checkbox", { name: /^Relevant:/ })).toHaveCount(23); + const threadId = (await currentRow(page).getAttribute("data-thread-id"))!; + const original = (await page.evaluate(threadId => window.modex!.invoke("thread:items", { threadId }), threadId)).find(item => item.kind === "user"); + expect(original?.kind).toBe("user"); + fs.writeFileSync(path.join(repo, "reference-1.ts"), "// changed before Save\n"); + fs.unlinkSync(path.join(repo, "reference-21.ts")); + await tid(page, "save-task-note").last().click(); + const saved = tid(page, "task-note-saved").last(); await expect(saved).toContainText("Saved to Notes"); + const note = (await page.evaluate(() => window.modex!.invoke("space:list", undefined))).find(note => note.title.startsWith("Task:"))!; + const snapshot = await page.evaluate(pageId => window.modex!.invoke("task:note", { pageId }), note.id); + expect(snapshot!.record.contextEvidence).toEqual(original?.kind === "user" ? original.contextEvidence : undefined); + const inputs = fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8"); + fs.writeFileSync(path.join(repo, "reference-0.ts"), "// changed after Save\n"); + fs.unlinkSync(path.join(repo, "reference-22.ts")); + // Capture the fixed clipboard IPC locally: never overwrite the person's clipboard. + await app.evaluate(({ ipcMain }) => { ipcMain.removeHandler("clipboard:write"); let fail = true; ipcMain.handle("clipboard:write", (_event, request) => { if (fail) { fail = false; throw Error("Fixture clipboard unavailable"); } (globalThis as any).__savedTaskCopy = request.text; }); }); + await saved.getByRole("button", { name: "Open", exact: true }).click(); await tid(page, "task-view-open").click(); + const frozen = tid(page, "task-view"); + await frozen.getByRole("button", { name: "Copy full saved record", exact: true }).click({ timeout: 3000 }); + await expect(frozen.getByRole("alert")).toContainText("Fixture clipboard unavailable"); + await expect(frozen.getByRole("status")).toHaveCount(0); + expect(await app.evaluate(() => (globalThis as any).__savedTaskCopy)).toBeUndefined(); + await frozen.getByRole("button", { name: "Copy full saved record", exact: true }).click(); + await expect(frozen.getByRole("status")).toContainText("Full saved record copied"); + const copied = await app.evaluate(() => (globalThis as any).__savedTaskCopy as string); + const metadata = JSON.parse(copied.match(/```json\n([\s\S]*?)\n```/)![1]!); + expect(metadata).toEqual(snapshot!.record.contextEvidence); expect(metadata.refs).toHaveLength(23); + expect(copied).toContain("reference-22.ts"); expect(copied).toContain(snapshot!.savedAt); + expect(copied).toContain(`Saved revision: ${snapshot!.savedRevision}`); + expect(copied).toContain(`Project: ${JSON.stringify(snapshot!.projectId)}`); + expect(copied).toContain(`Note: ${JSON.stringify(note.id)}`); + expect(copied).toContain(`Turn: ${JSON.stringify(snapshot!.turnId)}`); + expect(copied).not.toContain(repo); expect(copied).not.toContain("changed after Save"); + expect(await page.evaluate(pageId => window.modex!.invoke("task:note", { pageId }), note.id)).toEqual(snapshot); + expect((await page.evaluate(() => window.modex!.invoke("space:list", undefined))).find(page => page.id === note.id)).toEqual(note); + expect(fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8")).toBe(inputs); + } finally { await app.close(); } +}); + +test("Files includes an explicit reference in the next composer without sending or claiming inspection", async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + await tid(page, "new-chat").click(); await tid(page, "composer-input").fill("Open files"); await tid(page, "send").click(); + await expect(items(page, "assistant").last()).toContainText("Context fixture complete"); await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + if (!await tid(page, "workspace").isVisible()) await tid(page, "changes-toggle").click(); + await tid(page, "workspace-add").click(); await page.getByRole("menuitem", { name: "New tab ⇧⌘B", exact: true }).click(); await page.getByRole("button", { name: "Files ⌘P", exact: true }).click(); + await page.getByRole("treeitem", { name: "Open README.md", exact: true }).click(); + await page.getByRole("button", { name: "Include in next request", exact: true }).click({ timeout: 3000 }); + const panel = page.getByRole("region", { name: "Context for next request", exact: true }); + await expect(panel.getByText("README.md", { exact: true }).last()).toBeVisible(); + await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(1); + expect(fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8").trim().split("\n")).toHaveLength(1); + } finally { await app.close(); } +}); + +test("browser create/send retains its held binding when the person switches projects and returns", async () => { + const { home } = setup(), stateFile = path.join(home, "app/state.json"), state = JSON.parse(fs.readFileSync(stateFile, "utf8")); + state.settings.default_backend = "mock"; state.settings.mock_script = path.join(home, "browser-context.json"); + fs.writeFileSync(state.settings.mock_script, JSON.stringify([{ content: "Browser context done." }])); fs.writeFileSync(stateFile, JSON.stringify(state)); + const keys = ["MODEX_BROWSER_HOME", "MODEX_E2E", "MODEX_NO_LOGIN_PATH", "TYPESAFE_API_KEY", "JEV_CONFIG", "JEV_API_KEY"] as const; + const previous = Object.fromEntries(keys.map(key => [key, process.env[key]])); + Object.assign(process.env, { MODEX_BROWSER_HOME: home, MODEX_E2E: "1", MODEX_NO_LOGIN_PATH: "1", TYPESAFE_API_KEY: "", JEV_API_KEY: "", JEV_CONFIG: path.join(home, "no-jev-config.json") }); + const server = await createServer({ configFile: path.join(appDir, "vite.config.ts"), server: { port: 0, host: "127.0.0.1", open: false } }); + let browser: Browser | undefined, release!: () => void; const gate = new Promise(resolve => { release = resolve; }); + let creates = 0, sends = 0; + try { + await server.listen(); const address = server.httpServer!.address(); if (!address || typeof address === "string") throw Error("Missing browser listener"); + browser = await chromium.launch(); const page = await browser.newPage({ viewport: { width: 1400, height: 1000 } }); + await page.route("**/__modex/invoke", async route => { + const request = route.request().postDataJSON(); + if (request.channel === "thread:create") { creates++; await gate; } + if (request.channel === "thread:send") sends++; + await route.continue(); + }); + await page.goto(`http://127.0.0.1:${address.port}`); await tid(page, "new-chat").click(); + const initial = await openContext(page); await initial.getByRole("checkbox", { name: "README.md", exact: true }).check(); await initial.getByRole("button", { name: "Apply context", exact: true }).click(); + await tid(page, "composer-input").fill("Browser selected context"); await tid(page, "send").click(); await expect.poll(() => creates).toBe(1); + await tid(page, "draft-project").click(); await page.locator('[data-testid="draft-project-option"][data-project-id="p2"]').click(); + await tid(page, "draft-project").click(); await page.locator('[data-testid="draft-project-option"][data-project-id="p1"]').click(); + const returned = page.getByRole("region", { name: "Context for next request", exact: true }); + await expect(returned).toBeVisible({ timeout: 3000 }); + await expect(returned.getByRole("checkbox", { name: "README.md", exact: true })).toBeDisabled(); + release(); await expect(items(page, "assistant").last()).toContainText("Browser context done."); await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + expect(creates).toBe(1); expect(sends).toBe(1); + const id = (await currentRow(page).getAttribute("data-thread-id"))!; + const stored = JSON.parse(fs.readFileSync(path.join(home, "app/threads", `${id}.json`), "utf8")); + expect(stored.filter((item: any) => item.kind === "user")).toHaveLength(1); + expect(stored.find((item: any) => item.kind === "user").contextEvidence.refs[0].target).toEqual({ kind: "file", path: "README.md" }); + } finally { + release(); await browser?.close(); await server.close(); + for (const key of keys) { if (previous[key] === undefined) delete process.env[key]; else process.env[key] = previous[key]; } + } +}); + +test("a revoked draft reloads saved metadata without recapturing changed files", async () => { + const { home, repo } = setup(), { app, page } = await launch(home); + try { + await app.evaluate(({ ipcMain }) => { + const handlers = (ipcMain as any)._invokeHandlers; + const open = handlers.get("task:context-draft-open"), select = handlers.get("task:context-draft-select"); + ipcMain.removeHandler("task:context-draft-open"); ipcMain.removeHandler("task:context-draft-select"); (globalThis as any).__captureSelections = 0; + ipcMain.handle("task:context-draft-open", async (event, request) => { const binding = await open(event, request); (globalThis as any).__openedBinding = binding; return binding; }); + ipcMain.handle("task:context-draft-select", async (event, request) => { (globalThis as any).__captureSelections++; const result = await select(event, request); (globalThis as any).__selectedBinding = result.binding; (globalThis as any).__savedSelection = result.context; return result; }); + }); + await tid(page, "new-chat").click(); const panel = await openContext(page); + await panel.getByRole("checkbox", { name: "README.md", exact: true }).check(); await panel.getByRole("button", { name: "Apply context", exact: true }).click(); + await expect(panel.getByText("Selected, not inspected", { exact: true })).toHaveCount(1); + const saved = await app.evaluate(() => ({ binding: (globalThis as any).__selectedBinding, context: (globalThis as any).__savedSelection })); + await page.evaluate(binding => window.modex!.invoke("task:context-draft-close", { binding }), saved.binding); + fs.writeFileSync(path.join(repo, "README.md"), "changed since saved selection\n"); + await panel.getByRole("checkbox", { name: "package.json", exact: true }).check(); await panel.getByRole("button", { name: "Apply context", exact: true }).click(); + await expect(panel.getByRole("alert")).toContainText(/draft|stale/i); + await panel.getByRole("button", { name: "Reload saved context", exact: true }).click({ timeout: 3000 }); + await expect(panel.getByRole("alert")).toHaveCount(0); + await expect(panel.getByRole("checkbox", { name: "README.md", exact: true })).toBeChecked(); await expect(panel.getByRole("checkbox", { name: "package.json", exact: true })).not.toBeChecked(); + const reloaded = await app.evaluate(() => ({ binding: (globalThis as any).__openedBinding, calls: (globalThis as any).__captureSelections })); + expect(reloaded.binding.nonce).not.toBe(saved.binding.nonce); expect(reloaded.calls).toBe(2); + const metadata = await page.evaluate(binding => window.modex!.invoke("task:context-draft", { binding }), reloaded.binding); + expect(metadata).toEqual(saved.context); + await panel.getByTestId("context-reference").first().locator("summary").first().click(); await expect(panel.getByTestId("context-digest").first()).toHaveText(saved.context.captures[0].revision.digest); + } finally { await app.close(); } +}); + + +test("legacy saved Task view and full copy omit private commands without rewriting saved bytes", async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + await tid(page, "new-chat").click(); await tid(page, "composer-input").fill("Portable legacy task"); await tid(page, "send").click(); + await expect(items(page, "assistant").last()).toContainText("Context fixture complete"); await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + await tid(page, "save-task-note").last().click(); const saved = tid(page, "task-note-saved").last(); await expect(saved).toContainText("Saved to Notes"); + const note = (await page.evaluate(() => window.modex!.invoke("space:list", undefined))).find(note => note.title.startsWith("Task:"))!; + const taskFile = path.join(home, "notes", ".modex", "tasks", `${note.id}.json`); + const snapshot = JSON.parse(fs.readFileSync(taskFile, "utf8")); + const sentinel = "legacy-command-private-sentinel", first = `pnpm test --config /private/${sentinel}`, second = `${first}-second`; + snapshot.record.commands = [{ command: first, outcome: "failed", exitCode: 1 }, { command: second, outcome: "succeeded", exitCode: 0 }]; + snapshot.record.verification = { state: "failed", edits: 1, checks: [{ command: first, outcome: "failed", exitCode: 1, passed: false, stale: false }, { command: second, outcome: "succeeded", exitCode: 0, passed: true, stale: false }] }; + delete snapshot.record.contextEvidence; + snapshot.record.context = { files: [], outside: 1, unlisted: [first, second] }; + fs.writeFileSync(taskFile, JSON.stringify(snapshot)); + const snapshotBytes = fs.readFileSync(taskFile), inputs = fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8"); + const history = await page.evaluate(pageId => window.modex!.invoke("space:history", { id: pageId }), note.id); + await app.evaluate(({ ipcMain }) => { ipcMain.removeHandler("clipboard:write"); ipcMain.handle("clipboard:write", (_event, request) => { (globalThis as any).__legacyTaskCopy = request.text; }); }); + await saved.getByRole("button", { name: "Open", exact: true }).click(); await tid(page, "task-view-open").click(); + const frozen = tid(page, "task-view"); await expect(frozen).toBeVisible(); + await expect(frozen).not.toContainText(sentinel); + await expect(frozen.getByTestId("task-view-unlisted")).toHaveCount(2); + await expect(frozen.getByTestId("task-view-unlisted").nth(0)).toContainText("Recorded command 1"); + await expect(frozen.getByTestId("task-view-unlisted").nth(1)).toContainText("Recorded command 2"); + await expect(frozen.getByTestId("task-view-verification")).toHaveAttribute("data-state", "failed"); + expect(await frozen.locator("[title]").evaluateAll(nodes => nodes.map(node => node.getAttribute("title")).join(" "))).not.toContain(sentinel); + await frozen.getByRole("button", { name: "Copy full saved record", exact: true }).click(); + await expect(frozen.getByRole("status")).toContainText("Full saved record copied"); + const copied = await app.evaluate(() => (globalThis as any).__legacyTaskCopy as string); + expect(copied).not.toContain(sentinel); expect(copied).toContain("Recorded command 1"); expect(copied).toContain("Recorded command 2"); expect(copied).toContain("exit code 1"); + expect(fs.readFileSync(taskFile)).toEqual(snapshotBytes); + expect((await page.evaluate(() => window.modex!.invoke("space:list", undefined))).find(page => page.id === note.id)).toEqual(note); + expect(await page.evaluate(pageId => window.modex!.invoke("space:history", { id: pageId }), note.id)).toEqual(history); + expect(fs.readFileSync(path.join(home, "context-inputs.jsonl"), "utf8")).toBe(inputs); + } finally { await app.close(); } +}); + +for (const stale of [false, true]) test(`confirmation waits for main authority: ${stale ? "stale rejection" : "accepted update"}`, async () => { + const { home } = setup(), { app, page } = await launch(home); + try { + await notes(page, home); await tid(page, "new-chat").click(); + await tid(page, "composer-input").fill("Propose context"); await tid(page, "send").click(); + await expect(items(page, "assistant").last()).toContainText("Context fixture complete"); + await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); + const threadId = (await currentRow(page).getAttribute("data-thread-id"))!; + const user = (await page.evaluate(threadId => window.modex!.invoke("thread:items", { threadId }), threadId)).find(item => item.kind === "user")!; + const address = { threadId, turnId: user.id }; + const before = await page.evaluate(address => window.modex!.invoke("task:context", address), address); + const ref = before.refs.find(ref => ref.target.kind === "file" && ref.target.path === "README.md")!; + await page.getByRole("button", { name: "Task context · 2", exact: true }).click(); + const panel = page.getByRole("region", { name: "Task context", exact: true }); + const checkbox = panel.getByRole("checkbox", { name: "Relevant: README.md", exact: true }); + await checkbox.evaluate(input => { + const events: { type: string; trusted: boolean; checked: boolean; canceled: boolean }[] = []; + (window as any).__confirmationEvents = events; + for (const type of ["click", "change"]) input.addEventListener(type, event => { + if (events.length < 64) events.push({ type, trusted: event.isTrusted, checked: (input as HTMLInputElement).checked, canceled: event.defaultPrevented }); + }); + }); + await app.evaluate(({ ipcMain }) => { + const original = (ipcMain as any)._invokeHandlers.get("task:context-confirm"); + if (typeof original !== "function") throw Error("Missing original context confirmation handler"); + let release!: () => void; + const gate = new Promise(resolve => { release = resolve; }); + const probe = { requests: [] as any[], outcomes: [] as any[], release, restore: () => { ipcMain.removeHandler("task:context-confirm"); ipcMain.handle("task:context-confirm", original); } }; + (globalThis as any).__heldConfirmation = probe; + ipcMain.removeHandler("task:context-confirm"); + ipcMain.handle("task:context-confirm", async (event, request) => { + probe.requests.push(structuredClone(request)); + if (probe.requests.length === 1) await gate; + try { const result = await original(event, request); probe.outcomes.push({ accepted: true, revision: result.revision }); return result; } + catch (error) { probe.outcomes.push({ accepted: false, error: String(error) }); throw error; } + }); + }); + await checkbox.click(); + await expect(checkbox).toBeDisabled(); await expect(checkbox).not.toBeChecked(); + const requests = await app.evaluate(() => (globalThis as any).__heldConfirmation.requests); + expect(requests).toEqual([{ ...address, expectedRevision: before.revision, refIds: [ref.id] }]); + expect(await page.evaluate(address => window.modex!.invoke("task:context", address), address)).toEqual(before); + await expect(panel.getByText("Confirmed by you", { exact: true })).toHaveCount(0); + const events = await page.evaluate(() => (window as any).__confirmationEvents); + expect(events).toEqual([{ type: "click", trusted: true, checked: true, canceled: false }, { type: "change", trusted: true, checked: false, canceled: false }]); + let current = before; + if (stale) { + // A second, genuine main-owned confirmation advances the revision while the first request is held. + current = await page.evaluate(request => window.modex!.invoke("task:context-confirm", request), { ...address, expectedRevision: before.revision, refIds: [] }); + expect(current.revision).toBe(before.revision + 1); + } + await app.evaluate(() => (globalThis as any).__heldConfirmation.release()); + await expect(checkbox).toBeEnabled(); + if (stale) { + await expect(panel.getByRole("alert")).toContainText("Task context changed; review it again."); + await expect(checkbox).not.toBeChecked(); await expect(panel.getByText("Confirmed by you", { exact: true })).toHaveCount(0); + expect(await page.evaluate(address => window.modex!.invoke("task:context", address), address)).toEqual(current); + expect(await app.evaluate(() => (globalThis as any).__heldConfirmation.requests.length)).toBe(2); + } else { + await expect(checkbox).toBeChecked(); await expect(panel.getByText("Confirmed by you", { exact: true })).toHaveCount(1); + current = await page.evaluate(address => window.modex!.invoke("task:context", address), address); + expect(current.revision).toBe(before.revision + 1); + expect(current.refs.find(value => value.id === ref.id)!.relations.filter(value => value.provenance === "user-confirmed")).toHaveLength(1); + expect(await app.evaluate(() => (globalThis as any).__heldConfirmation.requests.length)).toBe(1); + } + const disk = JSON.parse(fs.readFileSync(path.join(home, "app/threads", `${threadId}.json`), "utf8")); + const persisted = disk.find((item: { id: string }) => item.id === user.id)!; + expect(persisted.kind).toBe("user"); expect(persisted.contextEvidence).toEqual(current); + await test.info().attach("held-confirmation-observations", { contentType: "application/json", body: JSON.stringify({ stale, requests, events, before, after: current, outcomes: await app.evaluate(() => (globalThis as any).__heldConfirmation.outcomes) }) }); + } finally { + await app.evaluate(() => { const probe = (globalThis as any).__heldConfirmation; probe?.release(); probe?.restore(); }); + await app.close(); + } +}); diff --git a/apps/desktop/e2e/task-to-notes.spec.ts b/apps/desktop/e2e/task-to-notes.spec.ts index 780b8a43..d1d8c9b8 100644 --- a/apps/desktop/e2e/task-to-notes.spec.ts +++ b/apps/desktop/e2e/task-to-notes.spec.ts @@ -287,8 +287,12 @@ test("a saved task note reopens read-only as the task it saved, asks the CLI not await expect(tid(view, "task-view-request")).toHaveText("Fix the failing import"); await expect(tid(view, "task-view-step")).toHaveText(["Reported done Ask which import", "Reported done Fix the import", "Reported done Run the tests"]); await expect(tid(view, "task-view-clarification")).toHaveText(["Which import should I fix? — lodash"]); - await expect(tid(view, "task-view-file")).toHaveText(["src/app.ts changed"]); - await expect(tid(view, "task-view-verification")).toHaveAttribute("data-state", "verified"); + await expect(tid(view, "task-view-file")).toHaveCount(0); + await view.getByRole("button", { name: "Task context · 1", exact: true }).click(); + await expect(tid(view, "context-reference")).toContainText("src/app.ts"); + await expect(tid(view, "context-reference")).toContainText("Observed change"); + await expect(tid(view, "task-view-verification")).toHaveAttribute("data-state", "unknown"); + await expect(tid(view, "task-view-verification")).toContainText("confirmed relevance"); await expect(tid(view, "task-view-verification")).toContainText("pnpm test"); await expect(tid(view, "task-view-edited")).toHaveCount(0); await expect(tid(view, "task-view-moved")).toHaveCount(0); @@ -319,3 +323,28 @@ test("a saved task note reopens read-only as the task it saved, asks the CLI not for (const dir of [seeded.home, seeded.repo, logs]) fs.rmSync(dir, { recursive: true, force: true }); } }); + +test("a modern task cannot inherit legacy footer verification from an unrelated green command", async () => { + const priorLists = await tid(page, "task-context").count(); + const origin = { thread: threadId, turn: "modern-check", attempt: "modern-attempt", backend: "codex", native: { runtime: "fixture", item: "green-command" } }; + await app.evaluate(({ BrowserWindow }, events) => { + for (const event of events) BrowserWindow.getAllWindows()[0]!.webContents.send("thread:event", event); + }, [ + { type: "item", threadId, item: { id: "modern-check", kind: "user", text: "Modern task", at: at(), contextEvidence: { revision: 0, refs: [], captures: [], checks: [] } } }, + { type: "item", threadId, item: { id: "modern-green", kind: "tool", name: "exec_command", title: "test", args: {}, exec: { command: "pnpm test", cwd: repo }, status: "done", outcome: "succeeded", exitCode: 0, origin, at: at() } }, + { type: "item", threadId, item: { id: "modern-outside", kind: "tool", name: "Read", title: "outside read", args: { file_path: "/outside/private-context" }, status: "done", outcome: "succeeded", ok: true, origin, at: at() } }, + { type: "item", threadId, item: { id: "modern-unlisted", kind: "tool", name: "Bash", title: "possible writer", args: { command: "printf changed > partial.txt" }, exec: { command: "printf changed > partial.txt" }, status: "done", outcome: "succeeded", ok: true, origin, at: at() } }, + { type: "item", threadId, item: { id: "modern-answer", kind: "assistant", text: "Fixture complete", phase: "final", outcome: "succeeded", origin: { ...origin, native: { runtime: "fixture", item: "answer" } }, at: at() } }, + { type: "status", threadId, status: "idle" }, + ]); + const line = tid(page, "task-verification").last(); + await expect(line).toHaveAttribute("data-state", "unknown"); + await expect(line).toContainText("confirmed relevance"); + await expect(line).toContainText("timed snapshots"); + await expect(tid(page, "task-context")).toHaveCount(priorLists); + await page.getByRole("button", { name: "Task context · 0", exact: true }).click(); + const context = page.getByRole("region", { name: "Task context", exact: true }); + await expect(context).toContainText("1 outside the project, not named"); + await expect(context).toContainText("printf changed > partial.txt"); + await expect(context).not.toContainText("/outside/private-context"); +}); diff --git a/apps/desktop/e2e/workspace-store.spec.ts b/apps/desktop/e2e/workspace-store.spec.ts index 9a32b610..f442836c 100644 --- a/apps/desktop/e2e/workspace-store.spec.ts +++ b/apps/desktop/e2e/workspace-store.spec.ts @@ -35,6 +35,8 @@ test("Store browser guests belong to the client and hide while its host is disco await page.getByLabel("Connection link", { exact: true }).fill(link); await page.getByRole("button", { name: "Connect to my Mac" }).click(); await expect(tid(page, "draft-view")).toBeVisible(); + await expect(page.getByRole("button", { name: "Context for next request", exact: true })).toHaveCount(0); + await expect(page.evaluate(() => window.modex!.invoke("task:context-draft-open", { owner: { kind: "project", projectId: "p1", key: "draft:0:p1" } }))).rejects.toThrow(/Unknown desktop command/); await createThread(page, "Browse in the Store client"); const tabs = page.locator('.workspace-tabs [role="tab"]'); const initialTabs = await tabs.count(); diff --git a/apps/desktop/src/main/commands.ts b/apps/desktop/src/main/commands.ts index 7b00f26b..0f3a24fe 100644 --- a/apps/desktop/src/main/commands.ts +++ b/apps/desktop/src/main/commands.ts @@ -97,12 +97,20 @@ export function registerCommands(handle: RegisterCommand, { await runner.removeProject(projectId); return store.snapshot(); }); - handle("thread:create", ({ projectId, worktree, mode, model, backend, auto }) => runner.createThread(projectId, { worktree, mode, model, backend, auto })); + handle("thread:create", ({ projectId, worktree, mode, model, backend, auto, contextDraft }) => runner.createThread(projectId, { worktree, mode, model, backend, auto, contextDraft })); handle("thread:items", ({ threadId }) => runner.items(threadId)); + handle("task:context-draft-open", ({ owner }) => runner.openContextDraft(owner)); + handle("task:context-draft-select", ({ binding, targets }) => runner.selectTaskContext(binding, targets)); + handle("task:context-draft-close", ({ binding }) => runner.closeContextDraft(binding)); + handle("task:context-candidates", ({ binding }) => runner.taskContextCandidates(binding)); + handle("task:context-draft", ({ binding }) => runner.draftTaskContext(binding)); + handle("task:context", ({ threadId, turnId }) => runner.taskContext(threadId, turnId)); + handle("task:context-confirm", ({ threadId, turnId, expectedRevision, refIds }) => runner.confirmTaskContext(threadId, turnId, expectedRevision, refIds)); + handle("task:check-confirm", ({ threadId, turnId, expectedRevision, itemId, refIds }) => runner.confirmTaskCheck(threadId, turnId, expectedRevision, itemId, refIds)); handle("task:save-note", ({ threadId, turnId }) => runner.saveTaskNote(threadId, turnId)); handle("task:note", ({ pageId }) => runner.taskNote(pageId)); handle("thread:followup", ({ threadId }) => runner.followUp(threadId)); - handle("thread:send", ({ threadId, text, attachments }) => startTurn(threadId, runner.send(threadId, text, attachments, "browser"))); + handle("thread:send", ({ threadId, text, attachments, contextDraft }) => startTurn(threadId, runner.send(threadId, text, attachments, "browser", contextDraft))); registerAttachmentCommands(handle, runner.attachments, { bytesOnly: true, reveal: revealAttachment ?? (() => { throw new Error("Attachment reveal is unavailable."); }) }); handle("thread:retry", ({ threadId }) => startTurn(threadId, runner.retry(threadId, "browser"))); handle("thread:stop", ({ threadId }) => runner.stop(threadId)); diff --git a/apps/desktop/src/main/engine/attachment-recovery-file.ts b/apps/desktop/src/main/engine/attachment-recovery-file.ts index 086f7f8d..c3cdf592 100644 --- a/apps/desktop/src/main/engine/attachment-recovery-file.ts +++ b/apps/desktop/src/main/engine/attachment-recovery-file.ts @@ -17,7 +17,8 @@ const freeze = (value: T): Immutable => { /** Same data-only and duplicate-member boundaries as UpdateRecovery, with an * explicit per-record bound for the separately stored pending intent/journal. */ -export function recoveryJSON(value: unknown, limit: number): string { +export function recoveryJSON(value: unknown, limit: number, maxNodes = 20000): string { + if (!Number.isSafeInteger(maxNodes) || maxNodes < 1 || maxNodes > 1024 * 1024) throw invalid(); let bytes = 0, nodes = 0; const ancestors = new Set(); const add = (n: number) => { bytes += n; if (bytes > limit) throw invalid(); }; @@ -34,7 +35,7 @@ export function recoveryJSON(value: unknown, limit: number): string { } }; const visit = (v: unknown, depth: number): unknown => { - if (++nodes > 20000 || depth > 12) throw invalid(); + if (++nodes > maxNodes || depth > 12) throw invalid(); if (v === null) { add(4); return null; } if (typeof v === "string") { text(v); return v; } if (typeof v === "boolean") { add(v ? 4 : 5); return v; } @@ -62,7 +63,7 @@ export function recoveryJSON(value: unknown, limit: number): string { return body; } -export function parseRecoveryJSON(body: string, limit: number): unknown { +export function parseRecoveryJSON(body: string, limit: number, maxNodes = 20000): unknown { if (Buffer.byteLength(body) > limit) throw invalid(); const stack: ({ keys: Set; expectingKey: boolean } | null)[] = []; for (let i = 0; i < body.length; i++) { @@ -84,7 +85,7 @@ export function parseRecoveryJSON(body: string, limit: number): unknown { else if (char === ",") { const object = stack.at(-1); if (object) object.expectingKey = true; } } const value: unknown = JSON.parse(body); - recoveryJSON(value, limit); + recoveryJSON(value, limit, maxNodes); return value; } @@ -105,8 +106,11 @@ export class AttachmentRecoveryFile { private busy = false; private reentered = false; private uncertain = false; - constructor(directory: string, filename: string, private readonly limit: number, private readonly validate: (value: unknown) => void) { - if (!/^[A-Za-z0-9_-]{1,128}\.json$/.test(filename) || !Number.isSafeInteger(limit) || limit < 1 || limit > 4 * 1024 * 1024) throw invalid(); + constructor(directory: string, filename: string, private readonly limit: number, private readonly validate: (value: unknown) => void, private readonly maxNodes = 20000) { + // Pending messages retain independently bounded composer and context data. + // Each caller still supplies its own smaller read/write limit. + if (!/^[A-Za-z0-9_-]{1,128}\.json$/.test(filename) || !Number.isSafeInteger(limit) || limit < 1 || limit > 8 * 1024 * 1024) throw invalid(); + if (!Number.isSafeInteger(maxNodes) || maxNodes < 1 || maxNodes > 1024 * 1024) throw invalid(); fs.mkdirSync(directory, { recursive: true, mode: 0o700 }); const stat = fs.lstatSync(directory); if (!stat.isDirectory() || stat.isSymbolicLink()) throw invalid(); @@ -147,7 +151,7 @@ export class AttachmentRecoveryFile { } private snapshot(observed: Observed | null): RecoveryFileSnapshot { let value: unknown = null; - if (observed) { value = parseRecoveryJSON(observed.body, this.limit); this.validate(value); if (this.reentered) throw invalid(); } + if (observed) { value = parseRecoveryJSON(observed.body, this.limit, this.maxNodes); this.validate(value); if (this.reentered) throw invalid(); } const snapshot = Object.freeze({ value: freeze(value as T | null), sha256: observed ? digest(observed.body) : null }) as RecoveryFileSnapshot; this.proofs.set(snapshot, observed); return snapshot; } @@ -201,7 +205,7 @@ export class AttachmentRecoveryFile { if (this.uncertain) throw new RecoveryFileWriteError(false, invalid()); if (process.platform === "win32") throw new RecoveryFileWriteError(true, invalid()); const before = this.expected(expected); - const body = recoveryJSON(input, this.limit), checked = parseRecoveryJSON(body, this.limit); + const body = recoveryJSON(input, this.limit, this.maxNodes), checked = parseRecoveryJSON(body, this.limit, this.maxNodes); this.validate(checked); if (this.reentered) throw invalid(); const temporary = `${this.file}.${randomUUID()}.tmp`; let fd: number | undefined, created = false; diff --git a/apps/desktop/src/main/engine/attachment-transfer-journal.ts b/apps/desktop/src/main/engine/attachment-transfer-journal.ts index a00fe9d8..bfe201dc 100644 --- a/apps/desktop/src/main/engine/attachment-transfer-journal.ts +++ b/apps/desktop/src/main/engine/attachment-transfer-journal.ts @@ -4,7 +4,7 @@ import { createHash, randomUUID } from "node:crypto"; import type { Attachment, AttachmentDraftOwner } from "../../shared/attachments.js"; import type { ComposerRecovery, UpdateRecoveryRecord } from "../../shared/update-recovery.js"; import type { AttachmentStore } from "./attachments.js"; -import { PENDING_RECORD_BYTES, type PendingSends, type PendingSnapshot, type PendingTarget } from "./pending-sends.js"; +import { pendingRecordJSON, type PendingSends, type PendingSnapshot, type PendingTarget } from "./pending-sends.js"; import { copyComposerRecovery, copyUpdateRecoveryRecord, type ComposerRecoveryWriter, type DraftTransactionReceipt, type UpdateRecovery } from "./update-recovery.js"; import { AttachmentRecoveryFile, parseRecoveryJSON, recoveryJSON, type RecoveryFileSnapshot } from "./attachment-recovery-file.js"; @@ -15,6 +15,7 @@ class UnstartedRefusal extends Error { constructor(cause: unknown) { super("Attachment transfer request was refused before starting.", { cause }); } } const digest = (value: unknown, limit = JOURNAL_BYTES) => createHash("sha256").update(recoveryJSON(value, limit)).digest("hex"); +const pendingDigest = (value: unknown) => createHash("sha256").update(pendingRecordJSON(value)).digest("hex"); const equal = (a: unknown, b: unknown) => recoveryJSON(a, JOURNAL_BYTES) === recoveryJSON(b, JOURNAL_BYTES); const hash = (v: unknown): v is string => typeof v === "string" && /^[a-f0-9]{64}$/.test(v); const id = (v: unknown): v is string => typeof v === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(v) && v !== "staging"; @@ -150,7 +151,7 @@ export class AttachmentTransferJournal { const journal: Journal = { schema: 1, id: randomUUID(), intentId, source: { ...source }, target: { ...target }, files: entries, recovery: { beforeSha: proof.sha256, beforeRevision: before.revision, afterSha: digest(after, RECORD_BYTES), after }, - intent: { beforeSha: intent.sha256!, afterSha: digest(preview.after, PENDING_RECORD_BYTES), files: claimedIntent } }; + intent: { beforeSha: intent.sha256!, afterSha: pendingDigest(preview.after), files: claimedIntent } }; // Fully validate and bound the journal before the first ownership write. validate(journal); recoveryJSON(journal, JOURNAL_BYTES); const sealed = this.file.write(empty, journal); @@ -205,7 +206,7 @@ export class AttachmentTransferJournal { let nextIntent: unknown; if (ib) { const preview = pending.adoption(j.intentId, j.intent.files); - if (preview.before.sha256 !== i.sha256 || digest(preview.after, PENDING_RECORD_BYTES) !== j.intent.afterSha) throw fail(); + if (preview.before.sha256 !== i.sha256 || pendingDigest(preview.after) !== j.intent.afterSha) throw fail(); nextIntent = preview.after; } if (transitions && beforeCount === transitions) { diff --git a/apps/desktop/src/main/engine/backends/account-claude.ts b/apps/desktop/src/main/engine/backends/account-claude.ts index 45777fa8..6f5ad16c 100644 --- a/apps/desktop/src/main/engine/backends/account-claude.ts +++ b/apps/desktop/src/main/engine/backends/account-claude.ts @@ -37,6 +37,7 @@ export class AccountClaudeBackend extends ClaudeBackend { catch (error) { return { status: "failed", error: (error as Error).message }; } } if (!secret) return super.runTurn(text, opts, sink, signal); + sink.protectCredential?.(secret); const protectedSink = claudeSecretSink(sink, secret); try { return protectedSink.redact(await super.runTurn(text, opts, protectedSink.sink, signal)); } catch (error) { throw new Error(protectedSink.redact(error instanceof Error ? error.message : String(error))); } diff --git a/apps/desktop/src/main/engine/backends/account-codex.ts b/apps/desktop/src/main/engine/backends/account-codex.ts index 835b4cfd..aae075fe 100644 --- a/apps/desktop/src/main/engine/backends/account-codex.ts +++ b/apps/desktop/src/main/engine/backends/account-codex.ts @@ -50,7 +50,7 @@ export class AccountCodexBackend implements Backend { if (this.busy(id)) throw new Error("Wait for this account's active or initializing turns to finish before signing out."); const runtime = this.runtimes.get(id); this.runtimes.delete(id); await runtime?.backend?.dispose(); } - private async use(id: string, action: (backend: CodexBackend) => Promise): Promise { + private async use(id: string, action: (backend: CodexBackend, credential?: string) => Promise): Promise { if (this.disposed) throw new Error("Modex is shutting down."); if (this.changing.has(id)) throw new Error("Wait for this account's sign-in or sign-out to finish."); if (id === "cli") return action(this.cli); @@ -75,14 +75,18 @@ export class AccountCodexBackend implements Backend { })(); } const backend = runtime.preparing ? await runtime.preparing : runtime.backend!; - return await action(backend); + return await action(backend, runtime.token); } finally { runtime.leases--; if (runtime.leases === 0) runtime.preparing = null; } } runTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { const id = opts.account ?? this.identity(); // Legacy resume handles are CLI-owned; explicitly bound handles stay with their original account. if (opts.resume && !opts.account && id !== "cli") return Promise.resolve({ status: "failed", error: "This conversation belongs to Codex CLI authentication. Select CLI authentication to resume it." }); - return this.use(id, (backend) => signal.aborted ? Promise.resolve({ status: "interrupted" as const }) : backend.runTurn(text, opts, sink, signal)); + return this.use(id, (backend, credential) => { + if (signal.aborted) return Promise.resolve({ status: "interrupted" as const }); + if (credential) sink.protectCredential?.(credential); + return backend.runTurn(text, opts, sink, signal); + }); } generateTitle(text: string, opts: TitleOptions, signal: AbortSignal): Promise { const id = opts.account ?? this.identity(); diff --git a/apps/desktop/src/main/engine/backends/acp.ts b/apps/desktop/src/main/engine/backends/acp.ts index 4bac66f1..d554b05a 100644 --- a/apps/desktop/src/main/engine/backends/acp.ts +++ b/apps/desktop/src/main/engine/backends/acp.ts @@ -4,7 +4,7 @@ import { randomUUID } from "node:crypto"; import type { CliExecutable } from "../cli-path.js"; import { generateTitle } from "../titles.js"; import type { Backend, ModelInfo, NativeRequest, TitleOptions, TurnOptions, TurnResult, TurnSink } from "./types.js"; -import { LineBuffer, stderrTail, writeJsonResponse } from "./types.js"; +import { LineBuffer, stderrTail, writeJsonResponse, withTaskContextCapability } from "./types.js"; import { UsageMeter, withUsage } from "./usage.js"; import { cliEnvironment, cliSpawn, health, installation, probe } from "./health.js"; import type { ApprovalAnswer, BackendHealth, Mode } from "../../../shared/types.js"; @@ -241,6 +241,9 @@ export class AcpBackend implements Backend { } async runTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { + return withTaskContextCapability({ state: "unavailable", reason: "unsupported-adapter" }, sink, reported => this.runAcpTurn(text, opts, reported, signal)); + } + private async runAcpTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { if (signal.aborted) return { status: "interrupted" }; // CR-6 review snapshots need a read-only, hook-free, prompt-free mode. The ACP server has no such mode: refuse rather than run loosely. if (opts.confinement) return { status: "failed", error: `${this.family.label} has no confined review mode; Code Review runs on Claude.`, detail: { stage: "confinement" } }; diff --git a/apps/desktop/src/main/engine/backends/claude-secret-sink.ts b/apps/desktop/src/main/engine/backends/claude-secret-sink.ts index 1b3b7c6a..9af3dd7b 100644 --- a/apps/desktop/src/main/engine/backends/claude-secret-sink.ts +++ b/apps/desktop/src/main/engine/backends/claude-secret-sink.ts @@ -1,4 +1,4 @@ -import type { Observation } from "../../../shared/conversation-events.js"; +import type { Observation, UnavailableNativeInput } from "../../../shared/conversation-events.js"; import type { TurnSink } from "./types.js"; type Delta = Extract; @@ -15,8 +15,8 @@ export function claudeSecretSink(sink: TurnSink, secret: string): { sink: TurnSi const tail = pending.get(key); if (!tail) return; pending.delete(key); - // The stream is closed: a proper prefix cannot complete a key, so preserve ordinary text. - sink.observe(redact({ ...tail.event, text: tail.text, ...(tail.event.source ? { source: `${tail.event.source}:redaction-tail` } : {}) })); + // Preserve ordinary short words; a long unfinished credential prefix is still private. + sink.observe(redact({ ...tail.event, text: tail.text.length >= 9 ? "[redacted]" : tail.text, ...(tail.event.source ? { source: `${tail.event.source}:redaction-tail` } : {}) })); }; return { redact, @@ -24,6 +24,19 @@ export function claudeSecretSink(sink: TurnSink, secret: string): { sink: TurnSi sink: { ...sink, observe(event) { + // A native identity is exact or unavailable; masking must never create another identity. + if (contains(event.native) || (event.source && event.source.includes(secret))) return; + // Redaction changes display text, not the identity of the file the native tool used. + // Preserve static unavailable-field receipts before discarding the original value. + const args = event.type === "tool.start" ? event.tool.args : event.type === "tool.update" || event.type === "tool.done" ? event.progress?.args : undefined; + if (args) { + const unavailable: UnavailableNativeInput[] = []; + for (const field of ["file_path", "notebook_path", "path"] as const) if (typeof args[field] === "string" && args[field].includes(secret)) unavailable.push(`args.${field}`); + if (contains(args)) { + if (event.type === "tool.start") event = { ...event, tool: { ...event.tool, displayOnlyInputs: true, ...(unavailable.length ? { unavailableInputs: [...new Set([...(event.tool.unavailableInputs ?? []), ...unavailable])] } : {}) } }; + else if (event.type === "tool.update" || event.type === "tool.done") event = { ...event, progress: { ...event.progress, displayOnlyInputs: true, ...(unavailable.length ? { unavailableInputs: [...new Set([...(event.progress?.unavailableInputs ?? []), ...unavailable])] } : {}) } }; + } + } if (event.type === "message.delta" || event.type === "reasoning.delta") { const key = `${nativeKey(event)}:${event.type}:${event.type === "reasoning.delta" ? `${event.representation}:${event.index}` : ""}`; const text = redactText((pending.get(key)?.text ?? "") + event.text); @@ -51,6 +64,12 @@ export function claudeSecretSink(sink: TurnSink, secret: string): { sink: TurnSi sink.notice("warn", "Claude requested an action containing the gateway credential. Modex denied it without displaying the credential."); return "no"; }, + ...(sink.question ? { question: async (request, withdrawn) => { + if (!contains(request)) return sink.question!(request, withdrawn); + if (request.native) denied.add(JSON.stringify(request.native)); + sink.notice("warn", "Claude asked a question containing the gateway credential. Modex withdrew it without displaying the credential."); + return { withdrawn: "ended" as const }; + } } : {}), refusedByRule(request) { return contains(request) ? "The action contains a protected gateway credential." : sink.refusedByRule?.(request); }, async deliverRequest(native, write) { // A local secret refusal has no human request in the broker; only its fixed denial is written. diff --git a/apps/desktop/src/main/engine/backends/claude.ts b/apps/desktop/src/main/engine/backends/claude.ts index 4f116e0b..a36ba2d4 100644 --- a/apps/desktop/src/main/engine/backends/claude.ts +++ b/apps/desktop/src/main/engine/backends/claude.ts @@ -8,9 +8,10 @@ import { randomUUID } from "node:crypto"; import fs from "node:fs"; import { StringDecoder } from "node:string_decoder"; import { PAGES_TOOLS } from "../../../shared/pages.js"; +import { TASK_CONTEXT_TOOLS } from "../../../shared/task-context-tools.js"; import { KNOWLEDGE_TOOLS } from "../../../shared/knowledge.js"; import type { ApprovalRequest, Backend, ModelInfo, StopReport, TitleOptions, TurnAttachment, TurnOptions, TurnResult, TurnSink } from "./types.js"; -import { LineBuffer, shortJson, stderrTail, writeJsonResponse } from "./types.js"; +import { LineBuffer, shortJson, stderrTail, writeJsonResponse, withTaskContextCapability } from "./types.js"; import { claudeCall, UsageMeter, withUsage } from "./usage.js"; import { cliEnvironment, cliSpawn, health, installation, probe } from "./health.js"; import type { ApprovalAnswer, BackendHealth } from "../../../shared/types.js"; @@ -141,13 +142,14 @@ export class ClaudeBackend implements Backend { if (opts.fast || opts.conserveCredits) args.push("--settings", JSON.stringify({ fastMode: opts.conserveCredits ? false : true })); if (opts.resume) args.push("--resume", opts.resume); for (const d of opts.addDirs ?? []) args.push("--add-dir", d); - if (opts.knowledge || opts.pages || opts.images) args.push("--mcp-config", JSON.stringify({ mcpServers: { + if (opts.knowledge || opts.pages || opts.images || opts.taskContext) args.push("--mcp-config", JSON.stringify({ mcpServers: { ...(opts.images ? { modex_images: { type: "http", url: opts.images.url } } : {}), ...(opts.knowledge ? { modex_knowledge: { type: "http", url: opts.knowledge.url } } : {}), ...(opts.pages ? { modex_pages: { type: "http", url: opts.pages.url } } : {}), - } }), "--append-system-prompt", [opts.knowledge?.instructions, opts.pages?.instructions, opts.images?.instructions].filter(Boolean).join("\n\n"), + ...(opts.taskContext ? { modex_task_context: { type: "http", url: opts.taskContext.url } } : {}), + } }), "--append-system-prompt", [opts.knowledge?.instructions, opts.pages?.instructions, opts.taskContext?.instructions, opts.images?.instructions].filter(Boolean).join("\n\n"), "--allowedTools", ...(opts.images ? ["mcp__modex_images__register"] : []), ...(opts.knowledge ? KNOWLEDGE_TOOLS.map(name => `mcp__modex_knowledge__${name}`) : []), - ...(opts.pages ? PAGES_TOOLS.map(name => `mcp__modex_pages__${name}`) : [])); + ...(opts.pages ? PAGES_TOOLS.map(name => `mcp__modex_pages__${name}`) : []), ...(opts.taskContext ? TASK_CONTEXT_TOOLS.map(name => `mcp__modex_task_context__${name}`) : [])); return args; } @@ -156,6 +158,9 @@ export class ClaudeBackend implements Backend { } runTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { + return withTaskContextCapability({ state: "unavailable", reason: "configuration-not-bound" }, sink, reported => this.runClaudeTurn(text, opts, reported, signal)); + } + private runClaudeTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { if (signal.aborted) return Promise.resolve({ status: "interrupted" }); return new Promise((resolve) => { let child: ChildProcess; @@ -399,7 +404,12 @@ export class ClaudeBackend implements Backend { // The thread's own calls only: sub-agents run on their own context. One call can span several messages with one id. if (msg.type === "assistant" && !msg.parent_tool_use_id && msg.message?.usage) meter.call(claudeCall(msg.message.usage), msg.message.id); if ((msg.type === "result" || (msg.type === "system" && msg.subtype === "init")) && msg.fast_mode_state !== undefined) fast = { state: msg.fast_mode_state, reason: msg.fast_mode_disabled_reason }; - if (msg.type === "system" && msg.subtype === "init") sawAssistant = false; + if (msg.type === "system" && msg.subtype === "init") { + sawAssistant = false; + const connected = opts.taskContext && !opts.confinement && Array.isArray(msg.mcp_servers) + && msg.mcp_servers.some(server => server && server.name === "modex_task_context" && server.status === "connected"); + sink.taskContextCapability?.(connected ? { state: "available" } : { state: "unavailable", reason: "configuration-not-bound" }); + } if (msg.type === "assistant" && !msg.parent_tool_use_id) sawAssistant = true; if (msg.type === "result") return onResult(msg); // An API refusal arrives as a synthetic assistant reply, then again as the failed result. The @@ -700,6 +710,7 @@ interface ClaudeUsage { output_tokens?: number; } interface ClaudeMessage { + mcp_servers?: { name?: string; status?: string }[]; type: string; subtype?: string; parent_tool_use_id?: string | null; diff --git a/apps/desktop/src/main/engine/backends/codex.ts b/apps/desktop/src/main/engine/backends/codex.ts index 348cfcbd..8027937e 100644 --- a/apps/desktop/src/main/engine/backends/codex.ts +++ b/apps/desktop/src/main/engine/backends/codex.ts @@ -2,17 +2,18 @@ import { codexQuota, type QuotaSnapshot } from "../routing/quota.js"; import { requestedPermissions } from "./codex-permissions.js"; import { AgentTracker } from "./agents.js"; import { PAGES_TOOLS } from "../../../shared/pages.js"; +import { TASK_CONTEXT_TOOLS } from "../../../shared/task-context-tools.js"; import { KNOWLEDGE_TOOLS } from "../../../shared/knowledge.js"; import type { CliExecutable } from "../cli-path.js"; import { generateTitle } from "../titles.js"; import type { ChildProcess } from "node:child_process"; import { randomUUID } from "node:crypto"; import type { Backend, ModelInfo, NativeRequest, TitleOptions, TurnOptions, TurnResult, TurnSink } from "./types.js"; -import { LineBuffer, stderrTail, writeJsonResponse } from "./types.js"; +import { LineBuffer, stderrTail, writeJsonResponse, withTaskContextCapability } from "./types.js"; import { codexCall, UsageMeter, withUsage } from "./usage.js"; import { cliEnvironment, cliSpawn, health, installation } from "./health.js"; import type { AgentActivity, BackendHealth } from "../../../shared/types.js"; -import type { MessageAsk, MessagePhase, NativeRef, PlanStepStatus, SettledOutcome } from "../../../shared/conversation-events.js"; +import type { MessageAsk, MessagePhase, NativeRef, PlanStepStatus, SettledOutcome, UnavailableNativeInput } from "../../../shared/conversation-events.js"; import { boundOutput, byteLength, codexWebSearch, memoryEvidence, type ExecAction } from "../../../shared/evidence.js"; import { actionDigest, codexAnswerResponse, codexQuestions } from "../../../shared/human-requests.js"; @@ -40,10 +41,11 @@ export class CodexBackend implements Backend { private readonly subscribers = new Set<(msg: RpcMessage) => void>(); private readonly disconnects = new Set<(error: Error) => void>(); private readonly loaded = new Set(); + private readonly metadataConnections = new Map(); /** The model Codex chose for each loaded thread, from thread/start or thread/resume: a Plan turn's collaboration mode needs one. */ private readonly threadModels = new Map(); /** File paths of in-flight file changes, keyed `threadId:itemId`, so an approval can name them without the patch. */ - private readonly changePaths = new Map(); + private readonly changePaths = new Map(); /** From the `initialize` reply: which codex build the server is. Kept for failure reports. */ private userAgent: string | null = null; /** Opaque id of the running app-server process; native thread, turn and item ids are unique only within it. */ @@ -88,6 +90,7 @@ export class CodexBackend implements Backend { this.child = child; this.generation = randomUUID(); this.loaded.clear(); + this.metadataConnections.clear(); this.threadModels.clear(); const lines = new LineBuffer(); let stderr = ""; @@ -121,6 +124,8 @@ export class CodexBackend implements Backend { this.child = null; this.ready = null; this.loaded.clear(); + this.metadataConnections.clear(); + this.changePaths.clear(); this.threadModels.clear(); for (const request of this.openRequests.values()) request.controller.abort(); this.openRequests.clear(); @@ -132,22 +137,83 @@ export class CodexBackend implements Backend { private dispatch(line: string): void { let msg: RpcMessage; try { - msg = JSON.parse(line, (_key, value: unknown) => typeof value === "string" ? this.configuration?.redact(value) ?? value : value) as RpcMessage; + const fields = this.redactedInputs, protectedValues = this.protectedValues, rawIds = this.protectedTransportIds, redact = this.configuration?.redact; + msg = JSON.parse(line, function(this: object, key, value: unknown) { + if (redact && redact(key) !== key) protectedValues.add(this); + if (typeof value === "string") { + const safe = redact?.(value) ?? value; + if (safe !== value) { + const changed = fields.get(this) ?? new Set(); changed.add(key); fields.set(this, changed); protectedValues.add(this); + // Exact protected RPC ids are retained only to send a fixed refusal back to their owner. + if (key === "id") rawIds.set(this, value); + } + return safe; + } + if (value && typeof value === "object") { + const object = value as Record; + if (!Array.isArray(value) && redact && Object.keys(object).some(name => redact(name) !== name)) { + const safe = Object.fromEntries(Object.entries(object).map(([name, item]) => [redact(name), item])); + if (fields.has(object)) fields.set(safe, fields.get(object)!); + if (rawIds.has(object)) rawIds.set(safe, rawIds.get(object)!); + protectedValues.add(safe); value = safe; + } + if (protectedValues.has(value as object)) protectedValues.add(this); + } + return value; + }) as RpcMessage; } catch { return; } + if (msg.method && msg.id !== undefined) { + const p = msg.params as Record | undefined; + const patch = msg.method === "item/fileChange/requestApproval" ? this.changePaths.get(`${String(p?.threadId ?? "")}:${String(p?.itemId ?? "")}`) : undefined; + if (this.protectedValues.has(msg) || patch?.protected) { + this.send({ id: this.protectedTransportIds.get(msg) ?? msg.id, error: { code: -32602, message: "Modex refused this request because it contains protected credential data." } }); + return; + } + } if (typeof msg.id === "number" && !msg.method) { const p = this.pending.get(msg.id); if (p) { this.pending.delete(msg.id); if (msg.error) p.reject(new CodexError(msg.error.message ?? "codex error", { method: p.method, rpcCode: msg.error.code, rpcData: msg.error.data })); + else if (this.unavailableIdentity(msg.result, "thread") || this.unavailableIdentity(msg.result, "turn")) p.reject(new CodexError("Codex returned an unavailable protected protocol identity.", { method: p.method })); else p.resolve(msg.result); } return; } + const params = msg.params as Record | undefined; + if (this.redactedInputs.get(msg)?.has("method") || (params && (["threadId", "turnId", "itemId"].some(key => this.redactedInputs.get(params)?.has(key)) + || ["thread", "turn", "item"].some(key => this.unavailableIdentity(params, key))))) return; for (const s of this.subscribers) s(msg); } + /** Object-local field names only: raw credentials and values are never retained here. */ + private readonly redactedInputs = new WeakMap>(); + private readonly protectedValues = new WeakSet(); + private readonly protectedTransportIds = new WeakMap(); + private unavailableIdentity(value: unknown, key: string): boolean { + if (!value || typeof value !== "object") return false; + const identity = (value as Record)[key]; + return !!identity && typeof identity === "object" && this.redactedInputs.get(identity)?.has("id") === true; + } + private inputAvailability(item: CodexItem): { displayOnlyInputs?: true; unavailableInputs?: UnavailableNativeInput[] } { + const fields: UnavailableNativeInput[] = [], changed = (object: object | undefined, key: string) => object !== undefined && this.redactedInputs.get(object)?.has(key); + if (changed(item, "command")) fields.push("exec.command"); + if (changed(item, "cwd")) fields.push("exec.cwd"); + let index = 0; + for (const action of Array.isArray(item.commandActions) ? item.commandActions : []) { + if (!codexActions([action])) continue; + if (changed(action, "path")) fields.push(`exec.actions.${index}.path`); + index++; + } + for (const [i, file] of (item.changes ?? []).entries()) { + if (changed(file, "path")) fields.push(`args.files.${i}.path`); + if (changed(file.kind, "move_path")) fields.push(`args.files.${i}.movePath`); + } + return fields.length ? { displayOnlyInputs: true, unavailableInputs: fields } : {}; + } + private send(o: unknown): void { this.child?.stdin?.write(JSON.stringify(o) + "\n"); } @@ -282,6 +348,9 @@ export class CodexBackend implements Backend { * is run again on a server that has read the current sign-in. Other failures return as they are. */ async runTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { + return withTaskContextCapability({ state: "unavailable", reason: this.configuration?.local ? "local-execution-unqualified" : "configuration-not-bound" }, sink, reported => this.runCodingTurn(text, opts, reported, signal)); + } + private async runCodingTurn(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { const local = this.configuration?.local; if (local && opts.model !== local.model) return { status: "failed", error: "This local Codex runs only its selected model. Select the local target again to start a fresh conversation." }; if (local && opts.resume && !this.loaded.has(opts.resume)) return { status: "failed", error: "This conversation's native session belongs to another Codex process. Select the local target again to start a fresh conversation." }; @@ -300,6 +369,7 @@ export class CodexBackend implements Backend { if (!shared) this.restart("Codex was restarted to pick up a new sign-in."); const opened = first.detail?.codexThreadId; const resume = typeof opened === "string" ? opened : opts.resume; + sink.taskContextCapability?.({ state: "unavailable", reason: "configuration-not-bound" }); const second = await this.attempt(text, { ...opts, resume }, sink, signal); if (second.status !== "failed") return second; return { ...second, detail: { ...second.detail, firstAttempt: { error: first.error, ...first.detail } }, recovery: [`${step}: failed again`] }; @@ -322,7 +392,10 @@ export class CodexBackend implements Backend { // but avoid a second sandbox_apply inside the parent's macOS sandbox. if (this.configuration?.local?.externalSandbox) pol.sandboxPolicy = { type: "externalSandbox", networkAccess: "restricted" }; // Only these scoped adapter tools are preapproved. Main enforces project/turn permissions. - const config = { ...(this.configuration?.local ? {} : THREAD_CONFIG), ...(opts.images ? { "mcp_servers.modex_images": { url: opts.images.url, enabled_tools: ["register"], tools: { register: { approval_mode: "approve" } } } } : {}), ...(opts.knowledge ? { "mcp_servers.modex_knowledge": { + const taskContext = this.configuration?.local ? undefined : opts.taskContext; + const config: Record = { ...(this.configuration?.local ? {} : THREAD_CONFIG), ...(taskContext ? { "mcp_servers.modex_task_context": { + url: taskContext.url, enabled_tools: [...TASK_CONTEXT_TOOLS], tools: Object.fromEntries(TASK_CONTEXT_TOOLS.map(name => [name, { approval_mode: "approve" }])), + } } : {}), ...(opts.images ? { "mcp_servers.modex_images": { url: opts.images.url, enabled_tools: ["register"], tools: { register: { approval_mode: "approve" } } } } : {}), ...(opts.knowledge ? { "mcp_servers.modex_knowledge": { url: opts.knowledge.url, enabled_tools: [...KNOWLEDGE_TOOLS], tools: Object.fromEntries(KNOWLEDGE_TOOLS.map(name => [name, { approval_mode: "approve" }])), } } : {}), ...(opts.pages ? { "mcp_servers.modex_pages": { @@ -330,6 +403,10 @@ export class CodexBackend implements Backend { tools: Object.fromEntries(PAGES_TOOLS.map(name => [name, { approval_mode: "approve" }])), } } : {}) }; let threadId = opts.resume; + const metadata = { pages: opts.pages?.url, taskContext: taskContext?.url }, priorMetadata = threadId ? this.metadataConnections.get(threadId) : undefined; + const rebindMetadata = Boolean(threadId && this.loaded.has(threadId) && JSON.stringify(priorMetadata ?? {}) !== JSON.stringify(metadata)); + if (priorMetadata?.pages && !opts.pages) config["mcp_servers.modex_pages"] = { enabled: false }; + if (priorMetadata?.taskContext && !taskContext) config["mcp_servers.modex_task_context"] = { enabled: false }; const local = this.configuration?.local; // The child's effective configuration, as Codex itself resolved it across every layer, is checked before any // thread is opened: confirming the provider's name on open does not say where its requests go (#380). @@ -355,9 +432,10 @@ export class CodexBackend implements Backend { if (local && (r.model !== local.model || r.modelProvider !== local.provider)) throw new CodexError("Codex did not confirm the selected local model and provider.", { confirmedModel: r.model, confirmedProvider: r.modelProvider }); }; try { - if (threadId && !this.loaded.has(threadId)) { + if (threadId && (!this.loaded.has(threadId) || rebindMetadata)) { const r = await duringSetup(this.request<{ thread: { id: string }; model?: unknown; modelProvider?: unknown }>("thread/resume", { threadId, cwd: opts.cwd, approvalPolicy: pol.approvalPolicy, sandbox: pol.sandbox, model: opts.model || null, ...pinned, config }), signal); confirmed(r); + if (rebindMetadata && r.thread.id !== threadId) throw new CodexError("Codex changed conversation identity while rebinding metadata connections.", { stage: "bind metadata connections" }); threadId = r.thread.id; if (typeof r.model === "string" && r.model) this.threadModels.set(threadId, r.model); } else if (!threadId) { @@ -367,14 +445,17 @@ export class CodexBackend implements Backend { if (typeof r.model === "string" && r.model) this.threadModels.set(threadId, r.model); } } catch (err) { - return failed(err, { stage: "open thread", codexThreadId: opts.resume }); + return { ...failed(err, { stage: rebindMetadata ? "bind metadata connections" : "open thread", codexThreadId: opts.resume }), + ...((taskContext || priorMetadata?.taskContext) ? { taskContextCapability: { state: "unavailable" as const, reason: "configuration-not-bound" as const } } : {}) }; } if (signal.aborted) return { status: "interrupted" }; this.loaded.add(threadId); + this.metadataConnections.set(threadId, metadata); + if (taskContext) sink.taskContextCapability?.({ state: "available" }); sink.session(threadId); const tid = threadId; - const context = [opts.knowledge?.instructions, opts.pages?.instructions, opts.images?.instructions, text].filter(value => value !== undefined).join("\n\n"); + const context = [opts.knowledge?.instructions, opts.pages?.instructions, taskContext?.instructions, opts.images?.instructions, text].filter(value => value !== undefined).join("\n\n"); // Plan mode runs in Codex's native plan collaboration mode (codex-cli 0.163.0, experimental): Codex's own plan // instructions, its question tool offered by default, and the plan as a `plan` item. The mode overrides the // turn's model and effort, so it carries Modex's. Without a model to name, or when Codex refuses the mode, @@ -432,6 +513,7 @@ export class CodexBackend implements Backend { const finish = (r: TurnResult) => { if (done) return; done = true; + for (const key of this.changePaths.keys()) if (key.startsWith(`${tid}:`)) this.changePaths.delete(key); agents.finish(signal.aborted || r.status === "interrupted"); this.subscribers.delete(onMsg); this.disconnects.delete(onDisconnect); @@ -550,13 +632,13 @@ export class CodexBackend implements Backend { } else if (item.type === "commandExecution") { tools.set(item.id, { started: Date.now(), output: "", bytes: 0 }); commands.add(item.id); - sink.observe({ type: "tool.start", native: native(item.id), tool: { name: "shell", title: `$ ${stripShell(item.command ?? "")}`, args: { command: item.command, cwd: item.cwd }, + sink.observe({ type: "tool.start", native: native(item.id), tool: { name: "shell", title: `$ ${stripShell(item.command ?? "")}`, args: { command: item.command, cwd: item.cwd }, ...this.inputAvailability(item), ...(item.command ? { exec: { command: item.command, ...(item.cwd ? { cwd: item.cwd } : {}), ...(codexActions(item.commandActions) ? { actions: codexActions(item.commandActions)! } : {}) } } : {}) } }); } else if (item.type === "fileChange") { tools.set(item.id, { started: Date.now(), output: "", bytes: 0 }); const files = (item.changes ?? []).map((c) => c.path); - this.changePaths.set(`${tid}:${item.id}`, files); - sink.observe({ type: "tool.start", native: native(item.id), tool: { name: "apply_patch", title: `edit ${files.join(", ")}`, args: { patch: (item.changes ?? []).map((c) => c.diff).join("\n"), files: (item.changes ?? []).map(patchFile) } } }); + this.changePaths.set(`${tid}:${item.id}`, { paths: files, protected: this.protectedValues.has(item) }); + sink.observe({ type: "tool.start", native: native(item.id), tool: { name: "apply_patch", title: `edit ${files.join(", ")}`, args: { patch: (item.changes ?? []).map((c) => c.diff).join("\n"), files: (item.changes ?? []).map(patchFile) }, ...this.inputAvailability(item) } }); } else if (item.type === "mcpToolCall" || item.type === "dynamicToolCall" || item.type === "webSearch") { tools.set(item.id, { started: Date.now(), output: "", bytes: 0 }); // 0.163.0 starts a web search with an empty query; the completed item carries it. @@ -598,7 +680,12 @@ export class CodexBackend implements Backend { case "item/completed": { const item = p.item as CodexItem; commands.delete(item.id); - if (item.type === "fileChange") this.changePaths.delete(`${tid}:${item.id}`); + if (item.type === "fileChange") { + const key = `${tid}:${item.id}`; + // Keep a body-free refusal fence until this turn ends, including late approval requests. + if (this.changePaths.get(key)?.protected || this.protectedValues.has(item)) this.changePaths.set(key, { paths: [], protected: true }); + else this.changePaths.delete(key); + } if (item.type === "agentMessage") { // A message that completes without streaming is kept, commentary included: its phase says what it is. const evidence = memoryEvidence(item.memoryCitation); @@ -615,6 +702,8 @@ export class CodexBackend implements Backend { const total = Math.max(t.bytes, byteLength(output)); const shown = boundOutput(output).text; sink.observe({ type: "tool.done", native: native(item.id), outcome: codexOutcome(item), progress: { + ...this.inputAvailability(item), + ...(item.type === "fileChange" ? { args: { patch: (item.changes ?? []).map(c => c.diff).join("\n"), files: (item.changes ?? []).map(patchFile) } } : {}), ...(search ? { search, ...(search.query ? { title: `search ${search.query}` } : {}) } : {}), ...(shown ? { output: shown } : {}), ...(total > byteLength(shown) ? { outputBytes: total, outputTruncated: true } : {}), ...(item.type === "commandExecution" && "exitCode" in item ? { exitCode: item.exitCode ?? null } : {}), @@ -734,7 +823,7 @@ export class CodexBackend implements Backend { /** Paths of a file change the approval refers to (from its item/started), consumed once. */ private takeChangePaths(p: Record): string[] { const key = `${String(p.threadId ?? "")}:${String(p.itemId ?? "")}`; - const paths = this.changePaths.get(key) ?? []; + const paths = this.changePaths.get(key)?.paths ?? []; this.changePaths.delete(key); return paths; } diff --git a/apps/desktop/src/main/engine/backends/types.ts b/apps/desktop/src/main/engine/backends/types.ts index 758bdc35..aafd8475 100644 --- a/apps/desktop/src/main/engine/backends/types.ts +++ b/apps/desktop/src/main/engine/backends/types.ts @@ -2,9 +2,20 @@ import type { Writable } from "node:stream"; import { StringDecoder } from "node:string_decoder"; import type { AgentActivity, ApprovalAnswer, BackendHealth, BackendId, Mode, ModelInfo } from "../../../shared/types.js"; import type { NativeRef, Observation } from "../../../shared/conversation-events.js"; +import type { TaskContextCapability } from "../../../shared/task-context-tools.js"; export type { ModelInfo }; +/** Carry setup reports through early failures without treating a task result as a capability check. */ +export async function withTaskContextCapability(initial: TaskContextCapability, sink: TurnSink, run: (sink: TurnSink) => Promise): Promise { + let latest = structuredClone(initial); + const report = (value: TaskContextCapability) => { latest = structuredClone(value); sink.taskContextCapability?.(structuredClone(value)); }; + report(initial); + const result = await run({ ...sink, taskContextCapability: report }); + if (result.taskContextCapability) report(result.taskContextCapability); + return { ...result, taskContextCapability: latest }; +} + export interface ToolStart { id: string; /** Backend tool name, e.g. "Bash", "commandExecution", "Edit". */ @@ -70,6 +81,10 @@ export type QuestionReply = { answers: Record } | { withdrawn: /** Callbacks a backend uses to report one turn to the ThreadRunner. */ export interface TurnSink { + /** Trusted adapter, main memory only: protect an already-loaded credential before native execution. */ + protectCredential?(value: string): void; + /** Main-only adapter report. It grants no execution or Notes authority and contains no route/token. */ + taskContextCapability?(capability: import("../../../shared/task-context-tools.js").TaskContextCapability): void; /** * Assistant text, reasoning and tool work, each named by its native identity: the one path for * all three (shared/conversation-events.ts, docs/conversation-events.md). The runner stamps it @@ -115,6 +130,7 @@ export interface TurnAttachment { } export interface TurnOptions { + taskContext?: import("../../../shared/task-context-tools.js").TaskContextConnection; /** Images travel inline (base64 or local path); the prompt text already names the other files by path. */ attachments?: TurnAttachment[]; images?: import("../../../shared/images.js").ImagesConnection; @@ -162,6 +178,7 @@ export interface TurnUsage { } export interface TurnResult { + taskContextCapability?: import("../../../shared/task-context-tools.js").TaskContextCapability; status: "completed" | "interrupted" | "failed"; error?: string; /** Backend facts behind a failure (exit code, stderr tail, RPC method and code, CLI build, ids) for the debug report. */ diff --git a/apps/desktop/src/main/engine/context-privacy.ts b/apps/desktop/src/main/engine/context-privacy.ts new file mode 100644 index 00000000..158b22a4 --- /dev/null +++ b/apps/desktop/src/main/engine/context-privacy.ts @@ -0,0 +1,130 @@ +import type { Observation } from "../../shared/conversation-events.js"; +import type { Exec } from "../../shared/evidence.js"; +import type { TurnSink } from "./backends/types.js"; + +export interface NativeContextInput { args?: Record; exec?: Exec } +const MASK = "[private connection]"; +// A bare eight-character hex prefix is an ordinary identifier/digest fragment, not a UUID-shaped secret. +const PRIVATE_PREFIX = 9; +type Delta = Extract; +interface Pending { text: string; emit(text: string): void } + +/** Literal main-owned connections and already-loaded credentials. Bounded history, never persisted. */ +export class ContextPrivacy { + private readonly pending = new Map(); + private readonly originals = new WeakMap(); + private readonly seen = new Set(); + private values: string[]; + private bytes: number; + private closed = false; + constructor(private readonly known: Set) { + this.values = [...known].sort((a, b) => b.length - a.length); + this.bytes = this.values.reduce((sum, value) => sum + Buffer.byteLength(value) + 64, 0); + } + addConnection(url: string): void { + const values = [url]; + try { const token = new URL(url).pathname.split("/").at(-1); if (token && /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(token)) values.push(token); } catch { /* An exact opaque fixture URL is still protected. */ } + this.add(values); + } + addCredential(value: string): void { + if (typeof value !== "string" || !value) throw new Error("Invalid protected credential registration."); + this.add([value]); + } + private add(values: string[]): void { + if (this.closed) return; + const fresh = values.filter(value => value && !this.known.has(value)); + const bytes = fresh.reduce((sum, value) => sum + Buffer.byteLength(value) + 64, 0); + if (this.bytes + bytes > 2 * 1024 * 1024) throw new Error("This thread's private connection registry is full. Restart Modex before continuing."); + for (const value of fresh) this.known.add(value); + this.bytes += bytes; this.values = [...this.known].sort((a, b) => b.length - a.length); + } + contains(value: unknown): boolean { + const sensitive = (text: string) => this.values.some(secret => text.includes(secret)) || this.prefix(text) >= PRIVATE_PREFIX; + if (typeof value === "string") return sensitive(value); + let found = false; + JSON.stringify(value, (key, item: unknown) => { if (sensitive(key) || (typeof item === "string" && sensitive(item))) found = true; return item; }); + return found; + } + private replace(value: string): string { for (const secret of this.values) value = value.split(secret).join(MASK); return value; } + text(value: string): string { + const text = this.replace(value), held = this.prefix(text); + return held >= PRIVATE_PREFIX ? text.slice(0, -held) + MASK : text; + } + redact(value: T): T { + if (value === undefined || !this.values.length) return value; + const encoded = JSON.stringify(value, (_key, item: unknown) => typeof item === "string" ? this.text(item) : item); + return JSON.parse(this.replace(encoded)) as T; + } + original(event: Observation): NativeContextInput | undefined { return this.originals.get(event); } + error(value: unknown): Error { + const message = value instanceof Error ? value.message : String(value), safe = this.text(message); + return value instanceof Error && safe === message ? value : new Error(safe); + } + private prefix(text: string): number { + let held = 0; + for (const secret of this.values) { + for (let length = Math.min(secret.length - 1, text.length); length > held; length--) { + if (text.endsWith(secret.slice(0, length))) { held = length; break; } + } + } + return held; + } + private push(key: string, chunk: string, emit: Pending["emit"]): void { + const text = this.replace((this.pending.get(key)?.text ?? "") + chunk), held = this.prefix(text); + if (held) this.pending.set(key, { text: text.slice(-held), emit }); else this.pending.delete(key); + const safe = text.slice(0, text.length - held); if (safe) emit(safe); + } + private flush(key: string): void { + const pending = this.pending.get(key); if (!pending) return; this.pending.delete(key); + // Preserve ordinary short text; suppress long incomplete credential prefixes at a terminal boundary. + pending.emit(pending.text.length >= PRIVATE_PREFIX ? MASK : pending.text); + } + finish(): void { if (this.closed) return; this.closed = true; for (const key of [...this.pending.keys()]) this.flush(key); } + wrap(sink: TurnSink): TurnSink { + let tail = 0; + return { + ...sink, + observe: event => { + if (this.closed) return; + const native = JSON.stringify(event.native); + if (event.source) { const id = JSON.stringify([event.native.runtime, event.source]); if (this.seen.has(id)) return; this.seen.add(id); } + // Native identities remain exact or unavailable; never replace them with invented redacted identities. + if (this.contains(event.native) || (event.source && this.contains(event.source))) return; + if (event.type === "message.delta" || event.type === "reasoning.delta") { + const template: Delta = { ...event, text: "" }; + const key = `${native}:${event.type}:${event.type === "reasoning.delta" ? `${event.representation}:${event.index}` : ""}`; + this.push(key, event.text, text => sink.observe({ ...template, text, ...(template.source ? { source: `${template.source}:private-stream:${++tail}` } : {}) })); + return; + } + if (event.type === "message.done" || event.type === "reasoning.done") for (const key of [...this.pending.keys()]) if (key.startsWith(`${native}:`)) this.flush(key); + if (event.type === "tool.done") this.flush(`${native}:output`); + let safe = this.redact(event); + if (event.type === "tool.start" && safe.type === "tool.start" && this.contains({ args: event.tool.args, exec: event.tool.exec, title: event.tool.title })) safe = { ...safe, tool: { ...safe.tool, displayOnlyInputs: true } }; + if ((event.type === "tool.update" || event.type === "tool.done") && (safe.type === "tool.update" || safe.type === "tool.done") + && this.contains({ args: event.progress?.args, title: event.progress?.title })) safe = { ...safe, progress: { ...safe.progress, displayOnlyInputs: true } }; + if (safe.type === "tool.update" && safe.progress.output !== undefined) { + const output = safe.progress.output, held = this.prefix(output); + safe = { ...safe, progress: { ...safe.progress, output: output.slice(0, output.length - held) } }; + } + const original = event.type === "tool.start" ? { args: event.tool.args, exec: event.tool.exec } : event.type === "tool.done" ? { args: event.progress?.args } : undefined; + if (original) this.originals.set(safe, original); + try { sink.observe(safe); } finally { this.originals.delete(safe); } + }, + ...(sink.output ? { output: (native, chunk) => { if (!this.closed && !this.contains(native)) this.push(`${JSON.stringify(native)}:output`, chunk, text => sink.output!(native, text)); } } : {}), + notice: (level, text) => { if (!this.closed) sink.notice(level, this.text(text)); }, + session: handle => { if (this.closed) return; if (this.contains(handle)) sink.notice("warn", "The native session identifier contained a private connection and was not saved."); else sink.session(handle); }, + approval: async (request, signal) => { + if (this.closed) return "no"; + if (!this.contains(request)) return sink.approval(request, signal); + sink.notice("warn", "The native action contained a private connection and was refused."); return "no"; + }, + refusedByRule: request => this.closed ? undefined : this.contains(request) ? "The action contains a private connection." : sink.refusedByRule?.(request), + ...(sink.deliverRequest ? { deliverRequest: async (native, write) => { if (!this.closed) await sink.deliverRequest!(native, write); } } : {}), + ...(sink.question ? { question: async (request, signal) => { + if (this.closed) return { withdrawn: "ended" as const }; + if (!this.contains(request)) return sink.question!(request, signal); + sink.notice("warn", "The native question contained a private connection and was withdrawn."); return { withdrawn: "ended" as const }; + } } : {}), + }; + } +} diff --git a/apps/desktop/src/main/engine/local-models/execution.ts b/apps/desktop/src/main/engine/local-models/execution.ts index c9905b1d..0e3eaca4 100644 --- a/apps/desktop/src/main/engine/local-models/execution.ts +++ b/apps/desktop/src/main/engine/local-models/execution.ts @@ -166,6 +166,7 @@ export class LocalExecution { if (opts.model !== target.model.id || opts.cwd !== thread.cwd || opts.account) throw new Error("The local target changed. Select it again to start a fresh conversation."); if (opts.plan || opts.images || opts.pages || opts.knowledge || opts.attachments?.some(file => file.kind === "image")) throw new Error("Plan, images and Modex's note and knowledge tools are not qualified for local models yet."); const connection = this.connections.resolve(target.model.connection)!; + if (connection.token) sink.protectCredential?.(connection.token); sink = redactingSink(sink, connection.token); // Every loopback alias of a port is the same server; separate servers sharing a port also wait on each other. let queuedForCapacity = false; diff --git a/apps/desktop/src/main/engine/native-drafts.ts b/apps/desktop/src/main/engine/native-drafts.ts index 9b4d7cc9..e3a1281a 100644 --- a/apps/desktop/src/main/engine/native-drafts.ts +++ b/apps/desktop/src/main/engine/native-drafts.ts @@ -6,7 +6,7 @@ import type { Thread } from "../../shared/types.js"; import type { Store } from "./store.js"; import type { ThreadRunner } from "./runner.js"; import type { AttachmentStore } from "./attachments.js"; -import { PendingSends, type PendingSendInput, type PendingSendRecord, type PendingTarget, type PendingSnapshot } from "./pending-sends.js"; +import { PendingSends, PENDING_CONTEXT_BYTES, PENDING_CONTEXT_NODES, type PendingSendInput, type PendingSendRecord, type PendingTarget, type PendingSnapshot } from "./pending-sends.js"; import { ComposerRecoveryWriter, UpdateRecovery, copyComposerRecovery } from "./update-recovery.js"; import { AttachmentDocuments, type AttachmentDocument } from "./attachment-documents.js"; import { AttachmentTransferJournal } from "./attachment-transfer-journal.js"; @@ -96,7 +96,8 @@ export class NativeDrafts { const matches = this.options.store.items(record.target.threadId).filter(item => item.id === record.id); const item = matches[0]; return matches.length === 1 && item?.kind === "user" && item.text === record.rawText && item.at === record.createdAt - && equal(item.attachments ?? [], record.files) && (!record.execution || equal(item.execution, record.execution)); + && equal(item.attachments ?? [], record.files) && (!record.execution || equal(item.execution, record.execution)) + && (record.context ? recoveryJSON(item.contextEvidence, PENDING_CONTEXT_BYTES, PENDING_CONTEXT_NODES) === recoveryJSON(record.context.evidence, PENDING_CONTEXT_BYTES, PENDING_CONTEXT_NODES) : item.contextEvidence === undefined); } private current(session: string): AttachmentDocument { if (!this.document || session !== this.session) throw fail(); @@ -247,12 +248,20 @@ export class NativeDrafts { const existing = input?.source?.kind === "thread" ? this.options.store.thread(input.source.threadId) : undefined; const destination = existing ?? input?.configuration?.settings; if (!destination) throw fail(); + const context = input.contextDraft !== undefined ? this.options.runner.snapshotContextForPending(input.contextDraft, input.source) : undefined; const pending = this.pending.begin(input, record => { if (this.current(session) !== document || !this.ownerExists(record.source)) return false; for (const other of this.records()) if (key(other.owner) === key(record.source) || other.files.some(file => record.files.some(selected => selected.id === file.id))) return false; - return record.files.every(file => this.documents.validate(document, record.source, file)); - }, this.execution(destination)); + if (!record.files.every(file => this.documents.validate(document, record.source, file))) return false; + if (input.contextDraft !== undefined) { + const threadId = record.creation?.threadId ?? existing?.id, at = record.creation?.createdAt ?? existing?.createdAt; + if (!threadId || !at) throw fail(); + this.options.runner.preparePendingTaskContext(input.contextDraft, record.id, threadId, at); + } + return true; + }, this.execution(destination), context); + if (input.contextDraft !== undefined) this.options.runner.pendingTaskContextPersisted(input.contextDraft, pending.value!.id); if (!this.documents.refreshRetention()) throw fail(); return { id: pending.value!.id }; } @@ -271,7 +280,7 @@ export class NativeDrafts { this.assertExecution(record, thread); const target = { threadId: thread.id, projectId: thread.projectId, createdAt: thread.createdAt }; if (!this.proveTarget(target)) throw fail(); - this.pending.bind(intentId, () => target); + this.pending.bind(intentId, () => target, context => this.options.runner.pendingContextForThread(context, thread!.id)); // An explicit retry can revive a retired source only from the strictly // persisted original. A late picker still holds its revoked old handle. this.documents.restorePending(document, record.owner, record.files); @@ -322,7 +331,7 @@ export class NativeDrafts { if (!thread) throw fail(); this.assertExecution(record, thread); await this.options.runner.sendDurably(record.target.threadId, record.rawText, record.files, { - id: record.id, at: record.createdAt, committed: () => { + id: record.id, at: record.createdAt, ...(record.context ? { contextEvidence: record.context.evidence } : {}), committed: () => { if (this.current(session) !== document) throw fail(); this.pending.complete(intentId, value => this.proveUser(value)); this.documents.discard(this.documents.capture(document, record.owner), record.files.map(file => file.id)); diff --git a/apps/desktop/src/main/engine/pages-agents.ts b/apps/desktop/src/main/engine/pages-agents.ts index 507d59f9..6c5fe937 100644 --- a/apps/desktop/src/main/engine/pages-agents.ts +++ b/apps/desktop/src/main/engine/pages-agents.ts @@ -8,14 +8,20 @@ import type { PagesChange, PagesConnection } from "../../shared/pages.js"; import { pageTitle } from "../../shared/space.js"; import type { SpacePagesStore } from "./space-store.js"; import type { NotesStore } from "./notes-store.js"; +import { TaskContextCapture, type CapturedNoteInput } from "./task-context-capture.js"; +import type { TurnRef } from "../../shared/conversation-events.js"; +import type { ContextCapture, ContextSource, ContextTarget } from "../../shared/task-context-evidence.js"; + +export interface PagesReadReceipt { source: Extract; captures: ContextCapture[] } +export interface PagesReadObserver { attempt: TurnRef; cwd: string; isCurrent(): boolean; observed(receipt: PagesReadReceipt): void | Promise } type Turn = Pick; /** A project's own notes: its notes project's id and folder. Agents of that project see nothing outside it. */ export type PagesScope = { id: string; rel: string }; -type Active = { turn: Turn; projectId: string; readOnly: boolean; available: boolean; scope?: PagesScope; error?: string; changed(change: PagesChange): void }; +type Active = { turn: Turn; projectId: string; readOnly: boolean; available: boolean; closed: AbortController; scope?: PagesScope; error?: string; observer?: PagesReadObserver; leaseId?: string; changed(change: PagesChange): void }; type TreeNotes = Pick; type Route = { token: string; active?: Active }; -export type PagesLease = { connection: PagesConnection; close(): Promise }; +export type PagesLease = { connection: PagesConnection; leaseId?: string; close(): Promise }; const string = { type: "string" }; const id = { type: "string", description: "Page ID returned by search/read/create." }; const revision = { type: "integer", minimum: 1, description: "Current revision from read. Stale writes fail; read and reconcile before retrying." }; @@ -53,6 +59,7 @@ export class PagesAgents { private readonly server: http.Server; private listening?: Promise; private disposed = false; + private readonly capture: Pick; constructor(private readonly options: { space: SpacePagesStore; enabled(projectId: string): boolean; updated?(): void; /** @@ -62,7 +69,9 @@ export class PagesAgents { */ scope?(projectId: string, create: boolean): PagesScope | null | undefined; notes?: TreeNotes; + capture?: Pick; }) { + this.capture = options.capture ?? new TaskContextCapture(); this.server = http.createServer((req, res) => { void this.handle(req, res).catch(() => { if (!res.headersSent) res.writeHead(500); res.end(); }); }); } private listen(): Promise { @@ -74,10 +83,12 @@ export class PagesAgents { }); }); } - async prepare(turn: Turn, changed: Active["changed"], signal?: AbortSignal): Promise { + async prepare(turn: Turn, changed: Active["changed"], signal?: AbortSignal, observer?: PagesReadObserver): Promise { // Capture authority before the first await; Store mutates thread objects in place. const readOnly = turn.plan || turn.mode === "chat"; const projectId = turn.projectId; + const bound = observer ? { ...observer, attempt: structuredClone(observer.attempt) } : undefined; + if (bound && (bound.attempt.thread !== turn.id || !bound.isCurrent())) throw new Error("Notes observation does not belong to this active attempt."); // A notes folder that cannot be read must not fail the turn. The lease stays attached and // refuses every call, rather than running unscoped and showing other projects' notes. let scope: PagesScope | undefined, error: string | undefined; @@ -91,14 +102,22 @@ export class PagesAgents { const base = await this.listen(); signal?.throwIfAborted(); if (this.disposed) throw new Error("Pages agents are shutting down."); - const route = this.routes.get(turn.id) ?? { token: randomUUID() }; - if (route.active) throw new Error("Pages is already attached to this turn."); - this.routes.set(turn.id, route); - const active: Active = { turn, projectId, readOnly, available: true, ...(scope ? { scope } : {}), ...(error ? { error } : {}), changed }; + if ([...this.routes.values()].some(route => route.active?.available && route.active.turn.id === turn.id)) throw new Error("Pages is already attached to this turn."); + const leaseId = bound ? randomUUID() : undefined, key = leaseId ? `attempt:${leaseId}` : turn.id; + const route = this.routes.get(key) ?? { token: randomUUID() }; + this.routes.set(key, route); + const active: Active = { turn, projectId, readOnly, available: true, closed: new AbortController(), ...(bound ? { observer: bound, leaseId } : {}), ...(scope ? { scope } : {}), ...(error ? { error } : {}), changed }; route.active = active; + const close = () => { + active.available = false; active.closed.abort(); + if (route.active === active) route.active = undefined; + if (leaseId && this.routes.get(key) === route) this.routes.delete(key); + signal?.removeEventListener("abort", close); + }; + signal?.addEventListener("abort", close, { once: true }); if (signal?.aborted) close(); return { connection: { url: `${base}/mcp/${route.token}`, instructions: pagesInstructions(this.writable(active)) }, - close: async () => { active.available = false; if (route.active === active) route.active = undefined; }, + ...(leaseId ? { leaseId } : {}), close: async () => close(), }; } private writable(active: Active): boolean { @@ -114,9 +133,10 @@ export class PagesAgents { const requestActive = route.active; const mcp = new Server({ name: "modex-pages", version: "1" }, { capabilities: { tools: {} } }); mcp.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS })); - mcp.setRequestHandler(CallToolRequestSchema, async request => { + mcp.setRequestHandler(CallToolRequestSchema, async (request, extra) => { try { - const result = this.call(requestActive, request.params.name, request.params.arguments ?? {}); + const args = request.params.arguments ?? {}; + const result = await this.observeRead(requestActive, request.params.name, args, this.call(requestActive, request.params.name, args), extra.requestId, extra.signal, route.token); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: (error as Error).message }] }; } }); @@ -125,8 +145,61 @@ export class PagesAgents { await mcp.connect(transport); await transport.handleRequest(req, res); } + private assertReadActive(active: Active, targets: ContextTarget[], signal: AbortSignal): void { + signal.throwIfAborted(); + if (!active.available || active.closed.signal.aborted || this.disposed || !active.observer?.isCurrent()) throw new Error("Notes observation ended with this attempt."); + if (!this.options.enabled(active.projectId)) throw new Error("Agent notes access is disabled for this project."); + const scope = active.scope, notes = this.options.notes; + if (scope && notes) { + const nodes = notes.tree().nodes, home = nodes.find(node => node.id === scope.id && node.kind === "project")?.rel; + if (home === undefined) throw new Error("This project's notes are no longer available."); + const rels = notes.pageRels(), within = (rel: string | undefined) => rel !== undefined && (rel === home || rel.startsWith(`${home}/`)); + for (const target of targets) { + const rel = target.kind === "note" ? rels.get(target.pageId) : target.kind === "notes-file" ? nodes.find(node => node.kind === "file" && node.id === target.id)?.rel : undefined; + if (!within(rel)) throw new Error("This note is outside this project's notes."); + } + } + } + private async observeRead(active: Active | undefined, name: string, args: Record, result: unknown, requestId: string | number, requestSignal: AbortSignal, token: string): Promise { + if (!active?.observer || !["read", "history", "read_file"].includes(name)) return result; + if ((typeof requestId === "string" && (!requestId || requestId.length > 512 || /[\u0000-\u001f\u007f\ud800-\udfff]/u.test(requestId) || /^(?:[\\/]|[A-Za-z]:[\\/])/.test(requestId) || requestId.includes(token))) + || (typeof requestId === "number" && !Number.isSafeInteger(requestId))) throw new Error("Invalid Notes read request identity."); + const signal = AbortSignal.any([requestSignal, active.closed.signal]); + this.assertReadActive(active, [], signal); + // Snapshot before the first await: later edits cannot change either the + // returned response or the exact bodies/revisions used for its receipts. + const snapshot = structuredClone(result); + const source: PagesReadReceipt["source"] = { kind: "notes-read", turn: structuredClone(active.observer.attempt), leaseId: active.leaseId!, receiptId: randomUUID(), requestId, method: name as PagesReadReceipt["source"]["method"], at: new Date().toISOString() }; + const notes: CapturedNoteInput[] = []; + const pageBody = (value: unknown, via: "read" | "history") => { + if (!value || typeof value !== "object" || Array.isArray(value)) { if (via === "read") throw new Error("Invalid Notes read response."); return; } + const page = value as { id?: unknown; markdown?: unknown; revision?: unknown; updatedAt?: unknown }; + if (page.id !== args.id) throw new Error("History returned a different Note identity."); + if (typeof page.markdown !== "string") { if (via === "read") throw new Error("Notes read did not return a body."); return; } + if (!Number.isSafeInteger(page.revision) || (page.revision as number) < 1 || typeof page.updatedAt !== "string" || !/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(page.updatedAt) || !Number.isFinite(Date.parse(page.updatedAt)) || new Date(page.updatedAt).toISOString() !== page.updatedAt) throw new Error("Invalid returned Note revision."); + notes.push({ target: { kind: "note", pageId: args.id as string }, source, text: page.markdown, version: { via, revision: page.revision as number, updatedAt: page.updatedAt } }); + }; + if (name === "read") pageBody(snapshot, "read"); + else if (name === "history") { + const versions = (snapshot as { versions?: unknown })?.versions; + if (!Array.isArray(versions)) throw new Error("Invalid Notes history response."); + for (const version of versions) pageBody(version?.page, "history"); + } else { + const file = snapshot as { id?: unknown; text?: unknown }; + if (!file || file.id !== args.id || typeof file.text !== "string") throw new Error("Invalid Notes file response."); + notes.push({ target: { kind: "notes-file", id: args.id as string }, source, text: file.text }); + } + if (!notes.length) return snapshot; + this.assertReadActive(active, notes.map(note => note.target), signal); + const captures = (await this.capture.captureNotes(active.observer.cwd, notes, "after-notes-read", signal)).map(capture => ({ ...capture, workspace: { kind: "thread" as const, projectId: active.projectId, threadId: source.turn.thread } })); + this.assertReadActive(active, notes.map(note => note.target), signal); + await active.observer.observed({ source: structuredClone(source), captures }); + this.assertReadActive(active, notes.map(note => note.target), signal); + return snapshot; + } private call(active: Active | undefined, name: string, args: Record): unknown { if (!active?.available) throw new Error("Pages access ended with this turn."); + if (active.observer && !active.observer.isCurrent()) throw new Error("Pages access ended with this attempt."); if (!this.options.enabled(active.projectId)) throw new Error("Agent notes access is disabled for this project. Enable it in Space."); if (active.error) throw new Error(`This project's notes are unavailable: ${active.error}`); validate(name, args); @@ -240,7 +313,7 @@ export class PagesAgents { } async dispose(): Promise { this.disposed = true; - for (const route of this.routes.values()) if (route.active) route.active.available = false; + for (const route of this.routes.values()) if (route.active) { route.active.available = false; route.active.closed.abort(); } if (this.listening) { await this.listening; this.server.closeAllConnections(); await new Promise(resolve => this.server.close(() => resolve())); } this.routes.clear(); } diff --git a/apps/desktop/src/main/engine/pending-sends.ts b/apps/desktop/src/main/engine/pending-sends.ts index d8329fd3..43f012e9 100644 --- a/apps/desktop/src/main/engine/pending-sends.ts +++ b/apps/desktop/src/main/engine/pending-sends.ts @@ -1,3 +1,4 @@ +import { sameContextDraftOwner, isContextDraftBinding, isPendingTaskContext, taskContextBelongsTo, type PendingTaskContext } from "../../shared/task-context-evidence.js"; import { threadTarget } from "../../shared/local-models.js"; import { isBackendId } from "../../shared/types.js"; import type { PendingExecutionBinding } from "../../shared/native-drafts.js"; @@ -12,7 +13,18 @@ import { AttachmentRecoveryFile, RecoveryFileWriteError, parseRecoveryJSON, reco export const PENDING_TEXT_CHARACTERS = 1024 * 1024; // Composer accepts up to 2 MiB encoded snapshots, including escaping/UTF-8. // Add bounded intent identity/configuration metadata without narrowing that text. -export const PENDING_RECORD_BYTES = 2 * 1024 * 1024 + 64 * 1024; +export const PENDING_BASE_BYTES = 2 * 1024 * 1024 + 64 * 1024; +// A <=2 MiB selected snapshot may gain one unavailable thread-workspace +// observation per file when a project intent binds its first target. Every +// selected ref already has a capture, so reserve a second bounded snapshot's +// worth of metadata without borrowing from the composer's text allowance. +export const PENDING_CONTEXT_BYTES = 4 * 1024 * 1024 + 4096; +export const PENDING_RECORD_BYTES = PENDING_BASE_BYTES + PENDING_CONTEXT_BYTES; +// Context contains many small typed receipts. Its separate node allowance does +// not change the existing composer's 20,000-node validation below. +export const PENDING_CONTEXT_NODES = 512000; +export const PENDING_RECORD_NODES = PENDING_CONTEXT_NODES + 20000; +export const pendingRecordJSON = (value: unknown): string => recoveryJSON(value, PENDING_RECORD_BYTES, PENDING_RECORD_NODES); const MAX_PENDING = 1000; const uuid = (value: unknown): value is string => typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value); const id = (value: unknown): value is string => typeof value === "string" && value.length <= 128 && /^[A-Za-z0-9_-]+$/.test(value) && value !== "staging"; @@ -20,7 +32,7 @@ const fail = () => new Error("Pending message recovery requires attention; origi export type { PendingTarget, PendingCreation, PendingSendRecord, PendingSendInput } from "../../shared/native-drafts.js"; import type { PendingTarget, PendingSendRecord } from "../../shared/native-drafts.js"; export type PendingSnapshot = RecoveryFileSnapshot; -const equal = (left: unknown, right: unknown) => recoveryJSON(left, PENDING_RECORD_BYTES) === recoveryJSON(right, PENDING_RECORD_BYTES); +const equal = (left: unknown, right: unknown) => pendingRecordJSON(left) === pendingRecordJSON(right); function shape(value: unknown, keys: string[]): asserts value is Record { if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length !== keys.length || keys.some(key => !Object.hasOwn(value, key))) throw fail(); } @@ -43,6 +55,7 @@ function validateTarget(value: unknown): asserts value is PendingTarget { function validateRecord(value: unknown): asserts value is PendingSendRecord { const keys = ["schema", "id", "createdAt", "source", "owner", "rawText", "configuration", "files", "creation", "target"]; if (value && typeof value === "object" && Object.hasOwn(value, "execution")) keys.push("execution"); + if (value && typeof value === "object" && Object.hasOwn(value, "context")) keys.push("context"); shape(value, keys); if (Object.hasOwn(value, "execution")) { const binding = value.execution; @@ -53,6 +66,11 @@ function validateRecord(value: unknown): asserts value is PendingSendRecord { if (parsed.kind !== "local" || parsed.target.model.id !== binding.model || binding.account !== null || !Number.isSafeInteger(binding.localContextTokens) || (binding.localContextTokens as number) < 4096 || (binding.localContextTokens as number) > 262144) throw fail(); } else if (binding.localContextTokens !== null) throw fail(); } + if (Object.hasOwn(value, "context")) { + if (!isPendingTaskContext(value.context)) throw fail(); + recoveryJSON(value.context, PENDING_CONTEXT_BYTES, PENDING_CONTEXT_NODES); + const { context: _context, ...base } = value; recoveryJSON(base, PENDING_BASE_BYTES); + } else recoveryJSON(value, PENDING_BASE_BYTES); if (value.schema !== 1 || !uuid(value.id) || typeof value.rawText !== "string") throw fail(); date(value.createdAt); owner(value.source); owner(value.owner); if (value.rawText.length > PENDING_TEXT_CHARACTERS) throw fail(); @@ -81,10 +99,17 @@ function validateRecord(value: unknown): asserts value is PendingSendRecord { if (value.target && (value.target.threadId !== value.creation.threadId || value.target.createdAt !== value.creation.createdAt || value.target.projectId !== value.source.projectId)) throw fail(); } else if (value.creation !== null || (value.target && value.target.threadId !== value.source.threadId)) throw fail(); - if (!equal(value.owner, value.source) && (value.owner.kind !== "thread" || !value.target || value.owner.threadId !== value.target.threadId)) throw fail(); + if (!sameContextDraftOwner(value.owner, value.source) && (value.owner.kind !== "thread" || !value.target || value.owner.threadId !== value.target.threadId)) throw fail(); + if (value.context) { + const context = value.context as PendingTaskContext; + if (!sameContextDraftOwner(context.owner, value.source) || (value.source.kind === "project" && context.projectId !== value.source.projectId) + || (value.target && context.projectId !== value.target.projectId)) throw fail(); + const threadId = value.target?.threadId ?? value.creation?.threadId ?? (value.source.kind === "thread" ? value.source.threadId : ""); + if (!taskContextBelongsTo(context.evidence, threadId as string, value.id as string, context.projectId)) throw fail(); + } } function copyRecord(value: unknown): PendingSendRecord { - const copied = parseRecoveryJSON(recoveryJSON(value, PENDING_RECORD_BYTES), PENDING_RECORD_BYTES); + const copied = parseRecoveryJSON(pendingRecordJSON(value), PENDING_RECORD_BYTES, PENDING_RECORD_NODES); validateRecord(copied); return copied; } @@ -134,7 +159,7 @@ export class PendingSends { if (!file) { file = new AttachmentRecoveryFile(this.directory, `${id}.json`, PENDING_RECORD_BYTES, value => { validateRecord(value); if (value.id !== id) throw fail(); - }); + }, PENDING_RECORD_NODES); this.files.set(id, file); } return file; @@ -150,14 +175,21 @@ export class PendingSends { this.file(id).checkpoint(expected); }); } - begin(input: unknown, authorize: (record: NonNullable) => boolean, execution?: PendingExecutionBinding): PendingSnapshot { + begin(input: unknown, authorize: (record: NonNullable) => boolean, execution?: PendingExecutionBinding, context?: PendingTaskContext): PendingSnapshot { return this.operation(() => { const copied: unknown = parseRecoveryJSON(recoveryJSON(input, PENDING_RECORD_BYTES), PENDING_RECORD_BYTES); - shape(copied, ["source", "rawText", "configuration", "files"]); owner(copied.source); + const keys = ["source", "rawText", "configuration", "files"]; + if (copied && typeof copied === "object" && Object.hasOwn(copied, "contextDraft")) keys.push("contextDraft"); + shape(copied, keys); owner(copied.source); + const provided = Object.hasOwn(copied, "contextDraft"); + if (provided ? !isContextDraftBinding(copied.contextDraft) || !context : context !== undefined) throw fail(); + if (context && (!isPendingTaskContext(context) || !sameContextDraftOwner(context.owner, copied.source) || !sameContextDraftOwner((copied.contextDraft as { owner: unknown }).owner, copied.source))) throw fail(); + const { contextDraft: _editable, ...message } = copied; + recoveryJSON(message, PENDING_BASE_BYTES); const id = randomUUID(), createdAt = new Date().toISOString(); - const record = copyRecord({ schema: 1, id, createdAt, ...copied, ...(execution ? { execution } : {}), owner: copied.source, + const record = copyRecord({ schema: 1, id, createdAt, ...message, ...(execution ? { execution } : {}), ...(context ? { context } : {}), owner: copied.source, creation: copied.source.kind === "project" ? { threadId: randomUUID(), createdAt } : null, target: null }); - const admitted = JSON.parse(recoveryJSON(record, PENDING_RECORD_BYTES)) as PendingSendRecord; + const admitted = JSON.parse(pendingRecordJSON(record)) as PendingSendRecord; const freeze = (value: unknown): void => { if (value && typeof value === "object") { for (const child of Object.values(value)) freeze(child); Object.freeze(value); } }; freeze(admitted); if (authorize(admitted) !== true || this.reentered) throw fail(); @@ -168,13 +200,14 @@ export class PendingSends { }); } /** resolve is a main-only Store checkpoint/result lookup, never IPC input. */ - bind(id: string, resolve: (record: NonNullable) => PendingTarget): PendingSnapshot { + bind(id: string, resolve: (record: NonNullable) => PendingTarget, contextForTarget?: (context: PendingTaskContext, target: PendingTarget) => PendingTaskContext): PendingSnapshot { return this.operation(() => { const file = this.file(id), before = file.read(); if (!before.value) throw fail(); const target: unknown = parseRecoveryJSON(recoveryJSON(resolve(before.value), 2048), 2048); validateTarget(target); if (this.reentered) throw fail(); if (before.value.target && !equal(before.value.target, target)) throw fail(); - const next = copyRecord({ ...before.value, target }); + const context = !before.value.target && before.value.context && contextForTarget ? contextForTarget(structuredClone(before.value.context) as PendingTaskContext, target) : before.value.context; + const next = copyRecord({ ...before.value, target, ...(context ? { context } : {}) }); return equal(before.value, next) ? before : file.write(before, next); }); } diff --git a/apps/desktop/src/main/engine/runner.ts b/apps/desktop/src/main/engine/runner.ts index a8556d07..8ae9f580 100644 --- a/apps/desktop/src/main/engine/runner.ts +++ b/apps/desktop/src/main/engine/runner.ts @@ -1,3 +1,9 @@ +import { TaskContextCapture } from "./task-context-capture.js"; +import { TaskContextAttempt } from "./task-context-attempt.js"; +import { ContextPrivacy } from "./context-privacy.js"; +import type { TaskContextAgents } from "./task-context-agents.js"; +import { TaskContextService } from "./task-context-service.js"; +import { describeTaskContext, isContextDraftBinding, isTaskContextEvidence, taskContextBelongsTo, copyTaskContextEvidence, type ContextDraftOwner, type ContextDraftBinding, type ContextTarget, type TaskContextEvidence, type PendingTaskContext } from "../../shared/task-context-evidence.js"; import { executionBinding } from "./execution-binding.js"; import { resolveCli } from "./cli-path.js"; import type { LocalExecution } from "./local-models/execution.js"; @@ -35,7 +41,7 @@ import type { PendingCreation, PendingSendRecord } from "./pending-sends.js"; import { copyComposerRecovery } from "./update-recovery.js"; import { parseRecoveryJSON, recoveryJSON } from "./attachment-recovery-file.js"; import type { DraftRemovalScope } from "../../shared/native-drafts.js"; -import { boundOutput } from "../../shared/evidence.js"; +import { boundOutput, type Exec } from "../../shared/evidence.js"; import { taskMarkdown, taskRecord, type TaskNoteView } from "../../shared/task-context.js"; import { openingTitle } from "./titles.js"; import { actionDigest, emptyRequests, publicAnswers, register as registerRequest, resolve as resolveRequest, settle as settleRequest, validateAnswers, withdraw as withdrawRequests, type Question, type RequestBinding, type RequestsState, type Resolution } from "../../shared/human-requests.js"; @@ -50,6 +56,7 @@ type UserItem = Extract; export interface DurableSendAdmission { readonly id: string; readonly at: string; + readonly contextEvidence?: TaskContextEvidence; /** Main-only synchronous callback; never IPC data. Called after the raw user * item is durable and before routing/backend work. No automatic replay. */ readonly committed: (item: Readonly> & { readonly attachments?: readonly Readonly[] }) => void; @@ -88,9 +95,14 @@ interface Live { followUp?: { key: string; abort: AbortController; result: Promise }; /** Processes a Stop asked to end that never confirmed their exit; their thread's files stay while one lives. */ unconfirmed?: Set; + /** Known context capabilities and loaded credentials survive retries/turns in main memory; never serialized. */ + privateConnections?: Set; } export interface RunnerOptions { + taskContext?: Pick; + /** Main shares draft, native and Notes capture scheduling through one engine. */ + contextCapture?: TaskContextCapture; /** Runs threads with a local target through their own Codex children (#379). Without it, such threads refuse to run. */ localExecution?: LocalExecution; beforeSend?(thread: Thread): void; @@ -151,6 +163,7 @@ export class ThreadRunner { /** Threads running an unattended routine turn: no person is there to answer, so approvals are refused. */ private readonly unattended = new Set(); private readonly creating = new Map>>(); + private readonly contextCreations = new Map }>(); private readonly pendingCreations = new Map }>(); private readonly backends: Record; /** Auto routing: judges a request (Jev or the offline heuristic) and picks model/effort/fast per turn. */ @@ -158,8 +171,13 @@ export class ThreadRunner { readonly attachments: AttachmentStore; /** Raw output of tools that outgrew the transcript's preview (main/engine/tool-logs.ts). */ readonly toolLogs: ToolLogStore; + private readonly contextDrafts: TaskContextService; + private readonly contextCapture: TaskContextCapture; + private readonly contextAttempts = new Map(); constructor(private readonly o: RunnerOptions) { + this.contextCapture = o.contextCapture ?? new TaskContextCapture(); + this.contextDrafts = new TaskContextService({ store: o.store, capture: this.contextCapture }); this.attachments = o.attachments ?? new AttachmentStore(path.join(o.home, "attachments")); this.toolLogs = new ToolLogStore(path.join(o.home, "app", "tool-logs")); const s = () => o.store.settings; @@ -202,6 +220,7 @@ export class ThreadRunner { async dispose(): Promise { this.disposing = true; + this.contextDrafts.dispose(); for (const abort of this.naming.values()) abort.abort(); this.naming.clear(); const runs = [...this.live.values()].flatMap((l) => l.run ? [l.run] : []); @@ -217,7 +236,7 @@ export class ThreadRunner { holdForRestart(): () => void { this.assertNotRestarting(); if (this.disposing) throw new Error("Modex is shutting down."); - if (this.creating.size || this.deleting.size || this.removingProjects.size || this.titleRuns.size + if (this.creating.size || this.contextCreations.size || this.deleting.size || this.removingProjects.size || this.titleRuns.size || [...this.live.values()].some(l => l.run || l.pending.size || l.status === "running" || l.status === "waiting")) { throw new Error("Let current turns and worktree operations finish before restarting."); } @@ -333,8 +352,30 @@ export class ThreadRunner { return this.o.store.thread(thread.id)!; } - async createThread(projectId: string, opts: { worktree?: boolean; mode?: Mode; model?: string; backend?: BackendId; auto?: boolean } = {}): Promise { - return this.trackCreation(projectId, opts); + async createThread(projectId: string, opts: { worktree?: boolean; mode?: Mode; model?: string; backend?: BackendId; auto?: boolean; contextDraft?: ContextDraftBinding } = {}): Promise { + if (opts.contextDraft === undefined) return this.trackCreation(projectId, opts); + if (!isContextDraftBinding(opts.contextDraft)) throw new Error("Invalid task context draft."); + const binding = structuredClone(opts.contextDraft); + this.assertContextEditing(binding.owner); + const existing = this.contextDrafts.creationFor(binding, projectId); + if (existing) return this.o.store.thread(existing)!; + const pending = this.contextCreations.get(binding.nonce); + if (pending) { + if (pending.projectId !== projectId || JSON.stringify(pending.binding) !== JSON.stringify(binding)) throw new Error("Context creation identity changed."); + return pending.promise; + } + const reserved = this.contextDrafts.reserveCreation(binding, opts.worktree === true); + let resolve!: (thread: Thread) => void, reject!: (error: unknown) => void; + const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); + this.contextCreations.set(binding.nonce, { projectId, binding, promise }); + void this.trackCreation(projectId, opts, reserved).then(thread => { this.contextDrafts.bindCreatedThread(binding, thread.id); return thread; }).then(thread => { + this.contextCreations.delete(binding.nonce); resolve(thread); + }, error => { + // A failed worktree script may have made evidence. Never run it again from this nonce. + if (!opts.worktree) this.contextCreations.delete(binding.nonce); + reject(error); + }); + return promise; } /** @@ -367,6 +408,86 @@ export class ThreadRunner { } finally { this.unattended.delete(threadId); } } + private assertContextEditing(owner: ContextDraftOwner): void { + this.assertNotRestarting(); + if (this.disposing) throw new Error("Modex is shutting down."); + if (owner?.kind === "thread") this.assertThreadAvailable(owner.threadId); + else if (owner?.kind === "project" && this.removingProjects.has(owner.projectId)) throw new Error("This project is being removed."); + } + openContextDraft(owner: ContextDraftOwner): ContextDraftBinding { + this.assertContextEditing(owner); + return this.contextDrafts.openContextDraft(owner); + } + selectTaskContext(binding: ContextDraftBinding, targets: ContextTarget[]): Promise<{ binding: ContextDraftBinding; context: TaskContextEvidence }> { + this.assertContextEditing(binding?.owner); + return this.background.run(() => this.contextDrafts.selectTaskContext(binding, targets)); + } + closeContextDraft(binding: ContextDraftBinding): void { this.contextDrafts.closeContextDraft(binding); } + draftTaskContext(binding: ContextDraftBinding): TaskContextEvidence { + this.assertContextEditing(binding?.owner); return this.contextDrafts.draftTaskContext(binding); + } + confirmTaskContext(threadId: string, turnId: string, expectedRevision: number, refIds: string[]): TaskContextEvidence { + this.assertContextEditing({ kind: "thread", threadId }); + const thread = this.o.store.thread(threadId)!, context = this.taskContext(threadId, turnId); + if (!Number.isSafeInteger(expectedRevision) || context.revision !== expectedRevision) throw new Error("Task context changed; review it again."); + if (!Array.isArray(refIds) || refIds.length > context.refs.length || new Set(refIds).size !== refIds.length || refIds.some(id => !context.refs.some(ref => ref.id === id))) throw new Error("Choose existing references from this task."); + this.contextDrafts.assertReferenceScope(thread.projectId, context.refs.filter(ref => refIds.includes(ref.id)).map(ref => ref.target)); + const source = { kind: "human" as const, actionId: newId(), at: new Date().toISOString() }; + this.updateTaskContext(threadId, this.slot(threadId), { thread: threadId, turn: turnId, attempt: source.actionId, backend: thread.backend }, thread.projectId, evidence => { + if (evidence.revision !== expectedRevision) throw new Error("Task context changed; review it again."); + for (const ref of evidence.refs) { + ref.relations = ref.relations.filter(relation => relation.provenance !== "user-confirmed"); + if (refIds.includes(ref.id)) ref.relations.push({ kind: "relevant", provenance: "user-confirmed", source }); + } + evidence.revision++; + }); + return this.taskContext(threadId, turnId); + } + taskContext(threadId: string, turnId: string): TaskContextEvidence { + const thread = this.o.store.thread(threadId); + if (!thread || !this.o.store.project(thread.projectId)) throw new Error("Task context project is unavailable."); + const user = this.items(threadId).find(item => item.kind === "user" && item.id === turnId); + if (user?.kind !== "user") throw new Error("That request is no longer in this thread."); + const context = copyTaskContextEvidence(user.contextEvidence ?? { revision: 0, refs: [], captures: [], checks: [] }); + if (!taskContextBelongsTo(context, threadId, turnId, thread.projectId)) throw new Error("Task context scope changed."); + return context; + } + confirmTaskCheck(threadId: string, turnId: string, expectedRevision: number, itemId: string, refIds: string[]): TaskContextEvidence { + this.assertContextEditing({ kind: "thread", threadId }); + const thread = this.o.store.thread(threadId)!; + const context = this.taskContext(threadId, turnId); + if (!Number.isSafeInteger(expectedRevision) || context.revision !== expectedRevision) throw new Error("Task context changed; review it again."); + if (!Array.isArray(refIds) || refIds.length > context.refs.length || refIds.some(id => typeof id !== "string") || new Set(refIds).size !== refIds.length + || refIds.some(id => !context.refs.some(ref => ref.id === id))) throw new Error("Choose existing references from this task."); + const items = this.items(threadId), start = items.findIndex(item => item.kind === "user" && item.id === turnId); + const next = items.findIndex((item, i) => i > start && item.kind === "user"); + const check = context.checks.find(check => check.itemId === itemId); + const item = items.slice(start + 1, next < 0 ? undefined : next).find(item => item.kind === "tool" && item.id === itemId); + if (!check?.origin || item?.kind !== "tool" || !item.origin || JSON.stringify(check.origin) !== JSON.stringify(item.origin) || item.exec?.command !== check.command) + throw new Error("This check does not belong to the requested task."); + // Confirmation adds relevance only. It never changes the native result, plan or timed captures. + this.updateTaskContext(threadId, this.slot(threadId), check.origin, thread.projectId, evidence => { + if (evidence.revision !== expectedRevision) throw new Error("Task context changed; review it again."); + const current = evidence.checks.find(c => c.itemId === itemId)!; + current.relevantRefIds = [...refIds]; current.relevance = refIds.length ? "user-confirmed" : "unestablished"; evidence.revision++; + }); + return this.taskContext(threadId, turnId); + } + invalidateContextDocument(): void { this.contextDrafts.invalidateDocument(); this.contextCreations.clear(); } + taskContextCandidates(binding: ContextDraftBinding): ReturnType { + this.assertContextEditing(binding?.owner); + return this.background.run(() => this.contextDrafts.candidates(binding)); + } + snapshotContextForPending(binding: ContextDraftBinding, owner: ContextDraftOwner): PendingTaskContext { + this.assertContextEditing(owner); return this.contextDrafts.snapshotForPending(binding, owner); + } + preparePendingTaskContext(binding: ContextDraftBinding, turnId: string, threadId: string, threadCreatedAt: string): void { + this.contextDrafts.preparePending(binding, { kind: "pending", turnId, threadId, threadCreatedAt }); + } + pendingTaskContextPersisted(binding: ContextDraftBinding, turnId: string): void { this.contextDrafts.pendingPersisted(binding, turnId); } + pendingContextForThread(context: PendingTaskContext, threadId: string): PendingTaskContext { return this.contextDrafts.pendingForThread(context, threadId); } + + /** * Saves a turn's task record (shared/task-context.ts) to the thread's project notes, at a person's * request. Saving again updates the same note, unless it was edited since: then the edit stays and @@ -394,8 +515,19 @@ export class ThreadRunner { if (!project) throw new Error("This project has no notes yet. Open Notes for it, then save again."); const existing = saved ? notes.list().find(page => page.id === saved.pageId && !page.trashedAt) : undefined; const page = reused ?? notes.create({ title: existing ? `${record.title} (saved again)` : record.title, markdown, containerId: project.id }, change); - notes.writeTaskSnapshot({ version: 1, pageId: page.id, revision: page.revision, projectId: thread.projectId, threadId, turnId, savedAt: page.updatedAt, record }); - const updated = this.o.store.updateThread(threadId, { taskNotes: { ...thread.taskNotes, [turnId]: { pageId: page.id, revision: page.revision } } }); + // These are separate durable writes, not a transaction. Preserve the completed + // note and identify it if a later write cannot be confirmed; never guess rollback. + try { + notes.writeTaskSnapshot({ version: 1, pageId: page.id, revision: page.revision, projectId: thread.projectId, threadId, turnId, savedAt: page.updatedAt, record }); + } catch (error) { + throw new Error(`Note ${page.id}, revision ${page.revision}, was written, but its saved task record could not be confirmed. Keep this note for recovery; saving again may create another copy.`, { cause: error }); + } + let updated: Thread; + try { + updated = this.o.store.updateThread(threadId, { taskNotes: { ...thread.taskNotes, [turnId]: { pageId: page.id, revision: page.revision } } }); + } catch (error) { + throw new Error(`Note ${page.id}, revision ${page.revision}, and its saved task record were written, but the thread link could not be confirmed. Keep this note for recovery; saving again may create another copy.`, { cause: error }); + } this.o.emit({ threadId, type: "thread", thread: updated }); return { pageId: page.id, title: page.title, revision: page.revision, created: !reused, preserved: Boolean(existing && !reused) }; } @@ -432,7 +564,7 @@ export class ThreadRunner { const latest = thread?.taskNotes?.[snapshot.turnId]; const other = latest && latest.pageId !== pageId ? notes.list().find((p) => p.id === latest.pageId && !p.trashedAt) : undefined; return { - record: snapshot.record, threadId: snapshot.threadId, turnId: snapshot.turnId, savedAt: snapshot.savedAt, savedRevision: snapshot.revision, + record: snapshot.record, pageId: snapshot.pageId, projectId: snapshot.projectId, threadId: snapshot.threadId, turnId: snapshot.turnId, savedAt: snapshot.savedAt, savedRevision: snapshot.revision, note: { title: page.title, revision: page.revision, trashed: Boolean(page.trashedAt) }, ...(edited ? { edited } : {}), ...(!home ? { unlinked: true as const } : now && now.id !== home.id ? { moved: { from: home.name, to: now.name } } : {}), @@ -553,6 +685,7 @@ export class ThreadRunner { private async removeProjectHeld(projectId: string): Promise { if (this.removingProjects.has(projectId)) throw new Error("This project is being removed."); this.removingProjects.add(projectId); + this.contextDrafts.cancelProjectCaptures(projectId); try { await Promise.allSettled(this.creating.get(projectId) ?? []); const threads = this.o.store.snapshot().threads.filter((thread) => thread.projectId === projectId); @@ -560,6 +693,7 @@ export class ThreadRunner { const failure = removals.find((result): result is PromiseRejectedResult => result.status === "rejected"); if (failure) throw failure.reason; this.o.store.removeProject(projectId); + this.contextDrafts.invalidateProject(projectId); } finally { this.removingProjects.delete(projectId); } @@ -573,6 +707,7 @@ export class ThreadRunner { private async deleteThreadHeld(threadId: string, removeWorktree = false): Promise { if (this.deleting.has(threadId)) throw new Error("This thread is being deleted. If its CLI has not confirmed the stop, press Stop again to force it."); this.deleting.add(threadId); + this.contextDrafts.cancelThreadCaptures(threadId); this.naming.get(threadId)?.abort(); this.naming.delete(threadId); try { @@ -602,6 +737,7 @@ export class ThreadRunner { } clearTimeout(l?.flushTimer); this.o.store.deleteThread(threadId); + this.contextDrafts.invalidateThread(threadId); this.attachments.removeThread(threadId); this.toolLogs.removeThread(threadId); this.live.delete(threadId); @@ -655,9 +791,11 @@ export class ThreadRunner { } /** Runs one user turn. Resolves when the thread is idle again (or errored). */ - async send(threadId: string, text: string, attachments: Attachment[] = [], by = "desktop"): Promise { + async send(threadId: string, text: string, attachments: Attachment[] = [], by = "desktop", contextDraft?: ContextDraftBinding): Promise { this.assertLocalSender(threadId, by); - return this.start(threadId, text, undefined, attachments); + if (contextDraft !== undefined && !isContextDraftBinding(contextDraft)) throw new Error("Invalid task context draft."); + if (contextDraft !== undefined && by !== "desktop" && by !== "browser") throw new Error("Select draft context from this Mac."); + return this.start(threadId, text, undefined, attachments, undefined, contextDraft); } private assertLocalSender(threadId: string, by: string): void { @@ -669,13 +807,13 @@ export class ThreadRunner { /** Native durable adapter only. Files have already crossed its journaled * ownership transaction. Existing generic send and explicit Retry stay separate. */ async sendDurably(threadId: string, rawText: string, attachments: readonly Readonly[], admission: DurableSendAdmission): Promise { - const id = admission.id, at = admission.at, committed = admission.committed; + const id = admission.id, at = admission.at, committed = admission.committed, contextEvidence = admission.contextEvidence; if (!this.o.store.supportsDurableAdmission) throw new Error("Durable native message admission is unavailable."); if (typeof rawText !== "string" || rawText.length > 1024 * 1024 || !/^[A-Za-z0-9_-]{1,128}$/.test(id) || typeof at !== "string" || !/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(at) || !Number.isFinite(Date.parse(at)) || typeof committed !== "function") throw new Error("Invalid durable message admission."); const files = attachments.map(file => ({ ...file })); - await this.start(threadId, rawText.trim(), undefined, files, Object.freeze({ id, at, committed, rawText })); + await this.start(threadId, rawText.trim(), undefined, files, Object.freeze({ id, at, committed, rawText, ...(contextEvidence !== undefined ? { contextEvidence } : {}) })); } /** @@ -698,7 +836,7 @@ export class ThreadRunner { return this.start(threadId, user.text, user.id, user.attachments); } - private async start(threadId: string, text: string, reuse?: string, attachments: Attachment[] = [], prepared?: PreparedAdmission): Promise { + private async start(threadId: string, text: string, reuse?: string, attachments: Attachment[] = [], prepared?: PreparedAdmission, contextDraft?: ContextDraftBinding): Promise { this.assertThreadAvailable(threadId); this.o.beforeSend?.(this.o.store.thread(threadId)!); const l = this.slot(threadId); @@ -711,13 +849,13 @@ export class ThreadRunner { // A new message on an archived thread brings it back: an archived thread is never one that is working. try { this.unarchive(threadId); - await this.runTurn(threadId, text, reuse, attachments, prepared); + await this.runTurn(threadId, text, reuse, attachments, prepared, contextDraft); } finally { if (l.run === reservation) l.run = null; release(); } } /** `reuse` names an existing user item to run again instead of adding one (Retry). */ - private async runTurn(threadId: string, text: string, reuse?: string, attachments: Attachment[] = [], prepared?: PreparedAdmission): Promise { + private async runTurn(threadId: string, text: string, reuse?: string, attachments: Attachment[] = [], prepared?: PreparedAdmission, contextDraft?: ContextDraftBinding): Promise { const savedThread = this.o.store.thread(threadId); if (!savedThread) throw new Error(`unknown thread ${threadId}`); let thread = savedThread; @@ -727,6 +865,7 @@ export class ThreadRunner { l.completed = false; if (prepared) { + if (prepared.contextEvidence !== undefined && (!isTaskContextEvidence(prepared.contextEvidence) || !taskContextBelongsTo(prepared.contextEvidence, threadId, prepared.id, thread.projectId))) throw new Error("Prepared task context does not belong to this message."); if (reuse !== undefined || l.items.some(item => item.id === prepared.id)) throw new Error("This message was already admitted. Use Retry explicitly."); const checkpoint = this.o.store.checkpointThread(threadId); if (checkpoint.projectId !== savedThread.projectId || checkpoint.createdAt !== savedThread.createdAt) throw new Error("Prepared message target changed."); @@ -756,6 +895,9 @@ export class ThreadRunner { claude: thread.claudeAccount ?? (thread.backend === "claude" && thread.sessionHandle ? "cli" : prospectiveClaudeAccount), }; const admittedExecution = executionBinding(thread, prospectiveCodexAccount); + const turnId = reuse ?? prepared?.id ?? newId(); + if (contextDraft && (reuse !== undefined || prepared)) throw new Error("Editable draft context cannot replace an admitted message."); + const contextAdmission = contextDraft ? this.contextDrafts.prepareContextDraft(contextDraft, threadId, turnId) : undefined; // Staged files move into the thread's folder now, so a Retry (or a restart) still finds them. let draft: AttachmentDraft | undefined; if ((prepared || retry) && Array.isArray(attachments) && attachments.length) { @@ -779,13 +921,14 @@ export class ThreadRunner { l.followUp?.abort.abort(); l.followUp = undefined; // The Modex turn is the user message it answers: Retry runs the same turn again as a new attempt. - const turnId = reuse ?? prepared?.id ?? newId(); if (!retry) { const item: UserItem = { id: turnId, kind: "user", text: prepared?.rawText ?? text, execution: admittedExecution, - at: prepared?.at ?? new Date().toISOString(), ...(files.length ? { attachments: files } : {}) }; - this.addItem(threadId, item, prepared ? () => { + at: prepared?.at ?? new Date().toISOString(), ...(files.length ? { attachments: files } : {}), ...(contextAdmission ? { contextEvidence: contextAdmission.context } : prepared?.contextEvidence !== undefined ? { contextEvidence: copyTaskContextEvidence(prepared.contextEvidence) } : {}) }; + this.addItem(threadId, item, prepared || contextAdmission ? () => { + contextAdmission?.commit(); + if (contextDraft) this.contextDrafts.contextPersisted(contextDraft, threadId, turnId); const receipt = Object.freeze({ ...item, ...(item.attachments ? { attachments: Object.freeze(item.attachments.map(file => Object.freeze({ ...file }))) } : {}) }); - if (prepared.committed(receipt) !== undefined) throw new Error("Durable admission callback must complete synchronously."); + if (prepared && prepared.committed(receipt) !== undefined) throw new Error("Durable admission callback must complete synchronously."); } : undefined); this.assertThreadAvailable(threadId); if (thread.title === "New thread" && thread.titleSource !== "manual") this.updateThread(threadId, { title: derivedTitle }, { automaticTitle: true }); @@ -885,32 +1028,19 @@ export class ThreadRunner { const turnStart = l.items.length; let knowledge: KnowledgeLease | undefined; let pages: PagesLease | undefined; + let taskContext: import("./task-context-agents.js").TaskContextLease | undefined; let images: import("./images/agent.js").ImagesLease | undefined; const revokeConnections = () => { void pages?.close().catch(() => {}); + void taskContext?.close().catch(() => {}); void images?.close().catch(() => {}); void knowledge?.close().catch(() => {}); }; abort.signal.addEventListener("abort", revokeConnections, { once: true }); + let publicationAborted = false; try { this.o.beforeSend?.(thread); if (thread.imageJobId && this.o.images) images = await this.o.images.prepare(thread, abort.signal); - // Prepare Pages first: snapshot the turn's read-only ceiling before knowledge setup awaits. - // Always attach its stable route, including when the project's setting currently disables it. - // Modex's note and knowledge tools are not offered to a local model until they are qualified for it. - if (!local && thread.backend !== "mock" && this.o.pages) { - pages = await this.o.pages.prepare(connectionTurn, change => this.addItem(threadId, { - id: newId(), kind: "notice", level: "info", text: `Note updated: ${change.summary}`, pages: change, at: new Date().toISOString(), - }), abort.signal); - if (abort.signal.aborted) { this.setStatus(threadId, "idle"); return; } - } - if (!local && thread.backend !== "mock" && this.o.knowledge) { - knowledge = await this.o.knowledge.prepare(connectionTurn, change => this.addItem(threadId, { - id: newId(), kind: "notice", level: "info", text: `Knowledge updated: ${change.summary}`, knowledge: change, at: new Date().toISOString(), - }), abort.signal); - if (knowledge.warning) sink.notice("warn", knowledge.warning); - if (abort.signal.aborted) { this.setStatus(threadId, "idle"); return; } - } // Preserve Codex's admitted identity across asynchronous setup; Claude keeps its account-pool binding. const bound = threadAccount(thread); const account = !local && (thread.backend === "codex" || thread.backend === "claude") && backend.identity @@ -920,31 +1050,128 @@ export class ThreadRunner { if (account && !bound) this.o.store.updateThread(threadId, thread.backend === "codex" ? { codexAccount: account } : { claudeAccount: account }); const accountFor = (ran: Thread) => threadAccount(ran) ?? account; const freshConversation = !thread.sessionHandle; - const turnFiles: TurnAttachment[] = files.map((f) => { - const file = this.attachments.resolve(f.rel); - if (!file) throw new Error(`Attachment ${f.name} is missing or changed. Attach it again before retrying.`); - return { name: f.name, mime: f.mime, kind: f.kind, path: file }; - }); // Each run of the backend is one attempt with its own sink; ending it settles whatever it left open. const runOn = async (ran: Thread): Promise => { - const attempt = this.beginAttempt(threadId, l, { thread: threadId, turn: turnId, attempt: newId(), backend: ran.backend, ...(accountFor(ran) ? { account: accountFor(ran) } : {}) }); + const codingCwd = ran.cwd, projectId = ran.projectId; + const actualTurn: TurnRef = { thread: threadId, turn: turnId, attempt: newId(), backend: ran.backend, ...(accountFor(ran) ? { account: accountFor(ran) } : {}) }; + const privacy = new ContextPrivacy(l.privateConnections ??= new Set()); + let publicationFailure: { error: Error; stage: string } | undefined; + const shutdownNotices: { level: "info" | "warn" | "error"; text: string }[] = []; + let shutdownBytes = 0, shutdownTruncated = false, nativeSettled = false; + const retainShutdownNotice = (level: "info" | "warn" | "error", text: string) => { + const safe = privacy.text(text), bytes = Buffer.byteLength(safe); + if (shutdownNotices.length >= 20 || shutdownBytes + bytes > 64 * 1024) { shutdownTruncated = true; return; } + shutdownNotices.push({ level, text: safe }); shutdownBytes += bytes; + }; + const failPublication = (error: unknown, stage: string) => { + publicationFailure ??= { error: privacy.error(error), stage }; + publicationAborted = true; + this.contextAttempts.get(actualTurn.attempt)?.seal(); + try { privacy.finish(); } + catch (cleanup) { retainShutdownNotice("warn", privacy.error(cleanup).message); } + finally { abort.abort(); } + }; + const attempt = this.beginAttempt(threadId, l, actualTurn, privacy, error => failPublication(error, "publish native observation")); + const context = new TaskContextAttempt({ turn: actualTurn, cwd: codingCwd, projectId, signal: abort.signal, capture: this.contextCapture, + privateValue: value => privacy.contains(value), + read: () => { + const user = l.items.find(item => item.kind === "user" && item.id === turnId); + if (user?.kind !== "user" || !user.contextEvidence) throw new Error("Task context user item is missing."); + return user.contextEvidence; + }, + planRevision: () => { + const index = l.items.findIndex(item => item.kind === "user" && item.id === turnId); + const plan = l.items.slice(index + 1).reverse().find(item => item.kind === "plan"); + return plan?.kind === "plan" ? plan.revision : undefined; + }, + current: () => { + const current = this.o.store.thread(threadId); + return this.live.get(threadId) === l && l.abort === abort && l.attempt === actualTurn.attempt && Boolean(current) + && current!.projectId === projectId && current!.cwd === codingCwd && current!.backend === actualTurn.backend && (threadAccount(current!) ?? null) === (actualTurn.account ?? null) + && !this.deleting.has(threadId) && !this.removingProjects.has(projectId); + }, + update: change => this.updateTaskContext(threadId, l, actualTurn, projectId, change), + publicationFailed: error => failPublication(error, "persist task context metadata"), + }); + this.contextAttempts.set(actualTurn.attempt, context); + const ceiling = local ? "local-execution-unqualified" : ran.backend !== "claude" && ran.backend !== "codex" ? "unsupported-adapter" : undefined; + const reportCapability: NonNullable = value => { + if (!context.accepting()) return; + try { context.capability(ceiling ? { state: "unavailable", reason: ceiling } : value.state === "available" && !taskContext ? { state: "unavailable", reason: "configuration-not-bound" } : value); } + catch (error) { failPublication(error, "persist task context capability"); } + }; + let failed = false, failure: unknown; try { - const result = await backend.runTurn( - describeAttachments(text, turnFiles), + context.capability({ state: "unavailable", reason: ceiling ?? "configuration-not-bound" }); + if (!local && (ran.backend === "claude" || ran.backend === "codex") && this.o.taskContext) { + taskContext = await this.o.taskContext.prepare({ turn: actualTurn, cwd: codingCwd, projectId, isCurrent: () => context.accepting(), accept: proposal => context.proposed(proposal) }, context.signal); + privacy.addConnection(taskContext.connection.url); + } + if (!local && ran.backend !== "mock" && this.o.pages) { + pages = await this.o.pages.prepare(connectionTurn, change => this.addItem(threadId, { + id: newId(), kind: "notice", level: "info", text: `Note updated: ${change.summary}`, pages: change, at: new Date().toISOString(), + }), context.signal, { attempt: actualTurn, cwd: codingCwd, isCurrent: () => context.accepting(), observed: receipt => context.observedNotes(receipt) }); + privacy.addConnection(pages.connection.url); + } + if (!knowledge && !local && thread.backend !== "mock" && this.o.knowledge) { + knowledge = await this.o.knowledge.prepare(connectionTurn, change => this.addItem(threadId, { + id: newId(), kind: "notice", level: "info", text: `Knowledge updated: ${change.summary}`, knowledge: change, at: new Date().toISOString(), + }), abort.signal); + if (knowledge.warning) attempt.sink.notice("warn", knowledge.warning); + } + if (abort.signal.aborted) { context.seal(); privacy.finish(); attempt.end("interrupted"); return { status: "interrupted" }; } + const turnFiles: TurnAttachment[] = files.map((f) => { + const file = this.attachments.resolve(f.rel); + if (!file) throw new Error(`Attachment ${f.name} is missing or changed. Attach it again before retrying.`); + return { name: f.name, mime: f.mime, kind: f.kind, path: file }; + }); + let result = privacy.redact(await backend.runTurn( + describeAttachments(describeTaskContext(text, contextAdmission?.context ?? (l.items.find(item => item.id === turnId && item.kind === "user") as UserItem | undefined)?.contextEvidence), turnFiles), { cwd: ran.cwd, mode: ran.mode, plan: ran.plan, model: ran.model, effort: ran.effort, fast, ...(conserveCredits ? { conserveCredits: true } : {}), resume: ran.sessionHandle, account: accountFor(ran), // Files the CLI reads by path sit outside cwd; --add-dir keeps that from prompting. addDirs: turnFiles.some((f) => f.kind === "file") ? [path.join(this.attachments.root, threadId)] : [], - ...(turnFiles.length ? { attachments: turnFiles } : {}), ...(knowledge ? { knowledge: knowledge.connection } : {}), ...(pages ? { pages: pages.connection } : {}), ...(images ? { images: images.connection } : {}), + ...(turnFiles.length ? { attachments: turnFiles } : {}), ...(knowledge ? { knowledge: knowledge.connection } : {}), ...(pages ? { pages: pages.connection } : {}), ...(taskContext ? { taskContext: taskContext.connection } : {}), ...(images ? { images: images.connection } : {}), }, - attempt.sink, + { ...attempt.sink, taskContextCapability: reportCapability, protectCredential: value => { if (context.accepting()) privacy.addCredential(value); }, notice: (level, text) => { + if (nativeSettled) return; + if (publicationFailure) { retainShutdownNotice(level, text); return; } + try { attempt.sink.notice(level, text); } catch (error) { failPublication(error, "publish native notice"); } + } }, abort.signal, - ); - attempt.end(abort.signal.aborted ? "interrupted" : ATTEMPT_OUTCOME[result.status]); + )); + nativeSettled = true; + // Preserve this independently of later persistence: a failed journal cannot prove exit. + if (result.stop?.outcome === "unconfirmed" && result.stop.pid) (l.unconfirmed ??= new Set()).add(result.stop.pid); + if (!publicationFailure && result.taskContextCapability) reportCapability(result.taskContextCapability); + if (publicationFailure) { + failed = true; failure = publicationFailure.error; + result = { ...result, status: "failed", error: publicationFailure.error.message, detail: { ...result.detail, stage: publicationFailure.stage, shutdownNotices: shutdownNotices.map(notice => ({ ...notice })), ...(shutdownTruncated ? { shutdownTruncated: true } : {}) }, taskContextCapability: { state: "unavailable", reason: "configuration-not-bound" } }; + } + context.seal(); privacy.finish(); + attempt.end(publicationFailure ? "failed" : abort.signal.aborted ? "interrupted" : ATTEMPT_OUTCOME[result.status]); + for (const notice of shutdownNotices) this.addItem(threadId, { id: newId(), kind: "notice", ...notice, at: new Date().toISOString() }); + if (shutdownTruncated) this.addItem(threadId, { id: newId(), kind: "notice", level: "warn", text: "Some native shutdown diagnostics could not be retained.", at: new Date().toISOString() }); + if (taskContext) { const closing = taskContext; taskContext = undefined; await closing.close(); } + if (pages) { const closing = pages; pages = undefined; await closing.close(); } + const drain = context.flush(); + if (drain) await drain; + if (publicationFailure) result = { ...result, status: "failed", error: publicationFailure.error.message, detail: { ...result.detail, stage: publicationFailure.stage }, taskContextCapability: { state: "unavailable", reason: "configuration-not-bound" } }; return result; } catch (err) { + failed = true; failure = err; + context.seal(); privacy.finish(); attempt.end(abort.signal.aborted ? "interrupted" : "failed"); - throw err; + throw privacy.error(err); + } finally { + nativeSettled = true; + context.seal(); privacy.finish(); + context.close(); + this.contextAttempts.delete(actualTurn.attempt); + const closing = [taskContext, pages]; taskContext = undefined; pages = undefined; + const closed = await Promise.allSettled(closing.filter(lease => lease !== undefined).map(lease => Promise.resolve().then(() => lease.close()))); + const errors = closed.flatMap(result => result.status === "rejected" ? [privacy.error(result.reason).message] : []); + if (errors.length) throw new Error(`${failed ? `${privacy.error(failure).message} ` : ""}Connection cleanup failed: ${errors.join("; ")}`); } }; let replacementBlocked = false; @@ -1046,7 +1273,7 @@ export class ThreadRunner { this.setStatus(threadId, "idle"); } } catch (err) { - if (abort.signal.aborted) { + if (abort.signal.aborted && !publicationAborted) { sink.notice("info", "Stopped."); this.setStatus(threadId, "idle"); } else { @@ -1058,6 +1285,7 @@ export class ThreadRunner { await Promise.all([ images?.close().catch(err => sink.notice("warn", `Images connection could not close: ${(err as Error).message}`)), pages?.close().catch(err => sink.notice("warn", `Pages connection could not close: ${(err as Error).message}`)), + taskContext?.close().catch(err => sink.notice("warn", `Task context connection could not close: ${(err as Error).message}`)), knowledge?.close().catch(err => sink.notice("warn", `Knowledge connection could not close: ${(err as Error).message}`)), ]); // Attempts settle their own items. An item from before the conversation contract can still be @@ -1133,6 +1361,7 @@ export class ThreadRunner { } stop(threadId: string): void { + this.contextDrafts.cancelThreadCaptures(threadId); const l = this.live.get(threadId); if (!l) return; l.followUp?.abort.abort(); @@ -1447,7 +1676,7 @@ export class ThreadRunner { * with an id, its ingress order and time, and projects it into transcript items. Starting an * attempt makes every earlier attempt's sink inert. */ - private beginAttempt(threadId: string, l: Live, turn: TurnRef): { sink: TurnSink; end: (outcome: SettledOutcome) => void } { + private beginAttempt(threadId: string, l: Live, turn: TurnRef, privacy?: ContextPrivacy, publicationFailed?: (error: unknown) => void): { sink: TurnSink; end: (outcome: SettledOutcome) => void } { l.attempt = turn.attempt; let seq = 0; const seen = new Set(); @@ -1462,17 +1691,19 @@ export class ThreadRunner { if (seen.has(observation.source)) return; seen.add(observation.source); } - this.project(threadId, l, stamp(redactObservation(observation))); + const original = privacy?.original(observation); + try { this.project(threadId, l, stamp(redactObservation(observation)), original?.args ?? (observation.type === "tool.start" ? observation.tool.args : observation.type === "tool.done" ? observation.progress?.args : undefined), original?.exec); } + catch (error) { if (publicationFailed) publicationFailed(error); else throw error; } }); this.project(threadId, l, stamp({ type: "attempt.start" })); - return { sink, end: (outcome) => { + return { sink: privacy ? privacy.wrap(sink) : sink, end: (outcome) => { this.toolLogs.endThread(threadId); if (this.live.get(threadId) === l) this.project(threadId, l, stamp({ type: "attempt.end", outcome })); } }; } /** Applies one event to the turn's conversation and emits what changed as item events. */ - private project(threadId: string, l: Live, event: ConversationEvent): void { + private project(threadId: string, l: Live, event: ConversationEvent, nativeArgs?: Record, nativeExec?: Exec): void { const before = l.conversation; const after = applyConversationEvent(before, event); if (after === before) return; @@ -1486,6 +1717,27 @@ export class ThreadRunner { const patch = Object.fromEntries(Object.entries(item).filter(([key, value]) => (old as Record)[key] !== value)); this.patchItem(threadId, item.id, patch as Partial, { persist }); }); + if (event.type === "tool.start" || event.type === "tool.done") { + const id = toolItemId(after, event.turn, event.native); + const item = id ? l.items.find(item => item.id === id) : undefined; + if (item?.kind === "tool") this.contextAttempts.get(event.turn.attempt)?.accepted(event, item, nativeArgs, nativeExec); + } + if (event.type === "plan.update") this.contextAttempts.get(event.turn.attempt)?.planChanged(); + } + + /** Context publication is transactional: failed storage never changes live metadata or emits success. */ + private updateTaskContext(threadId: string, l: Live, turn: TurnRef, projectId: string, change: (context: TaskContextEvidence) => void): void { + const index = l.items.findIndex(item => item.kind === "user" && item.id === turn.turn); + const user = l.items[index]; + if (user?.kind !== "user") throw new Error("Task context user item is missing."); + const next = user.contextEvidence ? copyTaskContextEvidence(user.contextEvidence) : { revision: 0, refs: [], captures: [], checks: [] }; + change(next); + const contextEvidence = copyTaskContextEvidence(next); + if (!taskContextBelongsTo(contextEvidence, threadId, turn.turn, projectId)) throw new Error("Task context scope changed."); + const items = l.items.map((item, i) => i === index ? { ...user, contextEvidence } : item); + this.o.store.saveItems(threadId, items); + l.items = items; + this.o.emit({ threadId, type: "item_update", id: user.id, patch: { contextEvidence } }); } /** A sink for the running turn. One bound to `attempt` goes inert, every method of it, once another attempt starts. */ diff --git a/apps/desktop/src/main/engine/task-context-agents.ts b/apps/desktop/src/main/engine/task-context-agents.ts new file mode 100644 index 00000000..a3e18c69 --- /dev/null +++ b/apps/desktop/src/main/engine/task-context-agents.ts @@ -0,0 +1,197 @@ +import type { TurnRef } from "../../shared/conversation-events.js"; +import type { TaskContextConnection, TaskContextProposal, TaskContextProposalResult } from "../../shared/task-context-tools.js"; +import type { Store } from "./store.js"; +import http from "node:http"; +import fs from "node:fs"; +import fsp from "node:fs/promises"; +import path from "node:path"; +import { randomUUID } from "node:crypto"; +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; +import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; +import { copyTaskContextEvidence, type ContextTarget } from "../../shared/task-context-evidence.js"; + +export interface TaskContextBinding { + turn: TurnRef; projectId: string; cwd: string; isCurrent(): boolean; + accept(proposal: TaskContextProposal): TaskContextProposalResult; +} +export interface TaskContextLease { connection: TaskContextConnection; close(): Promise } +interface Active { + token: string; leaseId: string; binding: TaskContextBinding; identity: string; closed: AbortController; + requests: Set; records: Map }>; bytes: number; + detach?(): void; +} +const BODY_BYTES = 64 * 1024, LEASE_BYTES = 2 * 1024 * 1024; +const uuid = (value: unknown): value is string => typeof value === "string" && /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(value); +const record = (value: unknown): value is Record => typeof value === "object" && value !== null && !Array.isArray(value); +const exact = (value: Record, keys: string[]) => Object.keys(value).length === keys.length && keys.every(key => Object.hasOwn(value, key)); +const denied = () => new Error("Proposal scope is unavailable or no longer belongs to this attempt."); +const inside = (root: string, file: string) => { const rel = path.relative(root, file); return rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel); }; + +/** Bounded metadata reads only. No Notes open/refresh/repair, Markdown, plain-file body or mutation API. */ +function metadata(file: string, root: string, limit: number): unknown { + if (!inside(root, file) || fs.realpathSync(file) !== file) throw denied(); + const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK); + try { + const before = fs.fstatSync(fd); + if (!before.isFile() || before.size > limit) throw denied(); + const buffer = Buffer.alloc(before.size + 1); let bytes = 0; + while (bytes < buffer.length) { const read = fs.readSync(fd, buffer, bytes, buffer.length - bytes, bytes); if (!read) break; bytes += read; } + const after = fs.fstatSync(fd); + if (bytes !== before.size || after.ino !== before.ino || after.dev !== before.dev || after.size !== before.size || after.mtimeMs !== before.mtimeMs || after.ctimeMs !== before.ctimeMs) throw denied(); + return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, bytes))); + } finally { fs.closeSync(fd); } +} +function notesInScope(notesRoot: string, projectId: string, targets: Exclude[]): void { + if (!targets.length) return; + const root = fs.realpathSync(notesRoot); + if (root !== path.resolve(notesRoot)) throw denied(); + const index = metadata(path.join(root, ".modex", "index.json"), root, 8 * 1024 * 1024); + if (!record(index) || index.version !== 1 || !record(index.notes)) throw denied(); + const markers = new Map(); + for (const target of targets) { + const entries = target.kind === "note" ? index.notes : index.files, id = target.kind === "note" ? target.pageId : target.id; + const entry = record(entries) && Object.hasOwn(entries, id) ? entries[id] : undefined; + if (!record(entry) || typeof entry.rel !== "string" || !Number.isSafeInteger(entry.ino) || entry.trash || !entry.rel || /[\\\u0000-\u001f\u007f]/.test(entry.rel) + || path.isAbsolute(entry.rel) || entry.rel.split("/").some(part => !part || part === "." || part === "..")) throw denied(); + const file = path.join(root, entry.rel); + if (!inside(root, file) || fs.realpathSync(file) !== file) throw denied(); + const stat = fs.lstatSync(file); if (!stat.isFile() || stat.ino !== entry.ino) throw denied(); + let directory = path.dirname(file), found = false; + for (let depth = 0; depth <= 30 && directory !== root; depth++, directory = path.dirname(directory)) { + let marker = markers.get(directory); + if (!markers.has(directory)) { + try { marker = metadata(path.join(directory, ".modex-project.json"), root, 64 * 1024); } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; marker = null; } + markers.set(directory, marker); + } + if (marker !== null) { if (!record(marker) || marker.modexProjectId !== projectId) throw denied(); found = true; break; } + } + if (!found) throw denied(); + } +} + +/** A dedicated loopback capability: records uninspected relevance proposals only. */ +export class TaskContextAgents { + private readonly routes = new Map(); + private readonly server: http.Server; + private listening?: Promise; + private disposed = false; + constructor(private readonly options: { store: Store }) { + this.server = http.createServer((req, res) => { void this.handle(req, res).catch(() => { if (!res.headersSent) res.writeHead(500); res.end(); }); }); + } + private identity(binding: TaskContextBinding): string { + const thread = this.options.store.thread(binding.turn.thread), project = this.options.store.project(binding.projectId); + const account = thread?.backend === "claude" ? thread.claudeAccount ?? "cli" : thread?.backend === "codex" ? thread.codexAccount ?? "cli" : undefined; + if (!thread || !project || thread.retired || thread.localTarget || !["claude", "codex"].includes(thread.backend) || thread.backend !== binding.turn.backend + || thread.projectId !== project.id || thread.cwd !== binding.cwd || account !== binding.turn.account) throw denied(); + return JSON.stringify([project.id, project.path, project.addedAt, thread.id, thread.createdAt, thread.cwd, thread.backend, account]); + } + private current(active: Active, signal?: AbortSignal): void { + if (this.disposed || active.closed.signal.aborted || signal?.aborted || !active.binding.isCurrent() || this.identity(active.binding) !== active.identity) throw denied(); + } + async prepare(binding: TaskContextBinding, signal: AbortSignal): Promise { + const bound = { ...binding, turn: structuredClone(binding.turn) }, identity = this.identity(bound); + // Validate the same public turn identity persisted in proposal provenance, before opening any route. + copyTaskContextEvidence({ revision: 0, refs: [{ id: "identity", target: { kind: "file", path: "identity" }, captureIds: [], relations: [{ kind: "relevant", provenance: "model-proposed", + source: { kind: "model-proposal", turn: bound.turn, leaseId: "identity", receiptId: "identity", requestId: "identity", at: new Date().toISOString() } }] }], captures: [], checks: [] }); + const active: Active = { token: randomUUID(), leaseId: randomUUID(), binding: bound, identity, closed: new AbortController(), requests: new Set(), records: new Map(), bytes: 0 }; + const close = () => { active.closed.abort(); this.routes.delete(active.token); for (const req of active.requests) req.destroy(); active.records.clear(); signal.removeEventListener("abort", close); }; + active.detach = () => signal.removeEventListener("abort", close); + signal.addEventListener("abort", close, { once: true }); + try { + this.current(active, signal); + const base = await (this.listening ??= new Promise((resolve, reject) => { + this.server.once("error", reject); this.server.listen(0, "127.0.0.1", () => { this.server.removeListener("error", reject); resolve(`http://127.0.0.1:${(this.server.address() as import("node:net").AddressInfo).port}`); }); + })); + this.current(active, signal); + this.routes.set(active.token, active); + return { connection: { url: `${base}/mcp/${active.token}`, instructions: "modex_task_context.propose_refs records candidate project file/test/Note references as model-proposed relevance only. It reads no bodies, runs no tools, verifies no checks and grants no permissions. Supply refs with target and role only; omit raw content, rationale, origins and private paths." }, close: async () => { close(); } }; + } catch (error) { close(); throw error; } + } + private async handle(req: http.IncomingMessage, res: http.ServerResponse): Promise { + const port = (this.server.address() as import("node:net").AddressInfo | null)?.port; + if (req.headers.origin !== undefined || req.headers.host !== `127.0.0.1:${port}`) { res.writeHead(403).end(); return; } + const active = [...this.routes.values()].find(route => req.url === `/mcp/${route.token}`); + if (!active || this.disposed) { res.writeHead(404).end(); return; } + active.requests.add(req); res.once("close", () => active.requests.delete(req)); + const mcp = new Server({ name: "modex-task-context", version: "1" }, { capabilities: { tools: {} } }); + const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true }); + res.on("close", () => { void transport.close(); void mcp.close(); }); + mcp.setRequestHandler(ListToolsRequestSchema, async () => { + this.current(active); + return { tools: [{ name: "propose_refs", description: "Propose uninspected context relevance; no reads, writes, execution, verification or permission changes.", inputSchema: { type: "object", additionalProperties: false, required: ["refs"], properties: { refs: { type: "array", minItems: 1, maxItems: 100, items: { type: "object", additionalProperties: false, required: ["target", "role"], properties: { target: { oneOf: [ + { type: "object", additionalProperties: false, required: ["kind", "path"], properties: { kind: { const: "file" }, path: { type: "string", minLength: 1, maxLength: 4096 } } }, + { type: "object", additionalProperties: false, required: ["kind", "pageId"], properties: { kind: { const: "note" }, pageId: { type: "string", minLength: 1, maxLength: 512 } } }, + { type: "object", additionalProperties: false, required: ["kind", "id"], properties: { kind: { const: "notes-file" }, id: { type: "string", minLength: 1, maxLength: 512 } } }, + ] }, role: { type: "string", enum: ["file", "test", "note"] } } } } } } }] }; + }); + mcp.setRequestHandler(CallToolRequestSchema, async (request, extra) => { + try { + if (request.params.name !== "propose_refs") throw new Error("Only propose_refs is available."); + const result = await this.propose(active, request.params.arguments, extra.requestId, extra.signal); + return { content: [{ type: "text", text: JSON.stringify(result) }] }; + } catch (error) { return { isError: true, content: [{ type: "text", text: (error as Error).message }] }; } + }); + try { + this.current(active); + if (Number(req.headers["content-length"]) > BODY_BYTES) { res.writeHead(413).end(); req.resume(); return; } + const chunks: Buffer[] = []; let bytes = 0; + for await (const chunk of req) { bytes += chunk.length; if (bytes > BODY_BYTES) { res.writeHead(413).end(); return; } chunks.push(Buffer.from(chunk)); } + this.current(active); + const body = chunks.length ? JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks))) : undefined; + // JSON-RPC echoes request IDs even in errors. Reject a private token before the SDK can reflect it. + if (body !== undefined && JSON.stringify(body).includes(active.token)) { res.writeHead(400).end(); return; } + await mcp.connect(transport); await transport.handleRequest(req, res, body); + } catch { if (!res.headersSent && !res.destroyed) res.writeHead(400).end(); } + } + private async propose(active: Active, args: unknown, requestId: string | number, signal: AbortSignal): Promise { + this.current(active, signal); + if ((typeof requestId !== "string" && typeof requestId !== "number") || (typeof requestId === "string" && (!requestId || requestId.length > 512 || /[\u0000-\u001f\u007f\ud800-\udfff]/u.test(requestId) || /^(?:[\\/]|[A-Za-z]:[\\/])/.test(requestId) || requestId.includes(active.token))) + || (typeof requestId === "number" && !Number.isSafeInteger(requestId))) throw new Error("Invalid proposal request identity."); + if (!record(args) || !exact(args, ["refs"]) || !Array.isArray(args.refs) || !args.refs.length || args.refs.length > 100) throw new Error("Invalid proposal arguments."); + const refs: TaskContextProposal["refs"] = args.refs.map(ref => { + if (!record(ref) || !exact(ref, ["target", "role"]) || !["file", "test", "note"].includes(ref.role as string)) throw new Error("Invalid proposal reference."); + try { copyTaskContextEvidence({ revision: 0, refs: [{ id: "target", target: ref.target as ContextTarget, relations: [], captureIds: [] }], captures: [], checks: [] }); } + catch { throw new Error("Invalid proposal target."); } + const target = ref.target as ContextTarget; + if ((target.kind === "file") === (ref.role === "note") || /[\ud800-\udfff]/u.test(JSON.stringify(target)) || JSON.stringify(target).includes(active.token)) throw new Error("Invalid proposal target or role."); + return { target: target.kind === "file" ? { kind: "file", path: target.path } : target.kind === "note" ? { kind: "note", pageId: target.pageId } : { kind: "notes-file", id: target.id }, role: ref.role as "file" | "test" | "note" }; + }); + const canonical = JSON.stringify(refs), key = JSON.stringify(requestId), prior = active.records.get(key); + if (prior) { if (prior.canonical !== canonical) throw new Error("Proposal request identity was reused with changed references."); return prior.result; } + const proposal: TaskContextProposal = { receiptId: randomUUID(), leaseId: active.leaseId, requestId, at: new Date().toISOString(), refs }; + const reservation = Buffer.byteLength(JSON.stringify(proposal)) + 512 + refs.length * 40; + if (active.bytes + reservation > LEASE_BYTES) throw new Error("This attempt's proposal metadata budget is exhausted."); + active.bytes += reservation; + const result = (async () => { + try { + const root = await fsp.realpath(active.binding.cwd); + for (const ref of refs) if (ref.target.kind === "file") { + let candidate = path.join(root, ref.target.path); + while (true) { + try { if (!inside(root, await fsp.realpath(candidate))) throw denied(); break; } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT" || candidate === root) throw error; candidate = path.dirname(candidate); } + } + } + if (await fsp.realpath(active.binding.cwd) !== root) throw denied(); + this.current(active, signal); + notesInScope(this.options.store.space.root, active.binding.projectId, refs.flatMap(ref => ref.target.kind === "file" ? [] : [ref.target])); + } catch { throw denied(); } + this.current(active, signal); + let accepted: TaskContextProposalResult; + try { accepted = active.binding.accept(structuredClone(proposal)); } + catch { throw new Error("Proposal could not be recorded; its outcome is unknown. Do not resend automatically."); } + if (!accepted || accepted.receiptId !== proposal.receiptId || !Array.isArray(accepted.refIds) || !accepted.refIds.every(uuid) || new Set(accepted.refIds).size !== accepted.refIds.length + || accepted.accepted !== accepted.refIds.length || accepted.accepted > refs.length) throw new Error("Proposal result is unavailable; do not resend automatically."); + return { receiptId: accepted.receiptId, refIds: [...accepted.refIds], accepted: accepted.accepted }; + })(); + active.records.set(key, { canonical, result }); void result.catch(() => {}); return result; + } + async dispose(): Promise { + this.disposed = true; + for (const active of this.routes.values()) { active.closed.abort(); active.detach?.(); active.records.clear(); for (const req of active.requests) req.destroy(); } + this.routes.clear(); this.server.closeAllConnections(); + if (this.listening) await new Promise(resolve => this.server.close(() => resolve())); + } +} diff --git a/apps/desktop/src/main/engine/task-context-attempt.ts b/apps/desktop/src/main/engine/task-context-attempt.ts new file mode 100644 index 00000000..067e2f76 --- /dev/null +++ b/apps/desktop/src/main/engine/task-context-attempt.ts @@ -0,0 +1,276 @@ +import path from "node:path"; +import { randomUUID } from "node:crypto"; +import type { ConversationEvent, TurnRef } from "../../shared/conversation-events.js"; +import type { ThreadItem } from "../../shared/types.js"; +import { contextRefId, type ContextCapture, type ContextRelation, type ContextTarget, type TaskContextEvidence } from "../../shared/task-context-evidence.js"; +import type { ExecutionScope, TaskContextCapture } from "./task-context-capture.js"; +import type { PagesReadReceipt } from "./pages-agents.js"; +import { isTaskContextCapability, type TaskContextCapability, type TaskContextProposal, type TaskContextProposalResult } from "../../shared/task-context-tools.js"; +import type { Exec } from "../../shared/evidence.js"; +import { checkDirectoryChanges, isCheckCommand, mayWrite } from "../../shared/task-context.js"; + +type Tool = Extract; +type Operation = { target: ContextTarget; kind: "read" | "changed" | "searched" }; +const sameTurn = (a: TurnRef, b: TurnRef) => a.thread === b.thread && a.turn === b.turn && a.attempt === b.attempt && a.backend === b.backend && a.account === b.account; + +/** One real runtime attempt. Async observations may only update its still-authoritative user item. */ +export class TaskContextAttempt { + private readonly abort = new AbortController(); + readonly signal: AbortSignal; + private readonly native = new Set(); + /** Normalized targets only, never retained raw arguments or tool bodies. */ + private readonly requested = new Map(); + private readonly notes = new Map(); + private readonly proposals = new Map(); + private readonly pending = new Set>(); + private readonly errors: unknown[] = []; + private readonly checkScopes = new Map(); + private sealed = false; + private lastCapability?: string; + constructor(private readonly o: { + turn: TurnRef; cwd: string; projectId: string; signal: AbortSignal; + capture: Pick; + privateValue?(value: unknown): boolean; + current(): boolean; + update(change: (context: TaskContextEvidence) => void): void; + read(): TaskContextEvidence; + planRevision(): number | undefined; + publicationFailed?(error: unknown): void; + }) { this.signal = AbortSignal.any([o.signal, this.abort.signal]); } + + current(): boolean { return !this.signal.aborted && this.o.current(); } + accepting(): boolean { return !this.sealed && this.current(); } + seal(): void { this.sealed = true; } + close(): void { this.abort.abort(); } + capability(value: TaskContextCapability): void { + if (!this.accepting()) return; + if (!isTaskContextCapability(value)) throw new Error("Invalid task context capability report."); + const signature = JSON.stringify(value); + if (this.lastCapability === signature) return; + this.change(context => { + const receipts = context.capabilities ??= [], index = receipts.findIndex(c => c.turn.attempt === this.o.turn.attempt); + const receipt = { turn: structuredClone(this.o.turn), at: new Date().toISOString(), capability: structuredClone(value) }; + if (index < 0) receipts.push(receipt); else receipts[index] = receipt; + }); + this.lastCapability = signature; + } + flush(): Promise | undefined { + if (!this.pending.size) { if (this.errors.length) throw this.errors[0]; return; } + return Promise.all(this.pending).then(() => { if (this.errors.length) throw this.errors[0]; }); + } + + private change(change: (context: TaskContextEvidence) => void): void { + if (!this.current()) return; + try { this.o.update(context => { change(context); context.revision++; }); } + catch (error) { this.o.publicationFailed?.(error); throw error; } + } + private ref(context: TaskContextEvidence, target: ContextTarget, relation: ContextRelation) { + let ref = context.refs.find(ref => contextRefId(ref.target) === contextRefId(target)); + if (!ref) { ref = { id: randomUUID(), target, relations: [], captureIds: [] }; context.refs.push(ref); } + if (!ref.relations.some(existing => JSON.stringify(existing) === JSON.stringify(relation))) ref.relations.push(relation); + return ref; + } + private operations(item: Tool): Operation[] { + // Only structured native arguments/actions count. Never parse model prose or shell output. + const operations: Operation[] = []; + const add = (value: unknown, kind: "read" | "changed" | "searched", cwd?: string) => { + if (typeof value !== "string" || !value || /[\u0000-\u001f\u007f\\]/.test(value) || (!path.isAbsolute(value) && !cwd)) return; + if (this.o.privateValue?.(value)) return; + const relative = path.relative(this.o.cwd, path.resolve(cwd ?? this.o.cwd, value)); + if (!relative || relative.length > 4096 || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative) || /^[A-Za-z]:/.test(relative)) return; + const target: ContextTarget = { kind: "file", path: relative.split(path.sep).join("/") }; + if (!operations.some(op => op.kind === kind && contextRefId(op.target) === contextRefId(target))) operations.push({ target, kind }); + }; + if (this.o.turn.backend === "claude") { + if (item.name === "Read" && !item.unavailableInputs?.includes("args.file_path")) add(item.args.file_path, "read", this.o.cwd); + else if (["Write", "Edit", "MultiEdit"].includes(item.name) && !item.unavailableInputs?.includes("args.file_path")) add(item.args.file_path, "changed", this.o.cwd); + else if (item.name === "NotebookEdit" && !item.unavailableInputs?.includes("args.notebook_path")) add(item.args.notebook_path, "changed", this.o.cwd); + else if (["Grep", "Glob"].includes(item.name) && !item.unavailableInputs?.includes("args.path")) add(item.args.path, "searched", this.o.cwd); + } + if (this.o.turn.backend === "codex") { + for (const [i, action] of (item.exec?.actions ?? []).entries()) { + if (!item.unavailableInputs?.includes(`exec.actions.${i}.path`)) add(action.path, action.kind === "read" ? "read" : "searched", item.unavailableInputs?.includes("exec.cwd") ? undefined : item.exec?.cwd); + } + if (item.name === "apply_patch" && Array.isArray(item.args.files)) { + for (const [i, file] of item.args.files.entries()) if (file && typeof file === "object") { + if (!item.unavailableInputs?.includes(`args.files.${i}.path`)) add(file.path, "changed", this.o.cwd); + if (!item.unavailableInputs?.includes(`args.files.${i}.movePath`)) add(file.movePath, "changed", this.o.cwd); + } + } + } + return operations; + } + planChanged(): void { + if (!this.accepting() || !this.o.read().checks.length) return; + this.change(context => { for (const check of context.checks) check.staleness = "stale"; }); + } + private checkAccepted(event: ConversationEvent, item: Tool, nativeExec?: Exec): void { + if (event.type !== "tool.start" && event.type !== "tool.done") return; + const exec = nativeExec ?? item.exec, command = exec?.command; + if (!command || !item.origin) return; + if (item.unavailableInputs?.includes("exec.command") || this.o.privateValue?.(command)) { + if (event.type === "tool.done" && event.outcome !== "denied") this.planChanged(); + return; + } + if (!isCheckCommand(command)) { + if (event.type === "tool.done" && event.outcome !== "denied" && mayWrite(command) && this.o.read().checks.length) this.planChanged(); + return; + } + const source = { kind: "native" as const, itemId: item.id, origin: item.origin }; + const phase = event.type === "tool.start" ? "check-start-observed" : "check-end-observed"; + const placeholders: ContextCapture[] = []; + const changes = checkDirectoryChanges(command); + const cwd = exec?.cwd && path.isAbsolute(exec.cwd) && !item.unavailableInputs?.includes("exec.cwd") && changes ? path.resolve(exec.cwd) : undefined; + let lexical: ExecutionScope = cwd === undefined ? "unknown" : "project"; + if (cwd !== undefined && changes) { + let directory = cwd; + for (let i = 0; i <= changes.length; i++) { + if (i) directory = path.resolve(directory, changes[i - 1]!); + const relative = path.relative(this.o.cwd, directory); + if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) { lexical = "outside"; break; } + } + } + const scopeState = this.checkScopes.get(item.id) ?? { pending: 0, values: [] }; + this.checkScopes.set(item.id, scopeState); scopeState.pending++; + this.change(context => { + let check = context.checks.find(c => c.itemId === item.id); + if (!check) { + const planRevision = this.o.planRevision(); + check = { itemId: item.id, origin: item.origin, eventIds: [], command, outcome: "running", scope: "unknown", + relevantRefIds: [], relevance: "unestablished", startCaptureIds: [], endCaptureIds: [], staleness: "unknown", + ...(planRevision === undefined ? {} : { planRevision }) }; + context.checks.push(check); + } + check.scope = scopeState.values.includes("outside") ? "outside" : "unknown"; + check.eventIds.push(event.id); + if (event.type === "tool.start") check.startedAt = event.at; + else { + check.finishedAt = event.at; check.outcome = event.outcome; + if (item.exitCode !== undefined) check.exitCode = item.exitCode; + // Millisecond timestamps can tie even when the start read is still queued. Preserve + // that missed boundary rather than retroactively promoting a late hash on settlement. + if (check.staleness !== "stale") check.staleness = check.startedAt && check.startCaptureIds.length + && check.startCaptureIds.every(id => context.captures.some(c => c.id === id && c.revision.state === "available")) ? "current-observation" : "unknown"; + } + // An unconfirmed model proposal never causes a filesystem or Notes body read. + const refs = event.type === "tool.start" + ? context.refs.filter(ref => ref.relations.some(r => r.provenance === "user-confirmed" || r.provenance === "observed")) + : context.refs.filter(ref => ref.captureIds.some(id => check!.startCaptureIds.includes(id))); + for (const ref of refs) { + const capture: ContextCapture = { id: randomUUID(), target: ref.target, source, phase, startedAt: event.at, finishedAt: event.at, + repository: { state: "unavailable", reason: "not-captured" }, revision: { state: "unavailable", reason: "not-captured" }, + workspace: { kind: "thread", projectId: this.o.projectId, threadId: this.o.turn.thread } }; + placeholders.push(capture); context.captures.push(capture); ref.captureIds.push(capture.id); + (event.type === "tool.start" ? check.startCaptureIds : check.endCaptureIds).push(capture.id); + } + }); + const scopePending = Promise.resolve().then(() => lexical === "project" && cwd !== undefined + ? this.o.capture.executionScope(this.o.cwd, cwd, this.signal, changes ?? []) : lexical).then(scope => { + if (!this.current()) return; + scopeState.pending--; scopeState.values.push(scope); + this.change(context => { + const check = context.checks.find(c => c.itemId === item.id); + if (check) check.scope = scopeState.values.includes("outside") ? "outside" + : scopeState.pending || scopeState.values.includes("unknown") ? "unknown" : "project"; + }); + }).catch(error => { if (this.current()) this.errors.push(error); }).finally(() => { this.pending.delete(scopePending); }); + this.pending.add(scopePending); + if (!placeholders.length) return; + const pending = this.o.capture.capture(this.o.cwd, placeholders.map(c => c.target), source, phase, this.signal).then(captures => { + if (!this.current()) return; + this.change(context => { + for (const placeholder of placeholders) { + const capture = captures.find(c => contextRefId(c.target) === contextRefId(placeholder.target)); + const index = context.captures.findIndex(c => c.id === placeholder.id); + if (capture && index >= 0) context.captures[index] = { ...capture, id: placeholder.id, workspace: placeholder.workspace }; + } + }); + }).catch(error => { if (this.current()) this.errors.push(error); }).finally(() => { this.pending.delete(pending); }); + this.pending.add(pending); + } + accepted(event: ConversationEvent, item: Tool, nativeArgs?: Record, nativeExec?: Exec): void { + if (!this.accepting() || !item.origin || !sameTurn(item.origin, this.o.turn) || (event.type !== "tool.start" && event.type !== "tool.done")) return; + const key = JSON.stringify([item.id, event.type]); + if (this.native.has(key)) return; + this.checkAccepted(event, item, nativeExec); + this.native.add(key); + if (event.type === "tool.done" && event.outcome !== "denied" && ["Write", "Edit", "MultiEdit", "NotebookEdit", "apply_patch"].includes(item.name) && this.o.read().checks.length) this.planChanged(); + // Display previews may truncate strings; derive identities from accepted native arguments instead. + const operations = event.type === "tool.done" && nativeArgs === undefined + ? this.requested.get(item.id) ?? [] + : this.operations({ ...item, ...(nativeArgs === undefined ? {} : { args: nativeArgs }), ...(nativeExec === undefined ? {} : { exec: nativeExec }) }); + if (!operations.length) return; + const targets = [...new Map(operations.map(op => [contextRefId(op.target), op.target])).values()]; + const source = { kind: "native" as const, itemId: item.id, origin: item.origin }; + const succeeded = event.type === "tool.done" && event.outcome === "succeeded"; + const placeholders: ContextCapture[] = succeeded ? targets.map(target => ({ + id: randomUUID(), target, source, phase: "after-native-result", startedAt: event.at, finishedAt: event.at, + repository: { state: "unavailable", reason: "not-captured" }, revision: { state: "unavailable", reason: "not-captured" }, + workspace: { kind: "thread", projectId: this.o.projectId, threadId: this.o.turn.thread }, + })) : []; + this.change(context => { + for (const { target, kind } of operations) { + this.ref(context, target, { kind, provenance: "native-requested", source }); + if (succeeded) { + this.ref(context, target, { kind, provenance: "observed", source }); + } + } + for (const placeholder of placeholders) { + const ref = context.refs.find(ref => contextRefId(ref.target) === contextRefId(placeholder.target))!; + ref.captureIds.push(placeholder.id); context.captures.push(placeholder); + } + }); + this.native.add(key); + if (event.type === "tool.start") this.requested.set(item.id, operations); + if (!succeeded) return; + const pending = this.o.capture.capture(this.o.cwd, targets, source, "after-native-result", this.signal).then(captures => { + if (!this.current()) return; + this.change(context => { + for (const placeholder of placeholders) { + const replacement = captures.find(c => contextRefId(c.target) === contextRefId(placeholder.target)); + const index = context.captures.findIndex(c => c.id === placeholder.id); + if (replacement && index >= 0) context.captures[index] = { ...replacement, id: placeholder.id, workspace: placeholder.workspace }; + } + }); + }).catch(error => { if (this.current()) this.errors.push(error); }).finally(() => { this.pending.delete(pending); }); + this.pending.add(pending); + } + observedNotes(receipt: PagesReadReceipt): void { + if (!this.accepting()) throw new Error("Task context attempt is no longer accepting Notes receipts."); + if (this.o.privateValue?.(receipt)) throw new Error("Notes context receipt contains a private connection."); + if (!sameTurn(receipt.source.turn, this.o.turn) || receipt.captures.some(c => c.phase !== "after-notes-read" || JSON.stringify(c.source) !== JSON.stringify(receipt.source) + || c.workspace?.kind !== "thread" || c.workspace.projectId !== this.o.projectId || c.workspace.threadId !== this.o.turn.thread)) throw new Error("Notes context receipt scope changed."); + const key = receipt.source.receiptId, signature = JSON.stringify(receipt); + if (this.notes.has(key)) { if (this.notes.get(key) !== signature) throw new Error("Notes receipt changed."); return; } + this.change(context => { + for (const capture of receipt.captures) { + const ref = this.ref(context, capture.target, { kind: "read", provenance: "observed", source: receipt.source }); + ref.captureIds.push(capture.id); context.captures.push(capture); + } + }); + this.notes.set(key, signature); + } + proposed(proposal: TaskContextProposal): TaskContextProposalResult { + if (!this.accepting()) throw new Error("Task context attempt is no longer accepting proposals."); + if (this.o.privateValue?.(proposal)) throw new Error("Task context proposal contains a private connection."); + const signature = JSON.stringify(proposal), prior = this.proposals.get(proposal.receiptId); + if (prior) { if (prior.signature !== signature) throw new Error("Task context proposal receipt changed."); return structuredClone(prior.result); } + this.capability({ state: "available" }); + const source = { kind: "model-proposal" as const, turn: structuredClone(this.o.turn), leaseId: proposal.leaseId, receiptId: proposal.receiptId, requestId: proposal.requestId, at: proposal.at }; + let result: TaskContextProposalResult | undefined; + this.change(context => { + // Public acceptedRefIds must never expose legacy tuple/path keys. Only this mutable user graph changes. + const remap = new Map(); + for (const ref of context.refs) { + if (/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(ref.id)) continue; + const id = randomUUID(); remap.set(ref.id, id); ref.id = id; + } + for (const check of context.checks) check.relevantRefIds = check.relevantRefIds.map(id => remap.get(id) ?? id); + const accepted = new Set(); + for (const { target, role } of proposal.refs) accepted.add(this.ref(context, target, { kind: "relevant", provenance: "model-proposed", role, source }).id); + result = { receiptId: proposal.receiptId, accepted: accepted.size, refIds: [...accepted] }; + }); + if (!result) throw new Error("Task context attempt is no longer current."); + this.proposals.set(proposal.receiptId, { signature, result }); return structuredClone(result); + } +} diff --git a/apps/desktop/src/main/engine/task-context-capture.ts b/apps/desktop/src/main/engine/task-context-capture.ts new file mode 100644 index 00000000..160e2efb --- /dev/null +++ b/apps/desktop/src/main/engine/task-context-capture.ts @@ -0,0 +1,206 @@ +import fs from "node:fs/promises"; +import { constants } from "node:fs"; +import path from "node:path"; +import { createHash, randomUUID } from "node:crypto"; +import { execFile } from "node:child_process"; +import { contextRefId, type ContextCapture, type ContextSource, type ContextTarget } from "../../shared/task-context-evidence.js"; + +export interface CaptureClock { now(): number; setTimeout(callback: () => void, delay: number): unknown; clearTimeout(id: unknown): void } +export type CaptureReader = (cwd: string, relative: string, options: { signal: AbortSignal; charge(bytes: number): boolean; remainingBytes(): number }) => Promise; +type Revision = ContextCapture["revision"]; +export type ExecutionScope = "project" | "outside" | "unknown"; +type ScopeReader = (cwd: string, directory: string, signal: AbortSignal) => Promise; +export interface CapturedNoteInput { target: Exclude; text: string; source: ContextSource; version?: NonNullable } +interface CaptureJob { target: ContextTarget; source: ContextSource; read: (options: Parameters[2]) => Promise; version?: NonNullable } +const unavailable = (reason: Extract["reason"]): Revision => ({ state: "unavailable", reason }); +const FILE_BYTES = 25 * 1024 * 1024, BATCH_BYTES = 100 * 1024 * 1024; +const clock: CaptureClock = { now: () => Date.now(), setTimeout: (callback, delay) => setTimeout(callback, delay), clearTimeout: id => clearTimeout(id as ReturnType) }; + +/** Non-atomic observations: canonical/descriptor checks reject detected replacement, not all OS races. */ +const readFile: CaptureReader = async (cwd, relative, options) => { + if (!relative || /[\u0000-\u001f\u007f\\]/.test(relative) || /^(?:\/|[A-Za-z]:)/.test(relative) || relative.split("/").some(p => !p || p === "." || p === "..")) return unavailable("outside"); + let file: import("node:fs/promises").FileHandle | undefined; + try { + options.signal.throwIfAborted(); + const root = await fs.realpath(cwd), abs = path.join(root, relative), real = await fs.realpath(abs); + const inside = path.relative(root, real); + if (inside === ".." || inside.startsWith(`..${path.sep}`) || path.isAbsolute(inside)) return unavailable("outside"); + // Retain every ancestor's identity, including root, and refuse changed/symlinked ancestry. + const dirs = [root]; + for (const part of inside.split(path.sep).slice(0, -1)) dirs.push(path.join(dirs.at(-1)!, part)); + const ancestors = await Promise.all(dirs.map(async name => ({ name, stat: await fs.lstat(name, { bigint: true }) }))); + if (ancestors.some(a => !a.stat.isDirectory() || a.stat.isSymbolicLink())) return unavailable("outside"); + options.signal.throwIfAborted(); + file = await fs.open(real, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); + const before = await file.stat({ bigint: true }); + if (!before.isFile()) return unavailable("not-regular"); + if (before.size > BigInt(FILE_BYTES)) return unavailable("too-large"); + if (!options.charge(Number(before.size))) return unavailable("budget"); + const hash = createHash("sha256"), buffer = Buffer.alloc(64 * 1024); let bytes = 0; + while (bytes < Number(before.size)) { + options.signal.throwIfAborted(); + const length = Math.min(buffer.length, Number(before.size) - bytes); + const read = await file.read(buffer, 0, length, bytes); + if (read.bytesRead === 0) return unavailable("changed-during-read"); + bytes += read.bytesRead; hash.update(buffer.subarray(0, read.bytesRead)); + } + options.signal.throwIfAborted(); + const after = await file.stat({ bigint: true }); + const current = await fs.lstat(real, { bigint: true }); + if (!current.isFile() || [after, current].some(s => s.dev !== before.dev || s.ino !== before.ino || s.size !== before.size || s.mtimeNs !== before.mtimeNs || s.ctimeNs !== before.ctimeNs)) return unavailable("changed-during-read"); + if (await fs.realpath(cwd) !== root || await fs.realpath(abs) !== real) return unavailable("changed-during-read"); + for (const ancestor of ancestors) { + const current = await fs.lstat(ancestor.name, { bigint: true }); + if (!current.isDirectory() || current.isSymbolicLink() || current.dev !== ancestor.stat.dev || current.ino !== ancestor.stat.ino) return unavailable("changed-during-read"); + } + options.signal.throwIfAborted(); + return { state: "available", algorithm: "sha256", digest: hash.digest("hex"), bytes }; + } catch (error) { + if (options.signal.aborted) return unavailable("budget"); + const code = (error as NodeJS.ErrnoException).code; + return unavailable(code === "ENOENT" ? "missing" : code === "ELOOP" || code === "ENOTDIR" ? "outside" : "unreadable"); + } finally { await file?.close().catch(() => {}); } +}; + +function readHead(cwd: string, signal: AbortSignal): Promise { + if (signal.aborted) return Promise.resolve({ state: "unavailable", reason: "budget" }); + const env: NodeJS.ProcessEnv = { ...process.env, GIT_OPTIONAL_LOCKS: "0" }; + for (const name of ["GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR", "GIT_NAMESPACE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES"]) delete env[name]; + return new Promise(resolve => execFile("git", ["-c", "core.fsmonitor=false", "rev-parse", "--verify", "HEAD"], { cwd, env, timeout: 1000, maxBuffer: 4096, signal }, (error, stdout) => { + const head = String(stdout).trim(); + if (!error && /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(head)) resolve({ state: "available", head }); + else resolve({ state: "unavailable", reason: signal.aborted || error?.killed ? "budget" : error?.code === 128 ? "not-repository" : "unreadable" }); + })); +} + +function abortable(work: Promise, signal: AbortSignal, fallback: T): Promise { + return new Promise(resolve => { + let settled = false; + const finish = (result: T) => { if (settled) return; settled = true; signal.removeEventListener("abort", stop); resolve(result); }; + const stop = () => finish(fallback); + signal.addEventListener("abort", stop, { once: true }); + if (signal.aborted) stop(); + void work.then(finish, () => finish(fallback)); + }); +} +interface Job { signal: AbortSignal; run(): Promise; cancel(): void } + +/** One instance is shared by main: two live capture/scope jobs and 64 queued jobs across every turn. */ +export class TaskContextCapture { + private active = 0; + private readonly queue: Job[] = []; + private readonly reader: CaptureReader; + private readonly head: (cwd: string, signal: AbortSignal) => Promise; + private readonly clock: CaptureClock; + private readonly scopeReader: ScopeReader; + constructor(options: { reader?: CaptureReader; head?: (cwd: string, signal: AbortSignal) => Promise; clock?: CaptureClock; scopeReader?: ScopeReader } = {}) { + this.reader = options.reader ?? readFile; this.head = options.head ?? readHead; this.clock = options.clock ?? clock; + this.scopeReader = options.scopeReader ?? (async (cwd, directory, signal) => { + signal.throwIfAborted(); + const [root, real] = await Promise.all([fs.realpath(cwd), fs.realpath(directory)]); + signal.throwIfAborted(); + if (!(await fs.stat(real)).isDirectory()) return "unknown"; + const relative = path.relative(root, real); + return relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative) ? "outside" : "project"; + }); + } + private pump(): void { + while (this.active < 2 && this.queue.length) { + const job = this.queue.shift()!; + if (job.signal.aborted) { job.cancel(); continue; } + this.active++; + // A cancelled caller settles immediately; the real slot stays occupied until OS cleanup finishes. + void Promise.resolve().then(() => job.run()).finally(() => { this.active--; this.pump(); }); + } + } + private schedule(run: () => Promise, signal: AbortSignal, cancelled: T, rejected: T): Promise { + if (signal.aborted || (this.active >= 2 && this.queue.length >= 64)) return Promise.resolve(cancelled); + return new Promise(resolve => { + let settled = false; + const finish = (value: T) => { if (settled) return; settled = true; signal.removeEventListener("abort", stop); resolve(value); }; + const job: Job = { signal, run: async () => { try { finish(await run()); } catch { finish(rejected); } }, cancel: () => finish(cancelled) }; + const stop = () => { const index = this.queue.indexOf(job); if (index >= 0) this.queue.splice(index, 1); job.cancel(); }; + signal.addEventListener("abort", stop, { once: true }); + if (signal.aborted) { stop(); return; } + this.queue.push(job); this.pump(); + }); + } + /** Canonical directory metadata only, sharing the two real slots/64 queue and 2 s caller budget. */ + async executionScope(cwd: string, directory: string, signal: AbortSignal, changes: readonly string[] = []): Promise { + if (!path.isAbsolute(cwd) || !path.isAbsolute(directory)) return "unknown"; + const controller = new AbortController(), stop = () => controller.abort(); + signal.addEventListener("abort", stop, { once: true }); if (signal.aborted) stop(); + const timer = this.clock.setTimeout(stop, 2000); + try { return await this.schedule(async () => { + let current = directory; + for (let i = 0; i <= changes.length; i++) { + controller.signal.throwIfAborted(); + if (i) current = path.resolve(current, changes[i - 1]!); + const scope = await this.scopeReader(cwd, current, controller.signal); + if (scope !== "project") return scope; + } + return "project"; + }, controller.signal, "unknown" as ExecutionScope, "unknown" as ExecutionScope); } + finally { this.clock.clearTimeout(timer); signal.removeEventListener("abort", stop); } + } + async capture(cwd: string, targets: readonly ContextTarget[], source: ContextSource, phase: ContextCapture["phase"], signal: AbortSignal): Promise { + const requested = structuredClone(targets); + return this.captureJobs(cwd, requested.map(target => ({ target, source, read: options => target.kind === "file" ? this.reader(cwd, target.path, options) : Promise.resolve(unavailable("not-captured")) })), phase, signal); + } + /** Hash exact bodies already returned by the scoped Notes reader; never reread a later revision. */ + async captureNotes(cwd: string, notes: readonly CapturedNoteInput[], phase: ContextCapture["phase"], signal: AbortSignal): Promise { + return this.captureJobs(cwd, notes.map(note => this.noteJob(note)), phase, signal); + } + /** A human selection is one budget/boundary, including its mixed file and Note references. */ + async captureSelection(cwd: string, targets: readonly ContextTarget[], source: ContextSource, notes: readonly CapturedNoteInput[], phase: ContextCapture["phase"], signal: AbortSignal): Promise { + const prepared = new Map(notes.map(note => [contextRefId(note.target), this.noteJob(note)])); + const jobs = structuredClone(targets).map(target => target.kind === "file" + ? { target, source, read: (options: Parameters[2]) => this.reader(cwd, target.path, options) } + : prepared.get(contextRefId(target)) ?? { target, source, read: async () => unavailable("not-captured") }); + return this.captureJobs(cwd, jobs, phase, signal); + } + private noteJob(input: CapturedNoteInput): CaptureJob { + const note = { ...input, target: structuredClone(input.target), source: structuredClone(input.source), ...(input.version ? { version: { ...input.version } } : {}) }; + return { target: note.target, source: note.source, version: note.version, read: async options => { + try { + options.signal.throwIfAborted(); + const length = Buffer.byteLength(note.text); + if (length > FILE_BYTES) return unavailable("too-large"); + if (!options.charge(length)) return unavailable("budget"); + const body = Buffer.from(note.text), hash = createHash("sha256"); + for (let offset = 0; offset < body.length; offset += 64 * 1024) { + options.signal.throwIfAborted(); + const chunk = body.subarray(offset, offset + 64 * 1024); + hash.update(chunk); + await new Promise(resolve => setImmediate(resolve)); + } + options.signal.throwIfAborted(); + return { state: "available", algorithm: "sha256", digest: hash.digest("hex"), bytes: body.length, ...(note.version ? { noteRevision: note.version.revision } : {}) }; + } catch { return unavailable(options.signal.aborted ? "budget" : "unreadable"); } + } }; + } + private async captureJobs(cwd: string, requested: readonly CaptureJob[], phase: ContextCapture["phase"], signal: AbortSignal): Promise { + if (!requested.length) return []; + const sources = requested.map(job => structuredClone(job.source)), startedAt = new Date(this.clock.now()).toISOString(); + const controller = new AbortController(), stop = () => controller.abort(); + signal.addEventListener("abort", stop, { once: true }); if (signal.aborted) stop(); + const timer = this.clock.setTimeout(stop, 2000), repositoryFallback = { state: "unavailable", reason: "budget" } as const; + const repository = controller.signal.aborted ? Promise.resolve(repositoryFallback) : abortable(Promise.resolve().then(() => this.head(cwd, controller.signal)), controller.signal, repositoryFallback); + const rows: ContextCapture[] = new Array(requested.length); let cursor = 0, reserved = 0; + const options = { signal: controller.signal, remainingBytes: () => BATCH_BYTES - reserved, charge: (bytes: number) => { + if (!Number.isSafeInteger(bytes) || reserved + bytes < 0 || reserved + bytes > BATCH_BYTES) return false; + reserved += bytes; return true; + } }; + const worker = async () => { + while (cursor < requested.length) { + const index = cursor++, job = requested[index]!; + const revision = controller.signal.aborted ? unavailable("budget") : await this.schedule(() => job.read(options), controller.signal, unavailable("budget"), unavailable("unreadable")); + rows[index] = { id: randomUUID(), target: structuredClone(job.target), source: sources[index]!, phase, startedAt, finishedAt: new Date(this.clock.now()).toISOString(), repository: repositoryFallback, revision, ...(job.version ? { noteVersion: { ...job.version } } : {}) }; + } + }; + try { + await Promise.all([worker(), worker()]); const head = await repository; + return rows.map(row => ({ ...row, source: structuredClone(row.source), repository: { ...head } })); + } finally { this.clock.clearTimeout(timer); signal.removeEventListener("abort", stop); } + } +} diff --git a/apps/desktop/src/main/engine/task-context-recovery.ts b/apps/desktop/src/main/engine/task-context-recovery.ts new file mode 100644 index 00000000..8adb211f --- /dev/null +++ b/apps/desktop/src/main/engine/task-context-recovery.ts @@ -0,0 +1,142 @@ +import path from "node:path"; +import { createHash, randomUUID } from "node:crypto"; +import type { Store } from "./store.js"; +import { AttachmentRecoveryFile, recoveryJSON } from "./attachment-recovery-file.js"; +import { PendingSends, PENDING_CONTEXT_NODES } from "./pending-sends.js"; +import { contextDraftOwnerKey, sameContextDraftOwner, isContextDraftOwner, isPendingTaskContext, isTaskContextEvidence, taskContextBelongsTo, type ContextDraftOwner, type TaskContextEvidence } from "../../shared/task-context-evidence.js"; + +export interface ContextRecoveryScope { projectId: string; identity: string } +export interface ContextCreation { id: string; at: string } +export interface ContextReservation extends ContextCreation { worktree: boolean } +export interface ContextTransfer { kind: "user" | "pending"; threadId: string; threadCreatedAt: string; turnId: string } +interface ContextRecoveryRecord { schema: 1; owner: ContextDraftOwner; scope: string; projectId: string; context: TaskContextEvidence; created: ContextCreation | null; transfer: ContextTransfer | null; requested?: ContextReservation | null; previous?: TaskContextEvidence | null } +// A replacement retains its predecessor through an uncertain rename/fsync. +// Each selection still independently obeys the original 2 MiB metadata bound. +const LIMIT = 4 * 1024 * 1024 + 64 * 1024; +const NODES = 2 * PENDING_CONTEXT_NODES + 256; +const empty = (): TaskContextEvidence => ({ revision: 0, refs: [], captures: [], checks: [] }); +const digest = (value: string) => createHash("sha256").update(value).digest("hex"); +const encoded = (value: unknown) => recoveryJSON(value, LIMIT, NODES); +const equal = (a: unknown, b: unknown) => encoded(a) === encoded(b); +const fail = () => new Error("Task context recovery requires attention; the original selection was preserved."); +const shape = (v: unknown, keys: string[]): v is Record => !!v && typeof v === "object" && !Array.isArray(v) && Object.keys(v).length === keys.length && keys.every(key => Object.hasOwn(v, key)); +const id = (v: unknown): v is string => typeof v === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(v); +const date = (v: unknown): v is string => typeof v === "string" && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(v) && Number.isFinite(Date.parse(v)) && new Date(v).toISOString() === v; +function validate(value: unknown): asserts value is ContextRecoveryRecord { + const keys = ["schema", "owner", "scope", "projectId", "context", "created", "transfer"]; + if (value && typeof value === "object" && Object.hasOwn(value, "requested")) keys.push("requested"); + if (value && typeof value === "object" && Object.hasOwn(value, "previous")) keys.push("previous"); + if (!shape(value, keys) || value.schema !== 1 || !isContextDraftOwner(value.owner) + || typeof value.scope !== "string" || !/^[a-f0-9]{64}$/.test(value.scope) || !id(value.projectId) + || !isPendingTaskContext({ owner: value.owner, projectId: value.projectId, evidence: value.context }) + || (value.owner.kind === "project" && value.owner.projectId !== value.projectId)) throw fail(); + if (Buffer.byteLength(JSON.stringify(value.context)) > 2 * 1024 * 1024) throw fail(); + if (value.previous !== undefined && value.previous !== null && (!isPendingTaskContext({ owner: value.owner, projectId: value.projectId, evidence: value.previous }) || Buffer.byteLength(JSON.stringify(value.previous)) > 2 * 1024 * 1024)) throw fail(); + if (value.created !== null && (!shape(value.created, ["id", "at"]) || !id(value.created.id) || !date(value.created.at) || value.owner.kind !== "project")) throw fail(); + if (value.requested !== undefined && value.requested !== null && (!shape(value.requested, ["id", "at", "worktree"]) || !id(value.requested.id) || !date(value.requested.at) || typeof value.requested.worktree !== "boolean" || value.owner.kind !== "project" + || (value.created && (value.created.id !== value.requested.id || value.created.at !== value.requested.at)))) throw fail(); + if (value.transfer !== null && (!shape(value.transfer, ["kind", "threadId", "threadCreatedAt", "turnId"]) || (value.transfer.kind !== "user" && value.transfer.kind !== "pending") + || !id(value.transfer.threadId) || !id(value.transfer.turnId) || !date(value.transfer.threadCreatedAt) + || (value.owner.kind === "thread" && value.owner.threadId !== value.transfer.threadId))) throw fail(); + if (value.transfer?.kind === "user" && value.owner.kind === "project" && (!value.created || value.created.id !== value.transfer.threadId || value.created.at !== value.transfer.threadCreatedAt)) throw fail(); +} + +/** Later native/proposal receipts may extend a turn. Its original explicit + * selection must remain byte-identical, including its observed capture IDs. */ +function contains(recorded: unknown, selected: TaskContextEvidence): boolean { + if (!isTaskContextEvidence(recorded)) return false; + const refs = new Map(recorded.refs.map(ref => [ref.id, ref])), captures = new Map(recorded.captures.map(capture => [capture.id, capture])); + return selected.refs.every(ref => { + const actual = refs.get(ref.id); + if (!actual || !equal(actual.target, ref.target)) return false; + const ids = new Set(actual.captureIds), relations = new Set(actual.relations.map(encoded)); + return ref.captureIds.every(id => ids.has(id)) && ref.relations.every(relation => relations.has(encoded(relation))); + }) && selected.captures.every(capture => captures.has(capture.id) && equal(captures.get(capture.id), capture)); +} + +/** Canonical local metadata, never renderer-supplied evidence or an editable + * nonce. Prepared destinations resolve the crash gap between two durable files. */ +export class TaskContextRecovery { + private readonly files = new Map>(); + constructor(private readonly store: Store) {} + private file(owner: ContextDraftOwner): AttachmentRecoveryFile { + const key = digest(contextDraftOwnerKey(owner)); + let file = this.files.get(key); + if (!file) { file = new AttachmentRecoveryFile(path.join(this.store.dir, "context-drafts"), `${key}.json`, LIMIT, validate, NODES); this.files.set(key, file); } + return file; + } + private read(owner: ContextDraftOwner, scope: ContextRecoveryScope) { + const file = this.file(owner), snapshot = file.read(), value = snapshot.value; + if (value && (!sameContextDraftOwner(value.owner, owner) || value.scope !== digest(scope.identity) || value.projectId !== scope.projectId)) throw fail(); + return { file, snapshot, record: value ? structuredClone(value) as ContextRecoveryRecord : { schema: 1 as const, owner: structuredClone(owner), scope: digest(scope.identity), projectId: scope.projectId, context: empty(), created: null, transfer: null } }; + } + private delivered(record: ContextRecoveryRecord, checkpoint: boolean): boolean { + const transfer = record.transfer; if (!transfer) return false; + const thread = this.store.thread(transfer.threadId); + if (thread && (thread.createdAt !== transfer.threadCreatedAt || thread.projectId !== record.projectId)) throw fail(); + const users = thread ? this.store.items(thread.id).filter(item => item.id === transfer.turnId) : []; + if (users.length > 1) throw fail(); + const user = users[0]; + if (user && (user.kind !== "user" || !contains(user.contextEvidence, record.context) || !taskContextBelongsTo(user.contextEvidence!, thread!.id, transfer.turnId, record.projectId))) throw fail(); + let pendingDelivered = false; + if (transfer.kind === "pending") { + const pending = new PendingSends(this.store.dir), snapshot = pending.read(transfer.turnId), value = snapshot.value; + if (value) { + const target = value.target?.threadId ?? value.creation?.threadId ?? (value.source.kind === "thread" ? value.source.threadId : ""); + if (!value.context || !sameContextDraftOwner(value.source, record.owner) || target !== transfer.threadId || value.context.projectId !== record.projectId || !contains(value.context.evidence, record.context)) throw fail(); + if (checkpoint) pending.checkpoint(transfer.turnId, snapshot); + pendingDelivered = true; + } + } + if (user && checkpoint) this.store.saveItems(thread!.id, this.store.items(thread!.id)); + return !!user || pendingDelivered; + } + open(owner: ContextDraftOwner, scope: ContextRecoveryScope): { context: TaskContextEvidence; created?: ContextCreation; requested?: ContextReservation } { + const { file, record, snapshot: initial } = this.read(owner, scope); let snapshot = initial; + if (snapshot.value) file.checkpoint(snapshot); + if (record.transfer) { + const delivered = this.delivered(record, true); + if (delivered) { record.context = empty(); record.previous = null; record.created = null; record.requested = null; } + record.transfer = null; snapshot = file.write(snapshot, record); + } + if (record.requested && !record.created) { + const thread = this.store.thread(record.requested.id); + if (thread) { + if (thread.retired || thread.createdAt !== record.requested.at || thread.projectId !== record.projectId) throw fail(); + this.store.checkpointThread(thread.id); + record.created = { id: thread.id, at: thread.createdAt }; file.write(snapshot, record); + } + } + if (record.created) { + const thread = this.store.thread(record.created.id); + if (!thread || thread.retired || thread.createdAt !== record.created.at || thread.projectId !== record.projectId) throw fail(); + } + return { context: structuredClone(record.context), ...(record.created ? { created: structuredClone(record.created) } : {}), ...(record.requested ? { requested: structuredClone(record.requested) } : {}) }; + } + reserve(owner: ContextDraftOwner, scope: ContextRecoveryScope, context: TaskContextEvidence, worktree: boolean): ContextReservation { + const { file, snapshot, record } = this.read(owner, scope); + if (owner.kind !== "project" || !equal(record.context, context) || (record.transfer && this.delivered(record, false))) throw fail(); + if (record.requested) { + if (record.requested.worktree !== worktree) throw fail(); + if (worktree && !this.store.thread(record.requested.id)) throw new Error("Worktree creation is unresolved; the original context was preserved. Inspect its existing evidence before creating another worktree."); + return structuredClone(record.requested); + } + const requested = { id: randomUUID(), at: new Date().toISOString(), worktree }; + file.write(snapshot, { ...record, requested, transfer: null }); return requested; + } + save(owner: ContextDraftOwner, scope: ContextRecoveryScope, previous: TaskContextEvidence, context: TaskContextEvidence, created?: ContextCreation): void { + const { file, snapshot, record } = this.read(owner, scope); + if (!equal(record.context, previous) || (record.transfer && this.delivered(record, false))) throw fail(); + file.write(snapshot, { ...record, context, previous: equal(previous, context) ? record.previous ?? null : previous, created: created ?? null, transfer: null }); + } + prepare(owner: ContextDraftOwner, scope: ContextRecoveryScope, context: TaskContextEvidence, transfer: ContextTransfer): void { + const { file, snapshot, record } = this.read(owner, scope); + if (!equal(record.context, context) || (record.transfer && this.delivered(record, false))) throw fail(); + file.write(snapshot, { ...record, transfer }); + } + complete(owner: ContextDraftOwner, scope: ContextRecoveryScope, transfer: ContextTransfer): void { + const { file, snapshot, record } = this.read(owner, scope); + if (!record.transfer || !equal(record.transfer, transfer) || !this.delivered(record, false)) throw fail(); + file.write(snapshot, { ...record, context: empty(), previous: null, created: null, requested: null, transfer: null }); + } +} diff --git a/apps/desktop/src/main/engine/task-context-service.ts b/apps/desktop/src/main/engine/task-context-service.ts new file mode 100644 index 00000000..84e9421d --- /dev/null +++ b/apps/desktop/src/main/engine/task-context-service.ts @@ -0,0 +1,299 @@ +import { randomUUID } from "node:crypto"; +import { listWorkspaceFiles } from "./workspace-files.js"; +import type { TaskContextCandidates } from "../../shared/task-context-evidence.js"; +import { TaskContextRecovery, type ContextTransfer, type ContextReservation } from "./task-context-recovery.js"; +import type { Store } from "./store.js"; +import type { TaskContextCapture, CapturedNoteInput } from "./task-context-capture.js"; +import { sameContextDraftOwner, contextRefId, copyTaskContextEvidence, isContextDraftBinding, taskContextBelongsTo, type ContextCapture, type ContextDraftOwner, type ContextDraftBinding, type ContextTarget, type TaskContextEvidence, type PendingTaskContext } from "../../shared/task-context-evidence.js"; + +interface Scope { projectId: string; cwd: string; identity: string } +interface Draft { binding: ContextDraftBinding; scope: Scope; context: TaskContextEvidence; abort: AbortController; generation: number; created?: { id: string; at: string }; requested?: ContextReservation; pending?: ContextTransfer } +const fail = () => new Error("Task context draft is stale or no longer belongs to this composer/project."); +const key = (owner: ContextDraftOwner) => owner.kind === "thread" ? `thread:${owner.threadId}` : owner.key; +const equal = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b); +const SELECTION_BYTES = 2 * 1024 * 1024; +const tooLarge = () => new Error("Selected context is too large to save; the previous draft was preserved."); + +/** Main owns draft identities and receipt bodies; renderer requests contain targets only. */ +export class TaskContextService { + private readonly drafts = new Map(); + private readonly owners = new Map(); + private readonly spent = new Map(); + private readonly recovery: TaskContextRecovery; + constructor(private readonly options: { store: Store; capture: Pick; listFiles?: typeof listWorkspaceFiles }) { this.recovery = new TaskContextRecovery(options.store); } + private scope(owner: ContextDraftOwner): Scope { + if (!owner || typeof owner !== "object") throw fail(); + if (owner.kind === "thread") { + if (Object.keys(owner).sort().join() !== "kind,threadId") throw fail(); + const t = this.options.store.thread(owner.threadId), p = t && this.options.store.project(t.projectId); + if (!t || t.retired || !p) throw fail(); + return { projectId: p.id, cwd: t.cwd, identity: JSON.stringify([p.id, p.path, p.addedAt, t.id, t.createdAt, t.cwd]) }; + } + if (owner.kind !== "project" || Object.keys(owner).sort().join() !== "key,kind,projectId" || typeof owner.key !== "string") throw fail(); + const p = this.options.store.project(owner.projectId), match = owner.key.match(/^draft:(0|[1-9]\d*):/); + if (!p || !match || !Number.isSafeInteger(Number(match[1])) || owner.key !== `draft:${match[1]}:${p.id}`) throw fail(); + return { projectId: p.id, cwd: p.path, identity: JSON.stringify([p.id, p.path, p.addedAt]) }; + } + private current(binding: ContextDraftBinding, revision = true): Draft { + if (!isContextDraftBinding(binding)) throw fail(); + const draft = this.drafts.get(binding.nonce); + if (!draft || !sameContextDraftOwner(draft.binding.owner, binding.owner) || (revision && draft.binding.revision !== binding.revision) || this.scope(binding.owner).identity !== draft.scope.identity) throw fail(); + return draft; + } + openContextDraft(owner: ContextDraftOwner): ContextDraftBinding { + const scope = this.scope(owner), old = this.owners.get(key(owner)); + if (old) this.closeContextDraft(this.drafts.get(old)!.binding); + const recovered = this.recovery.open(owner, scope); + const binding = { owner: structuredClone(owner), nonce: randomUUID(), revision: recovered.context.revision }; + this.drafts.set(binding.nonce, { binding, scope, context: recovered.context, ...(recovered.created ? { created: recovered.created } : {}), ...(recovered.requested ? { requested: recovered.requested } : {}), abort: new AbortController(), generation: 0 }); + this.owners.set(key(owner), binding.nonce); + return structuredClone(binding); + } + draftTaskContext(binding: ContextDraftBinding): TaskContextEvidence { return copyTaskContextEvidence(this.current(binding).context); } + assertReferenceScope(projectId: string, targets: ContextTarget[]): void { + for (const target of targets) if (target.kind !== "file" && !this.noteInScope(projectId, target)) throw new Error("This note is outside this project's notes."); + } + async candidates(binding: ContextDraftBinding): Promise { + const draft = this.current(binding), generation = draft.generation; + const listed = await (this.options.listFiles ?? listWorkspaceFiles)(draft.scope.cwd); + if (this.current(binding) !== draft || draft.generation !== generation) throw fail(); + const files: TaskContextCandidates["files"] = { paths: [], truncated: listed.truncated }; + let fileBytes = 2; // Include the array delimiters; one byte per row reserves its comma. + for (const relative of listed.paths) { + const target: ContextTarget = { kind: "file", path: relative }; + try { copyTaskContextEvidence({ revision: 0, refs: [{ id: "candidate", target, relations: [], captureIds: [] }], captures: [], checks: [] }); } + catch { files.truncated = true; continue; } + const bytes = Buffer.byteLength(JSON.stringify(relative)) + 1; + if (files.paths.length === 5000 || fileBytes + bytes > 2 * 1024 * 1024) { files.truncated = true; break; } + files.paths.push(relative); fileBytes += bytes; + } + const space = this.options.store.space, own = space.projectFor(draft.scope.projectId); + const notes: TaskContextCandidates["notes"] = { items: [], truncated: false }; + if (own) { + const within = (rel: string | undefined) => rel !== undefined && (rel === own.rel || rel.startsWith(`${own.rel}/`)); + const rels = space.pageRels(), tree = space.tree(); + notes.truncated = tree.truncated; + let bytes = 2; + const append = (row: TaskContextCandidates["notes"]["items"][number]) => { + const size = Buffer.byteLength(JSON.stringify(row)) + 1; + if (notes.items.length === 5000 || bytes + size > 2 * 1024 * 1024) { notes.truncated = true; return false; } + notes.items.push(row); bytes += size; return true; + }; + for (const page of space.list()) { + if (!page.trashedAt && within(rels.get(page.id)) && !append({ target: { kind: "note", pageId: page.id }, title: page.title })) break; + } + for (const node of tree.nodes) { + if (node.kind === "file" && within(node.rel) && !append({ target: { kind: "notes-file", id: node.id }, title: node.name })) break; + } + } + if (this.current(binding) !== draft || draft.generation !== generation) throw fail(); + return { files, notes }; + } + private noteInScope(projectId: string, target: Exclude): boolean { + const notes = this.options.store.space, own = notes.projectFor(projectId); + if (!own) return false; + const within = (rel: string | undefined) => rel !== undefined && (rel === own.rel || rel.startsWith(`${own.rel}/`)); + if (target.kind === "note") { + if (!within(notes.pageRels().get(target.pageId))) return false; + return notes.list().some(p => p.id === target.pageId && !p.trashedAt); + } + return notes.tree().nodes.some(n => n.kind === "file" && n.id === target.id && within(n.rel)); + } + async selectTaskContext(binding: ContextDraftBinding, targets: ContextTarget[]): Promise<{ binding: ContextDraftBinding; context: TaskContextEvidence }> { + const draft = this.current(binding), generation = draft.generation; + if (!Array.isArray(targets)) throw fail(); + const unique = new Map(); + let minimumReferenceBytes = 0; + for (const target of targets) { + // Reuse the strict persisted-target validator before any filesystem/Notes read. + const candidate: TaskContextEvidence = { revision: 0, refs: [{ id: "candidate", target, relations: [], captureIds: [] }], captures: [], checks: [] }; + copyTaskContextEvidence(candidate); + const id = contextRefId(target); + if (unique.has(id)) continue; + // These ref fields alone cannot fit, regardless of capture results. Bound + // the logical selection rather than rejecting large duplicate inputs. + minimumReferenceBytes += Buffer.byteLength(JSON.stringify({ id, target, relations: [], captureIds: [] })) + 1; + if (minimumReferenceBytes > SELECTION_BYTES) throw tooLarge(); + if (target.kind !== "file" && !this.noteInScope(draft.scope.projectId, target)) throw new Error("This note is outside this project's notes."); + unique.set(id, structuredClone(target)); + } + const requested = [...unique.values()]; + const source = { kind: "human" as const, actionId: randomUUID(), at: new Date().toISOString() }; + const retained = draft.context.captures.filter(c => unique.has(contextRefId(c.target))); + const previousRefs = new Map(draft.context.refs.map(ref => [ref.id, ref])); + const workspace = (target: ContextTarget): NonNullable => target.kind === "file" && binding.owner.kind === "thread" + ? { kind: "thread", projectId: draft.scope.projectId, threadId: binding.owner.threadId } : { kind: "project", projectId: draft.scope.projectId }; + const compose = (captures: ContextCapture[]): TaskContextEvidence => { + const capturedIds = new Map(); + for (const capture of captures) { const id = contextRefId(capture.target); capturedIds.set(id, [...(capturedIds.get(id) ?? []), capture.id]); } + return { revision: draft.context.revision + 1, refs: [...unique].map(([id, target]) => { + const previous = previousRefs.get(id); + return { id, target, relations: [...(previous?.relations ?? []), { kind: "relevant", provenance: "user-confirmed", source }], captureIds: [...(previous?.captureIds ?? []), ...(capturedIds.get(id) ?? [])] }; + }), captures: [...retained, ...captures], checks: [] }; + }; + // Size prediction only: unavailable/budget is the shortest capture shape, + // IDs and ISO timestamps have their actual widths, and optional Note/hash + // metadata is omitted. Never publish or persist these predicted receipts. + const minimum = compose(requested.map((target, i): ContextCapture => ({ id: String(i).padStart(36, "0"), target, source, phase: "selected", startedAt: source.at, finishedAt: source.at, + repository: { state: "unavailable", reason: "budget" }, revision: { state: "unavailable", reason: "budget" }, workspace: workspace(target) }))); + if (Buffer.byteLength(JSON.stringify(minimum)) > SELECTION_BYTES) throw tooLarge(); + const notes: CapturedNoteInput[] = []; + for (const target of requested) { + if (target.kind === "note") { + const page = this.options.store.space.list().find(p => p.id === target.pageId && !p.trashedAt); + if (!page || !this.noteInScope(draft.scope.projectId, target)) throw fail(); + notes.push({ target, source, text: page.markdown, version: { via: "read", updatedAt: page.updatedAt, revision: page.revision } }); + } else if (target.kind === "notes-file") { + if (!this.noteInScope(draft.scope.projectId, target)) throw fail(); + notes.push({ target, source, text: this.options.store.space.readFile(target.id) }); + } + } + const captured = await this.options.capture.captureSelection(draft.scope.cwd, requested, source, notes, "selected", draft.abort.signal); + const captures: ContextCapture[] = captured.map(capture => ({ ...capture, workspace: workspace(capture.target) })); + if (this.current(binding) !== draft || draft.generation !== generation) throw fail(); + for (const target of requested) if (target.kind !== "file" && !this.noteInScope(draft.scope.projectId, target)) throw fail(); + for (const note of notes) if (note.target.kind === "note") { + const pageId = note.target.pageId; + const current = this.options.store.space.list().find(p => p.id === pageId && !p.trashedAt); + if (!current || current.revision !== note.version!.revision || current.updatedAt !== note.version!.updatedAt) throw new Error("The selected Note changed; the previous context draft was preserved."); + } + const context = compose(captures); + if (Buffer.byteLength(JSON.stringify(context)) > SELECTION_BYTES) throw tooLarge(); + this.recovery.save(binding.owner, draft.scope, draft.context, context, draft.created); + draft.context = copyTaskContextEvidence(context); + draft.binding = { ...draft.binding, revision: draft.binding.revision + 1 }; + return { binding: structuredClone(draft.binding), context: copyTaskContextEvidence(draft.context) }; + } + closeContextDraft(binding: ContextDraftBinding): void { + const draft = this.drafts.get(binding.nonce); + if (!draft || !sameContextDraftOwner(draft.binding.owner, binding.owner)) return; + draft.abort.abort(); this.drafts.delete(binding.nonce); + if (this.owners.get(key(binding.owner)) === binding.nonce) this.owners.delete(key(binding.owner)); + } + bindCreatedThread(binding: ContextDraftBinding, threadId: string): void { + const draft = this.current(binding), thread = this.options.store.thread(threadId); + if (binding.owner.kind !== "project" || !thread || thread.retired || thread.projectId !== draft.scope.projectId || (draft.created && (draft.created.id !== thread.id || draft.created.at !== thread.createdAt)) || (draft.requested && (draft.requested.id !== thread.id || draft.requested.at !== thread.createdAt))) throw new Error("The created thread is not bound to this project draft."); + const created = { id: thread.id, at: thread.createdAt }; + this.recovery.save(binding.owner, draft.scope, draft.context, draft.context, created); + draft.created = created; + } + snapshotForPending(binding: ContextDraftBinding, owner: ContextDraftOwner): PendingTaskContext { + const draft = this.current(binding); + if (!sameContextDraftOwner(draft.binding.owner, owner)) throw fail(); + return { owner: structuredClone(draft.binding.owner), projectId: draft.scope.projectId, evidence: copyTaskContextEvidence(draft.context) }; + } + preparePending(binding: ContextDraftBinding, transfer: ContextTransfer): void { + const draft = this.current(binding); + if (transfer.kind !== "pending") throw fail(); + this.recovery.prepare(binding.owner, draft.scope, draft.context, transfer); draft.pending = structuredClone(transfer); + } + pendingPersisted(binding: ContextDraftBinding, turnId: string): void { + const draft = this.current(binding); + if (!draft.pending || draft.pending.turnId !== turnId) throw fail(); + this.recovery.complete(binding.owner, draft.scope, draft.pending); this.closeContextDraft(binding); + } + /** A recovered project intent cannot attest to the newly bound thread's input bytes. */ + pendingForThread(context: PendingTaskContext, threadId: string): PendingTaskContext { + const thread = this.options.store.thread(threadId); + if (!thread || thread.retired || thread.projectId !== context.projectId) throw fail(); + const result = structuredClone(context); + if (result.owner.kind === "thread") { + if (result.owner.threadId !== threadId) throw fail(); + return result; + } + const at = new Date().toISOString(), source = { kind: "human" as const, actionId: randomUUID(), at }; + for (const ref of result.evidence.refs) if (ref.target.kind === "file") { + const capture: ContextCapture = { id: randomUUID(), target: { ...ref.target }, source, phase: "selected", startedAt: at, finishedAt: at, + repository: { state: "unavailable", reason: "not-captured" }, revision: { state: "unavailable", reason: "not-captured" }, + workspace: { kind: "thread", projectId: thread.projectId, threadId } }; + result.evidence.captures.push(capture); ref.captureIds.push(capture.id); + } + result.evidence.revision++; + return result; + } + creationFor(binding: ContextDraftBinding, projectId: string): string | undefined { + const draft = this.current(binding); + if (binding.owner.kind !== "project" || draft.scope.projectId !== projectId) throw new Error("Context draft belongs to another project."); + if (!draft.created && draft.requested && this.options.store.thread(draft.requested.id)) this.bindCreatedThread(binding, draft.requested.id); + if (draft.created) { + const thread = this.options.store.thread(draft.created.id); + if (!thread || thread.retired || thread.projectId !== projectId || thread.createdAt !== draft.created.at) throw fail(); + return thread.id; + } + return undefined; + } + reserveCreation(binding: ContextDraftBinding, worktree: boolean): { threadId: string; createdAt: string } { + const draft = this.current(binding), requested = this.recovery.reserve(binding.owner, draft.scope, draft.context, worktree); + draft.requested = requested; return { threadId: requested.id, createdAt: requested.at }; + } + /** Main-only admission: committing is separate so a failed user write leaves the draft editable. */ + prepareContextDraft(binding: ContextDraftBinding, threadId: string, turnId: string): { context: TaskContextEvidence; commit(): void } { + if (!isContextDraftBinding(binding)) throw fail(); + const spent = this.spent.get(binding.nonce); + if (spent) { + if (!equal(spent.binding, binding) || spent.threadId !== threadId || spent.turnId !== turnId || !this.options.store.project(spent.projectId)) throw new Error("Context draft was already consumed by another request."); + const users = this.options.store.items(threadId).filter(item => item.id === turnId && item.kind === "user"); + if (users.length > 1) throw fail(); + const user = users[0], recorded = user?.kind === "user" ? user.contextEvidence : undefined; + const context = recorded ?? spent.context; + if (!context || !taskContextBelongsTo(copyTaskContextEvidence(context), threadId, turnId, spent.projectId)) throw new Error("The recorded task context is unavailable; the original turn was preserved."); + if (recorded) delete spent.context; + return { context: copyTaskContextEvidence(context), commit: () => {} }; + } + const draft = this.current(binding), thread = this.options.store.thread(threadId); + if (!thread || thread.retired || thread.projectId !== draft.scope.projectId || !/^[A-Za-z0-9_-]{1,128}$/.test(turnId)) throw fail(); + if (binding.owner.kind === "thread" ? binding.owner.threadId !== threadId : draft.created?.id !== threadId || draft.created?.at !== thread.createdAt) throw new Error("Context can transfer only to its main-bound created thread."); + const transfer: ContextTransfer = { kind: "user", threadId, threadCreatedAt: thread.createdAt, turnId }; + this.recovery.prepare(binding.owner, draft.scope, draft.context, transfer); + const context = copyTaskContextEvidence(draft.context); + if (binding.owner.kind === "project" && thread.cwd !== draft.scope.cwd) { + const at = new Date().toISOString(), source = { kind: "human" as const, actionId: randomUUID(), at }; + for (const ref of context.refs) if (ref.target.kind === "file") { + const capture: ContextCapture = { id: randomUUID(), target: { ...ref.target }, source, phase: "selected", startedAt: at, finishedAt: at, + repository: { state: "unavailable", reason: "not-captured" }, revision: { state: "unavailable", reason: "not-captured" }, + workspace: { kind: "thread", projectId: thread.projectId, threadId: thread.id } }; + context.captures.push(capture); ref.captureIds.push(capture.id); + } + context.revision++; + } + let committed = false; + const generation = draft.generation; + return { context: copyTaskContextEvidence(context), commit: () => { + if (committed) return; + if (this.current(binding) !== draft || draft.generation !== generation) throw fail(); + this.recovery.complete(binding.owner, draft.scope, transfer); + this.spent.set(binding.nonce, { binding: structuredClone(binding), threadId, turnId, context, projectId: draft.scope.projectId }); + this.closeContextDraft(binding); committed = true; + } }; + } + consumeContextDraft(binding: ContextDraftBinding, threadId: string, turnId: string): TaskContextEvidence { + const admission = this.prepareContextDraft(binding, threadId, turnId); admission.commit(); return admission.context; + } + /** Called only from main's post-durable user callback; retain replay identity, not duplicate snapshots. */ + contextPersisted(binding: ContextDraftBinding, threadId: string, turnId: string): void { + const spent = this.spent.get(binding.nonce); + if (!spent || !equal(spent.binding, binding) || spent.threadId !== threadId || spent.turnId !== turnId) throw fail(); + delete spent.context; + } + readDraft(binding: ContextDraftBinding): TaskContextEvidence { return copyTaskContextEvidence(this.current(binding).context); } + invalidateDocument(): void { this.dispose(); } + private cancelCapture(draft: Draft): void { + draft.abort.abort(); draft.abort = new AbortController(); draft.generation++; + } + /** Cancel work at removal start while keeping accepted selections if removal later refuses. */ + cancelProjectCaptures(projectId: string): void { + for (const draft of this.drafts.values()) if (draft.scope.projectId === projectId) this.cancelCapture(draft); + } + cancelThreadCaptures(threadId: string): void { + for (const draft of this.drafts.values()) if ((draft.binding.owner.kind === "thread" && draft.binding.owner.threadId === threadId) || draft.created?.id === threadId) this.cancelCapture(draft); + } + invalidateProject(projectId: string): void { + for (const draft of this.drafts.values()) if (draft.scope.projectId === projectId) this.closeContextDraft(draft.binding); + for (const [nonce, row] of this.spent) if (row.projectId === projectId) this.spent.delete(nonce); + } + invalidateThread(threadId: string): void { + for (const draft of this.drafts.values()) if ((draft.binding.owner.kind === "thread" && draft.binding.owner.threadId === threadId) || draft.created?.id === threadId) this.closeContextDraft(draft.binding); + for (const [nonce, row] of this.spent) if (row.threadId === threadId) this.spent.delete(nonce); + } + dispose(): void { for (const draft of this.drafts.values()) this.closeContextDraft(draft.binding); this.spent.clear(); } +} diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts index d699de22..31788200 100644 --- a/apps/desktop/src/main/index.ts +++ b/apps/desktop/src/main/index.ts @@ -21,6 +21,8 @@ import { WorkspaceCanvas } from "./workspace-canvas.js"; import { CANVAS_SCHEME } from "../shared/canvas.js"; import { KnowledgeService } from "./engine/knowledge.js"; import { PagesAgents } from "./engine/pages-agents.js"; +import { TaskContextCapture } from "./engine/task-context-capture.js"; +import { TaskContextAgents } from "./engine/task-context-agents.js"; import { KnowledgeAgents } from "./engine/knowledge-agents.js"; import { UsageLedger } from "./engine/usage-ledger.js"; import { ANALYTICS_SCHEME, WorkspaceAnalytics } from "./workspace-analytics.js"; @@ -136,7 +138,10 @@ const knowledgeAgents = new KnowledgeAgents({ return { notes, own: projectNotes(id, create)?.rel ?? null }; }, }); +const contextCapture = new TaskContextCapture(); +const taskContextAgents = new TaskContextAgents({ store }); const pagesAgents = new PagesAgents({ + capture: contextCapture, space: store.space, notes: store.space, enabled: id => store.project(id)?.pagesMaintenance === true, // Each project's agents work in that project's notes only (docs/pages-maintenance.md). scope: (id, create) => { const notes = projectNotes(id, create); return notes ? { id: notes.id, rel: notes.rel } : null; }, @@ -245,7 +250,7 @@ const attachments = new AttachmentStore(path.join(home, "attachments"), { // before startup inspection can update the installed-version receipt. attachments.setRecoveryRetention(null); const usageLedger = new UsageLedger(store.dir); -const runner = new ThreadRunner({ beforeSend: thread => { if (thread.localTarget === undefined) media?.beforeSend(thread); }, imageOutcome: (id, status) => media?.jobs.outcome(id, status), ...((demo || process.env.MODEX_E2E) ? { imageBackend: imageFixtureBackend } : {}), images: { prepare: (thread, signal) => { if (media) return media.jobs.prepare(thread, signal); return Promise.resolve(undefined); } }, home, store, emit, secrets, knowledge: knowledgeAgents, pages: pagesAgents, usage: usageLedger, backends: { codex: accountCodex, claude: accountClaude }, claudeAccounts, attachments, beforeDeleteThread: (id) => terminals.close(id), withOwnerRemoval }); +const runner = new ThreadRunner({ contextCapture, taskContext: taskContextAgents, beforeSend: thread => { if (thread.localTarget === undefined) media?.beforeSend(thread); }, imageOutcome: (id, status) => media?.jobs.outcome(id, status), ...((demo || process.env.MODEX_E2E) ? { imageBackend: imageFixtureBackend } : {}), images: { prepare: (thread, signal) => { if (media) return media.jobs.prepare(thread, signal); return Promise.resolve(undefined); } }, home, store, emit, secrets, knowledge: knowledgeAgents, pages: pagesAgents, usage: usageLedger, backends: { codex: accountCodex, claude: accountClaude }, claudeAccounts, attachments, beforeDeleteThread: (id) => terminals.close(id), withOwnerRemoval }); let mediaInitializationError: string | undefined; try { media = new Media({ home, store, runner: () => runner, window: () => win, pathReady, offline: Boolean(demo || process.env.MODEX_E2E), changed: kind => { if (win && !win.isDestroyed()) win.webContents.send("media:event", { kind }); @@ -304,6 +309,7 @@ async function withOwnerRemoval(scope: DraftRemovalScope, operation: () => Promi return ownerRemoval.run(scope, operation); } function invalidateNativeDocument(): void { + runner.invalidateContextDocument(); ownerRemoval.invalidate(); rendererPreflight.invalidate(); recoveryQuitPreflight.invalidate(); recoveryQuitOperation = undefined; @@ -559,7 +565,7 @@ function drainServices(): Promise { modexAccount.cancel(); chatgpt.cancel(); claudeLogin.cancel(); retirer.stop(); drainingServices = (async () => { // Start every disposer even if another throws before returning its promise. - const operations = [() => media?.dispose(), () => pagesAgents.dispose(), () => knowledgeAgents.dispose().then(() => knowledge.dispose()), + const operations = [() => media?.dispose(), () => pagesAgents.dispose(), () => taskContextAgents.dispose(), () => knowledgeAgents.dispose().then(() => knowledge.dispose()), () => desktopHost?.dispose(), () => companion.dispose(), () => terminals.dispose(), () => runner.dispose(), () => codeReviewAgent.dispose()]; const results = await Promise.allSettled(operations.map(dispose => Promise.resolve().then(dispose))); modexAccount.dispose(); chatgpt.dispose(); @@ -813,8 +819,19 @@ handle("project:remove", async ({ projectId }) => { await runner.removeProject(projectId); return store.snapshot(); }); -handle("thread:create", ({ projectId, worktree, mode, model, backend, auto }) => runner.createThread(projectId, { worktree, mode, model, backend, auto })); +handle("thread:create", ({ projectId, worktree, mode, model, backend, auto, contextDraft }, event) => { + if (contextDraft !== undefined && !event) throw new Error("Select draft context from this Mac."); + return runner.createThread(projectId, { worktree, mode, model, backend, auto, contextDraft }); +}); handle("thread:items", ({ threadId }) => runner.items(threadId)); +handle("task:context-draft-open", ({ owner }) => runner.openContextDraft(owner), { localOnly: true }); +handle("task:context-draft-select", ({ binding, targets }) => runner.selectTaskContext(binding, targets), { localOnly: true }); +handle("task:context-draft-close", ({ binding }) => runner.closeContextDraft(binding), { localOnly: true }); +handle("task:context-candidates", ({ binding }) => runner.taskContextCandidates(binding), { localOnly: true }); +handle("task:context-draft", ({ binding }) => runner.draftTaskContext(binding), { localOnly: true }); +handle("task:context", ({ threadId, turnId }) => runner.taskContext(threadId, turnId), { localOnly: true }); +handle("task:context-confirm", ({ threadId, turnId, expectedRevision, refIds }) => runner.confirmTaskContext(threadId, turnId, expectedRevision, refIds), { localOnly: true }); +handle("task:check-confirm", ({ threadId, turnId, expectedRevision, itemId, refIds }) => runner.confirmTaskCheck(threadId, turnId, expectedRevision, itemId, refIds), { localOnly: true }); handle("task:save-note", ({ threadId, turnId }) => runner.saveTaskNote(threadId, turnId)); handle("task:note", ({ pageId }) => runner.taskNote(pageId)); handle("thread:followup", ({ threadId }) => runner.followUp(threadId)); @@ -837,7 +854,7 @@ async function startTurn(threadId: string, run: Promise): Promise<{ ok: bo accepting = false; return earlyError !== undefined ? { ok: false, error: earlyError } : { ok: true }; } -handle("thread:send", ({ threadId, text, attachments: files }, event) => startTurn(threadId, runner.send(threadId, text, files, event ? "desktop" : "paired-desktop"))); +handle("thread:send", ({ threadId, text, attachments: files, contextDraft }, event) => startTurn(threadId, runner.send(threadId, text, files, event ? "desktop" : "paired-desktop", contextDraft))); registerAttachmentCommands(handle, attachments, { bytesOnly: false, reveal: (abs) => shell.showItemInFolder(abs) }); handle("attachments:pick", async (req) => { // Explicit renderer paths keep the strict whole-request guard. Only the native dialog's diff --git a/apps/desktop/src/main/preload.cjs b/apps/desktop/src/main/preload.cjs index 679d1401..61aeec88 100644 --- a/apps/desktop/src/main/preload.cjs +++ b/apps/desktop/src/main/preload.cjs @@ -105,6 +105,7 @@ const onOwnerRemovalPreparation = preparationBridge("drafts:remove-prepare", "dr const onOwnerRemovalAdoption = preparationBridge("drafts:remove-adopt", "drafts:remove-adopted"); contextBridge.exposeInMainWorld("modex", { + taskContextEditing: true, ...(nativeDrafts ? { nativeDrafts } : {}), onRestartPreparation, onRecoveryQuitPreparation, diff --git a/apps/desktop/src/renderer/App.tsx b/apps/desktop/src/renderer/App.tsx index fc132cb5..2f842cc6 100644 --- a/apps/desktop/src/renderer/App.tsx +++ b/apps/desktop/src/renderer/App.tsx @@ -3,6 +3,8 @@ import type { NativeDraftBridge, NativeDraftReadOnlyOpen, PendingSendView } from import type { ComposerRecovery } from "../shared/update-recovery"; import type { Attachment, AttachmentDraftOwner } from "../shared/attachments"; import { useComposerRecovery } from "./useComposerRecovery"; +import { useTaskContextDraft } from "./task-context-draft"; +import { TaskContextSelect } from "./components/transcript/TaskContextPanel"; import { ComposerRestartPreparation } from "../shared/composer-restart-preparation"; import { ComposerOwnerRemoval } from "../shared/composer-owner-removal"; import { restartDrafts } from "./restart-drafts"; @@ -550,6 +552,7 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE }, [draftProjectId]); const thread = useMemo(() => state?.threads.find((t) => t.id === selected) ?? null, [state, selected]); + const contextUi = useTaskContextDraft(thread ? { kind: "thread", threadId: thread.id } : draft ? ownerFor(draftKey) : null); /** Choosing a thread (sidebar, history) discards any draft: an unsent draft never becomes a thread. */ const selectThread = useCallback((id: string) => { if (nativeHoldRef.current) return; @@ -728,13 +731,16 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE const beginNativeSend = async (key: string, rawText: string, configuration: PendingSendView["configuration"]): Promise => { const capability = nativeRef.current; if (!capability || localSelectionKeysRef.current.has(key) || nativeHoldRef.current || nativeRecoveryError || attachmentDrafts.get(key).copying || attachmentDrafts.get(key).sending) return; + const contextSend = contextUi.hold(key); if (!contextSend) return; + let contextAdmitted = false; holdNative(true); try { // Save the exact original before any IPC admission ambiguity or UI clear. composerRecovery.queue.update(captureDrafts()); await composerRecovery.flush(); const hold = composerRecovery.queue.holdForTransfer(); await hold.drained; const files = attachmentDrafts.get(key).attachments; - const intent = await capability.begin({ source: ownerFor(key), rawText, configuration, files }); + const intent = await capability.begin({ source: ownerFor(key), rawText, configuration, files, ...(contextSend.binding ? { contextDraft: contextSend.binding } : {}) }); + contextAdmitted = true; const record = await capability.readPending(intent.id); if (!record || attachmentDrafts.beginSend(key) === null) throw new Error("The original message is preserved. Quit and reopen Modex to recover it."); rememberPending(record); setUnsentFor(key, ""); @@ -747,7 +753,7 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE const message = error instanceof Error ? error.message : String(error); if (composerRecovery.queue.held) setNativeRecoveryError(message); else { holdNative(false); setError({ message }); } - } + } finally { contextUi.release(contextSend, contextAdmitted); } }; // Send/create retries belong to the restored composer: replaying only an inner act() closure // would bypass its capacity and duplicate-send reservations. @@ -761,14 +767,15 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE const { effort, ...settings } = d.settings; return beginNativeSend(draftKey, text, { worktree: d.worktree, settings: { ...settings, ...(effort === undefined ? {} : { effort }) } }); } + const contextSend = contextUi.hold(draftKey); if (!contextSend) return; const attachments = attachmentDrafts.beginSend(draftKey); - if (attachments === null) return; + if (attachments === null) { contextUi.release(contextSend, false); return; } setUnsentFor(draftKey, ""); let restoreKey = draftKey; let restoreRevision = unsentRevision.current.get(draftKey)!; const sent = await act(async () => { const s = d.settings; - const t = await bridge.invoke("thread:create", { projectId: d.projectId, worktree: d.worktree, backend: s.backend, mode: s.mode, model: s.model || undefined, auto: s.auto }); + const t = await bridge.invoke("thread:create", { projectId: d.projectId, worktree: d.worktree, backend: s.backend, mode: s.mode, model: s.model || undefined, auto: s.auto, ...(contextSend.binding ? { contextDraft: contextSend.binding } : {}) }); if (s.plan || s.effort) await bridge.invoke("thread:update", { threadId: t.id, patch: { ...(s.plan ? { plan: true } : {}), ...(s.effort ? { effort: s.effort } : {}) } }); await refresh(); // Transfer ownership with selection, including edits made while thread creation was pending. @@ -783,27 +790,30 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE setDraft(null); setSelected(t.id); } - const r = await bridge.invoke("thread:send", { threadId: t.id, text: text.trim(), ...(attachments.length ? { attachments } : {}) }); + const r = await bridge.invoke("thread:send", { threadId: t.id, text: text.trim(), ...(attachments.length ? { attachments } : {}), ...(contextSend.binding ? { contextDraft: contextSend.binding } : {}) }); if (!r.ok) setError({ message: r.error ?? "send failed" }); return r.ok; }, { retry: false }); attachmentDrafts.finishSend(restoreKey, sent === true); + contextUi.release(contextSend, sent === true); if (sent !== true) restoreUnsentIfCurrent(restoreKey, restoreRevision, text); }; const send = async (text: string): Promise => { if (!thread || localSelectionKeysRef.current.has(thread.id) || thread.retired || thread.status === "running" || thread.status === "waiting") return; const threadId = thread.id; if (native) return beginNativeSend(threadId, text, { worktree: Boolean(thread.worktree), settings: { backend: thread.backend, mode: thread.mode, plan: thread.plan, auto: Boolean(thread.auto), model: thread.model, ...(thread.effort ? { effort: thread.effort } : {}) } }); + const contextSend = contextUi.hold(threadId); if (!contextSend) return; const attachments = attachmentDrafts.beginSend(threadId); - if (attachments === null) return; + if (attachments === null) { contextUi.release(contextSend, false); return; } setUnsentFor(threadId, ""); const revision = unsentRevision.current.get(threadId)!; const sent = await act(async () => { - const r = await bridge.invoke("thread:send", { threadId, text: text.trim(), ...(attachments.length ? { attachments } : {}) }); + const r = await bridge.invoke("thread:send", { threadId, text: text.trim(), ...(attachments.length ? { attachments } : {}), ...(contextSend.binding ? { contextDraft: contextSend.binding } : {}) }); if (!r.ok) setError({ message: r.error ?? "send failed" }); return r.ok; }, { retry: false }); attachmentDrafts.finishSend(threadId, sent === true); + contextUi.release(contextSend, sent === true); if (sent !== true) restoreUnsentIfCurrent(threadId, revision, text); }; /** The failure card's Retry: main runs the thread's last message again without adding a second bubble. */ @@ -1123,6 +1133,8 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE {thread && project ? (
} + contextBusy={contextUi.state.busy || contextUi.state.held} key={thread.id} thread={thread} project={project} @@ -1150,6 +1162,8 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE ) : draft ? (
} + contextBusy={contextUi.state.busy || contextUi.state.held} key={`${draft.projectId}:${draftRevision}`} draft={draft} projects={state.projects} @@ -1172,6 +1186,8 @@ export function App({ persistPreferences = true, spaceEnabled = true, analyticsE ) : null} {thread && loadChanges(thread.id)} onRevert={revert} + onIncludeContext={!bridge.taskContextEditing || thread.retired ? undefined : path => contextUi.include({ kind: "file", path })} + contextBusy={contextUi.state.busy || contextUi.state.held || thread.status === "running" || thread.status === "waiting"} items={items[thread.id] ?? []} subagentsSignal={subagentsSignal} visible={surface === "chat" && showChanges} onShow={() => setLayout({ changes: true })} onHide={() => setLayout({ changes: false })} suspended={suspended || surface !== "chat" || showSettings || showCompanion} diff --git a/apps/desktop/src/renderer/browser-bridge.ts b/apps/desktop/src/renderer/browser-bridge.ts index 82863858..743cf7ac 100644 --- a/apps/desktop/src/renderer/browser-bridge.ts +++ b/apps/desktop/src/renderer/browser-bridge.ts @@ -42,6 +42,7 @@ export function createBrowserBridge(): ModexBridge { if (import.meta.hot) import.meta.hot.dispose(disconnect); return { platform: "web", + taskContextEditing: true, pickFiles: () => new Promise((resolve, reject) => { const input = document.createElement("input"); input.type = "file"; input.multiple = true; input.hidden = true; diff --git a/apps/desktop/src/renderer/components/Composer.tsx b/apps/desktop/src/renderer/components/Composer.tsx index 8fc854ee..3106519d 100644 --- a/apps/desktop/src/renderer/components/Composer.tsx +++ b/apps/desktop/src/renderer/components/Composer.tsx @@ -26,6 +26,8 @@ export interface ComposerContext { } interface Props { + contextControls?: React.ReactNode; + sendBlocked?: boolean; /** The unsent text. Owned by App so it survives switching threads (see App's `unsent`). */ text: string; onText: (text: string) => void; @@ -73,7 +75,7 @@ const MAX_INPUT = 180; * input and one control row — `+` (plan, auto, backend), the access pill (mode), any active chips, * the model picker, and a round send/stop button. */ -export function Composer({ text, onText: setText, attachments, attachmentActions, busy, context, backend, mode, approvals = "ask", onApprovals, plan, model, effort, auto, models, modelsError, onRetryModels, openModelPickerRequest, local, suggestion, onBackend, onMode, onPlan, onModel, onEffort, onAuto, onSend, onStop, inputRef }: Props) { +export function Composer({ contextControls, sendBlocked, text, onText: setText, attachments, attachmentActions, busy, context, backend, mode, approvals = "ask", onApprovals, plan, model, effort, auto, models, modelsError, onRetryModels, openModelPickerRequest, local, suggestion, onBackend, onMode, onPlan, onModel, onEffort, onAuto, onSend, onStop, inputRef }: Props) { const fallbackRef = useRef(null); const ta = inputRef ?? fallbackRef; const { copying, sending, warning: attachWarning, onFiles: attach, onPick: pick, onRemove, onDismissWarning } = attachmentActions; @@ -95,7 +97,7 @@ export function Composer({ text, onText: setText, attachments, attachmentActions }, [busy]); const unsupportedImages = Boolean(local?.current && attachments.some(file => file.kind === "image")); - const canSend = Boolean(text.trim() || attachments.length) && !unsupportedImages; + const canSend = Boolean(text.trim() || attachments.length) && !unsupportedImages && !sendBlocked; const submit = () => { if (!canSend || busy || copying || sending) return; void onSend(text); @@ -146,6 +148,7 @@ export function Composer({ text, onText: setText, attachments, attachmentActions return (
+ {contextControls} {local?.current &&

Runs through Codex at {local.entries.find(e => e.connection.id === local.current!.model.connection.id && e.connection.revision === local.current!.model.connection.revision)?.connection.origin ?? "an unavailable local server"} · connection revision {local.current.model.connection.revision}. A local address does not verify offline operation.

} {unsupportedImages &&

Images are not qualified for local models. Remove the image or choose a cloud model before sending; the attachment is still here.

} diff --git a/apps/desktop/src/renderer/components/DraftView.tsx b/apps/desktop/src/renderer/components/DraftView.tsx index da223fb2..9646bfae 100644 --- a/apps/desktop/src/renderer/components/DraftView.tsx +++ b/apps/desktop/src/renderer/components/DraftView.tsx @@ -26,6 +26,8 @@ export interface Draft { } interface Props { + contextControls?: React.ReactNode; + contextBusy?: boolean; draft: Draft; projects: Project[]; /** True while the first send is creating the thread (a worktree can take a moment). */ @@ -50,7 +52,7 @@ interface Props { * The "What should we build in ?" screen: a live composer before any thread exists. * The project name is a picker; the context strip's Local/Worktree item is a toggle. */ -export function DraftView({ draft, projects, creating, models, modelsError, onRetryModels, local, branch, onChange, onSend, inputRef, text, onText, attachments, attachmentActions }: Props) { +export function DraftView({ contextControls, contextBusy, draft, projects, creating, models, modelsError, onRetryModels, local, branch, onChange, onSend, inputRef, text, onText, attachments, attachmentActions }: Props) { const project = projects.find((p) => p.id === draft.projectId); const set = (patch: Partial) => onChange({ ...draft, settings: { ...draft.settings, ...patch } }); if (!project) return null; @@ -64,6 +66,8 @@ export function DraftView({ draft, projects, creating, models, modelsError, onRe
(null); const reader = useMemo(() => readerStateFor(thread.id), [thread.id]); const restored = useRef(false); @@ -228,7 +232,7 @@ export function ThreadView({ thread, project, items, text, onText, attachments, {turns.map((turn, i) => ( <>} + footer={(t, revealItem) => <>{!t.user?.contextEvidence && }} render={(item, live) => onUpdate({ approvals })} onFix={onFix} onRetry={!thread.retired && !busy && item.id === retryId ? onRetry : undefined} onReply={!thread.retired && !busy && item.id === askId ? (text) => void onSend(text) : undefined} />} /> ))} @@ -250,6 +254,8 @@ export function ThreadView({ thread, project, items, text, onText, attachments,
} {thread.retired ?

Pull request #{thread.retired.pr} merged. This task is finished; start a new thread to continue.

: }) { const status = toolStatusLabel(item); const outcome = count !== undefined ? `${count} ${count === 1 ? "result" : "results"}` : status.toLowerCase(); return ( -
+
{markdown && } {selected && } + {selected && onIncludeContext && } {selected && } setRevision((n) => n + 1)} /> {crumb !== null &&
diff --git a/apps/desktop/src/renderer/components/transcript/TaskContextPanel.tsx b/apps/desktop/src/renderer/components/transcript/TaskContextPanel.tsx new file mode 100644 index 00000000..88e174bc --- /dev/null +++ b/apps/desktop/src/renderer/components/transcript/TaskContextPanel.tsx @@ -0,0 +1,126 @@ +import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; +import { bridge } from "../../bridge"; +import type { ContextDraftUi } from "../../task-context-draft"; +import { contextRefId, type ContextSource, type ContextTarget, type TaskCheckEvidence, type TaskContextEvidence } from "../../../shared/task-context-evidence"; +import { useExpansionState } from "./reader-state"; +import { omittedCommandLabel, type TaskContext } from "../../../shared/task-context"; +import { UnlistedCommands } from "./TaskPlanCard"; +import { Icon } from "../ui/Icon"; + +export const contextTargetLabel = (target: ContextTarget): string => target.kind === "file" ? target.path : target.kind === "note" ? `Note ${target.pageId}` : `Notes file ${target.id}`; +type Ref = TaskContextEvidence["refs"][number]; + +function RecordedCheckMappings({ context, refId }: { context: TaskContextEvidence; refId: string }) { + const checks = context.checks.filter(check => check.relevantRefIds.includes(refId)); + return <>{checks.slice(0, 3).map(check =>

Relevant to recorded check: {check.commandOmission ? omittedCommandLabel(check.commandOmission) : check.command.length > 160 ? `${check.command.slice(0, 160)}…` : check.command} · {check.relevance} · {check.itemId}

)}{checks.length > 3 &&

{checks.length - 3} more check mappings are retained in the full saved record.

}; +} + +function ContextSourceLabel({ source, onRevealItem }: { source: ContextSource; onRevealItem?: (id: string) => void }) { + const link = source.kind === "native" ? { itemId: source.itemId } : source.kind === "notes-read" || source.kind === "model-proposal" ? source.nativeLink : undefined; + const text = source.kind === "human" ? `Source: you · ${source.at}` : source.kind === "native" ? `Source: ${source.origin.backend} native tool` + : source.kind === "notes-read" ? `Source: Modex Notes ${source.method} · ${source.at}` : source.kind === "model-proposal" ? `Source: ${source.turn.backend} proposal · ${source.at}` : `Source: historical tool; native origin unavailable · ${source.at}`; + return

{text}{link && (onRevealItem ? <> · : <> · native item {link.itemId})}

; +} + +/** Expanding these rows is a pure view of retained metadata. It makes no bridge call. */ +export function ContextReferences({ context, renderControls, onRevealItem }: { context: TaskContextEvidence; renderControls?: (ref: Ref) => ReactNode; onRevealItem?: (id: string) => void }) { + const [all, setAll] = useState(false), refs = all ? context.refs : context.refs.slice(0, 20); + const captures = useMemo(() => new Map(context.captures.map(capture => [capture.id, capture])), [context.captures]); + return <> +
    {refs.map(ref => { + const observed = ref.relations.some(r => r.kind === "read" && r.provenance === "observed"); + return
  • {contextTargetLabel(ref.target)} + {ref.relations.some(r => r.provenance === "model-proposed") && Proposed by model} + {[...new Set(ref.relations.filter(relation => relation.provenance === "model-proposed" && relation.role).map(relation => relation.role!))].map(role => Proposed role: {role})} + {ref.relations.some(r => r.provenance === "user-confirmed") && Confirmed by you} + {observed ? Observed read : {ref.relations.some(r => r.provenance === "user-confirmed") ? "Selected, not inspected" : "Not inspected"}} + {ref.relations.some(r => r.kind === "changed" && r.provenance === "observed") && Observed change} + {[...new Map(ref.relations.map(relation => [JSON.stringify(relation.source), relation.source])).values()].map((source, i) => )} + {renderControls?.(ref)} + {ref.captureIds.map(id => captures.get(id)).filter(c => c !== undefined).map(c =>
    {c.phase} · +

    Observation started . {c.revision.state === "available" ? <>{c.revision.digest} · {c.revision.bytes} bytes : <>Revision unavailable: {c.revision.reason}}

    + {c.noteVersion &&

    Note revision {c.noteVersion.revision} · {c.noteVersion.via} · {c.noteVersion.updatedAt}

    } +

    {c.repository.state === "available" ? <>Repository HEAD {c.repository.head} (not a dirty-tree snapshot) : <>Repository HEAD unavailable: {c.repository.reason}}

    +
    )} +
  • ; + })}
+ {context.refs.length > 20 && } + ; +} + +export function TaskContextSelect({ ui }: { ui: ContextDraftUi }) { + const trigger = useRef(null), [all, setAll] = useState(false), state = ui.state; + if (!state.owner || !bridge.taskContextEditing) return null; + const candidates = [ + ...(state.candidates?.files.paths ?? []).map(path => ({ target: { kind: "file" as const, path }, title: path })), + ...(state.candidates?.notes.items ?? []).map(row => ({ target: row.target, title: `Note: ${row.title}` })), + ]; + const busy = state.busy || state.held; + return
+ + {state.open &&
{ if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); ui.close(); trigger.current?.focus(); } }}> +

Select relevant references. Selection records a snapshot; it does not establish agent inspection or grant tool access.

+ {state.error &&

{state.error}

} + {state.error && state.binding && } + {state.busy &&

Loading context…

} + {!state.binding && !state.busy && } +
{(all ? candidates : candidates.slice(0, 20)).map(row =>
{row.target.kind === "note" && }
)}
+ {candidates.length > 20 && } + {(state.candidates?.files.truncated || state.candidates?.notes.truncated) &&

The candidate list is bounded and may omit references.

} + + {state.context && } +
} +
; +} + +export function TaskContextPanel({ context, reportedContext, threadId, turnId, readOnly = false, onRevealItem }: { context?: TaskContextEvidence; reportedContext?: TaskContext; threadId?: string; turnId?: string; readOnly?: boolean; onRevealItem?: (id: string) => void }) { + const [open, setOpen] = useExpansionState(`task-context:${turnId ?? "saved"}`, false), [busy, setBusy] = useState(false), [error, setError] = useState(""); + if (!context || (!context.refs.length && !context.checks.length && !reportedContext?.outside && !reportedContext?.unlisted?.length)) return null; + const editable = !readOnly && bridge.taskContextEditing === true; + const confirm = async (id: string, selected: boolean) => { + if (!editable || !threadId || !turnId || busy) return; + const refIds = context.refs.filter(ref => ref.id === id ? selected : ref.relations.some(r => r.provenance === "user-confirmed")).map(ref => ref.id); + setBusy(true); setError(""); + try { await bridge.invoke("task:context-confirm", { threadId, turnId, expectedRevision: context.revision, refIds }); } + catch (failure) { setError(failure instanceof Error ? failure.message : String(failure)); } + finally { setBusy(false); } + }; + return
+ + {open &&
+ {!editable && !readOnly &&

Edit context on the desktop that owns this project.

} + {readOnly &&

Saved metadata. Read-only; expanding it does not refresh any observation.

} + {error &&

{error}

} +

Snapshots record timed observations; process input bytes remain unverified.

+ {Boolean(reportedContext?.outside) &&

{reportedContext!.outside} outside the project, not named

} + {reportedContext?.unlisted?.length ? : null} + {context.capabilities?.map(receipt =>

Attempt setup reported: {receipt.capability.state}{receipt.capability.state === "unavailable" ? ` (${receipt.capability.reason})` : ""} · {receipt.at}

)} + {!editable ? } /> :
Relevant to this request
{ref.target.kind === "note" && }
} />
} + {context.checks.map(check => )} +
} +
; +} + +function CheckRelevance({ check, context, threadId, turnId, readOnly, onRevealItem }: { check: TaskCheckEvidence; context: TaskContextEvidence; threadId?: string; turnId?: string; readOnly: boolean; onRevealItem?: (id: string) => void }) { + const [selected, setSelected] = useState(check.relevantRefIds), [all, setAll] = useState(false), [busy, setBusy] = useState(false), [error, setError] = useState(""); + const baseline = JSON.stringify(check.relevantRefIds); + useEffect(() => { setSelected(check.relevantRefIds); }, [baseline]); + const apply = async (refIds: string[]) => { + if (readOnly || !threadId || !turnId || busy) return; + setBusy(true); setError(""); + try { await bridge.invoke("task:check-confirm", { threadId, turnId, expectedRevision: context.revision, itemId: check.itemId, refIds }); } + catch (failure) { setError(failure instanceof Error ? failure.message : String(failure)); } + finally { setBusy(false); } + }; + const label = check.commandOmission ? omittedCommandLabel(check.commandOmission) : check.command; + return
{label} +

{check.origin ? <>Source: {check.origin.backend} native check · {onRevealItem ? : {check.itemId}} : "Native check origin unavailable"}

+

{check.outcome} · exit {check.exitCode ?? "not reported"} · {check.scope} scope · {check.relevance}

{check.startedAt ?? "Start unavailable"} → {check.finishedAt ?? "Completion unavailable"}

+ {error &&

{error}

} + {!readOnly && <>

Choose references this check is relevant to. Its result and snapshots stay as recorded.

+ {(all ? context.refs : context.refs.slice(0, 20)).map(ref => )} + {context.refs.length > 20 && } + + } +
; +} diff --git a/apps/desktop/src/renderer/components/transcript/TaskPlanCard.tsx b/apps/desktop/src/renderer/components/transcript/TaskPlanCard.tsx index 4d2144b5..9dc36297 100644 --- a/apps/desktop/src/renderer/components/transcript/TaskPlanCard.tsx +++ b/apps/desktop/src/renderer/components/transcript/TaskPlanCard.tsx @@ -3,7 +3,7 @@ import type { ThreadItem } from "../../../shared/types"; import type { PlanStep } from "../../../shared/conversation-events"; import { bridge } from "../../bridge"; import { ErrorNotice } from "../ui/ErrorNotice"; -import { CONTEXT_SHOWN, UNLISTED_WORDS, contextCount, displayCommand, noPlan, noPlanText, taskContext, verification, type TaskCheck, type TaskVerification } from "../../../shared/task-context"; +import { CONTEXT_SHOWN, UNLISTED_WORDS, contextCount, displayCommand, omittedCommandLabel, noPlan, noPlanText, taskContext, verification, verificationWords, type TaskCheck, type TaskContext, type TaskVerification } from "../../../shared/task-context"; export const PLAN_STATUS: Record = { pending: "Not started", "in-progress": "In progress", completed: "Reported done", unknown: "Status unknown" }; export const PLAN_FROM = { "codex-plan": "Codex's plan", "claude-todo": "Claude's todo list", "claude-tasks": "Claude's task list", "acp-plan": "The agent's plan" } as const; @@ -64,13 +64,15 @@ export function TaskNoPlanLine({ items }: { items: readonly ThreadItem[] }) { /** One check, as its script, with the exact command the CLI reported on hover. */ function Check({ check }: { check: TaskCheck }) { - return {displayCommand(check.command)}; + const label = check.commandOmission ? omittedCommandLabel(check.commandOmission) : displayCommand(check.command); + return {label}; } /** The checks named in a line: the first two, then how many more. */ function Checks({ checks }: { checks: TaskCheck[] }) { - const unique = checks.filter((check, i) => checks.findIndex((other) => displayCommand(other.command) === displayCommand(check.command)) === i); - return <>{unique.slice(0, 2).map((check, i) => {i ? " and " : ""})}{unique.length > 2 ? ` and ${unique.length - 2} more` : ""}; + const key = (check: TaskCheck) => check.commandOmission ? `omitted:${check.commandOmission.id}` : `text:${displayCommand(check.command)}`; + const unique = checks.filter((check, i) => checks.findIndex(other => key(other) === key(check)) === i); + return <>{unique.slice(0, 2).map((check, i) => {i ? " and " : ""})}{unique.length > 2 ? ` and ${unique.length - 2} more` : ""}; } /** @@ -86,6 +88,7 @@ export function TaskVerificationLine({ items }: { items: readonly ThreadItem[] } /** What a turn's checks say, in words: the footer's line, and a reopened task note's (docs/task-context.md#reopening-a-saved-note). */ export function VerificationText({ v }: { v: TaskVerification }) { + if (v.basis === "context-snapshots") return <>{verificationWords(v)}{v.checks.length ? <> Recorded checks: . : null}; const current = v.checks.filter((check) => !check.stale); const failing = current.filter((check) => !check.passed && (check.outcome === "failed" || check.outcome === "denied" || (typeof check.exitCode === "number" && check.exitCode !== 0))); const text = v.state === "verified" ? <>Verified: passed after the last change @@ -130,11 +133,12 @@ export function TaskContextLine({ items, cwd }: { items: readonly ThreadItem[]; } /** Commands that may have changed files no CLI named, shown as their scripts with the exact command on hover. */ -export function UnlistedCommands({ commands, testId }: { commands: readonly string[]; testId: string }) { +export function UnlistedCommands({ commands, omissions, testId }: { commands: readonly string[]; omissions?: TaskContext["unlistedOmissions"]; testId: string }) { + const byIndex = new Map(omissions?.map(row => [row.index, row.omission])); return <>

{UNLISTED_WORDS}

    - {commands.slice(0, CONTEXT_SHOWN).map((command) =>
  • {displayCommand(command)}
  • )} + {commands.slice(0, CONTEXT_SHOWN).map((command, index) => { const omission = byIndex.get(index), label = omission ? omittedCommandLabel(omission) : displayCommand(command); return
  • {label}
  • ; })} {commands.length > CONTEXT_SHOWN &&
  • and {commands.length - CONTEXT_SHOWN} more
  • }
; diff --git a/apps/desktop/src/renderer/components/transcript/TurnContent.tsx b/apps/desktop/src/renderer/components/transcript/TurnContent.tsx index d6629088..af88e73a 100644 --- a/apps/desktop/src/renderer/components/transcript/TurnContent.tsx +++ b/apps/desktop/src/renderer/components/transcript/TurnContent.tsx @@ -45,8 +45,9 @@ export function groupTurns(items: ThreadItem[]): Turn[] { * Keep the final answer and actionable items visible; completed work stays inspectable. Which item * answers, which are live and which may fold comes from `layoutTurn` (shared/turn-layout.ts). */ -export function TurnContent({ turn, status, render, footer }: { turn: Turn; status?: Thread["status"]; render: (item: ThreadItem, live: boolean) => ReactNode; footer?: (turn: Turn) => ReactNode }) { +export function TurnContent({ turn, status, render, footer }: { turn: Turn; status?: Thread["status"]; render: (item: ThreadItem, live: boolean) => ReactNode; footer?: (turn: Turn, revealItem: (id: string) => void) => ReactNode }) { const [expanded, setExpanded] = useExpansionState(`turn:${turn.user?.id ?? turn.rest[0]?.id}`, false); + const [focusedItem, setFocusedItem] = useState(null); const historyId = useId(); const live = status !== undefined; const layout = layoutTurn(turn.rest, { busy: live }); @@ -57,6 +58,15 @@ export function TurnContent({ turn, status, render, footer }: { turn: Turn; stat const content = useRef(null); const toggle = useRef(null); const wasLive = useRef(live); + useLayoutEffect(() => { + if (!focusedItem || !showWork) return; + const target = [...(content.current?.querySelectorAll("[data-item-id]") ?? [])].find(element => element.dataset.itemId === focusedItem); + target?.scrollIntoView({ block: "nearest" }); target?.focus({ preventScroll: true }); setFocusedItem(null); + }, [focusedItem, showWork]); + const revealItem = (id: string) => { + if (!turn.rest.some(item => item.kind === "tool" && item.id === id)) return; + setExpanded(true); setFocusedItem(id); + }; useLayoutEffect(() => { const folded = wasLive.current && !live && !showWork; wasLive.current = live; @@ -75,7 +85,7 @@ export function TurnContent({ turn, status, render, footer }: { turn: Turn; stat , ])}
- {!live && turn.user && turn.rest.length > 0 && footer?.(turn)} + {!live && turn.user && turn.rest.length > 0 && footer?.(turn, revealItem)} ; } diff --git a/apps/desktop/src/renderer/space/TaskNoteView.tsx b/apps/desktop/src/renderer/space/TaskNoteView.tsx index 000a2c0a..ed6a459b 100644 --- a/apps/desktop/src/renderer/space/TaskNoteView.tsx +++ b/apps/desktop/src/renderer/space/TaskNoteView.tsx @@ -1,6 +1,7 @@ -import { useEffect, useState } from "react"; +import { TaskContextPanel } from "../components/transcript/TaskContextPanel"; +import { useEffect, useMemo, useState } from "react"; import type { SpacePage } from "../../shared/space"; -import { CONTEXT_SHOWN, PROPOSED_WORDS, clarificationLine, contextCount, type TaskNoteView as View } from "../../shared/task-context"; +import { CONTEXT_SHOWN, PROPOSED_WORDS, clarificationLine, contextCount, portableTaskRecord, taskMarkdown, type TaskNoteView as View } from "../../shared/task-context"; import { bridge } from "../bridge"; import { PLAN_FROM, PLAN_STATUS, UnlistedCommands, VerificationText } from "../components/transcript/TaskPlanCard"; @@ -34,11 +35,23 @@ export function useTaskNote(page: SpacePage | undefined, watch: boolean): View | * CLI or edits the note; continuing is a new message in the thread. */ export function TaskNoteView({ view, onOpenThread, onClose }: { view: View; onOpenThread?: (threadId: string) => void; onClose(): void }) { - const { record } = view; + const record = useMemo(() => portableTaskRecord(view.record), [view.record]); const context = record.context; const verification = record.verification; + const [copy, setCopy] = useState<{ busy: boolean; copied?: boolean; error?: string }>({ busy: false }); + const copyRecord = async () => { + setCopy({ busy: true }); + try { + const identity = [`Saved at: ${view.savedAt}`, `Saved revision: ${view.savedRevision}`, `Project: ${JSON.stringify(view.projectId)}`, `Note: ${JSON.stringify(view.pageId)}`, `Thread: ${JSON.stringify(view.threadId)}`, `Turn: ${JSON.stringify(view.turnId)}`].join("\n\n"); + await bridge.invoke("clipboard:write", { text: `${taskMarkdown(record, { fullContext: true })}\n## Saved record identity\n\n${identity}\n` }); + setCopy({ busy: false, copied: true }); + } catch (error) { setCopy({ busy: false, error: (error as Error).message }); } + }; return
Task view
+ + {copy.copied &&

Full saved record copied

} + {copy.error &&

Could not copy the saved record: {copy.error}

}

Saved {new Date(view.savedAt).toLocaleString()} as revision {view.savedRevision}. Read-only: nothing here runs again. To continue, send a new message in the thread. {view.thread && onOpenThread && <> } @@ -63,16 +76,17 @@ export function TaskNoteView({ view, onOpenThread, onClose }: { view: View; onOp {!record.plan && record.proposedPlan && <>

Plan

{PROPOSED_WORDS}

{record.proposedPlan}
} - {context && (context.files.length > 0 || context.outside > 0 || (context.unlisted?.length ?? 0) > 0) && <>

Files touched · {contextCount(context)}

+ {!record.contextEvidence && context && (context.files.length > 0 || context.outside > 0 || (context.unlisted?.length ?? 0) > 0) && <>

Files touched · {contextCount(context)}

As the CLI reported them. Modex did not index the project.

    {context.files.slice(0, CONTEXT_SHOWN).map((file) =>
  • {file.path} {file.relations.join(", ")}
  • )} {context.files.length > CONTEXT_SHOWN &&
  • and {context.files.length - CONTEXT_SHOWN} more
  • } {context.outside > 0 &&
  • {context.outside} outside the project, not named
  • }
- {context.unlisted?.length ? : null}} + {context.unlisted?.length ? : null}} {verification && (verification.checks.length > 0 || verification.edits > 0) && <>

Verification

} + {record.answer && <>

Answer

{record.answer}

}
; } diff --git a/apps/desktop/src/renderer/styles.css b/apps/desktop/src/renderer/styles.css index ed7f7fda..96de81c3 100644 --- a/apps/desktop/src/renderer/styles.css +++ b/apps/desktop/src/renderer/styles.css @@ -458,7 +458,7 @@ a.thread-pr:hover { text-decoration: underline; } /* ── Composer: 960×109 at rest, with a 38 px context strip inset 13 px. ── */ .composer { flex: none; padding: 0 16px 16px; } -.composer-inner { max-width: 960px; margin: 0 auto; } +.composer-inner { container-type: inline-size; container-name: composer; max-width: 960px; margin: 0 auto; } .composer-context { display: flex; align-items: center; gap: 28px; height: 38px; margin: 0 13px; padding: 0 14px; overflow: hidden; border-radius: 12px 12px 0 0; background: var(--bg-composer-context); color: var(--text-1); font-size: var(--text-sm); white-space: nowrap; box-shadow: inset 0 1px rgba(255, 255, 255, .025); } .context-item { display: inline-flex; align-items: center; gap: 7px; min-width: 0; } .context-item .icon-svg { color: var(--text-2); } @@ -1093,3 +1093,30 @@ button.turn-label:hover { color: var(--text-2); } .ui-size-options { display: inline-flex; gap: 4px; padding: 3px; border-radius: 10px; background: var(--bg-composer); } .ui-size-options button { height: 28px; padding: 0 14px; border: 0; border-radius: 7px; background: transparent; color: var(--text-2); font: inherit; font-size: 13px; cursor: pointer; } .ui-size-options button[aria-checked="true"] { background: var(--bg-row-selected); color: var(--text-1); } + +.task-context-select { position: relative; display: flow-root; margin: 0 27px; z-index: 4; font-size: 12px; } +.task-context-select > button { position: absolute; top: calc(100% + 4px); right: 0; color: var(--muted); } +.task-context-select > .task-context-panel { max-height: min(340px, calc(100vh - 360px)); background: var(--bg-elevated); } +.composer-inner:has(> .task-context-select) > .composer-context { padding-right: 206px; } +.task-context-compact-icon { display: none; } +@container composer (max-width: 420px) { + .task-context-select > button.btn.small { display: grid; place-items: center; width: 26px; height: 26px; padding: 0; } + .task-context-label { display: none; } + .task-context-compact-icon { display: inline-flex; } + .composer-inner:has(> .task-context-select) > .composer-context { gap: 12px; padding-left: 8px; padding-right: 48px; } + .composer-inner:has(> .task-context-select) > .composer-context .context-item { gap: 4px; } +} +.task-context-panel { max-height: 340px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin: 6px 0; text-align: left; } +.task-context-panel p { margin: 6px 0; } +.task-context-panel button { margin: 4px 8px 4px 0; } +.task-context-candidates { display: grid; gap: 6px; } +.task-context-candidates label { display: flex; align-items: center; gap: 8px; overflow-wrap: anywhere; } +.task-context-refs { list-style: none; margin: 10px 0; padding: 0; display: grid; gap: 10px; } +.task-context-refs li { display: grid; gap: 4px; overflow-wrap: anywhere; } +.task-context-refs li > span { color: var(--muted); } +.task-context-refs code { overflow-wrap: anywhere; } + +.task-view > .task-context-turn { display: block; min-width: 0; } +.task-context-panel { box-sizing: border-box; width: 100%; min-width: 0; } +.task-context-confirm { display: flex; align-items: center; gap: 8px; min-width: 0; overflow-wrap: anywhere; } +.task-context-panel fieldset { min-width: 0; margin: 8px 0; border: 1px solid var(--line); border-radius: 6px; padding: 8px; } diff --git a/apps/desktop/src/renderer/task-context-draft.ts b/apps/desktop/src/renderer/task-context-draft.ts new file mode 100644 index 00000000..09003617 --- /dev/null +++ b/apps/desktop/src/renderer/task-context-draft.ts @@ -0,0 +1,98 @@ +import { useLayoutEffect, useMemo, useReducer, useRef } from "react"; +import { bridge } from "./bridge"; +import { contextRefId, type ContextDraftBinding, type ContextDraftOwner, type ContextTarget, type TaskContextCandidates, type TaskContextEvidence } from "../shared/task-context-evidence"; + +type Entry = { key: string; owner: ContextDraftOwner | null; active: boolean; generation: number; held: boolean; open: boolean; busy: boolean; error: string; binding?: ContextDraftBinding; context?: TaskContextEvidence; candidates?: TaskContextCandidates; selected: ContextTarget[] }; +export type ContextSend = { entry: Entry; binding?: ContextDraftBinding }; +export interface ContextDraftUi { + state: Entry; + toggle(): void; + close(): void; + load(): Promise; + reload(): Promise; + choose(target: ContextTarget, selected: boolean): void; + apply(): Promise; + include(target: ContextTarget): Promise; + hold(key: string): ContextSend | null; + release(send: ContextSend, consumed: boolean): void; +} +const ownerKey = (owner: ContextDraftOwner | null) => owner?.kind === "thread" ? owner.threadId : owner?.key ?? ""; +const disposeBinding = (binding?: ContextDraftBinding) => { if (binding) void bridge.invoke("task:context-draft-close", { binding }).catch(() => {}); }; + +/** A main-minted draft binding stays held across create/send, even if selection changes meanwhile. */ +export function useTaskContextDraft(owner: ContextDraftOwner | null): ContextDraftUi { + const [, redraw] = useReducer((n: number) => n + 1, 0); + const entries = useRef(new Map()); + const key = ownerKey(owner); + const entry = useMemo(() => { + const held = entries.current.get(key); if (held?.held) return held; + const created = { key, owner, active: true, generation: 0, held: false, open: false, busy: false, error: "", selected: [] }; + return created; + }, [key]); + useLayoutEffect(() => { + entries.current.set(key, entry); + entry.active = true; + return () => { entry.active = false; if (!entry.held) { entry.generation++; disposeBinding(entry.binding); entry.binding = undefined; if (entries.current.get(key) === entry) entries.current.delete(key); } }; + }, [entry]); + const notify = () => { if (entry.active) redraw(); }; + const load = async () => { + if (!entry.owner || entry.busy || entry.held || (entry.binding && entry.context && entry.candidates)) return; + const generation = entry.generation; entry.busy = true; entry.error = ""; notify(); + let opened: ContextDraftBinding | undefined; + try { + opened = await bridge.invoke("task:context-draft-open", { owner: entry.owner }); + if (!entry.active || entry.generation !== generation) { disposeBinding(opened); return; } + const context = await bridge.invoke("task:context-draft", { binding: opened }); + const candidates = await bridge.invoke("task:context-candidates", { binding: opened }); + if (!entry.active || entry.generation !== generation) { disposeBinding(opened); return; } + entry.binding = opened; entry.context = context; entry.candidates = candidates; entry.selected = context.refs.map(ref => ref.target); + } catch (error) { + disposeBinding(opened); + if (entry.active && entry.generation === generation) { entry.binding = undefined; entry.error = error instanceof Error ? error.message : String(error); } + } finally { if (entry.generation === generation) { entry.busy = false; notify(); } } + }; + const apply = async () => { + if (!entry.binding || entry.busy || entry.held) return; + const binding = entry.binding, generation = entry.generation, targets = structuredClone(entry.selected); + entry.busy = true; entry.error = ""; notify(); + try { + const selected = await bridge.invoke("task:context-draft-select", { binding, targets }); + if (!entry.active || entry.generation !== generation) return; + entry.binding = selected.binding; entry.context = selected.context; + } catch (error) { if (entry.active && entry.generation === generation) entry.error = error instanceof Error ? error.message : String(error); } + finally { if (entry.generation === generation) { entry.busy = false; notify(); } } + }; + return { + state: entry, load, apply, + reload: async () => { + if (entry.busy || entry.held || !entry.active) return; + entry.generation++; disposeBinding(entry.binding); entry.binding = undefined; entry.candidates = undefined; + await load(); + }, + toggle: () => { entry.open = !entry.open; notify(); if (entry.open) void load(); }, + close: () => { entry.open = false; notify(); }, + choose: (target, selected) => { + if (entry.busy || entry.held) return; + entry.selected = entry.selected.filter(value => contextRefId(value) !== contextRefId(target)); + if (selected) entry.selected.push(target); notify(); + }, + include: async target => { + entry.open = true; notify(); await load(); if (!entry.binding || entry.busy || entry.held) return; + if (!entry.selected.some(value => contextRefId(value) === contextRefId(target))) entry.selected.push(target); + await apply(); + }, + hold: source => { + if (source !== key || entry.busy || entry.held || (entry.context?.refs.length && !entry.binding)) return null; + entry.held = true; notify(); return { entry, ...(entry.binding ? { binding: structuredClone(entry.binding) } : {}) }; + }, + release: (send, consumed) => { + const previous = send.entry; previous.held = false; + if (consumed || !previous.active) { + previous.generation++; disposeBinding(previous.binding); previous.binding = undefined; + if (consumed) { previous.context = undefined; previous.candidates = undefined; previous.selected = []; previous.open = false; } + if (!previous.active && entries.current.get(previous.key) === previous) entries.current.delete(previous.key); + } + if (previous.active) redraw(); + }, + }; +} diff --git a/apps/desktop/src/shared/conversation-events.ts b/apps/desktop/src/shared/conversation-events.ts index 71faa70e..dfa7988b 100644 --- a/apps/desktop/src/shared/conversation-events.ts +++ b/apps/desktop/src/shared/conversation-events.ts @@ -73,7 +73,13 @@ export interface PlanStep { text: string; status: PlanStepStatus } /** Where a plan came from: Codex's `turn/plan/updated`, Claude Code's `TodoWrite` tool, its `TaskCreate`/`TaskUpdate` tools, or an ACP agent's `plan` update (OpenCode / MiMo Code `todowrite`). */ export type PlanSource = "codex-plan" | "claude-todo" | "claude-tasks" | "acp-plan"; +/** Static field addresses only, never a redacted value or a credential. */ +export type UnavailableNativeInput = "args.file_path" | "args.notebook_path" | "args.path" | "exec.command" | "exec.cwd" | `exec.actions.${number}.path` | `args.files.${number}.path` | `args.files.${number}.movePath`; + export interface ToolCall { + /** Main changed displayed evidence inputs; only separately retained native metadata may establish targets. */ + displayOnlyInputs?: true; + unavailableInputs?: UnavailableNativeInput[]; name: string; title: string; args: Record; @@ -83,6 +89,8 @@ export interface ToolCall { } export interface ToolProgress { + displayOnlyInputs?: true; + unavailableInputs?: UnavailableNativeInput[]; args?: Record; title?: string; agent?: AgentActivity; @@ -249,7 +257,7 @@ export function applyConversationEvent(state: ConversationState, event: Conversa } case "tool.start": { if (entry) return state; - const item: ConversationItem = { id: newId, kind: "tool", name: event.tool.name, title: event.tool.title, args: event.tool.args, ...(event.tool.agent ? { agent: event.tool.agent } : {}), ...(event.tool.exec ? { exec: event.tool.exec } : {}), status: "running", outcome: "running", origin, at: event.at }; + const item: ConversationItem = { id: newId, kind: "tool", name: event.tool.name, title: event.tool.title, args: event.tool.args, ...(event.tool.displayOnlyInputs ? { displayOnlyInputs: true } : {}), ...(event.tool.unavailableInputs ? { unavailableInputs: event.tool.unavailableInputs } : {}), ...(event.tool.agent ? { agent: event.tool.agent } : {}), ...(event.tool.exec ? { exec: event.tool.exec } : {}), status: "running", outcome: "running", origin, at: event.at }; return draft.add(item).entry(key, { ...fresh("tool", turn), ids: { tool: newId } }).done(); } case "plan.update": { @@ -348,6 +356,8 @@ export function parseConversationEvent(value: unknown): ConversationEvent | null case "reasoning.delta": return (value.representation === "summary" || value.representation === "provider-text") && Number.isSafeInteger(value.index) && (value.index as number) >= 0 && isString(value.text); case "reasoning.done": return optional(value.summary, strings) && optional(value.text, strings); case "tool.start": return isRecord(value.tool) && isString(value.tool.name) && isString(value.tool.title) && isRecord(value.tool.args) && optional(value.tool.agent, isRecord) + && optional(value.tool.displayOnlyInputs, (x) => x === true) + && optional(value.tool.unavailableInputs, unavailableInputs) && optional(value.tool.exec, (x) => isRecord(x) && isString(x.command) && optional(x.cwd, isString) && optional(x.actions, Array.isArray)); case "tool.update": return progress(value.progress); case "tool.done": return SETTLED.includes(value.outcome as string) && optional(value.progress, progress); @@ -454,8 +464,13 @@ function isString(value: unknown): value is string { return typeof value === "st function text(value: unknown): value is string { return typeof value === "string" && value.length > 0; } function strings(value: unknown): boolean { return Array.isArray(value) && value.every(isString); } function optional(value: unknown, check: (v: unknown) => boolean): boolean { return value === undefined || check(value); } +function unavailableInputs(value: unknown): boolean { + return Array.isArray(value) && value.length <= 10_000 && value.every(field => typeof field === "string" && /^(?:args\.(?:file_path|notebook_path|path)|exec\.(?:command|cwd)|exec\.actions\.(?:0|[1-9]\d{0,5})\.path|args\.files\.(?:0|[1-9]\d{0,5})\.(?:path|movePath))$/.test(field)); +} function progress(value: unknown): boolean { return isRecord(value) && optional(value.args, isRecord) && optional(value.title, isString) && optional(value.agent, isRecord) && optional(value.output, isString) + && optional(value.displayOnlyInputs, (x) => x === true) + && optional(value.unavailableInputs, unavailableInputs) && optional(value.durationMs, (d) => typeof d === "number" && Number.isFinite(d)) && optional(value.outputBytes, (b) => Number.isSafeInteger(b) && (b as number) >= 0) && optional(value.outputTruncated, (t) => typeof t === "boolean") && optional(value.exitCode, (c) => c === null || Number.isSafeInteger(c)) diff --git a/apps/desktop/src/shared/native-drafts.ts b/apps/desktop/src/shared/native-drafts.ts index effd350a..a6a0114a 100644 --- a/apps/desktop/src/shared/native-drafts.ts +++ b/apps/desktop/src/shared/native-drafts.ts @@ -15,6 +15,7 @@ export interface PendingExecutionBinding { export interface PendingSendRecord { schema: 1; execution?: PendingExecutionBinding; + context?: import("./task-context-evidence.js").PendingTaskContext; id: string; createdAt: string; source: AttachmentDraftOwner; @@ -27,7 +28,7 @@ export interface PendingSendRecord { creation: PendingCreation | null; target: PendingTarget | null; } -export interface PendingSendInput { source: AttachmentDraftOwner; rawText: string; configuration: Pick, "worktree" | "settings">; files: Attachment[] } +export interface PendingSendInput { source: AttachmentDraftOwner; rawText: string; configuration: Pick, "worktree" | "settings">; files: Attachment[]; contextDraft?: import("./task-context-evidence.js").ContextDraftBinding } export interface NativeTransferRequest { id: string; sequence: number; snapshot: ComposerRecovery; intentId: string; files: Attachment[] } export type NativeTransferReply = { status: "committed"; revision: number; files: Attachment[] } | { status: "unchanged" | "stale"; revision: number }; diff --git a/apps/desktop/src/shared/task-context-evidence.ts b/apps/desktop/src/shared/task-context-evidence.ts new file mode 100644 index 00000000..189d16d3 --- /dev/null +++ b/apps/desktop/src/shared/task-context-evidence.ts @@ -0,0 +1,224 @@ +/** Serializable, body-free task provenance. Captures are timed observations, never input attestations. */ +import type { ExecutionOutcome, ItemOrigin, TurnRef } from "./conversation-events.js"; +import { isTaskContextCapability, type TaskContextCapability } from "./task-context-tools.js"; + +export type ContextDraftOwner = { kind: "thread"; threadId: string } | { kind: "project"; projectId: string; key: string }; +export interface ContextDraftBinding { owner: ContextDraftOwner; nonce: string; revision: number } +/** Main-owned recovery metadata; no editable nonce or attachment capability is persisted here. */ +export interface PendingTaskContext { owner: ContextDraftOwner; projectId: string; evidence: TaskContextEvidence } + +export type ContextTarget = { kind: "file"; path: string } | { kind: "note"; pageId: string } | { kind: "notes-file"; id: string }; +export type ContextSource = + | { kind: "native"; itemId: string; origin: ItemOrigin } + | { kind: "human"; actionId: string; at: string } + | { kind: "model-proposal"; turn: TurnRef; leaseId: string; receiptId: string; requestId: string | number; at: string; nativeLink?: { itemId: string; origin: ItemOrigin } } + | { kind: "notes-read"; turn: TurnRef; leaseId: string; receiptId: string; requestId: string | number; method: "read" | "history" | "read_file"; at: string; nativeLink?: { itemId: string; origin: ItemOrigin } } + | { kind: "legacy-unavailable"; itemId: string; at: string; reason: "native-origin-not-recorded" }; +export interface ContextRelation { + kind: "read" | "changed" | "searched" | "relevant"; + provenance: "observed" | "model-proposed" | "user-confirmed" | "native-requested"; + source: ContextSource; + /** Model-proposed classification only; never detected coverage or inspection. Absent in older receipts. */ + role?: "file" | "test" | "note"; +} +export interface ContextCapture { + id: string; + target: ContextTarget; + source: ContextSource; + phase: "selected" | "check-start-observed" | "check-end-observed" | "after-native-result" | "after-notes-read"; + startedAt: string; + finishedAt: string; + repository: { state: "available"; head: string } | { state: "unavailable"; reason: "not-repository" | "budget" | "unreadable" | "not-captured" }; + revision: { state: "available"; algorithm: "sha256"; digest: string; bytes: number; noteRevision?: number } | { state: "unavailable"; reason: "missing" | "outside" | "not-regular" | "too-large" | "changed-during-read" | "budget" | "not-captured" | "unreadable" }; + workspace?: { kind: "project"; projectId: string } | { kind: "thread"; projectId: string; threadId: string }; + noteVersion?: { via: "read" | "history"; updatedAt: string; revision: number }; +} +export interface TaskCheckEvidence { + itemId: string; + eventIds: string[]; + origin?: ItemOrigin; + planRevision?: number; + startedAt?: string; + finishedAt?: string; + command: string; + commandOmission?: CommandOmission; + outcome: ExecutionOutcome; + exitCode?: number | null; + scope: "project" | "outside" | "unknown"; + relevantRefIds: string[]; + relevance: "user-confirmed" | "native" | "unestablished"; + startCaptureIds: string[]; + endCaptureIds: string[]; + staleness: "current-observation" | "stale" | "unknown"; +} +/** Export-local equality key; never a replacement native identity or an original command payload. */ +export interface CommandOmission { id: number; reason: "privacy" } +export const OMITTED_COMMAND = "Native command text omitted for privacy; result retained."; +export function isCommandOmission(v: unknown): v is CommandOmission { + return shape(v, ["id", "reason"]) && integer(v.id) && v.id > 0 && v.reason === "privacy"; +} +export function isTaskCommandText(v: unknown): v is Record & { command: string; commandOmission?: CommandOmission } { + return object(v) && typeof v.command === "string" && (v.commandOmission === undefined || (isCommandOmission(v.commandOmission) && v.command === OMITTED_COMMAND)); +} +export interface TaskContextEvidence { + revision: number; + refs: { id: string; target: ContextTarget; relations: ContextRelation[]; captureIds: string[] }[]; + captures: ContextCapture[]; + checks: TaskCheckEvidence[]; + /** Latest adapter report per actual attempt; absent in older records. No private connection data. */ + capabilities?: { turn: TurnRef; at: string; capability: TaskContextCapability }[]; +} + +export interface TaskContextCandidates { + files: { paths: string[]; truncated: boolean }; + notes: { items: { target: Exclude; title: string }[]; truncated: boolean }; +} + +/** Tuple encoding preserves kind and literal delimiters; scope/canonicalization belongs to main. */ +export function contextRefId(target: ContextTarget): string { + return JSON.stringify(target.kind === "file" ? ["file", target.path] : target.kind === "note" ? ["note", target.pageId] : ["notes-file", target.id]); +} + +type Obj = Record; +const object = (v: unknown): v is Obj => typeof v === "object" && v !== null && !Array.isArray(v); +function shape(v: unknown, required: string[], optional: string[] = []): v is Obj { + return object(v) && required.every(k => Object.hasOwn(v, k)) && Object.keys(v).every(k => required.includes(k) || optional.includes(k)); +} +const integer = (v: unknown): v is number => typeof v === "number" && Number.isSafeInteger(v) && v >= 0; +const text = (v: unknown, max = 512): v is string => typeof v === "string" && v.length > 0 && v.length <= max && !/[\u0000-\u001f\u007f]/.test(v); +const identity = (v: unknown): v is string => text(v) && !/^(?:[\\/]|[A-Za-z]:[\\/])/.test(v); +const date = (v: unknown): v is string => typeof v === "string" && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(v) && Number.isFinite(Date.parse(v)) && new Date(v).toISOString() === v; +const one = (v: unknown, values: readonly string[]): boolean => typeof v === "string" && values.includes(v); +const refIdentity = (v: unknown): v is string => text(v, 8192) && !/^(?:[\\/]|[A-Za-z]:[\\/])/.test(v); +const ids = (v: unknown): v is string[] => Array.isArray(v) && v.every(refIdentity) && new Set(v).size === v.length; +/** Editable bindings are minted by main; a provided malformed value is never an absent selection. */ +export function isContextDraftOwner(v: unknown): v is ContextDraftOwner { + if (!object(v)) return false; + if (v.kind === "thread") return shape(v, ["kind", "threadId"]) && identity(v.threadId); + if (!shape(v, ["kind", "projectId", "key"]) || v.kind !== "project" || !identity(v.projectId) || typeof v.key !== "string") return false; + const match = v.key.match(/^draft:(0|[1-9]\d*):/); + return Boolean(match && Number.isSafeInteger(Number(match[1])) && v.key === `draft:${match[1]}:${v.projectId}`); +} +export function isContextDraftBinding(v: unknown): v is ContextDraftBinding { + return shape(v, ["owner", "nonce", "revision"]) && typeof v.nonce === "string" && /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(v.nonce) && integer(v.revision) && isContextDraftOwner(v.owner); +} +/** Ownership uses typed identities, independent of an object's JSON key order. */ +export function contextDraftOwnerKey(owner: ContextDraftOwner): string { + if (!isContextDraftOwner(owner)) throw new Error("Invalid task context owner."); + return JSON.stringify(owner.kind === "thread" ? ["thread", owner.threadId] : ["project", owner.projectId, owner.key]); +} +export function sameContextDraftOwner(left: unknown, right: unknown): boolean { + return isContextDraftOwner(left) && isContextDraftOwner(right) && contextDraftOwnerKey(left) === contextDraftOwnerKey(right); +} +export function isPendingTaskContext(v: unknown): v is PendingTaskContext { + return shape(v, ["owner", "projectId", "evidence"]) && isContextDraftOwner(v.owner) && identity(v.projectId) && isTaskContextEvidence(v.evidence) + && v.evidence.checks.length === 0 + && !(v.evidence.capabilities?.length) + && v.evidence.refs.every(ref => ref.captureIds.length > 0 && ref.relations.length > 0 && ref.relations.every(r => r.kind === "relevant" && r.provenance === "user-confirmed" && r.source.kind === "human")) + && v.evidence.captures.every(c => c.phase === "selected" && c.source.kind === "human" && c.workspace?.projectId === v.projectId); +} +function target(v: unknown): v is ContextTarget { + if (!object(v)) return false; + if (v.kind === "note") return shape(v, ["kind", "pageId"]) && identity(v.pageId); + if (v.kind === "notes-file") return shape(v, ["kind", "id"]) && identity(v.id); + return shape(v, ["kind", "path"]) && v.kind === "file" && text(v.path, 4096) && !/[\\]/.test(v.path) && !/^(?:\/|[A-Za-z]:)/.test(v.path) && v.path.split("/").every(p => p !== "" && p !== "." && p !== ".."); +} +function turn(v: unknown, native = false): boolean { + if (!shape(v, ["thread", "turn", "attempt", "backend", ...(native ? ["native"] : [])], ["account"])) return false; + if (![v.thread, v.turn, v.attempt].every(identity) || !one(v.backend, ["claude", "codex", "opencode", "mimo", "mock"]) || (v.account !== undefined && !identity(v.account))) return false; + if (!native) return true; + const n = v.native; + return shape(n, ["runtime", "item"], ["thread", "turn", "part"]) && identity(n.runtime) && identity(n.item) && [n.thread, n.turn, n.part].every(x => x === undefined || identity(x)); +} +function source(v: unknown): v is ContextSource { + if (!object(v)) return false; + if (v.kind === "native") return shape(v, ["kind", "itemId", "origin"]) && identity(v.itemId) && turn(v.origin, true); + if (v.kind === "human") return shape(v, ["kind", "actionId", "at"]) && identity(v.actionId) && date(v.at); + if (v.kind === "legacy-unavailable") return shape(v, ["kind", "itemId", "at", "reason"]) && identity(v.itemId) && date(v.at) && v.reason === "native-origin-not-recorded"; + if (!one(v.kind, ["model-proposal", "notes-read"]) || !shape(v, ["kind", "turn", "leaseId", "receiptId", "requestId", "at", ...(v.kind === "notes-read" ? ["method"] : [])], ["nativeLink"])) return false; + if (!turn(v.turn) || !identity(v.leaseId) || !identity(v.receiptId) || !(identity(v.requestId) || (typeof v.requestId === "number" && Number.isSafeInteger(v.requestId))) || !date(v.at)) return false; + if (v.kind === "notes-read" && !one(v.method, ["read", "history", "read_file"])) return false; + if (v.nativeLink !== undefined) { + if (!shape(v.nativeLink, ["itemId", "origin"]) || !identity(v.nativeLink.itemId) || !turn(v.nativeLink.origin, true)) return false; + const t = v.turn as unknown as TurnRef, n = v.nativeLink.origin as unknown as ItemOrigin; + if (t.thread !== n.thread || t.turn !== n.turn || t.attempt !== n.attempt || t.backend !== n.backend || t.account !== n.account) return false; + } + return true; +} +function relation(v: unknown): boolean { + if (!shape(v, ["kind", "provenance", "source"], ["role"]) || !one(v.kind, ["read", "changed", "searched", "relevant"]) || !source(v.source) + || (v.role !== undefined && (v.provenance !== "model-proposed" || !one(v.role, ["file", "test", "note"])))) return false; + switch (v.provenance) { + case "model-proposed": return v.kind === "relevant" && v.source.kind === "model-proposal"; + case "user-confirmed": return v.kind === "relevant" && v.source.kind === "human"; + case "native-requested": return v.source.kind === "native" && v.kind !== "relevant"; + case "observed": return v.kind !== "relevant" && (v.source.kind === "native" || v.source.kind === "legacy-unavailable" || (v.source.kind === "notes-read" && v.kind === "read")); + default: return false; + } +} +function capture(v: unknown): v is ContextCapture { + if (!shape(v, ["id", "target", "source", "phase", "startedAt", "finishedAt", "repository", "revision"], ["noteVersion", "workspace"]) || !identity(v.id) || !target(v.target) || !source(v.source) || !one(v.phase, ["selected", "check-start-observed", "check-end-observed", "after-native-result", "after-notes-read"]) || !date(v.startedAt) || !date(v.finishedAt) || v.finishedAt < v.startedAt) return false; + const repo = v.repository, rev = v.revision; + if (!object(repo) || !(repo.state === "available" ? shape(repo, ["state", "head"]) && typeof repo.head === "string" && /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(repo.head) : shape(repo, ["state", "reason"]) && repo.state === "unavailable" && one(repo.reason, ["not-repository", "budget", "unreadable", "not-captured"]))) return false; + if (!object(rev) || !(rev.state === "available" ? shape(rev, ["state", "algorithm", "digest", "bytes"], ["noteRevision"]) && rev.algorithm === "sha256" && typeof rev.digest === "string" && /^[a-f0-9]{64}$/.test(rev.digest) && integer(rev.bytes) && (rev.noteRevision === undefined || (v.target.kind === "note" && integer(rev.noteRevision))) : shape(rev, ["state", "reason"]) && rev.state === "unavailable" && one(rev.reason, ["missing", "outside", "not-regular", "too-large", "changed-during-read", "budget", "not-captured", "unreadable"]))) return false; + if (v.workspace !== undefined) { + const w = v.workspace; + if (!object(w) || !identity(w.projectId) || !(w.kind === "project" ? shape(w, ["kind", "projectId"]) : w.kind === "thread" && shape(w, ["kind", "projectId", "threadId"]) && identity(w.threadId))) return false; + } + if (v.noteVersion !== undefined && !(v.target.kind === "note" && shape(v.noteVersion, ["via", "updatedAt", "revision"]) && one(v.noteVersion.via, ["read", "history"]) && date(v.noteVersion.updatedAt) && integer(v.noteVersion.revision))) return false; + if (v.phase === "after-notes-read") { + if (v.source.kind !== "notes-read") return false; + if (v.source.method === "read_file") { + if (v.target.kind !== "notes-file" || v.noteVersion !== undefined) return false; + } else if (v.target.kind !== "note" || !object(v.noteVersion) || v.noteVersion.via !== v.source.method || !integer(v.noteVersion.revision) || v.noteVersion.revision < 1 + || (rev.state === "available" && rev.noteRevision !== v.noteVersion.revision)) return false; + } + return true; +} +function check(v: unknown): v is TaskCheckEvidence { + return shape(v, ["itemId", "eventIds", "command", "outcome", "scope", "relevantRefIds", "relevance", "startCaptureIds", "endCaptureIds", "staleness"], ["origin", "planRevision", "exitCode", "startedAt", "finishedAt", "commandOmission"]) && isTaskCommandText(v) && identity(v.itemId) && ids(v.eventIds) && typeof v.command === "string" && v.command.length > 0 && v.command.length <= 1024 * 1024 && !v.command.includes("\0") && one(v.outcome, ["running", "succeeded", "failed", "denied", "interrupted", "unknown"]) && one(v.scope, ["project", "outside", "unknown"]) && ids(v.relevantRefIds) && one(v.relevance, ["user-confirmed", "native", "unestablished"]) && ids(v.startCaptureIds) && ids(v.endCaptureIds) && one(v.staleness, ["current-observation", "stale", "unknown"]) && (v.origin === undefined || turn(v.origin, true)) && (v.planRevision === undefined || integer(v.planRevision)) && (v.startedAt === undefined || date(v.startedAt)) && (v.finishedAt === undefined || (date(v.finishedAt) && (v.startedAt === undefined || v.finishedAt >= v.startedAt))) && (v.exitCode === undefined || v.exitCode === null || (typeof v.exitCode === "number" && Number.isSafeInteger(v.exitCode))); +} + +/** Strictly validate supplied metadata; absence is handled by legacy callers, never by coercion. */ +export function isTaskContextEvidence(v: unknown): v is TaskContextEvidence { + if (!shape(v, ["revision", "refs", "captures", "checks"], ["capabilities"]) || !integer(v.revision) || !Array.isArray(v.refs) || !Array.isArray(v.captures) || !Array.isArray(v.checks)) return false; + if (v.capabilities !== undefined && (!Array.isArray(v.capabilities) || v.capabilities.length > 1000 + || !v.capabilities.every(c => shape(c, ["turn", "at", "capability"]) && turn(c.turn) && date(c.at) && isTaskContextCapability(c.capability)) + || new Set(v.capabilities.map(c => (c.turn as TurnRef).attempt)).size !== v.capabilities.length)) return false; + if (!v.captures.every(capture) || !v.checks.every(check)) return false; + const captures = new Map(v.captures.map(c => [c.id, c])); + if (captures.size !== v.captures.length || new Set(v.checks.map(c => c.itemId)).size !== v.checks.length) return false; + const refs = new Set(), targets = new Set(); + for (const ref of v.refs) { + if (!shape(ref, ["id", "target", "relations", "captureIds"]) || !refIdentity(ref.id) || !target(ref.target) || !Array.isArray(ref.relations) || !ref.relations.every(relation) || !ids(ref.captureIds)) return false; + const key = contextRefId(ref.target); + const fileTarget = ref.target.kind === "file"; + if (ref.relations.some(rel => rel.role !== undefined && (fileTarget === (rel.role === "note")))) return false; + if (refs.has(ref.id) || targets.has(key)) return false; + refs.add(ref.id); targets.add(key); + if (!ref.captureIds.every(id => captures.has(id) && contextRefId(captures.get(id)!.target) === key)) return false; + } + return v.checks.every(c => c.relevantRefIds.every(id => refs.has(id)) && [...c.startCaptureIds, ...c.endCaptureIds].every(id => captures.has(id))); +} + +export function copyTaskContextEvidence(value: TaskContextEvidence): TaskContextEvidence { + if (!isTaskContextEvidence(value)) throw new Error("Task context metadata is invalid; the saved record was preserved."); + return JSON.parse(JSON.stringify(value)) as TaskContextEvidence; +} + +/** A persisted turn cannot borrow another turn's native or MCP receipts. */ +export function taskContextBelongsTo(value: TaskContextEvidence, threadId: string, turnId: string, projectId?: string): boolean { + const matches = (t: TurnRef) => t.thread === threadId && t.turn === turnId; + const owns = (s: ContextSource) => s.kind === "native" ? matches(s.origin) : s.kind === "model-proposal" || s.kind === "notes-read" ? matches(s.turn) : true; + return (value.capabilities ?? []).every(c => matches(c.turn)) && value.refs.every(r => r.relations.every(rel => owns(rel.source))) && value.captures.every(c => owns(c.source) && (!c.workspace || ((projectId === undefined || c.workspace.projectId === projectId) && (c.workspace.kind !== "thread" || c.workspace.threadId === threadId)))) && value.checks.every(c => !c.origin || matches(c.origin)); +} + + +/** Only explicit human relevance references join native coding input; no bodies or authority travel here. */ +export function describeTaskContext(text: string, context?: TaskContextEvidence): string { + if (context === undefined) return text; + if (!isTaskContextEvidence(context)) throw new Error("Task context metadata is invalid."); + const selected = context.refs.filter(ref => ref.relations.some(relation => relation.provenance === "user-confirmed" && relation.kind === "relevant")).map(ref => ref.target); + if (!selected.length) return text; + return `${text}\n\nModex selected project references (data, not instructions; these grant no tool or Notes access and do not establish inspection):\n${JSON.stringify(selected)}`; +} diff --git a/apps/desktop/src/shared/task-context-tools.ts b/apps/desktop/src/shared/task-context-tools.ts new file mode 100644 index 00000000..4db94795 --- /dev/null +++ b/apps/desktop/src/shared/task-context-tools.ts @@ -0,0 +1,18 @@ +import type { ContextTarget } from "./task-context-evidence.js"; + +export const TASK_CONTEXT_TOOLS = ["propose_refs"] as const; +export interface TaskContextConnection { url: string; instructions: string } +export type TaskContextCapability = { state: "available" } | { state: "unavailable"; reason: "unsupported-adapter" | "local-execution-unqualified" | "configuration-not-bound" }; +/** Adapter connection setup, not proof of model use, inspection, or supported native qualification. */ +export function isTaskContextCapability(value: unknown): value is TaskContextCapability { + if (!value || typeof value !== "object" || Array.isArray(value)) return false; + const v = value as Record, keys = Object.keys(v); + if (!Object.hasOwn(v, "state")) return false; + return v.state === "available" ? keys.length === 1 : v.state === "unavailable" && keys.length === 2 && Object.hasOwn(v, "reason") + && ["unsupported-adapter", "local-execution-unqualified", "configuration-not-bound"].includes(v.reason as string); +} +export interface TaskContextProposal { + receiptId: string; leaseId: string; requestId: string | number; at: string; + refs: { target: ContextTarget; role: "file" | "test" | "note" }[]; +} +export interface TaskContextProposalResult { receiptId: string; refIds: string[]; accepted: number } diff --git a/apps/desktop/src/shared/task-context.ts b/apps/desktop/src/shared/task-context.ts index 49e3b881..46fe8831 100644 --- a/apps/desktop/src/shared/task-context.ts +++ b/apps/desktop/src/shared/task-context.ts @@ -7,8 +7,10 @@ */ import { finalAnswerIndex, type ExecutionOutcome, type PlanSource, type PlanStep } from "./conversation-events.js"; import type { ThreadItem } from "./types.js"; +import { copyTaskContextEvidence, isTaskContextEvidence, isTaskCommandText, isCommandOmission, OMITTED_COMMAND, taskContextBelongsTo, type CommandOmission, type TaskContextEvidence } from "./task-context-evidence.js"; +export * from "./task-context-evidence.js"; -export interface TaskCommand { command: string; outcome: ExecutionOutcome; exitCode?: number | null } +export interface TaskCommand { command: string; outcome: ExecutionOutcome; exitCode?: number | null; commandOmission?: CommandOmission; itemId?: string } /** A check the turn ran: a test, build, typecheck or lint command. `stale`: something that may change files ran after it. */ export interface TaskCheck extends TaskCommand { passed: boolean; stale: boolean } @@ -19,6 +21,8 @@ export interface TaskVerification { checks: TaskCheck[]; /** Edits the turn made (patch tools not denied). With none and no check, there is nothing to verify. */ edits: number; + /** Modern provenance is limited to timed snapshots, never subprocess input attestation. */ + basis?: "context-snapshots"; } /** A command as the CLI reported it, shown as the script when the whole of it is `sh`/`bash`/`zsh -c '…'`. */ @@ -54,6 +58,8 @@ export interface TaskRecord { /** The project files the turn touched, by what the CLI reported. */ context?: TaskContext; answer?: string; + /** Main-owned timed provenance, frozen at Save; omitted for legacy turns. */ + contextEvidence?: TaskContextEvidence; } /** @@ -76,6 +82,8 @@ export interface TaskSnapshot { /** A saved task note, reopened read-only: the record as saved, and what has happened to the note since. */ export interface TaskNoteView { record: TaskRecord; + pageId: string; + projectId: string; threadId: string; turnId: string; savedAt: string; @@ -101,13 +109,14 @@ export function isTaskSnapshot(value: unknown): value is TaskSnapshot { return typeof v === "object" && v !== null && v.version === 1 && typeof v.pageId === "string" && Number.isInteger(v.revision) && typeof v.projectId === "string" && typeof v.threadId === "string" && typeof v.turnId === "string" && typeof v.savedAt === "string" && typeof r === "object" && r !== null && typeof r.title === "string" && typeof r.request === "string" && strings(r.files) - && Number.isInteger(r.outsideFiles) && Array.isArray(r.commands) && r.commands.every((c) => typeof c?.command === "string") + && Number.isInteger(r.outsideFiles) && Array.isArray(r.commands) && r.commands.every((c) => isTaskCommandText(c) && (c.itemId === undefined || portableItemId(c.itemId))) && Array.isArray(r.clarifications) && r.clarifications.every((c) => typeof c?.question === "string" && (c.answer === null || strings(c.answer))) && (r.plan === undefined || (Array.isArray(r.plan.steps) && r.plan.steps.every((s) => typeof s?.text === "string" && typeof s.status === "string"))) - && (r.verification === undefined || (typeof r.verification.state === "string" && Array.isArray(r.verification.checks) && r.verification.checks.every((c) => typeof c?.command === "string"))) + && (r.verification === undefined || (typeof r.verification.state === "string" && Array.isArray(r.verification.checks) && r.verification.checks.every(isTaskCommandText))) && (r.context === undefined || (Array.isArray(r.context.files) && r.context.files.every((f) => typeof f?.path === "string" && strings(f.relations)) && Number.isInteger(r.context.outside) - && (r.context.unlisted === undefined || strings(r.context.unlisted)))) - && (r.answer === undefined || typeof r.answer === "string"); + && (r.context.unlisted === undefined || strings(r.context.unlisted)) && validUnlistedOmissions(r.context))) + && (r.answer === undefined || typeof r.answer === "string") + && (r.contextEvidence === undefined || (isTaskContextEvidence(r.contextEvidence) && taskContextBelongsTo(r.contextEvidence, v.threadId, v.turnId, v.projectId))); } /** A question the CLI asked, with the answer as its card shows it. Nothing is filled in that the card does not hold. */ @@ -150,7 +159,7 @@ function proposedPlan(item: ThreadItem): boolean { return item.kind === "assistant" && item.nativePhase === "plan" && Boolean(item.text.trim()); } -export function taskRecord(items: readonly ThreadItem[], opts: { threadId: string; title: string; cwd: string }): TaskRecord { +export function taskRecord(items: readonly ThreadItem[], opts: { threadId: string; title: string; cwd: string; contextEvidence?: TaskContextEvidence }): TaskRecord { const user = items.find((item) => item.kind === "user"); const rest = items.filter((item) => item.kind !== "user"); const plan = [...rest].reverse().find((item) => item.kind === "plan"); @@ -168,27 +177,31 @@ export function taskRecord(items: readonly ThreadItem[], opts: { threadId: strin // A question the agent asked in its message has no answer in this turn: the next message is the answer. if (item.kind === "assistant") for (const ask of item.asks ?? []) clarifications.push({ question: ask.title, answer: null, closed: "message" }); if (item.kind !== "tool") continue; - if (PATCH_TOOLS.has(item.name)) { + if (PATCH_TOOLS.has(item.name) && !item.displayOnlyInputs) { for (const file of patchFiles(item)) { const rel = relative(file, opts.cwd); if (rel === null) outsideFiles++; else files.add(rel); } } - if (item.exec) commands.push({ command: item.exec.command, outcome: item.outcome ?? (item.ok === true ? "succeeded" : "unknown"), ...(item.exitCode !== undefined ? { exitCode: item.exitCode } : {}) }); + if (item.exec) commands.push({ command: item.exec.command, ...(!isPortableCommand(item.exec.command) && portableItemId(item.id) ? { itemId: item.id } : {}), outcome: item.outcome ?? (item.ok === true ? "succeeded" : "unknown"), ...(item.exitCode !== undefined ? { exitCode: item.exitCode } : {}) }); } + const evidence = opts.contextEvidence ?? (user?.kind === "user" ? user.contextEvidence : undefined); + if (evidence !== undefined && (!isTaskContextEvidence(evidence) || !user || !taskContextBelongsTo(evidence, opts.threadId, user.id))) throw new Error("Task context does not belong to this request."); + const context = taskContext(items, opts.cwd, evidence); const final = finalAnswerIndex(rest); const answer = final >= 0 ? rest[final] : undefined; const missing = noPlan(rest); const proposed = [...rest].reverse().find(proposedPlan); - return { + return portableTaskRecord({ + ...(evidence !== undefined ? { contextEvidence: copyTaskContextEvidence(evidence) } : {}), title: opts.title, threadId: opts.threadId, request: user?.kind === "user" ? user.text : "", ...(plan?.kind === "plan" ? { plan: { steps: plan.steps, from: plan.from, revision: plan.revision } } : {}), ...(proposed?.kind === "assistant" ? { proposedPlan: proposed.text.trim() } : {}), ...(missing ? { noPlan: missing } : {}), - files: [...files], outsideFiles, commands, clarifications, verification: verification(rest), context: taskContext(rest, opts.cwd), + files: evidence === undefined ? [...files] : context.files.filter(file => file.relations.includes("changed")).map(file => file.path), outsideFiles, commands, clarifications, verification: verification(rest, evidence), context, ...(answer?.kind === "assistant" && answer !== proposed && answer.text.trim() ? { answer: answer.text.trim() } : {}), - }; + }); } export const PROPOSED_WORDS = "Codex proposed this plan in plan mode. Its steps have no statuses."; @@ -196,7 +209,9 @@ const STEP_WORDS: Record = { pending: "not started", const FROM_WORDS: Record = { "codex-plan": "Codex's plan", "claude-todo": "Claude's todo list", "claude-tasks": "Claude's task list", "acp-plan": "The agent's plan" }; /** The record as portable Markdown for a note. */ -export function taskMarkdown(record: TaskRecord): string { +export function taskMarkdown(record: TaskRecord, options: { fullContext?: boolean } = {}): string { + record = portableTaskRecord(record); + const evidence = record.contextEvidence === undefined ? undefined : copyTaskContextEvidence(record.contextEvidence); const lines: string[] = [`# ${record.title}`, ""]; if (record.request.trim()) lines.push(...record.request.trim().split("\n").map((line) => `> ${line}`), ""); const clarifications = record.clarifications ?? []; @@ -210,25 +225,50 @@ export function taskMarkdown(record: TaskRecord): string { } if (record.files.length || record.outsideFiles) { lines.push("## Files changed", ""); - for (const file of record.files) lines.push(`- \`${file}\``); + if (evidence) lines.push(`${record.files.length} project ${record.files.length === 1 ? "file" : "files"} changed; recorded relations are in Context references below.`); + else for (const file of record.files) lines.push(`- \`${file}\``); if (record.outsideFiles) lines.push(`- ${record.outsideFiles} ${record.outsideFiles === 1 ? "file" : "files"} outside the project (not named)`); lines.push(""); } const context = record.context; const unlisted = context?.unlisted ?? []; - if (context && (context.files.length || context.outside || unlisted.length)) { + if (context && ((!evidence && context.files.length) || context.outside || unlisted.length)) { lines.push("## Files the turn touched", "", "As the CLI reported them. Modex did not index the project.", ""); - for (const file of context.files.slice(0, CONTEXT_SHOWN)) lines.push(`- \`${file.path}\` — ${file.relations.join(", ")}`); - if (context.files.length > CONTEXT_SHOWN) lines.push(`- and ${context.files.length - CONTEXT_SHOWN} more`); + if (!evidence) { + for (const file of (options.fullContext ? context.files : context.files.slice(0, CONTEXT_SHOWN))) lines.push(`- \`${file.path}\` — ${file.relations.join(", ")}`); + if (!options.fullContext && context.files.length > CONTEXT_SHOWN) lines.push(`- and ${context.files.length - CONTEXT_SHOWN} more`); + } if (context.outside) lines.push(`- ${context.outside} outside the project (not named)`); if (context.files.length || context.outside) lines.push(""); if (unlisted.length) { lines.push(UNLISTED_WORDS, ""); - for (const command of unlisted.slice(0, CONTEXT_SHOWN)) lines.push(`- \`${command.replace(/`/g, "'")}\``); - if (unlisted.length > CONTEXT_SHOWN) lines.push(`- and ${unlisted.length - CONTEXT_SHOWN} more`); + for (const [index, command] of (options.fullContext ? unlisted : unlisted.slice(0, CONTEXT_SHOWN)).entries()) { + const omission = context.unlistedOmissions?.find(row => row.index === index)?.omission; + lines.push(`- ${omission ? omittedCommandLabel(omission) : `\`${command.replace(/`/g, "'")}\``}`); + } + if (!options.fullContext && unlisted.length > CONTEXT_SHOWN) lines.push(`- and ${unlisted.length - CONTEXT_SHOWN} more`); lines.push(""); } } + if (evidence?.refs.length) { + const captures = new Map(evidence.captures.map(capture => [capture.id, capture])); + const refs = options.fullContext ? evidence.refs : evidence.refs.slice(0, CONTEXT_SHOWN); + lines.push("## Context references", "", "Snapshots record timed observations; process input bytes remain unverified.", ""); + for (const ref of refs) { + const label = ref.target.kind === "file" ? `File ${contextCode(ref.target.path)}` : ref.target.kind === "note" ? `Note ${contextCode(ref.target.pageId)}` : `Notes file ${contextCode(ref.target.id)}`; + const relations = [...new Set(ref.relations.map(relation => relation.provenance === "user-confirmed" ? "human-confirmed relevance" : relation.provenance === "model-proposed" ? `model-proposed ${relation.role ?? "relevance"}` : `${relation.provenance} ${relation.kind}`))]; + let row = `- ${label}${relations.length ? ` — ${relations.join("; ")}` : ""}. ${ref.captureIds.length} retained captures.`; + const capture = ref.captureIds.map(id => captures.get(id)).filter(c => c !== undefined).at(-1); + if (capture) { + const revision = capture.revision.state === "available" ? `SHA-256 ${capture.revision.digest}, ${capture.revision.bytes} bytes` : `revision unavailable: ${capture.revision.reason}`; + row += ` Retained capture: ${capture.phase}, ${capture.startedAt} → ${capture.finishedAt}; ${revision}${capture.noteVersion ? `; Note revision ${capture.noteVersion.revision} (${capture.noteVersion.via})` : ""}.`; + } + lines.push(row); + } + if (!options.fullContext && evidence.refs.length > CONTEXT_SHOWN) lines.push("", `${evidence.refs.length - CONTEXT_SHOWN} more context references are retained. Use the full record export for every reference and provenance receipt.`); + else if (!options.fullContext) lines.push("", "Use the full record export for all provenance receipts, captures and check-reference mappings."); + lines.push(""); + } if (record.commands.length) { lines.push("## Commands", ""); for (const command of record.commands) lines.push(`- ${commandLine(command)}`); @@ -236,23 +276,31 @@ export function taskMarkdown(record: TaskRecord): string { } const checked = record.verification; if (checked && (checked.checks.length || record.files.length || record.outsideFiles)) { - lines.push("## Verification", "", VERIFICATION_WORDS[checked.state], ""); + lines.push("## Verification", "", verificationWords(checked), ""); if (checked.checks.length) lines.push(...checked.checks.map((check) => `- ${commandLine(check)}${check.stale ? " (ran before a later change)" : ""}`), ""); } if (record.answer) lines.push("## Answer", "", record.answer, ""); // A check a later fix superseded is settled by what came after it; the verification line says how. - const superseded = new Set(checked?.checks.filter((check) => check.stale).map((check) => check.command + "\0" + check.outcome) ?? []); + const superseded = new Set(checked?.checks.filter((check) => check.stale).map((check) => commandKey(check) + "\0" + check.outcome) ?? []); const loose = [ - ...record.commands.filter((command) => !SETTLED_WELL.has(command.outcome) && !(checked?.state === "verified" && superseded.has(command.command + "\0" + command.outcome))).map(commandLine), + ...record.commands.filter((command) => !SETTLED_WELL.has(command.outcome) && !(checked?.state === "verified" && superseded.has(commandKey(command) + "\0" + command.outcome))).map(commandLine), ...(record.plan?.steps.filter((step) => step.status !== "completed").map((step) => `${step.text} (${STEP_WORDS[step.status]})`) ?? []), ...clarifications.filter((c) => !c.answer || c.uncertain).map(clarificationLine), - ...(checked && checked.state !== "verified" && (checked.checks.length || record.files.length || record.outsideFiles) ? [VERIFICATION_WORDS[checked.state]] : []), + ...(checked && checked.state !== "verified" && (checked.checks.length || record.files.length || record.outsideFiles) ? [verificationWords(checked)] : []), ]; if (loose.length) lines.push("## Not settled", "", ...loose.map((line) => `- ${line}`), ""); + if (options.fullContext && evidence) lines.push("## Full context provenance", "", "Retained metadata only. Export does not refresh files, execute a check or reconstruct a missing observation.", "", "```json", JSON.stringify(evidence, null, 2), "```", ""); lines.push("---", `From modex thread ${record.threadId}.`); return lines.join("\n") + "\n"; } +/** Quoted data in a code span, including literal backticks and whitespace in filenames. */ +function contextCode(value: string): string { + const quoted = /`|\s/.test(value) ? JSON.stringify(value) : value, ticks = quoted.match(/`+/g) ?? []; + const fence = "`".repeat(Math.max(1, ...ticks.map(run => run.length + 1))); + return `${fence}${quoted}${fence}`; +} + const CLOSED_WORDS: Record, string> = { native: "the agent withdrew it", stopped: "the turn was stopped", ended: "the turn ended first", message: "it was asked in a message" }; /** A clarification as one line: the question, then its answer or why it has none. */ @@ -265,7 +313,7 @@ export function clarificationLine(c: TaskClarification): string { function commandLine(command: TaskCommand): string { const words: Record = { running: "running", succeeded: "succeeded", failed: "failed", denied: "denied", interrupted: "stopped", unknown: "status unknown" }; const exit = typeof command.exitCode === "number" ? `exit code ${command.exitCode}` : command.exitCode === null ? "no exit code" : "exit code not reported"; - return `\`${command.command.replace(/`/g, "'")}\` — ${words[command.outcome]}, ${exit}`; + return `${command.commandOmission ? omittedCommandLabel(command.commandOmission) : `\`${command.command.replace(/`/g, "'")}\``} — ${words[command.outcome]}, ${exit}${command.itemId ? `; source item ${contextCode(command.itemId)}` : ""}`; } /** Files a patch tool touched: Claude's `file_path`/`notebook_path`, or the names in Codex's "edit a, b" title. */ @@ -330,6 +378,18 @@ const VERIFICATION_WORDS: Record = { none: "Not verified: no check result confirms the latest changes.", }; +export function verificationWords(value: TaskVerification): string { + if (value.basis !== "context-snapshots") return VERIFICATION_WORDS[value.state]; + const words: Record = { + verified: "Checks passed for confirmed references with matching timed snapshots. These snapshots do not prove which bytes the check consumed.", + failed: "A check for confirmed project references did not pass. Timed snapshots do not prove which bytes the check consumed.", + unknown: "Not verified: project scope, confirmed relevance, exit status or matching timed snapshots are incomplete.", + stale: "Not verified: a later change or plan revision superseded the check's timed snapshots.", + none: "Not verified: no check receipt covers the current task context.", + }; + return words[value.state]; +} + /** Tools that change files. `write_file` is the offline engine's. A denied one changed nothing. */ const EDIT_TOOLS = new Set([...PATCH_TOOLS, "write_file"]); @@ -340,7 +400,57 @@ const EDIT_TOOLS = new Set([...PATCH_TOOLS, "write_file"]); * after it: Modex cannot see what an unknown command wrote. Nothing here links a check to a plan * step, because neither CLI reports such a link. */ -export function verification(items: readonly ThreadItem[]): TaskVerification { +export function verification(items: readonly ThreadItem[], supplied?: TaskContextEvidence): TaskVerification { + const user = items.find(item => item.kind === "user"); + const evidence = supplied ?? (user?.kind === "user" ? user.contextEvidence : undefined); + const legacy = legacyVerification(items); + if (evidence === undefined) return legacy; + const result: TaskVerification = { ...legacy, state: legacy.checks.length ? "unknown" : "none", basis: "context-snapshots" }; + if (!isTaskContextEvidence(evidence)) return result; + const plan = [...items].reverse().find(item => item.kind === "plan"); + const currentPlan = plan?.kind === "plan" ? plan.revision : undefined; + const needed = evidence.refs.filter(ref => ref.relations.some(r => r.provenance === "user-confirmed" || (r.kind === "changed" && r.provenance === "observed"))); + const covered = new Set(); + let stale = false, failed = false; + const sameOrigin = (a: import("./conversation-events.js").ItemOrigin | undefined, b: import("./conversation-events.js").ItemOrigin | undefined) => Boolean(a && b + && a.thread === b.thread && a.turn === b.turn && a.attempt === b.attempt && a.backend === b.backend && a.account === b.account + && a.native.runtime === b.native.runtime && a.native.item === b.native.item && a.native.thread === b.native.thread && a.native.turn === b.native.turn && a.native.part === b.native.part); + for (const check of evidence.checks) { + const index = items.findIndex(item => item.kind === "tool" && item.id === check.itemId && sameOrigin(item.origin, check.origin)); + const item = items[index]; + if (item?.kind !== "tool" || item.unavailableInputs?.includes("exec.command") || item.exec?.command !== check.command || !isCheckCommand(check.command)) continue; + if (check.scope !== "project" || check.relevance !== "user-confirmed" || !check.relevantRefIds.length) continue; + // A later write can affect a reference even when no native file-change item named it. + const laterWriter = items.slice(index + 1).some(later => later.kind === "tool" && later.outcome !== "denied" + && (EDIT_TOOLS.has(later.name) || Boolean(later.exec?.command && mayWrite(later.exec.command)))); + if (laterWriter || check.staleness === "stale" || check.planRevision !== currentPlan) { stale = true; continue; } + if (check.outcome === "failed" || check.outcome === "denied" || (typeof check.exitCode === "number" && check.exitCode !== 0)) { failed = true; continue; } + if (check.outcome !== "succeeded" || check.exitCode !== 0 || item.outcome !== "succeeded" || item.exitCode !== 0 || check.staleness !== "current-observation" || check.eventIds.length < 2 || !check.startedAt || !check.finishedAt) continue; + const captures = new Map(evidence.captures.map(c => [c.id, c])); + for (const refId of check.relevantRefIds) { + const ref = evidence.refs.find(ref => ref.id === refId); + if (!ref) continue; + const owns = (c: import("./task-context-evidence.js").ContextCapture | undefined, phase: "check-start-observed" | "check-end-observed") => Boolean(c && c.phase === phase && c.source.kind === "native" + && c.source.itemId === check.itemId && sameOrigin(c.source.origin, check.origin) + && JSON.stringify(c.target) === JSON.stringify(ref.target) && ref.captureIds.includes(c.id)); + const start = check.startCaptureIds.map(id => captures.get(id)).find(c => owns(c, "check-start-observed")); + const end = check.endCaptureIds.map(id => captures.get(id)).find(c => owns(c, "check-end-observed")); + if (!start || !end || start.finishedAt > end.startedAt || (check.startedAt !== undefined && start.startedAt < check.startedAt) + || (check.finishedAt !== undefined && (start.finishedAt > check.finishedAt || end.startedAt < check.finishedAt)) + || start.revision.state !== "available" || end.revision.state !== "available") continue; + if (start.revision.digest !== end.revision.digest || start.revision.bytes !== end.revision.bytes || start.revision.noteRevision !== end.revision.noteRevision) { stale = true; continue; } + const endRevision = end.revision; + const later = ref.captureIds.map(id => captures.get(id)).filter(c => c && c.startedAt > end.finishedAt && c.noteVersion?.via !== "history"); + if (later.some(c => c!.revision.state === "available" && (c!.revision.digest !== endRevision.digest || c!.revision.bytes !== endRevision.bytes || c!.revision.noteRevision !== endRevision.noteRevision))) { stale = true; continue; } + if (later.some(c => c!.revision.state !== "available")) continue; + covered.add(refId); + } + } + result.state = failed ? "failed" : needed.length && needed.every(ref => covered.has(ref.id)) ? "verified" : stale ? "stale" : result.state; + return result; +} + +function legacyVerification(items: readonly ThreadItem[]): TaskVerification { const checks: TaskCheck[] = []; let edits = 0; const staleAll = () => { for (const check of checks) check.stale = true; }; @@ -398,6 +508,21 @@ export function isCheckCommand(command: string): boolean { return sawCheck; } +/** Literal leading cd operations only. Directory-changing tool flags remain unqualified. */ +export function checkDirectoryChanges(command: string): string[] | null { + const parts = segments(command, { checks: true, noWrites: true }); + if (!parts || !isCheckCommand(command)) return null; + const changes: string[] = []; + for (const words of parts) { + if (words[0] === "cd") { + const dir = words.at(-1)!; + if (dir === "-" || dir.startsWith("~")) return null; + changes.push(dir); + } else if (words.some(word => /^(?:-C|--(?:cwd|dir|directory|prefix))(?:=|$)/.test(word) || /^-C.+/.test(word))) return null; + } + return changes; +} + function checkSegment(words: string[]): boolean { const [head, ...rest] = words; if (!head) return false; @@ -450,14 +575,30 @@ function isReadOnlyCommand(command: string): boolean { * the tool's final exit status cannot prove earlier checks passed across ;, ||, pipes or backgrounding. * `noWrites` rejects output redirection; its destination could be a file the check was meant to verify. */ -function segments(command: string, options: { noWrites?: boolean; checks?: boolean } = {}): string[][] | null { +function segments(command: string, options: { noWrites?: boolean; checks?: boolean; portable?: boolean } = {}, depth = 0): string[][] | null { + if (options.portable && depth > 16) return null; const out: string[][] = []; let words: string[] = []; let word = ""; let quote: string | null = null; let quoted = false; + let portable = true; const endWord = () => { if (word || quoted) words.push(word); word = ""; quoted = false; }; - const endSegment = () => { endWord(); while (words.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[0]!)) words.shift(); if (words.length) out.push(words); words = []; }; + const endSegment = () => { endWord(); + if (options.portable && words.length) { + const program = (words[0] ?? "").split("/").pop()!; + const shell = /^(sh|bash|zsh|dash)$/.test(program) && words.length === 3 && /^-l?c$/.test(words[1] ?? ""); + if (!shell && !portableInvocation(words)) portable = false; + if (words.some((word, index) => !(shell && index === 2) && !portableWord(word, index === 0, program === "printf" && index > 0, (program === "printf" || program === "echo") && index > 0))) portable = false; + if ((/^node(?:js)?$/.test(program) && words.slice(1).some(word => /^-[ep]|^--(?:eval|print)(?:=|$)/.test(word))) + || (/^python[0-9.]*$/.test(program) && words.slice(1).some(word => /^-c|^--command/.test(word))) + || (/^(perl|ruby)$/.test(program) && words.slice(1).some(word => /^-[^-]*e/.test(word))) + || /^(cmd|powershell|pwsh)$/.test(program) + || (program === "env" && words.some(word => word === "-S" || word.startsWith("--split-string"))) + || (program === "sed" && !(words[1] === "-n" && /^\d+(?:,\d+)?p$/.test(words[2] ?? ""))) + || (program === "awk" && !words.includes("-f"))) portable = false; + } + while (words.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[0]!)) words.shift(); if (words.length) out.push(words); words = []; }; for (let i = 0; i < command.length; i++) { const c = command[i]!; if (quote) { @@ -485,6 +626,7 @@ function segments(command: string, options: { noWrites?: boolean; checks?: boole } if (quote || (options.checks && !word && !quoted && !words.length)) return null; endSegment(); + if (!portable) return null; // `/bin/zsh -lc '