agent-loft 0.2.105 → 0.2.107
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/package.json +1 -1
- package/src/agents/health.ts +3 -1
- package/src/core/hub.ts +50 -0
package/package.json
CHANGED
package/src/agents/health.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { spawn, spawnSync } from "node:child_process";
|
|
6
6
|
import { createRequire } from "node:module";
|
|
7
7
|
import path from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
8
9
|
import { agentEnvironment } from "../core/agent-env.js";
|
|
9
10
|
import { config } from "../core/config.js";
|
|
10
11
|
import { withProbe } from "./claude/probe.js";
|
|
@@ -210,7 +211,8 @@ async function piHealth(): Promise<AgentHealth> {
|
|
|
210
211
|
}
|
|
211
212
|
let version: string | undefined;
|
|
212
213
|
try {
|
|
213
|
-
|
|
214
|
+
// SDK 只有 import 导出,require.resolve 解不到,得走 import.meta.resolve
|
|
215
|
+
const v = (require(path.join(path.dirname(fileURLToPath(import.meta.resolve("@earendil-works/pi-coding-agent"))), "..", "package.json")) as { version?: string }).version;
|
|
214
216
|
version = v ? `SDK ${v}` : undefined;
|
|
215
217
|
} catch {
|
|
216
218
|
return {
|
package/src/core/hub.ts
CHANGED
|
@@ -254,6 +254,15 @@ function withTimeout<T>(p: Promise<T>, what: string, ms = QUERY_TIMEOUT_MS): Pro
|
|
|
254
254
|
|
|
255
255
|
type Live = { id: number; agent: string; cwd: string; session: AgentSession };
|
|
256
256
|
|
|
257
|
+
/**
|
|
258
|
+
* 空闲多久就关掉会话进程。每个活着的会话都占着一个 agent 进程(Claude Code 一条对话一个 CLI),
|
|
259
|
+
* 不回收的话切过的对话越积越多:占内存,而且 Claude Code 的登录凭证刷新时会互相抢
|
|
260
|
+
* (refresh token 一次性,几个进程同时刷就会把彼此顶掉、要求重新登录)。
|
|
261
|
+
* 关掉不丢上下文:绑定里的 sessionId 还在,下一条消息会接着原来的会话拉起来。
|
|
262
|
+
*/
|
|
263
|
+
const LIVE_IDLE_MS = 10 * 60 * 1000;
|
|
264
|
+
const LIVE_SWEEP_MS = 60 * 1000;
|
|
265
|
+
|
|
257
266
|
/**
|
|
258
267
|
* 把旧会话的记录穿插进主线:按第一条有时间的消息找位置,整段插进去。
|
|
259
268
|
* 没有时间的排在最前面;专用对话里旧会话本来就都早于当前会话,结果和直接接在一起一样。
|
|
@@ -329,10 +338,13 @@ class Hub extends EventEmitter<{ event: [EventEnvelope]; task: [TaskTurnEnd]; "p
|
|
|
329
338
|
* 只在内存里,最多留 CONTEXT_MAX 条。
|
|
330
339
|
*/
|
|
331
340
|
private contextBySession = new Map<string, ContextUsage>();
|
|
341
|
+
/** 每个会话进程(Live.id)最后一次被用到的时刻,见 reapIdle() */
|
|
342
|
+
private liveUsedAt = new Map<number, number>();
|
|
332
343
|
|
|
333
344
|
constructor() {
|
|
334
345
|
super();
|
|
335
346
|
this.setMaxListeners(100);
|
|
347
|
+
setInterval(() => this.reapIdle(), LIVE_SWEEP_MS).unref();
|
|
336
348
|
interactions.on("created", (i) => this.emitEvent(i.convKey, { type: "ix", id: i.id }));
|
|
337
349
|
this.on("task", (e) => this.endIsolatedRun(e));
|
|
338
350
|
taskEvents.on("changed", (owner, keys) => {
|
|
@@ -611,6 +623,8 @@ class Hub extends EventEmitter<{ event: [EventEnvelope]; task: [TaskTurnEnd]; "p
|
|
|
611
623
|
}
|
|
612
624
|
|
|
613
625
|
private setInflight(key: string, n: number): void {
|
|
626
|
+
const live = this.live.get(key);
|
|
627
|
+
if (live) this.liveUsedAt.set(live.id, Date.now());
|
|
614
628
|
const before = this.isBusy(key);
|
|
615
629
|
this.inflight.set(key, Math.max(0, n));
|
|
616
630
|
const after = this.isBusy(key);
|
|
@@ -745,6 +759,7 @@ class Hub extends EventEmitter<{ event: [EventEnvelope]; task: [TaskTurnEnd]; "p
|
|
|
745
759
|
if (!canUseExpert(memoryOwnerOf(conv), conv.expert ?? conv.agent)) throw new Error("这条对话用的专家或 Agent 已删除、取消共享或未获授权。新开一个对话,换个专家吧。");
|
|
746
760
|
const existing = this.live.get(key);
|
|
747
761
|
if (existing && existing.agent === conv.agent && existing.cwd === conv.cwd && !opts.resumeId) {
|
|
762
|
+
this.liveUsedAt.set(existing.id, Date.now());
|
|
748
763
|
return existing.session;
|
|
749
764
|
}
|
|
750
765
|
if (existing) {
|
|
@@ -1783,6 +1798,41 @@ class Hub extends EventEmitter<{ event: [EventEnvelope]; task: [TaskTurnEnd]; "p
|
|
|
1783
1798
|
/** 记忆过期了的会话进程(Live.id):空闲下来就丢掉,下次说话带着新记忆重新拉起 */
|
|
1784
1799
|
private staleLive = new Set<number>();
|
|
1785
1800
|
|
|
1801
|
+
/** 这条会话有没有正在进行、或排着队要用到进程的事 */
|
|
1802
|
+
private liveInUse(key: string): boolean {
|
|
1803
|
+
return this.isBusy(key)
|
|
1804
|
+
|| (this.waitingSends.get(key) ?? 0) > 0
|
|
1805
|
+
|| (this.pendingOps.get(key) ?? 0) > 0
|
|
1806
|
+
|| this.isolatedRuns.has(key)
|
|
1807
|
+
|| this.rewinding.has(key)
|
|
1808
|
+
|| interactions.listPending(key).length > 0;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
/** 关掉空闲太久的会话进程(只关进程,不动绑定),见 LIVE_IDLE_MS */
|
|
1812
|
+
private reapIdle(): void {
|
|
1813
|
+
const now = Date.now();
|
|
1814
|
+
for (const id of this.liveUsedAt.keys()) {
|
|
1815
|
+
if (![...this.live.values()].some((l) => l.id === id)) this.liveUsedAt.delete(id);
|
|
1816
|
+
}
|
|
1817
|
+
for (const [key, live] of this.live) {
|
|
1818
|
+
const usedAt = this.liveUsedAt.get(live.id);
|
|
1819
|
+
if (usedAt === undefined) {
|
|
1820
|
+
this.liveUsedAt.set(live.id, now);
|
|
1821
|
+
continue;
|
|
1822
|
+
}
|
|
1823
|
+
if (now - usedAt < LIVE_IDLE_MS || this.liveInUse(key)) continue;
|
|
1824
|
+
void this.serial(key, async () => {
|
|
1825
|
+
// 排队期间可能已经换了进程、或者又开始用了
|
|
1826
|
+
const current = this.live.get(key);
|
|
1827
|
+
if (current?.id !== live.id || this.liveInUse(key)) return;
|
|
1828
|
+
if (Date.now() - (this.liveUsedAt.get(live.id) ?? 0) < LIVE_IDLE_MS) return;
|
|
1829
|
+
this.liveUsedAt.delete(live.id);
|
|
1830
|
+
console.log(`[会话] ${key} 闲置超过 ${LIVE_IDLE_MS / 60000} 分钟,关闭 ${live.agent} 进程`);
|
|
1831
|
+
await this.drop(key, "会话闲置,已释放进程");
|
|
1832
|
+
}).catch((err) => console.warn("[会话] 释放闲置进程失败:", err instanceof Error ? err.message : err));
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1786
1836
|
private dropIfStale(key: string): void {
|
|
1787
1837
|
const live = this.live.get(key);
|
|
1788
1838
|
if (!live || !this.staleLive.has(live.id) || this.isBusy(key)) return;
|