pi-better-background-tasks 0.13.0 → 0.14.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/README.md +131 -4
- package/package.json +3 -1
- package/src/adapter-contract.ts +16 -2
- package/src/batch.ts +108 -0
- package/src/delivery-observer.ts +42 -0
- package/src/goal-provider.ts +72 -5
- package/src/index.ts +39 -0
- package/src/navigator-provider.ts +175 -6
- package/src/observed-waits.ts +3677 -0
- package/src/output.ts +22 -2
- package/src/process.ts +100 -1
- package/src/runtime.ts +189 -10
- package/src/sandbox.ts +276 -2
- package/src/shared-callback-batcher.ts +18 -1
- package/src/shared-execution-policy.ts +2 -0
- package/src/shared-failure-observations.ts +78 -24
- package/src/shared-log-utils.ts +277 -31
- package/src/shared-operation-coordinator/index.ts +113 -0
- package/src/shared-operation-coordinator/src/errors.ts +75 -0
- package/src/shared-operation-coordinator/src/operations.ts +376 -0
- package/src/shared-operation-coordinator/src/outbox.ts +431 -0
- package/src/shared-operation-coordinator/src/rows.ts +439 -0
- package/src/shared-operation-coordinator/src/schema.ts +244 -0
- package/src/shared-operation-coordinator/src/sqlite.ts +239 -0
- package/src/shared-operation-coordinator/src/store.ts +397 -0
- package/src/shared-operation-coordinator/src/types.ts +648 -0
- package/src/shared-operation-coordinator/src/validate.ts +239 -0
- package/src/shared-operation-coordinator/src/waits.ts +1378 -0
- package/src/stall.ts +4 -1
- package/src/supervisor/README.md +111 -0
- package/src/supervisor/emit-runner.mjs +101 -0
- package/src/supervisor/generated/conditions.mjs +96 -0
- package/src/supervisor/generated/identity.mjs +95 -0
- package/src/supervisor/generated/process-identity.mjs +43 -0
- package/src/supervisor/generated/process.mjs +514 -0
- package/src/supervisor/generated/runner.mjs +872 -0
- package/src/supervisor/generated/store.mjs +605 -0
- package/src/supervisor/generated/types.mjs +50 -0
- package/src/supervisor/identity.ts +99 -0
- package/src/supervisor/index.ts +37 -0
- package/src/supervisor/invocation.ts +44 -0
- package/src/supervisor/runner.ts +982 -0
- package/src/supervisor/store.ts +649 -0
- package/src/supervisor/supervisor.ts +730 -0
- package/src/supervisor/types.ts +384 -0
- package/src/tools.ts +410 -12
- package/src/types.ts +41 -1
package/README.md
CHANGED
|
@@ -116,16 +116,32 @@ new Pi process, a task whose process is gone is marked lost.
|
|
|
116
116
|
|
|
117
117
|
A task can remain `running` without recent progress evidence. Compact status and
|
|
118
118
|
navigator use the same observation. Process tasks use recorded progress and the
|
|
119
|
-
log's modification time.
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
log's modification time. A watch advances only when a poll observes a changed
|
|
120
|
+
outcome: running a poll, appending its result to the watch log, or polling on
|
|
121
|
+
schedule is activity, not progress. Before any progress, the task's launch time
|
|
122
|
+
anchors the silence duration. This does not measure CPU activity, network waits,
|
|
123
|
+
or useful application work.
|
|
122
124
|
|
|
123
125
|
At 60 seconds without observed progress, work is `quiet`. At 5 minutes, it has a
|
|
124
126
|
`suspected stall`. Both thresholds are inclusive. Set `PI_BETTER_STALL_QUIET_MS`
|
|
125
127
|
and `PI_BETTER_STALL_MS` to positive millisecond durations to override them.
|
|
126
128
|
Compact status shows the last observed time, silence duration, and a warning:
|
|
127
129
|
silence alone does not prove the task is hung. Terminal tasks are not reported as
|
|
128
|
-
suspected stalls. New output or a
|
|
130
|
+
suspected stalls. New process output or a changed watch outcome refreshes the
|
|
131
|
+
observation, so a watch that keeps reporting the same thing becomes quiet and
|
|
132
|
+
then a suspected stall while it keeps polling on schedule.
|
|
133
|
+
|
|
134
|
+
A watch's outcome is compared by content, not by when it ran. JSON stdout is
|
|
135
|
+
compared with sorted keys and without insignificant whitespace, terminal control
|
|
136
|
+
sequences and carriage-return redraws are ignored, and the poll's start time, end
|
|
137
|
+
time, duration, and check count are never compared. Exit status, signal, error,
|
|
138
|
+
stdout, and stderr content are compared. A watch whose output carries its own
|
|
139
|
+
changing timestamps or elapsed durations will therefore read as progress: project
|
|
140
|
+
the fields that matter into stdout JSON (for example with `jq`) instead of
|
|
141
|
+
printing a whole status page. The comparison is stored with the task, so it
|
|
142
|
+
survives `/reload` and session resume; a watch restored from an older record
|
|
143
|
+
adopts a comparison without resetting its silence, so reloads cannot hide a
|
|
144
|
+
stationary watch.
|
|
129
145
|
|
|
130
146
|
A status cursor changes when the observation crosses either threshold, even if
|
|
131
147
|
metadata and log bytes have not changed. Within the same observation state,
|
|
@@ -239,6 +255,39 @@ items.", and kubectl and npm print warnings), which can raise a false alarm.
|
|
|
239
255
|
If the stderr is expected, redirect it (`2>/dev/null`) or set
|
|
240
256
|
`blind_checks: 0`. Set `blind_checks` to another number to change the count.
|
|
241
257
|
|
|
258
|
+
## Batches
|
|
259
|
+
|
|
260
|
+
`bg_task_spawn_batch` starts 1 to 100 independent background tasks at once and
|
|
261
|
+
returns immediately with a `batchId`, one task id per launched job, and any job
|
|
262
|
+
that was refused. Every job is an ordinary background task with its own process,
|
|
263
|
+
log, and metadata; the batch only groups them.
|
|
264
|
+
|
|
265
|
+
```json
|
|
266
|
+
{
|
|
267
|
+
"batchName": "local builds",
|
|
268
|
+
"shared": { "cwd": "/repo", "env": { "CI": "1" } },
|
|
269
|
+
"jobs": [
|
|
270
|
+
{ "name": "api", "command": "npm run build" },
|
|
271
|
+
{ "name": "web", "command": "npm run build:web" }
|
|
272
|
+
]
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
- `jobs` is required. `shared` applies launch options to every job and cannot
|
|
277
|
+
carry `name`, `command`, `argv`, `batchId`, or `batchName`: those belong to a
|
|
278
|
+
single job, and the launcher refuses a batch that puts one in `shared`.
|
|
279
|
+
- A job's own value wins over `shared`. An explicit `false`, `0`, or `null`
|
|
280
|
+
overrides the shared value instead of inheriting it.
|
|
281
|
+
- The whole batch is preflighted before anything starts. One invalid job refuses
|
|
282
|
+
the batch, so a partial launch never happens.
|
|
283
|
+
- Each launched task records `batchId` and `batchName` in its metadata, so a
|
|
284
|
+
reloaded task still says which batch it came from.
|
|
285
|
+
- The result lists `launched` and `failed` with 1-based job positions, so a
|
|
286
|
+
refused entry points at the job in the request. Stopping one task does not
|
|
287
|
+
stop the others.
|
|
288
|
+
- Do not wait or poll for the batch. Inspect, log, or stop each task by its id,
|
|
289
|
+
or watch one with `bg_task_watch`.
|
|
290
|
+
|
|
242
291
|
## Install
|
|
243
292
|
|
|
244
293
|
```sh
|
|
@@ -251,6 +300,40 @@ Try it for one run:
|
|
|
251
300
|
pi -e npm:pi-better-background-tasks
|
|
252
301
|
```
|
|
253
302
|
|
|
303
|
+
## Observed operations and waits
|
|
304
|
+
|
|
305
|
+
Supervised local tasks use `bgs_` identifiers and a detached runner. The runner
|
|
306
|
+
keeps observing after the worker or Pi process exits. Status and logs recover
|
|
307
|
+
from durable evidence; an ambiguous launch becomes `lost` and is not relaunched.
|
|
308
|
+
SSH tasks and older `bg_` identifiers use the existing runtime.
|
|
309
|
+
|
|
310
|
+
Stop and timeout evidence can contain `groupCleanup=unproven` when the command's
|
|
311
|
+
leader exits before cleanup can be verified. Descendants may remain running;
|
|
312
|
+
inspect that evidence before interpreting the result.
|
|
313
|
+
|
|
314
|
+
Call `wait_for_operations` with `task_ids` and an optional `continuation_note` to
|
|
315
|
+
register an all-settled wait. The parent remains responsive. A notification means
|
|
316
|
+
every dependency is terminal, including failed, cancelled, timed-out or lost
|
|
317
|
+
operations; it does not mean the command succeeded.
|
|
318
|
+
|
|
319
|
+
Read each operation's status or log, interpret its terminal revision, then call
|
|
320
|
+
`acknowledge_operation_result` with the wait identity, exact revisions, disposition
|
|
321
|
+
and interpretation note. The acknowledgement releases the wait's evidence pins.
|
|
322
|
+
Reading evidence or receiving a callback alone does not consume the wait.
|
|
323
|
+
`bg_task_wait_list` shows owned generations. `bg_task_wait_cancel` cancels a wait;
|
|
324
|
+
use `bg_task_stop` separately when the command itself should stop.
|
|
325
|
+
|
|
326
|
+
`watch_delivery` registers the installed stateful Mergify delivery watcher and its
|
|
327
|
+
wait. The watcher observes delivery; the model interprets its result. The tool
|
|
328
|
+
does not enqueue or merge a pull request.
|
|
329
|
+
|
|
330
|
+
With Subagents loaded, workers can use the guarded operation bridge and park
|
|
331
|
+
while waiting. Parking requires SDK 0.82.1 or later. The worker saves its wait
|
|
332
|
+
result, exits cleanly and releases its capacity slot. The same conversation can
|
|
333
|
+
resume after settlement when its original parent session and branch are active
|
|
334
|
+
and current capacity, Goal, workflow and permission gates allow it. Restarting Pi
|
|
335
|
+
does not start an unattended model. Uncertain recovery stays held with a reason.
|
|
336
|
+
|
|
254
337
|
## Failure observations
|
|
255
338
|
|
|
256
339
|
Task lifecycle and failure evidence are reported separately. A watch can remain
|
|
@@ -293,6 +376,50 @@ history count line with no incident cursor; pass `history: true` to
|
|
|
293
376
|
(120-byte excerpt, evidence such as `output.log#poll=3`); the raw log keeps the
|
|
294
377
|
full evidence path.
|
|
295
378
|
|
|
379
|
+
## Chained commands and the step gate
|
|
380
|
+
|
|
381
|
+
A shell command that chains steps with `;` or newlines reports only its last
|
|
382
|
+
command's exit status. `ctest; write-summary` therefore reads as success when
|
|
383
|
+
`ctest` fails and the summary exits 0. That is ordinary shell behavior, and it
|
|
384
|
+
stays the default.
|
|
385
|
+
|
|
386
|
+
Pass `require_successful_steps: true` on `bg_task_spawn` (or `bg_task` with
|
|
387
|
+
`action:spawn`) to opt into a step gate:
|
|
388
|
+
|
|
389
|
+
```json
|
|
390
|
+
{ "command": "ctest --output-on-failure\nnpm run report", "require_successful_steps": true }
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
The gate is a bash `errexit` contract, not shell parsing: the command runs under
|
|
394
|
+
`set -e`, so its first unhandled failing step ends the task with that step's
|
|
395
|
+
exit code and the remaining steps do not run. That covers the same list, shell
|
|
396
|
+
functions and subshells, and it is why a summary command
|
|
397
|
+
after a failed `ctest` can no longer turn the task green. A failure the command
|
|
398
|
+
handles itself (`|| true`, `if`/`while`/`until` conditions, `!`, `&&`/`||`
|
|
399
|
+
lists) is not a gate failure, and neither is a failing process substitution
|
|
400
|
+
whose status bash discards. Status reports the real process exit with
|
|
401
|
+
`(step gate)` plus the failing step:
|
|
402
|
+
|
|
403
|
+
```text
|
|
404
|
+
exit=8 (step gate)
|
|
405
|
+
step gate: ctest --output-on-failure exited 8; the shell stopped at that step
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
A command that switches errexit off (`set +e`) keeps a different own exit; when
|
|
409
|
+
that exit is 0 the gate still fails the task with the failing step's code and
|
|
410
|
+
the status says so (`the shell's own exit was 0`).
|
|
411
|
+
|
|
412
|
+
The gate is refused (and the task is not started) with `shell:false`, whose
|
|
413
|
+
argv already reports the real exit code, and with structured `ssh`, whose
|
|
414
|
+
command runs under remote semantics the local gate cannot observe. It applies
|
|
415
|
+
to spawns, not to watch checks: a check is one polled command whose own exit
|
|
416
|
+
code is the outcome. Limits inherited from bash's own errexit rules: assignment
|
|
417
|
+
builtins such as `local` and `export` mask a command substitution's status.
|
|
418
|
+
Bash also clears errexit inside command substitutions by default, so
|
|
419
|
+
`x=$(false; echo summary)` can still succeed. A command that disables errexit
|
|
420
|
+
and clears the ERR trap can bypass the gate, and
|
|
421
|
+
`exec` replaces the shell before the gate can decide.
|
|
422
|
+
|
|
296
423
|
## When To Use
|
|
297
424
|
|
|
298
425
|
Use this package for shell commands that need logs, status, cancellation, or completion notifications across a Pi turn.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-better-background-tasks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Pi extension for durable background shell tasks, watchers, logs, and status inspection.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -44,6 +44,8 @@
|
|
|
44
44
|
},
|
|
45
45
|
"files": [
|
|
46
46
|
"src/**/*.ts",
|
|
47
|
+
"src/supervisor/**/*.mjs",
|
|
48
|
+
"src/supervisor/**/*.js",
|
|
47
49
|
"!src/**/*.test.ts",
|
|
48
50
|
"!src/test-support/**",
|
|
49
51
|
"!src/shared-ssh-core/test-support/**",
|
package/src/adapter-contract.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { getObservedWaitHost } from "./observed-waits.js";
|
|
2
3
|
import { inspectMeta } from "./registry.js";
|
|
3
4
|
|
|
4
5
|
/** Optional-peer contract, mirrored by pi-better-sandbox/background-admission.ts. */
|
|
@@ -22,18 +23,26 @@ export function advertiseBackgroundAdapter(pi: ExtensionAPI, schemas: ReadonlyMa
|
|
|
22
23
|
typeof request.reply !== "function") return;
|
|
23
24
|
const parameters = schemas.get(request.name);
|
|
24
25
|
if (!parameters) return;
|
|
25
|
-
const admission = classify(request.name, request.input);
|
|
26
|
+
const admission = classify(pi, request.name, request.input);
|
|
26
27
|
if (admission) request.reply({ version: BACKGROUND_ADAPTER_VERSION, parameters, admission, executionControls: 1 });
|
|
27
28
|
});
|
|
28
29
|
pi.on("session_shutdown", () => { if (typeof unsubscribe === "function") unsubscribe(); });
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
function classify(name: string, input: unknown): Admission | undefined {
|
|
32
|
+
function classify(pi: ExtensionAPI, name: string, input: unknown): Admission | undefined {
|
|
32
33
|
if (!input || typeof input !== "object" || Array.isArray(input)) return;
|
|
33
34
|
const params = input as Record<string, unknown>;
|
|
34
35
|
let action: unknown;
|
|
35
36
|
switch (name) {
|
|
36
37
|
case "bg_task_spawn": action = "spawn"; break;
|
|
38
|
+
case "bg_task_spawn_batch": {
|
|
39
|
+
// A batch is a local launch only when no job is remote: per-job ssh options
|
|
40
|
+
// are decided by the batch launcher, not in this trusted process.
|
|
41
|
+
const jobs = Array.isArray(params.jobs) ? params.jobs : [];
|
|
42
|
+
const remote = jobs.some((job) => job !== null && typeof job === "object" &&
|
|
43
|
+
Boolean((job as Record<string, unknown>).ssh));
|
|
44
|
+
return remote ? undefined : "local-launch";
|
|
45
|
+
}
|
|
37
46
|
case "bg_task_watch": action = "watch"; break;
|
|
38
47
|
case "bg_task_list": action = "list"; break;
|
|
39
48
|
case "bg_task_status": action = "status"; break;
|
|
@@ -53,4 +62,9 @@ function classify(name: string, input: unknown): Admission | undefined {
|
|
|
53
62
|
// of a local stop; a remote stop can execute SSH in the trusted Pi process.
|
|
54
63
|
const meta = inspectMeta(params.id).meta;
|
|
55
64
|
if (meta && !meta.ssh && !meta.remote && (meta.kind === "process" || meta.kind === "command_watch")) return "registry";
|
|
65
|
+
// A durable local task has no legacy registry meta. Only the currently active
|
|
66
|
+
// owning session/workspace may admit it as a registry operation; the exact tool
|
|
67
|
+
// source and schema are still verified by the caller before this reply is
|
|
68
|
+
// trusted. A foreign session or worker-owned id stays unauthorized.
|
|
69
|
+
if (getObservedWaitHost(pi)?.ownsDurableTask(params.id) === true) return "registry";
|
|
56
70
|
}
|
package/src/batch.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { readTaskIntent } from "./failures.js";
|
|
4
|
+
import { validateCommandSpec } from "./process.js";
|
|
5
|
+
import { resolveSshCommand } from "./shared-ssh-core/index.js";
|
|
6
|
+
import { spawnTask, type ActiveSessionProvider, type SpawnTaskParams, type TaskRuntimeDependencies } from "./runtime.js";
|
|
7
|
+
import type { BackgroundTaskCallbackOrigin } from "./types.js";
|
|
8
|
+
|
|
9
|
+
export interface SpawnBatchParams {
|
|
10
|
+
batchName?: string;
|
|
11
|
+
jobs: SpawnTaskParams[];
|
|
12
|
+
shared?: Omit<SpawnTaskParams, "name" | "command" | "argv" | "batchId" | "batchName">;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SpawnBatchResult {
|
|
16
|
+
batchId: string;
|
|
17
|
+
batchName?: string;
|
|
18
|
+
launched: Array<{ job: number; id: string; name?: string }>;
|
|
19
|
+
failed: Array<{ job: number; name?: string; error: string }>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Undefined inherits; false, zero, and explicit-null intent declarations override. */
|
|
23
|
+
function mergeJob(shared: SpawnBatchParams["shared"], job: SpawnTaskParams): SpawnTaskParams {
|
|
24
|
+
return { ...shared, ...Object.fromEntries(Object.entries(job).filter(([, value]) => value !== undefined)) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function spawnTaskBatch(
|
|
28
|
+
pi: ExtensionAPI,
|
|
29
|
+
params: SpawnBatchParams,
|
|
30
|
+
defaultCwd: string,
|
|
31
|
+
callbackOrigin?: BackgroundTaskCallbackOrigin,
|
|
32
|
+
getActiveSession?: ActiveSessionProvider,
|
|
33
|
+
dependencies: TaskRuntimeDependencies = {},
|
|
34
|
+
): SpawnBatchResult {
|
|
35
|
+
if (!Array.isArray(params.jobs) || params.jobs.length < 1 || params.jobs.length > 100) {
|
|
36
|
+
throw new Error("jobs must contain between 1 and 100 background tasks.");
|
|
37
|
+
}
|
|
38
|
+
for (const field of ["name", "command", "argv", "batchId", "batchName"]) {
|
|
39
|
+
if (params.shared && Object.hasOwn(params.shared, field)) {
|
|
40
|
+
throw new Error(`Invalid batch shared field ${field}: each job must supply its own command, argv, and name; batch identity is assigned by the launcher.`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Preflight the whole batch before any process, task directory, or SSH connection exists.
|
|
44
|
+
const jobs = params.jobs.map((job, index) => {
|
|
45
|
+
try {
|
|
46
|
+
if (!job || typeof job !== "object" || Array.isArray(job)) throw new Error("job must be an object");
|
|
47
|
+
const merged = mergeJob(params.shared, job);
|
|
48
|
+
readTaskIntent(merged);
|
|
49
|
+
validateLaunchStrings({
|
|
50
|
+
command: merged.command,
|
|
51
|
+
argv: merged.argv,
|
|
52
|
+
cwd: merged.cwd ?? defaultCwd,
|
|
53
|
+
env: merged.env,
|
|
54
|
+
ssh: merged.ssh,
|
|
55
|
+
remote: merged.remote,
|
|
56
|
+
});
|
|
57
|
+
if (merged.ssh) {
|
|
58
|
+
resolveSshCommand({ command: merged.command, cwd: merged.cwd ?? defaultCwd, env: merged.env, ssh: merged.ssh });
|
|
59
|
+
} else {
|
|
60
|
+
validateCommandSpec(merged);
|
|
61
|
+
}
|
|
62
|
+
return merged;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
throw new Error(`Invalid batch job ${index + 1}: ${errorMessage(error)}`);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
const result: SpawnBatchResult = {
|
|
68
|
+
batchId: `bg_batch_${randomUUID()}`,
|
|
69
|
+
...(params.batchName !== undefined ? { batchName: params.batchName } : {}),
|
|
70
|
+
launched: [],
|
|
71
|
+
failed: [],
|
|
72
|
+
};
|
|
73
|
+
for (const [index, job] of jobs.entries()) {
|
|
74
|
+
try {
|
|
75
|
+
const meta = spawnTask(pi, { ...job, batchId: result.batchId, batchName: result.batchName }, defaultCwd, callbackOrigin, getActiveSession, dependencies);
|
|
76
|
+
result.launched.push({ job: index + 1, id: meta.id, ...(meta.name !== undefined ? { name: meta.name } : {}) });
|
|
77
|
+
} catch (error) {
|
|
78
|
+
result.failed.push({ job: index + 1, ...(job.name !== undefined ? { name: job.name } : {}), error: errorMessage(error) });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function formatBatchLaunch(result: SpawnBatchResult): string {
|
|
85
|
+
const label = result.batchName ? `${result.batchName} (${result.batchId})` : result.batchId;
|
|
86
|
+
return [
|
|
87
|
+
`Batch ${label}: ${result.launched.length} launched, ${result.failed.length} failed to launch.`,
|
|
88
|
+
...result.launched.map((job) => `Job ${job.job}${job.name ? ` (${job.name})` : ""}: ${job.id}`),
|
|
89
|
+
...result.failed.map((job) => `Job ${job.job}${job.name ? ` (${job.name})` : ""}: failed to launch: ${job.error}`),
|
|
90
|
+
"Inspect, log, or stop each task by its id. Do not wait or poll in the foreground.",
|
|
91
|
+
].join("\n");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Node rejects NULs synchronously, even when the rest of the command spec is valid.
|
|
95
|
+
function validateLaunchStrings(value: unknown, path = "launch"): void {
|
|
96
|
+
if (typeof value === "string") {
|
|
97
|
+
if (value.includes("\0")) throw new Error(`${path} must not contain NUL bytes`);
|
|
98
|
+
} else if (value && typeof value === "object") {
|
|
99
|
+
for (const [key, child] of Object.entries(value)) {
|
|
100
|
+
if (key.includes("\0")) throw new Error(`${path} keys must not contain NUL bytes`);
|
|
101
|
+
validateLaunchStrings(child, `${path}.${key}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function errorMessage(error: unknown): string {
|
|
107
|
+
return error instanceof Error ? error.message : String(error);
|
|
108
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {existsSync,realpathSync,statSync} from 'node:fs';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
import {homedir} from 'node:os';
|
|
4
|
+
import {Type} from 'typebox';
|
|
5
|
+
import type {ExtensionAPI,ExtensionContext} from '@earendil-works/pi-coding-agent';
|
|
6
|
+
|
|
7
|
+
/** Domain observation uses the installed stateful watcher, never a generated polling predicate. */
|
|
8
|
+
export function resolveDeliveryWatcher(agentDir=process.env.PI_CODING_AGENT_DIR??join(homedir(),'.pi','agent')): string {
|
|
9
|
+
const candidates=[
|
|
10
|
+
join(agentDir,'skills','mergify','scripts','watch-pr-delivery.mjs'),
|
|
11
|
+
join(homedir(),'.agents','skills','mergify','scripts','watch-pr-delivery.mjs'),
|
|
12
|
+
join(agentDir,'npm','node_modules','@1aboveio','skills','skills','cicd','mergify','scripts','watch-pr-delivery.mjs'),
|
|
13
|
+
];
|
|
14
|
+
for(const candidate of candidates)if(existsSync(candidate)&&statSync(candidate).isFile())return realpathSync.native(candidate);
|
|
15
|
+
throw new Error('The installed Mergify delivery watcher is unavailable. Install the mergify skill; no replacement polling script was launched.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function deliveryObserverArguments(repo:string,pr:number,watcher:string): string[] {
|
|
19
|
+
if(!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo)||!Number.isSafeInteger(pr)||pr<1)throw new Error('A repository owner/name and positive PR number are required');
|
|
20
|
+
if(!watcher.endsWith('/watch-pr-delivery.mjs'))throw new Error('Unsupported delivery observer implementation');
|
|
21
|
+
return [process.execPath,watcher,'--repo',repo,'--pr',String(pr)];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DeliveryObserverDependencies {
|
|
25
|
+
resolveWatcher?:()=>string;
|
|
26
|
+
launch:(params:{argv:string[];shell:false;cwd:string;name:string;callback:true},ctx:ExtensionContext)=>Promise<{id:string}>|{id:string};
|
|
27
|
+
wait:(taskIds:string[],note:string,ctx:ExtensionContext)=>Promise<unknown>|unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function registerDeliveryObserver(pi:Pick<ExtensionAPI,'registerTool'>,dependencies:DeliveryObserverDependencies):void {
|
|
31
|
+
pi.registerTool({
|
|
32
|
+
name:'watch_delivery',label:'Watch delivery',
|
|
33
|
+
description:'Observe an already-enqueued GitHub pull request through Mergify with the installed stateful delivery watcher. Starts a supervised background task and registers its wait. This does not enqueue, merge, push, edit the PR, or delegate polling to an LLM. Inspect the terminal verdict on completion before reporting delivery.',
|
|
34
|
+
parameters:Type.Object({repo:Type.String({description:'Repository owner/name'}),pr:Type.Integer({minimum:1})}),
|
|
35
|
+
async execute(_id,params,_signal,_update,ctx){
|
|
36
|
+
const argv=deliveryObserverArguments(params.repo,params.pr,(dependencies.resolveWatcher??resolveDeliveryWatcher)());
|
|
37
|
+
const task=await dependencies.launch({argv,shell:false,cwd:ctx.cwd,name:`Mergify delivery ${params.repo}#${params.pr}`,callback:true},ctx);
|
|
38
|
+
const wait=await dependencies.wait([task.id],`Interpret the Mergify terminal verdict for ${params.repo}#${params.pr}; inspect merge/dequeue evidence before reporting delivery.`,ctx);
|
|
39
|
+
return {content:[{type:'text',text:JSON.stringify({taskId:task.id,wait,status:'observing',mutationPerformed:false})}],details:{taskId:task.id,wait}};
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
package/src/goal-provider.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { listMetas, listMetasForOrigin, onMetaChanged } from "./registry.js";
|
|
3
|
+
import { getObservedWaitHost, type DurableTaskProjection } from "./observed-waits.js";
|
|
3
4
|
import type { BackgroundTaskMeta } from "./types.js";
|
|
4
5
|
import { isTerminalStatus } from "./types.js";
|
|
5
6
|
import { backgroundTaskProgressAt, observeBackgroundTaskStall } from "./stall.js";
|
|
@@ -31,13 +32,19 @@ export function collectBackgroundTaskGoalActivity(
|
|
|
31
32
|
metas: BackgroundTaskMeta[] = listMetas(),
|
|
32
33
|
ctx?: ExtensionContext,
|
|
33
34
|
now = Date.now(),
|
|
35
|
+
durable: readonly DurableTaskProjection[] = [],
|
|
34
36
|
): GoalBackgroundProviderSnapshot {
|
|
35
37
|
return {
|
|
36
38
|
providerId: "background-tasks",
|
|
37
39
|
label: "Background Tasks",
|
|
38
|
-
items:
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
items: [
|
|
41
|
+
...metas
|
|
42
|
+
.filter((meta) => isVisibleGoalActivity(meta, ctx, now))
|
|
43
|
+
.map(backgroundTaskToGoalItem),
|
|
44
|
+
...durable
|
|
45
|
+
.filter((task) => isVisibleDurableGoalActivity(task, ctx, now))
|
|
46
|
+
.map(durableTaskToGoalItem),
|
|
47
|
+
],
|
|
41
48
|
};
|
|
42
49
|
}
|
|
43
50
|
|
|
@@ -46,8 +53,23 @@ export function registerBackgroundTasksGoalProvider(pi: ExtensionAPI): void {
|
|
|
46
53
|
pi.events?.emit(GOAL_REGISTER_PROVIDER_EVENT, {
|
|
47
54
|
id: "background-tasks",
|
|
48
55
|
label: "Background Tasks",
|
|
49
|
-
getActivity: (ctx: ExtensionContext) =>
|
|
50
|
-
|
|
56
|
+
getActivity: (ctx: ExtensionContext) => {
|
|
57
|
+
const origin = getGoalActivityOrigin(ctx);
|
|
58
|
+
const durable = getObservedWaitHost(pi)?.listDurableTasks(origin, {
|
|
59
|
+
retentionMs: TERMINAL_GOAL_ACTIVITY_RETENTION_MS,
|
|
60
|
+
}) ?? [];
|
|
61
|
+
return collectBackgroundTaskGoalActivity(listMetasForOrigin(origin), ctx, Date.now(), durable);
|
|
62
|
+
},
|
|
63
|
+
// A durable operation change must refresh Goal activity even though it
|
|
64
|
+
// never writes a mutable legacy registry meta.
|
|
65
|
+
onActivityChanged: (notify: () => void) => {
|
|
66
|
+
const offMeta = onMetaChanged(notify);
|
|
67
|
+
const offDurable = getObservedWaitHost(pi)?.onDurableTaskChange(notify);
|
|
68
|
+
return () => {
|
|
69
|
+
offMeta();
|
|
70
|
+
offDurable?.();
|
|
71
|
+
};
|
|
72
|
+
},
|
|
51
73
|
});
|
|
52
74
|
};
|
|
53
75
|
|
|
@@ -91,6 +113,51 @@ function getGoalActivityOrigin(ctx: ExtensionContext) {
|
|
|
91
113
|
return { cwd: ctx.cwd, sessionId };
|
|
92
114
|
}
|
|
93
115
|
|
|
116
|
+
function isVisibleDurableGoalActivity(
|
|
117
|
+
task: DurableTaskProjection,
|
|
118
|
+
ctx: ExtensionContext | undefined,
|
|
119
|
+
now: number,
|
|
120
|
+
): boolean {
|
|
121
|
+
if (task.dismissedAt !== undefined) return false;
|
|
122
|
+
if (ctx) {
|
|
123
|
+
const active = getGoalActivityOrigin(ctx);
|
|
124
|
+
if (task.owner.workspace !== active.cwd) return false;
|
|
125
|
+
if (active.sessionId || task.owner.sessionId) {
|
|
126
|
+
if (task.owner.sessionId !== active.sessionId) return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!task.terminal) return true;
|
|
130
|
+
if (!ctx) return true;
|
|
131
|
+
if (task.endedAt === undefined) return true;
|
|
132
|
+
return now - task.endedAt < TERMINAL_GOAL_ACTIVITY_RETENTION_MS;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function durableTaskToGoalItem(task: DurableTaskProjection): GoalBackgroundWorkItem {
|
|
136
|
+
const item: GoalBackgroundWorkItem = {
|
|
137
|
+
id: task.id,
|
|
138
|
+
status: task.status,
|
|
139
|
+
active: task.active,
|
|
140
|
+
terminal: task.terminal,
|
|
141
|
+
attention: task.attention,
|
|
142
|
+
startedAt: task.startedAt,
|
|
143
|
+
details: {
|
|
144
|
+
durable: true,
|
|
145
|
+
operationId: task.operationId,
|
|
146
|
+
kind: task.kind,
|
|
147
|
+
cwd: task.cwd,
|
|
148
|
+
command: task.command,
|
|
149
|
+
argv: task.argv,
|
|
150
|
+
logPath: task.logPaths.stdout,
|
|
151
|
+
lastProgressAt: task.endedAt ?? task.startedAt,
|
|
152
|
+
waits: task.waits.map((wait) => `${wait.waitId}:${wait.phase}`),
|
|
153
|
+
...(task.reason ? { reason: task.reason } : {}),
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
if (task.name !== undefined) item.label = task.name;
|
|
157
|
+
if (task.endedAt !== undefined) item.endedAt = task.endedAt;
|
|
158
|
+
return item;
|
|
159
|
+
}
|
|
160
|
+
|
|
94
161
|
function backgroundTaskToGoalItem(meta: BackgroundTaskMeta): GoalBackgroundWorkItem {
|
|
95
162
|
const active = meta.status === "running";
|
|
96
163
|
const terminal = isTerminalStatus(meta.status);
|
package/src/index.ts
CHANGED
|
@@ -4,11 +4,37 @@ import { initializeNavigatorPanels } from "./shared-navigator-panels.ts";
|
|
|
4
4
|
import { registerBackgroundTasksGoalProvider } from "./goal-provider.js";
|
|
5
5
|
import { clearBackgroundTasksNavigatorSession, ensureBackgroundTasksNavigator, ensureBackgroundTasksNavigatorProvider } from "./navigator-provider.js";
|
|
6
6
|
import { resumeScheduledWork, suspendScheduledWork } from "./runtime.js";
|
|
7
|
+
import { createObservedWaitHost } from "./observed-waits.js";
|
|
8
|
+
import { getCallbackBatcher } from "./shared-callback-batcher.js";
|
|
7
9
|
import { observeForegroundSandboxPolicy } from "./sandbox.js";
|
|
8
10
|
import { registerTools } from "./tools.js";
|
|
9
11
|
import { appendExecutionPolicy } from "./shared-execution-policy.js";
|
|
10
12
|
|
|
11
13
|
export default function backgroundTasksExtension(pi: ExtensionAPI): void {
|
|
14
|
+
// Created before registerTools so the tools can resolve it lazily; the durable
|
|
15
|
+
// store opens on session activation and closes on switch/unload.
|
|
16
|
+
const observedWaits = createObservedWaitHost(pi, {
|
|
17
|
+
notifyParent: (notification, delivery) => {
|
|
18
|
+
getCallbackBatcher(pi).enqueue({
|
|
19
|
+
source: "background-task",
|
|
20
|
+
id: notification.taskId,
|
|
21
|
+
label: notification.taskId,
|
|
22
|
+
status: notification.waitId
|
|
23
|
+
? `${notification.status}; wait ${notification.waitId} ready`
|
|
24
|
+
: notification.status,
|
|
25
|
+
detailTool: "bg_task_status",
|
|
26
|
+
outcome: notification.outcome,
|
|
27
|
+
callback: true,
|
|
28
|
+
// The batcher can hold the event across a busy turn or a session switch.
|
|
29
|
+
// Receipt is the durable outbox/sidecar acknowledgement, not enqueue.
|
|
30
|
+
getSuppressionReason: () => observedWaits.isParentOriginActive(notification.origin, notification.branchId)
|
|
31
|
+
? undefined
|
|
32
|
+
: "the originating session or branch is no longer active",
|
|
33
|
+
onDelivered: () => delivery.delivered(),
|
|
34
|
+
onSuppressed: (reason) => delivery.defer(reason ?? "callback delivery was suppressed"),
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
});
|
|
12
38
|
initializeNavigatorPanels(pi);
|
|
13
39
|
pi.on("before_agent_start", (event) => ({ systemPrompt: appendExecutionPolicy(event.systemPrompt) }));
|
|
14
40
|
registerBackgroundTasksGoalProvider(pi);
|
|
@@ -23,14 +49,27 @@ export default function backgroundTasksExtension(pi: ExtensionAPI): void {
|
|
|
23
49
|
ensureBackgroundTasksNavigator(ctx);
|
|
24
50
|
});
|
|
25
51
|
pi.on("session_before_switch", async () => {
|
|
52
|
+
observedWaits.close();
|
|
26
53
|
clearBackgroundTasksNavigatorSession();
|
|
27
54
|
disposeBackgroundWorkNavigator();
|
|
28
55
|
});
|
|
29
56
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
30
57
|
// /reload, session replacement, and quit load a fresh instance; stop this one's timers (#324).
|
|
58
|
+
observedWaits.close();
|
|
31
59
|
suspendScheduledWork();
|
|
32
60
|
clearBackgroundTasksNavigatorSession();
|
|
33
61
|
disposeBackgroundWorkNavigator(ctx);
|
|
34
62
|
});
|
|
35
63
|
registerTools(pi);
|
|
64
|
+
// Registered after registerTools so the callback batcher has its session
|
|
65
|
+
// context before a reconciled delivery can enqueue a parent notification.
|
|
66
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
67
|
+
observedWaits.start(ctx);
|
|
68
|
+
});
|
|
69
|
+
// A fork or branch navigation does not restart the session, but it changes
|
|
70
|
+
// which continuation may receive a queued callback. Update the host snapshot
|
|
71
|
+
// on the same event the callback batcher uses.
|
|
72
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
73
|
+
observedWaits.noteParentContext(ctx);
|
|
74
|
+
});
|
|
36
75
|
}
|