@arcforge/kernel 2.0.175 → 2.0.176

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.175",
3
+ "version": "2.0.176",
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.175",
19
- "@arcforge/engines": "2.0.175",
20
- "@arcforge/err": "2.0.175",
21
- "@arcforge/session": "2.0.175",
22
- "@arcforge/types": "2.0.175",
23
- "@arcforge/cloud": "2.0.175",
24
- "@arcforge/capsule": "2.0.175"
18
+ "@arcforge/air": "2.0.176",
19
+ "@arcforge/engines": "2.0.176",
20
+ "@arcforge/err": "2.0.176",
21
+ "@arcforge/session": "2.0.176",
22
+ "@arcforge/types": "2.0.176",
23
+ "@arcforge/cloud": "2.0.176",
24
+ "@arcforge/capsule": "2.0.176"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/bun": "latest",
package/src/engine.ts CHANGED
@@ -811,6 +811,8 @@ export function Engine(opts: EngineOpts) {
811
811
  * below for why that is safe, and what it fixes.
812
812
  */
813
813
  let streamedOut = 0
814
+ /** `<text>` blocks OPENED — see the text:open case for why this is not `texts`. */
815
+ let opened = 0
814
816
 
815
817
  /**
816
818
  * Blocks produced by this attempt, in order.
@@ -868,7 +870,7 @@ export function Engine(opts: EngineOpts) {
868
870
  // whole attempt may be discarded — stop streaming and let the
869
871
  // post-loop checks decide. Anything already sent has crossed;
870
872
  // that is what `spoke` reports to those checks.
871
- if (texts > 1 || scripts > 0) return []
873
+ if (opened > 1 || texts > 1 || scripts > 0) return []
872
874
 
873
875
  const ready: AxonEngineEvent[] = []
874
876
  while (streamedOut < events.length) {
@@ -886,6 +888,15 @@ export function Engine(opts: EngineOpts) {
886
888
  switch (block.type) {
887
889
  case "text:open":
888
890
  lang = block.lang
891
+ // Counted at OPEN, not at close.
892
+ //
893
+ // `drain()` has to stop the instant a surplus block
894
+ // becomes possible, and `texts` only reaches 2 when the
895
+ // second block CLOSES — by which point its deltas have
896
+ // already streamed. A chunk of the block we are about
897
+ // to refuse would reach the user, which is exactly the
898
+ // leak this whole gate exists to prevent.
899
+ opened++
889
900
  break
890
901
 
891
902
  case "text:delta":
@@ -990,16 +1001,63 @@ export function Engine(opts: EngineOpts) {
990
1001
  // The assertion states that invariant where it matters rather than
991
1002
  // trusting the reader to re-derive it, and fails loudly if a future
992
1003
  // change to `drain()` breaks it.
993
- if (violations.length > 0 || texts > 1 || scripts > 1 || (!spoke && !acted)) {
994
- if (streamedOut > 0) {
995
- // Not an EngineFailure: no provider did anything wrong.
996
- // This is an internal invariant of this loop, and it must
997
- // be unmissable rather than degrade into a duplicated reply.
998
- throw new Error(
999
- `AXON_INTERNAL: retry reached after ${streamedOut} block(s) already streamed to the caller — `
1000
- + "drain() emitted something a retry path can still discard",
1001
- )
1002
- }
1004
+ // ── When a retry would discard what the user has already read ──
1005
+ //
1006
+ // A retry rewrites the attempt and asks again. That is only honest
1007
+ // while the caller has been shown NOTHING — re-answering after a
1008
+ // block reached the session leaves the discarded text on screen
1009
+ // with its replacement underneath.
1010
+ //
1011
+ // This case is REACHABLE, and used to throw AXON_INTERNAL:
1012
+ //
1013
+ // <text>...</text><text>second</text><done/>
1014
+ //
1015
+ // The first block closes, `texts` is 1, no retry path can fire, so
1016
+ // drain() streams it — correct at that instant. The second block
1017
+ // then makes `texts` 2, which IS a retry path, and by then the
1018
+ // first block has crossed. The old assertion read that as a broken
1019
+ // invariant and killed the wake; it was really the one shape where
1020
+ // retrying and honesty genuinely conflict.
1021
+ //
1022
+ // Resolved in favour of what the user already has: keep the reply,
1023
+ // drop the surplus blocks, and correct the model through the same
1024
+ // fault channel a retry would have used. It learns the rule either
1025
+ // way — the only difference is that it is not asked to say again
1026
+ // what the user has already read.
1027
+ //
1028
+ // `streamedOut > 0` is the whole switch, and nothing else changes:
1029
+ // a violation with nothing on screen (two scripts, an empty reply)
1030
+ // still retries exactly as before, which is strictly better there
1031
+ // because the user sees one clean answer instead of a partial one
1032
+ // plus a correction.
1033
+ const crossed = streamedOut > 0
1034
+ if (crossed && (violations.length > 0 || texts > 1 || scripts > 1)) {
1035
+ lastFault = violations.length > 0
1036
+ ? describeViolations(violations, violatingScript)
1037
+ : describeTooManyBlocks(texts, scripts)
1038
+
1039
+ // `rejected` carries the whole reply, so the surplus block is
1040
+ // in the durable record rather than silently gone. Dropping
1041
+ // content the model meant to send without a trace is how a
1042
+ // graceful degradation becomes an invisible one.
1043
+ await opts.fault({
1044
+ code: violations.length > 0 ? "OUTPUT_CONTRACT" : "OUTPUT_TOO_MANY_BLOCKS",
1045
+ message: lastFault,
1046
+ rejected: step.value?.text,
1047
+ attempt: attempt + 1,
1048
+ })
1049
+
1050
+ // NOTHING further is yielded. `drain()` stopped the moment the
1051
+ // surplus appeared, so everything from `streamedOut` on is
1052
+ // exactly the part being dropped — yielding it would deliver
1053
+ // the blocks this branch exists to refuse, and a dropped
1054
+ // `<script>` would actually RUN.
1055
+ //
1056
+ // `acted` is reported as false for the same reason: no script
1057
+ // crossed, so the loop's stop condition must not be told one
1058
+ // did. `spoke` stays true — the text really was shown.
1059
+ yield { type: "engine:done", response: step.value, spoke, acted: false, yielded }
1060
+ return
1003
1061
  }
1004
1062
 
1005
1063
  if (violations.length === 0 && (texts > 1 || scripts > 1)) {
package/src/kernel.ts CHANGED
@@ -1078,6 +1078,44 @@ export async function Kernel(opts: KernelOpts) {
1078
1078
  /** Invoke on a stimulus arrival — invocation-mode cognets only; throws for continuous-mode. */
1079
1079
  stream: streamWithContract,
1080
1080
 
1081
+ /**
1082
+ * Deliver a stimulus into the wake that is ALREADY running.
1083
+ *
1084
+ * `request`/`stream` both reserve, so a caller holding a message while
1085
+ * the brain is mid-conversation has only two options today: refuse it
1086
+ * (RUN_IN_PROGRESS) or wait for the whole agent loop to finish. Both
1087
+ * are wrong for the case this exists for — a user typing while the
1088
+ * agent works. The message is not a new conversation, it is more of
1089
+ * the one in flight, and it should reach the brain at the first moment
1090
+ * the brain can hear it.
1091
+ *
1092
+ * That moment already exists. A cognet's loop re-reads the session log
1093
+ * each pass (zero's `sync()`), so an entry committed mid-wake is
1094
+ * folded into the next turn's history automatically — the scheduler's
1095
+ * own comment has promised this the whole time ("it stays on the queue
1096
+ * and the running wake's own drain picks it up"); there was simply no
1097
+ * public verb that could put it there without also trying to start a
1098
+ * wake.
1099
+ *
1100
+ * So this is ingest and nothing else: commit the entry, push it on the
1101
+ * stimuli buffer, return. No reservation, no wake, no admission
1102
+ * verdict. If NO wake is running the entry still lands durably, and
1103
+ * the scheduler's bus subscription starts one — which is exactly the
1104
+ * behaviour a caller racing the end of a wake wants, and the reason
1105
+ * this does not need to know whether one is in flight.
1106
+ */
1107
+ async ingest(input: KernelInput = {}): Promise<void> {
1108
+ const contents = input.content === undefined
1109
+ ? []
1110
+ : Array.isArray(input.content) ? input.content : [input.content]
1111
+ for (const content of contents) {
1112
+ await opts.session.stimuli.ingest("cognet:stimulus:text", {
1113
+ channel: input.channel ?? "terminal",
1114
+ content,
1115
+ })
1116
+ }
1117
+ },
1118
+
1081
1119
  /**
1082
1120
  * Collect a full wake: every durable entry, in commit order.
1083
1121
  *