agent-lyceum 0.55.0 → 0.57.0
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/README.md +1 -1
- package/README.zh-TW.md +1 -1
- package/dist/doctor.js +17 -1
- package/dist/guard.d.ts +15 -5
- package/dist/guard.js +100 -8
- package/dist/memory-tidy.js +18 -2
- package/dist/policy.d.ts +12 -2
- package/dist/policy.js +24 -4
- package/dist/prompt.js +21 -2
- package/dist/proposals.d.ts +29 -0
- package/dist/proposals.js +282 -10
- package/dist/quota.js +62 -10
- package/dist/run-session.js +8 -3
- package/dist/status-watch.d.ts +4 -2
- package/dist/status-watch.js +27 -5
- package/dist/status.d.ts +3 -1
- package/dist/status.js +11 -8
- package/package.json +1 -1
- package/web/dist/server/hub.js +40 -5
- package/web/dist/start.js +45 -5
package/dist/status.js
CHANGED
|
@@ -306,8 +306,17 @@ function fallbackView(a, state, now) {
|
|
|
306
306
|
.filter((c) => Number.isFinite(c.remaining_sec) && c.remaining_sec > 0);
|
|
307
307
|
return { runtime: ran?.runtime ?? a.runtime, ...(model ? { model } : {}), ...(fallback ? { fallback } : {}), ...(cooldowns.length ? { cooldowns } : {}) };
|
|
308
308
|
}
|
|
309
|
+
/** Tidy hints for a status report. A failure is an empty list: a hint must not break status. */
|
|
310
|
+
export function memoryAdviceLines(project) {
|
|
311
|
+
try {
|
|
312
|
+
return tidyAdvice(project).map((a) => `${a.agent}: ${a.reason}`);
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
return [];
|
|
316
|
+
}
|
|
317
|
+
}
|
|
309
318
|
/** The status of a project: its agents and one run (the newest running one, else the newest, or the one named by `runId`). */
|
|
310
|
-
export function buildStatusReport(base, runId, now = Date.now()) {
|
|
319
|
+
export function buildStatusReport(base, runId, now = Date.now(), memoryAdvice) {
|
|
311
320
|
let run;
|
|
312
321
|
if (runId) {
|
|
313
322
|
const dir = path.join(base.paths.runs, runId);
|
|
@@ -320,13 +329,7 @@ export function buildStatusReport(base, runId, now = Date.now()) {
|
|
|
320
329
|
// Mail is per run: count the unread mail of the run being shown.
|
|
321
330
|
const project = run ? bindRunProject(base, run.dir, run.state.mail_layout, run.state.instances) : base;
|
|
322
331
|
const report = run ? buildRunReport(run, base) : undefined;
|
|
323
|
-
|
|
324
|
-
try {
|
|
325
|
-
advice = tidyAdvice(base).map((a) => `${a.agent}: ${a.reason}`);
|
|
326
|
-
}
|
|
327
|
-
catch {
|
|
328
|
-
/* a hint only: never let it break status */
|
|
329
|
-
}
|
|
332
|
+
const advice = memoryAdvice ?? memoryAdviceLines(base);
|
|
330
333
|
const wakes = run?.state.wakes ?? [];
|
|
331
334
|
const below = new Set();
|
|
332
335
|
for (const a of Object.values(project.agents)) {
|
package/package.json
CHANGED
package/web/dist/server/hub.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
export function createWebHub(factory) {
|
|
1
|
+
export function createWebHub(factory, hooks) {
|
|
2
2
|
const watchers = new Map();
|
|
3
3
|
const latest = new Map();
|
|
4
4
|
const listeners = new Set();
|
|
5
5
|
let quota;
|
|
6
|
+
let quotaAt = 0;
|
|
6
7
|
let quotaToken = 0;
|
|
8
|
+
let inflight = 0;
|
|
7
9
|
const quotaAbort = new AbortController();
|
|
8
10
|
const emit = (event) => {
|
|
9
11
|
for (const listener of listeners) {
|
|
@@ -61,22 +63,47 @@ export function createWebHub(factory) {
|
|
|
61
63
|
if (!factory.quota)
|
|
62
64
|
return Promise.resolve(quota);
|
|
63
65
|
const token = ++quotaToken;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
inflight += 1;
|
|
67
|
+
const read = factory.quota;
|
|
68
|
+
// Call now, so a subscriber sees the in-flight read before the next turn. A throw still becomes a rejection.
|
|
69
|
+
let pending;
|
|
70
|
+
try {
|
|
71
|
+
pending = Promise.resolve(read(quotaAbort.signal));
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
pending = Promise.reject(error);
|
|
75
|
+
}
|
|
76
|
+
return pending
|
|
66
77
|
.then((reports) => {
|
|
67
78
|
if (token !== quotaToken)
|
|
68
79
|
return quota;
|
|
69
80
|
quota = reports;
|
|
81
|
+
quotaAt = Date.now();
|
|
70
82
|
emit({ type: "quota", reports });
|
|
71
83
|
return reports;
|
|
72
84
|
})
|
|
73
|
-
.catch(() => quota)
|
|
85
|
+
.catch(() => quota)
|
|
86
|
+
.finally(() => {
|
|
87
|
+
inflight -= 1;
|
|
88
|
+
});
|
|
74
89
|
};
|
|
75
90
|
const projectList = () => [...latest].map(([name, frame]) => ({ name, frame })).sort((a, b) => a.name.localeCompare(b.name));
|
|
76
91
|
return {
|
|
77
92
|
start: sync,
|
|
78
93
|
beat: sync,
|
|
79
94
|
refreshQuota,
|
|
95
|
+
subscriberCount: () => listeners.size,
|
|
96
|
+
quotaBusy: () => inflight > 0,
|
|
97
|
+
/**
|
|
98
|
+
* Milliseconds until a successful read leaves the freshness window, capped at `maxAgeMs`.
|
|
99
|
+
* A missing or failed read has none left, so the next subscriber reads at once.
|
|
100
|
+
*/
|
|
101
|
+
quotaRemaining: (maxAgeMs) => {
|
|
102
|
+
if (quota === undefined)
|
|
103
|
+
return 0;
|
|
104
|
+
const left = maxAgeMs - (Date.now() - quotaAt);
|
|
105
|
+
return Math.min(maxAgeMs, Math.max(0, left));
|
|
106
|
+
},
|
|
80
107
|
frame(name) {
|
|
81
108
|
return latest.get(name);
|
|
82
109
|
},
|
|
@@ -90,6 +117,7 @@ export function createWebHub(factory) {
|
|
|
90
117
|
},
|
|
91
118
|
snapshot: projectList,
|
|
92
119
|
subscribe(listener) {
|
|
120
|
+
const previous = listeners.size;
|
|
93
121
|
listeners.add(listener);
|
|
94
122
|
const projects = projectList();
|
|
95
123
|
try {
|
|
@@ -97,8 +125,15 @@ export function createWebHub(factory) {
|
|
|
97
125
|
}
|
|
98
126
|
catch {
|
|
99
127
|
listeners.delete(listener);
|
|
128
|
+
return () => { };
|
|
100
129
|
}
|
|
101
|
-
|
|
130
|
+
hooks?.onSubscribers?.(listeners.size, previous);
|
|
131
|
+
return () => {
|
|
132
|
+
const before = listeners.size;
|
|
133
|
+
if (!listeners.delete(listener))
|
|
134
|
+
return;
|
|
135
|
+
hooks?.onSubscribers?.(listeners.size, before);
|
|
136
|
+
};
|
|
102
137
|
},
|
|
103
138
|
};
|
|
104
139
|
}
|
package/web/dist/start.js
CHANGED
|
@@ -8,9 +8,12 @@ import { handleSummary } from "./server/summary.js";
|
|
|
8
8
|
/** A repeated header arrives as an array; it is never valid for Origin or the token, so it becomes "" (the guard refuses an empty value). */
|
|
9
9
|
const singleHeader = (v) => (Array.isArray(v) ? "" : v);
|
|
10
10
|
const JSON_HEADERS = { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" };
|
|
11
|
+
/** How often a page that is watching `/events` asks the account usage commands again. */
|
|
12
|
+
const QUOTA_REFRESH_MS = 5 * 60_000;
|
|
11
13
|
/** Binds first; the hub, the quota reads and the timers start only once the port is ours, and are undone if that fails. */
|
|
12
14
|
export async function startWebServer(opts) {
|
|
13
|
-
|
|
15
|
+
let onSubscribers = (_count, _previous) => { };
|
|
16
|
+
const hub = createWebHub(opts.factory, { onSubscribers: (count, previous) => onSubscribers(count, previous) });
|
|
14
17
|
const startedAt = new Date().toISOString();
|
|
15
18
|
let boundPort = opts.port;
|
|
16
19
|
const token = randomBytes(24).toString("hex");
|
|
@@ -105,10 +108,49 @@ export async function startWebServer(opts) {
|
|
|
105
108
|
const address = server.address();
|
|
106
109
|
boundPort = typeof address === "object" && address ? address.port : opts.port;
|
|
107
110
|
let heartbeat;
|
|
108
|
-
let
|
|
111
|
+
let quotaTimer;
|
|
112
|
+
const clearQuotaTimer = () => {
|
|
113
|
+
if (quotaTimer)
|
|
114
|
+
clearTimeout(quotaTimer);
|
|
115
|
+
quotaTimer = undefined;
|
|
116
|
+
};
|
|
117
|
+
const readIfIdle = () => {
|
|
118
|
+
if (hub.subscriberCount() === 0 || hub.quotaBusy())
|
|
119
|
+
return;
|
|
120
|
+
void hub.refreshQuota();
|
|
121
|
+
};
|
|
122
|
+
// The delay is the cadence. It does not ask again whether the cache is still fresh: a read that
|
|
123
|
+
// finishes after the timer was armed would otherwise skip the first beat.
|
|
124
|
+
const armQuota = (delayMs) => {
|
|
125
|
+
clearQuotaTimer();
|
|
126
|
+
quotaTimer = setTimeout(() => {
|
|
127
|
+
quotaTimer = undefined;
|
|
128
|
+
readIfIdle();
|
|
129
|
+
if (hub.subscriberCount() > 0)
|
|
130
|
+
armQuota(QUOTA_REFRESH_MS);
|
|
131
|
+
}, delayMs);
|
|
132
|
+
};
|
|
133
|
+
onSubscribers = (count, previous) => {
|
|
134
|
+
if (count === 0) {
|
|
135
|
+
clearQuotaTimer();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// Only the 0 → 1 transition may read or arm. A later page joining, and a
|
|
139
|
+
// disconnect that still leaves a page, must not read and must not move the
|
|
140
|
+
// timer, including after a failed read that left no cached quota.
|
|
141
|
+
if (previous !== 0)
|
|
142
|
+
return;
|
|
143
|
+
const remaining = hub.quotaRemaining(QUOTA_REFRESH_MS);
|
|
144
|
+
if (remaining > 0) {
|
|
145
|
+
armQuota(remaining);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
readIfIdle();
|
|
149
|
+
armQuota(QUOTA_REFRESH_MS);
|
|
150
|
+
};
|
|
109
151
|
const close = () => new Promise((resolve, reject) => {
|
|
110
152
|
clearInterval(heartbeat);
|
|
111
|
-
|
|
153
|
+
clearQuotaTimer();
|
|
112
154
|
hub.close();
|
|
113
155
|
for (const socket of sockets)
|
|
114
156
|
socket.destroy();
|
|
@@ -124,9 +166,7 @@ export async function startWebServer(opts) {
|
|
|
124
166
|
const url = `http://${shown}:${boundPort}/`;
|
|
125
167
|
try {
|
|
126
168
|
hub.start();
|
|
127
|
-
void hub.refreshQuota();
|
|
128
169
|
heartbeat = setInterval(() => hub.beat(), opts.heartbeatMs ?? 5_000);
|
|
129
|
-
quotaBeat = setInterval(() => void hub.refreshQuota(), 60_000);
|
|
130
170
|
if (opts.open)
|
|
131
171
|
openBrowser(url);
|
|
132
172
|
}
|