@arcforge/kernel 2.0.167 → 2.0.169

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcforge/kernel",
3
- "version": "2.0.167",
3
+ "version": "2.0.169",
4
4
  "description": "The Axon kernel — ring 0. Owns resources and execution, enforces policy, and is the only constructor of the unprivileged capsule.",
5
5
  "type": "module",
6
6
  "private": false,
@@ -15,13 +15,13 @@
15
15
  "deploy": "echo \"This package is published ONLY by apps/tui/scripts/release.ts, which pins workspace:* deps to concrete versions first. Publishing it directly ships an unresolvable dependency.\" && exit 1"
16
16
  },
17
17
  "dependencies": {
18
- "@arcforge/air": "2.0.167",
19
- "@arcforge/engines": "2.0.167",
20
- "@arcforge/err": "2.0.167",
21
- "@arcforge/session": "2.0.167",
22
- "@arcforge/types": "2.0.167",
23
- "@arcforge/cloud": "2.0.167",
24
- "@arcforge/capsule": "2.0.167"
18
+ "@arcforge/air": "2.0.169",
19
+ "@arcforge/engines": "2.0.169",
20
+ "@arcforge/err": "2.0.169",
21
+ "@arcforge/session": "2.0.169",
22
+ "@arcforge/types": "2.0.169",
23
+ "@arcforge/cloud": "2.0.169",
24
+ "@arcforge/capsule": "2.0.169"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/bun": "latest",
package/src/capsule.ts CHANGED
@@ -178,8 +178,27 @@ export async function AxonCapsule(opts: AxonCapsuleOpts) {
178
178
  // move as it happened (capsule:cwd), so there is nothing to ask it
179
179
  // for on the way out. This used to execute process.cwd() inside a
180
180
  // dying incarnation and silently keep a stale value when that failed.
181
+ const parked = liveCwd ?? opts.cwd
181
182
  current = await boot()
182
183
  await previous?.shutdown()
184
+ // Re-park AFTER the outgoing sandbox has gone.
185
+ //
186
+ // The sandboxes share one process, so cwd is shared state with an
187
+ // owner rather than a value each holds. The outgoing one restores the
188
+ // directory it entered with — correct when nothing succeeded it, and
189
+ // wrong here, because the successor booted first and had already
190
+ // chdir'd the process to where it was configured. Without this, a
191
+ // `cwd` passed to Axon() and any mid-session chdir() silently reset to
192
+ // the host's directory on every hot reload, and the model's "cwd
193
+ // persists across blocks" contract broke at exactly the seam it was
194
+ // least visible.
195
+ //
196
+ // Done here rather than inside the sandbox because this is the only
197
+ // layer that knows a successor exists: from inside, "someone else owns
198
+ // the cwd now" is indistinguishable from "nobody does".
199
+ if (process.cwd() !== parked) {
200
+ try { process.chdir(parked) } catch { /* gone — the next run fails on its own terms */ }
201
+ }
183
202
  if (previousId) await detach(previousId, "reload")
184
203
  }
185
204
 
package/src/kernel.ts CHANGED
@@ -221,6 +221,21 @@ function interruptedResult(reason: unknown): AxonRunResult {
221
221
  * output back as part of the result reads `stdout` here, no callback wired
222
222
  * by hand.
223
223
  */
224
+ /**
225
+ * A refusal as a sentence the model can act on.
226
+ *
227
+ * Names the verb and the rule, because those imply opposite fixes: a
228
+ * `not-allowed` needs a rule added, a `deny` needs one removed, and an
229
+ * `escalation-denied` means a person said no and retrying is pointless.
230
+ * "Denied by policy" tells a model nothing it can do anything about — the
231
+ * same reasoning `shellDenial()` already applies in the capsule.
232
+ */
233
+ function denialMessage(denials: { fn: string; rule: string }[]): string {
234
+ const first = denials[0]!
235
+ const rest = denials.length > 1 ? ` (and ${denials.length - 1} more)` : ""
236
+ return `denied by policy: ${first.fn} — ${first.rule}${rest}`
237
+ }
238
+
224
239
  async function runOne(capsule: AxonCapsuleT, code: string, opts?: { signal?: AbortSignal; id?: string; origin?: CapsuleCommandOrigin }): Promise<AxonRunResult> {
225
240
  const stdout: string[] = []
226
241
  const onConsole = (level: string, args: unknown[]) => {
@@ -228,15 +243,71 @@ async function runOne(capsule: AxonCapsuleT, code: string, opts?: { signal?: Abo
228
243
  stdout.push(level === "log" || level === "info" ? line : `[${level}] ${line}`)
229
244
  }
230
245
 
246
+ /**
247
+ * Denials seen for THIS block, whether it returned or threw.
248
+ *
249
+ * Collected here rather than read off the result alone because the two
250
+ * refusal shapes settle differently: a denied `process.spawn()` RETURNS
251
+ * (the handle reports `exited`), while a denied TOOL call THROWS
252
+ * (`CAPSULE_POLICY_DENIED` out of the mediate wrapper). Reading only the
253
+ * resolved result classified the throwing half as a plain "exception" —
254
+ * true but useless, since "your policy refused this" and "your code has a
255
+ * bug" are the two things a reader most needs to tell apart.
256
+ */
257
+ const denials: { fn: string; module: string; rule: string }[] = []
258
+ const offDenied = capsule.current.on("process:policy:denied", event => {
259
+ if (opts?.id && event.commandId !== opts.id) return
260
+ denials.push({ fn: event.fn, module: event.module, rule: event.rule })
261
+ })
262
+
231
263
  try {
232
264
  // exec() rather than run(): the bindings come back alongside the
233
265
  // value, so a cognet rendering a template from the same turn has
234
266
  // something to interpolate against.
235
267
  const { value, scope } = await capsule.current.exec(code, { onConsole, signal: opts?.signal, id: opts?.id, origin: opts?.origin })
268
+
269
+ /**
270
+ * A block whose calls were REFUSED is not a block that succeeded.
271
+ *
272
+ * `ok` used to mean "did the code throw", which is the wrong question:
273
+ * a denial does not throw. `process.spawn()` hands back a handle whose
274
+ * status is `exited`, and `process.run()` returns `{ ok: false }` as a
275
+ * VALUE — neither reaches the catch below. So a policy could refuse
276
+ * every call a block made and the kernel still committed
277
+ * `cognet:action:result` with `ok: true`.
278
+ *
279
+ * That is a silent failure at the one boundary the whole policy system
280
+ * exists to make visible. The model believed it had spawned a process,
281
+ * the timeline rendered an ordinary tool call, and the user's own
282
+ * policy had stopped it with nothing anywhere saying so.
283
+ *
284
+ * The value and stdout are kept: a block that made ten calls and had
285
+ * one refused still did nine real things, and throwing that away would
286
+ * replace one silent failure with another.
287
+ */
288
+ if (denials.length > 0) {
289
+ return {
290
+ ok: false,
291
+ value,
292
+ stdout,
293
+ scope,
294
+ denials: denials,
295
+ // "policy", the kind the entry ontology already had and nothing
296
+ // ever set — a slot reserved for exactly this and left empty.
297
+ error: { kind: "policy", message: denialMessage(denials) },
298
+ }
299
+ }
300
+
236
301
  return { ok: true, value, stdout, scope }
237
302
  } catch (cause) {
238
303
  const interrupted = opts?.signal?.aborted || (cause instanceof Error && cause.name === "AbortError")
239
304
  const timedOut = cause instanceof Error && /timed out/.test(cause.message)
305
+ // A recorded denial is the more SPECIFIC truth than "it threw", and
306
+ // it is what the throw was caused by — so it wins the classification.
307
+ // An interrupt still outranks it: cancelling mid-block can refuse a
308
+ // call on the way out, and reporting that as a policy problem would
309
+ // send a reader after a rule that is working fine.
310
+ const denied = denials.length > 0 && !interrupted && !timedOut
240
311
  return {
241
312
  ok: false,
242
313
  stdout,
@@ -244,11 +315,14 @@ async function runOne(capsule: AxonCapsuleT, code: string, opts?: { signal?: Abo
244
315
  // the honest value, and keeps callers from having to test for
245
316
  // absence before every interpolation.
246
317
  scope: EMPTY_CAPSULE_SCOPE,
318
+ ...(denials.length > 0 ? { denials } : {}),
247
319
  error: {
248
- kind: interrupted ? "interrupt" : timedOut ? "timeout" : "exception",
249
- message: cause instanceof Error ? cause.message : String(cause),
320
+ kind: denied ? "policy" : interrupted ? "interrupt" : timedOut ? "timeout" : "exception",
321
+ message: denied ? denialMessage(denials) : cause instanceof Error ? cause.message : String(cause),
250
322
  },
251
323
  }
324
+ } finally {
325
+ offDenied()
252
326
  }
253
327
  }
254
328
 
@@ -329,6 +403,11 @@ export async function Kernel(opts: KernelOpts) {
329
403
  session,
330
404
  ...(opts.escalate ? { escalate: opts.escalate } : {}),
331
405
  run: () => scheduler.current(),
406
+ // Read LIVE through the capsule manager's `current`, never captured:
407
+ // a reload replaces the capsule, and a captured handle would keep
408
+ // reporting the old one's execution store — which after a reload is
409
+ // always null, silently orphaning every span again.
410
+ commandId: () => capsule.current.commandId,
332
411
  })
333
412
 
334
413
  const engine = Engine({
package/src/mediation.ts CHANGED
@@ -32,6 +32,16 @@ type MediationOpts = {
32
32
  escalate?: AxonEscalate
33
33
  /** Correlates each span to the run that caused it, when one is active. */
34
34
  run?: () => { runId: string } | null
35
+ /**
36
+ * The capsule block currently executing, or null outside one.
37
+ *
38
+ * A tool call happens INSIDE a block, so every span and every denial this
39
+ * layer records belongs to one. The id lives in the capsule's execution
40
+ * store; this reads it so a surface can hang a refusal under the
41
+ * `Run(...)` that provoked it. Every span here committed `commandId: ""`
42
+ * before, which is why a denied tool call was an orphan on the log.
43
+ */
44
+ commandId?: () => string | null
35
45
  }
36
46
 
37
47
  /** How long an unanswered escalation waits before failing closed. */
@@ -40,6 +50,11 @@ const ESCALATION_TIMEOUT_MS = 30_000
40
50
  const POLICY_WILDCARD = "*"
41
51
 
42
52
  export function Mediation(opts: MediationOpts) {
53
+ /** The block this call belongs to — "" when there is none, as the span shape expects. */
54
+ function commandId(): string {
55
+ return opts.commandId?.() ?? ""
56
+ }
57
+
43
58
  /**
44
59
  * Resolve which rule governs a call, by walking its ADDRESS.
45
60
  *
@@ -102,7 +117,7 @@ export function Mediation(opts: MediationOpts) {
102
117
 
103
118
  if (verdict === "deny") {
104
119
  await opts.session.commit("process:policy:denied", {
105
- id: randomUUID(), module: owner, fn, args, rule: String(rule),
120
+ id: randomUUID(), commandId: commandId() || null, module: owner, fn, args, rule: String(rule),
106
121
  }, span())
107
122
  return false
108
123
  }
@@ -113,7 +128,7 @@ export function Mediation(opts: MediationOpts) {
113
128
  // with nothing written is a hole in its only function.
114
129
  const id = randomUUID()
115
130
  await opts.session.commit("process:policy:escalation", {
116
- id, module: owner, fn, args, rule: String(rule),
131
+ id, commandId: commandId() || null, module: owner, fn, args, rule: String(rule),
117
132
  }, span())
118
133
 
119
134
  const started = Date.now()
@@ -121,6 +136,15 @@ export function Mediation(opts: MediationOpts) {
121
136
  await opts.session.commit("process:policy:decision", {
122
137
  id, allow, durationMs: Date.now() - started,
123
138
  }, span())
139
+ // A refused escalation is a DENIAL and says so on the record. The
140
+ // decision event alone says WHAT was answered; only this says the call
141
+ // did not happen, which is the fact a surface renders.
142
+ if (!allow) {
143
+ await opts.session.commit("process:policy:denied", {
144
+ id: randomUUID(), commandId: commandId() || null, module: owner, fn, args,
145
+ rule: "escalation-denied",
146
+ }, span())
147
+ }
124
148
  return allow
125
149
  }
126
150
 
@@ -177,7 +201,7 @@ export function Mediation(opts: MediationOpts) {
177
201
 
178
202
  if (decision.verdict === "deny") {
179
203
  await opts.session.commit("process:policy:denied", {
180
- id: randomUUID(), module: owner, fn, args: argv, rule: decision.reason,
204
+ id: randomUUID(), commandId: commandId() || null, module: owner, fn, args: argv, rule: decision.reason,
181
205
  }, span())
182
206
  return decision
183
207
  }
@@ -203,14 +227,14 @@ export function Mediation(opts: MediationOpts) {
203
227
  */
204
228
  emit: {
205
229
  start(input: { module: string; fn: string; args: unknown[] }) {
206
- void opts.session.commit("process:fn:start", { commandId: "", ...input }, span())
230
+ void opts.session.commit("process:fn:start", { commandId: commandId(), ...input }, span())
207
231
  },
208
232
  complete(input: { module: string; fn: string; result: unknown; durationMs: number }) {
209
- void opts.session.commit("process:fn:complete", { commandId: "", ...input }, span())
233
+ void opts.session.commit("process:fn:complete", { commandId: commandId(), ...input }, span())
210
234
  },
211
235
  failed(input: { module: string; fn: string; error: unknown; durationMs: number }) {
212
236
  void opts.session.commit("process:fn:failed", {
213
- commandId: "",
237
+ commandId: commandId(),
214
238
  module: input.module,
215
239
  fn: input.fn,
216
240
  error: input.error as never,
package/src/scope.ts CHANGED
@@ -39,6 +39,11 @@ export function toScopeModule(tool: AxonTool): AxonScopeModule {
39
39
  ...(tool.description ? { description: tool.description } : {}),
40
40
  members: tool.fns,
41
41
  ...(tool.ambientTypes ? { ambientTypes: tool.ambientTypes } : {}),
42
+ // ORIGIN is the placement decision, and this is the one place it is
43
+ // made. Dropping it here is what let both scope renderers declare
44
+ // every tool flat while the runtime installed a module's under its
45
+ // namespace — see AxonScopeModule.flat.
46
+ flat: tool.origin !== "module",
42
47
  }
43
48
  }
44
49