@crewhaus/runtime-core 0.5.2 → 0.5.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/dist/index.d.ts +46 -1
- package/dist/index.js +337 -18
- package/package.json +40 -40
package/dist/index.d.ts
CHANGED
|
@@ -126,6 +126,51 @@ export type { CreateJanitorOptions, Janitor, JanitorBuiltinStepName, JanitorRese
|
|
|
126
126
|
export { AGENT_IDENTITY_SCHEMA_VERSION, DEFAULT_IDENTITY_DIR, IDENTITY_FILENAME, agentFingerprint, loadOrCreateAgentIdentity, } from "./identity";
|
|
127
127
|
export type { AgentIdentityFile } from "./identity";
|
|
128
128
|
export { type ApprovalDecision, type PendingApproval, type PendingApprovalStore, type PendingApprovalStoreOptions, createPendingApprovalStore, evictExpiredApprovals, generateApprovalId, hashApprovalInput, } from "@crewhaus/session-store";
|
|
129
|
+
/**
|
|
130
|
+
* How long after an operator's DECISION a grant may still satisfy a call the
|
|
131
|
+
* model has re-worded. Generous enough for a resume (which follows the grant
|
|
132
|
+
* within seconds on every surface), short enough that an unspent grant cannot
|
|
133
|
+
* be replayed onto an unrelated turn an hour later.
|
|
134
|
+
*/
|
|
135
|
+
export declare const REPLAYABLE_GRANT_MAX_AGE_MS: number;
|
|
136
|
+
/**
|
|
137
|
+
* #400 — find the grant an operator made for a call this run has since
|
|
138
|
+
* REGENERATED differently.
|
|
139
|
+
*
|
|
140
|
+
* Only ever consulted after the exact-hash lookup misses, so the normal path
|
|
141
|
+
* (a byte-identical re-issue) is untouched. Every condition below is load-
|
|
142
|
+
* bearing; together they are what keeps a replay from becoming a standing
|
|
143
|
+
* allow:
|
|
144
|
+
*
|
|
145
|
+
* - `sessionId` — a grant never leaks into another conversation.
|
|
146
|
+
* - `toolName` — a grant for one tool never authorizes another.
|
|
147
|
+
* - `decision === "grant"` and no `consumedAt` — one-shot means one shot.
|
|
148
|
+
* - `runId !== <this run>` — the grant must come from a PRIOR run, i.e.
|
|
149
|
+
* this really is a resume. Without it a run could park, be granted, and
|
|
150
|
+
* then spend that grant on a *second, different* call inside the same
|
|
151
|
+
* run — turning one approval into a tool-wide licence for the turn.
|
|
152
|
+
* - `input !== undefined` — the record must actually carry what was
|
|
153
|
+
* approved. `PendingApproval.input` is optional and unvalidated, so a
|
|
154
|
+
* record written by an older or foreign writer has nothing to replay; the
|
|
155
|
+
* honest answer there is to park, not to guess.
|
|
156
|
+
*
|
|
157
|
+
* Newest-first, so when several grants are outstanding the most recent
|
|
158
|
+
* decision wins — the same "latest decision" posture the store's own `get`
|
|
159
|
+
* takes.
|
|
160
|
+
*
|
|
161
|
+
* `list` is optional on the injected store: a caller that supplies a minimal
|
|
162
|
+
* `{persist,get,resolve}` store simply never replays and keeps the pre-#400
|
|
163
|
+
* behaviour, rather than failing.
|
|
164
|
+
*/
|
|
165
|
+
export declare function findReplayableGrant(store: Pick<PendingApprovalStore, "persist" | "get" | "resolve"> & Partial<Pick<PendingApprovalStore, "list">>, where: {
|
|
166
|
+
readonly toolName: string;
|
|
167
|
+
readonly sessionId: string;
|
|
168
|
+
readonly runId: string;
|
|
169
|
+
/** Epoch ms; the freshness window is measured from the DECISION. */
|
|
170
|
+
readonly now?: number;
|
|
171
|
+
/** Override the window (tests). */
|
|
172
|
+
readonly maxAgeMs?: number;
|
|
173
|
+
}): Promise<PendingApproval | undefined>;
|
|
129
174
|
export { type AlertBreachPayload, type AlertSink, type AttachDefaultSubscribersOptions, attachDefaultSubscribers, } from "./observability";
|
|
130
175
|
export { type AttachedSloMonitor, type AttachSloMonitorOptions, type SloBreach, type SloMitigationEvent, type SloMitigationRung, type SloMitigationSink, type SloTargets, type SloWindowMetrics, DEFAULT_SLO_WINDOW_MS, MIN_SLO_SAMPLES, SloWindow, attachSloMonitor, detectSloBreaches, } from "./slo-monitor";
|
|
131
176
|
export { type AttachIncidentCollectorOptions, type IncidentCapture, type IncidentTriggerKind, attachIncidentCollector, classifyIncidentTrigger, } from "./incident-collector";
|
|
@@ -1134,7 +1179,7 @@ export type RunChatLoopOptions = {
|
|
|
1134
1179
|
* from the full `PendingApprovalStore` so a caller may inject a minimal
|
|
1135
1180
|
* store; the concrete `createPendingApprovalStore` satisfies it.
|
|
1136
1181
|
*/
|
|
1137
|
-
readonly store: Pick<PendingApprovalStore, "persist" | "get" | "resolve"
|
|
1182
|
+
readonly store: Pick<PendingApprovalStore, "persist" | "get" | "resolve"> & Partial<Pick<PendingApprovalStore, "list">>;
|
|
1138
1183
|
readonly notify?: (approval: PendingApproval) => Promise<void>;
|
|
1139
1184
|
readonly surface?: string;
|
|
1140
1185
|
};
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ import { createStore } from "@crewhaus/state-store";
|
|
|
28
28
|
import { executeStreaming } from "@crewhaus/streaming-tool-executor";
|
|
29
29
|
import { currentTenantContext } from "@crewhaus/tenancy";
|
|
30
30
|
import { TokenBudget, estimateTokens } from "@crewhaus/token-budget";
|
|
31
|
+
import { stripJustificationField, withJustificationField } from "@crewhaus/tool-catalog";
|
|
31
32
|
import { executeTool } from "@crewhaus/tool-executor";
|
|
32
33
|
import { detectLoop } from "@crewhaus/tool-loop-detection";
|
|
33
34
|
import { partitionToolCalls } from "@crewhaus/tool-orchestrator";
|
|
@@ -145,6 +146,33 @@ function toAnthropicInputSchema(t) {
|
|
|
145
146
|
// Degenerate (no object type, no union): coerce to an empty object schema.
|
|
146
147
|
return { type: "object", properties: converted.properties ?? {} };
|
|
147
148
|
}
|
|
149
|
+
// Pillar 3 intent gate (#386) — the advertised (model-facing) schema for one
|
|
150
|
+
// registered tool. For `requireJustification` tools the gate reads
|
|
151
|
+
// `input["justification"]`, but models conform tightly to advertised schemas
|
|
152
|
+
// and never invent undeclared fields — so unless the contract is IN the
|
|
153
|
+
// schema, every gated call is denied `justification too brief (0 chars)` and
|
|
154
|
+
// the gate degrades into a hard-off switch (verified live: 12/12 wiki_write
|
|
155
|
+
// calls schema-exact with the field absent even after spec instructions
|
|
156
|
+
// taught it). `withJustificationField` injects a required, described
|
|
157
|
+
// `justification` string property unless the tool's own schema already
|
|
158
|
+
// declares one; `justificationInjected` tells the execute path whether to
|
|
159
|
+
// strip the field back out before the input reaches the tool (remote MCP
|
|
160
|
+
// servers validate against THEIR schema, which may set
|
|
161
|
+
// `additionalProperties: false`). Augmentation runs on the COERCED object
|
|
162
|
+
// schema (after `toAnthropicInputSchema`), so MCP verbatim schemas, flattened
|
|
163
|
+
// unions, and degenerate schemas are all covered, and it copies rather than
|
|
164
|
+
// mutates — `t.jsonSchema` may pass through `toAnthropicInputSchema` by
|
|
165
|
+
// reference and is hashed elsewhere for schema-drift detection.
|
|
166
|
+
function toAdvertisedTool(t) {
|
|
167
|
+
const base = toAnthropicInputSchema(t);
|
|
168
|
+
if (t.requireJustification !== true)
|
|
169
|
+
return { inputSchema: base, justificationInjected: false };
|
|
170
|
+
const { schema, injected } = withJustificationField(base);
|
|
171
|
+
return {
|
|
172
|
+
inputSchema: schema,
|
|
173
|
+
justificationInjected: injected,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
148
176
|
// Merge a top-level union of object branches into a single object schema.
|
|
149
177
|
// Anthropic forbids a top-level `anyOf`, but a nested `anyOf` on an individual
|
|
150
178
|
// property is fine — so each property becomes the union of its per-branch
|
|
@@ -385,6 +413,91 @@ export { AGENT_IDENTITY_SCHEMA_VERSION, DEFAULT_IDENTITY_DIR, IDENTITY_FILENAME,
|
|
|
385
413
|
// build the store + input hash it passes as `RunChatLoopOptions.approvals`
|
|
386
414
|
// without depending on session-store directly.
|
|
387
415
|
export { createPendingApprovalStore, evictExpiredApprovals, generateApprovalId, hashApprovalInput, } from "@crewhaus/session-store";
|
|
416
|
+
/**
|
|
417
|
+
* How long after an operator's DECISION a grant may still satisfy a call the
|
|
418
|
+
* model has re-worded. Generous enough for a resume (which follows the grant
|
|
419
|
+
* within seconds on every surface), short enough that an unspent grant cannot
|
|
420
|
+
* be replayed onto an unrelated turn an hour later.
|
|
421
|
+
*/
|
|
422
|
+
export const REPLAYABLE_GRANT_MAX_AGE_MS = 15 * 60 * 1000;
|
|
423
|
+
/**
|
|
424
|
+
* #400 — find the grant an operator made for a call this run has since
|
|
425
|
+
* REGENERATED differently.
|
|
426
|
+
*
|
|
427
|
+
* Only ever consulted after the exact-hash lookup misses, so the normal path
|
|
428
|
+
* (a byte-identical re-issue) is untouched. Every condition below is load-
|
|
429
|
+
* bearing; together they are what keeps a replay from becoming a standing
|
|
430
|
+
* allow:
|
|
431
|
+
*
|
|
432
|
+
* - `sessionId` — a grant never leaks into another conversation.
|
|
433
|
+
* - `toolName` — a grant for one tool never authorizes another.
|
|
434
|
+
* - `decision === "grant"` and no `consumedAt` — one-shot means one shot.
|
|
435
|
+
* - `runId !== <this run>` — the grant must come from a PRIOR run, i.e.
|
|
436
|
+
* this really is a resume. Without it a run could park, be granted, and
|
|
437
|
+
* then spend that grant on a *second, different* call inside the same
|
|
438
|
+
* run — turning one approval into a tool-wide licence for the turn.
|
|
439
|
+
* - `input !== undefined` — the record must actually carry what was
|
|
440
|
+
* approved. `PendingApproval.input` is optional and unvalidated, so a
|
|
441
|
+
* record written by an older or foreign writer has nothing to replay; the
|
|
442
|
+
* honest answer there is to park, not to guess.
|
|
443
|
+
*
|
|
444
|
+
* Newest-first, so when several grants are outstanding the most recent
|
|
445
|
+
* decision wins — the same "latest decision" posture the store's own `get`
|
|
446
|
+
* takes.
|
|
447
|
+
*
|
|
448
|
+
* `list` is optional on the injected store: a caller that supplies a minimal
|
|
449
|
+
* `{persist,get,resolve}` store simply never replays and keeps the pre-#400
|
|
450
|
+
* behaviour, rather than failing.
|
|
451
|
+
*/
|
|
452
|
+
export async function findReplayableGrant(store, where) {
|
|
453
|
+
if (typeof store.list !== "function")
|
|
454
|
+
return undefined;
|
|
455
|
+
let all;
|
|
456
|
+
try {
|
|
457
|
+
all = await store.list();
|
|
458
|
+
}
|
|
459
|
+
catch {
|
|
460
|
+
// A read that fails must not fail the tool call: park as before.
|
|
461
|
+
return undefined;
|
|
462
|
+
}
|
|
463
|
+
const now = where.now ?? Date.now();
|
|
464
|
+
const maxAgeMs = where.maxAgeMs ?? REPLAYABLE_GRANT_MAX_AGE_MS;
|
|
465
|
+
const matches = all.filter((a) => {
|
|
466
|
+
if (a.toolName !== where.toolName)
|
|
467
|
+
return false;
|
|
468
|
+
if (a.sessionId !== where.sessionId)
|
|
469
|
+
return false;
|
|
470
|
+
if (a.decision !== "grant")
|
|
471
|
+
return false;
|
|
472
|
+
if (a.consumedAt !== undefined)
|
|
473
|
+
return false;
|
|
474
|
+
if (a.runId === where.runId)
|
|
475
|
+
return false;
|
|
476
|
+
if (a.input === undefined)
|
|
477
|
+
return false;
|
|
478
|
+
// FRESHNESS, measured from the DECISION — not from the park.
|
|
479
|
+
//
|
|
480
|
+
// Without it a grant that was never spent sits in a long-lived session
|
|
481
|
+
// forever and is replayed onto an unrelated later turn: the operator
|
|
482
|
+
// asked for X an hour ago, approved it, the resume happened not to call
|
|
483
|
+
// the tool, and now an unrelated request runs X. Bounding from
|
|
484
|
+
// `decidedAt` is what makes the window track the approve→resume gap
|
|
485
|
+
// (seconds: the Slack button resumes immediately, and a CLI grant is
|
|
486
|
+
// followed by `runs resume`) rather than the park→approve gap, which is
|
|
487
|
+
// human-paced and may be hours.
|
|
488
|
+
//
|
|
489
|
+
// A grant older than this is not discarded — it stays a valid record for
|
|
490
|
+
// the EXACT-hash path, which is unbounded. Only the fuzzy replay is
|
|
491
|
+
// time-boxed.
|
|
492
|
+
const decidedAt = a.decidedAt !== undefined ? Date.parse(a.decidedAt) : Number.NaN;
|
|
493
|
+
if (!Number.isFinite(decidedAt))
|
|
494
|
+
return false;
|
|
495
|
+
return now - decidedAt <= maxAgeMs;
|
|
496
|
+
});
|
|
497
|
+
// `list()` is newest-first by contract, but sort defensively rather than
|
|
498
|
+
// trust the ordering of a store a caller injected.
|
|
499
|
+
return [...matches].sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1))[0];
|
|
500
|
+
}
|
|
388
501
|
// Ops item 31 — alert watchdog seams re-exported so the CLI/codegen can build
|
|
389
502
|
// the durable + off-box alert sink (audit append + settings.json alert hook /
|
|
390
503
|
// webhook) it passes as `RunChatLoopOptions.alertSink`.
|
|
@@ -1756,13 +1869,43 @@ export async function runChatLoop(opts) {
|
|
|
1756
1869
|
}
|
|
1757
1870
|
}
|
|
1758
1871
|
const tools = effectiveTools;
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1872
|
+
// (#386) — advertise each tool's model-facing schema, with the intent
|
|
1873
|
+
// gate's `justification` contract injected for `requireJustification`
|
|
1874
|
+
// tools whose own schema doesn't declare it (see `toAdvertisedTool`).
|
|
1875
|
+
const advertisedTools = tools.map((t) => ({ tool: t, advertised: toAdvertisedTool(t) }));
|
|
1876
|
+
const anthropicTools = advertisedTools.length > 0
|
|
1877
|
+
? advertisedTools.map(({ tool, advertised }) => ({
|
|
1878
|
+
name: tool.name,
|
|
1879
|
+
description: tool.description,
|
|
1880
|
+
input_schema: advertised.inputSchema,
|
|
1764
1881
|
}))
|
|
1765
1882
|
: undefined;
|
|
1883
|
+
// (#386) — tools whose advertised schema carries a RUNTIME-INJECTED
|
|
1884
|
+
// justification property. For exactly these, the field is gate metadata
|
|
1885
|
+
// rather than tool input — the underlying validator/server never declared
|
|
1886
|
+
// it (a remote MCP server with `additionalProperties: false` would reject
|
|
1887
|
+
// the call) — so every surface that reasons about WHAT THE TOOL DOES
|
|
1888
|
+
// consumes the OPERATIVE input (justification stripped) while the
|
|
1889
|
+
// surfaces that reason about WHAT THE MODEL DID keep the full input.
|
|
1890
|
+
// Operative-input consumers: permission-rule matching (arg-scoped
|
|
1891
|
+
// `Tool(argGlob)` rules must keep matching the way they did before the
|
|
1892
|
+
// field existed), the headless approval `inputHash` (a one-shot grant must
|
|
1893
|
+
// be consumable by a regenerated call whose operative args are identical
|
|
1894
|
+
// even though the re-worded justification is not), tool-loop detection
|
|
1895
|
+
// history (re-worded justifications must not make identical calls hash
|
|
1896
|
+
// apart), the egress classifier (scan what is actually transmitted), and
|
|
1897
|
+
// the tool executor itself. Full-input consumers: the event log, trace
|
|
1898
|
+
// bus, pre-tool hooks, the approval record's display `input`, the
|
|
1899
|
+
// justification gate, and the justification audit sink. Tools that
|
|
1900
|
+
// declare the field in their own schema are NOT in this set — for them
|
|
1901
|
+
// the field is genuine tool input everywhere.
|
|
1902
|
+
// #400 — approval ids this RUN has already replayed. Claimed synchronously
|
|
1903
|
+
// so two concurrent tool calls cannot both spend one one-shot grant.
|
|
1904
|
+
const replayedApprovalIds = new Set();
|
|
1905
|
+
const justificationInjectedTools = new Set(advertisedTools
|
|
1906
|
+
.filter(({ advertised }) => advertised.justificationInjected)
|
|
1907
|
+
.map(({ tool }) => tool.name));
|
|
1908
|
+
const operativeToolInput = (name, input) => justificationInjectedTools.has(name) ? stripJustificationField(input) : input;
|
|
1766
1909
|
const toolByName = new Map(tools.map((t) => [t.name, t]));
|
|
1767
1910
|
// G17 — per-tool rate limiter, built once at loop start from the lowered
|
|
1768
1911
|
// `agent.rate_limits` map. One token bucket per entry: `rpm` is the
|
|
@@ -1919,7 +2062,13 @@ export async function runChatLoop(opts) {
|
|
|
1919
2062
|
* Used by both the partitioned (post-stream) path and the streaming
|
|
1920
2063
|
* path so the permission/abort/store contract is uniform.
|
|
1921
2064
|
*/
|
|
1922
|
-
async function executeOneToolUse(
|
|
2065
|
+
async function executeOneToolUse(tuAsRequested) {
|
|
2066
|
+
// REBINDABLE on purpose (#400): a replayed approval substitutes the
|
|
2067
|
+
// operator-approved input for the model's regenerated one, and every
|
|
2068
|
+
// consumer below — the validator, the executor, the justification gate,
|
|
2069
|
+
// the egress classifier, the result record — reads through `tu`. Rebinding
|
|
2070
|
+
// here is what makes the substitution reach all of them instead of one.
|
|
2071
|
+
let tu = tuAsRequested;
|
|
1923
2072
|
// End the "thinking" wait — but only when no tool is already mid-flight.
|
|
1924
2073
|
// The streaming executor dispatches concurrency-safe tools with overlap, so
|
|
1925
2074
|
// an unconditional stop here would tear down a sibling tool's live spinner;
|
|
@@ -1953,7 +2102,30 @@ export async function runChatLoop(opts) {
|
|
|
1953
2102
|
});
|
|
1954
2103
|
const t0Tool = performance.now();
|
|
1955
2104
|
const tool = toolByName.get(tu.name);
|
|
1956
|
-
|
|
2105
|
+
// #400 — set when this call ran the input an operator APPROVED rather
|
|
2106
|
+
// than the one the model regenerated. Function-scoped so `finish` (the
|
|
2107
|
+
// single choke point every result passes through) can tell the model,
|
|
2108
|
+
// whichever return path fires.
|
|
2109
|
+
let replayedFrom;
|
|
2110
|
+
const finish = async (resultIn) => {
|
|
2111
|
+
let result = resultIn;
|
|
2112
|
+
// Say so IN THE RESULT. The assistant message still shows the input the
|
|
2113
|
+
// model asked for, so without this the model would reason about an
|
|
2114
|
+
// action that did not happen — and might "correct" itself by re-issuing
|
|
2115
|
+
// the call it thinks failed.
|
|
2116
|
+
if (replayedFrom !== undefined) {
|
|
2117
|
+
const notice = `[approval ${replayedFrom.id} was granted for a different wording of this call; the APPROVED input was executed, not the one you just sent. Read the result as the outcome of the approved action.]`;
|
|
2118
|
+
// Block content (images, structured blocks) is just as capable of
|
|
2119
|
+
// misleading the model as a string, so the notice rides both shapes —
|
|
2120
|
+
// prepended as its own text block when the content is an array.
|
|
2121
|
+
result =
|
|
2122
|
+
typeof result.content === "string"
|
|
2123
|
+
? { ...result, content: `${notice}\n${result.content}` }
|
|
2124
|
+
: {
|
|
2125
|
+
...result,
|
|
2126
|
+
content: [{ type: "text", text: notice }, ...(result.content ?? [])],
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
1957
2129
|
await logEvent("tool_result", {
|
|
1958
2130
|
toolUseId: tu.id,
|
|
1959
2131
|
content: typeof result.content === "string"
|
|
@@ -2011,9 +2183,16 @@ export async function runChatLoop(opts) {
|
|
|
2011
2183
|
is_error: true,
|
|
2012
2184
|
});
|
|
2013
2185
|
}
|
|
2186
|
+
// (#386) — the operative input for this call: for tools whose advertised
|
|
2187
|
+
// schema carries a runtime-injected justification, the field is stripped
|
|
2188
|
+
// here once and every surface below that reasons about what the tool
|
|
2189
|
+
// DOES (rule matching, approval hashing, egress, execution) consumes
|
|
2190
|
+
// this; surfaces that record what the model DID keep `tu.input`. See the
|
|
2191
|
+
// `justificationInjectedTools` comment at loop start for the full split.
|
|
2192
|
+
let operativeInput = operativeToolInput(tu.name, tu.input);
|
|
2014
2193
|
const decisionDetails = evaluateWithReason({
|
|
2015
2194
|
toolName: tu.name,
|
|
2016
|
-
input:
|
|
2195
|
+
input: operativeInput,
|
|
2017
2196
|
readOnly: tool.readOnly,
|
|
2018
2197
|
destructive: tool.destructive,
|
|
2019
2198
|
requiresSandbox: tool.requiresSandbox,
|
|
@@ -2041,9 +2220,55 @@ export async function runChatLoop(opts) {
|
|
|
2041
2220
|
}
|
|
2042
2221
|
else if (approvals !== undefined && askMode === "pause") {
|
|
2043
2222
|
// G11 headless parking/resume. Consult the store for a prior decision
|
|
2044
|
-
// on this exact (toolName, inputHash).
|
|
2045
|
-
|
|
2046
|
-
|
|
2223
|
+
// on this exact (toolName, inputHash). The hash keys on the OPERATIVE
|
|
2224
|
+
// input (#386): a runtime-injected justification is re-worded by the
|
|
2225
|
+
// model on every regeneration, so hashing it would make a granted
|
|
2226
|
+
// approval unconsumable — the rerun would park a fresh approval
|
|
2227
|
+
// forever. The persisted record still carries the full `tu.input`
|
|
2228
|
+
// below so the approving operator SEES the justification.
|
|
2229
|
+
const inputHash = hashApprovalInput(tu.name, operativeInput);
|
|
2230
|
+
let existing = await approvals.store.get(tu.name, inputHash);
|
|
2231
|
+
// #400 — THE RESUMED CALL IS NOT THE PARKED CALL.
|
|
2232
|
+
//
|
|
2233
|
+
// A grant is keyed on whole-input equality, which silently assumes the
|
|
2234
|
+
// resumed run re-issues a byte-identical tool call. It does not: resume
|
|
2235
|
+
// re-drives the turn, the model regenerates the call, and
|
|
2236
|
+
// `sanitizeOrphanToolUses` has stripped the parked `tool_use` from the
|
|
2237
|
+
// replayed transcript — so the model is generating it FRESH and may
|
|
2238
|
+
// word any field differently. One re-worded argument moves the hash,
|
|
2239
|
+
// `get` misses, and the run parks AGAIN under a new id. Approve →
|
|
2240
|
+
// regenerate → re-park, forever: a plain one-shot grant could never
|
|
2241
|
+
// satisfy a model-driven tool, leaving `--always` (a STANDING allow on
|
|
2242
|
+
// a quota-spending or irreversible tool) as the only thing that landed
|
|
2243
|
+
// a single call.
|
|
2244
|
+
//
|
|
2245
|
+
// So when the exact key misses, look for the grant the operator
|
|
2246
|
+
// actually made — same session, same tool, still unspent — and run THE
|
|
2247
|
+
// INPUT THEY APPROVED rather than the one the model just regenerated.
|
|
2248
|
+
// Substituting is what keeps the security property intact: the
|
|
2249
|
+
// operator authorized a specific action shown to them on the approval
|
|
2250
|
+
// card, and that action is what executes. Honouring the grant for the
|
|
2251
|
+
// regenerated input instead would let a re-worded `place_call` dial a
|
|
2252
|
+
// different number under an approval for the first.
|
|
2253
|
+
if (existing === null) {
|
|
2254
|
+
const replay = await findReplayableGrant(approvals.store, {
|
|
2255
|
+
toolName: tu.name,
|
|
2256
|
+
sessionId,
|
|
2257
|
+
runId: bus.runId,
|
|
2258
|
+
now: Date.now(),
|
|
2259
|
+
});
|
|
2260
|
+
// CLAIM IT SYNCHRONOUSLY. The lookup reads and the consuming
|
|
2261
|
+
// `persist` writes, and up to `maxConcurrentTools` ask-gated calls
|
|
2262
|
+
// run through this gate at once — so without a claim two parallel
|
|
2263
|
+
// calls both find the same unconsumed grant and both execute. The
|
|
2264
|
+
// set is per-run and in-process, which is where that race is;
|
|
2265
|
+
// across processes the store's `consumedAt` remains the authority.
|
|
2266
|
+
if (replay !== undefined && !replayedApprovalIds.has(replay.id)) {
|
|
2267
|
+
replayedApprovalIds.add(replay.id);
|
|
2268
|
+
replayedFrom = replay;
|
|
2269
|
+
existing = replay;
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2047
2272
|
if (existing?.decision === "grant") {
|
|
2048
2273
|
// One-shot allow: consume the grant so a later identical call
|
|
2049
2274
|
// re-asks. A STANDING grant (#383, `existing.always`) is consumed
|
|
@@ -2054,7 +2279,86 @@ export async function runChatLoop(opts) {
|
|
|
2054
2279
|
// revokes the standing allow (no unconsumed record lingers to
|
|
2055
2280
|
// pre-approve the parked input until the TTL sweep).
|
|
2056
2281
|
approved = true;
|
|
2057
|
-
|
|
2282
|
+
if (replayedFrom !== undefined) {
|
|
2283
|
+
// Run WHAT WAS APPROVED. `tu.input` is the model's regenerated
|
|
2284
|
+
// wording; the record carries the input the operator saw and said
|
|
2285
|
+
// yes to, and that is the one with an authorization behind it.
|
|
2286
|
+
const approvedInput = replayedFrom.input;
|
|
2287
|
+
const approvedOperative = operativeToolInput(tu.name, approvedInput);
|
|
2288
|
+
// Re-evaluate POLICY against what will actually run. The approved
|
|
2289
|
+
// input passed these rules in the run that parked it, but the
|
|
2290
|
+
// rules are read fresh each loop and an operator may have tightened
|
|
2291
|
+
// them since — and a grant is permission from a human, not an
|
|
2292
|
+
// exemption from the spec. A rule that now DENIES wins.
|
|
2293
|
+
const recheck = evaluateWithReason({
|
|
2294
|
+
toolName: tu.name,
|
|
2295
|
+
input: approvedOperative,
|
|
2296
|
+
readOnly: tool.readOnly,
|
|
2297
|
+
destructive: tool.destructive,
|
|
2298
|
+
requiresSandbox: tool.requiresSandbox,
|
|
2299
|
+
}, permissionMode, permissionRules, { sandboxAvailable: opts.sandboxAvailable === true });
|
|
2300
|
+
if (recheck.decision === "deny") {
|
|
2301
|
+
approved = false;
|
|
2302
|
+
denialMessage =
|
|
2303
|
+
`tool denied: approval ${replayedFrom.id} was granted, but \`${tu.name}\` is now denied by permission policy` +
|
|
2304
|
+
`${recheck.reason !== undefined ? ` (${recheck.reason})` : ""}.`;
|
|
2305
|
+
// The grant is NOT spent on a call that never ran, and the
|
|
2306
|
+
// result must not claim an approved input executed.
|
|
2307
|
+
replayedFrom = undefined;
|
|
2308
|
+
}
|
|
2309
|
+
else {
|
|
2310
|
+
tu = { ...tu, input: approvedInput };
|
|
2311
|
+
operativeInput = approvedOperative;
|
|
2312
|
+
// The `pre-tool` hook already judged the input the MODEL sent,
|
|
2313
|
+
// several steps above. A hook that can deny must judge what will
|
|
2314
|
+
// actually RUN, so re-fire it against the substituted input.
|
|
2315
|
+
const replayHook = await fireHook("pre-tool", {
|
|
2316
|
+
id: tu.id,
|
|
2317
|
+
name: tu.name,
|
|
2318
|
+
input: tu.input,
|
|
2319
|
+
});
|
|
2320
|
+
if (!replayHook.allowed) {
|
|
2321
|
+
approved = false;
|
|
2322
|
+
denialMessage = `[blocked by hook] ${replayHook.reason ?? "denied"}`;
|
|
2323
|
+
replayedFrom = undefined;
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
if (approved) {
|
|
2328
|
+
// Consume only once the call is really going to run: a replay the
|
|
2329
|
+
// policy or a hook then refused must leave the operator's grant
|
|
2330
|
+
// unspent, or they have to approve the same action twice.
|
|
2331
|
+
await approvals.store.persist({
|
|
2332
|
+
...existing,
|
|
2333
|
+
consumedAt: new Date().toISOString(),
|
|
2334
|
+
});
|
|
2335
|
+
}
|
|
2336
|
+
if (replayedFrom !== undefined && approved) {
|
|
2337
|
+
bus.publish({
|
|
2338
|
+
...bus.envelope(),
|
|
2339
|
+
kind: "permission_decision",
|
|
2340
|
+
toolName: tu.name,
|
|
2341
|
+
decision: "allow",
|
|
2342
|
+
mode: permissionMode,
|
|
2343
|
+
reason: `approval ${replayedFrom.id} replayed: executing the APPROVED input, not the regenerated one`,
|
|
2344
|
+
});
|
|
2345
|
+
runContext.logger.info("tool approval replayed", {
|
|
2346
|
+
approvalId: replayedFrom.id,
|
|
2347
|
+
toolName: tu.name,
|
|
2348
|
+
detail: "the resumed turn regenerated a different input; the operator-approved input was executed instead",
|
|
2349
|
+
});
|
|
2350
|
+
// The durable `tool_use` record holds what the MODEL asked for —
|
|
2351
|
+
// correct, that is what the model did — but nothing yet recorded
|
|
2352
|
+
// what RAN, and an audit trail showing only the discarded input is
|
|
2353
|
+
// worse than one showing both.
|
|
2354
|
+
await logEvent("tool_use", {
|
|
2355
|
+
id: tu.id,
|
|
2356
|
+
name: tu.name,
|
|
2357
|
+
input: replayedFrom.input,
|
|
2358
|
+
approvalReplayedFrom: replayedFrom.id,
|
|
2359
|
+
requestedInput: tuAsRequested.input,
|
|
2360
|
+
});
|
|
2361
|
+
}
|
|
2058
2362
|
}
|
|
2059
2363
|
else if (existing?.decision === "deny") {
|
|
2060
2364
|
approved = false;
|
|
@@ -2221,7 +2525,7 @@ export async function runChatLoop(opts) {
|
|
|
2221
2525
|
return finish({
|
|
2222
2526
|
type: "tool_result",
|
|
2223
2527
|
tool_use_id: tu.id,
|
|
2224
|
-
content: `[justification denied] ${verdict.reason}. Reformulate the call with a justification that ties to the session's declared goal, or omit this tool from the call.`,
|
|
2528
|
+
content: `[justification denied] ${verdict.reason}. Reformulate the call with a \`justification\` string field in the tool input that ties to the session's declared goal, or omit this tool from the call.`,
|
|
2225
2529
|
is_error: true,
|
|
2226
2530
|
});
|
|
2227
2531
|
}
|
|
@@ -2238,7 +2542,9 @@ export async function runChatLoop(opts) {
|
|
|
2238
2542
|
// `"external-configured"` (warn). Callers can override via
|
|
2239
2543
|
// `opts.resolveSinkScope` to mark federation-joined or other dynamic sinks.
|
|
2240
2544
|
if (tool.scope === "external") {
|
|
2241
|
-
|
|
2545
|
+
// (#386) — scan the OPERATIVE payload: for injected-justification
|
|
2546
|
+
// tools this is what `executeTool` actually transmits to the sink.
|
|
2547
|
+
const payload = JSON.stringify(operativeInput ?? null);
|
|
2242
2548
|
const sinkScope = (opts.resolveSinkScope ?? defaultSinkScope)(tu.name);
|
|
2243
2549
|
const egress = await classifyEgress(payload, runContext, {
|
|
2244
2550
|
sinkId: tu.name,
|
|
@@ -2385,7 +2691,7 @@ export async function runChatLoop(opts) {
|
|
|
2385
2691
|
// every gate (permission/justification/egress) so it never overlaps the
|
|
2386
2692
|
// approval prompt, and torn down in `.finally` on success or throw.
|
|
2387
2693
|
beginToolWork(tu.name);
|
|
2388
|
-
const raw = await executeTool(tool,
|
|
2694
|
+
const raw = await executeTool(tool, operativeInput, {
|
|
2389
2695
|
toolUseId: tu.id,
|
|
2390
2696
|
signal: toolAbort.signal,
|
|
2391
2697
|
// #160 follow-up — the bridge (built on every run, above) carries
|
|
@@ -3105,7 +3411,15 @@ export async function runChatLoop(opts) {
|
|
|
3105
3411
|
// Item 26 — accrue this turn's tool_use count (next-turn tier signal).
|
|
3106
3412
|
thisTurnToolUseCount += toolUses.length;
|
|
3107
3413
|
for (const tu of toolUses) {
|
|
3108
|
-
|
|
3414
|
+
// (#386) — loop detection compares OPERATIVE inputs: a
|
|
3415
|
+
// runtime-injected justification is re-worded every call, so
|
|
3416
|
+
// recording it would make identical gated calls hash apart
|
|
3417
|
+
// and hide exactly the loops the detector exists to catch.
|
|
3418
|
+
toolUseHistory.push({
|
|
3419
|
+
id: tu.id,
|
|
3420
|
+
name: tu.name,
|
|
3421
|
+
input: operativeToolInput(tu.name, tu.input),
|
|
3422
|
+
});
|
|
3109
3423
|
}
|
|
3110
3424
|
messages.push({ role: "user", content: [...toolResults] });
|
|
3111
3425
|
await logEvent("user_message", { content: [...toolResults] });
|
|
@@ -3250,8 +3564,13 @@ export async function runChatLoop(opts) {
|
|
|
3250
3564
|
name: t.name,
|
|
3251
3565
|
input: t.input,
|
|
3252
3566
|
}));
|
|
3253
|
-
|
|
3254
|
-
|
|
3567
|
+
// (#386) — history records the OPERATIVE input (injected
|
|
3568
|
+
// justification stripped) so re-worded justifications don't
|
|
3569
|
+
// hide loops; `tsmBlocks` itself stays full-input because it
|
|
3570
|
+
// flows on to `executeOneToolUse`, whose gate reads the field.
|
|
3571
|
+
for (const tu of tsmBlocks) {
|
|
3572
|
+
toolUseHistory.push({ ...tu, input: operativeToolInput(tu.name, tu.input) });
|
|
3573
|
+
}
|
|
3255
3574
|
// Item 7 — the response fully drained (usage known) before these
|
|
3256
3575
|
// calls dispatch in `NeedTools`, so the split IS computable here:
|
|
3257
3576
|
// split the priceable response cost evenly across its tool_use
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewhaus/runtime-core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Minimal runtime imported by generated harness bundles",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -16,45 +16,45 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@anthropic-ai/sdk": "^0.96.0",
|
|
19
|
-
"@crewhaus/abort-controller": "0.5.
|
|
20
|
-
"@crewhaus/adapter-anthropic": "0.5.
|
|
21
|
-
"@crewhaus/agent-context-isolation": "0.5.
|
|
22
|
-
"@crewhaus/boundary-classifier": "0.5.
|
|
23
|
-
"@crewhaus/circuit-breaker": "0.5.
|
|
24
|
-
"@crewhaus/compaction-autocompact": "0.5.
|
|
25
|
-
"@crewhaus/compaction-curator": "0.5.
|
|
26
|
-
"@crewhaus/compaction-snip": "0.5.
|
|
27
|
-
"@crewhaus/cost-tracker": "0.5.
|
|
28
|
-
"@crewhaus/egress-classifier": "0.5.
|
|
29
|
-
"@crewhaus/errors": "0.5.
|
|
30
|
-
"@crewhaus/event-log": "0.5.
|
|
31
|
-
"@crewhaus/hooks-engine": "0.5.
|
|
32
|
-
"@crewhaus/model-router": "0.5.
|
|
33
|
-
"@crewhaus/permission-engine": "0.5.
|
|
34
|
-
"@crewhaus/prompt-cache-manager": "0.5.
|
|
35
|
-
"@crewhaus/prompt-injection-detector": "0.5.
|
|
36
|
-
"@crewhaus/rate-limiter": "0.5.
|
|
37
|
-
"@crewhaus/recovery-engine": "0.5.
|
|
38
|
-
"@crewhaus/routing-store": "0.5.
|
|
39
|
-
"@crewhaus/run-context": "0.5.
|
|
40
|
-
"@crewhaus/session-store": "0.5.
|
|
41
|
-
"@crewhaus/skills-registry": "0.5.
|
|
42
|
-
"@crewhaus/slash-commands": "0.5.
|
|
43
|
-
"@crewhaus/state-store": "0.5.
|
|
44
|
-
"@crewhaus/tenancy": "0.5.
|
|
45
|
-
"@crewhaus/token-budget": "0.5.
|
|
46
|
-
"@crewhaus/tool-catalog": "0.5.
|
|
47
|
-
"@crewhaus/tool-executor": "0.5.
|
|
48
|
-
"@crewhaus/tool-loop-detection": "0.5.
|
|
49
|
-
"@crewhaus/tool-orchestrator": "0.5.
|
|
50
|
-
"@crewhaus/tool-result-store": "0.5.
|
|
51
|
-
"@crewhaus/streaming-tool-executor": "0.5.
|
|
52
|
-
"@crewhaus/turn-state-machine": "0.5.
|
|
53
|
-
"@crewhaus/trace-event-bus": "0.5.
|
|
54
|
-
"@crewhaus/worker-runtime": "0.5.
|
|
55
|
-
"@crewhaus/structured-event-printer": "0.5.
|
|
56
|
-
"@crewhaus/metrics-collector": "0.5.
|
|
57
|
-
"@crewhaus/otel-exporter": "0.5.
|
|
19
|
+
"@crewhaus/abort-controller": "0.5.4",
|
|
20
|
+
"@crewhaus/adapter-anthropic": "0.5.4",
|
|
21
|
+
"@crewhaus/agent-context-isolation": "0.5.4",
|
|
22
|
+
"@crewhaus/boundary-classifier": "0.5.4",
|
|
23
|
+
"@crewhaus/circuit-breaker": "0.5.4",
|
|
24
|
+
"@crewhaus/compaction-autocompact": "0.5.4",
|
|
25
|
+
"@crewhaus/compaction-curator": "0.5.4",
|
|
26
|
+
"@crewhaus/compaction-snip": "0.5.4",
|
|
27
|
+
"@crewhaus/cost-tracker": "0.5.4",
|
|
28
|
+
"@crewhaus/egress-classifier": "0.5.4",
|
|
29
|
+
"@crewhaus/errors": "0.5.4",
|
|
30
|
+
"@crewhaus/event-log": "0.5.4",
|
|
31
|
+
"@crewhaus/hooks-engine": "0.5.4",
|
|
32
|
+
"@crewhaus/model-router": "0.5.4",
|
|
33
|
+
"@crewhaus/permission-engine": "0.5.4",
|
|
34
|
+
"@crewhaus/prompt-cache-manager": "0.5.4",
|
|
35
|
+
"@crewhaus/prompt-injection-detector": "0.5.4",
|
|
36
|
+
"@crewhaus/rate-limiter": "0.5.4",
|
|
37
|
+
"@crewhaus/recovery-engine": "0.5.4",
|
|
38
|
+
"@crewhaus/routing-store": "0.5.4",
|
|
39
|
+
"@crewhaus/run-context": "0.5.4",
|
|
40
|
+
"@crewhaus/session-store": "0.5.4",
|
|
41
|
+
"@crewhaus/skills-registry": "0.5.4",
|
|
42
|
+
"@crewhaus/slash-commands": "0.5.4",
|
|
43
|
+
"@crewhaus/state-store": "0.5.4",
|
|
44
|
+
"@crewhaus/tenancy": "0.5.4",
|
|
45
|
+
"@crewhaus/token-budget": "0.5.4",
|
|
46
|
+
"@crewhaus/tool-catalog": "0.5.4",
|
|
47
|
+
"@crewhaus/tool-executor": "0.5.4",
|
|
48
|
+
"@crewhaus/tool-loop-detection": "0.5.4",
|
|
49
|
+
"@crewhaus/tool-orchestrator": "0.5.4",
|
|
50
|
+
"@crewhaus/tool-result-store": "0.5.4",
|
|
51
|
+
"@crewhaus/streaming-tool-executor": "0.5.4",
|
|
52
|
+
"@crewhaus/turn-state-machine": "0.5.4",
|
|
53
|
+
"@crewhaus/trace-event-bus": "0.5.4",
|
|
54
|
+
"@crewhaus/worker-runtime": "0.5.4",
|
|
55
|
+
"@crewhaus/structured-event-printer": "0.5.4",
|
|
56
|
+
"@crewhaus/metrics-collector": "0.5.4",
|
|
57
|
+
"@crewhaus/otel-exporter": "0.5.4",
|
|
58
58
|
"zod-to-json-schema": "^3.23.5"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|