@tangle-network/agent-provider-tangle 1.3.2 → 1.5.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/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/tangle-capabilities.js +13 -2
- package/dist/tangle-contract-safety.d.ts +19 -0
- package/dist/tangle-contract-safety.js +25 -1
- package/dist/tangle-environment-session.d.ts +1 -1
- package/dist/tangle-environment-session.js +132 -2
- package/dist/tangle-environment.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export type { TangleExactProcessOptions } from "./tangle-types.js";
|
|
2
|
+
export { JsonBoundError } from "./tangle-contract-safety.js";
|
|
3
|
+
export type { JsonBoundRule, JsonBoundViolation } from "./tangle-contract-safety.js";
|
|
2
4
|
export * from "./tangle-types.js";
|
|
3
5
|
export { createTangleProvider } from "./tangle-provider.js";
|
|
4
6
|
export { DEFAULT_TANGLE_READY_TIMEOUT_MS } from "./tangle-readiness.js";
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,16 @@ export function defaultTangleSandboxCapabilities(harness) {
|
|
|
46
46
|
// together over one Sandbox surface, and the capability schema refuses
|
|
47
47
|
// a partial block, so they stand or fall on the same fact set.
|
|
48
48
|
sessions: { continue: true, list: false, messages: false },
|
|
49
|
+
// Same-session continuation is declared as intent and stripped by
|
|
50
|
+
// narrowing with retained control, because it is built on the same
|
|
51
|
+
// facts: an exact run identity to bind the boundary to, and a session
|
|
52
|
+
// whose prompts the sandbox serializes. The boundary is the executing
|
|
53
|
+
// run's identity, and a competing prompt on the session is refused by
|
|
54
|
+
// the sandbox rather than interleaved, so a check-then-dispatch on one
|
|
55
|
+
// handle cannot be split by another turn. `admissionControl` is not
|
|
56
|
+
// claimed: the adapter learns the continued run's identity from the
|
|
57
|
+
// prompt result, not before it.
|
|
58
|
+
nativeContinuation: { atomicBoundary: true, requestIdempotency: true },
|
|
49
59
|
// Answering an ask is declared as intent and stripped by narrowing unless
|
|
50
60
|
// the session exposes the command route and the deployment discloses its
|
|
51
61
|
// durable response record. Three claims are bounded by what the adapter
|
|
@@ -400,9 +410,10 @@ export function narrowedTangleCapabilities(declared, support, deployment, option
|
|
|
400
410
|
if (!tangleInteractionResponsesSupported(declared, support, deployment)) {
|
|
401
411
|
delete narrowed.interactions;
|
|
402
412
|
}
|
|
403
|
-
|
|
404
|
-
if (!supportsRetainedControl)
|
|
413
|
+
if (!supportsRetainedControl) {
|
|
405
414
|
delete narrowed.retainedControl;
|
|
415
|
+
delete narrowed.nativeContinuation;
|
|
416
|
+
}
|
|
406
417
|
if (narrowed.confidential &&
|
|
407
418
|
(!support.confidentialAttestation ||
|
|
408
419
|
typeof options?.confidentialAttestationVerifier !== "function")) {
|
|
@@ -52,6 +52,25 @@ export declare function describeJsonBoundViolation(violation: JsonBoundViolation
|
|
|
52
52
|
* profile, the metadata, or a providerOptions map — which is how three children settled on five
|
|
53
53
|
* words that identified nothing.
|
|
54
54
|
*/
|
|
55
|
+
/**
|
|
56
|
+
* A value refused by a JSON bound, as an error a consumer can classify without reading text.
|
|
57
|
+
*
|
|
58
|
+
* Every refusal in this package was a plain `Error` with a message, and the runtime that
|
|
59
|
+
* supervises these environments classifies a failed retained execution from the error's
|
|
60
|
+
* STRUCTURE — class name, `code`, status — never its message, because this package's messages
|
|
61
|
+
* are not a contract. So a bound refusal reached its journal under the one name reserved for an
|
|
62
|
+
* execution nobody can observe, telling the operator to reconcile before retrying when nothing
|
|
63
|
+
* had run and nothing needed reconciling (agent-runtime#1204, exhibit 6).
|
|
64
|
+
*
|
|
65
|
+
* `name` and `code` are stable; `message` stays the operator-facing line and may change. The
|
|
66
|
+
* violation is carried whole so a reader can act on the rule and the path without parsing.
|
|
67
|
+
*/
|
|
68
|
+
export declare class JsonBoundError extends Error {
|
|
69
|
+
readonly code: "JSON_BOUND_VIOLATION";
|
|
70
|
+
readonly label: string;
|
|
71
|
+
readonly violation: JsonBoundViolation;
|
|
72
|
+
constructor(label: string, violation: JsonBoundViolation, message: string);
|
|
73
|
+
}
|
|
55
74
|
export declare function assertBoundedJson(value: unknown, label?: string): void;
|
|
56
75
|
/** Keep a late-created provider handle reachable when an abort wins the race. */
|
|
57
76
|
export declare function attachCleanupHandle(error: unknown, handle: unknown, cleanupError?: unknown): void;
|
|
@@ -186,6 +186,30 @@ export function describeJsonBoundViolation(violation) {
|
|
|
186
186
|
* profile, the metadata, or a providerOptions map — which is how three children settled on five
|
|
187
187
|
* words that identified nothing.
|
|
188
188
|
*/
|
|
189
|
+
/**
|
|
190
|
+
* A value refused by a JSON bound, as an error a consumer can classify without reading text.
|
|
191
|
+
*
|
|
192
|
+
* Every refusal in this package was a plain `Error` with a message, and the runtime that
|
|
193
|
+
* supervises these environments classifies a failed retained execution from the error's
|
|
194
|
+
* STRUCTURE — class name, `code`, status — never its message, because this package's messages
|
|
195
|
+
* are not a contract. So a bound refusal reached its journal under the one name reserved for an
|
|
196
|
+
* execution nobody can observe, telling the operator to reconcile before retrying when nothing
|
|
197
|
+
* had run and nothing needed reconciling (agent-runtime#1204, exhibit 6).
|
|
198
|
+
*
|
|
199
|
+
* `name` and `code` are stable; `message` stays the operator-facing line and may change. The
|
|
200
|
+
* violation is carried whole so a reader can act on the rule and the path without parsing.
|
|
201
|
+
*/
|
|
202
|
+
export class JsonBoundError extends Error {
|
|
203
|
+
code = "JSON_BOUND_VIOLATION";
|
|
204
|
+
label;
|
|
205
|
+
violation;
|
|
206
|
+
constructor(label, violation, message) {
|
|
207
|
+
super(message);
|
|
208
|
+
this.name = "JsonBoundError";
|
|
209
|
+
this.label = label;
|
|
210
|
+
this.violation = violation;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
189
213
|
export function assertBoundedJson(value, label = "value") {
|
|
190
214
|
const violation = firstJsonBoundViolation(value);
|
|
191
215
|
if (violation === undefined)
|
|
@@ -194,7 +218,7 @@ export function assertBoundedJson(value, label = "value") {
|
|
|
194
218
|
// oversized, and "exceeds" sends the reader after a size problem that does not exist.
|
|
195
219
|
const verdict = violation.limit === undefined ? "violates" : "exceeds";
|
|
196
220
|
const message = `${label} ${verdict} its JSON bound: ${describeJsonBoundViolation(violation)}`;
|
|
197
|
-
throw new
|
|
221
|
+
throw new JsonBoundError(label, violation, message.length > MAX_BOUND_MESSAGE_LENGTH
|
|
198
222
|
? `${message.slice(0, MAX_BOUND_MESSAGE_LENGTH - 3)}...`
|
|
199
223
|
: message);
|
|
200
224
|
}
|
|
@@ -23,5 +23,5 @@ type ExactExecutionEventStream = (options: {
|
|
|
23
23
|
* environment observation reads it, because Sandbox reports usage per
|
|
24
24
|
* execution result and never for the environment.
|
|
25
25
|
*/
|
|
26
|
-
export declare function sandboxSessionAsAgentSession(session: SandboxSessionLike, controlRef: AgentRunControlRef | undefined, provider: string, environmentId: string, dispatch: ((input: AgentTurnInput) => Promise<AgentSessionRef>) | undefined, exactExecutionEvents: ExactExecutionEventStream | undefined, retainedControl: boolean, interactionResponses: boolean, usageLog?: ExecutionUsageLog): AgentSession;
|
|
26
|
+
export declare function sandboxSessionAsAgentSession(session: SandboxSessionLike, controlRef: AgentRunControlRef | undefined, provider: string, environmentId: string, dispatch: ((input: AgentTurnInput) => Promise<AgentSessionRef>) | undefined, exactExecutionEvents: ExactExecutionEventStream | undefined, retainedControl: boolean, interactionResponses: boolean, usageLog?: ExecutionUsageLog, nativeContinuation?: boolean): AgentSession;
|
|
27
27
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { AgentTurnInputSchema } from "@tangle-network/agent-interface";
|
|
3
3
|
import { AgentRunCancellationAcknowledgementSchema, AgentRunCancellationRequestSchema, agentRunCancellationAcknowledgementMatchesRequest, } from "@tangle-network/agent-interface";
|
|
4
|
+
import { NativeContextContinuationRequestSchema, nativeContextContinuationTurnDigest, } from "@tangle-network/agent-interface";
|
|
4
5
|
import { createStepUsageFold } from "./tangle-step-usage.js";
|
|
5
6
|
import { carriedSessionIds, environmentEventFromSandboxEvent, isSandboxConnectionMarker, sandboxEventIdentity, } from "./tangle-events.js";
|
|
6
7
|
import { agentTurnResultFromPromptRecord, promptFromTurnInput, promptOptionsFromTurnInput, validatedSandboxPromptResult, } from "./tangle-prompt.js";
|
|
@@ -23,7 +24,7 @@ import { hasReplayPayload, interruptExecutionAfterAbort, sessionPromptRequestDig
|
|
|
23
24
|
* environment observation reads it, because Sandbox reports usage per
|
|
24
25
|
* execution result and never for the environment.
|
|
25
26
|
*/
|
|
26
|
-
export function sandboxSessionAsAgentSession(session, controlRef, provider, environmentId, dispatch, exactExecutionEvents, retainedControl, interactionResponses, usageLog) {
|
|
27
|
+
export function sandboxSessionAsAgentSession(session, controlRef, provider, environmentId, dispatch, exactExecutionEvents, retainedControl, interactionResponses, usageLog, nativeContinuation = false) {
|
|
27
28
|
const measured = (executionId, result) => {
|
|
28
29
|
usageLog?.record(executionId, result.usage);
|
|
29
30
|
return result;
|
|
@@ -62,7 +63,134 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
62
63
|
return exactAcknowledgement;
|
|
63
64
|
}
|
|
64
65
|
: undefined;
|
|
65
|
-
|
|
66
|
+
// One record per continuation operation. The sandbox already caches a
|
|
67
|
+
// prompt by its turnId, and continueNative sets turnId to the operationId,
|
|
68
|
+
// so a retry after a lost response replays the same dispatch on the wire.
|
|
69
|
+
// This map lets the retry return the same acknowledgement and result with
|
|
70
|
+
// no dispatch at all, and turns a changed turn under a reused operationId
|
|
71
|
+
// into a conflict instead of a second run.
|
|
72
|
+
const nativeOperations = new Map();
|
|
73
|
+
const currentBoundary = () => {
|
|
74
|
+
if (activeControlRef === undefined)
|
|
75
|
+
return null;
|
|
76
|
+
return {
|
|
77
|
+
runId: activeControlRef.runId,
|
|
78
|
+
provider: activeControlRef.provider,
|
|
79
|
+
environmentId: activeControlRef.environmentId,
|
|
80
|
+
sessionId: activeControlRef.sessionId,
|
|
81
|
+
executionId: activeControlRef.executionId,
|
|
82
|
+
requestDigest: activeControlRef.requestDigest,
|
|
83
|
+
// The conversation's boundary is the run that last extended it. A
|
|
84
|
+
// continuation is verified against exactly that run, so a turn that
|
|
85
|
+
// landed in between moves the boundary and the continuation is refused
|
|
86
|
+
// rather than appended to a conversation the caller has not seen.
|
|
87
|
+
boundary: { kind: "revision", revision: activeControlRef.executionId },
|
|
88
|
+
observedAt: new Date().toISOString(),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
const contextBoundary = async (options) => {
|
|
92
|
+
assertOptionKeys(options, ["signal"], "Tangle native context boundary");
|
|
93
|
+
options?.signal?.throwIfAborted();
|
|
94
|
+
return currentBoundary();
|
|
95
|
+
};
|
|
96
|
+
const continueNative = async (request, continuationOptions) => {
|
|
97
|
+
const exactRequest = NativeContextContinuationRequestSchema.parse(request);
|
|
98
|
+
if (nativeContextContinuationTurnDigest(continuationOptions.turn) !==
|
|
99
|
+
exactRequest.turnDigest) {
|
|
100
|
+
throw new Error("Tangle native continuation turn does not match its request digest");
|
|
101
|
+
}
|
|
102
|
+
if (continuationOptions.onAdmission !== undefined) {
|
|
103
|
+
throw new Error("Tangle native continuation does not advertise early admission control");
|
|
104
|
+
}
|
|
105
|
+
continuationOptions.signal?.throwIfAborted();
|
|
106
|
+
// A retry names the run the conversation had BEFORE the continuation it
|
|
107
|
+
// is retrying, so the operation record is consulted before the request is
|
|
108
|
+
// bound to the current run; a replay must answer even though the run has
|
|
109
|
+
// since advanced past it.
|
|
110
|
+
const prior = nativeOperations.get(exactRequest.operationId);
|
|
111
|
+
if (prior !== undefined) {
|
|
112
|
+
if (prior.requestDigest === exactRequest.requestDigest) {
|
|
113
|
+
return {
|
|
114
|
+
...prior.outcome,
|
|
115
|
+
acknowledgement: { ...prior.outcome.acknowledgement, status: "replayed" },
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
acknowledgement: {
|
|
120
|
+
operationId: exactRequest.operationId,
|
|
121
|
+
requestDigest: exactRequest.requestDigest,
|
|
122
|
+
status: "conflict",
|
|
123
|
+
historyMessagesSent: 0,
|
|
124
|
+
existingRequestDigest: prior.requestDigest,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const expectedRun = activeControlRef;
|
|
129
|
+
if (expectedRun === undefined) {
|
|
130
|
+
throw new Error("Tangle native continuation requires an exact run control reference");
|
|
131
|
+
}
|
|
132
|
+
if (!sameRunControlRef(exactRequest.run, expectedRun)) {
|
|
133
|
+
throw new Error("Tangle native continuation targets another retained run");
|
|
134
|
+
}
|
|
135
|
+
const actual = currentBoundary();
|
|
136
|
+
const expected = exactRequest.expectedBoundary.boundary;
|
|
137
|
+
const boundaryMatches = actual !== null &&
|
|
138
|
+
actual.boundary.kind === "revision" &&
|
|
139
|
+
expected.kind === "revision" &&
|
|
140
|
+
actual.boundary.revision === expected.revision;
|
|
141
|
+
if (!boundaryMatches) {
|
|
142
|
+
return {
|
|
143
|
+
acknowledgement: {
|
|
144
|
+
operationId: exactRequest.operationId,
|
|
145
|
+
requestDigest: exactRequest.requestDigest,
|
|
146
|
+
status: "boundary_mismatch",
|
|
147
|
+
historyMessagesSent: 0,
|
|
148
|
+
...(actual === null ? {} : { actualBoundary: actual }),
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const turn = continuationOptions.turn;
|
|
153
|
+
// The adapter's own prompt path mints the continued run's identity from
|
|
154
|
+
// the turn and its turnId, binds the control reference, and records usage.
|
|
155
|
+
const result = await agentSession.prompt({
|
|
156
|
+
...(turn.prompt === undefined ? {} : { prompt: turn.prompt }),
|
|
157
|
+
...(turn.parts === undefined ? {} : { parts: turn.parts }),
|
|
158
|
+
...(turn.model === undefined ? {} : { model: turn.model }),
|
|
159
|
+
...(turn.context === undefined ? {} : { context: turn.context }),
|
|
160
|
+
...(turn.providerOptions === undefined
|
|
161
|
+
? {}
|
|
162
|
+
: { providerOptions: turn.providerOptions }),
|
|
163
|
+
sessionId: session.id,
|
|
164
|
+
turnId: exactRequest.operationId,
|
|
165
|
+
...(continuationOptions.timeoutMs === undefined
|
|
166
|
+
? {}
|
|
167
|
+
: { timeoutMs: continuationOptions.timeoutMs }),
|
|
168
|
+
...(continuationOptions.signal === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: { signal: continuationOptions.signal }),
|
|
171
|
+
});
|
|
172
|
+
const controlRef = activeControlRef;
|
|
173
|
+
if (controlRef === undefined || sameRunControlRef(controlRef, expectedRun)) {
|
|
174
|
+
throw new Error("Tangle native continuation did not advance the run control reference");
|
|
175
|
+
}
|
|
176
|
+
const outcome = {
|
|
177
|
+
acknowledgement: {
|
|
178
|
+
operationId: exactRequest.operationId,
|
|
179
|
+
requestDigest: exactRequest.requestDigest,
|
|
180
|
+
status: "accepted",
|
|
181
|
+
historyMessagesSent: 0,
|
|
182
|
+
actualBoundary: actual,
|
|
183
|
+
},
|
|
184
|
+
result,
|
|
185
|
+
controlRef: { ...controlRef },
|
|
186
|
+
};
|
|
187
|
+
nativeOperations.set(exactRequest.operationId, {
|
|
188
|
+
requestDigest: exactRequest.requestDigest,
|
|
189
|
+
outcome,
|
|
190
|
+
});
|
|
191
|
+
return outcome;
|
|
192
|
+
};
|
|
193
|
+
const agentSession = {
|
|
66
194
|
id: session.id,
|
|
67
195
|
get controlRef() {
|
|
68
196
|
return activeControlRef === undefined ? undefined : { ...activeControlRef };
|
|
@@ -350,5 +478,7 @@ export function sandboxSessionAsAgentSession(session, controlRef, provider, envi
|
|
|
350
478
|
},
|
|
351
479
|
...(cancelRun ? { cancelRun } : {}),
|
|
352
480
|
...(respondToInteraction ? { respondToInteraction } : {}),
|
|
481
|
+
...(nativeContinuation ? { contextBoundary, continueNative } : {}),
|
|
353
482
|
};
|
|
483
|
+
return agentSession;
|
|
354
484
|
}
|
|
@@ -175,7 +175,7 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
175
175
|
if (session.id !== id) {
|
|
176
176
|
throw new Error("sandbox session(id) returned an unrelated session");
|
|
177
177
|
}
|
|
178
|
-
const agentSession = sandboxSessionAsAgentSession(session, resolveRetainedSessionControlRef(options?.controlRef, id, providerName, environmentId), providerName, environmentId, dispatch, exactExecutionEvents, retainedControl, interactionResponses, usageLog);
|
|
178
|
+
const agentSession = sandboxSessionAsAgentSession(session, resolveRetainedSessionControlRef(options?.controlRef, id, providerName, environmentId), providerName, environmentId, dispatch, exactExecutionEvents, retainedControl, interactionResponses, usageLog, capabilities.nativeContinuation !== undefined);
|
|
179
179
|
// sessions.continue was granted from the probe session and the
|
|
180
180
|
// deployment document together; this backstop holds every
|
|
181
181
|
// concrete session to that grant, so a client whose sessions
|