@iowarp/clio-coder 0.3.3 → 0.3.4

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 (244) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/CONTRIBUTING.md +1 -1
  3. package/README.md +3 -3
  4. package/dist/{acp-P2AQILE2.js → acp-S5R4RR5B.js} +7 -6
  5. package/dist/{agents-72W3BI7I.js → agents-P6DMMVZY.js} +24 -21
  6. package/dist/assets/codewiki.json +1 -1
  7. package/dist/{auth-5TWEIYDN.js → auth-2XCZLPKS.js} +12 -8
  8. package/dist/{chunk-GGXXDWE4.js → chunk-22NAGB7X.js} +2 -2
  9. package/dist/{chunk-2DJ2KNFG.js → chunk-2LZI5CAG.js} +133 -13
  10. package/dist/{chunk-OAO4GE4M.js → chunk-2TZWSW76.js} +2 -2
  11. package/dist/{chunk-OOJYHWRB.js → chunk-34475P3I.js} +2 -2
  12. package/dist/{chunk-4XUGQOHA.js → chunk-35MKKU5R.js} +4 -4
  13. package/dist/{chunk-LM5TQCJZ.js → chunk-3HZ5RWN2.js} +5 -5
  14. package/dist/{chunk-AGYYIBLL.js → chunk-3JLKSKD7.js} +2 -2
  15. package/dist/{chunk-KZWTDYJF.js → chunk-4JUF2NNX.js} +7 -7
  16. package/dist/{chunk-ZDOOVTXZ.js → chunk-4OC57DA6.js} +27 -4
  17. package/dist/chunk-5M54SPOL.js +926 -0
  18. package/dist/{chunk-STBPMHSX.js → chunk-7RXG6QRZ.js} +51 -11
  19. package/dist/{chunk-X6IAEBZR.js → chunk-A2GZF7DC.js} +5 -5
  20. package/dist/{chunk-A3CYT5EX.js → chunk-AD2SYQYC.js} +55 -2
  21. package/dist/chunk-AOCYTWAV.js +449 -0
  22. package/dist/chunk-BEY543CS.js +258 -0
  23. package/dist/{chunk-6N5PTWMY.js → chunk-BP4OYD6A.js} +32 -13
  24. package/dist/chunk-BPGS2WCQ.js +612 -0
  25. package/dist/{chunk-V6RTAOC2.js → chunk-BRXQQJFP.js} +8 -8
  26. package/dist/chunk-CFGTUFWB.js +67 -0
  27. package/dist/{chunk-CBCAPZAA.js → chunk-E25LMLRW.js} +2 -2
  28. package/dist/{chunk-DUYJ5IO6.js → chunk-EDRHSCIE.js} +4 -4
  29. package/dist/{chunk-XBXAASKX.js → chunk-EFADSJET.js} +2 -2
  30. package/dist/{chunk-M6SHUN7Q.js → chunk-FO5ZOVUY.js} +2 -2
  31. package/dist/chunk-FYYLNIL5.js +313 -0
  32. package/dist/{chunk-FNTMWMX5.js → chunk-HV5X7OR2.js} +14 -12
  33. package/dist/{chunk-TZTZS7QK.js → chunk-HXG4IURW.js} +5 -3
  34. package/dist/{chunk-5UFT4SUX.js → chunk-K6WL7QZT.js} +3 -3
  35. package/dist/chunk-K7VKOLQQ.js +15 -0
  36. package/dist/{chunk-BMEMKKIT.js → chunk-KOHPCX4K.js} +2 -2
  37. package/dist/{chunk-OC7FIQPC.js → chunk-KRPY7NTG.js} +10 -7
  38. package/dist/chunk-LL4KHSZI.js +22 -0
  39. package/dist/{chunk-DSELYM6W.js → chunk-MEQ45TQ4.js} +15 -9
  40. package/dist/{chunk-5UUP6MWO.js → chunk-MV3K5QF2.js} +5 -436
  41. package/dist/{chunk-6SGHMWE3.js → chunk-N4CZJQRK.js} +5 -5
  42. package/dist/{chunk-TZK7PACC.js → chunk-NILBFAPG.js} +14 -8
  43. package/dist/chunk-OZNBF4L3.js +23 -0
  44. package/dist/{verify-375KUB3Y.js → chunk-PCZJO5TI.js} +127 -42
  45. package/dist/chunk-QQK64KLB.js +1360 -0
  46. package/dist/{chunk-SRDMMSEP.js → chunk-QQL5RT5M.js} +979 -1619
  47. package/dist/{chunk-LZSJBIVT.js → chunk-QWU7ZBO7.js} +70 -720
  48. package/dist/{chunk-2TLUCQVG.js → chunk-RD5U66HV.js} +3 -3
  49. package/dist/{chunk-OKGUZO2U.js → chunk-SPULKLCF.js} +4 -3
  50. package/dist/{chunk-OQ33BKR3.js → chunk-TTNYS3EA.js} +3 -60
  51. package/dist/chunk-TW3WDMVS.js +677 -0
  52. package/dist/chunk-TZSKNMZG.js +434 -0
  53. package/dist/{chunk-7MNJORFF.js → chunk-UL3WSD3F.js} +6 -1
  54. package/dist/{chunk-PIWWS5BL.js → chunk-UZHIZC5S.js} +7 -7
  55. package/dist/{chunk-UFIIWP2H.js → chunk-VAWWTKDP.js} +8 -8
  56. package/dist/{chunk-COU2UHX6.js → chunk-VEZEGCGW.js} +170 -2
  57. package/dist/{chunk-LW6DSM3M.js → chunk-VMNQ6OZA.js} +98 -202
  58. package/dist/chunk-VSNATDE6.js +122 -0
  59. package/dist/chunk-W6GROXXM.js +69 -0
  60. package/dist/chunk-WPQLXFOZ.js +375 -0
  61. package/dist/{chunk-ORBHGJC5.js → chunk-WR67VIZY.js} +3 -3
  62. package/dist/{chunk-PAJK6MAQ.js → chunk-X6COSD2O.js} +5 -5
  63. package/dist/chunk-ZGVHUX3M.js +66 -0
  64. package/dist/{chunk-LWLEKMDQ.js → chunk-ZYKPLLNQ.js} +510 -547
  65. package/dist/cli/index.js +27 -23
  66. package/dist/{clio-JOU4FXVA.js → clio-J5JIOIDS.js} +7 -6
  67. package/dist/{code-nav-7AX6FYE6.js → code-nav-AXCXSBHX.js} +5 -3
  68. package/dist/{config-XCDVKR23.js → config-OEBMIN2U.js} +37 -27
  69. package/dist/{configure-4GAP54ZW.js → configure-PUQOSIXQ.js} +16 -13
  70. package/dist/{context-5VKGUVJJ.js → context-EKDCKUUZ.js} +82 -7
  71. package/dist/{context-4UOGGLQ5.js → context-MGSE4Z2T.js} +33 -23
  72. package/dist/{context-77FM5DV5.js → context-URSXPBCK.js} +17 -9
  73. package/dist/{context-clear-XXJRLCJJ.js → context-clear-KDAJRNUK.js} +33 -23
  74. package/dist/context-working-set-SBKMPPI2.js +1552 -0
  75. package/dist/{dispatch-runner-QPRDDBDX.js → dispatch-runner-MSWN72NK.js} +43 -29
  76. package/dist/{doctor-HR46URBJ.js → doctor-7BSE27PJ.js} +10 -10
  77. package/dist/{eval-XSSNATB4.js → eval-IZGDOO4H.js} +9 -8
  78. package/dist/{evidence-6HG2PY2B.js → evidence-SR7WXB5B.js} +51 -23
  79. package/dist/{evolve-K7YU3NCY.js → evolve-K7VE2CBX.js} +30 -20
  80. package/dist/{fleet-VY3HHKN6.js → fleet-7XMJNQNF.js} +48 -38
  81. package/dist/{fleet-preflight-DDN536IT.js → fleet-preflight-AQNAH644.js} +3 -3
  82. package/dist/{init-JYGXI3FK.js → init-JGNPAYXT.js} +41 -31
  83. package/dist/{memory-WFZMGYHX.js → memory-4ALKDJ4Q.js} +32 -22
  84. package/dist/{models-I5QWSEOM.js → models-ZMMLFJNN.js} +22 -19
  85. package/dist/{monitor-GE4ID3IA.js → monitor-2F3T5KHP.js} +55 -43
  86. package/dist/{orchestrator-EM5MC3HM.js → orchestrator-ORHT43JB.js} +572 -388
  87. package/dist/{reset-L2FQEE3E.js → reset-NXGTYNUO.js} +4 -3
  88. package/dist/{run-ZU3QMZPZ.js → run-RF4WJGMT.js} +51 -41
  89. package/dist/{share-S5BZQC5I.js → share-UT3W6E4M.js} +5 -4
  90. package/dist/{skills-X5VXCRNQ.js → skills-PSACKC5Q.js} +2 -2
  91. package/dist/{skills-eval-WKIHWTHR.js → skills-eval-WJSI55RZ.js} +34 -24
  92. package/dist/{targets-SNCPI2NR.js → targets-PIIRAOYS.js} +23 -20
  93. package/dist/{terminal-lease-BNAHVHBS.js → terminal-lease-ULWXWNVY.js} +4 -3
  94. package/dist/{upgrade-JQHHPQ4K.js → upgrade-346TZ6AV.js} +18 -17
  95. package/dist/{usage-OR4O5SMZ.js → usage-6KKXR32N.js} +34 -24
  96. package/dist/verifiers-4UUM6TEE.js +1214 -0
  97. package/dist/verify-X5HDROLA.js +25 -0
  98. package/dist/{wiki-generate-UEXP2ARI.js → wiki-generate-7STOCIFZ.js} +42 -31
  99. package/dist/worker/entry.js +33 -24
  100. package/docs/README.md +7 -6
  101. package/docs/acp.md +1 -1
  102. package/docs/alcf-provider.md +1 -1
  103. package/docs/architecture.md +2 -2
  104. package/docs/artifact-versions.md +1 -1
  105. package/docs/built-in-agents.md +1 -1
  106. package/docs/capacity-and-scheduling.md +1 -1
  107. package/docs/commands-and-modes.md +53 -21
  108. package/docs/config-knobs-audit.md +1 -2
  109. package/docs/configuration-and-targets.md +15 -1
  110. package/docs/context-engine.md +64 -12
  111. package/docs/context-working-set.md +194 -0
  112. package/docs/development-pipeline.md +1 -1
  113. package/docs/documentation-coverage.md +5 -5
  114. package/docs/documentation-guide.md +6 -5
  115. package/docs/environment-variables.md +2 -1
  116. package/docs/eval-runner.md +1 -1
  117. package/docs/evals-internal.md +14 -1
  118. package/docs/evidence-and-memory.md +74 -2
  119. package/docs/evolution.md +1 -1
  120. package/docs/exit-codes-and-output.md +1 -1
  121. package/docs/extensions-and-sharing.md +2 -2
  122. package/docs/fleet-dispatch.md +22 -7
  123. package/docs/glossary.md +21 -1
  124. package/docs/installation-and-lifecycle.md +2 -2
  125. package/docs/middleware-and-components.md +1 -1
  126. package/docs/model-catalog.md +7 -9
  127. package/docs/observability.md +4 -4
  128. package/docs/proactive-memory.md +1 -1
  129. package/docs/prompt-envelope-and-tools.md +4 -4
  130. package/docs/provider-adapter-cookbook.md +1 -1
  131. package/docs/release-cut-checklist.md +31 -31
  132. package/docs/safety-model.md +23 -4
  133. package/docs/scientific-validation.md +21 -3
  134. package/docs/session-lifecycle.md +3 -3
  135. package/docs/skills-marketplace.md +1 -1
  136. package/docs/tool-usage.md +79 -12
  137. package/docs/trace-store.md +1 -1
  138. package/docs/troubleshooting.md +1 -1
  139. package/docs/tui-design.md +1 -1
  140. package/docs/worker-dispatch-mechanics.md +11 -1
  141. package/package.json +8 -11
  142. package/skills/meta/clio-test/SKILL.md +20 -17
  143. package/skills/meta/clio-test/evals.md +3 -3
  144. package/skills/meta/clio-test/references/harness.md +35 -6
  145. package/skills/meta/clio-test/references/test-map.md +20 -10
  146. package/skills/registry.yaml +2 -2
  147. package/skills/skill-marketplace.json +1 -1
  148. package/src/cli/context-working-set.ts +513 -0
  149. package/src/cli/context.ts +8 -0
  150. package/src/cli/evidence.ts +20 -2
  151. package/src/cli/index.ts +4 -0
  152. package/src/cli/verifiers.ts +325 -0
  153. package/src/core/bash-exec.ts +39 -14
  154. package/src/core/bus-events.ts +19 -4
  155. package/src/core/config.ts +54 -0
  156. package/src/core/defaults.ts +50 -3
  157. package/src/core/verification-scripts.ts +6 -0
  158. package/src/domains/agents/builtins/verifier.md +3 -0
  159. package/src/domains/config/classify.ts +1 -0
  160. package/src/domains/context/working-set/contract.ts +161 -0
  161. package/src/domains/context/working-set/defaults.ts +28 -0
  162. package/src/domains/context/working-set/engine.ts +203 -0
  163. package/src/domains/context/working-set/fold.ts +62 -0
  164. package/src/domains/context/working-set/horizon.ts +38 -0
  165. package/src/domains/context/working-set/marker.ts +103 -0
  166. package/src/domains/context/working-set/path-index.ts +436 -0
  167. package/src/domains/context/working-set/payload.ts +152 -0
  168. package/src/domains/context/working-set/policies/age-horizon.ts +55 -0
  169. package/src/domains/context/working-set/policies/index.ts +21 -0
  170. package/src/domains/context/working-set/policies/structural.ts +160 -0
  171. package/src/domains/context/working-set/project.ts +132 -0
  172. package/src/domains/context/working-set/protect.ts +109 -0
  173. package/src/domains/context/working-set/recall.ts +177 -0
  174. package/src/domains/context/working-set/replay/controls.ts +112 -0
  175. package/src/domains/context/working-set/replay/load-clio.ts +199 -0
  176. package/src/domains/context/working-set/replay/metrics.ts +185 -0
  177. package/src/domains/context/working-set/replay/reference-graph.ts +79 -0
  178. package/src/domains/context/working-set/replay/report.ts +139 -0
  179. package/src/domains/context/working-set/replay/runner.ts +325 -0
  180. package/src/domains/context/working-set/replay/synthetic.ts +422 -0
  181. package/src/domains/context/working-set/replay/trace.ts +21 -0
  182. package/src/domains/context/working-set/visible.ts +54 -0
  183. package/src/domains/evidence/build.ts +112 -45
  184. package/src/domains/evidence/eval.ts +24 -7
  185. package/src/domains/evidence/index.ts +53 -0
  186. package/src/domains/evidence/ordering.ts +12 -0
  187. package/src/domains/evidence/run-trust.ts +221 -0
  188. package/src/domains/evidence/store.ts +46 -6
  189. package/src/domains/evidence/trust-status.ts +854 -0
  190. package/src/domains/evidence/types.ts +26 -0
  191. package/src/domains/middleware/memory-intervention.ts +3 -0
  192. package/src/domains/middleware/stalled-turn.ts +165 -4
  193. package/src/domains/safety/autonomy.ts +1 -1
  194. package/src/domains/safety/default-path-policy.ts +8 -0
  195. package/src/domains/safety/finish-contract.ts +4 -3
  196. package/src/domains/safety/policy-engine.ts +48 -6
  197. package/src/domains/session/compaction/compact.ts +23 -1
  198. package/src/domains/session/compaction/cut-point.ts +2 -0
  199. package/src/domains/session/compaction/tokens.ts +16 -1
  200. package/src/domains/session/context-ledger.ts +2 -0
  201. package/src/domains/session/entries.ts +107 -1
  202. package/src/domains/session/manager.ts +9 -2
  203. package/src/domains/session/migrations/index.ts +22 -3
  204. package/src/engine/acp/server.ts +3 -0
  205. package/src/engine/agent.ts +18 -1
  206. package/src/engine/session.ts +9 -3
  207. package/src/entry/orchestrator.ts +16 -4
  208. package/src/interactive/chat-loop-messages.ts +18 -6
  209. package/src/interactive/chat-panel.ts +17 -1
  210. package/src/interactive/chat-renderer.ts +30 -21
  211. package/src/interactive/context-meter.ts +10 -0
  212. package/src/interactive/context-overlay.ts +81 -6
  213. package/src/interactive/context-recall-command.ts +110 -0
  214. package/src/interactive/interactive-slash-runtime.ts +37 -1
  215. package/src/interactive/model-session-replay.ts +21 -0
  216. package/src/interactive/overlay-general-openers.ts +6 -0
  217. package/src/interactive/overlay-session-lifecycle.ts +8 -4
  218. package/src/interactive/renderers/tool-execution.ts +18 -2
  219. package/src/interactive/session-transcript.ts +2 -2
  220. package/src/interactive/slash-commands.ts +29 -2
  221. package/src/interactive/turn-context.ts +238 -88
  222. package/src/interactive/turn-middleware.ts +6 -6
  223. package/src/tools/agent-tools.ts +11 -4
  224. package/src/tools/bash.ts +144 -82
  225. package/src/tools/builtin-tool-catalog.ts +11 -5
  226. package/src/tools/context/index.ts +105 -3
  227. package/src/tools/context/surface.ts +3 -2
  228. package/src/tools/core-bootstrap.ts +21 -0
  229. package/src/tools/dispatch-runner.ts +9 -7
  230. package/src/tools/monitor.ts +28 -20
  231. package/src/tools/registry.ts +59 -7
  232. package/src/tools/result-disposition.ts +550 -0
  233. package/src/tools/result-shaping.ts +262 -19
  234. package/src/tools/safe-exec.ts +2 -0
  235. package/src/tools/verify/authoring.ts +1119 -0
  236. package/src/tools/verify/catalog.ts +346 -0
  237. package/src/tools/verify/index.ts +13 -3
  238. package/src/tools/verify/scripts.ts +135 -37
  239. package/src/tools/verify/surface.ts +9 -5
  240. package/src/tools/worker-evidence.ts +35 -12
  241. package/dist/chunk-J7CWMCQD.js +0 -255
  242. package/dist/chunk-T6YILFSB.js +0 -80
  243. package/dist/chunk-VAKQQHWR.js +0 -434
  244. package/dist/chunk-VPAYEGVX.js +0 -184
@@ -56,6 +56,7 @@ const NEXT_TURN_FIELDS = new Set<string>([
56
56
  "skills",
57
57
  "delegation",
58
58
  "compaction",
59
+ "context",
59
60
  "retry",
60
61
  ]);
61
62
 
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Working-set layer contract.
3
+ *
4
+ * The working set is what the model sees on the next request. The ledger is
5
+ * durable, append-only truth. This layer decides which tool-result bodies and
6
+ * thinking blocks leave the working set, records that decision as
7
+ * `contextEviction` entries, and applies it as an in-memory projection when
8
+ * the replay messages are built. Nothing here rewrites the ledger and nothing
9
+ * here calls a model.
10
+ *
11
+ * Shared by the live engine and the replay-lite runner by construction: a
12
+ * policy is a pure function of `PolicyInput`, so the same selection runs in
13
+ * both places. Widening these types is an owner decision; workers build
14
+ * against them.
15
+ */
16
+
17
+ import type { WorkingSetPolicyId, WorkingSetSettings } from "../../../core/defaults.js";
18
+ import type {
19
+ ContextEvictionEntry,
20
+ ContextRecallEntry,
21
+ EvictedItem,
22
+ EvictionReason,
23
+ EvictionTrigger,
24
+ RecallTrigger,
25
+ SessionEntry,
26
+ WorkingSetRef,
27
+ } from "../../session/entries.js";
28
+
29
+ export { EVICTION_REASONS, EVICTION_TRIGGERS, RECALL_TRIGGERS } from "../../session/entries.js";
30
+ export type {
31
+ ContextEvictionEntry,
32
+ ContextRecallEntry,
33
+ EvictedItem,
34
+ EvictionReason,
35
+ EvictionTrigger,
36
+ RecallTrigger,
37
+ WorkingSetPolicyId,
38
+ WorkingSetRef,
39
+ WorkingSetSettings,
40
+ };
41
+
42
+ /**
43
+ * What the fold knows about one evicted unit. Keyed in `WorkingSetView.evicted`
44
+ * by the entry turnId (`ref.entry`); `fold.ts` owns `refKey` / `parseRefKey`.
45
+ */
46
+ export interface EvictedState {
47
+ reason: EvictionReason;
48
+ marker: string;
49
+ by?: string;
50
+ tokensFreed: number;
51
+ /** turnId of the `contextEviction` entry that evicted it (the latest one, after churn). */
52
+ evictedAtTurnId: string;
53
+ policyId: string;
54
+ }
55
+
56
+ /**
57
+ * The fold of every `contextEviction` / `contextRecall` entry on the active
58
+ * path. A recall does not remove its key: the recalled body lives in the
59
+ * recall tool result at the tail of the working set, the marker stays at the
60
+ * original position so the prefix cache is untouched, and repeated recalls of
61
+ * one ref are the churn signal.
62
+ */
63
+ export interface WorkingSetView {
64
+ evicted: ReadonlyMap<string, EvictedState>;
65
+ /** Applied eviction events on the active path. */
66
+ evictionEvents: number;
67
+ /** Items evicted across all events, including re-evictions after recall. */
68
+ itemsEvicted: number;
69
+ /** Recall entries on the active path. `churn = recalls / itemsEvicted`. */
70
+ recalls: number;
71
+ /** Policy that produced the most recent event; null when none. */
72
+ lastPolicyId: string | null;
73
+ /** turnId of the most recent eviction event; null when none. */
74
+ lastEvictionTurnId: string | null;
75
+ }
76
+
77
+ export const EMPTY_WORKING_SET_VIEW: WorkingSetView = Object.freeze({
78
+ evicted: new Map<string, EvictedState>(),
79
+ evictionEvents: 0,
80
+ itemsEvicted: 0,
81
+ recalls: 0,
82
+ lastPolicyId: null,
83
+ lastEvictionTurnId: null,
84
+ });
85
+
86
+ export interface PressureInput {
87
+ /** Estimated tokens in the current working set (projected), same estimator as the live pressure check. */
88
+ tokens: number;
89
+ contextWindow: number;
90
+ /** `compaction.threshold`. */
91
+ threshold: number;
92
+ /** `context.workingSet.target`: the ratio an applied event batches down to. */
93
+ target: number;
94
+ }
95
+
96
+ /**
97
+ * Everything a policy may look at. `entries` are the active-path entries the
98
+ * model can currently see: after the latest `compactionSummary` cut, in ledger
99
+ * order, as `selectVisibleEntries` in visible.ts produces them. They are NOT
100
+ * projected: a policy must consult `view.evicted` to skip units that are
101
+ * already out. The view is folded over the full active path, so a ref evicted
102
+ * before a later compaction is still known. Token counts enter selection only
103
+ * through `settings.minEvictableTokens` and the headroom arithmetic against
104
+ * `pressure.target`; no rule may rank candidates by size or recency score.
105
+ */
106
+ export interface PolicyInput {
107
+ entries: ReadonlyArray<SessionEntry>;
108
+ view: WorkingSetView;
109
+ /** Session working directory for the path index; null when unknown (paths stay relative). */
110
+ cwd: string | null;
111
+ settings: WorkingSetSettings;
112
+ pressure: PressureInput;
113
+ /** chars/4 estimator shared with `context-accounting.ts`, so replay and live agree. */
114
+ estimateTokens: (entry: SessionEntry) => number;
115
+ }
116
+
117
+ /** A unit the policy wants out, with the typed reason. Ordered: apply in this order, stop when headroom is met. */
118
+ export interface EvictionCandidate {
119
+ ref: WorkingSetRef;
120
+ reason: EvictionReason;
121
+ by?: string;
122
+ }
123
+
124
+ export interface WorkingSetPolicy {
125
+ readonly id: WorkingSetPolicyId;
126
+ /**
127
+ * Select candidates. Must be pure and deterministic for a given input.
128
+ * Returns an empty array when nothing qualifies. Units already in
129
+ * `input.view.evicted` must not be returned.
130
+ */
131
+ select(input: PolicyInput): ReadonlyArray<EvictionCandidate>;
132
+ }
133
+
134
+ /** Materialized selection: markers rendered, tokens estimated, ready to become a ledger entry. */
135
+ export interface EvictionPlan {
136
+ policyId: WorkingSetPolicyId;
137
+ items: ReadonlyArray<EvictedItem>;
138
+ tokensBefore: number;
139
+ tokensAfter: number;
140
+ }
141
+
142
+ /** Fields the caller adds when appending the plan as a ledger entry. */
143
+ export type ContextEvictionFields = Omit<ContextEvictionEntry, "turnId" | "parentTurnId" | "timestamp">;
144
+ export type ContextRecallFields = Omit<ContextRecallEntry, "turnId" | "parentTurnId" | "timestamp">;
145
+
146
+ /** Typed failure for recall by ref. `recallErrorMessage` lists the refs that are evicted beside it. */
147
+ export type RecallError =
148
+ | { kind: "not_on_active_path"; ref: string }
149
+ | { kind: "not_evicted"; ref: string }
150
+ | { kind: "invalid_ref"; ref: string };
151
+
152
+ export interface RecallResult {
153
+ ref: WorkingSetRef;
154
+ /** The ledger entry whose body is readmitted. */
155
+ entry: SessionEntry;
156
+ /** Exact original body as the projection would have rendered it before eviction. */
157
+ body: string;
158
+ tokens: number;
159
+ /** Present when the original result was offloaded; recall returns the pointer, never the file. */
160
+ offloadPath?: string;
161
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Working-set settings: user-visible defaults. The structural type lives in
3
+ * `src/core/defaults.ts` beside the rest of the settings tree so core stays
4
+ * free of a backward domain dependency; this module pairs it with the value
5
+ * the DEFAULT_SETTINGS tree and the engine read at runtime.
6
+ *
7
+ * `structural-v1` is the default: typed path-keyed rules first, the age rule
8
+ * last and batched to `target`. On the reproducible 24-trace procedural grid,
9
+ * it meets the recorded default rule at 32k, 64k, and 128k: retention is no
10
+ * lower than `age-horizon`, precision is higher than random, and the target
11
+ * stop remains active above 32k. `age-horizon` stays available as the exact
12
+ * pre-layer selection recorded through the ledger.
13
+ */
14
+
15
+ import type { WorkingSetSettings } from "../../../core/defaults.js";
16
+
17
+ export type { WorkingSetPolicyId, WorkingSetSettings } from "../../../core/defaults.js";
18
+
19
+ export const DEFAULT_WORKING_SET_SETTINGS: WorkingSetSettings = {
20
+ enabled: true,
21
+ policy: "structural-v1",
22
+ target: 0.6,
23
+ protectLastTurns: 6,
24
+ // The procedural floor sweep found marker break-even near 50 tokens. A zero
25
+ // floor saved only 0.167 summaries at 64k and none at 128k while reducing
26
+ // covered retention by 0.0076 and 0.0237. Keep 200 as the churn guard.
27
+ minEvictableTokens: 200,
28
+ };
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Turn a policy's selection into something the session can append.
3
+ *
4
+ * `planEviction` is the whole decision: it asks the policy what should leave,
5
+ * materializes each candidate into an `EvictedItem` (marker rendered, tokens
6
+ * measured), and prices the result against the projection the model will
7
+ * actually receive. It writes nothing and calls no model, so the live engine
8
+ * and the replay-lite runner drive it identically.
9
+ *
10
+ * `buildEvictionFields` is the boring half: the plan plus the trigger facts,
11
+ * shaped as the ledger entry minus the three fields the session owns
12
+ * (`turnId`, `parentTurnId`, `timestamp`).
13
+ */
14
+
15
+ import type { EvictedItem, SessionEntry } from "../../session/entries.js";
16
+ import type {
17
+ ContextEvictionFields,
18
+ EvictedState,
19
+ EvictionCandidate,
20
+ EvictionPlan,
21
+ EvictionTrigger,
22
+ PolicyInput,
23
+ WorkingSetPolicy,
24
+ WorkingSetView,
25
+ } from "./contract.js";
26
+ import { refKey } from "./fold.js";
27
+ import { renderMarker } from "./marker.js";
28
+ import { callPathsByToolCallId } from "./path-index.js";
29
+ import { hasThinking, offloadPathOf, primaryPathOf, toolResultPayload, toolResultText } from "./payload.js";
30
+ import { projectWorkingSet } from "./project.js";
31
+
32
+ /** Paths of the tool calls whose results may be evicted: `callPathsByToolCallId` over the policy input. */
33
+ export type CallPaths = ReadonlyMap<string, string>;
34
+ const NO_CALL_PATHS: CallPaths = new Map();
35
+
36
+ /**
37
+ * The event has no turnId until `session.appendEntry` gives it one, and the
38
+ * projection reads only `reason` and `marker`, so plan-time states carry this
39
+ * placeholder rather than a fabricated id.
40
+ */
41
+ const PENDING_EVENT_TURN_ID = "";
42
+
43
+ /**
44
+ * The stub that replaces this unit's body, or null when there is nothing to
45
+ * evict. Thinking eviction renders no marker at all: the reasoning simply
46
+ * stops being replayed.
47
+ */
48
+ function markerFor(entry: SessionEntry, candidate: EvictionCandidate, callPaths: CallPaths): string | null {
49
+ if (entry.kind !== "message") return null;
50
+ if (entry.role === "assistant") return hasThinking(entry.payload) ? "" : null;
51
+ if (entry.role !== "tool_result") return null;
52
+ const payload = toolResultPayload(entry.payload);
53
+ const toolCallId = typeof payload.obj.toolCallId === "string" ? payload.obj.toolCallId : undefined;
54
+ return renderMarker({
55
+ ref: candidate.ref,
56
+ reason: candidate.reason,
57
+ by: candidate.by,
58
+ toolName: payload.toolName,
59
+ text: toolResultText(payload.result),
60
+ offloadPath: offloadPathOf(payload),
61
+ path: primaryPathOf(payload) ?? (toolCallId === undefined ? undefined : callPaths.get(toolCallId)),
62
+ });
63
+ }
64
+
65
+ function pendingState(candidate: EvictionCandidate, marker: string, policyId: string): EvictedState {
66
+ return {
67
+ reason: candidate.reason,
68
+ marker,
69
+ ...(candidate.by === undefined ? {} : { by: candidate.by }),
70
+ tokensFreed: 0,
71
+ evictedAtTurnId: PENDING_EVENT_TURN_ID,
72
+ policyId,
73
+ };
74
+ }
75
+
76
+ /** A view holding exactly one item, for pricing that item on its own. */
77
+ function soloView(key: string, state: EvictedState): WorkingSetView {
78
+ return {
79
+ evicted: new Map([[key, state]]),
80
+ // Zero events: pricing one body must not also stamp usage invalidation,
81
+ // which would put the cost of a different mechanism in this item's total.
82
+ evictionEvents: 0,
83
+ itemsEvicted: 1,
84
+ recalls: 0,
85
+ lastPolicyId: null,
86
+ lastEvictionTurnId: null,
87
+ };
88
+ }
89
+
90
+ /**
91
+ * The view this plan would produce. `evictionEvents` and `lastEvictionTurnId`
92
+ * stay where they were on purpose: both totals are then measured under the same
93
+ * usage-invalidation state, so `tokensBefore - tokensAfter` is exactly the
94
+ * bodies this event removes and nothing else.
95
+ */
96
+ function viewWithItems(view: WorkingSetView, items: ReadonlyArray<EvictedItem>, policyId: string): WorkingSetView {
97
+ const evicted = new Map(view.evicted);
98
+ for (const item of items) {
99
+ evicted.set(refKey(item.ref), {
100
+ reason: item.reason,
101
+ marker: item.marker,
102
+ ...(item.by === undefined ? {} : { by: item.by }),
103
+ tokensFreed: item.tokensFreed,
104
+ evictedAtTurnId: PENDING_EVENT_TURN_ID,
105
+ policyId,
106
+ });
107
+ }
108
+ return { ...view, evicted, itemsEvicted: view.itemsEvicted + items.length };
109
+ }
110
+
111
+ function sumTokens(entries: ReadonlyArray<SessionEntry>, estimate: (entry: SessionEntry) => number): number {
112
+ let total = 0;
113
+ for (const entry of entries) total += estimate(entry);
114
+ return total;
115
+ }
116
+
117
+ /**
118
+ * What one candidate takes out of the working set: the entry as it stands now
119
+ * minus the entry as the projection would render it. Zero when the candidate
120
+ * does not apply to the entry, and never negative, because a marker longer than
121
+ * the body it replaces is a bad trade, not a negative saving.
122
+ *
123
+ * Exported so a policy can do headroom arithmetic (`structural-v1` rung 6 needs
124
+ * to know when to stop) against the same numbers `planEviction` will record.
125
+ * A policy that priced evictions its own way would report headroom the ledger
126
+ * then contradicts. Pass the same `callPaths` the plan will use, or the marker
127
+ * priced here is a few bytes shorter than the one recorded.
128
+ */
129
+ export function tokensFreedByEviction(
130
+ estimateTokens: (entry: SessionEntry) => number,
131
+ entry: SessionEntry,
132
+ candidate: EvictionCandidate,
133
+ callPaths: CallPaths = NO_CALL_PATHS,
134
+ ): number {
135
+ const marker = markerFor(entry, candidate, callPaths);
136
+ if (marker === null) return 0;
137
+ const key = refKey(candidate.ref);
138
+ const projected = projectWorkingSet([entry], soloView(key, pendingState(candidate, marker, "")))[0] ?? entry;
139
+ return Math.max(0, estimateTokens(entry) - estimateTokens(projected));
140
+ }
141
+
142
+ export function planEviction(policy: WorkingSetPolicy, input: PolicyInput): EvictionPlan | null {
143
+ const candidates = policy.select(input);
144
+ if (candidates.length === 0) return null;
145
+
146
+ const byTurnId = new Map<string, SessionEntry>();
147
+ for (const entry of input.entries) byTurnId.set(entry.turnId, entry);
148
+ const callPaths = callPathsByToolCallId(input.entries);
149
+
150
+ const items: EvictedItem[] = [];
151
+ const claimed = new Set<string>();
152
+ for (const candidate of candidates) {
153
+ const key = refKey(candidate.ref);
154
+ // A policy is contractually forbidden from returning a unit that is
155
+ // already out, and a duplicate inside one selection would double-count
156
+ // the tokens it frees. Both are cheap to refuse here.
157
+ if (input.view.evicted.has(key) || claimed.has(key)) continue;
158
+ const entry = byTurnId.get(key);
159
+ if (entry === undefined) continue;
160
+ const marker = markerFor(entry, candidate, callPaths);
161
+ if (marker === null) continue;
162
+ // A marker at least as long as the body it replaces is a cold turn bought
163
+ // for nothing, whatever the policy's reason. Refused here so no policy can
164
+ // record an eviction that freed nothing.
165
+ const tokensFreed = tokensFreedByEviction(input.estimateTokens, entry, candidate, callPaths);
166
+ if (tokensFreed <= 0) continue;
167
+ claimed.add(key);
168
+ items.push({
169
+ ref: candidate.ref,
170
+ reason: candidate.reason,
171
+ tokensFreed,
172
+ marker,
173
+ ...(candidate.by === undefined ? {} : { by: candidate.by }),
174
+ });
175
+ }
176
+ if (items.length === 0) return null;
177
+
178
+ return {
179
+ policyId: policy.id,
180
+ items,
181
+ tokensBefore: sumTokens(projectWorkingSet(input.entries, input.view), input.estimateTokens),
182
+ tokensAfter: sumTokens(
183
+ projectWorkingSet(input.entries, viewWithItems(input.view, items, policy.id)),
184
+ input.estimateTokens,
185
+ ),
186
+ };
187
+ }
188
+
189
+ export function buildEvictionFields(
190
+ plan: EvictionPlan,
191
+ meta: { trigger: EvictionTrigger; pressureBefore: number | null; snapshotIdBefore: string | null },
192
+ ): ContextEvictionFields {
193
+ return {
194
+ kind: "contextEviction",
195
+ policyId: plan.policyId,
196
+ trigger: meta.trigger,
197
+ evicted: plan.items,
198
+ tokensBefore: plan.tokensBefore,
199
+ tokensAfter: plan.tokensAfter,
200
+ pressureBefore: meta.pressureBefore,
201
+ snapshotIdBefore: meta.snapshotIdBefore,
202
+ };
203
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Fold the working-set ledger records on the active path into a view.
3
+ *
4
+ * Pure over entries. The active path is selected here, through
5
+ * `filterEntriesToActivePath`, so every consumer (live projection, recall,
6
+ * overlay, replay-lite) shares the same #94 discipline: after a `/tree` switch
7
+ * the raw file still holds abandoned turns, and an eviction recorded on an
8
+ * abandoned branch must not project onto the live one. Forks inherit the view
9
+ * of their shared prefix for the same reason compaction summaries do.
10
+ */
11
+
12
+ import type { SessionEntry } from "../../session/entries.js";
13
+ import { filterEntriesToActivePath } from "../../session/tree/active-path.js";
14
+ import type { EvictedState, WorkingSetRef, WorkingSetView } from "./contract.js";
15
+
16
+ /** Ref keys index `WorkingSetView.evicted`. Today a key is the entry turnId. */
17
+ export function refKey(ref: WorkingSetRef): string {
18
+ return ref.entry;
19
+ }
20
+
21
+ export function parseRefKey(key: string): WorkingSetRef | null {
22
+ const trimmed = key.trim();
23
+ if (trimmed.length === 0 || /\s/.test(trimmed)) return null;
24
+ return { entry: trimmed };
25
+ }
26
+
27
+ export function foldWorkingSet(entries: ReadonlyArray<SessionEntry>, activeLeafTurnId?: string): WorkingSetView {
28
+ const active = filterEntriesToActivePath(entries, activeLeafTurnId);
29
+ const evicted = new Map<string, EvictedState>();
30
+ let evictionEvents = 0;
31
+ let itemsEvicted = 0;
32
+ let recalls = 0;
33
+ let lastPolicyId: string | null = null;
34
+ let lastEvictionTurnId: string | null = null;
35
+ for (const entry of active) {
36
+ if (entry.kind === "contextEviction") {
37
+ evictionEvents += 1;
38
+ lastPolicyId = entry.policyId;
39
+ lastEvictionTurnId = entry.turnId;
40
+ for (const item of entry.evicted) {
41
+ itemsEvicted += 1;
42
+ evicted.set(refKey(item.ref), {
43
+ reason: item.reason,
44
+ marker: item.marker,
45
+ ...(item.by === undefined ? {} : { by: item.by }),
46
+ tokensFreed: item.tokensFreed,
47
+ evictedAtTurnId: entry.turnId,
48
+ policyId: entry.policyId,
49
+ });
50
+ }
51
+ continue;
52
+ }
53
+ // A recall does not un-evict. The recalled body rides the recall tool
54
+ // result at the tail of the working set, which is where the model asked
55
+ // for it and where it costs no cold prefix; readmitting it at the
56
+ // original position would duplicate the bytes and invalidate the cache
57
+ // for everything after it. The marker stays, byte-stable, and a second
58
+ // recall of the same ref is the churn signal.
59
+ if (entry.kind === "contextRecall") recalls += 1;
60
+ }
61
+ return { evicted, evictionEvents, itemsEvicted, recalls, lastPolicyId, lastEvictionTurnId };
62
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The protection horizon: where the recent, untouchable window begins.
3
+ *
4
+ * Both policies and every protection predicate answer "is this entry inside the
5
+ * last `protectLastTurns` turns" the same way, so the arithmetic lives here
6
+ * once. It is the same cutoff `maskStaleObservations` used before this layer
7
+ * existed, which is what keeps `age-horizon` selection-identical to the
8
+ * destructive stage it replaced.
9
+ */
10
+
11
+ import type { SessionEntry } from "../../session/entries.js";
12
+
13
+ /**
14
+ * What starts a turn, in the sense the protection horizon counts. A local `!`
15
+ * bash execution and a branch summary both open a new stretch of work the same
16
+ * way an operator message does.
17
+ */
18
+ export function isTurnStart(entry: SessionEntry): boolean {
19
+ if (entry.kind === "bashExecution" || entry.kind === "branchSummary") return true;
20
+ return entry.kind === "message" && entry.role === "user";
21
+ }
22
+
23
+ /**
24
+ * Index of the first protected entry: walk back until `protectLastTurns` turn
25
+ * starts have been seen. Entries before it are candidates, entries from it on
26
+ * are the recent window nothing touches.
27
+ */
28
+ export function protectionCutoffIndex(entries: ReadonlyArray<SessionEntry>, protectLastTurns: number): number {
29
+ const horizon = Math.max(1, Math.floor(protectLastTurns));
30
+ let seen = 0;
31
+ for (let i = entries.length - 1; i >= 0; i -= 1) {
32
+ const entry = entries[i];
33
+ if (!entry || !isTurnStart(entry)) continue;
34
+ seen += 1;
35
+ if (seen >= horizon) return i;
36
+ }
37
+ return 0;
38
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The one-line stub the projection renders in place of an evicted tool-result
3
+ * body.
4
+ *
5
+ * Byte-stable by construction: same input, same bytes, forever. No timestamp,
6
+ * no counter, no `Date.now()`. Two reasons. The marker is persisted inside the
7
+ * `contextEviction` entry and replayed by the projection on every request, so a
8
+ * marker that changed between renders would invalidate the prompt cache on a
9
+ * turn that evicted nothing new. And replay-lite reruns the same policy over
10
+ * recorded ledgers; a marker carrying wall-clock state would make two runs of
11
+ * the same trace disagree.
12
+ *
13
+ * The field order is fixed (ref, reason, by, tool, path, size, offload,
14
+ * recall, then the body tail) so a diff between two markers is readable and so
15
+ * a model reading many of them sees the same shape every time. Undefined
16
+ * fields are omitted rather than rendered empty. `recall` spells out the exact
17
+ * tool call that brings the body back, which is the only affordance the model
18
+ * has once the body is gone.
19
+ *
20
+ * The body tail is `preview` for every reason but one. A `failure_resolved`
21
+ * eviction renders `first_line` instead: failures are evidence, and the line
22
+ * that says what failed is the part worth a marker's tokens, where a preview
23
+ * of a stack trace is not.
24
+ */
25
+
26
+ import { formatSize } from "../../../engine/truncate.js";
27
+ import type { EvictionReason, WorkingSetRef } from "./contract.js";
28
+
29
+ /** Characters of the original body the marker keeps as a preview. */
30
+ const PREVIEW_LIMIT = 120;
31
+
32
+ export interface MarkerInput {
33
+ ref: WorkingSetRef;
34
+ reason: EvictionReason;
35
+ /** Ref key of the entry that superseded or resolved this one, when the reason names one. */
36
+ by?: string | undefined;
37
+ toolName: string;
38
+ /** The body leaving the working set; drives size and preview. */
39
+ text: string;
40
+ /** Set when the original result was offloaded to scratch. Replaces the preview. */
41
+ offloadPath?: string | undefined;
42
+ /** Primary file the result was about, when the payload names exactly one. */
43
+ path?: string | undefined;
44
+ }
45
+
46
+ /** Lines as `split(/\r\n|\r|\n/)` would count them, without materializing them. */
47
+ function lineCount(text: string): number {
48
+ if (text.length === 0) return 0;
49
+ let count = 1;
50
+ for (let index = 0; index < text.length; index += 1) {
51
+ const code = text.charCodeAt(index);
52
+ if (code === 10 || (code === 13 && text.charCodeAt(index + 1) !== 10)) count += 1;
53
+ }
54
+ return count;
55
+ }
56
+
57
+ /**
58
+ * First `PREVIEW_LIMIT` characters with whitespace collapsed to single spaces
59
+ * and double quotes escaped, so the preview never breaks the quoted field or
60
+ * spills onto a second line. Bounded: the body is scanned only as far as the
61
+ * preview reaches, because the marker is priced once per candidate on every
62
+ * eviction and a body-length collapse was most of that price.
63
+ */
64
+ function preview(text: string): string {
65
+ let out = "";
66
+ let pendingSpace = false;
67
+ for (const char of text) {
68
+ if (/\s/.test(char)) {
69
+ pendingSpace = out.length > 0;
70
+ continue;
71
+ }
72
+ if (pendingSpace) out += " ";
73
+ pendingSpace = false;
74
+ out += char;
75
+ if (out.length >= PREVIEW_LIMIT) break;
76
+ }
77
+ return out.slice(0, PREVIEW_LIMIT).replace(/"/g, '\\"');
78
+ }
79
+
80
+ /** The first line that says anything, bounded and escaped like a preview. */
81
+ function firstLine(text: string): string {
82
+ const line = /[^\r\n]*\S[^\r\n]*/.exec(text)?.[0] ?? "";
83
+ return line.trim().slice(0, PREVIEW_LIMIT).replace(/"/g, '\\"');
84
+ }
85
+
86
+ export function renderMarker(input: MarkerInput): string {
87
+ const ref = input.ref.entry;
88
+ const fields: string[] = [`ref=${ref}`, `reason=${input.reason}`];
89
+ if (input.by !== undefined) fields.push(`by=${input.by}`);
90
+ fields.push(`tool=${input.toolName}`);
91
+ if (input.path !== undefined) fields.push(`path=${input.path}`);
92
+ fields.push(`size=${lineCount(input.text)} lines/${formatSize(Buffer.byteLength(input.text, "utf8"))}`);
93
+ if (input.offloadPath !== undefined) fields.push(`offload=${input.offloadPath}`);
94
+ fields.push(`recall=context(scope="recall", ref="${ref}")`);
95
+ // An offloaded body is one `read` away at a stable path; a preview of it
96
+ // would spend tokens repeating what the pointer already promises.
97
+ if (input.offloadPath === undefined) {
98
+ const failed = input.reason === "failure_resolved";
99
+ const tail = failed ? firstLine(input.text) : preview(input.text);
100
+ if (tail.length > 0) fields.push(`${failed ? "first_line" : "preview"}="${tail}"`);
101
+ }
102
+ return `[evicted ${fields.join(" ")}]`;
103
+ }