aegisone 1.0.34 → 1.0.35

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 (226) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +1 -1
  3. package/SOURCE-OFFER.txt +9 -0
  4. package/bin/aegis.js +1 -322
  5. package/package.json +4 -5
  6. package/scripts/bump.mjs +1 -211
  7. package/scripts/bundle.js +1 -129
  8. package/scripts/predist.mjs +1 -976
  9. package/src/child.js +1 -285
  10. package/src/dispatch.js +1 -269
  11. package/src/doctor.js +1 -639
  12. package/src/host.js +1 -111
  13. package/src/permissions.js +1 -383
  14. package/src/probes.js +1 -159
  15. package/src/release.js +1 -409
  16. package/vendor/cli/package.json +3 -7
  17. package/vendor/cli/scripts/check.mjs +1 -52
  18. package/vendor/cli/scripts/demo.mjs +1 -94
  19. package/vendor/cli/scripts/predist.mjs +1 -75
  20. package/vendor/cli/scripts/runtests.mjs +1 -56
  21. package/vendor/cli/scripts/seal-gate.mjs +1 -74
  22. package/vendor/cli/src/agents.js +1 -139
  23. package/vendor/cli/src/app.js +1 -2957
  24. package/vendor/cli/src/art.js +1 -289
  25. package/vendor/cli/src/chatflow.js +1 -3023
  26. package/vendor/cli/src/checkpoint.js +1 -90
  27. package/vendor/cli/src/clipboard.js +1 -424
  28. package/vendor/cli/src/cloudsync.js +1 -528
  29. package/vendor/cli/src/commands.js +1 -4089
  30. package/vendor/cli/src/config.js +1 -581
  31. package/vendor/cli/src/connect.js +1 -349
  32. package/vendor/cli/src/credentials.js +1 -33
  33. package/vendor/cli/src/deps.js +1 -232
  34. package/vendor/cli/src/devrun.js +1 -187
  35. package/vendor/cli/src/diff.js +1 -333
  36. package/vendor/cli/src/diffview.js +1 -296
  37. package/vendor/cli/src/engine.js +1 -297
  38. package/vendor/cli/src/entitlement.js +1 -355
  39. package/vendor/cli/src/events.js +1 -707
  40. package/vendor/cli/src/export.js +1 -67
  41. package/vendor/cli/src/filepreview.js +1 -153
  42. package/vendor/cli/src/fingerprint.js +1 -592
  43. package/vendor/cli/src/format.js +1 -259
  44. package/vendor/cli/src/fuzzy.js +1 -119
  45. package/vendor/cli/src/history.js +1 -423
  46. package/vendor/cli/src/init.js +1 -175
  47. package/vendor/cli/src/input.js +1 -139
  48. package/vendor/cli/src/keys.js +1 -183
  49. package/vendor/cli/src/markdown.js +1 -306
  50. package/vendor/cli/src/models.js +1 -507
  51. package/vendor/cli/src/naming.js +1 -57
  52. package/vendor/cli/src/overlays.js +1 -462
  53. package/vendor/cli/src/panels.js +1 -1418
  54. package/vendor/cli/src/ping.js +1 -226
  55. package/vendor/cli/src/presence.js +1 -238
  56. package/vendor/cli/src/render.js +1 -516
  57. package/vendor/cli/src/screen.js +1 -518
  58. package/vendor/cli/src/screens.js +1 -896
  59. package/vendor/cli/src/secret.js +1 -59
  60. package/vendor/cli/src/securefs.js +1 -359
  61. package/vendor/cli/src/settings.js +1 -155
  62. package/vendor/cli/src/shared.js +1 -52
  63. package/vendor/cli/src/sharedpaths.js +1 -53
  64. package/vendor/cli/src/signup.js +1 -156
  65. package/vendor/cli/src/summarize.js +1 -95
  66. package/vendor/cli/src/sysdiag.js +1 -585
  67. package/vendor/cli/src/system.js +1 -40
  68. package/vendor/cli/src/theme.js +1 -570
  69. package/vendor/cli/src/title.js +1 -220
  70. package/vendor/cli/src/todos.js +1 -115
  71. package/vendor/cli/src/tokens.js +1 -263
  72. package/vendor/cli/src/topup.js +1 -210
  73. package/vendor/cli/src/update.js +1 -191
  74. package/vendor/cli/vendor/client/aegis.js +1 -2441
  75. package/vendor/cli/vendor/client/attest.js +1 -518
  76. package/vendor/cli/vendor/client/authorship-key.js +1 -410
  77. package/vendor/cli/vendor/client/auto-provision.js +1 -134
  78. package/vendor/cli/vendor/client/brain-catalog.js +1 -268
  79. package/vendor/cli/vendor/client/byok-catalog.js +1 -199
  80. package/vendor/cli/vendor/client/catalog-client.js +1 -487
  81. package/vendor/cli/vendor/client/conversation-export.js +1 -177
  82. package/vendor/cli/vendor/client/credentials.js +1 -494
  83. package/vendor/cli/vendor/client/device-fingerprint.js +1 -141
  84. package/vendor/cli/vendor/client/drive-connect.js +1 -110
  85. package/vendor/cli/vendor/client/drive-upload.js +1 -234
  86. package/vendor/cli/vendor/client/env-file.js +1 -1273
  87. package/vendor/cli/vendor/client/fingerprint.js +1 -1064
  88. package/vendor/cli/vendor/client/foreign-memory.js +1 -669
  89. package/vendor/cli/vendor/client/google-signin.js +1 -133
  90. package/vendor/cli/vendor/client/hooks.js +1 -430
  91. package/vendor/cli/vendor/client/model-discovery.js +1 -373
  92. package/vendor/cli/vendor/client/naming.js +1 -283
  93. package/vendor/cli/vendor/client/platform.js +1 -937
  94. package/vendor/cli/vendor/client/prefs.js +1 -351
  95. package/vendor/cli/vendor/client/presence-constants.js +1 -107
  96. package/vendor/cli/vendor/client/presence.js +1 -438
  97. package/vendor/cli/vendor/client/record-vault.js +3 -1055
  98. package/vendor/cli/vendor/client/session-store.js +1 -575
  99. package/vendor/cli/vendor/client/topup.js +1 -297
  100. package/vendor/cli/vendor/client/touch.js +1 -458
  101. package/vendor/cli/vendor/client/update.js +1 -763
  102. package/vendor/cli/vendor/client/user-models.js +1 -381
  103. package/vendor/cli/vendor/desktop/lib/local/agents.js +1 -105
  104. package/vendor/cli/vendor/desktop/lib/local/attachments.js +1 -276
  105. package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1045
  106. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -1028
  107. package/vendor/cli/vendor/desktop/lib/local/context.js +1 -125
  108. package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -3666
  109. package/vendor/cli/vendor/desktop/lib/local/entitlement.js +1 -660
  110. package/vendor/cli/vendor/desktop/lib/local/git-scope.js +1 -228
  111. package/vendor/cli/vendor/desktop/lib/local/local.js +1 -2145
  112. package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -280
  113. package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -560
  114. package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -238
  115. package/vendor/cli/vendor/desktop/lib/local/spill.js +1 -75
  116. package/vendor/cli/vendor/desktop/lib/local/turn-guard.js +1 -300
  117. package/vendor/cli/vendor/desktop/lib/local/worktree-lock.js +1 -235
  118. package/vendor/cli/vendor/desktop/lib/settings.js +1 -890
  119. package/vendor/cli/vendor/desktop/renderer/usage.js +1 -1239
  120. package/vendor/cli/vendor/hosts/install.js +1 -638
  121. package/vendor/cli/vendor/hosts/openai-shim.js +1 -515
  122. package/vendor/cli/vendor/hosts/spec.js +1 -164
  123. package/vendor/cli/vendor/hosts/targets.js +1 -346
  124. package/vendor/cli/vendor/mcp/server.js +1 -221
  125. package/vendor/cli/vendor/mcp/tools.js +1 -408
  126. package/vendor/cli/vendor/one/src/child.js +1 -285
  127. package/vendor/cli/vendor/one/src/permissions.js +1 -383
  128. package/vendor/desktop/bin/aegis.js +1 -32
  129. package/vendor/desktop/lib/account-key.js +1 -56
  130. package/vendor/desktop/lib/auto.js +1 -461
  131. package/vendor/desktop/lib/avatar/companion.js +1 -698
  132. package/vendor/desktop/lib/avatar/cosmetics.js +1 -532
  133. package/vendor/desktop/lib/avatar/events.js +1 -344
  134. package/vendor/desktop/lib/avatar/holder-turn.js +1 -350
  135. package/vendor/desktop/lib/avatar/holders.js +1 -612
  136. package/vendor/desktop/lib/avatar/identity.js +1 -641
  137. package/vendor/desktop/lib/avatar/level.js +1 -368
  138. package/vendor/desktop/lib/avatar/mirror.js +1 -524
  139. package/vendor/desktop/lib/avatar/persona.js +1 -388
  140. package/vendor/desktop/lib/avatar/profile.js +1 -977
  141. package/vendor/desktop/lib/avatar/register.js +1 -307
  142. package/vendor/desktop/lib/avatar/store.js +1 -472
  143. package/vendor/desktop/lib/avatar/tiers.js +1 -38
  144. package/vendor/desktop/lib/avatar/transfer.js +1 -415
  145. package/vendor/desktop/lib/avatar/turn.js +1 -216
  146. package/vendor/desktop/lib/avatar/voice.js +1 -328
  147. package/vendor/desktop/lib/avatar/xp.js +1 -404
  148. package/vendor/desktop/lib/deep-link.js +1 -120
  149. package/vendor/desktop/lib/fingerprint.js +2 -347
  150. package/vendor/desktop/lib/google-connect.js +1 -323
  151. package/vendor/desktop/lib/local/agents.js +1 -105
  152. package/vendor/desktop/lib/local/attachments.js +1 -276
  153. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  154. package/vendor/desktop/lib/local/claude-pro.js +1 -1028
  155. package/vendor/desktop/lib/local/context.js +1 -125
  156. package/vendor/desktop/lib/local/engine.js +1 -3666
  157. package/vendor/desktop/lib/local/entitlement.js +1 -660
  158. package/vendor/desktop/lib/local/git-scope.js +1 -228
  159. package/vendor/desktop/lib/local/local.js +1 -2145
  160. package/vendor/desktop/lib/local/prompt.js +1 -280
  161. package/vendor/desktop/lib/local/queue.js +1 -560
  162. package/vendor/desktop/lib/local/session-rounds.js +1 -238
  163. package/vendor/desktop/lib/local/spill.js +1 -75
  164. package/vendor/desktop/lib/local/telemetry.js +1 -470
  165. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  166. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  167. package/vendor/desktop/lib/provider-key-mirror.js +1 -252
  168. package/vendor/desktop/lib/quick-launcher.js +1 -84
  169. package/vendor/desktop/lib/settings.js +1 -890
  170. package/vendor/desktop/lib/sync/memory-queue.js +1 -60
  171. package/vendor/desktop/lib/sync/persist-gate.js +1 -148
  172. package/vendor/desktop/lib/sync/sessions.js +1 -106
  173. package/vendor/desktop/lib/window-state.js +1 -66
  174. package/vendor/desktop/package.json +2 -4
  175. package/vendor/desktop/preload.js +1 -679
  176. package/vendor/desktop/renderer/app.js +1 -7250
  177. package/vendor/desktop/renderer/avatar/assemble.js +1 -335
  178. package/vendor/desktop/renderer/avatar/avatar.js +1 -380
  179. package/vendor/desktop/renderer/avatar/cards.js +1 -141
  180. package/vendor/desktop/renderer/avatar/hud.js +1 -206
  181. package/vendor/desktop/renderer/avatar/machine.js +1 -213
  182. package/vendor/desktop/renderer/avatar/pane.js +1 -439
  183. package/vendor/desktop/renderer/avatar/parts.js +1 -437
  184. package/vendor/desktop/renderer/budget.js +1 -172
  185. package/vendor/desktop/renderer/companion.js +1 -223
  186. package/vendor/desktop/renderer/diff.js +1 -359
  187. package/vendor/desktop/renderer/diffview.js +1 -336
  188. package/vendor/desktop/renderer/filepreview.js +1 -211
  189. package/vendor/desktop/renderer/fingerprint.js +3 -789
  190. package/vendor/desktop/renderer/markdown.js +1 -168
  191. package/vendor/desktop/renderer/provider-mark.js +1 -311
  192. package/vendor/desktop/renderer/provider-toolbar.js +1 -134
  193. package/vendor/desktop/renderer/providers.js +1 -259
  194. package/vendor/desktop/renderer/quick.js +1 -285
  195. package/vendor/desktop/renderer/stream-policy.js +1 -387
  196. package/vendor/desktop/renderer/transcript-view.js +1 -323
  197. package/vendor/desktop/renderer/usage.js +1 -1239
  198. package/vendor/desktop/vendor/aegis.js +1 -2441
  199. package/vendor/desktop/vendor/attest.js +1 -518
  200. package/vendor/desktop/vendor/authorship-key.js +1 -410
  201. package/vendor/desktop/vendor/auto-provision.js +1 -134
  202. package/vendor/desktop/vendor/brain-catalog.js +1 -268
  203. package/vendor/desktop/vendor/byok-catalog.js +1 -199
  204. package/vendor/desktop/vendor/catalog-client.js +1 -487
  205. package/vendor/desktop/vendor/conversation-export.js +1 -177
  206. package/vendor/desktop/vendor/credentials.js +1 -494
  207. package/vendor/desktop/vendor/device-fingerprint.js +1 -141
  208. package/vendor/desktop/vendor/drive-connect.js +1 -110
  209. package/vendor/desktop/vendor/drive-upload.js +1 -234
  210. package/vendor/desktop/vendor/env-file.js +1 -1273
  211. package/vendor/desktop/vendor/fingerprint.js +1 -1064
  212. package/vendor/desktop/vendor/foreign-memory.js +1 -669
  213. package/vendor/desktop/vendor/google-signin.js +1 -133
  214. package/vendor/desktop/vendor/hooks.js +1 -430
  215. package/vendor/desktop/vendor/model-discovery.js +1 -373
  216. package/vendor/desktop/vendor/naming.js +1 -283
  217. package/vendor/desktop/vendor/platform.js +1 -937
  218. package/vendor/desktop/vendor/prefs.js +1 -351
  219. package/vendor/desktop/vendor/presence-constants.js +1 -107
  220. package/vendor/desktop/vendor/presence.js +1 -438
  221. package/vendor/desktop/vendor/record-vault.js +3 -1055
  222. package/vendor/desktop/vendor/session-store.js +1 -575
  223. package/vendor/desktop/vendor/topup.js +1 -297
  224. package/vendor/desktop/vendor/touch.js +1 -458
  225. package/vendor/desktop/vendor/update.js +1 -763
  226. package/vendor/desktop/vendor/user-models.js +1 -381
@@ -1,240 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * Session-scoped tool-round ledger.
10
- *
11
- * The engine's tool-round cap used to be a wall. A turn that reached it ended
12
- * mid-investigation with `[stopped at 24 tool rounds…]`, and because the
13
- * accounting lived only for the duration of that call, the next turn in the
14
- * same conversation started from zero: it knew nothing about the horizon that
15
- * had just cut it off, so a long job could be truncated by the cap forever
16
- * without ever being finished.
17
- *
18
- * This makes the cap *session* state instead of turn state:
19
- *
20
- * - a turn that reaches its horizon records the unfinished work against the
21
- * session key (rounds spent, tokens, the note the user saw, and the
22
- * tool-call transcript itself — see `record`'s `messages`);
23
- * - the next turn in that session takes the record — consuming it, so one
24
- * resume per interruption and a chain of interruptions is a chain of
25
- * deliberate asks, never an automatic loop — and gets a continuation
26
- * preamble plus a small round bonus, so re-orientation does not eat the
27
- * new horizon before any work happens;
28
- * - an entry nobody comes back for expires after TTL_MS.
29
- *
30
- * State is module-level, so it outlives a turn, a `send()` and whichever
31
- * engine instance the desktop happens to be using. It never leaves the
32
- * process and is never written to disk.
33
- */
34
-
35
- /** How long an unclaimed interruption stays resumable (30 min). */
36
- const TTL_MS = 30 * 60 * 1000;
37
- /** Bound on live entries: a long-lived app must not grow without limit. */
38
- const MAX_ENTRIES = 64;
39
-
40
- const entries = new Map(); // key -> { rounds, tokens, note, at, interruptions }
41
- const historyKeys = new WeakMap(); // history[] -> key
42
- let historyKeySeq = 0;
43
-
44
- /** Drop expired entries, then the oldest ones past the bound. */
45
- function prune(now = Date.now()) {
46
- for (const [key, entry] of entries) {
47
- if (now - entry.at > TTL_MS) entries.delete(key);
48
- }
49
- while (entries.size > MAX_ENTRIES) {
50
- // Map iteration is insertion-ordered and `record` re-inserts on write, so
51
- // the first key is the oldest.
52
- const oldest = entries.keys().next();
53
- if (oldest.done) break;
54
- entries.delete(oldest.value);
55
- }
56
- return entries.size;
57
- }
58
-
59
- /**
60
- * The key an interruption is filed under. A caller that states its session
61
- * (desktop payloads carry an id) gets an exact match; otherwise the working
62
- * directory, then the identity of the history array, then a single shared
63
- * bucket. `adopt` covers the remaining case — a caller that mints a fresh key
64
- * every turn.
65
- */
66
- function keyFor(payload, history) {
67
- const p = payload && typeof payload === 'object' ? payload : {};
68
- const stated = p.sessionKey || p.sessionId || p.chatId || p.threadId || p.conversationId;
69
- if (typeof stated === 'string' && stated.trim()) return `s:${stated.trim()}`;
70
- if (typeof p.cwd === 'string' && p.cwd.trim()) return `cwd:${p.cwd.trim()}`;
71
- if (history && typeof history === 'object') {
72
- let key = historyKeys.get(history);
73
- if (!key) {
74
- historyKeySeq += 1;
75
- key = `hist-${historyKeySeq}`;
76
- historyKeys.set(history, key);
77
- }
78
- return key;
79
- }
80
- return 'default';
81
- }
82
-
83
- /**
84
- * Whether `keyFor` would resolve to an identity the CALLER actually chose
85
- * (a stated session id/key, or an explicit cwd) rather than one this module
86
- * had to synthesise (the history-array identity, or the bare `'default'`
87
- * bucket).
88
- *
89
- * This is what `adopt()` must be gated on. Every real caller today — the
90
- * desktop renderer, the CLI (`sessionKey`), the queue worker (`queue-<id>`)
91
- * — states an id, so a miss on THEIR key means exactly what it says: this
92
- * conversation has no held work, full stop. Before this gate, engine.js
93
- * called `adopt()` on every such miss regardless, which is the "bridge for a
94
- * caller whose key does not survive between turns" firing for callers whose
95
- * key demonstrably DOES survive — stealing whatever OTHER session's
96
- * interruption happened to be the most recent one in the process-wide ledger
97
- * and splicing its tool-call transcript into a conversation that never ran
98
- * it. Live symptom: an unrelated chat opens with "[session resume] The
99
- * previous turn in this conversation was cut off…" quoting a job it never
100
- * started, because some other stated session — a benchmark run, a different
101
- * open thread, anything that hit the horizon in the last `adopt()` window —
102
- * had an entry sitting in the shared, module-level `entries` map when this
103
- * one's `take()` came up empty.
104
- */
105
- function isStatedKey(payload) {
106
- const p = payload && typeof payload === 'object' ? payload : {};
107
- const stated = p.sessionKey || p.sessionId || p.chatId || p.threadId || p.conversationId;
108
- if (typeof stated === 'string' && stated.trim()) return true;
109
- return typeof p.cwd === 'string' && p.cwd.trim().length > 0;
110
- }
111
-
112
- /**
113
- * Extra rounds granted to a resuming turn. Deliberately small and bounded: the
114
- * point of resuming is to *continue* work that already exists, not to hand out
115
- * a second full horizon (every round re-sends the whole conversation, so this
116
- * is the cheapest way to make the cap non-fatal).
117
- */
118
- function bonus(base) {
119
- const n = Number.isFinite(base) && base > 0 ? base : 0;
120
- return Math.max(4, Math.ceil(n / 4));
121
- }
122
-
123
- /** Peek without consuming — the entry stays resumable. */
124
- function peek(key, now = Date.now()) {
125
- prune(now);
126
- return entries.get(key) || null;
127
- }
128
-
129
- /** Claim the interruption for `key`, if any. Consuming: a second call gets null. */
130
- function take(key, now = Date.now()) {
131
- prune(now);
132
- const entry = entries.get(key);
133
- if (!entry) return null;
134
- entries.delete(key);
135
- return entry;
136
- }
137
-
138
- /**
139
- * Claim the most recent interruption filed under ANY key. This is the bridge
140
- * for a caller whose key does not survive between turns: the next turn on this
141
- * process adopts the held work rather than losing it. Bounded by `maxAgeMs` so
142
- * an unrelated new conversation is not handed yesterday's job.
143
- */
144
- function adopt({ now = Date.now(), maxAgeMs = 10 * 60 * 1000 } = {}) {
145
- prune(now);
146
- let bestKey = null;
147
- let best = null;
148
- for (const [key, entry] of entries) {
149
- if (now - entry.at > maxAgeMs) continue;
150
- if (!best || entry.at > best.at) {
151
- best = entry;
152
- bestKey = key;
153
- }
154
- }
155
- if (!best) return null;
156
- entries.delete(bestKey);
157
- return { key: bestKey, entry: best };
158
- }
159
-
160
- /**
161
- * File an interruption. `chain` carries the count forward from the entry this
162
- * turn resumed, so `interruptions` reports how many times one job has been cut
163
- * off — visible in the note and useful when deciding to raise the horizon.
164
- *
165
- * `messages` is the interrupted turn's own tool-call transcript (its live
166
- * `history` array at the moment it hit the horizon) — real memory of what was
167
- * already done, not just a rounds/tokens count of it. Without it a caller
168
- * that supplies no conversation of its own (a queue task's fresh dispatch)
169
- * resumes with an empty history: the model is told "continue, don't restart"
170
- * with nothing to continue FROM, which is a cold start wearing a note.
171
- * Stored as a defensive copy — the caller's `history` array keeps being
172
- * mutated by the turn that is returning it.
173
- */
174
- function record(key, { rounds, tokens, note, chain, messages, added } = {}, now = Date.now()) {
175
- prune(now);
176
- const prior = entries.get(key);
177
- const entry = {
178
- rounds: Number.isFinite(rounds) ? rounds : 0,
179
- tokens: Number.isFinite(tokens) ? tokens : 0,
180
- note: typeof note === 'string' ? note : '',
181
- at: now,
182
- interruptions: (Number.isFinite(chain) ? chain : prior && prior.interruptions) || 0,
183
- messages: Array.isArray(messages) ? messages.slice() : [],
184
- // And what the interrupted turn added on TOP of what its caller already
185
- // had. A resuming caller who brings its own conversation (an interactive
186
- // chat) must get this half, not `messages` — pushing the full transcript
187
- // into a history that already holds its first half duplicates the user's
188
- // own turns back to the model.
189
- added: Array.isArray(added) ? added.slice() : [],
190
- };
191
- entry.interruptions += 1;
192
- entries.delete(key);
193
- entries.set(key, entry); // re-insert so Map order stays age order
194
- prune(now);
195
- return entry;
196
- }
197
-
198
- /** The instruction that turns a cold start into a continuation. */
199
- function resumePreamble(entry) {
200
- const rounds = entry && entry.rounds ? entry.rounds : 'the previous turn\'s';
201
- const tokens = entry && entry.tokens ? ` (${entry.tokens.toLocaleString()} tokens)` : '';
202
- return (
203
- `[session resume] The previous turn in this conversation was cut off at ${rounds} tool rounds${tokens} ` +
204
- 'because it reached the round horizon, so the work it started is unfinished. Continue that work now: ' +
205
- 'do not restart, do not re-do steps that already completed, and do not treat the interruption as a ' +
206
- 'failure to report. Pick up the next unfinished step and finish with a written answer.'
207
- );
208
- }
209
-
210
- /** Read-only snapshot for the UI (and for tests). */
211
- function state(now = Date.now()) {
212
- prune(now);
213
- return {
214
- size: entries.size,
215
- ttlMs: TTL_MS,
216
- maxEntries: MAX_ENTRIES,
217
- entries: [...entries].map(([key, entry]) => ({ key, ...entry })),
218
- };
219
- }
220
-
221
- /** Test hook: forget everything. */
222
- function reset() {
223
- entries.clear();
224
- return entries.size;
225
- }
226
-
227
- module.exports = {
228
- TTL_MS,
229
- MAX_ENTRIES,
230
- keyFor,
231
- isStatedKey,
232
- bonus,
233
- peek,
234
- take,
235
- adopt,
236
- record,
237
- resumePreamble,
238
- state,
239
- reset,
240
- };
3
+ "use strict";const TTL_MS=18e5,MAX_ENTRIES=64,entries=new Map,historyKeys=new WeakMap;let historyKeySeq=0;function prune(e=Date.now()){for(const[t,n]of entries)e-n.at>TTL_MS&&entries.delete(t);for(;entries.size>MAX_ENTRIES;){const e=entries.keys().next();if(e.done)break;entries.delete(e.value)}return entries.size}function keyFor(e,t){const n=e&&"object"==typeof e?e:{},r=n.sessionKey||n.sessionId||n.chatId||n.threadId||n.conversationId;if("string"==typeof r&&r.trim())return`s:${r.trim()}`;if("string"==typeof n.cwd&&n.cwd.trim())return`cwd:${n.cwd.trim()}`;if(t&&"object"==typeof t){let e=historyKeys.get(t);return e||(historyKeySeq+=1,e=`hist-${historyKeySeq}`,historyKeys.set(t,e)),e}return"default"}function isStatedKey(e){const t=e&&"object"==typeof e?e:{},n=t.sessionKey||t.sessionId||t.chatId||t.threadId||t.conversationId;return!("string"!=typeof n||!n.trim())||"string"==typeof t.cwd&&t.cwd.trim().length>0}function bonus(e){const t=Number.isFinite(e)&&e>0?e:0;return Math.max(4,Math.ceil(t/4))}function peek(e,t=Date.now()){return prune(t),entries.get(e)||null}function take(e,t=Date.now()){prune(t);const n=entries.get(e);return n?(entries.delete(e),n):null}function adopt({now:e=Date.now(),maxAgeMs:t=6e5}={}){prune(e);let n=null,r=null;for(const[s,i]of entries)e-i.at>t||(!r||i.at>r.at)&&(r=i,n=s);return r?(entries.delete(n),{key:n,entry:r}):null}function record(e,{rounds:t,tokens:n,note:r,chain:s,messages:i,added:o}={},a=Date.now()){prune(a);const u=entries.get(e),d={rounds:Number.isFinite(t)?t:0,tokens:Number.isFinite(n)?n:0,note:"string"==typeof r?r:"",at:a,interruptions:(Number.isFinite(s)?s:u&&u.interruptions)||0,messages:Array.isArray(i)?i.slice():[],added:Array.isArray(o)?o.slice():[]};return d.interruptions+=1,entries.delete(e),entries.set(e,d),prune(a),d}function resumePreamble(e){return`[session resume] The previous turn in this conversation was cut off at ${e&&e.rounds?e.rounds:"the previous turn's"} tool rounds${e&&e.tokens?` (${e.tokens.toLocaleString()} tokens)`:""} because it reached the round horizon, so the work it started is unfinished. Continue that work now: do not restart, do not re-do steps that already completed, and do not treat the interruption as a failure to report. Pick up the next unfinished step and finish with a written answer.`}function state(e=Date.now()){return prune(e),{size:entries.size,ttlMs:TTL_MS,maxEntries:MAX_ENTRIES,entries:[...entries].map((([e,t])=>({key:e,...t})))}}function reset(){return entries.clear(),entries.size}module.exports={TTL_MS:TTL_MS,MAX_ENTRIES:MAX_ENTRIES,keyFor:keyFor,isStatedKey:isStatedKey,bonus:bonus,peek:peek,take:take,adopt:adopt,record:record,resumePreamble:resumePreamble,state:state,reset:reset};
@@ -1,77 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * spill.js — what an oversized tool result becomes instead of nothing.
10
- *
11
- * `tools.js` (readFile/grep/glob/exec-one-shot) and `shell.js` (the persistent
12
- * session `exec`) each cut their output at OUTPUT_CAP. That was lossless for the
13
- * model only as long as the answer was in the first 30 000 chars — and for the
14
- * commands that overflow it usually is not: a wide grep, a full test log, a
15
- * `find` over a big tree. The model's only recovery was to re-run a narrower
16
- * command, which re-sends the whole prefix and bills a fresh round for
17
- * information it had already paid to produce. Pi spilled the overflow to a file
18
- * and told the model where it was; this is the same behaviour, in one place,
19
- * because two copies would drift.
20
- *
21
- * Why it lives in its own module rather than in either caller: `shell.js` may
22
- * not require `tools.js` (tools.js requires shell.js), so a shared helper is the
23
- * only arrangement where both sides spill identically.
24
- *
25
- * Bounded on purpose — this is reachable from a tool call:
26
- * · one temp dir per process (`mkdtemp`, 0700), lazily created;
27
- * · at most SPILL_MAX_FILES files, after which `spill` returns null and the
28
- * caller degrades to its old "… (truncated)" behaviour;
29
- * · every failure path returns null rather than throwing, because this runs
30
- * inside executors whose contract is "never throw".
31
- *
32
- * Cleanup is left to the OS temp reaper on purpose: a file the model may still
33
- * read on the next round must not disappear when the turn ends (the queue and
34
- * resume paths make "the turn" a fuzzy boundary), and a bounded set of 40 files
35
- * per process in the temp dir is not a leak worth that risk.
36
- */
37
-
38
- const fs = require('node:fs');
39
- const os = require('node:os');
40
- const path = require('node:path');
41
-
42
- const SPILL_MAX_FILES = 40;
43
-
44
- let spillDir = null;
45
- let spillCount = 0;
46
-
47
- /** Write `text` to a fresh temp file and return its path, or null when
48
- * spilling is unavailable (no cap left, no temp dir, read-only fs). */
49
- function spill(text) {
50
- try {
51
- if (spillCount >= SPILL_MAX_FILES) return null;
52
- if (!spillDir) spillDir = fs.mkdtempSync(path.join(os.tmpdir(), 'aegiscode-tool-'));
53
- spillCount += 1;
54
- const file = path.join(spillDir, `output-${spillCount}.txt`);
55
- fs.writeFileSync(file, String(text), 'utf8');
56
- return file;
57
- } catch {
58
- return null;
59
- }
60
- }
61
-
62
- /** Cut `text` at `cap` chars, keeping the full text reachable on disk. The
63
- * returned string stays within cap + ~200 chars so callers (and the tests in
64
- * test/shell-buffer-bound.test.mjs) that budget on the cap keep their bound. */
65
- function cap(text, limit) {
66
- const s = String(text == null ? '' : text);
67
- if (s.length <= limit) return s;
68
- const head = s.slice(0, limit);
69
- const file = spill(s);
70
- if (!file) return `${head}\n… (truncated)`;
71
- return (
72
- `${head}\n… (truncated after ${limit} of ${s.length} chars — full output: ${file} ` +
73
- `— grep or readFile that path)`
74
- );
75
- }
76
-
77
- module.exports = { spill, cap, SPILL_MAX_FILES };
3
+ "use strict";const fs=require("node:fs"),os=require("node:os"),path=require("node:path"),SPILL_MAX_FILES=40;let spillDir=null,spillCount=0;function spill(t){try{if(spillCount>=SPILL_MAX_FILES)return null;spillDir||(spillDir=fs.mkdtempSync(path.join(os.tmpdir(),"aegiscode-tool-"))),spillCount+=1;const l=path.join(spillDir,`output-${spillCount}.txt`);return fs.writeFileSync(l,String(t),"utf8"),l}catch{return null}}function cap(t,l){const n=String(null==t?"":t);if(n.length<=l)return n;const i=n.slice(0,l),r=spill(n);return r?`${i}\n… (truncated after ${l} of ${n.length} chars — full output: ${r} — grep or readFile that path)`:`${i}\n… (truncated)`}module.exports={spill:spill,cap:cap,SPILL_MAX_FILES:SPILL_MAX_FILES};