@qilin-agent/tools 0.0.0-stage → 3.1.4
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/LICENSE +21 -0
- package/README.i18n.yaml +103 -0
- package/README.md +241 -2
- package/README.zh.md +242 -0
- package/lib/index.js +3703 -0
- package/lib/types/index.d.ts +844 -0
- package/lib/types/index.js +1341 -0
- package/lib/types/json-schema.d.ts +106 -0
- package/lib/types/json-schema.js +577 -0
- package/lib/types/presentation.d.ts +367 -0
- package/lib/types/presentation.js +8 -0
- package/lib/types/ptc.d.ts +82 -0
- package/lib/types/ptc.js +662 -0
- package/lib/types/py-types.d.ts +48 -0
- package/lib/types/py-types.js +787 -0
- package/lib/types/schema.d.ts +243 -0
- package/lib/types/schema.js +350 -0
- package/lib/types/testing.d.ts +25 -0
- package/lib/types/testing.js +25 -0
- package/lib/types/ts-types.d.ts +37 -0
- package/lib/types/ts-types.js +291 -0
- package/lib/types/types.d.ts +64 -0
- package/lib/types/types.js +7 -0
- package/package.json +70 -3
package/lib/types/ptc.js
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PTC mode `run_code` transport. Programs call the registry's agent-visible
|
|
3
|
+
* tools through nested executions scheduled under the native concurrency
|
|
4
|
+
* contract; each sub-dispatch is logged for reconstruction, while only the
|
|
5
|
+
* outer curated result enters model history.
|
|
6
|
+
* @module @qilin-agent/tools/src/ptc
|
|
7
|
+
*/
|
|
8
|
+
import { brandString } from '@qilin-agent/brand';
|
|
9
|
+
import { createUserMessage, HarnessError } from '@qilin-agent/llm';
|
|
10
|
+
import { approveEscalation, ESCALATION_TARGETS, validateEscalationArgs } from '@qilin-agent/sandbox';
|
|
11
|
+
import { deepFreeze, snapshotJsonValue } from '@qilin-agent/util-values';
|
|
12
|
+
import { defineTool, parameterSchemaSpecToJsonSchema } from "./schema.js";
|
|
13
|
+
import { TOOL_RUNTIME_SCHEDULER } from "./index.js";
|
|
14
|
+
/** The model-facing name of the PTC mode tool. */
|
|
15
|
+
export const RUN_CODE_NAME = 'run_code';
|
|
16
|
+
/**
|
|
17
|
+
* The TypeScript flavor: the fallback for a schema read with no runtime
|
|
18
|
+
* mounted ({@link resolveFlavor} owns which readers reach that). A real
|
|
19
|
+
* assembly always resolves a runtime first, so the model never sees this
|
|
20
|
+
* fallback outside its own language.
|
|
21
|
+
*/
|
|
22
|
+
const TYPESCRIPT_FLAVOR = {
|
|
23
|
+
description: 'Execute a TypeScript program against the available tools. Takes two required '
|
|
24
|
+
+ 'arguments: `description`, a short summary of what the program does, and `code`, '
|
|
25
|
+
+ 'the BODY of an async function (erasable syntax only; top-level `await` and '
|
|
26
|
+
+ '`return` work). Call tools as `await tools.name(args)` per the declarations in the system '
|
|
27
|
+
+ 'prompt. Only what you print or return is program output — curate it. Image-bearing '
|
|
28
|
+
+ 'subtool results are attached after the run.',
|
|
29
|
+
codeDescription: 'The program: the body of an async TypeScript function.',
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The Python flavor: the body of an async function, top-level `await` and
|
|
33
|
+
* `return`, answer via `print` and/or the returned value, matching
|
|
34
|
+
* {@link ./py-types.ts}'s SDK instructions.
|
|
35
|
+
*/
|
|
36
|
+
const PYTHON_FLAVOR = {
|
|
37
|
+
description: 'Execute a Python program against the available tools. Takes two required '
|
|
38
|
+
+ 'arguments: `description`, a short summary of what the program does, and `code`, '
|
|
39
|
+
+ 'the BODY of an async function (top-level `await` and `return` work). Call tools as '
|
|
40
|
+
+ '`await tools.name(args)` per the declarations in the system prompt. Use '
|
|
41
|
+
+ '`print(...)` and/or `return <value>` for program output — curate it. Image-bearing '
|
|
42
|
+
+ 'subtool results are attached after the run.',
|
|
43
|
+
codeDescription: 'The program: the body of an async Python function.',
|
|
44
|
+
};
|
|
45
|
+
/** Per-language `run_code` schema flavors (see {@link RunCodeFlavor}); one entry per {@link PtcSdkLanguage}. */
|
|
46
|
+
const RUN_CODE_FLAVORS = {
|
|
47
|
+
typescript: TYPESCRIPT_FLAVOR,
|
|
48
|
+
python: PYTHON_FLAVOR,
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The `description` parameter's model-facing description: language-independent
|
|
52
|
+
* (the UI label contract is the same for every runtime), shared between the
|
|
53
|
+
* static spec and the language-aware `parameters` getter so the two emissions
|
|
54
|
+
* can never drift.
|
|
55
|
+
*/
|
|
56
|
+
const RUN_CODE_DESCRIPTION_PARAM_DESCRIPTION = 'Clear, concise description of what this program does in active voice, '
|
|
57
|
+
+ '5-10 words (shown in the UI). Provide `description` before `code` in the arguments. '
|
|
58
|
+
+ 'Examples: "Count TODO markers across packages"; '
|
|
59
|
+
+ '"Read failing test and its fixture"; "Rename config key in every cordis.yml".';
|
|
60
|
+
const RUN_CODE_CONTROLS = {
|
|
61
|
+
timeoutMs: { type: 'number', description: 'Positive elapsed-time budget in milliseconds, capped by the deployment maximum.' },
|
|
62
|
+
sandbox_permissions: { type: 'string', enum: [...ESCALATION_TARGETS], description: 'Wider sandbox mode for this complete program execution; requires justification and approval.' },
|
|
63
|
+
justification: { type: 'string', description: 'Reason this complete program needs wider access, shown to the user for approval.' },
|
|
64
|
+
};
|
|
65
|
+
function controlParameters(runtime) {
|
|
66
|
+
// Catalog readers have no mounted runtime; real model assembly requires one.
|
|
67
|
+
if (runtime === undefined)
|
|
68
|
+
return RUN_CODE_CONTROLS;
|
|
69
|
+
return {
|
|
70
|
+
...runtime.timeout === undefined ? {} : {
|
|
71
|
+
timeoutMs: { ...RUN_CODE_CONTROLS.timeoutMs,
|
|
72
|
+
description: `Positive elapsed-time budget in milliseconds, including nested tool and approval waits. Default ${runtime.timeout.defaultMs}; capped at ${runtime.timeout.maxMs}. Zero does not disable the deadline.` },
|
|
73
|
+
},
|
|
74
|
+
...runtime.sandboxMode === undefined ? {} : {
|
|
75
|
+
sandbox_permissions: RUN_CODE_CONTROLS.sandbox_permissions,
|
|
76
|
+
justification: RUN_CODE_CONTROLS.justification,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function escalationGuidance(runtime) {
|
|
81
|
+
return runtime?.sandboxMode === undefined ? ''
|
|
82
|
+
: ' A sandbox escalation approves this complete program for one execution only. Nested tools retain their own policies and approvals. Request wider access only after evidence of a denial. Earlier effects may already have completed: inspect them before explicitly retrying. Programs are never replayed automatically.';
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Resolve the {@link RunCodeFlavor} for the loaded runtime's language, read at
|
|
86
|
+
* schema-emission time so the model-visible `run_code` schema always matches
|
|
87
|
+
* the SDK section's language. `peekRuntime` returns `undefined` only when no
|
|
88
|
+
* runtime is mounted, which reaches this function through definition readers
|
|
89
|
+
* and `schemas()` — the doc-catalog harvest is the only shipped one, and none
|
|
90
|
+
* of them feeds a model, because `wireSchemas` calls `requirePtcRuntime`
|
|
91
|
+
* before projecting — so that path degrades to {@link TYPESCRIPT_FLAVOR}. A
|
|
92
|
+
* mounted runtime whose language has no flavor entry fails loud, exactly as
|
|
93
|
+
* `requirePtcRuntime` rejects it at assembly. Keeping this table in step with
|
|
94
|
+
* `SDK_RENDERERS` is the compiler's job ({@link PtcSdkLanguage}); what this
|
|
95
|
+
* guard owns is the runtime-supplied language neither table knows, which never
|
|
96
|
+
* yields a wrong-language schema for a real runtime.
|
|
97
|
+
*/
|
|
98
|
+
function resolveFlavor(peekRuntime) {
|
|
99
|
+
const runtime = peekRuntime();
|
|
100
|
+
if (runtime === undefined) {
|
|
101
|
+
// No runtime mounted: reached by definition readers and `schemas()`, of
|
|
102
|
+
// which the doc-catalog harvest is the only shipped one. None feeds a
|
|
103
|
+
// model — `wireSchemas` calls `requirePtcRuntime` before projecting, so
|
|
104
|
+
// the assembly path never arrives here. Degrade to the TS default.
|
|
105
|
+
return TYPESCRIPT_FLAVOR;
|
|
106
|
+
}
|
|
107
|
+
// Own-property read: a language like `toString`/`constructor` would otherwise
|
|
108
|
+
// resolve an inherited Object.prototype member as a flavor.
|
|
109
|
+
const flavor = RUN_CODE_FLAVORS[runtime.language];
|
|
110
|
+
if (!Object.hasOwn(RUN_CODE_FLAVORS, runtime.language) || flavor === undefined) {
|
|
111
|
+
const known = Object.keys(RUN_CODE_FLAVORS).map(name => JSON.stringify(name)).join(', ');
|
|
112
|
+
throw new Error(`qilin-tools: no run_code schema flavor registered for runtime language ${JSON.stringify(runtime.language)} (known: ${known})`);
|
|
113
|
+
}
|
|
114
|
+
return flavor;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Thrown by `run_code` when the program run itself failed — a program
|
|
118
|
+
* exception, a budget expiry, an abort, or substrate death. Extends
|
|
119
|
+
* {@link HarnessError} (`code: 'CODE_RUN_FAILED'`); the registry's execution
|
|
120
|
+
* pipeline converts it into a structured `isError` result whose text carries
|
|
121
|
+
* the failure kind plus the captured logs, so the model can self-correct.
|
|
122
|
+
*/
|
|
123
|
+
export class CodeRunFailedError extends HarnessError {
|
|
124
|
+
constructor(message) {
|
|
125
|
+
super(message, 'CODE_RUN_FAILED');
|
|
126
|
+
this.name = 'CodeRunFailedError';
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Snapshot one binding call's argument as lossless JSON, then snapshot that
|
|
131
|
+
* detached value again so dispatch and logging stay independent without
|
|
132
|
+
* reintroducing structured-clone's platform-specific nesting limit.
|
|
133
|
+
*/
|
|
134
|
+
function jsonNormalizeArgs(value) {
|
|
135
|
+
let snapshot;
|
|
136
|
+
try {
|
|
137
|
+
snapshot = snapshotJsonValue(value);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
throw new Error(`tool arguments must be lossless JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
141
|
+
}
|
|
142
|
+
if (snapshot === undefined) {
|
|
143
|
+
throw new Error('tool arguments must be lossless JSON (call the tool with an arguments object, e.g. `{}`)');
|
|
144
|
+
}
|
|
145
|
+
const logged = snapshotJsonValue(snapshot);
|
|
146
|
+
/* v8 ignore next -- snapshot is already a detached lossless JSON value. */
|
|
147
|
+
if (logged === undefined) {
|
|
148
|
+
throw new Error('tool arguments could not be detached for durable logging');
|
|
149
|
+
}
|
|
150
|
+
return { dispatched: snapshot, logged };
|
|
151
|
+
}
|
|
152
|
+
/** Two-space JSON presentation, matching the existing shallow `run_code` text contract. */
|
|
153
|
+
const JSON_INDENT = ' ';
|
|
154
|
+
/**
|
|
155
|
+
* ECMAScript caps `JSON.stringify`'s `space` string at ten characters. The
|
|
156
|
+
* renderer also caps TOTAL indentation there, compacting deeper subtrees, so
|
|
157
|
+
* formatted output remains linear in the canonical JSON size.
|
|
158
|
+
*/
|
|
159
|
+
const MAX_JSON_INDENT_CHARS = 10;
|
|
160
|
+
/** Render one non-string JSON root without recursive traversal or unbounded indentation growth. */
|
|
161
|
+
function renderJsonValue(value) {
|
|
162
|
+
const chunks = [];
|
|
163
|
+
const tasks = [{ kind: 'value', value, depth: 0, compact: false }];
|
|
164
|
+
for (let task = tasks.pop(); task !== undefined; task = tasks.pop()) {
|
|
165
|
+
if (task.kind === 'text') {
|
|
166
|
+
chunks.push(task.text);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const current = task.value;
|
|
170
|
+
if (current === null || typeof current === 'boolean' || typeof current === 'number') {
|
|
171
|
+
chunks.push(String(current));
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (typeof current === 'string') {
|
|
175
|
+
chunks.push(JSON.stringify(current));
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const compact = task.compact || (task.depth + 1) * JSON_INDENT.length > MAX_JSON_INDENT_CHARS;
|
|
179
|
+
const childDepth = task.depth + 1;
|
|
180
|
+
if (Array.isArray(current)) {
|
|
181
|
+
chunks.push('[');
|
|
182
|
+
if (current.length === 0) {
|
|
183
|
+
chunks.push(']');
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
tasks.push({ kind: 'text', text: compact ? ']' : `\n${JSON_INDENT.repeat(task.depth)}]` });
|
|
187
|
+
for (let index = current.length - 1; index >= 0; index--) {
|
|
188
|
+
const item = current[index];
|
|
189
|
+
/* v8 ignore next -- canonical JsonValue arrays are dense. */
|
|
190
|
+
if (item === undefined)
|
|
191
|
+
throw new Error('cannot render a sparse JSON array');
|
|
192
|
+
tasks.push({ kind: 'value', value: item, depth: childDepth, compact });
|
|
193
|
+
tasks.push({
|
|
194
|
+
kind: 'text',
|
|
195
|
+
text: compact
|
|
196
|
+
? index === 0 ? '' : ','
|
|
197
|
+
: `${index === 0 ? '\n' : ',\n'}${JSON_INDENT.repeat(childDepth)}`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const keys = Object.keys(current);
|
|
203
|
+
chunks.push('{');
|
|
204
|
+
if (keys.length === 0) {
|
|
205
|
+
chunks.push('}');
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
tasks.push({ kind: 'text', text: compact ? '}' : `\n${JSON_INDENT.repeat(task.depth)}}` });
|
|
209
|
+
for (let index = keys.length - 1; index >= 0; index--) {
|
|
210
|
+
const key = keys[index];
|
|
211
|
+
/* v8 ignore next -- the loop is bounded by the captured key count. */
|
|
212
|
+
if (key === undefined)
|
|
213
|
+
throw new Error('cannot render a missing JSON object key');
|
|
214
|
+
const item = current[key];
|
|
215
|
+
/* v8 ignore next -- canonical JsonValue records contain no undefined properties. */
|
|
216
|
+
if (item === undefined)
|
|
217
|
+
throw new Error('cannot render an undefined JSON object property');
|
|
218
|
+
tasks.push({ kind: 'value', value: item, depth: childDepth, compact });
|
|
219
|
+
tasks.push({
|
|
220
|
+
kind: 'text',
|
|
221
|
+
text: compact
|
|
222
|
+
? `${index === 0 ? '' : ','}${JSON.stringify(key)}:`
|
|
223
|
+
: `${index === 0 ? '\n' : ',\n'}${JSON_INDENT.repeat(childDepth)}${JSON.stringify(key)}: `,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return chunks.join('');
|
|
228
|
+
}
|
|
229
|
+
/** Render one present program completion value for the model-facing result text. */
|
|
230
|
+
function renderValue(value) {
|
|
231
|
+
return typeof value === 'string' ? value : renderJsonValue(value);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Build the `run_code` {@link ToolDefinition}: required `description` and
|
|
235
|
+
* `code` parameters, executed through the dispatch bridge described
|
|
236
|
+
* above. The
|
|
237
|
+
* registry reserves it as presentation infrastructure under non-native modes,
|
|
238
|
+
* outside the filterable global/scoped capability layers.
|
|
239
|
+
* @param registry - the owning registry (sub-calls go through its `execute`,
|
|
240
|
+
* bindings cover its registered tools).
|
|
241
|
+
* @param options - the registry-private capabilities described above.
|
|
242
|
+
* @returns the registry-ready definition.
|
|
243
|
+
*/
|
|
244
|
+
export function createRunCodeTool(registry, options) {
|
|
245
|
+
const { requireRuntime, peekRuntime, maxParallel, shapeDispatchLog } = options;
|
|
246
|
+
const definition = defineTool({
|
|
247
|
+
name: RUN_CODE_NAME,
|
|
248
|
+
// The description and `code` parameter description are placeholders here:
|
|
249
|
+
// the language-aware getters installed below replace both, resolving the
|
|
250
|
+
// loaded runtime's flavor at schema-emission time so the schema the MODEL
|
|
251
|
+
// sees matches the SDK section's language. Argument VALIDATION still keys
|
|
252
|
+
// off this static spec (defineTool closes over it), which is language-
|
|
253
|
+
// independent (one required string `code`).
|
|
254
|
+
description: TYPESCRIPT_FLAVOR.description,
|
|
255
|
+
parameters: {
|
|
256
|
+
description: {
|
|
257
|
+
type: 'string',
|
|
258
|
+
required: true,
|
|
259
|
+
description: RUN_CODE_DESCRIPTION_PARAM_DESCRIPTION,
|
|
260
|
+
},
|
|
261
|
+
code: { type: 'string', required: true, description: TYPESCRIPT_FLAVOR.codeDescription },
|
|
262
|
+
...RUN_CODE_CONTROLS,
|
|
263
|
+
},
|
|
264
|
+
output: {
|
|
265
|
+
schema: {
|
|
266
|
+
type: 'object',
|
|
267
|
+
additionalProperties: false,
|
|
268
|
+
properties: {
|
|
269
|
+
logs: { type: 'array', required: true, items: { type: 'string' } },
|
|
270
|
+
result: { type: 'json' },
|
|
271
|
+
sandbox: {
|
|
272
|
+
type: 'object',
|
|
273
|
+
additionalProperties: false,
|
|
274
|
+
properties: {
|
|
275
|
+
mode: { type: 'string', required: true, enum: ['read-only', 'workspace-write', 'danger-full-access'] },
|
|
276
|
+
denied: { type: 'boolean', required: true },
|
|
277
|
+
enforcement: { type: 'string', enum: ['full', 'partial'] },
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
render: (_args, value) => {
|
|
283
|
+
const rendered = value.result === undefined ? '' : renderValue(value.result);
|
|
284
|
+
const parts = [value.logs.join('\n'), rendered].filter(part => part.length > 0);
|
|
285
|
+
if (value.sandbox?.enforcement === 'partial')
|
|
286
|
+
parts.push('File sandbox enforcement is partial on this host.');
|
|
287
|
+
if (value.sandbox?.denied)
|
|
288
|
+
parts.push(`The ${value.sandbox.mode} file sandbox denied an operation.${escalationGuidance(peekRuntime())}`);
|
|
289
|
+
return [{ type: 'text', text: parts.length > 0 ? parts.join('\n') : '(run_code completed with no output)' }];
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
async execute(args, exec) {
|
|
293
|
+
if (args.description.trim().length === 0) {
|
|
294
|
+
throw new Error('invalid description: expected a non-empty string');
|
|
295
|
+
}
|
|
296
|
+
const runtime = requireRuntime();
|
|
297
|
+
validateEscalationArgs(args.sandbox_permissions, args.justification);
|
|
298
|
+
if (args.timeoutMs !== undefined && runtime.timeout === undefined) {
|
|
299
|
+
throw new Error('timeoutMs is not available for this PTC runtime');
|
|
300
|
+
}
|
|
301
|
+
if (args.timeoutMs !== undefined && (!Number.isFinite(args.timeoutMs) || args.timeoutMs <= 0)) {
|
|
302
|
+
throw new Error('invalid timeoutMs: expected a positive finite number');
|
|
303
|
+
}
|
|
304
|
+
const standingPolicy = runtime.sandboxMode === undefined ? undefined : options.resolveSandboxPolicy(exec);
|
|
305
|
+
let policy = standingPolicy;
|
|
306
|
+
if (args.sandbox_permissions !== undefined && args.justification !== undefined) {
|
|
307
|
+
if (standingPolicy === undefined)
|
|
308
|
+
throw new Error('sandbox_permissions is not available for this PTC runtime');
|
|
309
|
+
const approvedMode = await approveEscalation({
|
|
310
|
+
requestedMode: args.sandbox_permissions,
|
|
311
|
+
justification: args.justification,
|
|
312
|
+
effectiveMode: standingPolicy.mode,
|
|
313
|
+
subject: 'program',
|
|
314
|
+
}, {
|
|
315
|
+
approver: options.peekApprover(), agent: exec.agent, callId: exec.callId,
|
|
316
|
+
toolName: RUN_CODE_NAME, signal: exec.signal,
|
|
317
|
+
});
|
|
318
|
+
policy = { ...standingPolicy, mode: approvedMode };
|
|
319
|
+
}
|
|
320
|
+
const cwd = await options.resolveWorkingDirectory(exec);
|
|
321
|
+
exec.signal.throwIfAborted();
|
|
322
|
+
// The run-scoped abort: follows the outer signal in, and fires when the
|
|
323
|
+
// run settles for ANY reason, so an in-flight sub-dispatch is aborted
|
|
324
|
+
// (its executor kills on this signal) instead of orphaned, and
|
|
325
|
+
// queued-unstarted dispatches are abandoned.
|
|
326
|
+
const runController = new AbortController();
|
|
327
|
+
const onOuterAbort = () => { runController.abort(exec.signal.reason); };
|
|
328
|
+
exec.signal.addEventListener('abort', onOuterAbort, { once: true });
|
|
329
|
+
let dispatches = 0;
|
|
330
|
+
const pendingQueue = [];
|
|
331
|
+
const inFlight = new Set();
|
|
332
|
+
/** Tracked settle-event side work (log-content listener + append), drained at run settlement. */
|
|
333
|
+
const logWork = new Set();
|
|
334
|
+
const commitQueue = [];
|
|
335
|
+
let exclusiveActive = false;
|
|
336
|
+
let driving = false;
|
|
337
|
+
let driverRun = Promise.resolve();
|
|
338
|
+
let wake;
|
|
339
|
+
const wakeup = () => {
|
|
340
|
+
const release = wake;
|
|
341
|
+
wake = undefined;
|
|
342
|
+
release?.();
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* The single ordered lane. Each pass commits the head-of-line settled
|
|
346
|
+
* dispatch (ordered post-execute), then starts the next queued entry if
|
|
347
|
+
* its slot is free (ordered pre-execute), and otherwise sleeps until a
|
|
348
|
+
* body settles or a new submission arrives. One run reaching the
|
|
349
|
+
* empty-queues/empty-pool state is quiescence.
|
|
350
|
+
*/
|
|
351
|
+
const drive = () => {
|
|
352
|
+
if (driving)
|
|
353
|
+
return driverRun;
|
|
354
|
+
driving = true;
|
|
355
|
+
driverRun = (async () => {
|
|
356
|
+
try {
|
|
357
|
+
for (;;) {
|
|
358
|
+
// Create the wakeup promise before inspecting state so a settle or submission arriving
|
|
359
|
+
// between the checks and the await below cannot be lost.
|
|
360
|
+
const signal = new Promise((resolve) => { wake = resolve; });
|
|
361
|
+
const commitHead = commitQueue[0];
|
|
362
|
+
if (commitHead !== undefined && commitHead.settled) {
|
|
363
|
+
commitQueue.shift();
|
|
364
|
+
await commitHead.commit();
|
|
365
|
+
// The barrier covers post-execute: later starts wait for the
|
|
366
|
+
// exclusive call's full pipeline, as under the native loop.
|
|
367
|
+
if (commitHead.mode === 'exclusive')
|
|
368
|
+
exclusiveActive = false;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
const head = pendingQueue[0];
|
|
372
|
+
if (head !== undefined) {
|
|
373
|
+
if (runController.signal.aborted) {
|
|
374
|
+
pendingQueue.shift();
|
|
375
|
+
head.abandon();
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
// Reclassify at start time (fail-closed on registry changes).
|
|
379
|
+
const mode = head.classify();
|
|
380
|
+
const capacity = !exclusiveActive
|
|
381
|
+
&& (mode === 'exclusive' ? inFlight.size === 0 : inFlight.size < maxParallel);
|
|
382
|
+
if (capacity) {
|
|
383
|
+
if (mode === 'exclusive')
|
|
384
|
+
exclusiveActive = true;
|
|
385
|
+
head.mode = mode;
|
|
386
|
+
pendingQueue.shift();
|
|
387
|
+
// Joined before start() so the commit cursor sees submission
|
|
388
|
+
// order; nothing commits it until `settled` flips.
|
|
389
|
+
commitQueue.push(head);
|
|
390
|
+
await head.start();
|
|
391
|
+
const flight = head.flight.finally(() => {
|
|
392
|
+
inFlight.delete(flight);
|
|
393
|
+
wakeup();
|
|
394
|
+
});
|
|
395
|
+
inFlight.add(flight);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (pendingQueue.length === 0 && commitQueue.length === 0 && inFlight.size === 0)
|
|
400
|
+
return;
|
|
401
|
+
await signal;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
finally {
|
|
405
|
+
driving = false;
|
|
406
|
+
wake = undefined;
|
|
407
|
+
}
|
|
408
|
+
})();
|
|
409
|
+
return driverRun;
|
|
410
|
+
};
|
|
411
|
+
/** Every dispatch settled AND committed; nothing can start (the run is aborted at call time). */
|
|
412
|
+
const drainDispatches = async () => {
|
|
413
|
+
// The abort already fired: the driver abandons queued-unstarted
|
|
414
|
+
// entries, awaits the live pool, and drains the ordered commit lane —
|
|
415
|
+
// including a commit already in progress when the program returned.
|
|
416
|
+
await drive();
|
|
417
|
+
// Every settle event is appended inside the open run_code turn
|
|
418
|
+
// (tasks self-remove on settlement).
|
|
419
|
+
while (logWork.size > 0)
|
|
420
|
+
await Promise.allSettled([...logWork]);
|
|
421
|
+
};
|
|
422
|
+
// Read through a call, not a bare property: the abort state genuinely
|
|
423
|
+
// changes across awaits, and a direct `.aborted` re-check after one
|
|
424
|
+
// would be narrowed away by control flow analysis.
|
|
425
|
+
const runOver = () => runController.signal.aborted;
|
|
426
|
+
const binding = (schema) => async (rawArgs) => {
|
|
427
|
+
const { name } = schema;
|
|
428
|
+
if (runOver()) {
|
|
429
|
+
throw new Error(`run_code run is over (${String(runController.signal.reason)}); ${name} not dispatched`);
|
|
430
|
+
}
|
|
431
|
+
const normalized = jsonNormalizeArgs(rawArgs);
|
|
432
|
+
const n = ++dispatches;
|
|
433
|
+
const subCallId = brandString(`${String(exec.callId)}:ptc:${n}`);
|
|
434
|
+
const input = {
|
|
435
|
+
callId: subCallId,
|
|
436
|
+
rootCallId: exec.rootCallId,
|
|
437
|
+
name,
|
|
438
|
+
schema,
|
|
439
|
+
arguments: normalized.dispatched,
|
|
440
|
+
...exec.agent ? { agent: exec.agent } : {},
|
|
441
|
+
parent: exec.token,
|
|
442
|
+
signal: runController.signal,
|
|
443
|
+
};
|
|
444
|
+
const scheduler = registry[TOOL_RUNTIME_SCHEDULER];
|
|
445
|
+
const outcome = await new Promise((resolve, reject) => {
|
|
446
|
+
// Set by the dispatch stage (or start() for a pre-settled result): what commit() finalizes in submission order.
|
|
447
|
+
let parked;
|
|
448
|
+
const settle = (result) => {
|
|
449
|
+
// The program gets its value NOW: the log-content listener (for
|
|
450
|
+
// example, a spill backend) must never delay the binding or occupy
|
|
451
|
+
// a dispatch slot. The event append is tracked side work; the run's
|
|
452
|
+
// settlement drains logWork so every settle event is still appended
|
|
453
|
+
// inside the open turn (shapeDispatchLog is contained, so this
|
|
454
|
+
// chain cannot reject).
|
|
455
|
+
resolve(result.isError
|
|
456
|
+
? { isError: true, message: result.error.message }
|
|
457
|
+
: { isError: false, value: result.value });
|
|
458
|
+
const agent = exec.agent;
|
|
459
|
+
if (agent === undefined)
|
|
460
|
+
return;
|
|
461
|
+
const task = (async () => {
|
|
462
|
+
// The listener may replace the durable copy with a preview and
|
|
463
|
+
// locator; the program's value and model-visible result are
|
|
464
|
+
// untouched.
|
|
465
|
+
const logged = await shapeDispatchLog({
|
|
466
|
+
exec, agent, subCallId, name, isError: result.isError,
|
|
467
|
+
// The registry deep-froze this projection at result
|
|
468
|
+
// finalization; append snapshots the final copy again, so
|
|
469
|
+
// the log stays detached.
|
|
470
|
+
content: result.content,
|
|
471
|
+
});
|
|
472
|
+
agent.session.append('tool/ptc-dispatch', {
|
|
473
|
+
rootCallId: exec.rootCallId,
|
|
474
|
+
parentCallId: exec.callId,
|
|
475
|
+
subCallId,
|
|
476
|
+
name,
|
|
477
|
+
// The SIBLING parse of the dispatched value: byte-identical JSON,
|
|
478
|
+
// but a separate object — a tool mutating its args cannot desync
|
|
479
|
+
// this record from what it actually received.
|
|
480
|
+
arguments: normalized.logged,
|
|
481
|
+
isError: result.isError,
|
|
482
|
+
...result.error?.info === undefined ? {} : { error: result.error.info },
|
|
483
|
+
content: logged,
|
|
484
|
+
...result.meta === undefined ? {} : { meta: result.meta },
|
|
485
|
+
});
|
|
486
|
+
})().finally(() => { logWork.delete(task); });
|
|
487
|
+
logWork.add(task);
|
|
488
|
+
};
|
|
489
|
+
pendingQueue.push({
|
|
490
|
+
flight: Promise.resolve(),
|
|
491
|
+
settled: false,
|
|
492
|
+
// Re-read per driver pass against the same agent view the SDK
|
|
493
|
+
// declared; fail-closed exclusive when undeclared/invalid.
|
|
494
|
+
classify: () => registry.executionMode(input).kind,
|
|
495
|
+
abandon: () => {
|
|
496
|
+
reject(new Error(`run_code run is over (${String(runController.signal.reason)}); ${name} tool call abandoned`));
|
|
497
|
+
},
|
|
498
|
+
async start() {
|
|
499
|
+
exec.agent?.session.append('tool/ptc-dispatch-start', {
|
|
500
|
+
rootCallId: exec.rootCallId,
|
|
501
|
+
parentCallId: exec.callId,
|
|
502
|
+
subCallId,
|
|
503
|
+
name,
|
|
504
|
+
arguments: normalized.logged,
|
|
505
|
+
});
|
|
506
|
+
// Ordered prepare runs INSIDE the driver lane: the next entry's
|
|
507
|
+
// pre-execute waits for this resolution, as under the native
|
|
508
|
+
// scheduler. Only the launched body below overlaps.
|
|
509
|
+
const prepared = await scheduler.prepare(input);
|
|
510
|
+
if (prepared.kind === 'dispatch') {
|
|
511
|
+
this.flight = scheduler.dispatch(prepared.exec).then((dispatchOutcome) => {
|
|
512
|
+
parked = { kind: dispatchOutcome.kind, exec: prepared.exec, result: dispatchOutcome.result };
|
|
513
|
+
this.settled = true;
|
|
514
|
+
});
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
parked = { kind: prepared.kind, exec: prepared.exec, result: prepared.result };
|
|
518
|
+
this.settled = true;
|
|
519
|
+
},
|
|
520
|
+
async commit() {
|
|
521
|
+
/* v8 ignore next -- commit() runs only after `settled` flipped, which set parked. */
|
|
522
|
+
if (parked === undefined)
|
|
523
|
+
return;
|
|
524
|
+
const result = parked.kind === 'post-result'
|
|
525
|
+
? await scheduler.finalize(parked.exec, parked.result)
|
|
526
|
+
: scheduler.finish(parked.exec, parked.result);
|
|
527
|
+
if (!result.isError && result.content.some(block => block.type === 'image')) {
|
|
528
|
+
exec.deferContext(createUserMessage({
|
|
529
|
+
content: result.content,
|
|
530
|
+
source: { kind: 'ptc-mode' },
|
|
531
|
+
}));
|
|
532
|
+
}
|
|
533
|
+
for (const context of result.additionalContexts ?? []) {
|
|
534
|
+
exec.deferContext(context);
|
|
535
|
+
}
|
|
536
|
+
// The composite forwards `additionalContexts` above and
|
|
537
|
+
// `concludesTurn` here from the nested result. Only a successful
|
|
538
|
+
// nested result can carry the terminal marker
|
|
539
|
+
// (ToolExecutionFailure types it never), so a policy-converted
|
|
540
|
+
// failure cannot stop the turn through a recovering program.
|
|
541
|
+
if (result.concludesTurn)
|
|
542
|
+
exec.concludeTurn();
|
|
543
|
+
settle(result);
|
|
544
|
+
// Backpressure on pending event-append tasks: each task retains
|
|
545
|
+
// a full result while a slow backend stores it, so the pool cap
|
|
546
|
+
// bounds their count. Beyond the cap, the
|
|
547
|
+
// ordered lane waits, so later sub-calls cannot start and
|
|
548
|
+
// pending I/O/memory cannot grow without bound.
|
|
549
|
+
while (logWork.size > maxParallel)
|
|
550
|
+
await Promise.race(logWork);
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
wakeup();
|
|
554
|
+
void drive();
|
|
555
|
+
});
|
|
556
|
+
// A budget expiry or outer cancel that occurs while this call was in
|
|
557
|
+
// flight already aborted the dispatch; stop the program now rather
|
|
558
|
+
// than hand it a result from a run that is over.
|
|
559
|
+
if (runOver()) {
|
|
560
|
+
throw new Error(`run_code run is over (${String(runController.signal.reason)}); ${name} result discarded`);
|
|
561
|
+
}
|
|
562
|
+
// The worker turns a binding rejection into ToolCallError and adds
|
|
563
|
+
// only the binding name. Native content and internal error metadata
|
|
564
|
+
// stay outside the program-facing failure contract.
|
|
565
|
+
if (outcome.isError)
|
|
566
|
+
throw new Error(outcome.message);
|
|
567
|
+
return outcome.value;
|
|
568
|
+
};
|
|
569
|
+
// Null-prototype + defineProperty, mirroring the worker-side namespace
|
|
570
|
+
// build: a registered tool named `__proto__` must become an ordinary
|
|
571
|
+
// own key (a plain-object assignment would hit the prototype setter,
|
|
572
|
+
// silently dropping the binding), and the runtime host resolves
|
|
573
|
+
// binding names as own properties only.
|
|
574
|
+
const functions = Object.create(null);
|
|
575
|
+
// Enumerate the CALLING AGENT's visible set (scoped tools join,
|
|
576
|
+
// restricted globals vanish) — the same view the SDK section declared,
|
|
577
|
+
// so a program can bind exactly what its prompt promised; sub-dispatch
|
|
578
|
+
// re-resolves per call through the same view (exec.agent threads down).
|
|
579
|
+
for (const schema of registry.schemas(exec.agent)) {
|
|
580
|
+
if (schema.name === RUN_CODE_NAME)
|
|
581
|
+
continue;
|
|
582
|
+
Object.defineProperty(functions, schema.name, { enumerable: true, value: binding(deepFreeze(schema)) });
|
|
583
|
+
}
|
|
584
|
+
try {
|
|
585
|
+
let result;
|
|
586
|
+
try {
|
|
587
|
+
result = await runtime.run(runtime.resolve({
|
|
588
|
+
program: args.code,
|
|
589
|
+
bindings: [{
|
|
590
|
+
global: 'tools',
|
|
591
|
+
functions,
|
|
592
|
+
errorClass: { name: 'ToolCallError', memberNameProperty: 'toolName' },
|
|
593
|
+
}],
|
|
594
|
+
signal: runController.signal,
|
|
595
|
+
...cwd !== undefined ? { cwd } : {},
|
|
596
|
+
...policy !== undefined ? { sandboxPolicy: policy } : {},
|
|
597
|
+
...args.timeoutMs !== undefined ? { timeoutMs: args.timeoutMs } : {},
|
|
598
|
+
}));
|
|
599
|
+
}
|
|
600
|
+
finally {
|
|
601
|
+
// Abort sub-dispatches and drain every in-flight dispatch before
|
|
602
|
+
// closing the turn (queued-unstarted ones are abandoned unlogged).
|
|
603
|
+
// Binding failures remain observable through their individual promises.
|
|
604
|
+
runController.abort('run_code settled');
|
|
605
|
+
await drainDispatches();
|
|
606
|
+
}
|
|
607
|
+
if (result.error) {
|
|
608
|
+
const logsText = result.logs.length > 0 ? `\nCaptured output:\n${result.logs.join('\n')}` : '';
|
|
609
|
+
const sandboxText = result.sandbox === undefined ? ''
|
|
610
|
+
: `\nFile sandbox: ${result.sandbox.mode}${result.sandbox.enforcement === undefined ? '' : `; enforcement: ${result.sandbox.enforcement}`}${result.sandbox.denied ? '; operation denied' : ''}.`;
|
|
611
|
+
throw new CodeRunFailedError(`code run failed (${result.error.kind}): ${result.error.message}${logsText}${sandboxText}${result.sandbox?.denied ? escalationGuidance(runtime) : ''}`);
|
|
612
|
+
}
|
|
613
|
+
return {
|
|
614
|
+
logs: result.logs,
|
|
615
|
+
...result.sandbox === undefined ? {} : { sandbox: result.sandbox },
|
|
616
|
+
...result.value !== undefined ? { result: result.value } : {},
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
finally {
|
|
620
|
+
exec.signal.removeEventListener('abort', onOuterAbort);
|
|
621
|
+
}
|
|
622
|
+
},
|
|
623
|
+
// The model-authored description is the call's always-visible UI label
|
|
624
|
+
// (the bash `description` precedent); the program itself rides rawInput.
|
|
625
|
+
presentCall: args => ({
|
|
626
|
+
card: 'generic',
|
|
627
|
+
title: args.description,
|
|
628
|
+
kind: 'execute',
|
|
629
|
+
rawInput: args.code,
|
|
630
|
+
}),
|
|
631
|
+
// Deliberately no presentResult: the generic card fallback keeps this
|
|
632
|
+
// title and reads durable result content without duplicating a large raw
|
|
633
|
+
// result into the host view payload.
|
|
634
|
+
});
|
|
635
|
+
// Resolve the language flavor lazily, at the moment the registry projects the
|
|
636
|
+
// schema (`schemaOf` destructures `description`/`parameters`). The definition
|
|
637
|
+
// is minted once at registration, before a runtime is known; deferring here
|
|
638
|
+
// is the least invasive point that still emits the loaded runtime's language.
|
|
639
|
+
Object.defineProperty(definition, 'description', {
|
|
640
|
+
enumerable: true,
|
|
641
|
+
get: () => {
|
|
642
|
+
const runtime = peekRuntime();
|
|
643
|
+
const instructions = runtime?.executionInstructions;
|
|
644
|
+
return resolveFlavor(peekRuntime).description
|
|
645
|
+
+ (instructions ? ` ${instructions}` : '')
|
|
646
|
+
+ (runtime === undefined ? '' : " Each program starts in the Session's current directory. Running programs keep their initial directory.")
|
|
647
|
+
+ escalationGuidance(runtime);
|
|
648
|
+
},
|
|
649
|
+
});
|
|
650
|
+
Object.defineProperty(definition, 'parameters', {
|
|
651
|
+
enumerable: true,
|
|
652
|
+
// Recompile through the same spec→schema projection defineTool used, so
|
|
653
|
+
// the emitted schema always matches the validated specification.
|
|
654
|
+
get: () => parameterSchemaSpecToJsonSchema({
|
|
655
|
+
description: { type: 'string', required: true, description: RUN_CODE_DESCRIPTION_PARAM_DESCRIPTION },
|
|
656
|
+
code: { type: 'string', required: true, description: resolveFlavor(peekRuntime).codeDescription },
|
|
657
|
+
...controlParameters(peekRuntime()),
|
|
658
|
+
}),
|
|
659
|
+
});
|
|
660
|
+
return definition;
|
|
661
|
+
}
|
|
662
|
+
//# sourceMappingURL=ptc.js.map
|