@arcforge/kernel 2.0.142 → 2.0.143

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 (2) hide show
  1. package/package.json +8 -8
  2. package/src/kernel.ts +69 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcforge/kernel",
3
- "version": "2.0.142",
3
+ "version": "2.0.143",
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.142",
19
- "@arcforge/engines": "2.0.142",
20
- "@arcforge/err": "2.0.142",
21
- "@arcforge/session": "2.0.142",
22
- "@arcforge/types": "2.0.142",
23
- "@arclabs/cloud": "2.0.142",
24
- "@axon/capsule": "2.0.142"
18
+ "@arcforge/air": "2.0.143",
19
+ "@arcforge/engines": "2.0.143",
20
+ "@arcforge/err": "2.0.143",
21
+ "@arcforge/session": "2.0.143",
22
+ "@arcforge/types": "2.0.143",
23
+ "@arclabs/cloud": "2.0.143",
24
+ "@axon/capsule": "2.0.143"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/bun": "latest",
package/src/kernel.ts CHANGED
@@ -85,6 +85,32 @@ function formatValue(value: unknown): string {
85
85
  */
86
86
  export type RunOptions = { signal?: AbortSignal; origin?: CapsuleCommandOrigin }
87
87
 
88
+ /** Thrown by a script that was cancelled before it started — see runAndCommit. */
89
+ function interruptedError(signal: AbortSignal): DOMException {
90
+ return new DOMException(String(signal.reason ?? "user"), "AbortError")
91
+ }
92
+
93
+ /**
94
+ * The result shape for a script the wake cancelled before it ran.
95
+ *
96
+ * A settled `AxonRunResult` rather than a rejection, because the batch's other
97
+ * blocks DID run and a caller reading `results[i]` must still get block i. The
98
+ * `interrupt` kind already exists on the ABI for exactly this outcome, so a
99
+ * consumer that handles a timed-out or interrupted block handles this with no
100
+ * new case.
101
+ */
102
+ function interruptedResult(reason: unknown): AxonRunResult {
103
+ return {
104
+ ok: false,
105
+ stdout: [],
106
+ scope: EMPTY_CAPSULE_SCOPE,
107
+ error: {
108
+ kind: "interrupt",
109
+ message: reason instanceof Error ? reason.message : String(reason),
110
+ },
111
+ }
112
+ }
113
+
88
114
  /**
89
115
  * Run one block against the capsule and normalize the outcome — the ABI
90
116
  * boundary this seam exists for. Exec() below (capsule's own contract)
@@ -246,8 +272,50 @@ export async function Kernel(opts: KernelOpts) {
246
272
  * the two-verb rule — the cognet never calls session.commit itself; it
247
273
  * just reads the returned result for its own control flow.
248
274
  */
275
+ /**
276
+ * A batch of blocks from one turn — concurrent, as it has always been.
277
+ *
278
+ * Concurrency is deliberate (see `run` on the capsule surface and zero's
279
+ * own comment): the kernel commits each action and its result in order
280
+ * regardless of the order the work finishes in, so running them together
281
+ * is safe and much faster than a queue.
282
+ *
283
+ * What changes under cancellation is only WHETHER a script starts. Each
284
+ * `runAndCommit` re-checks the signal, so a batch interrupted partway
285
+ * commits the blocks that had already begun and skips the rest, instead of
286
+ * executing all of them because they were all dispatched in one tick.
287
+ *
288
+ * `allSettled`, not `all`: a skipped script rejects, and one rejection must
289
+ * not discard the results of siblings that genuinely ran. The results keep
290
+ * their positions so a caller reading `results[i]` still gets block `i`.
291
+ */
292
+ async function runBatch(code: string[], signal: AbortSignal): Promise<AxonRunResult[]> {
293
+ const settled = await Promise.allSettled(code.map(c => runAndCommit(c, { signal })))
294
+ return settled.map(outcome => outcome.status === "fulfilled"
295
+ ? outcome.value
296
+ : interruptedResult(outcome.reason))
297
+ }
298
+
249
299
  async function runAndCommit(code: string, runOpts?: { signal?: AbortSignal }): Promise<AxonRunResult> {
250
300
  const run = scheduler.active() // throws SYSCALL_OUTSIDE_RUN if no wake is running
301
+
302
+ // A script that has not STARTED must not start once the wake is
303
+ // cancelled. This is the boundary the kernel actually controls: the
304
+ // capsule can kill a spawned process (procs.ts kills the tree on
305
+ // abort), but a tool already inside `await` is only cancellable if it
306
+ // threads the signal itself — which is not a contract we put on module
307
+ // authors. So the kernel stops the SEQUENCE instead.
308
+ //
309
+ // The case this exists for is a turn that emitted several blocks: they
310
+ // run as a batch (see `run` below), and without this check every
311
+ // remaining script still executed after the user pressed Escape. One
312
+ // long command was already killed; forty small writes were not.
313
+ //
314
+ // Checked here rather than in `run` so it covers both entry points,
315
+ // and BEFORE the action is committed — a script that never ran must
316
+ // not appear in the log as one that did.
317
+ if (runOpts?.signal?.aborted) throw interruptedError(runOpts.signal)
318
+
251
319
  const id = Bun.randomUUIDv7()
252
320
  await session.commitEntry("cognet:action:typescript", { id, content: code }, run)
253
321
  // The entry id IS the capsule command id — one id for one execution,
@@ -407,7 +475,7 @@ export async function Kernel(opts: KernelOpts) {
407
475
  */
408
476
  run: ((code: string | string[]) => {
409
477
  const { signal } = scheduler.active() // throws SYSCALL_OUTSIDE_RUN if no wake is running
410
- if (Array.isArray(code)) return Promise.all(code.map(c => runAndCommit(c, { signal })))
478
+ if (Array.isArray(code)) return runBatch(code, signal)
411
479
  return runAndCommit(code, { signal })
412
480
  }) as KernelAbi["run"],
413
481
  scope: () => capsule.current.scope,