somora 2026.930.1
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/LICENSE +21 -0
- package/README.md +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// agent_ask call registry — since 2026-09-13 a VIEW over the work
|
|
2
|
+
// ledger (work-ledger.ts). The functions and shapes here are what
|
|
3
|
+
// agent_ask_result, GET /a2a/ask-result and the ask-attention test
|
|
4
|
+
// read; they stay byte-compatible while the state lives in one book
|
|
5
|
+
// with the sub-agent tasks and the voice consults.
|
|
6
|
+
//
|
|
7
|
+
// Lifecycle (unchanged): registerAskCall (queued) → markAskCallRunning
|
|
8
|
+
// → completeAskCall / failAskCall. An asker that hung up before the
|
|
9
|
+
// answer (markAskCallPending) is woken by the ledger's single wake.
|
|
10
|
+
// After a restart the registry is empty and GET /a2a/ask-result falls
|
|
11
|
+
// back to the target session's history (user_message.agent_ask_call_id).
|
|
12
|
+
|
|
13
|
+
import type { ChatTurnResult } from './run-turn-types.ts';
|
|
14
|
+
import {
|
|
15
|
+
configureWorkWake,
|
|
16
|
+
failWork,
|
|
17
|
+
finishWork,
|
|
18
|
+
getWork,
|
|
19
|
+
markFetched,
|
|
20
|
+
markRunning,
|
|
21
|
+
openWork,
|
|
22
|
+
setWaiting,
|
|
23
|
+
waitForWork,
|
|
24
|
+
type WorkItem,
|
|
25
|
+
} from './work-ledger.ts';
|
|
26
|
+
|
|
27
|
+
export type AskCallState = 'queued' | 'running' | 'done' | 'failed';
|
|
28
|
+
|
|
29
|
+
export interface AskCallEntry {
|
|
30
|
+
call_id: string;
|
|
31
|
+
state: AskCallState;
|
|
32
|
+
from_agent: string;
|
|
33
|
+
from_session?: string;
|
|
34
|
+
target_agent: string;
|
|
35
|
+
target_session: string;
|
|
36
|
+
started_at: number;
|
|
37
|
+
finished_at?: number;
|
|
38
|
+
result?: ChatTurnResult;
|
|
39
|
+
error?: string;
|
|
40
|
+
/** The asker stopped waiting: `agent_ask` returned `pending`. Only
|
|
41
|
+
* such a call needs waking — a caller still holding the line gets
|
|
42
|
+
* the answer as its tool result. */
|
|
43
|
+
went_pending?: boolean;
|
|
44
|
+
/** The asker read the result (agent_ask_result). No wake needed. */
|
|
45
|
+
result_fetched?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function toEntry(it: WorkItem): AskCallEntry {
|
|
49
|
+
const from = it.origin.kind === 'agent' ? it.origin.from : { agent: '?' as string, session: undefined as string | undefined };
|
|
50
|
+
const state: AskCallState =
|
|
51
|
+
it.state === 'queued' ? 'queued' : it.state === 'running' ? 'running' : it.state === 'done' ? 'done' : 'failed';
|
|
52
|
+
return {
|
|
53
|
+
call_id: it.id,
|
|
54
|
+
state,
|
|
55
|
+
from_agent: from.agent,
|
|
56
|
+
...(from.session ? { from_session: from.session } : {}),
|
|
57
|
+
target_agent: it.target.agent,
|
|
58
|
+
target_session: it.target.session,
|
|
59
|
+
started_at: it.enqueuedAt,
|
|
60
|
+
...(it.finishedAt !== undefined ? { finished_at: it.finishedAt } : {}),
|
|
61
|
+
...(it.result ? { result: it.result } : {}),
|
|
62
|
+
...(it.error ? { error: it.error } : {}),
|
|
63
|
+
...(it.waiting ? {} : { went_pending: true }),
|
|
64
|
+
...(it.resultFetched ? { result_fetched: true } : {}),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface AskAttentionDeps {
|
|
69
|
+
dispatchWakeTurn(args: { agent: string; session: string; text: string; callId: string }): Promise<void>;
|
|
70
|
+
/** Grace period: an asker that polls right away needs no wake. */
|
|
71
|
+
graceMs: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Compatibility wiring: routes every ledger wake through an ask-shaped
|
|
75
|
+
* dispatcher. The server wires configureWorkWake directly; this is
|
|
76
|
+
* for callers (tests) that only know the ask side. */
|
|
77
|
+
export function configureAskAttention(deps: AskAttentionDeps): void {
|
|
78
|
+
configureWorkWake({
|
|
79
|
+
graceMs: deps.graceMs,
|
|
80
|
+
// The legacy shape carried the frame inside the text.
|
|
81
|
+
dispatchWakeTurn: (w) => deps.dispatchWakeTurn({ agent: w.agent, session: w.session, text: w.prefix ? `${w.text}\n${w.prefix}` : w.text, callId: w.ref }),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function markAskCallPending(call_id: string): void {
|
|
86
|
+
setWaiting(call_id, false);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function registerAskCall(
|
|
90
|
+
entry: Omit<AskCallEntry, 'state' | 'started_at' | 'finished_at' | 'result' | 'error'> & {
|
|
91
|
+
/** The question, for the queue display. Optional for older callers. */
|
|
92
|
+
text?: string;
|
|
93
|
+
/** `agent_ask wait:false`: the asker never waits inline. */
|
|
94
|
+
detached?: boolean;
|
|
95
|
+
},
|
|
96
|
+
): AskCallEntry {
|
|
97
|
+
const it = openWork({
|
|
98
|
+
id: entry.call_id,
|
|
99
|
+
origin: {
|
|
100
|
+
kind: 'agent',
|
|
101
|
+
from: { agent: entry.from_agent, ...(entry.from_session ? { session: entry.from_session } : {}) },
|
|
102
|
+
callId: entry.call_id,
|
|
103
|
+
},
|
|
104
|
+
target: { agent: entry.target_agent, session: entry.target_session },
|
|
105
|
+
requester: { agent: entry.from_agent, session: entry.from_session ?? '?' },
|
|
106
|
+
text: entry.text ?? '',
|
|
107
|
+
waiting: !entry.detached,
|
|
108
|
+
});
|
|
109
|
+
return toEntry(it);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function markAskCallRunning(call_id: string): void {
|
|
113
|
+
markRunning(call_id);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function completeAskCall(call_id: string, result: ChatTurnResult): void {
|
|
117
|
+
finishWork(call_id, result);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function failAskCall(call_id: string, error: string): void {
|
|
121
|
+
failWork(call_id, error);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function getAskCall(call_id: string): AskCallEntry | undefined {
|
|
125
|
+
const it = getWork(call_id);
|
|
126
|
+
if (!it || it.origin.kind !== 'agent') return undefined;
|
|
127
|
+
// Reading a finished call IS fetching it: no wake afterwards, or the
|
|
128
|
+
// asker is told twice about an answer it already has.
|
|
129
|
+
if (it.finishedAt !== undefined) markFetched(call_id);
|
|
130
|
+
return toEntry(it);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Poll until the call reaches a terminal state or `timeoutMs` passes. */
|
|
134
|
+
export async function waitForAskCall(call_id: string, timeoutMs: number): Promise<AskCallEntry | null> {
|
|
135
|
+
const it = await waitForWork(call_id, timeoutMs);
|
|
136
|
+
return it && it.origin.kind === 'agent' ? toEntry(it) : null;
|
|
137
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Wait-graph for blocking A2A calls — circular-wait (deadlock)
|
|
2
|
+
// detection. Born from the 2026-06-01 feedback (Luca's Gideon↔Donna
|
|
3
|
+
// hang); spawn-family waits added 2026-06-10.
|
|
4
|
+
//
|
|
5
|
+
// ── The failure mode ─────────────────────────────────────────────────
|
|
6
|
+
// Agent A's turn blocks waiting on agent B (agent_ask, or a wait:true
|
|
7
|
+
// sub-spawn, or subagent_result wait_until_done). While blocked, A's
|
|
8
|
+
// SESSION LOCK stays held. If B — directly or via any chain of further
|
|
9
|
+
// waits — ends up waiting on A's session, every member of the chain
|
|
10
|
+
// queues on a lock that can only be released by another member: a
|
|
11
|
+
// classic circular wait. Symptom pre-fix: all involved sessions show
|
|
12
|
+
// "streaming" forever, no error, until the per-call timeouts fire
|
|
13
|
+
// minutes later. The canonical trigger is an agent "answering" an
|
|
14
|
+
// agent_ask by opening a NEW agent_ask back at its caller instead of
|
|
15
|
+
// just replying in its turn output. Chains over 3+ agents happen in
|
|
16
|
+
// practice, so detection is TRANSITIVE (DFS over the edge set), not
|
|
17
|
+
// just pairwise.
|
|
18
|
+
//
|
|
19
|
+
// ── The model ────────────────────────────────────────────────────────
|
|
20
|
+
// Nodes are (agent, session) — the session whose turn holds the lock —
|
|
21
|
+
// NOT bare agent names: agent-level edges would false-positive on
|
|
22
|
+
// legitimate cross-session traffic (proven by the cross-session case in
|
|
23
|
+
// the smoke tests). An edge from→to means "from's turn is blocked until
|
|
24
|
+
// to's work completes". Edges are reference-counted because one turn
|
|
25
|
+
// can hold several parallel waits on the same target.
|
|
26
|
+
//
|
|
27
|
+
// Dispatch protocol for every blocking wait:
|
|
28
|
+
// 1. findWaitCycle(from, to) — would this edge close a cycle?
|
|
29
|
+
// If yes → REFUSE to wait (reject the call / return pending) with
|
|
30
|
+
// a teaching message; the refusal makes the situation self-healing
|
|
31
|
+
// because the refused agent simply finishes its turn, which is
|
|
32
|
+
// exactly what the waiting chain needs.
|
|
33
|
+
// 2. registerWait(from, to) BEFORE starting to block (incl. before
|
|
34
|
+
// queueing on the target's session lock — the caller is already
|
|
35
|
+
// committed to waiting while queued).
|
|
36
|
+
// 3. release() in a finally, whatever way the wait ends.
|
|
37
|
+
// Check + register run synchronously back-to-back (no await between),
|
|
38
|
+
// so two simultaneous calls can't both slip past the cycle check (Node
|
|
39
|
+
// is single-threaded).
|
|
40
|
+
//
|
|
41
|
+
// ── Coverage map — every blocking A2A wait and who registers it ──────
|
|
42
|
+
// The graph lives in MAIN-SERVER process memory. That works because
|
|
43
|
+
// every wait either happens in the main server itself or announces
|
|
44
|
+
// itself to a main-server HTTP route via waiter_agent/waiter_session
|
|
45
|
+
// params. (waiter_* is deliberately separate from from_agent: from_agent
|
|
46
|
+
// changes message semantics in the target session, waiter_* is pure
|
|
47
|
+
// wait-graph bookkeeping.)
|
|
48
|
+
//
|
|
49
|
+
// wait transport registered by
|
|
50
|
+
// ──────────────────────────── ─────────────────── ─────────────────
|
|
51
|
+
// agent_ask (any engine — loopback HTTP /chat/send-sync
|
|
52
|
+
// the tool always HTTPs) + waiter_* route
|
|
53
|
+
// spawn_subagent wait:true, direct runChatTurn spawn.ts runOneSpawn
|
|
54
|
+
// in-process (main server) (direct import)
|
|
55
|
+
// spawn_subagent wait:true, loopback HTTP /chat/send-sync
|
|
56
|
+
// MCP-child fallback + waiter_* route
|
|
57
|
+
// subagent_result direct status.ts handler
|
|
58
|
+
// wait_until_done, waitForTask- (direct import)
|
|
59
|
+
// in-process (main server) Completion
|
|
60
|
+
// subagent_result loopback HTTP /spawn-result
|
|
61
|
+
// wait_until_done, + waiter_* route
|
|
62
|
+
// MCP-child fallback
|
|
63
|
+
//
|
|
64
|
+
// NOT registered (correctly — they don't block the caller's turn):
|
|
65
|
+
// spawn_subagent wait:false (fire-and-forget), subagent_status,
|
|
66
|
+
// subagent_result without wait_until_done.
|
|
67
|
+
//
|
|
68
|
+
// Spawn waits skip step 1 (cycle check): the sub-session is created
|
|
69
|
+
// fresh immediately before the wait, so no edges can touch it yet and
|
|
70
|
+
// the registration cannot close a cycle. subagent_result DOES need the
|
|
71
|
+
// check — its target task has been running since an earlier turn and
|
|
72
|
+
// may already be waiting on the caller; on a cycle it returns
|
|
73
|
+
// state:'pending' with a hint instead of erroring, because "still
|
|
74
|
+
// running" is not an error there.
|
|
75
|
+
//
|
|
76
|
+
// MCP children import this module too (via spawn.ts/status.ts) and get
|
|
77
|
+
// their own empty graph instance — harmless, because the in-process
|
|
78
|
+
// branches that touch the graph only execute in the main server
|
|
79
|
+
// (injectedDeps set / getTask hit), and child waits go through the HTTP
|
|
80
|
+
// routes instead. Same cross-process pattern as the sentinel store.
|
|
81
|
+
//
|
|
82
|
+
// Graceful degradation: a caller that can't name its session (ctx.session
|
|
83
|
+
// missing — shouldn't happen) just doesn't participate; its wait is
|
|
84
|
+
// invisible but nothing breaks.
|
|
85
|
+
//
|
|
86
|
+
// Design decisions (discussed 2026-06-10): reject/refuse rather than
|
|
87
|
+
// auto-interpret a reverse-ask as the pending answer (no magic), no
|
|
88
|
+
// extra watchdog timeout layer (agent_ask/subagent_result already have
|
|
89
|
+
// their own timeouts), transitive detection from day one. Full design
|
|
90
|
+
// notes: private/A2A-design.md → "Circular-wait detection".
|
|
91
|
+
|
|
92
|
+
export interface WaitNode {
|
|
93
|
+
agent: string;
|
|
94
|
+
session: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const keyOf = (n: WaitNode) => `${n.agent}\u0000${n.session}`;
|
|
98
|
+
const labelOf = (k: string) => k.replace('\u0000', '/');
|
|
99
|
+
|
|
100
|
+
/** from-key → (to-key → count of in-flight waits). Counts because one
|
|
101
|
+
* turn can have several parallel asks to the same target. */
|
|
102
|
+
const graph = new Map<string, Map<string, number>>();
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Record "`from`'s turn is now blocked waiting on `to`". Returns an
|
|
106
|
+
* idempotent release callback — call it when the wait ends (reply,
|
|
107
|
+
* error, or caller gave up).
|
|
108
|
+
*/
|
|
109
|
+
export function registerWait(from: WaitNode, to: WaitNode): () => void {
|
|
110
|
+
const f = keyOf(from);
|
|
111
|
+
const t = keyOf(to);
|
|
112
|
+
let outs = graph.get(f);
|
|
113
|
+
if (!outs) {
|
|
114
|
+
outs = new Map();
|
|
115
|
+
graph.set(f, outs);
|
|
116
|
+
}
|
|
117
|
+
outs.set(t, (outs.get(t) ?? 0) + 1);
|
|
118
|
+
let released = false;
|
|
119
|
+
return () => {
|
|
120
|
+
if (released) return;
|
|
121
|
+
released = true;
|
|
122
|
+
const m = graph.get(f);
|
|
123
|
+
if (!m) return;
|
|
124
|
+
const n = (m.get(t) ?? 0) - 1;
|
|
125
|
+
if (n <= 0) m.delete(t);
|
|
126
|
+
else m.set(t, n);
|
|
127
|
+
if (m.size === 0) graph.delete(f);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Would adding the edge from→to close a wait cycle? True iff a wait
|
|
133
|
+
* path to→…→from already exists. Returns the full cycle as readable
|
|
134
|
+
* "agent/session" labels starting and ending at `from`
|
|
135
|
+
* (e.g. ["donna/main", "gideon/main", "donna/main"]), or null.
|
|
136
|
+
*/
|
|
137
|
+
export function findWaitCycle(from: WaitNode, to: WaitNode): string[] | null {
|
|
138
|
+
const target = keyOf(from);
|
|
139
|
+
const start = keyOf(to);
|
|
140
|
+
const seen = new Set<string>();
|
|
141
|
+
const path: string[] = [];
|
|
142
|
+
const dfs = (node: string): boolean => {
|
|
143
|
+
if (node === target) {
|
|
144
|
+
path.push(node);
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
if (seen.has(node)) return false;
|
|
148
|
+
seen.add(node);
|
|
149
|
+
for (const next of graph.get(node)?.keys() ?? []) {
|
|
150
|
+
if (dfs(next)) {
|
|
151
|
+
path.push(node);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
if (!dfs(start)) return null;
|
|
158
|
+
path.push(keyOf(from)); // path is reversed: [from(target), …, to(start)] → append from-as-origin
|
|
159
|
+
return path.reverse().map(labelOf);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Diagnostic snapshot for debug endpoints / tests. */
|
|
163
|
+
export function waitGraphSnapshot(): Array<{ from: string; to: string; count: number }> {
|
|
164
|
+
const out: Array<{ from: string; to: string; count: number }> = [];
|
|
165
|
+
for (const [f, m] of graph) {
|
|
166
|
+
for (const [t, count] of m) {
|
|
167
|
+
out.push({ from: labelOf(f), to: labelOf(t), count });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Sub-agent task registry — since 2026-09-13 a VIEW over the work
|
|
2
|
+
// ledger (work-ledger.ts). spawn_subagent with wait:false, the
|
|
3
|
+
// /spawn-* routes, subagent_status/result/list/cancel and the
|
|
4
|
+
// async-tasks test read these functions and shapes; they stay
|
|
5
|
+
// byte-compatible while the state lives in one book with the
|
|
6
|
+
// agent_ask calls and the voice consults.
|
|
7
|
+
//
|
|
8
|
+
// Sentinel fires register here too (parent_agent 'sentinel'); the
|
|
9
|
+
// ledger keeps them as their own kind and never wakes anyone for them.
|
|
10
|
+
//
|
|
11
|
+
// Lifecycle: tasks are kept in memory for the server's lifetime. On
|
|
12
|
+
// restart they're lost — sub-sessions persist in JSONL, the entries
|
|
13
|
+
// do not.
|
|
14
|
+
|
|
15
|
+
import type { ChatTurnResult } from './run-turn-types.ts';
|
|
16
|
+
import {
|
|
17
|
+
cancelWork,
|
|
18
|
+
configureWorkWake,
|
|
19
|
+
failWork,
|
|
20
|
+
finishWork,
|
|
21
|
+
getWork,
|
|
22
|
+
listWork,
|
|
23
|
+
markFetched,
|
|
24
|
+
openWork,
|
|
25
|
+
waitForWork,
|
|
26
|
+
type WorkItem,
|
|
27
|
+
} from './work-ledger.ts';
|
|
28
|
+
|
|
29
|
+
export type AsyncTaskState = 'running' | 'done' | 'failed' | 'cancelled';
|
|
30
|
+
|
|
31
|
+
export interface AsyncTaskEntry {
|
|
32
|
+
task_id: string;
|
|
33
|
+
state: AsyncTaskState;
|
|
34
|
+
parent_agent: string;
|
|
35
|
+
parent_session: string;
|
|
36
|
+
target_agent: string;
|
|
37
|
+
target_session: string;
|
|
38
|
+
started_at: number;
|
|
39
|
+
finished_at?: number;
|
|
40
|
+
result?: ChatTurnResult;
|
|
41
|
+
error?: string;
|
|
42
|
+
/** Attention-wake opt-out recorded at spawn time (default: wake). */
|
|
43
|
+
attention?: boolean;
|
|
44
|
+
/** Set when subagent_result delivered the terminal state to the
|
|
45
|
+
* parent — suppresses the attention wake (nothing left to report). */
|
|
46
|
+
result_fetched?: boolean;
|
|
47
|
+
/** Nesting depth of the PARENT (0 = a top-level agent spawned this). */
|
|
48
|
+
parent_depth?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const TASK_KINDS: Array<WorkItem['origin']['kind']> = ['subagent', 'sentinel'];
|
|
52
|
+
|
|
53
|
+
function toEntry(it: WorkItem): AsyncTaskEntry {
|
|
54
|
+
const r = it.requester;
|
|
55
|
+
const parent = r && 'agent' in r ? r : { agent: '?', session: '?' };
|
|
56
|
+
const state: AsyncTaskState =
|
|
57
|
+
it.state === 'queued' || it.state === 'running' ? 'running' : it.state === 'done' ? 'done' : it.state === 'failed' ? 'failed' : 'cancelled';
|
|
58
|
+
return {
|
|
59
|
+
task_id: it.id,
|
|
60
|
+
state,
|
|
61
|
+
parent_agent: parent.agent,
|
|
62
|
+
parent_session: parent.session,
|
|
63
|
+
target_agent: it.target.agent,
|
|
64
|
+
target_session: it.target.session,
|
|
65
|
+
started_at: it.enqueuedAt,
|
|
66
|
+
...(it.finishedAt !== undefined ? { finished_at: it.finishedAt } : {}),
|
|
67
|
+
...(it.result ? { result: it.result } : {}),
|
|
68
|
+
...(it.error ? { error: it.error } : {}),
|
|
69
|
+
...(it.wake === 'never' && it.origin.kind === 'subagent' ? { attention: false } : {}),
|
|
70
|
+
...(it.resultFetched ? { result_fetched: true } : {}),
|
|
71
|
+
...(it.parentDepth !== undefined ? { parent_depth: it.parentDepth } : {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function newTaskId(): string {
|
|
76
|
+
// Short, URL-safe, alphabet-only — easier to read in logs than a UUID.
|
|
77
|
+
const ts = Date.now().toString(36);
|
|
78
|
+
const rnd = Math.random().toString(36).slice(2, 8);
|
|
79
|
+
return `task_${ts}_${rnd}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function registerTask(
|
|
83
|
+
entry: Omit<AsyncTaskEntry, 'state'> & {
|
|
84
|
+
/** The brief, for the queue display. Optional for older callers. */
|
|
85
|
+
text?: string;
|
|
86
|
+
},
|
|
87
|
+
): AsyncTaskEntry {
|
|
88
|
+
const sentinel = entry.parent_agent === 'sentinel';
|
|
89
|
+
const depth = (entry.parent_depth ?? 0) + 1;
|
|
90
|
+
const it = openWork({
|
|
91
|
+
id: entry.task_id,
|
|
92
|
+
origin: sentinel
|
|
93
|
+
? { kind: 'sentinel', triggerId: entry.parent_session, taskId: entry.task_id }
|
|
94
|
+
: {
|
|
95
|
+
kind: 'subagent',
|
|
96
|
+
taskId: entry.task_id,
|
|
97
|
+
depth,
|
|
98
|
+
...(entry.parent_session !== '?' ? { parent: { agent: entry.parent_agent, session: entry.parent_session } } : {}),
|
|
99
|
+
},
|
|
100
|
+
target: { agent: entry.target_agent, session: entry.target_session },
|
|
101
|
+
requester: { agent: entry.parent_agent, session: entry.parent_session },
|
|
102
|
+
text: entry.text ?? '',
|
|
103
|
+
// The parent does not wait inline on an async sub; when it blocks in
|
|
104
|
+
// subagent_result it fetches, which suppresses the wake.
|
|
105
|
+
waiting: false,
|
|
106
|
+
wake: sentinel || entry.attention === false ? 'never' : 'auto',
|
|
107
|
+
...(entry.parent_depth !== undefined ? { parentDepth: entry.parent_depth } : {}),
|
|
108
|
+
// Opened as queued; toEntry shows it as 'running' to the tools, which
|
|
109
|
+
// never had a queued state. The ledger itself needs the truth: a sub
|
|
110
|
+
// still waiting for its session lock can be taken back, a running
|
|
111
|
+
// one has to be stopped.
|
|
112
|
+
});
|
|
113
|
+
return toEntry(it);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function completeTask(task_id: string, result: ChatTurnResult): void {
|
|
117
|
+
finishWork(task_id, result);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function failTask(task_id: string, error: string): void {
|
|
121
|
+
failWork(task_id, error);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function getTask(task_id: string): AsyncTaskEntry | undefined {
|
|
125
|
+
const it = getWork(task_id);
|
|
126
|
+
return it && TASK_KINDS.includes(it.origin.kind) ? toEntry(it) : undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Tasks a parent started. Sentinel fires show only for the synthetic
|
|
130
|
+
* parent 'sentinel' itself (GET /spawn-list?parent_agent=sentinel). */
|
|
131
|
+
export function listTasksForAgent(parent_agent: string): AsyncTaskEntry[] {
|
|
132
|
+
const kinds: Array<WorkItem['origin']['kind']> = parent_agent === 'sentinel' ? ['sentinel'] : ['subagent'];
|
|
133
|
+
return listWork({ requester: { agent: parent_agent }, kinds }).map(toEntry);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Children of a given sub: tasks spawned FROM the sub's own session. */
|
|
137
|
+
export function listChildTasks(agent: string, session: string): AsyncTaskEntry[] {
|
|
138
|
+
return listWork({ requester: { agent, session }, kinds: ['subagent'] }).map(toEntry);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Mark that the parent received the terminal state via
|
|
142
|
+
* subagent_result — suppresses the pending attention wake. */
|
|
143
|
+
export function markResultFetched(task_id: string): void {
|
|
144
|
+
markFetched(task_id);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ── cancel ────────────────────────────────────────────────────────────
|
|
148
|
+
|
|
149
|
+
export interface CancelOutcome {
|
|
150
|
+
/** Tasks moved running → cancelled (includes cascaded children). */
|
|
151
|
+
cancelled: string[];
|
|
152
|
+
/** Tasks already terminal when reached (skipped, state noted). */
|
|
153
|
+
skipped: Array<{ task_id: string; state: AsyncTaskState }>;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Cancel a running sub-task AND its descendants (a sub that spawned
|
|
158
|
+
* its own subs). The ledger marks first, then aborts the target
|
|
159
|
+
* session's turn — the same signal the Stop button uses. Sync
|
|
160
|
+
* (wait:true) grand-children are not registry-visible and are NOT
|
|
161
|
+
* reached until Phase 3.
|
|
162
|
+
*/
|
|
163
|
+
export function cancelTaskCascade(task_id: string, reason: string, by: 'human' | 'agent' = 'agent'): CancelOutcome | null {
|
|
164
|
+
const out = cancelWork(task_id, reason, by);
|
|
165
|
+
if (!out) return null;
|
|
166
|
+
return {
|
|
167
|
+
cancelled: out.cancelled,
|
|
168
|
+
skipped: out.skipped.map((s) => ({
|
|
169
|
+
task_id: s.id,
|
|
170
|
+
state: s.state === 'done' ? 'done' : s.state === 'failed' ? 'failed' : 'cancelled',
|
|
171
|
+
})),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Resolve when the task leaves the 'running' state or `timeoutMs`
|
|
176
|
+
* passes (200 ms polling inside the server). */
|
|
177
|
+
export async function waitForTaskCompletion(task_id: string, timeoutMs: number): Promise<AsyncTaskEntry | null> {
|
|
178
|
+
const it = await waitForWork(task_id, timeoutMs);
|
|
179
|
+
return it && TASK_KINDS.includes(it.origin.kind) ? toEntry(it) : null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ── attention wake ────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
interface AttentionDeps {
|
|
185
|
+
dispatchWakeTurn: (args: { agent: string; session: string; text: string; taskId: string; depth: number }) => Promise<void>;
|
|
186
|
+
/** Grace period before the wake check. */
|
|
187
|
+
graceMs: number;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Compatibility wiring: routes every ledger wake through a task-shaped
|
|
191
|
+
* dispatcher. The server wires configureWorkWake directly; this is
|
|
192
|
+
* for callers (tests) that only know the task side. */
|
|
193
|
+
export function configureSubagentAttention(deps: AttentionDeps): void {
|
|
194
|
+
configureWorkWake({
|
|
195
|
+
graceMs: deps.graceMs,
|
|
196
|
+
// The legacy shape carried the frame inside the text.
|
|
197
|
+
dispatchWakeTurn: (w) => deps.dispatchWakeTurn({ agent: w.agent, session: w.session, text: w.prefix ? `${w.text}\n${w.prefix}` : w.text, taskId: w.ref, depth: w.depth }),
|
|
198
|
+
});
|
|
199
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// One builder per working directory at a time.
|
|
2
|
+
//
|
|
3
|
+
// Two builders editing the same working copy overwrite each other's
|
|
4
|
+
// files; nothing in git or the file tools would notice until the tests
|
|
5
|
+
// fail. So a builder's running turn claims its working directory here,
|
|
6
|
+
// and a hand-over (builder_dispatch) or a Go into a folder that is
|
|
7
|
+
// claimed is refused with the claimant named — the orderer waits for
|
|
8
|
+
// that report or picks another project. Nested folders count as the
|
|
9
|
+
// same folder: a claim on /repo covers /repo/packages/a and vice versa.
|
|
10
|
+
//
|
|
11
|
+
// In-memory, per server process: claims are released when the turn
|
|
12
|
+
// ends (start-turn's finally) and vanish with a restart, which is right —
|
|
13
|
+
// a restart ends every turn.
|
|
14
|
+
|
|
15
|
+
import { normalize } from 'node:path';
|
|
16
|
+
|
|
17
|
+
export interface WorkdirClaim {
|
|
18
|
+
agent: string;
|
|
19
|
+
session: string;
|
|
20
|
+
turnId: string;
|
|
21
|
+
workdir: string;
|
|
22
|
+
since: number;
|
|
23
|
+
/** Live progress of the claiming turn — the task panel shows it. */
|
|
24
|
+
toolCalls: number;
|
|
25
|
+
lastTool?: string;
|
|
26
|
+
lastToolAt?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const claims = new Map<string, WorkdirClaim>(); // by turnId
|
|
30
|
+
|
|
31
|
+
function norm(p: string): string {
|
|
32
|
+
return normalize(p).replace(/\/+$/, '') || '/';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function overlaps(a: string, b: string): boolean {
|
|
36
|
+
if (a === b) return true;
|
|
37
|
+
return a.startsWith(b + '/') || b.startsWith(a + '/');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The claim that covers `workdir` (same folder, a parent, or a child), or null. */
|
|
41
|
+
export function workdirClaimedBy(workdir: string, opts: { exceptTurnId?: string } = {}): WorkdirClaim | null {
|
|
42
|
+
const w = norm(workdir);
|
|
43
|
+
for (const c of claims.values()) {
|
|
44
|
+
if (opts.exceptTurnId && c.turnId === opts.exceptTurnId) continue;
|
|
45
|
+
if (overlaps(w, c.workdir)) return c;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Register a builder turn as working in `workdir`. Never refuses: the
|
|
51
|
+
* checks happen before the turn is started (dispatch, Go); a turn that
|
|
52
|
+
* reached this point runs. */
|
|
53
|
+
export function claimWorkdir(entry: Pick<WorkdirClaim, 'agent' | 'session' | 'turnId'> & { workdir: string }): WorkdirClaim {
|
|
54
|
+
const claim: WorkdirClaim = { ...entry, workdir: norm(entry.workdir), since: Date.now(), toolCalls: 0 };
|
|
55
|
+
claims.set(entry.turnId, claim);
|
|
56
|
+
return claim;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Count a tool call on the claiming turn (no-op for a chat turn). */
|
|
60
|
+
export function noteToolCall(turnId: string, tool: string): void {
|
|
61
|
+
const c = claims.get(turnId);
|
|
62
|
+
if (!c) return;
|
|
63
|
+
c.toolCalls += 1;
|
|
64
|
+
c.lastTool = tool;
|
|
65
|
+
c.lastToolAt = Date.now();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The running builder turn of a session, or null. */
|
|
69
|
+
export function claimOfSession(agent: string, session: string): WorkdirClaim | null {
|
|
70
|
+
for (const c of claims.values()) if (c.agent === agent && c.session === session) return c;
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function releaseWorkdir(turnId: string): void {
|
|
75
|
+
claims.delete(turnId);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function listWorkdirClaims(): WorkdirClaim[] {
|
|
79
|
+
return [...claims.values()];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The sentence a refused caller reads. */
|
|
83
|
+
export function describeClaim(c: WorkdirClaim): string {
|
|
84
|
+
const mins = Math.max(1, Math.round((Date.now() - c.since) / 60_000));
|
|
85
|
+
return `${c.agent} is working in ${c.workdir} (session ${c.session}, for ${mins} min) — wait for its report or use another project folder`;
|
|
86
|
+
}
|