@tangle-network/agent-provider-tangle 1.7.0 → 1.8.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 +18 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/tangle-environment.js +15 -2
- package/dist/tangle-evidence.d.ts +164 -0
- package/dist/tangle-evidence.js +548 -0
- package/dist/tangle-types.d.ts +121 -0
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -452,3 +452,21 @@ const environment = await provider.exactProcess!.create({
|
|
|
452
452
|
```
|
|
453
453
|
|
|
454
454
|
The adapter rejects ordinary sandboxes during create, recovery, and list operations.
|
|
455
|
+
|
|
456
|
+
## Private session evidence
|
|
457
|
+
|
|
458
|
+
Call `readTangleEvidenceCapabilities(client)` before creating a sandbox when complete evidence is required.
|
|
459
|
+
It accepts only a deployment that attests workspace capture, generic native session capture, and the next-create sidecar image digest.
|
|
460
|
+
Apply `assertTangleEvidenceProfileCapability(proof, profile)` to the exact profile in each create hook.
|
|
461
|
+
An unavailable or unproven capability fails before the sandbox spends resources.
|
|
462
|
+
|
|
463
|
+
Call `captureTangleEnvironmentEvidence(environment, options)` before destroying a provider environment.
|
|
464
|
+
For a Runtime-owned box, call `captureTangleSandboxEvidence(box, options)` with exact session and execution IDs.
|
|
465
|
+
Set `workspaceRoot` to a canonical relative worker directory when the box is shared.
|
|
466
|
+
Both functions return private binary files and provenance for the workspace, native session roots, process streams, and transport replay.
|
|
467
|
+
The caller must store those bytes in private durable custody before deleting the box.
|
|
468
|
+
Credential exclusions remain listed with path, type, owner IDs, timestamps, mode, size, and reason.
|
|
469
|
+
Each process stream has exact frame order and a terminal receipt.
|
|
470
|
+
The capture records the running sidecar image digest and the served Sidecar bundle revision.
|
|
471
|
+
The image digest can be compared with the pre-create proof only when that proof identifies the actual placement image.
|
|
472
|
+
An incomplete inventory, missing stream, unrelated backend, or mismatched digest does not produce complete evidence.
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export { JsonBoundError, MAX_INLINE_PAYLOAD_BYTES, MAX_PAYLOAD_STRING_BYTES, MAX
|
|
|
3
3
|
export type { JsonBoundRule, JsonBoundViolation } from "./tangle-contract-safety.js";
|
|
4
4
|
export * from "./tangle-types.js";
|
|
5
5
|
export { createTangleProvider } from "./tangle-provider.js";
|
|
6
|
+
export { assertTangleEvidenceCapability, assertTangleEvidenceProfileCapability, readTangleEvidenceCapabilities, captureTangleEnvironmentEvidence, captureTangleSandboxEvidence } from "./tangle-evidence.js";
|
|
7
|
+
export type { TangleEvidenceCapabilities, TangleEnvironmentEvidence, TangleEnvironmentEvidenceOptions, TangleSandboxEvidenceOptions } from "./tangle-evidence.js";
|
|
6
8
|
export { DEFAULT_TANGLE_READY_TIMEOUT_MS } from "./tangle-readiness.js";
|
|
7
9
|
export { defaultTangleSandboxCapabilities } from "./tangle-capabilities.js";
|
|
8
10
|
export { createTangleWorkspaceBranching, supportsWorkspaceBranching, } from "./tangle-workspace-branching.js";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { JsonBoundError, MAX_INLINE_PAYLOAD_BYTES, MAX_PAYLOAD_STRING_BYTES, MAX_TOTAL_PAYLOAD_BYTES, } from "./tangle-contract-safety.js";
|
|
2
2
|
export * from "./tangle-types.js";
|
|
3
3
|
export { createTangleProvider } from "./tangle-provider.js";
|
|
4
|
+
export { assertTangleEvidenceCapability, assertTangleEvidenceProfileCapability, readTangleEvidenceCapabilities, captureTangleEnvironmentEvidence, captureTangleSandboxEvidence } from "./tangle-evidence.js";
|
|
4
5
|
export { DEFAULT_TANGLE_READY_TIMEOUT_MS } from "./tangle-readiness.js";
|
|
5
6
|
export { defaultTangleSandboxCapabilities } from "./tangle-capabilities.js";
|
|
6
7
|
export { createTangleWorkspaceBranching, supportsWorkspaceBranching, } from "./tangle-workspace-branching.js";
|
|
@@ -19,6 +19,7 @@ import { observeTangleEnvironment } from "./tangle-observation.js";
|
|
|
19
19
|
import { createTangleTerminalRegistry } from "./tangle-terminal.js";
|
|
20
20
|
import { createTangleInteractiveAgentRegistry } from "./tangle-interactive.js";
|
|
21
21
|
import { confidentialVerifierOption, createTangleWorkspaceBranching, } from "./tangle-workspace-branching.js";
|
|
22
|
+
import { bindTangleEvidenceEnvironment, noteTangleSession } from "./tangle-evidence.js";
|
|
22
23
|
/**
|
|
23
24
|
* Compose one concrete sandbox into an environment.
|
|
24
25
|
*
|
|
@@ -94,7 +95,7 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
94
95
|
...(options.controlRef ? { runControlRef: options.controlRef } : {}),
|
|
95
96
|
});
|
|
96
97
|
const creation = creationFromSandboxCreateReceipt(box.createReceipt?.());
|
|
97
|
-
|
|
98
|
+
const environment = {
|
|
98
99
|
id: environmentId,
|
|
99
100
|
provider: providerName,
|
|
100
101
|
...(creation === undefined ? {} : { creation }),
|
|
@@ -114,6 +115,8 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
114
115
|
input.signal?.throwIfAborted();
|
|
115
116
|
const expectedExecutionId = executionIdFromTurnInput(input);
|
|
116
117
|
const expectedSessionId = input.sessionId ?? input.controlRef?.sessionId;
|
|
118
|
+
if (expectedSessionId)
|
|
119
|
+
noteTangleSession(environment, expectedSessionId, expectedExecutionId);
|
|
117
120
|
// A turn resumed after a cursor starts past its first steps, so it cannot
|
|
118
121
|
// report the execution's running total.
|
|
119
122
|
const stepUsage = input.lastEventId === undefined ? createStepUsageFold() : undefined;
|
|
@@ -130,6 +133,9 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
130
133
|
break;
|
|
131
134
|
}
|
|
132
135
|
input.signal?.throwIfAborted();
|
|
136
|
+
if (typeof next.value.data?.runtimeSessionId === "string") {
|
|
137
|
+
noteTangleSession(environment, next.value.data.runtimeSessionId, expectedExecutionId);
|
|
138
|
+
}
|
|
133
139
|
const converted = environmentEventFromSandboxEvent(next.value, {
|
|
134
140
|
executionId: expectedExecutionId,
|
|
135
141
|
sessionId: expectedSessionId,
|
|
@@ -159,12 +165,17 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
159
165
|
}
|
|
160
166
|
input.signal?.throwIfAborted();
|
|
161
167
|
},
|
|
162
|
-
...(dispatch ? { dispatch
|
|
168
|
+
...(dispatch ? { async dispatch(input) {
|
|
169
|
+
const ref = await dispatch(input);
|
|
170
|
+
noteTangleSession(environment, ref.id, input.executionId ?? input.controlRef?.executionId ?? ref.controlRef?.executionId);
|
|
171
|
+
return ref;
|
|
172
|
+
} } : {}),
|
|
163
173
|
...((capabilities.sessions.continue || capabilities.streaming.replay || capabilities.streaming.detach) &&
|
|
164
174
|
box.session
|
|
165
175
|
? {
|
|
166
176
|
session(id, options) {
|
|
167
177
|
boundedIdentifier(id, "Tangle session id");
|
|
178
|
+
noteTangleSession(environment, id);
|
|
168
179
|
assertOptionKeys(options, ["controlRef", "signal"], "Tangle session");
|
|
169
180
|
options?.signal?.throwIfAborted();
|
|
170
181
|
const session = box.session?.(id, options?.signal ? { signal: options.signal } : undefined);
|
|
@@ -347,6 +358,8 @@ export async function sandboxInstanceAsEnvironment(box, providerName, client, de
|
|
|
347
358
|
}
|
|
348
359
|
: {}),
|
|
349
360
|
};
|
|
361
|
+
bindTangleEvidenceEnvironment(environment, box);
|
|
362
|
+
return environment;
|
|
350
363
|
}
|
|
351
364
|
function snapshotMetadata(metadata) {
|
|
352
365
|
return deepFreeze(structuredClone(metadata));
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { AgentProfile } from "@tangle-network/agent-interface";
|
|
2
|
+
import type { AgentEnvironment } from "@tangle-network/agent-interface/environment-provider";
|
|
3
|
+
import type { SandboxClientLike, SandboxInstanceLike } from "./tangle-types.js";
|
|
4
|
+
export interface TangleEvidenceCapabilities {
|
|
5
|
+
readonly workspaceCaptureV1: true;
|
|
6
|
+
readonly nativeSessionCaptureV1: true;
|
|
7
|
+
readonly sidecarImageDigest: string;
|
|
8
|
+
}
|
|
9
|
+
/** Read a deployment guarantee once, before Runtime starts creating environments. */
|
|
10
|
+
export declare function readTangleEvidenceCapabilities(client: SandboxClientLike): Promise<TangleEvidenceCapabilities>;
|
|
11
|
+
/** The generic guarantee covers the profile's exact backend without a provider-side harness list. */
|
|
12
|
+
export declare function assertTangleEvidenceProfileCapability(document: TangleEvidenceCapabilities, profile: AgentProfile): void;
|
|
13
|
+
/** Refuse unsupported or unproven native trace retention before Sandbox create. */
|
|
14
|
+
export declare function assertTangleEvidenceCapability(client: SandboxClientLike, profile: AgentProfile): Promise<void>;
|
|
15
|
+
interface WorkspaceEntryMetadata {
|
|
16
|
+
path: string;
|
|
17
|
+
type: "file" | "directory" | "symlink";
|
|
18
|
+
sizeBytes: number;
|
|
19
|
+
mode: number;
|
|
20
|
+
owner: string | null;
|
|
21
|
+
group: string | null;
|
|
22
|
+
modifiedAt: string | null;
|
|
23
|
+
accessedAt: string | null;
|
|
24
|
+
/** The Sandbox file listing does not disclose a symbolic link target. */
|
|
25
|
+
symlinkTarget: null;
|
|
26
|
+
}
|
|
27
|
+
export interface TangleEnvironmentEvidenceOptions {
|
|
28
|
+
executionId: string;
|
|
29
|
+
harness: NonNullable<AgentProfile["harness"]>;
|
|
30
|
+
/** Optional next-create proof to compare with the running sidecar image. */
|
|
31
|
+
expectedSidecarImageDigest?: string;
|
|
32
|
+
/** Exact Sandbox session id when execution was reattached after provider restart. */
|
|
33
|
+
sandboxSessionId?: string | null;
|
|
34
|
+
maxBytes: number;
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
}
|
|
37
|
+
export interface TangleSandboxEvidenceOptions extends Omit<TangleEnvironmentEvidenceOptions, "sandboxSessionId"> {
|
|
38
|
+
sandboxSessionIds: readonly string[];
|
|
39
|
+
/** Every observed execution on each retained Sandbox session. */
|
|
40
|
+
sessionExecutionIds?: Readonly<Record<string, readonly string[]>>;
|
|
41
|
+
/** Exact relative worker directory. Defaults to the Sandbox workspace root. */
|
|
42
|
+
workspaceRoot?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface TangleEnvironmentEvidence {
|
|
45
|
+
files: Array<{
|
|
46
|
+
path: string;
|
|
47
|
+
bytes: Uint8Array;
|
|
48
|
+
mode: number;
|
|
49
|
+
}>;
|
|
50
|
+
provenance: {
|
|
51
|
+
provider: string;
|
|
52
|
+
environmentId: string;
|
|
53
|
+
executionId: string;
|
|
54
|
+
workspaceScope: "environment";
|
|
55
|
+
workspaceRoot: string;
|
|
56
|
+
capturedAt: string;
|
|
57
|
+
entries: WorkspaceEntryMetadata[];
|
|
58
|
+
excludedPaths: Array<WorkspaceEntryMetadata & {
|
|
59
|
+
reason: "credential-path" | "symlink" | "runtime-owned";
|
|
60
|
+
}>;
|
|
61
|
+
workspace: {
|
|
62
|
+
scannedFiles: number;
|
|
63
|
+
scannedDirectories: number;
|
|
64
|
+
reportedFiles: number;
|
|
65
|
+
reportedDirectories: number;
|
|
66
|
+
complete: true;
|
|
67
|
+
};
|
|
68
|
+
sessions: Array<{
|
|
69
|
+
id: string;
|
|
70
|
+
executionId: string;
|
|
71
|
+
executionIds: string[];
|
|
72
|
+
eventCountsByExecutionId: Record<string, number>;
|
|
73
|
+
backendType: string;
|
|
74
|
+
sidecarImageDigest: string | null;
|
|
75
|
+
sidecarBundleRevision: string | null;
|
|
76
|
+
transportEvents: "complete" | "unavailable";
|
|
77
|
+
eventCount: number;
|
|
78
|
+
messageCount: number;
|
|
79
|
+
messageScope: "session";
|
|
80
|
+
nativeSessionId: string | null;
|
|
81
|
+
nativeReason: string | null;
|
|
82
|
+
nativeStore: {
|
|
83
|
+
scope: "session";
|
|
84
|
+
roots: Array<{
|
|
85
|
+
scope: "session-home" | "workspace-session";
|
|
86
|
+
path: string;
|
|
87
|
+
}>;
|
|
88
|
+
inventory: {
|
|
89
|
+
scannedFiles: number;
|
|
90
|
+
reportedFiles: number;
|
|
91
|
+
excludedFiles: number;
|
|
92
|
+
scannedDirectories: number;
|
|
93
|
+
reportedDirectories: number;
|
|
94
|
+
excludedDirectories: number;
|
|
95
|
+
scannedSymlinks: number;
|
|
96
|
+
reportedSymlinks: number;
|
|
97
|
+
excludedSymlinks: number;
|
|
98
|
+
skippedEntries: 0;
|
|
99
|
+
} | null;
|
|
100
|
+
complete: boolean;
|
|
101
|
+
entries: Array<{
|
|
102
|
+
rootScope: "session-home" | "workspace-session";
|
|
103
|
+
path: string;
|
|
104
|
+
kind: "file" | "directory" | "symlink";
|
|
105
|
+
mode: number;
|
|
106
|
+
uid: number;
|
|
107
|
+
gid: number;
|
|
108
|
+
mtimeMs: number;
|
|
109
|
+
ctimeMs: number;
|
|
110
|
+
sizeBytes: number;
|
|
111
|
+
sha256: string | null;
|
|
112
|
+
linkTarget: string | null;
|
|
113
|
+
}>;
|
|
114
|
+
excludedPaths: Array<{
|
|
115
|
+
rootScope: "session-home" | "workspace-session";
|
|
116
|
+
path: string;
|
|
117
|
+
kind: "file" | "directory" | "symlink";
|
|
118
|
+
mode: number;
|
|
119
|
+
uid: number;
|
|
120
|
+
gid: number;
|
|
121
|
+
mtimeMs: number;
|
|
122
|
+
ctimeMs: number;
|
|
123
|
+
sizeBytes: number;
|
|
124
|
+
reason: "credential";
|
|
125
|
+
}>;
|
|
126
|
+
};
|
|
127
|
+
processStreams: {
|
|
128
|
+
complete: boolean;
|
|
129
|
+
streamCount: number;
|
|
130
|
+
processCount: number;
|
|
131
|
+
terminalCount: number;
|
|
132
|
+
stdinBytes: number;
|
|
133
|
+
stdoutBytes: number;
|
|
134
|
+
stderrBytes: number;
|
|
135
|
+
protocolBytes: number;
|
|
136
|
+
terminals: Array<{
|
|
137
|
+
processId: string;
|
|
138
|
+
sequence: number;
|
|
139
|
+
at: string;
|
|
140
|
+
result: {
|
|
141
|
+
code: number | null;
|
|
142
|
+
signal: string | null;
|
|
143
|
+
timedOut: boolean;
|
|
144
|
+
timeoutReason: string | null;
|
|
145
|
+
captureError: null;
|
|
146
|
+
spawnError?: string;
|
|
147
|
+
};
|
|
148
|
+
}>;
|
|
149
|
+
};
|
|
150
|
+
nativeEvents: {
|
|
151
|
+
complete: boolean;
|
|
152
|
+
count: number;
|
|
153
|
+
};
|
|
154
|
+
}>;
|
|
155
|
+
missing: string[];
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
export declare function bindTangleEvidenceEnvironment(environment: AgentEnvironment, box: SandboxInstanceLike): void;
|
|
159
|
+
export declare function noteTangleSession(environment: AgentEnvironment, sessionId: string, executionId?: string): void;
|
|
160
|
+
/** Export an environment through its retained Sandbox handle. */
|
|
161
|
+
export declare function captureTangleEnvironmentEvidence(environment: AgentEnvironment, options: TangleEnvironmentEvidenceOptions): Promise<TangleEnvironmentEvidence>;
|
|
162
|
+
/** Export exact Sandbox session and workspace evidence before its owning box is deleted. */
|
|
163
|
+
export declare function captureTangleSandboxEvidence(box: SandboxInstanceLike, options: TangleSandboxEvidenceOptions): Promise<TangleEnvironmentEvidence>;
|
|
164
|
+
export {};
|
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { parseBackendType } from "@tangle-network/sandbox";
|
|
3
|
+
const handles = new WeakMap();
|
|
4
|
+
const blockedNames = new Set([".ssh", ".config", ".claude", ".codex", ".opencode", ".env", ".env.local", ".npmrc", ".sidecar"]);
|
|
5
|
+
const MAX_ENTRIES = 100_000;
|
|
6
|
+
const MAX_EVENTS = 100_000;
|
|
7
|
+
const verifiedCapabilities = new WeakSet();
|
|
8
|
+
/** Read a deployment guarantee once, before Runtime starts creating environments. */
|
|
9
|
+
export async function readTangleEvidenceCapabilities(client) {
|
|
10
|
+
if (typeof client.evidenceCapabilities !== "function")
|
|
11
|
+
throw new Error("Tangle deployment has no pre-create evidence capability query");
|
|
12
|
+
const document = await client.evidenceCapabilities();
|
|
13
|
+
if (document?.workspaceCaptureV1 !== true || document.nativeSessionCaptureV1 !== true ||
|
|
14
|
+
!/^sha256:[0-9a-f]{64}$/.test(document.sidecarImageDigest ?? "")) {
|
|
15
|
+
throw new Error("Tangle deployment has not proven complete workspace and native session capture for next-create placement");
|
|
16
|
+
}
|
|
17
|
+
const verified = Object.freeze({
|
|
18
|
+
workspaceCaptureV1: true,
|
|
19
|
+
nativeSessionCaptureV1: true,
|
|
20
|
+
sidecarImageDigest: document.sidecarImageDigest,
|
|
21
|
+
});
|
|
22
|
+
verifiedCapabilities.add(verified);
|
|
23
|
+
return verified;
|
|
24
|
+
}
|
|
25
|
+
/** The generic guarantee covers the profile's exact backend without a provider-side harness list. */
|
|
26
|
+
export function assertTangleEvidenceProfileCapability(document, profile) {
|
|
27
|
+
if (!verifiedCapabilities.has(document))
|
|
28
|
+
throw new Error("Tangle evidence capability was not read from the deployment");
|
|
29
|
+
if (!profile.harness)
|
|
30
|
+
throw new Error("Tangle complete native session capture requires an exact profile harness");
|
|
31
|
+
parseBackendType(profile.harness);
|
|
32
|
+
}
|
|
33
|
+
/** Refuse unsupported or unproven native trace retention before Sandbox create. */
|
|
34
|
+
export async function assertTangleEvidenceCapability(client, profile) {
|
|
35
|
+
const document = await readTangleEvidenceCapabilities(client);
|
|
36
|
+
assertTangleEvidenceProfileCapability(document, profile);
|
|
37
|
+
}
|
|
38
|
+
export function bindTangleEvidenceEnvironment(environment, box) {
|
|
39
|
+
handles.set(environment, { box, sessions: new Map() });
|
|
40
|
+
}
|
|
41
|
+
export function noteTangleSession(environment, sessionId, executionId) {
|
|
42
|
+
const state = handles.get(environment);
|
|
43
|
+
if (!state || !executionId || !safeIdentifier(sessionId) || !safeIdentifier(executionId))
|
|
44
|
+
return;
|
|
45
|
+
const executions = state.sessions.get(sessionId) ?? new Set();
|
|
46
|
+
executions.add(executionId);
|
|
47
|
+
state.sessions.set(sessionId, executions);
|
|
48
|
+
}
|
|
49
|
+
/** Export an environment through its retained Sandbox handle. */
|
|
50
|
+
export async function captureTangleEnvironmentEvidence(environment, options) {
|
|
51
|
+
const state = handles.get(environment);
|
|
52
|
+
if (!state || state.box.id !== environment.id)
|
|
53
|
+
throw new Error("Tangle evidence requires a live provider environment handle");
|
|
54
|
+
const sessionIds = new Set([...state.sessions.entries()]
|
|
55
|
+
.filter(([, executions]) => executions.has(options.executionId))
|
|
56
|
+
.map(([id]) => id));
|
|
57
|
+
if (options.sandboxSessionId != null)
|
|
58
|
+
sessionIds.add(options.sandboxSessionId);
|
|
59
|
+
return captureTangleSandboxEvidence(state.box, {
|
|
60
|
+
executionId: options.executionId,
|
|
61
|
+
harness: options.harness,
|
|
62
|
+
expectedSidecarImageDigest: options.expectedSidecarImageDigest,
|
|
63
|
+
sandboxSessionIds: [...sessionIds],
|
|
64
|
+
sessionExecutionIds: Object.fromEntries([...sessionIds].filter((id) => state.sessions.has(id))
|
|
65
|
+
.map((id) => [id, [...state.sessions.get(id)]])),
|
|
66
|
+
maxBytes: options.maxBytes,
|
|
67
|
+
signal: options.signal,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** Export exact Sandbox session and workspace evidence before its owning box is deleted. */
|
|
71
|
+
export async function captureTangleSandboxEvidence(box, options) {
|
|
72
|
+
if (!safeIdentifier(box.id) || !safeIdentifier(options.executionId))
|
|
73
|
+
throw new Error("Tangle evidence requires exact box and execution ids");
|
|
74
|
+
if (!safeIdentifier(options.harness))
|
|
75
|
+
throw new Error("Tangle evidence requires an exact profile harness");
|
|
76
|
+
if (options.expectedSidecarImageDigest !== undefined &&
|
|
77
|
+
!/^sha256:[0-9a-f]{64}$/.test(options.expectedSidecarImageDigest)) {
|
|
78
|
+
throw new Error("Tangle evidence expected sidecar digest is invalid");
|
|
79
|
+
}
|
|
80
|
+
if (!Array.isArray(options.sandboxSessionIds) || options.sandboxSessionIds.some((id) => !safeIdentifier(id))) {
|
|
81
|
+
throw new Error("Tangle evidence Sandbox session ids are invalid");
|
|
82
|
+
}
|
|
83
|
+
if (!Number.isSafeInteger(options.maxBytes) || options.maxBytes < 1)
|
|
84
|
+
throw new Error("Tangle evidence maxBytes must be positive");
|
|
85
|
+
if (options.sessionExecutionIds && Object.keys(options.sessionExecutionIds).some((id) => !options.sandboxSessionIds.includes(id))) {
|
|
86
|
+
throw new Error("Tangle evidence execution mapping names an unrelated Sandbox session");
|
|
87
|
+
}
|
|
88
|
+
const workspaceRoot = options.workspaceRoot ?? ".";
|
|
89
|
+
if (workspaceRoot !== ".")
|
|
90
|
+
canonicalEntryPath(workspaceRoot);
|
|
91
|
+
const fs = box.fs;
|
|
92
|
+
if (!fs?.list || !fs.usage || !fs.readBatch)
|
|
93
|
+
throw new Error("Tangle evidence requires workspace list, usage, and binary batch read");
|
|
94
|
+
options.signal?.throwIfAborted();
|
|
95
|
+
const usage = await fs.usage(workspaceRoot);
|
|
96
|
+
if (!usage.complete || usage.skippedEntries !== 0)
|
|
97
|
+
throw new Error("Tangle workspace usage scan is incomplete");
|
|
98
|
+
if (usage.sizeBytes > options.maxBytes)
|
|
99
|
+
throw new Error("Tangle workspace exceeds evidence byte limit");
|
|
100
|
+
const files = [];
|
|
101
|
+
const metadata = [];
|
|
102
|
+
const seenPaths = new Set();
|
|
103
|
+
const excludedPaths = [];
|
|
104
|
+
const stack = [workspaceRoot];
|
|
105
|
+
let scannedFiles = 0;
|
|
106
|
+
let scannedDirectories = 0;
|
|
107
|
+
let scannedEntries = 0;
|
|
108
|
+
let scannedSize = 0;
|
|
109
|
+
let capturedBytes = 0;
|
|
110
|
+
while (stack.length) {
|
|
111
|
+
options.signal?.throwIfAborted();
|
|
112
|
+
const directory = stack.pop();
|
|
113
|
+
const entries = await fs.list(directory, { all: true, long: true });
|
|
114
|
+
if (!Array.isArray(entries))
|
|
115
|
+
throw new Error("Tangle workspace list is malformed");
|
|
116
|
+
for (const entry of entries) {
|
|
117
|
+
if (++scannedEntries > MAX_ENTRIES)
|
|
118
|
+
throw new Error("Tangle workspace entry limit exceeded");
|
|
119
|
+
const sourcePath = canonicalEntryPath(entry.path);
|
|
120
|
+
if (workspaceRoot !== "." && !sourcePath.startsWith(workspaceRoot + "/")) {
|
|
121
|
+
throw new Error("Tangle workspace list returned a path outside its worker directory");
|
|
122
|
+
}
|
|
123
|
+
const path = workspaceRoot === "." ? sourcePath : canonicalEntryPath(sourcePath.slice(workspaceRoot.length + 1));
|
|
124
|
+
if (seenPaths.has(path))
|
|
125
|
+
throw new Error("Tangle workspace inventory repeats a path");
|
|
126
|
+
seenPaths.add(path);
|
|
127
|
+
const expectedParent = sourcePath.includes("/") ? sourcePath.slice(0, sourcePath.lastIndexOf("/")) : ".";
|
|
128
|
+
if (expectedParent !== directory || entry.name !== path.split("/").at(-1))
|
|
129
|
+
throw new Error("Tangle workspace list returned a path outside its parent");
|
|
130
|
+
if (!Number.isSafeInteger(entry.size) || entry.size < 0)
|
|
131
|
+
throw new Error("Tangle workspace entry has invalid size");
|
|
132
|
+
const item = entryMetadata(path, entry);
|
|
133
|
+
metadata.push(item);
|
|
134
|
+
if (path.split("/").includes(".sidecar")) {
|
|
135
|
+
excludedPaths.push({ ...item, reason: "runtime-owned" });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (entry.isDir && !entry.isSymlink) {
|
|
139
|
+
scannedDirectories++;
|
|
140
|
+
if (path.split("/").some((part) => blockedNames.has(part))) {
|
|
141
|
+
excludedPaths.push({ ...item, reason: "credential-path" });
|
|
142
|
+
}
|
|
143
|
+
stack.push(sourcePath);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (!entry.isFile && !entry.isSymlink)
|
|
147
|
+
throw new Error("Tangle workspace contains an unsupported file type");
|
|
148
|
+
scannedFiles++;
|
|
149
|
+
scannedSize += entry.size;
|
|
150
|
+
if (entry.isSymlink) {
|
|
151
|
+
excludedPaths.push({ ...item, reason: "symlink" });
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (path.split("/").some((part) => blockedNames.has(part) || /^\.env\./.test(part))) {
|
|
155
|
+
excludedPaths.push({ ...item, reason: "credential-path" });
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const result = await fs.readBatch([sourcePath], { encoding: "base64" });
|
|
159
|
+
if (result.errors.length || result.files.length !== 1 || result.files[0]?.path !== sourcePath || result.files[0]?.encoding !== "base64") {
|
|
160
|
+
throw new Error(`Tangle evidence could not read workspace file ${path}`);
|
|
161
|
+
}
|
|
162
|
+
const read = result.files[0];
|
|
163
|
+
const bytes = Buffer.from(read.content, "base64");
|
|
164
|
+
if (bytes.byteLength !== entry.size || read.size !== entry.size || bytes.toString("base64") !== read.content) {
|
|
165
|
+
throw new Error(`Tangle evidence workspace file changed or was truncated: ${path}`);
|
|
166
|
+
}
|
|
167
|
+
if (read.hash && read.hash.replace(/^sha256:/, "") !== createHash("sha256").update(bytes).digest("hex")) {
|
|
168
|
+
throw new Error(`Tangle evidence workspace file hash mismatch: ${path}`);
|
|
169
|
+
}
|
|
170
|
+
files.push({ path, bytes, mode: entry.permissions & 0o777 });
|
|
171
|
+
capturedBytes += bytes.byteLength;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (scannedFiles !== usage.fileCount || scannedDirectories !== usage.directoryCount || scannedSize !== usage.sizeBytes) {
|
|
175
|
+
throw new Error("Tangle workspace inventory does not match the complete usage scan");
|
|
176
|
+
}
|
|
177
|
+
const sessionIds = new Set(options.sandboxSessionIds);
|
|
178
|
+
const sessions = [];
|
|
179
|
+
const missing = [];
|
|
180
|
+
if (sessionIds.size === 0)
|
|
181
|
+
missing.push("No exact Sandbox session id was attributed to this execution");
|
|
182
|
+
for (const id of [...sessionIds].sort()) {
|
|
183
|
+
const session = box.session?.(id);
|
|
184
|
+
if (!session?.events || !session.messages) {
|
|
185
|
+
missing.push(`Sandbox session ${id} has no replay and message export`);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const status = await session.status();
|
|
189
|
+
if (status === null) {
|
|
190
|
+
missing.push(`Sandbox session ${id} no longer exists`);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const executionIds = options.sessionExecutionIds?.[id] ?? [options.executionId];
|
|
194
|
+
if (!executionIds.length || new Set(executionIds).size !== executionIds.length ||
|
|
195
|
+
executionIds.some((executionId) => !safeIdentifier(executionId))) {
|
|
196
|
+
throw new Error("Tangle evidence has invalid session execution attribution");
|
|
197
|
+
}
|
|
198
|
+
const events = [];
|
|
199
|
+
const eventCountsByExecutionId = {};
|
|
200
|
+
for (const executionId of executionIds) {
|
|
201
|
+
eventCountsByExecutionId[executionId] = 0;
|
|
202
|
+
for await (const event of session.events({ since: "0", executionId, signal: options.signal })) {
|
|
203
|
+
options.signal?.throwIfAborted();
|
|
204
|
+
if (events.length >= MAX_EVENTS)
|
|
205
|
+
throw new Error("Tangle event replay limit exceeded");
|
|
206
|
+
if (typeof event.data?.executionId === "string" && event.data.executionId !== executionId) {
|
|
207
|
+
throw new Error("Tangle event replay returned an unrelated execution");
|
|
208
|
+
}
|
|
209
|
+
if (typeof event.data?.runtimeSessionId === "string" && event.data.runtimeSessionId !== id) {
|
|
210
|
+
throw new Error("Tangle event replay returned an unrelated session");
|
|
211
|
+
}
|
|
212
|
+
const attributed = { executionId, event };
|
|
213
|
+
capturedBytes += Buffer.byteLength(JSON.stringify(attributed));
|
|
214
|
+
if (capturedBytes > options.maxBytes)
|
|
215
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
216
|
+
events.push(attributed);
|
|
217
|
+
eventCountsByExecutionId[executionId] += 1;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const messages = [];
|
|
221
|
+
for (let offset = 0;; offset += 1000) {
|
|
222
|
+
options.signal?.throwIfAborted();
|
|
223
|
+
if (offset >= MAX_EVENTS)
|
|
224
|
+
throw new Error("Tangle message pagination limit exceeded");
|
|
225
|
+
const page = await session.messages({ limit: 1000, offset });
|
|
226
|
+
if (!Array.isArray(page) || page.length > 1000)
|
|
227
|
+
throw new Error("Tangle session message page is malformed");
|
|
228
|
+
capturedBytes += Buffer.byteLength(JSON.stringify(page));
|
|
229
|
+
if (capturedBytes > options.maxBytes)
|
|
230
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
231
|
+
messages.push(...page);
|
|
232
|
+
if (page.length < 1000)
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
const bytes = Buffer.from(JSON.stringify({ kind: "tangle-session-transport.v1", environmentId: box.id, sessionId: id, executionIds, status, events, messages, messageScope: "session" }));
|
|
236
|
+
if (files.reduce((sum, file) => sum + file.bytes.byteLength, 0) + bytes.byteLength > options.maxBytes)
|
|
237
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
238
|
+
capturedBytes = files.reduce((sum, file) => sum + file.bytes.byteLength, 0) + bytes.byteLength;
|
|
239
|
+
files.push({ path: `__retention__/sessions/${id}.json`, bytes, mode: 0o600 });
|
|
240
|
+
const native = await session.rawEvidence?.();
|
|
241
|
+
const nativeStore = {
|
|
242
|
+
scope: "session", roots: [], inventory: null, complete: false, entries: [], excludedPaths: [],
|
|
243
|
+
};
|
|
244
|
+
const processStreams = {
|
|
245
|
+
complete: false, streamCount: 0, processCount: 0, terminalCount: 0,
|
|
246
|
+
stdinBytes: 0, stdoutBytes: 0, stderrBytes: 0, protocolBytes: 0, terminals: [],
|
|
247
|
+
};
|
|
248
|
+
const nativeEvents = {
|
|
249
|
+
complete: false, count: 0,
|
|
250
|
+
};
|
|
251
|
+
let nativeSessionId = null;
|
|
252
|
+
let sidecarImageDigest = null;
|
|
253
|
+
let sidecarBundleRevision = null;
|
|
254
|
+
let nativeReason = "raw-session-capture-capability-absent";
|
|
255
|
+
if (native !== undefined) {
|
|
256
|
+
if (native.sessionId !== id || native.backendType !== options.harness) {
|
|
257
|
+
throw new Error("Tangle raw evidence returned an unrelated session or profile backend");
|
|
258
|
+
}
|
|
259
|
+
if (native.nativeSessionId !== undefined && !safeIdentifier(native.nativeSessionId)) {
|
|
260
|
+
throw new Error("Tangle raw evidence native session id is invalid");
|
|
261
|
+
}
|
|
262
|
+
nativeSessionId = native.nativeSessionId ?? null;
|
|
263
|
+
if (native.status === "captured") {
|
|
264
|
+
if (!/^sha256:[0-9a-f]{64}$/.test(native.sidecarImageDigest) ||
|
|
265
|
+
(options.expectedSidecarImageDigest !== undefined &&
|
|
266
|
+
native.sidecarImageDigest !== options.expectedSidecarImageDigest)) {
|
|
267
|
+
throw new Error("Tangle raw session image digest is missing or differs from deployment proof");
|
|
268
|
+
}
|
|
269
|
+
if (!/^[0-9a-f]{40}$/.test(native.sidecarBundleRevision)) {
|
|
270
|
+
throw new Error("Tangle raw session sidecar bundle revision is missing or invalid");
|
|
271
|
+
}
|
|
272
|
+
sidecarImageDigest = native.sidecarImageDigest;
|
|
273
|
+
sidecarBundleRevision = native.sidecarBundleRevision;
|
|
274
|
+
if (native.completeness?.nativeStore !== true || native.completeness.processIo !== true ||
|
|
275
|
+
native.completeness.events !== true || !Array.isArray(native.files) ||
|
|
276
|
+
!Array.isArray(native.processIo) || !Array.isArray(native.events) ||
|
|
277
|
+
!Array.isArray(native.excluded) || !Array.isArray(native.nativeRoots) || !native.inventory ||
|
|
278
|
+
!Array.isArray(native.processTerminals) || native.coverageComplete !== true) {
|
|
279
|
+
throw new Error("Tangle raw session capture is incomplete");
|
|
280
|
+
}
|
|
281
|
+
if (native.files.length + native.processIo.length + native.processTerminals.length +
|
|
282
|
+
native.events.length > MAX_ENTRIES) {
|
|
283
|
+
throw new Error("Tangle raw session capture exceeds entry limit");
|
|
284
|
+
}
|
|
285
|
+
const roots = new Set();
|
|
286
|
+
for (const root of native.nativeRoots) {
|
|
287
|
+
if (!["session-home", "workspace-session"].includes(root.scope) || roots.has(root.scope) ||
|
|
288
|
+
(root.path !== "." && canonicalEntryPath(root.path) !== root.path)) {
|
|
289
|
+
throw new Error("Tangle raw session native root is invalid");
|
|
290
|
+
}
|
|
291
|
+
roots.add(root.scope);
|
|
292
|
+
nativeStore.roots.push(root);
|
|
293
|
+
}
|
|
294
|
+
if (!roots.size)
|
|
295
|
+
throw new Error("Tangle raw session has no native root");
|
|
296
|
+
const nativePaths = new Set();
|
|
297
|
+
const included = { file: 0, directory: 0, symlink: 0 };
|
|
298
|
+
const excluded = { file: 0, directory: 0, symlink: 0 };
|
|
299
|
+
for (const entry of native.files) {
|
|
300
|
+
options.signal?.throwIfAborted();
|
|
301
|
+
const path = canonicalEntryPath(entry.path);
|
|
302
|
+
if (!roots.has(entry.rootScope))
|
|
303
|
+
throw new Error("Tangle raw session entry has no native root");
|
|
304
|
+
const identity = entry.rootScope + "/" + path;
|
|
305
|
+
if (nativePaths.has(identity))
|
|
306
|
+
throw new Error("Tangle raw session repeats a native path");
|
|
307
|
+
nativePaths.add(identity);
|
|
308
|
+
if (!nativeStatMetadata(entry))
|
|
309
|
+
throw new Error("Tangle raw session entry metadata is invalid");
|
|
310
|
+
if (entry.kind === "file") {
|
|
311
|
+
const content = exactNativeBytes(entry.contentBase64, entry.sizeBytes, entry.sha256, options.maxBytes - capturedBytes);
|
|
312
|
+
if (capturedBytes + content.byteLength > options.maxBytes)
|
|
313
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
314
|
+
files.push({ path: "__retention__/sessions/" + id + "/native/" + entry.rootScope + "/" + path, bytes: content, mode: entry.mode & 0o777 });
|
|
315
|
+
capturedBytes += content.byteLength;
|
|
316
|
+
}
|
|
317
|
+
else if (entry.kind === "directory" || entry.kind === "symlink") {
|
|
318
|
+
if (entry.contentBase64 !== undefined || entry.sha256 !== undefined ||
|
|
319
|
+
(entry.kind === "symlink" && (typeof entry.linkTarget !== "string" || entry.linkTarget.includes("\0")))) {
|
|
320
|
+
throw new Error("Tangle raw session non-file entry is malformed");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
throw new Error("Tangle raw session entry kind is invalid");
|
|
325
|
+
}
|
|
326
|
+
included[entry.kind] += 1;
|
|
327
|
+
nativeStore.entries.push({
|
|
328
|
+
rootScope: entry.rootScope, path, kind: entry.kind, mode: entry.mode,
|
|
329
|
+
uid: entry.uid, gid: entry.gid, mtimeMs: entry.mtimeMs, ctimeMs: entry.ctimeMs,
|
|
330
|
+
sizeBytes: entry.sizeBytes, sha256: entry.sha256 ?? null, linkTarget: entry.linkTarget ?? null,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
for (const exclusion of native.excluded) {
|
|
334
|
+
const path = canonicalEntryPath(exclusion.path);
|
|
335
|
+
const identity = exclusion.rootScope + "/" + path;
|
|
336
|
+
if (!roots.has(exclusion.rootScope) || nativePaths.has(identity) ||
|
|
337
|
+
exclusion.reason !== "credential" || !["file", "directory", "symlink"].includes(exclusion.kind) ||
|
|
338
|
+
!nativeStatMetadata(exclusion)) {
|
|
339
|
+
throw new Error("Tangle raw session exclusion is invalid");
|
|
340
|
+
}
|
|
341
|
+
nativePaths.add(identity);
|
|
342
|
+
excluded[exclusion.kind] += 1;
|
|
343
|
+
nativeStore.excludedPaths.push({
|
|
344
|
+
rootScope: exclusion.rootScope, path, kind: exclusion.kind, mode: exclusion.mode,
|
|
345
|
+
uid: exclusion.uid, gid: exclusion.gid, mtimeMs: exclusion.mtimeMs, ctimeMs: exclusion.ctimeMs,
|
|
346
|
+
sizeBytes: exclusion.sizeBytes, reason: exclusion.reason,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
const inventory = native.inventory;
|
|
350
|
+
const counts = [
|
|
351
|
+
inventory.reportedFiles, inventory.scannedFiles, inventory.excludedFiles,
|
|
352
|
+
inventory.reportedDirectories, inventory.scannedDirectories, inventory.excludedDirectories,
|
|
353
|
+
inventory.reportedSymlinks, inventory.scannedSymlinks, inventory.excludedSymlinks,
|
|
354
|
+
];
|
|
355
|
+
if (counts.some((value) => !Number.isSafeInteger(value) || value < 0) ||
|
|
356
|
+
inventory.skippedEntries !== 0 ||
|
|
357
|
+
inventory.reportedFiles !== included.file ||
|
|
358
|
+
inventory.reportedDirectories !== included.directory ||
|
|
359
|
+
inventory.reportedSymlinks !== included.symlink ||
|
|
360
|
+
inventory.excludedFiles !== excluded.file ||
|
|
361
|
+
inventory.excludedDirectories !== excluded.directory ||
|
|
362
|
+
inventory.excludedSymlinks !== excluded.symlink ||
|
|
363
|
+
inventory.scannedFiles !== inventory.reportedFiles + inventory.excludedFiles ||
|
|
364
|
+
inventory.scannedDirectories !== inventory.reportedDirectories + inventory.excludedDirectories ||
|
|
365
|
+
inventory.scannedSymlinks !== inventory.reportedSymlinks + inventory.excludedSymlinks) {
|
|
366
|
+
throw new Error("Tangle raw session inventory does not reconcile");
|
|
367
|
+
}
|
|
368
|
+
nativeStore.inventory = inventory;
|
|
369
|
+
const ioSequences = new Map();
|
|
370
|
+
const frameMetadata = [];
|
|
371
|
+
for (const frame of native.processIo) {
|
|
372
|
+
options.signal?.throwIfAborted();
|
|
373
|
+
if (!safeIdentifier(frame.processId) || !Number.isSafeInteger(frame.sequence) ||
|
|
374
|
+
frame.sequence < 0 || !Number.isFinite(Date.parse(frame.at)) ||
|
|
375
|
+
!["stdin", "stdout", "stderr", "protocol"].includes(frame.stream)) {
|
|
376
|
+
throw new Error("Tangle raw session process stream metadata is invalid");
|
|
377
|
+
}
|
|
378
|
+
const sequences = ioSequences.get(frame.processId) ?? new Set();
|
|
379
|
+
if (sequences.has(frame.sequence))
|
|
380
|
+
throw new Error("Tangle raw session repeats a process frame");
|
|
381
|
+
sequences.add(frame.sequence);
|
|
382
|
+
ioSequences.set(frame.processId, sequences);
|
|
383
|
+
const content = exactNativeBytes(frame.contentBase64, frame.sizeBytes, frame.sha256, options.maxBytes - capturedBytes);
|
|
384
|
+
if (capturedBytes + content.byteLength > options.maxBytes)
|
|
385
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
386
|
+
const label = String(frame.sequence).padStart(16, "0");
|
|
387
|
+
files.push({ path: "__retention__/sessions/" + id + "/io/" + frame.processId + "/" + label +
|
|
388
|
+
"-" + frame.stream + ".bin", bytes: content, mode: 0o600 });
|
|
389
|
+
frameMetadata.push({
|
|
390
|
+
processId: frame.processId, sequence: frame.sequence, at: frame.at, stream: frame.stream,
|
|
391
|
+
sizeBytes: frame.sizeBytes, sha256: frame.sha256,
|
|
392
|
+
...(frame.metadata === undefined ? {} : { metadata: frame.metadata }),
|
|
393
|
+
});
|
|
394
|
+
capturedBytes += content.byteLength;
|
|
395
|
+
const counter = (frame.stream + "Bytes");
|
|
396
|
+
processStreams[counter] += content.byteLength;
|
|
397
|
+
processStreams.streamCount += 1;
|
|
398
|
+
}
|
|
399
|
+
const terminals = new Set();
|
|
400
|
+
for (const terminal of native.processTerminals) {
|
|
401
|
+
if (!safeIdentifier(terminal.processId) || terminals.has(terminal.processId) ||
|
|
402
|
+
!Number.isSafeInteger(terminal.sequence) || terminal.sequence < 0 ||
|
|
403
|
+
!Number.isFinite(Date.parse(terminal.at)) ||
|
|
404
|
+
terminal.result?.captureError !== null ||
|
|
405
|
+
!(terminal.result.code === null || Number.isSafeInteger(terminal.result.code)) ||
|
|
406
|
+
!(terminal.result.signal === null || typeof terminal.result.signal === "string") ||
|
|
407
|
+
typeof terminal.result.timedOut !== "boolean" ||
|
|
408
|
+
!(terminal.result.timeoutReason === null || typeof terminal.result.timeoutReason === "string") ||
|
|
409
|
+
(terminal.result.spawnError !== undefined && typeof terminal.result.spawnError !== "string")) {
|
|
410
|
+
throw new Error("Tangle raw session process terminal metadata is invalid");
|
|
411
|
+
}
|
|
412
|
+
const sequences = ioSequences.get(terminal.processId) ?? new Set();
|
|
413
|
+
if (terminal.sequence !== sequences.size ||
|
|
414
|
+
[...sequences].some((sequence) => sequence < 0 || sequence >= terminal.sequence)) {
|
|
415
|
+
throw new Error("Tangle raw session process sequence is incomplete");
|
|
416
|
+
}
|
|
417
|
+
terminals.add(terminal.processId);
|
|
418
|
+
processStreams.terminals.push(terminal);
|
|
419
|
+
}
|
|
420
|
+
if ([...ioSequences.keys()].some((processId) => !terminals.has(processId))) {
|
|
421
|
+
throw new Error("Tangle raw session has a process without a terminal receipt");
|
|
422
|
+
}
|
|
423
|
+
processStreams.processCount = terminals.size;
|
|
424
|
+
processStreams.terminalCount = terminals.size;
|
|
425
|
+
const processManifest = Buffer.from(JSON.stringify({
|
|
426
|
+
kind: "tangle-native-process-streams.v1", sessionId: id,
|
|
427
|
+
frames: frameMetadata, terminals: native.processTerminals,
|
|
428
|
+
}));
|
|
429
|
+
if (capturedBytes + processManifest.byteLength > options.maxBytes)
|
|
430
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
431
|
+
files.push({ path: "__retention__/sessions/" + id + "/process-manifest.json",
|
|
432
|
+
bytes: processManifest, mode: 0o600 });
|
|
433
|
+
capturedBytes += processManifest.byteLength;
|
|
434
|
+
const nativeEventBytes = Buffer.from(JSON.stringify({
|
|
435
|
+
kind: "tangle-native-session-events.v1", sessionId: id, backendType: native.backendType,
|
|
436
|
+
nativeSessionId, events: native.events,
|
|
437
|
+
}));
|
|
438
|
+
if (capturedBytes + nativeEventBytes.byteLength > options.maxBytes)
|
|
439
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
440
|
+
files.push({ path: "__retention__/sessions/" + id + "/native-events.json", bytes: nativeEventBytes, mode: 0o600 });
|
|
441
|
+
capturedBytes += nativeEventBytes.byteLength;
|
|
442
|
+
nativeStore.complete = true;
|
|
443
|
+
processStreams.complete = true;
|
|
444
|
+
nativeEvents.complete = true;
|
|
445
|
+
nativeEvents.count = native.events.length;
|
|
446
|
+
nativeReason = null;
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
if (!safeIdentifier(native.reason))
|
|
450
|
+
throw new Error("Tangle raw session unavailability reason is invalid");
|
|
451
|
+
nativeReason = native.reason;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
sessions.push({
|
|
455
|
+
id, executionId: options.executionId, executionIds: [...executionIds], eventCountsByExecutionId,
|
|
456
|
+
backendType: options.harness, sidecarImageDigest, sidecarBundleRevision,
|
|
457
|
+
transportEvents: executionIds.every((executionId) => eventCountsByExecutionId[executionId] > 0) ? "complete" : "unavailable",
|
|
458
|
+
eventCount: events.length, messageCount: messages.length, messageScope: "session",
|
|
459
|
+
nativeSessionId, nativeReason, nativeStore, processStreams, nativeEvents,
|
|
460
|
+
});
|
|
461
|
+
for (const executionId of executionIds) {
|
|
462
|
+
if (!eventCountsByExecutionId[executionId]) {
|
|
463
|
+
missing.push("Sandbox session " + id + " returned no events for execution " + executionId);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (!nativeStore.complete || !processStreams.complete || !nativeEvents.complete) {
|
|
467
|
+
missing.push("Raw native session capture for Sandbox session " + id + " is unavailable: " + nativeReason);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
const provenance = {
|
|
471
|
+
provider: "tangle-sandbox",
|
|
472
|
+
environmentId: box.id,
|
|
473
|
+
executionId: options.executionId,
|
|
474
|
+
workspaceScope: "environment",
|
|
475
|
+
workspaceRoot,
|
|
476
|
+
capturedAt: new Date().toISOString(),
|
|
477
|
+
entries: metadata.sort((a, b) => a.path.localeCompare(b.path)),
|
|
478
|
+
excludedPaths,
|
|
479
|
+
workspace: { scannedFiles, scannedDirectories, reportedFiles: usage.fileCount, reportedDirectories: usage.directoryCount, complete: true },
|
|
480
|
+
sessions,
|
|
481
|
+
missing,
|
|
482
|
+
};
|
|
483
|
+
const provenanceBytes = Buffer.from(JSON.stringify({ kind: "tangle-evidence-provenance.v1", ...provenance }));
|
|
484
|
+
if (capturedBytes + provenanceBytes.byteLength > options.maxBytes)
|
|
485
|
+
throw new Error("Tangle evidence exceeds byte limit");
|
|
486
|
+
files.push({ path: "__retention__/provenance.json", bytes: provenanceBytes, mode: 0o600 });
|
|
487
|
+
return { files: files.sort((a, b) => a.path.localeCompare(b.path)), provenance };
|
|
488
|
+
}
|
|
489
|
+
function safeIdentifier(value) {
|
|
490
|
+
return /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(value);
|
|
491
|
+
}
|
|
492
|
+
function canonicalEntryPath(value) {
|
|
493
|
+
if (!value || value.startsWith("/") || value.includes("\\") || value.includes("\0"))
|
|
494
|
+
throw new Error("Tangle workspace entry path is unsafe");
|
|
495
|
+
const parts = value.split("/");
|
|
496
|
+
if (parts.some((part) => !part || part === "." || part === ".."))
|
|
497
|
+
throw new Error("Tangle workspace entry path is not canonical");
|
|
498
|
+
if (parts[0] === "__retention__")
|
|
499
|
+
throw new Error("Tangle workspace conflicts with reserved retention path");
|
|
500
|
+
return value;
|
|
501
|
+
}
|
|
502
|
+
function entryMetadata(path, entry) {
|
|
503
|
+
if (!Number.isInteger(entry.permissions) || entry.permissions < 0)
|
|
504
|
+
throw new Error("Tangle workspace entry has invalid mode");
|
|
505
|
+
const type = entry.isSymlink ? "symlink" : entry.isDir ? "directory" : entry.isFile ? "file" : undefined;
|
|
506
|
+
if (!type)
|
|
507
|
+
throw new Error("Tangle workspace entry has unknown type");
|
|
508
|
+
return {
|
|
509
|
+
path,
|
|
510
|
+
type,
|
|
511
|
+
sizeBytes: entry.size,
|
|
512
|
+
mode: entry.permissions & 0o777,
|
|
513
|
+
owner: entry.owner ?? null,
|
|
514
|
+
group: entry.group ?? null,
|
|
515
|
+
modifiedAt: isoTime(entry.modTime),
|
|
516
|
+
accessedAt: isoTime(entry.accessTime),
|
|
517
|
+
symlinkTarget: null,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
function isoTime(value) {
|
|
521
|
+
if (value === undefined)
|
|
522
|
+
return null;
|
|
523
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
524
|
+
if (!Number.isFinite(date.getTime()))
|
|
525
|
+
throw new Error("Tangle workspace entry has invalid timestamp");
|
|
526
|
+
return date.toISOString();
|
|
527
|
+
}
|
|
528
|
+
function exactNativeBytes(contentBase64, sizeBytes, sha256, remainingBytes) {
|
|
529
|
+
if (typeof contentBase64 !== "string" || !Number.isSafeInteger(sizeBytes) || sizeBytes < 0 ||
|
|
530
|
+
sizeBytes > remainingBytes || contentBase64.length !== Math.ceil(sizeBytes / 3) * 4 ||
|
|
531
|
+
!/^sha256:[0-9a-f]{64}$/.test(sha256 ?? "")) {
|
|
532
|
+
throw new Error("Tangle raw session bytes have invalid metadata");
|
|
533
|
+
}
|
|
534
|
+
const bytes = Buffer.from(contentBase64, "base64");
|
|
535
|
+
const digest = "sha256:" + createHash("sha256").update(bytes).digest("hex");
|
|
536
|
+
if (bytes.byteLength !== sizeBytes || bytes.toString("base64") !== contentBase64 || digest !== sha256) {
|
|
537
|
+
throw new Error("Tangle raw session bytes do not match the server receipt");
|
|
538
|
+
}
|
|
539
|
+
return bytes;
|
|
540
|
+
}
|
|
541
|
+
function nativeStatMetadata(entry) {
|
|
542
|
+
return Number.isSafeInteger(entry.mode) && entry.mode >= 0 &&
|
|
543
|
+
Number.isSafeInteger(entry.uid) && entry.uid >= 0 &&
|
|
544
|
+
Number.isSafeInteger(entry.gid) && entry.gid >= 0 &&
|
|
545
|
+
Number.isFinite(entry.mtimeMs) && entry.mtimeMs >= 0 &&
|
|
546
|
+
Number.isFinite(entry.ctimeMs) && entry.ctimeMs >= 0 &&
|
|
547
|
+
Number.isSafeInteger(entry.sizeBytes) && entry.sizeBytes >= 0;
|
|
548
|
+
}
|
package/dist/tangle-types.d.ts
CHANGED
|
@@ -15,6 +15,12 @@ export interface TangleExactProcessOptions {
|
|
|
15
15
|
teamId?: string;
|
|
16
16
|
}
|
|
17
17
|
export interface SandboxClientLike {
|
|
18
|
+
/** Deployment-scoped evidence guarantees for a new sandbox on this client. */
|
|
19
|
+
evidenceCapabilities?(): Promise<{
|
|
20
|
+
nativeSessionCaptureV1?: boolean;
|
|
21
|
+
workspaceCaptureV1?: boolean;
|
|
22
|
+
sidecarImageDigest?: string;
|
|
23
|
+
}>;
|
|
18
24
|
create(options?: CreateSandboxOptions, requestOptions?: {
|
|
19
25
|
signal?: AbortSignal;
|
|
20
26
|
timeoutMs?: number;
|
|
@@ -426,6 +432,29 @@ export interface SandboxInstanceLike {
|
|
|
426
432
|
size: number;
|
|
427
433
|
isFile: boolean;
|
|
428
434
|
}>;
|
|
435
|
+
list?(path: string, options?: {
|
|
436
|
+
all?: boolean;
|
|
437
|
+
long?: boolean;
|
|
438
|
+
}): Promise<Array<{
|
|
439
|
+
name: string;
|
|
440
|
+
path: string;
|
|
441
|
+
size: number;
|
|
442
|
+
isDir: boolean;
|
|
443
|
+
isFile: boolean;
|
|
444
|
+
isSymlink: boolean;
|
|
445
|
+
permissions: number;
|
|
446
|
+
owner?: string;
|
|
447
|
+
group?: string;
|
|
448
|
+
modTime?: Date | string;
|
|
449
|
+
accessTime?: Date | string;
|
|
450
|
+
}>>;
|
|
451
|
+
usage?(path: string): Promise<{
|
|
452
|
+
sizeBytes: number;
|
|
453
|
+
fileCount: number;
|
|
454
|
+
directoryCount: number;
|
|
455
|
+
complete: boolean;
|
|
456
|
+
skippedEntries: number;
|
|
457
|
+
}>;
|
|
429
458
|
readBatch(paths: string[], options?: {
|
|
430
459
|
encoding?: "utf8" | "base64";
|
|
431
460
|
}): Promise<{
|
|
@@ -434,6 +463,7 @@ export interface SandboxInstanceLike {
|
|
|
434
463
|
content: string;
|
|
435
464
|
encoding: "utf8" | "base64";
|
|
436
465
|
size: number;
|
|
466
|
+
hash?: string;
|
|
437
467
|
}>;
|
|
438
468
|
errors: Array<{
|
|
439
469
|
path: string;
|
|
@@ -515,6 +545,97 @@ export interface SandboxInstanceLike {
|
|
|
515
545
|
}
|
|
516
546
|
export interface SandboxSessionLike {
|
|
517
547
|
readonly id: string;
|
|
548
|
+
messages?(options?: {
|
|
549
|
+
limit?: number;
|
|
550
|
+
offset?: number;
|
|
551
|
+
since?: number;
|
|
552
|
+
}): Promise<unknown[]>;
|
|
553
|
+
rawEvidence?(): Promise<{
|
|
554
|
+
status: "captured";
|
|
555
|
+
sessionId: string;
|
|
556
|
+
backendType: string;
|
|
557
|
+
nativeSessionId?: string;
|
|
558
|
+
sidecarImageDigest: string;
|
|
559
|
+
sidecarBundleRevision: string;
|
|
560
|
+
nativeRoots: Array<{
|
|
561
|
+
scope: "session-home" | "workspace-session";
|
|
562
|
+
path: string;
|
|
563
|
+
}>;
|
|
564
|
+
inventory: {
|
|
565
|
+
scannedFiles: number;
|
|
566
|
+
reportedFiles: number;
|
|
567
|
+
excludedFiles: number;
|
|
568
|
+
scannedDirectories: number;
|
|
569
|
+
reportedDirectories: number;
|
|
570
|
+
excludedDirectories: number;
|
|
571
|
+
scannedSymlinks: number;
|
|
572
|
+
reportedSymlinks: number;
|
|
573
|
+
excludedSymlinks: number;
|
|
574
|
+
skippedEntries: 0;
|
|
575
|
+
};
|
|
576
|
+
files: Array<{
|
|
577
|
+
rootScope: "session-home" | "workspace-session";
|
|
578
|
+
path: string;
|
|
579
|
+
kind: "file" | "directory" | "symlink";
|
|
580
|
+
mode: number;
|
|
581
|
+
uid: number;
|
|
582
|
+
gid: number;
|
|
583
|
+
mtimeMs: number;
|
|
584
|
+
ctimeMs: number;
|
|
585
|
+
sizeBytes: number;
|
|
586
|
+
sha256?: string;
|
|
587
|
+
contentBase64?: string;
|
|
588
|
+
linkTarget?: string;
|
|
589
|
+
}>;
|
|
590
|
+
processIo: Array<{
|
|
591
|
+
processId: string;
|
|
592
|
+
sequence: number;
|
|
593
|
+
at: string;
|
|
594
|
+
stream: "stdin" | "stdout" | "stderr" | "protocol";
|
|
595
|
+
sizeBytes: number;
|
|
596
|
+
sha256: string;
|
|
597
|
+
contentBase64: string;
|
|
598
|
+
metadata?: unknown;
|
|
599
|
+
}>;
|
|
600
|
+
processTerminals: Array<{
|
|
601
|
+
processId: string;
|
|
602
|
+
sequence: number;
|
|
603
|
+
at: string;
|
|
604
|
+
result: {
|
|
605
|
+
code: number | null;
|
|
606
|
+
signal: string | null;
|
|
607
|
+
timedOut: boolean;
|
|
608
|
+
timeoutReason: string | null;
|
|
609
|
+
captureError: null;
|
|
610
|
+
spawnError?: string;
|
|
611
|
+
};
|
|
612
|
+
}>;
|
|
613
|
+
events: unknown[];
|
|
614
|
+
excluded: Array<{
|
|
615
|
+
rootScope: "session-home" | "workspace-session";
|
|
616
|
+
path: string;
|
|
617
|
+
kind: "file" | "directory" | "symlink";
|
|
618
|
+
mode: number;
|
|
619
|
+
uid: number;
|
|
620
|
+
gid: number;
|
|
621
|
+
mtimeMs: number;
|
|
622
|
+
ctimeMs: number;
|
|
623
|
+
sizeBytes: number;
|
|
624
|
+
reason: "credential";
|
|
625
|
+
}>;
|
|
626
|
+
completeness: {
|
|
627
|
+
nativeStore: true;
|
|
628
|
+
processIo: true;
|
|
629
|
+
events: true;
|
|
630
|
+
};
|
|
631
|
+
coverageComplete: true;
|
|
632
|
+
} | {
|
|
633
|
+
status: "unavailable";
|
|
634
|
+
sessionId: string;
|
|
635
|
+
backendType: string;
|
|
636
|
+
nativeSessionId?: string;
|
|
637
|
+
reason: string;
|
|
638
|
+
}>;
|
|
518
639
|
/** Exact native coding-agent TUI bound to this session id. */
|
|
519
640
|
interactive?(options?: {
|
|
520
641
|
ref?: AgentInteractiveSessionRef;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tangle-network/agent-provider-tangle",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "AgentEnvironmentProvider adapter for Tangle sandboxes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -49,6 +49,8 @@
|
|
|
49
49
|
"dist/tangle-environment-values.js",
|
|
50
50
|
"dist/tangle-environment.d.ts",
|
|
51
51
|
"dist/tangle-environment.js",
|
|
52
|
+
"dist/tangle-evidence.d.ts",
|
|
53
|
+
"dist/tangle-evidence.js",
|
|
52
54
|
"dist/tangle-environment-dispatch.d.ts",
|
|
53
55
|
"dist/tangle-environment-dispatch.js",
|
|
54
56
|
"dist/tangle-environment-session.d.ts",
|
|
@@ -99,7 +101,7 @@
|
|
|
99
101
|
"LICENSE"
|
|
100
102
|
],
|
|
101
103
|
"dependencies": {
|
|
102
|
-
"@tangle-network/agent-interface": "^2.
|
|
104
|
+
"@tangle-network/agent-interface": "^2.14.0"
|
|
103
105
|
},
|
|
104
106
|
"peerDependencies": {
|
|
105
107
|
"@tangle-network/sandbox": ">=0.39.0 <1.0.0"
|
|
@@ -110,13 +112,12 @@
|
|
|
110
112
|
}
|
|
111
113
|
},
|
|
112
114
|
"devDependencies": {
|
|
113
|
-
"@tangle-network/agent-
|
|
114
|
-
"@tangle-network/agent-runtime": "0.
|
|
115
|
-
"@tangle-network/sandbox": "0.
|
|
116
|
-
"@types/node": "26.
|
|
115
|
+
"@tangle-network/agent-provider-testkit": "0.10.0",
|
|
116
|
+
"@tangle-network/agent-runtime": "0.282.2",
|
|
117
|
+
"@tangle-network/sandbox": "0.55.3",
|
|
118
|
+
"@types/node": "26.6.3",
|
|
117
119
|
"typescript": "7.0.2",
|
|
118
|
-
"vitest": "
|
|
119
|
-
"@tangle-network/agent-provider-testkit": "0.10.0"
|
|
120
|
+
"vitest": "5.0.2"
|
|
120
121
|
},
|
|
121
122
|
"scripts": {
|
|
122
123
|
"build": "tsc -p tsconfig.json",
|