@zhuxixi/pi-agent-board 0.12.1 → 0.13.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 +22 -0
- package/README.md +7 -7
- package/docs/superpowers/plans/2026-10-21-issue-221-ask-user-question.md +198 -0
- package/docs/superpowers/plans/2026-10-23-issue-225-monotonic-foreground-state.md +450 -0
- package/docs/superpowers/plans/2026-10-23-issue-229-vim-jkl.md +449 -0
- package/docs/superpowers/plans/2026-10-23-issue-232-attach-bg-only-cells.md +153 -0
- package/docs/superpowers/plans/2026-10-23-lease-heartbeat-staleness.md +537 -0
- package/docs/superpowers/plans/2026-10-23-selected-row-highlight.md +405 -0
- package/docs/superpowers/plans/2026-10-24-issue-217-windows-pid-reuse-stopping.md +367 -0
- package/docs/superpowers/plans/2026-10-24-issue-230-warm-host-background-runs.md +806 -0
- package/docs/superpowers/plans/2026-10-24-issue-238-windows-process-identity-token.md +490 -0
- package/docs/superpowers/plans/2026-10-24-issue-241-coordinator-root-exit.md +461 -0
- package/docs/superpowers/plans/2026-10-25-issue-239-coordinator-replacement-identity.md +504 -0
- package/docs/superpowers/specs/2026-10-20-issue-221-ask-user-question-design.md +270 -0
- package/docs/superpowers/specs/2026-10-22-issue-225-monotonic-foreground-state-design.md +225 -0
- package/docs/superpowers/specs/2026-10-23-issue-229-vim-jkl-design.md +90 -0
- package/docs/superpowers/specs/2026-10-23-issue-232-attach-bg-only-cells-design.md +146 -0
- package/docs/superpowers/specs/2026-10-23-lease-heartbeat-staleness-design.md +271 -0
- package/docs/superpowers/specs/2026-10-23-selected-row-highlight-design.md +191 -0
- package/docs/superpowers/specs/2026-10-24-issue-217-windows-pid-reuse-stopping-design.md +334 -0
- package/docs/superpowers/specs/2026-10-24-issue-230-warm-host-background-runs-design.md +283 -0
- package/docs/superpowers/specs/2026-10-24-issue-238-windows-process-identity-token-design.md +286 -0
- package/docs/superpowers/specs/2026-10-24-issue-241-coordinator-root-exit-design.md +381 -0
- package/docs/superpowers/specs/2026-10-25-issue-239-coordinator-replacement-identity-design.md +331 -0
- package/package.json +3 -3
- package/runner/pty-runner.mjs +18 -22
- package/runner/state-coordinator.mjs +213 -26
- package/scripts/run-tests.mjs +375 -0
- package/src/core/background-runs.mjs +273 -0
- package/src/core/coordinator-client.mjs +346 -26
- package/src/core/diagnostics.mjs +24 -0
- package/src/core/events.mjs +16 -3
- package/src/core/foreground-projection-cache.mjs +99 -0
- package/src/core/host-coordination.mjs +69 -1
- package/src/core/launch.mjs +21 -1
- package/src/core/locks.mjs +176 -31
- package/src/core/pid.mjs +4 -2
- package/src/core/process-identity-probe.mjs +228 -0
- package/src/core/state-commands.mjs +5 -1
- package/src/core/types.mjs +1 -0
- package/src/core/warm-host-sweeper.mjs +7 -2
- package/src/runtime/service.mjs +385 -73
- package/src/ui/dashboard.ts +48 -25
- package/src/ui/pty-attach.ts +14 -6
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background-workflow run detection for warm-host eviction (issue #230).
|
|
3
|
+
*
|
|
4
|
+
* A warm host whose session owns a `running` pi-workflow run must not be swept:
|
|
5
|
+
* eviction revokes the host, the child pi gets SIGTERM, and the in-flight agent
|
|
6
|
+
* step turns into a `skipped/interrupted` event — the run ends `paused` while the
|
|
7
|
+
* user is never told. The sweeper cannot see this from row state because
|
|
8
|
+
* `workflow({ background: true })` returns `started` before the step finishes, so
|
|
9
|
+
* the row falls back to `idle` and looks evictable.
|
|
10
|
+
*
|
|
11
|
+
* This module is the decision core plus its thin IO shell: parse a run head
|
|
12
|
+
* record, match it to a row's session file, classify the protection outcome, and
|
|
13
|
+
* enumerate/probe the on-disk run store (`scanBackgroundRuns`/`probeBackgroundRun`).
|
|
14
|
+
* Design: docs/superpowers/specs/2026-10-24-issue-230-warm-host-background-runs-design.md
|
|
15
|
+
*/
|
|
16
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
17
|
+
import * as os from "node:os";
|
|
18
|
+
import * as path from "node:path";
|
|
19
|
+
|
|
20
|
+
/** Timestamp tolerance for FS mtime granularity and cross-process clock skew
|
|
21
|
+
* when comparing a run's `updatedAt` against the host start time (spec D1). */
|
|
22
|
+
export const CLOCK_EPSILON_MS = 2000;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {Object} RunHead
|
|
26
|
+
* @property {string} runId
|
|
27
|
+
* @property {string} status
|
|
28
|
+
* @property {string|null} parentSessionFile legacy records carry none
|
|
29
|
+
* @property {number|null} startedAtMs
|
|
30
|
+
* @property {number|null} updatedAtMs
|
|
31
|
+
* @property {boolean} hasCheckpoint
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/** @typedef {"protecting"|"paused"|"irrelevant"} RunVerdict */
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Root directory of pi-dynamic-workflows: `$AGENT_BOARD_WORKFLOW_HOME` (explicit
|
|
38
|
+
* override) > `$PI_CODING_AGENT_DIR/workflows` > `<home>/.pi/workflows`.
|
|
39
|
+
* Both `env` and `home` are injectable so callers and tests never depend on the
|
|
40
|
+
* ambient environment or the real homedir.
|
|
41
|
+
* @param {{ env?: Record<string, string|undefined>, home?: string }} [o]
|
|
42
|
+
* @returns {string}
|
|
43
|
+
*/
|
|
44
|
+
export function resolveWorkflowHome({ env = process.env, home = os.homedir() } = {}) {
|
|
45
|
+
const expand = (p) => (p.startsWith("~") ? home + p.slice(1) : p);
|
|
46
|
+
const explicit = env.AGENT_BOARD_WORKFLOW_HOME;
|
|
47
|
+
if (typeof explicit === "string" && explicit.length > 0) return expand(explicit);
|
|
48
|
+
const agentDir = env.PI_CODING_AGENT_DIR;
|
|
49
|
+
const base = typeof agentDir === "string" && agentDir.length > 0 ? expand(agentDir) : joinPath(home, ".pi");
|
|
50
|
+
return joinPath(base, "workflows");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Parse a run head record into the fields the protection decision needs, or
|
|
55
|
+
* `null` when it is not a workflow run. A v2 record nests the run fields under
|
|
56
|
+
* `index` but keeps `summary` (and therefore the checkpoint) as a sibling at the
|
|
57
|
+
* record root; legacy flat records carry `checkpoint` at the root themselves and
|
|
58
|
+
* can never match a row because they have no `parentSessionFile` (spec non-goal:
|
|
59
|
+
* no legacy session matching).
|
|
60
|
+
* Never throws: the input is external, unversioned JSON.
|
|
61
|
+
* @param {unknown} raw parsed JSON
|
|
62
|
+
* @returns {RunHead|null}
|
|
63
|
+
*/
|
|
64
|
+
export function parseRunHead(raw) {
|
|
65
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return null;
|
|
66
|
+
const record = /** @type {Record<string, any>} */ (raw);
|
|
67
|
+
const source = record.format === "pi-workflow-run-v2" ? record.index : record;
|
|
68
|
+
if (typeof source !== "object" || source === null || Array.isArray(source)) return null;
|
|
69
|
+
if (typeof source.runId !== "string") return null;
|
|
70
|
+
return {
|
|
71
|
+
runId: source.runId,
|
|
72
|
+
status: typeof source.status === "string" ? source.status : "",
|
|
73
|
+
parentSessionFile: typeof source.parentSessionFile === "string" ? source.parentSessionFile : null,
|
|
74
|
+
startedAtMs: toMs(source.startedAt),
|
|
75
|
+
updatedAtMs: toMs(source.updatedAt),
|
|
76
|
+
hasCheckpoint: Boolean(record.summary?.checkpoint ?? source.checkpoint),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether two spellings point at the same session file. win32 compares
|
|
82
|
+
* case-insensitively (the row meta and the workflow record disagree on drive
|
|
83
|
+
* letter and segment casing; they also mix `\` and `/`); POSIX stays
|
|
84
|
+
* case-sensitive. `platform` is injectable because CI only runs Linux, so the
|
|
85
|
+
* win32 branch would otherwise be untested (spec A5).
|
|
86
|
+
* @param {string} a
|
|
87
|
+
* @param {string} b
|
|
88
|
+
* @param {{ platform?: string }} [o]
|
|
89
|
+
* @returns {boolean}
|
|
90
|
+
*/
|
|
91
|
+
export function isSameSessionFile(a, b, { platform = process.platform } = {}) {
|
|
92
|
+
if (typeof a !== "string" || typeof b !== "string" || a.length === 0 || b.length === 0) return false;
|
|
93
|
+
const impl = platform === "win32" ? path.win32 : path.posix;
|
|
94
|
+
const na = impl.resolve(a);
|
|
95
|
+
const nb = impl.resolve(b);
|
|
96
|
+
return platform === "win32" ? na.toLowerCase() === nb.toLowerCase() : na === nb;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Protection verdict for one run against one row (spec D1/D2).
|
|
101
|
+
*
|
|
102
|
+
* Order matters: a run belonging to another session is always irrelevant (a
|
|
103
|
+
* running or paused run of a sibling row must never keep this host alive);
|
|
104
|
+
* `paused` is reported separately because a paused run needs no host at all and
|
|
105
|
+
* only drives the "run was interrupted" notice; a running run protects only when
|
|
106
|
+
* it was updated after the host started, so a stale `running` record left behind
|
|
107
|
+
* by a previous host incarnation cannot exempt the row forever.
|
|
108
|
+
* @param {RunHead|null|undefined} run
|
|
109
|
+
* @param {{ sessionFile: string|null|undefined, hostStartedAt: number|null|undefined }} o
|
|
110
|
+
* @returns {RunVerdict}
|
|
111
|
+
*/
|
|
112
|
+
export function classifyRunForRow(run, { sessionFile, hostStartedAt }) {
|
|
113
|
+
if (!run || !run.parentSessionFile) return "irrelevant";
|
|
114
|
+
if (!isSameSessionFile(run.parentSessionFile, sessionFile)) return "irrelevant";
|
|
115
|
+
if (run.status === "paused") return "paused";
|
|
116
|
+
if (run.status !== "running") return "irrelevant";
|
|
117
|
+
if (hostStartedAt == null || run.updatedAtMs == null) return "protecting";
|
|
118
|
+
if (run.updatedAtMs >= hostStartedAt - CLOCK_EPSILON_MS) return "protecting";
|
|
119
|
+
return "irrelevant";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Strongest verdict across a scanned head list: protecting > paused >
|
|
124
|
+
* irrelevant. Returns the run that produced it so callers can name its runId in
|
|
125
|
+
* a diagnostic or a row notice; `run` is null for "irrelevant".
|
|
126
|
+
* @param {Array<RunHead|null|undefined>} runs
|
|
127
|
+
* @param {{ sessionFile: string|null|undefined, hostStartedAt: number|null|undefined }} o
|
|
128
|
+
* @returns {{ state: RunVerdict, run: RunHead|null }}
|
|
129
|
+
*/
|
|
130
|
+
export function classifyRunsForRow(runs, o) {
|
|
131
|
+
let state = /** @type {RunVerdict} */ ("irrelevant");
|
|
132
|
+
let match = null;
|
|
133
|
+
for (const run of Array.isArray(runs) ? runs : []) {
|
|
134
|
+
const verdict = classifyRunForRow(run, o);
|
|
135
|
+
if (verdict === "protecting") return { state: "protecting", run };
|
|
136
|
+
if (verdict === "paused" && state === "irrelevant") {
|
|
137
|
+
state = "paused";
|
|
138
|
+
match = run;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { state, run: match };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @typedef {RunHead & { path: string, mtimeMs: number }} RunHeadWithPath
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Enumerate every run head under `<workflowHome>/projects/<project>/runs/`.
|
|
150
|
+
* Only `*.json` regular files are candidates (upstream keeps `.bak`,
|
|
151
|
+
* `.events.jsonl`, `.log` and `.result-*` siblings next to the head), and
|
|
152
|
+
* `minMtimeMs` drops heads too old to belong to a live host — a prefilter only,
|
|
153
|
+
* the protection verdict still comes from `classifyRunsForRow`.
|
|
154
|
+
*
|
|
155
|
+
* Failure policy (spec D6): an absent store is normal and yields `ok:true`; any
|
|
156
|
+
* incomplete scan yields `ok:false` so the caller logs a `warm_prune_probe_failed`
|
|
157
|
+
* diagnostic instead of silently guessing. An incomplete scan is any unreadable
|
|
158
|
+
* directory or head file, including corrupt JSON — the eviction outcome is the
|
|
159
|
+
* same either way (a run that cannot be read protects nothing), but only
|
|
160
|
+
* `ok:false` marks the coverage as incomplete (it does not disable protection).
|
|
161
|
+
* Heads that were read successfully are still returned and still participate in
|
|
162
|
+
* the protection classification; a run that could not be read simply has no
|
|
163
|
+
* evidence and protects nothing. A head file that parses but is not a run head
|
|
164
|
+
* (`parseRunHead` → null) is not an error: it is simply not a candidate.
|
|
165
|
+
* The store root is stat-ed before the child paths are enumerated because on
|
|
166
|
+
* win32 a child path under a regular file reports `ENOENT` exactly like a
|
|
167
|
+
* missing directory does: a missing `workflowHome` is "no store yet", a
|
|
168
|
+
* `workflowHome` that is not a directory is a read failure.
|
|
169
|
+
* @param {{ workflowHome: string, minMtimeMs?: number }} o
|
|
170
|
+
* @returns {{ ok: boolean, heads: Array<RunHeadWithPath>, error: string|null }}
|
|
171
|
+
*/
|
|
172
|
+
export function scanBackgroundRuns({ workflowHome, minMtimeMs = 0 }) {
|
|
173
|
+
const heads = [];
|
|
174
|
+
let error = null;
|
|
175
|
+
const noteError = (message) => {
|
|
176
|
+
error ??= message;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
let projects;
|
|
180
|
+
try {
|
|
181
|
+
if (!statSync(workflowHome).isDirectory()) {
|
|
182
|
+
return { ok: false, heads: [], error: `workflow home is not a directory: ${workflowHome}` };
|
|
183
|
+
}
|
|
184
|
+
projects = readdirSync(joinPath(workflowHome, "projects"), { withFileTypes: true });
|
|
185
|
+
} catch (e) {
|
|
186
|
+
if (e?.code === "ENOENT") return { ok: true, heads: [], error: null };
|
|
187
|
+
return { ok: false, heads: [], error: errorMessage(e) };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const project of projects) {
|
|
191
|
+
if (!project.isDirectory()) continue;
|
|
192
|
+
const runsDir = joinPath(workflowHome, "projects", project.name, "runs");
|
|
193
|
+
let entries;
|
|
194
|
+
try {
|
|
195
|
+
entries = readdirSync(runsDir, { withFileTypes: true });
|
|
196
|
+
} catch (e) {
|
|
197
|
+
if (e?.code !== "ENOENT") noteError(`${runsDir}: ${errorMessage(e)}`);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
for (const entry of entries) {
|
|
201
|
+
if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
|
|
202
|
+
const filePath = joinPath(runsDir, entry.name);
|
|
203
|
+
try {
|
|
204
|
+
const mtimeMs = statSync(filePath).mtimeMs;
|
|
205
|
+
if (mtimeMs < minMtimeMs) continue;
|
|
206
|
+
const head = parseRunHead(JSON.parse(readFileSync(filePath, "utf8")));
|
|
207
|
+
if (head) heads.push({ ...head, path: filePath, mtimeMs });
|
|
208
|
+
} catch (e) {
|
|
209
|
+
noteError(`${filePath}: ${errorMessage(e)}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return { ok: error === null, heads, error };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Fresh protection probe for one row: the scan prefiltered to runs that could
|
|
219
|
+
* have been updated by this host incarnation, classified against the row's
|
|
220
|
+
* session file. Each field answers one question and is filled independently
|
|
221
|
+
* (design §5.1: a running/paused split): `running` is the run that must keep the
|
|
222
|
+
* host alive (spec D1), `paused` is the run whose interruption the reconcile
|
|
223
|
+
* pass announces (spec D2). They coexist because one session may hold an older
|
|
224
|
+
* paused run while a new one is running, and each consumer reads one field.
|
|
225
|
+
* `ok:false` only means the store could not be read (spec D6).
|
|
226
|
+
* @param {{ sessionFile: string|null, hostStartedAt: number|null, workflowHome: string }} o
|
|
227
|
+
* @returns {{ ok: boolean, running: RunHeadWithPath|null, paused: RunHeadWithPath|null, error: string|null }}
|
|
228
|
+
*/
|
|
229
|
+
export function probeBackgroundRun({ sessionFile, hostStartedAt, workflowHome }) {
|
|
230
|
+
const minMtimeMs = hostStartedAt == null ? 0 : hostStartedAt - CLOCK_EPSILON_MS;
|
|
231
|
+
const scan = scanBackgroundRuns({ workflowHome, minMtimeMs });
|
|
232
|
+
if (!scan.ok) return { ok: false, running: null, paused: null, error: scan.error };
|
|
233
|
+
const verdict = classifyRunsForRow(scan.heads, { sessionFile, hostStartedAt });
|
|
234
|
+
const running = verdict.state === "protecting" ? verdict.run : null;
|
|
235
|
+
// Newest paused run wins, because it is the one most likely to be the run the
|
|
236
|
+
// just-ended host left behind; `path` only breaks ties deterministically.
|
|
237
|
+
const paused = scan.heads
|
|
238
|
+
.filter((head) => classifyRunForRow(head, { sessionFile, hostStartedAt }) === "paused")
|
|
239
|
+
.sort((a, b) => (b.updatedAtMs ?? 0) - (a.updatedAtMs ?? 0) || a.path.localeCompare(b.path))[0] ?? null;
|
|
240
|
+
return { ok: true, running, paused, error: scan.error };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Join segments with the separator style of the base path. The ambient
|
|
245
|
+
* `path.join` uses the host separator, which would turn POSIX-style injected
|
|
246
|
+
* inputs (tests, Linux CI) into backslash paths when the suite runs on Windows,
|
|
247
|
+
* so the style is read from the base instead of the platform.
|
|
248
|
+
* @param {string} base
|
|
249
|
+
* @param {...string} parts
|
|
250
|
+
* @returns {string}
|
|
251
|
+
*/
|
|
252
|
+
function joinPath(base, ...parts) {
|
|
253
|
+
const impl = /^[A-Za-z]:/.test(base) || base.includes("\\") ? path.win32 : path.posix;
|
|
254
|
+
return impl.join(base, ...parts);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {unknown} e
|
|
259
|
+
* @returns {string}
|
|
260
|
+
*/
|
|
261
|
+
function errorMessage(e) {
|
|
262
|
+
return e instanceof Error ? e.message : String(e);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @param {unknown} value
|
|
267
|
+
* @returns {number|null}
|
|
268
|
+
*/
|
|
269
|
+
function toMs(value) {
|
|
270
|
+
if (typeof value !== "string") return null;
|
|
271
|
+
const ms = Date.parse(value);
|
|
272
|
+
return Number.isFinite(ms) ? ms : null;
|
|
273
|
+
}
|