@sema-agent/core 7.0.2 → 7.1.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/agents/repair-loop.d.ts +8 -7
  3. package/dist/agents/roster-store.d.ts +7 -2
  4. package/dist/brain/errors.d.ts +18 -0
  5. package/dist/brain/errors.js +3 -0
  6. package/dist/brain/stream-engine.js +6 -4
  7. package/dist/core/context-edit.d.ts +3 -0
  8. package/dist/core/governance-codes.d.ts +1 -1
  9. package/dist/core/governance-codes.js +4 -0
  10. package/dist/core/hooks.d.ts +26 -6
  11. package/dist/core/hooks.js +8 -5
  12. package/dist/core/image-downsample.d.ts +4 -3
  13. package/dist/core/roles.d.ts +30 -8
  14. package/dist/core/roles.js +12 -8
  15. package/dist/core/runner/prepare-task.d.ts +2 -1
  16. package/dist/core/runner/prepare-task.js +75 -39
  17. package/dist/core/runner/runtask.d.ts +12 -3
  18. package/dist/core/runner/runtask.js +32 -4
  19. package/dist/core/safety-axis-vocab.d.ts +1 -1
  20. package/dist/core/strategy-store.d.ts +4 -1
  21. package/dist/core/task-registry-shared.d.ts +7 -3
  22. package/dist/core/tool-errors.d.ts +1 -1
  23. package/dist/core/tool-policy.d.ts +40 -7
  24. package/dist/core/tool-policy.js +63 -9
  25. package/dist/core/types.d.ts +87 -9
  26. package/dist/engine/compaction/compaction.js +6 -2
  27. package/dist/engine/harness/agent-harness.d.ts +28 -6
  28. package/dist/engine/harness/agent-harness.js +34 -2
  29. package/dist/engine/harness/messages.js +4 -0
  30. package/dist/engine/harness/types.d.ts +37 -0
  31. package/dist/engine/harness/types.js +5 -0
  32. package/dist/engine/session/session.js +3 -2
  33. package/dist/internal/harness.d.ts +1 -0
  34. package/dist/internal/harness.js +1 -0
  35. package/dist/orchestration/builtin-workflows.d.ts +17 -9
  36. package/dist/orchestration/run-workflow-tool.js +7 -2
  37. package/dist/orchestration/workflow-governance.js +1 -1
  38. package/dist/orchestration/workflow-types.d.ts +1 -0
  39. package/dist/orchestration/workflow.js +1 -1
  40. package/dist/stores/file/mailbox-store.d.ts +2 -1
  41. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  import { createHash, randomBytes } from "node:crypto";
2
2
  import { realpathSync } from "node:fs";
3
3
  import { resolve as resolveFsPath } from "node:path";
4
- import { AgentHarness, DEFAULT_CHARS_PER_TOKEN, DEFAULT_CLAMP_TOLERANCE, DEFAULT_COMPACTION_SETTINGS, estimateContextTokens, readCompactionActiveTools, summaryOutputBudgetTokens } from "../../internal/harness.js";
4
+ import { AgentHarness, DEFAULT_CHARS_PER_TOKEN, DEFAULT_CLAMP_TOLERANCE, DEFAULT_COMPACTION_SETTINGS, estimateContextTokens, isSyntheticApiErrorMessage, readCompactionActiveTools, summaryOutputBudgetTokens } from "../../internal/harness.js";
5
5
  const PROMPT_HASH_SALT = randomBytes(16);
6
6
  import { sanitizeCompactionSettings } from "../auto-compaction.js";
7
7
  import { projectStaleToolResults, resolveStaleToolResultOffload } from "./compaction-call-options.js";
@@ -662,6 +662,57 @@ export function batchContextAt(messages, currentId) {
662
662
  const completedCallIds = batch.filter((id) => id !== currentId && resolved.has(id));
663
663
  return { batchToolCallIds: batch, completedCallIds };
664
664
  }
665
+ function composeCallSignal(runSignal, callSignal) {
666
+ return callSignal !== undefined ? AbortSignal.any([runSignal, callSignal]) : runSignal;
667
+ }
668
+ function lateAskSettlementObserver(args) {
669
+ return (late) => {
670
+ if (late.kind === "approve") {
671
+ deliverEngineNotice(args.onNotice, {
672
+ code: "task.late_approval",
673
+ message: `an approval for "${args.toolName}" was not consumed: a run or turn interrupt released the ask wait, ` +
674
+ `so the tool did NOT run and the approval was not honored (an unconsumed answer is the approver ` +
675
+ `releasing its wait, never a verdict).`,
676
+ detail: {
677
+ toolName: args.toolName,
678
+ toolCallId: args.toolCallId,
679
+ sessionId: args.sessionId,
680
+ runId: args.runId,
681
+ ...(args.taskId !== undefined ? { taskId: args.taskId } : {}),
682
+ },
683
+ });
684
+ return;
685
+ }
686
+ args.onError?.(late.error, { phase: "hook", sessionId: args.sessionId });
687
+ };
688
+ }
689
+ function askGrantShapeOf(args) {
690
+ try {
691
+ return JSON.stringify(args ?? null);
692
+ }
693
+ catch {
694
+ return undefined;
695
+ }
696
+ }
697
+ function consumeInheritedAskGrant(grants, onAsk, humanReviewRef, decision, req) {
698
+ if (decision.decisionReason !== undefined && decision.decisionReason !== "rule")
699
+ return undefined;
700
+ const grant = grants.get(req.toolCallId);
701
+ if (grant === undefined || askApproverIdentity(grant.approver) !== askApproverIdentity(onAsk) || grant.argsJson !== askGrantShapeOf(req.args))
702
+ return undefined;
703
+ grants.delete(req.toolCallId);
704
+ humanReviewRef.count += 1;
705
+ humanReviewRef.totalWaitMs += grant.waitMs;
706
+ const toolArg = primaryActivityArg(req.args);
707
+ humanReviewRef.gates.push({
708
+ kind: "human",
709
+ waitMs: grant.waitMs,
710
+ decision: "allow",
711
+ toolName: req.toolName,
712
+ ...(toolArg !== undefined ? { toolArg } : {}),
713
+ });
714
+ return { action: "allow", presentedInput: grant.presented };
715
+ }
665
716
  function raceAbort(p, signal, onAbort) {
666
717
  if (signal.aborted)
667
718
  return Promise.resolve(onAbort());
@@ -1666,7 +1717,8 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
1666
1717
  ...(am.onBreakerOpen !== undefined ? { onBreakerOpen: am.onBreakerOpen } : {}),
1667
1718
  classify: async (input, signal) => {
1668
1719
  const ctx = await session.buildContext();
1669
- const known = ctx.messages.filter((m) => m.role === "user" || m.role === "assistant" || m.role === "toolResult");
1720
+ const known = ctx.messages.filter((m) => !isSyntheticApiErrorMessage(m) &&
1721
+ (m.role === "user" || m.role === "assistant" || m.role === "toolResult"));
1670
1722
  const userPrompt = renderAutoModeWindow(known, am.window) + renderAutoModeAction(input);
1671
1723
  const classifierAuth = await spec.getApiKeyAndHeaders?.(classifierModel);
1672
1724
  const response = await classifierRuntime.completeSimple(classifierModel, { systemPrompt: classifierSystemPrompt, messages: [{ role: "user", content: userPrompt, timestamp: Date.now() }] }, {
@@ -3487,14 +3539,6 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
3487
3539
  return true;
3488
3540
  };
3489
3541
  const inheritedAskGrants = new Map();
3490
- const askGrantShapeOf = (args) => {
3491
- try {
3492
- return JSON.stringify(args ?? null);
3493
- }
3494
- catch {
3495
- return undefined;
3496
- }
3497
- };
3498
3542
  const recordInheritedAskGrant = (toolCallId, approver, presented, waitMs) => {
3499
3543
  if (typeof approver !== "function")
3500
3544
  return;
@@ -4159,14 +4203,18 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4159
4203
  });
4160
4204
  }
4161
4205
  if (effectivePolicy || hooks?.preToolUse || egressTools.size > 0 || irreversibleTools.size > 0 || resourceSuspendEligible || platformSuspendArmed || spec.enablePlanMode === true) {
4206
+ const composedCallSignal = (callSignal) => composeCallSignal(abortController.signal, callSignal);
4162
4207
  const adjudicate = effectivePolicy
4163
- ? (req) => raceAbort(Promise.resolve(effectivePolicy.check({ ...req, budget: budgetSnapshot, ...(handsCwdRef !== undefined ? { cwd: handsCwdRef.current } : {}) }, abortController.signal)), abortController.signal, () => ({
4164
- action: "deny",
4165
- message: "policy check aborted (task timed out or cancelled)",
4166
- }))
4208
+ ? (req, callSignal) => {
4209
+ const signal = composedCallSignal(callSignal);
4210
+ return raceAbort(Promise.resolve(effectivePolicy.check({ ...req, budget: budgetSnapshot, ...(handsCwdRef !== undefined ? { cwd: handsCwdRef.current } : {}) }, signal)), signal, () => ({
4211
+ action: "deny",
4212
+ message: "policy check aborted (task timed out or cancelled)",
4213
+ }));
4214
+ }
4167
4215
  : undefined;
4168
4216
  const approvalPreviewOf = (toolName, args) => resolveApprovalPreview(tools, toolName, args);
4169
- const resolveAskBound = async (decision, req) => {
4217
+ const resolveAskBound = async (decision, req, callSignal) => {
4170
4218
  if (inheritedUnavailableAsks.delete(req.toolCallId)) {
4171
4219
  return {
4172
4220
  action: "deny",
@@ -4177,23 +4225,9 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4177
4225
  approverUnavailable: true,
4178
4226
  };
4179
4227
  }
4180
- if (decision.decisionReason === undefined || decision.decisionReason === "rule") {
4181
- const grant = inheritedAskGrants.get(req.toolCallId);
4182
- if (grant !== undefined && askApproverIdentity(grant.approver) === askApproverIdentity(onAsk) && grant.argsJson === askGrantShapeOf(req.args)) {
4183
- inheritedAskGrants.delete(req.toolCallId);
4184
- humanReviewRef.count += 1;
4185
- humanReviewRef.totalWaitMs += grant.waitMs;
4186
- const toolArg = primaryActivityArg(req.args);
4187
- humanReviewRef.gates.push({
4188
- kind: "human",
4189
- waitMs: grant.waitMs,
4190
- decision: "allow",
4191
- toolName: req.toolName,
4192
- ...(toolArg !== undefined ? { toolArg } : {}),
4193
- });
4194
- return { action: "allow", presentedInput: grant.presented };
4195
- }
4196
- }
4228
+ const grantReuse = consumeInheritedAskGrant(inheritedAskGrants, onAsk, humanReviewRef, decision, req);
4229
+ if (grantReuse !== undefined)
4230
+ return grantReuse;
4197
4231
  const t0 = now();
4198
4232
  const resolved = await resolveAsk({
4199
4233
  toolName: req.toolName,
@@ -4212,9 +4246,9 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4212
4246
  ...(decision.action === "ask" && decision.probeReason !== undefined ? { probeReason: decision.probeReason } : {}),
4213
4247
  ...(decision.action === "ask" && decision.probeCause !== undefined ? { probeCause: decision.probeCause } : {}),
4214
4248
  ...(decision.action === "ask" && decision.ruleEvidence !== undefined ? { ruleEvidence: decision.ruleEvidence } : {}),
4215
- }, onAsk, abortController.signal);
4249
+ }, onAsk, composedCallSignal(callSignal), lateAskSettlementObserver({ toolName: req.toolName, toolCallId: req.toolCallId, sessionId, runId, ...(spec.taskId !== undefined ? { taskId: spec.taskId } : {}), onNotice: deps.onNotice, onError: deps.onError }));
4216
4250
  const waitMs = Math.max(0, now() - t0);
4217
- if (resolved.approverUnavailable !== true) {
4251
+ if (resolved.approverUnavailable !== true && resolved.resolution !== "task_aborted") {
4218
4252
  humanReviewRef.count += 1;
4219
4253
  humanReviewRef.totalWaitMs += waitMs;
4220
4254
  const toolArg = primaryActivityArg(req.args);
@@ -4602,7 +4636,8 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4602
4636
  return true;
4603
4637
  }
4604
4638
  : undefined;
4605
- const resolveContentAsk = async (req) => {
4639
+ const resolveContentAsk = async (req, callSignal) => {
4640
+ const contentAskSignal = composedCallSignal(callSignal);
4606
4641
  if (!contentAskRoutable(req.toolCallId) || liveQuestionFace === undefined || mountedQuestionTool === undefined) {
4607
4642
  return { kind: "unavailable", parkDeclined: false };
4608
4643
  }
@@ -4633,7 +4668,7 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4633
4668
  if (contentAskBindings.size >= CONTENT_ASK_BINDING_CAP && !contentAskBindings.has(req.toolCallId)) {
4634
4669
  return { kind: "unavailable", parkDeclined: true };
4635
4670
  }
4636
- if (abortController.signal.aborted) {
4671
+ if (contentAskSignal.aborted) {
4637
4672
  return { kind: "delivery_failure", code: "question.aborted", presentedInput: retained };
4638
4673
  }
4639
4674
  try {
@@ -4644,8 +4679,8 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4644
4679
  sourceTaskId: sessionId,
4645
4680
  boundInputHash: boundInputHashOf(retained),
4646
4681
  deliveryId,
4647
- }, abortController.signal))();
4648
- const settlement = await raceAbort(facePromise.then((outcome) => ({ tag: "outcome", outcome }), (error) => ({ tag: "threw", error })), abortController.signal, () => ({ tag: "aborted" }));
4682
+ }, contentAskSignal))();
4683
+ const settlement = await raceAbort(facePromise.then((outcome) => ({ tag: "outcome", outcome }), (error) => ({ tag: "threw", error })), contentAskSignal, () => ({ tag: "aborted" }));
4649
4684
  if (settlement.tag === "aborted") {
4650
4685
  void facePromise.then((late) => {
4651
4686
  if (classifyQuestionOutcome(late).shape !== "answered")
@@ -4673,7 +4708,7 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4673
4708
  bindOutcome({ kind: "failed", error }, retainedQuestionsHash);
4674
4709
  return {
4675
4710
  kind: "delivery_failure",
4676
- code: settlement.tag === "aborted" || abortController.signal.aborted ? "question.aborted" : "question.human_channel_failed",
4711
+ code: settlement.tag === "aborted" || contentAskSignal.aborted ? "question.aborted" : "question.human_channel_failed",
4677
4712
  presentedInput: retained,
4678
4713
  };
4679
4714
  }
@@ -4956,6 +4991,7 @@ export async function prepareTask(spec, deps, sessions, resume, internals, runne
4956
4991
  result = await runToolGate({
4957
4992
  onNotifyError: (f) => emitTrace(deps.tracer, () => ({ kind: "observer.notify_failed", version: 1, taskId: hostTaskId, site: f.site, message: f.error.message, ts: Date.now() })),
4958
4993
  event: e,
4994
+ ...(e.signal !== undefined ? { callSignal: e.signal } : {}),
4959
4995
  identity: hookIdentity,
4960
4996
  reminderMark,
4961
4997
  preToolUse: ownGatePreToolUse,
@@ -147,6 +147,10 @@ export declare function awaitChargeWithSlowDisclosure<T>(charge: Promise<T>, onS
147
147
  * already hold would be a fabricated stall. The second pass is what makes the loop terminate.
148
148
  */
149
149
  export declare function raceUntilDeadline<T>(p: Promise<T>, deadline: number): Promise<T | typeof GOVERNANCE_READ_STALLED>;
150
+ /**
151
+ * A stateless task runner. Holds shared deps (the external brain, model catalog) and an
152
+ * in-memory session store so that passing a `sessionId` continues a prior conversation.
153
+ */
150
154
  export declare class Runner {
151
155
  private deps;
152
156
  readonly sessions: SessionStore;
@@ -223,9 +227,14 @@ export declare class Runner {
223
227
  /**
224
228
  * Hot-swap the model catalog (and optionally the tier bindings) without restarting the process or
225
229
  * rebuilding the Runner — the deployment seat that makes "switching models" a zero-restart
226
- * operation for catalogs the constructor froze (the constructor runs {@link expandTiers} once and
227
- * keeps a private expanded copy, so mutating a shared table after construction never took effect;
228
- * this verb is the sanctioned generation change).
230
+ * operation, and the ONLY sanctioned generation change.
231
+ *
232
+ * (Precisely: the constructor runs {@link expandTiers} once and keeps a private expanded copy **only
233
+ * when `RunnerDeps.tiers` is configured** — that is the arm where mutating the shared table after
234
+ * construction provably never took effect. A tiers-less deployment's Runner holds the caller's own
235
+ * `models` object BY REFERENCE, so mutating it after construction does leak through; that is an
236
+ * accident of the expansion being a no-op, not a contract, and this verb is still the supported way
237
+ * to change a generation — it is what validates, announces, and computes the pairing disclosure.)
229
238
  *
230
239
  * Semantics:
231
240
  * - **Atomic**: the candidate catalog is tier-expanded and validated FIRST (an illegal tier
@@ -4,7 +4,7 @@ import { mintSystemReminder, openSystemReminder } from "../reminder-mint.js";
4
4
  import { GLOBAL_USAGE_KEY, resolveUsageWindows, usageRetryAfterMs } from "../usage-window-store.js";
5
5
  import { deliverDelegationLifecycle, deliverEngineNotice, undrainedUserInputNotices } from "../types.js";
6
6
  import { planRejectionClears, resolveTriggerWindow } from "../context-edit.js";
7
- import { AgentHarness, DEFAULT_COMPACTION_SETTINGS, uuidv7 } from "../../internal/harness.js";
7
+ import { AgentHarness, DEFAULT_COMPACTION_SETTINGS, isSyntheticApiErrorMessage, uuidv7 } from "../../internal/harness.js";
8
8
  import { snapshotActorAssertion } from "../../internal/llm.js";
9
9
  import { CheckpointError, BINDING_CHECKPOINT_VERSION, checkpointVersionOf, F012_CHECKPOINT_VERSION, MAX_SUPPORTED_CHECKPOINT_VERSION, REAL_APPROVAL_CHECKPOINT_VERSION, FACE_CHECKPOINT_VERSION, remainingBudgetMicroUsd, readPendingSteerQueue, remainingTokens, LEGACY_PENDING_STEER_INPUT_ID, MAX_STEER_INPUT_ID_CHARS, validatePendingSteer, winnerFromOutcome, } from "../checkpoint-store.js";
10
10
  import { GIT_STATUS_ECHO_PREVIEW, branchCarriesVisiblePositiveGitFrame, newestEngineGitFrame, stripGitStatusUnits } from "./git-status-frame.js";
@@ -2166,8 +2166,21 @@ export class Runner {
2166
2166
  const apply = () => {
2167
2167
  const abortOwnedBeforeHalt = h.abortController.signal.aborted;
2168
2168
  const receipt = h.harness.halt();
2169
- if (receipt.accepted && !abortOwnedBeforeHalt)
2169
+ if (receipt.accepted && !abortOwnedBeforeHalt) {
2170
2170
  h.loop.userHalted = true;
2171
+ }
2172
+ else {
2173
+ deliverEngineNotice(this.deps.onNotice, {
2174
+ code: "task.halt_unconsumed",
2175
+ message: "a user halt arrived while the run was already ending for its own reason: nothing was cut or stopped " +
2176
+ "by it — the run's own ending stands, and the result will not carry haltedByUser for this halt.",
2177
+ detail: {
2178
+ sessionId: h.sessionId,
2179
+ runId: h.runId,
2180
+ ...(spec.taskId !== undefined ? { taskId: spec.taskId } : {}),
2181
+ },
2182
+ });
2183
+ }
2171
2184
  if (receipt.turnCut) {
2172
2185
  deliverEngineNotice(this.deps.onNotice, {
2173
2186
  code: "task.turn_interrupted",
@@ -2861,6 +2874,7 @@ export class Runner {
2861
2874
  ...(s.attempt !== undefined ? { attempt: s.attempt } : {}),
2862
2875
  ...(s.maxRetries !== undefined ? { maxRetries: s.maxRetries } : {}),
2863
2876
  ...(s.errClass !== undefined ? { errClass: s.errClass } : {}),
2877
+ ...(s.errorStatus !== undefined ? { errorStatus: s.errorStatus } : {}),
2864
2878
  ...ident(),
2865
2879
  };
2866
2880
  Object.freeze(frame);
@@ -4317,7 +4331,21 @@ export class Runner {
4317
4331
  model = resolved.model;
4318
4332
  thinking = resolved.thinking;
4319
4333
  }
4320
- catch {
4334
+ catch (roleErr) {
4335
+ if (cfg.role !== undefined) {
4336
+ let asked;
4337
+ try {
4338
+ asked = String(cfg.role);
4339
+ }
4340
+ catch {
4341
+ asked = `<unrenderable ${typeof cfg.role}>`;
4342
+ }
4343
+ try {
4344
+ this.deps.onError?.(new Error(`suggestNextPrompts.role ${JSON.stringify(asked.length > 80 ? `${asked.slice(0, 80)}…` : asked)} did not resolve to a model — the prompt-suggestion pass ran on the task's own model (${prepared.model.id}) instead. Configure that role in RunnerDeps.roles / TaskSpec.roles, or omit the field to use the "summarize" role.`, { cause: roleErr }), { phase: "suggestions", sessionId: prepared.sessionId });
4345
+ }
4346
+ catch {
4347
+ }
4348
+ }
4321
4349
  }
4322
4350
  if (!sameRouteIdentity(model, prepared.model)) {
4323
4351
  const verdict = await adjudicateDerivedRoute({ brain: this.deps.brain, model, getApiKeyAndHeaders: spec.getApiKeyAndHeaders });
@@ -4329,7 +4357,7 @@ export class Runner {
4329
4357
  }
4330
4358
  const pricing = this.deps.pricing?.[model.id] ?? modelCostToPricing(model.cost);
4331
4359
  const ctx = await prepared.session.buildContext();
4332
- const transcript = ctx.messages.slice(-SUGGESTIONS_TRANSCRIPT_MESSAGES);
4360
+ const transcript = ctx.messages.filter((m) => !isSyntheticApiErrorMessage(m)).slice(-SUGGESTIONS_TRANSCRIPT_MESSAGES);
4333
4361
  const out = await generatePromptSuggestions({ brain: this.deps.brain, model, pricing, thinking, getApiKeyAndHeaders: spec.getApiKeyAndHeaders, signal: ac.signal }, transcript, count);
4334
4362
  if (out.tokens > 0 || out.costMicroUsd > 0) {
4335
4363
  result.stats.suggestions = { tokens: out.tokens, costMicroUsd: out.costMicroUsd };
@@ -25,7 +25,7 @@ export declare const SAFETY_AXIS_VOCABULARY: {
25
25
  readonly safetyAxis: readonly ["egress", "irreversible", "shell"];
26
26
  /** `RiskDescriptor.severity` — ToolEmu-style tier (5 = most severe), the inbox triage key (checkpoint-store.ts:85). */
27
27
  readonly severity: readonly [1, 2, 3, 4, 5];
28
- /** `TaskSpec.shellGate` — deployment shell-command gate rank, `off` < `classify` < `always` (types.ts:632). */
28
+ /** `TaskSpec.shellGate` (declared in core/types.ts) — deployment shell-command gate rank, `off` < `classify` < `always`. */
29
29
  readonly shellGate: readonly ["off", "classify", "always"];
30
30
  /** `ToolPolicy` decision — per-tool adjudication; `deny` short-circuits the `deny > ask > allow` fold (tool-policy.ts). */
31
31
  readonly permissionDecision: readonly ["allow", "ask", "deny"];
@@ -28,7 +28,10 @@ export interface StoredStrategy {
28
28
  scope: string;
29
29
  /** ISO timestamp stored. */
30
30
  ts: string;
31
- /** Teacher model id, for future staleness handling. */
31
+ /** What the teacher leg was ADDRESSED as, for future staleness handling: the caller's word verbatim
32
+ * when the teacher was configured by string (a catalog key, tier word or CC alias), else the
33
+ * `Model.id` of the object it was configured with. NOT normalized to a served model id — two rows
34
+ * written by the same physical model under different spellings do not compare equal. */
32
35
  teacherModel?: string;
33
36
  /** Reserved for a future generalized signature (v3 semantic matching). */
34
37
  signature?: string;
@@ -874,9 +874,13 @@ export interface RegisterBackgroundAgentInput extends TaskAccess {
874
874
  * internals chain; equals parentSessionId at depth 1). Persisted on the handle, the durable row
875
875
  * and the roster so recovery faces enumerate the whole tree under the root without alias walks. */
876
876
  rootSessionId?: string;
877
- /** design/151 S3b — revival lookup keys (CC meta-sidecar shape): the RESOLVED model id and the
878
- * team name, persisted on the durable row so a tier-3 revival rebuilds the spec the way a fresh
879
- * named-teammate spawn would. Lookup keys only, never a serialized spec. */
877
+ /** design/151 S3b — revival lookup keys (CC meta-sidecar shape): the model RECORD KEY and the team
878
+ * name, persisted on the durable row so a tier-3 revival rebuilds the spec the way a fresh
879
+ * named-teammate spawn would. Lookup keys only, never a serialized spec. `model` is NOT the
880
+ * resolved model id: a spawn that named a model in WORDS records the caller's spelling verbatim
881
+ * (catalog key / tier word / CC alias) so the revival can re-resolve it against the catalog in
882
+ * force at WAKE time; only a spawn that carried a Model OBJECT records that object's id. Same
883
+ * value and same rule as the roster row's `model` column. */
880
884
  model?: string;
881
885
  teamName?: string;
882
886
  toolUseId?: string;
@@ -40,7 +40,7 @@ export declare function formatToolError(error: unknown): string;
40
40
  * D-G data contract; the WorkerReport "errorClass" small-slice). A terminal `errorCode` (1.37+) is a
41
41
  * **dotted namespace** (`limits.max_tokens_exceeded` / `limits.max_cost_exceeded` / `output.invalid` …)
42
42
  * so a consumer can prefix-match a whole CLASS — but every aggregator/consumer hand-rolls
43
- * `errorCode.startsWith("budget.")` (types.ts:707 documents this very pattern), which silently
43
+ * `errorCode.startsWith("budget.")` a prefix convention no declaration site records, which silently
44
44
  * mis-classifies any code that does NOT follow the convention and drifts as new prefixes are added.
45
45
  * This is the single shared folder.
46
46
  *
@@ -341,7 +341,8 @@ export declare function decisionText(d: PermissionResult): string | undefined;
341
341
  *
342
342
  * `check` may be async, which is also how **human-in-the-loop approval** works: a deployment can
343
343
  * hold the promise open until an operator approves/denies. `signal` fires when the task aborts
344
- * (timeout / max turns / cancel) honor it to release a pending approval instead of hanging (F4).
344
+ * (timeout / max turns / cancel) and, since design/384, when the asking turn is interrupted (a bare
345
+ * user halt / steer-now boundary cut) — honor it to release a pending approval instead of hanging (F4).
345
346
  * The Runner also races `check` against `signal` itself, so a policy that ignores it still cannot
346
347
  * hang the worker past the deadline; passing it through just lets you clean up the wait early.
347
348
  */
@@ -725,8 +726,9 @@ export declare function createApprovalPolicy(opts: {
725
726
  requireApproval: string[];
726
727
  /**
727
728
  * The approval decision (e.g. await an operator). Resolve true to allow, false to deny. `signal`
728
- * fires when the task aborts — race your wait against it (e.g. an OA approval callback) so a
729
- * never-answered request is released at the deadline rather than holding the worker.
729
+ * fires when the task aborts — and, since design/384, when the asking turn is interrupted race
730
+ * your wait against it (e.g. an OA approval callback) so a never-answered request is released at
731
+ * the deadline rather than holding the worker.
730
732
  */
731
733
  approve: (req: ToolCallRequest, signal?: AbortSignal) => boolean | Promise<boolean>;
732
734
  /** Always-denied tools. */
@@ -942,7 +944,11 @@ export interface AskDelegationProvenance {
942
944
  * never adjudication input — same posture as {@link AskRequest.sourceAgentName}. */
943
945
  readonly agentName?: string;
944
946
  }
945
- /** The structured context an `onAsk` approver receives for an `ask` decision (design/37). */
947
+ /** The structured context an `onAsk` approver receives for an `ask` decision (design/37).
948
+ * Lifecycle (design/384): the wait this request fronts can be released by the run's abort AND by a
949
+ * turn-level interrupt; after that release the engine no longer awaits the approver, so anything an
950
+ * approver retains off this object it must release itself, keyed on its `signal` argument's abort
951
+ * (see {@link OnAsk} for the full detached-settlement contract). */
946
952
  export interface AskRequest {
947
953
  toolName: string;
948
954
  /** #144: a persisted allow rule MATCHED this call but could not clear the ask (mandated — see
@@ -1212,7 +1218,16 @@ export interface AskRequest {
1212
1218
  * deterministically to `deny` with a model-readable reason. The safe default for stateless automation.
1213
1219
  * - `"allow"` — auto-approve every `ask` (e.g. a trusted batch run).
1214
1220
  * - a function — await an operator's decision (true=allow, false=deny). `signal` fires when the task
1215
- * aborts; race your wait against it so an unanswered ask is released at the deadline, not hung.
1221
+ * aborts AND (design/384, when the gate threaded a per-call signal) when the asking turn is
1222
+ * interrupted — a bare user halt or a steer-now boundary cut releases the ask exactly as the run's
1223
+ * own end does; race your wait against it so an unanswered ask is released at the deadline, not hung.
1224
+ * Since design/384 the engine no longer waits for you after the signal fires: `resolveAsk` races its
1225
+ * await against the signal and settles the gate as an abort-family deny on its own. Your still-pending
1226
+ * promise is DETACHED — a resolve the released wait never consumes is you releasing your wait, never
1227
+ * a verdict (an unconsumed approval is disclosed as a notice, not honored — no claim is made about
1228
+ * which settled first); an unconsumed reject is disclosed on the deployment's error face, never
1229
+ * silently swallowed. Cleanup of anything you hold for the wait (the request
1230
+ * payload, the signal, your own timers) is YOUR responsibility, keyed on the signal's abort.
1216
1231
  * G1 three-value: the function may also return `"unavailable"` — an affirmative "no operator
1217
1232
  * is reachable for THIS ask right now" (judged PER-ASK inside the callback, not at wire time). It is a
1218
1233
  * ROUTING verdict, not a decision: the gate re-routes the ask onto the durable park leg (same behavior
@@ -1371,7 +1386,10 @@ export declare function describeThrown(err: unknown): string;
1371
1386
  * - `"blanket_allow_refused"` — a blanket allow posture met a `requiresRealApproval` ask;
1372
1387
  * - `"approver_unavailable"` — the approver answered the ROUTING question "nobody reachable"
1373
1388
  * (the G1 marker's fail-closed carry — the gate may re-route it to a durable park instead);
1374
- * - `"task_aborted"` — the task's own signal ended the wait (pre-wait and mid-wait arms);
1389
+ * - `"task_aborted"` — the wait's abort signal ended it (pre-wait, mid-wait and race arms). The
1390
+ * signal is the run's own end AND, since design/384, any turn-level interrupt composed into the
1391
+ * wait (a bare user halt, a steer-now boundary cut): one abort family, one word — a consumer
1392
+ * that must tell the sources apart reads the run's own terminal facts, not this classification;
1375
1393
  * - `"presentation_failed"` — the args/edit could not be safely presented or adopted (unclonable);
1376
1394
  * - `"approver_error"` — the approver callback threw;
1377
1395
  * - `"approver_contract"` — the approver returned something outside the contract (non-boolean
@@ -1466,5 +1484,20 @@ export declare function carriesBidiControls(value: unknown, limits?: {
1466
1484
  * headless auto-deny — carry NO source on purpose: nobody was asked, so there is no wait for anyone to
1467
1485
  * have ended, and `decisionReason: "mode"` is already the honest word for what produced them.
1468
1486
  */
1469
- export declare function resolveAsk(req: AskRequest, onAsk: OnAsk | undefined, signal?: AbortSignal): Promise<ResolvedAsk>;
1487
+ export declare function resolveAsk(req: AskRequest, onAsk: OnAsk | undefined, signal?: AbortSignal,
1488
+ /** design/384 — observer for a DETACHED approver's settlement, consulted only after the race
1489
+ * arm released the wait on `signal`'s abort: `"approve"` = a value that reads as an approval
1490
+ * went unconsumed by the released wait (a person said yes to an action that will never run —
1491
+ * the caller turns this into its own disclosure, e.g. an engine notice; no arrival-order claim
1492
+ * is made); `"error"` = the detached promise
1493
+ * rejected, or its members threw on the post-release read (the caller's error face, never
1494
+ * silence). An unconsumed NON-approve resolve is the approver releasing its wait — no verdict,
1495
+ * no record, this observer is not consulted. Optional and advisory: absent, the detached
1496
+ * settlement is still swallow-guarded (no unhandled rejection), it just leaves no trace. */
1497
+ onLateSettlement?: (late: {
1498
+ kind: "approve";
1499
+ } | {
1500
+ kind: "error";
1501
+ error: unknown;
1502
+ }) => void): Promise<ResolvedAsk>;
1470
1503
  export {};
@@ -314,7 +314,7 @@ export function createApprovalPolicy(opts) {
314
314
  }
315
315
  if (need.has(toolName) || namespacedCoveringHit(needCovering, toolName)) {
316
316
  if (signal?.aborted) {
317
- return withCoreMintedResolution({ action: "deny", message: `approval aborted for "${req.toolName}" (task ended)`, settledBy: "aborted" }, "task_aborted", req);
317
+ return withCoreMintedResolution({ action: "deny", message: `approval aborted for "${req.toolName}" (run or turn interrupted)`, settledBy: "aborted" }, "task_aborted", req);
318
318
  }
319
319
  let ok;
320
320
  try {
@@ -336,7 +336,7 @@ export function createApprovalPolicy(opts) {
336
336
  }, "window_expired", req);
337
337
  }
338
338
  if (signal?.aborted) {
339
- return withCoreMintedResolution({ action: "deny", message: `approval aborted for "${req.toolName}" (task ended)`, settledBy: "aborted" }, "task_aborted", req);
339
+ return withCoreMintedResolution({ action: "deny", message: `approval aborted for "${req.toolName}" (run or turn interrupted)`, settledBy: "aborted" }, "task_aborted", req);
340
340
  }
341
341
  const okRaw = ok;
342
342
  if (okRaw === true)
@@ -1003,13 +1003,58 @@ export function carriesBidiControls(value, limits) {
1003
1003
  return false;
1004
1004
  }
1005
1005
  }
1006
- export async function resolveAsk(req, onAsk, signal) {
1007
- const r = await resolveAskArms(req, onAsk, signal);
1006
+ export async function resolveAsk(req, onAsk, signal, onLateSettlement) {
1007
+ const r = await resolveAskArms(req, onAsk, signal, onLateSettlement);
1008
1008
  if (r.action === "deny" && isAskDenyResolution(r.resolution))
1009
1009
  return withCoreMintedResolution(r, r.resolution, req);
1010
1010
  return r;
1011
1011
  }
1012
- async function resolveAskArms(req, onAsk, signal) {
1012
+ function raceAskWaitAgainstSignal(wait, signal) {
1013
+ const settledWait = wait.then((value) => ({ tag: "value", value }), (error) => ({ tag: "threw", error }));
1014
+ if (signal === undefined)
1015
+ return settledWait;
1016
+ if (signal.aborted)
1017
+ return Promise.resolve({ tag: "aborted" });
1018
+ return new Promise((resolve) => {
1019
+ let settled = false;
1020
+ const finish = (r) => {
1021
+ if (settled)
1022
+ return;
1023
+ settled = true;
1024
+ signal.removeEventListener("abort", onAbort);
1025
+ resolve(r);
1026
+ };
1027
+ const onAbort = () => finish({ tag: "aborted" });
1028
+ signal.addEventListener("abort", onAbort, { once: true });
1029
+ void settledWait.then(finish);
1030
+ });
1031
+ }
1032
+ function detachLateAskWait(wait, onLate) {
1033
+ const disclose = (late) => {
1034
+ try {
1035
+ onLate?.(late);
1036
+ }
1037
+ catch {
1038
+ }
1039
+ };
1040
+ void wait
1041
+ .then((late) => {
1042
+ let approved = false;
1043
+ try {
1044
+ approved = late === true || (typeof late === "object" && late !== null && late.allow === true);
1045
+ }
1046
+ catch (err) {
1047
+ disclose({ kind: "error", error: err });
1048
+ return;
1049
+ }
1050
+ if (approved)
1051
+ disclose({ kind: "approve" });
1052
+ }, (error) => {
1053
+ disclose({ kind: "error", error });
1054
+ })
1055
+ .catch(() => undefined);
1056
+ }
1057
+ async function resolveAskArms(req, onAsk, signal, onLateSettlement) {
1013
1058
  if (onAsk === "allow") {
1014
1059
  if (req.requiresRealApproval === true) {
1015
1060
  return {
@@ -1032,7 +1077,7 @@ async function resolveAskArms(req, onAsk, signal) {
1032
1077
  };
1033
1078
  }
1034
1079
  if (signal?.aborted) {
1035
- return { action: "deny", message: `approval aborted for "${req.toolName}" (task ended)`, decisionReason: "mode",
1080
+ return { action: "deny", message: `approval aborted for "${req.toolName}" (run or turn interrupted)`, decisionReason: "mode",
1036
1081
  resolution: "task_aborted", settledBy: "aborted" };
1037
1082
  }
1038
1083
  const presented = tryCloneArgs(req.args);
@@ -1059,12 +1104,21 @@ async function resolveAskArms(req, onAsk, signal) {
1059
1104
  }
1060
1105
  const bidi = carriesBidiControls(presented.value) || carriesBidiControls(req.preview);
1061
1106
  const { hasBidiControls: _carried, ...bare } = req;
1062
- ok = await onAsk({
1107
+ const wait = Promise.resolve(onAsk({
1063
1108
  ...bare,
1064
1109
  boundInputHash: boundInputHashOf(presented.value),
1065
1110
  args: approverView.value,
1066
1111
  ...(bidi ? { hasBidiControls: true } : {}),
1067
- }, signal);
1112
+ }, signal));
1113
+ const raced = await raceAskWaitAgainstSignal(wait, signal);
1114
+ if (raced.tag === "aborted") {
1115
+ detachLateAskWait(wait, onLateSettlement);
1116
+ return { action: "deny", message: `approval aborted for "${req.toolName}" (run or turn interrupted)`, decisionReason: "mode",
1117
+ resolution: "task_aborted", settledBy: "aborted" };
1118
+ }
1119
+ if (raced.tag === "threw")
1120
+ throw raced.error;
1121
+ ok = raced.value;
1068
1122
  }
1069
1123
  catch (err) {
1070
1124
  return {
@@ -1076,7 +1130,7 @@ async function resolveAskArms(req, onAsk, signal) {
1076
1130
  };
1077
1131
  }
1078
1132
  if (signal?.aborted) {
1079
- return { action: "deny", message: `approval aborted for "${req.toolName}" (task ended)`, decisionReason: "mode",
1133
+ return { action: "deny", message: `approval aborted for "${req.toolName}" (run or turn interrupted)`, decisionReason: "mode",
1080
1134
  resolution: "task_aborted", settledBy: "aborted" };
1081
1135
  }
1082
1136
  if (ok === "unavailable") {