@ftrdotdev/pi-delegates 0.1.0 → 0.2.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 +51 -0
- package/OPERATIONS.md +79 -6
- package/README.md +25 -17
- package/ROADMAP.md +21 -0
- package/TESTING.md +153 -318
- package/package.json +4 -3
- package/skills/pi-delegates/SKILL.md +29 -0
- package/skills/pi-delegates/references/launch.md +28 -0
- package/src/docs-contract.test.ts +106 -0
- package/src/domain/definitions.ts +3 -1
- package/src/domain/failure.ts +33 -0
- package/src/domain/model-source.ts +30 -0
- package/src/domain/orchestration/control.test.ts +69 -35
- package/src/domain/orchestration/control.ts +110 -89
- package/src/domain/orchestration/fanout.test.ts +125 -20
- package/src/domain/orchestration/fanout.ts +60 -22
- package/src/domain/orchestration/gates.test.ts +14 -15
- package/src/domain/orchestration/launch.test.ts +210 -11
- package/src/domain/orchestration/launch.ts +142 -32
- package/src/domain/orchestration/requests.test.ts +11 -16
- package/src/domain/orchestration/requests.ts +14 -1
- package/src/domain/orchestration/review.test.ts +89 -11
- package/src/domain/orchestration/run-wait.test.ts +4 -1
- package/src/domain/orchestration/run-wait.ts +1 -1
- package/src/domain/orchestration/workflow.test.ts +157 -16
- package/src/domain/orchestration/workflow.ts +269 -196
- package/src/domain/run-records.ts +214 -52
- package/src/domain/settlement.test.ts +186 -0
- package/src/domain/settlement.ts +137 -0
- package/src/domain/shapes.test.ts +20 -0
- package/src/domain/skills.test.ts +31 -20
- package/src/domain/skills.ts +36 -19
- package/src/domain/toolkit.test.ts +15 -1
- package/src/domain/toolkit.ts +8 -13
- package/src/domain/turn-budget.test.ts +37 -0
- package/src/domain/turn-budget.ts +23 -0
- package/src/domain/view.test.ts +78 -14
- package/src/domain/view.ts +67 -35
- package/src/extension/commands.test.ts +27 -23
- package/src/extension/commands.ts +10 -7
- package/src/extension/entry.test.ts +214 -56
- package/src/extension/entry.ts +78 -40
- package/src/extension/gate-alerts.test.ts +10 -8
- package/src/extension/inspector-command.test.ts +173 -21
- package/src/extension/inspector-command.ts +100 -53
- package/src/extension/prompt.test.ts +2 -4
- package/src/extension/retry-observer.test.ts +292 -0
- package/src/extension/retry-observer.ts +150 -0
- package/src/extension/settle-alerts.test.ts +579 -0
- package/src/extension/settle-alerts.ts +175 -0
- package/src/extension/shortcut.test.ts +6 -8
- package/src/extension/tools.test.ts +325 -24
- package/src/extension/tools.ts +61 -13
- package/src/extension/version.test.ts +21 -29
- package/src/goldens.test.ts +294 -0
- package/src/harness-policy.test.ts +13 -11
- package/src/layers.test.ts +309 -0
- package/src/layers.ts +405 -0
- package/src/package-manifest.test.ts +32 -19
- package/src/presentation/actions.test.ts +8 -8
- package/src/presentation/activity.test.ts +7 -8
- package/src/presentation/close.test.ts +14 -16
- package/src/presentation/inspector.test.ts +14 -11
- package/src/presentation/inspector.ts +27 -25
- package/src/presentation/overlay.test.ts +1 -3
- package/src/presentation/repaint.test.ts +226 -190
- package/src/presentation/snapshot.test.ts +5 -0
- package/src/presentation/status.test.ts +43 -0
- package/src/presentation/status.ts +30 -0
- package/src/presentation/strip.test.ts +13 -6
- package/src/presentation/strip.ts +7 -14
- package/src/presentation/wording.test.ts +153 -3
- package/src/presentation/wording.ts +78 -6
- package/src/runtime/bridge.ts +38 -44
- package/src/runtime/{timers.test.ts → clock.test.ts} +1 -1
- package/src/runtime/clock.ts +31 -0
- package/src/runtime/config.test.ts +26 -7
- package/src/runtime/config.ts +6 -1
- package/src/runtime/execution-env.test.ts +6 -8
- package/src/runtime/extensions.test.ts +4 -4
- package/src/runtime/extensions.ts +23 -11
- package/src/runtime/handle.ts +54 -0
- package/src/runtime/log.test.ts +19 -7
- package/src/runtime/log.ts +23 -0
- package/src/runtime/mcp.test.ts +29 -32
- package/src/runtime/mcp.ts +70 -3
- package/src/runtime/runtime-state.test.ts +25 -0
- package/src/runtime/runtime-state.ts +36 -0
- package/src/runtime/session-tag.ts +11 -0
- package/src/runtime/session.test.ts +24 -31
- package/src/runtime/session.ts +99 -261
- package/src/runtime/storage.ts +29 -0
- package/src/test-hygiene.test.ts +97 -0
- package/src/runtime/timers.ts +0 -10
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { LiveDoc, type LiveState } from "@earendil-works/pi-durable";
|
|
2
|
+
import { listedRuns, RunDoc } from "../domain/run-records.ts";
|
|
3
|
+
import { conversationId as toConversationId } from "../domain/ids.ts";
|
|
4
|
+
import { errorFields, type LogSink } from "../runtime/log.ts";
|
|
5
|
+
import type { SessionRuntime } from "../runtime/session.ts";
|
|
6
|
+
|
|
7
|
+
/** How often the observer looks for runs and child conversations it does not watch yet. */
|
|
8
|
+
export const RETRY_RECONCILE_MS = 1_000;
|
|
9
|
+
|
|
10
|
+
export interface RetryObserverDeps {
|
|
11
|
+
readonly runtime: SessionRuntime;
|
|
12
|
+
readonly log: LogSink;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface RetryObserverWatch {
|
|
16
|
+
readonly close: () => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Counts each durable retry attempt on the run document and logs it once. It
|
|
21
|
+
* observes the decision; it never makes one, and never aborts or resumes a run.
|
|
22
|
+
* A retry is not a terminal fact, so the dedupe key lives in memory and the
|
|
23
|
+
* count on the document survives a restart.
|
|
24
|
+
*/
|
|
25
|
+
export const startRetryObserver = (deps: RetryObserverDeps): RetryObserverWatch => {
|
|
26
|
+
let closed = false;
|
|
27
|
+
let armed: unknown;
|
|
28
|
+
let chain: Promise<void> = Promise.resolve();
|
|
29
|
+
const detaches = new Map<string, () => void>();
|
|
30
|
+
const seen = new Set<string>();
|
|
31
|
+
|
|
32
|
+
const log = (error: unknown): void => {
|
|
33
|
+
deps.log.write("warn", "retry.observer_failed", errorFields(error));
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const enqueue = (task: () => Promise<void>): Promise<void> => {
|
|
37
|
+
chain = chain.then(task).catch(log);
|
|
38
|
+
return chain;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const count = async (
|
|
42
|
+
runId: string,
|
|
43
|
+
attempt: number,
|
|
44
|
+
at: number,
|
|
45
|
+
detail: string,
|
|
46
|
+
): Promise<void> => {
|
|
47
|
+
const key = `${runId}:${attempt}:${at}`;
|
|
48
|
+
if (seen.has(key)) return;
|
|
49
|
+
// A writer that must not invent a document snapshots first. The key is only
|
|
50
|
+
// spent once the document exists, so a retry seen too early is still counted.
|
|
51
|
+
const doc = await deps.runtime.snapshot(RunDoc, runId);
|
|
52
|
+
if (doc === undefined) return;
|
|
53
|
+
seen.add(key);
|
|
54
|
+
await deps.runtime.run((context) =>
|
|
55
|
+
deps.runtime.harness.commit(async (tx) => {
|
|
56
|
+
const run = await tx.doc(RunDoc, runId, { definition: doc.definition, lane: doc.lane });
|
|
57
|
+
run.retryCount += 1;
|
|
58
|
+
}, context),
|
|
59
|
+
);
|
|
60
|
+
deps.log.write("warn", "run.retry", { runId, attempt, at, detail });
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const onLive = (runId: string, live: Readonly<LiveState>): void => {
|
|
64
|
+
const generation = live.generation;
|
|
65
|
+
const retry = generation?.retry;
|
|
66
|
+
const attempt = generation?.attempt;
|
|
67
|
+
if (retry === undefined || attempt === undefined) return;
|
|
68
|
+
void enqueue(() => count(runId, attempt, retry.at, retry.error));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const attach = async (runId: string, conversationId: number): Promise<void> => {
|
|
72
|
+
if (closed || detaches.has(runId)) return;
|
|
73
|
+
let advanced = false;
|
|
74
|
+
let ended = false;
|
|
75
|
+
const observer = {
|
|
76
|
+
advance(call: { readonly kind: string; readonly value?: unknown }) {
|
|
77
|
+
if (call.kind !== "value" || ended) return;
|
|
78
|
+
advanced = true;
|
|
79
|
+
onLive(runId, call.value as Readonly<LiveState>);
|
|
80
|
+
},
|
|
81
|
+
end() {
|
|
82
|
+
ended = true;
|
|
83
|
+
detaches.delete(runId);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
const observation = await deps.runtime.run((context) =>
|
|
87
|
+
deps.runtime.harness.observeDoc(LiveDoc, toConversationId(conversationId), observer, context),
|
|
88
|
+
);
|
|
89
|
+
if (observation === undefined) return;
|
|
90
|
+
detaches.set(runId, () => {
|
|
91
|
+
ended = true;
|
|
92
|
+
observation.detach();
|
|
93
|
+
detaches.delete(runId);
|
|
94
|
+
});
|
|
95
|
+
if (!advanced && !ended) onLive(runId, observation.value);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const reconcile = async (): Promise<void> => {
|
|
99
|
+
if (closed) return;
|
|
100
|
+
// Group children are reached through their parent document, never the index.
|
|
101
|
+
// Every indexed run is read on every tick, watched or not: a workflow commits
|
|
102
|
+
// its children to `childRunIds` after it starts, so a skipped parent read
|
|
103
|
+
// would hide each child that appears later. A child has no returned document
|
|
104
|
+
// and is read once, here.
|
|
105
|
+
// The read set is the whole index, not a window: a cap would drop the retry
|
|
106
|
+
// lines of an old live run, and the log promises one line per attempt. A
|
|
107
|
+
// loaded document is served from the session's in-memory cache, so a tick
|
|
108
|
+
// costs one lookup per run rather than one store read.
|
|
109
|
+
const { runIds, docs } = await listedRuns(deps.runtime);
|
|
110
|
+
for (const runId of runIds) {
|
|
111
|
+
if (closed) return;
|
|
112
|
+
if (detaches.has(runId)) continue;
|
|
113
|
+
const doc = docs.get(runId) ?? (await deps.runtime.snapshot(RunDoc, runId));
|
|
114
|
+
const conversationId = doc?.childConversationId;
|
|
115
|
+
if (conversationId === null || conversationId === undefined) continue;
|
|
116
|
+
await attach(runId, conversationId);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const schedule = (): void => {
|
|
121
|
+
if (closed) return;
|
|
122
|
+
armed = deps.runtime.timers.setTimeout(() => {
|
|
123
|
+
armed = undefined;
|
|
124
|
+
void enqueue(reconcile);
|
|
125
|
+
schedule();
|
|
126
|
+
}, RETRY_RECONCILE_MS);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
void enqueue(reconcile);
|
|
130
|
+
schedule();
|
|
131
|
+
|
|
132
|
+
const unsubscribe = deps.runtime.onState((state) => {
|
|
133
|
+
if (state.kind === "closed" || state.kind === "failing") void close();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const close = async (): Promise<void> => {
|
|
137
|
+
if (closed) return;
|
|
138
|
+
closed = true;
|
|
139
|
+
unsubscribe();
|
|
140
|
+
if (armed !== undefined) {
|
|
141
|
+
deps.runtime.timers.clearTimeout(armed);
|
|
142
|
+
armed = undefined;
|
|
143
|
+
}
|
|
144
|
+
await chain;
|
|
145
|
+
for (const detach of detaches.values()) detach();
|
|
146
|
+
detaches.clear();
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return { close };
|
|
150
|
+
};
|