@fabric-harness/sdk 6.9.0 → 6.9.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/dist/experimental.d.ts +6 -5
- package/dist/experimental.d.ts.map +1 -1
- package/dist/experimental.js +5 -3
- package/dist/experimental.js.map +1 -1
- package/dist/policied-sandbox.d.ts +9 -3
- package/dist/policied-sandbox.d.ts.map +1 -1
- package/dist/policied-sandbox.js +276 -53
- package/dist/policied-sandbox.js.map +1 -1
- package/dist/policy.d.ts +3 -3
- package/dist/policy.d.ts.map +1 -1
- package/dist/policy.js +115 -64
- package/dist/policy.js.map +1 -1
- package/dist/sandbox-ref.d.ts +2 -2
- package/dist/sandbox-ref.d.ts.map +1 -1
- package/dist/sandbox-ref.js +143 -70
- package/dist/sandbox-ref.js.map +1 -1
- package/dist/sandbox.d.ts +8 -0
- package/dist/sandbox.d.ts.map +1 -1
- package/dist/sandbox.js +22 -1
- package/dist/sandbox.js.map +1 -1
- package/dist/session.d.ts +17 -3
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +294 -146
- package/dist/session.js.map +1 -1
- package/dist/suspending-sandbox.d.ts +2 -2
- package/dist/suspending-sandbox.d.ts.map +1 -1
- package/dist/suspending-sandbox.js +65 -26
- package/dist/suspending-sandbox.js.map +1 -1
- package/dist/tools.d.ts +15 -3
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +94 -46
- package/dist/tools.js.map +1 -1
- package/package.json +1 -1
package/dist/session.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { abandonToolOnAbort } from "./abort.js";
|
|
4
|
+
import { approvalGrantFromJson, approvalGrantFromState, approvalGrantToJson, approvalInputDigest, createApprovalGrant, sameApprovalOperation, } from "./approval.js";
|
|
5
|
+
import { BoundedInMemoryAttachmentStore } from "./attachment-store.js";
|
|
6
|
+
import { estimateSessionEntryTokens, evaluateContextBudget, isContextOverflowError, } from "./context-budget.js";
|
|
2
7
|
import { CostBudgetTracker } from "./cost-budget.js";
|
|
3
|
-
import {
|
|
8
|
+
import { deliveredSignalToEntryData, renderDeliveredMessage } from "./delivered-message.js";
|
|
9
|
+
import { dynamicStateEntriesForTool, unwrapDynamicToolResult } from "./dynamic-agent.js";
|
|
4
10
|
import { FabricError } from "./error.js";
|
|
5
|
-
import {
|
|
6
|
-
import { executeTool } from "./tool-execution.js";
|
|
11
|
+
import { createFabricFs } from "./filesystem.js";
|
|
7
12
|
import { openGovernedVoice, } from "./governed-voice.js";
|
|
8
|
-
import { dynamicStateEntriesForTool, unwrapDynamicToolResult } from "./dynamic-agent.js";
|
|
9
|
-
import { approvalGrantFromJson, approvalGrantFromState, approvalGrantToJson, approvalInputDigest, createApprovalGrant, sameApprovalOperation, } from "./approval.js";
|
|
10
13
|
import { SessionHistory, buildModelMessagesFromHistory, findTrailingDanglingToolCalls, findTrailingUnfinishedTasks, } from "./history.js";
|
|
11
14
|
import { getLogger } from "./logger.js";
|
|
12
15
|
import { defaultLoopRuntime } from "./loop.js";
|
|
13
|
-
import { SessionStreamRecovery } from "./session-stream-recovery.js";
|
|
14
16
|
import { defaultModelProvider, generateWithRuntime } from "./model.js";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { estimateSessionEntryTokens, evaluateContextBudget, isContextOverflowError, } from "./context-budget.js";
|
|
18
|
-
import { policiedSandboxEnv, runWithSandboxPolicyApproval } from "./policied-sandbox.js";
|
|
19
|
-
import { currentSubmissionContext } from "./submission-context.js";
|
|
20
|
-
import { deliveredSignalToEntryData, renderDeliveredMessage } from "./delivered-message.js";
|
|
21
|
-
import { BoundedInMemoryAttachmentStore } from "./attachment-store.js";
|
|
17
|
+
import { evaluateToolCallPolicyInSandbox, policiedSandboxEnv, runWithSandboxPolicyApproval, } from "./policied-sandbox.js";
|
|
18
|
+
import { canonicalizeToolCall, clampCommandTimeout, evaluateCommandPolicy } from "./policy.js";
|
|
22
19
|
import { createAttachmentHydrationBudget, hydrateModelMessageAttachments, preparePromptAttachments, recordsToModelAttachments, validatePromptAttachments, } from "./prompt-attachments.js";
|
|
23
|
-
import {
|
|
24
|
-
import { createFabricFs } from "./filesystem.js";
|
|
20
|
+
import { buildResultRetryPrompt, validateResult } from "./result.js";
|
|
25
21
|
import { registerSandbox, serializeSandboxRef, unregisterSandbox } from "./sandbox-ref.js";
|
|
26
|
-
import
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
22
|
+
import { createSandboxEnv, createScopedSandboxEnv, execSandboxCommand } from "./sandbox.js";
|
|
23
|
+
import { inMemorySessionMemory } from "./session-memory.js";
|
|
24
|
+
import { SessionStreamRecovery } from "./session-stream-recovery.js";
|
|
29
25
|
import { formatPackagedSkillResourceList, readPackagedSkillFile } from "./skills.js";
|
|
26
|
+
import { defaultSessionStore } from "./store.js";
|
|
27
|
+
import { currentSubmissionContext } from "./submission-context.js";
|
|
28
|
+
import { executeTool } from "./tool-execution.js";
|
|
29
|
+
import { createActivateSkillTool, createBuiltinTools, createCommandTools, hasScopedCommandRenderer, renderScopedCommand, shellQuote, unwrapHookToolRunResult, } from "./tools.js";
|
|
30
30
|
import { MAX_TASK_DEPTH } from "./types.js";
|
|
31
31
|
let nextTaskId = 0;
|
|
32
32
|
const CROCKFORD_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
@@ -282,9 +282,10 @@ export class StubFabricSession {
|
|
|
282
282
|
// local prompt_start/end pair so local subscribers see activity.
|
|
283
283
|
await this.emit("prompt_start", { text });
|
|
284
284
|
try {
|
|
285
|
+
const forwardedPolicy = this.durablePolicy(options.policy);
|
|
285
286
|
const result = await durable.prompt({
|
|
286
287
|
text,
|
|
287
|
-
|
|
288
|
+
options: forwardedPolicy !== undefined ? { ...options, policy: forwardedPolicy } : options,
|
|
288
289
|
...(this.options.dynamic ? { dynamic: this.options.dynamic.executionDescriptor() } : {}),
|
|
289
290
|
});
|
|
290
291
|
await this.emit("prompt_end", { text });
|
|
@@ -303,7 +304,7 @@ export class StubFabricSession {
|
|
|
303
304
|
const lifecycleSignal = options.signal ?? new AbortController().signal;
|
|
304
305
|
try {
|
|
305
306
|
let sandbox = createScopedSandboxEnv(await this.sandbox, options.cwd);
|
|
306
|
-
let tools = this.resolveTools(
|
|
307
|
+
let tools = this.resolveTools(options.tools, options.commands);
|
|
307
308
|
let joinedRenderReady = false;
|
|
308
309
|
const currentTurnConfiguration = () => ({
|
|
309
310
|
...(typeof this.options.model === "string" ? { model: this.options.model } : {}),
|
|
@@ -392,7 +393,7 @@ export class StubFabricSession {
|
|
|
392
393
|
}
|
|
393
394
|
role = this.resolveRole(this.options.role);
|
|
394
395
|
}
|
|
395
|
-
tools = this.resolveTools(
|
|
396
|
+
tools = this.resolveTools(options.tools, options.commands);
|
|
396
397
|
if (!config.dynamic) {
|
|
397
398
|
throw new Error("[fabric-harness] A dynamic-agent refresh did not return its runtime.");
|
|
398
399
|
}
|
|
@@ -876,7 +877,11 @@ export class StubFabricSession {
|
|
|
876
877
|
const taskId = options.id ?? `${this.id}:task-${++nextTaskId}`;
|
|
877
878
|
await this.emit("task_start", { taskId, depth: 1, parentSessionId: this.id });
|
|
878
879
|
try {
|
|
879
|
-
const { id: _id, ...
|
|
880
|
+
const { id: _id, ...requestedTaskOptions } = options;
|
|
881
|
+
const forwardedPolicy = this.durablePolicy(options.policy);
|
|
882
|
+
const taskOptions = forwardedPolicy !== undefined
|
|
883
|
+
? { ...requestedTaskOptions, policy: forwardedPolicy }
|
|
884
|
+
: requestedTaskOptions;
|
|
880
885
|
const result = await durable.task({
|
|
881
886
|
text,
|
|
882
887
|
taskId,
|
|
@@ -997,6 +1002,13 @@ export class StubFabricSession {
|
|
|
997
1002
|
// instructions, model, and capabilities.
|
|
998
1003
|
if (delegatedRole)
|
|
999
1004
|
Reflect.deleteProperty(childOptions, "dynamic");
|
|
1005
|
+
// A policy the caller set on this session, or on the task itself, stays
|
|
1006
|
+
// explicit for the child, so the child's dynamic refresh cannot replace it.
|
|
1007
|
+
const childExplicitPolicy = options.policy ?? explicitSessionPolicies.get(this.options);
|
|
1008
|
+
if (childExplicitPolicy !== undefined) {
|
|
1009
|
+
childOptions.policy = childExplicitPolicy;
|
|
1010
|
+
markExplicitSessionPolicy(childOptions, childExplicitPolicy);
|
|
1011
|
+
}
|
|
1000
1012
|
const child = new StubFabricSession(this.agent, taskId, childOptions);
|
|
1001
1013
|
const result = await child.prompt(text, {
|
|
1002
1014
|
...childPromptOptions,
|
|
@@ -1094,7 +1106,11 @@ export class StubFabricSession {
|
|
|
1094
1106
|
const durable = await this.getDurableRuntime();
|
|
1095
1107
|
if (durable) {
|
|
1096
1108
|
await this.emit("command_start", { command });
|
|
1097
|
-
const
|
|
1109
|
+
const forwardedPolicy = this.durablePolicy(options.policy, false);
|
|
1110
|
+
const result = await durable.shell({
|
|
1111
|
+
command,
|
|
1112
|
+
options: forwardedPolicy !== undefined ? { ...options, policy: forwardedPolicy } : options,
|
|
1113
|
+
});
|
|
1098
1114
|
await this.emit("command_end", { command, exitCode: result.exitCode });
|
|
1099
1115
|
return result;
|
|
1100
1116
|
}
|
|
@@ -1292,6 +1308,7 @@ export class StubFabricSession {
|
|
|
1292
1308
|
const sandboxFork = sandbox.fork;
|
|
1293
1309
|
const agent = this.agent;
|
|
1294
1310
|
const baseOptions = this.options;
|
|
1311
|
+
const baseExplicitPolicy = explicitSessionPolicies.get(this.options);
|
|
1295
1312
|
await this.recordEntry("checkpoint_created", {
|
|
1296
1313
|
label: label ?? `fork:${snapshot.id}`,
|
|
1297
1314
|
...(label ? { metadata: { fork: true } } : { metadata: { fork: true } }),
|
|
@@ -1320,6 +1337,13 @@ export class StubFabricSession {
|
|
|
1320
1337
|
...(label !== undefined ? { forkLabel: label } : {}),
|
|
1321
1338
|
},
|
|
1322
1339
|
};
|
|
1340
|
+
// attach() creates a session like agent.session() does: a policy passed
|
|
1341
|
+
// here, else the origin's explicit one, outranks a dynamic agent's render.
|
|
1342
|
+
const explicitPolicy = attachOptions.policy ?? baseExplicitPolicy;
|
|
1343
|
+
if (explicitPolicy !== undefined) {
|
|
1344
|
+
merged.policy = explicitPolicy;
|
|
1345
|
+
markExplicitSessionPolicy(merged, explicitPolicy);
|
|
1346
|
+
}
|
|
1323
1347
|
return new StubFabricSession(agent, newId, merged);
|
|
1324
1348
|
},
|
|
1325
1349
|
};
|
|
@@ -1679,6 +1703,30 @@ export class StubFabricSession {
|
|
|
1679
1703
|
...(isSnapshot(snapshot) ? { snapshotId: snapshot.id } : {}),
|
|
1680
1704
|
};
|
|
1681
1705
|
}
|
|
1706
|
+
/**
|
|
1707
|
+
* The sandbox a tool sees under `policy`. The session's sandbox already
|
|
1708
|
+
* enforces the policy it was created with; this layers the call's effective
|
|
1709
|
+
* policy on top, so the two intersect and a call can narrow, never widen.
|
|
1710
|
+
*/
|
|
1711
|
+
sandboxUnderPolicy(sandbox, policy) {
|
|
1712
|
+
return wrapWithPolicy(sandbox, policy, this.options.bypassPolicyEnforcement);
|
|
1713
|
+
}
|
|
1714
|
+
/**
|
|
1715
|
+
* The policy forwarded to a durable runtime: the call's, else the session's
|
|
1716
|
+
* (which `agent.session()` fills from the agent's), so the worker enforces
|
|
1717
|
+
* call over session over agent rather than its own default. A dynamic
|
|
1718
|
+
* agent's rendered policy is re-rendered by the runtime, so for one only a
|
|
1719
|
+
* policy the caller set on the session is forwarded.
|
|
1720
|
+
*/
|
|
1721
|
+
durablePolicy(callPolicy, reRendered = true) {
|
|
1722
|
+
if (callPolicy !== undefined)
|
|
1723
|
+
return callPolicy;
|
|
1724
|
+
// Only prompts and tasks re-render a dynamic agent on the worker; a
|
|
1725
|
+
// shell call does not, so it carries the rendered policy itself.
|
|
1726
|
+
if (this.options.dynamic && reRendered)
|
|
1727
|
+
return explicitSessionPolicies.get(this.options);
|
|
1728
|
+
return this.options.policy;
|
|
1729
|
+
}
|
|
1682
1730
|
async createSandbox() {
|
|
1683
1731
|
const sandbox = this.options.sandbox;
|
|
1684
1732
|
let env;
|
|
@@ -1698,9 +1746,12 @@ export class StubFabricSession {
|
|
|
1698
1746
|
const scoped = createScopedSandboxEnv(env, this.options.cwd);
|
|
1699
1747
|
return wrapWithPolicy(scoped, this.options.policy, this.options.bypassPolicyEnforcement);
|
|
1700
1748
|
}
|
|
1701
|
-
async executeToolCalls(calls, tools,
|
|
1749
|
+
async executeToolCalls(calls, tools, sessionSandbox, callPolicy, approvalTimeoutMs, approvalHandler, attribution, signal, prepareOutput, recordToolCall) {
|
|
1702
1750
|
const results = [];
|
|
1703
1751
|
const policy = callPolicy ?? this.options.policy;
|
|
1752
|
+
// Tools get the sandbox under the call's effective policy, layered over
|
|
1753
|
+
// the session's, so a narrower prompt policy reaches `context.sandbox`.
|
|
1754
|
+
const sandbox = this.sandboxUnderPolicy(sessionSandbox, policy);
|
|
1704
1755
|
for (const requestedCall of calls) {
|
|
1705
1756
|
const tool = tools.get(requestedCall.tool);
|
|
1706
1757
|
const canonical = canonicalizeToolCall({
|
|
@@ -1722,51 +1773,79 @@ export class StubFabricSession {
|
|
|
1722
1773
|
}
|
|
1723
1774
|
const approvedSandboxCalls = [];
|
|
1724
1775
|
let approvalGrant;
|
|
1776
|
+
const approvalGrants = [];
|
|
1777
|
+
// A scoped command tool's command, rendered once: the same command is
|
|
1778
|
+
// judged, shown to the approver, recorded and granted to the sandbox.
|
|
1779
|
+
const renderedCommand = renderScopedCommand(tool, call.input);
|
|
1780
|
+
const boundCommand = hasScopedCommandRenderer(tool)
|
|
1781
|
+
? renderedCommand
|
|
1782
|
+
: (renderedCommand ?? this.commandTextForApproval(tool, call.input));
|
|
1725
1783
|
const executingPrincipal = toolExecutingPrincipal(tool);
|
|
1726
1784
|
const mountDecision = this.checkMountWriteAccess(call);
|
|
1727
1785
|
const policyCall = tool?.policyAlias ? { ...call, tool: tool.policyAlias } : call;
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1786
|
+
// File paths are judged in every form the sandbox reaches, as its layer
|
|
1787
|
+
// judges them, so an approval is asked for here when one is needed there.
|
|
1788
|
+
const decision = mountDecision ?? evaluateToolCallPolicyInSandbox(policyCall, policy, sandbox);
|
|
1789
|
+
const commandDecision = this.evaluateScopedCommandToolCall(tool, call.input, policy, renderedCommand);
|
|
1790
|
+
if (!decision.allowed && !decision.approvalRequired) {
|
|
1791
|
+
const error = decision.reason ?? `Tool call denied by policy: ${call.tool}`;
|
|
1792
|
+
results.push({
|
|
1793
|
+
tool: call.tool,
|
|
1794
|
+
input: call.input,
|
|
1795
|
+
error,
|
|
1796
|
+
...(call.id ? { id: call.id } : {}),
|
|
1797
|
+
});
|
|
1798
|
+
await this.recordEntry("error", {
|
|
1799
|
+
tool: call.tool,
|
|
1800
|
+
toolCallId,
|
|
1801
|
+
error,
|
|
1802
|
+
approvalRequired: false,
|
|
1803
|
+
});
|
|
1804
|
+
await this.emit("error", { tool: call.tool, error, approvalRequired: false });
|
|
1805
|
+
continue;
|
|
1806
|
+
}
|
|
1807
|
+
if (!commandDecision.allowed && !commandDecision.approvalRequired) {
|
|
1808
|
+
const error = commandDecision.reason ?? `Command denied by policy: ${call.tool}`;
|
|
1809
|
+
results.push({
|
|
1810
|
+
tool: call.tool,
|
|
1811
|
+
input: call.input,
|
|
1812
|
+
error,
|
|
1813
|
+
...(call.id ? { id: call.id } : {}),
|
|
1814
|
+
});
|
|
1815
|
+
await this.recordEntry("error", {
|
|
1816
|
+
tool: call.tool,
|
|
1817
|
+
toolCallId,
|
|
1818
|
+
error,
|
|
1819
|
+
approvalRequired: false,
|
|
1820
|
+
});
|
|
1821
|
+
await this.emit("error", { tool: call.tool, error, approvalRequired: false });
|
|
1822
|
+
continue;
|
|
1823
|
+
}
|
|
1824
|
+
if (!decision.allowed && decision.approvalRequired) {
|
|
1825
|
+
const grant = await this.requestApproval({
|
|
1826
|
+
kind: "tool",
|
|
1827
|
+
tool: call.tool,
|
|
1828
|
+
subject: call.tool,
|
|
1829
|
+
toolCallId,
|
|
1830
|
+
...(executingPrincipal ? { executingPrincipal } : {}),
|
|
1831
|
+
...(boundCommand !== undefined ? { command: boundCommand } : {}),
|
|
1832
|
+
input: call.input,
|
|
1833
|
+
reason: decision.reason ?? `Approval required for tool: ${call.tool}`,
|
|
1834
|
+
...(decision.risk ? { risk: decision.risk } : {}),
|
|
1835
|
+
...(decision.matchedPattern ? { matchedPattern: decision.matchedPattern } : {}),
|
|
1836
|
+
...(decision.approvalAudience ? { audience: decision.approvalAudience } : {}),
|
|
1837
|
+
...(decision.approvalTtlSeconds !== undefined
|
|
1838
|
+
? { ttlSeconds: decision.approvalTtlSeconds }
|
|
1839
|
+
: {}),
|
|
1840
|
+
affectedPaths: affectedPathsFromInput(call.input, call.tool),
|
|
1841
|
+
}, policy, approvalTimeoutMs, approvalHandler);
|
|
1842
|
+
if (grant) {
|
|
1843
|
+
approvalGrant = grant;
|
|
1844
|
+
approvalGrants.push(grant);
|
|
1845
|
+
approvedSandboxCalls.push(policyCall);
|
|
1767
1846
|
}
|
|
1768
1847
|
else {
|
|
1769
|
-
const error = decision.reason ?? `
|
|
1848
|
+
const error = decision.reason ?? `Approval required for tool call: ${call.tool}`;
|
|
1770
1849
|
results.push({
|
|
1771
1850
|
tool: call.tool,
|
|
1772
1851
|
input: call.input,
|
|
@@ -1777,61 +1856,37 @@ export class StubFabricSession {
|
|
|
1777
1856
|
tool: call.tool,
|
|
1778
1857
|
toolCallId,
|
|
1779
1858
|
error,
|
|
1780
|
-
approvalRequired:
|
|
1859
|
+
approvalRequired: true,
|
|
1781
1860
|
});
|
|
1782
|
-
await this.emit("error", { tool: call.tool, error, approvalRequired:
|
|
1861
|
+
await this.emit("error", { tool: call.tool, error, approvalRequired: true });
|
|
1783
1862
|
continue;
|
|
1784
1863
|
}
|
|
1785
1864
|
}
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
if (!grant) {
|
|
1812
|
-
const error = commandDecision.reason ?? `Approval required for command: ${call.tool}`;
|
|
1813
|
-
results.push({
|
|
1814
|
-
tool: call.tool,
|
|
1815
|
-
input: call.input,
|
|
1816
|
-
error,
|
|
1817
|
-
...(call.id ? { id: call.id } : {}),
|
|
1818
|
-
});
|
|
1819
|
-
await this.recordEntry("error", {
|
|
1820
|
-
tool: call.tool,
|
|
1821
|
-
toolCallId,
|
|
1822
|
-
error,
|
|
1823
|
-
approvalRequired: true,
|
|
1824
|
-
});
|
|
1825
|
-
await this.emit("error", { tool: call.tool, error, approvalRequired: true });
|
|
1826
|
-
continue;
|
|
1827
|
-
}
|
|
1828
|
-
approvalGrant = grant;
|
|
1829
|
-
const approvedCommand = this.commandTextForApproval(tool, call.input);
|
|
1830
|
-
if (approvedCommand)
|
|
1831
|
-
approvedSandboxCalls.push({ tool: "bash", input: { command: approvedCommand } });
|
|
1832
|
-
}
|
|
1833
|
-
else {
|
|
1834
|
-
const error = commandDecision.reason ?? `Command denied by policy: ${call.tool}`;
|
|
1865
|
+
if (!commandDecision.allowed && commandDecision.approvalRequired) {
|
|
1866
|
+
// The approval names the exact command the tool will run.
|
|
1867
|
+
const grant = await this.requestApproval({
|
|
1868
|
+
kind: "command",
|
|
1869
|
+
tool: call.tool,
|
|
1870
|
+
toolCallId,
|
|
1871
|
+
...(executingPrincipal ? { executingPrincipal } : {}),
|
|
1872
|
+
...(boundCommand !== undefined ? { command: boundCommand } : {}),
|
|
1873
|
+
input: call.input,
|
|
1874
|
+
reason: commandDecision.reason ?? `Approval required for command: ${call.tool}`,
|
|
1875
|
+
...(commandDecision.risk ? { risk: commandDecision.risk } : {}),
|
|
1876
|
+
...(commandDecision.matchedPattern
|
|
1877
|
+
? { matchedPattern: commandDecision.matchedPattern }
|
|
1878
|
+
: {}),
|
|
1879
|
+
...(commandDecision.approvalAudience
|
|
1880
|
+
? { audience: commandDecision.approvalAudience }
|
|
1881
|
+
: {}),
|
|
1882
|
+
...(commandDecision.approvalTtlSeconds !== undefined
|
|
1883
|
+
? { ttlSeconds: commandDecision.approvalTtlSeconds }
|
|
1884
|
+
: {}),
|
|
1885
|
+
envKeys: commandEnvKeys(tool),
|
|
1886
|
+
affectedPaths: affectedPathsFromInput(call.input, call.tool),
|
|
1887
|
+
}, policy, approvalTimeoutMs, approvalHandler);
|
|
1888
|
+
if (!grant) {
|
|
1889
|
+
const error = commandDecision.reason ?? `Approval required for command: ${call.tool}`;
|
|
1835
1890
|
results.push({
|
|
1836
1891
|
tool: call.tool,
|
|
1837
1892
|
input: call.input,
|
|
@@ -1842,11 +1897,18 @@ export class StubFabricSession {
|
|
|
1842
1897
|
tool: call.tool,
|
|
1843
1898
|
toolCallId,
|
|
1844
1899
|
error,
|
|
1845
|
-
approvalRequired:
|
|
1900
|
+
approvalRequired: true,
|
|
1846
1901
|
});
|
|
1847
|
-
await this.emit("error", { tool: call.tool, error, approvalRequired:
|
|
1902
|
+
await this.emit("error", { tool: call.tool, error, approvalRequired: true });
|
|
1848
1903
|
continue;
|
|
1849
1904
|
}
|
|
1905
|
+
approvalGrant = grant;
|
|
1906
|
+
approvalGrants.push(grant);
|
|
1907
|
+
// The sandbox grant names the exact command the tool will run.
|
|
1908
|
+
// A metadata-only command tool runs its command itself; the grant
|
|
1909
|
+
// names the command its approval described.
|
|
1910
|
+
if (boundCommand !== undefined)
|
|
1911
|
+
approvedSandboxCalls.push({ tool: "bash", input: { command: boundCommand } });
|
|
1850
1912
|
}
|
|
1851
1913
|
await (recordToolCall ?? ((data) => this.recordEntry("tool_call", data)))({
|
|
1852
1914
|
tool: call.tool,
|
|
@@ -1857,8 +1919,13 @@ export class StubFabricSession {
|
|
|
1857
1919
|
? { executingPrincipal: executingPrincipal }
|
|
1858
1920
|
: {}),
|
|
1859
1921
|
...(approvalGrant ? { approval: approvalGrantToJson(approvalGrant) } : {}),
|
|
1922
|
+
...(approvalGrants.length > 0
|
|
1923
|
+
? { approvals: approvalGrants.map(approvalGrantToJson) }
|
|
1924
|
+
: {}),
|
|
1860
1925
|
...(tool?.metadata?.effect ? { effect: tool.metadata.effect } : {}),
|
|
1861
1926
|
...(tool?.durable ? { durable: true } : {}),
|
|
1927
|
+
// Recovery grants a scoped command only if it still renders this command.
|
|
1928
|
+
...(tool?.durable && boundCommand !== undefined ? { renderedCommand: boundCommand } : {}),
|
|
1862
1929
|
toolCallId,
|
|
1863
1930
|
...(attribution ? { attribution: attribution } : {}),
|
|
1864
1931
|
});
|
|
@@ -1900,6 +1967,7 @@ export class StubFabricSession {
|
|
|
1900
1967
|
const agentName = this.agent.name ?? submission?.agent;
|
|
1901
1968
|
const execute = () => Promise.resolve(executeTool(tool, input, {
|
|
1902
1969
|
sandbox,
|
|
1970
|
+
...(renderedCommand !== undefined ? { __fabricScopedCommand: renderedCommand } : {}),
|
|
1903
1971
|
toolCallId,
|
|
1904
1972
|
...(signal ? { signal } : {}),
|
|
1905
1973
|
onOrphanSettled: (settlement) => this.recordSandboxOrphanSettlement(call.tool, toolCallId, settlement),
|
|
@@ -2174,9 +2242,22 @@ export class StubFabricSession {
|
|
|
2174
2242
|
onCredentialMissing: this.options.autonomy?.onCredentialMissing ?? "fail",
|
|
2175
2243
|
};
|
|
2176
2244
|
}
|
|
2177
|
-
evaluateScopedCommandToolCall(tool, input, policy) {
|
|
2245
|
+
evaluateScopedCommandToolCall(tool, input, policy, rendered) {
|
|
2246
|
+
if (hasScopedCommandRenderer(tool)) {
|
|
2247
|
+
if (rendered === undefined) {
|
|
2248
|
+
return {
|
|
2249
|
+
allowed: false,
|
|
2250
|
+
reason: `Failed to render command for tool: ${tool?.name ?? "unknown"}`,
|
|
2251
|
+
};
|
|
2252
|
+
}
|
|
2253
|
+
return evaluateCommandPolicy(rendered, policy);
|
|
2254
|
+
}
|
|
2178
2255
|
if (tool?.metadata?.kind !== "command" || typeof tool.metadata.command !== "string")
|
|
2179
2256
|
return { allowed: true };
|
|
2257
|
+
// The exact command the tool will run (executable, default arguments,
|
|
2258
|
+
// buildArgs, quoting), as the sandbox layer will judge it.
|
|
2259
|
+
if (rendered !== undefined)
|
|
2260
|
+
return evaluateCommandPolicy(rendered, policy);
|
|
2180
2261
|
const args = typeof input === "object" &&
|
|
2181
2262
|
input !== null &&
|
|
2182
2263
|
Array.isArray(input.args)
|
|
@@ -2193,7 +2274,8 @@ export class StubFabricSession {
|
|
|
2193
2274
|
Array.isArray(input.args)
|
|
2194
2275
|
? input.args.filter((value) => typeof value === "string")
|
|
2195
2276
|
: [];
|
|
2196
|
-
|
|
2277
|
+
// Quoted as evaluateScopedCommandToolCall judges it and the tool runs it.
|
|
2278
|
+
return [tool.metadata.command, ...args.map(shellQuote)].join(" ");
|
|
2197
2279
|
}
|
|
2198
2280
|
clampBashToolInput(input, policy) {
|
|
2199
2281
|
if (!policy?.maxCommandTimeoutMs || typeof input !== "object" || input === null)
|
|
@@ -2202,12 +2284,14 @@ export class StubFabricSession {
|
|
|
2202
2284
|
const timeout = typeof record.timeout === "number" ? record.timeout : undefined;
|
|
2203
2285
|
return { ...record, timeout: clampCommandTimeout(timeout, policy) };
|
|
2204
2286
|
}
|
|
2205
|
-
resolveTools(
|
|
2287
|
+
resolveTools(promptTools = [], promptCommands = []) {
|
|
2206
2288
|
const tools = new Map();
|
|
2207
2289
|
const commandToolOptions = this.options.resolveSecret
|
|
2208
2290
|
? { resolveSecret: this.options.resolveSecret }
|
|
2209
2291
|
: {};
|
|
2210
|
-
|
|
2292
|
+
// Built-ins run on the sandbox each call is handed (`context.sandbox`),
|
|
2293
|
+
// which carries that call's effective policy, not one captured here.
|
|
2294
|
+
for (const tool of createBuiltinTools(undefined, this.effectivePackagedSkills()))
|
|
2211
2295
|
tools.set(tool.name, tool);
|
|
2212
2296
|
for (const tool of createCommandTools(this.options.commands ?? [], commandToolOptions))
|
|
2213
2297
|
tools.set(tool.name, tool);
|
|
@@ -2370,10 +2454,11 @@ export class StubFabricSession {
|
|
|
2370
2454
|
* appends one advisory `signal` entry so the model knows the previous turn
|
|
2371
2455
|
* was cut short. Idempotent: a repaired window has no dangling calls.
|
|
2372
2456
|
*/
|
|
2373
|
-
async repairDanglingToolCalls(tools,
|
|
2457
|
+
async repairDanglingToolCalls(tools, sessionSandbox, signal,
|
|
2374
2458
|
/** The resuming prompt's policy; the session's applies when absent. */
|
|
2375
2459
|
promptPolicy) {
|
|
2376
2460
|
const recoveryPolicy = promptPolicy ?? this.options.policy;
|
|
2461
|
+
const sandbox = this.sandboxUnderPolicy(sessionSandbox, recoveryPolicy);
|
|
2377
2462
|
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
2378
2463
|
let data = await this.history();
|
|
2379
2464
|
let path = SessionHistory.fromData(data).getActivePath();
|
|
@@ -2397,11 +2482,18 @@ export class StubFabricSession {
|
|
|
2397
2482
|
});
|
|
2398
2483
|
const recoveryStartedAt = performance.now();
|
|
2399
2484
|
try {
|
|
2400
|
-
|
|
2485
|
+
// Rendered once: authorised, compared with the recorded command, run.
|
|
2486
|
+
const scoped = renderScopedCommand(tool, call.data?.input);
|
|
2487
|
+
// A metadata-only command tool runs the command its approval described.
|
|
2488
|
+
const rendered = hasScopedCommandRenderer(tool)
|
|
2489
|
+
? scoped
|
|
2490
|
+
: (scoped ?? this.commandTextForApproval(tool, call.data?.input));
|
|
2491
|
+
const authorization = await this.authorizeRecoveredToolCall(call, toolName, tool, toolCallId, recoveryPolicy, sandbox, scoped, rendered);
|
|
2401
2492
|
const submission = currentSubmissionContext();
|
|
2402
2493
|
const agentName = this.agent.name ?? submission?.agent;
|
|
2403
2494
|
const execute = () => Promise.resolve(executeTool(tool, call.data?.input, {
|
|
2404
2495
|
sandbox,
|
|
2496
|
+
...(scoped !== undefined ? { __fabricScopedCommand: scoped } : {}),
|
|
2405
2497
|
toolCallId,
|
|
2406
2498
|
...(signal ? { signal } : {}),
|
|
2407
2499
|
onOrphanSettled: (settlement) => this.recordSandboxOrphanSettlement(toolName, toolCallId, settlement),
|
|
@@ -2425,10 +2517,15 @@ export class StubFabricSession {
|
|
|
2425
2517
|
}
|
|
2426
2518
|
: {}),
|
|
2427
2519
|
}));
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
:
|
|
2520
|
+
// Only re-grant the sandbox gate(s) whose persisted approvals were
|
|
2521
|
+
// verified against the active policy and recorded command.
|
|
2522
|
+
const approvedCalls = [
|
|
2523
|
+
...(authorization.approvedToolCall ? [authorization.approvedToolCall] : []),
|
|
2524
|
+
...(authorization.approvedCommand !== undefined
|
|
2525
|
+
? [{ tool: "bash", input: { command: authorization.approvedCommand } }]
|
|
2526
|
+
: []),
|
|
2527
|
+
];
|
|
2528
|
+
const rawOutput = await abandonToolOnAbort(approvedCalls.reduceRight((next, granted) => () => runWithSandboxPolicyApproval(sandbox, granted, next), execute), signal);
|
|
2432
2529
|
const dynamicOutput = unwrapDynamicToolResult(rawOutput);
|
|
2433
2530
|
const hookOutput = unwrapHookToolRunResult(dynamicOutput.output);
|
|
2434
2531
|
const recoveryDurationMs = Math.max(0, Math.round(performance.now() - recoveryStartedAt));
|
|
@@ -2550,7 +2647,7 @@ export class StubFabricSession {
|
|
|
2550
2647
|
}
|
|
2551
2648
|
throw new Error(`Could not repair interrupted session ${this.id}: session leaf kept changing.`);
|
|
2552
2649
|
}
|
|
2553
|
-
async authorizeRecoveredToolCall(call, toolName, tool, toolCallId, policy) {
|
|
2650
|
+
async authorizeRecoveredToolCall(call, toolName, tool, toolCallId, policy, sandbox, scopedCommand, expectedCommand) {
|
|
2554
2651
|
const input = call.data?.input;
|
|
2555
2652
|
const inputDigest = approvalInputDigest(input);
|
|
2556
2653
|
if (call.data?.inputDigest !== inputDigest) {
|
|
@@ -2572,11 +2669,10 @@ export class StubFabricSession {
|
|
|
2572
2669
|
input,
|
|
2573
2670
|
id: toolCallId,
|
|
2574
2671
|
};
|
|
2575
|
-
const
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
];
|
|
2672
|
+
const toolDecision = this.checkMountWriteAccess({ tool: toolName, input, id: toolCallId }) ??
|
|
2673
|
+
evaluateToolCallPolicyInSandbox(policyCall, policy, sandbox);
|
|
2674
|
+
const commandDecision = this.evaluateScopedCommandToolCall(tool, input, policy, scopedCommand);
|
|
2675
|
+
const decisions = [toolDecision, commandDecision];
|
|
2580
2676
|
const denied = decisions.find((decision) => !decision.allowed && !decision.approvalRequired);
|
|
2581
2677
|
if (denied) {
|
|
2582
2678
|
throw new FabricError({
|
|
@@ -2584,12 +2680,28 @@ export class StubFabricSession {
|
|
|
2584
2680
|
message: denied.reason ?? `Recovered tool ${toolName} is denied by the current policy.`,
|
|
2585
2681
|
});
|
|
2586
2682
|
}
|
|
2683
|
+
if ((expectedCommand !== undefined || call.data?.renderedCommand !== undefined) &&
|
|
2684
|
+
call.data?.renderedCommand !== expectedCommand) {
|
|
2685
|
+
throw new FabricError({
|
|
2686
|
+
code: "POLICY_DENIED",
|
|
2687
|
+
message: `Recovered tool ${toolName} renders a different command than the recorded call.`,
|
|
2688
|
+
});
|
|
2689
|
+
}
|
|
2587
2690
|
const requiresApproval = decisions.some((decision) => decision.approvalRequired);
|
|
2588
2691
|
if (!requiresApproval)
|
|
2589
2692
|
return { inputDigest, policyDigest };
|
|
2590
|
-
const
|
|
2693
|
+
const rawApprovals = Array.isArray(call.data?.approvals)
|
|
2694
|
+
? call.data.approvals
|
|
2695
|
+
: call.data?.approval !== undefined
|
|
2696
|
+
? [call.data.approval]
|
|
2697
|
+
: [];
|
|
2698
|
+
const grants = rawApprovals
|
|
2699
|
+
.map((item) => approvalGrantFromJson(item))
|
|
2700
|
+
.filter((item) => item !== undefined);
|
|
2591
2701
|
const principal = toolExecutingPrincipal(tool) ?? this.approvalPrincipal();
|
|
2592
|
-
if (
|
|
2702
|
+
if (grants.length === 0 ||
|
|
2703
|
+
rawApprovals.length !== grants.length ||
|
|
2704
|
+
grants.some((grant) => !sameApprovalOperation(grant, { toolCallId, toolInput: input, principal }))) {
|
|
2593
2705
|
throw new FabricError({
|
|
2594
2706
|
code: "POLICY_DENIED",
|
|
2595
2707
|
message: `Recovered tool ${toolName} requires a current approval bound to this call and principal.`,
|
|
@@ -2598,22 +2710,41 @@ export class StubFabricSession {
|
|
|
2598
2710
|
if (!this.store.getApprovalState) {
|
|
2599
2711
|
throw new FabricError({
|
|
2600
2712
|
code: "POLICY_DENIED",
|
|
2601
|
-
message: `Recovered tool ${toolName} cannot prove that approval ${
|
|
2713
|
+
message: `Recovered tool ${toolName} cannot prove that approval ${grants[0]?.approvalId ?? ""} remains valid.`,
|
|
2602
2714
|
});
|
|
2603
2715
|
}
|
|
2604
|
-
const
|
|
2605
|
-
const
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2716
|
+
const verifiedStates = [];
|
|
2717
|
+
const verifiedGrants = [];
|
|
2718
|
+
for (const grant of grants) {
|
|
2719
|
+
const state = await this.store.getApprovalState(this.id, grant.approvalId);
|
|
2720
|
+
const currentGrant = state
|
|
2721
|
+
? approvalGrantFromState(state, input, principal, toolCallId)
|
|
2722
|
+
: undefined;
|
|
2723
|
+
if (!state || !currentGrant) {
|
|
2724
|
+
throw new FabricError({
|
|
2725
|
+
code: "POLICY_DENIED",
|
|
2726
|
+
message: `Recovered tool ${toolName} approval is expired, denied, revoked, or unavailable.`,
|
|
2727
|
+
});
|
|
2728
|
+
}
|
|
2729
|
+
verifiedStates.push(state);
|
|
2730
|
+
verifiedGrants.push(currentGrant);
|
|
2731
|
+
}
|
|
2732
|
+
const hasToolApproval = verifiedStates.some((state) => state.request.kind === "tool");
|
|
2733
|
+
const hasCommandApproval = verifiedStates.some((state) => state.request.kind === "command");
|
|
2734
|
+
if ((toolDecision.approvalRequired && !hasToolApproval) ||
|
|
2735
|
+
(commandDecision.approvalRequired && !hasCommandApproval)) {
|
|
2609
2736
|
throw new FabricError({
|
|
2610
2737
|
code: "POLICY_DENIED",
|
|
2611
|
-
message: `Recovered tool ${toolName} approval
|
|
2738
|
+
message: `Recovered tool ${toolName} does not have an approval covering the active policy gate.`,
|
|
2612
2739
|
});
|
|
2613
2740
|
}
|
|
2741
|
+
const latestGrant = verifiedGrants.at(-1);
|
|
2614
2742
|
return {
|
|
2615
|
-
approval:
|
|
2616
|
-
|
|
2743
|
+
...(latestGrant ? { approval: latestGrant } : {}),
|
|
2744
|
+
...(hasToolApproval ? { approvedToolCall: policyCall } : {}),
|
|
2745
|
+
...(hasCommandApproval && expectedCommand !== undefined
|
|
2746
|
+
? { approvedCommand: expectedCommand }
|
|
2747
|
+
: {}),
|
|
2617
2748
|
inputDigest,
|
|
2618
2749
|
policyDigest,
|
|
2619
2750
|
};
|
|
@@ -2887,11 +3018,28 @@ function normalizeSkillReference(value) {
|
|
|
2887
3018
|
function stripMarkdownExtension(value) {
|
|
2888
3019
|
return value.endsWith(".md") ? value.slice(0, -3) : value;
|
|
2889
3020
|
}
|
|
2890
|
-
|
|
3021
|
+
const SINGLE_PATH_TOOLS = new Set([
|
|
3022
|
+
"read",
|
|
3023
|
+
"read_buffer",
|
|
3024
|
+
"stat",
|
|
3025
|
+
"readdir",
|
|
3026
|
+
"exists",
|
|
3027
|
+
"grep",
|
|
3028
|
+
"write",
|
|
3029
|
+
"edit",
|
|
3030
|
+
"mkdir",
|
|
3031
|
+
"rm",
|
|
3032
|
+
]);
|
|
3033
|
+
function affectedPathsFromInput(input, toolName) {
|
|
2891
3034
|
if (typeof input !== "object" || input === null)
|
|
2892
3035
|
return [];
|
|
2893
3036
|
const record = input;
|
|
2894
|
-
const
|
|
3037
|
+
const candidates = toolName === "glob"
|
|
3038
|
+
? [record.cwd]
|
|
3039
|
+
: toolName !== undefined && SINGLE_PATH_TOOLS.has(toolName)
|
|
3040
|
+
? [record.path]
|
|
3041
|
+
: [record.path, record.cwd];
|
|
3042
|
+
const paths = candidates.filter((value) => typeof value === "string");
|
|
2895
3043
|
return [...new Set(paths)];
|
|
2896
3044
|
}
|
|
2897
3045
|
function commandEnvKeys(tool) {
|