managed-deepagents 0.10.6-dev.5 → 0.10.6-dev.7
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/channels/slack/files.d.ts.map +1 -1
- package/dist/channels/slack/files.js +32 -17
- package/dist/channels/slack/files.js.map +1 -1
- package/dist/identity/loopback.d.ts +8 -0
- package/dist/identity/loopback.d.ts.map +1 -1
- package/dist/identity/loopback.js +13 -0
- package/dist/identity/loopback.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/auth.d.ts.map +1 -1
- package/dist/runtime/auth.js +31 -9
- package/dist/runtime/auth.js.map +1 -1
- package/dist/runtime/backend.d.ts.map +1 -1
- package/dist/runtime/backend.js +23 -20
- package/dist/runtime/backend.js.map +1 -1
- package/dist/runtime/sandbox-binding.d.ts +23 -0
- package/dist/runtime/sandbox-binding.d.ts.map +1 -0
- package/dist/runtime/sandbox-binding.js +130 -0
- package/dist/runtime/sandbox-binding.js.map +1 -0
- package/dist/runtime/sandbox-manager.d.ts +13 -6
- package/dist/runtime/sandbox-manager.d.ts.map +1 -1
- package/dist/runtime/sandbox-manager.js +85 -85
- package/dist/runtime/sandbox-manager.js.map +1 -1
- package/dist/runtime/schedule-auth.d.ts +9 -0
- package/dist/runtime/schedule-auth.d.ts.map +1 -1
- package/dist/runtime/schedule-auth.js +39 -4
- package/dist/runtime/schedule-auth.js.map +1 -1
- package/dist/runtime/thread-record.d.ts +2 -0
- package/dist/runtime/thread-record.d.ts.map +1 -1
- package/dist/runtime/thread-record.js +2 -0
- package/dist/runtime/thread-record.js.map +1 -1
- package/dist/sandbox.d.ts +15 -0
- package/dist/sandbox.d.ts.map +1 -1
- package/dist/sandbox.js +20 -0
- package/dist/sandbox.js.map +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SANDBOX_BINDING_METADATA } from "./thread-record.js";
|
|
2
|
+
export { SANDBOX_BINDING_METADATA };
|
|
3
|
+
/** The sandboxes a thread is bound to, as name to LangSmith sandbox id. */
|
|
4
|
+
export type BoundSandboxes = ReadonlyMap<string, string | null>;
|
|
5
|
+
/** The sandboxes a thread's metadata binds it to. */
|
|
6
|
+
export declare function boundSandboxes(metadata: unknown): Map<string, string | null>;
|
|
7
|
+
/** The thread's binding metadata with `name` bound to `sandboxId`. */
|
|
8
|
+
export declare function bindingWith(metadata: unknown, name: string, sandboxId: string | null): Record<string, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* The thread's bound sandboxes; `undefined` outside a deployed Agent Server.
|
|
11
|
+
*
|
|
12
|
+
* A failed read throws: without the record, MDA can't tell a new thread from
|
|
13
|
+
* one whose sandbox is gone.
|
|
14
|
+
*/
|
|
15
|
+
export declare function readBoundSandboxes(threadId: string, { deployed }: {
|
|
16
|
+
deployed: boolean;
|
|
17
|
+
}): Promise<BoundSandboxes | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Bind the thread to the sandbox, unless it already is. A failed write
|
|
20
|
+
* throws: an unbound thread could later get a new sandbox.
|
|
21
|
+
*/
|
|
22
|
+
export declare function recordBoundSandbox(threadId: string, name: string, sandboxId: string | null, bound: BoundSandboxes): Promise<void>;
|
|
23
|
+
//# sourceMappingURL=sandbox-binding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-binding.d.ts","sourceRoot":"","sources":["../../src/runtime/sandbox-binding.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,EAAE,wBAAwB,EAAE,CAAC;AAKpC,2EAA2E;AAC3E,MAAM,MAAM,cAAc,GAAG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAAC;AAEhE,qDAAqD;AACrD,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAa5E;AAED,sEAAsE;AACtE,wBAAgB,WAAW,CACzB,QAAQ,EAAE,OAAO,EACjB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAAG,IAAI,GACvB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAYzB;AAwCD;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,EAChB,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,EAAE,OAAO,CAAA;CAAE,GAClC,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAcrC;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,IAAI,CAAC,CA2Bf"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The thread's record of the LangSmith sandboxes that hold its work.
|
|
3
|
+
*
|
|
4
|
+
* A thread and its sandbox are one unit. MDA records each sandbox it creates or
|
|
5
|
+
* adopts for a thread in the thread's `ls_mda_sandbox_binding` metadata. When a
|
|
6
|
+
* bound sandbox is gone, or another sandbox now holds its name, the open fails
|
|
7
|
+
* with `SandboxMissingError`: MDA never creates a replacement.
|
|
8
|
+
*
|
|
9
|
+
* The record is keyed by sandbox name, because one thread can hold one sandbox
|
|
10
|
+
* per set of per-user mounts. A thread without a record is new, so MDA creates
|
|
11
|
+
* its sandbox.
|
|
12
|
+
*/
|
|
13
|
+
import { isRecord } from "../values.js";
|
|
14
|
+
import { logger } from "./log.js";
|
|
15
|
+
import { SANDBOX_BINDING_HEADER, handoffRequestConfig, } from "./schedule-auth.js";
|
|
16
|
+
import { SANDBOX_BINDING_METADATA } from "./thread-record.js";
|
|
17
|
+
export { SANDBOX_BINDING_METADATA };
|
|
18
|
+
const BINDING_VERSION = 1;
|
|
19
|
+
/** How long a sandbox open waits on the Agent Server for the thread's record. */
|
|
20
|
+
const THREAD_API_TIMEOUT_MS = 30_000;
|
|
21
|
+
/** The sandboxes a thread's metadata binds it to. */
|
|
22
|
+
export function boundSandboxes(metadata) {
|
|
23
|
+
const binding = isRecord(metadata)
|
|
24
|
+
? metadata[SANDBOX_BINDING_METADATA]
|
|
25
|
+
: undefined;
|
|
26
|
+
const sandboxes = isRecord(binding) ? binding.sandboxes : undefined;
|
|
27
|
+
const bound = new Map();
|
|
28
|
+
if (!isRecord(sandboxes))
|
|
29
|
+
return bound;
|
|
30
|
+
for (const [name, entry] of Object.entries(sandboxes)) {
|
|
31
|
+
if (!name)
|
|
32
|
+
continue;
|
|
33
|
+
const id = isRecord(entry) ? entry.id : undefined;
|
|
34
|
+
bound.set(name, typeof id === "string" && id ? id : null);
|
|
35
|
+
}
|
|
36
|
+
return bound;
|
|
37
|
+
}
|
|
38
|
+
/** The thread's binding metadata with `name` bound to `sandboxId`. */
|
|
39
|
+
export function bindingWith(metadata, name, sandboxId) {
|
|
40
|
+
const binding = isRecord(metadata)
|
|
41
|
+
? metadata[SANDBOX_BINDING_METADATA]
|
|
42
|
+
: undefined;
|
|
43
|
+
const sandboxes = isRecord(binding) ? binding.sandboxes : undefined;
|
|
44
|
+
return {
|
|
45
|
+
v: BINDING_VERSION,
|
|
46
|
+
sandboxes: {
|
|
47
|
+
...(isRecord(sandboxes) ? sandboxes : {}),
|
|
48
|
+
[name]: { id: sandboxId, bound_at: new Date().toISOString() },
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The thread's URL and the headers of a sandbox binding grant for it. The
|
|
54
|
+
* grant reads and writes the thread whoever owns it, because the run's caller
|
|
55
|
+
* may not own it. Throws without an authenticated Agent Server.
|
|
56
|
+
*/
|
|
57
|
+
function threadRequest(threadId) {
|
|
58
|
+
const { base, headers } = handoffRequestConfig(SANDBOX_BINDING_HEADER, {
|
|
59
|
+
thread: threadId,
|
|
60
|
+
});
|
|
61
|
+
return { url: `${base}/threads/${threadId}`, headers };
|
|
62
|
+
}
|
|
63
|
+
async function getThread(url, headers) {
|
|
64
|
+
const response = await fetch(url, {
|
|
65
|
+
headers,
|
|
66
|
+
redirect: "error",
|
|
67
|
+
signal: AbortSignal.timeout(THREAD_API_TIMEOUT_MS),
|
|
68
|
+
});
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
await response.body?.cancel();
|
|
71
|
+
throw new Error(`HTTP ${response.status}`);
|
|
72
|
+
}
|
|
73
|
+
return response.json();
|
|
74
|
+
}
|
|
75
|
+
function readError(threadId, error) {
|
|
76
|
+
return new Error(`Could not read thread ${threadId}'s sandbox binding; retry the run (${logger.safeErrorLabel(error)})`, { cause: error });
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The thread's bound sandboxes; `undefined` outside a deployed Agent Server.
|
|
80
|
+
*
|
|
81
|
+
* A failed read throws: without the record, MDA can't tell a new thread from
|
|
82
|
+
* one whose sandbox is gone.
|
|
83
|
+
*/
|
|
84
|
+
export async function readBoundSandboxes(threadId, { deployed }) {
|
|
85
|
+
let request;
|
|
86
|
+
try {
|
|
87
|
+
request = threadRequest(threadId);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (!deployed)
|
|
91
|
+
return undefined;
|
|
92
|
+
throw readError(threadId, error);
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const thread = await getThread(request.url, request.headers);
|
|
96
|
+
return boundSandboxes(isRecord(thread) ? thread.metadata : undefined);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
throw readError(threadId, error);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Bind the thread to the sandbox, unless it already is. A failed write
|
|
104
|
+
* throws: an unbound thread could later get a new sandbox.
|
|
105
|
+
*/
|
|
106
|
+
export async function recordBoundSandbox(threadId, name, sandboxId, bound) {
|
|
107
|
+
if (bound.has(name) && bound.get(name) === sandboxId)
|
|
108
|
+
return;
|
|
109
|
+
try {
|
|
110
|
+
const { url, headers } = threadRequest(threadId);
|
|
111
|
+
const thread = await getThread(url, headers);
|
|
112
|
+
const binding = bindingWith(isRecord(thread) ? thread.metadata : undefined, name, sandboxId);
|
|
113
|
+
const response = await fetch(url, {
|
|
114
|
+
method: "PATCH",
|
|
115
|
+
headers,
|
|
116
|
+
redirect: "error",
|
|
117
|
+
body: JSON.stringify({
|
|
118
|
+
metadata: { [SANDBOX_BINDING_METADATA]: binding },
|
|
119
|
+
}),
|
|
120
|
+
signal: AbortSignal.timeout(THREAD_API_TIMEOUT_MS),
|
|
121
|
+
});
|
|
122
|
+
await response.body?.cancel();
|
|
123
|
+
if (!response.ok)
|
|
124
|
+
throw new Error(`HTTP ${response.status}`);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
throw new Error(`Could not bind thread ${threadId} to sandbox ${name}; retry the run (${logger.safeErrorLabel(error)})`, { cause: error });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=sandbox-binding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-binding.js","sourceRoot":"","sources":["../../src/runtime/sandbox-binding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EACL,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAE9D,OAAO,EAAE,wBAAwB,EAAE,CAAC;AACpC,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,iFAAiF;AACjF,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAKrC,qD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O;YACf,OAAO;YACP,QAAQ,EAAE,OAAO;YACjB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;gBACnB,QAAQ,EAAE,EAAE,CAAC,wBAAwB,CAAC,EAAE,OAAO,EAAE;aAClD,CAAC;YACF,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,qBAAqB,CAAC;SACnD,CAAC,CAAC;QACH,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC;QAC9B,IAAI,CAAC,QAAQ,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC/D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,yBAAyB,QAAQ,eAAe,IAAI,oBAAoB,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG,EACvG,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type SandboxBackendProtocolV2 } from "deepagents";
|
|
2
2
|
import type { CreateSandboxOptions, SandboxClientConfig } from "langsmith/sandbox";
|
|
3
3
|
import type { IdentityDefinition } from "../identity/index.js";
|
|
4
|
-
import type
|
|
4
|
+
import { type SandboxDefinition } from "../sandbox.js";
|
|
5
5
|
import { type Connector } from "./connector.js";
|
|
6
6
|
import type { ManagedRunConfig } from "./types.js";
|
|
7
7
|
/** Deployment secret naming the recipe snapshot baked at `mda deploy`. */
|
|
@@ -17,8 +17,12 @@ export declare const SANDBOX_API_ENDPOINT_ENV = "MDA_SANDBOX_API_ENDPOINT";
|
|
|
17
17
|
* resolves `snapshotName` in the wrong workspace and 404s on the recipe.
|
|
18
18
|
*/
|
|
19
19
|
export declare const WORKSPACE_ID_ENV = "LANGSMITH_WORKSPACE_ID";
|
|
20
|
-
/**
|
|
21
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Seconds a stopped sandbox is kept before LangSmith deletes it, when the
|
|
22
|
+
* sandbox doesn't set `deleteAfterStopSeconds`: 30 days, the most the sandbox
|
|
23
|
+
* API accepts.
|
|
24
|
+
*/
|
|
25
|
+
export declare const DEFAULT_DELETE_AFTER_STOP_SECONDS: number;
|
|
22
26
|
/** Run `fn` with sandbox scope keys resolving to `leadThread`'s sandbox, if set. */
|
|
23
27
|
export declare function withLeadSandbox<T>(leadThread: string | undefined, fn: () => T): T;
|
|
24
28
|
/** Optional runtime inputs that drive connector sandbox provisioning. */
|
|
@@ -46,12 +50,15 @@ export declare function resolveManagedSandbox(sandbox: SandboxDefinition, config
|
|
|
46
50
|
export declare function claimManagedSandbox(sandbox: SandboxDefinition, config: ManagedRunConfig | undefined, options?: ResolveManagedSandboxOptions): Promise<string | undefined>;
|
|
47
51
|
/**
|
|
48
52
|
* The thread's sandbox if this process has opened it, by any caller; never
|
|
49
|
-
* opens one.
|
|
50
|
-
* replacement is ready. Like resolving, it needs `configurable.thread_id`.
|
|
53
|
+
* opens one. Like resolving, it needs `configurable.thread_id`.
|
|
51
54
|
*/
|
|
52
55
|
export declare function openedManagedSandbox(config: ManagedRunConfig | undefined, selection?: SandboxSelection): SandboxBackendProtocolV2 | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Drop `backend` from the process cache, if it still holds this scope, so the
|
|
58
|
+
* next open finds the thread's binding instead of a sandbox LangSmith reports gone.
|
|
59
|
+
*/
|
|
60
|
+
export declare function forgetManagedSandbox(key: string, backend: SandboxBackendProtocolV2): void;
|
|
53
61
|
export declare function isSandboxNotFoundError(error: unknown): boolean;
|
|
54
|
-
export declare function recoverManagedSandbox(sandbox: SandboxDefinition, config: ManagedRunConfig | undefined, failedBackend: SandboxBackendProtocolV2, options?: ResolveManagedSandboxOptions): Promise<SandboxBackendProtocolV2>;
|
|
55
62
|
/**
|
|
56
63
|
* Derive the process-level reuse key for a thread and any dynamic selection's mounts.
|
|
57
64
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox-manager.d.ts","sourceRoot":"","sources":["../../src/runtime/sandbox-manager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAoB,KAAK,wBAAwB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"sandbox-manager.d.ts","sourceRoot":"","sources":["../../src/runtime/sandbox-manager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAoB,KAAK,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAO7E,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EAAuB,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAK5E,OAAO,EAGL,KAAK,SAAS,EACf,MAAM,gBAAgB,CAAC;AAYxB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,0EAA0E;AAC1E,eAAO,MAAM,mBAAmB,gCAAgC,CAAC;AAEjE,oEAAoE;AACpE,eAAO,MAAM,wBAAwB,6BAA6B,CAAC;AAEnE;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,2BAA2B,CAAC;AAEzD;;;;GAIG;AACH,eAAO,MAAM,iCAAiC,QAAoB,CAAC;AAmFnE,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,CAAC,EAC/B,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,EAAE,EAAE,MAAM,CAAC,GACV,CAAC,CAEH;AAED,yEAAyE;AAEzE,MAAM,WAAW,4BAA4B;IAC3C,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;IACzB,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B,6CAA6C;IAC7C,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAC9B;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,iBAAiB,EAC1B,M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M,CAAC,UAAU,GACtB,MAAM,GAAG,SAAS,CAOpB;AAaD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,MAAM,CAMR;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,aAAa,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GAC1C,MAAM,CAQR;AAyJD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,iBAAiB,EAC3B,QAAQ,EAAE,iBAAiB,GAAG,SAAS,GACtC,iBAAiB,CAqBnB;AAED;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,oBAAoB,CAAC;CAC/B;AAED,mCAAmC;AACnC,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,iBAAiB,EAC1B,SAAS,CAAC,EAAE,gBAAgB,GAC3B,oBAAoB,CA8BtB;AAeD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,iBAAiB,GACzB,mBAAmB,GAAG,SAAS,CAYjC;AAwUD,iFAAiF;AACjF,wBAAsB,+BAA+B,CACnD,OAAO,EAAE,iBAAiB,EAC1B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,OAAO,mBAAmB,EAAE,kBAAkB,GAC1D,OAAO,CAAC,IAAI,CAAC,CAmCf"}
|
|
@@ -2,12 +2,14 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { hostname } from "node:os";
|
|
4
4
|
import { LangSmithSandbox } from "deepagents";
|
|
5
|
-
import { Sandbox, SandboxClient, LangSmithResourceNotFoundError,
|
|
5
|
+
import { Sandbox, SandboxClient, LangSmithResourceNotFoundError, LangSmithSandboxConnectionError, } from "langsmith/sandbox";
|
|
6
6
|
import { isConnectionReference } from "../connections.js";
|
|
7
|
+
import { SandboxMissingError } from "../sandbox.js";
|
|
7
8
|
import { validateAndCollectSandboxProxyConfigReferences, staticSandboxProxyConfig, } from "../sandbox-proxy.js";
|
|
8
9
|
import { hasSandboxConnectors, runConnectorSandboxHooks, } from "./connector.js";
|
|
9
10
|
import { resolveLangSmithEndpoint, resolveServerApiKey, } from "./langsmith-env.js";
|
|
10
11
|
import { logger } from "./log.js";
|
|
12
|
+
import { readBoundSandboxes, recordBoundSandbox, } from "./sandbox-binding.js";
|
|
11
13
|
import { HOST_SANDBOX_NAME_LIMIT } from "./sandbox-names.js";
|
|
12
14
|
/** Deployment secret naming the recipe snapshot baked at `mda deploy`. */
|
|
13
15
|
export const RECIPE_SNAPSHOT_ENV = "MDA_SANDBOX_RECIPE_SNAPSHOT";
|
|
@@ -22,6 +24,12 @@ export const SANDBOX_API_ENDPOINT_ENV = "MDA_SANDBOX_API_ENDPOINT";
|
|
|
22
24
|
* resolves `snapshotName` in the wrong workspace and 404s on the recipe.
|
|
23
25
|
*/
|
|
24
26
|
export const WORKSPACE_ID_ENV = "LANGSMITH_WORKSPACE_ID";
|
|
27
|
+
/**
|
|
28
|
+
* Seconds a stopped sandbox is kept before LangSmith deletes it, when the
|
|
29
|
+
* sandbox doesn't set `deleteAfterStopSeconds`: 30 days, the most the sandbox
|
|
30
|
+
* API accepts.
|
|
31
|
+
*/
|
|
32
|
+
export const DEFAULT_DELETE_AFTER_STOP_SECONDS = 30 * 24 * 60 * 60;
|
|
25
33
|
/** Header the LangSmith APIs use to scope a request to one workspace. */
|
|
26
34
|
const TENANT_HEADER = "X-Tenant-Id";
|
|
27
35
|
/**
|
|
@@ -37,7 +45,7 @@ const managedSandboxes = new Map();
|
|
|
37
45
|
* `managedSandboxes`.
|
|
38
46
|
*/
|
|
39
47
|
const claimedSandboxes = new Map();
|
|
40
|
-
/** Host box name for a resolved backend instance
|
|
48
|
+
/** Host box name for a resolved backend instance. */
|
|
41
49
|
const backendHostNames = new WeakMap();
|
|
42
50
|
/**
|
|
43
51
|
* Separator between the deployment name and the identity digest in a managed
|
|
@@ -63,10 +71,6 @@ const STOPPED_STATUS = "stopped";
|
|
|
63
71
|
const PROVISIONING_STATUS = "provisioning";
|
|
64
72
|
/** Lead thread whose sandbox this run shares; set only by the verified backend, never from config. */
|
|
65
73
|
const sharedLeadThread = new AsyncLocalStorage();
|
|
66
|
-
/** The lead thread whose sandbox the current call shares, if any. */
|
|
67
|
-
export function currentLeadThread() {
|
|
68
|
-
return sharedLeadThread.getStore();
|
|
69
|
-
}
|
|
70
74
|
/** Run `fn` with sandbox scope keys resolving to `leadThread`'s sandbox, if set. */
|
|
71
75
|
export function withLeadSandbox(leadThread, fn) {
|
|
72
76
|
return leadThread ? sharedLeadThread.run(leadThread, fn) : fn();
|
|
@@ -99,9 +103,8 @@ export function resolveManagedSandbox(sandbox, config, options) {
|
|
|
99
103
|
freshlyCreated: true,
|
|
100
104
|
selectionFingerprint: options?.selection?.fingerprint,
|
|
101
105
|
ready: Promise.resolve().then(async () => {
|
|
102
|
-
const { backend, hostName
|
|
106
|
+
const { backend, hostName } = await createLangSmithSandbox(sandbox, config, options, "create", key, { adoptedMode: sharesLeadSandbox ? "credentials" : "reuse", claim });
|
|
103
107
|
created.hostName = hostName;
|
|
104
|
-
created.sandboxId = sandboxId;
|
|
105
108
|
created.backend = backend;
|
|
106
109
|
if (hostName) {
|
|
107
110
|
backendHostNames.set(backend, hostName);
|
|
@@ -180,62 +183,25 @@ export async function claimManagedSandbox(sandbox, config, options) {
|
|
|
180
183
|
}
|
|
181
184
|
/**
|
|
182
185
|
* The thread's sandbox if this process has opened it, by any caller; never
|
|
183
|
-
* opens one.
|
|
184
|
-
* replacement is ready. Like resolving, it needs `configurable.thread_id`.
|
|
186
|
+
* opens one. Like resolving, it needs `configurable.thread_id`.
|
|
185
187
|
*/
|
|
186
188
|
export function openedManagedSandbox(config, selection) {
|
|
187
189
|
return managedSandboxes.get(sandboxScopeKey(config, selection))?.backend;
|
|
188
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Drop `backend` from the process cache, if it still holds this scope, so the
|
|
193
|
+
* next open finds the thread's binding instead of a sandbox LangSmith reports gone.
|
|
194
|
+
*/
|
|
195
|
+
export function forgetManagedSandbox(key, backend) {
|
|
196
|
+
if (managedSandboxes.get(key)?.backend === backend) {
|
|
197
|
+
managedSandboxes.delete(key);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
189
200
|
export function isSandboxNotFoundError(error) {
|
|
190
201
|
return (error instanceof LangSmithResourceNotFoundError ||
|
|
191
202
|
(error instanceof LangSmithSandboxConnectionError &&
|
|
192
203
|
/\bHTTP 404\b/.test(error.message)));
|
|
193
204
|
}
|
|
194
|
-
export async function recoverManagedSandbox(sandbox, config, failedBackend, options) {
|
|
195
|
-
const key = sandboxScopeKey(config, options?.selection);
|
|
196
|
-
const entry = managedSandboxes.get(key);
|
|
197
|
-
if (!entry)
|
|
198
|
-
return failedBackend;
|
|
199
|
-
const current = await entry.ready;
|
|
200
|
-
if (current !== failedBackend || !entry.hostName)
|
|
201
|
-
return current;
|
|
202
|
-
const client = new SandboxClient(sandboxClientOptions(sandbox));
|
|
203
|
-
let existing;
|
|
204
|
-
try {
|
|
205
|
-
existing = await client.getSandbox(entry.hostName);
|
|
206
|
-
}
|
|
207
|
-
catch (error) {
|
|
208
|
-
if (!(error instanceof LangSmithResourceNotFoundError))
|
|
209
|
-
throw error;
|
|
210
|
-
}
|
|
211
|
-
if (existing && existing.id === entry.sandboxId)
|
|
212
|
-
return current;
|
|
213
|
-
const latest = managedSandboxes.get(key);
|
|
214
|
-
if (latest !== entry)
|
|
215
|
-
return latest ? await latest.ready : current;
|
|
216
|
-
const replacement = {
|
|
217
|
-
syncedThreads: new Set([key]),
|
|
218
|
-
syncChain: Promise.resolve(),
|
|
219
|
-
freshlyCreated: false,
|
|
220
|
-
hostName: entry.hostName,
|
|
221
|
-
selectionFingerprint: options?.selection?.fingerprint,
|
|
222
|
-
ready: Promise.resolve().then(async () => {
|
|
223
|
-
const { backend, hostName, sandboxId } = await createLangSmithSandbox(sandbox, config, options, "create", key, { recoveryName: entry.hostName, existingSandbox: existing });
|
|
224
|
-
replacement.sandboxId = sandboxId;
|
|
225
|
-
replacement.backend = backend;
|
|
226
|
-
if (hostName)
|
|
227
|
-
backendHostNames.set(backend, hostName);
|
|
228
|
-
return backend;
|
|
229
|
-
}),
|
|
230
|
-
};
|
|
231
|
-
replacement.ready = replacement.ready.catch((error) => {
|
|
232
|
-
if (managedSandboxes.get(key) === replacement)
|
|
233
|
-
managedSandboxes.set(key, entry);
|
|
234
|
-
throw error;
|
|
235
|
-
});
|
|
236
|
-
managedSandboxes.set(key, replacement);
|
|
237
|
-
return await replacement.ready;
|
|
238
|
-
}
|
|
239
205
|
/**
|
|
240
206
|
* Derive the process-level reuse key for a thread and any dynamic selection's mounts.
|
|
241
207
|
*
|
|
@@ -478,12 +444,8 @@ function managedSandboxLookupNames(deployment, sandbox, config, selection) {
|
|
|
478
444
|
names.push(legacyManagedSandboxName(deployment, scopeKey, legacySandboxCreateOptions(sandbox)));
|
|
479
445
|
return [...new Set(names)];
|
|
480
446
|
}
|
|
481
|
-
async function createLangSmithSandbox(sandbox, config, options, mode, scopeKey, {
|
|
482
|
-
const { backend, adopted, hostName
|
|
483
|
-
claimLangSmithSandbox(sandbox, config, options?.selection, {
|
|
484
|
-
recoveryName,
|
|
485
|
-
existingSandbox,
|
|
486
|
-
})));
|
|
447
|
+
async function createLangSmithSandbox(sandbox, config, options, mode, scopeKey, { adoptedMode = "reuse", claim, } = {}) {
|
|
448
|
+
const { backend, adopted, hostName } = await createLangSmithSandboxBackend(sandbox, scopeKey, await (claim ?? claimLangSmithSandbox(sandbox, config, options?.selection)));
|
|
487
449
|
await runConnectorSandboxHooks(options?.connectors, {
|
|
488
450
|
backend,
|
|
489
451
|
// An adopted sandbox already ran create-time provisioning in an earlier
|
|
@@ -492,7 +454,7 @@ async function createLangSmithSandbox(sandbox, config, options, mode, scopeKey,
|
|
|
492
454
|
config,
|
|
493
455
|
identity: options?.identity,
|
|
494
456
|
});
|
|
495
|
-
return { backend, hostName
|
|
457
|
+
return { backend, hostName };
|
|
496
458
|
}
|
|
497
459
|
const BAKE_BASE_KEYS = ["snapshotId", "snapshotName", "dockerImage"];
|
|
498
460
|
function bakeBase(sandbox) {
|
|
@@ -531,6 +493,9 @@ export function inheritSetupRecipe(selected, declared) {
|
|
|
531
493
|
export function sandboxCreateOptions(sandbox, selection) {
|
|
532
494
|
const { apiKey: _apiKey, defaultTimeout: _defaultTimeout, snapshotName, snapshotId, dockerImage: _dockerImage, registry: _registry, proxyConfig, ...resolvedCreateOptions } = sandbox.options ?? {};
|
|
533
495
|
const options = { ...resolvedCreateOptions };
|
|
496
|
+
// MDA never replaces a thread's sandbox, so keep a stopped one long enough
|
|
497
|
+
// that a thread rarely outlives it.
|
|
498
|
+
options.deleteAfterStopSeconds ??= DEFAULT_DELETE_AFTER_STOP_SECONDS;
|
|
534
499
|
const request = { options };
|
|
535
500
|
const recipe = stringOption(process.env[RECIPE_SNAPSHOT_ENV]);
|
|
536
501
|
if (selection && snapshotId) {
|
|
@@ -607,6 +572,10 @@ async function createLangSmithSandboxBackend(sandbox, scopeKey, claim) {
|
|
|
607
572
|
const langSmithSandbox = await readySandbox(claim);
|
|
608
573
|
const { adopted } = claim;
|
|
609
574
|
const hostName = stringOption(langSmithSandbox.name) ?? claim.hostName;
|
|
575
|
+
// Bound only once it boots, so a sandbox that never booted never locks the thread.
|
|
576
|
+
if (claim.binding && hostName) {
|
|
577
|
+
await recordBoundSandbox(claim.binding.thread, hostName, stringOption(langSmithSandbox.id) ?? null, claim.binding.bound);
|
|
578
|
+
}
|
|
610
579
|
console.info(`[mda] sandbox resolve: ${adopted ? "adopted" : "created"} ` +
|
|
611
580
|
`name=${JSON.stringify(hostName ?? null)} ` +
|
|
612
581
|
`id=${JSON.stringify(langSmithSandbox.id ?? null)} ` +
|
|
@@ -618,7 +587,6 @@ async function createLangSmithSandboxBackend(sandbox, scopeKey, claim) {
|
|
|
618
587
|
}),
|
|
619
588
|
adopted,
|
|
620
589
|
hostName,
|
|
621
|
-
sandboxId: langSmithSandbox.id,
|
|
622
590
|
};
|
|
623
591
|
}
|
|
624
592
|
/** The claimed sandbox once it has booted; throws if it fails to boot. */
|
|
@@ -635,7 +603,7 @@ async function readySandbox(claim) {
|
|
|
635
603
|
* boots. A stopped sandbox still restarts here and waits: the SDK's start
|
|
636
604
|
* always waits.
|
|
637
605
|
*/
|
|
638
|
-
async function claimLangSmithSandbox(sandbox, config, selection, {
|
|
606
|
+
async function claimLangSmithSandbox(sandbox, config, selection, { wait = true } = {}) {
|
|
639
607
|
const createRequest = sandboxCreateOptions(sandbox, selection);
|
|
640
608
|
if (!wait) {
|
|
641
609
|
createRequest.options.waitForReady = false;
|
|
@@ -645,32 +613,25 @@ async function claimLangSmithSandbox(sandbox, config, selection, { recoveryName,
|
|
|
645
613
|
const lookupNames = deployment
|
|
646
614
|
? managedSandboxLookupNames(deployment, sandbox, config, selection)
|
|
647
615
|
: [];
|
|
648
|
-
const sandboxName =
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
616
|
+
const sandboxName = lookupNames[0];
|
|
617
|
+
const bindingThread = sandboxName ? sandboxBindingThread(config) : undefined;
|
|
618
|
+
const bound = bindingThread
|
|
619
|
+
? await readBoundSandboxes(bindingThread, {
|
|
620
|
+
deployed: managedDeploymentName(process.env) !== undefined,
|
|
621
|
+
})
|
|
622
|
+
: undefined;
|
|
623
|
+
const { sandbox: langSmithSandbox, adopted } = await resolveSandboxInstance(client, createRequest, sandboxName, lookupNames.slice(1), bound ?? new Map());
|
|
654
624
|
return {
|
|
655
625
|
client,
|
|
656
626
|
sandbox: langSmithSandbox,
|
|
657
627
|
adopted,
|
|
658
628
|
hostName: stringOption(langSmithSandbox.name) ?? sandboxName,
|
|
629
|
+
binding: bindingThread && bound ? { thread: bindingThread, bound } : undefined,
|
|
659
630
|
};
|
|
660
631
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
sandbox: await createHostSandbox(client, request, name),
|
|
665
|
-
adopted: false,
|
|
666
|
-
};
|
|
667
|
-
}
|
|
668
|
-
catch (error) {
|
|
669
|
-
if (!(error instanceof LangSmithResourceAlreadyExistsError) &&
|
|
670
|
-
!(error instanceof LangSmithResourceNameConflictError))
|
|
671
|
-
throw error;
|
|
672
|
-
return { sandbox: await client.getSandbox(name), adopted: true };
|
|
673
|
-
}
|
|
632
|
+
/** The thread whose metadata records this scope's sandbox: the lead's, when shared. */
|
|
633
|
+
function sandboxBindingThread(config) {
|
|
634
|
+
return sharedLeadThread.getStore() ?? threadIdFromConfig(config);
|
|
674
635
|
}
|
|
675
636
|
/**
|
|
676
637
|
* Adopt this deployment's existing sandbox for `name`, or create it.
|
|
@@ -682,8 +643,12 @@ async function createReplacementSandbox(client, request, name) {
|
|
|
682
643
|
* Only a confirmed not-found lookup permits a create, and the create never
|
|
683
644
|
* falls back to another name: a second sandbox would hold none of the thread's
|
|
684
645
|
* work, and replicas could each cache a different one.
|
|
646
|
+
*
|
|
647
|
+
* `bound` is the thread's record of its sandboxes. MDA never replaces one: a
|
|
648
|
+
* bound sandbox LangSmith confirms is gone, or one whose name now holds a
|
|
649
|
+
* different sandbox, fails with `SandboxMissingError`.
|
|
685
650
|
*/
|
|
686
|
-
async function resolveSandboxInstance(client, createRequest, name, adoptAliases
|
|
651
|
+
async function resolveSandboxInstance(client, createRequest, name, adoptAliases, bound) {
|
|
687
652
|
if (!name) {
|
|
688
653
|
return {
|
|
689
654
|
sandbox: await createHostSandbox(client, createRequest),
|
|
@@ -692,13 +657,34 @@ async function resolveSandboxInstance(client, createRequest, name, adoptAliases
|
|
|
692
657
|
}
|
|
693
658
|
// Aliases are 0.9.0 and 0.5.x names for this thread: reconnect a box that
|
|
694
659
|
// already exists, never create under the old formula.
|
|
695
|
-
const
|
|
660
|
+
const allCandidates = [
|
|
661
|
+
name,
|
|
662
|
+
...adoptAliases.filter((alias) => alias !== name),
|
|
663
|
+
];
|
|
664
|
+
const isBound = allCandidates.some((candidate) => bound.has(candidate));
|
|
665
|
+
// A bound thread reopens only the sandboxes it recorded, never an older alias.
|
|
666
|
+
const candidates = isBound
|
|
667
|
+
? allCandidates.filter((candidate) => bound.has(candidate))
|
|
668
|
+
: allCandidates;
|
|
696
669
|
for (const candidate of candidates) {
|
|
697
670
|
const existing = await adoptSandbox(client, candidate);
|
|
698
671
|
if (existing !== undefined) {
|
|
672
|
+
const boundId = bound.get(candidate);
|
|
673
|
+
const existingId = stringOption(existing.id);
|
|
674
|
+
if (boundId && existingId && boundId !== existingId) {
|
|
675
|
+
throw new SandboxMissingError(candidate, { replaced: true });
|
|
676
|
+
}
|
|
699
677
|
return { sandbox: existing, adopted: true };
|
|
700
678
|
}
|
|
701
679
|
}
|
|
680
|
+
if (isBound) {
|
|
681
|
+
for (const candidate of candidates) {
|
|
682
|
+
if (await sandboxExists(client, candidate)) {
|
|
683
|
+
throw new Error(`Sandbox ${JSON.stringify(candidate)} exists but cannot be used; retry the run`);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
throw new SandboxMissingError(name);
|
|
687
|
+
}
|
|
702
688
|
try {
|
|
703
689
|
return {
|
|
704
690
|
sandbox: await createHostSandbox(client, createRequest, name),
|
|
@@ -762,6 +748,20 @@ async function adoptSandbox(client, name) {
|
|
|
762
748
|
}
|
|
763
749
|
return undefined;
|
|
764
750
|
}
|
|
751
|
+
/** Whether anything holds `name`; anything but a confirmed not-found throws. */
|
|
752
|
+
async function sandboxExists(client, name) {
|
|
753
|
+
if (typeof client.getSandbox !== "function")
|
|
754
|
+
return false;
|
|
755
|
+
try {
|
|
756
|
+
await client.getSandbox(name);
|
|
757
|
+
return true;
|
|
758
|
+
}
|
|
759
|
+
catch (error) {
|
|
760
|
+
if (isSandboxNotFoundError(error))
|
|
761
|
+
return false;
|
|
762
|
+
throw new Error(`Could not look up sandbox ${JSON.stringify(name)}; retry the run: ${errorMessage(error)}`, { cause: error });
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
765
|
function errorMessage(error) {
|
|
766
766
|
return error instanceof Error ? error.message : String(error);
|
|
767
767
|
}
|