@oyasmi/pipiclaw 0.9.2-beta.2 → 0.9.2-beta.3
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/dist/agent/job-manager.js +3 -0
- package/dist/runtime/bootstrap.js +29 -7
- package/dist/runtime/channel-event.d.ts +8 -0
- package/dist/runtime/subagent-commands.d.ts +8 -0
- package/dist/runtime/subagent-commands.js +28 -4
- package/dist/runtime/task-wake.d.ts +5 -0
- package/dist/runtime/task-wake.js +20 -1
- package/dist/settings.d.ts +12 -0
- package/dist/settings.js +9 -0
- package/dist/subagents/external/claude-code.js +20 -0
- package/dist/subagents/external/codex-cli.js +33 -0
- package/dist/subagents/external/harness.d.ts +8 -0
- package/dist/subagents/external/progress-tail.d.ts +14 -0
- package/dist/subagents/external/progress-tail.js +84 -0
- package/dist/subagents/external/run.js +1 -0
- package/dist/subagents/external/settlement.d.ts +4 -1
- package/dist/subagents/external/settlement.js +9 -2
- package/dist/subagents/runs.d.ts +36 -0
- package/dist/subagents/runs.js +80 -3
- package/dist/tasks/artifact-subject.d.ts +8 -0
- package/dist/tasks/artifact-subject.js +25 -0
- package/dist/tools/grep.js +4 -1
- package/examples/sub-agents/README.md +1 -0
- package/examples/sub-agents/builder-hard.md +17 -16
- package/examples/sub-agents/builder.md +16 -12
- package/examples/sub-agents/documenter.md +15 -16
- package/examples/sub-agents/explorer.md +12 -13
- package/examples/sub-agents/git-committer.md +15 -26
- package/examples/sub-agents/log-sifter.md +13 -12
- package/examples/sub-agents/planner.md +11 -11
- package/examples/sub-agents/reviewer.md +20 -13
- package/examples/sub-agents/scout.md +14 -11
- package/examples/sub-agents/verifier.md +16 -13
- package/examples/sub-agents/worker.md +15 -9
- package/package.json +1 -1
|
@@ -365,6 +365,9 @@ export class ChannelJobManager {
|
|
|
365
365
|
ts: String(Date.now()),
|
|
366
366
|
conversationType: this.channelId.startsWith("group_") ? "2" : "1",
|
|
367
367
|
dispatchId: `job:${this.channelId}:${record.id}:done`,
|
|
368
|
+
// P0-2: a user is waiting on this job's result — render the resulting turn's progress
|
|
369
|
+
// instead of the "none" style autonomous check-ins get.
|
|
370
|
+
presentation: "awaited",
|
|
368
371
|
...(record.contract.taskId
|
|
369
372
|
? {
|
|
370
373
|
internalWake: {
|
|
@@ -37,7 +37,7 @@ import { handleProjectCommand as runProjectCommand } from "./project-commands.js
|
|
|
37
37
|
import { resolveProjectScope } from "./project-scope-store.js";
|
|
38
38
|
import { installLlmProxy } from "./proxy.js";
|
|
39
39
|
import { ChannelStore } from "./store.js";
|
|
40
|
-
import { handleSubagentsCommand as runSubagentsCommand } from "./subagent-commands.js";
|
|
40
|
+
import { renderRunNotice, handleSubagentsCommand as runSubagentsCommand } from "./subagent-commands.js";
|
|
41
41
|
import { pauseTask, handleTasksCommand as runTasksCommand } from "./task-commands.js";
|
|
42
42
|
import { createTaskDriverEvent, TaskDriver } from "./task-driver.js";
|
|
43
43
|
import { migrateLegacyTaskScheduleEvents, migrateLegacyTaskState } from "./task-migration.js";
|
|
@@ -480,9 +480,12 @@ export async function createRuntimeContext(options) {
|
|
|
480
480
|
isBot: false,
|
|
481
481
|
skipContextSync: Boolean(builtInCommand),
|
|
482
482
|
}, "user message");
|
|
483
|
-
// Background wakes deliver their result, not their process: no progress card,
|
|
484
|
-
//
|
|
485
|
-
|
|
483
|
+
// Background wakes deliver their result, not their process: no progress card, no
|
|
484
|
+
// thinking stream, nothing to delete when the check-in ends `[SILENT]`. An "awaited"
|
|
485
|
+
// wake (a delegation or job finishing) is the opposite — a human is actually waiting
|
|
486
|
+
// on it — so it renders progress the same way a normal message does (P0-2).
|
|
487
|
+
const backgroundOnly = Boolean(_isEvent) && event.presentation !== "awaited";
|
|
488
|
+
const ctx = createDingTalkContext(event, bot, store, backgroundOnly ? "none" : undefined);
|
|
486
489
|
if (builtInCommand) {
|
|
487
490
|
const commandStartedAt = Date.now();
|
|
488
491
|
const logCtx = { channelId: event.channelId, userName: event.userName };
|
|
@@ -534,7 +537,7 @@ export async function createRuntimeContext(options) {
|
|
|
534
537
|
ctx: { channelId: event.channelId, userName: event.userName },
|
|
535
538
|
fields: { messageLength: event.text.length, source: _isEvent ? "event" : "message" },
|
|
536
539
|
});
|
|
537
|
-
if (!
|
|
540
|
+
if (!backgroundOnly) {
|
|
538
541
|
ctx.primeCard(350);
|
|
539
542
|
}
|
|
540
543
|
// Effects are tallied per channel; the governor needs them per task, so one
|
|
@@ -563,7 +566,11 @@ export async function createRuntimeContext(options) {
|
|
|
563
566
|
recoveredJobTaskId = claimed.taskId;
|
|
564
567
|
await claimed.finish();
|
|
565
568
|
structuredWakeFinalized = true;
|
|
566
|
-
|
|
569
|
+
// Drop without a turn only when something else is still driving this task (a
|
|
570
|
+
// sibling wake in the same fan-out already activated it); every other case —
|
|
571
|
+
// the task is done, archived, disabled, or gone — has nobody left to see this
|
|
572
|
+
// result if it is dropped here, so it must still reach a normal turn (P3-1).
|
|
573
|
+
if (event.internalWake?.kind === "job" && !claimed.activated && claimed.taskStillDriven)
|
|
567
574
|
return;
|
|
568
575
|
}
|
|
569
576
|
else {
|
|
@@ -590,7 +597,9 @@ export async function createRuntimeContext(options) {
|
|
|
590
597
|
recoveredDelegationTaskId = claimed.taskId;
|
|
591
598
|
await claimed.finish();
|
|
592
599
|
structuredWakeFinalized = true;
|
|
593
|
-
|
|
600
|
+
// See the JOB branch above (P3-1): only skip the turn when the task is still
|
|
601
|
+
// driven by something else.
|
|
602
|
+
if (event.internalWake?.kind === "subagent" && !claimed.activated && claimed.taskStillDriven)
|
|
594
603
|
return;
|
|
595
604
|
}
|
|
596
605
|
else {
|
|
@@ -669,6 +678,19 @@ export async function createRuntimeContext(options) {
|
|
|
669
678
|
configureSubAgentRuntime({
|
|
670
679
|
stateDir: join(options.paths.appHomeDir, "state", "subagent-runs"),
|
|
671
680
|
dispatch: (event) => durableDispatch?.dispatch(event) ?? false,
|
|
681
|
+
// P0-1/P1a: a best-effort out-of-band notice, independent of the completion wake — it reaches
|
|
682
|
+
// the channel in roughly the time the run itself takes, not the wake turn's own LLM latency
|
|
683
|
+
// on top of that. `notices: "off"` (settings.json) restores today's silent behavior.
|
|
684
|
+
notify: (channelId, notice) => {
|
|
685
|
+
const level = runtimeSettingsManager.getDelegationSettings().notices;
|
|
686
|
+
if (level === "off")
|
|
687
|
+
return;
|
|
688
|
+
if (level === "settled" && notice.kind !== "settled")
|
|
689
|
+
return;
|
|
690
|
+
const text = renderRunNotice(notice);
|
|
691
|
+
if (text)
|
|
692
|
+
void bot.sendPlain(channelId, text);
|
|
693
|
+
},
|
|
672
694
|
ledger: getUsageLedger(),
|
|
673
695
|
store,
|
|
674
696
|
});
|
|
@@ -32,4 +32,12 @@ export interface ChannelEvent {
|
|
|
32
32
|
taskId: string;
|
|
33
33
|
dispatchId: string;
|
|
34
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Purely a display hint: `"awaited"` marks a synthetic wake a human is actually waiting on (a
|
|
37
|
+
* delegation or background job finishing), as opposed to an autonomous check-in (task-driver
|
|
38
|
+
* polling, a scheduled event) that normally has nothing to say. The runtime uses it to decide
|
|
39
|
+
* whether the resulting turn renders progress or stays silent until a final answer — it must
|
|
40
|
+
* never gate task activation or any other trust decision; that stays on `internalWake` alone.
|
|
41
|
+
*/
|
|
42
|
+
presentation?: "awaited";
|
|
35
43
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SubAgentDiscoveryResult } from "../subagents/discovery.js";
|
|
2
|
+
import { type RunNotice } from "../subagents/runs.js";
|
|
2
3
|
/**
|
|
3
4
|
* `/subagents` — the human control path that does not depend on the model (spec 040 D6, polished
|
|
4
5
|
* by spec 041). Mirrors `task-commands.ts`'s shape: pure functions returning markdown, no
|
|
@@ -34,4 +35,11 @@ type SubagentsCommand = {
|
|
|
34
35
|
/** Exported so `test/commands-subcommands.test.ts` can feed every broadcast example back through it. */
|
|
35
36
|
export declare function parseSubagentsCommand(args: string): SubagentsCommand;
|
|
36
37
|
export declare function handleSubagentsCommand(options: HandleSubagentsCommandOptions): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Render a `RunNotice` (P0-1/P1a) as a one-line reply — a confirmation, per the command reply
|
|
40
|
+
* conventions. `notices: "off"` is enforced by the caller not invoking this at all; a `"settled"`
|
|
41
|
+
* caller filters `kind: "progress"` out before calling. Returns `undefined` for a notice with
|
|
42
|
+
* nothing worth showing (a progress step that never resolved to a label).
|
|
43
|
+
*/
|
|
44
|
+
export declare function renderRunNotice(notice: RunNotice): string | undefined;
|
|
37
45
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { renderSubcommandUsage } from "../agent/commands.js";
|
|
4
|
+
import { formatDuration } from "../shared/duration.js";
|
|
4
5
|
import { errorMessage } from "../shared/text-utils.js";
|
|
5
6
|
import { formatCost, formatRunDuration, harnessLabel } from "../subagents/format.js";
|
|
6
7
|
import { getSubAgentRunManager } from "../subagents/runs.js";
|
|
@@ -262,7 +263,10 @@ function formatRoleSummaryLine(agent) {
|
|
|
262
263
|
const kind = agent.runtime === "external" ? `外部/${agent.harness}` : "内置";
|
|
263
264
|
const mutates = agent.mutates ?? (agent.tools.includes("write") || agent.tools.includes("edit") ? "write" : "read");
|
|
264
265
|
const unavailable = agent.unavailable ? ` — ⚠ 不可用:${agent.unavailable}` : "";
|
|
265
|
-
|
|
266
|
+
const modelLabel = agent.runtime === "external" ? (agent.externalModelRef ?? "CLI 决定") : (agent.modelRef ?? "默认");
|
|
267
|
+
const thinkingLabel = agent.thinkingLevel ?? (agent.runtime === "external" ? "未设置" : "默认 medium");
|
|
268
|
+
// Keyless by design: the four positions are fixed — runtime, mutates, model, thinking.
|
|
269
|
+
return `- \`${agent.name}\` (${kind}, ${mutates}, ${modelLabel}, ${thinkingLabel}) — ${agent.description}${unavailable}`;
|
|
266
270
|
}
|
|
267
271
|
function formatRoleDetail(agent) {
|
|
268
272
|
const kind = agent.runtime === "external" ? `外部/${agent.harness}` : "内置";
|
|
@@ -276,12 +280,12 @@ function formatRoleDetail(agent) {
|
|
|
276
280
|
lines.push(`- ⚠ 不可用:${agent.unavailable}`);
|
|
277
281
|
lines.push(`- mutates:${agent.mutates ?? "(未声明,由 tools 推断)"}`);
|
|
278
282
|
if (agent.runtime === "internal") {
|
|
279
|
-
lines.push(`-
|
|
283
|
+
lines.push(`- model:${agent.modelRef ?? "默认"}`, `- thinking:${agent.thinkingLevel ?? "默认 medium"}`, `- tools:${agent.tools.join(", ") || "(none)"}`, `- 预算:maxTurns=${agent.maxTurns} maxToolCalls=${agent.maxToolCalls} maxWallTimeSec=${agent.maxWallTimeSec} bashTimeoutSec=${agent.bashTimeoutSec}`, `- contextMode:${agent.contextMode},memory:${agent.memory}`);
|
|
280
284
|
}
|
|
281
285
|
else {
|
|
282
286
|
lines.push(`- command:\`${agent.command ?? ""}\`${agent.shell ? "(通过 shell 执行)" : ""}`, `- maxWallTimeSec:${agent.maxWallTimeSec}`);
|
|
283
|
-
|
|
284
|
-
|
|
287
|
+
lines.push(`- model:${agent.externalModelRef ?? "CLI 决定"}`);
|
|
288
|
+
lines.push(`- thinking:${agent.thinkingLevel ?? "未设置(verify 派发默认 medium)"}`);
|
|
285
289
|
// spec 042 D4: contextMode/memory are effective for external roles too (memory 默认 none,
|
|
286
290
|
// 显式声明 session/relevant 才会把频道会话状态发给外部进程) — 显示出来而不是只在内置分支里说明。
|
|
287
291
|
lines.push(`- contextMode:${agent.contextMode},memory:${agent.memory}`);
|
|
@@ -361,3 +365,23 @@ export async function handleSubagentsCommand(options) {
|
|
|
361
365
|
return `执行 /subagents ${command.action} 失败:${errorMessage(error)}`;
|
|
362
366
|
}
|
|
363
367
|
}
|
|
368
|
+
/**
|
|
369
|
+
* Render a `RunNotice` (P0-1/P1a) as a one-line reply — a confirmation, per the command reply
|
|
370
|
+
* conventions. `notices: "off"` is enforced by the caller not invoking this at all; a `"settled"`
|
|
371
|
+
* caller filters `kind: "progress"` out before calling. Returns `undefined` for a notice with
|
|
372
|
+
* nothing worth showing (a progress step that never resolved to a label).
|
|
373
|
+
*/
|
|
374
|
+
export function renderRunNotice(notice) {
|
|
375
|
+
if (notice.kind === "progress") {
|
|
376
|
+
return notice.step ? `⏳ ${notice.agent} · ${notice.step}` : undefined;
|
|
377
|
+
}
|
|
378
|
+
const durationText = formatDuration(notice.durationMs);
|
|
379
|
+
switch (notice.status) {
|
|
380
|
+
case "completed":
|
|
381
|
+
return `✅ ${notice.agent} 完成 · ${durationText}`;
|
|
382
|
+
case "cancelled":
|
|
383
|
+
return `🛑 ${notice.agent} 已取消 · ${durationText}`;
|
|
384
|
+
default:
|
|
385
|
+
return `⚠️ ${notice.agent} 失败 · ${durationText}`;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
@@ -34,6 +34,9 @@ export declare function isTrustedInternalWake(event: ChannelEvent, kind: "job" |
|
|
|
34
34
|
export interface ClaimedDelegationWake {
|
|
35
35
|
taskId: string;
|
|
36
36
|
activated: boolean;
|
|
37
|
+
/** True when it is safe to drop this wake without a turn even though it did not activate the
|
|
38
|
+
* task itself — see `isTaskActivelyDriven`. Always true when `activated` is true. */
|
|
39
|
+
taskStillDriven: boolean;
|
|
37
40
|
finish(): Promise<void>;
|
|
38
41
|
rollback(): Promise<void>;
|
|
39
42
|
}
|
|
@@ -44,6 +47,8 @@ export declare function claimVerifiedDelegationWake(event: ChannelEvent, workspa
|
|
|
44
47
|
export interface ClaimedJobWake {
|
|
45
48
|
taskId: string;
|
|
46
49
|
activated: boolean;
|
|
50
|
+
/** See `ClaimedDelegationWake.taskStillDriven`. */
|
|
51
|
+
taskStillDriven: boolean;
|
|
47
52
|
finish(): Promise<void>;
|
|
48
53
|
}
|
|
49
54
|
/** Same shape as `claimVerifiedDelegationWake`, for a `[JOB:<jobId>] ... belongs to task
|
|
@@ -6,8 +6,23 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { getChannelJobManager } from "../agent/job-manager.js";
|
|
8
8
|
import { getSubAgentRunManager } from "../subagents/runs.js";
|
|
9
|
-
import { activateWaitingTask, rollbackWaitingTask } from "../tasks/store.js";
|
|
9
|
+
import { activateWaitingTask, readStoredTask, rollbackWaitingTask, } from "../tasks/store.js";
|
|
10
|
+
import { normalizeStoredStatus } from "../tasks/transitions.js";
|
|
10
11
|
import { getChannelDir } from "./channel-paths.js";
|
|
12
|
+
/**
|
|
13
|
+
* True when `taskId` is currently `active` and enabled — i.e. the task driver already owns
|
|
14
|
+
* advancing it. A completion wake that could not activate the task (because it was not
|
|
15
|
+
* `waiting`) is safe to drop without spending an agent turn only in this case: some other wake in
|
|
16
|
+
* the same fan-out already activated it, and the driver will pick up the result. Every other case
|
|
17
|
+
* — `done`, archived, disabled, or the task missing entirely — has nobody left to look at the
|
|
18
|
+
* wake's result if it is dropped here, so the caller must still route it to a normal turn.
|
|
19
|
+
*/
|
|
20
|
+
async function isTaskActivelyDriven(channelDir, taskId) {
|
|
21
|
+
const document = await readStoredTask(channelDir, taskId, false, true).catch(() => undefined);
|
|
22
|
+
return (document !== undefined &&
|
|
23
|
+
normalizeStoredStatus(document.fields.status) === "active" &&
|
|
24
|
+
document.fields.enabled !== false);
|
|
25
|
+
}
|
|
11
26
|
/**
|
|
12
27
|
* Whether a `[JOB:<jobId>] ... belongs to task <taskId>.` wake actually names a job that: exists
|
|
13
28
|
* on this channel, has actually finished (a still-`running` job cannot have produced this wake —
|
|
@@ -56,9 +71,11 @@ export async function claimVerifiedDelegationWake(event, workspaceDir, hooks) {
|
|
|
56
71
|
}
|
|
57
72
|
const channelDir = getChannelDir(workspaceDir, event.channelId);
|
|
58
73
|
const activated = await activateWaitingTask(channelDir, wake.taskId, hooks);
|
|
74
|
+
const taskStillDriven = activated !== undefined || (await isTaskActivelyDriven(channelDir, wake.taskId));
|
|
59
75
|
return {
|
|
60
76
|
taskId: wake.taskId,
|
|
61
77
|
activated: activated !== undefined,
|
|
78
|
+
taskStillDriven,
|
|
62
79
|
finish: async () => {
|
|
63
80
|
await runManager.finishWakeConsumption(wake.resourceId, event.dispatchId);
|
|
64
81
|
},
|
|
@@ -81,9 +98,11 @@ export async function claimVerifiedJobWake(event, workspaceDir, executor, hooks)
|
|
|
81
98
|
}
|
|
82
99
|
const channelDir = getChannelDir(workspaceDir, event.channelId);
|
|
83
100
|
const activated = await activateWaitingTask(channelDir, wake.taskId, hooks);
|
|
101
|
+
const taskStillDriven = activated !== undefined || (await isTaskActivelyDriven(channelDir, wake.taskId));
|
|
84
102
|
return {
|
|
85
103
|
taskId: wake.taskId,
|
|
86
104
|
activated: activated !== undefined,
|
|
105
|
+
taskStillDriven,
|
|
87
106
|
finish: async () => {
|
|
88
107
|
await jobManager.finishWakeConsumption(wake.resourceId, event.dispatchId);
|
|
89
108
|
},
|
package/dist/settings.d.ts
CHANGED
|
@@ -71,6 +71,16 @@ export interface PipiclawSessionSearchSettings {
|
|
|
71
71
|
summarizeWithModel: boolean;
|
|
72
72
|
timeoutMs: number;
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* How much out-of-band commentary a delegation run (`src/subagents/`) or background job produces
|
|
76
|
+
* outside of its own completion wake — plain text, not an LLM turn, so it reaches the channel in
|
|
77
|
+
* roughly the time the run itself takes rather than waiting on a wake turn's latency.
|
|
78
|
+
* `"off"` is today's behavior; `"settled"` adds only the one-line completion receipt; `"live"`
|
|
79
|
+
* also adds a sparse trickle of running-progress notices for long external runs.
|
|
80
|
+
*/
|
|
81
|
+
export interface PipiclawDelegationSettings {
|
|
82
|
+
notices: "off" | "settled" | "live";
|
|
83
|
+
}
|
|
74
84
|
/**
|
|
75
85
|
* Everything `settings.json` may contain (spec 035 D1).
|
|
76
86
|
*
|
|
@@ -120,6 +130,7 @@ export interface PipiclawSettings {
|
|
|
120
130
|
};
|
|
121
131
|
};
|
|
122
132
|
tui?: Partial<PipiclawTuiSettings>;
|
|
133
|
+
delegation?: Partial<PipiclawDelegationSettings>;
|
|
123
134
|
}
|
|
124
135
|
export interface PipiclawTuiSettings {
|
|
125
136
|
/**
|
|
@@ -168,6 +179,7 @@ export declare class PipiclawSettingsManager {
|
|
|
168
179
|
getSessionSearchSettings(): PipiclawSessionSearchSettings;
|
|
169
180
|
getLoggingSettings(): PipiclawLoggingSettings;
|
|
170
181
|
getTuiSettings(): PipiclawTuiSettings;
|
|
182
|
+
getDelegationSettings(): PipiclawDelegationSettings;
|
|
171
183
|
getRetryEnabled(): boolean;
|
|
172
184
|
setRetryEnabled(enabled: boolean): void;
|
|
173
185
|
/**
|
package/dist/settings.js
CHANGED
|
@@ -15,6 +15,9 @@ import { fileStamp } from "./shared/file-stamp.js";
|
|
|
15
15
|
const DEFAULT_TUI = {
|
|
16
16
|
responseMode: "full_progress_then_plain_final",
|
|
17
17
|
};
|
|
18
|
+
const DEFAULT_DELEGATION = {
|
|
19
|
+
notices: "live",
|
|
20
|
+
};
|
|
18
21
|
const DEFAULT_COMPACTION = {
|
|
19
22
|
enabled: true,
|
|
20
23
|
reserveTokens: 16384,
|
|
@@ -284,6 +287,12 @@ export class PipiclawSettingsManager {
|
|
|
284
287
|
getTuiSettings() {
|
|
285
288
|
return { ...DEFAULT_TUI, ...this.settings.tui };
|
|
286
289
|
}
|
|
290
|
+
getDelegationSettings() {
|
|
291
|
+
return {
|
|
292
|
+
...DEFAULT_DELEGATION,
|
|
293
|
+
notices: this.settings.delegation?.notices ?? DEFAULT_DELEGATION.notices,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
287
296
|
getRetryEnabled() {
|
|
288
297
|
return this.settings.retry?.enabled ?? DEFAULT_RETRY.enabled;
|
|
289
298
|
}
|
|
@@ -193,4 +193,24 @@ export const claudeCodeHarness = {
|
|
|
193
193
|
errorMessage,
|
|
194
194
|
};
|
|
195
195
|
},
|
|
196
|
+
// P1a: the first tool_use block in a streamed assistant message names the step currently
|
|
197
|
+
// running. Text-only assistant messages (reasoning, the final answer) have nothing to show.
|
|
198
|
+
toProgressLabel(line) {
|
|
199
|
+
let event;
|
|
200
|
+
try {
|
|
201
|
+
event = JSON.parse(line.trim());
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
if (!isRecord(event) || event.type !== "assistant" || !isRecord(event.message))
|
|
207
|
+
return undefined;
|
|
208
|
+
const parts = Array.isArray(event.message.content) ? event.message.content : [];
|
|
209
|
+
for (const part of parts) {
|
|
210
|
+
if (isRecord(part) && part.type === "tool_use" && typeof part.name === "string") {
|
|
211
|
+
return part.name;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return undefined;
|
|
215
|
+
},
|
|
196
216
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isRecord } from "../../shared/type-guards.js";
|
|
1
2
|
import { detectExistingFlags, expandPlaceholders, formatDroppedPlaceholderWarnings, } from "./harness.js";
|
|
2
3
|
/**
|
|
3
4
|
* `codex-cli` harness (spec 040, D3/D4). One prompt, one short-lived process, NDJSON on stdout
|
|
@@ -130,4 +131,36 @@ export const codexCliHarness = {
|
|
|
130
131
|
errorMessage,
|
|
131
132
|
};
|
|
132
133
|
},
|
|
134
|
+
// P1a: the two item types worth surfacing mid-run — the shell command a step is currently
|
|
135
|
+
// running, and todo-list progress. `agent_message`/everything else has nothing to show before
|
|
136
|
+
// the run is over.
|
|
137
|
+
toProgressLabel(line) {
|
|
138
|
+
let event;
|
|
139
|
+
try {
|
|
140
|
+
event = JSON.parse(line.trim());
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
if (typeof event !== "object" || event === null)
|
|
146
|
+
return undefined;
|
|
147
|
+
const record = event;
|
|
148
|
+
if (record.type !== "item.started" && record.type !== "item.updated")
|
|
149
|
+
return undefined;
|
|
150
|
+
const item = record.item;
|
|
151
|
+
if (!item)
|
|
152
|
+
return undefined;
|
|
153
|
+
if (item.type === "command_execution" && typeof item.command === "string") {
|
|
154
|
+
const firstLine = item.command.split("\n")[0]?.trim();
|
|
155
|
+
return firstLine ? firstLine.slice(0, 80) : undefined;
|
|
156
|
+
}
|
|
157
|
+
if (item.type === "todo_list" && Array.isArray(item.items)) {
|
|
158
|
+
const total = item.items.length;
|
|
159
|
+
if (total === 0)
|
|
160
|
+
return undefined;
|
|
161
|
+
const done = item.items.filter((entry) => isRecord(entry) && entry.completed === true).length;
|
|
162
|
+
return `待办 ${done}/${total}`;
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
},
|
|
133
166
|
};
|
|
@@ -76,6 +76,14 @@ export interface ExternalHarness {
|
|
|
76
76
|
/** Parse stdout into a unified outcome. Must never throw — a parse failure degrades to
|
|
77
77
|
* `protocolStatus: "unparsable"`, not an exception (D4). */
|
|
78
78
|
parseOutcome(input: ParseOutcomeInput): ExternalOutcome;
|
|
79
|
+
/**
|
|
80
|
+
* P1a: map one raw output line, still mid-run, to a short human-readable step label — e.g. the
|
|
81
|
+
* command a coding CLI just started, or a todo-list progress count. Same contract as
|
|
82
|
+
* `parseOutcome`: must never throw, and returns `undefined` for a line that carries no
|
|
83
|
+
* progress-worthy information (most lines). Optional — `exec` has no structured protocol to
|
|
84
|
+
* read mid-run, so it omits this entirely rather than implement a no-op.
|
|
85
|
+
*/
|
|
86
|
+
toProgressLabel?(line: string): string | undefined;
|
|
79
87
|
}
|
|
80
88
|
/**
|
|
81
89
|
* The D4 status table, shared by the live post-exit path and restart reconciliation (D10.3) so
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RunHarness } from "../runs.js";
|
|
2
|
+
export interface ExternalProgressTailInput {
|
|
3
|
+
artifactDir: string;
|
|
4
|
+
harnessId: RunHarness;
|
|
5
|
+
/** Process start time, so the "don't bother for a short run" window is measured from when the
|
|
6
|
+
* run actually started producing output, not from registration. */
|
|
7
|
+
startedAt: number;
|
|
8
|
+
onProgress: (label: string) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface ExternalProgressTail {
|
|
11
|
+
stop(): void;
|
|
12
|
+
}
|
|
13
|
+
/** Starts polling; returns a handle whose `stop()` is idempotent and always safe to call. */
|
|
14
|
+
export declare function startExternalProgressTail(input: ExternalProgressTailInput): ExternalProgressTail;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { getExternalHarness } from "./registry.js";
|
|
4
|
+
/**
|
|
5
|
+
* P1a: turns an external run's `events.jsonl` — already being written incrementally by the child
|
|
6
|
+
* process — into a trickle of human-readable "what is it doing right now" notices, instead of the
|
|
7
|
+
* silence that today lasts the whole run. Polls rather than watches: correctness never depends on
|
|
8
|
+
* catching every write, so a missed tick just costs one extra poll interval of staleness, and one
|
|
9
|
+
* timer per run stays far simpler than a filesystem watcher per run.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately re-reads the whole file each tick instead of tracking a byte offset: an offset read
|
|
12
|
+
* has to handle a half-written trailing line, this doesn't, and a run's artifact file is small
|
|
13
|
+
* enough (single-digit MB even for a long run) that re-reading it every `POLL_MS` costs nothing a
|
|
14
|
+
* background process needs to care about.
|
|
15
|
+
*/
|
|
16
|
+
const POLL_MS = 15_000;
|
|
17
|
+
/** A run that finishes inside this window never gets a progress notice — the settled notice
|
|
18
|
+
* (P0-1) already covers it, and a 45s-or-shorter run does not need a running commentary. */
|
|
19
|
+
const FIRST_NOTICE_DELAY_MS = 45_000;
|
|
20
|
+
/** Floor between two progress notices for the same run, so a chatty harness cannot turn this into
|
|
21
|
+
* a stream of messages. */
|
|
22
|
+
const MIN_NOTICE_GAP_MS = 180_000;
|
|
23
|
+
/** Ceiling on notices for a single run — even a multi-hour run stops narrating after this many. */
|
|
24
|
+
const MAX_NOTICES = 6;
|
|
25
|
+
const NOOP_TAIL = { stop() { } };
|
|
26
|
+
/** Starts polling; returns a handle whose `stop()` is idempotent and always safe to call. */
|
|
27
|
+
export function startExternalProgressTail(input) {
|
|
28
|
+
const harness = getExternalHarness(input.harnessId);
|
|
29
|
+
const toProgressLabel = harness?.toProgressLabel;
|
|
30
|
+
if (!toProgressLabel)
|
|
31
|
+
return NOOP_TAIL; // exec, or a harness that hasn't implemented this.
|
|
32
|
+
const eventsPath = join(input.artifactDir, "events.jsonl");
|
|
33
|
+
let lastLabel;
|
|
34
|
+
let lastNoticeAt = 0;
|
|
35
|
+
let noticeCount = 0;
|
|
36
|
+
let stopped = false;
|
|
37
|
+
const tick = async () => {
|
|
38
|
+
if (stopped || noticeCount >= MAX_NOTICES)
|
|
39
|
+
return;
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
if (now - input.startedAt < FIRST_NOTICE_DELAY_MS)
|
|
42
|
+
return;
|
|
43
|
+
if (lastNoticeAt && now - lastNoticeAt < MIN_NOTICE_GAP_MS)
|
|
44
|
+
return;
|
|
45
|
+
let text;
|
|
46
|
+
try {
|
|
47
|
+
text = await readFile(eventsPath, "utf-8");
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return; // Not written yet, or already cleaned up — try again next tick.
|
|
51
|
+
}
|
|
52
|
+
// Walk from the end: the most recent line that actually maps to a label wins, so a trailing
|
|
53
|
+
// run of unlabeled lines (a text message, a torn final line) does not hide real progress.
|
|
54
|
+
const lines = text.split("\n");
|
|
55
|
+
let label;
|
|
56
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
57
|
+
const line = lines[i]?.trim();
|
|
58
|
+
if (!line)
|
|
59
|
+
continue;
|
|
60
|
+
try {
|
|
61
|
+
label = toProgressLabel(line);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
label = undefined;
|
|
65
|
+
}
|
|
66
|
+
if (label)
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
if (!label || label === lastLabel || stopped)
|
|
70
|
+
return;
|
|
71
|
+
lastLabel = label;
|
|
72
|
+
lastNoticeAt = now;
|
|
73
|
+
noticeCount++;
|
|
74
|
+
input.onProgress(label);
|
|
75
|
+
};
|
|
76
|
+
const timer = setInterval(() => void tick(), POLL_MS);
|
|
77
|
+
timer.unref?.();
|
|
78
|
+
return {
|
|
79
|
+
stop() {
|
|
80
|
+
stopped = true;
|
|
81
|
+
clearInterval(timer);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -392,6 +392,7 @@ export async function launchExternalRun(input) {
|
|
|
392
392
|
verifySubjectBefore,
|
|
393
393
|
verifyBaseCommit,
|
|
394
394
|
verifyBaselineUntrackedPaths,
|
|
395
|
+
mutates: input.mutates,
|
|
395
396
|
}, (settleInput, options) => runManager.settle(input.runId, settleInput, options), { announce: terminationReason !== "cancelled" }).catch((error) => {
|
|
396
397
|
log.logWarning(`Failed to settle external run ${input.runId}`, errorMessage(error));
|
|
397
398
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RunHarness, SettleInput } from "../runs.js";
|
|
1
|
+
import type { RunHarness, RunMutates, SettleInput } from "../runs.js";
|
|
2
2
|
import { type ExternalOutcome } from "./harness.js";
|
|
3
3
|
export interface BuildExternalSettleInputInput {
|
|
4
4
|
harnessId: RunHarness;
|
|
@@ -39,6 +39,9 @@ export interface FinalizeExternalRunInput {
|
|
|
39
39
|
/** Fixed verification base and initial untracked manifest, persisted for restart reconciliation. */
|
|
40
40
|
verifyBaseCommit?: string;
|
|
41
41
|
verifyBaselineUntrackedPaths?: string[];
|
|
42
|
+
/** P2-2: only a `write` run's completion pays for a `git status` summary — a `read` run cannot
|
|
43
|
+
* have changed anything, and this must never become a hidden cost on the common read-only path. */
|
|
44
|
+
mutates?: RunMutates;
|
|
42
45
|
}
|
|
43
46
|
/**
|
|
44
47
|
* Read this run's artifact files, parse them through its harness, decide its verdict (including a
|
|
@@ -3,7 +3,7 @@ import { join } from "node:path";
|
|
|
3
3
|
import * as log from "../../log.js";
|
|
4
4
|
import { formatLocalTime } from "../../shared/local-time.js";
|
|
5
5
|
import { errorMessage } from "../../shared/text-utils.js";
|
|
6
|
-
import { workspaceSubjectHash } from "../../tasks/artifact-subject.js";
|
|
6
|
+
import { changedPathsSummary, workspaceSubjectHash } from "../../tasks/artifact-subject.js";
|
|
7
7
|
import { writeVerificationAttestation } from "../../tasks/verification.js";
|
|
8
8
|
import { resolveVerificationOutcome } from "../verification-outcome.js";
|
|
9
9
|
import { classifyExternalOutcome } from "./harness.js";
|
|
@@ -78,7 +78,7 @@ export async function finalizeExternalRun(input, settle, options) {
|
|
|
78
78
|
stderrTail,
|
|
79
79
|
errorMessage: `Unknown harness "${input.harnessId}"; cannot judge this run.`,
|
|
80
80
|
};
|
|
81
|
-
|
|
81
|
+
let settleInput = buildExternalSettleInput({
|
|
82
82
|
harnessId: input.harnessId,
|
|
83
83
|
outcome,
|
|
84
84
|
durationMs: input.durationMs,
|
|
@@ -86,6 +86,13 @@ export async function finalizeExternalRun(input, settle, options) {
|
|
|
86
86
|
terminationReason: input.terminationReason,
|
|
87
87
|
maxWallTimeSec: input.maxWallTimeSec,
|
|
88
88
|
});
|
|
89
|
+
// P2-2: only for a run that actually could have changed the working directory, and only once it
|
|
90
|
+
// is known to have finished cleanly — best-effort, never blocks or fails settlement.
|
|
91
|
+
if (input.mutates === "write" && settleInput.status === "completed") {
|
|
92
|
+
const workspaceSummary = await changedPathsSummary(input.workingDirectory).catch(() => undefined);
|
|
93
|
+
if (workspaceSummary)
|
|
94
|
+
settleInput = { ...settleInput, workspaceSummary };
|
|
95
|
+
}
|
|
89
96
|
const shouldVerify = input.purpose === "verify" &&
|
|
90
97
|
input.taskId !== undefined &&
|
|
91
98
|
input.channelDir !== undefined &&
|
package/dist/subagents/runs.d.ts
CHANGED
|
@@ -77,6 +77,9 @@ export interface RunRecord extends RunUsage {
|
|
|
77
77
|
verificationVerdict?: "pass" | "fail";
|
|
78
78
|
/** `enforced` only for a structurally read-only verifier; write-capable/external runs are advisory. */
|
|
79
79
|
verificationStrength?: "enforced" | "advisory";
|
|
80
|
+
/** P2-2: `git status --porcelain` taken right after a `mutates: "write"` run finished, so the
|
|
81
|
+
* wake's reader does not have to spend its own first tool call finding out what changed. */
|
|
82
|
+
workspaceSummary?: string;
|
|
80
83
|
/** Set once settlement (write output, free resources) has happened; guards against replay. */
|
|
81
84
|
settledAt?: number;
|
|
82
85
|
/**
|
|
@@ -169,6 +172,7 @@ interface PendingSettlement {
|
|
|
169
172
|
durationEstimated?: boolean;
|
|
170
173
|
verificationVerdict?: "pass" | "fail";
|
|
171
174
|
verificationStrength?: "enforced" | "advisory";
|
|
175
|
+
workspaceSummary?: string;
|
|
172
176
|
sessionId?: string;
|
|
173
177
|
finishedAt: number;
|
|
174
178
|
announce: boolean;
|
|
@@ -189,13 +193,35 @@ export interface SettleInput {
|
|
|
189
193
|
outputText: string;
|
|
190
194
|
verificationVerdict?: "pass" | "fail";
|
|
191
195
|
verificationStrength?: "enforced" | "advisory";
|
|
196
|
+
/** P2-2: see `RunRecord.workspaceSummary`. */
|
|
197
|
+
workspaceSummary?: string;
|
|
192
198
|
/** The harness's own session/thread id, captured even on failure so a later resume can still use it. */
|
|
193
199
|
sessionId?: string;
|
|
194
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* A best-effort, out-of-band notice about a run — distinct from the completion wake (`dispatch`).
|
|
203
|
+
* The wake is an LLM turn (its delivery latency is whatever the model takes to respond); a notice
|
|
204
|
+
* is plain text rendered straight to the channel, so it reaches the user in roughly the time the
|
|
205
|
+
* run itself takes to settle, not the wake turn's latency on top of that.
|
|
206
|
+
*/
|
|
207
|
+
export interface RunNotice {
|
|
208
|
+
kind: "settled" | "progress";
|
|
209
|
+
runId: string;
|
|
210
|
+
agent: string;
|
|
211
|
+
/** Only set for `kind: "settled"`. */
|
|
212
|
+
status?: RunStatus;
|
|
213
|
+
durationMs: number;
|
|
214
|
+
/** Only set for `kind: "progress"`: the harness's most recently observed step, already short. */
|
|
215
|
+
step?: string;
|
|
216
|
+
}
|
|
217
|
+
export type RunNotifier = (channelId: string, notice: RunNotice) => void | Promise<void>;
|
|
195
218
|
export interface RunManagerOptions {
|
|
196
219
|
/** Root of the per-channel record directories (`<stateDir>/<channelId>/`). Omit to skip persistence. */
|
|
197
220
|
stateDir?: string;
|
|
198
221
|
dispatch?: (event: ChannelEvent) => boolean | Promise<boolean>;
|
|
222
|
+
/** Best-effort out-of-band notice sink (see `RunNotice`). Never awaited by settlement or the
|
|
223
|
+
* progress tail — a slow or failing notifier must not delay either. */
|
|
224
|
+
notify?: RunNotifier;
|
|
199
225
|
ledger?: UsageLedger;
|
|
200
226
|
store?: ChannelStore;
|
|
201
227
|
}
|
|
@@ -219,6 +245,10 @@ export declare class SubAgentRunManager {
|
|
|
219
245
|
/** One-shot deadline checks for external processes adopted during restore. Unlike a child this
|
|
220
246
|
* daemon spawned itself, an adopted process cannot emit a `close` event to this process. */
|
|
221
247
|
private readonly externalRecoveryTimers;
|
|
248
|
+
/** Live external runs' tail-the-artifact-file progress pollers (P1a). Never persisted or
|
|
249
|
+
* reconstructed on restore — an adopted run has no fresher information to poll for than
|
|
250
|
+
* restart reconciliation already reads once from the finished process's own output. */
|
|
251
|
+
private readonly externalProgressTails;
|
|
222
252
|
private readonly queue;
|
|
223
253
|
constructor(channelId: string, options: RunManagerOptions);
|
|
224
254
|
private recordPath;
|
|
@@ -231,6 +261,12 @@ export declare class SubAgentRunManager {
|
|
|
231
261
|
* the artifact is bad, leaving the run stuck in `running` is worse.
|
|
232
262
|
*/
|
|
233
263
|
private writeOutputFile;
|
|
264
|
+
/** Fire-and-forget: a notifier failure must never affect settlement or the progress tail. */
|
|
265
|
+
private emitNotice;
|
|
266
|
+
/** Start tailing a just-launched external run's artifact file for a human-readable step label
|
|
267
|
+
* (P1a). Best-effort only — see `progress-tail.ts` for the polling/pacing contract. */
|
|
268
|
+
private startExternalProgressTail;
|
|
269
|
+
private stopExternalProgressTail;
|
|
234
270
|
private forget;
|
|
235
271
|
get(runId: string): RunRecord | undefined;
|
|
236
272
|
list(): RunRecord[];
|