aegisone 1.0.18 → 1.0.20

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 (48) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -1
  3. package/scripts/bump.mjs +29 -4
  4. package/scripts/predist.mjs +2 -0
  5. package/src/dispatch.js +1 -0
  6. package/vendor/cli/bin/aegiscode.js +29 -0
  7. package/vendor/cli/package.json +1 -1
  8. package/vendor/cli/src/app.js +335 -24
  9. package/vendor/cli/src/autonomous.js +19 -2
  10. package/vendor/cli/src/chatflow.js +77 -4
  11. package/vendor/cli/src/commands.js +175 -5
  12. package/vendor/cli/src/config.js +21 -0
  13. package/vendor/cli/src/connect.js +351 -0
  14. package/vendor/cli/src/deps.js +97 -1
  15. package/vendor/cli/src/engine.js +98 -8
  16. package/vendor/cli/src/fingerprint.js +46 -16
  17. package/vendor/cli/src/limits.js +1 -1
  18. package/vendor/cli/src/overlays.js +54 -0
  19. package/vendor/cli/src/provenance.lock.json +3 -3
  20. package/vendor/cli/src/screens.js +14 -1
  21. package/vendor/cli/vendor/client/credentials.js +9 -2
  22. package/vendor/cli/vendor/client/google-signin.js +127 -0
  23. package/vendor/cli/vendor/client/record-vault.js +431 -113
  24. package/vendor/cli/vendor/client/update.js +266 -0
  25. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +134 -11
  26. package/vendor/cli/vendor/desktop/lib/local/engine.js +47 -25
  27. package/vendor/cli/vendor/desktop/lib/settings.js +65 -24
  28. package/vendor/desktop/lib/auto.js +11 -7
  29. package/vendor/desktop/lib/fingerprint.js +76 -17
  30. package/vendor/desktop/lib/local/claude-pro.js +134 -11
  31. package/vendor/desktop/lib/local/engine.js +47 -25
  32. package/vendor/desktop/lib/settings.js +65 -24
  33. package/vendor/desktop/main.js +79 -24
  34. package/vendor/desktop/package.json +4 -2
  35. package/vendor/desktop/preload.js +31 -12
  36. package/vendor/desktop/renderer/app.js +676 -17
  37. package/vendor/desktop/renderer/budget.js +18 -9
  38. package/vendor/desktop/renderer/companion.js +225 -0
  39. package/vendor/desktop/renderer/fingerprint.css +16 -0
  40. package/vendor/desktop/renderer/fingerprint.js +62 -2
  41. package/vendor/desktop/renderer/index.html +285 -121
  42. package/vendor/desktop/renderer/provider-toolbar.js +136 -0
  43. package/vendor/desktop/renderer/providers.js +261 -0
  44. package/vendor/desktop/renderer/style.css +547 -171
  45. package/vendor/desktop/vendor/credentials.js +9 -2
  46. package/vendor/desktop/vendor/google-signin.js +127 -0
  47. package/vendor/desktop/vendor/record-vault.js +431 -113
  48. package/vendor/desktop/vendor/update.js +266 -0
@@ -103,6 +103,19 @@ const QUICK_LAUNCHER_NAMESPACE = '__quickLauncher';
103
103
  * fighting existing app/OS bindings, memorable enough to type once. */
104
104
  const DEFAULT_QUICK_LAUNCHER_SHORTCUT = 'CmdOrCtrl+Shift+Space';
105
105
 
106
+ /** Reserved namespace for where `fingerprint publish`/the vault card sends
107
+ * sealed records: `{ backend }`, one of VAULT_BACKEND_VALUES — app-level, not
108
+ * a provider, for the same reason the quick launcher isn't. Default is
109
+ * `aegiscloud` (unset reads as the default, same convention as confirm mode)
110
+ * because it is the only backend that exists on a fresh install — see
111
+ * client/record-vault.js's own DEFAULT_BACKEND. `drive` asks the fingerprint
112
+ * service to talk to rclone's `gdrive` remote instead (the user's own Drive,
113
+ * configured by them); `off` means publish/anchor/vault/audit refuse with a
114
+ * named reason rather than silently reaching either backend. */
115
+ const VAULT_BACKEND_NAMESPACE = '__vaultBackend';
116
+ const DEFAULT_VAULT_BACKEND = 'aegiscloud';
117
+ const VAULT_BACKEND_VALUES = Object.freeze(['aegiscloud', 'drive', 'off']);
118
+
106
119
  /** Reserved namespace for the tool-call approval ("confirm mode") preference:
107
120
  * `{ enabled }` — app-level, not a provider, so it stays out of the provider
108
121
  * CRUD surface for the same reason the AEGIS key does. Default is ON
@@ -138,31 +151,34 @@ const AVATAR_NAMESPACE = '__avatar';
138
151
  * main.js telemetryEnabled() through getTelemetryOptIn(). */
139
152
  const TELEMETRY_NAMESPACE = '__telemetry';
140
153
 
141
- /** Reserved namespace for the auto-drain opt-in:
154
+ /** Reserved namespace for the auto-drain switch:
142
155
  * `{ enabled, intervalMs }` — app-level, not a provider.
143
156
  *
144
- * DEFAULT OFF, and unlike every preference above that default is the design
145
- * rather than a convention: this is the one setting in the app that causes a
146
- * full tool loop to run on a real model in the user's checkout with nobody
147
- * watching. desktop/main.js and desktop/preload.js both used to state the
148
- * absolute rule — "there is no interval, no drain at startup, and no
149
- * drain-on-idle anywhere" — and that rule is still what an install that never
150
- * touches this namespace gets. What changed (user request, phase A of the
151
- * automation work) is that the rule became a DEFAULT instead of an absolute:
152
- * an explicit `enabled: true` here is the only thing that arms a timer, the
153
- * toggle that writes it is in the queue card, and `lib/auto.js` refuses to
154
- * run a tick when the queue is empty or another host already holds the drain
155
- * lock. Nothing secret lives here, so no encryption — same shape as
156
- * `__confirmMode`/`__memoryPersist` above. */
157
+ * DEFAULT ON (user request: the queue must work with no click and nothing on
158
+ * screen). An absent namespace means "drain in the background"; only an
159
+ * explicit `enabled: false` disarms it, and that is what the settings store
160
+ * writes when someone turns the worker off from the CLI or by hand.
161
+ *
162
+ * The guards are what make that default defensible, and they live in
163
+ * `lib/auto.js`, not here: a tick skips when the queue is empty (so an idle
164
+ * install costs one JSON parse and no lock I/O), when another host holds the
165
+ * drain lock, when a drain is already in flight, and when the switch was
166
+ * turned off since the last tick — the switch is re-read on EVERY tick, so
167
+ * disarming stops the very next one. Nothing secret lives here, so no
168
+ * encryption — same shape as `__confirmMode`/`__memoryPersist` above. */
157
169
  const AUTO_DRAIN_NAMESPACE = '__autoDrain';
158
170
 
159
- /** Reserved namespace for the auto-mint (seal-on-quiet) opt-in:
171
+ /** Reserved namespace for the auto-mint (seal-on-quiet) switch:
160
172
  * `{ enabled, publish }` — app-level, not a provider.
161
173
  *
162
- * DEFAULT OFF for the same reason as auto-drain, and `publish` is a second,
163
- * independent opt-in: sealing writes a local record, whereas publishing sends
164
- * those bytes to the vault over `rclone` and writes an anchor beside them. An
165
- * automatic network copy is never implied by an automatic seal.
174
+ * `enabled` is DEFAULT ON, same as auto-drain: a session seals itself once it
175
+ * has been quiet, with nothing on screen asking for it. Only an explicit
176
+ * `enabled: false` disarms it.
177
+ *
178
+ * `publish` stays a separate, DEFAULT OFF switch: sealing writes a local
179
+ * record, whereas publishing sends those bytes to the vault over `rclone` and
180
+ * writes an anchor beside them. An automatic network copy is never implied by
181
+ * an automatic seal.
166
182
  *
167
183
  * The AI half of the record resolves through the shared rungs in
168
184
  * client/attest.js (`$AEGIS_AI_SECRET`/`$AEGIS_AI_MODEL` in the app scope) —
@@ -195,6 +211,7 @@ const RESERVED_NAMESPACES = Object.freeze([
195
211
  AEGIS_KEY_NAMESPACE,
196
212
  LEGACY_AEGIS_NAMESPACE,
197
213
  QUICK_LAUNCHER_NAMESPACE,
214
+ VAULT_BACKEND_NAMESPACE,
198
215
  CONFIRM_MODE_NAMESPACE,
199
216
  MEMORY_PERSIST_NAMESPACE,
200
217
  AVATAR_NAMESPACE,
@@ -539,6 +556,24 @@ function createSettingsStore({ dir, safeStorage, prefsDir, env } = {}) {
539
556
  return quickLauncherConfig();
540
557
  }
541
558
 
559
+ // --- Vault backend: reserved namespace, plain (unencrypted) preference ---
560
+ // No secret lives here either — just which of three named destinations a
561
+ // publish goes to — so like the quick launcher there is nothing to encrypt.
562
+
563
+ function vaultBackendConfig() {
564
+ const cfg = load()[VAULT_BACKEND_NAMESPACE] || {};
565
+ const backend = VAULT_BACKEND_VALUES.includes(cfg.backend) ? cfg.backend : DEFAULT_VAULT_BACKEND;
566
+ return { backend };
567
+ }
568
+
569
+ function setVaultBackendConfig(backend) {
570
+ if (!VAULT_BACKEND_VALUES.includes(backend)) return vaultBackendConfig();
571
+ const data = load();
572
+ data[VAULT_BACKEND_NAMESPACE] = { backend };
573
+ save(data);
574
+ return vaultBackendConfig();
575
+ }
576
+
542
577
  // --- Confirm mode: SHARED preference (AEGISONE ON8, decision D8 rule 1) --
543
578
  // "Confirm before running tools" — the switch for the renderer's tool-call
544
579
  // approval gate (desktop/lib/local/engine.js gatedExecuteTool). ON (default,
@@ -726,10 +761,10 @@ function createSettingsStore({ dir, safeStorage, prefsDir, env } = {}) {
726
761
  function getAutoDrain() {
727
762
  const cfg = load()[AUTO_DRAIN_NAMESPACE] || {};
728
763
  return {
729
- // Unset === false. This is the opposite of confirm mode/memory persist,
730
- // and it is the whole point of the feature: nothing drains itself until
731
- // someone says so in as many words.
732
- enabled: cfg.enabled === true,
764
+ // Unset === TRUE. The queue drains in the background unless someone
765
+ // switched it off in as many words; `lib/auto.js` re-reads this on every
766
+ // tick, so that off switch stops the very next one.
767
+ enabled: cfg.enabled !== false,
733
768
  intervalMs: clampAutoDrainInterval(cfg.intervalMs),
734
769
  };
735
770
  }
@@ -760,7 +795,8 @@ function createSettingsStore({ dir, safeStorage, prefsDir, env } = {}) {
760
795
  function getAutoMint() {
761
796
  const cfg = load()[AUTO_MINT_NAMESPACE] || {};
762
797
  return {
763
- enabled: cfg.enabled === true,
798
+ // Unset === TRUE (background sealing). `publish` below stays opt-in.
799
+ enabled: cfg.enabled !== false,
764
800
  // Independent of `enabled` on purpose: an automatic seal must not drag
765
801
  // an automatic upload along with it.
766
802
  publish: cfg.publish === true,
@@ -807,6 +843,8 @@ function createSettingsStore({ dir, safeStorage, prefsDir, env } = {}) {
807
843
  migrateLegacyAegisKey,
808
844
  quickLauncherConfig,
809
845
  setQuickLauncherConfig,
846
+ vaultBackendConfig,
847
+ setVaultBackendConfig,
810
848
  getConfirmMode,
811
849
  setConfirmMode,
812
850
  confirmModeState,
@@ -833,6 +871,7 @@ module.exports = {
833
871
  AEGIS_KEY_NAMESPACE,
834
872
  LEGACY_AEGIS_NAMESPACE,
835
873
  QUICK_LAUNCHER_NAMESPACE,
874
+ VAULT_BACKEND_NAMESPACE,
836
875
  CONFIRM_MODE_NAMESPACE,
837
876
  MEMORY_PERSIST_NAMESPACE,
838
877
  AVATAR_NAMESPACE,
@@ -844,6 +883,8 @@ module.exports = {
844
883
  AUTO_DRAIN_INTERVAL_MAX_MS,
845
884
  AVATAR_MOTION_MODES,
846
885
  DEFAULT_QUICK_LAUNCHER_SHORTCUT,
886
+ DEFAULT_VAULT_BACKEND,
887
+ VAULT_BACKEND_VALUES,
847
888
  RESERVED_NAMESPACES,
848
889
  isReservedNamespace,
849
890
  maskKey,
@@ -12,13 +12,17 @@
12
12
  * BACKGROUND. desktop/main.js and desktop/preload.js both stated an absolute
13
13
  * rule when the queue shipped: "NOTHING DRAINS UNLESS THE USER ASKED. There is
14
14
  * no interval, no drain at startup, and no drain-on-idle anywhere in this
15
- * file." That rule was right when the only trigger was a click, and it is still
16
- * the behaviour of a default install — but it made "queue a task and walk away"
17
- * impossible, which is the one thing an unattended queue is FOR. The user asked
18
- * for both automations explicitly (option A of that discussion), so the rule
19
- * becomes a DEFAULT rather than an absolute, in the only way that keeps the
20
- * original sentence true: an explicit, persisted, default-off switch, and a
21
- * tick that does nothing unless it is armed.
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).
22
26
  *
23
27
  * There are exactly two automations, because there are exactly two acts in the
24
28
  * app that are irreversible-ish and cost something:
@@ -94,12 +94,43 @@ function slug(value, max = 80) {
94
94
  * @param {object} o
95
95
  * @param {string} o.dir the app's data directory (userData in a package)
96
96
  * @param {object} [o.env] process env, injectable for tests
97
+ * @param {() => string} [o.getVaultBackend] reads the live preference
98
+ * ('aegiscloud' | 'drive' | 'off', desktop/lib/settings.js's
99
+ * vaultBackendConfig) — a function, not a value, so a change made in
100
+ * Settings while the app is running reaches the very next vault call with
101
+ * no restart, the same contract getConfirmMode's gate already keeps.
97
102
  */
98
- function createFingerprintService({ dir, env } = {}) {
103
+ function createFingerprintService({ dir, env, getVaultBackend } = {}) {
99
104
  if (!dir) throw new Error('aegis-desktop: createFingerprintService needs a data dir');
100
105
  // One store: the shared modules read AEGIS_HOME and nothing else.
101
106
  const scope = { ...(env || process.env), AEGIS_HOME: (env || process.env).AEGIS_HOME || dir };
102
107
 
108
+ /** `true` only when the user has explicitly turned sync off — not when no
109
+ * preference has been recorded yet, which must keep resolving to whatever
110
+ * record-vault.js's own default is. */
111
+ function vaultDisabled() {
112
+ return typeof getVaultBackend === 'function' && getVaultBackend() === 'off';
113
+ }
114
+
115
+ /**
116
+ * `scope`, with the vault backend layered on top of it when a preference
117
+ * names one. 'aegiscloud' and an absent preference both fall through to
118
+ * `scope` unchanged — client/record-vault.js's own DEFAULT_BACKEND is
119
+ * already 'aegiscloud', so there is nothing to add for either case. 'drive'
120
+ * is the one preference that actually changes the destination: it asks the
121
+ * rclone adapter for the `gdrive` remote (the user's own Google Drive,
122
+ * configured by them — see client/record-vault.js's DEFAULT_REMOTE). 'off'
123
+ * is handled by `vaultDisabled()` before this is ever called, not here.
124
+ */
125
+ function vaultScope() {
126
+ if (typeof getVaultBackend === 'function' && getVaultBackend() === 'drive') {
127
+ return { ...scope, AEGIS_VAULT_BACKEND: 'rclone', AEGIS_VAULT_REMOTE: scope.AEGIS_VAULT_REMOTE || 'gdrive' };
128
+ }
129
+ return scope;
130
+ }
131
+
132
+ const VAULT_OFF_ERROR = 'the vault is turned off — turn it back on (Google Drive or AE) in Settings to publish or inspect it';
133
+
103
134
  const recordPath = (record, session) =>
104
135
  path.join(dir, 'fingerprints', `${slug(session)}.${String(record.createdAt).replace(/[:.]/g, '-')}.json`);
105
136
 
@@ -110,7 +141,22 @@ function createFingerprintService({ dir, env } = {}) {
110
141
  const { fp, authorship, vault } = load();
111
142
  const key = authorship.resolveKey({ argv: [], env: scope });
112
143
  const history = path.join(authorship.dataDir(scope), 'history.jsonl');
113
- const target = vault.vaultTarget(scope);
144
+ if (vaultDisabled()) {
145
+ return {
146
+ ok: true,
147
+ configured: Boolean(key.secret),
148
+ keySource: key.source,
149
+ keyPath: key.path,
150
+ keyFingerprint: key.secret ? fp.keyFingerprint(key.secret) : null,
151
+ publicKey: key.public,
152
+ defaultKeyPath: authorship.defaultKeyPath(scope),
153
+ historyPath: history,
154
+ historyPresent: fs.existsSync(history),
155
+ dataDir: authorship.dataDir(scope),
156
+ vault: { backend: 'off', remote: null, root: null, recordsDir: null },
157
+ };
158
+ }
159
+ const target = vault.vaultTarget(vaultScope());
114
160
  return {
115
161
  ok: true,
116
162
  configured: Boolean(key.secret),
@@ -125,7 +171,12 @@ function createFingerprintService({ dir, env } = {}) {
125
171
  // Where a publish goes, so the panel can say it before the user asks
126
172
  // for one — and so a misconfigured root is visible, not discovered by
127
173
  // a record landing in the wrong folder.
128
- vault: { remote: target.remote, root: target.root, recordsDir: `${target.root}/${vault.RECORDS_DIR}` },
174
+ vault: {
175
+ backend: target.backend,
176
+ remote: target.remote,
177
+ root: target.root,
178
+ recordsDir: target.backend === vault.BACKEND_CLOUD ? vault.CLOUD_PREFIX : `${target.root}/${vault.RECORDS_DIR}`,
179
+ },
129
180
  };
130
181
  } catch (e) {
131
182
  return { ok: false, error: (e && e.message) || String(e) };
@@ -155,7 +206,7 @@ function createFingerprintService({ dir, env } = {}) {
155
206
  * session carry the same behaviour digest — and the same AI digest, from the
156
207
  * same turns.
157
208
  */
158
- mint({ history = null, session = null, turns = null, label = null, account = null,
209
+ async mint({ history = null, session = null, turns = null, label = null, account = null,
159
210
  model = null, tools = null, codeRoots = null, codeDirty = null, out = null,
160
211
  createdAt = null, publish = false, aiSecret = undefined, aiModel = null } = {}) {
161
212
  try {
@@ -196,11 +247,15 @@ function createFingerprintService({ dir, env } = {}) {
196
247
  let vault = null;
197
248
  let vaultError = null;
198
249
  if (publish) {
199
- try {
200
- const key = authorship.resolveKey({ argv: [], env: scope });
201
- vault = load().vault.publish({ file, argv: [], env: scope, secret: key.secret });
202
- } catch (e) {
203
- vaultError = (e && e.message) || String(e);
250
+ if (vaultDisabled()) {
251
+ vaultError = VAULT_OFF_ERROR;
252
+ } else {
253
+ try {
254
+ const key = authorship.resolveKey({ argv: [], env: scope });
255
+ vault = await load().vault.publish({ file, argv: [], env: vaultScope(), secret: key.secret });
256
+ } catch (e) {
257
+ vaultError = (e && e.message) || String(e);
258
+ }
204
259
  }
205
260
  }
206
261
  return {
@@ -225,13 +280,14 @@ function createFingerprintService({ dir, env } = {}) {
225
280
  * preferred path is the file, because the sha256 in the anchor is then about
226
281
  * the bytes on disk rather than a re-serialisation of them.
227
282
  */
228
- publish({ file = null, record = null, dryRun = false } = {}) {
283
+ async publish({ file = null, record = null, dryRun = false } = {}) {
284
+ if (vaultDisabled()) return { ok: false, error: VAULT_OFF_ERROR };
229
285
  try {
230
286
  const { authorship, vault } = load();
231
287
  const key = authorship.resolveKey({ argv: [], env: scope });
232
288
  return {
233
289
  ok: true,
234
- ...vault.publish({ file, record, argv: [], env: scope, secret: key.secret, dryRun }),
290
+ ...(await vault.publish({ file, record, argv: [], env: vaultScope(), secret: key.secret, dryRun })),
235
291
  };
236
292
  } catch (e) {
237
293
  return { ok: false, error: (e && e.message) || String(e) };
@@ -246,22 +302,24 @@ function createFingerprintService({ dir, env } = {}) {
246
302
  * sealed it has moved on" are three different situations and a single
247
303
  * boolean would flatten them into one that hides the interesting case.
248
304
  */
249
- anchor({ file = null, record = null, anchor = null } = {}) {
305
+ async anchor({ file = null, record = null, anchor = null } = {}) {
306
+ if (vaultDisabled()) return { ok: false, error: VAULT_OFF_ERROR };
250
307
  try {
251
308
  const { authorship, vault } = load();
252
309
  if (!file && !record && !anchor) return { ok: false, error: 'anchor needs a record or an anchor' };
253
310
  const key = authorship.resolveKey({ argv: [], env: scope });
254
- return { ok: true, ...vault.checkAnchor({ file, record, anchor, argv: [], env: scope, key }) };
311
+ return { ok: true, ...(await vault.checkAnchor({ file, record, anchor, argv: [], env: vaultScope(), key })) };
255
312
  } catch (e) {
256
313
  return { ok: false, error: (e && e.message) || String(e) };
257
314
  }
258
315
  },
259
316
 
260
317
  /** What is in the vault. */
261
- vaultList() {
318
+ async vaultList() {
319
+ if (vaultDisabled()) return { ok: false, error: VAULT_OFF_ERROR };
262
320
  try {
263
321
  const { vault } = load();
264
- return { ok: true, ...vault.list({ argv: [], env: scope }) };
322
+ return { ok: true, ...(await vault.list({ argv: [], env: vaultScope() })) };
265
323
  } catch (e) {
266
324
  return { ok: false, error: (e && e.message) || String(e) };
267
325
  }
@@ -272,11 +330,12 @@ function createFingerprintService({ dir, env } = {}) {
272
330
  * already there before publishing was restricted. A finding is a fact about
273
331
  * the vault, not about this app, so it is returned rather than acted on.
274
332
  */
275
- vaultAudit() {
333
+ async vaultAudit() {
334
+ if (vaultDisabled()) return { ok: false, error: VAULT_OFF_ERROR };
276
335
  try {
277
336
  const { authorship, vault } = load();
278
337
  const key = authorship.resolveKey({ argv: [], env: scope });
279
- return { ok: true, ...vault.audit({ argv: [], env: scope, secret: key.secret || null }) };
338
+ return { ok: true, ...(await vault.audit({ argv: [], env: vaultScope(), secret: key.secret || null })) };
280
339
  } catch (e) {
281
340
  return { ok: false, error: (e && e.message) || String(e) };
282
341
  }
@@ -116,6 +116,23 @@ function claudeOAuthToken(env) {
116
116
  */
117
117
  const SHADOWING_ENV_VARS = Object.freeze(['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN']);
118
118
 
119
+ /**
120
+ * Whether `value` is shaped like an Anthropic console API key.
121
+ *
122
+ * Deliberately a SHAPE test, not a validity test. This process cannot know
123
+ * whether a key is live without spending it, so it does not try: it answers
124
+ * only "could this possibly be the console credential that is allowed to
125
+ * outrank a subscription". Anthropic console keys are `sk-ant-`-prefixed. The
126
+ * check is case-sensitive and anchored, because that is the literal the vendor
127
+ * uses — a case-insensitive match would accept `SK-ANT-` junk.
128
+ *
129
+ * @param {string} value
130
+ * @returns {boolean}
131
+ */
132
+ function isAnthropicConsoleKey(value) {
133
+ return typeof value === 'string' && /^sk-ant-[A-Za-z0-9_-]{8,}$/.test(value.trim());
134
+ }
135
+
119
136
  /**
120
137
  * The env for one `claude` child, with a shadowing value that cannot be a
121
138
  * credential removed.
@@ -151,28 +168,69 @@ const SHADOWING_ENV_VARS = Object.freeze(['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_T
151
168
  * value that provably cannot be a key is dropped, and the drop is REPORTED
152
169
  * rather than silent (see `envDroppedLine`).
153
170
  *
171
+ * ── the second rule: a length floor is not a shape (measured, 2026-10-21) ───
172
+ *
173
+ * The floor above answers "is this too short to be anything?" and nothing more.
174
+ * A 35-character value that is NOT an Anthropic console key clears it, still
175
+ * wins the CLI's precedence, and still takes the plan down — so the floor alone
176
+ * reproduced the original bug one paste later. Measured state that produced it:
177
+ *
178
+ * ~/.aegiscode/.env held ANTHROPIC_API_KEY=<35 chars, not sk-ant-*>
179
+ * ~/.claude/.credentials.json held a valid `claudeAiOauth` for plan `pro`
180
+ * `aegiscode -p "say hi" --json`
181
+ * → {"text":"Invalid API key · Fix external API key","model":"sonnet",
182
+ * "tokens":0,"balance_eur":1.54} exit 0, in 2.3 s
183
+ *
184
+ * and the child was spawned with the stale value present (`ANTHROPIC_LEN=35`,
185
+ * observed by a fake `claude` on PATH). Unsetting the variable in the SHELL did
186
+ * not help either: this host loads `~/.aegiscode/.env` into `process.env`
187
+ * itself, so the file is an input no caller can undo.
188
+ *
189
+ * So when a subscription IS present, only a value shaped like an Anthropic
190
+ * console key may shadow it. That is the narrowest rule that keeps the promise
191
+ * the old comment made — "a user who set one deliberately is asking for the
192
+ * pay-per-token path" — while refusing a value that provably is not one. With
193
+ * no subscription there is nothing to shadow and no opinion is taken: any value
194
+ * clearing the floor passes, exactly as before.
195
+ *
154
196
  * @param {object} [baseEnv] the caller's env bag
155
197
  * @param {string} [token] a `CLAUDE_CODE_OAUTH_TOKEN` to hand the child
156
- * @returns {{env: object, dropped: Array<{name: string, length: number}>}}
198
+ * @param {{hasSubscription?: boolean}} [opts] whether a subscription is present
199
+ * (see the second rule above); absent means "assume not", which keeps every
200
+ * pre-existing caller and test on the old behaviour.
201
+ * @returns {{env: object, dropped: Array<{name: string, length: number, reason: string}>}}
157
202
  */
158
- function childEnvPlan(baseEnv, token) {
203
+ function childEnvPlan(baseEnv, token, opts) {
159
204
  const env = { ...(baseEnv || {}) };
160
205
  const dropped = [];
161
206
  const usable = envFileLib && typeof envFileLib.credentialLooksUsable === 'function'
162
207
  ? envFileLib.credentialLooksUsable
163
208
  : null;
209
+ // Only a present subscription can be shadowed, so it is the only case that
210
+ // earns the stricter shape rule (see the docstring's second section).
211
+ const hasSubscription = !!(opts && opts.hasSubscription);
164
212
  // A host whose env module predates the floor keeps today's behaviour (the
165
213
  // variable passes through): a second copy of the floor here is the drift
166
214
  // client/env-file.js exists to prevent.
167
215
  if (usable) {
168
216
  for (const name of SHADOWING_ENV_VARS) {
169
217
  const value = env[name];
170
- if (typeof value !== 'string' || usable(value)) continue;
218
+ if (typeof value !== 'string') continue;
219
+ // Two independent reasons to refuse, and they are reported apart because
220
+ // they mean different things to the user: too short is junk, while a
221
+ // well-formed value that is not a console key is a key for something
222
+ // else that would silently outrank the plan they are paying for.
223
+ let reason = null;
224
+ if (!usable(value)) reason = 'too-short';
225
+ else if (hasSubscription && name === 'ANTHROPIC_API_KEY' && !isAnthropicConsoleKey(value)) {
226
+ reason = 'not-a-console-key';
227
+ }
228
+ if (!reason) continue;
171
229
  delete env[name];
172
230
  // A variable set to '' shadows nothing worth reporting — it is removed
173
231
  // (harmless) but the user is not told about a value they never gave.
174
232
  const length = value.trim().length;
175
- if (length > 0) dropped.push({ name, length });
233
+ if (length > 0) dropped.push({ name, length, reason });
176
234
  }
177
235
  }
178
236
  if (token) env.CLAUDE_CODE_OAUTH_TOKEN = token;
@@ -183,20 +241,51 @@ function childEnvPlan(baseEnv, token) {
183
241
  function envDroppedLine(dropped) {
184
242
  const list = Array.isArray(dropped) ? dropped : [];
185
243
  if (!list.length) return null;
186
- const names = list.map((d) => `${d.name} (${d.length} character${d.length === 1 ? '' : 's'})`).join(', ');
187
- return `aegis: not passed to the claude child — ${names}: too short to be a credential, and the CLI would have spent it instead of your subscription`;
244
+ const shaped = list.filter((d) => d && d.reason === 'not-a-console-key');
245
+ const short = list.filter((d) => !d || d.reason !== 'not-a-console-key');
246
+ const parts = [];
247
+ if (short.length) {
248
+ const names = short.map((d) => `${d.name} (${d.length} character${d.length === 1 ? '' : 's'})`).join(', ');
249
+ parts.push(`${names}: too short to be a credential, and the CLI would have spent it instead of your subscription`);
250
+ }
251
+ if (shaped.length) {
252
+ const names = shaped.map((d) => `${d.name} (${d.length} characters)`).join(', ');
253
+ parts.push(
254
+ `${names}: not an Anthropic console key (\`sk-ant-\`), and the CLI would have spent it instead of your subscription`
255
+ );
256
+ }
257
+ return `aegis: not passed to the claude child — ${parts.join('; ')}`;
188
258
  }
189
259
 
190
- /** The shadowing value in `env` that cannot be a credential, if any (see above). */
191
- function shadowingValue(env) {
260
+ /**
261
+ * The shadowing value in `env` that cannot be spent as the credential the CLI
262
+ * would take it for, if any (see the two rules in `childEnvPlan`).
263
+ *
264
+ * This must agree with `childEnvPlan` exactly: a status surface that says "no
265
+ * shadowing value" while the child is in fact being handed one is worse than no
266
+ * report at all. Both reasons are therefore reported, and each carries the same
267
+ * `reason` tag the drop report uses so the two lines cannot drift apart.
268
+ *
269
+ * @param {object} env
270
+ * @param {{hasSubscription?: boolean}} [opts]
271
+ * @returns {{name: string, length: number, reason: string}|null}
272
+ */
273
+ function shadowingValue(env, opts) {
192
274
  const bag = env || {};
193
275
  const usable = envFileLib && typeof envFileLib.credentialLooksUsable === 'function'
194
276
  ? envFileLib.credentialLooksUsable
195
277
  : null;
196
278
  if (!usable) return null;
279
+ const hasSubscription = !!(opts && opts.hasSubscription);
197
280
  for (const name of SHADOWING_ENV_VARS) {
198
281
  const value = bag[name];
199
- if (typeof value === 'string' && !usable(value)) return { name, length: value.trim().length };
282
+ if (typeof value !== 'string') continue;
283
+ let reason = null;
284
+ if (!usable(value)) reason = 'too-short';
285
+ else if (hasSubscription && name === 'ANTHROPIC_API_KEY' && !isAnthropicConsoleKey(value)) {
286
+ reason = 'not-a-console-key';
287
+ }
288
+ if (reason) return { name, length: value.trim().length, reason };
200
289
  }
201
290
  return null;
202
291
  }
@@ -264,7 +353,30 @@ function claudeProStatus(opts) {
264
353
  // child is fixed regardless (childEnvPlan); this reports it so a status
265
354
  // surface can say why, rather than leaving `apiKeySource: ANTHROPIC_API_KEY`
266
355
  // to be discovered from `claude auth status` by hand.
267
- out.shadowed = shadowingValue(env);
356
+ // A SUBSCRIPTION — the thing a stray `ANTHROPIC_API_KEY` would outrank — is an
357
+ // `oauth` plan or a `CLAUDE_CODE_OAUTH_TOKEN` handed to the child. A
358
+ // `credentials.json` holding a plain API key is not: there is no plan there to
359
+ // shadow, so the strict shape rule must not fire and drop the user's own key.
360
+ // This is computed once and published, so the report and the child env read
361
+ // ONE answer instead of each deciding for itself.
362
+ out.subscriptionPresent = out.loggedIn && out.kind !== 'apikey';
363
+ // The stricter rule only applies when a subscription is present, so the
364
+ // subscription this function just resolved is the same input the child env
365
+ // gets. Passing it here is what keeps `shadowed` and the drop report agreeing.
366
+ out.shadowed = shadowingValue(env, { hasSubscription: out.subscriptionPresent });
367
+ // "Can this host serve a `pro` turn at all?" is NOT the same question as
368
+ // "is there a subscription to shadow?" (above). A pin is phantom only when
369
+ // nothing usable survives to the child, and a bare API key in
370
+ // `credentials.json` (`kind: 'apikey'`) is a route with no plan behind it —
371
+ // so `subscriptionPresent` excludes it deliberately, while callability must
372
+ // count it. Answered by running the SAME planner the child env comes from, so
373
+ // this cannot disagree with what `claude` is actually handed: a variable the
374
+ // planner drops is not a credential, whatever it looks like in `env`.
375
+ const plan = childEnvPlan(env, null, { hasSubscription: out.subscriptionPresent });
376
+ out.credentialPresent = out.loggedIn
377
+ || typeof plan.env.ANTHROPIC_API_KEY === 'string'
378
+ || typeof plan.env.ANTHROPIC_AUTH_TOKEN === 'string'
379
+ || typeof plan.env.CLAUDE_CODE_OAUTH_TOKEN === 'string';
268
380
  return out;
269
381
  }
270
382
 
@@ -633,7 +745,18 @@ function runClaudePro(opts, deps) {
633
745
  // "API key: he" and the subscription is unreachable — see childEnvPlan.
634
746
  const baseEnv = o.env || process.env;
635
747
  const token = claudeOAuthToken(baseEnv);
636
- const plan = childEnvPlan(baseEnv, token);
748
+ // Whether a subscription is present decides whether a non-console
749
+ // `ANTHROPIC_API_KEY` may shadow it (childEnvPlan's second rule). It is read
750
+ // from the same credential file `claudeProStatus` reports on, and a failure
751
+ // to read it must NOT weaken the child's env: an unreadable file means "no
752
+ // subscription proven", which is the old behaviour, not a crash mid-turn.
753
+ let hasSubscription = false;
754
+ try {
755
+ hasSubscription = !!claudeProStatus({ env: baseEnv }).subscriptionPresent;
756
+ } catch {
757
+ /* unprovable subscription → the floor-only rule, exactly as before */
758
+ }
759
+ const plan = childEnvPlan(baseEnv, token, { hasSubscription });
637
760
  const childEnv = plan.env;
638
761
  const droppedLine = envDroppedLine(plan.dropped);
639
762
  if (droppedLine && o.onDelta) {