aegiscode 6.8.30 → 6.8.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/config.js CHANGED
@@ -19,17 +19,32 @@ const { writePrivateFile } = require('./securefs.js');
19
19
  // The data dir is defined once, in the shared credential module, because the
20
20
  // desktop app and the MCP plugin now read and write the same store: a second
21
21
  // definition here is how the hosts would drift back apart.
22
- const { credentials } = require('./shared.js');
22
+ const { credentials, prefs } = require('./shared.js');
23
23
 
24
24
  /** Data directory: $AEGISCODE_HOME or ~/.aegiscode (see client/credentials.js). */
25
25
  function aegisDir(env) {
26
26
  return credentials.aegisHome(env);
27
27
  }
28
28
 
29
+ /**
30
+ * The host-local config file (`$AEGISCODE_HOME/config.json`).
31
+ *
32
+ * Since AEGISONE D5 this holds only what is HOST-LOCAL: the update-check cache,
33
+ * `lastCwd`, and anything the CLI alone owns (a stored `provider`/`baseURL` row,
34
+ * the `light` palette flag). Non-secret *preferences* live in the shared store
35
+ * (`$AEGISCODE_HOME/preferences.json`, client/prefs.js) so the window and the
36
+ * shell read one value instead of two that can disagree — see `SHARED_KEYS`, and
37
+ * `loadConfig()`/`updateConfig()` for the split and the one-way migration.
38
+ */
29
39
  function configPath() {
30
40
  return path.join(aegisDir(), 'config.json');
31
41
  }
32
42
 
43
+ /** The shared non-secret preference file the desktop's settings store writes too. */
44
+ function prefsPath() {
45
+ return prefs.prefsFile({ dir: aegisDir() });
46
+ }
47
+
33
48
  /**
34
49
  * True once the user has run before — the config file is written during
35
50
  * onboarding (and on the first preference change). Used to gate the trust
@@ -165,31 +180,168 @@ function setMemoryPersist(on) {
165
180
  });
166
181
  }
167
182
 
168
- /** Read the config, falling back to defaults (never throws). */
169
- function loadConfig() {
183
+ /**
184
+ * The config keys that live in the SHARED store (D5) rather than in this host's
185
+ * config.json: the non-secret preferences the window and the shell must agree
186
+ * about. The list itself lives in client/prefs.js (`SHARED_PREF_KEYS`) because
187
+ * the desktop's settings store reads it too — one definition, two readers.
188
+ */
189
+ const SHARED_KEYS = new Set(prefs.SHARED_PREF_KEYS);
190
+
191
+ /** The subset of `obj` whose keys live in the shared store. */
192
+ function pickShared(obj) {
193
+ const out = {};
194
+ for (const k of Object.keys(obj || {})) if (SHARED_KEYS.has(k)) out[k] = obj[k];
195
+ return out;
196
+ }
197
+
198
+ /** Read the host-local config file (never throws; {} when absent/corrupt). */
199
+ function readHostConfig() {
170
200
  try {
171
- const raw = fs.readFileSync(configPath(), 'utf8');
172
- const parsed = JSON.parse(raw);
173
- if (parsed && typeof parsed === 'object') return { ...DEFAULT_CONFIG, ...parsed };
201
+ const parsed = JSON.parse(fs.readFileSync(configPath(), 'utf8'));
202
+ if (parsed && typeof parsed === 'object') return parsed;
174
203
  } catch {}
175
- return { ...DEFAULT_CONFIG };
204
+ return {};
205
+ }
206
+
207
+ /** Read the shared preference store (`$AEGISCODE_HOME/preferences.json`). */
208
+ function readSharedPrefs() {
209
+ try {
210
+ return prefs.load({ dir: aegisDir() });
211
+ } catch {
212
+ return {};
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Read the config, falling back to defaults (never throws).
218
+ *
219
+ * Three layers, lowest first: `DEFAULT_CONFIG`, this host's config.json, the
220
+ * shared preference store. The shared store wins because it is the store both
221
+ * faces write — reading it second is what makes a preference set in the window
222
+ * visible in the shell with no cloud round trip.
223
+ *
224
+ * A config.json written by an older CLI still holds the shared keys inline
225
+ * (`themeIndex`, `model`, `effort`, …). That legacy copy is read here — this is
226
+ * the read half of the one-way migration — but never written back: the next
227
+ * `updateConfig()` moves it into the shared store and drops it from
228
+ * config.json, so one preference ends up in exactly one place.
229
+ */
230
+ function loadConfig() {
231
+ const host = readHostConfig();
232
+ const shared = readSharedPrefs();
233
+ const merged = { ...DEFAULT_CONFIG, ...host };
234
+ const migrated = {};
235
+ for (const k of SHARED_KEYS) {
236
+ const inHost = host[k] !== undefined;
237
+ const inShared = shared[k] !== undefined;
238
+ if (inHost && inShared) {
239
+ // The shared store outranks the host copy — except when its value is an
240
+ // explicit `null` and the host copy is a real one. `sharedView()` writes
241
+ // EVERY shared key on every save, filling an unset `model`/`effort` with
242
+ // its `DEFAULT_CONFIG` value, so `null` there is usually the default and
243
+ // not a decision. Reading it as one swallowed a legacy pin: a config.json
244
+ // from a pre-D5 CLI keeps `model` inline, and the moment the new code
245
+ // saved any other preference it also wrote `model: null` to the shared
246
+ // store, which then beat the pin on every launch. That made the read half
247
+ // of the one-way migration described above dead code — the exact
248
+ // regression `test/cli-models.test.mjs` pins. A real value in the shared
249
+ // store still wins (that is what makes a pin chosen in the window visible
250
+ // in the shell), and once `updateConfig()` runs, shared keys are dropped
251
+ // from config.json, so there is no stale host copy left to prefer.
252
+ migrated[k] = shared[k] === null && host[k] !== null ? host[k] : shared[k];
253
+ } else if (inHost) {
254
+ migrated[k] = host[k];
255
+ } else if (inShared) {
256
+ migrated[k] = shared[k];
257
+ }
258
+ }
259
+ return { ...merged, ...migrated };
260
+ }
261
+
262
+ /**
263
+ * The shared store's view of this config.
264
+ *
265
+ * `model: null` / `effort: null` are written out explicitly rather than left
266
+ * absent: the shared document is the resolved preference set both faces read,
267
+ * and "no pin" is itself a stored decision (`DEFAULT_CONFIG` says so, and
268
+ * `validatePinnedModel()` clears a phantom pin by writing `null`).
269
+ *
270
+ * `memoryPersist` is the one exception, and deliberately: filling it with the
271
+ * default would defeat `memoryPersistState()`'s legacy migration, which exists
272
+ * so that an explicit pre-flip `cloudSync: false` on disk is never quietly
273
+ * opted back in. So it is written only when it is actually known — set by this
274
+ * patch, already in the shared store, already in config.json, or stated by the
275
+ * legacy `cloudSync` key.
276
+ *
277
+ * `confirmMode` (ON8) is the second, for the same reason and one more: it is a
278
+ * posture, and `DEFAULT_CONFIG` has no entry for it, so the default-fill would
279
+ * have written `undefined` — which `prefs.set()` drops. Every unrelated config
280
+ * save (`/theme`, onboarding, an update-check cache write) would then DELETE the
281
+ * gate's one shared copy, silently reverting the window's switch and the shell's
282
+ * `/yolo` to the rule file on the next launch. And filling it with `true` instead
283
+ * would be worse, not better: an install whose desktop still holds a legacy
284
+ * `__confirmMode: { enabled: false }` would be flipped ON by the first CLI save,
285
+ * which is exactly the migration `client/prefs.js`'s `adopt()` exists to get
286
+ * right. So: written when this patch states it, or when the store already has
287
+ * one, and otherwise left alone for the rule file to answer.
288
+ */
289
+ const WRITE_WHEN_KNOWN = new Set(['memoryPersist', 'confirmMode']);
290
+
291
+ function sharedView(patch, host, shared) {
292
+ const view = {};
293
+ for (const k of SHARED_KEYS) {
294
+ if (WRITE_WHEN_KNOWN.has(k)) continue;
295
+ if (patch[k] !== undefined) view[k] = patch[k];
296
+ else if (shared[k] !== undefined) view[k] = shared[k];
297
+ else if (host[k] !== undefined) view[k] = host[k];
298
+ else view[k] = DEFAULT_CONFIG[k];
299
+ }
300
+ if (patch.memoryPersist !== undefined) view.memoryPersist = patch.memoryPersist;
301
+ else if (shared.memoryPersist !== undefined) view.memoryPersist = shared.memoryPersist;
302
+ else if (host.memoryPersist !== undefined) view.memoryPersist = host.memoryPersist;
303
+ else if (typeof host[LEGACY_MEMORY_PERSIST_KEY] === 'boolean') {
304
+ view.memoryPersist = host[LEGACY_MEMORY_PERSIST_KEY];
305
+ }
306
+ if (patch.confirmMode !== undefined) view.confirmMode = patch.confirmMode;
307
+ else if (shared.confirmMode !== undefined) view.confirmMode = shared.confirmMode;
308
+ return view;
176
309
  }
177
310
 
178
311
  /**
179
312
  * Merge a patch into the config on disk and return the merged config.
180
- * A no-op that still returns the merged view when the data dir is unwritable.
313
+ *
314
+ * Split by destination, never duplicated: the shared keys go to
315
+ * `$AEGISCODE_HOME/preferences.json`, everything else to this host's
316
+ * config.json (with any shared key a legacy file still carries dropped, which
317
+ * completes the one-way migration). A no-op that still returns the merged view
318
+ * when a file cannot be written.
181
319
  */
182
320
  function updateConfig(patch) {
183
- const next = { ...loadConfig(), ...patch };
321
+ const p = patch || {};
322
+ const host = readHostConfig();
323
+ const sharedNow = readSharedPrefs();
324
+ const sharedKeys = pickShared(p);
325
+ const hostKeys = { ...p };
326
+ for (const k of SHARED_KEYS) delete hostKeys[k];
327
+
328
+ try {
329
+ prefs.set(sharedView(p, host, sharedNow), { dir: aegisDir() });
330
+ } catch (e) {
331
+ if (process.env.AEGIS_HIST_DEBUG) console.error('[prefs] write failed:', e);
332
+ }
333
+
334
+ const hostNext = { ...DEFAULT_CONFIG, ...host, ...hostKeys };
335
+ for (const k of SHARED_KEYS) delete hostNext[k];
184
336
  try {
185
337
  // Atomic (tmp + rename) so a crash can't corrupt it, and written 0600:
186
338
  // config.json carries `provider` and `baseURL`, so a group-writable copy is
187
339
  // a way for another local account to repoint the session's endpoint.
188
- writePrivateFile(configPath(), JSON.stringify(next, null, 2) + '\n');
340
+ writePrivateFile(configPath(), JSON.stringify(hostNext, null, 2) + '\n');
189
341
  } catch (e) {
190
342
  if (process.env.AEGIS_HIST_DEBUG) console.error('[config] write failed:', e);
191
343
  }
192
- return next;
344
+ return loadConfig();
193
345
  }
194
346
 
195
347
  // ── Permissions (~/.aegiscode/permissions.json) ─────────────────────────────
@@ -256,9 +408,103 @@ function removePermissionRule(list, pattern, rules) {
256
408
  return { removed: patterns.length !== rules[key].length, rules: { ...rules, [key]: patterns } };
257
409
  }
258
410
 
411
+ // ── The confirmation posture (AEGISONE ON8, decision D8 rule 1) ─────────────
412
+ //
413
+ // "One posture, two faces": the window's *Confirm before running tools* switch
414
+ // and this face's gate (app.js's `getConfirmMode`, which the engine reads on
415
+ // every tool round) answer the same question — may a mutating call run
416
+ // unattended? — so they are one value, stored once, in the shared preference
417
+ // store both hosts read (`confirmMode`).
418
+ //
419
+ // Why the writer lives HERE and not in permissions.js: this module owns BOTH
420
+ // files, so it is the only place that can keep them consistent in one step.
421
+ // permissions.json's `defaultMode` is NOT a second copy of the posture to be
422
+ // retired — it is a field of the rule schema `evalPermission` reads (an
423
+ // explicit `ask` is what makes the rule engine prompt on an unmatched call), so
424
+ // it stays, and it is written in lockstep with the shared key. Then the read
425
+ // order below is consistent by construction: the shared key answers, and it
426
+ // cannot disagree with the file because nothing writes one without the other.
427
+ //
428
+ // Rule 2 is untouched and unreachable from here: the ÆGIS covenant's
429
+ // `preActionAssert` and the avatar's `write|shell|network|offDevice` approvals
430
+ // are asserted above this gate, in the engine, on purpose (see
431
+ // desktop/lib/local/engine.js's "DO NOT MOVE THIS BELOW A SHORT-CIRCUIT"). A
432
+ // posture is the *configurable* half and can only ever move that half.
433
+
434
+ /**
435
+ * The posture, resolved from the two stores, shared store first.
436
+ *
437
+ * @returns {{enabled: boolean, source: 'shared'|'rules'|'default'}}
438
+ * `enabled` true means the gate is UP (confirmations on), which is the
439
+ * polarity the window's switch and `getConfirmMode()` both use.
440
+ *
441
+ * `rules` is the un-migrated fallback rather than a competitor: an install that
442
+ * never touched the switch has no `confirmMode` key and is answered by its rule
443
+ * file exactly as it was before this phase. Nothing here throws — a store that
444
+ * cannot be read keeps the gate UP, never off.
445
+ */
446
+ function confirmPosture() {
447
+ try {
448
+ const shared = readSharedPrefs().confirmMode;
449
+ if (typeof shared === 'boolean') return { enabled: shared, source: 'shared' };
450
+ } catch {
451
+ /* fall through to the rule file, then the safe default */
452
+ }
453
+ try {
454
+ const parsed = JSON.parse(fs.readFileSync(permissionsPath(), 'utf8'));
455
+ if (parsed && typeof parsed === 'object' && typeof parsed.defaultMode === 'string') {
456
+ // The pre-ON8 reading, verbatim: anything that is not an explicit `allow`
457
+ // leaves the gate up.
458
+ return { enabled: parsed.defaultMode !== 'allow', source: 'rules' };
459
+ }
460
+ } catch {
461
+ /* absent or corrupt: the default below is ON */
462
+ }
463
+ return { enabled: true, source: 'default' };
464
+ }
465
+
466
+ /**
467
+ * Set the posture: the shared key (both faces) + the rule file's residual.
468
+ *
469
+ * Called by every command that means "confirmations on/off" — /confirm, /yolo,
470
+ * /multiyolo and the flag the desktop's switch mirrors — so there is exactly one
471
+ * writer per fact and no command can leave the two stores disagreeing.
472
+ */
473
+ function setConfirmPosture(enabled) {
474
+ const want = Boolean(enabled);
475
+ try {
476
+ prefs.set({ confirmMode: want }, { dir: aegisDir() });
477
+ } catch (e) {
478
+ if (process.env.AEGIS_HIST_DEBUG) console.error('[prefs] write failed:', e);
479
+ }
480
+ savePermissions({ ...loadPermissions(), defaultMode: want ? 'ask' : 'allow' });
481
+ return want;
482
+ }
483
+
484
+ /**
485
+ * `/permissions default <ask|allow|deny>` — the residual mode, and the posture
486
+ * that follows from it.
487
+ *
488
+ * `ask`/`allow` ARE the posture (`setConfirmPosture`). `deny` is not: there is
489
+ * no "confirmations: deny" switch, so the residual is written to the file while
490
+ * the posture stays UP — a default of `deny` must not be implemented as a gate
491
+ * that stopped asking.
492
+ */
493
+ function setPermissionDefaultMode(mode) {
494
+ const want = mode === 'allow' ? 'allow' : mode === 'deny' ? 'deny' : 'ask';
495
+ try {
496
+ prefs.set({ confirmMode: want !== 'allow' }, { dir: aegisDir() });
497
+ } catch (e) {
498
+ if (process.env.AEGIS_HIST_DEBUG) console.error('[prefs] write failed:', e);
499
+ }
500
+ savePermissions({ ...loadPermissions(), defaultMode: want });
501
+ return want;
502
+ }
503
+
259
504
  module.exports = {
260
505
  aegisDir,
261
506
  configPath,
507
+ prefsPath,
262
508
  configExists,
263
509
  permissionsPath,
264
510
  DEFAULT_CONFIG,
@@ -275,4 +521,7 @@ module.exports = {
275
521
  savePermissions,
276
522
  addPermissionRule,
277
523
  removePermissionRule,
524
+ confirmPosture,
525
+ setConfirmPosture,
526
+ setPermissionDefaultMode,
278
527
  };
@@ -263,6 +263,12 @@ function paymentView(snapshot, { host = 'cli', fee = null } = {}) {
263
263
  const entitled = snap.entitled === true;
264
264
  const feeLine = feeView(fee).line;
265
265
  const wall = entitled ? 'owned' : snap.wall === 'balance' ? 'balance' : 'plan';
266
+ // The paid tier's NAME, resolved from the server's plan string. `unlimited`
267
+ // is the tier currently sold (€9/mo); `pro` is the grandfathered €2 line —
268
+ // still paid, still named Pro, but no longer offered to anyone.
269
+ const tierName =
270
+ { unlimited: 'Unlimited', pro: 'Pro' }[String(snap.plan || '').toLowerCase()] ||
271
+ 'Unlimited';
266
272
  const which = wall === 'balance' ? BALANCE_WALL : BILLING_WALL;
267
273
  return {
268
274
  host,
@@ -291,11 +297,11 @@ function paymentView(snapshot, { host = 'cli', fee = null } = {}) {
291
297
  // bill and the pitch disagreed: a user was told BYOK costs "your provider's
292
298
  // cost" while aegis1 charged a handling fee on top of it.
293
299
  byok: feeLine,
294
- label: entitled ? 'Pro' : wall === 'balance' ? 'Top up' : 'Upgrade',
300
+ label: entitled ? tierName : wall === 'balance' ? 'Top up' : 'Upgrade',
295
301
  unlock:
296
302
  wall === 'balance'
297
- ? 'Unlock: top up the Token Bank, or upgrade to Pro'
298
- : 'Unlock: Pro, or any Token Bank balance',
303
+ ? 'Unlock: top up the Token Bank, or upgrade to Unlimited'
304
+ : 'Unlock: Unlimited, or any Token Bank balance',
299
305
  };
300
306
  }
301
307
 
package/src/panels.js CHANGED
@@ -967,7 +967,7 @@ function buildBilling(state, ctx = {}) {
967
967
  if (pay) {
968
968
  const { cols } = getSize();
969
969
  const mark = pay.paid ? '✔' : '✦';
970
- lines.push(...wrapRows(`${mark} ${str(pay.label) || 'Pro'}: ${str(pay.reason)}`, 2, pay.paid ? t.green : t.gold, cols));
970
+ lines.push(...wrapRows(`${mark} ${str(pay.label) || 'Unlimited'}: ${str(pay.reason)}`, 2, pay.paid ? t.green : t.gold, cols));
971
971
  // The honesty line rides every paid prompt, wrapped rather than clipped —
972
972
  // `padLine()` truncates, and a truncated honesty line would read as the
973
973
  // promise alone.
@@ -135,6 +135,95 @@ function evalPermission(toolName, args, rules = {}) {
135
135
  return 'allow';
136
136
  }
137
137
 
138
+ // ── "Skip permissions", resolved ONCE (AEGISONE, D8 rules 3 and 5) ──────────
139
+ //
140
+ // One resolver, two callers: `one/src/permissions.js` (the product's posture,
141
+ // which feeds a spawned Claude child's argv) and `cli/src/app.js` (this face's
142
+ // own confirmation gate). It lives HERE, beside `evalPermission`, so the rule
143
+ // engine and the skip resolver are one module rather than two spellings that
144
+ // can drift — the same reason D8 keeps one rule language.
145
+ //
146
+ // The three sources, and why they are three:
147
+ //
148
+ // flag `--dangerously-skip-permissions`, typed by a human. Always
149
+ // honored: it is the whole point of the flag, and rule 3 requires
150
+ // a one-line report of what it skipped (the caller prints it).
151
+ // productEnv `AEGISONE_SKIP_PERMISSIONS`, set by the `aegis` bin ONLY after
152
+ // it resolved the posture for the child it is about to spawn —
153
+ // i.e. the flag, already checked against rule 5. That indirection
154
+ // is what lets the terminal face honor `aegis … --dangerously-
155
+ // skip-permissions` without the face ever reading the *user's*
156
+ // environment for a skip it was not asked for.
157
+ // env `AEGIS_SKIP_PERMISSIONS`, the CI/queue spelling. It equals the
158
+ // flag only when the process is NON-interactive: a stray `export`
159
+ // in a shell profile must not be able to disarm a human's own
160
+ // gate (rule 5). Interactive + unasked ⇒ warn and ignore.
161
+ //
162
+ // It never returns why something was skipped beyond naming its source: what a
163
+ // skip DOES cover is rule 2's business, and rule 2's floors (the covenant
164
+ // assert and the four level-independent approvals) are not reachable from here
165
+ // at all.
166
+ const SKIP = Object.freeze({
167
+ flag: '--dangerously-skip-permissions',
168
+ env: 'AEGIS_SKIP_PERMISSIONS',
169
+ productEnv: 'AEGISONE_SKIP_PERMISSIONS',
170
+ truthy: Object.freeze(['1', 'true', 'yes', 'on']),
171
+ });
172
+
173
+ const isTruthyEnv = (v) => v !== undefined && SKIP.truthy.includes(String(v).trim().toLowerCase());
174
+
175
+ /** Rule 5's warning, spelled once so both callers print the same sentence. */
176
+ const SKIP_ENV_WARNING =
177
+ `AEGIS_SKIP_PERMISSIONS is set but this session is interactive — ignoring it. ` +
178
+ `Pass --dangerously-skip-permissions to skip the configurable gates on purpose; ` +
179
+ `the ÆGIS covenant and the write|shell|network|offDevice approvals are never skipped.`;
180
+
181
+ /**
182
+ * @param {object} [input]
183
+ * @param {boolean} [input.flag] the flag was explicitly passed
184
+ * @param {object} [input.env] environment to read (never mutated)
185
+ * @param {boolean} [input.interactive] both stdio ends are a terminal
186
+ * @returns {{skip: boolean, source: 'flag'|'product'|'env'|'env-ignored'|'none',
187
+ * flag: boolean, envSet: boolean, interactive: boolean,
188
+ * warned: boolean, warning: string|null}}
189
+ */
190
+ function resolveSkip(input = {}) {
191
+ const flag = input.flag === true;
192
+ const env = input.env || {};
193
+ const interactive = input.interactive === true;
194
+ const envSet = isTruthyEnv(env[SKIP.env]);
195
+ const productSet = isTruthyEnv(env[SKIP.productEnv]);
196
+
197
+ // src: flag → product → env(non-interactive). No `catch`-all default that
198
+ // could skip: every other path returns skip: false.
199
+ if (flag || productSet) {
200
+ return {
201
+ skip: true,
202
+ source: flag ? 'flag' : 'product',
203
+ flag,
204
+ envSet,
205
+ interactive,
206
+ warned: false,
207
+ warning: null,
208
+ };
209
+ }
210
+ if (envSet && !interactive) {
211
+ return { skip: true, source: 'env', flag, envSet, interactive, warned: false, warning: null };
212
+ }
213
+ if (envSet && interactive) {
214
+ return {
215
+ skip: false,
216
+ source: 'env-ignored',
217
+ flag,
218
+ envSet,
219
+ interactive,
220
+ warned: true,
221
+ warning: SKIP_ENV_WARNING,
222
+ };
223
+ }
224
+ return { skip: false, source: 'none', flag, envSet: false, interactive, warned: false, warning: null };
225
+ }
226
+
138
227
  module.exports = {
139
228
  canonTool,
140
229
  isShellTool,
@@ -142,4 +231,7 @@ module.exports = {
142
231
  matchRule,
143
232
  isMultiDirCommand,
144
233
  evalPermission,
234
+ SKIP,
235
+ SKIP_ENV_WARNING,
236
+ resolveSkip,
145
237
  };