@evandu/super-pi-server 0.0.0-stage → 0.1.260
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -3
- package/agents/code-auditor.md +64 -0
- package/agents/code-designer.md +64 -0
- package/agents/coder.md +24 -0
- package/agents/critic.md +24 -0
- package/agents/document-organizer.md +48 -0
- package/agents/patcher.md +27 -0
- package/agents/planner.md +30 -0
- package/agents/project-leader.md +28 -0
- package/agents/reviewer.md +27 -0
- package/agents/scout.md +27 -0
- package/agents/solution-designer.md +62 -0
- package/agents/super-pi-documents/index.ts +92 -0
- package/agents/super-pi-feishu/index.ts +88 -0
- package/agents/super-pi-knowledge/index.ts +70 -0
- package/agents/super-pi-managed-model/index.ts +9 -0
- package/agents/super-pi-memory/index.ts +75 -0
- package/agents/super-pi-native-tools/index.ts +1 -0
- package/agents/super-pi-task/core.ts +2262 -0
- package/agents/super-pi-task/index.ts +814 -0
- package/agents/super-pi-task/workflow-schema.ts +34 -0
- package/agents/super-pi-web/index.ts +32 -0
- package/agents/verifier.md +25 -0
- package/assets/fonts/LICENSE +92 -0
- package/assets/fonts/NotoSansSC-Bold.otf +0 -0
- package/assets/fonts/NotoSansSC-Regular.otf +0 -0
- package/assets/knowledge-model/README.md +37 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/config.json +31 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/onnx/model_quantized.onnx +0 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/special_tokens_map.json +7 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer.json +21278 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer_config.json +15 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/vocab.txt +21128 -0
- package/bin/npm-prefix.d.ts +1 -0
- package/bin/npm-prefix.js +10 -0
- package/bin/super-pi.js +349 -0
- package/dist/client/.super-pi-client-sources.json +82 -0
- package/dist/client/assets/CodeMirrorEditor-mi9rGUce.js +2 -0
- package/dist/client/assets/GitDiffEditor-CpoDDQrA.css +1 -0
- package/dist/client/assets/GitDiffEditor-FQBfGxOY.js +29 -0
- package/dist/client/assets/diff-ChtP43wD.js +1 -0
- package/dist/client/assets/dist-AW5lyI3I.js +1 -0
- package/dist/client/assets/dist-B3-a6uF0.js +7 -0
- package/dist/client/assets/dist-BGxkyh6e.js +1 -0
- package/dist/client/assets/dist-BJxfd7yb.js +11 -0
- package/dist/client/assets/dist-BRVQKlcp.js +1 -0
- package/dist/client/assets/dist-BV64A9Ji.js +1 -0
- package/dist/client/assets/dist-BXAOrsjq.js +1 -0
- package/dist/client/assets/dist-CKF2Jksy.js +1 -0
- package/dist/client/assets/dist-CX_Hm4GS.js +1 -0
- package/dist/client/assets/dist-Cus6_oaF.js +2 -0
- package/dist/client/assets/dist-Cx00EZKR.js +23 -0
- package/dist/client/assets/dist-D71y2EI1.js +1 -0
- package/dist/client/assets/dist-D978o95T.js +5 -0
- package/dist/client/assets/dist-Dc1MBGAG.js +1 -0
- package/dist/client/assets/dist-Dds-qLxY.js +9 -0
- package/dist/client/assets/dist-DeD9copn.js +1 -0
- package/dist/client/assets/dist-DuvGReu-.js +6 -0
- package/dist/client/assets/dist-Dvb0SqcK.js +2 -0
- package/dist/client/assets/dist-J6XWqRpB.js +3 -0
- package/dist/client/assets/dist-QzbOCAU6.js +1 -0
- package/dist/client/assets/dist-moiMRpw_.js +1 -0
- package/dist/client/assets/dist-scBkrwv-.js +2 -0
- package/dist/client/assets/dockerfile-BDePf5PZ.js +1 -0
- package/dist/client/assets/index-Cn7hdrDw.js +350 -0
- package/dist/client/assets/index-UfzqdECL.css +1 -0
- package/dist/client/assets/powershell-D8WpQPaX.js +1 -0
- package/dist/client/assets/properties-C357ku5U.js +1 -0
- package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/client/assets/ruby-ChfNwlCY.js +1 -0
- package/dist/client/assets/shell-DwuoZtxw.js +1 -0
- package/dist/client/assets/swift-DKB6_1j6.js +1 -0
- package/dist/client/assets/toml-BPTmHmyx.js +1 -0
- package/dist/client/favicon.ico +0 -0
- package/dist/client/favicon.png +0 -0
- package/dist/client/favicon.svg +10 -0
- package/dist/client/index.html +18 -0
- package/dist/extension.d.ts +15 -0
- package/dist/extension.js +126 -0
- package/dist/server/active-memory.d.ts +247 -0
- package/dist/server/active-memory.js +669 -0
- package/dist/server/agents.d.ts +245 -0
- package/dist/server/agents.js +701 -0
- package/dist/server/application/catalog-service.d.ts +98 -0
- package/dist/server/application/catalog-service.js +245 -0
- package/dist/server/application/conversation-service.d.ts +245 -0
- package/dist/server/application/conversation-service.js +654 -0
- package/dist/server/application/document-service.d.ts +68 -0
- package/dist/server/application/document-service.js +120 -0
- package/dist/server/application/extension-ui-service.d.ts +44 -0
- package/dist/server/application/extension-ui-service.js +135 -0
- package/dist/server/application/feishu-service.d.ts +179 -0
- package/dist/server/application/feishu-service.js +358 -0
- package/dist/server/application/group-chat-service.d.ts +75 -0
- package/dist/server/application/group-chat-service.js +953 -0
- package/dist/server/application/group-execution.d.ts +15 -0
- package/dist/server/application/group-execution.js +77 -0
- package/dist/server/application/project-service.d.ts +464 -0
- package/dist/server/application/project-service.js +2523 -0
- package/dist/server/application/provider-service.d.ts +174 -0
- package/dist/server/application/provider-service.js +510 -0
- package/dist/server/application/run-service.d.ts +547 -0
- package/dist/server/application/run-service.js +2683 -0
- package/dist/server/application/scheduled-task-service.d.ts +184 -0
- package/dist/server/application/scheduled-task-service.js +391 -0
- package/dist/server/application/session-orchestrator-service.d.ts +388 -0
- package/dist/server/application/session-orchestrator-service.js +1146 -0
- package/dist/server/application/session-reminder-service.d.ts +59 -0
- package/dist/server/application/session-reminder-service.js +141 -0
- package/dist/server/application/session-watchdog.d.ts +171 -0
- package/dist/server/application/session-watchdog.js +284 -0
- package/dist/server/application/workspace-service.d.ts +52 -0
- package/dist/server/application/workspace-service.js +186 -0
- package/dist/server/application/worktree-service.d.ts +13 -0
- package/dist/server/application/worktree-service.js +76 -0
- package/dist/server/browser-access.d.ts +34 -0
- package/dist/server/browser-access.js +89 -0
- package/dist/server/capability.d.ts +105 -0
- package/dist/server/capability.js +170 -0
- package/dist/server/command-handler.d.ts +93 -0
- package/dist/server/command-handler.js +540 -0
- package/dist/server/content-search.d.ts +16 -0
- package/dist/server/content-search.js +235 -0
- package/dist/server/conversation-usage.d.ts +22 -0
- package/dist/server/conversation-usage.js +43 -0
- package/dist/server/documents.d.ts +152 -0
- package/dist/server/documents.js +998 -0
- package/dist/server/engineering-learning.d.ts +30 -0
- package/dist/server/engineering-learning.js +106 -0
- package/dist/server/exec-service.d.ts +45 -0
- package/dist/server/exec-service.js +167 -0
- package/dist/server/explicit-file-open.d.ts +34 -0
- package/dist/server/explicit-file-open.js +105 -0
- package/dist/server/failure-text.d.ts +18 -0
- package/dist/server/failure-text.js +44 -0
- package/dist/server/feishu-cli.d.ts +223 -0
- package/dist/server/feishu-cli.js +464 -0
- package/dist/server/file-changes.d.ts +119 -0
- package/dist/server/file-changes.js +280 -0
- package/dist/server/file-diff.d.ts +41 -0
- package/dist/server/file-diff.js +97 -0
- package/dist/server/file-index.d.ts +100 -0
- package/dist/server/file-index.js +296 -0
- package/dist/server/file-name-index.d.ts +160 -0
- package/dist/server/file-name-index.js +411 -0
- package/dist/server/files.d.ts +269 -0
- package/dist/server/files.js +748 -0
- package/dist/server/git.d.ts +245 -0
- package/dist/server/git.js +903 -0
- package/dist/server/http-server.d.ts +45 -0
- package/dist/server/http-server.js +3830 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +485 -0
- package/dist/server/inline-thinking.d.ts +90 -0
- package/dist/server/inline-thinking.js +181 -0
- package/dist/server/json-comments.d.ts +14 -0
- package/dist/server/json-comments.js +71 -0
- package/dist/server/knowledge-embedding-worker.d.ts +1 -0
- package/dist/server/knowledge-embedding-worker.js +27 -0
- package/dist/server/knowledge-vectors.d.ts +150 -0
- package/dist/server/knowledge-vectors.js +385 -0
- package/dist/server/knowledge.d.ts +273 -0
- package/dist/server/knowledge.js +848 -0
- package/dist/server/length-continuation.d.ts +83 -0
- package/dist/server/length-continuation.js +118 -0
- package/dist/server/logging.d.ts +32 -0
- package/dist/server/logging.js +185 -0
- package/dist/server/managed-http-tunnel.d.ts +6 -0
- package/dist/server/managed-http-tunnel.js +85 -0
- package/dist/server/managed-http.d.ts +4 -0
- package/dist/server/managed-http.js +85 -0
- package/dist/server/managed-model-adapter.d.ts +17 -0
- package/dist/server/managed-model-adapter.js +75 -0
- package/dist/server/managed-model-gateway.d.ts +49 -0
- package/dist/server/managed-model-gateway.js +73 -0
- package/dist/server/managed-servers.d.ts +55 -0
- package/dist/server/managed-servers.js +396 -0
- package/dist/server/managed-ssh.d.ts +26 -0
- package/dist/server/managed-ssh.js +73 -0
- package/dist/server/manager-vault.d.ts +11 -0
- package/dist/server/manager-vault.js +67 -0
- package/dist/server/memory-recall.d.ts +25 -0
- package/dist/server/memory-recall.js +45 -0
- package/dist/server/memory.d.ts +67 -0
- package/dist/server/memory.js +216 -0
- package/dist/server/message-media.d.ts +7 -0
- package/dist/server/message-media.js +54 -0
- package/dist/server/model-context-window.d.ts +186 -0
- package/dist/server/model-context-window.js +444 -0
- package/dist/server/model-filter.d.ts +23 -0
- package/dist/server/model-filter.js +48 -0
- package/dist/server/models-store.d.ts +36 -0
- package/dist/server/models-store.js +132 -0
- package/dist/server/native-tool-settings.d.ts +25 -0
- package/dist/server/native-tool-settings.js +41 -0
- package/dist/server/native-tools-extension.d.ts +2 -0
- package/dist/server/native-tools-extension.js +39 -0
- package/dist/server/native-tools-service.d.ts +39 -0
- package/dist/server/native-tools-service.js +240 -0
- package/dist/server/persistence/app-state-repository.d.ts +65 -0
- package/dist/server/persistence/app-state-repository.js +1 -0
- package/dist/server/persistence/json-state-repository.d.ts +14 -0
- package/dist/server/persistence/json-state-repository.js +43 -0
- package/dist/server/persistence/open-state-store.d.ts +3 -0
- package/dist/server/persistence/open-state-store.js +35 -0
- package/dist/server/persistence/rename.d.ts +22 -0
- package/dist/server/persistence/rename.js +44 -0
- package/dist/server/persistence/sqlite-state-repository.d.ts +12 -0
- package/dist/server/persistence/sqlite-state-repository.js +264 -0
- package/dist/server/pi-command.d.ts +94 -0
- package/dist/server/pi-command.js +265 -0
- package/dist/server/pi-native.d.ts +12 -0
- package/dist/server/pi-native.js +16 -0
- package/dist/server/pi-session-events.d.ts +158 -0
- package/dist/server/pi-session-events.js +373 -0
- package/dist/server/pi-session-pool.d.ts +233 -0
- package/dist/server/pi-session-pool.js +382 -0
- package/dist/server/pi-session.d.ts +642 -0
- package/dist/server/pi-session.js +655 -0
- package/dist/server/presentations/schema.d.ts +120 -0
- package/dist/server/presentations/schema.js +128 -0
- package/dist/server/provider-network.d.ts +16 -0
- package/dist/server/provider-network.js +106 -0
- package/dist/server/provider-proxy.d.ts +18 -0
- package/dist/server/provider-proxy.js +200 -0
- package/dist/server/providers.d.ts +333 -0
- package/dist/server/providers.js +989 -0
- package/dist/server/remote-bootstrap.d.ts +17 -0
- package/dist/server/remote-bootstrap.js +119 -0
- package/dist/server/rpc-channel.d.ts +131 -0
- package/dist/server/rpc-channel.js +135 -0
- package/dist/server/runtime.d.ts +1393 -0
- package/dist/server/runtime.js +3286 -0
- package/dist/server/scheduled-tasks.d.ts +101 -0
- package/dist/server/scheduled-tasks.js +138 -0
- package/dist/server/server-context.d.ts +119 -0
- package/dist/server/server-context.js +1 -0
- package/dist/server/session-cwd.d.ts +19 -0
- package/dist/server/session-cwd.js +58 -0
- package/dist/server/session-ledger.d.ts +67 -0
- package/dist/server/session-ledger.js +76 -0
- package/dist/server/session-memory-scope.d.ts +4 -0
- package/dist/server/session-memory-scope.js +30 -0
- package/dist/server/session-repository.d.ts +100 -0
- package/dist/server/session-repository.js +246 -0
- package/dist/server/session-tree-scope.d.ts +125 -0
- package/dist/server/session-tree-scope.js +337 -0
- package/dist/server/skills.d.ts +23 -0
- package/dist/server/skills.js +190 -0
- package/dist/server/store.d.ts +1062 -0
- package/dist/server/store.js +3305 -0
- package/dist/server/subagent-runs.d.ts +185 -0
- package/dist/server/subagent-runs.js +383 -0
- package/dist/server/subagents.d.ts +200 -0
- package/dist/server/subagents.js +619 -0
- package/dist/server/task-tool.d.ts +282 -0
- package/dist/server/task-tool.js +507 -0
- package/dist/server/tavily.d.ts +32 -0
- package/dist/server/tavily.js +136 -0
- package/dist/server/team-context.d.ts +12 -0
- package/dist/server/team-context.js +180 -0
- package/dist/server/terminal.d.ts +163 -0
- package/dist/server/terminal.js +557 -0
- package/dist/server/thinking-level-discovery.d.ts +38 -0
- package/dist/server/thinking-level-discovery.js +183 -0
- package/dist/server/time-zone.d.ts +20 -0
- package/dist/server/time-zone.js +32 -0
- package/dist/server/tool-calls.d.ts +130 -0
- package/dist/server/tool-calls.js +341 -0
- package/dist/server/tool-llm.d.ts +72 -0
- package/dist/server/tool-llm.js +146 -0
- package/dist/server/tool-registry.d.ts +129 -0
- package/dist/server/tool-registry.js +249 -0
- package/dist/server/tool-runner.d.ts +112 -0
- package/dist/server/tool-runner.js +306 -0
- package/dist/server/turn-failure.d.ts +48 -0
- package/dist/server/turn-failure.js +148 -0
- package/dist/server/types.d.ts +2315 -0
- package/dist/server/types.js +11 -0
- package/dist/server/update-status.d.ts +14 -0
- package/dist/server/update-status.js +69 -0
- package/dist/server/update.d.ts +46 -0
- package/dist/server/update.js +104 -0
- package/dist/server/upload.d.ts +35 -0
- package/dist/server/upload.js +177 -0
- package/dist/server/version.d.ts +33 -0
- package/dist/server/version.js +111 -0
- package/dist/server/web.d.ts +27 -0
- package/dist/server/web.js +183 -0
- package/dist/server/workflow-engine.d.ts +47 -0
- package/dist/server/workflow-engine.js +255 -0
- package/dist/server/workflow.d.ts +329 -0
- package/dist/server/workflow.js +469 -0
- package/dist/server/workspace-file-indexes.d.ts +2 -0
- package/dist/server/workspace-file-indexes.js +61 -0
- package/dist/server/workspace-file-operations.d.ts +36 -0
- package/dist/server/workspace-file-operations.js +281 -0
- package/dist/server/workspace-file-response.d.ts +7 -0
- package/dist/server/workspace-file-response.js +79 -0
- package/dist/server/ws-server.d.ts +149 -0
- package/dist/server/ws-server.js +640 -0
- package/dist/shared/agent-order.d.ts +44 -0
- package/dist/shared/agent-order.js +50 -0
- package/dist/shared/archive-decisions.d.ts +44 -0
- package/dist/shared/archive-decisions.js +98 -0
- package/dist/shared/background-task.d.ts +25 -0
- package/dist/shared/background-task.js +183 -0
- package/dist/shared/child-session-lock.d.ts +37 -0
- package/dist/shared/child-session-lock.js +59 -0
- package/dist/shared/content-search.d.ts +22 -0
- package/dist/shared/content-search.js +1 -0
- package/dist/shared/conversation-title.d.ts +4 -0
- package/dist/shared/conversation-title.js +4 -0
- package/dist/shared/file-mentions.d.ts +17 -0
- package/dist/shared/file-mentions.js +91 -0
- package/dist/shared/git-commit.d.ts +11 -0
- package/dist/shared/git-commit.js +1 -0
- package/dist/shared/git-graph.d.ts +42 -0
- package/dist/shared/git-graph.js +30 -0
- package/dist/shared/group-chat.d.ts +132 -0
- package/dist/shared/group-chat.js +6 -0
- package/dist/shared/managed-servers.d.ts +36 -0
- package/dist/shared/managed-servers.js +1 -0
- package/dist/shared/message-media.d.ts +12 -0
- package/dist/shared/message-media.js +27 -0
- package/dist/shared/model-window-label.d.ts +39 -0
- package/dist/shared/model-window-label.js +55 -0
- package/dist/shared/native-tools.d.ts +43 -0
- package/dist/shared/native-tools.js +1 -0
- package/dist/shared/pi-cache-warming.d.ts +9 -0
- package/dist/shared/pi-cache-warming.js +4 -0
- package/dist/shared/project-overview.d.ts +107 -0
- package/dist/shared/project-overview.js +92 -0
- package/dist/shared/project-presentation.d.ts +55 -0
- package/dist/shared/project-presentation.js +111 -0
- package/dist/shared/provider-network.d.ts +30 -0
- package/dist/shared/provider-network.js +1 -0
- package/dist/shared/session-reporting.d.ts +40 -0
- package/dist/shared/session-reporting.js +97 -0
- package/dist/shared/session-search.d.ts +14 -0
- package/dist/shared/session-search.js +1 -0
- package/dist/shared/team-context-view.d.ts +33 -0
- package/dist/shared/team-context-view.js +49 -0
- package/dist/shared/team-context.d.ts +59 -0
- package/dist/shared/team-context.js +3 -0
- package/dist/shared/terminal-protocol.d.ts +71 -0
- package/dist/shared/terminal-protocol.js +1 -0
- package/dist/shared/thinking-levels.d.ts +53 -0
- package/dist/shared/thinking-levels.js +85 -0
- package/dist/shared/tool-glyph.d.ts +7 -0
- package/dist/shared/tool-glyph.js +19 -0
- package/dist/shared/transcript-order.d.ts +7 -0
- package/dist/shared/transcript-order.js +12 -0
- package/dist/shared/update-status.d.ts +28 -0
- package/dist/shared/update-status.js +1 -0
- package/dist/shared/workspace-file-preview.d.ts +4 -0
- package/dist/shared/workspace-file-preview.js +28 -0
- package/package.json +450 -4
- package/scripts/diagnose.mjs +603 -0
- package/scripts/patch-brace-expansion.mjs +260 -0
- package/scripts/restart-server.mjs +104 -0
- package/skills/presentation/SKILL.md +30 -0
- package/skills/presentation/references/data.md +10 -0
- package/skills/presentation/references/quality.md +12 -0
- package/skills/presentation/references/story.md +12 -0
- package/skills/presentation/references/templates-and-editing.md +11 -0
- package/skills/presentation/references/visual-system.md +10 -0
- package/skills/super-pi-agent-development/SKILL.md +25 -0
- package/skills/super-pi-development/SKILL.md +31 -0
- package/skills/super-pi-tool-development/SKILL.md +39 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { ExtensionUiHost } from "./runtime.js";
|
|
2
|
+
import { type SubagentSupervisor } from "./subagents.js";
|
|
3
|
+
import type { Store } from "./store.js";
|
|
4
|
+
import type { ModelSelection, ServerEvent, SubagentRun } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Making one sub-agent run VISIBLE and RECOVERABLE, per
|
|
7
|
+
* [docs/SUBAGENT-DESIGN.md](../../docs/SUBAGENT-DESIGN.md) §5-§6.
|
|
8
|
+
*
|
|
9
|
+
* `subagents.ts` spawns and drains; this file remembers and publishes. The split is the same one Part 1
|
|
10
|
+
* drew: a spawner that also wrote records would have to be tested against a store to prove it streams,
|
|
11
|
+
* and a store that also spawned children could not be tested without one. Everything here is about the
|
|
12
|
+
* two obligations the design names and nothing else:
|
|
13
|
+
*
|
|
14
|
+
* 1. **A record before the child.** Written as `running` BEFORE `run()` is called, so a server that dies
|
|
15
|
+
* mid-run leaves an orphan - and `Store.open` fails that orphan with a reason on the next start.
|
|
16
|
+
* 2. **A snapshot on every delta.** `subagent.event` carries the run as it stands, not just at the end,
|
|
17
|
+
* because streaming a sub-agent is the entire reason Super Pi spawns its own instead of shipping Pi's
|
|
18
|
+
* extension (design §2, reason 1). The snapshot's `output` is the TAIL of the accumulated text
|
|
19
|
+
* ({@link LIVE_OUTPUT_TAIL_CAP}); the whole text is in the store and in the settle event.
|
|
20
|
+
*
|
|
21
|
+
* What deliberately does NOT go into either is the child's transcript. See `SubagentRun`.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* The model-visible output cap, 50 KB - design doc §6's `PER_TASK_OUTPUT_CAP`, taken from the reference
|
|
25
|
+
* implementation (`index.ts:36`) rather than invented here.
|
|
26
|
+
*
|
|
27
|
+
* APPLIED TO THE STORED COPY ONLY. A human looking at a sub-agent is debugging it, and trimming its
|
|
28
|
+
* answer to fit somebody else's context window is the wrong trade for them; the browser gets every byte
|
|
29
|
+
* through `subagent.event`, and `outputTruncated` is what tells a reader the stored copy is less than
|
|
30
|
+
* what was said. The parent model is handed the capped copy, because that is the window it actually has.
|
|
31
|
+
*/
|
|
32
|
+
export declare const PER_TASK_OUTPUT_CAP: number;
|
|
33
|
+
/**
|
|
34
|
+
* How much of the ACCUMULATED output a delta-phase `subagent.event` may carry.
|
|
35
|
+
*
|
|
36
|
+
* A delta event is a SNAPSHOT the card replaces itself with, and it fires PER DELTA. Carrying the whole
|
|
37
|
+
* accumulated text makes the Nth event N characters long - the traffic is quadratic in the output length
|
|
38
|
+
* (the flood behind "连接中 / 断连 / 连接中", see docs/SUBAGENT-WS-FLOOD-FIX-2026-10-29.md). So a live
|
|
39
|
+
* event carries only the TAIL of what the child has said; the run's full output still reaches everyone -
|
|
40
|
+
* the store keeps a larger tail for reconnects, and the settle event carries the saved result.
|
|
41
|
+
* A watching human needs the last few
|
|
42
|
+
* hundred characters, not the first ones, so the tail is the useful end.
|
|
43
|
+
*
|
|
44
|
+
* THIS CHANGES WHAT AN OLD CLIENT SEES: a card whose child has said more than this shows the latest 8 KB
|
|
45
|
+
* (sliding) instead of the whole text, then the full text at settle. The field shape is unchanged.
|
|
46
|
+
*/
|
|
47
|
+
export declare const LIVE_OUTPUT_TAIL_CAP: number;
|
|
48
|
+
/** What a caller supplies to dispatch one run. The workspace directory is required, like `AnswerInput.cwd`. */
|
|
49
|
+
export type SubagentDispatch = {
|
|
50
|
+
id?: string;
|
|
51
|
+
conversationId: string;
|
|
52
|
+
agentId: string;
|
|
53
|
+
task: string;
|
|
54
|
+
/**
|
|
55
|
+
* Who decided to run this. Defaults to "human" when omitted, which is right for every caller that
|
|
56
|
+
* predates the task tool; the `/api/subagent/dispatch` endpoint - reachable only from the task tool -
|
|
57
|
+
* passes "model" explicitly.
|
|
58
|
+
*/
|
|
59
|
+
dispatchedBy?: "human" | "model";
|
|
60
|
+
/**
|
|
61
|
+
* Where in the dispatching message this run was asked for, resolved by the caller from the tool call's
|
|
62
|
+
* id. Copied straight onto the record, because a card's position is a fact about its BIRTH - it is
|
|
63
|
+
* settled here and no update has to supply it, so the card cannot drift while the child streams.
|
|
64
|
+
* Omitted means "no call asked for it" (a person pressing the button), which is not row zero and never is.
|
|
65
|
+
*/
|
|
66
|
+
originMessageId?: string;
|
|
67
|
+
originCallRow?: number;
|
|
68
|
+
parentRunId?: string;
|
|
69
|
+
depth?: number;
|
|
70
|
+
loopId?: string;
|
|
71
|
+
/** Optional orchestration label. The dispatcher treats labels as opaque data. */
|
|
72
|
+
role?: string;
|
|
73
|
+
/** The dispatching conversation's workspace, resolved by the caller. Never defaulted to the server's own directory. */
|
|
74
|
+
cwd: string;
|
|
75
|
+
model?: ModelSelection;
|
|
76
|
+
/**
|
|
77
|
+
* Tools the dispatching agent denied, inherited by the child. Carried all the way down to
|
|
78
|
+
* `agentSpawnArgs` for the same reason it exists on `SubagentRunInput`: a parent's deny is inherited,
|
|
79
|
+
* its allowlist is not.
|
|
80
|
+
*/
|
|
81
|
+
denyTools?: readonly string[];
|
|
82
|
+
/** Cancels this run. Also cancelled by `abortAll`, so a caller may pass either or both. */
|
|
83
|
+
signal?: AbortSignal;
|
|
84
|
+
};
|
|
85
|
+
/** What the recorder needs from the server around it. Injected, so this file holds no globals of its own. */
|
|
86
|
+
export type SubagentRunHooks = {
|
|
87
|
+
store: Store;
|
|
88
|
+
/**
|
|
89
|
+
* Publishes one event to whoever is listening. Kept as a plain function rather than a socket set so
|
|
90
|
+
* this file can be tested with an array, and so a second consumer (a future HTTP probe, say) can be
|
|
91
|
+
* attached without changing the recorder.
|
|
92
|
+
*/
|
|
93
|
+
broadcast: (event: ServerEvent) => void;
|
|
94
|
+
/**
|
|
95
|
+
* Fires ONCE per run, AFTER the terminal `subagent.event` has been broadcast from `settle()`. This -
|
|
96
|
+
* not the delta phase - is when a caller should broadcast a full `state` snapshot: the run's final
|
|
97
|
+
* record is on disk by then, so a browser that reloads after it sees the finished card. Broadcasting
|
|
98
|
+
* state on every DELTA is what flooded the socket (each delta demanded a full state frame; a burst of
|
|
99
|
+
* deltas demanded hundreds).
|
|
100
|
+
*/
|
|
101
|
+
onSettled?: () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Cap on `run.output` in delta-phase events, in characters; defaults to {@link LIVE_OUTPUT_TAIL_CAP}.
|
|
104
|
+
* A settle event always carries the full output regardless. `Number.POSITIVE_INFINITY` disables the
|
|
105
|
+
* cap - the diagnostic "legacy flood" mode uses that to reproduce the pre-fix wire shape.
|
|
106
|
+
*/
|
|
107
|
+
liveTailCap?: number;
|
|
108
|
+
/**
|
|
109
|
+
* Builds the host that answers a CHILD's questions, keyed by the run as well as the conversation
|
|
110
|
+
* (design §4). Optional because a headless caller has nobody to ask; a child that asks in that case is
|
|
111
|
+
* cancelled, never guessed at.
|
|
112
|
+
*/
|
|
113
|
+
host?: (conversationId: string, runId: string) => ExtensionUiHost;
|
|
114
|
+
closeHost?: (conversationId: string, runId: string) => void;
|
|
115
|
+
};
|
|
116
|
+
export declare class SubagentRunRecorder {
|
|
117
|
+
private readonly supervisor;
|
|
118
|
+
private readonly hooks;
|
|
119
|
+
/**
|
|
120
|
+
* One entry per in-flight run, so `abortAll` reaches children the caller cannot name and `stop` can
|
|
121
|
+
* reach one the caller CAN.
|
|
122
|
+
*
|
|
123
|
+
* The conversation id is carried alongside the controller rather than looked up from the supervisor's
|
|
124
|
+
* live view, for two reasons that are really one: the supervisor's entry is deleted the moment the
|
|
125
|
+
* run settles (so it is absent exactly when a stop is most likely to be sent - the card reacting to
|
|
126
|
+
* a late click), and `run.stop` needs the CONVERSATION a run belongs to, which only the recorder
|
|
127
|
+
* knows before the supervisor is asked.
|
|
128
|
+
*/
|
|
129
|
+
private readonly controllers;
|
|
130
|
+
constructor(supervisor: SubagentSupervisor, hooks: SubagentRunHooks);
|
|
131
|
+
/** Updates agent discovery directories on the underlying supervisor. */
|
|
132
|
+
setAgentsDir(dir: string | string[]): void;
|
|
133
|
+
/** Every child still running, optionally narrowed to one conversation. The supervisor owns the pids. */
|
|
134
|
+
liveRuns(conversationId?: string): {
|
|
135
|
+
id: string;
|
|
136
|
+
conversationId: string;
|
|
137
|
+
pid: number | undefined;
|
|
138
|
+
}[];
|
|
139
|
+
/**
|
|
140
|
+
* Stops ONE run, and says whether there was anything to stop.
|
|
141
|
+
*
|
|
142
|
+
* THIS IS THE BUTTON THE 32B GAP LEFT OPEN. `run.stop` used to abort the conversation's own Pi and
|
|
143
|
+
* nothing else, so a sub-agent the user could see on screen kept running - and kept holding the
|
|
144
|
+
* workspace directory - after they pressed stop. The abort here is the SAME signal the supervisor
|
|
145
|
+
* turns into an `abort` write followed by `killTree` on Windows, so stopping one really does reach
|
|
146
|
+
* the grandchild Pi rather than only the `.cmd` shim.
|
|
147
|
+
*
|
|
148
|
+
* `false` for an unknown or already-settled id is deliberate, not a swallowed failure: the click
|
|
149
|
+
* that produces it raced the run's own terminal `subagent.event`, and an `error` reply for that
|
|
150
|
+
* would put a failure banner on top of a card that says it finished normally.
|
|
151
|
+
*/
|
|
152
|
+
stop(runId: string): boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Stops every run in flight for one conversation, and returns how many were signalled.
|
|
155
|
+
*
|
|
156
|
+
* What `run.stop` on a conversation now does to the children it started, so the conversation's own
|
|
157
|
+
* stop button and a sub-agent card's stop button end the same way. Same reason for the return value
|
|
158
|
+
* as `stop`: it is a count the caller can log, not an error.
|
|
159
|
+
*/
|
|
160
|
+
stopConversation(conversationId: string): number;
|
|
161
|
+
/**
|
|
162
|
+
* Aborts every run in flight.
|
|
163
|
+
*
|
|
164
|
+
* Called on shutdown, and the reason it is worth having: a child left running when the server exits
|
|
165
|
+
* keeps holding its workspace directory, and Windows will then refuse to delete it. Startup
|
|
166
|
+
* reconciliation cleans up the RECORD of such a run; this stops the PROCESS from being one.
|
|
167
|
+
*/
|
|
168
|
+
abortAll(): void;
|
|
169
|
+
/** Waits for every child that was live at shutdown to settle and record its outcome. */
|
|
170
|
+
waitForIdle(): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* Runs one sub-agent, recording it from before the spawn to after the settle, and returns the record
|
|
173
|
+
* as it ended.
|
|
174
|
+
*
|
|
175
|
+
* ALWAYS RESOLVES, including for a dispatch that never started - an unknown agent, a deleted workspace,
|
|
176
|
+
* a Pi that is not installed. By the time `run()` is called the `running` record is already written, so
|
|
177
|
+
* the honest account of "this never ran" is a record that says exactly why, next to the runs that did.
|
|
178
|
+
* Rejecting instead would leave that row claiming to be running until the next restart, which is the
|
|
179
|
+
* orphan this whole file is about.
|
|
180
|
+
*
|
|
181
|
+
* A caller that wants the child's answer rather than a verdict reads `status` and `output` off the
|
|
182
|
+
* returned record; it does not pattern-match an error message to find out whether work happened.
|
|
183
|
+
*/
|
|
184
|
+
dispatch(input: SubagentDispatch): Promise<SubagentRun>;
|
|
185
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { SubagentCancelled, newSubagentRunId } from "./subagents.js";
|
|
2
|
+
/**
|
|
3
|
+
* Making one sub-agent run VISIBLE and RECOVERABLE, per
|
|
4
|
+
* [docs/SUBAGENT-DESIGN.md](../../docs/SUBAGENT-DESIGN.md) §5-§6.
|
|
5
|
+
*
|
|
6
|
+
* `subagents.ts` spawns and drains; this file remembers and publishes. The split is the same one Part 1
|
|
7
|
+
* drew: a spawner that also wrote records would have to be tested against a store to prove it streams,
|
|
8
|
+
* and a store that also spawned children could not be tested without one. Everything here is about the
|
|
9
|
+
* two obligations the design names and nothing else:
|
|
10
|
+
*
|
|
11
|
+
* 1. **A record before the child.** Written as `running` BEFORE `run()` is called, so a server that dies
|
|
12
|
+
* mid-run leaves an orphan - and `Store.open` fails that orphan with a reason on the next start.
|
|
13
|
+
* 2. **A snapshot on every delta.** `subagent.event` carries the run as it stands, not just at the end,
|
|
14
|
+
* because streaming a sub-agent is the entire reason Super Pi spawns its own instead of shipping Pi's
|
|
15
|
+
* extension (design §2, reason 1). The snapshot's `output` is the TAIL of the accumulated text
|
|
16
|
+
* ({@link LIVE_OUTPUT_TAIL_CAP}); the whole text is in the store and in the settle event.
|
|
17
|
+
*
|
|
18
|
+
* What deliberately does NOT go into either is the child's transcript. See `SubagentRun`.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* The model-visible output cap, 50 KB - design doc §6's `PER_TASK_OUTPUT_CAP`, taken from the reference
|
|
22
|
+
* implementation (`index.ts:36`) rather than invented here.
|
|
23
|
+
*
|
|
24
|
+
* APPLIED TO THE STORED COPY ONLY. A human looking at a sub-agent is debugging it, and trimming its
|
|
25
|
+
* answer to fit somebody else's context window is the wrong trade for them; the browser gets every byte
|
|
26
|
+
* through `subagent.event`, and `outputTruncated` is what tells a reader the stored copy is less than
|
|
27
|
+
* what was said. The parent model is handed the capped copy, because that is the window it actually has.
|
|
28
|
+
*/
|
|
29
|
+
export const PER_TASK_OUTPUT_CAP = 50 * 1024;
|
|
30
|
+
/**
|
|
31
|
+
* How much of the ACCUMULATED output a delta-phase `subagent.event` may carry.
|
|
32
|
+
*
|
|
33
|
+
* A delta event is a SNAPSHOT the card replaces itself with, and it fires PER DELTA. Carrying the whole
|
|
34
|
+
* accumulated text makes the Nth event N characters long - the traffic is quadratic in the output length
|
|
35
|
+
* (the flood behind "连接中 / 断连 / 连接中", see docs/SUBAGENT-WS-FLOOD-FIX-2026-10-29.md). So a live
|
|
36
|
+
* event carries only the TAIL of what the child has said; the run's full output still reaches everyone -
|
|
37
|
+
* the store keeps a larger tail for reconnects, and the settle event carries the saved result.
|
|
38
|
+
* A watching human needs the last few
|
|
39
|
+
* hundred characters, not the first ones, so the tail is the useful end.
|
|
40
|
+
*
|
|
41
|
+
* THIS CHANGES WHAT AN OLD CLIENT SEES: a card whose child has said more than this shows the latest 8 KB
|
|
42
|
+
* (sliding) instead of the whole text, then the full text at settle. The field shape is unchanged.
|
|
43
|
+
*/
|
|
44
|
+
export const LIVE_OUTPUT_TAIL_CAP = 8 * 1024;
|
|
45
|
+
export class SubagentRunRecorder {
|
|
46
|
+
supervisor;
|
|
47
|
+
hooks;
|
|
48
|
+
/**
|
|
49
|
+
* One entry per in-flight run, so `abortAll` reaches children the caller cannot name and `stop` can
|
|
50
|
+
* reach one the caller CAN.
|
|
51
|
+
*
|
|
52
|
+
* The conversation id is carried alongside the controller rather than looked up from the supervisor's
|
|
53
|
+
* live view, for two reasons that are really one: the supervisor's entry is deleted the moment the
|
|
54
|
+
* run settles (so it is absent exactly when a stop is most likely to be sent - the card reacting to
|
|
55
|
+
* a late click), and `run.stop` needs the CONVERSATION a run belongs to, which only the recorder
|
|
56
|
+
* knows before the supervisor is asked.
|
|
57
|
+
*/
|
|
58
|
+
controllers = new Map();
|
|
59
|
+
constructor(supervisor, hooks) {
|
|
60
|
+
this.supervisor = supervisor;
|
|
61
|
+
this.hooks = hooks;
|
|
62
|
+
}
|
|
63
|
+
/** Updates agent discovery directories on the underlying supervisor. */
|
|
64
|
+
setAgentsDir(dir) {
|
|
65
|
+
this.supervisor.setAgentsDir?.(dir);
|
|
66
|
+
}
|
|
67
|
+
/** Every child still running, optionally narrowed to one conversation. The supervisor owns the pids. */
|
|
68
|
+
liveRuns(conversationId) {
|
|
69
|
+
return this.supervisor.liveRuns(conversationId);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Stops ONE run, and says whether there was anything to stop.
|
|
73
|
+
*
|
|
74
|
+
* THIS IS THE BUTTON THE 32B GAP LEFT OPEN. `run.stop` used to abort the conversation's own Pi and
|
|
75
|
+
* nothing else, so a sub-agent the user could see on screen kept running - and kept holding the
|
|
76
|
+
* workspace directory - after they pressed stop. The abort here is the SAME signal the supervisor
|
|
77
|
+
* turns into an `abort` write followed by `killTree` on Windows, so stopping one really does reach
|
|
78
|
+
* the grandchild Pi rather than only the `.cmd` shim.
|
|
79
|
+
*
|
|
80
|
+
* `false` for an unknown or already-settled id is deliberate, not a swallowed failure: the click
|
|
81
|
+
* that produces it raced the run's own terminal `subagent.event`, and an `error` reply for that
|
|
82
|
+
* would put a failure banner on top of a card that says it finished normally.
|
|
83
|
+
*/
|
|
84
|
+
stop(runId) {
|
|
85
|
+
const entry = this.controllers.get(runId);
|
|
86
|
+
if (!entry)
|
|
87
|
+
return false;
|
|
88
|
+
entry.controller.abort();
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Stops every run in flight for one conversation, and returns how many were signalled.
|
|
93
|
+
*
|
|
94
|
+
* What `run.stop` on a conversation now does to the children it started, so the conversation's own
|
|
95
|
+
* stop button and a sub-agent card's stop button end the same way. Same reason for the return value
|
|
96
|
+
* as `stop`: it is a count the caller can log, not an error.
|
|
97
|
+
*/
|
|
98
|
+
stopConversation(conversationId) {
|
|
99
|
+
let stopped = 0;
|
|
100
|
+
for (const entry of this.controllers.values())
|
|
101
|
+
if (entry.conversationId === conversationId) {
|
|
102
|
+
entry.controller.abort();
|
|
103
|
+
stopped += 1;
|
|
104
|
+
}
|
|
105
|
+
return stopped;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Aborts every run in flight.
|
|
109
|
+
*
|
|
110
|
+
* Called on shutdown, and the reason it is worth having: a child left running when the server exits
|
|
111
|
+
* keeps holding its workspace directory, and Windows will then refuse to delete it. Startup
|
|
112
|
+
* reconciliation cleans up the RECORD of such a run; this stops the PROCESS from being one.
|
|
113
|
+
*/
|
|
114
|
+
abortAll() {
|
|
115
|
+
for (const entry of this.controllers.values())
|
|
116
|
+
entry.controller.abort();
|
|
117
|
+
}
|
|
118
|
+
/** Waits for every child that was live at shutdown to settle and record its outcome. */
|
|
119
|
+
async waitForIdle() {
|
|
120
|
+
while (this.controllers.size > 0) {
|
|
121
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Runs one sub-agent, recording it from before the spawn to after the settle, and returns the record
|
|
126
|
+
* as it ended.
|
|
127
|
+
*
|
|
128
|
+
* ALWAYS RESOLVES, including for a dispatch that never started - an unknown agent, a deleted workspace,
|
|
129
|
+
* a Pi that is not installed. By the time `run()` is called the `running` record is already written, so
|
|
130
|
+
* the honest account of "this never ran" is a record that says exactly why, next to the runs that did.
|
|
131
|
+
* Rejecting instead would leave that row claiming to be running until the next restart, which is the
|
|
132
|
+
* orphan this whole file is about.
|
|
133
|
+
*
|
|
134
|
+
* A caller that wants the child's answer rather than a verdict reads `status` and `output` off the
|
|
135
|
+
* returned record; it does not pattern-match an error message to find out whether work happened.
|
|
136
|
+
*/
|
|
137
|
+
async dispatch(input) {
|
|
138
|
+
// `dispatchedBy` defaults to "human" because every caller that existed before the task tool was a
|
|
139
|
+
// person pressing the button, and the model's own calls are the exception that must be marked.
|
|
140
|
+
const { conversationId, agentId, task, cwd, model, denyTools, dispatchedBy = "human" } = input;
|
|
141
|
+
// Generated HERE, not by `run()`, because the record has to exist first - see this file's header.
|
|
142
|
+
const id = input.id ?? newSubagentRunId();
|
|
143
|
+
const started = { id, conversationId, agentId, task, dispatchedBy, parentRunId: input.parentRunId, depth: input.depth ?? 0, loopId: input.loopId, role: input.role, originMessageId: input.originMessageId, originCallRow: input.originCallRow, status: "running", startedAt: new Date().toISOString(), sessionId: id };
|
|
144
|
+
// Own cancellation before the first persistence await. A conversation stop must also reach a
|
|
145
|
+
// human dispatch whose initial row is still being written, before a supervisor/child exists.
|
|
146
|
+
const controller = new AbortController();
|
|
147
|
+
this.controllers.set(id, { controller, conversationId });
|
|
148
|
+
const onCallerAbort = () => controller.abort();
|
|
149
|
+
input.signal?.addEventListener("abort", onCallerAbort, { once: true });
|
|
150
|
+
if (input.signal?.aborted)
|
|
151
|
+
controller.abort();
|
|
152
|
+
try {
|
|
153
|
+
await this.hooks.store.beginSubagentRun(started);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
input.signal?.removeEventListener("abort", onCallerAbort);
|
|
157
|
+
this.controllers.delete(id);
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
// The recorder's own copy, held separately from the store's. Mutating the object `beginSubagentRun`
|
|
161
|
+
// was handed would reach into the store's state behind its back, and every snapshot it hands out
|
|
162
|
+
// would then move under its reader.
|
|
163
|
+
const record = { ...started };
|
|
164
|
+
/** The child's deltas so far. Persisted on every one, exactly as a conversation's assistant text is. */
|
|
165
|
+
let streamed = "";
|
|
166
|
+
/**
|
|
167
|
+
* Tools the child has started, oldest first. Accumulated here rather than taken from the record,
|
|
168
|
+
* for the same reason `streamed` is: the recorder's copy is what a snapshot is built from, so
|
|
169
|
+
* mutating the store's object would move it under every reader.
|
|
170
|
+
*/
|
|
171
|
+
let toolCalls = [];
|
|
172
|
+
/**
|
|
173
|
+
* Pi's tool-call id → the row it became, so a `tool_execution_end` can find the call it belongs to.
|
|
174
|
+
*
|
|
175
|
+
* Held here rather than on the record, because `SubagentToolCall` deliberately carries no id: card
|
|
176
|
+
* rows are ordered, not addressable, and an id on a persisted row would be an identity nothing
|
|
177
|
+
* outside Pi can resolve. The mapping only has to survive the run, which is exactly its scope.
|
|
178
|
+
*/
|
|
179
|
+
const toolRowById = new Map();
|
|
180
|
+
/**
|
|
181
|
+
* What the child was doing last, or undefined while it has done nothing observable yet.
|
|
182
|
+
*
|
|
183
|
+
* The gap this exists for is a card that FREEZES: a run that spends its whole life inside one long
|
|
184
|
+
* tool call emits no `text_delta`, so a browser watching only the text sees nothing change and
|
|
185
|
+
* cannot tell a working child from a dead one. `at` is what lets the card say how stale the news is.
|
|
186
|
+
*/
|
|
187
|
+
let activity;
|
|
188
|
+
/**
|
|
189
|
+
* The phase the child last reached. Starts at `dispatch`, which covers every refusal before the spawn
|
|
190
|
+
* and is what a card should say about a run whose child never existed. See `SubagentFailure`.
|
|
191
|
+
*/
|
|
192
|
+
let stage = "dispatch";
|
|
193
|
+
// Live events carry only the tail of the accumulated output (see LIVE_OUTPUT_TAIL_CAP) so a
|
|
194
|
+
// per-delta snapshot stays small no matter how much the child has said.
|
|
195
|
+
const liveTailCap = this.hooks.liveTailCap ?? LIVE_OUTPUT_TAIL_CAP;
|
|
196
|
+
let checkpoint;
|
|
197
|
+
const publish = () => {
|
|
198
|
+
record.output = streamed.length > liveTailCap ? streamed.slice(streamed.length - liveTailCap) : streamed;
|
|
199
|
+
record.toolCalls = toolCalls;
|
|
200
|
+
record.activity = activity;
|
|
201
|
+
// Update the snapshot before streaming so reconnects read at least the progress already sent.
|
|
202
|
+
this.hooks.store.updateSubagentProgress(id, {
|
|
203
|
+
output: streamed.slice(-PER_TASK_OUTPUT_CAP), toolCalls, activity,
|
|
204
|
+
outputTruncated: streamed.length > PER_TASK_OUTPUT_CAP || undefined,
|
|
205
|
+
});
|
|
206
|
+
this.hooks.broadcast({ type: "subagent.event", run: structuredClone(record) });
|
|
207
|
+
if (!checkpoint)
|
|
208
|
+
checkpoint = setTimeout(() => {
|
|
209
|
+
checkpoint = undefined;
|
|
210
|
+
void this.hooks.store.checkpointSubagentProgress(id).catch(error => {
|
|
211
|
+
console.warn(`Super Pi: could not checkpoint sub-agent run ${id}:`, error);
|
|
212
|
+
});
|
|
213
|
+
}, 500).unref();
|
|
214
|
+
};
|
|
215
|
+
const settle = async (changes) => {
|
|
216
|
+
clearTimeout(checkpoint);
|
|
217
|
+
Object.assign(record, changes);
|
|
218
|
+
// PERSIST FIRST, ANNOUNCE SECOND. The order is load-bearing and was previously reversed.
|
|
219
|
+
//
|
|
220
|
+
// Broadcasting the terminal frame before the write lands tells the browser a run has finished
|
|
221
|
+
// while `state.json` still says `running`. Measured, the gap is 0.36-8.19ms - long enough that a
|
|
222
|
+
// refresh in that window reloads a card that spins forever, and the startup reconciliation that
|
|
223
|
+
// would fix it only runs on a SERVER restart, not a page refresh. The same principle as
|
|
224
|
+
// `Store.open` persisting its reconciliation immediately: nothing is announced before it is on
|
|
225
|
+
// disk. The write is serialized, so the broadcast is a few ms later than it used to be. That is
|
|
226
|
+
// the correct trade: never announce state that a reload would contradict.
|
|
227
|
+
const settled = await this.hooks.store.setSubagentRun(id, changes);
|
|
228
|
+
this.hooks.broadcast({ type: "subagent.event", run: structuredClone(record) });
|
|
229
|
+
// The ONE moment a full `state` broadcast is needed: the final record is on disk NOW, so a browser
|
|
230
|
+
// that reloads after this point rebuilds the finished card from `state`. See the `onSettled` doc.
|
|
231
|
+
this.hooks.onSettled?.();
|
|
232
|
+
return settled;
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Wall-clock from the `running` row to now, in whole milliseconds.
|
|
236
|
+
*
|
|
237
|
+
* This project's clock, measured across the row the recorder itself wrote, so it covers the spawn
|
|
238
|
+
* and the drain rather than only the model's thinking. The point of recording it at all is a
|
|
239
|
+
* measurement: a real sub-agent reading three files finished in 2.5s, which is far too short for
|
|
240
|
+
* anyone to have watched it, so without a duration left behind the transcript cannot even say the
|
|
241
|
+
* run happened.
|
|
242
|
+
*/
|
|
243
|
+
const elapsedMs = () => Math.max(0, Date.now() - new Date(started.startedAt).getTime());
|
|
244
|
+
try {
|
|
245
|
+
if (controller.signal.aborted)
|
|
246
|
+
throw new SubagentCancelled(id);
|
|
247
|
+
const result = await this.supervisor.run({
|
|
248
|
+
id,
|
|
249
|
+
conversationId,
|
|
250
|
+
agentId,
|
|
251
|
+
task,
|
|
252
|
+
cwd,
|
|
253
|
+
model,
|
|
254
|
+
denyTools,
|
|
255
|
+
signal: controller.signal,
|
|
256
|
+
// Asked for BEFORE the run, because the host's key needs the run id and only this file decided it.
|
|
257
|
+
host: this.hooks.host?.(conversationId, id),
|
|
258
|
+
onTextDelta: (delta) => {
|
|
259
|
+
streamed += delta;
|
|
260
|
+
publish();
|
|
261
|
+
},
|
|
262
|
+
// Appended, never replaced. `detail` is the DERIVED label `describeToolCall` made from the
|
|
263
|
+
// child's arguments - one bounded line naming the file, pattern or command - and never the
|
|
264
|
+
// arguments themselves. `status` stays absent until an `onToolEnd` fills it in, because Pi
|
|
265
|
+
// announces a tool with `tool_execution_start` and a row with no outcome is exactly how a card
|
|
266
|
+
// says "this one is still running".
|
|
267
|
+
onToolCall: (call) => {
|
|
268
|
+
toolRowById.set(call.id, toolCalls.length);
|
|
269
|
+
toolCalls = [...toolCalls, { label: call.name, ...(call.detail === undefined ? {} : { detail: call.detail }) }];
|
|
270
|
+
activity = { kind: "tool", label: call.name, at: new Date().toISOString() };
|
|
271
|
+
publish();
|
|
272
|
+
},
|
|
273
|
+
onToolUpdate: (id, output, final) => {
|
|
274
|
+
const row = toolRowById.get(id);
|
|
275
|
+
if (row === undefined)
|
|
276
|
+
return;
|
|
277
|
+
const current = toolCalls[row];
|
|
278
|
+
if (final && current.output) {
|
|
279
|
+
if (current.output.includes(output))
|
|
280
|
+
return;
|
|
281
|
+
if (!output.includes(current.output))
|
|
282
|
+
output = current.output + "\n\n" + output;
|
|
283
|
+
}
|
|
284
|
+
const tail = output.slice(-8_000);
|
|
285
|
+
const truncated = current.outputTruncated || output.length > tail.length || undefined;
|
|
286
|
+
if (tail === current.output && truncated === current.outputTruncated)
|
|
287
|
+
return;
|
|
288
|
+
toolCalls = toolCalls.map((item, index) => {
|
|
289
|
+
if (index === row)
|
|
290
|
+
return { ...item, output: tail, outputTruncated: truncated };
|
|
291
|
+
return item;
|
|
292
|
+
});
|
|
293
|
+
const loggedRows = toolCalls.flatMap((item, index) => item.output ? [index] : []);
|
|
294
|
+
for (const index of loggedRows.slice(0, -4)) {
|
|
295
|
+
toolCalls[index] = { ...toolCalls[index], output: undefined, outputTruncated: true };
|
|
296
|
+
}
|
|
297
|
+
publish();
|
|
298
|
+
},
|
|
299
|
+
// The end of a call, matched back to its row by Pi's own id. A `tool_execution_end` with no
|
|
300
|
+
// matching start is DROPPED rather than appended: it would otherwise become a row for a call the
|
|
301
|
+
// card never announced, which reads as a tool the child used that it did not.
|
|
302
|
+
onToolEnd: (call) => {
|
|
303
|
+
const row = toolRowById.get(call.id);
|
|
304
|
+
if (row === undefined)
|
|
305
|
+
return;
|
|
306
|
+
activity = { kind: "thinking", at: new Date().toISOString() };
|
|
307
|
+
toolCalls = toolCalls.map((item, index) => index === row
|
|
308
|
+
? { ...item, status: call.status, ...(call.durationMs === undefined ? {} : { durationMs: call.durationMs }) }
|
|
309
|
+
: item);
|
|
310
|
+
publish();
|
|
311
|
+
},
|
|
312
|
+
// Thinking, not tool: the child is alive and composing, and saying so is the only thing that
|
|
313
|
+
// distinguishes a reasoning model mid-turn from a hung one.
|
|
314
|
+
onThinking: () => {
|
|
315
|
+
activity = { kind: "thinking", at: new Date().toISOString() };
|
|
316
|
+
publish();
|
|
317
|
+
},
|
|
318
|
+
onStage: (next) => { stage = next; },
|
|
319
|
+
});
|
|
320
|
+
// `run()` returns the authoritative last assistant message, which is what the record means by
|
|
321
|
+
// `output`; the streamed copy is only a fallback for a child that never sent a `done`.
|
|
322
|
+
const output = result.finalText || streamed;
|
|
323
|
+
const truncated = output.length > PER_TASK_OUTPUT_CAP;
|
|
324
|
+
const settled = await settle({
|
|
325
|
+
status: "completed",
|
|
326
|
+
output: truncated ? output.slice(0, PER_TASK_OUTPUT_CAP) : output,
|
|
327
|
+
// `undefined` rather than `false`: the key is absent on an untruncated run, so it serializes
|
|
328
|
+
// identically to a record written before this field existed.
|
|
329
|
+
outputTruncated: truncated || undefined,
|
|
330
|
+
toolCalls,
|
|
331
|
+
usage: result.usage,
|
|
332
|
+
sessionId: result.sessionId,
|
|
333
|
+
durationMs: elapsedMs(),
|
|
334
|
+
// CLEARED rather than left describing the child's last moment: a finished card that still says
|
|
335
|
+
// 正在调用 read is making a claim about a process that no longer exists.
|
|
336
|
+
activity: undefined,
|
|
337
|
+
failure: undefined,
|
|
338
|
+
finishedAt: new Date().toISOString(),
|
|
339
|
+
});
|
|
340
|
+
// THE CHILD'S MONEY IS THE CALLER'S MONEY, AND IT IS ADDED ONCE.
|
|
341
|
+
//
|
|
342
|
+
// The run has just been settled with its FINAL accumulated usage, and that is the number the
|
|
343
|
+
// dispatching conversation's ledger is given - not one message's worth, and not one addition
|
|
344
|
+
// per message the child produced. A project budget reads the conversation's ledger, so a child
|
|
345
|
+
// that is not in there is a child nobody is charged for.
|
|
346
|
+
//
|
|
347
|
+
// A FAILURE HERE IS ANNOUNCED AND SWALLOWED: the run record is already on disk, so the work is
|
|
348
|
+
// real and recorded, and losing the ledger entry must not turn a finished run into a failed one.
|
|
349
|
+
// The next start folds it in - the startup sweep accounts for every run whose usage has not been
|
|
350
|
+
// accounted for - so the money is late rather than lost.
|
|
351
|
+
try {
|
|
352
|
+
await this.hooks.store.accountSubagentUsage(id);
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
console.warn(`Super Pi: the usage of sub-agent run ${id} could not be added to the usage of conversation ${conversationId}: ${error instanceof Error ? error.message : "unknown error"}`);
|
|
356
|
+
}
|
|
357
|
+
return settled;
|
|
358
|
+
}
|
|
359
|
+
catch (error) {
|
|
360
|
+
const stopped = error instanceof SubagentCancelled;
|
|
361
|
+
return await settle({
|
|
362
|
+
status: stopped ? "stopped" : "failed",
|
|
363
|
+
output: undefined,
|
|
364
|
+
partialOutput: streamed.slice(-PER_TASK_OUTPUT_CAP) || undefined,
|
|
365
|
+
outputTruncated: streamed.length > PER_TASK_OUTPUT_CAP || undefined,
|
|
366
|
+
toolCalls,
|
|
367
|
+
// WHERE, not only WHAT. `error` says what went wrong; this says which phase the child was in when
|
|
368
|
+
// it did, so a card can answer 「卡在哪」 - and a run stopped by a person is not given one, because
|
|
369
|
+
// nobody failed: 「已停止」 is a complete and true account.
|
|
370
|
+
...(stopped ? {} : { failure: { stage } }),
|
|
371
|
+
durationMs: elapsedMs(),
|
|
372
|
+
activity: undefined,
|
|
373
|
+
error: stopped ? `Run ${id} was stopped before it settled.` : error instanceof Error ? error.message : "unknown error",
|
|
374
|
+
finishedAt: new Date().toISOString(),
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
finally {
|
|
378
|
+
this.hooks.closeHost?.(conversationId, id);
|
|
379
|
+
input.signal?.removeEventListener("abort", onCallerAbort);
|
|
380
|
+
this.controllers.delete(id);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|