mixdog 0.9.152 → 0.9.154
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/LICENSES/browser-import-NOTICE.txt +11 -0
- package/NOTICE.md +11 -0
- package/README.md +8 -10
- package/package.json +19 -3
- package/scripts/computer-task-cost-report.mjs +144 -0
- package/scripts/run-office-live-tests.mjs +32 -0
- package/src/agents/front-worker/AGENT.md +13 -0
- package/src/agents/front-worker/agent.json +6 -0
- package/src/agents/heavy-worker/AGENT.md +10 -11
- package/src/agents/heavy-worker/agent.json +1 -1
- package/src/agents/maintainer/AGENT.md +0 -2
- package/src/agents/reviewer/AGENT.md +9 -13
- package/src/agents/reviewer/agent.json +1 -1
- package/src/agents/security/AGENT.md +14 -0
- package/src/agents/security/agent.json +6 -0
- package/src/agents/worker/AGENT.md +8 -8
- package/src/agents/worker/agent.json +1 -1
- package/src/agents/writer/AGENT.md +14 -0
- package/src/agents/writer/agent.json +6 -0
- package/src/rules/agent/00-common.md +4 -7
- package/src/rules/lead/01-general.md +0 -3
- package/src/rules/shared/10-tool-workflow.md +10 -2
- package/src/rules/shared/60-verification.md +2 -0
- package/src/runtime/agent/orchestrator/agent-runtime/agent-dispatch.mjs +183 -26
- package/src/runtime/agent/orchestrator/agent-runtime/session-builder.mjs +4 -0
- package/src/runtime/agent/orchestrator/cache-break-trace.mjs +109 -0
- package/src/runtime/agent/orchestrator/config.mjs +7 -2
- package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +16 -3
- package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +9 -1
- package/src/runtime/agent/orchestrator/providers/media-normalization.mjs +30 -9
- package/src/runtime/agent/orchestrator/providers/openai-codex-metadata.mjs +18 -26
- package/src/runtime/agent/orchestrator/providers/openai-oauth-http-sse.mjs +25 -2
- package/src/runtime/agent/orchestrator/providers/openai-oauth-ws.mjs +103 -10
- package/src/runtime/agent/orchestrator/providers/openai-turn-state.mjs +68 -0
- package/src/runtime/agent/orchestrator/providers/openai-ws-delta.mjs +36 -11
- package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +140 -86
- package/src/runtime/agent/orchestrator/providers/openai-ws-stream.mjs +9 -13
- package/src/runtime/agent/orchestrator/session/agent-loop.mjs +72 -4
- package/src/runtime/agent/orchestrator/session/browser-snapshot-supersession.mjs +61 -0
- package/src/runtime/agent/orchestrator/session/compact/handoff.mjs +0 -22
- package/src/runtime/agent/orchestrator/session/compact/messages.mjs +2 -2
- package/src/runtime/agent/orchestrator/session/compact/runner.mjs +11 -23
- package/src/runtime/agent/orchestrator/session/compact/summary.mjs +28 -269
- package/src/runtime/agent/orchestrator/session/compact.mjs +0 -6
- package/src/runtime/agent/orchestrator/session/context-utils.mjs +86 -11
- package/src/runtime/agent/orchestrator/session/eager-dispatch.mjs +17 -1
- package/src/runtime/agent/orchestrator/session/image-strip-recovery.mjs +12 -8
- package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +110 -2
- package/src/runtime/agent/orchestrator/session/loop/pre-dispatch-deny.mjs +3 -16
- package/src/runtime/agent/orchestrator/session/loop/recall-fasttrack.mjs +80 -18
- package/src/runtime/agent/orchestrator/session/loop/steering.mjs +6 -0
- package/src/runtime/agent/orchestrator/session/loop/tool-exec.mjs +3 -0
- package/src/runtime/agent/orchestrator/session/loop/tool-helpers.mjs +6 -0
- package/src/runtime/agent/orchestrator/session/manager/ask-session.mjs +80 -21
- package/src/runtime/agent/orchestrator/session/manager/compaction-runner.mjs +33 -72
- package/src/runtime/agent/orchestrator/session/manager/session-crud.mjs +21 -2
- package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +25 -51
- package/src/runtime/agent/orchestrator/session/manager/status-telemetry.mjs +3 -0
- package/src/runtime/agent/orchestrator/session/manager/tool-resolution.mjs +4 -110
- package/src/runtime/agent/orchestrator/session/manager/turn-checkpoint-journal.mjs +39 -15
- package/src/runtime/agent/orchestrator/session/manager/turn-checkpoint.mjs +14 -4
- package/src/runtime/agent/orchestrator/session/pre-send-compact.mjs +41 -8
- package/src/runtime/agent/orchestrator/session/provider-prefix-guard.mjs +129 -12
- package/src/runtime/agent/orchestrator/session/send-with-recovery.mjs +2 -2
- package/src/runtime/agent/orchestrator/session/store/listing.mjs +8 -1
- package/src/runtime/agent/orchestrator/session/store/save-worker.mjs +15 -1
- package/src/runtime/agent/orchestrator/session/store-summary-index.mjs +36 -2
- package/src/runtime/agent/orchestrator/session/store-summary-reader.mjs +157 -30
- package/src/runtime/agent/orchestrator/session/store-summary-visibility.mjs +57 -0
- package/src/runtime/agent/orchestrator/session/store.mjs +3 -1
- package/src/runtime/agent/orchestrator/session/tool-batch.mjs +22 -0
- package/src/runtime/agent/orchestrator/tools/builtin/read-special-files.mjs +6 -6
- package/src/runtime/agent/orchestrator/tools/progress-message.mjs +4 -0
- package/src/runtime/attachments/pdf-extract.mjs +3 -0
- package/src/runtime/attachments/pdfjs-runtime.mjs +13 -0
- package/src/runtime/browser-bridge/action-schema.mjs +251 -0
- package/src/runtime/browser-bridge/client.mjs +75 -26
- package/src/runtime/browser-bridge/tool-defs.mjs +132 -15
- package/src/runtime/computer-bridge/action-schema.mjs +653 -0
- package/src/runtime/computer-bridge/client.mjs +211 -28
- package/src/runtime/computer-bridge/tool-defs.mjs +25 -45
- package/src/runtime/office/assurance-benchmark.mjs +358 -0
- package/src/runtime/office/assurance.mjs +833 -0
- package/src/runtime/office/benchmark.mjs +496 -0
- package/src/runtime/office/capabilities.mjs +551 -0
- package/src/runtime/office/com-adapter.mjs +283 -0
- package/src/runtime/office/content-model.mjs +183 -0
- package/src/runtime/office/contract-benchmark.mjs +105 -0
- package/src/runtime/office/design-library.mjs +1562 -0
- package/src/runtime/office/design-system.mjs +1966 -0
- package/src/runtime/office/index.mjs +2413 -0
- package/src/runtime/office/journal.mjs +82 -0
- package/src/runtime/office/office-com-host.ps1 +3781 -0
- package/src/runtime/office/office-com-session-host.ps1 +855 -0
- package/src/runtime/office/ooxml-validator.mjs +271 -0
- package/src/runtime/office/pagination.mjs +132 -0
- package/src/runtime/office/pdf-adapter.mjs +591 -0
- package/src/runtime/office/pdf-analysis.mjs +372 -0
- package/src/runtime/office/pdf-render.mjs +167 -0
- package/src/runtime/office/pdf-security.mjs +63 -0
- package/src/runtime/office/polish-benchmark.mjs +190 -0
- package/src/runtime/office/portable-ooxml.mjs +1867 -0
- package/src/runtime/office/quality-live-benchmark.mjs +486 -0
- package/src/runtime/office/quality-pipeline.mjs +136 -0
- package/src/runtime/office/tabular.mjs +302 -0
- package/src/runtime/office/templates/mixdog-executive.pptx +0 -0
- package/src/runtime/office/templates/mixdog-executive.pptx.mixdog.json +190 -0
- package/src/runtime/office/tool-defs.mjs +113 -0
- package/src/runtime/office/visual-diff.mjs +89 -0
- package/src/runtime/office/xlsx-assertions.mjs +172 -0
- package/src/runtime/office/xlsx-contract.mjs +147 -0
- package/src/runtime/shared/agent-route-config.mjs +9 -0
- package/src/runtime/shared/tool-execution-contract.mjs +16 -0
- package/src/runtime/shared/tool-surface.mjs +9 -0
- package/src/session-runtime/context-status.mjs +9 -3
- package/src/session-runtime/deferred-tool-delta.mjs +104 -0
- package/src/session-runtime/goal-runtime.mjs +617 -197
- package/src/session-runtime/lifecycle-api.mjs +50 -8
- package/src/session-runtime/provider-request-snapshot.mjs +33 -9
- package/src/session-runtime/resource-api.mjs +31 -94
- package/src/session-runtime/runtime-core.mjs +78 -26
- package/src/session-runtime/session-lifecycle.mjs +34 -12
- package/src/session-runtime/session-title.mjs +2 -2
- package/src/session-runtime/session-turn-api.mjs +15 -14
- package/src/session-runtime/tool-catalog-data.mjs +1 -1
- package/src/session-runtime/tool-catalog.mjs +38 -99
- package/src/session-runtime/tool-defs.mjs +6 -1
- package/src/session-runtime/tool-surface.mjs +18 -8
- package/src/session-runtime/workflow-agents-api.mjs +8 -25
- package/src/session-runtime/workflow.mjs +8 -6
- package/src/standalone/agent-tool/helpers.mjs +2 -2
- package/src/standalone/agent-tool/job-views.mjs +3 -1
- package/src/standalone/agent-tool/notify.mjs +5 -1
- package/src/standalone/agent-tool/render.mjs +3 -0
- package/src/standalone/agent-tool/spawn-flow.mjs +61 -16
- package/src/standalone/agent-tool/tool-def.mjs +1 -1
- package/src/standalone/agent-tool/worker-index.mjs +123 -0
- package/src/standalone/agent-tool/worker-rows.mjs +2 -1
- package/src/standalone/agent-tool.mjs +57 -10
- package/src/standalone/daemon.mjs +105 -6
- package/src/standalone/session-runtime-host.mjs +61 -48
- package/src/standalone/session-runtime-inline-host.mjs +34 -127
- package/src/standalone/session-runtime-worker.mjs +20 -198
- package/src/standalone/session-service.mjs +632 -7
- package/src/tui/dist/index.mjs +24 -1
- package/src/tui/lib/voice-setup.mjs +4 -1
- package/src/tui/session/context-state.mjs +2 -13
- package/src/tui/session/goal-continuation.mjs +38 -7
- package/src/tui/session/session-api-ext.mjs +9 -0
- package/src/tui/session/session-api.mjs +15 -1
- package/src/tui/session/session-flow.mjs +25 -1
- package/src/tui/session/turn.mjs +88 -16
- package/src/tui/session-local.mjs +25 -3
- package/src/vendor/statusline/src/gateway/route-meta.mjs +31 -0
- package/scripts/generate-voice-runtime-manifest.test.mjs +0 -135
- package/scripts/native-binary-arch.test.mjs +0 -111
- package/scripts/patch-replay.test.mjs +0 -94
- package/src/headless-exec.test.mjs +0 -539
- package/src/runtime/agent/orchestrator/agent-trace-timing.test.mjs +0 -57
- package/src/runtime/agent/orchestrator/context/collect-skills.test.mjs +0 -153
- package/src/runtime/agent/orchestrator/mcp/cancellation.test.mjs +0 -139
- package/src/runtime/agent/orchestrator/mcp/features.test.mjs +0 -66
- package/src/runtime/agent/orchestrator/mcp/security.test.mjs +0 -80
- package/src/runtime/agent/orchestrator/providers/anthropic-fast-mode.test.mjs +0 -90
- package/src/runtime/agent/orchestrator/providers/anthropic-server-fallback.test.mjs +0 -215
- package/src/runtime/agent/orchestrator/providers/antigravity-oauth.test.mjs +0 -165
- package/src/runtime/agent/orchestrator/providers/media-parity.test.mjs +0 -142
- package/src/runtime/agent/orchestrator/providers/openai-codex-identity.test.mjs +0 -153
- package/src/runtime/agent/orchestrator/providers/openai-codex-model.test.mjs +0 -24
- package/src/runtime/agent/orchestrator/providers/openai-ws-delta-parity.test.mjs +0 -190
- package/src/runtime/agent/orchestrator/providers/provider-replay.test.mjs +0 -235
- package/src/runtime/agent/orchestrator/providers/sse-framing.test.mjs +0 -719
- package/src/runtime/agent/orchestrator/session/cache/prefetch-cache.test.mjs +0 -40
- package/src/runtime/agent/orchestrator/session/compact-policy.test.mjs +0 -176
- package/src/runtime/agent/orchestrator/session/compaction-read-reset.test.mjs +0 -161
- package/src/runtime/agent/orchestrator/session/eager-dispatch.test.mjs +0 -115
- package/src/runtime/agent/orchestrator/session/evidence-union.test.mjs +0 -228
- package/src/runtime/agent/orchestrator/session/image-strip-recovery.test.mjs +0 -245
- package/src/runtime/agent/orchestrator/session/lossless-tool-output.test.mjs +0 -125
- package/src/runtime/agent/orchestrator/session/manager/pending-messages-persistence.test.mjs +0 -325
- package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.test.mjs +0 -70
- package/src/runtime/agent/orchestrator/session/manager/turn-checkpoint-journal.test.mjs +0 -457
- package/src/runtime/agent/orchestrator/session/provider-prefix-guard.test.mjs +0 -117
- package/src/runtime/agent/orchestrator/session/reduction-metrics.test.mjs +0 -87
- package/src/runtime/agent/orchestrator/session/store/serialize.test.mjs +0 -60
- package/src/runtime/agent/orchestrator/session/store-summary-reader.test.mjs +0 -583
- package/src/runtime/agent/orchestrator/session/task-wait-control.test.mjs +0 -78
- package/src/runtime/agent/orchestrator/tools/builtin/absence-absorption.test.mjs +0 -43
- package/src/runtime/agent/orchestrator/tools/builtin/absolute-glob-expand.test.mjs +0 -63
- package/src/runtime/agent/orchestrator/tools/builtin/bash-tool-cwd-env.test.mjs +0 -14
- package/src/runtime/agent/orchestrator/tools/builtin/edit-encoding-eol.test.mjs +0 -224
- package/src/runtime/agent/orchestrator/tools/builtin/edit-sequential-occupation.test.mjs +0 -224
- package/src/runtime/agent/orchestrator/tools/builtin/enoent-outside-project.test.mjs +0 -56
- package/src/runtime/agent/orchestrator/tools/builtin/find-search-budget.test.mjs +0 -103
- package/src/runtime/agent/orchestrator/tools/builtin/git-command-policy.test.mjs +0 -30
- package/src/runtime/agent/orchestrator/tools/builtin/git-command-tool.test.mjs +0 -390
- package/src/runtime/agent/orchestrator/tools/builtin/git-repo-rw-lock.test.mjs +0 -83
- package/src/runtime/agent/orchestrator/tools/builtin/grep-output-budget.test.mjs +0 -31
- package/src/runtime/agent/orchestrator/tools/builtin/grep-single-file-rescue.test.mjs +0 -106
- package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-job-records.test.mjs +0 -102
- package/src/runtime/agent/orchestrator/tools/builtin/list-tool-integrity.test.mjs +0 -152
- package/src/runtime/agent/orchestrator/tools/builtin/native-search-health.test.mjs +0 -24
- package/src/runtime/agent/orchestrator/tools/builtin/native-search-transport.test.mjs +0 -24
- package/src/runtime/agent/orchestrator/tools/builtin/noise-dir-visibility.test.mjs +0 -69
- package/src/runtime/agent/orchestrator/tools/builtin/read-glob-survey.test.mjs +0 -71
- package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.test.mjs +0 -56
- package/src/runtime/agent/orchestrator/tools/builtin/search-builders.test.mjs +0 -16
- package/src/runtime/agent/orchestrator/tools/builtin/search-glob-top-k.test.mjs +0 -39
- package/src/runtime/agent/orchestrator/tools/builtin/write-symlink.test.mjs +0 -153
- package/src/runtime/agent/orchestrator/tools/code-graph/aggregate-anchor-relocation.test.mjs +0 -55
- package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.test.mjs +0 -88
- package/src/runtime/agent/orchestrator/tools/code-graph/loose-file-target.test.mjs +0 -72
- package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.test.mjs +0 -54
- package/src/runtime/agent/orchestrator/tools/code-graph/search-references.test.mjs +0 -20
- package/src/runtime/agent/orchestrator/tools/code-graph/symbol-search-scope.test.mjs +0 -55
- package/src/runtime/agent/orchestrator/tools/code-graph/text-span-mask.test.mjs +0 -268
- package/src/runtime/agent/orchestrator/tools/env-scrub.test.mjs +0 -57
- package/src/runtime/agent/orchestrator/tools/graph-binary-fetcher.test.mjs +0 -19
- package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.test.mjs +0 -104
- package/src/runtime/agent/orchestrator/tools/patch/engine-contract-gate.test.mjs +0 -661
- package/src/runtime/agent/orchestrator/tools/patch/patch-codec-eol.test.mjs +0 -552
- package/src/runtime/agent/orchestrator/tools/patch/patch-symlink.test.mjs +0 -141
- package/src/runtime/agent/orchestrator/tools/patch/v4a-pure-move.test.mjs +0 -57
- package/src/runtime/agent/orchestrator/tools/patch/v4a-section-coalesce.test.mjs +0 -81
- package/src/runtime/agent/orchestrator/tools/spawn-binary-fetcher.test.mjs +0 -52
- package/src/runtime/attachments/store.test.mjs +0 -209
- package/src/runtime/channels/lib/voice-runtime-fetcher.test.mjs +0 -17
- package/src/runtime/channels/lib/webhook/deliveries.test.mjs +0 -18
- package/src/runtime/channels/lib/webhook/relay-tunnel.test.mjs +0 -68
- package/src/runtime/media/adapters/codex-image.test.mjs +0 -22
- package/src/runtime/media/download.test.mjs +0 -52
- package/src/runtime/media/renditions.test.mjs +0 -99
- package/src/runtime/media/store.test.mjs +0 -159
- package/src/runtime/memory/lib/archive-security.test.mjs +0 -128
- package/src/runtime/memory/lib/embedding-model-config.test.mjs +0 -33
- package/src/runtime/memory/lib/http-wire.test.mjs +0 -15
- package/src/runtime/memory/lib/memory-cycle-packets.test.mjs +0 -49
- package/src/runtime/memory/lib/memory-cycle2-shared.test.mjs +0 -78
- package/src/runtime/memory/lib/memory-embedding-migration.test.mjs +0 -50
- package/src/runtime/memory/lib/pg/process.test.mjs +0 -36
- package/src/runtime/memory/lib/trace-store.test.mjs +0 -57
- package/src/runtime/memory/lib/transcript-ingest.test.mjs +0 -52
- package/src/runtime/shared/background-tasks.test.mjs +0 -78
- package/src/runtime/shared/bounded-download.test.mjs +0 -41
- package/src/runtime/shared/channel-notification-routing.test.mjs +0 -25
- package/src/runtime/shared/child-spawn-remote.test.mjs +0 -124
- package/src/runtime/shared/remote-intent.test.mjs +0 -40
- package/src/runtime/shared/resource-admission.test.mjs +0 -43
- package/src/runtime/shared/runtime-root.test.mjs +0 -34
- package/src/runtime/shared/schedule-time.test.mjs +0 -29
- package/src/runtime/shared/session-runtime-health.test.mjs +0 -83
- package/src/runtime/shared/skill-document.test.mjs +0 -81
- package/src/runtime/shared/tool-surface.test.mjs +0 -87
- package/src/runtime/shared/update-checker.test.mjs +0 -33
- package/src/runtime/shared/user-cwd.test.mjs +0 -43
- package/src/runtime/shared/webhook-session-run.test.mjs +0 -14
- package/src/session-runtime/agent-disable.test.mjs +0 -123
- package/src/session-runtime/cwd-plugins.test.mjs +0 -75
- package/src/session-runtime/cwd-tool-routing.test.mjs +0 -61
- package/src/session-runtime/global-extensions.test.mjs +0 -28
- package/src/session-runtime/goal-runtime.test.mjs +0 -108
- package/src/session-runtime/lifecycle-api.test.mjs +0 -21
- package/src/session-runtime/model-settings-persist.test.mjs +0 -105
- package/src/session-runtime/notification-bus.test.mjs +0 -148
- package/src/session-runtime/plugin-mcp-standard.test.mjs +0 -177
- package/src/session-runtime/prewarm.test.mjs +0 -52
- package/src/session-runtime/provider-request-snapshot.test.mjs +0 -37
- package/src/session-runtime/resource-api-global.test.mjs +0 -69
- package/src/session-runtime/session-lifecycle.test.mjs +0 -125
- package/src/session-runtime/session-title.test.mjs +0 -36
- package/src/session-runtime/skills-api.test.mjs +0 -99
- package/src/session-runtime/tool-policy-surface.test.mjs +0 -331
- package/src/standalone/agent-tool/helpers.test.mjs +0 -62
- package/src/standalone/agent-tool/notify.test.mjs +0 -37
- package/src/standalone/agent-tool/spawn-flow.test.mjs +0 -63
- package/src/standalone/channel-restart.test.mjs +0 -145
- package/src/standalone/channel-session-router.test.mjs +0 -37
- package/src/standalone/channel-worker-heartbeat.test.mjs +0 -30
- package/src/standalone/hook-bus/handlers.test.mjs +0 -88
- package/src/standalone/plugin-admin.test.mjs +0 -173
- package/src/standalone/session-runtime-agent-control-client.test.mjs +0 -71
- package/src/standalone/session-runtime-dispatch-cancel.test.mjs +0 -139
- package/src/standalone/session-runtime-host-factory.test.mjs +0 -31
- package/src/standalone/session-runtime-host-health.test.mjs +0 -160
- package/src/standalone/session-runtime-inline-host.test.mjs +0 -83
- package/src/standalone/session-runtime-provider-cooldown.test.mjs +0 -80
- package/src/standalone/session-runtime-shard-host.test.mjs +0 -569
- package/src/standalone/session-runtime-shard-router.test.mjs +0 -101
- package/src/tui/app/live-spinner-visibility.test.mjs +0 -44
- package/src/tui/app/panel-epoch.test.mjs +0 -1009
- package/src/tui/app/panel-handoff.test.mjs +0 -311
- package/src/tui/app/prompt-submit.test.mjs +0 -206
- package/src/tui/session/completion-card-restore.test.mjs +0 -111
- package/src/tui/session/goal-continuation.test.mjs +0 -84
- package/src/tui/session/oauth-flows.test.mjs +0 -134
- package/src/tui/session/queue-helpers.test.mjs +0 -46
- package/src/tui/session/session-action-surface.test.mjs +0 -108
- package/src/tui/session/task-wait-submit.test.mjs +0 -80
- package/src/tui/session/tool-result-status.test.mjs +0 -131
- package/src/tui/session/tui-steering-persist.test.mjs +0 -104
|
@@ -20,10 +20,21 @@ import {
|
|
|
20
20
|
materializePromptSubmission,
|
|
21
21
|
preparePromptSubmissionForProvider,
|
|
22
22
|
} from '../runtime/attachments/store.mjs';
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
cancelBackgroundTasks,
|
|
25
|
+
hasActiveBackgroundTasks,
|
|
26
|
+
} from '../runtime/shared/background-tasks.mjs';
|
|
24
27
|
|
|
25
28
|
const MAX_CLONE_DEPTH = 24;
|
|
29
|
+
const SESSION_ID_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
26
30
|
const requireDesktopService = createRequire(import.meta.url);
|
|
31
|
+
const EXTERNAL_SESSION_ACTIONS = new Set([
|
|
32
|
+
...SESSION_READ_ACTION_SET,
|
|
33
|
+
...SESSION_CONFIGURE_ACTION_SET,
|
|
34
|
+
'submitAsync',
|
|
35
|
+
'abort',
|
|
36
|
+
'resolveToolApproval',
|
|
37
|
+
]);
|
|
27
38
|
|
|
28
39
|
async function loadDesktopServiceModule(moduleUrl) {
|
|
29
40
|
const parsed = new URL(moduleUrl);
|
|
@@ -97,7 +108,11 @@ export function createSessionService({
|
|
|
97
108
|
createSessionRuntime = null,
|
|
98
109
|
sessionExists = null,
|
|
99
110
|
readStoredSession = null,
|
|
111
|
+
readStoredGoal = null,
|
|
112
|
+
listStoredActiveGoalSessionIds = null,
|
|
100
113
|
subscribeExternalSessionStates = null,
|
|
114
|
+
invokeExternalSessionAction = null,
|
|
115
|
+
readExternalSessionState = null,
|
|
101
116
|
listSessions = null,
|
|
102
117
|
getRemoteSessionState = null,
|
|
103
118
|
desktopRuntime = null,
|
|
@@ -124,6 +139,15 @@ export function createSessionService({
|
|
|
124
139
|
// addressed by clients; sessionId is the only identity outside this module.
|
|
125
140
|
const sessions = new Set();
|
|
126
141
|
const sessionsById = new Map();
|
|
142
|
+
// Agent children are catalog metadata over ordinary daemon-owned sessions.
|
|
143
|
+
// Their transcript/execution state remains exclusively in `sessionsById`;
|
|
144
|
+
// this index carries only the Parent–Child relationship and public Agent
|
|
145
|
+
// routing fields needed before/after a turn.
|
|
146
|
+
const agentSessions = new Map();
|
|
147
|
+
const agentChildren = new Map();
|
|
148
|
+
const agentCancelRuns = new Map();
|
|
149
|
+
let agentRehydrated = false;
|
|
150
|
+
let agentRehydratePromise = null;
|
|
127
151
|
let busyEntries = 0;
|
|
128
152
|
// Desktop adapters are keyed by their exact module URL so a dev preview and
|
|
129
153
|
// installed build never share module state accidentally.
|
|
@@ -495,9 +519,27 @@ export function createSessionService({
|
|
|
495
519
|
let state = { ...snapshot, sessionId };
|
|
496
520
|
const runtime = {
|
|
497
521
|
isWireSafe: true,
|
|
522
|
+
externalAction: typeof invokeExternalSessionAction === 'function',
|
|
498
523
|
getState: () => state,
|
|
499
524
|
setState: (next) => { state = next; },
|
|
500
525
|
};
|
|
526
|
+
Object.defineProperties(runtime, {
|
|
527
|
+
id: { get: () => sessionId },
|
|
528
|
+
provider: { get: () => String(state.provider || '') },
|
|
529
|
+
model: { get: () => String(state.model || '') },
|
|
530
|
+
session: {
|
|
531
|
+
get: () => ({
|
|
532
|
+
id: sessionId,
|
|
533
|
+
provider: String(state.provider || ''),
|
|
534
|
+
model: String(state.model || ''),
|
|
535
|
+
}),
|
|
536
|
+
},
|
|
537
|
+
});
|
|
538
|
+
if (typeof invokeExternalSessionAction === 'function') {
|
|
539
|
+
for (const name of EXTERNAL_SESSION_ACTIONS) {
|
|
540
|
+
runtime[name] = (...args) => invokeExternalSessionAction(sessionId, name, args);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
501
543
|
entry = {
|
|
502
544
|
runtime,
|
|
503
545
|
subscribers: new Set(),
|
|
@@ -587,9 +629,13 @@ export function createSessionService({
|
|
|
587
629
|
cwd: params.cwd || process.cwd(),
|
|
588
630
|
provider: params.provider,
|
|
589
631
|
model: params.model,
|
|
632
|
+
effort: params.effort,
|
|
633
|
+
fast: params.fast,
|
|
634
|
+
modelParameters: params.modelParameters,
|
|
590
635
|
toolMode: params.toolMode || 'full',
|
|
591
636
|
remote: params.remote === true,
|
|
592
637
|
desktopSession: params.desktopSession ?? null,
|
|
638
|
+
sessionProfile: params.sessionProfile ?? null,
|
|
593
639
|
});
|
|
594
640
|
const entry = {
|
|
595
641
|
runtime, cwd: params.cwd || process.cwd(), timer: null, disposed: false,
|
|
@@ -660,11 +706,64 @@ export function createSessionService({
|
|
|
660
706
|
return entry;
|
|
661
707
|
}
|
|
662
708
|
|
|
709
|
+
/** Bind a retained external Agent snapshot into the ordinary view entry
|
|
710
|
+
* without materializing a daemon runtime. publishExternalSessionState
|
|
711
|
+
* adopts pending viewers so a completed turn is visible immediately. */
|
|
712
|
+
async function bindExternalSessionView(sessionId) {
|
|
713
|
+
const id = String(sessionId || '');
|
|
714
|
+
if (!id) return null;
|
|
715
|
+
const owner = sessionOwner(id);
|
|
716
|
+
if (owner) return owner;
|
|
717
|
+
const existing = externalEntryForView(id);
|
|
718
|
+
if (existing) return existing;
|
|
719
|
+
if (typeof readExternalSessionState !== 'function') return null;
|
|
720
|
+
|
|
721
|
+
// Register the address before the retained-snapshot read. Even a
|
|
722
|
+
// synchronous reader crosses an `await` boundary, so a newer external
|
|
723
|
+
// frame can otherwise arrive in that microtask, find no pending viewer,
|
|
724
|
+
// and be dropped before the stale retained snapshot is bound.
|
|
725
|
+
const placeholder = new Set();
|
|
726
|
+
const ownsPlaceholder = !pendingViewers.has(id);
|
|
727
|
+
if (ownsPlaceholder) pendingViewers.set(id, placeholder);
|
|
728
|
+
const clearPlaceholder = () => {
|
|
729
|
+
if (ownsPlaceholder
|
|
730
|
+
&& pendingViewers.get(id) === placeholder
|
|
731
|
+
&& placeholder.size === 0) {
|
|
732
|
+
pendingViewers.delete(id);
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
let snapshot = null;
|
|
737
|
+
try {
|
|
738
|
+
snapshot = await readExternalSessionState(id);
|
|
739
|
+
} catch (error) {
|
|
740
|
+
log(`external session state read failed session=${id}: ${error?.message || error}`);
|
|
741
|
+
const raced = sessionOwner(id) || externalEntryForView(id);
|
|
742
|
+
clearPlaceholder();
|
|
743
|
+
return raced;
|
|
744
|
+
}
|
|
745
|
+
const raced = sessionOwner(id) || externalEntryForView(id);
|
|
746
|
+
if (raced) {
|
|
747
|
+
clearPlaceholder();
|
|
748
|
+
return raced;
|
|
749
|
+
}
|
|
750
|
+
if (!snapshot || typeof snapshot !== 'object') {
|
|
751
|
+
clearPlaceholder();
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
publishExternalSessionState({ sessionId: id, snapshot });
|
|
755
|
+
const bound = sessionOwner(id) || externalEntryForView(id);
|
|
756
|
+
if (!bound) clearPlaceholder();
|
|
757
|
+
return bound;
|
|
758
|
+
}
|
|
759
|
+
|
|
663
760
|
/** The runtime hosting sessionId: the existing owner, or a fresh load. One
|
|
664
761
|
* load per session at a time — concurrent panes converge on one session runtime. */
|
|
665
762
|
async function entryForSession(sessionId, hints = {}) {
|
|
666
763
|
const owner = sessionOwner(sessionId);
|
|
667
764
|
if (owner) return owner;
|
|
765
|
+
const external = await bindExternalSessionView(sessionId);
|
|
766
|
+
if (external?.runtime?.externalAction === true) return external;
|
|
668
767
|
const inFlight = sessionLoads.get(sessionId);
|
|
669
768
|
if (inFlight) return inFlight;
|
|
670
769
|
let loading;
|
|
@@ -711,9 +810,19 @@ export function createSessionService({
|
|
|
711
810
|
return null;
|
|
712
811
|
}
|
|
713
812
|
if (!snapshot || typeof snapshot !== 'object') return null;
|
|
813
|
+
let goal;
|
|
814
|
+
if (typeof readStoredGoal === 'function') {
|
|
815
|
+
try {
|
|
816
|
+
goal = await readStoredGoal(sessionId) ?? null;
|
|
817
|
+
} catch (err) {
|
|
818
|
+
log(`stored Goal projection failed session=${sessionId}: ${err?.message || err}`);
|
|
819
|
+
goal = null;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
714
822
|
return sanitizeForWire({
|
|
715
823
|
...snapshot,
|
|
716
824
|
sessionId,
|
|
825
|
+
...(typeof readStoredGoal === 'function' ? { goal } : {}),
|
|
717
826
|
queued: Array.isArray(snapshot.queued) ? snapshot.queued : [],
|
|
718
827
|
});
|
|
719
828
|
}
|
|
@@ -809,7 +918,12 @@ export function createSessionService({
|
|
|
809
918
|
if (typeof listSessions !== 'function') {
|
|
810
919
|
throw new Error('session catalog is unavailable');
|
|
811
920
|
}
|
|
812
|
-
const sessions = await listSessions(
|
|
921
|
+
const sessions = await listSessions({
|
|
922
|
+
...(options || {}),
|
|
923
|
+
// Agent-only records are an internal ancestry/reuse source, never part
|
|
924
|
+
// of the ordinary session catalog returned over the public transport.
|
|
925
|
+
includeAgentOnly: false,
|
|
926
|
+
});
|
|
813
927
|
const remoteSession = typeof getRemoteSessionState === 'function'
|
|
814
928
|
? await getRemoteSessionState()
|
|
815
929
|
: null;
|
|
@@ -976,7 +1090,9 @@ export function createSessionService({
|
|
|
976
1090
|
}
|
|
977
1091
|
const id = String(sessionId || '');
|
|
978
1092
|
if (!id) throw new TypeError('sessionId is required');
|
|
979
|
-
const live = await liveEntryForView(id)
|
|
1093
|
+
const live = await liveEntryForView(id)
|
|
1094
|
+
|| externalEntryForView(id)
|
|
1095
|
+
|| await bindExternalSessionView(id);
|
|
980
1096
|
if (live) {
|
|
981
1097
|
const step = advance(live);
|
|
982
1098
|
retainUnwatched(live, 'headless session read');
|
|
@@ -1017,7 +1133,9 @@ export function createSessionService({
|
|
|
1017
1133
|
) {
|
|
1018
1134
|
const id = String(sessionId || '');
|
|
1019
1135
|
if (!id) throw new TypeError('sessionId is required');
|
|
1020
|
-
const live = await liveEntryForView(id)
|
|
1136
|
+
const live = await liveEntryForView(id)
|
|
1137
|
+
|| externalEntryForView(id)
|
|
1138
|
+
|| await bindExternalSessionView(id);
|
|
1021
1139
|
if (live) {
|
|
1022
1140
|
addSubscriber(live, ctx);
|
|
1023
1141
|
const step = advance(live);
|
|
@@ -1095,7 +1213,18 @@ export function createSessionService({
|
|
|
1095
1213
|
// Await intake only: submitAsync resolves once the prompt is represented by
|
|
1096
1214
|
// the queue/user row, while provider execution remains daemon-owned and
|
|
1097
1215
|
// detached.
|
|
1098
|
-
const
|
|
1216
|
+
const submissionOptions = entry.runtime.externalAction === true
|
|
1217
|
+
? {
|
|
1218
|
+
...intake.options,
|
|
1219
|
+
transcriptMeta: {
|
|
1220
|
+
...(intake.options?.transcriptMeta && typeof intake.options.transcriptMeta === 'object'
|
|
1221
|
+
? intake.options.transcriptMeta
|
|
1222
|
+
: {}),
|
|
1223
|
+
sender: 'user',
|
|
1224
|
+
},
|
|
1225
|
+
}
|
|
1226
|
+
: intake.options;
|
|
1227
|
+
const accepted = await Promise.resolve(target.call(entry.runtime, intake.prompt, submissionOptions));
|
|
1099
1228
|
const firstSubmit = accepted === true && entry.reservedOnly;
|
|
1100
1229
|
if (accepted === true) {
|
|
1101
1230
|
entry.reservedOnly = false;
|
|
@@ -1120,6 +1249,42 @@ export function createSessionService({
|
|
|
1120
1249
|
return entry.runtime;
|
|
1121
1250
|
}
|
|
1122
1251
|
|
|
1252
|
+
async function recoverActiveGoals() {
|
|
1253
|
+
if (typeof listStoredActiveGoalSessionIds !== 'function') {
|
|
1254
|
+
return { found: 0, resumed: 0, skipped: 0, failed: 0 };
|
|
1255
|
+
}
|
|
1256
|
+
let listed;
|
|
1257
|
+
try {
|
|
1258
|
+
listed = await listStoredActiveGoalSessionIds();
|
|
1259
|
+
} catch (err) {
|
|
1260
|
+
log(`active Goal discovery failed: ${err?.message || err}`);
|
|
1261
|
+
return { found: 0, resumed: 0, skipped: 0, failed: 1 };
|
|
1262
|
+
}
|
|
1263
|
+
const sessionIds = [...new Set(Array.isArray(listed) ? listed : [])]
|
|
1264
|
+
.map((sessionId) => String(sessionId || ''))
|
|
1265
|
+
.filter((sessionId) => SESSION_ID_PATTERN.test(sessionId));
|
|
1266
|
+
let resumed = 0;
|
|
1267
|
+
let skipped = 0;
|
|
1268
|
+
let failed = 0;
|
|
1269
|
+
for (const sessionId of sessionIds) {
|
|
1270
|
+
try {
|
|
1271
|
+
if (typeof readStoredGoal === 'function') {
|
|
1272
|
+
const goal = await readStoredGoal(sessionId);
|
|
1273
|
+
if (goal?.status !== 'active') {
|
|
1274
|
+
skipped += 1;
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
await materializeSession(sessionId);
|
|
1279
|
+
resumed += 1;
|
|
1280
|
+
} catch (err) {
|
|
1281
|
+
failed += 1;
|
|
1282
|
+
log(`active Goal recovery failed session=${sessionId}: ${err?.message || err}`);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
return { found: sessionIds.length, resumed, skipped, failed };
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1123
1288
|
async function abortSession({
|
|
1124
1289
|
sessionId, open: openHints = {}, options = {},
|
|
1125
1290
|
baseRevision = null,
|
|
@@ -1129,7 +1294,32 @@ export function createSessionService({
|
|
|
1129
1294
|
const entry = await entryForSession(id, openHints || {});
|
|
1130
1295
|
const target = entry.runtime.abort;
|
|
1131
1296
|
if (typeof target !== 'function') throw new TypeError('session action abort is unavailable');
|
|
1132
|
-
const
|
|
1297
|
+
const state = entry.runtime.getState?.() || {};
|
|
1298
|
+
const parentId = String(state.parentSessionId || state.ownerSessionId || '').trim();
|
|
1299
|
+
const isLegacyAgentChild = String(state.owner || '').trim().toLowerCase() === 'agent'
|
|
1300
|
+
&& String(state.agent || '').trim().toLowerCase() !== 'lead'
|
|
1301
|
+
&& parentId
|
|
1302
|
+
&& parentId !== id;
|
|
1303
|
+
const abortsAgentTurn = agentSessions.has(id)
|
|
1304
|
+
|| String(state.visibility || '').trim().toLowerCase() === 'agent-only'
|
|
1305
|
+
|| isLegacyAgentChild;
|
|
1306
|
+
let rawResult;
|
|
1307
|
+
try {
|
|
1308
|
+
rawResult = target.call(entry.runtime, options || {});
|
|
1309
|
+
} finally {
|
|
1310
|
+
// Lead cancellation must leave delegated work alive. An Agent cancelling
|
|
1311
|
+
// its own turn, however, is the live parent signal for Agent work nested
|
|
1312
|
+
// under that turn; task cancellation reaches the child's own controller
|
|
1313
|
+
// without enumerating durable sessions.
|
|
1314
|
+
if (abortsAgentTurn) {
|
|
1315
|
+
cancelBackgroundTasks({
|
|
1316
|
+
surface: 'agent',
|
|
1317
|
+
callerSessionId: id,
|
|
1318
|
+
reason: 'parent Agent turn aborted',
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
rawResult = await rawResult;
|
|
1133
1323
|
const abortResult = rawResult && typeof rawResult === 'object'
|
|
1134
1324
|
? rawResult
|
|
1135
1325
|
: { aborted: rawResult === true };
|
|
@@ -1139,6 +1329,437 @@ export function createSessionService({
|
|
|
1139
1329
|
return sessionResult(entry, step, baseRevision, abortResult);
|
|
1140
1330
|
}
|
|
1141
1331
|
|
|
1332
|
+
function linkAgentDescriptor(descriptor) {
|
|
1333
|
+
const sessionId = String(descriptor?.id || '').trim();
|
|
1334
|
+
const parentSessionId = String(descriptor?.parentSessionId || '').trim();
|
|
1335
|
+
if (!sessionId || !parentSessionId) {
|
|
1336
|
+
throw new TypeError('agent child requires session and parent ids');
|
|
1337
|
+
}
|
|
1338
|
+
const previous = agentSessions.get(sessionId);
|
|
1339
|
+
if (previous?.parentSessionId && previous.parentSessionId !== parentSessionId) {
|
|
1340
|
+
agentChildren.get(previous.parentSessionId)?.delete(sessionId);
|
|
1341
|
+
}
|
|
1342
|
+
const linked = {
|
|
1343
|
+
...(previous || {}),
|
|
1344
|
+
...descriptor,
|
|
1345
|
+
id: sessionId,
|
|
1346
|
+
parentSessionId,
|
|
1347
|
+
ownerSessionId: String(
|
|
1348
|
+
descriptor.ownerSessionId
|
|
1349
|
+
|| previous?.ownerSessionId
|
|
1350
|
+
|| agentSessions.get(parentSessionId)?.ownerSessionId
|
|
1351
|
+
|| parentSessionId,
|
|
1352
|
+
),
|
|
1353
|
+
owner: 'agent',
|
|
1354
|
+
visibility: 'agent-only',
|
|
1355
|
+
closed: descriptor.closed === true,
|
|
1356
|
+
};
|
|
1357
|
+
agentSessions.set(sessionId, linked);
|
|
1358
|
+
let children = agentChildren.get(parentSessionId);
|
|
1359
|
+
if (!children) agentChildren.set(parentSessionId, children = new Set());
|
|
1360
|
+
children.add(sessionId);
|
|
1361
|
+
return linked;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
function validLinkedSessionId(value, ownId = '') {
|
|
1365
|
+
const id = String(value || '').trim();
|
|
1366
|
+
return SESSION_ID_PATTERN.test(id) && id !== ownId ? id : '';
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
function storedAgentCandidate(row) {
|
|
1370
|
+
const id = String(row?.id || '').trim();
|
|
1371
|
+
if (!SESSION_ID_PATTERN.test(id)) return null;
|
|
1372
|
+
const parentSessionId = validLinkedSessionId(
|
|
1373
|
+
row?.parentSessionId || row?.ownerSessionId,
|
|
1374
|
+
id,
|
|
1375
|
+
);
|
|
1376
|
+
if (!parentSessionId) return null;
|
|
1377
|
+
const declaredVisibility = String(
|
|
1378
|
+
row?.visibility || row?.sessionVisibility || '',
|
|
1379
|
+
).trim().toLowerCase() === 'agent-only';
|
|
1380
|
+
const legacyAgentChild = String(row?.owner || '').trim().toLowerCase() === 'agent';
|
|
1381
|
+
if (!declaredVisibility && !legacyAgentChild) return null;
|
|
1382
|
+
return { row, id, parentSessionId };
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
function lastStoredAgentHandoff(row) {
|
|
1386
|
+
if (typeof row?.lastHandoff === 'string') return row.lastHandoff;
|
|
1387
|
+
const messages = Array.isArray(row?.messages) ? row.messages : [];
|
|
1388
|
+
const assistant = [...messages].reverse().find((message) => (
|
|
1389
|
+
message?.role === 'assistant'
|
|
1390
|
+
&& (typeof message.content === 'string' ? message.content.trim() : message.content)
|
|
1391
|
+
));
|
|
1392
|
+
if (!assistant) return '';
|
|
1393
|
+
return typeof assistant.content === 'string'
|
|
1394
|
+
? assistant.content
|
|
1395
|
+
: JSON.stringify(assistant.content);
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
/** Rebuild the Agent-only routing layer from lightweight durable summaries
|
|
1399
|
+
* after daemon replacement. Transcripts remain store/runtime owned and are
|
|
1400
|
+
* loaded by exact canonical session id only when a caller needs one. */
|
|
1401
|
+
async function rehydrateAgentSessions() {
|
|
1402
|
+
if (agentRehydrated) return agentSessions.size;
|
|
1403
|
+
if (agentRehydratePromise) return agentRehydratePromise;
|
|
1404
|
+
if (typeof listSessions !== 'function') {
|
|
1405
|
+
agentRehydrated = true;
|
|
1406
|
+
return agentSessions.size;
|
|
1407
|
+
}
|
|
1408
|
+
let loading;
|
|
1409
|
+
loading = (async () => {
|
|
1410
|
+
const stored = await listSessions({
|
|
1411
|
+
includeAgentOnly: true,
|
|
1412
|
+
summaryOnly: true,
|
|
1413
|
+
refreshFromStorage: false,
|
|
1414
|
+
});
|
|
1415
|
+
let candidates = (Array.isArray(stored) ? stored : [])
|
|
1416
|
+
.map(storedAgentCandidate)
|
|
1417
|
+
.filter(Boolean);
|
|
1418
|
+
if (typeof readStoredSession === 'function') {
|
|
1419
|
+
candidates = await Promise.all(candidates.map(async (candidate) => {
|
|
1420
|
+
if (candidate.row?.parentSessionId) return candidate;
|
|
1421
|
+
try {
|
|
1422
|
+
const metadata = await readStoredSession(candidate.id, { metadataOnly: true });
|
|
1423
|
+
return storedAgentCandidate({
|
|
1424
|
+
...candidate.row,
|
|
1425
|
+
...(metadata && typeof metadata === 'object' ? metadata : {}),
|
|
1426
|
+
id: candidate.id,
|
|
1427
|
+
}) || candidate;
|
|
1428
|
+
} catch (error) {
|
|
1429
|
+
log(`agent metadata migration failed session=${candidate.id}: ${error?.message || error}`);
|
|
1430
|
+
return candidate;
|
|
1431
|
+
}
|
|
1432
|
+
}));
|
|
1433
|
+
}
|
|
1434
|
+
const byId = new Map(candidates.map((candidate) => [candidate.id, candidate]));
|
|
1435
|
+
const roots = new Map();
|
|
1436
|
+
const resolveRoot = (candidate, seen = new Set()) => {
|
|
1437
|
+
if (!candidate || seen.has(candidate.id)) return candidate?.parentSessionId || '';
|
|
1438
|
+
if (roots.has(candidate.id)) return roots.get(candidate.id);
|
|
1439
|
+
seen.add(candidate.id);
|
|
1440
|
+
const explicitOwner = validLinkedSessionId(candidate.row?.ownerSessionId, candidate.id);
|
|
1441
|
+
if (explicitOwner && explicitOwner !== candidate.parentSessionId) {
|
|
1442
|
+
roots.set(candidate.id, explicitOwner);
|
|
1443
|
+
return explicitOwner;
|
|
1444
|
+
}
|
|
1445
|
+
const parent = byId.get(candidate.parentSessionId);
|
|
1446
|
+
const root = parent ? resolveRoot(parent, seen) : (explicitOwner || candidate.parentSessionId);
|
|
1447
|
+
roots.set(candidate.id, root);
|
|
1448
|
+
return root;
|
|
1449
|
+
};
|
|
1450
|
+
for (const candidate of candidates) {
|
|
1451
|
+
// A child created while the summary load was in flight is newer than
|
|
1452
|
+
// the stored row and must never be rolled back by rehydration.
|
|
1453
|
+
if (agentSessions.has(candidate.id)) continue;
|
|
1454
|
+
const row = candidate.row;
|
|
1455
|
+
linkAgentDescriptor({
|
|
1456
|
+
id: candidate.id,
|
|
1457
|
+
parentSessionId: candidate.parentSessionId,
|
|
1458
|
+
ownerSessionId: resolveRoot(candidate),
|
|
1459
|
+
owner: 'agent',
|
|
1460
|
+
visibility: 'agent-only',
|
|
1461
|
+
agent: row.agent || row.sourceName || 'worker',
|
|
1462
|
+
agentTag: row.agentTag || row.tag || null,
|
|
1463
|
+
cwd: row.cwd || process.cwd(),
|
|
1464
|
+
provider: row.provider || null,
|
|
1465
|
+
model: row.model || null,
|
|
1466
|
+
presetName: row.presetName || row.preset || row.profileId || null,
|
|
1467
|
+
effort: row.effort || null,
|
|
1468
|
+
fast: row.fast === true,
|
|
1469
|
+
modelParameters: row.modelParameters || null,
|
|
1470
|
+
taskType: row.taskType || null,
|
|
1471
|
+
maxLoopIterations: row.maxLoopIterations,
|
|
1472
|
+
permission: row.permission || null,
|
|
1473
|
+
permissionMode: row.permissionMode || null,
|
|
1474
|
+
toolPermission: row.toolPermission || null,
|
|
1475
|
+
schemaAllowedTools: Array.isArray(row.schemaAllowedTools)
|
|
1476
|
+
? row.schemaAllowedTools
|
|
1477
|
+
: null,
|
|
1478
|
+
sourceType: row.sourceType || 'agent',
|
|
1479
|
+
sourceName: row.sourceName || row.agent || 'agent',
|
|
1480
|
+
clientHostPid: row.clientHostPid || null,
|
|
1481
|
+
createdAt: row.createdAt || null,
|
|
1482
|
+
updatedAt: row.updatedAt || row.lastUsedAt || null,
|
|
1483
|
+
status: row.closed === true ? 'closed' : (row.status || 'idle'),
|
|
1484
|
+
stage: row.closed === true ? 'closed' : (row.stage || row.status || 'idle'),
|
|
1485
|
+
messageCount: Number(row.messageCount)
|
|
1486
|
+
|| (Array.isArray(row.messages) ? row.messages.length : 0),
|
|
1487
|
+
lastHandoff: lastStoredAgentHandoff(row),
|
|
1488
|
+
closed: row.closed === true,
|
|
1489
|
+
});
|
|
1490
|
+
}
|
|
1491
|
+
agentRehydrated = true;
|
|
1492
|
+
return agentSessions.size;
|
|
1493
|
+
})().finally(() => {
|
|
1494
|
+
if (agentRehydratePromise === loading) agentRehydratePromise = null;
|
|
1495
|
+
});
|
|
1496
|
+
agentRehydratePromise = loading;
|
|
1497
|
+
return loading;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
function agentDescriptor(sessionId) {
|
|
1501
|
+
const id = String(sessionId || '').trim();
|
|
1502
|
+
const descriptor = agentSessions.get(id);
|
|
1503
|
+
if (!descriptor) return null;
|
|
1504
|
+
const owner = sessionOwner(id);
|
|
1505
|
+
const state = owner?.runtime?.getState?.() || {};
|
|
1506
|
+
const status = descriptor.closed
|
|
1507
|
+
? (descriptor.status || 'closed')
|
|
1508
|
+
: stateBusy(state)
|
|
1509
|
+
? 'running'
|
|
1510
|
+
: (descriptor.status || 'idle');
|
|
1511
|
+
return {
|
|
1512
|
+
...descriptor,
|
|
1513
|
+
status,
|
|
1514
|
+
stage: status,
|
|
1515
|
+
messageCount: Array.isArray(state.items) && state.items.length > 0
|
|
1516
|
+
? state.items.length
|
|
1517
|
+
: Math.max(0, Number(descriptor.messageCount) || 0),
|
|
1518
|
+
updatedAt: descriptor.updatedAt || Date.now(),
|
|
1519
|
+
};
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
function rootOwnerSessionId(sessionId) {
|
|
1523
|
+
const id = String(sessionId || '').trim();
|
|
1524
|
+
return agentSessions.get(id)?.ownerSessionId || id || null;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
async function createAgentChild({ spec = {}, prompt = '', tag = null } = {}) {
|
|
1528
|
+
await rehydrateAgentSessions();
|
|
1529
|
+
const parentSessionId = String(spec.parentSessionId || '').trim();
|
|
1530
|
+
if (!parentSessionId) throw new TypeError('agent child parentSessionId is required');
|
|
1531
|
+
const ownerSessionId = String(
|
|
1532
|
+
spec.ownerSessionId
|
|
1533
|
+
|| agentSessions.get(parentSessionId)?.ownerSessionId
|
|
1534
|
+
|| parentSessionId,
|
|
1535
|
+
);
|
|
1536
|
+
const preset = spec.preset && typeof spec.preset === 'object' ? spec.preset : {};
|
|
1537
|
+
const provider = String(preset.provider || spec.provider || '').trim();
|
|
1538
|
+
const model = String(preset.model || spec.model || '').trim();
|
|
1539
|
+
if (!provider || !model) throw new Error('agent child route is incomplete');
|
|
1540
|
+
const sessionProfile = {
|
|
1541
|
+
owner: 'agent',
|
|
1542
|
+
agent: String(spec.agent || 'worker'),
|
|
1543
|
+
parentSessionId,
|
|
1544
|
+
ownerSessionId,
|
|
1545
|
+
visibility: 'agent-only',
|
|
1546
|
+
agentTag: String(spec.agentTag || tag || '').trim() || null,
|
|
1547
|
+
taskType: spec.taskType || null,
|
|
1548
|
+
maxLoopIterations: spec.maxLoopIterations,
|
|
1549
|
+
permission: spec.permission || null,
|
|
1550
|
+
permissionMode: spec.permissionMode || null,
|
|
1551
|
+
schemaAllowedTools: Array.isArray(spec.schemaAllowedTools)
|
|
1552
|
+
? spec.schemaAllowedTools
|
|
1553
|
+
: null,
|
|
1554
|
+
sourceType: spec.sourceType || 'agent',
|
|
1555
|
+
sourceName: spec.sourceName || spec.agent || 'agent',
|
|
1556
|
+
clientHostPid: spec.clientHostPid || null,
|
|
1557
|
+
};
|
|
1558
|
+
const created = await createSession({
|
|
1559
|
+
cwd: spec.cwd || process.cwd(),
|
|
1560
|
+
provider,
|
|
1561
|
+
model,
|
|
1562
|
+
effort: preset.effort,
|
|
1563
|
+
fast: preset.fast === true,
|
|
1564
|
+
modelParameters: preset.modelParameters,
|
|
1565
|
+
toolMode: 'full',
|
|
1566
|
+
sessionProfile,
|
|
1567
|
+
});
|
|
1568
|
+
const descriptor = linkAgentDescriptor({
|
|
1569
|
+
id: created.sessionId,
|
|
1570
|
+
...sessionProfile,
|
|
1571
|
+
cwd: spec.cwd || process.cwd(),
|
|
1572
|
+
provider,
|
|
1573
|
+
model,
|
|
1574
|
+
presetName: preset.id || preset.name || null,
|
|
1575
|
+
effort: preset.effort || null,
|
|
1576
|
+
fast: preset.fast === true,
|
|
1577
|
+
createdAt: Date.now(),
|
|
1578
|
+
updatedAt: Date.now(),
|
|
1579
|
+
status: 'idle',
|
|
1580
|
+
stage: 'idle',
|
|
1581
|
+
});
|
|
1582
|
+
void prompt;
|
|
1583
|
+
return { session: descriptor, effectiveCwd: descriptor.cwd };
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
async function runAgentTurn({
|
|
1587
|
+
session,
|
|
1588
|
+
prompt,
|
|
1589
|
+
context = null,
|
|
1590
|
+
onToolResult,
|
|
1591
|
+
onTerminalResult,
|
|
1592
|
+
} = {}) {
|
|
1593
|
+
await rehydrateAgentSessions();
|
|
1594
|
+
const sessionId = String(session?.id || session || '').trim();
|
|
1595
|
+
const descriptor = agentSessions.get(sessionId);
|
|
1596
|
+
if (!descriptor || descriptor.closed) {
|
|
1597
|
+
throw new Error(`agent session ${sessionId || '(empty)'} is closed`);
|
|
1598
|
+
}
|
|
1599
|
+
const entry = await entryForSession(sessionId, {
|
|
1600
|
+
cwd: descriptor.cwd,
|
|
1601
|
+
provider: descriptor.provider,
|
|
1602
|
+
model: descriptor.model,
|
|
1603
|
+
toolMode: 'full',
|
|
1604
|
+
});
|
|
1605
|
+
const target = entry.runtime.submitAndWait;
|
|
1606
|
+
if (typeof target !== 'function') {
|
|
1607
|
+
throw new TypeError('session runtime must implement submitAndWait');
|
|
1608
|
+
}
|
|
1609
|
+
descriptor.status = 'running';
|
|
1610
|
+
descriptor.stage = 'running';
|
|
1611
|
+
descriptor.updatedAt = Date.now();
|
|
1612
|
+
try {
|
|
1613
|
+
const options = {
|
|
1614
|
+
id: `agent-turn-${randomUUID()}`,
|
|
1615
|
+
mode: 'prompt',
|
|
1616
|
+
priority: 'next',
|
|
1617
|
+
context,
|
|
1618
|
+
transcriptMeta: { sender: 'lead' },
|
|
1619
|
+
...(entry.runtime.isWireSafe === true || typeof onToolResult !== 'function'
|
|
1620
|
+
? {}
|
|
1621
|
+
: { onToolResult }),
|
|
1622
|
+
};
|
|
1623
|
+
const detail = await target.call(entry.runtime, String(prompt || ''), options);
|
|
1624
|
+
if (detail?.status === 'failed') {
|
|
1625
|
+
throw new Error(String(detail.error || 'agent session turn failed'));
|
|
1626
|
+
}
|
|
1627
|
+
if (detail?.status === 'cancelled') {
|
|
1628
|
+
throw new Error('agent session turn cancelled');
|
|
1629
|
+
}
|
|
1630
|
+
const result = detail?.result || { content: '' };
|
|
1631
|
+
descriptor.status = 'idle';
|
|
1632
|
+
descriptor.stage = 'idle';
|
|
1633
|
+
descriptor.lastHandoff = typeof result?.content === 'string' ? result.content : '';
|
|
1634
|
+
try { onTerminalResult?.(result); } catch {}
|
|
1635
|
+
return result;
|
|
1636
|
+
} catch (error) {
|
|
1637
|
+
descriptor.status = /cancel/i.test(String(error?.message || '')) ? 'cancelled' : 'error';
|
|
1638
|
+
descriptor.stage = descriptor.status;
|
|
1639
|
+
throw error;
|
|
1640
|
+
} finally {
|
|
1641
|
+
descriptor.updatedAt = Date.now();
|
|
1642
|
+
retainUnwatched(entry, 'agent child idle');
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
function cancelAgentTree(sessionId, reason = 'agent session cancelled') {
|
|
1647
|
+
const id = String(sessionId || '').trim();
|
|
1648
|
+
if (!id) return Promise.resolve(false);
|
|
1649
|
+
const active = agentCancelRuns.get(id);
|
|
1650
|
+
if (active) return active;
|
|
1651
|
+
let run;
|
|
1652
|
+
run = (async () => {
|
|
1653
|
+
await rehydrateAgentSessions();
|
|
1654
|
+
cancelBackgroundTasks({
|
|
1655
|
+
surface: 'agent',
|
|
1656
|
+
callerSessionId: id,
|
|
1657
|
+
reason,
|
|
1658
|
+
});
|
|
1659
|
+
const children = [...(agentChildren.get(id) || [])];
|
|
1660
|
+
await Promise.all(children.map((childId) => cancelAgentTree(childId, reason)));
|
|
1661
|
+
const descriptor = agentSessions.get(id);
|
|
1662
|
+
if (!descriptor) return children.length > 0;
|
|
1663
|
+
if (descriptor.closed) return true;
|
|
1664
|
+
let entry = sessionOwner(id);
|
|
1665
|
+
if (!entry) {
|
|
1666
|
+
try {
|
|
1667
|
+
entry = await entryForSession(id, {
|
|
1668
|
+
cwd: descriptor.cwd,
|
|
1669
|
+
provider: descriptor.provider,
|
|
1670
|
+
model: descriptor.model,
|
|
1671
|
+
toolMode: 'full',
|
|
1672
|
+
});
|
|
1673
|
+
} catch (error) {
|
|
1674
|
+
log(`agent cancel load failed session=${id}: ${error?.message || error}`);
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
const closeCanonical = entry?.runtime?.closeCanonicalSession;
|
|
1678
|
+
if (typeof closeCanonical !== 'function') {
|
|
1679
|
+
throw new TypeError('session runtime must implement closeCanonicalSession');
|
|
1680
|
+
}
|
|
1681
|
+
const closed = await closeCanonical.call(entry.runtime, reason);
|
|
1682
|
+
if (closed !== true) throw new Error(`agent session ${id} could not be durably closed`);
|
|
1683
|
+
descriptor.closed = true;
|
|
1684
|
+
descriptor.status = 'closed';
|
|
1685
|
+
descriptor.stage = 'closed';
|
|
1686
|
+
descriptor.updatedAt = Date.now();
|
|
1687
|
+
return true;
|
|
1688
|
+
})().finally(() => {
|
|
1689
|
+
if (agentCancelRuns.get(id) === run) agentCancelRuns.delete(id);
|
|
1690
|
+
});
|
|
1691
|
+
agentCancelRuns.set(id, run);
|
|
1692
|
+
return run;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
async function cancelAgentDescendants(parentSessionId, reason = 'parent session cancelled') {
|
|
1696
|
+
await rehydrateAgentSessions();
|
|
1697
|
+
const parentId = String(parentSessionId || '');
|
|
1698
|
+
cancelBackgroundTasks({
|
|
1699
|
+
surface: 'agent',
|
|
1700
|
+
callerSessionId: parentId,
|
|
1701
|
+
reason,
|
|
1702
|
+
});
|
|
1703
|
+
const children = [...(agentChildren.get(parentId) || [])];
|
|
1704
|
+
if (!children.length) return false;
|
|
1705
|
+
await Promise.all(children.map((childId) => cancelAgentTree(childId, reason)));
|
|
1706
|
+
return true;
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
function agentDescendantSessionIds(parentSessionId) {
|
|
1710
|
+
const descendants = [];
|
|
1711
|
+
const seen = new Set();
|
|
1712
|
+
const visit = (parentId) => {
|
|
1713
|
+
for (const childId of agentChildren.get(String(parentId || '')) || []) {
|
|
1714
|
+
if (seen.has(childId)) continue;
|
|
1715
|
+
seen.add(childId);
|
|
1716
|
+
descendants.push(childId);
|
|
1717
|
+
visit(childId);
|
|
1718
|
+
}
|
|
1719
|
+
};
|
|
1720
|
+
visit(parentSessionId);
|
|
1721
|
+
return descendants;
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
const agentSurface = Object.freeze({
|
|
1725
|
+
canonical: true,
|
|
1726
|
+
canRun: (session) => Boolean(agentSessions.get(String(session?.id || ''))),
|
|
1727
|
+
createChild: createAgentChild,
|
|
1728
|
+
runTurn: runAgentTurn,
|
|
1729
|
+
});
|
|
1730
|
+
|
|
1731
|
+
const agentManager = Object.freeze({
|
|
1732
|
+
rehydrateAgentSessions,
|
|
1733
|
+
descendantSessionIds: agentDescendantSessionIds,
|
|
1734
|
+
getSession: (sessionId) => agentDescriptor(sessionId),
|
|
1735
|
+
listSessions: ({ includeClosed = false } = {}) => [...agentSessions.keys()]
|
|
1736
|
+
.map(agentDescriptor)
|
|
1737
|
+
.filter((session) => session && (includeClosed || session.closed !== true)),
|
|
1738
|
+
getSessionRuntime: (sessionId) => {
|
|
1739
|
+
const session = agentDescriptor(sessionId);
|
|
1740
|
+
return session ? { stage: session.stage || session.status || 'idle' } : null;
|
|
1741
|
+
},
|
|
1742
|
+
async readSessionHandoff(sessionId) {
|
|
1743
|
+
const id = String(sessionId || '').trim();
|
|
1744
|
+
const descriptor = agentSessions.get(id);
|
|
1745
|
+
if (!descriptor) return '';
|
|
1746
|
+
if (typeof descriptor.lastHandoff === 'string' && descriptor.lastHandoff.trim()) {
|
|
1747
|
+
return descriptor.lastHandoff;
|
|
1748
|
+
}
|
|
1749
|
+
if (typeof readStoredSession !== 'function') return '';
|
|
1750
|
+
const stored = await readStoredSession(id, { includeMessages: true });
|
|
1751
|
+
const handoff = lastStoredAgentHandoff(stored);
|
|
1752
|
+
if (handoff) descriptor.lastHandoff = handoff;
|
|
1753
|
+
return handoff;
|
|
1754
|
+
},
|
|
1755
|
+
async closeSession(sessionId, reason = 'agent session closed') {
|
|
1756
|
+
await rehydrateAgentSessions();
|
|
1757
|
+
return cancelAgentTree(sessionId, reason);
|
|
1758
|
+
},
|
|
1759
|
+
unloadSessionRuntime: () => false,
|
|
1760
|
+
hideSessionFromList: () => false,
|
|
1761
|
+
});
|
|
1762
|
+
|
|
1142
1763
|
async function approveSession({
|
|
1143
1764
|
sessionId, approvalId, decision, open: openHints = {}, baseRevision = null,
|
|
1144
1765
|
} = {}) {
|
|
@@ -1355,6 +1976,8 @@ export function createSessionService({
|
|
|
1355
1976
|
closed = true;
|
|
1356
1977
|
try { unsubscribeExternalSessionStates(); } catch {}
|
|
1357
1978
|
externalViewEntries.clear();
|
|
1979
|
+
agentSessions.clear();
|
|
1980
|
+
agentChildren.clear();
|
|
1358
1981
|
if (evictTimer) { clearInterval(evictTimer); evictTimer = null; }
|
|
1359
1982
|
const services = [...new Set(desktopServicesById.values())];
|
|
1360
1983
|
desktopServicesById.clear();
|
|
@@ -1372,8 +1995,10 @@ export function createSessionService({
|
|
|
1372
1995
|
|
|
1373
1996
|
return {
|
|
1374
1997
|
handleCall, listSessionCatalog, createSession, readSession, subscribeSession, unsubscribeSession,
|
|
1375
|
-
submitSession, materializeSession, abortSession, approveSession,
|
|
1998
|
+
submitSession, materializeSession, recoverActiveGoals, abortSession, approveSession,
|
|
1376
1999
|
configureSession, stop, releaseClient,
|
|
2000
|
+
agentSurface, agentManager, agentDescriptor, rootOwnerSessionId, rehydrateAgentSessions,
|
|
2001
|
+
cancelAgentTree, cancelAgentDescendants,
|
|
1377
2002
|
get size() { return sessions.size; },
|
|
1378
2003
|
/** Live work the daemon must not abandon (self-shutdown guard). */
|
|
1379
2004
|
get busyCount() { return liveBusyCount(); },
|