@leitwerk-dev/worker 0.1.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/dist/LICENSE +13 -0
- package/dist/automatic-turn-executor.d.ts +28 -0
- package/dist/automatic-turn-executor.d.ts.map +1 -0
- package/dist/automatic-turn-executor.js +58 -0
- package/dist/automatic-turn-executor.js.map +1 -0
- package/dist/chunk-N5HLJDWA.js +8124 -0
- package/dist/chunk-N5HLJDWA.js.map +1 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +51 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/entry-runtime.d.ts +24 -0
- package/dist/entry-runtime.d.ts.map +1 -0
- package/dist/entry-runtime.js +160 -0
- package/dist/entry-runtime.js.map +1 -0
- package/dist/event-translator.d.ts +8 -0
- package/dist/event-translator.d.ts.map +1 -0
- package/dist/event-translator.js +12 -0
- package/dist/event-translator.js.map +1 -0
- package/dist/extension-entry-roots.d.ts +7 -0
- package/dist/extension-entry-roots.d.ts.map +1 -0
- package/dist/extension-entry-roots.js +41 -0
- package/dist/extension-entry-roots.js.map +1 -0
- package/dist/identified-pi-entry.d.ts +56 -0
- package/dist/identified-pi-entry.d.ts.map +1 -0
- package/dist/identified-pi-entry.js +125 -0
- package/dist/identified-pi-entry.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/input-consumer.d.ts +24 -0
- package/dist/input-consumer.d.ts.map +1 -0
- package/dist/input-consumer.js +34 -0
- package/dist/input-consumer.js.map +1 -0
- package/dist/ipc.d.ts +22 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +192 -0
- package/dist/ipc.js.map +1 -0
- package/dist/logical-prompt-runner.d.ts +49 -0
- package/dist/logical-prompt-runner.d.ts.map +1 -0
- package/dist/logical-prompt-runner.js +225 -0
- package/dist/logical-prompt-runner.js.map +1 -0
- package/dist/managed-pi-agent-dir.d.ts +13 -0
- package/dist/managed-pi-agent-dir.d.ts.map +1 -0
- package/dist/managed-pi-agent-dir.js +66 -0
- package/dist/managed-pi-agent-dir.js.map +1 -0
- package/dist/managed-pi-bootstrap.d.ts +24 -0
- package/dist/managed-pi-bootstrap.d.ts.map +1 -0
- package/dist/managed-pi-bootstrap.js +135 -0
- package/dist/managed-pi-bootstrap.js.map +1 -0
- package/dist/missing-tool-recovery-runner.d.ts +23 -0
- package/dist/missing-tool-recovery-runner.d.ts.map +1 -0
- package/dist/missing-tool-recovery-runner.js +73 -0
- package/dist/missing-tool-recovery-runner.js.map +1 -0
- package/dist/pi-adapter.d.ts +134 -0
- package/dist/pi-adapter.d.ts.map +1 -0
- package/dist/pi-adapter.js +1648 -0
- package/dist/pi-adapter.js.map +1 -0
- package/dist/pi-branch-guard.d.ts +39 -0
- package/dist/pi-branch-guard.d.ts.map +1 -0
- package/dist/pi-branch-guard.js +75 -0
- package/dist/pi-branch-guard.js.map +1 -0
- package/dist/pi-event-reporter.d.ts +15 -0
- package/dist/pi-event-reporter.d.ts.map +1 -0
- package/dist/pi-event-reporter.js +136 -0
- package/dist/pi-event-reporter.js.map +1 -0
- package/dist/pi-resource-bundle.d.ts +12 -0
- package/dist/pi-resource-bundle.d.ts.map +1 -0
- package/dist/pi-resource-bundle.js +58 -0
- package/dist/pi-resource-bundle.js.map +1 -0
- package/dist/pre-turn-targeted-inputs.d.ts +44 -0
- package/dist/pre-turn-targeted-inputs.d.ts.map +1 -0
- package/dist/pre-turn-targeted-inputs.js +101 -0
- package/dist/pre-turn-targeted-inputs.js.map +1 -0
- package/dist/prompt-guards.d.ts +52 -0
- package/dist/prompt-guards.d.ts.map +1 -0
- package/dist/prompt-guards.js +318 -0
- package/dist/prompt-guards.js.map +1 -0
- package/dist/question-bridge.d.ts +16 -0
- package/dist/question-bridge.d.ts.map +1 -0
- package/dist/question-bridge.js +55 -0
- package/dist/question-bridge.js.map +1 -0
- package/dist/question-tool.d.ts +14 -0
- package/dist/question-tool.d.ts.map +1 -0
- package/dist/question-tool.js +54 -0
- package/dist/question-tool.js.map +1 -0
- package/dist/resource-loader-factory.d.ts +11 -0
- package/dist/resource-loader-factory.d.ts.map +1 -0
- package/dist/resource-loader-factory.js +87 -0
- package/dist/resource-loader-factory.js.map +1 -0
- package/dist/result-image-upload.d.ts +14 -0
- package/dist/result-image-upload.d.ts.map +1 -0
- package/dist/result-image-upload.js +183 -0
- package/dist/result-image-upload.js.map +1 -0
- package/dist/runtime/adapters.d.ts +48 -0
- package/dist/runtime/adapters.d.ts.map +1 -0
- package/dist/runtime/adapters.js +15 -0
- package/dist/runtime/adapters.js.map +1 -0
- package/dist/runtime/bootstrap-session.d.ts +95 -0
- package/dist/runtime/bootstrap-session.d.ts.map +1 -0
- package/dist/runtime/bootstrap-session.js +198 -0
- package/dist/runtime/bootstrap-session.js.map +1 -0
- package/dist/runtime/bootstrap.d.ts +81 -0
- package/dist/runtime/bootstrap.d.ts.map +1 -0
- package/dist/runtime/bootstrap.js +440 -0
- package/dist/runtime/bootstrap.js.map +1 -0
- package/dist/runtime/failure-policy.d.ts +79 -0
- package/dist/runtime/failure-policy.d.ts.map +1 -0
- package/dist/runtime/failure-policy.js +182 -0
- package/dist/runtime/failure-policy.js.map +1 -0
- package/dist/runtime/index.d.ts +9 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +326 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/lifecycle-reducer.d.ts +328 -0
- package/dist/runtime/lifecycle-reducer.d.ts.map +1 -0
- package/dist/runtime/lifecycle-reducer.js +1100 -0
- package/dist/runtime/lifecycle-reducer.js.map +1 -0
- package/dist/runtime/llm-turn-execution.d.ts +42 -0
- package/dist/runtime/llm-turn-execution.d.ts.map +1 -0
- package/dist/runtime/llm-turn-execution.js +472 -0
- package/dist/runtime/llm-turn-execution.js.map +1 -0
- package/dist/runtime/settings.d.ts +11 -0
- package/dist/runtime/settings.d.ts.map +1 -0
- package/dist/runtime/settings.js +23 -0
- package/dist/runtime/settings.js.map +1 -0
- package/dist/runtime/snapshot-policy.d.ts +11 -0
- package/dist/runtime/snapshot-policy.d.ts.map +1 -0
- package/dist/runtime/snapshot-policy.js +16 -0
- package/dist/runtime/snapshot-policy.js.map +1 -0
- package/dist/runtime/turn-execution.d.ts +49 -0
- package/dist/runtime/turn-execution.d.ts.map +1 -0
- package/dist/runtime/turn-execution.js +120 -0
- package/dist/runtime/turn-execution.js.map +1 -0
- package/dist/session-snapshot-exchange.d.ts +39 -0
- package/dist/session-snapshot-exchange.d.ts.map +1 -0
- package/dist/session-snapshot-exchange.js +102 -0
- package/dist/session-snapshot-exchange.js.map +1 -0
- package/dist/test-helpers/fake-websocket.d.ts +16 -0
- package/dist/test-helpers/fake-websocket.d.ts.map +1 -0
- package/dist/test-helpers/fake-websocket.js +40 -0
- package/dist/test-helpers/fake-websocket.js.map +1 -0
- package/dist/test-helpers/ipc-harness.d.ts +11 -0
- package/dist/test-helpers/ipc-harness.d.ts.map +1 -0
- package/dist/test-helpers/ipc-harness.js +17 -0
- package/dist/test-helpers/ipc-harness.js.map +1 -0
- package/dist/test-helpers/worker-message-builders.d.ts +21 -0
- package/dist/test-helpers/worker-message-builders.d.ts.map +1 -0
- package/dist/test-helpers/worker-message-builders.js +62 -0
- package/dist/test-helpers/worker-message-builders.js.map +1 -0
- package/dist/turn-continuation.d.ts +28 -0
- package/dist/turn-continuation.d.ts.map +1 -0
- package/dist/turn-continuation.js +80 -0
- package/dist/turn-continuation.js.map +1 -0
- package/dist/turn-execution-error.d.ts +39 -0
- package/dist/turn-execution-error.d.ts.map +1 -0
- package/dist/turn-execution-error.js +64 -0
- package/dist/turn-execution-error.js.map +1 -0
- package/dist/turn-execution-result.d.ts +35 -0
- package/dist/turn-execution-result.d.ts.map +1 -0
- package/dist/turn-execution-result.js +21 -0
- package/dist/turn-execution-result.js.map +1 -0
- package/dist/turn-outcome-tool-session.d.ts +50 -0
- package/dist/turn-outcome-tool-session.d.ts.map +1 -0
- package/dist/turn-outcome-tool-session.js +209 -0
- package/dist/turn-outcome-tool-session.js.map +1 -0
- package/dist/turn-record-id.d.ts +3 -0
- package/dist/turn-record-id.d.ts.map +1 -0
- package/dist/turn-record-id.js +10 -0
- package/dist/turn-record-id.js.map +1 -0
- package/dist/turn-result-markdown.d.ts +74 -0
- package/dist/turn-result-markdown.d.ts.map +1 -0
- package/dist/turn-result-markdown.js +263 -0
- package/dist/turn-result-markdown.js.map +1 -0
- package/dist/turn-tree-strategy.d.ts +29 -0
- package/dist/turn-tree-strategy.d.ts.map +1 -0
- package/dist/turn-tree-strategy.js +80 -0
- package/dist/turn-tree-strategy.js.map +1 -0
- package/dist/worker-entry.d.ts +2 -0
- package/dist/worker-entry.d.ts.map +1 -0
- package/dist/worker-entry.js +7 -0
- package/dist/worker-entry.js.map +1 -0
- package/dist/worker-http.d.ts +2 -0
- package/dist/worker-http.d.ts.map +1 -0
- package/dist/worker-http.js +9 -0
- package/dist/worker-http.js.map +1 -0
- package/dist/worker-ipc-reporter.d.ts +29 -0
- package/dist/worker-ipc-reporter.d.ts.map +1 -0
- package/dist/worker-ipc-reporter.js +58 -0
- package/dist/worker-ipc-reporter.js.map +1 -0
- package/dist/worker-payloads.d.ts +10 -0
- package/dist/worker-payloads.d.ts.map +1 -0
- package/dist/worker-payloads.js +66 -0
- package/dist/worker-payloads.js.map +1 -0
- package/dist/worker-start-payload.d.ts +7 -0
- package/dist/worker-start-payload.d.ts.map +1 -0
- package/dist/worker-start-payload.js +5 -0
- package/dist/worker-start-payload.js.map +1 -0
- package/dist/workspace/manifest.d.ts +35 -0
- package/dist/workspace/manifest.d.ts.map +1 -0
- package/dist/workspace/manifest.js +79 -0
- package/dist/workspace/manifest.js.map +1 -0
- package/dist/workspace/node-run-root-git-ops.d.ts +17 -0
- package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -0
- package/dist/workspace/node-run-root-git-ops.js +96 -0
- package/dist/workspace/node-run-root-git-ops.js.map +1 -0
- package/dist/workspace/resource-aggregator.d.ts +15 -0
- package/dist/workspace/resource-aggregator.d.ts.map +1 -0
- package/dist/workspace/resource-aggregator.js +39 -0
- package/dist/workspace/resource-aggregator.js.map +1 -0
- package/dist/workspace/run-root.d.ts +52 -0
- package/dist/workspace/run-root.d.ts.map +1 -0
- package/dist/workspace/run-root.js +209 -0
- package/dist/workspace/run-root.js.map +1 -0
- package/package.json +55 -0
- package/src/automatic-turn-executor.ts +106 -0
- package/src/diagnostics.ts +82 -0
- package/src/entry-runtime.ts +235 -0
- package/src/event-translator.ts +22 -0
- package/src/extension-entry-roots.ts +43 -0
- package/src/identified-pi-entry.ts +188 -0
- package/src/index.ts +54 -0
- package/src/input-consumer.ts +71 -0
- package/src/ipc.ts +250 -0
- package/src/logical-prompt-runner.ts +320 -0
- package/src/managed-pi-agent-dir.ts +86 -0
- package/src/managed-pi-bootstrap.ts +208 -0
- package/src/missing-tool-recovery-runner.ts +112 -0
- package/src/pi-adapter.ts +2197 -0
- package/src/pi-branch-guard.ts +104 -0
- package/src/pi-event-reporter.ts +164 -0
- package/src/pi-resource-bundle.ts +90 -0
- package/src/pre-turn-targeted-inputs.ts +167 -0
- package/src/prompt-guards.ts +385 -0
- package/src/question-bridge.ts +69 -0
- package/src/question-tool.ts +65 -0
- package/src/resource-loader-factory.ts +115 -0
- package/src/result-image-upload.ts +256 -0
- package/src/runtime/adapters.ts +67 -0
- package/src/runtime/bootstrap-session.ts +294 -0
- package/src/runtime/bootstrap.ts +621 -0
- package/src/runtime/failure-policy.ts +274 -0
- package/src/runtime/index.ts +362 -0
- package/src/runtime/lifecycle-reducer.ts +1537 -0
- package/src/runtime/llm-turn-execution.ts +696 -0
- package/src/runtime/settings.ts +41 -0
- package/src/runtime/snapshot-policy.ts +28 -0
- package/src/runtime/turn-execution.ts +203 -0
- package/src/session-snapshot-exchange.ts +155 -0
- package/src/test-helpers/fake-websocket.ts +46 -0
- package/src/test-helpers/ipc-harness.ts +19 -0
- package/src/test-helpers/worker-message-builders.ts +92 -0
- package/src/turn-continuation.ts +147 -0
- package/src/turn-execution-error.ts +102 -0
- package/src/turn-execution-result.ts +48 -0
- package/src/turn-outcome-tool-session.ts +311 -0
- package/src/turn-record-id.ts +17 -0
- package/src/turn-result-markdown.ts +355 -0
- package/src/turn-tree-strategy.ts +141 -0
- package/src/worker-entry.ts +8 -0
- package/src/worker-http.ts +6 -0
- package/src/worker-ipc-reporter.ts +100 -0
- package/src/worker-payloads.ts +94 -0
- package/src/worker-start-payload.ts +10 -0
- package/src/workspace/manifest.ts +126 -0
- package/src/workspace/node-run-root-git-ops.ts +137 -0
- package/src/workspace/resource-aggregator.ts +43 -0
- package/src/workspace/run-root.ts +361 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { PiTreeEntry } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
|
|
3
|
+
export type PiBranchOperation = "prompt" | "continue";
|
|
4
|
+
|
|
5
|
+
export class PiBranchDriftError extends Error {
|
|
6
|
+
readonly errorClass = "infrastructure" as const;
|
|
7
|
+
readonly failureCode = "branch_drift" as const;
|
|
8
|
+
readonly recoveryContext = null;
|
|
9
|
+
readonly restorePrimaryLeaf = false;
|
|
10
|
+
readonly operation: PiBranchOperation;
|
|
11
|
+
readonly anchorEntryId: string | null;
|
|
12
|
+
readonly rejectedResultEntryId: string;
|
|
13
|
+
readonly failureDetails: Record<string, unknown>;
|
|
14
|
+
|
|
15
|
+
constructor(input: {
|
|
16
|
+
operation: PiBranchOperation;
|
|
17
|
+
anchorEntryId?: string | null;
|
|
18
|
+
rejectedResultEntryId: string;
|
|
19
|
+
reason?: string;
|
|
20
|
+
}) {
|
|
21
|
+
const anchorEntryId = input.anchorEntryId ?? null;
|
|
22
|
+
super(
|
|
23
|
+
input.reason ??
|
|
24
|
+
(anchorEntryId
|
|
25
|
+
? `Pi ${input.operation} result branch drifted: result entry '${input.rejectedResultEntryId}' is not on the branch anchored at '${anchorEntryId}'`
|
|
26
|
+
: `Pi ${input.operation} result branch drifted: result entry '${input.rejectedResultEntryId}' is not on the branch selected before the turn`),
|
|
27
|
+
);
|
|
28
|
+
this.name = "PiBranchDriftError";
|
|
29
|
+
this.operation = input.operation;
|
|
30
|
+
this.anchorEntryId = anchorEntryId;
|
|
31
|
+
this.rejectedResultEntryId = input.rejectedResultEntryId;
|
|
32
|
+
this.failureDetails = {
|
|
33
|
+
operation: input.operation,
|
|
34
|
+
anchorEntryId,
|
|
35
|
+
rejectedResultEntryId: input.rejectedResultEntryId,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toTurnFailureOptions() {
|
|
40
|
+
return {
|
|
41
|
+
recoveryContext: this.recoveryContext,
|
|
42
|
+
restorePrimaryLeaf: this.restorePrimaryLeaf,
|
|
43
|
+
failureCode: this.failureCode,
|
|
44
|
+
failureDetails: this.failureDetails,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function isPiBranchDriftError(error: unknown): error is PiBranchDriftError {
|
|
50
|
+
return (
|
|
51
|
+
error instanceof PiBranchDriftError ||
|
|
52
|
+
(error instanceof Error &&
|
|
53
|
+
error.name === "PiBranchDriftError" &&
|
|
54
|
+
(error as { failureCode?: unknown }).failureCode === "branch_drift" &&
|
|
55
|
+
typeof (error as { toTurnFailureOptions?: unknown }).toTurnFailureOptions === "function")
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function resolveExecutionAnchorEntryId(input: {
|
|
60
|
+
operation: PiBranchOperation;
|
|
61
|
+
startLeafId: string | null;
|
|
62
|
+
createdEntries: readonly PiTreeEntry[];
|
|
63
|
+
}): string | null {
|
|
64
|
+
return input.operation === "continue"
|
|
65
|
+
? (input.startLeafId ?? input.createdEntries[0]?.id ?? null)
|
|
66
|
+
: (input.createdEntries.find((entry) => entry.message?.role === "user")?.id ??
|
|
67
|
+
input.createdEntries[0]?.id ??
|
|
68
|
+
input.startLeafId ??
|
|
69
|
+
null);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function assertResultStayedOnExecutionBranch(input: {
|
|
73
|
+
operation: PiBranchOperation;
|
|
74
|
+
startLeafId: string | null;
|
|
75
|
+
beforeEntryIds: ReadonlySet<string>;
|
|
76
|
+
anchorEntryId: string | null;
|
|
77
|
+
resultEntryId: string | null;
|
|
78
|
+
getBranch(entryId: string): PiTreeEntry[];
|
|
79
|
+
}): void {
|
|
80
|
+
const { anchorEntryId, beforeEntryIds, getBranch, operation, resultEntryId, startLeafId } = input;
|
|
81
|
+
if (!resultEntryId) return;
|
|
82
|
+
const resultBranch = getBranch(resultEntryId);
|
|
83
|
+
const resultBranchIds = new Set(resultBranch.map((entry) => entry.id));
|
|
84
|
+
const reject = (reason?: string): never => {
|
|
85
|
+
throw new PiBranchDriftError({
|
|
86
|
+
operation,
|
|
87
|
+
anchorEntryId,
|
|
88
|
+
rejectedResultEntryId: resultEntryId,
|
|
89
|
+
reason,
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
if (
|
|
93
|
+
!resultBranchIds.has(resultEntryId) ||
|
|
94
|
+
(anchorEntryId && !resultBranchIds.has(anchorEntryId))
|
|
95
|
+
) {
|
|
96
|
+
reject();
|
|
97
|
+
}
|
|
98
|
+
if (startLeafId && !resultBranchIds.has(startLeafId)) {
|
|
99
|
+
reject(`Pi ${operation} branch drifted away from the selected leaf '${startLeafId}'`);
|
|
100
|
+
}
|
|
101
|
+
if (!startLeafId && resultBranch.some((entry) => beforeEntryIds.has(entry.id))) {
|
|
102
|
+
reject(`Pi ${operation} branch drifted under an existing root branch`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { translatePiEvent } from "./event-translator.js";
|
|
2
|
+
import type { PiEvent, PiSessionDiagnostic, PiTreeHandle } from "./pi-adapter.js";
|
|
3
|
+
import type { WorkerIpcReporter } from "./worker-ipc-reporter.js";
|
|
4
|
+
|
|
5
|
+
export interface PiEventReporter {
|
|
6
|
+
attach(piHandle: PiTreeHandle): void;
|
|
7
|
+
resetRetryState(): void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface PiEventReporterOptions {
|
|
11
|
+
reporter: Pick<WorkerIpcReporter, "workerEvent" | "workerTrace" | "workerError">;
|
|
12
|
+
emitExtensionEvent?(event: string, payload: unknown): void;
|
|
13
|
+
getCurrentSelectedTurnId?(): string | null;
|
|
14
|
+
getSessionTainted?(): boolean;
|
|
15
|
+
onLifecycleObservation?(kind: "pi_turn_started" | "pi_turn_ended"): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readPiEventString(data: Record<string, unknown>, key: string): string | null {
|
|
19
|
+
const value = data[key];
|
|
20
|
+
return typeof value === "string" && value.trim() !== "" ? value : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function readPiEventNumber(data: Record<string, unknown>, key: string): number | null {
|
|
24
|
+
const value = data[key];
|
|
25
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
29
|
+
const parsed = Number(value);
|
|
30
|
+
if (Number.isFinite(parsed)) {
|
|
31
|
+
return parsed;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createPiEventReporter(options: PiEventReporterOptions): PiEventReporter {
|
|
38
|
+
let currentPiAttempt = 1;
|
|
39
|
+
let currentPiMaxAttempts: number | null = null;
|
|
40
|
+
|
|
41
|
+
const resetPiRetryState = () => {
|
|
42
|
+
currentPiAttempt = 1;
|
|
43
|
+
currentPiMaxAttempts = null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const reportPiError = (event: PiEvent) => {
|
|
47
|
+
const message =
|
|
48
|
+
readPiEventString(event.data, "message") ??
|
|
49
|
+
readPiEventString(event.data, "errorMessage") ??
|
|
50
|
+
"Pi reported an error";
|
|
51
|
+
const toolName = readPiEventString(event.data, "toolName");
|
|
52
|
+
const source = readPiEventString(event.data, "source");
|
|
53
|
+
const provider = readPiEventString(event.data, "provider");
|
|
54
|
+
const model = readPiEventString(event.data, "model");
|
|
55
|
+
const stopReason = readPiEventString(event.data, "stopReason");
|
|
56
|
+
options.reporter.workerError({
|
|
57
|
+
level: "error",
|
|
58
|
+
code: toolName || source === "tool" ? "pi.tool_error" : "pi.request_failed",
|
|
59
|
+
message,
|
|
60
|
+
timestamp: event.timestamp,
|
|
61
|
+
turnId: event.turnId,
|
|
62
|
+
errorClass: toolName || source === "tool" ? "infrastructure" : "llm_error",
|
|
63
|
+
details: {
|
|
64
|
+
attempt: currentPiAttempt,
|
|
65
|
+
...(currentPiMaxAttempts !== null ? { maxAttempts: currentPiMaxAttempts } : {}),
|
|
66
|
+
...(source ? { source } : {}),
|
|
67
|
+
...(toolName ? { toolName } : {}),
|
|
68
|
+
...(provider ? { provider } : {}),
|
|
69
|
+
...(model ? { model } : {}),
|
|
70
|
+
...(stopReason ? { stopReason } : {}),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const reportPiRetryLifecycle = (event: PiEvent) => {
|
|
76
|
+
const attempt = readPiEventNumber(event.data, "attempt");
|
|
77
|
+
const maxAttempts = readPiEventNumber(event.data, "maxAttempts");
|
|
78
|
+
const delayMs = readPiEventNumber(event.data, "delayMs");
|
|
79
|
+
const errorMessage = readPiEventString(event.data, "errorMessage");
|
|
80
|
+
const finalError = readPiEventString(event.data, "finalError");
|
|
81
|
+
const message =
|
|
82
|
+
readPiEventString(event.data, "message") ??
|
|
83
|
+
(event.type === "retry.start"
|
|
84
|
+
? "Pi scheduled an automatic retry"
|
|
85
|
+
: event.data.success === true
|
|
86
|
+
? "Pi retry sequence succeeded"
|
|
87
|
+
: "Pi retry sequence ended");
|
|
88
|
+
const success = event.data.success === true;
|
|
89
|
+
options.reporter.workerTrace({
|
|
90
|
+
level: event.type === "retry.end" && success ? "info" : "warn",
|
|
91
|
+
timestamp: event.timestamp,
|
|
92
|
+
code:
|
|
93
|
+
event.type === "retry.start"
|
|
94
|
+
? "pi.retry_scheduled"
|
|
95
|
+
: success
|
|
96
|
+
? "pi.retry_succeeded"
|
|
97
|
+
: "pi.retry_exhausted",
|
|
98
|
+
message,
|
|
99
|
+
turnId: event.turnId,
|
|
100
|
+
details: {
|
|
101
|
+
...(attempt !== null ? { attempt } : {}),
|
|
102
|
+
...(maxAttempts !== null ? { maxAttempts } : {}),
|
|
103
|
+
...(delayMs !== null ? { delayMs } : {}),
|
|
104
|
+
...(errorMessage ? { errorMessage } : {}),
|
|
105
|
+
...(finalError ? { finalError } : {}),
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
if (maxAttempts !== null) {
|
|
109
|
+
currentPiMaxAttempts = maxAttempts;
|
|
110
|
+
}
|
|
111
|
+
if (event.type === "retry.start" && attempt !== null) {
|
|
112
|
+
currentPiAttempt = Math.max(currentPiAttempt, attempt + 1);
|
|
113
|
+
}
|
|
114
|
+
if (event.type === "retry.end" && success) {
|
|
115
|
+
resetPiRetryState();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const reportPiDiagnostic = (diagnostic: PiSessionDiagnostic) => {
|
|
120
|
+
options.reporter.workerTrace({
|
|
121
|
+
level: diagnostic.level,
|
|
122
|
+
code: diagnostic.code,
|
|
123
|
+
message: diagnostic.message,
|
|
124
|
+
timestamp: diagnostic.timestamp,
|
|
125
|
+
...(diagnostic.turnId ? { turnId: diagnostic.turnId } : {}),
|
|
126
|
+
...(diagnostic.details ? { details: diagnostic.details } : {}),
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
attach(piHandle) {
|
|
132
|
+
piHandle.subscribe((event) => {
|
|
133
|
+
if (event.type === "turn.start") {
|
|
134
|
+
options.onLifecycleObservation?.("pi_turn_started");
|
|
135
|
+
resetPiRetryState();
|
|
136
|
+
}
|
|
137
|
+
if (event.type === "turn.end") {
|
|
138
|
+
options.onLifecycleObservation?.("pi_turn_ended");
|
|
139
|
+
resetPiRetryState();
|
|
140
|
+
}
|
|
141
|
+
if (event.type === "error") {
|
|
142
|
+
reportPiError(event);
|
|
143
|
+
}
|
|
144
|
+
if (event.type === "retry.start" || event.type === "retry.end") {
|
|
145
|
+
reportPiRetryLifecycle(event);
|
|
146
|
+
}
|
|
147
|
+
if (options.getSessionTainted?.()) return;
|
|
148
|
+
const translated = translatePiEvent(event, options.getCurrentSelectedTurnId?.() ?? null);
|
|
149
|
+
const payload = options.reporter.workerEvent(
|
|
150
|
+
translated.eventType,
|
|
151
|
+
translated.data,
|
|
152
|
+
translated.selectedTurnId,
|
|
153
|
+
);
|
|
154
|
+
options.emitExtensionEvent?.("worker.pi_event", payload);
|
|
155
|
+
});
|
|
156
|
+
piHandle.subscribeDiagnostics?.((diagnostic) => {
|
|
157
|
+
reportPiDiagnostic(diagnostic);
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
resetRetryState() {
|
|
161
|
+
resetPiRetryState();
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
createCanonicalPiResourceBundle,
|
|
6
|
+
type ExtractedPiResourceFile,
|
|
7
|
+
type PiResourceBundle,
|
|
8
|
+
type PiResourceFile,
|
|
9
|
+
sha256Digest,
|
|
10
|
+
validateResourceRelativePath,
|
|
11
|
+
verifyCanonicalPiResourceBundle,
|
|
12
|
+
} from "@leitwerk-dev/worker-protocol";
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
createCanonicalPiResourceBundle,
|
|
16
|
+
type ExtractedPiResourceFile,
|
|
17
|
+
type PiResourceBundle,
|
|
18
|
+
type PiResourceFile,
|
|
19
|
+
sha256Digest,
|
|
20
|
+
validateResourceRelativePath,
|
|
21
|
+
verifyCanonicalPiResourceBundle,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function fail(message: string): never {
|
|
25
|
+
throw new Error(`Invalid Pi resource bundle: ${message}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function writeExtractedFiles(
|
|
29
|
+
root: string,
|
|
30
|
+
files: readonly ExtractedPiResourceFile[],
|
|
31
|
+
): Promise<void> {
|
|
32
|
+
for (const file of files) {
|
|
33
|
+
const output = path.resolve(root, file.path);
|
|
34
|
+
if (
|
|
35
|
+
path.relative(root, output).startsWith("..") ||
|
|
36
|
+
path.isAbsolute(path.relative(root, output))
|
|
37
|
+
) {
|
|
38
|
+
fail(`path escapes materialization root: ${file.path}`);
|
|
39
|
+
}
|
|
40
|
+
await mkdir(path.dirname(output), { recursive: true, mode: 0o700 });
|
|
41
|
+
await writeFile(output, file.content, { mode: 0o644, flag: "wx" });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Extracts a verified bundle into an empty staging directory, then atomically publishes it. */
|
|
46
|
+
export async function materializeCanonicalPiResourceBundle(input: {
|
|
47
|
+
bundle: Uint8Array;
|
|
48
|
+
digest: string;
|
|
49
|
+
targetDir: string;
|
|
50
|
+
}): Promise<{ targetDir: string; reused: boolean }> {
|
|
51
|
+
const files = verifyCanonicalPiResourceBundle(input.bundle, input.digest);
|
|
52
|
+
const targetDir = path.resolve(input.targetDir);
|
|
53
|
+
const parent = path.dirname(targetDir);
|
|
54
|
+
await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
55
|
+
const marker = path.join(targetDir, ".leitwerk-resource-digest");
|
|
56
|
+
try {
|
|
57
|
+
const current = await import("node:fs/promises").then(({ readFile }) =>
|
|
58
|
+
readFile(marker, "utf8"),
|
|
59
|
+
);
|
|
60
|
+
if (current === `${input.digest}\n`) return { targetDir, reused: true };
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Managed Pi directory already exists with a different resource digest: ${targetDir}`,
|
|
63
|
+
);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
66
|
+
}
|
|
67
|
+
const staging = path.join(
|
|
68
|
+
parent,
|
|
69
|
+
`.${path.basename(targetDir)}.staging-${process.pid}-${randomUUID()}`,
|
|
70
|
+
);
|
|
71
|
+
try {
|
|
72
|
+
await mkdir(staging, { mode: 0o700 });
|
|
73
|
+
await writeExtractedFiles(staging, files);
|
|
74
|
+
await writeFile(path.join(staging, ".leitwerk-resource-digest"), `${input.digest}\n`, {
|
|
75
|
+
mode: 0o600,
|
|
76
|
+
flag: "wx",
|
|
77
|
+
});
|
|
78
|
+
await rename(staging, targetDir);
|
|
79
|
+
return { targetDir, reused: false };
|
|
80
|
+
} catch (error) {
|
|
81
|
+
await rm(staging, { recursive: true, force: true });
|
|
82
|
+
if ((error as NodeJS.ErrnoException).code === "EEXIST") {
|
|
83
|
+
const current = await import("node:fs/promises").then(({ readFile }) =>
|
|
84
|
+
readFile(marker, "utf8"),
|
|
85
|
+
);
|
|
86
|
+
if (current === `${input.digest}\n`) return { targetDir, reused: true };
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ProcessSemanticEntryRefKey,
|
|
3
|
+
type ProcessTurnRecordPathType,
|
|
4
|
+
type ProcessTurnStartSelection,
|
|
5
|
+
resolveProcessProductRefEntryId,
|
|
6
|
+
resolveProcessSemanticRefEntryId,
|
|
7
|
+
} from "@leitwerk-dev/domain";
|
|
8
|
+
import { ensureIdentifiedPrompt } from "./identified-pi-entry.js";
|
|
9
|
+
import type { InputItem } from "./input-consumer.js";
|
|
10
|
+
import type { PiTreeHandle } from "./pi-adapter.js";
|
|
11
|
+
import { resolveRootEntryIdFromHandle, type TurnTreePlan } from "./turn-tree-strategy.js";
|
|
12
|
+
import type { readProcessProductRefs, readProcessSemanticEntryRefs } from "./worker-payloads.js";
|
|
13
|
+
|
|
14
|
+
export type PreTurnTargetedInput = Pick<
|
|
15
|
+
InputItem,
|
|
16
|
+
"inputId" | "sequence" | "source" | "bodyMarkdown"
|
|
17
|
+
> & {
|
|
18
|
+
target: NonNullable<InputItem["target"]>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function resolveTargetSemanticRef(
|
|
22
|
+
target: PreTurnTargetedInput["target"],
|
|
23
|
+
): ProcessSemanticEntryRefKey | null {
|
|
24
|
+
return typeof target.semanticRef === "string" ? target.semanticRef : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function resolveTargetProductName(target: PreTurnTargetedInput["target"]): string | null {
|
|
28
|
+
return typeof target.productName === "string" ? target.productName : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function formatTarget(target: PreTurnTargetedInput["target"]): string {
|
|
32
|
+
const semanticRef = resolveTargetSemanticRef(target);
|
|
33
|
+
if (semanticRef) {
|
|
34
|
+
return `semantic ref '${semanticRef}'`;
|
|
35
|
+
}
|
|
36
|
+
return `product '${resolveTargetProductName(target) ?? "<unset>"}'`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function resolvePreTurnTargetStartSelection(
|
|
40
|
+
inputs: readonly PreTurnTargetedInput[],
|
|
41
|
+
): ProcessTurnStartSelection | null {
|
|
42
|
+
const target = inputs[0]?.target;
|
|
43
|
+
if (target && resolveTargetSemanticRef(target) === "currentPrimaryPathLeaf") {
|
|
44
|
+
return { kind: "semantic_ref", ref: "currentPrimaryPathLeaf" };
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function resolvePreTurnTargetEntryId(input: {
|
|
50
|
+
target: PreTurnTargetedInput["target"];
|
|
51
|
+
semanticEntryRefs: ReturnType<typeof readProcessSemanticEntryRefs>;
|
|
52
|
+
productRefs: ReturnType<typeof readProcessProductRefs>;
|
|
53
|
+
currentLeafId: string | null;
|
|
54
|
+
rootEntryId: string | null;
|
|
55
|
+
entryExists: (entryId: string) => boolean;
|
|
56
|
+
}): string | null {
|
|
57
|
+
const semanticRef = resolveTargetSemanticRef(input.target);
|
|
58
|
+
if (semanticRef) {
|
|
59
|
+
return resolveProcessSemanticRefEntryId({
|
|
60
|
+
ref: semanticRef,
|
|
61
|
+
semanticEntryRefs: input.semanticEntryRefs,
|
|
62
|
+
currentLeafId: input.currentLeafId,
|
|
63
|
+
rootEntryId: input.rootEntryId,
|
|
64
|
+
entryExists: input.entryExists,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const productName = resolveTargetProductName(input.target);
|
|
68
|
+
return productName
|
|
69
|
+
? resolveProcessProductRefEntryId({
|
|
70
|
+
productName,
|
|
71
|
+
productRefs: input.productRefs,
|
|
72
|
+
entryExists: input.entryExists,
|
|
73
|
+
})
|
|
74
|
+
: null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function validatePreTurnTargetedInputs(input: {
|
|
78
|
+
turnId: string;
|
|
79
|
+
inputs: readonly PreTurnTargetedInput[];
|
|
80
|
+
treePlan: TurnTreePlan;
|
|
81
|
+
semanticEntryRefs: ReturnType<typeof readProcessSemanticEntryRefs>;
|
|
82
|
+
productRefs: ReturnType<typeof readProcessProductRefs>;
|
|
83
|
+
currentLeafId: string | null;
|
|
84
|
+
rootEntryId: string | null;
|
|
85
|
+
entryExists: (entryId: string) => boolean;
|
|
86
|
+
}): string | null {
|
|
87
|
+
const expectedEntryId =
|
|
88
|
+
input.treePlan.startTarget.kind === "entry"
|
|
89
|
+
? input.treePlan.startTarget.entryId
|
|
90
|
+
: input.treePlan.startTarget.kind === "current_leaf"
|
|
91
|
+
? input.currentLeafId
|
|
92
|
+
: input.rootEntryId;
|
|
93
|
+
|
|
94
|
+
for (const targetedInput of input.inputs) {
|
|
95
|
+
if (targetedInput.bodyMarkdown.trim() === "") {
|
|
96
|
+
return `Turn '${input.turnId}' cannot apply queued targeted input '${targetedInput.inputId}' because bodyMarkdown is empty`;
|
|
97
|
+
}
|
|
98
|
+
const targetEntryId = resolvePreTurnTargetEntryId({
|
|
99
|
+
target: targetedInput.target,
|
|
100
|
+
semanticEntryRefs: input.semanticEntryRefs,
|
|
101
|
+
productRefs: input.productRefs,
|
|
102
|
+
currentLeafId: input.currentLeafId,
|
|
103
|
+
rootEntryId: input.rootEntryId,
|
|
104
|
+
entryExists: input.entryExists,
|
|
105
|
+
});
|
|
106
|
+
if (!targetEntryId) {
|
|
107
|
+
if (
|
|
108
|
+
resolveTargetSemanticRef(targetedInput.target) === "rootEntry" &&
|
|
109
|
+
input.treePlan.startTarget.kind === "root"
|
|
110
|
+
) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
return `Turn '${input.turnId}' cannot apply queued targeted input '${targetedInput.inputId}' because ${formatTarget(targetedInput.target)} is not resolved`;
|
|
114
|
+
}
|
|
115
|
+
if (!expectedEntryId || expectedEntryId !== targetEntryId) {
|
|
116
|
+
return `Turn '${input.turnId}' cannot apply queued targeted input '${targetedInput.inputId}' for ${formatTarget(targetedInput.target)} because the selected turn is not continuing from that branch`;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function applyPreTurnTargetedInputs(input: {
|
|
124
|
+
piHandle: PiTreeHandle;
|
|
125
|
+
inputs: readonly PreTurnTargetedInput[];
|
|
126
|
+
pathType: ProcessTurnRecordPathType;
|
|
127
|
+
/** Active-start recovery validates acknowledged inputs on the retained branch. */
|
|
128
|
+
recoveryBaseEntryId?: string | null;
|
|
129
|
+
reuseExistingWithoutBranching?: boolean;
|
|
130
|
+
requireExisting?: boolean;
|
|
131
|
+
onApplied?(
|
|
132
|
+
inputId: string,
|
|
133
|
+
sequence: number,
|
|
134
|
+
meta?: {
|
|
135
|
+
currentPrimaryPathLeafId?: string | null;
|
|
136
|
+
rootEntryId?: string | null;
|
|
137
|
+
targetSemanticRef?: ProcessSemanticEntryRefKey | null;
|
|
138
|
+
targetProductName?: string | null;
|
|
139
|
+
targetEntryId?: string | null;
|
|
140
|
+
},
|
|
141
|
+
): void;
|
|
142
|
+
}): Promise<void> {
|
|
143
|
+
if (input.inputs.length === 0) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
for (const targetedInput of input.inputs) {
|
|
147
|
+
const targetEntryId = await ensureIdentifiedPrompt({
|
|
148
|
+
piHandle: input.piHandle,
|
|
149
|
+
identity: { kind: "process_input", processInputId: targetedInput.inputId },
|
|
150
|
+
content: targetedInput.bodyMarkdown,
|
|
151
|
+
expectedParentId: input.recoveryBaseEntryId ?? input.piHandle.getLeafId(),
|
|
152
|
+
allowDescendantParent: input.recoveryBaseEntryId !== undefined,
|
|
153
|
+
requireOnCurrentBranch: input.reuseExistingWithoutBranching,
|
|
154
|
+
reuseWithoutBranching: input.reuseExistingWithoutBranching,
|
|
155
|
+
requireExisting: input.requireExisting,
|
|
156
|
+
});
|
|
157
|
+
const semanticRef = resolveTargetSemanticRef(targetedInput.target);
|
|
158
|
+
input.onApplied?.(targetedInput.inputId, targetedInput.sequence, {
|
|
159
|
+
...(input.pathType === "primary" ? { currentPrimaryPathLeafId: targetEntryId } : {}),
|
|
160
|
+
rootEntryId: resolveRootEntryIdFromHandle(input.piHandle),
|
|
161
|
+
...(semanticRef
|
|
162
|
+
? { targetSemanticRef: semanticRef }
|
|
163
|
+
: { targetProductName: resolveTargetProductName(targetedInput.target) }),
|
|
164
|
+
targetEntryId,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|