@abelxiaoxing/cadence 2.0.47 → 2.0.48
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/docs/conversation-mode.md +43 -3
- package/package.json +2 -1
- package/src/cadence-instructions.ts +1 -1
- package/src/cadence-mode.ts +2 -2
- package/src/coordinator-tools.mjs +3 -1
- package/src/model-preview.mjs +41 -11
- package/src/native-build.ts +1 -1
- package/src/research-tools.mjs +28 -21
- package/src/sdk-worker.mjs +161 -84
- package/src/service/contracts/workbench.ts +8 -0
- package/src/service/contracts/workflow.ts +34 -11
- package/src/service-cli.mjs +41 -11
- package/src/service-host.mjs +868 -463
- package/src/worker-capture.mjs +77 -23
- package/src/worker-model.mjs +343 -0
- package/src/worker-public.mjs +44 -16
- package/src/worker-toolkit.mjs +60 -46
- package/src/worker-tools.mjs +77 -57
- package/web/dist/assets/cadence.js +21 -21
- package/web/dist/manifest.json +5 -5
package/src/worker-capture.mjs
CHANGED
|
@@ -58,6 +58,7 @@ export class CaptureState {
|
|
|
58
58
|
this.bytes = 0;
|
|
59
59
|
this.coverage = completeCoverage();
|
|
60
60
|
this.selected = null;
|
|
61
|
+
this.retry = null;
|
|
61
62
|
this.failed = false;
|
|
62
63
|
this.request = request;
|
|
63
64
|
this.contexts = new Set();
|
|
@@ -68,13 +69,17 @@ export class CaptureState {
|
|
|
68
69
|
const e = frame.event;
|
|
69
70
|
const control = controlEvent(e.kind);
|
|
70
71
|
this.bytes += encodedBytes ?? utf8Bytes(canonicalJson(frame)) + 1;
|
|
72
|
+
// Counters are diagnostics, not a lifetime quota. Only unresolved state
|
|
73
|
+
// consumes the live protocol window; settled details are reclaimed below.
|
|
74
|
+
this[control ? "controlFrames" : "dataFrames"]++;
|
|
71
75
|
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
this.details.size +
|
|
77
|
+
this.messages.size +
|
|
78
|
+
this.calls.size +
|
|
79
|
+
this.blocks.size >
|
|
80
|
+
LIMITS.captureEntries
|
|
76
81
|
)
|
|
77
|
-
throw new ContractError("
|
|
82
|
+
throw new ContractError("capacity");
|
|
78
83
|
const message = (id) => {
|
|
79
84
|
const m = this.messages.get(id);
|
|
80
85
|
requireFact(m);
|
|
@@ -106,8 +111,24 @@ export class CaptureState {
|
|
|
106
111
|
break;
|
|
107
112
|
case "message-start":
|
|
108
113
|
requireFact(!this.messages.has(e.messageId));
|
|
109
|
-
if (e.resultCallId !== null)
|
|
114
|
+
if (e.resultCallId !== null) {
|
|
110
115
|
requireFact(this.calls.get(e.resultCallId)?.ended);
|
|
116
|
+
this.calls.delete(e.resultCallId);
|
|
117
|
+
}
|
|
118
|
+
// The previous response is no longer a final-result candidate. Keep
|
|
119
|
+
// unresolved calls, not all past messages or partial failed declarations.
|
|
120
|
+
if (e.role === "assistant") {
|
|
121
|
+
for (const [id, m] of this.messages)
|
|
122
|
+
if (m.ended) {
|
|
123
|
+
if (m.final) this.details.delete(m.final);
|
|
124
|
+
this.messages.delete(id);
|
|
125
|
+
}
|
|
126
|
+
for (const [id, c] of this.calls)
|
|
127
|
+
if (!c.started || c.ended) {
|
|
128
|
+
this.releaseCallDetails(id);
|
|
129
|
+
this.calls.delete(id);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
111
132
|
this.messages.set(e.messageId, {
|
|
112
133
|
role: e.role,
|
|
113
134
|
ended: false,
|
|
@@ -194,6 +215,8 @@ export class CaptureState {
|
|
|
194
215
|
d.hash = null;
|
|
195
216
|
d.ended = true;
|
|
196
217
|
this.mergeCoverage(e.coverage);
|
|
218
|
+
if (["context", "handoff"].includes(d.descriptor.owner.kind))
|
|
219
|
+
this.details.delete(e.detailId);
|
|
197
220
|
break;
|
|
198
221
|
}
|
|
199
222
|
case "message-text": {
|
|
@@ -229,6 +252,7 @@ export class CaptureState {
|
|
|
229
252
|
requireFact(!m.finalIndices.has(e.index));
|
|
230
253
|
m.finalIndices.add(e.index);
|
|
231
254
|
}
|
|
255
|
+
if (e.argsDetailId) this.releaseDetail(e.argsDetailId);
|
|
232
256
|
this.calls.set(e.callId, { ...e, started: false, ended: false });
|
|
233
257
|
break;
|
|
234
258
|
}
|
|
@@ -248,6 +272,8 @@ export class CaptureState {
|
|
|
248
272
|
detail(e.argsDetailId, "tool-args", e.callId).descriptor.owner
|
|
249
273
|
.stage === "execution-arguments",
|
|
250
274
|
);
|
|
275
|
+
if (e.argsDetailId) this.releaseDetail(e.argsDetailId);
|
|
276
|
+
this.releaseCallDetails(e.callId);
|
|
251
277
|
this.calls.set(e.callId, { ...old, ...e, started: true, ended: false });
|
|
252
278
|
break;
|
|
253
279
|
}
|
|
@@ -259,6 +285,7 @@ export class CaptureState {
|
|
|
259
285
|
detail(e.detailId, "tool-update", e.callId).descriptor.owner.stage ===
|
|
260
286
|
"update-snapshot",
|
|
261
287
|
);
|
|
288
|
+
this.releaseDetail(e.detailId);
|
|
262
289
|
break;
|
|
263
290
|
case "tool-end": {
|
|
264
291
|
const c = this.calls.get(e.callId);
|
|
@@ -276,6 +303,8 @@ export class CaptureState {
|
|
|
276
303
|
detail(e.resultDetailId, "tool-result", e.callId).descriptor.owner
|
|
277
304
|
.stage === "result",
|
|
278
305
|
);
|
|
306
|
+
this.releaseDetail(e.resultDetailId);
|
|
307
|
+
this.releaseCallDetails(e.callId);
|
|
279
308
|
this.calls.set(e.callId, {
|
|
280
309
|
...c,
|
|
281
310
|
...e,
|
|
@@ -307,6 +336,18 @@ export class CaptureState {
|
|
|
307
336
|
m.ended = true;
|
|
308
337
|
m.final = e.finalDetailId;
|
|
309
338
|
m.stopReason = e.stopReason;
|
|
339
|
+
for (const [id, d] of this.details)
|
|
340
|
+
if (
|
|
341
|
+
d.descriptor.owner.messageId === e.messageId &&
|
|
342
|
+
id !== e.finalDetailId
|
|
343
|
+
)
|
|
344
|
+
this.releaseDetail(id);
|
|
345
|
+
// Omitted blocks have no detail record.
|
|
346
|
+
for (const [id, key] of this.blocks)
|
|
347
|
+
if (JSON.parse(key).messageId === e.messageId) this.blocks.delete(id);
|
|
348
|
+
m.finalIndices.clear();
|
|
349
|
+
m.finalHash = null;
|
|
350
|
+
if (m.role !== "assistant") this.messages.delete(e.messageId);
|
|
310
351
|
break;
|
|
311
352
|
}
|
|
312
353
|
case "omitted-block":
|
|
@@ -326,6 +367,9 @@ export class CaptureState {
|
|
|
326
367
|
omissions: [e.block.reason],
|
|
327
368
|
});
|
|
328
369
|
break;
|
|
370
|
+
case "model-retry":
|
|
371
|
+
this.retry = e.progress;
|
|
372
|
+
break;
|
|
329
373
|
case "capture-status":
|
|
330
374
|
this.mergeCoverage(e.coverage);
|
|
331
375
|
break;
|
|
@@ -347,6 +391,7 @@ export class CaptureState {
|
|
|
347
391
|
const m = message(e.messageId);
|
|
348
392
|
requireFact(
|
|
349
393
|
!this.failed &&
|
|
394
|
+
this.retry === null &&
|
|
350
395
|
m.ended &&
|
|
351
396
|
m.role === "assistant" &&
|
|
352
397
|
m.stopReason === "stop" &&
|
|
@@ -366,6 +411,19 @@ export class CaptureState {
|
|
|
366
411
|
this.position = position;
|
|
367
412
|
return frame;
|
|
368
413
|
}
|
|
414
|
+
releaseCallDetails(callId) {
|
|
415
|
+
for (const [id, d] of this.details)
|
|
416
|
+
if (d.descriptor.owner.callId === callId && d.ended)
|
|
417
|
+
this.releaseDetail(id);
|
|
418
|
+
for (const [id, key] of this.blocks)
|
|
419
|
+
if (JSON.parse(key).callId === callId) this.blocks.delete(id);
|
|
420
|
+
}
|
|
421
|
+
releaseDetail(id) {
|
|
422
|
+
const d = this.details.get(id);
|
|
423
|
+
if (d?.descriptor.owner.blockId)
|
|
424
|
+
this.blocks.delete(d.descriptor.owner.blockId);
|
|
425
|
+
this.details.delete(id);
|
|
426
|
+
}
|
|
369
427
|
mergeCoverage(c) {
|
|
370
428
|
this.coverage.observed =
|
|
371
429
|
c.observed === "incomplete" ? "incomplete" : this.coverage.observed;
|
|
@@ -492,7 +550,6 @@ export class CaptureEmitter {
|
|
|
492
550
|
lease.release();
|
|
493
551
|
throw error;
|
|
494
552
|
}
|
|
495
|
-
let timer;
|
|
496
553
|
const original = new Promise((resolve, reject) => {
|
|
497
554
|
this.output.write(line, (error) =>
|
|
498
555
|
error ? reject(new ContractError("unavailable")) : resolve(),
|
|
@@ -501,13 +558,11 @@ export class CaptureEmitter {
|
|
|
501
558
|
this.pending.add(original);
|
|
502
559
|
const settled = original.then(
|
|
503
560
|
() => {
|
|
504
|
-
clearTimeout(timer);
|
|
505
561
|
this.queuedBytes -= bytes;
|
|
506
562
|
lease.release();
|
|
507
563
|
this.pending.delete(original);
|
|
508
564
|
},
|
|
509
565
|
() => {
|
|
510
|
-
clearTimeout(timer);
|
|
511
566
|
this.queuedBytes -= bytes;
|
|
512
567
|
lease.release();
|
|
513
568
|
this.pending.delete(original);
|
|
@@ -515,19 +570,11 @@ export class CaptureEmitter {
|
|
|
515
570
|
this.onFailure();
|
|
516
571
|
},
|
|
517
572
|
);
|
|
518
|
-
//
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
new Promise((_, reject) => {
|
|
522
|
-
timer = setTimeout(() => {
|
|
523
|
-
this.onFailure();
|
|
524
|
-
reject(new ContractError("recording-exhausted"));
|
|
525
|
-
}, LIMITS.recordingStallMs);
|
|
526
|
-
}),
|
|
527
|
-
]);
|
|
528
|
-
wait.catch(() => {});
|
|
573
|
+
// Slow output is backpressure, not failure. The original callback alone
|
|
574
|
+
// releases its charge; a real stream error still fails capture.
|
|
575
|
+
original.catch(() => {});
|
|
529
576
|
settled.catch(() => {});
|
|
530
|
-
return
|
|
577
|
+
return original;
|
|
531
578
|
}
|
|
532
579
|
async drain() {
|
|
533
580
|
await Promise.allSettled([...this.pending]);
|
|
@@ -792,6 +839,8 @@ export class SdkCapture {
|
|
|
792
839
|
ended: false,
|
|
793
840
|
resultLinked: false,
|
|
794
841
|
};
|
|
842
|
+
if (this.calls.length >= LIMITS.captureEntries)
|
|
843
|
+
throw new ContractError("capacity");
|
|
795
844
|
this.calls.push(call);
|
|
796
845
|
return call;
|
|
797
846
|
}
|
|
@@ -938,13 +987,15 @@ export class SdkCapture {
|
|
|
938
987
|
return detailId;
|
|
939
988
|
}
|
|
940
989
|
async event(event) {
|
|
941
|
-
|
|
990
|
+
this.events++;
|
|
942
991
|
const type = own(event, "type");
|
|
943
992
|
if (typeof type !== "string") throw new ContractError();
|
|
944
993
|
switch (type) {
|
|
945
994
|
case "message_start": {
|
|
946
995
|
requireFact(this.current === null);
|
|
947
996
|
const message = own(event, "message");
|
|
997
|
+
if (own(message, "role") === "assistant")
|
|
998
|
+
this.calls = this.calls.filter((c) => c.started && !c.ended);
|
|
948
999
|
const rawRole = own(message, "role");
|
|
949
1000
|
const role = rawRole === "toolResult" ? "tool-result" : rawRole;
|
|
950
1001
|
requireFact(
|
|
@@ -959,7 +1010,10 @@ export class SdkCapture {
|
|
|
959
1010
|
)
|
|
960
1011
|
: [];
|
|
961
1012
|
const call = matches.length === 1 ? matches[0] : null;
|
|
962
|
-
if (call)
|
|
1013
|
+
if (call) {
|
|
1014
|
+
call.resultLinked = true;
|
|
1015
|
+
this.calls = this.calls.filter((c) => c !== call);
|
|
1016
|
+
}
|
|
963
1017
|
this.current = {
|
|
964
1018
|
id: randomUUID(),
|
|
965
1019
|
role,
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bool,
|
|
3
|
+
canonicalJson,
|
|
4
|
+
closed,
|
|
5
|
+
decodeModelRetry,
|
|
6
|
+
integer,
|
|
7
|
+
LIMITS,
|
|
8
|
+
list,
|
|
9
|
+
oneOf,
|
|
10
|
+
text,
|
|
11
|
+
} from "./worker-public.mjs";
|
|
12
|
+
import { own } from "./worker-redaction.mjs";
|
|
13
|
+
|
|
14
|
+
/** Service exceptions only. Pi owns defaults and user/project setting merges.
|
|
15
|
+
* No cache-warming requests or second provider retry layer in owned workers. */
|
|
16
|
+
export const WORKER_POLICY = Object.freeze({
|
|
17
|
+
retry: { provider: { maxRetries: 0 } },
|
|
18
|
+
cacheWarming: "off",
|
|
19
|
+
httpIdleTimeoutMs: 0,
|
|
20
|
+
});
|
|
21
|
+
export const WORKER_SETTINGS_BYTES = 32768;
|
|
22
|
+
|
|
23
|
+
/** Public SDK factories are the source of built-in/default tool identities. */
|
|
24
|
+
export function defaultWorkerTools(pi, cwd) {
|
|
25
|
+
const edit = pi.createCodingTools(cwd).map((tool) => tool.name);
|
|
26
|
+
const readonly = pi.createReadOnlyTools(cwd).map((tool) => tool.name);
|
|
27
|
+
return {
|
|
28
|
+
edit,
|
|
29
|
+
readonly,
|
|
30
|
+
available: [
|
|
31
|
+
...new Set([...edit, ...readonly, pi.createPowerShellTool(cwd).name]),
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Resolve a selected model through Pi's public getters, not a second merge or
|
|
37
|
+
* settings-default implementation. Only execution preferences cross the pipe;
|
|
38
|
+
* package/extension/auth/model discovery settings never do. */
|
|
39
|
+
export function resolveWorkerRuntime(manager, model, defaults) {
|
|
40
|
+
if (manager.drainErrors().length)
|
|
41
|
+
throw new Error("Pi settings could not be read");
|
|
42
|
+
const effective = manager.getSettings();
|
|
43
|
+
const selected = manager.getDefaultTools();
|
|
44
|
+
const tools = selected ?? defaults.edit;
|
|
45
|
+
if (
|
|
46
|
+
!Array.isArray(tools) ||
|
|
47
|
+
tools.some((name) => !defaults.available.includes(name))
|
|
48
|
+
)
|
|
49
|
+
throw new Error("Configured Pi tools are unavailable in service workers");
|
|
50
|
+
const provider = manager.getProviderRetrySettings();
|
|
51
|
+
return decodeWorkerRuntime({
|
|
52
|
+
projectTrusted: manager.isProjectTrusted(),
|
|
53
|
+
tools: {
|
|
54
|
+
edit: tools,
|
|
55
|
+
readonly:
|
|
56
|
+
selected === undefined
|
|
57
|
+
? defaults.readonly
|
|
58
|
+
: tools.filter((name) => defaults.readonly.includes(name)),
|
|
59
|
+
},
|
|
60
|
+
settings: {
|
|
61
|
+
compaction: manager.getCompactionSettings(model),
|
|
62
|
+
retry: {
|
|
63
|
+
...manager.getRetrySettings(),
|
|
64
|
+
provider: {
|
|
65
|
+
...WORKER_POLICY.retry.provider,
|
|
66
|
+
maxRetryDelayMs: provider.maxRetryDelayMs,
|
|
67
|
+
...(provider.timeoutMs === undefined
|
|
68
|
+
? {}
|
|
69
|
+
: { timeoutMs: provider.timeoutMs }),
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
// A product default, not a replacement for an explicit Pi request policy.
|
|
73
|
+
httpIdleTimeoutMs:
|
|
74
|
+
effective.httpIdleTimeoutMs === undefined
|
|
75
|
+
? WORKER_POLICY.httpIdleTimeoutMs
|
|
76
|
+
: manager.getHttpIdleTimeoutMs(),
|
|
77
|
+
...(manager.getWebSocketConnectTimeoutMs() === undefined
|
|
78
|
+
? {}
|
|
79
|
+
: {
|
|
80
|
+
websocketConnectTimeoutMs: manager.getWebSocketConnectTimeoutMs(),
|
|
81
|
+
}),
|
|
82
|
+
transport: manager.getTransport(),
|
|
83
|
+
...(manager.getThinkingBudgets() === undefined
|
|
84
|
+
? {}
|
|
85
|
+
: { thinkingBudgets: manager.getThinkingBudgets() }),
|
|
86
|
+
images: {
|
|
87
|
+
autoResize: manager.getImageAutoResize(),
|
|
88
|
+
blockImages: manager.getBlockImages(),
|
|
89
|
+
},
|
|
90
|
+
...(manager.getShellPath() === undefined
|
|
91
|
+
? {}
|
|
92
|
+
: { shellPath: manager.getShellPath() }),
|
|
93
|
+
...(manager.getShellCommandPrefix() === undefined
|
|
94
|
+
? {}
|
|
95
|
+
: { shellCommandPrefix: manager.getShellCommandPrefix() }),
|
|
96
|
+
cacheWarming: WORKER_POLICY.cacheWarming,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Closed, bounded private runtime snapshot; it grants no tool authority. */
|
|
102
|
+
export function decodeWorkerRuntime(value) {
|
|
103
|
+
// JSON also removes omitted SDK getter fields. The wire owns plain data only.
|
|
104
|
+
const v = JSON.parse(canonicalJson(value, WORKER_SETTINGS_BYTES));
|
|
105
|
+
const o = closed(v, ["projectTrusted", "tools", "settings"]);
|
|
106
|
+
const t = closed(o.tools, ["edit", "readonly"]);
|
|
107
|
+
const r = o.settings;
|
|
108
|
+
const s = closed(r, [
|
|
109
|
+
"compaction",
|
|
110
|
+
"retry",
|
|
111
|
+
"httpIdleTimeoutMs",
|
|
112
|
+
"transport",
|
|
113
|
+
"images",
|
|
114
|
+
"cacheWarming",
|
|
115
|
+
...[
|
|
116
|
+
"websocketConnectTimeoutMs",
|
|
117
|
+
"thinkingBudgets",
|
|
118
|
+
"shellPath",
|
|
119
|
+
"shellCommandPrefix",
|
|
120
|
+
].filter((key) => Object.hasOwn(r ?? {}, key)),
|
|
121
|
+
]);
|
|
122
|
+
const c = closed(s.compaction, [
|
|
123
|
+
"enabled",
|
|
124
|
+
"reserveTokens",
|
|
125
|
+
"keepRecentTokens",
|
|
126
|
+
]);
|
|
127
|
+
const retry = closed(s.retry, [
|
|
128
|
+
"enabled",
|
|
129
|
+
"maxRetries",
|
|
130
|
+
"baseDelayMs",
|
|
131
|
+
"maxAgentDelayMs",
|
|
132
|
+
"provider",
|
|
133
|
+
]);
|
|
134
|
+
const provider = closed(retry.provider, [
|
|
135
|
+
"maxRetries",
|
|
136
|
+
"maxRetryDelayMs",
|
|
137
|
+
...(Object.hasOwn(retry.provider ?? {}, "timeoutMs") ? ["timeoutMs"] : []),
|
|
138
|
+
]);
|
|
139
|
+
const images = closed(s.images, ["autoResize", "blockImages"]);
|
|
140
|
+
const milliseconds = (v) => integer(v, 0, 2147483647);
|
|
141
|
+
const names = (v) => {
|
|
142
|
+
const result = list(v, 64, (name) => text(name, 128, true));
|
|
143
|
+
if (new Set(result).size !== result.length)
|
|
144
|
+
throw new Error("Duplicate Pi tool selection");
|
|
145
|
+
return result;
|
|
146
|
+
};
|
|
147
|
+
const budgets = s.thinkingBudgets;
|
|
148
|
+
if (budgets !== undefined) {
|
|
149
|
+
closed(
|
|
150
|
+
budgets,
|
|
151
|
+
["minimal", "low", "medium", "high"].filter((key) =>
|
|
152
|
+
Object.hasOwn(budgets, key),
|
|
153
|
+
),
|
|
154
|
+
);
|
|
155
|
+
for (const value of Object.values(budgets)) integer(value, 0);
|
|
156
|
+
}
|
|
157
|
+
if (s.cacheWarming !== "off" || provider.maxRetries !== 0)
|
|
158
|
+
throw new Error("Invalid service policy");
|
|
159
|
+
return {
|
|
160
|
+
projectTrusted: bool(o.projectTrusted),
|
|
161
|
+
tools: { edit: names(t.edit), readonly: names(t.readonly) },
|
|
162
|
+
settings: {
|
|
163
|
+
compaction: {
|
|
164
|
+
enabled: bool(c.enabled),
|
|
165
|
+
reserveTokens: integer(c.reserveTokens, 0),
|
|
166
|
+
keepRecentTokens: integer(c.keepRecentTokens, 0),
|
|
167
|
+
},
|
|
168
|
+
retry: {
|
|
169
|
+
enabled: bool(retry.enabled),
|
|
170
|
+
maxRetries: integer(retry.maxRetries, 0),
|
|
171
|
+
baseDelayMs: milliseconds(retry.baseDelayMs),
|
|
172
|
+
maxAgentDelayMs: milliseconds(retry.maxAgentDelayMs),
|
|
173
|
+
provider: {
|
|
174
|
+
maxRetries: 0,
|
|
175
|
+
maxRetryDelayMs: milliseconds(provider.maxRetryDelayMs),
|
|
176
|
+
...(provider.timeoutMs === undefined
|
|
177
|
+
? {}
|
|
178
|
+
: { timeoutMs: milliseconds(provider.timeoutMs) }),
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
httpIdleTimeoutMs: milliseconds(s.httpIdleTimeoutMs),
|
|
182
|
+
...(s.websocketConnectTimeoutMs === undefined
|
|
183
|
+
? {}
|
|
184
|
+
: {
|
|
185
|
+
websocketConnectTimeoutMs: milliseconds(
|
|
186
|
+
s.websocketConnectTimeoutMs,
|
|
187
|
+
),
|
|
188
|
+
}),
|
|
189
|
+
transport: oneOf(s.transport, [
|
|
190
|
+
"auto",
|
|
191
|
+
"sse",
|
|
192
|
+
"websocket",
|
|
193
|
+
"websocket-cached",
|
|
194
|
+
]),
|
|
195
|
+
...(budgets === undefined ? {} : { thinkingBudgets: budgets }),
|
|
196
|
+
images: {
|
|
197
|
+
autoResize: bool(images.autoResize),
|
|
198
|
+
blockImages: bool(images.blockImages),
|
|
199
|
+
},
|
|
200
|
+
...(s.shellPath === undefined
|
|
201
|
+
? {}
|
|
202
|
+
: { shellPath: text(s.shellPath, 4096) }),
|
|
203
|
+
...(s.shellCommandPrefix === undefined
|
|
204
|
+
? {}
|
|
205
|
+
: { shellCommandPrefix: text(s.shellCommandPrefix, 8192) }),
|
|
206
|
+
cacheWarming: "off",
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function describeWorkerRuntime(runtime) {
|
|
212
|
+
const { settings: s, tools } = runtime;
|
|
213
|
+
return `Pi runtime: project settings ${runtime.projectTrusted ? "trusted" : "not loaded"}; tools ${tools.edit.join(", ") || "none"}; readonly ${tools.readonly.join(", ") || "none"}; auto-compaction ${s.compaction.enabled ? "on" : "off"} (reserve ${s.compaction.reserveTokens}, keep ${s.compaction.keepRecentTokens}); request retries ${s.retry.enabled ? s.retry.maxRetries : "off"}; HTTP idle ${s.httpIdleTimeoutMs === 0 ? "disabled" : `${s.httpIdleTimeoutMs}ms`}; shell ${s.shellPath || s.shellCommandPrefix ? "configured" : "default"}. Provider retries and cache warming are off.`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Observation only: AgentSession owns classification, backoff and continuation.
|
|
217
|
+
* Session listeners are synchronous. CaptureEmitter reserves/enqueues before
|
|
218
|
+
* returning its promise; every rejection is handled without leaking SDK text. */
|
|
219
|
+
export class WorkerModelState {
|
|
220
|
+
requestPrepared = false;
|
|
221
|
+
actualResponseStatus = "UNKNOWN";
|
|
222
|
+
retryAttempts = 0;
|
|
223
|
+
progress = null;
|
|
224
|
+
constructor(emit, fail) {
|
|
225
|
+
this.emit = emit;
|
|
226
|
+
this.fail = fail;
|
|
227
|
+
}
|
|
228
|
+
publish(progress) {
|
|
229
|
+
this.progress = progress;
|
|
230
|
+
try {
|
|
231
|
+
return Promise.resolve(this.emit({ kind: "model-retry", progress }));
|
|
232
|
+
} catch (error) {
|
|
233
|
+
return Promise.reject(error);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
event(event) {
|
|
237
|
+
try {
|
|
238
|
+
const type = own(event, "type");
|
|
239
|
+
let pending;
|
|
240
|
+
if (type === "auto_retry_start") {
|
|
241
|
+
const progress = decodeModelRetry({
|
|
242
|
+
phase: "waiting",
|
|
243
|
+
attempt: own(event, "attempt"),
|
|
244
|
+
maxAttempts: own(event, "maxAttempts"),
|
|
245
|
+
delayMs: own(event, "delayMs"),
|
|
246
|
+
});
|
|
247
|
+
this.retryAttempts = progress.attempt;
|
|
248
|
+
pending = this.publish(progress);
|
|
249
|
+
} else if (type === "auto_retry_end" && this.progress) {
|
|
250
|
+
// This event is not proof of success, native closure or task completion.
|
|
251
|
+
pending = this.publish(null);
|
|
252
|
+
}
|
|
253
|
+
pending?.catch(() => this.fail());
|
|
254
|
+
} catch {
|
|
255
|
+
this.fail();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async beginRequest() {
|
|
259
|
+
// Each attempt owns its observations, including one that gets no response.
|
|
260
|
+
this.requestPrepared = false;
|
|
261
|
+
this.actualResponseStatus = "UNKNOWN";
|
|
262
|
+
if (!this.progress) {
|
|
263
|
+
this.retryAttempts = 0;
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
await this.publish({ ...this.progress, phase: "requesting", delayMs: 0 });
|
|
268
|
+
} catch (error) {
|
|
269
|
+
this.fail();
|
|
270
|
+
throw error;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** One private, sequential request at a time. Replies contain credentials only
|
|
276
|
+
* on the owner/child IPC pipe, never in capture or a tool result. No retry here.
|
|
277
|
+
* Abort is owned by the session; disconnect is a real loss of its auth owner. */
|
|
278
|
+
export function ownerAuth(channel, retain, fail = () => {}) {
|
|
279
|
+
let sequence = 0;
|
|
280
|
+
let pending;
|
|
281
|
+
let queued = 0;
|
|
282
|
+
let tail = Promise.resolve();
|
|
283
|
+
let closed = false;
|
|
284
|
+
const disconnect = () => {
|
|
285
|
+
closed = true;
|
|
286
|
+
if (pending) fail("owner-auth-lost");
|
|
287
|
+
pending?.reject(new Error("Owner authentication channel disconnected"));
|
|
288
|
+
pending = undefined;
|
|
289
|
+
};
|
|
290
|
+
const message = (value) => {
|
|
291
|
+
if (value?.kind !== "auth" || !pending || value.id !== pending.id) return;
|
|
292
|
+
const waiting = pending;
|
|
293
|
+
pending = undefined;
|
|
294
|
+
if (!value.ok || !value.auth?.ok) {
|
|
295
|
+
fail("owner-auth-refused");
|
|
296
|
+
waiting.reject(new Error("Owner authentication refused"));
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
retain(value.auth);
|
|
300
|
+
waiting.resolve(value.auth);
|
|
301
|
+
};
|
|
302
|
+
channel.on("message", message);
|
|
303
|
+
channel.on("disconnect", disconnect);
|
|
304
|
+
return {
|
|
305
|
+
resolve() {
|
|
306
|
+
if (closed || !channel.connected || queued >= LIMITS.childReservations)
|
|
307
|
+
return Promise.reject(new Error("Owner authentication unavailable"));
|
|
308
|
+
queued++;
|
|
309
|
+
const original = tail.then(
|
|
310
|
+
() =>
|
|
311
|
+
new Promise((resolve, reject) => {
|
|
312
|
+
if (closed || !channel.connected) {
|
|
313
|
+
reject(new Error("Owner authentication unavailable"));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const id = ++sequence;
|
|
317
|
+
pending = { id, resolve, reject };
|
|
318
|
+
try {
|
|
319
|
+
channel.send({ kind: "auth", id }, (error) => {
|
|
320
|
+
if (error) disconnect();
|
|
321
|
+
});
|
|
322
|
+
} catch {
|
|
323
|
+
disconnect();
|
|
324
|
+
}
|
|
325
|
+
}),
|
|
326
|
+
);
|
|
327
|
+
tail = original
|
|
328
|
+
.then(
|
|
329
|
+
() => {},
|
|
330
|
+
() => {},
|
|
331
|
+
)
|
|
332
|
+
.finally(() => {
|
|
333
|
+
queued--;
|
|
334
|
+
});
|
|
335
|
+
return original;
|
|
336
|
+
},
|
|
337
|
+
close() {
|
|
338
|
+
disconnect();
|
|
339
|
+
channel.removeListener("message", message);
|
|
340
|
+
channel.removeListener("disconnect", disconnect);
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|