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.
@@ -176,34 +176,38 @@ function upsert(items, entry) {
176
176
 
177
177
  /**
178
178
  * Queue a task. `cwd` is where the work happens (the worker runs the turn
179
- * with it as the tool loop's working directory), `model` is the AEGIS Cloud
180
- * model id to run it on, and `commit` asks the worker to commit what the task
181
- * changed (scoped — see autonomous.js).
179
+ * with it as the tool loop's working directory), `class` + `model` are the
180
+ * same pair engine.js's interactive turns use ('aegis'/'byok'/'local' and the
181
+ * model id that class expects), and `commit` asks the worker to commit what
182
+ * the task changed (scoped — see autonomous.js).
182
183
  *
183
- * AEGIS CLOUD ONLY, REFUSED AT THE DOOR. The worker sends every task out as
184
- * `class: 'aegis'`, so a model the pool does not serve cannot run here at all —
185
- * it is a task that fails minutes into a drain, after being picked up, with an
186
- * opaque server error. `model: null` stays legal: that is "no pick", and
187
- * autonomous.resolveModel fills in the pooled brain. Anything else has to pass
188
- * that module's own accept-list, so the CLI (`aegiscode autonomous add
189
- * --model`), the desktop card and a hand-written queue file are all held to the
190
- * same rule. Required lazily: queue.js is a dependency of autonomous.js, and
191
- * this check must not turn that into a load-order cycle.
184
+ * REFUSED AT THE DOOR, not three stages into a drain. `class` defaults to
185
+ * 'aegis' (every task queued before this field existed), and the (model,
186
+ * class) pair has to pass autonomous.js's own accept-list — the CLI
187
+ * (`aegiscode autonomous add --model`), the desktop card and a hand-written
188
+ * queue file are all held to the same rule, so a bad row fails here with a
189
+ * reason instead of coming back as an opaque server error after being picked
190
+ * up. `model: null` stays legal for the 'aegis' class only: that is "no
191
+ * pick", and autonomous.resolveModel fills in the pooled brain. 'byok' and
192
+ * 'local' have no such default and are refused if blank — see
193
+ * autonomous.modelRefusal. Required lazily: queue.js is a dependency of
194
+ * autonomous.js, and this check must not turn that into a load-order cycle.
192
195
  */
193
196
  function addTask(env, opts = {}) {
194
197
  const task = String(opts.task == null ? '' : opts.task).trim();
195
198
  if (!task) throw new Error('queue: a task needs text');
199
+ const autonomousMod = require('./autonomous.js');
200
+ const cls = autonomousMod.normalizeClass(opts.class);
196
201
  const statedModel = String(opts.model == null ? '' : opts.model).trim();
197
- if (statedModel) {
198
- const refusal = require('./autonomous.js').modelRefusal(statedModel);
199
- if (refusal) throw new Error(`queue: ${refusal}`);
200
- }
202
+ const refusal = autonomousMod.modelRefusal(statedModel, cls);
203
+ if (refusal) throw new Error(`queue: ${refusal}`);
201
204
  const items = loadQueue(env);
202
205
  const now = opts.now || Date.now();
203
206
  const entry = {
204
207
  id: nextId(items),
205
208
  task,
206
209
  cwd: opts.cwd || process.cwd(),
210
+ class: cls,
207
211
  model: statedModel || null,
208
212
  effort: opts.effort || null,
209
213
  workers: Number.isInteger(opts.workers) ? opts.workers : null,
@@ -13,12 +13,52 @@
13
13
  *
14
14
  * Pure Node + injectable storage dir / safeStorage so it unit-tests without
15
15
  * an Electron binary.
16
+ *
17
+ * AEGISONE D5 splits this store in two, along the line the repo already drew
18
+ * for sessions and credentials:
19
+ *
20
+ * <userData>/settings.json HOST-LOCAL — provider rows and the AEGIS key
21
+ * (`safeStorage` blobs a plain Node process
22
+ * cannot decrypt on Linux), plus machine/UI
23
+ * state. `SETTINGS_FILE`, below.
24
+ * $AEGISCODE_HOME/preferences.json SHARED, NON-SECRET — the preferences both
25
+ * faces must agree about. Owned by
26
+ * `client/prefs.js`; `sharedDir` (an
27
+ * injectable seam, defaulting to
28
+ * `credentials.aegisHome()`) locates it.
29
+ *
30
+ * So `getSharedPref`/`setSharedPref` and the memory-persist preference read and
31
+ * write the SHARED file, a legacy `<userData>` `__memoryPersist` namespace is
32
+ * adopted once and then only read as a fallback, and everything key-bearing
33
+ * stays here in `<userData>`. The shared store refuses key material outright
34
+ * (`client/prefs.js` `assertNoSecrets`), which is what keeps the split honest.
16
35
  */
17
36
 
18
37
  const fs = require('node:fs');
19
38
  const path = require('node:path');
20
39
  const { execFileSync } = require('node:child_process');
21
40
 
41
+ // The shared, non-secret preference store (AEGISONE D5) and the data-dir
42
+ // helper, resolved the same two ways lib/sync/sessions.js resolves the other
43
+ // cross-host modules: in-repo `<repo>/client/*.js`, packaged
44
+ // `desktop/vendor/*.js`. A missing module is a loud error rather than a second
45
+ // implementation — the drift this arrangement exists to prevent.
46
+ const prefs = (() => {
47
+ try {
48
+ return require('../../client/prefs.js');
49
+ } catch {
50
+ return require('../vendor/prefs.js');
51
+ }
52
+ })();
53
+
54
+ const credentials = (() => {
55
+ try {
56
+ return require('../../client/credentials.js');
57
+ } catch {
58
+ return require('../vendor/credentials.js');
59
+ }
60
+ })();
61
+
22
62
  // The local-model transport. Required for ONE function: `remoteRefusal`, the
23
63
  // fail-closed check that keeps a non-local base URL out of the `local` row. The
24
64
  // module owns that policy so this store, the engine and the CLI cannot drift
@@ -207,10 +247,77 @@ function restrictFile(file) {
207
247
  }
208
248
  }
209
249
 
210
- function createSettingsStore({ dir, safeStorage } = {}) {
250
+ function createSettingsStore({ dir, safeStorage, prefsDir, env } = {}) {
211
251
  if (!dir) throw new Error('createSettingsStore requires a storage dir');
212
252
  const file = path.join(dir, SETTINGS_FILE);
213
253
 
254
+ // --- the shared, non-secret preference store (AEGISONE D5) --------------
255
+ //
256
+ // `prefsDir` is the injectable seam (tests point it at a temp dir); the
257
+ // default is the same `$AEGISCODE_HOME` / `~/.aegiscode` directory the
258
+ // session store and the credential store already use, so an install has one
259
+ // answer to "where does this host keep its data".
260
+ const sharedDir = (() => {
261
+ const explicit = prefsDir === undefined || prefsDir === null ? '' : String(prefsDir).trim();
262
+ return explicit ? path.resolve(explicit) : credentials.aegisHome(env);
263
+ })();
264
+
265
+ // One-way, once, idempotent adoption of a pre-D5 `<userData>/settings.json`
266
+ // non-secret half (D5, step 2): an existing user's preference survives the
267
+ // upgrade. Best-effort — a store must still open when the legacy file is
268
+ // unreadable or the shared dir is unwritable.
269
+ const adoption = (() => {
270
+ try {
271
+ return prefs.adopt({ dir: sharedDir, legacyDir: dir });
272
+ } catch (err) {
273
+ // Best-effort, but NOT silent (AEGISONE ON8). Swallowing this throw is how
274
+ // `prefs.adopt()`'s refusal to write its own ledger went unnoticed — the
275
+ // migration failed for every install that had something to adopt, and the
276
+ // store reported a healthy `adopted: false`. A failed migration is a
277
+ // preference a user set that this launch did not honor, so it is reported
278
+ // where a developer will see it; a store must still open either way.
279
+ if (process.env.AEGISCODE_DEBUG || process.env.AEGIS_HIST_DEBUG) {
280
+ process.stderr.write(`aegis-desktop: shared-preference adoption failed: ${err.message}\n`);
281
+ }
282
+ return { adopted: false, keys: [], reason: `adoption failed: ${err.message}` };
283
+ }
284
+ })();
285
+
286
+ /** The shared preference file this store reads and writes (never a secret). */
287
+ function sharedFile() {
288
+ return prefs.prefsFile({ dir: sharedDir });
289
+ }
290
+
291
+ function loadShared() {
292
+ try {
293
+ return prefs.load({ dir: sharedDir });
294
+ } catch {
295
+ return {};
296
+ }
297
+ }
298
+
299
+ /**
300
+ * One shared preference, as the CLI's `config.js` stores it. Restricted to
301
+ * `SHARED_PREF_KEYS` — the window may read exactly the preferences the shell
302
+ * owns, not reach into arbitrary state in a file another host reads.
303
+ */
304
+ function getSharedPref(key) {
305
+ if (!prefs.SHARED_PREF_KEYS.includes(key)) {
306
+ throw new Error(`"${key}" is not a shared preference (see client/prefs.js SHARED_PREF_KEYS)`);
307
+ }
308
+ const value = loadShared()[key];
309
+ return value === undefined ? null : value;
310
+ }
311
+
312
+ /** Write one shared preference. Key material is refused by the store itself. */
313
+ function setSharedPref(key, value) {
314
+ if (!prefs.SHARED_PREF_KEYS.includes(key)) {
315
+ throw new Error(`"${key}" is not a shared preference (see client/prefs.js SHARED_PREF_KEYS)`);
316
+ }
317
+ prefs.set({ [key]: value }, { dir: sharedDir });
318
+ return getSharedPref(key);
319
+ }
320
+
214
321
  const safeAvailable = () =>
215
322
  Boolean(
216
323
  safeStorage &&
@@ -297,13 +404,21 @@ function createSettingsStore({ dir, safeStorage } = {}) {
297
404
  */
298
405
  function set(provider, { baseURL, key } = {}) {
299
406
  assertNotReserved(provider);
407
+ let nextBaseURL = baseURL;
300
408
  if (provider === LOCAL_NAMESPACE && baseURL) {
301
409
  const refusal = localTransport.remoteRefusal(baseURL);
302
410
  if (refusal) throw refusal;
411
+ // Persist the DIALABLE spelling, not the typed one: `localhost:11434` is
412
+ // how a user writes the address, and `http://localhost:11434` is what the
413
+ // probe and the chat call must dial (local.js `normalizeBase`, whose
414
+ // header explains why a scheme-less string is the common case rather than
415
+ // an edge one). Storing the raw form left every consumer to normalize for
416
+ // itself — and a consumer that forgot read the URL as a remote host.
417
+ nextBaseURL = localTransport.normalizeBase(baseURL);
303
418
  }
304
419
  const data = load();
305
420
  const cfg = data[provider] || {};
306
- if (baseURL !== undefined) cfg.baseURL = baseURL;
421
+ if (nextBaseURL !== undefined) cfg.baseURL = nextBaseURL;
307
422
  if (key !== undefined) cfg.key = key ? encrypt(key) : null;
308
423
  data[provider] = cfg;
309
424
  save(data);
@@ -419,26 +534,50 @@ function createSettingsStore({ dir, safeStorage } = {}) {
419
534
  return quickLauncherConfig();
420
535
  }
421
536
 
422
- // --- Confirm mode: reserved namespace, plain (unencrypted) preference ----
423
- // "Confirm before running tools" — the ON/OFF switch for the renderer's
424
- // tool-call approval gate (desktop/lib/local/engine.js gatedExecuteTool).
425
- // ON (default, and the behaviour shipped so far) means every mutating tool
426
- // call — exec / writeFile / editFile — is previewed and approved by the user
427
- // first. OFF means the engine runs them straight through, like a tool the
428
- // session already allowed. Nothing secret is stored here, so no encryption.
537
+ // --- Confirm mode: SHARED preference (AEGISONE ON8, decision D8 rule 1) --
538
+ // "Confirm before running tools" — the switch for the renderer's tool-call
539
+ // approval gate (desktop/lib/local/engine.js gatedExecuteTool). ON (default,
540
+ // and the behaviour shipped so far) means every mutating tool call — exec /
541
+ // writeFile / editFile — is previewed and approved by the user first. OFF
542
+ // means the engine runs them straight through, like a tool the session
543
+ // already allowed. Nothing secret is stored here, so no encryption.
544
+ //
545
+ // This preference MOVED to the shared store (`confirmMode`) because D8 rule 1
546
+ // is "one posture, two faces": a rule set in the window has to be the rule the
547
+ // shell honors. The pre-ON8 `__confirmMode` namespace in `<userData>` is
548
+ // adopted once (client/prefs.js `extractNonSecretHalf`) and still read as a
549
+ // fallback, never written — a second writer is the split-brain this replaced.
550
+ // The gate's polarity is unchanged: ON when unset, in both places, so an
551
+ // install that never touched the switch keeps the gate exactly as it was.
552
+
553
+ function confirmModeState() {
554
+ const shared = loadShared();
555
+ if (typeof shared.confirmMode === 'boolean') {
556
+ return { enabled: shared.confirmMode, source: 'config' };
557
+ }
558
+ const cfg = load()[CONFIRM_MODE_NAMESPACE] || {};
559
+ if (cfg.enabled === undefined) return { enabled: true, source: 'default' };
560
+ return { enabled: Boolean(cfg.enabled), source: 'config' };
561
+ }
429
562
 
430
563
  function getConfirmMode() {
431
- const cfg = load()[CONFIRM_MODE_NAMESPACE] || {};
432
- // Unset === true: an existing install that never touched the toggle keeps
433
- // the gate exactly as it was.
434
- return cfg.enabled === undefined ? true : Boolean(cfg.enabled);
564
+ return confirmModeState().enabled;
435
565
  }
436
566
 
437
567
  function setConfirmMode(enabled) {
568
+ prefs.set({ confirmMode: Boolean(enabled) }, { dir: sharedDir });
569
+ // One-way: retire the legacy copy in the same breath, so the posture exists
570
+ // in exactly one place from here on (the same retirement the memory-persist
571
+ // preference below does).
438
572
  const data = load();
439
- data[CONFIRM_MODE_NAMESPACE] = { enabled: Boolean(enabled) };
440
- save(data);
441
- return getConfirmMode();
573
+ if (data[CONFIRM_MODE_NAMESPACE] !== undefined) {
574
+ delete data[CONFIRM_MODE_NAMESPACE];
575
+ save(data);
576
+ }
577
+ // The boolean, not the state record: main.js's IPC reply is `{ enabled }`
578
+ // and it wraps the return value itself, so widening this would have put an
579
+ // object inside an object on the wire.
580
+ return confirmModeState().enabled;
442
581
  }
443
582
 
444
583
  // --- Telemetry: reserved namespace, plain preference, DEFAULT OFF --------
@@ -463,33 +602,49 @@ function createSettingsStore({ dir, safeStorage } = {}) {
463
602
  return getTelemetryOptIn();
464
603
  }
465
604
 
466
- // --- Persisting memory: reserved namespace, plain preference -------------
605
+ // --- Persisting memory: SHARED preference (AEGISONE D5) -----------------
467
606
  // The desktop's half of "persisting memory for every account": whether a
468
607
  // finished turn is pushed to cloud memory automatically. ON when unset —
469
608
  // an install that never touched the toggle behaves like cli/, whose
470
609
  // `memoryPersist` also defaults to on. `memoryPersistState()` reports the
471
- // SOURCE as well as the value so "off" and "never set" stay distinguishable;
472
- // lib/sync/persist-gate.js reads the same namespace straight off disk, and
473
- // the two must agree on the default.
610
+ // SOURCE as well as the value so "off" and "never set" stay distinguishable.
611
+ //
612
+ // This preference is the seam D5 closes: it lives in the SHARED store under
613
+ // the shell's own key (`memoryPersist`), so `/cloud memory off` in the
614
+ // terminal and the Settings toggle in the window are one value, not two that
615
+ // can disagree. The pre-D5 `__memoryPersist` namespace in `<userData>` is
616
+ // adopted once (above) and still read as a fallback, never written — a second
617
+ // writer is exactly the split-brain this replaced. lib/sync/persist-gate.js
618
+ // reads the same shared key straight off disk, in the main process, and the
619
+ // two must agree on the default.
474
620
 
475
621
  function getMemoryPersist() {
476
- const cfg = load()[MEMORY_PERSIST_NAMESPACE] || {};
477
- return cfg.enabled === undefined ? true : Boolean(cfg.enabled);
622
+ return memoryPersistState().enabled;
478
623
  }
479
624
 
480
625
  /** `{ enabled, source }` — mirrors cli/src/cloudsync.js memoryPersistState(). */
481
626
  function memoryPersistState() {
627
+ const shared = loadShared();
628
+ if (typeof shared.memoryPersist === 'boolean') {
629
+ return { enabled: shared.memoryPersist, source: 'config' };
630
+ }
631
+ // Pre-D5 install, or one whose adoption had nothing to move: the legacy
632
+ // namespace still answers until the first write retires it.
482
633
  const cfg = load()[MEMORY_PERSIST_NAMESPACE] || {};
483
- return {
484
- enabled: getMemoryPersist(),
485
- source: cfg.enabled === undefined ? 'default' : 'config',
486
- };
634
+ if (cfg.enabled === undefined) return { enabled: true, source: 'default' };
635
+ return { enabled: Boolean(cfg.enabled), source: 'config' };
487
636
  }
488
637
 
489
638
  function setMemoryPersist(enabled) {
639
+ prefs.set({ memoryPersist: Boolean(enabled) }, { dir: sharedDir });
640
+ // One-way: drop the legacy copy in the same breath so the preference exists
641
+ // in exactly one place from here on (the same retirement
642
+ // cli/src/config.js's setMemoryPersist() does for the pre-flip `cloudSync`).
490
643
  const data = load();
491
- data[MEMORY_PERSIST_NAMESPACE] = { enabled: Boolean(enabled) };
492
- save(data);
644
+ if (data[MEMORY_PERSIST_NAMESPACE] !== undefined) {
645
+ delete data[MEMORY_PERSIST_NAMESPACE];
646
+ save(data);
647
+ }
493
648
  return memoryPersistState();
494
649
  }
495
650
 
@@ -629,6 +784,11 @@ function createSettingsStore({ dir, safeStorage } = {}) {
629
784
 
630
785
  return {
631
786
  file,
787
+ sharedDir,
788
+ adoption,
789
+ sharedFile,
790
+ getSharedPref,
791
+ setSharedPref,
632
792
  get,
633
793
  set,
634
794
  rawKey,
@@ -644,6 +804,7 @@ function createSettingsStore({ dir, safeStorage } = {}) {
644
804
  setQuickLauncherConfig,
645
805
  getConfirmMode,
646
806
  setConfirmMode,
807
+ confirmModeState,
647
808
  getTelemetryOptIn,
648
809
  setTelemetryOptIn,
649
810
  getMemoryPersist,