Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(pi): answer Subagent harness hooks instead of forwarding them as approvals

Zhao Dengfeng380cf4145ccc
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.ts
index 1ebd50082e..5dba3b5932 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
@@ -1532,6 +1532,73 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {
await handle.close();
});
+ // #7494: a child's internal `cindy:harness-hook` shares the extension_ui
+ // channel with permissions. It must be answered through the harness hook
+ // bridge, never surfaced as a `Subagent: tool` approval with `{}` input.
+ it('answers a durable Subagent harness hook through the hook bridge instead of an approval prompt', async () => {
+ const runId = '123e4567-e89b-42d3-a456-426614174098';
+ const control = vi.spyOn(piSubagentRuns, 'controlPiSubagentRuns').mockResolvedValue(1);
+ // Surface the child's request only after the hook handler is attached, as
+ // in a live session where the handler is wired before tools run.
+ let handlerAttached = false;
+ mockRunDiscovery().mockImplementation(async () => !handlerAttached ? [] : [{
+ version: 1,
+ runId,
+ taskId: 'tool-harness-hook',
+ parentSessionId: 's1',
+ runtimeOwnerId: ownerId(),
+ runnerInstanceId: 'runner-harness-hook',
+ state: 'running',
+ startedAt: 1,
+ updatedAt: 2,
+ tasks: [{
+ childId: `${runId}-1`,
+ sessionId: `${runId}-1`,
+ agent: 'worker',
+ status: 'running',
+ pendingApproval: {
+ id: 'hook-1',
+ method: 'input',
+ title: 'cindy:harness-hook',
+ placeholder: JSON.stringify({
+ name: 'tool.before',
+ toolName: 'bash',
+ toolUseId: 'bash-1',
+ input: { command: 'git status' },
+ }),
+ },
+ }],
+ }]);
+ 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;
+
+ await vi.waitFor(() => expect(control).toHaveBeenCalledWith(
+ expect.any(String),
+ 'tool-harness-hook',
+ 'approval',
+ expect.objectContaining({
+ childId: `${runId}-1`,
+ approvalId: 'hook-1',
+ // The bridge forwards only the action; reasons never reach Pi.
+ value: JSON.stringify({ action: 'block' }),
+ 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();
+ });
+
/**
* 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.ts
index a04d9ac3eb..321c1c384b 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -4727,6 +4727,54 @@ 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> = {};
+ 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.
+ if (accountBoundaryTeardown) return;
+ const controlled = await publishPiSubagentApproval(status.taskId, {
+ childId: task.childId,
+ approvalId: approval.id,
+ value: JSON.stringify(result),
+ runtimeOwnerId: controlOwnerId,
+ });
+ if (controlled > 0) piSubagentApprovalDeliveries.add(key);
+ } catch (error) {
+ this.deps.logger.warn('PI Subagent harness hook response failed', {
+ message: error instanceof Error ? error.message : String(error),
+ });
+ } finally {
+ piSubagentApprovalRequests.delete(key);
+ }
+ return;
+ }
let toolName = 'tool';
let input: Record<string, unknown> = {};
let resolvedWritePath: string | null | undefined;