@leitwerk-dev/worker 0.3.0 → 0.4.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/{chunk-4WNVRNE5.js → chunk-KJCBXN5F.js} +1856 -1394
- package/dist/chunk-KJCBXN5F.js.map +1 -0
- package/dist/container-entry-main.js +160 -3
- package/dist/container-entry-main.js.map +1 -1
- package/dist/container-entry.d.ts +1 -0
- package/dist/container-entry.d.ts.map +1 -1
- package/dist/development-tool-environment.d.ts.map +1 -1
- package/dist/diagnostics.d.ts +5 -1
- package/dist/diagnostics.d.ts.map +1 -1
- package/dist/docker-registry-credentials.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/ipc.d.ts.map +1 -1
- package/dist/logical-prompt-runner.d.ts.map +1 -1
- package/dist/missing-tool-recovery-runner.d.ts.map +1 -1
- package/dist/pi-adapter.d.ts +5 -0
- package/dist/pi-adapter.d.ts.map +1 -1
- package/dist/pi-event-reporter.d.ts +2 -0
- package/dist/pi-event-reporter.d.ts.map +1 -1
- package/dist/pi-inspection.d.ts +5 -0
- package/dist/pi-inspection.d.ts.map +1 -0
- package/dist/pi-resource-bundle.d.ts +8 -1
- package/dist/pi-resource-bundle.d.ts.map +1 -1
- package/dist/runtime/bootstrap-session.d.ts +5 -1
- package/dist/runtime/bootstrap-session.d.ts.map +1 -1
- package/dist/runtime/bootstrap.d.ts.map +1 -1
- package/dist/runtime/failure-policy.d.ts.map +1 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/llm-turn-execution.d.ts +2 -0
- package/dist/runtime/llm-turn-execution.d.ts.map +1 -1
- package/dist/runtime/turn-execution.d.ts.map +1 -1
- package/dist/session-snapshot-exchange.d.ts.map +1 -1
- package/dist/strict-outcome-schema.d.ts +11 -0
- package/dist/strict-outcome-schema.d.ts.map +1 -0
- package/dist/test-helpers/llm-turn.d.ts +4 -0
- package/dist/test-helpers/llm-turn.d.ts.map +1 -0
- package/dist/test-helpers/pi-provider.d.ts +13 -0
- package/dist/test-helpers/pi-provider.d.ts.map +1 -0
- package/dist/transport-error.d.ts +16 -0
- package/dist/transport-error.d.ts.map +1 -0
- package/dist/turn-outcome-tool-session.d.ts +6 -1
- package/dist/turn-outcome-tool-session.d.ts.map +1 -1
- package/dist/turn-result-markdown.d.ts +5 -0
- package/dist/turn-result-markdown.d.ts.map +1 -1
- package/dist/virtual-machine-docker.d.ts +10 -0
- package/dist/virtual-machine-docker.d.ts.map +1 -0
- package/dist/worker-entry.js +1 -1
- package/dist/workspace/checkout-tool.d.ts +6 -0
- package/dist/workspace/checkout-tool.d.ts.map +1 -0
- package/dist/workspace/manifest.d.ts +2 -0
- package/dist/workspace/manifest.d.ts.map +1 -1
- package/dist/workspace/node-run-root-git-ops.d.ts +1 -0
- package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -1
- package/dist/workspace/run-root.d.ts +5 -3
- package/dist/workspace/run-root.d.ts.map +1 -1
- package/package.json +9 -9
- package/src/container-entry.ts +22 -0
- package/src/development-tool-environment.ts +2 -0
- package/src/diagnostics.ts +6 -1
- package/src/docker-registry-credentials.ts +8 -0
- package/src/index.ts +1 -0
- package/src/ipc.ts +4 -24
- package/src/logical-prompt-runner.ts +45 -41
- package/src/missing-tool-recovery-runner.ts +26 -18
- package/src/pi-adapter.ts +222 -337
- package/src/pi-event-reporter.ts +8 -0
- package/src/pi-inspection.ts +126 -0
- package/src/pi-resource-bundle.ts +12 -2
- package/src/runtime/bootstrap-session.ts +14 -1
- package/src/runtime/bootstrap.ts +16 -5
- package/src/runtime/failure-policy.ts +20 -2
- package/src/runtime/index.ts +65 -10
- package/src/runtime/llm-turn-execution.ts +28 -1
- package/src/runtime/turn-execution.ts +71 -2
- package/src/session-snapshot-exchange.ts +2 -0
- package/src/strict-outcome-schema.ts +84 -0
- package/src/test-helpers/llm-turn.ts +22 -0
- package/src/test-helpers/pi-provider.ts +126 -0
- package/src/transport-error.ts +92 -0
- package/src/turn-outcome-tool-session.ts +123 -26
- package/src/turn-result-markdown.ts +23 -6
- package/src/virtual-machine-docker.ts +191 -0
- package/src/workspace/checkout-tool.ts +68 -0
- package/src/workspace/manifest.ts +3 -0
- package/src/workspace/node-run-root-git-ops.ts +19 -0
- package/src/workspace/run-root.ts +62 -26
- package/dist/chunk-4WNVRNE5.js.map +0 -1
package/src/pi-adapter.ts
CHANGED
|
@@ -25,10 +25,16 @@ import type {
|
|
|
25
25
|
PiTurnExecutionResult,
|
|
26
26
|
PiUsageData,
|
|
27
27
|
} from "@leitwerk-dev/process-sdk";
|
|
28
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
compileOutcomeParameters,
|
|
30
|
+
hasErrorCode,
|
|
31
|
+
isEnoent,
|
|
32
|
+
type OutcomeToolParameterSpec,
|
|
33
|
+
} from "@leitwerk-dev/process-sdk";
|
|
29
34
|
import { resolvePiAgentDir } from "@leitwerk-dev/process-sdk/pi-config";
|
|
30
35
|
import type { ConfigSnapshot } from "@leitwerk-dev/protocol";
|
|
31
36
|
import { parseDurationMs } from "@leitwerk-dev/watcher-utils";
|
|
37
|
+
import type { WorkerMappedItem } from "@leitwerk-dev/worker-protocol";
|
|
32
38
|
import type { ManagedPiResourceManifest } from "./managed-pi-bootstrap.js";
|
|
33
39
|
import {
|
|
34
40
|
assertResultStayedOnExecutionBranch,
|
|
@@ -37,8 +43,10 @@ import {
|
|
|
37
43
|
type PiBranchOperation,
|
|
38
44
|
resolveExecutionAnchorEntryId,
|
|
39
45
|
} from "./pi-branch-guard.js";
|
|
46
|
+
import { installPiInspection } from "./pi-inspection.js";
|
|
40
47
|
import { createRepositoryBashTool } from "./repository-bash-tool.js";
|
|
41
48
|
import { buildLeitwerkResourceLoaderOptions } from "./resource-loader-factory.js";
|
|
49
|
+
import { installStrictOutcomeSchemas } from "./strict-outcome-schema.js";
|
|
42
50
|
|
|
43
51
|
const LEITWERK_TOOL_BOOTSTRAP_NAME = "__leitwerk_runtime_bootstrap__";
|
|
44
52
|
|
|
@@ -77,6 +85,8 @@ export interface PiTreeHandleOptions {
|
|
|
77
85
|
configSnapshot?: ConfigSnapshot;
|
|
78
86
|
/** @internal */
|
|
79
87
|
modelProfileId?: string | null;
|
|
88
|
+
/** Active mapped-turn item, for test doubles that script per-item behavior. @internal */
|
|
89
|
+
iteration?: WorkerMappedItem;
|
|
80
90
|
/** @internal */
|
|
81
91
|
piConfig?: {
|
|
82
92
|
/** @internal */
|
|
@@ -185,17 +195,9 @@ export async function inspectPiTreeForPlanning(
|
|
|
185
195
|
path.dirname(input.treeFile),
|
|
186
196
|
input.sessionCwd,
|
|
187
197
|
);
|
|
188
|
-
const entries: Array<Pick<PiTreeEntry, "id" | "parentId">> = [];
|
|
189
|
-
const visit = (nodes: readonly PiTreeNode[]) => {
|
|
190
|
-
for (const node of nodes) {
|
|
191
|
-
entries.push({ id: node.entry.id, parentId: node.entry.parentId });
|
|
192
|
-
visit(node.children);
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
visit(manager.getTree() as unknown as readonly PiTreeNode[]);
|
|
196
198
|
return {
|
|
197
199
|
currentLeafId: manager.getLeafId() ?? null,
|
|
198
|
-
entries,
|
|
200
|
+
entries: manager.getEntries().map(({ id, parentId }) => ({ id, parentId })),
|
|
199
201
|
};
|
|
200
202
|
}
|
|
201
203
|
|
|
@@ -230,12 +232,15 @@ interface PiBeforeToolCallContext {
|
|
|
230
232
|
toolCall: { name: string };
|
|
231
233
|
}
|
|
232
234
|
|
|
235
|
+
type PiTurnDecision = { action: "end" | "continue" } | undefined;
|
|
236
|
+
|
|
233
237
|
interface PiAgentLoopConfig {
|
|
234
|
-
|
|
238
|
+
finishTurn?: (...args: unknown[]) => PiTurnDecision | Promise<PiTurnDecision>;
|
|
235
239
|
[key: string]: unknown;
|
|
236
240
|
}
|
|
237
241
|
|
|
238
242
|
interface PiCompletedTurnContext {
|
|
243
|
+
toolResults?: unknown[];
|
|
239
244
|
message?: {
|
|
240
245
|
content?: unknown;
|
|
241
246
|
stopReason?: unknown;
|
|
@@ -270,33 +275,10 @@ function toToolSchema(parameters: Record<string, unknown>): Record<string, unkno
|
|
|
270
275
|
return parameters;
|
|
271
276
|
}
|
|
272
277
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const {
|
|
278
|
-
required: isRequired,
|
|
279
|
-
requiredErrorCode: _requiredErrorCode,
|
|
280
|
-
invalidErrorCode: _invalidErrorCode,
|
|
281
|
-
minItemsErrorCode: _minItemsErrorCode,
|
|
282
|
-
minimumErrorCode: _minimumErrorCode,
|
|
283
|
-
...jsonSchemaSpec
|
|
284
|
-
} = spec;
|
|
285
|
-
properties[key] = { ...jsonSchemaSpec };
|
|
286
|
-
if (isRequired === true) {
|
|
287
|
-
required.push(key);
|
|
288
|
-
}
|
|
289
|
-
} else {
|
|
290
|
-
properties[key] = { type: "string" };
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return {
|
|
295
|
-
type: "object",
|
|
296
|
-
properties,
|
|
297
|
-
required,
|
|
298
|
-
additionalProperties: false,
|
|
299
|
-
};
|
|
278
|
+
return compileOutcomeParameters(parameters as Record<string, OutcomeToolParameterSpec>) as Record<
|
|
279
|
+
string,
|
|
280
|
+
unknown
|
|
281
|
+
>;
|
|
300
282
|
}
|
|
301
283
|
|
|
302
284
|
const LEITWERK_TOOL_BOOTSTRAP: ToolDefinition = {
|
|
@@ -395,18 +377,20 @@ function validateRequestedActiveTools(
|
|
|
395
377
|
}
|
|
396
378
|
}
|
|
397
379
|
|
|
398
|
-
function toPiRunDetails(
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}): PiRunDetails {
|
|
380
|
+
function toPiRunDetails(
|
|
381
|
+
{ resourceLoader }: AgentSessionServices,
|
|
382
|
+
availableToolNames: readonly string[],
|
|
383
|
+
): PiRunDetails {
|
|
403
384
|
return {
|
|
404
|
-
loadedAgentsFiles:
|
|
405
|
-
path
|
|
406
|
-
sizeBytes: Buffer.byteLength(
|
|
385
|
+
loadedAgentsFiles: resourceLoader.getAgentsFiles().agentsFiles.map(({ path, content }) => ({
|
|
386
|
+
path,
|
|
387
|
+
sizeBytes: Buffer.byteLength(content, "utf8"),
|
|
388
|
+
})),
|
|
389
|
+
loadedSkills: resourceLoader.getSkills().skills.map(({ name, filePath }) => ({
|
|
390
|
+
name,
|
|
391
|
+
path: filePath,
|
|
407
392
|
})),
|
|
408
|
-
|
|
409
|
-
availableToolNames: [...input.availableToolNames],
|
|
393
|
+
availableToolNames: [...availableToolNames],
|
|
410
394
|
};
|
|
411
395
|
}
|
|
412
396
|
|
|
@@ -586,18 +570,18 @@ class TerminalAcknowledgementRuntime {
|
|
|
586
570
|
this.clearTimeout();
|
|
587
571
|
}
|
|
588
572
|
|
|
589
|
-
onTurnCompleted(args: readonly unknown[]):
|
|
573
|
+
onTurnCompleted(args: readonly unknown[]): PiTurnDecision {
|
|
590
574
|
const state = this.control.state();
|
|
591
575
|
if (!this.acknowledgementTurnStarted) {
|
|
592
|
-
if (state !== "outcome_accepted") return
|
|
576
|
+
if (state !== "outcome_accepted") return undefined;
|
|
593
577
|
this.acknowledgementTurnStarted = true;
|
|
594
|
-
return
|
|
578
|
+
return { action: "continue" };
|
|
595
579
|
}
|
|
596
580
|
if (state === "outcome_accepted") {
|
|
597
581
|
const context = isObjectLike(args[0]) ? (args[0] as PiCompletedTurnContext) : null;
|
|
598
582
|
this.settleFromMessage(context?.message);
|
|
599
583
|
}
|
|
600
|
-
return
|
|
584
|
+
return { action: "end" };
|
|
601
585
|
}
|
|
602
586
|
|
|
603
587
|
async run(run: () => Promise<void>): Promise<void> {
|
|
@@ -879,18 +863,22 @@ function summarizeSessionGlobalDiagnosticDetails(
|
|
|
879
863
|
return keys.length > 0 ? { ...base, keys } : base;
|
|
880
864
|
}
|
|
881
865
|
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
866
|
+
const SESSION_GLOBAL_EVENT_MESSAGES = new Map([
|
|
867
|
+
["session_info_changed", "Pi session info changed"],
|
|
868
|
+
["agent_start", "Pi agent run started"],
|
|
869
|
+
["agent_end", "Pi agent run ended"],
|
|
870
|
+
["agent_settled", "Pi agent run settled"],
|
|
871
|
+
["queue_update", "Pi session queue updated"],
|
|
872
|
+
]);
|
|
873
|
+
|
|
874
|
+
const TOOL_CALL_EVENT_TYPES = {
|
|
875
|
+
toolcall_start: "toolcall.start",
|
|
876
|
+
toolcall_delta: "toolcall.delta",
|
|
877
|
+
toolcall_end: "toolcall.end",
|
|
878
|
+
} as const;
|
|
879
|
+
|
|
880
|
+
function singlePiEvent(event: PiEvent): AgentSessionEventTranslation {
|
|
881
|
+
return { piEvents: [event], diagnostics: [] };
|
|
894
882
|
}
|
|
895
883
|
|
|
896
884
|
function buildUnknownSessionEventDiagnostic(
|
|
@@ -953,16 +941,18 @@ export function translateAgentSessionEventEnvelope(
|
|
|
953
941
|
turnSequence: number;
|
|
954
942
|
},
|
|
955
943
|
): AgentSessionEventTranslation {
|
|
956
|
-
const
|
|
957
|
-
if (
|
|
944
|
+
const globalMessage = SESSION_GLOBAL_EVENT_MESSAGES.get(event.type);
|
|
945
|
+
if (globalMessage) {
|
|
958
946
|
return {
|
|
959
947
|
piEvents: [],
|
|
960
948
|
diagnostics: [
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
949
|
+
buildSessionDiagnostic({
|
|
950
|
+
level: "info",
|
|
951
|
+
code: `pi.${event.type}`,
|
|
952
|
+
message: globalMessage,
|
|
953
|
+
timestamp: readSessionEventTimestamp(event),
|
|
954
|
+
details: summarizeSessionGlobalDiagnosticDetails(event),
|
|
955
|
+
}),
|
|
966
956
|
],
|
|
967
957
|
};
|
|
968
958
|
}
|
|
@@ -976,32 +966,22 @@ export function translateAgentSessionEventEnvelope(
|
|
|
976
966
|
state.turnSequence = turnIndex;
|
|
977
967
|
const turnId = `turn-${turnIndex}`;
|
|
978
968
|
state.currentTurnId = turnId;
|
|
979
|
-
return {
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
timestamp: toIsoTimestamp((event as { timestamp?: unknown }).timestamp),
|
|
986
|
-
},
|
|
987
|
-
],
|
|
988
|
-
diagnostics: [],
|
|
989
|
-
};
|
|
969
|
+
return singlePiEvent({
|
|
970
|
+
type: "turn.start",
|
|
971
|
+
turnId,
|
|
972
|
+
data: {},
|
|
973
|
+
timestamp: toIsoTimestamp((event as { timestamp?: unknown }).timestamp),
|
|
974
|
+
});
|
|
990
975
|
}
|
|
991
976
|
case "turn_end": {
|
|
992
977
|
const turnId = state.currentTurnId ?? `turn-${state.turnSequence}`;
|
|
993
978
|
state.currentTurnId = null;
|
|
994
|
-
return {
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
timestamp: toIsoTimestamp((event.message as { timestamp?: unknown }).timestamp),
|
|
1001
|
-
},
|
|
1002
|
-
],
|
|
1003
|
-
diagnostics: [],
|
|
1004
|
-
};
|
|
979
|
+
return singlePiEvent({
|
|
980
|
+
type: "turn.end",
|
|
981
|
+
turnId,
|
|
982
|
+
data: {},
|
|
983
|
+
timestamp: toIsoTimestamp((event.message as { timestamp?: unknown }).timestamp),
|
|
984
|
+
});
|
|
1005
985
|
}
|
|
1006
986
|
case "message_start":
|
|
1007
987
|
return { piEvents: [], diagnostics: [] };
|
|
@@ -1017,71 +997,25 @@ export function translateAgentSessionEventEnvelope(
|
|
|
1017
997
|
const timestamp = toIsoTimestamp((event.message as { timestamp?: unknown }).timestamp);
|
|
1018
998
|
switch (event.assistantMessageEvent.type) {
|
|
1019
999
|
case "text_delta":
|
|
1020
|
-
return {
|
|
1021
|
-
piEvents: [
|
|
1022
|
-
{
|
|
1023
|
-
type: "stream.delta",
|
|
1024
|
-
turnId,
|
|
1025
|
-
data: {
|
|
1026
|
-
text: event.assistantMessageEvent.delta,
|
|
1027
|
-
streamType: "text",
|
|
1028
|
-
},
|
|
1029
|
-
timestamp,
|
|
1030
|
-
},
|
|
1031
|
-
],
|
|
1032
|
-
diagnostics: [],
|
|
1033
|
-
};
|
|
1034
1000
|
case "thinking_delta":
|
|
1035
|
-
return {
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
timestamp,
|
|
1045
|
-
},
|
|
1046
|
-
],
|
|
1047
|
-
diagnostics: [],
|
|
1048
|
-
};
|
|
1001
|
+
return singlePiEvent({
|
|
1002
|
+
type: "stream.delta",
|
|
1003
|
+
turnId,
|
|
1004
|
+
data: {
|
|
1005
|
+
text: event.assistantMessageEvent.delta,
|
|
1006
|
+
streamType: event.assistantMessageEvent.type === "text_delta" ? "text" : "thinking",
|
|
1007
|
+
},
|
|
1008
|
+
timestamp,
|
|
1009
|
+
});
|
|
1049
1010
|
case "toolcall_start":
|
|
1050
|
-
return {
|
|
1051
|
-
piEvents: [
|
|
1052
|
-
{
|
|
1053
|
-
type: "toolcall.start",
|
|
1054
|
-
turnId,
|
|
1055
|
-
data: normalizePiEventData(assistantMessageEvent),
|
|
1056
|
-
timestamp,
|
|
1057
|
-
},
|
|
1058
|
-
],
|
|
1059
|
-
diagnostics: [],
|
|
1060
|
-
};
|
|
1061
1011
|
case "toolcall_delta":
|
|
1062
|
-
return {
|
|
1063
|
-
piEvents: [
|
|
1064
|
-
{
|
|
1065
|
-
type: "toolcall.delta",
|
|
1066
|
-
turnId,
|
|
1067
|
-
data: normalizePiEventData(assistantMessageEvent),
|
|
1068
|
-
timestamp,
|
|
1069
|
-
},
|
|
1070
|
-
],
|
|
1071
|
-
diagnostics: [],
|
|
1072
|
-
};
|
|
1073
1012
|
case "toolcall_end":
|
|
1074
|
-
return {
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
timestamp,
|
|
1081
|
-
},
|
|
1082
|
-
],
|
|
1083
|
-
diagnostics: [],
|
|
1084
|
-
};
|
|
1013
|
+
return singlePiEvent({
|
|
1014
|
+
type: TOOL_CALL_EVENT_TYPES[event.assistantMessageEvent.type],
|
|
1015
|
+
turnId,
|
|
1016
|
+
data: normalizePiEventData(assistantMessageEvent),
|
|
1017
|
+
timestamp,
|
|
1018
|
+
});
|
|
1085
1019
|
case "start":
|
|
1086
1020
|
case "done":
|
|
1087
1021
|
case "error":
|
|
@@ -1163,79 +1097,59 @@ export function translateAgentSessionEventEnvelope(
|
|
|
1163
1097
|
return { piEvents, diagnostics: [] };
|
|
1164
1098
|
}
|
|
1165
1099
|
case "auto_retry_start":
|
|
1166
|
-
return {
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
timestamp: new Date().toISOString(),
|
|
1184
|
-
},
|
|
1185
|
-
],
|
|
1186
|
-
diagnostics: [],
|
|
1187
|
-
};
|
|
1100
|
+
return singlePiEvent({
|
|
1101
|
+
type: "retry.start",
|
|
1102
|
+
turnId: activeTurnId(state),
|
|
1103
|
+
data: {
|
|
1104
|
+
attempt: event.attempt,
|
|
1105
|
+
maxAttempts: event.maxAttempts,
|
|
1106
|
+
delayMs: event.delayMs,
|
|
1107
|
+
errorMessage: event.errorMessage,
|
|
1108
|
+
message: describeRetryStart({
|
|
1109
|
+
attempt: event.attempt,
|
|
1110
|
+
maxAttempts: event.maxAttempts,
|
|
1111
|
+
delayMs: event.delayMs,
|
|
1112
|
+
errorMessage: event.errorMessage,
|
|
1113
|
+
}),
|
|
1114
|
+
},
|
|
1115
|
+
timestamp: new Date().toISOString(),
|
|
1116
|
+
});
|
|
1188
1117
|
case "auto_retry_end": {
|
|
1189
1118
|
const finalError = readNonEmptyString(event.finalError);
|
|
1190
|
-
return {
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
timestamp: new Date().toISOString(),
|
|
1206
|
-
},
|
|
1207
|
-
],
|
|
1208
|
-
diagnostics: [],
|
|
1209
|
-
};
|
|
1119
|
+
return singlePiEvent({
|
|
1120
|
+
type: "retry.end",
|
|
1121
|
+
turnId: activeTurnId(state),
|
|
1122
|
+
data: {
|
|
1123
|
+
success: event.success,
|
|
1124
|
+
attempt: event.attempt,
|
|
1125
|
+
...(finalError ? { finalError } : {}),
|
|
1126
|
+
message: describeRetryEnd({
|
|
1127
|
+
success: event.success,
|
|
1128
|
+
attempt: event.attempt,
|
|
1129
|
+
finalError,
|
|
1130
|
+
}),
|
|
1131
|
+
},
|
|
1132
|
+
timestamp: new Date().toISOString(),
|
|
1133
|
+
});
|
|
1210
1134
|
}
|
|
1211
1135
|
case "tool_execution_start":
|
|
1212
|
-
return {
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
timestamp: new Date().toISOString(),
|
|
1223
|
-
},
|
|
1224
|
-
],
|
|
1225
|
-
diagnostics: [],
|
|
1226
|
-
};
|
|
1136
|
+
return singlePiEvent({
|
|
1137
|
+
type: "tool.call",
|
|
1138
|
+
turnId: state.currentTurnId ?? `turn-tool-${event.toolCallId}`,
|
|
1139
|
+
data: {
|
|
1140
|
+
toolCallId: event.toolCallId,
|
|
1141
|
+
name: event.toolName,
|
|
1142
|
+
arguments: event.args,
|
|
1143
|
+
},
|
|
1144
|
+
timestamp: new Date().toISOString(),
|
|
1145
|
+
});
|
|
1227
1146
|
case "tool_execution_update":
|
|
1228
|
-
return {
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
timestamp: readSessionEventTimestamp(event),
|
|
1235
|
-
},
|
|
1236
|
-
],
|
|
1237
|
-
diagnostics: [],
|
|
1238
|
-
};
|
|
1147
|
+
return singlePiEvent({
|
|
1148
|
+
type: "tool.update",
|
|
1149
|
+
turnId: state.currentTurnId ?? `turn-tool-${event.toolCallId}`,
|
|
1150
|
+
data: normalizePiEventData(event as unknown as Record<string, unknown>),
|
|
1151
|
+
timestamp: readSessionEventTimestamp(event),
|
|
1152
|
+
});
|
|
1239
1153
|
case "tool_execution_end": {
|
|
1240
1154
|
const turnId = state.currentTurnId ?? `turn-tool-${event.toolCallId}`;
|
|
1241
1155
|
const timestamp = new Date().toISOString();
|
|
@@ -1251,7 +1165,7 @@ export function translateAgentSessionEventEnvelope(
|
|
|
1251
1165
|
timestamp,
|
|
1252
1166
|
};
|
|
1253
1167
|
if (!event.isError) {
|
|
1254
|
-
return
|
|
1168
|
+
return singlePiEvent(toolResultEvent);
|
|
1255
1169
|
}
|
|
1256
1170
|
return {
|
|
1257
1171
|
piEvents: [
|
|
@@ -1274,57 +1188,13 @@ export function translateAgentSessionEventEnvelope(
|
|
|
1274
1188
|
};
|
|
1275
1189
|
}
|
|
1276
1190
|
case "compaction_start":
|
|
1277
|
-
return {
|
|
1278
|
-
piEvents: [
|
|
1279
|
-
{
|
|
1280
|
-
type: "compaction.start",
|
|
1281
|
-
turnId: activeTurnId(state),
|
|
1282
|
-
data: normalizePiEventData(event as unknown as Record<string, unknown>),
|
|
1283
|
-
timestamp: readSessionEventTimestamp(event),
|
|
1284
|
-
},
|
|
1285
|
-
],
|
|
1286
|
-
diagnostics: [],
|
|
1287
|
-
};
|
|
1288
1191
|
case "compaction_end":
|
|
1289
|
-
return {
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
timestamp: readSessionEventTimestamp(event),
|
|
1296
|
-
},
|
|
1297
|
-
],
|
|
1298
|
-
diagnostics: [],
|
|
1299
|
-
};
|
|
1300
|
-
case "agent_start":
|
|
1301
|
-
return {
|
|
1302
|
-
piEvents: [],
|
|
1303
|
-
diagnostics: [
|
|
1304
|
-
buildKnownSessionGlobalDiagnostic(event, "pi.agent_start", "Pi agent run started"),
|
|
1305
|
-
],
|
|
1306
|
-
};
|
|
1307
|
-
case "agent_end":
|
|
1308
|
-
return {
|
|
1309
|
-
piEvents: [],
|
|
1310
|
-
diagnostics: [
|
|
1311
|
-
buildKnownSessionGlobalDiagnostic(event, "pi.agent_end", "Pi agent run ended"),
|
|
1312
|
-
],
|
|
1313
|
-
};
|
|
1314
|
-
case "agent_settled":
|
|
1315
|
-
return {
|
|
1316
|
-
piEvents: [],
|
|
1317
|
-
diagnostics: [
|
|
1318
|
-
buildKnownSessionGlobalDiagnostic(event, "pi.agent_settled", "Pi agent run settled"),
|
|
1319
|
-
],
|
|
1320
|
-
};
|
|
1321
|
-
case "queue_update":
|
|
1322
|
-
return {
|
|
1323
|
-
piEvents: [],
|
|
1324
|
-
diagnostics: [
|
|
1325
|
-
buildKnownSessionGlobalDiagnostic(event, "pi.queue_update", "Pi session queue updated"),
|
|
1326
|
-
],
|
|
1327
|
-
};
|
|
1192
|
+
return singlePiEvent({
|
|
1193
|
+
type: event.type === "compaction_start" ? "compaction.start" : "compaction.end",
|
|
1194
|
+
turnId: activeTurnId(state),
|
|
1195
|
+
data: normalizePiEventData(event as unknown as Record<string, unknown>),
|
|
1196
|
+
timestamp: readSessionEventTimestamp(event),
|
|
1197
|
+
});
|
|
1328
1198
|
default:
|
|
1329
1199
|
return {
|
|
1330
1200
|
piEvents: [],
|
|
@@ -1349,6 +1219,7 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1349
1219
|
private continuationRetryAbortController: AbortController | null = null;
|
|
1350
1220
|
private readonly availableToolNames: string[];
|
|
1351
1221
|
private readonly runDetails: PiRunDetails;
|
|
1222
|
+
private readonly releaseInspection: () => void;
|
|
1352
1223
|
|
|
1353
1224
|
/** @internal */
|
|
1354
1225
|
constructor(
|
|
@@ -1373,6 +1244,17 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1373
1244
|
loadedSkills: options.runDetails.loadedSkills.map((skill) => ({ ...skill })),
|
|
1374
1245
|
availableToolNames: [...options.runDetails.availableToolNames],
|
|
1375
1246
|
};
|
|
1247
|
+
this.releaseInspection = installPiInspection(
|
|
1248
|
+
session,
|
|
1249
|
+
(capture) =>
|
|
1250
|
+
this.emitSubscribedEvent({
|
|
1251
|
+
type: "inspection",
|
|
1252
|
+
turnId: activeTurnId(this.eventState),
|
|
1253
|
+
timestamp: capture.timestamp,
|
|
1254
|
+
data: { capture },
|
|
1255
|
+
}),
|
|
1256
|
+
() => activeTurnId(this.eventState),
|
|
1257
|
+
);
|
|
1376
1258
|
}
|
|
1377
1259
|
|
|
1378
1260
|
/** @internal */
|
|
@@ -1482,8 +1364,10 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1482
1364
|
options.activeTools !== undefined ? [...options.activeTools] : [];
|
|
1483
1365
|
validateRequestedActiveTools(requestedActiveToolNames, this.availableToolNames);
|
|
1484
1366
|
const previousActiveToolNames = this.session.getActiveToolNames();
|
|
1485
|
-
const
|
|
1486
|
-
options.
|
|
1367
|
+
const needsFinishTurnHook = Boolean(
|
|
1368
|
+
options.terminalAcknowledgement || options.shouldStopAfterTurn,
|
|
1369
|
+
);
|
|
1370
|
+
const needsRuntimeHooks = options.shouldBlockToolCall !== undefined || needsFinishTurnHook;
|
|
1487
1371
|
if (
|
|
1488
1372
|
customTools.length === 0 &&
|
|
1489
1373
|
!needsRuntimeHooks &&
|
|
@@ -1505,8 +1389,7 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1505
1389
|
const previousCustomTools = [...mutableSession._customTools];
|
|
1506
1390
|
const previousBeforeToolCall = mutableAgent.beforeToolCall;
|
|
1507
1391
|
const previousCreateLoopConfig = mutableAgent.createLoopConfig;
|
|
1508
|
-
|
|
1509
|
-
if (options.terminalAcknowledgement && typeof loopConfigHook !== "function") {
|
|
1392
|
+
if (needsFinishTurnHook && typeof previousCreateLoopConfig !== "function") {
|
|
1510
1393
|
throw new Error("Pi SDK version incompatible: agent loop configuration hook unavailable");
|
|
1511
1394
|
}
|
|
1512
1395
|
const terminalAcknowledgement = options.terminalAcknowledgement
|
|
@@ -1537,28 +1420,31 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1537
1420
|
}
|
|
1538
1421
|
return await previousBeforeToolCall?.(context as never, signal);
|
|
1539
1422
|
};
|
|
1540
|
-
if (
|
|
1423
|
+
if (needsFinishTurnHook && typeof previousCreateLoopConfig === "function") {
|
|
1541
1424
|
mutableAgent.createLoopConfig = (loopOptions?: unknown) => {
|
|
1542
|
-
const baseConfig =
|
|
1543
|
-
const
|
|
1425
|
+
const baseConfig = previousCreateLoopConfig.call(mutableAgent, loopOptions);
|
|
1426
|
+
const previousFinishTurn = baseConfig.finishTurn;
|
|
1544
1427
|
return {
|
|
1545
1428
|
...baseConfig,
|
|
1546
|
-
|
|
1547
|
-
const
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
}
|
|
1555
|
-
return false;
|
|
1429
|
+
finishTurn: async (...args: unknown[]) => {
|
|
1430
|
+
const context = isObjectLike(args[0]) ? (args[0] as PiCompletedTurnContext) : null;
|
|
1431
|
+
if (options.shouldStopAfterTurn?.(context?.toolResults ?? []))
|
|
1432
|
+
return { action: "end" };
|
|
1433
|
+
return (
|
|
1434
|
+
terminalAcknowledgement?.onTurnCompleted(args) ??
|
|
1435
|
+
(await previousFinishTurn?.(...args))
|
|
1436
|
+
);
|
|
1556
1437
|
},
|
|
1557
1438
|
};
|
|
1558
1439
|
};
|
|
1559
1440
|
}
|
|
1560
1441
|
}
|
|
1561
1442
|
|
|
1443
|
+
const restoreStrictSchemas = installStrictOutcomeSchemas(
|
|
1444
|
+
this.session,
|
|
1445
|
+
customTools,
|
|
1446
|
+
(diagnostic) => this.emitSubscribedDiagnostic(diagnostic),
|
|
1447
|
+
);
|
|
1562
1448
|
try {
|
|
1563
1449
|
if (terminalAcknowledgement) {
|
|
1564
1450
|
await terminalAcknowledgement.run(run);
|
|
@@ -1566,6 +1452,7 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1566
1452
|
await run();
|
|
1567
1453
|
}
|
|
1568
1454
|
} finally {
|
|
1455
|
+
restoreStrictSchemas();
|
|
1569
1456
|
terminalAcknowledgement?.dispose();
|
|
1570
1457
|
mutableSession._customTools = previousCustomTools;
|
|
1571
1458
|
mutableAgent.beforeToolCall = previousBeforeToolCall;
|
|
@@ -1780,6 +1667,8 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1780
1667
|
});
|
|
1781
1668
|
}
|
|
1782
1669
|
branch.assertResult("continue", entryId, entryId);
|
|
1670
|
+
const timestamp = this.getEntry(entryId)?.timestamp ?? new Date().toISOString();
|
|
1671
|
+
this.emitEntryLink(entryId, timestamp, "user");
|
|
1783
1672
|
return entryId;
|
|
1784
1673
|
}
|
|
1785
1674
|
|
|
@@ -1821,7 +1710,33 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1821
1710
|
this.sessionUnsubscribe = this.session.subscribe((event) => this.emitSessionEvent(event));
|
|
1822
1711
|
}
|
|
1823
1712
|
|
|
1713
|
+
private emitEntryLink(entryId: string, timestamp: string, role: string): void {
|
|
1714
|
+
const turnId = activeTurnId(this.eventState);
|
|
1715
|
+
this.emitSubscribedEvent({
|
|
1716
|
+
type: "inspection",
|
|
1717
|
+
turnId,
|
|
1718
|
+
timestamp,
|
|
1719
|
+
data: {
|
|
1720
|
+
capture: {
|
|
1721
|
+
version: 1,
|
|
1722
|
+
id: `entry:${entryId}`,
|
|
1723
|
+
timestamp,
|
|
1724
|
+
fact: { kind: "entry_link", entryId, piTurnId: turnId, role },
|
|
1725
|
+
},
|
|
1726
|
+
},
|
|
1727
|
+
});
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1824
1730
|
private emitSessionEvent(event: AgentSessionEvent): void {
|
|
1731
|
+
if (event.type === "entry_appended") {
|
|
1732
|
+
const entry = event.entry;
|
|
1733
|
+
this.emitEntryLink(
|
|
1734
|
+
entry.id,
|
|
1735
|
+
entry.timestamp,
|
|
1736
|
+
entry.type === "message" ? entry.message.role : entry.type,
|
|
1737
|
+
);
|
|
1738
|
+
return;
|
|
1739
|
+
}
|
|
1825
1740
|
const translated = translateAgentSessionEventEnvelope(event, this.eventState);
|
|
1826
1741
|
for (const piEvent of translated.piEvents) this.emitSubscribedEvent(piEvent);
|
|
1827
1742
|
for (const diagnostic of translated.diagnostics) this.emitSubscribedDiagnostic(diagnostic);
|
|
@@ -1862,6 +1777,7 @@ export class SdkPiTreeHandle implements PiTreeHandle {
|
|
|
1862
1777
|
|
|
1863
1778
|
/** @internal */
|
|
1864
1779
|
async close(): Promise<void> {
|
|
1780
|
+
this.releaseInspection();
|
|
1865
1781
|
if (this.sessionUnsubscribe) {
|
|
1866
1782
|
this.sessionUnsubscribe();
|
|
1867
1783
|
this.sessionUnsubscribe = null;
|
|
@@ -1937,37 +1853,20 @@ function validateManagedLoadedResources(input: {
|
|
|
1937
1853
|
const skills = input.services.resourceLoader.getSkills().skills;
|
|
1938
1854
|
const prompts = input.services.resourceLoader.getPrompts().prompts;
|
|
1939
1855
|
const agentsFiles = input.services.resourceLoader.getAgentsFiles().agentsFiles;
|
|
1940
|
-
for (const
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
});
|
|
1955
|
-
}
|
|
1956
|
-
for (const prompt of prompts) {
|
|
1957
|
-
assertLoadedPathAllowed({
|
|
1958
|
-
path: prompt.filePath,
|
|
1959
|
-
agentDir: input.agentDir,
|
|
1960
|
-
workspaceRoot: input.workspaceRoot,
|
|
1961
|
-
kind: "prompt",
|
|
1962
|
-
});
|
|
1963
|
-
}
|
|
1964
|
-
for (const agentsFile of agentsFiles) {
|
|
1965
|
-
assertLoadedPathAllowed({
|
|
1966
|
-
path: agentsFile.path,
|
|
1967
|
-
agentDir: input.agentDir,
|
|
1968
|
-
workspaceRoot: input.workspaceRoot,
|
|
1969
|
-
kind: "context file",
|
|
1970
|
-
});
|
|
1856
|
+
for (const [kind, paths] of [
|
|
1857
|
+
["extension", extensions.map((extension) => extension.resolvedPath)],
|
|
1858
|
+
["skill", skills.map((skill) => skill.filePath)],
|
|
1859
|
+
["prompt", prompts.map((prompt) => prompt.filePath)],
|
|
1860
|
+
["context file", agentsFiles.map((file) => file.path)],
|
|
1861
|
+
] as const) {
|
|
1862
|
+
for (const loadedPath of paths) {
|
|
1863
|
+
assertLoadedPathAllowed({
|
|
1864
|
+
path: loadedPath,
|
|
1865
|
+
agentDir: input.agentDir,
|
|
1866
|
+
workspaceRoot: input.workspaceRoot,
|
|
1867
|
+
kind,
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1971
1870
|
}
|
|
1972
1871
|
|
|
1973
1872
|
const loadedResourceIds: string[] = [];
|
|
@@ -2171,14 +2070,7 @@ export class SdkPiTreeHandleFactory implements PiTreeHandleFactory {
|
|
|
2171
2070
|
agentDir: opts.agentDir,
|
|
2172
2071
|
workspaceRoot: opts.workspaceRoot,
|
|
2173
2072
|
});
|
|
2174
|
-
const runDetails = toPiRunDetails(
|
|
2175
|
-
loadedAgentsFiles: services.resourceLoader.getAgentsFiles().agentsFiles,
|
|
2176
|
-
loadedSkills: services.resourceLoader.getSkills().skills.map((skill) => ({
|
|
2177
|
-
name: skill.name,
|
|
2178
|
-
path: skill.filePath,
|
|
2179
|
-
})),
|
|
2180
|
-
availableToolNames: opts.piConfig?.availableToolNames ?? [],
|
|
2181
|
-
});
|
|
2073
|
+
const runDetails = toPiRunDetails(services, opts.piConfig?.availableToolNames ?? []);
|
|
2182
2074
|
this.preparedServices.set(managedServicesKey(opts.agentDir, opts.sessionCwd), services);
|
|
2183
2075
|
return {
|
|
2184
2076
|
loadedResourceIds,
|
|
@@ -2248,14 +2140,7 @@ export class SdkPiTreeHandleFactory implements PiTreeHandleFactory {
|
|
|
2248
2140
|
noTools: "builtin",
|
|
2249
2141
|
});
|
|
2250
2142
|
getMutableSession(session)._refreshToolRegistry({ activeToolNames: [] });
|
|
2251
|
-
const runDetails = toPiRunDetails(
|
|
2252
|
-
loadedAgentsFiles: services.resourceLoader.getAgentsFiles().agentsFiles,
|
|
2253
|
-
loadedSkills: services.resourceLoader.getSkills().skills.map((skill) => ({
|
|
2254
|
-
name: skill.name,
|
|
2255
|
-
path: skill.filePath,
|
|
2256
|
-
})),
|
|
2257
|
-
availableToolNames,
|
|
2258
|
-
});
|
|
2143
|
+
const runDetails = toPiRunDetails(services, availableToolNames);
|
|
2259
2144
|
return new SdkPiTreeHandle(session, {
|
|
2260
2145
|
treeFile: opts.treeFile,
|
|
2261
2146
|
isResumed: opts.resume,
|