aegisone 1.0.6 → 1.0.8

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.
@@ -182,6 +182,14 @@ const localTools = require('./lib/local/tools.js');
182
182
  // the `queue:` IPC surface registered by registerQueueIpc() below.
183
183
  const queueModule = require('./lib/local/queue.js');
184
184
  const autonomousModule = require('./lib/local/autonomous.js');
185
+ // Auto mode's round-horizon half: the same arithmetic the upstream engine
186
+ // (aegiscodex-dev src/autonomous.js) and the CLI's presenter use, read here by
187
+ // the one host that owns this face's policy. See createModelDispatch.
188
+ const {
189
+ autoContinuePlan,
190
+ autoContinueLimit,
191
+ autoRoundsDefault,
192
+ } = require('./lib/local/autonomous.js');
185
193
 
186
194
  // The Pro boundary for the autonomous queue (Phase 4 of
187
195
  // docs/upgrade-conversion-plan.md). MAIN-process only, no Electron inside, so
@@ -1078,7 +1086,34 @@ function createConfirmModeDispatch(settings) {
1078
1086
  };
1079
1087
  }
1080
1088
 
1081
- /** Register getConfirmMode/setConfirmMode as `aegis:<name>` on ipcMain —
1089
+ /**
1090
+ * Auto mode's other switch: a posture of "don't ask" IS auto mode on this face,
1091
+ * so an explicit setConfirmMode must move the model dispatch's session state
1092
+ * with it. Otherwise the two halves of the mode can be told different things —
1093
+ * the engine stops asking, the round horizon keeps raising its card, and the
1094
+ * chip in the composer claims auto mode is on.
1095
+ *
1096
+ * OFF starts auto mode for this sitting (`setAutoRounds(true)`, which also
1097
+ * retires a boot-time AEGIS_AUTO_ROUNDS=0), ON ends it. A malformed payload
1098
+ * changes nothing, exactly as in createConfirmModeDispatch — including the
1099
+ * session state, so the two cannot drift on a bad message.
1100
+ */
1101
+ function withAutoRoundsSwitch(confirmDispatch, modelDispatch) {
1102
+ const setConfirmMode = confirmDispatch.setConfirmMode;
1103
+ return {
1104
+ ...confirmDispatch,
1105
+ setConfirmMode: (payload) => {
1106
+ const res = setConfirmMode(payload);
1107
+ if (payload && typeof payload.enabled === 'boolean' && modelDispatch && typeof modelDispatch.setAutoRounds === 'function') {
1108
+ modelDispatch.setAutoRounds(payload.enabled === false);
1109
+ }
1110
+ return res;
1111
+ },
1112
+ };
1113
+ }
1114
+
1115
+ /**
1116
+ * Register getConfirmMode/setConfirmMode as `aegis:<name>` on ipcMain —
1082
1117
  * the same ${IPC_PREFIX}<method> convention registerIpc uses for the rest of
1083
1118
  * the renderer's aegis.* bridge (their handlers need nothing but the
1084
1119
  * payload, so the event is dropped exactly like every non-chatCompletion
@@ -1398,11 +1433,149 @@ function readTextFileForPreview(payload) {
1398
1433
  /**
1399
1434
  * Pure mapping: model:<name> -> LocalEngine call (unit-testable in Node).
1400
1435
  */
1401
- function createModelDispatch(engine) {
1402
- return {
1436
+ function createModelDispatch(engine, { env = process.env, settings } = {}) {
1437
+ // ── Auto mode's second half: the round horizon ────────────────────────────
1438
+ //
1439
+ // autoContinuePlan/autoContinueLimit (lib/local/autonomous.js) answer "may
1440
+ // this horizon ask be answered for the user?" — the arithmetic the engine
1441
+ // defines in src/autonomous.js and the ONLY thing separating auto mode in
1442
+ // aegiscodex-dev from auto mode here. The desktop wired the first half (no
1443
+ // approval cards, via the Tool-approvals switch) and nothing at all for this
1444
+ // one, so a desktop turn that hit its round horizon raised the blocking card
1445
+ // on the first hit and every hit after it. That is the exact question auto
1446
+ // mode exists to stop asking: same answer, same click, once per horizon.
1447
+ //
1448
+ // The policy lives HERE rather than in the engine on purpose — the engine's
1449
+ // own comment states the contract ("the hook is optional and the caller owns
1450
+ // the policy"), so the engine keeps asking and each host decides. This host's
1451
+ // answer is the posture the user already set in one place: confirmations OFF
1452
+ // (the same boolean behind the settings switch and the composer's "auto mode"
1453
+ // chip) means auto mode is on, and the tally is per session because that is
1454
+ // the lifetime of the turn it is funding.
1455
+ const autoUsed = new Map(); // sessionId -> automatic continuations spent
1456
+
1457
+ // Auto mode's session START state — the engine's AEGIS_AUTO_ROUNDS=0 opt-out
1458
+ // (src/autonomous.js autoRoundsDefault), read once here rather than on every
1459
+ // call. Re-derived per call it is a VETO, not a start state: with the variable
1460
+ // exported in the environment that launched the app, the Tool-approvals switch
1461
+ // and any `/automode on` from the CLI sharing the store would write the
1462
+ // posture and change nothing — the chip lit, the round card still every
1463
+ // horizon, and no way back to auto mode short of restarting the app. The
1464
+ // engine's own contract is the other one: the env var picks where the session
1465
+ // STARTS, and the toggle moves it freely from there.
1466
+ //
1467
+ // The opt-out is retired by an explicit act, of which there are two here:
1468
+ // setAutoRounds (the switch and the IPC, wired at bootstrap) and a stored
1469
+ // posture that differs from the one this process booted with (a flip made by
1470
+ // another face — the CLI's `/automode` writes the same key).
1471
+ let autoRoundsSession = autoRoundsDefault(env);
1472
+ let bootPosture;
1473
+ let autoRoundsRetired = false;
1474
+
1475
+ /** True only when auto mode is on: the session state AND a posture that says
1476
+ * don't ask. Read per call, never captured: the settings
1477
+ * switch and `/automode` in the CLI both write the same shared key, so a
1478
+ * flip takes effect on the next horizon without a restart.
1479
+ *
1480
+ * `undefined` from every accessor means "this store cannot answer" — and an
1481
+ * unknown posture must NOT be read as auto mode ON: silently extending turns
1482
+ * (and spending billed rounds) on a store we cannot read is the worse
1483
+ * failure, and the pre-change behaviour (show the card) is the safe default.
1484
+ * AEGIS_AUTO_ROUNDS=0 turns it off session-wide, exactly as in the engine. */
1485
+ const autoRoundsOn = () => {
1486
+ const store = settings || (engine && engine.settings) || null;
1487
+ const bool = (v) => (typeof v === 'boolean' ? v : undefined);
1488
+ let posture;
1489
+ try {
1490
+ if (store && typeof store.confirmModeState === 'function') {
1491
+ const st = store.confirmModeState();
1492
+ posture = bool(st && st.enabled);
1493
+ }
1494
+ if (posture === undefined && store && typeof store.getConfirmMode === 'function') {
1495
+ posture = bool(store.getConfirmMode());
1496
+ }
1497
+ } catch {
1498
+ /* an unreadable store is not auto mode — the card stays up */
1499
+ }
1500
+ // A posture that moved since boot means somebody acted (another face wrote
1501
+ // the same key), so the env opt-out has been answered and no longer applies.
1502
+ if (!autoRoundsRetired && posture !== undefined) {
1503
+ if (bootPosture === undefined) bootPosture = posture;
1504
+ else if (posture !== bootPosture) {
1505
+ autoRoundsRetired = true;
1506
+ autoRoundsSession = true;
1507
+ }
1508
+ }
1509
+ if (!autoRoundsSession || !store || posture === undefined) return false;
1510
+ return posture === false;
1511
+ };
1512
+
1513
+ /** Move the session state — the switch (via setConfirmMode at bootstrap) and
1514
+ * any explicit auto-mode request. Turns OFF means the operator asked for
1515
+ * manual NOW, so it sticks against the boot-time opt-out in both
1516
+ * directions. */
1517
+ const setAutoRounds = (v) => {
1518
+ autoRoundsSession = Boolean(v);
1519
+ autoRoundsRetired = true;
1520
+ };
1521
+
1522
+ /**
1523
+ * Wrap the renderer's stream so a round-horizon ask can be answered for the
1524
+ * human while auto mode is on, and forwarded untouched otherwise.
1525
+ *
1526
+ * Three things are load-bearing:
1527
+ *
1528
+ * · the ENGINE is what answers (`respondRoundsAsk`), not a synthetic frame
1529
+ * — the turn is held open on that promise, so the only honest way to skip
1530
+ * the dialog is to resolve it;
1531
+ * · `{ note }` reaches the renderer, because a budget that grew invisibly
1532
+ * is the "why is it still running?" report, and the CLI announces this
1533
+ * same continuation through its transcript note;
1534
+ * · a declined/failed answer FALLS THROUGH to the card. The allowance is
1535
+ * spent, so this is the real ask — the same "then it asks" contract the
1536
+ * CLI and the engine's status line advertise.
1537
+ */
1538
+ const chatWithAutoRounds = (payload) => {
1539
+ const forward = payload && payload.onStream;
1540
+ if (typeof forward !== 'function') return engine.chat(payload, forward);
1541
+ const sessionId = (payload && payload.sessionId) || '';
1542
+ const onStream = (chunk) => {
1543
+ if (chunk && chunk.rounds && chunk.rounds.id && autoRoundsOn()) {
1544
+ const plan = autoContinuePlan({
1545
+ on: true,
1546
+ used: autoUsed.get(sessionId) || 0,
1547
+ limit: autoContinueLimit(env),
1548
+ });
1549
+ autoUsed.set(sessionId, plan.used);
1550
+ if (plan.auto) {
1551
+ const answered =
1552
+ typeof engine.respondRoundsAsk === 'function'
1553
+ ? engine.respondRoundsAsk(chunk.rounds.id, true)
1554
+ : { ok: false };
1555
+ if (answered && answered.ok) {
1556
+ const spent = Number(chunk.rounds.spent) || 0;
1557
+ forward({
1558
+ note:
1559
+ `auto mode: the ${spent}-round limit was reached — continuing automatically ` +
1560
+ `(${plan.used}/${plan.cap}, then it asks).`,
1561
+ });
1562
+ return;
1563
+ }
1564
+ }
1565
+ // Not auto (allowance spent, or the engine could not take an answer):
1566
+ // show the real card. plan.used is 0 in the spent case, so the next
1567
+ // horizon starts a fresh block of automatic continuations — the same
1568
+ // reset the CLI's presenter applies once the human is actually asked.
1569
+ }
1570
+ forward(chunk);
1571
+ };
1572
+ return engine.chat(payload, onStream);
1573
+ };
1574
+
1575
+ const dispatch = {
1403
1576
  listClasses: () => engine.listClasses(),
1404
1577
  listModels: (payload) => engine.listModels(payload && payload.class),
1405
- chat: (payload) => engine.chat(payload, payload && payload.onStream),
1578
+ chat: chatWithAutoRounds,
1406
1579
  // The AEGIS key lives in a reserved namespace the store refuses to expose
1407
1580
  // or delete through this surface; the filter is belt-and-braces so a
1408
1581
  // mis-wired store can never hand the renderer a removable AEGIS entry.
@@ -1415,6 +1588,22 @@ function createModelDispatch(engine) {
1415
1588
  }),
1416
1589
  'settings.remove': (payload) =>
1417
1590
  engine.settings.remove(payload && payload.provider),
1591
+ // The USER's own model declarations (`client/user-models.js`, the file the
1592
+ // CLI's `/model-add` writes). Settings is the only place a desktop user can
1593
+ // add a model the server's catalog does not name, and until this existed the
1594
+ // only ways were a release or hand-editing a JSON file the app never named.
1595
+ //
1596
+ // These three go through the ENGINE rather than the settings store: the
1597
+ // engine's `listModels('byok')` is what applies them, so the write and the
1598
+ // read share one implementation and cannot disagree about the file. Add and
1599
+ // remove THROW on a malformed provider/model id — the renderer shows the
1600
+ // message, which is the honest outcome for a string that would otherwise be
1601
+ // forwarded to a vendor and 404 there.
1602
+ 'userModels.get': () => engine.userModels(),
1603
+ 'userModels.add': (payload) =>
1604
+ engine.addUserModel(payload && payload.provider, payload && payload.model),
1605
+ 'userModels.remove': (payload) =>
1606
+ engine.removeUserModel(payload && payload.provider, payload && payload.model),
1418
1607
  // Persisting memory ("sync finished turns to cloud memory automatically").
1419
1608
  // Reserved namespace, so it is invisible to the provider CRUD above — but
1420
1609
  // unlike a provider it IS reachable deliberately, through these two. The
@@ -1430,9 +1619,44 @@ function createModelDispatch(engine) {
1430
1619
  // session" grants so a fresh thread never inherits a prior one's.
1431
1620
  respondApproval: (payload) =>
1432
1621
  engine.respondApproval(payload && payload.approvalId, payload && payload.decision),
1622
+ // Round-horizon gate (desktop/lib/local/engine.js requestRoundsAsk): the
1623
+ // renderer's rounds card answers a pending "fund another block of tool
1624
+ // rounds?" ask here. The ask itself arrives as a `{ rounds }` chunk on the
1625
+ // same chat() delta stream the approval card rides — see
1626
+ // desktop/renderer/app.js renderRoundsCard. An unanswered id is a no-op,
1627
+ // same contract as respondApproval.
1628
+ //
1629
+ // Guarded on the method's existence rather than assuming it: an engine
1630
+ // without the ask can never emit a `{ rounds }` chunk, so this branch is
1631
+ // unreachable there — but a renderer/window pair that is newer than the
1632
+ // engine it drives must get an honest `{ ok: false }` instead of a
1633
+ // TypeError inside the IPC handler.
1634
+ respondRoundsAsk: (payload) =>
1635
+ typeof engine.respondRoundsAsk === 'function'
1636
+ ? engine.respondRoundsAsk(payload && payload.roundsAskId, payload && payload.decision)
1637
+ : { ok: false },
1433
1638
  clearApprovals: (payload) =>
1434
1639
  engine.clearSessionApprovals(payload && payload.sessionId),
1435
1640
  };
1641
+
1642
+ // Auto mode's session state is read and moved DIRECTLY, never over IPC.
1643
+ // Two reasons, and both are load-bearing:
1644
+ //
1645
+ // · every ENUMERABLE key of this object becomes an `aegis:<name>` channel
1646
+ // (registerModelIpc iterates Object.entries), so exporting these would
1647
+ // hand the renderer a way to move auto mode's round half without moving
1648
+ // the posture — the exact two-answers-one-question drift this file's
1649
+ // dispatch exists to prevent. The renderer's switch reaches both halves
1650
+ // through aegis:setConfirmMode (withAutoRoundsSwitch), which is the one
1651
+ // act that means "auto mode" on this face;
1652
+ //
1653
+ // · a signature change to the bridge has to be visible where the bridge is
1654
+ // described, so the count in test/model-dispatch.mjs stays a real check.
1655
+ Object.defineProperties(dispatch, {
1656
+ autoRoundsOn: { value: autoRoundsOn, enumerable: false },
1657
+ setAutoRounds: { value: setAutoRounds, enumerable: false },
1658
+ });
1659
+ return dispatch;
1436
1660
  }
1437
1661
 
1438
1662
  /**
@@ -1879,11 +2103,30 @@ function queueEngineWithDeniedApprovals(engine) {
1879
2103
  /* the turn's own error path reports a gate that cannot be answered */
1880
2104
  }
1881
2105
  };
2106
+ // The third net under the same rule, for the round-horizon ask. A queued task
2107
+ // is unattended, so nobody can fund another block and the answer is always
2108
+ // "no" — which is precisely the engine's own non-interactive behaviour (it
2109
+ // stops, files the session hold, and names the env knob). Answering `false`
2110
+ // rather than leaving the promise pending is what keeps a worker from parking
2111
+ // on a card nobody will ever click: an unanswered ask does resolve false
2112
+ // (requestRoundsAsk fails safe), but waiting for the abort instead would look
2113
+ // like a hung task in the queue for however long the turn ran.
2114
+ const stopRounds = (chunk) => {
2115
+ const ask = chunk && chunk.rounds;
2116
+ if (!ask || !ask.id) return;
2117
+ if (typeof engine.respondRoundsAsk !== 'function') return;
2118
+ try {
2119
+ engine.respondRoundsAsk(ask.id, false);
2120
+ } catch {
2121
+ /* as above: the turn reports a gate that cannot be answered */
2122
+ }
2123
+ };
1882
2124
  return {
1883
2125
  ...engine,
1884
2126
  chat(payload, onDelta) {
1885
2127
  return engine.chat(payload, (chunk) => {
1886
2128
  deny(chunk);
2129
+ stopRounds(chunk);
1887
2130
  if (typeof onDelta === 'function') onDelta(chunk);
1888
2131
  });
1889
2132
  },
@@ -3106,7 +3349,7 @@ function bootstrap() {
3106
3349
  handleReplyFinished,
3107
3350
  () => providerRouteConfigured(engine)
3108
3351
  );
3109
- registerModelIpc(
3352
+ const modelIpc = registerModelIpc(
3110
3353
  ipcMain,
3111
3354
  engine,
3112
3355
  sessionsDir,
@@ -3121,7 +3364,16 @@ function bootstrap() {
3121
3364
  // is where the settings store lands — the exact tool gate it controls lives
3122
3365
  // in the engine created alongside it (lib/local/engine.js gatedExecuteTool
3123
3366
  // reads settings.getConfirmMode() on every mutating tool call).
3124
- registerConfirmModeIpc(ipcMain, createConfirmModeDispatch(settings));
3367
+ //
3368
+ // …and it is also auto mode's OTHER switch: a posture of "don't ask" is what
3369
+ // auto mode means on this face, so flipping the switch off is an explicit
3370
+ // request for it and must move the session state with it. Without this, an
3371
+ // app launched with AEGIS_AUTO_ROUNDS=0 (a start-manual opt-out, per the
3372
+ // engine) could be switched to auto mode in the UI — approvals off, chip lit
3373
+ // — while the round horizon kept raising its card, because the env var was
3374
+ // re-read as a veto on every horizon. Same escape hatch as `/automode on` in
3375
+ // the CLI, which shares this store.
3376
+ registerConfirmModeIpc(ipcMain, withAutoRoundsSwitch(createConfirmModeDispatch(settings), modelIpc.modelDispatch));
3125
3377
 
3126
3378
  // Funnel instrumentation (Phase 2 of docs/upgrade-conversion-plan.md):
3127
3379
  // aegis:telemetryEvent / aegis:telemetryReport, backed by
@@ -3720,6 +3972,7 @@ module.exports = {
3720
3972
  createQuickLauncherDispatch,
3721
3973
  registerQuickLauncherIpc,
3722
3974
  createConfirmModeDispatch,
3975
+ withAutoRoundsSwitch,
3723
3976
  createTelemetryDispatch,
3724
3977
  registerTelemetryIpc,
3725
3978
  createCovenantDispatch,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aegis-desktop",
3
3
  "productName": "AEGIS Desktop",
4
- "version": "0.8.34",
4
+ "version": "1.0.0",
5
5
  "description": "Thin Electron host for AEGIS — a local chat UI over the shared client/aegis.js transport. Ships transport + UI only; engine logic stays server-side.",
6
6
  "author": {
7
7
  "name": "AEGIS Code",
@@ -349,6 +349,18 @@ const models = {
349
349
  }),
350
350
  remove: (provider) => invokeModel('settings.remove', { provider }),
351
351
  },
352
+ // The user's OWN model declarations (client/user-models.js): the file the
353
+ // CLI's `/model-add` writes, reached here so a model the server's catalog
354
+ // does not name can be added from Settings instead of by hand. `get()`
355
+ // resolves `{ dir, file, map, invalid }` — `file` is shown in the UI so the
356
+ // remaining escape hatch (an editor) is discoverable, and `invalid` names any
357
+ // hand-edited row that was skipped. `add`/`remove` reject with the validation
358
+ // message for a malformed provider or model id.
359
+ userModels: {
360
+ get: () => invokeModel('userModels.get'),
361
+ add: (provider, model) => invokeModel('userModels.add', { provider, model }),
362
+ remove: (provider, model) => invokeModel('userModels.remove', { provider, model }),
363
+ },
352
364
  cancel: (sessionId) => invokeModel('cancel', { sessionId }),
353
365
  // Tool-call approval gate: the renderer's approval card calls this to
354
366
  // answer a pending exec/writeFile/editFile request (the approval itself
@@ -357,6 +369,12 @@ const models = {
357
369
  // this session" grants; newChat() calls it so a fresh thread starts clean.
358
370
  respondApproval: (approvalId, decision) =>
359
371
  invokeModel('respondApproval', { approvalId, decision }),
372
+ // Round-horizon gate: answers a pending `{ rounds }` ask raised by the engine
373
+ // when a turn has used every tool round it was allowed (see app.js
374
+ // renderRoundsCard, and requestRoundsAsk in lib/local/engine.js). Same shape
375
+ // and same channel as respondApproval above.
376
+ respondRoundsAsk: (roundsAskId, decision) =>
377
+ invokeModel('respondRoundsAsk', { roundsAskId, decision }),
360
378
  clearApprovals: (sessionId) => invokeModel('clearApprovals', { sessionId }),
361
379
  };
362
380