@oyasmi/pipiclaw 0.9.5-beta.2 → 0.9.5-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +9 -11
- package/README.md +9 -11
- package/dist/agent/channel-runner.d.ts +12 -24
- package/dist/agent/channel-runner.js +74 -30
- package/dist/agent/maintenance-context.d.ts +2 -0
- package/dist/agent/maintenance-context.js +15 -0
- package/dist/agent/prompt/sections.js +8 -6
- package/dist/agent/prompt/types.d.ts +1 -1
- package/dist/agent/runner-factory.js +1 -1
- package/dist/agent/session/command-extension.js +11 -0
- package/dist/agent/session/compaction-summary.js +6 -2
- package/dist/agent/session/execution-trace.d.ts +14 -0
- package/dist/agent/session/execution-trace.js +57 -0
- package/dist/agent/session/session-events.d.ts +2 -7
- package/dist/agent/session/session-events.js +35 -12
- package/dist/agent/session/session-host.d.ts +12 -22
- package/dist/agent/session/session-host.js +46 -65
- package/dist/agent/turn/task-digest.d.ts +1 -2
- package/dist/agent/turn/task-digest.js +33 -33
- package/dist/agent/turn/turn-context.d.ts +1 -1
- package/dist/agent/turn/turn-context.js +1 -1
- package/dist/agent/turn/turn-finish.d.ts +2 -2
- package/dist/agent/turn/turn-finish.js +3 -2
- package/dist/agent/turn/turn-pipeline.d.ts +4 -2
- package/dist/agent/turn/turn-pipeline.js +33 -5
- package/dist/agent/types.d.ts +64 -14
- package/dist/agent/types.js +3 -0
- package/dist/app/home.js +3 -4
- package/dist/channel/channel-event.d.ts +4 -12
- package/dist/channel/ingress.d.ts +1 -9
- package/dist/channel/store.d.ts +1 -1
- package/dist/commands/catalog.js +25 -19
- package/dist/commands/router.d.ts +5 -2
- package/dist/commands/router.js +9 -4
- package/dist/config/settings.d.ts +5 -12
- package/dist/config/settings.js +0 -7
- package/dist/config/tools.d.ts +2 -2
- package/dist/config/tools.js +2 -2
- package/dist/dingtalk/bot.d.ts +2 -0
- package/dist/dingtalk/bot.js +4 -0
- package/dist/dingtalk/cards.d.ts +7 -0
- package/dist/dingtalk/cards.js +32 -2
- package/dist/dingtalk/delivery.d.ts +4 -1
- package/dist/dingtalk/delivery.js +104 -191
- package/dist/dingtalk/progress.d.ts +41 -6
- package/dist/dingtalk/progress.js +173 -16
- package/dist/file-store.d.ts +4 -0
- package/dist/file-store.js +6 -1
- package/dist/jobs/completion-observer.d.ts +20 -0
- package/dist/jobs/completion-observer.js +107 -0
- package/dist/jobs/completion-signal.d.ts +9 -0
- package/dist/jobs/completion-signal.js +32 -0
- package/dist/jobs/completion-wake.d.ts +1 -1
- package/dist/jobs/completion-wake.js +2 -13
- package/dist/jobs/manager.d.ts +11 -29
- package/dist/jobs/manager.js +103 -176
- package/dist/jobs/probe.d.ts +3 -0
- package/dist/jobs/probe.js +42 -0
- package/dist/jobs/record.d.ts +3 -6
- package/dist/jobs/runtime.d.ts +28 -0
- package/dist/jobs/runtime.js +69 -0
- package/dist/memory/boundary-work.d.ts +2 -0
- package/dist/memory/boundary-work.js +79 -37
- package/dist/memory/commands.js +4 -1
- package/dist/memory/index-budget.js +18 -15
- package/dist/memory/journal.js +2 -1
- package/dist/memory/lifecycle.d.ts +4 -8
- package/dist/memory/lifecycle.js +21 -51
- package/dist/memory/reflect-job.d.ts +0 -6
- package/dist/memory/reflect-job.js +13 -34
- package/dist/memory/reflect.d.ts +4 -0
- package/dist/memory/reflect.js +32 -5
- package/dist/memory/review-log.d.ts +13 -0
- package/dist/memory/review-log.js +49 -0
- package/dist/memory/search.js +7 -4
- package/dist/memory/source-window.d.ts +18 -1
- package/dist/memory/source-window.js +59 -9
- package/dist/memory/store.d.ts +2 -0
- package/dist/memory/store.js +45 -13
- package/dist/playbooks/agent-delegation.md +10 -4
- package/dist/playbooks/background-jobs.md +4 -4
- package/dist/playbooks/event-scheduling.md +2 -2
- package/dist/playbooks/memory-and-learning.md +3 -3
- package/dist/playbooks/outbound-media.md +2 -2
- package/dist/playbooks/runtime-orientation.md +10 -6
- package/dist/playbooks/task-lead.md +54 -33
- package/dist/runtime/bootstrap.d.ts +11 -24
- package/dist/runtime/bootstrap.js +92 -59
- package/dist/runtime/channel-queue.d.ts +2 -0
- package/dist/runtime/channel-queue.js +5 -1
- package/dist/runtime/durable-dispatch.d.ts +7 -0
- package/dist/runtime/durable-dispatch.js +66 -7
- package/dist/runtime/ingress-busy.d.ts +1 -2
- package/dist/runtime/ingress-busy.js +2 -35
- package/dist/runtime/ingress-commands.d.ts +1 -1
- package/dist/runtime/ingress-commands.js +28 -20
- package/dist/runtime/ingress-deps.d.ts +4 -7
- package/dist/runtime/ingress.js +23 -13
- package/dist/runtime/shutdown.d.ts +1 -1
- package/dist/runtime/shutdown.js +2 -2
- package/dist/runtime/task-close.d.ts +4 -0
- package/dist/runtime/task-close.js +102 -0
- package/dist/runtime/task-control.d.ts +34 -0
- package/dist/runtime/task-control.js +225 -0
- package/dist/runtime/task-input.d.ts +13 -0
- package/dist/runtime/task-input.js +129 -0
- package/dist/runtime/task-lane-types.d.ts +123 -0
- package/dist/runtime/task-lane-types.js +14 -0
- package/dist/runtime/task-lane.d.ts +99 -0
- package/dist/runtime/task-lane.js +703 -0
- package/dist/runtime/task-pending.d.ts +8 -0
- package/dist/runtime/task-pending.js +40 -0
- package/dist/runtime/task-spawn.d.ts +4 -4
- package/dist/runtime/task-spawn.js +4 -4
- package/dist/runtime/task-step.d.ts +32 -0
- package/dist/runtime/task-step.js +276 -0
- package/dist/runtime/turn-run.d.ts +4 -16
- package/dist/runtime/turn-run.js +17 -155
- package/dist/runtime/wake-coalescing.d.ts +1 -3
- package/dist/runtime/wake-coalescing.js +2 -4
- package/dist/shared/format.d.ts +0 -6
- package/dist/shared/format.js +0 -17
- package/dist/shared/host-process.d.ts +2 -0
- package/dist/shared/host-process.js +16 -1
- package/dist/shared/jsonl-appender.d.ts +5 -0
- package/dist/shared/jsonl-appender.js +25 -2
- package/dist/skills/catalog.d.ts +9 -0
- package/dist/skills/catalog.js +34 -0
- package/dist/skills/commands.d.ts +3 -12
- package/dist/skills/commands.js +36 -22
- package/dist/skills/loader.d.ts +2 -31
- package/dist/skills/loader.js +101 -68
- package/dist/skills/roots.d.ts +12 -0
- package/dist/skills/roots.js +18 -0
- package/dist/skills/types.d.ts +24 -0
- package/dist/skills/types.js +1 -0
- package/dist/subagents/completion-wake.js +0 -10
- package/dist/subagents/discovery.d.ts +1 -1
- package/dist/subagents/discovery.js +10 -16
- package/dist/subagents/dispatch.js +8 -16
- package/dist/subagents/external/cursor-cli.d.ts +2 -0
- package/dist/subagents/external/cursor-cli.js +256 -0
- package/dist/subagents/external/harness.d.ts +5 -5
- package/dist/subagents/external/reconciliation.d.ts +7 -0
- package/dist/subagents/external/reconciliation.js +105 -0
- package/dist/subagents/external/recovery.d.ts +20 -0
- package/dist/subagents/external/recovery.js +71 -0
- package/dist/subagents/external/registry.js +2 -0
- package/dist/subagents/external/role-warnings.d.ts +17 -0
- package/dist/subagents/external/role-warnings.js +42 -0
- package/dist/subagents/external/run.js +18 -22
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/grace.d.ts +2 -2
- package/dist/subagents/grace.js +3 -3
- package/dist/subagents/internal/run.js +7 -5
- package/dist/subagents/internal/worker.js +31 -2
- package/dist/subagents/result.js +2 -3
- package/dist/subagents/run-record.d.ts +4 -7
- package/dist/subagents/run-record.js +2 -2
- package/dist/subagents/runs.d.ts +33 -46
- package/dist/subagents/runs.js +126 -245
- package/dist/subagents/task-settlement.d.ts +3 -0
- package/dist/subagents/task-settlement.js +20 -0
- package/dist/subagents/types.d.ts +1 -2
- package/dist/subagents/workspace-summary.js +5 -1
- package/dist/tasks/archive.d.ts +11 -0
- package/dist/tasks/archive.js +141 -0
- package/dist/tasks/board.d.ts +5 -12
- package/dist/tasks/board.js +4 -30
- package/dist/tasks/bound-work.d.ts +12 -6
- package/dist/tasks/bound-work.js +37 -24
- package/dist/tasks/brief.d.ts +38 -14
- package/dist/tasks/brief.js +128 -70
- package/dist/tasks/budget.d.ts +3 -3
- package/dist/tasks/budget.js +9 -4
- package/dist/tasks/commands.d.ts +14 -9
- package/dist/tasks/commands.js +190 -121
- package/dist/tasks/completion.d.ts +12 -0
- package/dist/tasks/completion.js +35 -0
- package/dist/tasks/contract.d.ts +8 -17
- package/dist/tasks/contract.js +41 -43
- package/dist/tasks/control.d.ts +138 -0
- package/dist/tasks/control.js +1 -0
- package/dist/tasks/create.d.ts +1 -1
- package/dist/tasks/create.js +3 -3
- package/dist/tasks/delivery.d.ts +24 -0
- package/dist/tasks/delivery.js +221 -0
- package/dist/tasks/file-mutation.d.ts +5 -0
- package/dist/tasks/file-mutation.js +38 -0
- package/dist/tasks/frontmatter.d.ts +43 -28
- package/dist/tasks/frontmatter.js +83 -40
- package/dist/tasks/input-receipts.d.ts +7 -0
- package/dist/tasks/input-receipts.js +60 -0
- package/dist/tasks/loop-log.d.ts +147 -28
- package/dist/tasks/loop-log.js +239 -28
- package/dist/tasks/receipts.d.ts +19 -0
- package/dist/tasks/receipts.js +48 -0
- package/dist/tasks/state.d.ts +93 -0
- package/dist/tasks/state.js +156 -0
- package/dist/tasks/store.d.ts +81 -49
- package/dist/tasks/store.js +244 -147
- package/dist/tools/bash.d.ts +3 -5
- package/dist/tools/bash.js +8 -22
- package/dist/tools/contract/details.d.ts +6 -6
- package/dist/tools/contract/details.js +6 -6
- package/dist/tools/contract/execution-trace.d.ts +4 -0
- package/dist/tools/contract/execution-trace.js +23 -0
- package/dist/tools/contract/presentation.js +6 -2
- package/dist/tools/edit.d.ts +1 -0
- package/dist/tools/edit.js +45 -42
- package/dist/tools/index.d.ts +8 -12
- package/dist/tools/index.js +16 -6
- package/dist/tools/memory-manage.js +8 -1
- package/dist/tools/registry.d.ts +7 -11
- package/dist/tools/registry.js +52 -25
- package/dist/tools/skill.d.ts +2 -2
- package/dist/tools/skill.js +24 -10
- package/dist/tools/subagent-manage.js +7 -4
- package/dist/tools/subagent.d.ts +0 -2
- package/dist/tools/subagent.js +2 -7
- package/dist/tools/task-manage/control-tools.d.ts +4 -0
- package/dist/tools/task-manage/control-tools.js +65 -0
- package/dist/tools/task-manage/create.js +2 -1
- package/dist/tools/task-manage/factory.d.ts +12 -6
- package/dist/tools/task-manage/factory.js +72 -57
- package/dist/tools/task-manage/lifecycle.d.ts +7 -12
- package/dist/tools/task-manage/lifecycle.js +118 -66
- package/dist/tools/task-manage/log-view.js +8 -5
- package/dist/tools/task-manage/schema.d.ts +21 -26
- package/dist/tools/task-manage/schema.js +29 -35
- package/dist/tools/task-manage/shared.d.ts +8 -39
- package/dist/tools/task-manage/shared.js +19 -68
- package/dist/tools/task-manage/step-tools.d.ts +5 -0
- package/dist/tools/task-manage/step-tools.js +103 -0
- package/dist/tools/task-manage/types.d.ts +25 -18
- package/dist/tools/write-content.d.ts +1 -0
- package/dist/tools/write-content.js +6 -1
- package/dist/tools/write.d.ts +1 -0
- package/dist/tools/write.js +1 -0
- package/dist/usage/ledger.d.ts +4 -4
- package/dist/usage/render.js +2 -2
- package/dist/web/search-providers.js +0 -33
- package/dist/webapi/request-policy.js +1 -1
- package/examples/sub-agents/README.md +23 -16
- package/examples/sub-agents/agents/claude-high.md +1 -1
- package/examples/sub-agents/agents/claude-main.md +1 -1
- package/examples/sub-agents/agents/codex-flash.md +1 -1
- package/examples/sub-agents/agents/codex-high.md +1 -1
- package/examples/sub-agents/agents/codex-main.md +4 -4
- package/examples/sub-agents/agents/cursor-high.md +22 -0
- package/examples/sub-agents/agents/cursor-main.md +22 -0
- package/examples/sub-agents/agents/glm-flash.md +1 -1
- package/examples/sub-agents/agents/glm-high.md +2 -2
- package/examples/sub-agents/agents/scout.md +30 -0
- package/package.json +6 -6
- package/dist/runtime/task-driver.d.ts +0 -116
- package/dist/runtime/task-driver.js +0 -443
- package/dist/runtime/task-wake.d.ts +0 -48
- package/dist/runtime/task-wake.js +0 -32
- package/dist/shared/wake-claim.d.ts +0 -23
- package/dist/shared/wake-claim.js +0 -26
- package/dist/skills/validation.d.ts +0 -8
- package/dist/skills/validation.js +0 -91
- package/dist/skills/workspace.d.ts +0 -33
- package/dist/skills/workspace.js +0 -116
- package/dist/tasks/steer.d.ts +0 -8
- package/dist/tasks/steer.js +0 -81
- package/dist/tasks/ticket.d.ts +0 -71
- package/dist/tasks/ticket.js +0 -138
- package/dist/tools/task-manage/step-end.d.ts +0 -11
- package/dist/tools/task-manage/step-end.js +0 -117
package/dist/subagents/runs.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { readdir, readFile, rename, rmdir,
|
|
1
|
+
import { readdir, readFile, rename, rmdir, unlink } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import * as log from "../log.js";
|
|
4
4
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
5
|
-
import { isProcessAlive, killProcessGroup
|
|
5
|
+
import { isProcessAlive, killProcessGroup } from "../shared/host-process.js";
|
|
6
6
|
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
7
7
|
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
8
8
|
import { errorMessage } from "../shared/text-utils.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { redeemTicket } from "../tasks/store.js";
|
|
9
|
+
import { logTaskDispatch, logTaskStall } from "../tasks/bound-work.js";
|
|
10
|
+
import { readStoredTask } from "../tasks/store.js";
|
|
12
11
|
import { createEmptyUsageTotals } from "../usage/totals.js";
|
|
13
|
-
import { buildRunCompletionWake, countWakeSiblings
|
|
12
|
+
import { buildRunCompletionWake, countWakeSiblings } from "./completion-wake.js";
|
|
14
13
|
import { createProgressPacer, startExternalProgressTail } from "./external/progress-tail.js";
|
|
15
|
-
import {
|
|
14
|
+
import { processIdentity, reconcileExternalRun } from "./external/reconciliation.js";
|
|
15
|
+
import { AdoptedRunObserver } from "./external/recovery.js";
|
|
16
16
|
import { runArchiveEntry, runLedgerEntry } from "./run-archive.js";
|
|
17
17
|
import { applyPendingSettlement, createPendingSettlement, isTerminal, MAX_RUNNING_SUBAGENT_RUNS_PER_DESK, MAX_RUNNING_SUBAGENT_RUNS_PER_HOST, normalizeRunIdQuery, parseRunRecord, RUN_ARTIFACT_FILENAMES, RUN_ID_PREFIX, RUN_RETENTION_MS, randomRunIdSuffix, } from "./run-record.js";
|
|
18
|
+
import { creditTaskForRun } from "./task-settlement.js";
|
|
18
19
|
import { acquireWorkspaceLease, formatWorkspaceLeaseConflict, releaseWorkspaceLease } from "./workspace-lease.js";
|
|
19
20
|
/** Registration is serialized across channel managers so the host-wide count and insertion are
|
|
20
21
|
* one admission transaction. The per-run manager queue alone cannot protect the 19 -> 20 edge
|
|
@@ -48,9 +49,7 @@ export class SubAgentRunManager {
|
|
|
48
49
|
/** External launch intents exist before a safe live child handle does. A cancel in that window
|
|
49
50
|
* is remembered here and consumed before spawn, rather than spawning a process nothing can cancel. */
|
|
50
51
|
this.externalLaunches = new Map();
|
|
51
|
-
|
|
52
|
-
* daemon spawned itself, an adopted process cannot emit a `close` event to this process. */
|
|
53
|
-
this.externalRecoveryTimers = new Map();
|
|
52
|
+
this.recoveryQueue = createSerialQueue();
|
|
54
53
|
/** Live external runs' tail-the-artifact-file progress pollers (P1a). Never persisted or
|
|
55
54
|
* reconstructed on restore — an adopted run has no fresher information to poll for than
|
|
56
55
|
* restart reconciliation already reads once from the finished process's own output. */
|
|
@@ -60,16 +59,23 @@ export class SubAgentRunManager {
|
|
|
60
59
|
* external tail does. Created on first report, dropped at settlement. */
|
|
61
60
|
this.internalProgressPacers = new Map();
|
|
62
61
|
this.queue = createSerialQueue();
|
|
62
|
+
this.recovery = new AdoptedRunObserver((id) => this.runs.get(id), async (id) => {
|
|
63
|
+
const record = this.runs.get(id);
|
|
64
|
+
if (record)
|
|
65
|
+
await this.reconcileExternalRunAtDeadline(record);
|
|
66
|
+
}, (runId, error) => log.logWarn("subagent.reconcile.failed", "Adopted external run reconcile failed", {
|
|
67
|
+
reason: errorMessage(error),
|
|
68
|
+
fields: { runId, desk: this.target.desk },
|
|
69
|
+
}), options.adoptedCheckIntervalMs);
|
|
63
70
|
}
|
|
64
|
-
/** The completion wake's dispatch id
|
|
71
|
+
/** The completion wake's dispatch id (a chat-level run; a task-bound run never wakes a channel). */
|
|
65
72
|
wakeDispatchId(runId) {
|
|
66
73
|
return `subagent:${this.target.desk}:${runId}:done`;
|
|
67
74
|
}
|
|
68
75
|
/** Release observation timers without cancelling dispatched work or its settlement. */
|
|
69
76
|
stop() {
|
|
70
77
|
this.stopped = true;
|
|
71
|
-
|
|
72
|
-
this.clearExternalRecoveryTimer(runId);
|
|
78
|
+
this.recovery.stop();
|
|
73
79
|
for (const runId of this.externalProgressTails.keys())
|
|
74
80
|
this.stopExternalProgressTail(runId);
|
|
75
81
|
}
|
|
@@ -160,10 +166,29 @@ export class SubAgentRunManager {
|
|
|
160
166
|
durationMs: Date.now() - (record.processStartedAt ?? record.startedAt),
|
|
161
167
|
silentMs,
|
|
162
168
|
});
|
|
169
|
+
this.noteTaskStall(record, silentMs);
|
|
163
170
|
},
|
|
164
171
|
});
|
|
165
172
|
this.externalProgressTails.set(record.runId, tail);
|
|
166
173
|
}
|
|
174
|
+
/** Tell a task-bound run's task that the run has gone quiet: it is the leader's call what to do (spec 064, D16). */
|
|
175
|
+
noteTaskStall(record, silentMs) {
|
|
176
|
+
const taskId = record.taskId;
|
|
177
|
+
if (!taskId || this.stopped)
|
|
178
|
+
return;
|
|
179
|
+
void logTaskStall(this.target.deskDir, taskId, {
|
|
180
|
+
ref: record.runId,
|
|
181
|
+
silentMinutes: Math.max(1, Math.round(silentMs / 60_000)),
|
|
182
|
+
})
|
|
183
|
+
.then((logged) => {
|
|
184
|
+
if (logged)
|
|
185
|
+
this.options.onTaskInput?.(this.target.desk, taskId);
|
|
186
|
+
})
|
|
187
|
+
.catch((error) => log.logWarn("subagent.task_stall.log_failed", "Task stall record for run failed", {
|
|
188
|
+
reason: errorMessage(error),
|
|
189
|
+
fields: { runId: record.runId, taskId },
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
167
192
|
/**
|
|
168
193
|
* A step label from an internal run that has outlived its sync grace window: nobody is watching
|
|
169
194
|
* the tool call any more, so without this the run would be silent until it settles. Paced by the
|
|
@@ -211,7 +236,7 @@ export class SubAgentRunManager {
|
|
|
211
236
|
this.cancelHandles.delete(record.runId);
|
|
212
237
|
this.externalLaunches.delete(record.runId);
|
|
213
238
|
this.internalProgressPacers.delete(record.runId);
|
|
214
|
-
this.
|
|
239
|
+
this.recovery.remove(record.runId);
|
|
215
240
|
}
|
|
216
241
|
get(runId) {
|
|
217
242
|
return this.runs.get(runId);
|
|
@@ -353,10 +378,12 @@ export class SubAgentRunManager {
|
|
|
353
378
|
}
|
|
354
379
|
/** Persist best-effort probe results after the pid/launch recovery record is already durable.
|
|
355
380
|
* A slow or unavailable `ps`/`--version` probe must never reopen the post-spawn recovery window. */
|
|
356
|
-
async updateLaunchMetadata(runId, info) {
|
|
381
|
+
async updateLaunchMetadata(runId, info, launch) {
|
|
357
382
|
await this.queue.run(runId, async () => {
|
|
358
383
|
const record = this.runs.get(runId);
|
|
359
|
-
if (
|
|
384
|
+
if (this.stopped || !record || !record.pid)
|
|
385
|
+
return;
|
|
386
|
+
if (launch && (record.pid !== launch.pid || record.processStartedAt !== launch.processStartedAt))
|
|
360
387
|
return;
|
|
361
388
|
if (info.pidStartedAt !== undefined)
|
|
362
389
|
record.pidStartedAt = info.pidStartedAt;
|
|
@@ -365,42 +392,6 @@ export class SubAgentRunManager {
|
|
|
365
392
|
await this.persist(record);
|
|
366
393
|
});
|
|
367
394
|
}
|
|
368
|
-
/** Reserve a task wake exactly once. A replay of the same durable dispatch may resume an
|
|
369
|
-
* interrupted activation, while copied text or a different dispatch id can never claim it. */
|
|
370
|
-
async beginWakeConsumption(runId, taskId, dispatchId) {
|
|
371
|
-
return this.queue.run(runId, async () => {
|
|
372
|
-
const record = this.runs.get(runId);
|
|
373
|
-
if (!record)
|
|
374
|
-
return false;
|
|
375
|
-
const expected = this.wakeDispatchId(runId);
|
|
376
|
-
const eligible = record.settledAt !== undefined && record.taskId === taskId && dispatchId === expected;
|
|
377
|
-
const previousClaim = record.wakeClaimDispatchId;
|
|
378
|
-
if (!beginWakeClaim(record, eligible, dispatchId))
|
|
379
|
-
return false;
|
|
380
|
-
try {
|
|
381
|
-
await this.persist(record, true);
|
|
382
|
-
}
|
|
383
|
-
catch (error) {
|
|
384
|
-
record.wakeClaimDispatchId = previousClaim;
|
|
385
|
-
throw error;
|
|
386
|
-
}
|
|
387
|
-
return true;
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
async finishWakeConsumption(runId, dispatchId) {
|
|
391
|
-
await this.queue.run(runId, async () => {
|
|
392
|
-
const record = this.runs.get(runId);
|
|
393
|
-
if (!record || !finishWakeClaim(record, dispatchId))
|
|
394
|
-
return;
|
|
395
|
-
try {
|
|
396
|
-
await this.persist(record, true);
|
|
397
|
-
}
|
|
398
|
-
catch (error) {
|
|
399
|
-
record.wakeConsumedAt = undefined;
|
|
400
|
-
throw error;
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
395
|
/** Let a caller (subagent tool) register a way to abort this run's in-process worker. */
|
|
405
396
|
registerCancelHandle(runId, cancel) {
|
|
406
397
|
this.cancelHandles.set(runId, cancel);
|
|
@@ -408,18 +399,20 @@ export class SubAgentRunManager {
|
|
|
408
399
|
/** Record why a still-running external run is about to be killed, before the signal is sent
|
|
409
400
|
* (P1-1) — see the `terminationReason` field doc. A no-op once the run is no longer running.
|
|
410
401
|
* Called by both the live in-process watcher (`external/run.ts`) and `sweep()` below. */
|
|
411
|
-
async markTerminationReason(runId, reason) {
|
|
402
|
+
async markTerminationReason(runId, reason, required = false) {
|
|
412
403
|
await this.queue.run(runId, async () => {
|
|
413
404
|
const record = this.runs.get(runId);
|
|
414
405
|
if (!record || record.status !== "running")
|
|
415
406
|
return;
|
|
407
|
+
const previous = record.terminationReason;
|
|
416
408
|
record.terminationReason = reason;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
409
|
+
try {
|
|
410
|
+
await this.persist(record, required);
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
record.terminationReason = previous;
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
423
416
|
});
|
|
424
417
|
}
|
|
425
418
|
clearCancelHandle(runId) {
|
|
@@ -464,7 +457,7 @@ export class SubAgentRunManager {
|
|
|
464
457
|
await this.persist(record);
|
|
465
458
|
this.cancelHandles.delete(runId);
|
|
466
459
|
this.externalLaunches.delete(runId);
|
|
467
|
-
this.
|
|
460
|
+
this.recovery.remove(runId);
|
|
468
461
|
this.stopExternalProgressTail(runId);
|
|
469
462
|
this.internalProgressPacers.delete(runId);
|
|
470
463
|
if (!record.usageRecorded) {
|
|
@@ -487,14 +480,18 @@ export class SubAgentRunManager {
|
|
|
487
480
|
// is still around by the time the leader looks. Guarded by its own idempotency marker
|
|
488
481
|
// inside the same settlement lock, so a replay writes nothing twice.
|
|
489
482
|
if (!record.taskAccounted && record.taskId) {
|
|
490
|
-
|
|
491
|
-
|
|
483
|
+
try {
|
|
484
|
+
await creditTaskForRun(this.target.deskDir, record, input, outputSaved, !options.announce);
|
|
485
|
+
record.taskAccounted = true;
|
|
486
|
+
await this.persist(record);
|
|
487
|
+
}
|
|
488
|
+
catch (error) {
|
|
492
489
|
log.logWarn("subagent.task_settle.log_failed", "Task credit for run failed", {
|
|
493
490
|
reason: errorMessage(error),
|
|
494
491
|
fields: { runId: record.runId, taskId: record.taskId },
|
|
495
492
|
});
|
|
496
|
-
|
|
497
|
-
|
|
493
|
+
return; // Keep the unacknowledged terminal record eligible for retry.
|
|
494
|
+
}
|
|
498
495
|
}
|
|
499
496
|
if (record.wakeEnqueued)
|
|
500
497
|
return;
|
|
@@ -504,28 +501,11 @@ export class SubAgentRunManager {
|
|
|
504
501
|
return;
|
|
505
502
|
}
|
|
506
503
|
await this.announce(record, input.outputText, outputSaved);
|
|
504
|
+
log.logInfoEvent("subagent.settlement.committed", "Run settlement committed", {
|
|
505
|
+
fields: { desk: this.target.desk, runId, taskId: record.taskId, durationMs: record.durationMs },
|
|
506
|
+
});
|
|
507
507
|
});
|
|
508
508
|
}
|
|
509
|
-
/**
|
|
510
|
-
* Credit one settled run to the task that owns it: cost onto its usage, outcome onto its log.
|
|
511
|
-
* Best-effort by design: a task whose file has since been archived or hand-deleted must not
|
|
512
|
-
* block the completion wake.
|
|
513
|
-
*/
|
|
514
|
-
async creditOwningTask(record, input, outputSaved) {
|
|
515
|
-
const taskId = record.taskId;
|
|
516
|
-
if (!taskId)
|
|
517
|
-
return;
|
|
518
|
-
await logTaskSettlement(this.target.deskDir, taskId, {
|
|
519
|
-
ref: record.runId,
|
|
520
|
-
item: record.item,
|
|
521
|
-
status: record.status,
|
|
522
|
-
verdict: record.purpose === "verify" ? record.verificationVerdict : undefined,
|
|
523
|
-
output: outputSaved ? join(record.artifactDir, "output.md") : undefined,
|
|
524
|
-
tail: input.outputText.slice(-WAKE_OUTPUT_TAIL_CHARS).trim() || undefined,
|
|
525
|
-
changed: record.workspaceSummary,
|
|
526
|
-
durationMs: input.durationMs,
|
|
527
|
-
}, { usd: input.usage.cost.total, estimated: input.costKnown === false });
|
|
528
|
-
}
|
|
529
509
|
/**
|
|
530
510
|
* Dispatch the completion wake. Durable-dispatch persists its own pending record before this
|
|
531
511
|
* call can be interrupted (`DurableDispatchService.dispatch`), and `dispatch()` is idempotent on
|
|
@@ -533,6 +513,11 @@ export class SubAgentRunManager {
|
|
|
533
513
|
* harmless — this is the same fix as jobs/manager's `announce()` reordering (D7).
|
|
534
514
|
*/
|
|
535
515
|
async announce(record, outputText, outputSaved) {
|
|
516
|
+
if (record.taskId && !record.taskAccounted) {
|
|
517
|
+
await creditTaskForRun(this.target.deskDir, record, { outputText, durationMs: record.durationMs ?? 0, usage: record.usage, costKnown: record.costKnown }, outputSaved, false);
|
|
518
|
+
record.taskAccounted = true;
|
|
519
|
+
await this.persist(record);
|
|
520
|
+
}
|
|
536
521
|
// P0-1: a settled notice fires whenever this run actually needs a wake — i.e. whenever the
|
|
537
522
|
// caller was not already holding the result in the same tool call (`announce: true`, the same
|
|
538
523
|
// condition that gates the wake below). It reaches the channel in roughly the time the run
|
|
@@ -544,6 +529,15 @@ export class SubAgentRunManager {
|
|
|
544
529
|
status: record.status,
|
|
545
530
|
durationMs: record.durationMs ?? 0,
|
|
546
531
|
});
|
|
532
|
+
// A task-bound run reports to its task, not to a channel (spec 064, D19): the settle record
|
|
533
|
+
// is already in the task's log, so the lane only needs to look. A task that has since vanished
|
|
534
|
+
// has nowhere to read the result, so the run falls back to waking the chat.
|
|
535
|
+
if (record.taskId && (await readStoredTask(this.target.deskDir, record.taskId))) {
|
|
536
|
+
this.options.onTaskInput?.(this.target.desk, record.taskId);
|
|
537
|
+
record.wakeEnqueued = true;
|
|
538
|
+
await this.persist(record);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
547
541
|
if (!this.options.dispatch)
|
|
548
542
|
return;
|
|
549
543
|
if (this.options.isBoundTo && !this.options.isBoundTo(record.channelId, this.target.desk)) {
|
|
@@ -579,24 +573,14 @@ export class SubAgentRunManager {
|
|
|
579
573
|
await this.persist(record);
|
|
580
574
|
}
|
|
581
575
|
/**
|
|
582
|
-
* Spec 058, D8: the run's channel left this desk, so the wake has no recipient. Settlement
|
|
583
|
-
*
|
|
584
|
-
* `work` ticket it would have redeemed is redeemed here via `redeemTicket`, so the suspended task
|
|
585
|
-
* resumes as soon as its delivery channel is bound again.
|
|
576
|
+
* Spec 058, D8: the run's channel left this desk, so the chat wake has no recipient. Settlement
|
|
577
|
+
* already happened; the wake is dropped (marker set, never replayed).
|
|
586
578
|
*/
|
|
587
579
|
async suppressWake(record) {
|
|
588
580
|
log.logInfoEvent("subagent.wake.suppressed", "Completion wake dropped: channel not bound to the run's desk", {
|
|
589
581
|
channelId: record.channelId,
|
|
590
|
-
fields: { runId: record.runId, desk: this.target.desk
|
|
582
|
+
fields: { runId: record.runId, desk: this.target.desk },
|
|
591
583
|
});
|
|
592
|
-
if (record.taskId) {
|
|
593
|
-
await redeemTicket(this.target.deskDir, record.taskId, (ticket) => ticket.kind === "work").catch((error) => {
|
|
594
|
-
log.logWarn("subagent.wake.redeem_failed", "Work ticket redeem for a dropped wake failed", {
|
|
595
|
-
reason: errorMessage(error),
|
|
596
|
-
fields: { runId: record.runId, taskId: record.taskId },
|
|
597
|
-
});
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
584
|
record.wakeEnqueued = true;
|
|
601
585
|
await this.persist(record);
|
|
602
586
|
}
|
|
@@ -637,7 +621,6 @@ export class SubAgentRunManager {
|
|
|
637
621
|
// waiting for its one-shot deadline check.
|
|
638
622
|
record.terminationReason = "cancelled";
|
|
639
623
|
await this.persist(record).catch(() => undefined);
|
|
640
|
-
await killProcessGroup(record.pid).catch(() => undefined);
|
|
641
624
|
adoptedRecord = record;
|
|
642
625
|
}
|
|
643
626
|
else {
|
|
@@ -656,8 +639,7 @@ export class SubAgentRunManager {
|
|
|
656
639
|
});
|
|
657
640
|
if (!adoptedRecord)
|
|
658
641
|
return status;
|
|
659
|
-
this.
|
|
660
|
-
await this.reconcileExternalRun(adoptedRecord).catch((error) => {
|
|
642
|
+
await this.reconcileExternalRunAtDeadline(adoptedRecord).catch((error) => {
|
|
661
643
|
log.logWarn("subagent.reconcile.failed", "Cancelled adopted run reconcile failed", {
|
|
662
644
|
reason: errorMessage(error),
|
|
663
645
|
fields: { runId },
|
|
@@ -750,6 +732,8 @@ export class SubAgentRunManager {
|
|
|
750
732
|
fields: { runId: record?.runId },
|
|
751
733
|
});
|
|
752
734
|
});
|
|
735
|
+
if (record.status === "running" && record.runtime === "external")
|
|
736
|
+
this.recovery.add(record.runId);
|
|
753
737
|
continue;
|
|
754
738
|
}
|
|
755
739
|
if (record.runtime === "internal" && record.status === "running") {
|
|
@@ -803,13 +787,13 @@ export class SubAgentRunManager {
|
|
|
803
787
|
// Either an identity mismatch or an unavailable probe is settled as lost with the
|
|
804
788
|
// uncertainty recorded, instead of silently degrading to an unprotected writer
|
|
805
789
|
// (D10.1/D10.3).
|
|
806
|
-
const
|
|
790
|
+
const identity = record.pid ? await processIdentity(record) : "unknown";
|
|
807
791
|
let processSafetyNote;
|
|
808
|
-
if (
|
|
792
|
+
if (identity === "same") {
|
|
809
793
|
await killProcessGroup(record.pid).catch(() => undefined);
|
|
810
794
|
processSafetyNote = " The adopted process identity matched and its process group was signalled.";
|
|
811
795
|
}
|
|
812
|
-
else if (
|
|
796
|
+
else if (identity === "different") {
|
|
813
797
|
processSafetyNote =
|
|
814
798
|
" The persisted pid identity did not match the current process; no signal was sent.";
|
|
815
799
|
}
|
|
@@ -853,7 +837,7 @@ export class SubAgentRunManager {
|
|
|
853
837
|
});
|
|
854
838
|
});
|
|
855
839
|
if (record.status === "running")
|
|
856
|
-
this.
|
|
840
|
+
this.recovery.add(record.runId);
|
|
857
841
|
}
|
|
858
842
|
if (isTerminal(record.status) && record.settledAt) {
|
|
859
843
|
// A settled-but-unwoken record means the wake was lost between settlement and
|
|
@@ -873,160 +857,57 @@ export class SubAgentRunManager {
|
|
|
873
857
|
}
|
|
874
858
|
return restored;
|
|
875
859
|
}
|
|
876
|
-
/**
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
async processIdentity(record) {
|
|
889
|
-
if (!record.pid || !record.pidStartedAt)
|
|
890
|
-
return "unknown";
|
|
891
|
-
const current = await readProcessStartTime(record.pid);
|
|
892
|
-
if (!current)
|
|
893
|
-
return "unknown";
|
|
894
|
-
return current === record.pidStartedAt ? "same" : "different";
|
|
860
|
+
/** The lane retries task receipts before it decides a wait has no result. */
|
|
861
|
+
async retryTaskSettlements() {
|
|
862
|
+
for (const record of this.runs.values()) {
|
|
863
|
+
if (!record.taskId || !record.settledAt || record.taskAccounted)
|
|
864
|
+
continue;
|
|
865
|
+
await this.queue.run(record.runId, async () => {
|
|
866
|
+
if (record.taskAccounted)
|
|
867
|
+
return;
|
|
868
|
+
const output = await readFile(join(record.artifactDir, "output.md"), "utf-8").catch(() => "");
|
|
869
|
+
await this.announce(record, output, Boolean(output));
|
|
870
|
+
});
|
|
871
|
+
}
|
|
895
872
|
}
|
|
896
|
-
/**
|
|
897
|
-
* Restart reconciliation for an external run still marked `running` (D10.3), and the terminal
|
|
898
|
-
* judgement `sweep()` uses once an adopted run's process is confirmed gone. Unlike internal
|
|
899
|
-
* runs, a `detached` external process outlives the daemon, so the first move is a liveness
|
|
900
|
-
* probe, not an automatic `lost`:
|
|
901
|
-
*
|
|
902
|
-
* - no `pid` at all: the intent was persisted but spawn was never confirmed — cannot prove it
|
|
903
|
-
* ever started, so it is judged `lost` the same as an internal run (D1).
|
|
904
|
-
* - `pid` alive under the same identity: genuinely still running. Left alone.
|
|
905
|
-
* - `pid` gone (or reused by an unrelated process): the process finished, died, or was killed
|
|
906
|
-
* while the daemon was down. `finalizeExternalRun` (spec 042 D1) parses whatever the process
|
|
907
|
-
* actually wrote before applying `terminationReason` — the same sequence the live post-exit
|
|
908
|
-
* path uses, so a reconciled run never loses usage, output, session id, or its verify verdict
|
|
909
|
-
* just because the daemon happened to be down when the run finished.
|
|
910
|
-
*/
|
|
911
873
|
async reconcileExternalRun(record) {
|
|
912
|
-
|
|
913
|
-
await this.settle(record.runId, {
|
|
914
|
-
status: "lost",
|
|
915
|
-
failureReason: "No pid was ever recorded for this run; it cannot be proven to have started.",
|
|
916
|
-
usage: record.usage,
|
|
917
|
-
usageKnown: record.usageKnown,
|
|
918
|
-
costKnown: record.costKnown,
|
|
919
|
-
turns: record.turns ?? 0,
|
|
920
|
-
toolCalls: record.toolCalls ?? 0,
|
|
921
|
-
durationMs: Date.now() - record.startedAt,
|
|
922
|
-
durationEstimated: true,
|
|
923
|
-
outputText: "",
|
|
924
|
-
}, { announce: true });
|
|
925
|
-
return;
|
|
926
|
-
}
|
|
927
|
-
if (isProcessAlive(record.pid) && (await this.isSameProcess(record))) {
|
|
928
|
-
return; // Still genuinely running; nothing to reconcile yet.
|
|
929
|
-
}
|
|
930
|
-
if (!record.harness) {
|
|
931
|
-
// Defensive only: external runs always get a harness at register() time (spec 040 D1).
|
|
932
|
-
await this.settle(record.runId, {
|
|
933
|
-
status: "failed",
|
|
934
|
-
failureReason: "Run has no recorded harness; cannot judge it.",
|
|
935
|
-
usage: record.usage,
|
|
936
|
-
usageKnown: record.usageKnown,
|
|
937
|
-
costKnown: record.costKnown,
|
|
938
|
-
turns: record.turns ?? 0,
|
|
939
|
-
toolCalls: record.toolCalls ?? 0,
|
|
940
|
-
durationMs: Date.now() - record.startedAt,
|
|
941
|
-
durationEstimated: true,
|
|
942
|
-
outputText: "",
|
|
943
|
-
}, { announce: true });
|
|
944
|
-
return;
|
|
945
|
-
}
|
|
946
|
-
// Duration can only be estimated post-restart: the artifact file's own mtime is the closest
|
|
947
|
-
// proxy for "when the process last wrote something" (D1); wall clock from the last known
|
|
948
|
-
// process start is the fallback when the file is missing (e.g. already GC'd).
|
|
949
|
-
const processStartedAt = record.processStartedAt ?? record.startedAt;
|
|
950
|
-
let durationMs = Date.now() - processStartedAt;
|
|
951
|
-
try {
|
|
952
|
-
const eventsStat = await stat(join(record.artifactDir, "events.jsonl"));
|
|
953
|
-
durationMs = eventsStat.mtimeMs - processStartedAt;
|
|
954
|
-
}
|
|
955
|
-
catch {
|
|
956
|
-
// No events file (or already GC'd) — fall back to the wall-clock estimate above.
|
|
957
|
-
}
|
|
958
|
-
const maxWallTimeSec = record.maxWallTimeSec ??
|
|
959
|
-
(record.deadlineAt ? Math.round((record.deadlineAt - record.startedAt) / 1000) : undefined);
|
|
960
|
-
await finalizeExternalRun({
|
|
961
|
-
harnessId: record.harness,
|
|
962
|
-
purpose: record.purpose,
|
|
963
|
-
workingDirectory: record.workingDirectory,
|
|
964
|
-
artifactDir: record.artifactDir,
|
|
965
|
-
exitCode: undefined,
|
|
966
|
-
durationMs: Math.max(0, durationMs),
|
|
967
|
-
durationEstimated: true,
|
|
968
|
-
terminationReason: record.terminationReason,
|
|
969
|
-
maxWallTimeSec,
|
|
970
|
-
mutates: record.mutates,
|
|
971
|
-
workspaceBaseline: record.workspaceBaseline,
|
|
972
|
-
}, (settleInput, options) => this.settle(record.runId, settleInput, options), { announce: record.terminationReason !== "cancelled" });
|
|
874
|
+
await reconcileExternalRun(record, (id, input, options) => this.settle(id, input, options), () => !this.stopped && this.runs.get(record.runId) === record && record.status === "running");
|
|
973
875
|
}
|
|
974
|
-
/** Reconcile an adopted process once at restore and once at its persisted deadline. Probing
|
|
975
|
-
* liveness before assigning a timeout lets a process that finished just before the deadline be
|
|
976
|
-
* settled from its artifacts as a normal completion. */
|
|
977
876
|
async reconcileExternalRunAtDeadline(record, now = Date.now()) {
|
|
978
|
-
|
|
979
|
-
record.
|
|
980
|
-
|
|
981
|
-
record.
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
}
|
|
987
|
-
await this.reconcileExternalRun(record);
|
|
988
|
-
}
|
|
989
|
-
clearExternalRecoveryTimer(runId) {
|
|
990
|
-
const timer = this.externalRecoveryTimers.get(runId);
|
|
991
|
-
if (!timer)
|
|
992
|
-
return;
|
|
993
|
-
clearTimeout(timer);
|
|
994
|
-
this.externalRecoveryTimers.delete(runId);
|
|
995
|
-
}
|
|
996
|
-
scheduleExternalRecovery(record) {
|
|
997
|
-
if (this.stopped)
|
|
998
|
-
return;
|
|
999
|
-
if (!record.deadlineAt)
|
|
1000
|
-
return;
|
|
1001
|
-
this.clearExternalRecoveryTimer(record.runId);
|
|
1002
|
-
const timer = setTimeout(() => {
|
|
1003
|
-
this.externalRecoveryTimers.delete(record.runId);
|
|
1004
|
-
const current = this.runs.get(record.runId);
|
|
1005
|
-
if (!current || current.status !== "running")
|
|
877
|
+
await this.recoveryQueue.run(record.runId, async () => {
|
|
878
|
+
if (this.stopped || record.status !== "running")
|
|
879
|
+
return;
|
|
880
|
+
if (record.settlementPending && !record.settledAt) {
|
|
881
|
+
const pending = record.settlementPending;
|
|
882
|
+
const outputText = await readFile(join(record.artifactDir, "output.md"), "utf-8").catch(() => "");
|
|
883
|
+
if (!this.stopped)
|
|
884
|
+
await this.settle(record.runId, { ...pending, outputText }, { announce: pending.announce });
|
|
1006
885
|
return;
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
if (
|
|
1010
|
-
this.scheduleExternalRecovery(current);
|
|
886
|
+
}
|
|
887
|
+
const identity = await processIdentity(record);
|
|
888
|
+
if (this.stopped || record.status !== "running")
|
|
1011
889
|
return;
|
|
890
|
+
if (record.pid &&
|
|
891
|
+
isProcessAlive(record.pid) &&
|
|
892
|
+
identity === "same" &&
|
|
893
|
+
(record.terminationReason || (record.deadlineAt && now >= record.deadlineAt))) {
|
|
894
|
+
if (!record.terminationReason)
|
|
895
|
+
await this.markTerminationReason(record.runId, "timeout", true);
|
|
896
|
+
if (this.stopped || record.status !== "running")
|
|
897
|
+
return;
|
|
898
|
+
await killProcessGroup(record.pid);
|
|
1012
899
|
}
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
fields: { runId: current.runId },
|
|
1017
|
-
});
|
|
1018
|
-
});
|
|
1019
|
-
}, Math.max(0, record.deadlineAt - Date.now()));
|
|
1020
|
-
timer.unref?.();
|
|
1021
|
-
this.externalRecoveryTimers.set(record.runId, timer);
|
|
900
|
+
if (!this.stopped)
|
|
901
|
+
await this.reconcileExternalRun(record);
|
|
902
|
+
});
|
|
1022
903
|
}
|
|
1023
904
|
/**
|
|
1024
905
|
* Daily retention GC. A settled run and all runtime-managed artifacts are deleted together once
|
|
1025
|
-
* they are at least
|
|
906
|
+
* they are at least 30 days old. Other files deliberately created by the delegated task are
|
|
1026
907
|
* not recursively removed from the artifact directory.
|
|
1027
908
|
*/
|
|
1028
909
|
async collectGarbageIfExpired(record, now = Date.now()) {
|
|
1029
|
-
if (!record.settledAt)
|
|
910
|
+
if (!record.settledAt || (record.taskId && !record.taskAccounted))
|
|
1030
911
|
return;
|
|
1031
912
|
if (now - record.settledAt >= RUN_RETENTION_MS) {
|
|
1032
913
|
await this.forget(record);
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { RunRecord, SettleInput } from "./run-record.js";
|
|
2
|
+
/** Map settled run facts to the task journal; the run manager owns acknowledgment and retries. */
|
|
3
|
+
export declare function creditTaskForRun(deskDir: string, record: RunRecord, input: Pick<SettleInput, "outputText" | "durationMs" | "usage" | "costKnown">, outputSaved: boolean, inline: boolean): Promise<void>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { logTaskSettlement } from "../tasks/bound-work.js";
|
|
3
|
+
import { WAKE_OUTPUT_TAIL_CHARS } from "./completion-wake.js";
|
|
4
|
+
/** Map settled run facts to the task journal; the run manager owns acknowledgment and retries. */
|
|
5
|
+
export async function creditTaskForRun(deskDir, record, input, outputSaved, inline) {
|
|
6
|
+
const taskId = record.taskId;
|
|
7
|
+
if (!taskId)
|
|
8
|
+
return;
|
|
9
|
+
await logTaskSettlement(deskDir, taskId, {
|
|
10
|
+
ref: record.runId,
|
|
11
|
+
item: record.item,
|
|
12
|
+
status: record.status,
|
|
13
|
+
verdict: record.purpose === "verify" ? record.verificationVerdict : undefined,
|
|
14
|
+
output: outputSaved ? join(record.artifactDir, "output.md") : undefined,
|
|
15
|
+
tail: input.outputText.slice(-WAKE_OUTPUT_TAIL_CHARS).trim() || undefined,
|
|
16
|
+
changed: record.workspaceSummary,
|
|
17
|
+
durationMs: input.durationMs,
|
|
18
|
+
...(inline ? { inline: true } : {}),
|
|
19
|
+
}, { usd: input.usage.cost.total, estimated: false, known: input.costKnown });
|
|
20
|
+
}
|
|
@@ -57,7 +57,6 @@ export interface DelegationParams {
|
|
|
57
57
|
task: string;
|
|
58
58
|
workingDirectory?: string;
|
|
59
59
|
purpose?: "work" | "verify";
|
|
60
|
-
taskId?: string;
|
|
61
60
|
item?: string;
|
|
62
61
|
}
|
|
63
62
|
export interface SubAgentWorker {
|
|
@@ -128,7 +127,7 @@ export interface SubAgentToolOptions {
|
|
|
128
127
|
buildTools?: BuildSubagentTools;
|
|
129
128
|
/** Test-only override for the D6 convergence-turn wall clock; defaults to CONVERGENCE_WALL_CLOCK_MS. */
|
|
130
129
|
convergenceWallClockMs?: number;
|
|
131
|
-
/** Test-only override for the D2 sync grace window; defaults to
|
|
130
|
+
/** Test-only override for the D2 sync grace window; defaults to CHAT_SYNC_GRACE_MS for chat; task dispatch never waits for the worker. */
|
|
132
131
|
syncGraceMs?: number;
|
|
133
132
|
/** Test-only override for the transient-error retry backoff base; defaults to RETRY_BASE_DELAY_MS. */
|
|
134
133
|
retryBaseDelayMs?: number;
|
|
@@ -3,13 +3,17 @@ import { stat } from "node:fs/promises";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { promisify } from "node:util";
|
|
5
5
|
const execFileAsync = promisify(execFile);
|
|
6
|
+
const GIT_STATUS_TIMEOUT_MS = 1_000;
|
|
6
7
|
/** Past this many dirty paths a baseline is not worth persisting on every run record; the summary
|
|
7
8
|
* then falls back to the raw status and says so. */
|
|
8
9
|
const BASELINE_MAX_ENTRIES = 500;
|
|
9
10
|
async function readPorcelain(workingDirectory) {
|
|
10
11
|
try {
|
|
11
12
|
const args = ["-C", workingDirectory, "status", "--porcelain=v1", "--untracked-files=all"];
|
|
12
|
-
const { stdout } = await execFileAsync("git", args, {
|
|
13
|
+
const { stdout } = await execFileAsync("git", args, {
|
|
14
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
15
|
+
timeout: GIT_STATUS_TIMEOUT_MS,
|
|
16
|
+
});
|
|
13
17
|
return stdout
|
|
14
18
|
.split("\n")
|
|
15
19
|
.map((line) => line.trimEnd())
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TaskArchiveOutcome, TaskFrontmatter } from "./frontmatter.js";
|
|
2
|
+
/** A current-format crash-recovery record, removed only after publication and source cleanup. */
|
|
3
|
+
export declare function taskArchiveCommitPath(deskDir: string, id: string): string;
|
|
4
|
+
export declare function isTaskArchiving(deskDir: string, id: string): boolean;
|
|
5
|
+
/** Snapshot once, publish all shards, then clean up. Ordinary I/O failures and SIGKILL use the same recovery path. */
|
|
6
|
+
export declare function commitTaskArchive(deskDir: string, id: string, document?: {
|
|
7
|
+
fields: TaskFrontmatter;
|
|
8
|
+
body: string;
|
|
9
|
+
}, outcome?: TaskArchiveOutcome, closedAt?: string): Promise<void>;
|
|
10
|
+
/** Only unfinished closes are enumerated, never the growing historical archive. */
|
|
11
|
+
export declare function recoverTaskArchives(deskDir: string): Promise<void>;
|