@fabric-harness/sdk 6.14.2 → 6.15.0

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/session.js CHANGED
@@ -14,10 +14,10 @@ import { createFabricFs } from "./filesystem.js";
14
14
  import { openGovernedVoice, } from "./governed-voice.js";
15
15
  import { SessionHistory, buildModelMessagesFromHistory, findTrailingDanglingToolCalls, findTrailingUnfinishedTasks, } from "./history.js";
16
16
  import { getLogger } from "./logger.js";
17
- import { defaultLoopRuntime } from "./loop.js";
17
+ import { defaultLoopRuntime, sessionToolPolicyDispatchers } from "./loop.js";
18
18
  import { defaultModelProvider, generateWithRuntime } from "./model.js";
19
19
  import { evaluateToolCallPolicyInSandbox, policiedSandboxEnv, runWithSandboxPolicyApproval, } from "./policied-sandbox.js";
20
- import { canonicalizeToolCall, clampCommandTimeout, evaluateCommandPolicy } from "./policy.js";
20
+ import { aliasedToolDenial, canonicalizeToolCall, clampCommandTimeout, evaluateCommandPolicy, evaluateToolNameRestrictions, RUNTIME_TOOL_ALIAS_POLICY, } from "./policy.js";
21
21
  import { createAttachmentHydrationBudget, hydrateModelMessageAttachments, preparePromptAttachments, recordsToModelAttachments, validatePromptAttachments, } from "./prompt-attachments.js";
22
22
  import { buildResultRetryPrompt, validateResult } from "./result.js";
23
23
  import { registerSandbox, serializeSandboxRef, unregisterSandbox } from "./sandbox-ref.js";
@@ -617,6 +617,10 @@ export class StubFabricSession {
617
617
  // Use tool-based result extraction when a validator is provided and the caller
618
618
  // has not explicitly opted into delimiter-based extraction.
619
619
  const useResultTools = options.result !== undefined && options.resultExtraction === undefined;
620
+ const dispatchTools = (calls, policy, approvalTimeoutMs, attribution) => this.executeToolCalls(calls, tools, sandbox,
621
+ // The prompt's policy wins over the loop's current turn policy.
622
+ options.policy ?? policy, approvalTimeoutMs, options.onApproval, attribution, options.signal, undefined, streamRecovery ? (data) => streamRecovery.record("tool_call", data) : undefined);
623
+ sessionToolPolicyDispatchers.add(dispatchTools);
620
624
  let runtimeEndData;
621
625
  const runRuntime = () => {
622
626
  runtimeEndData = undefined;
@@ -764,10 +768,7 @@ export class StubFabricSession {
764
768
  },
765
769
  }
766
770
  : {}),
767
- executeToolCalls: (calls, policy, approvalTimeoutMs, attribution) => this.executeToolCalls(calls, tools, sandbox,
768
- // The prompt's policy wins (call over session over agent). A loop may pass none for
769
- // model-issued calls, and a dynamic agent's turn refresh passes its rendered agent policy.
770
- options.policy ?? policy, approvalTimeoutMs, options.onApproval, attribution, options.signal, undefined, streamRecovery ? (data) => streamRecovery.record("tool_call", data) : undefined),
771
+ executeToolCalls: dispatchTools,
771
772
  recordToolAdditions: async (toolCallId, names) => {
772
773
  if (names.length === 0)
773
774
  return;
@@ -1880,6 +1881,30 @@ export class StubFabricSession {
1880
1881
  const scoped = createScopedSandboxEnv(env, this.options.cwd);
1881
1882
  return wrapWithPolicy(scoped, this.options.policy, this.options.bypassPolicyEnforcement);
1882
1883
  }
1884
+ toolAliasDenial(toolName, policyToolName, policy) {
1885
+ if (toolName === policyToolName)
1886
+ return undefined;
1887
+ return (aliasedToolDenial(toolName, policyToolName, policy) ??
1888
+ (policy === this.options.policy
1889
+ ? undefined
1890
+ : aliasedToolDenial(toolName, policyToolName, this.options.policy)) ??
1891
+ aliasedToolDenial(toolName, policyToolName, this.options[RUNTIME_TOOL_ALIAS_POLICY]?.()));
1892
+ }
1893
+ assertToolAliasAtExecution(tool, toolName, policyToolName, policy) {
1894
+ if ((tool.policyAlias || toolName) !== policyToolName) {
1895
+ throw new FabricError({
1896
+ code: "POLICY_DENIED",
1897
+ message: `Policy alias changed before executing tool: ${toolName}`,
1898
+ });
1899
+ }
1900
+ const denial = this.toolAliasDenial(toolName, policyToolName, policy);
1901
+ if (denial) {
1902
+ throw new FabricError({
1903
+ code: "POLICY_DENIED",
1904
+ message: denial.reason ?? `Tool denied by policy: ${toolName}`,
1905
+ });
1906
+ }
1907
+ }
1883
1908
  async executeToolCalls(calls, tools, sessionSandbox, callPolicy, approvalTimeoutMs, approvalHandler, attribution, signal, prepareOutput, recordToolCall) {
1884
1909
  const results = [];
1885
1910
  const policy = callPolicy ?? this.options.policy;
@@ -1891,9 +1916,10 @@ export class StubFabricSession {
1891
1916
  // Decided when these tools were resolved, and never derived again from
1892
1917
  // the definition: a tool not classified then runs as an ordinary tool.
1893
1918
  const proposal = this.resolvedProposal(tools, requestedCall.tool, tool);
1919
+ const policyToolName = (!proposal && tool?.policyAlias) || requestedCall.tool;
1894
1920
  const canonical = canonicalizeToolCall({
1895
1921
  ...requestedCall,
1896
- tool: tool?.policyAlias ?? requestedCall.tool,
1922
+ tool: policyToolName,
1897
1923
  });
1898
1924
  const call = { ...requestedCall, input: canonical.input };
1899
1925
  // Providers are allowed to omit a call id. Mint one once at the session
@@ -1920,10 +1946,12 @@ export class StubFabricSession {
1920
1946
  const executingPrincipal = toolExecutingPrincipal(tool);
1921
1947
  const mountDecision = this.checkMountWriteAccess(call);
1922
1948
  // Policy judges a proposal tool by its own name; resolution refuses an alias.
1923
- const policyCall = !proposal && tool?.policyAlias ? { ...call, tool: tool.policyAlias } : call;
1949
+ const policyCall = { ...call, tool: policyToolName };
1924
1950
  // File paths are judged in every form the sandbox reaches, as its layer
1925
1951
  // judges them, so an approval is asked for here when one is needed there.
1926
- const decision = mountDecision ?? evaluateToolCallPolicyInSandbox(policyCall, policy, sandbox);
1952
+ const decision = this.toolAliasDenial(call.tool, policyToolName, policy) ??
1953
+ mountDecision ??
1954
+ evaluateToolCallPolicyInSandbox(policyCall, policy, sandbox);
1927
1955
  const commandDecision = this.evaluateScopedCommandToolCall(tool, call.input, policy, renderedCommand);
1928
1956
  if (!decision.allowed && !decision.approvalRequired) {
1929
1957
  const error = decision.reason ?? `Tool call denied by policy: ${call.tool}`;
@@ -2076,7 +2104,7 @@ export class StubFabricSession {
2076
2104
  ? { approvals: approvalGrants.map(approvalGrantToJson) }
2077
2105
  : {}),
2078
2106
  ...(tool?.metadata?.effect ? { effect: tool.metadata.effect } : {}),
2079
- ...(tool?.durable ? { durable: true } : {}),
2107
+ ...(tool?.durable ? { durable: true, policyToolName } : {}),
2080
2108
  // Recovery grants a scoped command only if it still renders this command.
2081
2109
  ...(tool?.durable && boundCommand !== undefined ? { renderedCommand: boundCommand } : {}),
2082
2110
  toolCallId,
@@ -2130,32 +2158,35 @@ export class StubFabricSession {
2130
2158
  const input = effectiveInput;
2131
2159
  const submission = currentSubmissionContext();
2132
2160
  const agentName = this.agent.name ?? submission?.agent;
2133
- const execute = () => Promise.resolve(executeTool(tool, input, {
2134
- sandbox,
2135
- ...(renderedCommand !== undefined ? { __fabricScopedCommand: renderedCommand } : {}),
2136
- toolCallId,
2137
- ...(signal ? { signal } : {}),
2138
- onOrphanSettled: (settlement) => this.recordSandboxOrphanSettlement(call.tool, toolCallId, settlement),
2139
- ...(approvalGrant ? { approval: approvalGrant } : {}),
2140
- ...(this.options.actor ? { actor: this.options.actor } : {}),
2141
- ...(this.options.tenantId ? { tenantId: this.options.tenantId } : {}),
2142
- ...(this.agent.id ? { agentId: this.agent.id } : {}),
2143
- ...(agentName ? { agentName } : {}),
2144
- policyVersion: approvalInputDigest(policy ?? null),
2145
- ...(policy ? { policy } : {}),
2146
- inputDigest: approvalInputDigest(input),
2147
- ...(tool.durable ? { step: this.createToolStep(toolCallId) } : {}),
2148
- log: this.createToolProgressLogger(call.tool, toolCallId),
2149
- harness: { sandbox },
2150
- ...(submission
2151
- ? {
2152
- __fabricSubmission: {
2153
- submissionId: submission.submissionId,
2154
- attemptId: submission.attemptId,
2155
- },
2156
- }
2157
- : {}),
2158
- }));
2161
+ const execute = () => {
2162
+ this.assertToolAliasAtExecution(tool, call.tool, policyToolName, policy);
2163
+ return Promise.resolve(executeTool(tool, input, {
2164
+ sandbox,
2165
+ ...(renderedCommand !== undefined ? { __fabricScopedCommand: renderedCommand } : {}),
2166
+ toolCallId,
2167
+ ...(signal ? { signal } : {}),
2168
+ onOrphanSettled: (settlement) => this.recordSandboxOrphanSettlement(call.tool, toolCallId, settlement),
2169
+ ...(approvalGrant ? { approval: approvalGrant } : {}),
2170
+ ...(this.options.actor ? { actor: this.options.actor } : {}),
2171
+ ...(this.options.tenantId ? { tenantId: this.options.tenantId } : {}),
2172
+ ...(this.agent.id ? { agentId: this.agent.id } : {}),
2173
+ ...(agentName ? { agentName } : {}),
2174
+ policyVersion: approvalInputDigest(policy ?? null),
2175
+ ...(policy ? { policy } : {}),
2176
+ inputDigest: approvalInputDigest(input),
2177
+ ...(tool.durable ? { step: this.createToolStep(toolCallId) } : {}),
2178
+ log: this.createToolProgressLogger(call.tool, toolCallId),
2179
+ harness: { sandbox },
2180
+ ...(submission
2181
+ ? {
2182
+ __fabricSubmission: {
2183
+ submissionId: submission.submissionId,
2184
+ attemptId: submission.attemptId,
2185
+ },
2186
+ }
2187
+ : {}),
2188
+ }));
2189
+ };
2159
2190
  const rawOutput = await abandonToolOnAbort(approvedSandboxCalls.reduceRight((next, approvedCall) => () => runWithSandboxPolicyApproval(sandbox, approvedCall, next), execute), signal);
2160
2191
  const dynamicOutput = unwrapDynamicToolResult(rawOutput);
2161
2192
  const hookOutput = unwrapHookToolRunResult(dynamicOutput.output);
@@ -2699,32 +2730,35 @@ export class StubFabricSession {
2699
2730
  const authorization = await this.authorizeRecoveredToolCall(call, toolName, tool, toolCallId, recoveryPolicy, sandbox, scoped, rendered);
2700
2731
  const submission = currentSubmissionContext();
2701
2732
  const agentName = this.agent.name ?? submission?.agent;
2702
- const execute = () => Promise.resolve(executeTool(tool, call.data?.input, {
2703
- sandbox,
2704
- ...(scoped !== undefined ? { __fabricScopedCommand: scoped } : {}),
2705
- toolCallId,
2706
- ...(signal ? { signal } : {}),
2707
- onOrphanSettled: (settlement) => this.recordSandboxOrphanSettlement(toolName, toolCallId, settlement),
2708
- ...(authorization.approval ? { approval: authorization.approval } : {}),
2709
- ...(this.options.actor ? { actor: this.options.actor } : {}),
2710
- ...(this.options.tenantId ? { tenantId: this.options.tenantId } : {}),
2711
- ...(this.agent.id ? { agentId: this.agent.id } : {}),
2712
- ...(agentName ? { agentName } : {}),
2713
- policyVersion: authorization.policyDigest,
2714
- ...(recoveryPolicy ? { policy: recoveryPolicy } : {}),
2715
- inputDigest: authorization.inputDigest,
2716
- step: this.createToolStep(toolCallId),
2717
- log: this.createToolProgressLogger(toolName, toolCallId),
2718
- harness: { sandbox },
2719
- ...(submission
2720
- ? {
2721
- __fabricSubmission: {
2722
- submissionId: submission.submissionId,
2723
- attemptId: submission.attemptId,
2724
- },
2725
- }
2726
- : {}),
2727
- }));
2733
+ const execute = () => {
2734
+ this.assertToolAliasAtExecution(tool, toolName, authorization.policyToolName, recoveryPolicy);
2735
+ return Promise.resolve(executeTool(tool, call.data?.input, {
2736
+ sandbox,
2737
+ ...(scoped !== undefined ? { __fabricScopedCommand: scoped } : {}),
2738
+ toolCallId,
2739
+ ...(signal ? { signal } : {}),
2740
+ onOrphanSettled: (settlement) => this.recordSandboxOrphanSettlement(toolName, toolCallId, settlement),
2741
+ ...(authorization.approval ? { approval: authorization.approval } : {}),
2742
+ ...(this.options.actor ? { actor: this.options.actor } : {}),
2743
+ ...(this.options.tenantId ? { tenantId: this.options.tenantId } : {}),
2744
+ ...(this.agent.id ? { agentId: this.agent.id } : {}),
2745
+ ...(agentName ? { agentName } : {}),
2746
+ policyVersion: authorization.policyDigest,
2747
+ ...(recoveryPolicy ? { policy: recoveryPolicy } : {}),
2748
+ inputDigest: authorization.inputDigest,
2749
+ step: this.createToolStep(toolCallId),
2750
+ log: this.createToolProgressLogger(toolName, toolCallId),
2751
+ harness: { sandbox },
2752
+ ...(submission
2753
+ ? {
2754
+ __fabricSubmission: {
2755
+ submissionId: submission.submissionId,
2756
+ attemptId: submission.attemptId,
2757
+ },
2758
+ }
2759
+ : {}),
2760
+ }));
2761
+ };
2728
2762
  // Only re-grant the sandbox gate(s) whose persisted approvals were
2729
2763
  // verified against the active policy and recorded command.
2730
2764
  const approvedCalls = [
@@ -2872,12 +2906,26 @@ export class StubFabricSession {
2872
2906
  });
2873
2907
  }
2874
2908
  const policyDigest = approvalInputDigest(policy ?? null);
2909
+ const policyToolName = tool.policyAlias || toolName;
2910
+ const recordedName = call.data?.policyToolName;
2911
+ // A legacy entry lacks evidence that an alias approval was absent or was
2912
+ // issued for the same name. Keep unrelated legacy recovery available.
2913
+ if (recordedName === undefined
2914
+ ? policyToolName !== toolName ||
2915
+ evaluateToolNameRestrictions(toolName, policy)?.approvalRequired
2916
+ : recordedName !== policyToolName) {
2917
+ throw new FabricError({
2918
+ code: "POLICY_DENIED",
2919
+ message: `Recovered tool ${toolName} has no matching recorded policy name.`,
2920
+ });
2921
+ }
2875
2922
  const policyCall = {
2876
- tool: tool.policyAlias ?? toolName,
2923
+ tool: policyToolName,
2877
2924
  input,
2878
2925
  id: toolCallId,
2879
2926
  };
2880
- const toolDecision = this.checkMountWriteAccess({ tool: toolName, input, id: toolCallId }) ??
2927
+ const toolDecision = this.toolAliasDenial(toolName, policyToolName, policy) ??
2928
+ this.checkMountWriteAccess({ tool: toolName, input, id: toolCallId }) ??
2881
2929
  evaluateToolCallPolicyInSandbox(policyCall, policy, sandbox);
2882
2930
  const commandDecision = this.evaluateScopedCommandToolCall(tool, input, policy, scopedCommand);
2883
2931
  const decisions = [toolDecision, commandDecision];
@@ -2897,7 +2945,7 @@ export class StubFabricSession {
2897
2945
  }
2898
2946
  const requiresApproval = decisions.some((decision) => decision.approvalRequired);
2899
2947
  if (!requiresApproval)
2900
- return { inputDigest, policyDigest };
2948
+ return { inputDigest, policyDigest, policyToolName };
2901
2949
  const rawApprovals = Array.isArray(call.data?.approvals)
2902
2950
  ? call.data.approvals
2903
2951
  : call.data?.approval !== undefined
@@ -2955,6 +3003,7 @@ export class StubFabricSession {
2955
3003
  : {}),
2956
3004
  inputDigest,
2957
3005
  policyDigest,
3006
+ policyToolName,
2958
3007
  };
2959
3008
  }
2960
3009
  async recordEntry(type, data) {