@tangle-network/agent-provider-tangle 4.5.7 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/tangle-create-options.d.ts +5 -2
- package/dist/tangle-create-options.js +33 -28
- package/dist/tangle-environment-dispatch.d.ts +2 -1
- package/dist/tangle-environment-dispatch.js +4 -4
- package/dist/tangle-environment-session.d.ts +4 -16
- package/dist/tangle-environment-session.js +285 -307
- package/dist/tangle-environment.d.ts +1 -23
- package/dist/tangle-environment.js +198 -221
- package/dist/tangle-prompt.d.ts +6 -24
- package/dist/tangle-prompt.js +91 -89
- package/dist/tangle-provider.js +13 -11
- package/dist/tangle-proxy-secrets.d.ts +1 -3
- package/dist/tangle-proxy-secrets.js +13 -43
- package/dist/tangle-types.d.ts +6 -12
- package/package.json +4 -4
|
@@ -4,27 +4,15 @@ import { AgentRunCancellationAcknowledgementSchema, AgentRunCancellationRequestS
|
|
|
4
4
|
import { NativeContextContinuationRequestSchema, nativeContextContinuationTurnDigest, } from "@tangle-network/agent-interface";
|
|
5
5
|
import { createStepUsageFold } from "./tangle-step-usage.js";
|
|
6
6
|
import { carriedSessionIds, environmentEventFromSandboxEvent, isSandboxConnectionMarker, sandboxEventIdentity, } from "./tangle-events.js";
|
|
7
|
-
import { agentTurnResultFromPromptRecord, promptFromTurnInput, promptOptionsFromTurnInput,
|
|
7
|
+
import { agentTurnResultFromPromptRecord, promptFromTurnInput, promptOptionsFromTurnInput, validatedSandboxPromptResult, } from "./tangle-prompt.js";
|
|
8
8
|
import { retainedSessionControlRef, resolveRetainedSessionControlRef, sameRunControlRef, sessionPromptExecutionId, } from "./tangle-session-control.js";
|
|
9
9
|
import { executionBoundSessionStatus, sessionStatusFromUnknown, } from "./tangle-environment-values.js";
|
|
10
10
|
import { awaitWithSignal, boundedIdentifier, } from "./tangle-contract-safety.js";
|
|
11
11
|
import { assertOptionKeys } from "./tangle-environment-validation.js";
|
|
12
12
|
import { tangleInteractionResponder } from "./tangle-interaction-response.js";
|
|
13
13
|
import { hasReplayPayload, interruptExecutionAfterAbort, sessionPromptRequestDigest, } from "./tangle-environment-control.js";
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
* document grants retained control. Canonical cancellation is offered only
|
|
17
|
-
* under that grant: a `cancelRun` method the deployment does not honor is an
|
|
18
|
-
* action the caller selects and finds rejected on the wire.
|
|
19
|
-
* @param interactionResponses Whether the environment's narrowed capability
|
|
20
|
-
* document claims interaction responses. The method is offered only under
|
|
21
|
-
* that claim, so a caller never selects an answer the deployment cannot
|
|
22
|
-
* record.
|
|
23
|
-
* @param usageLog Sink for the token usage each execution measured. The
|
|
24
|
-
* environment observation reads it, because Sandbox reports usage per
|
|
25
|
-
* execution result and never for the environment.
|
|
26
|
-
*/
|
|
27
|
-
export function sandboxSessionAsAgentSession(session, controlRef, provider, environmentId, dispatch, exactExecutionEvents, retainedControl, interactionResponses, usageLog, nativeContinuation = false, beforePrompt, onExecution, box) {
|
|
14
|
+
/** Compose exact session control, replay, prompt preparation and usage. */
|
|
15
|
+
export function sandboxSessionAsAgentSession(session, controlRef, provider, environmentId, dispatch, exactExecutionEvents, retainedControl, interactionResponses, usageLog, nativeContinuation = false, beforePrompt, onExecution, prepareOptions = async (options) => options) {
|
|
28
16
|
const measured = (executionId, result) => {
|
|
29
17
|
usageLog?.record(executionId, result.usage);
|
|
30
18
|
return result;
|
|
@@ -34,7 +22,6 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
34
22
|
: resolveRetainedSessionControlRef(controlRef, session.id, provider, environmentId);
|
|
35
23
|
if (activeControlRef !== undefined)
|
|
36
24
|
onExecution?.(activeControlRef.executionId);
|
|
37
|
-
let promptInFlight = false;
|
|
38
25
|
const respondToInteraction = interactionResponses && typeof session.respondToInteraction === "function"
|
|
39
26
|
? tangleInteractionResponder({
|
|
40
27
|
session,
|
|
@@ -65,133 +52,7 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
65
52
|
return exactAcknowledgement;
|
|
66
53
|
}
|
|
67
54
|
: undefined;
|
|
68
|
-
|
|
69
|
-
// prompt by its turnId, and continueNative sets turnId to the operationId,
|
|
70
|
-
// so a retry after a lost response replays the same dispatch on the wire.
|
|
71
|
-
// This map lets the retry return the same acknowledgement and result with
|
|
72
|
-
// no dispatch at all, and turns a changed turn under a reused operationId
|
|
73
|
-
// into a conflict instead of a second run.
|
|
74
|
-
const nativeOperations = new Map();
|
|
75
|
-
const currentBoundary = () => {
|
|
76
|
-
if (activeControlRef === undefined)
|
|
77
|
-
return null;
|
|
78
|
-
return {
|
|
79
|
-
runId: activeControlRef.runId,
|
|
80
|
-
provider: activeControlRef.provider,
|
|
81
|
-
environmentId: activeControlRef.environmentId,
|
|
82
|
-
sessionId: activeControlRef.sessionId,
|
|
83
|
-
executionId: activeControlRef.executionId,
|
|
84
|
-
requestDigest: activeControlRef.requestDigest,
|
|
85
|
-
// The conversation's boundary is the run that last extended it. A
|
|
86
|
-
// continuation is verified against exactly that run, so a turn that
|
|
87
|
-
// landed in between moves the boundary and the continuation is refused
|
|
88
|
-
// rather than appended to a conversation the caller has not seen.
|
|
89
|
-
boundary: { kind: "revision", revision: activeControlRef.executionId },
|
|
90
|
-
observedAt: new Date().toISOString(),
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
const contextBoundary = async (options) => {
|
|
94
|
-
assertOptionKeys(options, ["signal"], "Tangle native context boundary");
|
|
95
|
-
options?.signal?.throwIfAborted();
|
|
96
|
-
return currentBoundary();
|
|
97
|
-
};
|
|
98
|
-
const continueNative = async (request, continuationOptions) => {
|
|
99
|
-
const exactRequest = NativeContextContinuationRequestSchema.parse(request);
|
|
100
|
-
if (nativeContextContinuationTurnDigest(continuationOptions.turn) !==
|
|
101
|
-
exactRequest.turnDigest) {
|
|
102
|
-
throw new Error("Tangle native continuation turn does not match its request digest");
|
|
103
|
-
}
|
|
104
|
-
if (continuationOptions.onAdmission !== undefined) {
|
|
105
|
-
throw new Error("Tangle native continuation does not advertise early admission control");
|
|
106
|
-
}
|
|
107
|
-
continuationOptions.signal?.throwIfAborted();
|
|
108
|
-
// A retry names the run the conversation had BEFORE the continuation it
|
|
109
|
-
// is retrying, so the operation record is consulted before the request is
|
|
110
|
-
// bound to the current run; a replay must answer even though the run has
|
|
111
|
-
// since advanced past it.
|
|
112
|
-
const prior = nativeOperations.get(exactRequest.operationId);
|
|
113
|
-
if (prior !== undefined) {
|
|
114
|
-
if (prior.requestDigest === exactRequest.requestDigest) {
|
|
115
|
-
return {
|
|
116
|
-
...prior.outcome,
|
|
117
|
-
acknowledgement: { ...prior.outcome.acknowledgement, status: "replayed" },
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return {
|
|
121
|
-
acknowledgement: {
|
|
122
|
-
operationId: exactRequest.operationId,
|
|
123
|
-
requestDigest: exactRequest.requestDigest,
|
|
124
|
-
status: "conflict",
|
|
125
|
-
historyMessagesSent: 0,
|
|
126
|
-
existingRequestDigest: prior.requestDigest,
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
const expectedRun = activeControlRef;
|
|
131
|
-
if (expectedRun === undefined) {
|
|
132
|
-
throw new Error("Tangle native continuation requires an exact run control reference");
|
|
133
|
-
}
|
|
134
|
-
if (!sameRunControlRef(exactRequest.run, expectedRun)) {
|
|
135
|
-
throw new Error("Tangle native continuation targets another retained run");
|
|
136
|
-
}
|
|
137
|
-
const actual = currentBoundary();
|
|
138
|
-
const expected = exactRequest.expectedBoundary.boundary;
|
|
139
|
-
const boundaryMatches = actual !== null &&
|
|
140
|
-
actual.boundary.kind === "revision" &&
|
|
141
|
-
expected.kind === "revision" &&
|
|
142
|
-
actual.boundary.revision === expected.revision;
|
|
143
|
-
if (!boundaryMatches) {
|
|
144
|
-
return {
|
|
145
|
-
acknowledgement: {
|
|
146
|
-
operationId: exactRequest.operationId,
|
|
147
|
-
requestDigest: exactRequest.requestDigest,
|
|
148
|
-
status: "boundary_mismatch",
|
|
149
|
-
historyMessagesSent: 0,
|
|
150
|
-
...(actual === null ? {} : { actualBoundary: actual }),
|
|
151
|
-
},
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
const turn = continuationOptions.turn;
|
|
155
|
-
// The adapter's own prompt path mints the continued run's identity from
|
|
156
|
-
// the turn and its turnId, binds the control reference, and records usage.
|
|
157
|
-
const result = await agentSession.prompt({
|
|
158
|
-
...(turn.prompt === undefined ? {} : { prompt: turn.prompt }),
|
|
159
|
-
...(turn.parts === undefined ? {} : { parts: turn.parts }),
|
|
160
|
-
...(turn.model === undefined ? {} : { model: turn.model }),
|
|
161
|
-
...(turn.context === undefined ? {} : { context: turn.context }),
|
|
162
|
-
...(turn.providerOptions === undefined
|
|
163
|
-
? {}
|
|
164
|
-
: { providerOptions: turn.providerOptions }),
|
|
165
|
-
sessionId: session.id,
|
|
166
|
-
turnId: exactRequest.operationId,
|
|
167
|
-
...(continuationOptions.timeoutMs === undefined
|
|
168
|
-
? {}
|
|
169
|
-
: { timeoutMs: continuationOptions.timeoutMs }),
|
|
170
|
-
...(continuationOptions.signal === undefined
|
|
171
|
-
? {}
|
|
172
|
-
: { signal: continuationOptions.signal }),
|
|
173
|
-
});
|
|
174
|
-
const controlRef = activeControlRef;
|
|
175
|
-
if (controlRef === undefined || sameRunControlRef(controlRef, expectedRun)) {
|
|
176
|
-
throw new Error("Tangle native continuation did not advance the run control reference");
|
|
177
|
-
}
|
|
178
|
-
const outcome = {
|
|
179
|
-
acknowledgement: {
|
|
180
|
-
operationId: exactRequest.operationId,
|
|
181
|
-
requestDigest: exactRequest.requestDigest,
|
|
182
|
-
status: "accepted",
|
|
183
|
-
historyMessagesSent: 0,
|
|
184
|
-
actualBoundary: actual,
|
|
185
|
-
},
|
|
186
|
-
result,
|
|
187
|
-
controlRef: { ...controlRef },
|
|
188
|
-
};
|
|
189
|
-
nativeOperations.set(exactRequest.operationId, {
|
|
190
|
-
requestDigest: exactRequest.requestDigest,
|
|
191
|
-
outcome,
|
|
192
|
-
});
|
|
193
|
-
return outcome;
|
|
194
|
-
};
|
|
55
|
+
const { contextBoundary, continueNative } = nativeSessionContinuation(session, () => activeControlRef, (input) => agentSession.prompt(input));
|
|
195
56
|
const agentSession = {
|
|
196
57
|
id: session.id,
|
|
197
58
|
get controlRef() {
|
|
@@ -277,14 +138,7 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
277
138
|
sessionId: session.id,
|
|
278
139
|
...(useExactExecutionStream ? { streamBound: true } : {}),
|
|
279
140
|
});
|
|
280
|
-
//
|
|
281
|
-
// (agent-container apps/sidecar/src/routes/agents-events.ts). A frame with no
|
|
282
|
-
// position carries no id by the SSE contract, so a reconnect resumes from the last
|
|
283
|
-
// frame that had one. Refusing such a frame ended the whole stream, and with it the
|
|
284
|
-
// terminal receipt that carries the execution's token usage: three long pi turns on
|
|
285
|
-
// 2026-09-15 settled at 0 tokens over 846-1693 s each for exactly this reason. An
|
|
286
|
-
// id-less frame cannot be replayed, so it cannot arrive twice; deliver it without
|
|
287
|
-
// dedup and leave the cursor where it was.
|
|
141
|
+
// Id-less frames have no replay-buffer position: deliver without moving the cursor.
|
|
288
142
|
if (converted.id !== undefined) {
|
|
289
143
|
if (seenEventIds.has(converted.id))
|
|
290
144
|
continue;
|
|
@@ -316,162 +170,7 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
316
170
|
controlRef: activeControlRef,
|
|
317
171
|
}));
|
|
318
172
|
},
|
|
319
|
-
|
|
320
|
-
AgentTurnInputSchema.parse(input);
|
|
321
|
-
input.signal?.throwIfAborted();
|
|
322
|
-
if (promptInFlight) {
|
|
323
|
-
throw new Error("Tangle session already has a prompt in flight");
|
|
324
|
-
}
|
|
325
|
-
promptInFlight = true;
|
|
326
|
-
try {
|
|
327
|
-
if (input.sessionId !== undefined && input.sessionId !== session.id) {
|
|
328
|
-
throw new Error("Tangle sessionId conflicts with this session");
|
|
329
|
-
}
|
|
330
|
-
input = await beforePrompt?.({ ...input, sessionId: session.id }) ?? input;
|
|
331
|
-
input.signal?.throwIfAborted();
|
|
332
|
-
const requestedControlRef = resolveRetainedSessionControlRef(input.controlRef, session.id, provider, environmentId);
|
|
333
|
-
if (activeControlRef &&
|
|
334
|
-
requestedControlRef &&
|
|
335
|
-
!sameRunControlRef(activeControlRef, requestedControlRef)) {
|
|
336
|
-
throw new Error("Tangle prompt control reference conflicts with this session");
|
|
337
|
-
}
|
|
338
|
-
if (requestedControlRef?.executionId !== undefined &&
|
|
339
|
-
input.executionId !== undefined &&
|
|
340
|
-
requestedControlRef.executionId !== input.executionId) {
|
|
341
|
-
throw new Error("Tangle executionId conflicts with the control reference");
|
|
342
|
-
}
|
|
343
|
-
if (input.detach === true && input.lastEventId === undefined) {
|
|
344
|
-
if (dispatch === undefined) {
|
|
345
|
-
throw new Error("Tangle detached session prompt requires the sandbox dispatch primitive");
|
|
346
|
-
}
|
|
347
|
-
const detachedInput = {
|
|
348
|
-
...input,
|
|
349
|
-
signal: undefined,
|
|
350
|
-
controlRef: undefined,
|
|
351
|
-
sessionId: session.id,
|
|
352
|
-
detach: true,
|
|
353
|
-
...(input.turnId === undefined ? { turnId: randomUUID() } : {}),
|
|
354
|
-
};
|
|
355
|
-
const reference = await dispatch(detachedInput);
|
|
356
|
-
const nextControlRef = resolveRetainedSessionControlRef(reference.controlRef, session.id, provider, environmentId);
|
|
357
|
-
if (nextControlRef === undefined) {
|
|
358
|
-
throw new Error("Tangle detached session dispatch returned no exact control reference");
|
|
359
|
-
}
|
|
360
|
-
// The admission receipt is the durability boundary. Store it before
|
|
361
|
-
// waiting for the result so an aborted caller can reconnect later.
|
|
362
|
-
activeControlRef = nextControlRef;
|
|
363
|
-
onExecution?.(nextControlRef.executionId);
|
|
364
|
-
const result = await awaitWithSignal(session.result({
|
|
365
|
-
executionId: nextControlRef.executionId,
|
|
366
|
-
...(input.signal ? { signal: input.signal } : {}),
|
|
367
|
-
}), input.signal);
|
|
368
|
-
input.signal?.throwIfAborted();
|
|
369
|
-
const resultRecord = validatedSandboxPromptResult(result);
|
|
370
|
-
if (resultRecord.executionId !== nextControlRef.executionId) {
|
|
371
|
-
throw new Error("Tangle detached session prompt did not confirm its exact executionId");
|
|
372
|
-
}
|
|
373
|
-
return measured(nextControlRef.executionId, agentTurnResultFromPromptRecord(resultRecord, {
|
|
374
|
-
sessionId: session.id,
|
|
375
|
-
controlRef: nextControlRef,
|
|
376
|
-
}));
|
|
377
|
-
}
|
|
378
|
-
const sourceControlRef = requestedControlRef ?? activeControlRef;
|
|
379
|
-
const replay = input.lastEventId !== undefined;
|
|
380
|
-
if (replay &&
|
|
381
|
-
sourceControlRef?.executionId !== undefined &&
|
|
382
|
-
input.executionId !== undefined &&
|
|
383
|
-
input.executionId !== sourceControlRef.executionId) {
|
|
384
|
-
throw new Error("Tangle replay executionId conflicts with the control reference");
|
|
385
|
-
}
|
|
386
|
-
if (replay && sourceControlRef?.requestDigest === undefined) {
|
|
387
|
-
throw new Error("Tangle replay requires an exact request digest from its control reference");
|
|
388
|
-
}
|
|
389
|
-
const requestedExecutionId = input.executionId ?? requestedControlRef?.executionId;
|
|
390
|
-
const nonce = !replay &&
|
|
391
|
-
input.turnId === undefined &&
|
|
392
|
-
requestedExecutionId === undefined &&
|
|
393
|
-
requestedControlRef === undefined
|
|
394
|
-
? randomUUID()
|
|
395
|
-
: undefined;
|
|
396
|
-
const baseRequestDigest = sessionPromptRequestDigest(input, provider, environmentId, session.id, nonce === undefined ? {} : { nonce });
|
|
397
|
-
const executionId = replay
|
|
398
|
-
? input.executionId ?? sourceControlRef?.executionId
|
|
399
|
-
: requestedExecutionId ?? sessionPromptExecutionId(baseRequestDigest);
|
|
400
|
-
if (executionId === undefined) {
|
|
401
|
-
throw new Error("Tangle session replay requires the exact executionId from its control reference");
|
|
402
|
-
}
|
|
403
|
-
const explicitRequestDigest = sessionPromptRequestDigest(input, provider, environmentId, session.id, {
|
|
404
|
-
executionId,
|
|
405
|
-
...(nonce === undefined ? {} : { nonce }),
|
|
406
|
-
});
|
|
407
|
-
if (!replay &&
|
|
408
|
-
requestedControlRef?.requestDigest !== undefined &&
|
|
409
|
-
requestedControlRef.requestDigest !== explicitRequestDigest) {
|
|
410
|
-
throw new Error("Tangle prompt request digest conflicts with the control reference");
|
|
411
|
-
}
|
|
412
|
-
if (replay &&
|
|
413
|
-
hasReplayPayload(input) &&
|
|
414
|
-
sourceControlRef?.requestDigest !== explicitRequestDigest) {
|
|
415
|
-
throw new Error("Tangle prompt request digest conflicts with the control reference");
|
|
416
|
-
}
|
|
417
|
-
const requestDigest = replay
|
|
418
|
-
? sourceControlRef?.requestDigest
|
|
419
|
-
: requestedControlRef?.requestDigest ?? explicitRequestDigest;
|
|
420
|
-
if (requestDigest === undefined) {
|
|
421
|
-
throw new Error("Tangle prompt could not establish an exact request digest");
|
|
422
|
-
}
|
|
423
|
-
const targetControlRef = retainedSessionControlRef(session.id, executionId, provider, environmentId, requestDigest, requestedControlRef?.runId);
|
|
424
|
-
const promptInput = replay
|
|
425
|
-
? {
|
|
426
|
-
...input,
|
|
427
|
-
sessionId: session.id,
|
|
428
|
-
executionId,
|
|
429
|
-
controlRef: sourceControlRef,
|
|
430
|
-
}
|
|
431
|
-
: {
|
|
432
|
-
...input,
|
|
433
|
-
sessionId: session.id,
|
|
434
|
-
executionId,
|
|
435
|
-
controlRef: targetControlRef,
|
|
436
|
-
};
|
|
437
|
-
const prompt = promptFromTurnInput(input);
|
|
438
|
-
const promptOptions = await preparePromptOptions(box, promptOptionsFromTurnInput(promptInput, {
|
|
439
|
-
provider,
|
|
440
|
-
environmentId,
|
|
441
|
-
sessionId: session.id,
|
|
442
|
-
}), provider);
|
|
443
|
-
onExecution?.(executionId);
|
|
444
|
-
try {
|
|
445
|
-
const result = await awaitWithSignal(session.prompt(prompt, promptOptions), input.signal);
|
|
446
|
-
input.signal?.throwIfAborted();
|
|
447
|
-
const resultRecord = validatedSandboxPromptResult(result);
|
|
448
|
-
if (resultRecord.executionId !== executionId) {
|
|
449
|
-
void interruptExecutionAfterAbort(session, session.id, executionId);
|
|
450
|
-
throw new Error("Tangle session prompt did not confirm its exact executionId");
|
|
451
|
-
}
|
|
452
|
-
activeControlRef = targetControlRef;
|
|
453
|
-
return measured(executionId, agentTurnResultFromPromptRecord(resultRecord, {
|
|
454
|
-
sessionId: session.id,
|
|
455
|
-
controlRef: targetControlRef,
|
|
456
|
-
...(input.contextTransfer
|
|
457
|
-
? { contextTransferRequest: input.contextTransfer }
|
|
458
|
-
: {}),
|
|
459
|
-
...(input.contextTransfer
|
|
460
|
-
? { contextTransferRequested: true }
|
|
461
|
-
: {}),
|
|
462
|
-
}));
|
|
463
|
-
}
|
|
464
|
-
catch (error) {
|
|
465
|
-
if (input.signal?.aborted && input.detach !== true) {
|
|
466
|
-
void interruptExecutionAfterAbort(session, session.id, executionId);
|
|
467
|
-
}
|
|
468
|
-
throw error;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
finally {
|
|
472
|
-
promptInFlight = false;
|
|
473
|
-
}
|
|
474
|
-
},
|
|
173
|
+
prompt: sessionPrompt(session, provider, environmentId, dispatch, beforePrompt, onExecution, measured, prepareOptions, () => activeControlRef, (ref) => { activeControlRef = ref; }),
|
|
475
174
|
async cancel(options) {
|
|
476
175
|
assertOptionKeys(options, ["signal"], "Tangle session cancel");
|
|
477
176
|
const executionId = activeControlRef?.executionId;
|
|
@@ -489,3 +188,282 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
489
188
|
};
|
|
490
189
|
return agentSession;
|
|
491
190
|
}
|
|
191
|
+
function nativeSessionContinuation(session, getControlRef, prompt) {
|
|
192
|
+
// Operation identity makes continuation retries idempotent without redispatch.
|
|
193
|
+
const nativeOperations = new Map();
|
|
194
|
+
const currentBoundary = () => {
|
|
195
|
+
const activeControlRef = getControlRef();
|
|
196
|
+
if (activeControlRef === undefined)
|
|
197
|
+
return null;
|
|
198
|
+
return {
|
|
199
|
+
runId: activeControlRef.runId,
|
|
200
|
+
provider: activeControlRef.provider,
|
|
201
|
+
environmentId: activeControlRef.environmentId,
|
|
202
|
+
sessionId: activeControlRef.sessionId,
|
|
203
|
+
executionId: activeControlRef.executionId,
|
|
204
|
+
requestDigest: activeControlRef.requestDigest,
|
|
205
|
+
// A competing turn moves this boundary and refuses the stale continuation.
|
|
206
|
+
boundary: { kind: "revision", revision: activeControlRef.executionId },
|
|
207
|
+
observedAt: new Date().toISOString(),
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
const contextBoundary = async (options) => {
|
|
211
|
+
assertOptionKeys(options, ["signal"], "Tangle native context boundary");
|
|
212
|
+
options?.signal?.throwIfAborted();
|
|
213
|
+
return currentBoundary();
|
|
214
|
+
};
|
|
215
|
+
const continueNative = async (request, continuationOptions) => {
|
|
216
|
+
const exactRequest = NativeContextContinuationRequestSchema.parse(request);
|
|
217
|
+
if (nativeContextContinuationTurnDigest(continuationOptions.turn) !==
|
|
218
|
+
exactRequest.turnDigest) {
|
|
219
|
+
throw new Error("Tangle native continuation turn does not match its request digest");
|
|
220
|
+
}
|
|
221
|
+
if (continuationOptions.onAdmission !== undefined) {
|
|
222
|
+
throw new Error("Tangle native continuation does not advertise early admission control");
|
|
223
|
+
}
|
|
224
|
+
continuationOptions.signal?.throwIfAborted();
|
|
225
|
+
// Check replay before current-run binding: a successful continuation advanced it.
|
|
226
|
+
const prior = nativeOperations.get(exactRequest.operationId);
|
|
227
|
+
if (prior !== undefined) {
|
|
228
|
+
if (prior.requestDigest === exactRequest.requestDigest) {
|
|
229
|
+
return {
|
|
230
|
+
...prior.outcome,
|
|
231
|
+
acknowledgement: { ...prior.outcome.acknowledgement, status: "replayed" },
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
acknowledgement: {
|
|
236
|
+
operationId: exactRequest.operationId,
|
|
237
|
+
requestDigest: exactRequest.requestDigest,
|
|
238
|
+
status: "conflict",
|
|
239
|
+
historyMessagesSent: 0,
|
|
240
|
+
existingRequestDigest: prior.requestDigest,
|
|
241
|
+
},
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const expectedRun = getControlRef();
|
|
245
|
+
if (expectedRun === undefined) {
|
|
246
|
+
throw new Error("Tangle native continuation requires an exact run control reference");
|
|
247
|
+
}
|
|
248
|
+
if (!sameRunControlRef(exactRequest.run, expectedRun)) {
|
|
249
|
+
throw new Error("Tangle native continuation targets another retained run");
|
|
250
|
+
}
|
|
251
|
+
const actual = currentBoundary();
|
|
252
|
+
const expected = exactRequest.expectedBoundary.boundary;
|
|
253
|
+
const boundaryMatches = actual !== null &&
|
|
254
|
+
actual.boundary.kind === "revision" &&
|
|
255
|
+
expected.kind === "revision" &&
|
|
256
|
+
actual.boundary.revision === expected.revision;
|
|
257
|
+
if (!boundaryMatches) {
|
|
258
|
+
return {
|
|
259
|
+
acknowledgement: {
|
|
260
|
+
operationId: exactRequest.operationId,
|
|
261
|
+
requestDigest: exactRequest.requestDigest,
|
|
262
|
+
status: "boundary_mismatch",
|
|
263
|
+
historyMessagesSent: 0,
|
|
264
|
+
...(actual === null ? {} : { actualBoundary: actual }),
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
const turn = continuationOptions.turn;
|
|
269
|
+
// Reuse prompt identity, control binding and usage recording.
|
|
270
|
+
const result = await prompt({
|
|
271
|
+
...(turn.prompt === undefined ? {} : { prompt: turn.prompt }),
|
|
272
|
+
...(turn.parts === undefined ? {} : { parts: turn.parts }),
|
|
273
|
+
...(turn.model === undefined ? {} : { model: turn.model }),
|
|
274
|
+
...(turn.context === undefined ? {} : { context: turn.context }),
|
|
275
|
+
...(turn.providerOptions === undefined
|
|
276
|
+
? {}
|
|
277
|
+
: { providerOptions: turn.providerOptions }),
|
|
278
|
+
sessionId: session.id,
|
|
279
|
+
turnId: exactRequest.operationId,
|
|
280
|
+
...(continuationOptions.timeoutMs === undefined
|
|
281
|
+
? {}
|
|
282
|
+
: { timeoutMs: continuationOptions.timeoutMs }),
|
|
283
|
+
...(continuationOptions.signal === undefined
|
|
284
|
+
? {}
|
|
285
|
+
: { signal: continuationOptions.signal }),
|
|
286
|
+
});
|
|
287
|
+
const controlRef = getControlRef();
|
|
288
|
+
if (controlRef === undefined || sameRunControlRef(controlRef, expectedRun)) {
|
|
289
|
+
throw new Error("Tangle native continuation did not advance the run control reference");
|
|
290
|
+
}
|
|
291
|
+
const outcome = {
|
|
292
|
+
acknowledgement: {
|
|
293
|
+
operationId: exactRequest.operationId,
|
|
294
|
+
requestDigest: exactRequest.requestDigest,
|
|
295
|
+
status: "accepted",
|
|
296
|
+
historyMessagesSent: 0,
|
|
297
|
+
actualBoundary: actual,
|
|
298
|
+
},
|
|
299
|
+
result,
|
|
300
|
+
controlRef: { ...controlRef },
|
|
301
|
+
};
|
|
302
|
+
nativeOperations.set(exactRequest.operationId, {
|
|
303
|
+
requestDigest: exactRequest.requestDigest,
|
|
304
|
+
outcome,
|
|
305
|
+
});
|
|
306
|
+
return outcome;
|
|
307
|
+
};
|
|
308
|
+
return { contextBoundary, continueNative };
|
|
309
|
+
}
|
|
310
|
+
function sessionPrompt(session, provider, environmentId, dispatch, beforePrompt, onExecution, measured, prepareOptions, getControlRef, setControlRef) {
|
|
311
|
+
let promptInFlight = false;
|
|
312
|
+
return async (input) => {
|
|
313
|
+
const activeControlRef = getControlRef();
|
|
314
|
+
AgentTurnInputSchema.parse(input);
|
|
315
|
+
input.signal?.throwIfAborted();
|
|
316
|
+
if (promptInFlight) {
|
|
317
|
+
throw new Error("Tangle session already has a prompt in flight");
|
|
318
|
+
}
|
|
319
|
+
promptInFlight = true;
|
|
320
|
+
try {
|
|
321
|
+
if (input.sessionId !== undefined && input.sessionId !== session.id) {
|
|
322
|
+
throw new Error("Tangle sessionId conflicts with this session");
|
|
323
|
+
}
|
|
324
|
+
input = await beforePrompt?.({ ...input, sessionId: session.id }) ?? input;
|
|
325
|
+
input.signal?.throwIfAborted();
|
|
326
|
+
const requestedControlRef = resolveRetainedSessionControlRef(input.controlRef, session.id, provider, environmentId);
|
|
327
|
+
if (activeControlRef &&
|
|
328
|
+
requestedControlRef &&
|
|
329
|
+
!sameRunControlRef(activeControlRef, requestedControlRef)) {
|
|
330
|
+
throw new Error("Tangle prompt control reference conflicts with this session");
|
|
331
|
+
}
|
|
332
|
+
if (requestedControlRef?.executionId !== undefined &&
|
|
333
|
+
input.executionId !== undefined &&
|
|
334
|
+
requestedControlRef.executionId !== input.executionId) {
|
|
335
|
+
throw new Error("Tangle executionId conflicts with the control reference");
|
|
336
|
+
}
|
|
337
|
+
if (input.detach === true && input.lastEventId === undefined) {
|
|
338
|
+
if (dispatch === undefined) {
|
|
339
|
+
throw new Error("Tangle detached session prompt requires the sandbox dispatch primitive");
|
|
340
|
+
}
|
|
341
|
+
const detachedInput = {
|
|
342
|
+
...input,
|
|
343
|
+
signal: undefined,
|
|
344
|
+
controlRef: undefined,
|
|
345
|
+
sessionId: session.id,
|
|
346
|
+
detach: true,
|
|
347
|
+
...(input.turnId === undefined ? { turnId: randomUUID() } : {}),
|
|
348
|
+
};
|
|
349
|
+
const reference = await dispatch(detachedInput);
|
|
350
|
+
const nextControlRef = resolveRetainedSessionControlRef(reference.controlRef, session.id, provider, environmentId);
|
|
351
|
+
if (nextControlRef === undefined) {
|
|
352
|
+
throw new Error("Tangle detached session dispatch returned no exact control reference");
|
|
353
|
+
}
|
|
354
|
+
// The admission receipt is the durability boundary. Store it before
|
|
355
|
+
// waiting for the result so an aborted caller can reconnect later.
|
|
356
|
+
setControlRef(nextControlRef);
|
|
357
|
+
onExecution?.(nextControlRef.executionId);
|
|
358
|
+
const result = await awaitWithSignal(session.result({
|
|
359
|
+
executionId: nextControlRef.executionId,
|
|
360
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
361
|
+
}), input.signal);
|
|
362
|
+
input.signal?.throwIfAborted();
|
|
363
|
+
const resultRecord = validatedSandboxPromptResult(result);
|
|
364
|
+
if (resultRecord.executionId !== nextControlRef.executionId) {
|
|
365
|
+
throw new Error("Tangle detached session prompt did not confirm its exact executionId");
|
|
366
|
+
}
|
|
367
|
+
return measured(nextControlRef.executionId, agentTurnResultFromPromptRecord(resultRecord, {
|
|
368
|
+
sessionId: session.id,
|
|
369
|
+
controlRef: nextControlRef,
|
|
370
|
+
}));
|
|
371
|
+
}
|
|
372
|
+
const sourceControlRef = requestedControlRef ?? activeControlRef;
|
|
373
|
+
const replay = input.lastEventId !== undefined;
|
|
374
|
+
if (replay &&
|
|
375
|
+
sourceControlRef?.executionId !== undefined &&
|
|
376
|
+
input.executionId !== undefined &&
|
|
377
|
+
input.executionId !== sourceControlRef.executionId) {
|
|
378
|
+
throw new Error("Tangle replay executionId conflicts with the control reference");
|
|
379
|
+
}
|
|
380
|
+
if (replay && sourceControlRef?.requestDigest === undefined) {
|
|
381
|
+
throw new Error("Tangle replay requires an exact request digest from its control reference");
|
|
382
|
+
}
|
|
383
|
+
const requestedExecutionId = input.executionId ?? requestedControlRef?.executionId;
|
|
384
|
+
const nonce = !replay &&
|
|
385
|
+
input.turnId === undefined &&
|
|
386
|
+
requestedExecutionId === undefined &&
|
|
387
|
+
requestedControlRef === undefined
|
|
388
|
+
? randomUUID()
|
|
389
|
+
: undefined;
|
|
390
|
+
const baseRequestDigest = sessionPromptRequestDigest(input, provider, environmentId, session.id, nonce === undefined ? {} : { nonce });
|
|
391
|
+
const executionId = replay
|
|
392
|
+
? input.executionId ?? sourceControlRef?.executionId
|
|
393
|
+
: requestedExecutionId ?? sessionPromptExecutionId(baseRequestDigest);
|
|
394
|
+
if (executionId === undefined) {
|
|
395
|
+
throw new Error("Tangle session replay requires the exact executionId from its control reference");
|
|
396
|
+
}
|
|
397
|
+
const explicitRequestDigest = sessionPromptRequestDigest(input, provider, environmentId, session.id, {
|
|
398
|
+
executionId,
|
|
399
|
+
...(nonce === undefined ? {} : { nonce }),
|
|
400
|
+
});
|
|
401
|
+
if (!replay &&
|
|
402
|
+
requestedControlRef?.requestDigest !== undefined &&
|
|
403
|
+
requestedControlRef.requestDigest !== explicitRequestDigest) {
|
|
404
|
+
throw new Error("Tangle prompt request digest conflicts with the control reference");
|
|
405
|
+
}
|
|
406
|
+
if (replay &&
|
|
407
|
+
hasReplayPayload(input) &&
|
|
408
|
+
sourceControlRef?.requestDigest !== explicitRequestDigest) {
|
|
409
|
+
throw new Error("Tangle prompt request digest conflicts with the control reference");
|
|
410
|
+
}
|
|
411
|
+
const requestDigest = replay
|
|
412
|
+
? sourceControlRef?.requestDigest
|
|
413
|
+
: requestedControlRef?.requestDigest ?? explicitRequestDigest;
|
|
414
|
+
if (requestDigest === undefined) {
|
|
415
|
+
throw new Error("Tangle prompt could not establish an exact request digest");
|
|
416
|
+
}
|
|
417
|
+
const targetControlRef = retainedSessionControlRef(session.id, executionId, provider, environmentId, requestDigest, requestedControlRef?.runId);
|
|
418
|
+
const promptInput = replay
|
|
419
|
+
? {
|
|
420
|
+
...input,
|
|
421
|
+
sessionId: session.id,
|
|
422
|
+
executionId,
|
|
423
|
+
controlRef: sourceControlRef,
|
|
424
|
+
}
|
|
425
|
+
: {
|
|
426
|
+
...input,
|
|
427
|
+
sessionId: session.id,
|
|
428
|
+
executionId,
|
|
429
|
+
controlRef: targetControlRef,
|
|
430
|
+
};
|
|
431
|
+
const prompt = promptFromTurnInput(input);
|
|
432
|
+
const promptOptions = await prepareOptions(promptOptionsFromTurnInput(promptInput, {
|
|
433
|
+
provider,
|
|
434
|
+
environmentId,
|
|
435
|
+
sessionId: session.id,
|
|
436
|
+
}));
|
|
437
|
+
onExecution?.(executionId);
|
|
438
|
+
try {
|
|
439
|
+
const result = await awaitWithSignal(session.prompt(prompt, promptOptions), input.signal);
|
|
440
|
+
input.signal?.throwIfAborted();
|
|
441
|
+
const resultRecord = validatedSandboxPromptResult(result);
|
|
442
|
+
if (resultRecord.executionId !== executionId) {
|
|
443
|
+
void interruptExecutionAfterAbort(session, session.id, executionId);
|
|
444
|
+
throw new Error("Tangle session prompt did not confirm its exact executionId");
|
|
445
|
+
}
|
|
446
|
+
setControlRef(targetControlRef);
|
|
447
|
+
return measured(executionId, agentTurnResultFromPromptRecord(resultRecord, {
|
|
448
|
+
sessionId: session.id,
|
|
449
|
+
controlRef: targetControlRef,
|
|
450
|
+
...(input.contextTransfer
|
|
451
|
+
? { contextTransferRequest: input.contextTransfer }
|
|
452
|
+
: {}),
|
|
453
|
+
...(input.contextTransfer
|
|
454
|
+
? { contextTransferRequested: true }
|
|
455
|
+
: {}),
|
|
456
|
+
}));
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
if (input.signal?.aborted && input.detach !== true) {
|
|
460
|
+
void interruptExecutionAfterAbort(session, session.id, executionId);
|
|
461
|
+
}
|
|
462
|
+
throw error;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
finally {
|
|
466
|
+
promptInFlight = false;
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
}
|
|
@@ -3,29 +3,7 @@ import type { AgentEnvironment, AgentEnvironmentCapabilities, ResourceProfile, P
|
|
|
3
3
|
import type { SandboxClientLike, SandboxInstanceLike, SandboxRuntimeCapabilityDocument, TangleModelCredentialResolver } from "./tangle-types.js";
|
|
4
4
|
import { type ProfileCredentialSource } from "./model-credential-source.js";
|
|
5
5
|
import type { TangleConfidentialAttestationVerifier } from "./tangle-types.js";
|
|
6
|
-
/**
|
|
7
|
-
* Compose one concrete sandbox into an environment.
|
|
8
|
-
*
|
|
9
|
-
* This is the only stage that can read deployment truth, so it does: one
|
|
10
|
-
* `GET /capabilities` against the sandbox decides retained control, and the
|
|
11
|
-
* environment then exposes exactly the operations both the adapter surface
|
|
12
|
-
* and the deployment back. A deployment that cannot disclose a readable
|
|
13
|
-
* document yields no retained-control surface at all. The environment
|
|
14
|
-
* publishes the resulting document on `capabilities`, so a caller reads the
|
|
15
|
-
* answer for this sandbox rather than the provider's pre-sandbox claim.
|
|
16
|
-
*
|
|
17
|
-
* The document is measured once, here. A sandbox that is not yet running
|
|
18
|
-
* cannot answer, so an environment composed during provisioning claims
|
|
19
|
-
* nothing and keeps claiming nothing: the exposed operations and the document
|
|
20
|
-
* are composed together and a caller may already hold either one. This is why
|
|
21
|
-
* `provider.create()` holds until the sandbox is running before it composes
|
|
22
|
-
* (see `tangle-readiness.ts`). `provider.get(id)` composes whatever the
|
|
23
|
-
* sandbox is at that moment, so compose again once a starting sandbox runs.
|
|
24
|
-
*
|
|
25
|
-
* @param request What the create call asked for. An environment rebuilt by id
|
|
26
|
-
* carries none of it, so its observation reports the requested compute shape
|
|
27
|
-
* as absent instead of restating a request it never saw.
|
|
28
|
-
*/
|
|
6
|
+
/** Compose measured sandbox operations; metadata records intent, never grant authority. */
|
|
29
7
|
export declare function sandboxInstanceAsEnvironment(box: SandboxInstanceLike, providerName: string, client: SandboxClientLike, declaredCapabilities: AgentEnvironmentCapabilities, operation?: {
|
|
30
8
|
signal?: AbortSignal;
|
|
31
9
|
}, request?: {
|