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.
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * context.js — token-budget trimmer for very large inputs (plan P1 §5.1 /
5
+ * P2 §6.4).
6
+ *
7
+ * Pure Node, no Electron imports. Estimates tokens with a ~4 chars/token
8
+ * heuristic, keeps the system message plus the newest turns, and drops the
9
+ * oldest turns that exceed the budget. The full transcript always stays in
10
+ * the local session record — this only trims what is sent to the wire.
11
+ */
12
+
13
+ const CHARS_PER_TOKEN = 4;
14
+ const MESSAGE_OVERHEAD = 4; // per-message framing + role overhead
15
+
16
+ /** Rough token count for a string (no tokenizer in a zero-dep shell). */
17
+ function estimateTokens(text) {
18
+ if (text == null) return 0;
19
+ const s = typeof text === 'string' ? text : String(text);
20
+ return Math.max(0, Math.ceil(s.length / CHARS_PER_TOKEN));
21
+ }
22
+
23
+ /** Rough token cost of one { role, content } message. */
24
+ function messageTokens(message) {
25
+ if (!message) return 0;
26
+ const content =
27
+ message.content != null
28
+ ? message.content
29
+ : message.text != null
30
+ ? message.text
31
+ : '';
32
+ return MESSAGE_OVERHEAD + estimateTokens(content);
33
+ }
34
+
35
+ /**
36
+ * Trim a message list to fit a token budget, keeping the newest turns.
37
+ *
38
+ * @param {object} opts
39
+ * @param {Array<{role:string,content:string}>} [opts.messages=[]]
40
+ * @param {string} [opts.system='']
41
+ * @param {number} [opts.budgetTokens=8192] total tokens allowed on the wire
42
+ * @param {number} [opts.reserveTokens=0] tokens reserved for the reply
43
+ * @param {number} [opts.fixedTokens=0] wire cost that is NOT a message and
44
+ * cannot be dropped (the tool schemas), counted before any turn is kept
45
+ * @returns {{messages:Array, system:string, dropped:number, estimatedTokens:number, fixedTokens:number}}
46
+ */
47
+ function trimContext({
48
+ messages = [],
49
+ system = '',
50
+ budgetTokens = 8192,
51
+ reserveTokens = 0,
52
+ fixedTokens = 0,
53
+ } = {}) {
54
+ const limit = Math.max(0, budgetTokens - reserveTokens);
55
+ const kept = [];
56
+ // Tool schemas go on the wire every round and cannot be trimmed away, so they
57
+ // are charged against the budget FIRST. Without this the trimmer believed a
58
+ // 4096-token window had 4096 tokens for history when the schemas had already
59
+ // spent ~1971 of them (measured: 10 schemas, 7882 chars) — and the daemon,
60
+ // run with `--context-shift`, discarded the SYSTEM row to make room instead
61
+ // of erroring. Counting them here is what keeps the system row alive.
62
+ const fixed = Math.max(0, Number(fixedTokens) || 0);
63
+ let used = fixed;
64
+
65
+ if (system) used += MESSAGE_OVERHEAD + estimateTokens(system);
66
+
67
+ // Walk newest -> oldest; keep a turn if it fits, else stop (older turns are
68
+ // even less relevant). Always keep at least the newest turn so a single
69
+ // oversized message is still sent rather than silently emptied.
70
+ for (let i = messages.length - 1; i >= 0; i--) {
71
+ const cost = messageTokens(messages[i]);
72
+ if (used + cost > limit && kept.length > 0) break;
73
+ kept.unshift(messages[i]);
74
+ used += cost;
75
+ }
76
+
77
+ return {
78
+ messages: kept,
79
+ system,
80
+ dropped: messages.length - kept.length,
81
+ estimatedTokens: used,
82
+ fixedTokens: fixed,
83
+ };
84
+ }
85
+
86
+ module.exports = {
87
+ CHARS_PER_TOKEN,
88
+ MESSAGE_OVERHEAD,
89
+ estimateTokens,
90
+ messageTokens,
91
+ trimContext,
92
+ };
@@ -968,6 +968,19 @@ function createLocalEngine({
968
968
  // reverse its verdict. An approval that could waive Λ2 would be the exact
969
969
  // capture K3 describes. To change what is permitted, the covenant text
970
970
  // itself has to be re-sealed — which no runtime input can do.
971
+ //
972
+ // DO NOT MOVE THIS BELOW A SHORT-CIRCUIT. It sits above every `return` of
973
+ // the confirmation gate on purpose (ON8/D8 rule 2): `gatedExecuteTool`
974
+ // short-circuits on `!confirmModeEnabled()`, on a remembered session
975
+ // "allow", and on a `/permissions` skip — and since ON8 the confirm-mode
976
+ // preference is the SHARED store's, i.e. the same value the terminal face
977
+ // reads, and `--dangerously-skip-permissions` can arrive in that store as
978
+ // well. A refactor that hoists this below any of those three would make
979
+ // the covenant waivable by a setting a user can write, which is the one
980
+ // thing D8 rule 2 forbids outright. The frozen classes
981
+ // (`write`/`shell`/`network`/`offDevice`, always `'ask'`; see
982
+ // lib/avatar/level.js `LEVEL_INDEPENDENT_APPROVALS`) have the same floor
983
+ // and are asserted beside this one by one/test/permissions.test.mjs.
971
984
  const cv = covenant.preActionAssert({ tool: name, args: args || {} });
972
985
  if (!cv.allowed) {
973
986
  // Non-silent (never a quiet no-op) and bound for Λ4 accountability.
@@ -2114,6 +2127,18 @@ function createLocalEngine({
2114
2127
  return { ok: Boolean(controller) };
2115
2128
  }
2116
2129
 
2130
+ /**
2131
+ * Unload every local model this engine warmed, across every daemon it
2132
+ * talked to (local.js `unloadAll`). The desktop host calls this on its quit
2133
+ * path so closing the app also closes the model — the local lane bills
2134
+ * nothing precisely because it is YOUR hardware, and hardware left running
2135
+ * a model nobody is using anymore after the app exits is the one cost that
2136
+ * lane was supposed to never have.
2137
+ */
2138
+ function unloadLocalModels() {
2139
+ return localT.unloadAll();
2140
+ }
2141
+
2117
2142
  return {
2118
2143
  CLASSES,
2119
2144
  listClasses,
@@ -2123,6 +2148,7 @@ function createLocalEngine({
2123
2148
  respondApproval,
2124
2149
  clearSessionApprovals,
2125
2150
  settings,
2151
+ unloadLocalModels,
2126
2152
  };
2127
2153
  }
2128
2154