aegiscode 6.5.9 → 6.6.1
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/bin/aegiscode.js +39 -0
- package/package.json +1 -1
- package/scripts/predist.mjs +14 -0
- package/src/autonomous.js +372 -0
- package/src/deps.js +12 -1
- package/vendor/client/update.js +76 -11
- package/vendor/desktop/lib/local/autonomous.js +745 -0
- package/vendor/desktop/lib/local/engine.js +62 -7
- package/vendor/desktop/lib/local/git-scope.js +163 -9
- package/vendor/desktop/lib/local/queue.js +549 -0
- package/vendor/desktop/lib/local/session-rounds.js +188 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Session-scoped tool-round ledger.
|
|
5
|
+
*
|
|
6
|
+
* The engine's tool-round cap used to be a wall. A turn that reached it ended
|
|
7
|
+
* mid-investigation with `[stopped at 24 tool rounds…]`, and because the
|
|
8
|
+
* accounting lived only for the duration of that call, the next turn in the
|
|
9
|
+
* same conversation started from zero: it knew nothing about the horizon that
|
|
10
|
+
* had just cut it off, so a long job could be truncated by the cap forever
|
|
11
|
+
* without ever being finished.
|
|
12
|
+
*
|
|
13
|
+
* This makes the cap *session* state instead of turn state:
|
|
14
|
+
*
|
|
15
|
+
* - a turn that reaches its horizon records the unfinished work against the
|
|
16
|
+
* session key (rounds spent, tokens, the note the user saw);
|
|
17
|
+
* - the next turn in that session takes the record — consuming it, so one
|
|
18
|
+
* resume per interruption and a chain of interruptions is a chain of
|
|
19
|
+
* deliberate asks, never an automatic loop — and gets a continuation
|
|
20
|
+
* preamble plus a small round bonus, so re-orientation does not eat the
|
|
21
|
+
* new horizon before any work happens;
|
|
22
|
+
* - an entry nobody comes back for expires after TTL_MS.
|
|
23
|
+
*
|
|
24
|
+
* State is module-level, so it outlives a turn, a `send()` and whichever
|
|
25
|
+
* engine instance the desktop happens to be using. It never leaves the
|
|
26
|
+
* process and is never written to disk.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** How long an unclaimed interruption stays resumable (30 min). */
|
|
30
|
+
const TTL_MS = 30 * 60 * 1000;
|
|
31
|
+
/** Bound on live entries: a long-lived app must not grow without limit. */
|
|
32
|
+
const MAX_ENTRIES = 64;
|
|
33
|
+
|
|
34
|
+
const entries = new Map(); // key -> { rounds, tokens, note, at, interruptions }
|
|
35
|
+
const historyKeys = new WeakMap(); // history[] -> key
|
|
36
|
+
let historyKeySeq = 0;
|
|
37
|
+
|
|
38
|
+
/** Drop expired entries, then the oldest ones past the bound. */
|
|
39
|
+
function prune(now = Date.now()) {
|
|
40
|
+
for (const [key, entry] of entries) {
|
|
41
|
+
if (now - entry.at > TTL_MS) entries.delete(key);
|
|
42
|
+
}
|
|
43
|
+
while (entries.size > MAX_ENTRIES) {
|
|
44
|
+
// Map iteration is insertion-ordered and `record` re-inserts on write, so
|
|
45
|
+
// the first key is the oldest.
|
|
46
|
+
const oldest = entries.keys().next();
|
|
47
|
+
if (oldest.done) break;
|
|
48
|
+
entries.delete(oldest.value);
|
|
49
|
+
}
|
|
50
|
+
return entries.size;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The key an interruption is filed under. A caller that states its session
|
|
55
|
+
* (desktop payloads carry an id) gets an exact match; otherwise the working
|
|
56
|
+
* directory, then the identity of the history array, then a single shared
|
|
57
|
+
* bucket. `adopt` covers the remaining case — a caller that mints a fresh key
|
|
58
|
+
* every turn.
|
|
59
|
+
*/
|
|
60
|
+
function keyFor(payload, history) {
|
|
61
|
+
const p = payload && typeof payload === 'object' ? payload : {};
|
|
62
|
+
const stated = p.sessionKey || p.sessionId || p.chatId || p.threadId || p.conversationId;
|
|
63
|
+
if (typeof stated === 'string' && stated.trim()) return `s:${stated.trim()}`;
|
|
64
|
+
if (typeof p.cwd === 'string' && p.cwd.trim()) return `cwd:${p.cwd.trim()}`;
|
|
65
|
+
if (history && typeof history === 'object') {
|
|
66
|
+
let key = historyKeys.get(history);
|
|
67
|
+
if (!key) {
|
|
68
|
+
historyKeySeq += 1;
|
|
69
|
+
key = `hist-${historyKeySeq}`;
|
|
70
|
+
historyKeys.set(history, key);
|
|
71
|
+
}
|
|
72
|
+
return key;
|
|
73
|
+
}
|
|
74
|
+
return 'default';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Extra rounds granted to a resuming turn. Deliberately small and bounded: the
|
|
79
|
+
* point of resuming is to *continue* work that already exists, not to hand out
|
|
80
|
+
* a second full horizon (every round re-sends the whole conversation, so this
|
|
81
|
+
* is the cheapest way to make the cap non-fatal).
|
|
82
|
+
*/
|
|
83
|
+
function bonus(base) {
|
|
84
|
+
const n = Number.isFinite(base) && base > 0 ? base : 0;
|
|
85
|
+
return Math.max(4, Math.ceil(n / 4));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Peek without consuming — the entry stays resumable. */
|
|
89
|
+
function peek(key, now = Date.now()) {
|
|
90
|
+
prune(now);
|
|
91
|
+
return entries.get(key) || null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Claim the interruption for `key`, if any. Consuming: a second call gets null. */
|
|
95
|
+
function take(key, now = Date.now()) {
|
|
96
|
+
prune(now);
|
|
97
|
+
const entry = entries.get(key);
|
|
98
|
+
if (!entry) return null;
|
|
99
|
+
entries.delete(key);
|
|
100
|
+
return entry;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Claim the most recent interruption filed under ANY key. This is the bridge
|
|
105
|
+
* for a caller whose key does not survive between turns: the next turn on this
|
|
106
|
+
* process adopts the held work rather than losing it. Bounded by `maxAgeMs` so
|
|
107
|
+
* an unrelated new conversation is not handed yesterday's job.
|
|
108
|
+
*/
|
|
109
|
+
function adopt({ now = Date.now(), maxAgeMs = 10 * 60 * 1000 } = {}) {
|
|
110
|
+
prune(now);
|
|
111
|
+
let bestKey = null;
|
|
112
|
+
let best = null;
|
|
113
|
+
for (const [key, entry] of entries) {
|
|
114
|
+
if (now - entry.at > maxAgeMs) continue;
|
|
115
|
+
if (!best || entry.at > best.at) {
|
|
116
|
+
best = entry;
|
|
117
|
+
bestKey = key;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!best) return null;
|
|
121
|
+
entries.delete(bestKey);
|
|
122
|
+
return { key: bestKey, entry: best };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* File an interruption. `chain` carries the count forward from the entry this
|
|
127
|
+
* turn resumed, so `interruptions` reports how many times one job has been cut
|
|
128
|
+
* off — visible in the note and useful when deciding to raise the horizon.
|
|
129
|
+
*/
|
|
130
|
+
function record(key, { rounds, tokens, note, chain } = {}, now = Date.now()) {
|
|
131
|
+
prune(now);
|
|
132
|
+
const prior = entries.get(key);
|
|
133
|
+
const entry = {
|
|
134
|
+
rounds: Number.isFinite(rounds) ? rounds : 0,
|
|
135
|
+
tokens: Number.isFinite(tokens) ? tokens : 0,
|
|
136
|
+
note: typeof note === 'string' ? note : '',
|
|
137
|
+
at: now,
|
|
138
|
+
interruptions: (Number.isFinite(chain) ? chain : prior && prior.interruptions) || 0,
|
|
139
|
+
};
|
|
140
|
+
entry.interruptions += 1;
|
|
141
|
+
entries.delete(key);
|
|
142
|
+
entries.set(key, entry); // re-insert so Map order stays age order
|
|
143
|
+
prune(now);
|
|
144
|
+
return entry;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The instruction that turns a cold start into a continuation. */
|
|
148
|
+
function resumePreamble(entry) {
|
|
149
|
+
const rounds = entry && entry.rounds ? entry.rounds : 'the previous turn\'s';
|
|
150
|
+
const tokens = entry && entry.tokens ? ` (${entry.tokens.toLocaleString()} tokens)` : '';
|
|
151
|
+
return (
|
|
152
|
+
`[session resume] The previous turn in this conversation was cut off at ${rounds} tool rounds${tokens} ` +
|
|
153
|
+
'because it reached the round horizon, so the work it started is unfinished. Continue that work now: ' +
|
|
154
|
+
'do not restart, do not re-do steps that already completed, and do not treat the interruption as a ' +
|
|
155
|
+
'failure to report. Pick up the next unfinished step and finish with a written answer.'
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Read-only snapshot for the UI (and for tests). */
|
|
160
|
+
function state(now = Date.now()) {
|
|
161
|
+
prune(now);
|
|
162
|
+
return {
|
|
163
|
+
size: entries.size,
|
|
164
|
+
ttlMs: TTL_MS,
|
|
165
|
+
maxEntries: MAX_ENTRIES,
|
|
166
|
+
entries: [...entries].map(([key, entry]) => ({ key, ...entry })),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Test hook: forget everything. */
|
|
171
|
+
function reset() {
|
|
172
|
+
entries.clear();
|
|
173
|
+
return entries.size;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
module.exports = {
|
|
177
|
+
TTL_MS,
|
|
178
|
+
MAX_ENTRIES,
|
|
179
|
+
keyFor,
|
|
180
|
+
bonus,
|
|
181
|
+
peek,
|
|
182
|
+
take,
|
|
183
|
+
adopt,
|
|
184
|
+
record,
|
|
185
|
+
resumePreamble,
|
|
186
|
+
state,
|
|
187
|
+
reset,
|
|
188
|
+
};
|