Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): answer Subagent harness hooks without the reverted bridge
Zhao Dengfengac7fa8eff783
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.tsindex e145a3957e..4cc9ae08e8 100644--- a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts@@ -1577,17 +1577,25 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {}]);}- it('answers a durable Subagent harness hook through the hook bridge instead of an approval prompt', async () => {+ // The harness hook bridge was reverted, so the host no longer runs these+ // hooks, but a child started by an older bridge can still raise one. Every+ // `cindy:harness-hook` is answered `continue` and never becomes an approval.+ it.each([+ ['tool.before', toolBeforeHook],+ ['compaction.before', harnessHook(JSON.stringify({ name: 'compaction.before' }))],+ ['non-JSON payload', harnessHook('not json')],+ ['unknown hook name', harnessHook(JSON.stringify({ name: 'tool.after', toolName: 'bash' }))],+ ['tool.before without a tool name', harnessHook(JSON.stringify({ name: 'tool.before', input: {} }))],+ ['array payload', harnessHook('[]')],+ ] as const)('answers continue for a durable Subagent harness hook without an approval prompt (%s)', async (_case, pending) => {const runId = '123e4567-e89b-42d3-a456-426614174098';const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);- let handlerAttached = false;- mockHarnessHookRun(runId, 'tool-harness-hook', () => handlerAttached);+ let ready = false;+ mockHarnessHookRun(runId, 'tool-harness-hook', () => ready, [pending]);const resolver = vi.fn(async () => ({ kind: 'permission', behavior: 'allow' }) as const);- const hook = vi.fn(async () => ({ action: 'block', reason: 'policy' }) as const);const handle = await new PiAgent(buildDeps()).startSession(opts());handle.setInteractionResolver(resolver);- handle.setHarnessHookHandler?.(hook);- handlerAttached = true;+ ready = true;await vi.waitFor(() => expect(control).toHaveBeenCalledWith(expect.any(String),@@ -1596,72 +1604,10 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {expect.objectContaining({childId: `${runId}-1`,approvalId: 'hook-1',- // The bridge forwards only the action; reasons never reach Pi.- value: JSON.stringify({ action: 'block' }),+ value: JSON.stringify({ action: 'continue' }),runtimeOwnerId: ownerId(),}),), { timeout: 3_000 });- expect(hook).toHaveBeenCalledWith({- name: 'tool.before',- toolName: 'bash',- toolUseId: 'bash-1',- input: { command: 'git status' },- });- expect(resolver).not.toHaveBeenCalled();-- await handle.close();- });-- it('forwards a durable Subagent compaction.before hook with only its name', async () => {- const runId = '123e4567-e89b-42d3-a456-42661417409a';- const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);- let handlerAttached = false;- mockHarnessHookRun(runId, 'tool-harness-compaction', () => handlerAttached, [- harnessHook(JSON.stringify({ name: 'compaction.before' })),- ]);- const hook = vi.fn(async () => ({ action: 'continue' }) as const);- const handle = await new PiAgent(buildDeps()).startSession(opts());- handle.setHarnessHookHandler?.(hook);- handlerAttached = true;-- await vi.waitFor(() => expect(control).toHaveBeenCalledWith(- expect.any(String),- 'tool-harness-compaction',- 'approval',- expect.objectContaining({ approvalId: 'hook-1', value: JSON.stringify({ action: 'continue' }) }),- ), { timeout: 3_000 });- expect(hook).toHaveBeenCalledWith({ name: 'compaction.before' });-- await handle.close();- });-- // Same gate as the root session: anything that is not a well-formed- // tool.before / compaction.before keeps the native policy (continue) and- // never reaches the hook handler or an approval card.- it.each([- ['non-JSON payload', 'not json'],- ['unknown hook name', JSON.stringify({ name: 'tool.after', toolName: 'bash' })],- ['tool.before without a tool name', JSON.stringify({ name: 'tool.before', input: {} })],- ['array payload', '[]'],- ])('answers continue for a malformed durable Subagent hook (%s)', async (_case, placeholder) => {- const runId = '123e4567-e89b-42d3-a456-42661417409b';- const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);- let handlerAttached = false;- mockHarnessHookRun(runId, 'tool-harness-malformed', () => handlerAttached, [harnessHook(placeholder)]);- const resolver = vi.fn(async () => ({ kind: 'permission', behavior: 'allow' }) as const);- const hook = vi.fn(async () => ({ action: 'block' }) as const);- const handle = await new PiAgent(buildDeps()).startSession(opts());- handle.setInteractionResolver(resolver);- handle.setHarnessHookHandler?.(hook);- handlerAttached = true;-- await vi.waitFor(() => expect(control).toHaveBeenCalledWith(- expect.any(String),- 'tool-harness-malformed',- 'approval',- expect.objectContaining({ approvalId: 'hook-1', value: JSON.stringify({ action: 'continue' }) }),- ), { timeout: 3_000 });- expect(hook).not.toHaveBeenCalled();expect(resolver).not.toHaveBeenCalled();await handle.close();@@ -1672,15 +1618,14 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {// and was escalated as an unreviewable action.const runId = '123e4567-e89b-42d3-a456-42661417409c';const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);- let handlerAttached = false;- mockHarnessHookRun(runId, 'tool-harness-auto', () => handlerAttached);+ let ready = false;+ mockHarnessHookRun(runId, 'tool-harness-auto', () => ready);const review = vi.fn(async () => ({ verdict: 'allow' as const }));const resolver = vi.fn(async () => ({ kind: 'permission', behavior: 'allow' }) as const);const handle = await new PiAgent(buildDeps({ reviewAutoPermissionAction: review as never })).startSession({ ...opts(), permissionMode: 'auto' });handle.setInteractionResolver(resolver);- handle.setHarnessHookHandler?.(async () => ({ action: 'continue' }));- handlerAttached = true;+ ready = true;await vi.waitFor(() => expect(control).toHaveBeenCalledWith(expect.any(String),@@ -1695,12 +1640,12 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {});it('still forwards a real cindy:permission with its tool and input next to a hook', async () => {- // The hook gate must only take `cindy:harness-hook`; a permission raised+ // The hook branch must only take `cindy:harness-hook`; a permission raised// in the same run keeps its tool name and arguments and its approval card.const runId = '123e4567-e89b-42d3-a456-42661417409d';const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);- let handlerAttached = false;- mockHarnessHookRun(runId, 'tool-harness-mixed', () => handlerAttached, [+ let ready = false;+ mockHarnessHookRun(runId, 'tool-harness-mixed', () => ready, [toolBeforeHook,{id: 'approval-2',@@ -1710,11 +1655,9 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {},]);const resolver = vi.fn(async () => ({ kind: 'permission', behavior: 'allow' }) as const);- const hook = vi.fn(async () => ({ action: 'continue' }) as const);const handle = await new PiAgent(buildDeps()).startSession(opts());handle.setInteractionResolver(resolver);- handle.setHarnessHookHandler?.(hook);- handlerAttached = true;+ ready = true;await vi.waitFor(() => {expect(control).toHaveBeenCalledWith(@@ -1730,7 +1673,6 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {expect.objectContaining({ childId: `${runId}-2`, approvalId: 'approval-2', confirmed: true }),);}, { timeout: 3_000 });- expect(hook).toHaveBeenCalledOnce();expect(resolver).toHaveBeenCalledOnce();expect(resolver).toHaveBeenCalledWith(expect.objectContaining({kind: 'permission',@@ -1742,39 +1684,6 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {await handle.close();});- it('answers continue, not the bridge close block, when a navigation close lands mid-hook', async () => {- // Closing the bridge settles pending hooks as `block`; the detached child- // keeps running and must not have its tool refused because the view left.- const runId = '123e4567-e89b-42d3-a456-426614174099';- const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);- let handlerAttached = false;- mockHarnessHookRun(runId, 'tool-harness-hook-close', () => handlerAttached);- let hookEntered!: () => void;- const entered = new Promise<void>((resolve) => { hookEntered = resolve; });- const handle = await new PiAgent(buildDeps()).startSession(opts());- handle.setHarnessHookHandler?.(() => {- hookEntered();- return new Promise(() => {});- });- handlerAttached = true;- await entered;-- await handle.close({ reason: 'navigation' });-- await vi.waitFor(() => expect(control).toHaveBeenCalledWith(- expect.any(String),- 'tool-harness-hook-close',- 'approval',- expect.objectContaining({ approvalId: 'hook-1', value: JSON.stringify({ action: 'continue' }) }),- ), { timeout: 3_000 });- expect(control).not.toHaveBeenCalledWith(- expect.any(String),- 'tool-harness-hook-close',- 'approval',- expect.objectContaining({ value: JSON.stringify({ action: 'block' }) }),- );- });-/*** The caller-side fence check is not the write. Between them* `controlPiSubagentRuns` discovers runs and guards the run directory —diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.tsindex 62d3453a54..876dc44537 100644--- a/packages/maker-core/src/agents/pi/index.ts+++ b/packages/maker-core/src/agents/pi/index.ts@@ -4727,45 +4727,19 @@ export class PiAgent extends BaseAgent {return;}if (approval.method === 'input' && approval.title === 'cindy:harness-hook') {- // Child bridges raise their internal tool.before / compaction.before- // sync hooks on the same extension_ui channel as permissions. They are- // not approvals: answer them the way the root session's RPC loop does- // (same validity gate, `continue` when hooks are off or this handle has- // closed) instead of rendering a `Subagent: tool` card with `{}` input.- let request: Record<string, unknown> = {};+ // The harness hook bridge was reverted (2f2721a49b), so nothing on the+ // host handles these any more, but a durable child started by an older+ // bridge can still raise one. It is an internal sync hook, not an+ // approval: answer `continue` (the native policy) instead of rendering+ // a `Subagent: tool` card with `{}` input (#7494).try {- const parsed = JSON.parse(approval.placeholder ?? '{}') as unknown;- if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {- request = parsed as Record<string, unknown>;- }- } catch {- // Invalid bridge input preserves the original native policy.- }- const valid = harnessHooksEnabled && !closed- && (request.name === 'compaction.before'- || (request.name === 'tool.before' && typeof request.toolName === 'string'));- try {- const result = valid- ? await harnessHookBridge.run({- name: request.name as 'tool.before' | 'compaction.before',- ...(request.name === 'tool.before' ? {- toolName: request.toolName as string,- toolUseId: typeof request.toolUseId === 'string' ? request.toolUseId : undefined,- input: request.input,- } : {}),- })- : { action: 'continue' as const };- // Same fence as the turn-change acknowledgement: park across an- // account switch rather than answer for the outgoing account.+ // Park across an account switch rather than answer for the outgoing+ // account, as the turn-change acknowledgement does.if (accountBoundaryTeardown) return;- // A navigation close during the wait closes the bridge, which settles- // pending hooks as `block`. A detached child keeps running, so that- // must not reach it: once this handle is closed, answer `continue`.- const answer = closed ? { action: 'continue' as const } : result;const controlled = await publishPiSubagentApproval(status.taskId, {childId: task.childId,approvalId: approval.id,- value: JSON.stringify(answer),+ value: JSON.stringify({ action: 'continue' }),runtimeOwnerId: controlOwnerId,});if (controlled > 0) piSubagentApprovalDeliveries.add(key);