pi-better-background-tasks 0.13.1 → 0.14.1
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 +67 -0
- 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 +196 -6
- package/src/observed-waits.ts +3679 -0
- package/src/process-identity.ts +108 -31
- package/src/runtime.ts +10 -0
- package/src/sandbox.ts +276 -2
- package/src/shared-execution-policy.ts +2 -0
- 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/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 +113 -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 +804 -0
- package/src/supervisor/types.ts +394 -0
- package/src/tools.ts +396 -9
- package/src/types.ts +8 -0
|
@@ -14,6 +14,7 @@ import { actionableFailures, failureLabel, readFailureState } from "./shared-fai
|
|
|
14
14
|
import { failurePath, failureView } from "./failures.js";
|
|
15
15
|
import { readLog } from "./logs.js";
|
|
16
16
|
import { listMetasForOrigin, onMetaChanged, readMeta, writeMeta } from "./registry.js";
|
|
17
|
+
import { getObservedWaitHost, type DurableTaskProjection } from "./observed-waits.js";
|
|
17
18
|
import { stopTask } from "./runtime.js";
|
|
18
19
|
import { formatBackgroundTaskStall, observeBackgroundTaskStall } from "./stall.js";
|
|
19
20
|
import type { BackgroundTaskCallbackOrigin, BackgroundTaskMeta, BackgroundTaskStatus } from "./types.js";
|
|
@@ -24,8 +25,15 @@ let activeNavigatorOrigin: BackgroundTaskCallbackOrigin | undefined;
|
|
|
24
25
|
const TERMINAL_NAVIGATOR_RETENTION_MS = 30_000;
|
|
25
26
|
|
|
26
27
|
export function ensureBackgroundTasksNavigatorProvider(pi: ExtensionAPI): void {
|
|
28
|
+
// A reloaded or replaced extension instance gets a fresh host. Re-register so
|
|
29
|
+
// the provider's durable-change subscription targets that host, not the one
|
|
30
|
+
// captured by the previous instance.
|
|
31
|
+
if (piRef === pi && unregister) return;
|
|
32
|
+
if (unregister) {
|
|
33
|
+
try { unregister(); } catch { /* ignore */ }
|
|
34
|
+
unregister = undefined;
|
|
35
|
+
}
|
|
27
36
|
piRef = pi;
|
|
28
|
-
if (unregister) return;
|
|
29
37
|
unregister = registerBackgroundWorkProvider(provider);
|
|
30
38
|
}
|
|
31
39
|
|
|
@@ -51,11 +59,36 @@ const provider: BackgroundWorkProvider = {
|
|
|
51
59
|
id: "background-tasks",
|
|
52
60
|
label: "Background Tasks",
|
|
53
61
|
priority: 20,
|
|
54
|
-
visibleCount: () =>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
visibleCount: () => {
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
return visibleMetas(now).filter((meta) => meta.status === "running").length
|
|
65
|
+
+ visibleDurableTasks(now).filter((task) => task.active).length;
|
|
66
|
+
},
|
|
67
|
+
listRows: (now) => [
|
|
68
|
+
...visibleMetas(now).map((meta) => rowFromMeta(meta, now)),
|
|
69
|
+
...visibleDurableTasks(now).map((task) => rowFromDurable(task, now)),
|
|
70
|
+
],
|
|
71
|
+
detail: (id, now, options) => {
|
|
72
|
+
const durable = durableTask(id);
|
|
73
|
+
if (durable) return detailFromDurable(durable, options);
|
|
74
|
+
return detailFromMeta(readMeta(id), now, options);
|
|
75
|
+
},
|
|
76
|
+
armCloseLabel: (row) => row.status === "running" || row.status === "starting" ? "x again to stop" : "x again to dismiss",
|
|
58
77
|
close: (id) => {
|
|
78
|
+
const durable = durableTask(id);
|
|
79
|
+
if (durable) {
|
|
80
|
+
const host = navigatorHost();
|
|
81
|
+
const origin = activeNavigatorOrigin;
|
|
82
|
+
if (!host || !origin) return { action: "missing", providerId: "background-tasks", id };
|
|
83
|
+
if (durable.active) {
|
|
84
|
+
void host.stopDurableTask(origin, id);
|
|
85
|
+
return { action: "stopped", providerId: "background-tasks", id, status: "stopping" };
|
|
86
|
+
}
|
|
87
|
+
if (host.dismissDurableTask(origin, id)) {
|
|
88
|
+
return { action: "dismissed", providerId: "background-tasks", id, status: durable.status };
|
|
89
|
+
}
|
|
90
|
+
return { action: "denied", providerId: "background-tasks", id };
|
|
91
|
+
}
|
|
59
92
|
const meta = readMeta(id);
|
|
60
93
|
if (!meta) return { action: "missing", providerId: "background-tasks", id };
|
|
61
94
|
if (meta.status === "running") {
|
|
@@ -66,9 +99,166 @@ const provider: BackgroundWorkProvider = {
|
|
|
66
99
|
writeMeta(meta);
|
|
67
100
|
return { action: "dismissed", providerId: "background-tasks", id, status: meta.status };
|
|
68
101
|
},
|
|
69
|
-
onVisibleChanged:
|
|
102
|
+
onVisibleChanged: (notify) => {
|
|
103
|
+
const offMeta = onMetaChanged(notify);
|
|
104
|
+
const offDurable = getObservedWaitHost(piRef)?.onDurableTaskChange(() => {
|
|
105
|
+
invalidateDurableProjection();
|
|
106
|
+
notify();
|
|
107
|
+
});
|
|
108
|
+
return () => {
|
|
109
|
+
invalidateDurableProjection();
|
|
110
|
+
offMeta();
|
|
111
|
+
offDurable?.();
|
|
112
|
+
};
|
|
113
|
+
},
|
|
70
114
|
};
|
|
71
115
|
|
|
116
|
+
function navigatorHost() {
|
|
117
|
+
return piRef ? getObservedWaitHost(piRef) : undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* One refresh asks for {@link provider.visibleCount} and then {@link provider.listRows},
|
|
122
|
+
* and a detail view asks again. Rebuilding the projection re-reads every durable
|
|
123
|
+
* record, so a single paint would pay for the same list up to three times. Hold
|
|
124
|
+
* one result per paint window; a durable-change notification clears it.
|
|
125
|
+
*/
|
|
126
|
+
const PROJECTION_MEMO_MS = 250;
|
|
127
|
+
let projectionMemo: { at: number; tasks: DurableTaskProjection[] } | undefined;
|
|
128
|
+
|
|
129
|
+
function invalidateDurableProjection(): void {
|
|
130
|
+
projectionMemo = undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function visibleDurableTasks(now = Date.now()): DurableTaskProjection[] {
|
|
134
|
+
const active = activeNavigatorOrigin;
|
|
135
|
+
const host = navigatorHost();
|
|
136
|
+
if (!active || !host) return [];
|
|
137
|
+
if (projectionMemo && now - projectionMemo.at < PROJECTION_MEMO_MS) {
|
|
138
|
+
return projectionMemo.tasks.filter((task) => !isExpiredDurableRow(task, now));
|
|
139
|
+
}
|
|
140
|
+
const listed = host.listDurableTasks(active, { retentionMs: TERMINAL_NAVIGATOR_RETENTION_MS });
|
|
141
|
+
const tasks = listed.filter((task) => task.dismissedAt === undefined);
|
|
142
|
+
projectionMemo = { at: now, tasks };
|
|
143
|
+
return tasks.filter((task) => !isExpiredDurableRow(task, now));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function durableTask(id: string): DurableTaskProjection | undefined {
|
|
147
|
+
const active = activeNavigatorOrigin;
|
|
148
|
+
const host = navigatorHost();
|
|
149
|
+
if (!active || !host) return undefined;
|
|
150
|
+
return host.readDurableTask(active, id);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function isExpiredDurableRow(task: DurableTaskProjection, now: number): boolean {
|
|
154
|
+
if (!task.terminal || task.endedAt === undefined) return false;
|
|
155
|
+
return now - task.endedAt >= TERMINAL_NAVIGATOR_RETENTION_MS;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function rowFromDurable(task: DurableTaskProjection, now: number): BackgroundWorkRow {
|
|
159
|
+
const command = durableCommandLabel(task);
|
|
160
|
+
const elapsed = formatDuration((task.endedAt ?? now) - task.startedAt);
|
|
161
|
+
const facts: string[] = [];
|
|
162
|
+
if (task.attention && task.reason) facts.push(task.reason);
|
|
163
|
+
else if (task.attention) facts.push(task.status);
|
|
164
|
+
if (task.kind === "watch" && task.intervalSeconds) facts.push(`every ${formatDuration(task.intervalSeconds * 1000)}`);
|
|
165
|
+
if (task.active && task.deadlineAt) facts.push(`${formatDuration(task.deadlineAt - now)} left`);
|
|
166
|
+
if (task.waits.length) facts.push(`${task.waits.length} wait${task.waits.length === 1 ? "" : "s"}`);
|
|
167
|
+
return {
|
|
168
|
+
providerId: "background-tasks",
|
|
169
|
+
id: task.id,
|
|
170
|
+
name: task.name,
|
|
171
|
+
status: task.status,
|
|
172
|
+
statusTone: toneForDurableStatus(task.status),
|
|
173
|
+
kind: task.kind,
|
|
174
|
+
elapsed,
|
|
175
|
+
primary: compactDurableLabel(task),
|
|
176
|
+
command,
|
|
177
|
+
tool: compactDurableLabel(task),
|
|
178
|
+
secondary: [task.cwd, task.exitCode !== null ? `exit ${task.exitCode}` : undefined].filter(Boolean).join(" · ") || undefined,
|
|
179
|
+
facts: facts.slice(0, 2),
|
|
180
|
+
sortStartedAt: task.startedAt,
|
|
181
|
+
expiresAt: task.terminal && task.endedAt !== undefined
|
|
182
|
+
? task.endedAt + TERMINAL_NAVIGATOR_RETENTION_MS
|
|
183
|
+
: undefined,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function detailFromDurable(task: DurableTaskProjection, options?: { logTailLines?: number }): BackgroundWorkDetail {
|
|
188
|
+
const active = activeNavigatorOrigin;
|
|
189
|
+
const host = navigatorHost();
|
|
190
|
+
const lines = options?.logTailLines ?? NAVIGATOR_DETAIL_ROWS;
|
|
191
|
+
const log = active && host ? host.readDurableTaskLog(active, task.id, lines) : undefined;
|
|
192
|
+
const metadata = [
|
|
193
|
+
{ label: "provider", value: "Background Tasks (durable)" },
|
|
194
|
+
{ label: "kind", value: task.kind },
|
|
195
|
+
{ label: "operation", value: task.operationId },
|
|
196
|
+
{ label: "status", value: `${task.status}${task.terminalOutcome ? ` (${task.terminalOutcome})` : ""}` },
|
|
197
|
+
{ label: "started", value: new Date(task.startedAt).toISOString() },
|
|
198
|
+
...(task.endedAt !== undefined ? [{ label: "ended", value: new Date(task.endedAt).toISOString() }] : []),
|
|
199
|
+
{ label: "cwd", value: task.cwd },
|
|
200
|
+
...(task.exitCode !== null || task.signal ? [{ label: "exit", value: `${task.exitCode ?? "null"}${task.signal ? ` signal ${task.signal}` : ""}` }] : []),
|
|
201
|
+
...(task.reason ? [{ label: "reason", value: task.reason }] : []),
|
|
202
|
+
...(task.stepGateFailure ? [{ label: "step gate", value: `${task.stepGateFailure.command} exited ${task.stepGateFailure.stepExitCode}` }] : []),
|
|
203
|
+
{ label: "stdout", value: task.logPaths.stdout },
|
|
204
|
+
{ label: "stderr", value: task.logPaths.stderr },
|
|
205
|
+
{ label: "events", value: task.logPaths.events },
|
|
206
|
+
...(task.waits.length
|
|
207
|
+
? [{ label: "waits", value: task.waits.map((wait) => `${wait.waitId} ${wait.phase}`).join(", ") }]
|
|
208
|
+
: []),
|
|
209
|
+
];
|
|
210
|
+
const evidenceText = log
|
|
211
|
+
? [log.stdout, log.stderr ? `stderr:\n${log.stderr}` : "", log.events ? `events:\n${log.events}` : ""]
|
|
212
|
+
.filter(Boolean).join("\n") || "(log is empty)"
|
|
213
|
+
: "(durable log is unavailable)";
|
|
214
|
+
return {
|
|
215
|
+
providerId: "background-tasks",
|
|
216
|
+
id: task.id,
|
|
217
|
+
title: task.name || task.id,
|
|
218
|
+
status: task.status,
|
|
219
|
+
statusTone: toneForDurableStatus(task.status),
|
|
220
|
+
subtitle: task.stepGateFailure?.command ?? task.reason ?? compactDurableLabel(task),
|
|
221
|
+
metadata,
|
|
222
|
+
foldedSections: [{
|
|
223
|
+
id: "command",
|
|
224
|
+
label: "command",
|
|
225
|
+
text: task.command ?? (task.argv ? task.argv.join(" ") : "(no command recorded)"),
|
|
226
|
+
collapsedText: compactDurableLabel(task),
|
|
227
|
+
expandedByDefault: true,
|
|
228
|
+
}],
|
|
229
|
+
evidence: {
|
|
230
|
+
label: log?.truncated ? "log tail" : "log",
|
|
231
|
+
text: evidenceText,
|
|
232
|
+
},
|
|
233
|
+
footerActions: [task.active ? "x stop" : "x dismiss"],
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function durableCommandLabel(task: DurableTaskProjection): string {
|
|
238
|
+
if (task.command) return task.command;
|
|
239
|
+
if (task.argv?.length) return task.argv.join(" ");
|
|
240
|
+
return task.name ?? "(no command recorded)";
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function compactDurableLabel(task: DurableTaskProjection): string {
|
|
244
|
+
const value = durableCommandLabel(task).trim();
|
|
245
|
+
const parts = value.split(/\s+/).filter(Boolean);
|
|
246
|
+
return parts.length <= 3 ? value : parts.slice(0, 3).join(" ");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function toneForDurableStatus(status: string): BackgroundWorkRow["statusTone"] {
|
|
250
|
+
switch (status) {
|
|
251
|
+
case "running":
|
|
252
|
+
case "starting": return "running";
|
|
253
|
+
case "succeeded": return "success";
|
|
254
|
+
case "failed":
|
|
255
|
+
case "timed_out":
|
|
256
|
+
case "lost": return "failed";
|
|
257
|
+
case "cancelled": return "warning";
|
|
258
|
+
default: return "muted";
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
72
262
|
function visibleMetas(now = Date.now()): BackgroundTaskMeta[] {
|
|
73
263
|
const active = activeNavigatorOrigin;
|
|
74
264
|
if (!active) return [];
|