@namzu/sdk 42.0.0 → 42.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +174 -0
- package/dist/manager/resident/outbox.d.ts +8 -8
- package/dist/manager/resident/store.d.ts +4 -4
- package/dist/runtime/query/cancelled-before-start.d.ts +34 -0
- package/dist/runtime/query/cancelled-before-start.d.ts.map +1 -0
- package/dist/runtime/query/cancelled-before-start.js +152 -0
- package/dist/runtime/query/cancelled-before-start.js.map +1 -0
- package/dist/runtime/query/checkpoint.d.ts +21 -0
- package/dist/runtime/query/checkpoint.d.ts.map +1 -1
- package/dist/runtime/query/checkpoint.js +23 -0
- package/dist/runtime/query/checkpoint.js.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts +57 -0
- package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -0
- package/dist/runtime/query/executor/tool-call-admission.js +373 -0
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -0
- package/dist/runtime/query/executor.d.ts +70 -35
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +46 -380
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/finalize-run.d.ts +55 -0
- package/dist/runtime/query/finalize-run.d.ts.map +1 -0
- package/dist/runtime/query/finalize-run.js +113 -0
- package/dist/runtime/query/finalize-run.js.map +1 -0
- package/dist/runtime/query/index.d.ts +4 -9
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +238 -893
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +6 -161
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +23 -523
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/outstanding-work.d.ts +158 -0
- package/dist/runtime/query/iteration/outstanding-work.d.ts.map +1 -0
- package/dist/runtime/query/iteration/outstanding-work.js +365 -0
- package/dist/runtime/query/iteration/outstanding-work.js.map +1 -0
- package/dist/runtime/query/iteration/phases/plan.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/plan.js +13 -2
- package/dist/runtime/query/iteration/phases/plan.js.map +1 -1
- package/dist/runtime/query/iteration/step-shaping.d.ts +41 -0
- package/dist/runtime/query/iteration/step-shaping.d.ts.map +1 -0
- package/dist/runtime/query/iteration/step-shaping.js +184 -0
- package/dist/runtime/query/iteration/step-shaping.js.map +1 -0
- package/dist/runtime/query/prepare-run.d.ts +94 -0
- package/dist/runtime/query/prepare-run.d.ts.map +1 -0
- package/dist/runtime/query/prepare-run.js +589 -0
- package/dist/runtime/query/prepare-run.js.map +1 -0
- package/dist/runtime/query/release-run.d.ts +56 -0
- package/dist/runtime/query/release-run.d.ts.map +1 -0
- package/dist/runtime/query/release-run.js +101 -0
- package/dist/runtime/query/release-run.js.map +1 -0
- package/dist/runtime/query/resume-pending.d.ts +112 -1
- package/dist/runtime/query/resume-pending.d.ts.map +1 -1
- package/dist/runtime/query/resume-pending.js +133 -0
- package/dist/runtime/query/resume-pending.js.map +1 -1
- package/dist/store/evidence/compaction-archive.d.ts +2 -2
- package/dist/types/run/config.d.ts +12 -5
- package/dist/types/run/config.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/runtime/query/cancelled-before-start.ts +189 -0
- package/src/runtime/query/checkpoint.ts +22 -0
- package/src/runtime/query/executor/tool-call-admission.ts +473 -0
- package/src/runtime/query/executor.ts +63 -442
- package/src/runtime/query/finalize-run.ts +192 -0
- package/src/runtime/query/index.ts +270 -1011
- package/src/runtime/query/iteration/index.ts +40 -586
- package/src/runtime/query/iteration/outstanding-work.ts +386 -0
- package/src/runtime/query/iteration/phases/plan.ts +18 -2
- package/src/runtime/query/iteration/step-shaping.ts +271 -0
- package/src/runtime/query/prepare-run.ts +718 -0
- package/src/runtime/query/release-run.ts +168 -0
- package/src/runtime/query/resume-pending.ts +158 -0
- package/src/types/run/config.ts +12 -5
|
@@ -4,7 +4,6 @@ import { GENAI, NAMZU } from '../../constants/telemetry/index.js';
|
|
|
4
4
|
import { buildProbeContext } from '../../probe/context.js';
|
|
5
5
|
import { ProbeVetoError } from '../../probe/errors.js';
|
|
6
6
|
import { probe as defaultProbeRegistry } from '../../probe/registry.js';
|
|
7
|
-
import { renderToolSchema } from '../../registry/tool/schema.js';
|
|
8
7
|
import { SKILL_TOOL_NAME } from '../../tools/builtins/skill.js';
|
|
9
8
|
import { createFileReadTracker } from '../../tools/file-read-tracker.js';
|
|
10
9
|
import { createToolMessage, } from '../../types/message/index.js';
|
|
@@ -15,10 +14,10 @@ import { toErrorMessage } from '../../utils/error.js';
|
|
|
15
14
|
import { generateToolCallId } from '../../utils/id.js';
|
|
16
15
|
import { compressShellOutput } from '../../utils/shell-compress.js';
|
|
17
16
|
import { bindOwner } from '../jobs/registry.js';
|
|
17
|
+
import { formatFailedToolOutput, prepareDirectCall, repairTruncatedCall, resolveCall, runPreToolHook, truncatedToolInputMessage, } from './executor/tool-call-admission.js';
|
|
18
18
|
import { describeVisibleFileEvidence } from './file-evidence-context.js';
|
|
19
19
|
import { seedObservationLedger } from './file-evidence-seed.js';
|
|
20
20
|
import { DEFAULT_TOOL_RESULT_GUARDRAILS } from './guardrail-presets.js';
|
|
21
|
-
import { skippedToolResultText } from './plugin-hooks.js';
|
|
22
21
|
import { ToolCallBudget, assertMaxToolCalls } from './tool-call-budget.js';
|
|
23
22
|
import { DEFAULT_MAX_TOOL_OUTPUT_CHARS, applyToolOutputBudget, describeDroppedContent, measureContentBytes, } from './tool-output-budget.js';
|
|
24
23
|
function assertUniqueToolCallIds(toolCalls) {
|
|
@@ -172,13 +171,6 @@ export const DEFAULT_TOOL_RETRY_BACKOFF = {
|
|
|
172
171
|
initialDelayMs: 500,
|
|
173
172
|
maxDelayMs: 16_000,
|
|
174
173
|
};
|
|
175
|
-
/**
|
|
176
|
-
* An empty arguments string means "no arguments", not "malformed" — the
|
|
177
|
-
* shape a no-parameter tool arrives in.
|
|
178
|
-
*/
|
|
179
|
-
function parseArguments(raw) {
|
|
180
|
-
return JSON.parse(raw || '{}');
|
|
181
|
-
}
|
|
182
174
|
/**
|
|
183
175
|
* Model-visible text for a tool call that was never executed.
|
|
184
176
|
*
|
|
@@ -294,10 +286,28 @@ export class ToolExecutor {
|
|
|
294
286
|
*
|
|
295
287
|
* `denials` marks ids that must NOT run: each is answered with a
|
|
296
288
|
* synthetic error result carrying the caller's reason instead of being
|
|
297
|
-
* executed.
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
289
|
+
* executed. A gate denial, a human rejection and a partial approval all
|
|
290
|
+
* leave the history valid, because there is exactly one place that turns
|
|
291
|
+
* a batch of tool calls into messages and it covers all of them.
|
|
292
|
+
*
|
|
293
|
+
* **That is a property of every path that RETURNS, not of the batch as
|
|
294
|
+
* a whole.** A per-call throw rejects the batch before the fill-the-holes
|
|
295
|
+
* loop below can run: `serial = serial.then(run)` means one rejection
|
|
296
|
+
* skips every LATER serial call, and `Promise.all([...parallel, serial])`
|
|
297
|
+
* then rejects — so this method produces no messages at all and the
|
|
298
|
+
* assistant turn keeps its `tool_use` blocks unanswered. A resume is what
|
|
299
|
+
* repairs that turn; see the `unfinished` step `iteration/index.ts`
|
|
300
|
+
* records for it.
|
|
301
|
+
*
|
|
302
|
+
* Reachable, not hypothetical, and demonstrated end to end by
|
|
303
|
+
* `a-throwing-batch-answers-nothing.test.ts`: `executeSingle` rethrows a
|
|
304
|
+
* retry's budget-admission error, and a `runPreToolHook` failure on a
|
|
305
|
+
* call whose preparation did not already run the hook.
|
|
306
|
+
*
|
|
307
|
+
* So do not read the guarantee below as covering a throw. The invariant
|
|
308
|
+
* holds for denials, for approvals, for a rejected batch and for a
|
|
309
|
+
* generation that partially failed while still returning: each of those
|
|
310
|
+
* leaves a hole that the fill-the-holes loop closes.
|
|
301
311
|
*
|
|
302
312
|
* Answering with `is_error` semantics rather than dropping the call is
|
|
303
313
|
* the universal contract across providers: an unanswered `tool_use`
|
|
@@ -389,7 +399,7 @@ export class ToolExecutor {
|
|
|
389
399
|
assertUniqueToolCallIds(response.message.toolCalls ?? []);
|
|
390
400
|
const calls = new Map();
|
|
391
401
|
for (const toolCall of response.message.toolCalls ?? []) {
|
|
392
|
-
calls.set(toolCall.id, await this.
|
|
402
|
+
calls.set(toolCall.id, await prepareDirectCall(this.admissionHost(), toolCall));
|
|
393
403
|
}
|
|
394
404
|
return this.publishPreparedBatch(calls);
|
|
395
405
|
}
|
|
@@ -402,7 +412,7 @@ export class ToolExecutor {
|
|
|
402
412
|
const calls = new Map(previous.calls);
|
|
403
413
|
for (const toolCall of response.message.toolCalls ?? []) {
|
|
404
414
|
if (changedCallIds.has(toolCall.id)) {
|
|
405
|
-
calls.set(toolCall.id, await this.
|
|
415
|
+
calls.set(toolCall.id, await prepareDirectCall(this.admissionHost(), toolCall));
|
|
406
416
|
}
|
|
407
417
|
}
|
|
408
418
|
return this.publishPreparedBatch(calls);
|
|
@@ -990,7 +1000,7 @@ export class ToolExecutor {
|
|
|
990
1000
|
// was answered with a generic hint while the configured repairer sat
|
|
991
1001
|
// unused. Offer it the partial buffer first.
|
|
992
1002
|
const truncationRepair = toolCall.metadata?.inputTruncated === true
|
|
993
|
-
? await this.
|
|
1003
|
+
? await repairTruncatedCall(this.admissionHost(), toolCall, toolName)
|
|
994
1004
|
: null;
|
|
995
1005
|
if (toolCall.metadata?.inputTruncated === true && !truncationRepair) {
|
|
996
1006
|
const message = truncatedToolInputMessage(toolName);
|
|
@@ -1020,7 +1030,7 @@ export class ToolExecutor {
|
|
|
1020
1030
|
// error went back as a `tool_result`, the model re-read the whole
|
|
1021
1031
|
// context and tried again. A host that can repair it locally turns
|
|
1022
1032
|
// that into nothing. No-op when no repairer is configured.
|
|
1023
|
-
const resolved = await this.
|
|
1033
|
+
const resolved = await resolveCall(this.admissionHost(), truncationRepair
|
|
1024
1034
|
? {
|
|
1025
1035
|
...toolCall,
|
|
1026
1036
|
function: {
|
|
@@ -1063,7 +1073,7 @@ export class ToolExecutor {
|
|
|
1063
1073
|
input = resolved.input;
|
|
1064
1074
|
let preOutcome;
|
|
1065
1075
|
try {
|
|
1066
|
-
preOutcome = await this.
|
|
1076
|
+
preOutcome = await runPreToolHook(this.admissionHost(), toolName, input);
|
|
1067
1077
|
}
|
|
1068
1078
|
catch (error) {
|
|
1069
1079
|
if (!this.config.abortSignal.aborted)
|
|
@@ -1537,16 +1547,22 @@ export class ToolExecutor {
|
|
|
1537
1547
|
parentSignal.removeEventListener('abort', onParentAbort);
|
|
1538
1548
|
}
|
|
1539
1549
|
}
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
+
/**
|
|
1551
|
+
* The three things the admission family reads off this executor.
|
|
1552
|
+
*
|
|
1553
|
+
* Built per call rather than held: `setSandbox` REPLACES `config`, so a
|
|
1554
|
+
* host captured once would hand the next admission a stale sandbox.
|
|
1555
|
+
*
|
|
1556
|
+
* The one way this differs from the inline code it replaced, which
|
|
1557
|
+
* re-read `this.config` at every use: an admission that spans a
|
|
1558
|
+
* `setSandbox()` now finishes against the config it STARTED with rather
|
|
1559
|
+
* than against the new one. Distinguishing the two readings needs
|
|
1560
|
+
* `setSandbox` to be called from a hook awaited in the middle of one
|
|
1561
|
+
* admission — its only call site is the run's sandbox acquisition,
|
|
1562
|
+
* before the loop, so nothing in this tree can tell them apart.
|
|
1563
|
+
*/
|
|
1564
|
+
admissionHost() {
|
|
1565
|
+
return { config: this.config, emitEvent: this.emitEvent, log: this.log };
|
|
1550
1566
|
}
|
|
1551
1567
|
async prepareNestedCall(toolName, input, signal) {
|
|
1552
1568
|
const prepare = this.config.tools.prepareExecution;
|
|
@@ -1560,7 +1576,7 @@ export class ToolExecutor {
|
|
|
1560
1576
|
isError: true,
|
|
1561
1577
|
};
|
|
1562
1578
|
}
|
|
1563
|
-
const preOutcome = await this.
|
|
1579
|
+
const preOutcome = await runPreToolHook(this.admissionHost(), toolName, input, signal);
|
|
1564
1580
|
if (preOutcome.kind === 'skip' || preOutcome.kind === 'error') {
|
|
1565
1581
|
return {
|
|
1566
1582
|
kind: 'synthetic',
|
|
@@ -1591,7 +1607,7 @@ export class ToolExecutor {
|
|
|
1591
1607
|
isError: true,
|
|
1592
1608
|
};
|
|
1593
1609
|
}
|
|
1594
|
-
const preOutcome = await this.
|
|
1610
|
+
const preOutcome = await runPreToolHook(this.admissionHost(), toolName, preparation.prepared.input, signal);
|
|
1595
1611
|
if (preOutcome.kind === 'skip' || preOutcome.kind === 'error') {
|
|
1596
1612
|
return {
|
|
1597
1613
|
kind: 'synthetic',
|
|
@@ -1618,347 +1634,6 @@ export class ToolExecutor {
|
|
|
1618
1634
|
}
|
|
1619
1635
|
return { kind: 'ready', input: modified.prepared.input, prepared: modified.prepared };
|
|
1620
1636
|
}
|
|
1621
|
-
async prepareDirectCall(toolCall) {
|
|
1622
|
-
let toolName = toolCall.function.name;
|
|
1623
|
-
const truncationRepair = toolCall.metadata?.inputTruncated === true
|
|
1624
|
-
? await this.repairTruncatedCall(toolCall, toolName)
|
|
1625
|
-
: null;
|
|
1626
|
-
if (toolCall.metadata?.inputTruncated === true && !truncationRepair) {
|
|
1627
|
-
return {
|
|
1628
|
-
kind: 'synthetic',
|
|
1629
|
-
toolCall,
|
|
1630
|
-
toolName,
|
|
1631
|
-
input: {},
|
|
1632
|
-
message: truncatedToolInputMessage(toolName),
|
|
1633
|
-
isError: true,
|
|
1634
|
-
};
|
|
1635
|
-
}
|
|
1636
|
-
const prepare = this.config.tools.prepareExecution;
|
|
1637
|
-
const executePrepared = this.config.tools.executePrepared;
|
|
1638
|
-
if (typeof prepare !== 'function' || typeof executePrepared !== 'function') {
|
|
1639
|
-
const resolved = await this.resolveCall(truncationRepair
|
|
1640
|
-
? {
|
|
1641
|
-
...toolCall,
|
|
1642
|
-
function: {
|
|
1643
|
-
...toolCall.function,
|
|
1644
|
-
name: truncationRepair.toolName ?? toolName,
|
|
1645
|
-
arguments: truncationRepair.arguments,
|
|
1646
|
-
},
|
|
1647
|
-
metadata: {},
|
|
1648
|
-
}
|
|
1649
|
-
: toolCall);
|
|
1650
|
-
toolName = resolved.toolName;
|
|
1651
|
-
if (!resolved.ok) {
|
|
1652
|
-
return {
|
|
1653
|
-
kind: 'synthetic',
|
|
1654
|
-
toolCall,
|
|
1655
|
-
toolName,
|
|
1656
|
-
input: {},
|
|
1657
|
-
message: resolved.message,
|
|
1658
|
-
isError: true,
|
|
1659
|
-
};
|
|
1660
|
-
}
|
|
1661
|
-
const preOutcome = await this.runPreToolHook(toolName, resolved.input);
|
|
1662
|
-
if (preOutcome.kind === 'skip' || preOutcome.kind === 'error') {
|
|
1663
|
-
return {
|
|
1664
|
-
kind: 'synthetic',
|
|
1665
|
-
toolCall,
|
|
1666
|
-
toolName,
|
|
1667
|
-
input: preOutcome.input,
|
|
1668
|
-
message: preOutcome.output,
|
|
1669
|
-
isError: preOutcome.kind === 'error',
|
|
1670
|
-
};
|
|
1671
|
-
}
|
|
1672
|
-
if (!this.config.authorizationGate) {
|
|
1673
|
-
return {
|
|
1674
|
-
kind: 'legacy',
|
|
1675
|
-
toolCall,
|
|
1676
|
-
toolName,
|
|
1677
|
-
input: preOutcome.input,
|
|
1678
|
-
};
|
|
1679
|
-
}
|
|
1680
|
-
return {
|
|
1681
|
-
kind: 'synthetic',
|
|
1682
|
-
toolCall,
|
|
1683
|
-
toolName,
|
|
1684
|
-
input: preOutcome.input,
|
|
1685
|
-
message: `Tool "${toolName}" was not executed because its registry cannot bind authorization to one prepared input.`,
|
|
1686
|
-
isError: true,
|
|
1687
|
-
};
|
|
1688
|
-
}
|
|
1689
|
-
let raw = truncationRepair?.arguments ?? toolCall.function.arguments;
|
|
1690
|
-
toolName = truncationRepair?.toolName ?? toolName;
|
|
1691
|
-
let repairUsed = truncationRepair !== null;
|
|
1692
|
-
let preparation;
|
|
1693
|
-
for (;;) {
|
|
1694
|
-
let parsed;
|
|
1695
|
-
try {
|
|
1696
|
-
parsed = parseArguments(raw);
|
|
1697
|
-
}
|
|
1698
|
-
catch {
|
|
1699
|
-
const message = `Error: Invalid JSON in tool arguments for "${toolName}"`;
|
|
1700
|
-
const repair = !repairUsed && this.config.repairToolCall
|
|
1701
|
-
? await this.requestRepair(toolCall, toolName, {
|
|
1702
|
-
reason: 'invalid_json',
|
|
1703
|
-
message,
|
|
1704
|
-
})
|
|
1705
|
-
: null;
|
|
1706
|
-
if (repair) {
|
|
1707
|
-
repairUsed = true;
|
|
1708
|
-
toolName = repair.toolName ?? toolName;
|
|
1709
|
-
raw = repair.arguments;
|
|
1710
|
-
continue;
|
|
1711
|
-
}
|
|
1712
|
-
return { kind: 'synthetic', toolCall, toolName, input: {}, message, isError: true };
|
|
1713
|
-
}
|
|
1714
|
-
try {
|
|
1715
|
-
preparation = prepare.call(this.config.tools, toolName, parsed);
|
|
1716
|
-
}
|
|
1717
|
-
catch (err) {
|
|
1718
|
-
const message = `Error: Unknown or unavailable tool "${toolName}": ${toErrorMessage(err)}`;
|
|
1719
|
-
const repair = !repairUsed && this.config.repairToolCall
|
|
1720
|
-
? await this.requestRepair(toolCall, toolName, {
|
|
1721
|
-
reason: 'unknown_tool',
|
|
1722
|
-
message,
|
|
1723
|
-
})
|
|
1724
|
-
: null;
|
|
1725
|
-
if (repair) {
|
|
1726
|
-
repairUsed = true;
|
|
1727
|
-
toolName = repair.toolName ?? toolName;
|
|
1728
|
-
raw = repair.arguments;
|
|
1729
|
-
continue;
|
|
1730
|
-
}
|
|
1731
|
-
return { kind: 'synthetic', toolCall, toolName, input: parsed, message, isError: true };
|
|
1732
|
-
}
|
|
1733
|
-
if (preparation.success)
|
|
1734
|
-
break;
|
|
1735
|
-
const message = formatFailedToolOutput(preparation.result.output, preparation.result.error);
|
|
1736
|
-
const repair = !repairUsed && this.config.repairToolCall
|
|
1737
|
-
? await this.requestRepair(toolCall, toolName, {
|
|
1738
|
-
reason: 'schema_validation',
|
|
1739
|
-
message,
|
|
1740
|
-
})
|
|
1741
|
-
: null;
|
|
1742
|
-
if (repair) {
|
|
1743
|
-
repairUsed = true;
|
|
1744
|
-
toolName = repair.toolName ?? toolName;
|
|
1745
|
-
raw = repair.arguments;
|
|
1746
|
-
continue;
|
|
1747
|
-
}
|
|
1748
|
-
return {
|
|
1749
|
-
kind: 'synthetic',
|
|
1750
|
-
toolCall,
|
|
1751
|
-
toolName,
|
|
1752
|
-
input: parsed,
|
|
1753
|
-
message,
|
|
1754
|
-
isError: true,
|
|
1755
|
-
};
|
|
1756
|
-
}
|
|
1757
|
-
const preOutcome = await this.runPreToolHook(toolName, preparation.prepared.input);
|
|
1758
|
-
if (preOutcome.kind === 'skip' || preOutcome.kind === 'error') {
|
|
1759
|
-
return {
|
|
1760
|
-
kind: 'synthetic',
|
|
1761
|
-
toolCall,
|
|
1762
|
-
toolName,
|
|
1763
|
-
input: preOutcome.input,
|
|
1764
|
-
message: preOutcome.output,
|
|
1765
|
-
isError: preOutcome.kind === 'error',
|
|
1766
|
-
};
|
|
1767
|
-
}
|
|
1768
|
-
if (preOutcome.modified) {
|
|
1769
|
-
const modified = prepare.call(this.config.tools, toolName, preOutcome.input);
|
|
1770
|
-
if (!modified.success) {
|
|
1771
|
-
return {
|
|
1772
|
-
kind: 'synthetic',
|
|
1773
|
-
toolCall,
|
|
1774
|
-
toolName,
|
|
1775
|
-
input: preOutcome.input,
|
|
1776
|
-
message: formatFailedToolOutput(modified.result.output, modified.result.error),
|
|
1777
|
-
isError: true,
|
|
1778
|
-
};
|
|
1779
|
-
}
|
|
1780
|
-
preparation = modified;
|
|
1781
|
-
}
|
|
1782
|
-
return {
|
|
1783
|
-
kind: 'ready',
|
|
1784
|
-
toolCall,
|
|
1785
|
-
toolName,
|
|
1786
|
-
input: preparation.prepared.input,
|
|
1787
|
-
prepared: preparation.prepared,
|
|
1788
|
-
};
|
|
1789
|
-
}
|
|
1790
|
-
interpretPreToolResults(toolName, initialInput, results) {
|
|
1791
|
-
let currentInput = initialInput;
|
|
1792
|
-
let modified = false;
|
|
1793
|
-
for (const result of results) {
|
|
1794
|
-
switch (result.action) {
|
|
1795
|
-
case 'continue':
|
|
1796
|
-
continue;
|
|
1797
|
-
case 'modify':
|
|
1798
|
-
currentInput = result.input;
|
|
1799
|
-
modified = true;
|
|
1800
|
-
continue;
|
|
1801
|
-
case 'skip':
|
|
1802
|
-
return {
|
|
1803
|
-
kind: 'skip',
|
|
1804
|
-
input: currentInput,
|
|
1805
|
-
output: skippedToolResultText(toolName, result.reason),
|
|
1806
|
-
};
|
|
1807
|
-
case 'error':
|
|
1808
|
-
return {
|
|
1809
|
-
kind: 'error',
|
|
1810
|
-
input: currentInput,
|
|
1811
|
-
output: `Error: ${result.message}`,
|
|
1812
|
-
};
|
|
1813
|
-
case 'retry':
|
|
1814
|
-
case 'annotate':
|
|
1815
|
-
// There is no result to replace yet. Rejecting loudly beats
|
|
1816
|
-
// silently ignoring it: a hook author who returned this here
|
|
1817
|
-
// meant to redact something and would otherwise watch the secret
|
|
1818
|
-
// go through.
|
|
1819
|
-
case 'replace':
|
|
1820
|
-
throw new Error(`Plugin hook pre_tool_use returned unsupported action '${result.action}' for tool ${toolName}`);
|
|
1821
|
-
default: {
|
|
1822
|
-
const _exhaustive = result;
|
|
1823
|
-
throw new Error(`Unknown PluginHookResult: ${JSON.stringify(_exhaustive)}`);
|
|
1824
|
-
}
|
|
1825
|
-
}
|
|
1826
|
-
}
|
|
1827
|
-
return { kind: 'continue', input: currentInput, modified };
|
|
1828
|
-
}
|
|
1829
|
-
/**
|
|
1830
|
-
* Turn the call the model issued into a name and a parsed input, giving
|
|
1831
|
-
* a configured repairer one chance to fix it first.
|
|
1832
|
-
*
|
|
1833
|
-
* Exactly one chance: a repairer that produces a call which is still
|
|
1834
|
-
* broken will not do better on a second look, and an unbounded loop
|
|
1835
|
-
* here is a hang rather than a degradation.
|
|
1836
|
-
*
|
|
1837
|
-
* `invalid_json` is the ONLY failure that stops the call here, and it
|
|
1838
|
-
* stopped it before this function existed too. `unknown_tool` and
|
|
1839
|
-
* `schema_validation` merely OFFER the repair and otherwise fall
|
|
1840
|
-
* through to the registry, which reports both with better messages —
|
|
1841
|
-
* its schema error already ships a "Required: <field>: <type>" hint the
|
|
1842
|
-
* model can self-correct from. So with no repairer configured this is
|
|
1843
|
-
* behaviorally identical to the bare `JSON.parse` it replaced.
|
|
1844
|
-
*/
|
|
1845
|
-
async resolveCall(toolCall) {
|
|
1846
|
-
let toolName = toolCall.function.name;
|
|
1847
|
-
let raw = toolCall.function.arguments;
|
|
1848
|
-
for (let attempt = 0;; attempt++) {
|
|
1849
|
-
const failure = this.inspectCall(toolName, raw);
|
|
1850
|
-
if (!failure)
|
|
1851
|
-
return { ok: true, toolName, input: parseArguments(raw) };
|
|
1852
|
-
const repair = attempt === 0 && this.config.repairToolCall
|
|
1853
|
-
? await this.requestRepair(toolCall, toolName, failure)
|
|
1854
|
-
: null;
|
|
1855
|
-
if (!repair) {
|
|
1856
|
-
if (failure.reason === 'invalid_json') {
|
|
1857
|
-
return { ok: false, toolName, message: failure.message };
|
|
1858
|
-
}
|
|
1859
|
-
return { ok: true, toolName, input: parseArguments(raw) };
|
|
1860
|
-
}
|
|
1861
|
-
this.log.info('Repaired a malformed tool call', {
|
|
1862
|
-
[NAMZU.RUN_ID]: this.config.runId,
|
|
1863
|
-
[GENAI.TOOL_NAME]: toolName,
|
|
1864
|
-
'namzu.runtime.reason': failure.reason,
|
|
1865
|
-
...(repair.toolName && repair.toolName !== toolName
|
|
1866
|
-
? { 'namzu.runtime.repaired_to': repair.toolName }
|
|
1867
|
-
: {}),
|
|
1868
|
-
});
|
|
1869
|
-
toolName = repair.toolName ?? toolName;
|
|
1870
|
-
raw = repair.arguments;
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
/**
|
|
1874
|
-
* What is wrong with this call, or `null` if nothing is.
|
|
1875
|
-
*
|
|
1876
|
-
* JSON is checked before the tool is looked up: an unparseable argument
|
|
1877
|
-
* string is broken regardless of which tool it was aimed at, and it is
|
|
1878
|
-
* the one problem the executor itself has to answer.
|
|
1879
|
-
*/
|
|
1880
|
-
async repairTruncatedCall(toolCall, toolName) {
|
|
1881
|
-
if (!this.config.repairToolCall)
|
|
1882
|
-
return null;
|
|
1883
|
-
// Present the PARTIAL buffer, not the normalized `"{}"` — a repairer
|
|
1884
|
-
// handed an empty object has nothing to work from.
|
|
1885
|
-
const partial = toolCall.metadata?.partialArguments ?? '';
|
|
1886
|
-
const repair = await this.requestRepair({ ...toolCall, function: { ...toolCall.function, arguments: partial } }, toolName, { reason: 'invalid_json', message: truncatedToolInputMessage(toolName) });
|
|
1887
|
-
if (repair) {
|
|
1888
|
-
this.log.info('Repaired a tool call whose input stream was truncated', {
|
|
1889
|
-
[NAMZU.RUN_ID]: this.config.runId,
|
|
1890
|
-
[GENAI.TOOL_NAME]: toolName,
|
|
1891
|
-
'namzu.runtime.partial_length': partial.length,
|
|
1892
|
-
});
|
|
1893
|
-
}
|
|
1894
|
-
return repair;
|
|
1895
|
-
}
|
|
1896
|
-
inspectCall(toolName, raw) {
|
|
1897
|
-
let parsed;
|
|
1898
|
-
try {
|
|
1899
|
-
parsed = parseArguments(raw);
|
|
1900
|
-
}
|
|
1901
|
-
catch {
|
|
1902
|
-
return {
|
|
1903
|
-
reason: 'invalid_json',
|
|
1904
|
-
message: `Error: Invalid JSON in tool arguments for "${toolName}"`,
|
|
1905
|
-
};
|
|
1906
|
-
}
|
|
1907
|
-
const tool = this.config.tools.get?.(toolName);
|
|
1908
|
-
if (!tool) {
|
|
1909
|
-
// Either the model named a tool that does not exist, or this
|
|
1910
|
-
// registry does not implement `get`. Both are the registry's to
|
|
1911
|
-
// answer; a repairer still gets offered the `unknown_tool` case.
|
|
1912
|
-
return {
|
|
1913
|
-
reason: 'unknown_tool',
|
|
1914
|
-
message: `Error: Unknown tool "${toolName}"`,
|
|
1915
|
-
};
|
|
1916
|
-
}
|
|
1917
|
-
// A registry that hands back a tool with no schema has nothing to
|
|
1918
|
-
// validate against; that is not a repairable condition, just an
|
|
1919
|
-
// unvalidatable one.
|
|
1920
|
-
const validation = tool.inputSchema?.safeParse(parsed);
|
|
1921
|
-
if (validation && !validation.success) {
|
|
1922
|
-
return {
|
|
1923
|
-
reason: 'schema_validation',
|
|
1924
|
-
message: `Error: Invalid arguments for "${toolName}": ${validation.error.issues
|
|
1925
|
-
.map((issue) => `${issue.path.join('.') || '(root)'}: ${issue.message}`)
|
|
1926
|
-
.join('; ')}`,
|
|
1927
|
-
};
|
|
1928
|
-
}
|
|
1929
|
-
return null;
|
|
1930
|
-
}
|
|
1931
|
-
async requestRepair(toolCall, toolName, failure) {
|
|
1932
|
-
const repairToolCall = this.config.repairToolCall;
|
|
1933
|
-
if (!repairToolCall)
|
|
1934
|
-
return null;
|
|
1935
|
-
const tool = this.config.tools.get(toolName);
|
|
1936
|
-
try {
|
|
1937
|
-
return await repairToolCall({
|
|
1938
|
-
toolCall,
|
|
1939
|
-
reason: failure.reason,
|
|
1940
|
-
message: failure.message,
|
|
1941
|
-
...(tool
|
|
1942
|
-
? {
|
|
1943
|
-
tool,
|
|
1944
|
-
jsonSchema: tool.modelInputSchema ?? renderToolSchema(tool.inputSchema),
|
|
1945
|
-
}
|
|
1946
|
-
: {}),
|
|
1947
|
-
availableTools: this.config.tools.listNames(),
|
|
1948
|
-
});
|
|
1949
|
-
}
|
|
1950
|
-
catch (err) {
|
|
1951
|
-
// A broken repairer must not turn a recoverable tool error into a
|
|
1952
|
-
// failed run: the original error is still a perfectly good answer
|
|
1953
|
-
// to give the model.
|
|
1954
|
-
this.log.error('repairToolCall threw — falling back to the original error', {
|
|
1955
|
-
[NAMZU.RUN_ID]: this.config.runId,
|
|
1956
|
-
[GENAI.TOOL_NAME]: toolName,
|
|
1957
|
-
'exception.message': toErrorMessage(err),
|
|
1958
|
-
});
|
|
1959
|
-
return null;
|
|
1960
|
-
}
|
|
1961
|
-
}
|
|
1962
1637
|
/**
|
|
1963
1638
|
* One execution attempt, with a throw materialized as an error result.
|
|
1964
1639
|
*
|
|
@@ -2261,13 +1936,4 @@ class Semaphore {
|
|
|
2261
1936
|
this.available++;
|
|
2262
1937
|
}
|
|
2263
1938
|
}
|
|
2264
|
-
function formatFailedToolOutput(output, error) {
|
|
2265
|
-
const errorText = `Error: ${error ?? 'Tool execution failed'}`;
|
|
2266
|
-
if (!output || output.trim().length === 0)
|
|
2267
|
-
return errorText;
|
|
2268
|
-
return `${output}\n\n${errorText}`;
|
|
2269
|
-
}
|
|
2270
|
-
function truncatedToolInputMessage(toolName) {
|
|
2271
|
-
return `Error: Tool "${toolName}" call was cut off while the model was streaming JSON arguments. The tool was NOT executed. Retry with a much shorter input. Self-budget content/new_string under 12000 characters before calling file tools. For long files, create a short opening with write and a deterministic marker, then advance that marker with bounded exact edit calls; for delegated work, pass a shared workspace filename/reference instead of embedding the content in the tool call.`;
|
|
2272
|
-
}
|
|
2273
1939
|
//# sourceMappingURL=executor.js.map
|