pi-herdr-subagents-lite 0.7.2 → 0.9.0
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/CHANGELOG.md +17 -0
- package/README.md +14 -12
- package/README.zh-CN.md +14 -12
- package/package.json +1 -1
- package/src/config/index.ts +5 -5
- package/src/config/schema.ts +2 -2
- package/src/extension/index.ts +29 -25
- package/src/extension/report.ts +43 -0
- package/src/extension/shutdown.ts +17 -9
- package/src/herdr/agent.ts +0 -4
- package/src/herdr/index.ts +2 -4
- package/src/herdr/pane.ts +7 -35
- package/src/inbox/index.ts +14 -14
- package/src/index.ts +2 -4
- package/src/run/aggregate.ts +33 -18
- package/src/run/artifact.ts +4 -4
- package/src/run/close.ts +49 -21
- package/src/run/index.ts +21 -27
- package/src/run/pane.ts +3 -4
- package/src/run/paths.ts +18 -1
- package/src/run/result.ts +23 -28
- package/src/run/send.ts +32 -45
- package/src/run/settle.ts +65 -0
- package/src/run/spawn-args.ts +0 -2
- package/src/run/start.ts +19 -13
- package/src/run/state.ts +21 -24
- package/src/run/watch.ts +95 -0
- package/src/session-file/index.ts +12 -14
- package/src/session-file/jsonl.ts +6 -34
- package/src/tool/index.ts +5 -12
- package/src/tool/message.ts +14 -11
- package/src/turn/index.ts +236 -0
- package/src/extension/auto-exit.ts +0 -35
- package/src/run/poll-result.ts +0 -84
- package/src/run/poll.ts +0 -127
- package/src/session-file/receipt.ts +0 -39
package/src/run/aggregate.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export interface Aggregator<T> {
|
|
2
|
-
|
|
2
|
+
// 返回是否真的投递成功。批量窗口内的条目在 flush 之后才结算。
|
|
3
|
+
push(item: T): Promise<boolean>;
|
|
3
4
|
stop(): void;
|
|
4
5
|
}
|
|
5
6
|
|
|
@@ -9,43 +10,54 @@ export interface AggregatorOptions<T> {
|
|
|
9
10
|
onError: (error: unknown) => void;
|
|
10
11
|
}
|
|
11
12
|
|
|
13
|
+
interface Queued<T> {
|
|
14
|
+
item: T;
|
|
15
|
+
resolve: (delivered: boolean) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
12
18
|
export function createAggregator<T>({ windowMs, deliver, onError }: AggregatorOptions<T>): Aggregator<T> {
|
|
13
|
-
let pending: T
|
|
19
|
+
let pending: Array<Queued<T>> = [];
|
|
14
20
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
15
21
|
let stopped = false;
|
|
16
22
|
|
|
17
|
-
const send = async (items: T
|
|
23
|
+
const send = async (items: Array<Queued<T>>) => {
|
|
24
|
+
let delivered = true;
|
|
18
25
|
try {
|
|
19
|
-
await deliver(items);
|
|
26
|
+
await deliver(items.map((entry) => entry.item));
|
|
20
27
|
} catch (error) {
|
|
28
|
+
delivered = false;
|
|
21
29
|
onError(error);
|
|
22
30
|
}
|
|
31
|
+
for (const entry of items) {
|
|
32
|
+
entry.resolve(delivered);
|
|
33
|
+
}
|
|
23
34
|
};
|
|
24
35
|
|
|
25
36
|
const flush = () => {
|
|
26
37
|
timer = undefined;
|
|
27
|
-
if (pending.length === 0) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
38
|
const items = pending;
|
|
31
39
|
pending = [];
|
|
32
|
-
|
|
40
|
+
if (items.length > 0) {
|
|
41
|
+
void send(items);
|
|
42
|
+
}
|
|
33
43
|
};
|
|
34
44
|
|
|
35
45
|
return {
|
|
36
46
|
push(item) {
|
|
37
47
|
if (stopped) {
|
|
38
|
-
return Promise.resolve();
|
|
39
|
-
}
|
|
40
|
-
if (windowMs === 0) {
|
|
41
|
-
return send([item]);
|
|
48
|
+
return Promise.resolve(false);
|
|
42
49
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
return new Promise<boolean>((resolve) => {
|
|
51
|
+
if (windowMs === 0) {
|
|
52
|
+
void send([{ item, resolve }]);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
pending.push({ item, resolve });
|
|
56
|
+
if (timer === undefined) {
|
|
57
|
+
timer = setTimeout(flush, windowMs);
|
|
58
|
+
timer.unref?.();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
49
61
|
},
|
|
50
62
|
stop() {
|
|
51
63
|
stopped = true;
|
|
@@ -53,6 +65,9 @@ export function createAggregator<T>({ windowMs, deliver, onError }: AggregatorOp
|
|
|
53
65
|
clearTimeout(timer);
|
|
54
66
|
timer = undefined;
|
|
55
67
|
}
|
|
68
|
+
for (const entry of pending) {
|
|
69
|
+
entry.resolve(false);
|
|
70
|
+
}
|
|
56
71
|
pending = [];
|
|
57
72
|
},
|
|
58
73
|
};
|
package/src/run/artifact.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { rename, rm, writeFile } from "node:fs/promises";
|
|
2
2
|
|
|
3
|
-
function artifactPath(
|
|
4
|
-
return `${
|
|
3
|
+
function artifactPath(base: string, seq: number): string {
|
|
4
|
+
return `${base}.result-${seq}.md`;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
export async function writeArtifact(
|
|
8
|
-
const path = artifactPath(
|
|
7
|
+
export async function writeArtifact(base: string, seq: number, text: string): Promise<string> {
|
|
8
|
+
const path = artifactPath(base, seq);
|
|
9
9
|
const temp = `${path}.tmp`;
|
|
10
10
|
try {
|
|
11
11
|
await writeFile(temp, text);
|
package/src/run/close.ts
CHANGED
|
@@ -1,43 +1,71 @@
|
|
|
1
1
|
import { errorText } from "../error/index.ts";
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
2
|
+
import { HerdrHostError } from "../herdr/index.ts";
|
|
3
|
+
import type { Turn } from "../turn/index.ts";
|
|
4
|
+
import type { RunRecord, RunState } from "./state.ts";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
const CLOSE_ATTEMPTS = 3;
|
|
7
|
+
const CLOSE_RETRY_MS = 250;
|
|
8
|
+
|
|
9
|
+
// 关闭判据只看回合结果与子代理是否还有自己的子代理。
|
|
10
|
+
export function shouldClose(state: RunState, run: RunRecord, turn: Turn): boolean {
|
|
11
|
+
if (run.pane === undefined || run.paneClosed) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (turn.outcome === "done") {
|
|
15
|
+
return run.autoExit && turn.children === 0;
|
|
16
|
+
}
|
|
17
|
+
if (turn.outcome === "quit") {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return state.closeOnFailure;
|
|
7
21
|
}
|
|
8
22
|
|
|
9
|
-
|
|
10
|
-
|
|
23
|
+
// 只关自己记下的 pane。pane 已经不在了也算关掉。
|
|
24
|
+
export async function closeRunPane(state: RunState, run: RunRecord): Promise<void> {
|
|
25
|
+
const pane = run.pane;
|
|
26
|
+
if (pane === undefined || run.paneClosed) {
|
|
11
27
|
return;
|
|
12
28
|
}
|
|
13
|
-
|
|
29
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
30
|
+
try {
|
|
31
|
+
await state.host.closePane(pane.paneId);
|
|
32
|
+
run.paneClosed = true;
|
|
33
|
+
return;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error instanceof HerdrHostError && error.code === "pane_not_found") {
|
|
36
|
+
run.paneClosed = true;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (attempt >= CLOSE_ATTEMPTS) {
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
await state.delay(CLOSE_RETRY_MS);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
14
45
|
}
|
|
15
46
|
|
|
16
47
|
export interface CloseChildrenResult {
|
|
17
|
-
kept: string[];
|
|
18
48
|
failed: Array<{ name: string; reason: string }>;
|
|
19
49
|
}
|
|
20
50
|
|
|
21
51
|
interface CloseOutcome {
|
|
22
52
|
name: string;
|
|
23
|
-
kept: boolean;
|
|
24
53
|
failure?: string;
|
|
25
54
|
}
|
|
26
55
|
|
|
27
|
-
async function closePane(state: RunState, run: RunRecord, pane: RunPane): Promise<CloseOutcome> {
|
|
28
|
-
try {
|
|
29
|
-
const outcome = await state.host.closeTabIfOnlyPane(pane);
|
|
30
|
-
return { name: run.name, kept: outcome === "kept" };
|
|
31
|
-
} catch (error) {
|
|
32
|
-
return { name: run.name, kept: false, failure: errorText(error) };
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
56
|
export async function closeRuns(state: RunState, runs: RunRecord[]): Promise<CloseChildrenResult> {
|
|
37
|
-
const targets = runs.filter((run)
|
|
38
|
-
const outcomes = await Promise.all(
|
|
57
|
+
const targets = runs.filter((run) => run.pane !== undefined && !run.paneClosed);
|
|
58
|
+
const outcomes: CloseOutcome[] = await Promise.all(
|
|
59
|
+
targets.map(async (run) => {
|
|
60
|
+
try {
|
|
61
|
+
await closeRunPane(state, run);
|
|
62
|
+
return { name: run.name };
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return { name: run.name, failure: errorText(error) };
|
|
65
|
+
}
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
39
68
|
return {
|
|
40
|
-
kept: outcomes.filter((outcome) => outcome.kept).map((outcome) => outcome.name),
|
|
41
69
|
failed: outcomes.flatMap((outcome) =>
|
|
42
70
|
outcome.failure === undefined ? [] : [{ name: outcome.name, reason: outcome.failure }],
|
|
43
71
|
),
|
package/src/run/index.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dataDir } from "../config/index.ts";
|
|
3
|
-
import { agentDir } from "../profile/index.ts";
|
|
1
|
+
import { errorText } from "../error/index.ts";
|
|
4
2
|
import { createAggregator } from "./aggregate.ts";
|
|
5
3
|
import { type CloseChildrenResult, closeRuns } from "./close.ts";
|
|
6
4
|
import { createNamer } from "./naming.ts";
|
|
7
|
-
import { stopPoll } from "./poll.ts";
|
|
8
5
|
import type { ResultHandler, RunResult } from "./result.ts";
|
|
9
6
|
import { type SendResult, send } from "./send.ts";
|
|
10
7
|
import { type RunStarted, start } from "./start.ts";
|
|
11
8
|
import type { RunRecord, RunServiceDeps, RunState } from "./state.ts";
|
|
9
|
+
import { startWatch, stopWatch } from "./watch.ts";
|
|
12
10
|
|
|
13
11
|
export interface RunService {
|
|
14
12
|
run(profile: string, task: string, cwd?: string): Promise<RunStarted>;
|
|
@@ -19,45 +17,41 @@ export interface RunService {
|
|
|
19
17
|
active(): string[];
|
|
20
18
|
}
|
|
21
19
|
|
|
22
|
-
function stopService(state: RunState): void {
|
|
23
|
-
stopPoll(state);
|
|
24
|
-
state.results?.stop();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
20
|
function takeRuns(state: RunState): RunRecord[] {
|
|
28
21
|
const runs = [...state.runs];
|
|
29
22
|
for (const run of runs) {
|
|
30
|
-
state.
|
|
23
|
+
state.release(run);
|
|
31
24
|
}
|
|
32
|
-
state.runs.length = 0;
|
|
33
25
|
return runs;
|
|
34
26
|
}
|
|
35
27
|
|
|
36
28
|
export function createRunService(deps: RunServiceDeps): RunService {
|
|
37
29
|
const state: RunState = {
|
|
38
30
|
...deps,
|
|
39
|
-
sessionsDir: join(dataDir(agentDir()), "sessions"),
|
|
40
31
|
runs: [],
|
|
41
32
|
pending: new Set(),
|
|
42
33
|
stopped: false,
|
|
43
|
-
activeUntil: 0,
|
|
44
|
-
now: deps.now ?? Date.now,
|
|
45
34
|
resultHandler: undefined,
|
|
46
35
|
results: undefined,
|
|
47
|
-
|
|
36
|
+
scanning: false,
|
|
37
|
+
scanAgain: false,
|
|
38
|
+
now: deps.now ?? Date.now,
|
|
39
|
+
delay:
|
|
40
|
+
deps.delay ??
|
|
41
|
+
((ms) =>
|
|
42
|
+
new Promise<void>((resolve) => {
|
|
43
|
+
const timer = setTimeout(resolve, ms);
|
|
44
|
+
timer.unref?.();
|
|
45
|
+
})),
|
|
48
46
|
nextName: createNamer(),
|
|
49
47
|
active() {
|
|
50
|
-
return state.runs.filter((run) => run.
|
|
48
|
+
return state.runs.filter((run) => run.phase === "running").map((run) => run.name);
|
|
51
49
|
},
|
|
52
50
|
available() {
|
|
53
51
|
return state.runs.map((run) => run.name);
|
|
54
52
|
},
|
|
55
|
-
settle(run) {
|
|
56
|
-
run.live = false;
|
|
57
|
-
run.read = undefined;
|
|
58
|
-
},
|
|
59
53
|
release(run) {
|
|
60
|
-
|
|
54
|
+
run.phase = "closed";
|
|
61
55
|
const index = state.runs.indexOf(run);
|
|
62
56
|
if (index >= 0) {
|
|
63
57
|
state.runs.splice(index, 1);
|
|
@@ -67,8 +61,9 @@ export function createRunService(deps: RunServiceDeps): RunService {
|
|
|
67
61
|
state.results = createAggregator<RunResult>({
|
|
68
62
|
windowMs: deps.aggregateWindowMs,
|
|
69
63
|
deliver: (batch) => state.resultHandler?.(batch),
|
|
70
|
-
onError:
|
|
64
|
+
onError: (error) => state.onProblem(errorText(error)),
|
|
71
65
|
});
|
|
66
|
+
startWatch(state);
|
|
72
67
|
|
|
73
68
|
return {
|
|
74
69
|
run: (profile, task, cwd) => start(state, profile, task, cwd),
|
|
@@ -78,15 +73,14 @@ export function createRunService(deps: RunServiceDeps): RunService {
|
|
|
78
73
|
},
|
|
79
74
|
shutdown() {
|
|
80
75
|
state.stopped = true;
|
|
81
|
-
|
|
76
|
+
stopWatch(state);
|
|
82
77
|
takeRuns(state);
|
|
83
78
|
},
|
|
84
79
|
async closeChildren() {
|
|
85
80
|
state.stopped = true;
|
|
86
|
-
|
|
81
|
+
stopWatch(state);
|
|
87
82
|
await Promise.allSettled([...state.pending]);
|
|
88
83
|
const runs = takeRuns(state);
|
|
89
|
-
stopService(state);
|
|
90
84
|
return closeRuns(state, runs);
|
|
91
85
|
},
|
|
92
86
|
active: () => state.active(),
|
|
@@ -95,9 +89,9 @@ export function createRunService(deps: RunServiceDeps): RunService {
|
|
|
95
89
|
|
|
96
90
|
export type { CloseChildrenResult } from "./close.ts";
|
|
97
91
|
export { ToolError } from "./errors.ts";
|
|
92
|
+
export { childSessionsDir, runBaseOf, runsDir } from "./paths.ts";
|
|
98
93
|
export { profileLabel } from "./resolve.ts";
|
|
99
|
-
export type { ResultHandler, RunResult
|
|
94
|
+
export type { ResultHandler, RunResult } from "./result.ts";
|
|
100
95
|
export type { SendResult } from "./send.ts";
|
|
101
|
-
export { AUTO_EXIT_ENV } from "./spawn-args.ts";
|
|
102
96
|
export type { RunStarted } from "./start.ts";
|
|
103
97
|
export type { RunServiceDeps };
|
package/src/run/pane.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { samePath } from "./paths.ts";
|
|
2
|
-
import {
|
|
2
|
+
import { parentCwd } from "./spawn-args.ts";
|
|
3
3
|
import type { RunPane, RunRecord, RunState } from "./state.ts";
|
|
4
4
|
|
|
5
5
|
export async function createPane(state: RunState, run: RunRecord, cwd: string, label: string): Promise<RunPane> {
|
|
6
|
-
const env = { [AUTO_EXIT_ENV]: run.autoExit ? "1" : "0" };
|
|
7
6
|
const inParent = samePath(cwd, parentCwd());
|
|
8
7
|
const tab = inParent
|
|
9
|
-
? await state.host.createTab({ cwd, label: run.name
|
|
10
|
-
: await state.host.createWorkspace({ cwd, label: run.name
|
|
8
|
+
? await state.host.createTab({ cwd, label: run.name })
|
|
9
|
+
: await state.host.createWorkspace({ cwd, label: run.name });
|
|
11
10
|
if (!inParent) {
|
|
12
11
|
await state.host.renameTab({ tabId: tab.tabId, label: run.name });
|
|
13
12
|
}
|
package/src/run/paths.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
import { relative } from "node:path";
|
|
1
|
+
import { basename, dirname, join, relative } from "node:path";
|
|
2
|
+
import { dataDir } from "../config/index.ts";
|
|
3
|
+
import { agentDir } from "../profile/index.ts";
|
|
2
4
|
|
|
3
5
|
export function samePath(left: string, right: string): boolean {
|
|
4
6
|
return relative(left, right) === "";
|
|
5
7
|
}
|
|
8
|
+
|
|
9
|
+
export function runsDir(): string {
|
|
10
|
+
return join(dataDir(agentDir()), "runs");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// run 的附属文件共用一个前缀:brief、identity、inbox、turn 事件、结果 artifact。
|
|
14
|
+
export function runBaseOf(sessionFile: string): string {
|
|
15
|
+
return join(runsDir(), basename(sessionFile, ".jsonl"));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// 子会话进父会话所在的桶,与父会话同为 sessions 下的目录。
|
|
19
|
+
export function childSessionsDir(parentSessionFile: string | undefined): string {
|
|
20
|
+
const sessionsDir = parentSessionFile === undefined ? join(agentDir(), "sessions") : dirname(parentSessionFile);
|
|
21
|
+
return join(sessionsDir, "runs");
|
|
22
|
+
}
|
package/src/run/result.ts
CHANGED
|
@@ -3,49 +3,44 @@ import { writeArtifact } from "./artifact.ts";
|
|
|
3
3
|
import type { RunRecord } from "./state.ts";
|
|
4
4
|
import { leadingChars } from "./truncate.ts";
|
|
5
5
|
|
|
6
|
-
export
|
|
7
|
-
|
|
6
|
+
export type RunOutcome = "done" | "error" | "aborted" | "quit" | "lost";
|
|
7
|
+
|
|
8
|
+
export interface RunResult {
|
|
9
|
+
state: RunOutcome;
|
|
8
10
|
name: string;
|
|
9
|
-
text
|
|
11
|
+
text?: string;
|
|
12
|
+
errorMessage?: string;
|
|
10
13
|
artifact?: string;
|
|
11
14
|
warning?: string;
|
|
12
|
-
sessionFile: string;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
export type
|
|
17
|
+
export type ResultHandler = (results: RunResult[]) => void | Promise<void>;
|
|
16
18
|
|
|
17
|
-
export interface
|
|
18
|
-
state:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
sessionFile: string;
|
|
19
|
+
export interface ResultInput {
|
|
20
|
+
state: RunOutcome;
|
|
21
|
+
text?: string | null;
|
|
22
|
+
errorMessage?: string;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
if (text.length <= limit) {
|
|
31
|
-
|
|
25
|
+
export async function makeResult(run: RunRecord, input: ResultInput, limit: number): Promise<RunResult> {
|
|
26
|
+
const base: RunResult = { state: input.state, name: run.name };
|
|
27
|
+
if (input.errorMessage !== undefined) {
|
|
28
|
+
base.errorMessage = input.errorMessage;
|
|
29
|
+
}
|
|
30
|
+
const text = input.text ?? undefined;
|
|
31
|
+
if (text === undefined || text.length <= limit) {
|
|
32
|
+
if (text !== undefined) {
|
|
33
|
+
base.text = text;
|
|
34
|
+
}
|
|
35
|
+
return base;
|
|
32
36
|
}
|
|
33
37
|
const head = leadingChars(text, limit);
|
|
34
38
|
const seq = run.artifactSeq + 1;
|
|
35
39
|
try {
|
|
36
|
-
const artifact = await writeArtifact(run.
|
|
40
|
+
const artifact = await writeArtifact(run.runBase, seq, text);
|
|
37
41
|
run.artifactSeq = seq;
|
|
38
42
|
return { ...base, text: head, artifact };
|
|
39
43
|
} catch (error) {
|
|
40
44
|
return { ...base, text: head, warning: `[artifact write failed: ${errorText(error)}]` };
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
|
-
|
|
44
|
-
export function unknownResult(run: RunRecord, reason: UnknownReason): UnknownResult {
|
|
45
|
-
return {
|
|
46
|
-
state: "unknown",
|
|
47
|
-
name: run.name,
|
|
48
|
-
reason,
|
|
49
|
-
sessionFile: run.sessionFile,
|
|
50
|
-
};
|
|
51
|
-
}
|
package/src/run/send.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { writeFile } from "node:fs/promises";
|
|
3
3
|
import { createInbox, enqueue } from "../inbox/index.ts";
|
|
4
|
-
import { closeTab } from "./close.ts";
|
|
5
4
|
import { spawnFailed, ToolError } from "./errors.ts";
|
|
6
5
|
import { findAgent } from "./find.ts";
|
|
7
6
|
import { createPane } from "./pane.ts";
|
|
8
|
-
import { wake } from "./poll.ts";
|
|
9
7
|
import { resolveProfile, resolveSkills } from "./resolve.ts";
|
|
10
8
|
import { requireSlot } from "./slot.ts";
|
|
11
9
|
import { agentArgs } from "./spawn-args.ts";
|
|
12
|
-
import type { RunState } from "./state.ts";
|
|
10
|
+
import type { RunRecord, RunState } from "./state.ts";
|
|
11
|
+
import { watchRun } from "./watch.ts";
|
|
13
12
|
|
|
14
13
|
export interface SentResult {
|
|
15
14
|
state: "sent";
|
|
@@ -39,30 +38,36 @@ export async function send(state: RunState, name: string, message: string): Prom
|
|
|
39
38
|
throw new ToolError("spawn_failed", "the session is shutting down");
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!run.live) {
|
|
45
|
-
requireSlot(state);
|
|
46
|
-
run.lastHandledSeq = agent.completionSeq;
|
|
47
|
-
run.live = true;
|
|
48
|
-
run.pollFailures = 0;
|
|
49
|
-
}
|
|
50
|
-
wake(state);
|
|
51
|
-
await enqueue(run.sessionFile, inboxMessage(message));
|
|
41
|
+
if (run.phase === "running") {
|
|
42
|
+
await enqueue(run.runBase, inboxMessage(message));
|
|
52
43
|
return { state: "sent", name };
|
|
53
44
|
}
|
|
45
|
+
// pane 已关的 idle 不用再查 agent,直接重启。
|
|
46
|
+
if (run.phase === "idle" && !run.paneClosed) {
|
|
47
|
+
const agent = findAgent(await state.host.listAgents(), run);
|
|
48
|
+
if (agent) {
|
|
49
|
+
run.absentChecks = 0;
|
|
50
|
+
run.phase = "running";
|
|
51
|
+
watchRun(state, run);
|
|
52
|
+
await enqueue(run.runBase, inboxMessage(message));
|
|
53
|
+
return { state: "sent", name };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return resume(state, run, message);
|
|
57
|
+
}
|
|
54
58
|
|
|
59
|
+
async function resume(state: RunState, run: RunRecord, message: string): Promise<SendResult> {
|
|
55
60
|
if (!existsSync(run.sessionFile)) {
|
|
56
|
-
throw new ToolError("session_missing", `
|
|
61
|
+
throw new ToolError("session_missing", `"${run.name}" has no child session to resume; start a new run`);
|
|
57
62
|
}
|
|
58
|
-
const tail = state.
|
|
63
|
+
const tail = state.readSessionTail(run.sessionFile);
|
|
59
64
|
if (tail.unreadable) {
|
|
60
|
-
throw new ToolError("session_unreadable", `
|
|
65
|
+
throw new ToolError("session_unreadable", `"${run.name}" has a child session that cannot be read; start a new run`);
|
|
61
66
|
}
|
|
62
67
|
if (tail.pendingToolCall) {
|
|
63
68
|
throw new ToolError(
|
|
64
69
|
"tail_dirty",
|
|
65
|
-
`"${name}" cannot restart: its session ends with an unfinished tool call. Wait for the child to finish, or start a new run.`,
|
|
70
|
+
`"${run.name}" cannot restart: its session ends with an unfinished tool call. Wait for the child to finish, or start a new run.`,
|
|
66
71
|
);
|
|
67
72
|
}
|
|
68
73
|
|
|
@@ -70,54 +75,36 @@ export async function send(state: RunState, name: string, message: string): Prom
|
|
|
70
75
|
requireSlot(state);
|
|
71
76
|
const cwd = run.cwd;
|
|
72
77
|
const skillPaths = await resolveSkills(profile, cwd, state.trusted);
|
|
73
|
-
const identityFile = `${run.
|
|
78
|
+
const identityFile = `${run.runBase}.identity.md`;
|
|
74
79
|
const previousPaneId = run.pane?.paneId;
|
|
75
|
-
const envChanged = run.autoExit !== profile.autoExit;
|
|
76
80
|
run.autoExit = profile.autoExit;
|
|
77
81
|
|
|
78
|
-
if (run.pane) {
|
|
79
|
-
|
|
80
|
-
if (!paneAlive) {
|
|
81
|
-
run.pane = undefined;
|
|
82
|
-
} else if (envChanged) {
|
|
83
|
-
try {
|
|
84
|
-
await closeTab(state, run);
|
|
85
|
-
} catch (error) {
|
|
86
|
-
throw spawnFailed(error, previousPaneId, false);
|
|
87
|
-
}
|
|
88
|
-
run.pane = undefined;
|
|
89
|
-
}
|
|
82
|
+
if (run.pane !== undefined && !(await state.host.hasPane(run.pane.paneId))) {
|
|
83
|
+
run.pane = undefined;
|
|
90
84
|
}
|
|
91
85
|
|
|
92
|
-
run.read = undefined;
|
|
93
86
|
try {
|
|
94
87
|
const pane = run.pane ?? (await createPane(state, run, cwd, run.paneLabel || run.name));
|
|
95
88
|
run.pane = pane;
|
|
89
|
+
run.paneClosed = false;
|
|
96
90
|
if (profile.systemPrompt !== "task") {
|
|
97
91
|
await writeFile(identityFile, profile.body, "utf8");
|
|
98
92
|
}
|
|
99
|
-
createInbox(run.
|
|
93
|
+
createInbox(run.runBase);
|
|
100
94
|
await state.host.startAgent({
|
|
101
95
|
name: run.name,
|
|
102
96
|
paneId: pane.paneId,
|
|
103
97
|
sessionFile: run.sessionFile,
|
|
104
98
|
agentArgs: agentArgs(profile, identityFile, state.toolName, skillPaths),
|
|
105
99
|
});
|
|
106
|
-
const baseline = findAgent(await state.host.listAgents(), run);
|
|
107
|
-
if (!baseline) {
|
|
108
|
-
throw new Error(`agent "${run.name}" did not register after resume`);
|
|
109
|
-
}
|
|
110
|
-
run.lastHandledSeq = baseline.completionSeq;
|
|
111
100
|
} catch (error) {
|
|
112
|
-
|
|
101
|
+
run.phase = "dead";
|
|
113
102
|
throw spawnFailed(error, run.pane?.paneId, run.pane?.paneId !== previousPaneId);
|
|
114
103
|
}
|
|
115
104
|
|
|
116
|
-
run.
|
|
117
|
-
run.
|
|
118
|
-
run
|
|
119
|
-
run.
|
|
120
|
-
wake(state);
|
|
121
|
-
await enqueue(run.sessionFile, inboxMessage(message));
|
|
105
|
+
run.phase = "running";
|
|
106
|
+
run.absentChecks = 0;
|
|
107
|
+
watchRun(state, run);
|
|
108
|
+
await enqueue(run.runBase, inboxMessage(message));
|
|
122
109
|
return { state: "restarted", name: run.name, paneId: run.pane.paneId };
|
|
123
110
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { errorText } from "../error/index.ts";
|
|
2
|
+
import { removeTurn, type Turn } from "../turn/index.ts";
|
|
3
|
+
import { closeRunPane, shouldClose } from "./close.ts";
|
|
4
|
+
import { makeResult } from "./result.ts";
|
|
5
|
+
import type { RunRecord, RunState } from "./state.ts";
|
|
6
|
+
|
|
7
|
+
const DELIVERY_ATTEMPTS = 3;
|
|
8
|
+
|
|
9
|
+
// 一个回合只走这一条收尾路径:投递、删除、决定关闭。
|
|
10
|
+
export async function settleTurn(state: RunState, run: RunRecord, file: string, turn: Turn): Promise<void> {
|
|
11
|
+
if (run.phase === "closed") {
|
|
12
|
+
removeTurn(file);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (turn.turn <= run.lastTurn) {
|
|
16
|
+
removeTurn(file);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const result = await makeResult(
|
|
20
|
+
run,
|
|
21
|
+
{ state: turn.outcome, text: turn.text, errorMessage: turn.errorMessage },
|
|
22
|
+
state.maxResultChars,
|
|
23
|
+
);
|
|
24
|
+
const delivered = (await state.results?.push(result)) ?? false;
|
|
25
|
+
if (!delivered) {
|
|
26
|
+
run.deliveryFailures += 1;
|
|
27
|
+
if (run.deliveryFailures < DELIVERY_ATTEMPTS) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
run.deliveryFailures = 0;
|
|
32
|
+
run.lastTurn = turn.turn;
|
|
33
|
+
removeTurn(file);
|
|
34
|
+
await afterTurn(state, run, turn);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 回合结束不删记录:pane 关了也要留着 sessionFile,send 才能续。
|
|
38
|
+
async function afterTurn(state: RunState, run: RunRecord, turn: Turn): Promise<void> {
|
|
39
|
+
if (shouldClose(state, run, turn)) {
|
|
40
|
+
await closeOrReport(state, run);
|
|
41
|
+
}
|
|
42
|
+
run.phase = "idle";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function closeOrReport(state: RunState, run: RunRecord): Promise<void> {
|
|
46
|
+
try {
|
|
47
|
+
await closeRunPane(state, run);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
state.onProblem(`could not close the pane for "${run.name}": ${errorText(error)}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 没有回合事件、agent 又连续缺席:不猜结果,报 lost。记录留着,send 可以续。
|
|
54
|
+
export async function settleLost(state: RunState, run: RunRecord): Promise<void> {
|
|
55
|
+
const result = await makeResult(run, { state: "lost" }, state.maxResultChars);
|
|
56
|
+
await state.results?.push(result);
|
|
57
|
+
run.phase = "dead";
|
|
58
|
+
if (run.pane !== undefined && !run.paneClosed) {
|
|
59
|
+
try {
|
|
60
|
+
await closeRunPane(state, run);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
state.onProblem(`could not close the pane for "${run.name}": ${errorText(error)}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/run/spawn-args.ts
CHANGED