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,463 +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
- * auto.js — the two opt-in automations, and the rules that make them safe to
10
- * leave switched on.
11
- *
12
- * BACKGROUND. desktop/main.js and desktop/preload.js both stated an absolute
13
- * rule when the queue shipped: "NOTHING DRAINS UNLESS THE USER ASKED. There is
14
- * no interval, no drain at startup, and no drain-on-idle anywhere in this
15
- * file." A click was the only trigger, which made "queue a task and walk away"
16
- * impossible — the one thing an unattended queue is FOR. The user asked for the
17
- * automation (twice: "autonomous queue", then "work in the background
18
- * instead"), so both switches now DEFAULT ON and the sidebar card that used to
19
- * gate them is hidden: draining and sealing happen with nothing on screen.
20
- *
21
- * What keeps that safe is not an off-by-default — it is the guards below, which
22
- * are unchanged and all of them skips rather than drains: an empty queue, a
23
- * drain already in flight, another host holding the lock, and a switch turned
24
- * off since the last tick (re-read EVERY tick, so off stops the very next one,
25
- * whoever wrote it: the settings file, the CLI, a hand edit).
26
- *
27
- * There are exactly two automations, because there are exactly two acts in the
28
- * app that are irreversible-ish and cost something:
29
- *
30
- * createAutoDrain — runs pending queue tasks on a timer. Spend: a full tool
31
- * loop on a real model, in a real checkout. Guarded by the queue's own file
32
- * lock (a second drain is refused by queue.js, not here), by a re-entrancy
33
- * flag, by an empty-queue check, and by a lock-holder check that skips the
34
- * tick instead of racing another host for it.
35
- * createAutoMint — seals the current session as a φ(α) record once the
36
- * session goes QUIET. Not once per turn: a record is about a session, so a
37
- * debounce that resets on every finished turn means one record per session
38
- * that carries all of its turns — human axis and AI axis alike. Publishing
39
- * is a separate flag and is never implied by sealing.
40
- *
41
- * WHAT THIS MODULE DELIBERATELY DOES NOT DO:
42
- * - It does not decide whether it MAY run. `settings.getAutoDrain()` /
43
- * `getAutoMint()` are read on every tick, so turning the toggle off stops
44
- * the very next tick, and the namespaces are reserved
45
- * (settings.js RESERVED_NAMESPACES) so no provider row can forge one.
46
- * - It does not take the drain lock, spawn a worker, or write a record. The
47
- * queue worker owns the lock; lib/fingerprint.js owns the seal. This file
48
- * owns the TIMING and the skips, and calls in.
49
- * - It does not swallow a failure. Every skip and every failure is recorded
50
- * in `state()` with a reason, because an automation that fails silently is
51
- * indistinguishable from one that is switched off.
52
- *
53
- * Timers are injected (`setIntervalFn`/`setTimeoutFn`) so the tests drive the
54
- * cadence without waiting for it, and so this module never keeps a Node process
55
- * alive by itself.
56
- */
57
-
58
- /** How long a session must be quiet before an armed auto-mint seals it. Long
59
- * enough that a normal pause for thought does not file a half-finished
60
- * session, short enough that walking away leaves a record behind. */
61
- const AUTO_MINT_QUIET_DEFAULT_MS = 90_000;
62
-
63
- /** Floor: sealing on every turn boundary would file one record per exchange and
64
- * re-read the whole history each time. */
65
- const AUTO_MINT_QUIET_MIN_MS = 10_000;
66
-
67
- /** Ceiling: an hour of quiet. Beyond that the app is closed anyway. */
68
- const AUTO_MINT_QUIET_MAX_MS = 60 * 60_000;
69
-
70
- function errorText(err) {
71
- return (err && err.message) || String(err);
72
- }
73
-
74
- /**
75
- * The auto-drain timer.
76
- *
77
- * `proceed` is main.js's drain-all entry point (the same function the card's
78
- * "Drain all" button calls) — this module never re-implements the loop, the
79
- * lock, the settle-back-into-the-file write, or the Stop path.
80
- */
81
- function createAutoDrain({
82
- proceed,
83
- settings,
84
- queue,
85
- env = process.env,
86
- log = () => {},
87
- setIntervalFn = setInterval,
88
- clearIntervalFn = clearInterval,
89
- } = {}) {
90
- if (typeof proceed !== 'function') {
91
- throw new Error('createAutoDrain: a proceed() entry point is required');
92
- }
93
- if (!settings || typeof settings.getAutoDrain !== 'function') {
94
- throw new Error('createAutoDrain: the settings store is required');
95
- }
96
-
97
- /** The armed timer, or null. `null` is the OFF state and the only one a
98
- * default install is ever in. */
99
- let timer = null;
100
- /** A tick is mid-flight. A drain takes minutes; without this the next tick
101
- * would stack a second drainAll() on top of the first (queue.js would refuse
102
- * it at the lock, but only after a round trip and a second set of frames). */
103
- let busy = false;
104
- let ticks = 0;
105
- let drains = 0;
106
- let last = null;
107
- let lastSkip = null;
108
-
109
- const record = (entry) => {
110
- last = entry;
111
- log(entry);
112
- return entry;
113
- };
114
-
115
- const skip = (reason, extra) => {
116
- lastSkip = { at: Date.now(), reason, ...(extra || {}) };
117
- return lastSkip;
118
- };
119
-
120
- function state() {
121
- const cfg = settings.autoDrainState();
122
- return {
123
- ...cfg,
124
- armed: Boolean(timer),
125
- busy,
126
- ticks,
127
- drains,
128
- last,
129
- lastSkip,
130
- };
131
- }
132
-
133
- function arm() {
134
- const cfg = settings.getAutoDrain();
135
- // Off is checked BEFORE the "already armed at this cadence" shortcut, or
136
- // switching off would leave the old timer running (harmless — its tick
137
- // skips as 'disabled' — but `armed` would read true in the card, which is
138
- // the kind of lie that makes a user distrust the switch).
139
- if (!cfg.enabled) {
140
- stop();
141
- return state();
142
- }
143
- if (timer && cfg.intervalMs === timer.__intervalMs) return state();
144
- stop();
145
- timer = setIntervalFn(() => {
146
- // Fire and forget: a tick's own failures are recorded in `last`, and an
147
- // unhandled rejection here would take the main process's error handler
148
- // with it. tick() never throws.
149
- tick();
150
- }, cfg.intervalMs);
151
- timer.__intervalMs = cfg.intervalMs;
152
- // A drain must never be the reason a process stays alive; in Electron the
153
- // window is, and in a test the harness is.
154
- if (timer && typeof timer.unref === 'function') timer.unref();
155
- log({ type: 'auto-drain-armed', intervalMs: cfg.intervalMs });
156
- return state();
157
- }
158
-
159
- function stop() {
160
- if (!timer) return state();
161
- clearIntervalFn(timer);
162
- timer = null;
163
- log({ type: 'auto-drain-disarmed' });
164
- return state();
165
- }
166
-
167
- /**
168
- * One decision, and the only place the guards live.
169
- *
170
- * Order matters. `disabled` is first so an armed-then-switched-off timer
171
- * cannot act on a stale config; `busy` before any file read; `empty` before
172
- * the lock so an idle install costs one JSON parse and no lock I/O; and the
173
- * lock-holder check LAST, because taking a lock we would only hand straight
174
- * back is the one skip that has a real cost (it makes another host's drain
175
- * fail its own acquire).
176
- */
177
- async function tick() {
178
- ticks += 1;
179
- const cfg = settings.getAutoDrain();
180
- if (!cfg.enabled) return record({ type: 'skip', reason: 'disabled' });
181
- if (busy) return record({ type: 'skip', reason: 'busy' });
182
- if (!queue || typeof queue.loadQueue !== 'function') {
183
- return record({ type: 'skip', reason: 'no queue access' });
184
- }
185
-
186
- let items;
187
- try {
188
- items = queue.loadQueue(env);
189
- } catch (err) {
190
- return record({ type: 'error', phase: 'read', error: errorText(err) });
191
- }
192
- if (!queue.pendingCount(items)) return record({ type: 'skip', reason: 'empty' });
193
-
194
- // Another host holds the drain (the CLI's `aegiscode autonomous`, a
195
- // systemd unit, another window). queue.js's lock would refuse us anyway —
196
- // this check is so the refusal is a quiet skip rather than a drain attempt
197
- // whose every frame reports "locked".
198
- try {
199
- const holder = queue.lockHolder(env);
200
- if (holder && holder.pid && holder.pid !== process.pid) {
201
- const alive = typeof queue.isAlive === 'function' ? queue.isAlive(holder.pid) : true;
202
- if (alive) return record({ type: 'skip', reason: 'locked', holder });
203
- }
204
- } catch {
205
- // An unreadable lock file is not a reason to skip: queue.js's own
206
- // acquireLock decides, and a stale/unparseable marker is exactly the case
207
- // it reclaims immediately.
208
- }
209
-
210
- busy = true;
211
- try {
212
- const result = await proceed();
213
- if (result && result.locked) {
214
- return record({
215
- type: 'skip',
216
- reason: 'locked',
217
- holder: result.holder || null,
218
- });
219
- }
220
- drains += 1;
221
- return record({
222
- type: 'drain',
223
- ok: !(result && result.ok === false),
224
- ran: (result && result.ran) || [],
225
- reason: (result && result.reason) || null,
226
- });
227
- } catch (err) {
228
- return record({ type: 'error', phase: 'drain', error: errorText(err) });
229
- } finally {
230
- busy = false;
231
- }
232
- }
233
-
234
- return {
235
- state,
236
- /** Read the persisted switch and arm/disarm to match. Called at boot and
237
- * after every toggle — never called by a timer. */
238
- start: arm,
239
- arm,
240
- stop,
241
- tick,
242
- /** The card's toggle. Persists through the settings store, then re-arms,
243
- * so the answer the renderer paints is the answer the timer is running.
244
- * Without the re-arm the switch would persist and do nothing until the next
245
- * restart — the exact silent failure this test file exists to catch. */
246
- set(patch) {
247
- settings.setAutoDrain(patch || {});
248
- arm();
249
- return state();
250
- },
251
- /** Test seam: whatever is armed, for a harness that injects its own timer
252
- * and needs to fire it. */
253
- _timer: () => timer,
254
- };
255
- }
256
-
257
- /**
258
- * The auto-mint (seal-on-quiet) timer.
259
- *
260
- * `mint` is lib/fingerprint.js's own `mint()` — the same call the panel's "Seal
261
- * this session" button makes, reading the same session log through the same
262
- * shared adapter, so an automatic record and a hand-sealed one of the same
263
- * session are the same record.
264
- */
265
- function createAutoMint({
266
- mint,
267
- settings,
268
- log = () => {},
269
- quietMs = AUTO_MINT_QUIET_DEFAULT_MS,
270
- setTimeoutFn = setTimeout,
271
- clearTimeoutFn = clearTimeout,
272
- } = {}) {
273
- if (typeof mint !== 'function') throw new Error('createAutoMint: mint() is required');
274
- if (!settings || typeof settings.getAutoMint !== 'function') {
275
- throw new Error('createAutoMint: the settings store is required');
276
- }
277
-
278
- const quiet = (() => {
279
- const n = Number(quietMs);
280
- if (!Number.isFinite(n)) return AUTO_MINT_QUIET_DEFAULT_MS;
281
- const ms = Math.round(n);
282
- if (ms < AUTO_MINT_QUIET_MIN_MS || ms > AUTO_MINT_QUIET_MAX_MS) {
283
- return AUTO_MINT_QUIET_DEFAULT_MS;
284
- }
285
- return ms;
286
- })();
287
-
288
- /** The pending debounce, or null. */
289
- let timer = null;
290
- /** Turns that arrived since the last seal. The ONLY thing that makes a seal
291
- * due: without it, a quiet period with no new work would re-seal the same
292
- * session over and over, one identical record per quiet minute. */
293
- let pendingTurns = 0;
294
- /** The model the last finished turn REPORTED, or null. Held here because the
295
- * seal happens later, on the quiet clock, and by then the picker may already
296
- * have moved on (a model switch mid-session) — the record must state the
297
- * model that answered these turns, not whichever row is selected now. */
298
- let pendingModel = null;
299
- let busy = false;
300
- let seals = 0;
301
- let failures = 0;
302
- let last = null;
303
- let lastError = null;
304
- let lastSeal = null;
305
-
306
- function state() {
307
- const cfg = settings.autoMintState();
308
- return {
309
- ...cfg,
310
- armed: Boolean(timer),
311
- quietMs: quiet,
312
- pendingTurns,
313
- // What the pending turns reported, so the panel can say what the next
314
- // automatic seal will state rather than leaving it a surprise.
315
- model: pendingModel,
316
- busy,
317
- seals,
318
- failures,
319
- last,
320
- lastError,
321
- lastSeal,
322
- };
323
- }
324
-
325
- function disarm() {
326
- if (!timer) return;
327
- clearTimeoutFn(timer);
328
- timer = null;
329
- }
330
-
331
- /** A turn finished. Restart the quiet clock; do not seal anything now. */
332
- function note(info) {
333
- const cfg = settings.getAutoMint();
334
- // Recorded BEFORE the switch is read: a user who turns the automation on
335
- // mid-session means the work already done, and the model that answered it
336
- // is part of that work. Only a turn that actually reported one is kept —
337
- // an absent model stays absent rather than becoming the picker's current
338
- // row, which may name a model this session never ran.
339
- const reported = info && typeof info.model === 'string' ? info.model.trim() : '';
340
- if (reported) pendingModel = reported;
341
- if (!cfg.enabled) {
342
- // Counted even while off, so switching the toggle on mid-session seals a
343
- // session that already has work in it rather than waiting for the next
344
- // turn — which is what a user who just flipped the switch means.
345
- pendingTurns += 1;
346
- return state();
347
- }
348
- pendingTurns += 1;
349
- last = { type: 'turn', at: Date.now(), sessionId: (info && info.sessionId) || null };
350
- disarm();
351
- timer = setTimeoutFn(() => {
352
- timer = null;
353
- flush();
354
- }, quiet);
355
- if (timer && typeof timer.unref === 'function') timer.unref();
356
- return state();
357
- }
358
-
359
- /**
360
- * Seal now, if there is anything to seal. This is what the debounce timer
361
- * calls; it is also the seam a test drives directly instead of waiting.
362
- */
363
- async function flush() {
364
- const cfg = settings.getAutoMint();
365
- if (!cfg.enabled) return { type: 'skip', reason: 'disabled' };
366
- if (busy) return { type: 'skip', reason: 'busy' };
367
- if (!pendingTurns) return { type: 'skip', reason: 'nothing new' };
368
- busy = true;
369
- // Claimed BEFORE the call: a seal that throws must not leave the counter
370
- // armed, or every later quiet period retries a failure that will not heal
371
- // (no key, unreadable history) and files a new error each time.
372
- const claimed = pendingTurns;
373
- pendingTurns = 0;
374
- try {
375
- // `aiSecret` is left undefined on purpose: that makes lib/fingerprint.js
376
- // walk the shared rungs in client/attest.js, so the AI axis is attested
377
- // from `$AEGIS_AI_SECRET` in the app's scope, and failing that from the
378
- // provider key this machine already holds for the model below. This host
379
- // never invents a credential and never borrows the authorship key for the
380
- // AI axis (they answer different questions: who sealed it, and which mind
381
- // answered).
382
- //
383
- // `aiModel` IS passed: it is the model the finished turn reported, held
384
- // in `pendingModel`. Without it the app's unattended seals were the one
385
- // host whose records never named a model — and a null model also meant
386
- // the credential rung had nothing to resolve a provider key from, so
387
- // `aiKey` stayed null and "same mind?" had no answer. An absent model is
388
- // left undefined so the axis stays honestly unobserved.
389
- const result = await mint({
390
- publish: cfg.publish === true,
391
- aiModel: pendingModel || undefined,
392
- });
393
- if (!result || result.ok === false) {
394
- failures += 1;
395
- lastError = (result && result.error) || 'the seal did not complete';
396
- return (last = { type: 'error', phase: 'seal', error: lastError });
397
- }
398
- seals += 1;
399
- lastError = null;
400
- // Cleared only after the seal SUCCEEDED: a failed seal keeps the model,
401
- // because the retry is about the same turns and must state the same
402
- // model rather than falling back to "unobserved".
403
- pendingModel = null;
404
- lastSeal = {
405
- at: Date.now(),
406
- id: (result.record && result.record.id) || null,
407
- out: result.out || null,
408
- turns: claimed,
409
- // Reported separately by lib/fingerprint.js: the seal is the evidence,
410
- // the cloud copy is the convenience, and a failed publish never
411
- // unseals anything.
412
- vaultError: result.vaultError || null,
413
- };
414
- return (last = { type: 'seal', ...lastSeal });
415
- } catch (err) {
416
- failures += 1;
417
- lastError = errorText(err);
418
- return (last = { type: 'error', phase: 'seal', error: lastError });
419
- } finally {
420
- busy = false;
421
- }
422
- }
423
-
424
- return {
425
- state,
426
- note,
427
- flush,
428
- /** Disarm the debounce and forget the pending count — the renderer's
429
- * explicit "seal now" path calls this after sealing, so the record it just
430
- * filed is not filed again by the timer. */
431
- reset() {
432
- disarm();
433
- pendingTurns = 0;
434
- pendingModel = null;
435
- return state();
436
- },
437
- start() {
438
- // Arming at boot means "the switch is on", not "seal immediately": with
439
- // pendingTurns at 0 there is nothing to seal until a turn arrives.
440
- if (!settings.getAutoMint().enabled) disarm();
441
- return state();
442
- },
443
- stop() {
444
- disarm();
445
- return state();
446
- },
447
- set(patch) {
448
- settings.setAutoMint(patch || {});
449
- if (!settings.getAutoMint().enabled) disarm();
450
- return state();
451
- },
452
- /** Test seam: is the debounce armed? */
453
- _timer: () => timer,
454
- };
455
- }
456
-
457
- module.exports = {
458
- createAutoDrain,
459
- createAutoMint,
460
- AUTO_MINT_QUIET_DEFAULT_MS,
461
- AUTO_MINT_QUIET_MIN_MS,
462
- AUTO_MINT_QUIET_MAX_MS,
463
- };
3
+ "use strict";const AUTO_MINT_QUIET_DEFAULT_MS=9e4,AUTO_MINT_QUIET_MIN_MS=1e4,AUTO_MINT_QUIET_MAX_MS=36e5;function errorText(e){return e&&e.message||String(e)}function createAutoDrain({proceed:e,settings:t,queue:r,env:n=process.env,log:o=(()=>{}),setIntervalFn:i=setInterval,clearIntervalFn:u=clearInterval}={}){if("function"!=typeof e)throw new Error("createAutoDrain: a proceed() entry point is required");if(!t||"function"!=typeof t.getAutoDrain)throw new Error("createAutoDrain: the settings store is required");let s=null,a=!1,l=0,c=0,p=null,d=null;const f=e=>(p=e,o(e),e);function y(){return{...t.autoDrainState(),armed:Boolean(s),busy:a,ticks:l,drains:c,last:p,lastSkip:d}}function M(){const e=t.getAutoDrain();return e.enabled?(s&&e.intervalMs===s.__intervalMs||(A(),s=i((()=>{_()}),e.intervalMs),s.__intervalMs=e.intervalMs,s&&"function"==typeof s.unref&&s.unref(),o({type:"auto-drain-armed",intervalMs:e.intervalMs})),y()):(A(),y())}function A(){return s?(u(s),s=null,o({type:"auto-drain-disarmed"}),y()):y()}async function _(){if(l+=1,!t.getAutoDrain().enabled)return f({type:"skip",reason:"disabled"});if(a)return f({type:"skip",reason:"busy"});if(!r||"function"!=typeof r.loadQueue)return f({type:"skip",reason:"no queue access"});let o;try{o=r.loadQueue(n)}catch(e){return f({type:"error",phase:"read",error:errorText(e)})}if(!r.pendingCount(o))return f({type:"skip",reason:"empty"});try{const e=r.lockHolder(n);if(e&&e.pid&&e.pid!==process.pid&&("function"!=typeof r.isAlive||r.isAlive(e.pid)))return f({type:"skip",reason:"locked",holder:e})}catch{}a=!0;try{const t=await e();return t&&t.locked?f({type:"skip",reason:"locked",holder:t.holder||null}):(c+=1,f({type:"drain",ok:!(t&&!1===t.ok),ran:t&&t.ran||[],reason:t&&t.reason||null}))}catch(e){return f({type:"error",phase:"drain",error:errorText(e)})}finally{a=!1}}return{state:y,start:M,arm:M,stop:A,tick:_,set:e=>(t.setAutoDrain(e||{}),M(),y()),_timer:()=>s}}function createAutoMint({mint:e,settings:t,log:r=(()=>{}),quietMs:n=9e4,setTimeoutFn:o=setTimeout,clearTimeoutFn:i=clearTimeout}={}){if("function"!=typeof e)throw new Error("createAutoMint: mint() is required");if(!t||"function"!=typeof t.getAutoMint)throw new Error("createAutoMint: the settings store is required");const u=(()=>{const e=Number(n);if(!Number.isFinite(e))return 9e4;const t=Math.round(e);return t<1e4||t>36e5?9e4:t})();let s=null,a=0,l=null,c=!1,p=0,d=0,f=null,y=null,M=null;function A(){return{...t.autoMintState(),armed:Boolean(s),quietMs:u,pendingTurns:a,model:l,busy:c,seals:p,failures:d,last:f,lastError:y,lastSeal:M}}function _(){s&&(i(s),s=null)}async function T(){const r=t.getAutoMint();if(!r.enabled)return{type:"skip",reason:"disabled"};if(c)return{type:"skip",reason:"busy"};if(!a)return{type:"skip",reason:"nothing new"};c=!0;const n=a;a=0;try{const t=await e({publish:!0===r.publish,aiModel:l||void 0});return t&&!1!==t.ok?(p+=1,y=null,l=null,M={at:Date.now(),id:t.record&&t.record.id||null,out:t.out||null,turns:n,vaultError:t.vaultError||null},f={type:"seal",...M}):(d+=1,y=t&&t.error||"the seal did not complete",f={type:"error",phase:"seal",error:y})}catch(e){return d+=1,y=errorText(e),f={type:"error",phase:"seal",error:y}}finally{c=!1}}return{state:A,note:function(e){const r=t.getAutoMint(),n=e&&"string"==typeof e.model?e.model.trim():"";return n&&(l=n),r.enabled?(a+=1,f={type:"turn",at:Date.now(),sessionId:e&&e.sessionId||null},_(),s=o((()=>{s=null,T()}),u),s&&"function"==typeof s.unref&&s.unref(),A()):(a+=1,A())},flush:T,reset:()=>(_(),a=0,l=null,A()),start:()=>(t.getAutoMint().enabled||_(),A()),stop:()=>(_(),A()),set:e=>(t.setAutoMint(e||{}),t.getAutoMint().enabled||_(),A()),_timer:()=>s}}module.exports={createAutoDrain:createAutoDrain,createAutoMint:createAutoMint,AUTO_MINT_QUIET_DEFAULT_MS:9e4,AUTO_MINT_QUIET_MIN_MS:1e4,AUTO_MINT_QUIET_MAX_MS:36e5};