@sema-agent/core 7.73.0 → 7.74.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 (177) hide show
  1. package/CHANGELOG.md +246 -0
  2. package/dist/agents/list-agents-tool.js +1 -1
  3. package/dist/agents/retain-ledger.js +3 -3
  4. package/dist/agents/send-message-tool.js +4 -4
  5. package/dist/brain/anthropic.d.ts +42 -1
  6. package/dist/brain/anthropic.js +54 -4
  7. package/dist/brain/failover.d.ts +5 -0
  8. package/dist/brain/failover.js +51 -22
  9. package/dist/brain/model-lane.d.ts +12 -3
  10. package/dist/brain/model-lane.js +5 -5
  11. package/dist/brain/open-responses.js +3 -3
  12. package/dist/brain/openai.js +1 -1
  13. package/dist/brain/route-adjudicator.d.ts +33 -4
  14. package/dist/brain/route-adjudicator.js +49 -12
  15. package/dist/brain/stream-engine.js +9 -7
  16. package/dist/brain/stream-shared.d.ts +4 -3
  17. package/dist/brain/stream-shared.js +2 -2
  18. package/dist/core/allow-entry.js +1 -1
  19. package/dist/core/ask-class.d.ts +2 -2
  20. package/dist/core/ask-headline.d.ts +1 -1
  21. package/dist/core/ask-origin.d.ts +44 -4
  22. package/dist/core/ask-origin.js +29 -10
  23. package/dist/core/background-agent-store.d.ts +1 -0
  24. package/dist/core/background-agent-store.js +49 -8
  25. package/dist/core/checkpoint-row-verdict.d.ts +1 -1
  26. package/dist/core/checkpoint-row-verdict.js +2 -2
  27. package/dist/core/checkpoint-store.d.ts +2 -0
  28. package/dist/core/checkpoint-store.js +8 -2
  29. package/dist/core/config-refusal-codes.d.ts +1 -1
  30. package/dist/core/config-refusal-codes.js +1 -0
  31. package/dist/core/cron.js +9 -1
  32. package/dist/core/deployment-shape.d.ts +7 -0
  33. package/dist/core/deployment-shape.js +19 -0
  34. package/dist/core/engine-capabilities.d.ts +9 -1
  35. package/dist/core/engine-capabilities.js +1 -1
  36. package/dist/core/engine-notice.d.ts +14 -9
  37. package/dist/core/engine-version.js +1 -1
  38. package/dist/core/gate-fold.js +5 -4
  39. package/dist/core/gate-lanes.js +8 -12
  40. package/dist/core/gate-outcome.d.ts +8 -2
  41. package/dist/core/gate-outcome.js +10 -3
  42. package/dist/core/governance-codes.d.ts +8 -2
  43. package/dist/core/governance-codes.js +9 -1
  44. package/dist/core/hook-condition.d.ts +37 -0
  45. package/dist/core/hook-condition.js +567 -0
  46. package/dist/core/hooks.d.ts +2 -0
  47. package/dist/core/mode-policy.d.ts +4 -0
  48. package/dist/core/mode-policy.js +18 -2
  49. package/dist/core/model-seat.d.ts +8 -5
  50. package/dist/core/model-shell.d.ts +129 -0
  51. package/dist/core/model-shell.js +249 -0
  52. package/dist/core/mods/kernel/digest.d.ts +4 -0
  53. package/dist/core/mods/kernel/digest.js +5 -0
  54. package/dist/core/mods/kernel/dispatch.d.ts +3 -0
  55. package/dist/core/mods/kernel/dispatch.js +15 -6
  56. package/dist/core/mods/kernel/events.d.ts +21 -5
  57. package/dist/core/mods/kernel/events.js +19 -10
  58. package/dist/core/mods/kernel/hash-mirror.d.ts +1 -0
  59. package/dist/core/mods/kernel/hash-mirror.js +55 -0
  60. package/dist/core/mods/kernel/index.d.ts +1 -0
  61. package/dist/core/mods/kernel/index.js +1 -0
  62. package/dist/core/mods/kernel/package-reader.d.ts +48 -0
  63. package/dist/core/mods/kernel/package-reader.js +149 -0
  64. package/dist/core/mods/kernel/site-rules.d.ts +4 -0
  65. package/dist/core/mods/kernel/site-rules.js +13 -0
  66. package/dist/core/mods/package.d.ts +7 -3
  67. package/dist/core/mods/package.js +47 -80
  68. package/dist/core/permission-rule-org.js +13 -3
  69. package/dist/core/permission-rules.js +4 -1
  70. package/dist/core/retain-seat.d.ts +42 -0
  71. package/dist/core/retain-seat.js +76 -0
  72. package/dist/core/rewind-refusal-codes.d.ts +8 -0
  73. package/dist/core/rewind-refusal-codes.js +12 -0
  74. package/dist/core/roles.d.ts +2 -2
  75. package/dist/core/roles.js +15 -1
  76. package/dist/core/rule-store-fault.d.ts +9 -7
  77. package/dist/core/rule-store-fault.js +8 -3
  78. package/dist/core/runner/checkpoint-scope.d.ts +1 -0
  79. package/dist/core/runner/checkpoint-scope.js +4 -2
  80. package/dist/core/runner/compact-session.js +1 -1
  81. package/dist/core/runner/construction-doors.js +2 -0
  82. package/dist/core/runner/cross-boundary-lane.d.ts +8 -1
  83. package/dist/core/runner/cross-boundary-lane.js +4 -0
  84. package/dist/core/runner/gate-exit.d.ts +7 -0
  85. package/dist/core/runner/gate-exit.js +2 -1
  86. package/dist/core/runner/model-lane-seat.d.ts +3 -0
  87. package/dist/core/runner/model-lane-seat.js +4 -0
  88. package/dist/core/runner/person-path-rows.d.ts +6 -0
  89. package/dist/core/runner/person-path-rows.js +13 -0
  90. package/dist/core/runner/prepare-config-doors.js +9 -5
  91. package/dist/core/runner/prepare-gate-stations.d.ts +6 -0
  92. package/dist/core/runner/prepare-gate-stations.js +3 -1
  93. package/dist/core/runner/prepare-hands-readface.js +3 -2
  94. package/dist/core/runner/prepare-offload-wrappers.js +1 -1
  95. package/dist/core/runner/prepare-task.js +6 -3
  96. package/dist/core/runner/prepare-wiring-manifest.js +3 -4
  97. package/dist/core/runner/resume-policy-outcome.js +5 -2
  98. package/dist/core/runner/role-seat-defaulted.d.ts +3 -3
  99. package/dist/core/runner/role-seat-defaulted.js +1 -1
  100. package/dist/core/runner/rule-store-unreadable-notice.d.ts +1 -1
  101. package/dist/core/runner/rule-store-unreadable-notice.js +3 -3
  102. package/dist/core/runner/run-harness-handlers.js +1 -1
  103. package/dist/core/runner/run-leg.js +2 -2
  104. package/dist/core/runner/shell-env-withheld.d.ts +3 -2
  105. package/dist/core/runner/shell-env-withheld.js +24 -1
  106. package/dist/core/runner/sub-agent-deny-lists.js +9 -5
  107. package/dist/core/runner/tool-end-body.d.ts +1 -1
  108. package/dist/core/runner/tool-not-executed.d.ts +27 -8
  109. package/dist/core/runner/tool-not-executed.js +22 -11
  110. package/dist/core/runner/write-protection-station.d.ts +1 -1
  111. package/dist/core/runner/write-protection-station.js +1 -1
  112. package/dist/core/runner-deps.d.ts +4 -0
  113. package/dist/core/secret-env.d.ts +8 -0
  114. package/dist/core/secret-env.js +5 -2
  115. package/dist/core/shell-read-silence.d.ts +40 -2
  116. package/dist/core/shell-read-silence.js +23 -4
  117. package/dist/core/task-event.d.ts +4 -4
  118. package/dist/core/task-notification.js +6 -1
  119. package/dist/core/task-registry-agent.d.ts +1 -0
  120. package/dist/core/task-registry-agent.js +1 -1
  121. package/dist/core/task-registry.d.ts +1 -0
  122. package/dist/core/task-registry.js +4 -3
  123. package/dist/core/task-result.d.ts +5 -3
  124. package/dist/core/task-spec-members.js +8 -0
  125. package/dist/core/tool-gate-phase.d.ts +6 -3
  126. package/dist/core/tool-gate-phase.js +1 -0
  127. package/dist/core/tool-mount-facts.d.ts +7 -3
  128. package/dist/core/tool-mount-facts.js +6 -2
  129. package/dist/core/tool-mount-plan.d.ts +1 -1
  130. package/dist/core/tool-mount-plan.js +4 -3
  131. package/dist/core/tool-result-store.d.ts +37 -1
  132. package/dist/core/tool-result-store.js +168 -23
  133. package/dist/core/trace.d.ts +3 -3
  134. package/dist/engine/execution-env/node-execution-env.d.ts +75 -1
  135. package/dist/engine/execution-env/node-execution-env.js +225 -13
  136. package/dist/engine/harness/types.d.ts +25 -0
  137. package/dist/engine/llm/types.d.ts +8 -0
  138. package/dist/engine/llm/validation.d.ts +8 -1
  139. package/dist/engine/llm/validation.js +31 -2
  140. package/dist/host-env.d.ts +10 -0
  141. package/dist/host-env.js +1 -0
  142. package/dist/index.d.ts +10 -2
  143. package/dist/index.js +9 -2
  144. package/dist/internal/harness-types.d.ts +1 -1
  145. package/dist/orchestration/goal.js +1 -1
  146. package/dist/orchestration/workflow-primitives.js +10 -1
  147. package/dist/orchestration/workflow-script-runner.d.ts +2 -2
  148. package/dist/orchestration/workflow.d.ts +1 -1
  149. package/dist/orchestration/workflow.js +2 -2
  150. package/dist/shell/lex.d.ts +2 -2
  151. package/dist/shell/lex.js +1 -1
  152. package/dist/tools/fs/bash-model-shell.d.ts +23 -0
  153. package/dist/tools/fs/bash-model-shell.js +25 -0
  154. package/dist/tools/fs/bash-readonly-classifier.d.ts +6 -0
  155. package/dist/tools/fs/bash-readonly-classifier.js +3 -0
  156. package/dist/tools/fs/fs-bash.js +13 -11
  157. package/dist/tools/fs/shell-read-argv.js +1 -0
  158. package/dist/tools/fs/shell-read-check.js +39 -9
  159. package/dist/tools/fs/shell-read-plan.d.ts +2 -3
  160. package/dist/tools/fs/shell-read-plan.js +8 -5
  161. package/dist/tools/fs/shell-read-word.d.ts +8 -0
  162. package/dist/tools/fs/shell-read-word.js +13 -4
  163. package/dist/tools/monitor.js +12 -8
  164. package/dist/tools/scheduler-tools.d.ts +1 -0
  165. package/dist/tools/scheduler-tools.js +5 -2
  166. package/package.json +3 -2
  167. package/test/export-surface.snapshot.json +62 -3
  168. package/wire-changes/answers/core-stores-sql@7.74.0.json +7 -0
  169. package/wire-changes/answers/core@7.74.0.json +68 -0
  170. package/wire-changes/answers/mods-kernel@7.74.0.json +21 -0
  171. package/wire-changes/answers/primitives@7.74.0.json +7 -0
  172. package/wire-changes/answers/wiki-gate@7.74.0.json +8 -0
  173. package/wire-changes/core-stores-sql@7.74.0.json +143 -0
  174. package/wire-changes/core@7.74.0.json +2210 -0
  175. package/wire-changes/mods-kernel@7.74.0.json +421 -0
  176. package/wire-changes/primitives@7.74.0.json +174 -0
  177. package/wire-changes/wiki-gate@7.74.0.json +145 -0
@@ -0,0 +1,567 @@
1
+ import { types } from "node:util";
2
+ import { shellWordsOf } from "../shell/shell-words.js";
3
+ import { ENGINE_TOOLS } from "./tool-catalog-entries.js";
4
+ const objectFreeze = Object.freeze;
5
+ const objectPrototype = Object.prototype;
6
+ const arrayPrototype = Array.prototype;
7
+ const objectGetPrototypeOf = Object.getPrototypeOf;
8
+ const objectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
9
+ const objectHasOwn = Object.hasOwn;
10
+ const objectKeys = Object.keys;
11
+ const reflectOwnKeys = Reflect.ownKeys;
12
+ const arrayIsArray = Array.isArray;
13
+ const numberIsInteger = Number.isInteger;
14
+ const isProxy = types.isProxy;
15
+ const callBind = Function.prototype.bind.bind(Function.prototype.call);
16
+ const strCharCodeAt = callBind(String.prototype.charCodeAt);
17
+ const strIndexOf = callBind(String.prototype.indexOf);
18
+ const strLastIndexOf = callBind(String.prototype.lastIndexOf);
19
+ const strIncludes = callBind(String.prototype.includes);
20
+ const strSlice = callBind(String.prototype.slice);
21
+ const strStartsWith = callBind(String.prototype.startsWith);
22
+ const strEndsWith = callBind(String.prototype.endsWith);
23
+ const strTrim = callBind(String.prototype.trim);
24
+ const strToLowerCase = callBind(String.prototype.toLowerCase);
25
+ const arrPush = callBind(Array.prototype.push);
26
+ const arrJoin = callBind(Array.prototype.join);
27
+ const setHas = callBind(Set.prototype.has);
28
+ const mapGet = callBind(Map.prototype.get);
29
+ function snapshotOwnKeys(proto) {
30
+ const raw = reflectOwnKeys(proto);
31
+ const copy = [];
32
+ for (let i = 0; i < raw.length; i++) {
33
+ copy[i] = raw[i];
34
+ }
35
+ return objectFreeze(copy);
36
+ }
37
+ const OBJECT_PROTO_KEYS = snapshotOwnKeys(objectPrototype);
38
+ const ARRAY_PROTO_KEYS = snapshotOwnKeys(arrayPrototype);
39
+ export const HOOK_CONDITION_EVENTS = objectFreeze([
40
+ "PreToolUse",
41
+ "PostToolUse",
42
+ "PostToolUseFailure",
43
+ "PermissionRequest",
44
+ "PermissionDenied",
45
+ ]);
46
+ export const HOOK_CONDITION_UNTESTED_REASONS = objectFreeze([
47
+ "not_a_tool_event",
48
+ "condition_unreadable",
49
+ "name_unjudged",
50
+ "content_unjudged",
51
+ "invalid_input",
52
+ ]);
53
+ function frozenUntested(reason, detail) {
54
+ return objectFreeze({ verdict: "untested", reason, detail });
55
+ }
56
+ const MATCHED = objectFreeze({ verdict: "matched" });
57
+ const INVALID_REALM_PROTOTYPES = frozenUntested("invalid_input", "the own-key lists of the realm's base prototypes differ from those at load");
58
+ const INVALID_UNREADABLE = frozenUntested("invalid_input", "the input is not plain data");
59
+ const INVALID_ENVELOPE_KEY = frozenUntested("invalid_input", "the input carries an unrecognized member");
60
+ const INVALID_EVENT = frozenUntested("invalid_input", "event must be a string");
61
+ const INVALID_TOOL_NAME = frozenUntested("invalid_input", "toolName must be a string");
62
+ const INVALID_TOOL_NAME_REQUIRED = frozenUntested("invalid_input", "toolName must be a non-empty string for tool events");
63
+ const INVALID_ENGINE_OWNED = frozenUntested("invalid_input", "engineOwned must be true when present");
64
+ const INVALID_TOOL_INPUT_DATA = frozenUntested("invalid_input", "toolInput is not plain data");
65
+ const INVALID_BASH_KEY = frozenUntested("invalid_input", "toolInput carries an unrecognized Bash parameter");
66
+ const INVALID_BASH_COMMAND = frozenUntested("invalid_input", "Bash toolInput.command must be a string");
67
+ const INVALID_BASH_TYPE = frozenUntested("invalid_input", "Bash toolInput carries a parameter of the wrong type");
68
+ const CONDITION_UNREADABLE = frozenUntested("condition_unreadable", "condition is not a readable unescaped string");
69
+ const NOT_A_TOOL_EVENT = frozenUntested("not_a_tool_event", "event is not one of the five tool hook events");
70
+ const NAME_MISMATCH = frozenUntested("name_unjudged", "condition tool name does not match call tool name");
71
+ const NAME_OUTSIDE_CATALOG = frozenUntested("name_unjudged", "tool name is outside the engine tool catalog");
72
+ const NAME_MONITOR = frozenUntested("name_unjudged", "Monitor hook conditions depend on outer routing context");
73
+ const CONTENT_REQUIRES_ENGINE_OWNED = frozenUntested("content_unjudged", "a condition with content requires engineOwned: true");
74
+ const CONTENT_NON_BASH = frozenUntested("content_unjudged", "content matching is only judged for Bash in v1");
75
+ const CONTENT_ABSENT_TOOL_INPUT = frozenUntested("content_unjudged", "toolInput is absent for Bash content condition");
76
+ const CONTENT_TIMEOUT_RANGE = frozenUntested("content_unjudged", "Bash toolInput.timeout is outside the judged integer range");
77
+ const CONTENT_CONFIG_DEPENDENT_KEY = frozenUntested("content_unjudged", "Bash toolInput carries run_in_background or dangerouslyDisableSandbox, whose schema membership depends on reference configuration");
78
+ const CONTENT_TOO_LONG = frozenUntested("content_unjudged", "condition content exceeds the 1000-character ceiling");
79
+ const CONTENT_NOT_FLAT = frozenUntested("content_unjudged", "Bash command is not strictly flat");
80
+ const CONTENT_NOT_MATCHED = frozenUntested("content_unjudged", "Bash command did not match the condition pattern");
81
+ const ENVELOPE_KEYS = objectFreeze(new Set(["condition", "event", "toolName", "toolInput", "engineOwned"]));
82
+ const TOOL_EVENT_SET = objectFreeze(new Set(HOOK_CONDITION_EVENTS));
83
+ const FORMER_NAMES = new Map([
84
+ ["Task", "Agent"],
85
+ ["KillShell", "TaskStop"],
86
+ ["KillBash", "TaskStop"],
87
+ ["Brief", "SendUserMessage"],
88
+ ]);
89
+ const CATALOG_NAMES = objectFreeze(new Set(ENGINE_TOOLS.map((e) => e.name)));
90
+ const CATALOG_ALIASES = new Map(ENGINE_TOOLS.flatMap((e) => (e.aliases ?? []).map((alias) => [alias, e.name])));
91
+ const BASH_INPUT_KEYS = new Map([
92
+ ["command", "string"],
93
+ ["timeout", "number"],
94
+ ["description", "string"],
95
+ ["run_in_background", "boolean"],
96
+ ["dangerouslyDisableSandbox", "boolean"],
97
+ ]);
98
+ const BASH_RESERVED_WORDS = objectFreeze(new Set(shellWordsOf("rule_reserved")));
99
+ const WRAPPER_WORDS = objectFreeze(new Set(["timeout", "env", "nice", "sudo", "xargs"]));
100
+ const MAX_COMMAND_CHARS = 10_000;
101
+ const MAX_CONTENT_CHARS = 1_000;
102
+ const MAX_TIMEOUT_MS = 600_000;
103
+ function protoKeysUnchanged(proto, expected) {
104
+ const current = reflectOwnKeys(proto);
105
+ if (current.length !== expected.length)
106
+ return false;
107
+ for (let i = 0; i < expected.length; i++) {
108
+ if (current[i] !== expected[i])
109
+ return false;
110
+ }
111
+ return true;
112
+ }
113
+ function admitPlainRecord(v) {
114
+ try {
115
+ if (typeof v === "function")
116
+ return false;
117
+ if (isProxy(v))
118
+ return false;
119
+ if (v === null || typeof v !== "object" || arrayIsArray(v))
120
+ return false;
121
+ const proto = objectGetPrototypeOf(v);
122
+ if (proto !== objectPrototype && proto !== null)
123
+ return false;
124
+ const keys = reflectOwnKeys(v);
125
+ for (let i = 0; i < keys.length; i++) {
126
+ const k = keys[i];
127
+ if (typeof k === "symbol")
128
+ return false;
129
+ const desc = objectGetOwnPropertyDescriptor(v, k);
130
+ if (desc === undefined || !objectHasOwn(desc, "value") || desc.enumerable !== true) {
131
+ return false;
132
+ }
133
+ }
134
+ return true;
135
+ }
136
+ catch {
137
+ return false;
138
+ }
139
+ }
140
+ function validateBashInputShape(toolInput) {
141
+ const inputKeys = objectKeys(toolInput);
142
+ for (let i = 0; i < inputKeys.length; i++) {
143
+ const k = inputKeys[i];
144
+ const expectedType = mapGet(BASH_INPUT_KEYS, k);
145
+ if (expectedType === undefined) {
146
+ return INVALID_BASH_KEY;
147
+ }
148
+ const val = toolInput[k];
149
+ if (k === "command") {
150
+ if (typeof val !== "string") {
151
+ return INVALID_BASH_COMMAND;
152
+ }
153
+ }
154
+ else if (val !== undefined && typeof val !== expectedType) {
155
+ return INVALID_BASH_TYPE;
156
+ }
157
+ }
158
+ if (!objectHasOwn(toolInput, "command") || typeof toolInput.command !== "string") {
159
+ return INVALID_BASH_COMMAND;
160
+ }
161
+ return undefined;
162
+ }
163
+ function splitCondition(s) {
164
+ const o = strIndexOf(s, "(");
165
+ const c = strLastIndexOf(s, ")");
166
+ if (o <= 0 || c <= o || c !== s.length - 1) {
167
+ return { name: s, content: undefined };
168
+ }
169
+ const name = strSlice(s, 0, o);
170
+ if (strIncludes(name, "(") || strIncludes(name, ")")) {
171
+ return { name: s, content: undefined };
172
+ }
173
+ const inner = strSlice(s, o + 1, c);
174
+ if (inner === "" || inner === "*") {
175
+ return { name, content: undefined };
176
+ }
177
+ return { name, content: inner };
178
+ }
179
+ function canonicalName(n) {
180
+ const afterFormer = mapGet(FORMER_NAMES, n) ?? n;
181
+ return mapGet(CATALOG_ALIASES, afterFormer) ?? afterFormer;
182
+ }
183
+ function isAllowedCommandChar(code) {
184
+ if (code >= 0x30 && code <= 0x39)
185
+ return true;
186
+ if (code >= 0x41 && code <= 0x5a)
187
+ return true;
188
+ if (code >= 0x61 && code <= 0x7a)
189
+ return true;
190
+ if (code === 0x20)
191
+ return true;
192
+ return (code === 0x5f ||
193
+ code === 0x2e ||
194
+ code === 0x2f ||
195
+ code === 0x3d ||
196
+ code === 0x3a ||
197
+ code === 0x2c ||
198
+ code === 0x40 ||
199
+ code === 0x25 ||
200
+ code === 0x2b ||
201
+ code === 0x2d);
202
+ }
203
+ function isAssignmentWord(word) {
204
+ const len = word.length;
205
+ if (len < 2)
206
+ return false;
207
+ const c0 = strCharCodeAt(word, 0);
208
+ const firstOk = (c0 >= 0x41 && c0 <= 0x5a) || (c0 >= 0x61 && c0 <= 0x7a) || c0 === 0x5f;
209
+ if (!firstOk)
210
+ return false;
211
+ let i = 1;
212
+ while (i < len) {
213
+ const c = strCharCodeAt(word, i);
214
+ if ((c >= 0x41 && c <= 0x5a) ||
215
+ (c >= 0x61 && c <= 0x7a) ||
216
+ (c >= 0x30 && c <= 0x39) ||
217
+ c === 0x5f) {
218
+ i++;
219
+ continue;
220
+ }
221
+ break;
222
+ }
223
+ if (i >= len)
224
+ return false;
225
+ const op = strCharCodeAt(word, i);
226
+ if (op === 0x3d)
227
+ return true;
228
+ if (op === 0x2b && i + 1 < len && strCharCodeAt(word, i + 1) === 0x3d)
229
+ return true;
230
+ return false;
231
+ }
232
+ function flatWords(command) {
233
+ const len = command.length;
234
+ if (len === 0 || len > MAX_COMMAND_CHARS)
235
+ return undefined;
236
+ if (strCharCodeAt(command, 0) === 0x20 || strCharCodeAt(command, len - 1) === 0x20) {
237
+ return undefined;
238
+ }
239
+ const words = [];
240
+ let wordStart = 0;
241
+ let prevCode = -1;
242
+ for (let i = 0; i < len; i++) {
243
+ const code = strCharCodeAt(command, i);
244
+ if (!isAllowedCommandChar(code))
245
+ return undefined;
246
+ if (code === 0x20) {
247
+ if (prevCode === 0x20)
248
+ return undefined;
249
+ arrPush(words, strSlice(command, wordStart, i));
250
+ wordStart = i + 1;
251
+ }
252
+ prevCode = code;
253
+ }
254
+ arrPush(words, strSlice(command, wordStart, len));
255
+ if (words.length === 0)
256
+ return undefined;
257
+ if (setHas(BASH_RESERVED_WORDS, words[0]))
258
+ return undefined;
259
+ for (let i = 0; i < words.length; i++) {
260
+ const w = words[i];
261
+ if (w.length === 0)
262
+ return undefined;
263
+ if (strCharCodeAt(w, 0) === 0x3d)
264
+ return undefined;
265
+ if (isAssignmentWord(w))
266
+ return undefined;
267
+ if (setHas(WRAPPER_WORDS, w))
268
+ return undefined;
269
+ }
270
+ return words;
271
+ }
272
+ function collapseBlanks(s) {
273
+ let out = "";
274
+ let inBlank = false;
275
+ for (let i = 0; i < s.length; i++) {
276
+ const code = strCharCodeAt(s, i);
277
+ if (code === 0x20 || code === 0x09) {
278
+ if (!inBlank) {
279
+ out += " ";
280
+ inBlank = true;
281
+ }
282
+ }
283
+ else {
284
+ out += strSlice(s, i, i + 1);
285
+ inBlank = false;
286
+ }
287
+ }
288
+ return out;
289
+ }
290
+ function colonStarPrefix(content) {
291
+ if (content.length < 3 || !strEndsWith(content, ":*"))
292
+ return undefined;
293
+ if (strIndexOf(content, "\n") >= 0 ||
294
+ strIndexOf(content, "\r") >= 0 ||
295
+ strIndexOf(content, "\u2028") >= 0 ||
296
+ strIndexOf(content, "\u2029") >= 0) {
297
+ return undefined;
298
+ }
299
+ return strSlice(content, 0, content.length - 2);
300
+ }
301
+ function referenceWildcardMatch(pattern, value, ignoreCase = false, foldBlanks = false) {
302
+ if (strIncludes(pattern, "\u0000"))
303
+ return false;
304
+ const l = strTrim(pattern);
305
+ const c0 = foldBlanks ? collapseBlanks(l) : l;
306
+ const g0 = foldBlanks ? collapseBlanks(value) : value;
307
+ const c = ignoreCase ? strToLowerCase(c0) : c0;
308
+ const g = ignoreCase ? strToLowerCase(g0) : g0;
309
+ const chars = [];
310
+ const isUnescapedStar = [];
311
+ let starCount = 0;
312
+ for (let i = 0; i < c.length; i++) {
313
+ const ch = strSlice(c, i, i + 1);
314
+ if (ch === "\\" && i + 1 < c.length) {
315
+ const next = strSlice(c, i + 1, i + 2);
316
+ if (next === "*") {
317
+ arrPush(chars, "*");
318
+ arrPush(isUnescapedStar, false);
319
+ i++;
320
+ continue;
321
+ }
322
+ if (next === "\\") {
323
+ arrPush(chars, "\\");
324
+ arrPush(isUnescapedStar, false);
325
+ i++;
326
+ continue;
327
+ }
328
+ }
329
+ arrPush(chars, ch);
330
+ const star = ch === "*";
331
+ arrPush(isUnescapedStar, star);
332
+ if (star)
333
+ starCount++;
334
+ }
335
+ const tokens = [];
336
+ let litBuf = "";
337
+ const flushLit = () => {
338
+ if (litBuf.length > 0) {
339
+ arrPush(tokens, { kind: "lit", text: litBuf });
340
+ litBuf = "";
341
+ }
342
+ };
343
+ let pos = 0;
344
+ while (pos < chars.length) {
345
+ if (chars[pos] === "/" && !isUnescapedStar[pos]) {
346
+ let p = pos + 1;
347
+ let groups = 0;
348
+ while (p + 2 < chars.length &&
349
+ isUnescapedStar[p] &&
350
+ isUnescapedStar[p + 1] &&
351
+ chars[p + 2] === "/" &&
352
+ !isUnescapedStar[p + 2]) {
353
+ groups++;
354
+ p += 3;
355
+ }
356
+ if (groups > 0) {
357
+ flushLit();
358
+ arrPush(tokens, { kind: "globstar" });
359
+ pos = p;
360
+ continue;
361
+ }
362
+ }
363
+ if (isUnescapedStar[pos]) {
364
+ flushLit();
365
+ arrPush(tokens, { kind: "star" });
366
+ pos++;
367
+ continue;
368
+ }
369
+ litBuf += chars[pos];
370
+ pos++;
371
+ }
372
+ flushLit();
373
+ if (starCount === 1 && tokens.length >= 2) {
374
+ const last = tokens[tokens.length - 1];
375
+ const prev = tokens[tokens.length - 2];
376
+ if (last.kind === "star" && prev.kind === "lit" && strEndsWith(prev.text, " ")) {
377
+ const trimmedPrev = strSlice(prev.text, 0, prev.text.length - 1);
378
+ if (trimmedPrev.length > 0) {
379
+ tokens[tokens.length - 2] = { kind: "lit", text: trimmedPrev };
380
+ tokens[tokens.length - 1] = { kind: "optSpaceStar" };
381
+ }
382
+ else {
383
+ tokens[tokens.length - 2] = { kind: "optSpaceStar" };
384
+ tokens.length = tokens.length - 1;
385
+ }
386
+ }
387
+ }
388
+ const n = g.length;
389
+ let dp = [];
390
+ for (let i = 0; i <= n; i++)
391
+ dp[i] = i === 0;
392
+ for (let t = 0; t < tokens.length; t++) {
393
+ const tok = tokens[t];
394
+ const next = [];
395
+ for (let i = 0; i <= n; i++)
396
+ next[i] = false;
397
+ if (tok.kind === "lit") {
398
+ const tlen = tok.text.length;
399
+ for (let j = 0; j + tlen <= n; j++) {
400
+ if (dp[j] && strSlice(g, j, j + tlen) === tok.text) {
401
+ next[j + tlen] = true;
402
+ }
403
+ }
404
+ }
405
+ else if (tok.kind === "star") {
406
+ let first = -1;
407
+ for (let j = 0; j <= n; j++) {
408
+ if (dp[j]) {
409
+ first = j;
410
+ break;
411
+ }
412
+ }
413
+ if (first >= 0) {
414
+ for (let p = first; p <= n; p++)
415
+ next[p] = true;
416
+ }
417
+ }
418
+ else if (tok.kind === "globstar") {
419
+ let firstSlash = -1;
420
+ for (let j = 0; j < n; j++) {
421
+ if (dp[j] && strCharCodeAt(g, j) === 0x2f) {
422
+ firstSlash = j;
423
+ break;
424
+ }
425
+ }
426
+ if (firstSlash >= 0) {
427
+ for (let p = firstSlash; p < n; p++) {
428
+ if (strCharCodeAt(g, p) === 0x2f) {
429
+ next[p + 1] = true;
430
+ }
431
+ }
432
+ }
433
+ }
434
+ else {
435
+ let firstAfterSpace = -1;
436
+ for (let j = 0; j <= n; j++) {
437
+ if (!dp[j])
438
+ continue;
439
+ next[j] = true;
440
+ if (firstAfterSpace < 0 && j < n && strCharCodeAt(g, j) === 0x20) {
441
+ firstAfterSpace = j + 1;
442
+ }
443
+ }
444
+ if (firstAfterSpace >= 0) {
445
+ for (let p = firstAfterSpace; p <= n; p++)
446
+ next[p] = true;
447
+ }
448
+ }
449
+ dp = next;
450
+ }
451
+ return dp[n] === true;
452
+ }
453
+ export function judgeHookCondition(input) {
454
+ try {
455
+ if (!protoKeysUnchanged(objectPrototype, OBJECT_PROTO_KEYS) ||
456
+ !protoKeysUnchanged(arrayPrototype, ARRAY_PROTO_KEYS)) {
457
+ return INVALID_REALM_PROTOTYPES;
458
+ }
459
+ if (!admitPlainRecord(input)) {
460
+ return INVALID_UNREADABLE;
461
+ }
462
+ const keys = objectKeys(input);
463
+ for (let i = 0; i < keys.length; i++) {
464
+ if (!setHas(ENVELOPE_KEYS, keys[i])) {
465
+ return INVALID_ENVELOPE_KEY;
466
+ }
467
+ }
468
+ const event = objectHasOwn(input, "event") ? input.event : undefined;
469
+ if (typeof event !== "string") {
470
+ return INVALID_EVENT;
471
+ }
472
+ const hasToolName = objectHasOwn(input, "toolName") && input.toolName !== undefined;
473
+ const toolName = hasToolName ? input.toolName : undefined;
474
+ if (hasToolName && typeof toolName !== "string") {
475
+ return INVALID_TOOL_NAME;
476
+ }
477
+ const hasEngineOwned = objectHasOwn(input, "engineOwned") && input.engineOwned !== undefined;
478
+ if (hasEngineOwned && input.engineOwned !== true) {
479
+ return INVALID_ENGINE_OWNED;
480
+ }
481
+ const hasToolInput = objectHasOwn(input, "toolInput") && input.toolInput !== undefined;
482
+ const toolInput = hasToolInput ? input.toolInput : undefined;
483
+ if (hasToolInput && !admitPlainRecord(toolInput)) {
484
+ return INVALID_TOOL_INPUT_DATA;
485
+ }
486
+ if (hasToolInput && typeof toolName === "string" && canonicalName(toolName) === "Bash") {
487
+ const bashErr = validateBashInputShape(toolInput);
488
+ if (bashErr !== undefined) {
489
+ return bashErr;
490
+ }
491
+ }
492
+ const condition = objectHasOwn(input, "condition") ? input.condition : undefined;
493
+ if (typeof condition !== "string" || strIncludes(condition, "\\")) {
494
+ return CONDITION_UNREADABLE;
495
+ }
496
+ if (condition === "") {
497
+ return MATCHED;
498
+ }
499
+ if (!setHas(TOOL_EVENT_SET, event)) {
500
+ return NOT_A_TOOL_EVENT;
501
+ }
502
+ if (typeof toolName !== "string" || toolName.length === 0) {
503
+ return INVALID_TOOL_NAME_REQUIRED;
504
+ }
505
+ const { name: rawCondName, content } = splitCondition(condition);
506
+ const condName = canonicalName(rawCondName);
507
+ const callName = canonicalName(toolName);
508
+ if (condName !== callName) {
509
+ return NAME_MISMATCH;
510
+ }
511
+ if (!setHas(CATALOG_NAMES, condName)) {
512
+ return NAME_OUTSIDE_CATALOG;
513
+ }
514
+ if (condName === "Monitor") {
515
+ return NAME_MONITOR;
516
+ }
517
+ if (content === undefined) {
518
+ return MATCHED;
519
+ }
520
+ if (!hasEngineOwned) {
521
+ return CONTENT_REQUIRES_ENGINE_OWNED;
522
+ }
523
+ if (condName !== "Bash") {
524
+ return CONTENT_NON_BASH;
525
+ }
526
+ if (content.length > MAX_CONTENT_CHARS) {
527
+ return CONTENT_TOO_LONG;
528
+ }
529
+ if (!hasToolInput) {
530
+ return CONTENT_ABSENT_TOOL_INPUT;
531
+ }
532
+ const bashInput = toolInput;
533
+ if (objectHasOwn(bashInput, "run_in_background") ||
534
+ objectHasOwn(bashInput, "dangerouslyDisableSandbox")) {
535
+ return CONTENT_CONFIG_DEPENDENT_KEY;
536
+ }
537
+ if (objectHasOwn(bashInput, "timeout") && bashInput.timeout !== undefined) {
538
+ const timeout = bashInput.timeout;
539
+ if (!numberIsInteger(timeout) || timeout < 0 || timeout > MAX_TIMEOUT_MS) {
540
+ return CONTENT_TIMEOUT_RANGE;
541
+ }
542
+ }
543
+ const words = flatWords(bashInput.command);
544
+ if (words === undefined) {
545
+ return CONTENT_NOT_FLAT;
546
+ }
547
+ const w = arrJoin(words, " ");
548
+ const prefix = colonStarPrefix(content);
549
+ if (prefix !== undefined) {
550
+ if (w === prefix ||
551
+ strStartsWith(w, prefix + " ") ||
552
+ w === "xargs " + prefix ||
553
+ strStartsWith(w, "xargs " + prefix + " ")) {
554
+ return MATCHED;
555
+ }
556
+ return CONTENT_NOT_MATCHED;
557
+ }
558
+ if (referenceWildcardMatch(content, w, false, true) ||
559
+ referenceWildcardMatch("xargs " + content, w, false, true)) {
560
+ return MATCHED;
561
+ }
562
+ return CONTENT_NOT_MATCHED;
563
+ }
564
+ catch {
565
+ return INVALID_UNREADABLE;
566
+ }
567
+ }
@@ -1238,6 +1238,8 @@ export interface ToolGateInput extends PermissionSeatInputs, SkillGateSeats, All
1238
1238
  * `dangerouslyDisableSandbox` (`command-sandbox.ts`)? Such a call is never let go without an answer: a surviving `allow` becomes
1239
1239
  * the `sandbox_override` question and every ask on the call carries that class. Absent ⇒ the run has no command sandbox. */
1240
1240
  leavesCommandSandbox?: (args: unknown) => boolean;
1241
+ /** PRESENT ⇔ the Runner declares its shape: the call's destination on these arguments, the leg's mode fold for a bypass request, the deny rows the call may meet and the allow layer's read-only reading ({@link import("./ask-origin.js").ConfinedCall}). */
1242
+ confined?: import("./shell-read-silence.js").ConfinedSeat;
1241
1243
  /** The deployment's A2A peer-isolation switch: a set switch makes the A2A egress default an operator mandate. */
1242
1244
  a2aIsolatePeers?: boolean;
1243
1245
  /** The call goes to an A2A peer registered inside the trust boundary (and not isolated): the peer and the identity stamp its registration
@@ -271,6 +271,7 @@ export interface PermissionModeFold {
271
271
  * announced for a word nobody asked for — and the leg declares `default`. A word somebody asked for (the caller, a steer, the
272
272
  * word a checkpoint recorded because the leg ran under it) folds as it always does, announced.
273
273
  * A static answer: an arming that fails at run time (the classifier's leg cannot be built) is not foreseen here.
274
+ * A `requested`, `ceiling` or `whenUnset` outside its set is refused (`config.permission_mode_invalid`, `detail.field` names which).
274
275
  */
275
276
  export declare function foldPermissionMode(input: PermissionModeFoldInput): PermissionModeFold;
276
277
  /** `RunnerDeps.permissionModeWhenUnset` read: absent, or a start word — anything else is refused by name, never read as `default`. */
@@ -362,6 +363,9 @@ export declare class PermissionModeController {
362
363
  * A decision read is what starts it; an observation that did would arm a leg earlier than its own gate, and so move
363
364
  * who answers the leg's next ask. */
364
365
  get observed(): PermissionMode;
366
+ /** The effective mode this leg would run under were `word` asked for now — the same fold ({@link foldPermissionMode}) over the
367
+ * same ceiling (observed) and arming facts, with `word` as the request; an observation: nothing is steered, armed or announced. */
368
+ armsAs(word: PermissionMode): PermissionMode;
365
369
  /** The three-member answer as it stands right now. */
366
370
  receipt(): PermissionModeReceipt;
367
371
  private receiptOf;
@@ -159,9 +159,21 @@ function autoModeArmFactOf(word, a) {
159
159
  }
160
160
  return { armed: true, reason: "armed" };
161
161
  }
162
+ function refuseFoldModeWord(field, value, allowed) {
163
+ const e = new Error(`foldPermissionMode: ${field} must be one of ${allowed.map((w) => JSON.stringify(w)).join(", ")} when present (got ${describeRejectedValue(value)}) — omit it to leave it unset.`);
164
+ e.code = "config.permission_mode_invalid";
165
+ e.detail = { field };
166
+ throw e;
167
+ }
162
168
  export function foldPermissionMode(input) {
163
- const { requested, ceiling, arming } = input;
164
- const { effective: word, clamped } = ceiling === undefined ? { effective: requested ?? input.whenUnset ?? "default", clamped: false } : clampPermissionMode(requested, ceiling);
169
+ const { requested, ceiling, whenUnset, arming } = input;
170
+ if (requested !== undefined && !isPermissionMode(requested))
171
+ refuseFoldModeWord("requested", requested, PERMISSION_MODES);
172
+ if (ceiling !== undefined && !isPermissionMode(ceiling))
173
+ refuseFoldModeWord("ceiling", ceiling, PERMISSION_MODES);
174
+ if (whenUnset !== undefined && !PERMISSION_MODE_START_WORDS.includes(whenUnset))
175
+ refuseFoldModeWord("whenUnset", whenUnset, PERMISSION_MODE_START_WORDS);
176
+ const { effective: word, clamped } = ceiling === undefined ? { effective: requested ?? whenUnset ?? "default", clamped: false } : clampPermissionMode(requested, ceiling);
165
177
  const base = requested !== undefined ? { requested } : {};
166
178
  const autoMode = arming === undefined ? undefined : autoModeArmFactOf(word, arming);
167
179
  const armed = autoMode !== undefined ? { autoMode } : {};
@@ -252,6 +264,10 @@ export class PermissionModeController {
252
264
  get observed() {
253
265
  return this.receiptOf(true).effective;
254
266
  }
267
+ armsAs(word) {
268
+ const ceiling = this.ceilingNow(true);
269
+ return foldPermissionMode({ requested: word, ...(ceiling !== undefined ? { ceiling } : {}), ...(this.whenUnset !== undefined ? { whenUnset: this.whenUnset } : {}), ...(this.arming !== undefined ? { arming: this.arming } : {}) }).receipt.effective;
270
+ }
255
271
  receipt() {
256
272
  return this.receiptOf(false);
257
273
  }
@@ -97,14 +97,16 @@ export interface Brain {
97
97
  * per-call identity the request is dispatched with (the `getApiKeyAndHeaders` result), so a seat can
98
98
  * judge the exact request it is about to make. The engine's model lane asks it before EVERY hop it
99
99
  * dispatches — the requested model first — and does not dispatch a hop it refuses (the walk moves on;
100
- * no hop served ⇒ the requested model's refusal is the answer). First-party brains implement it against
100
+ * no hop served ⇒ the requested model's refusal is the answer); a failover chain also asks each leg right
101
+ * before it sends that leg, and does not send a leg that does not go ahead. First-party brains implement it against
101
102
  * their own config; the decorators (routing/failover/circuit-breaker) and the engine's model lane
102
103
  * re-dispatch it the way their `stream` would. `undefined` = this brain cannot judge (a custom brain
103
104
  * without the face) — callers must treat that as "no judgment", never as OK or as broken. A refusal is
104
105
  * an `ok: false` verdict, never a throw (the first-party faces include the per-call key handed beside its
105
106
  * lane's own key header, `route.credential_answer_conflict`). It answers SYNCHRONOUSLY. A throw, a Promise or
106
- * any other answer that is not a verdict is a defect — the model lane and a derived seat's pre-flight read
107
- * it as the route refused (`route.judgment_failed`), a failover chain's face a throw as that leg refused.
107
+ * any other answer that is not a verdict is a defect — the model lane, a derived seat's pre-flight and a failover
108
+ * chain's ask before a leg read it as the route refused (`route.judgment_failed`), a failover chain's face a throw as
109
+ * that leg refused.
108
110
  *
109
111
  * The same law runs again inside the first-party brains' request build (single source, called
110
112
  * twice): a broken pairing that skips the pre-flight still refuses loudly at the request instead
@@ -118,8 +120,9 @@ export interface Brain {
118
120
  * only hop of a request with no chain, the first hop of a walk), `"fallback"` for a later hop of its chain. Present ONLY on that ask: a
119
121
  * refusal answered to it means the call was not sent (save a request cancelled while it was judged: the Brain answers that one,
120
122
  * aborted). Absent on every other ask (a read face such as `routePairingStatus`, a derived seat's pre-flight, a host's own check).
121
- * The engine's one-target decorators hand it on as they hand on the second argument; a failover chain asks its legs without it (its
122
- * judgment is the chain's — which leg is sent is decided after); a host's own decorator hands it on to be heard below it.
123
+ * The engine's one-target decorators hand it on as they hand on the second argument; a failover chain asks its legs without it, both
124
+ * when it judges for the chain and when it asks a leg right before sending it (the gateway does not know which hop of a walk it
125
+ * serves); a host's own decorator hands it on to be heard below it.
123
126
  */
124
127
  export interface RouteJudgmentDispatch {
125
128
  readonly hop: "requested" | "fallback";