@nowcrew/daemon 0.6.102-beta.4 → 0.6.102-beta.5

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.
File without changes
@@ -1,8 +1,9 @@
1
- import { access, open, mkdir, readdir, readFile, rename, unlink } from "node:fs/promises";
1
+ import { access, open, mkdir, readdir, readFile, rename, unlink, stat } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { DaemonToServerExecutionFrameSchema, } from "./execution-protocol.js";
5
5
  const MAX_PENDING_FILES = 4_096;
6
+ const MAX_TELEMETRY_FRAME_BYTES = 256 * 1_024;
6
7
  const fileName = (frame) => `${frame.executionId}.${frame.type === "execution:activity" ? "activity" : "console"}.${frame.seq}.json`;
7
8
  export class ExecutionTelemetryJournal {
8
9
  directory;
@@ -10,6 +11,10 @@ export class ExecutionTelemetryJournal {
10
11
  this.directory = join(agentsRoot, ".execution-telemetry");
11
12
  }
12
13
  async append(frame) {
14
+ const serialized = JSON.stringify(frame);
15
+ if (Buffer.byteLength(serialized) > MAX_TELEMETRY_FRAME_BYTES) {
16
+ throw new Error("execution telemetry frame exceeds 256 KiB");
17
+ }
13
18
  await mkdir(this.directory, { recursive: true, mode: 0o700 });
14
19
  const path = join(this.directory, fileName(frame));
15
20
  if (await access(path).then(() => true, () => false))
@@ -22,7 +27,7 @@ export class ExecutionTelemetryJournal {
22
27
  let handle;
23
28
  try {
24
29
  handle = await open(tempPath, "wx", 0o600);
25
- await handle.writeFile(JSON.stringify(frame), "utf8");
30
+ await handle.writeFile(serialized, "utf8");
26
31
  await handle.sync();
27
32
  await handle.close();
28
33
  handle = undefined;
@@ -43,29 +48,36 @@ export class ExecutionTelemetryJournal {
43
48
  throw error;
44
49
  });
45
50
  }
46
- async replay() {
51
+ async *replay(onInvalid) {
47
52
  const names = await readdir(this.directory).catch((error) => {
48
53
  if (error.code === "ENOENT")
49
54
  return [];
50
55
  throw error;
51
56
  });
52
- const frames = [];
53
- for (const name of names.filter((entry) => entry.endsWith(".json")).sort()) {
57
+ for (const name of names.filter((entry) => entry.endsWith(".json")).sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))) {
54
58
  let raw;
55
59
  try {
56
- raw = JSON.parse(await readFile(join(this.directory, name), "utf8"));
60
+ const path = join(this.directory, name);
61
+ if ((await stat(path)).size > MAX_TELEMETRY_FRAME_BYTES) {
62
+ throw new Error("execution telemetry frame exceeds 256 KiB");
63
+ }
64
+ raw = JSON.parse(await readFile(path, "utf8"));
65
+ const parsed = DaemonToServerExecutionFrameSchema.safeParse(raw);
66
+ if (!parsed.success || (parsed.data.type !== "execution:activity" && parsed.data.type !== "execution:console")) {
67
+ throw new Error("invalid execution telemetry frame");
68
+ }
69
+ yield parsed.data;
57
70
  }
58
71
  catch (error) {
59
- throw new Error(`invalid execution telemetry journal entry: ${name}`, { cause: error });
60
- }
61
- const parsed = DaemonToServerExecutionFrameSchema.safeParse(raw);
62
- if (!parsed.success || (parsed.data.type !== "execution:activity" && parsed.data.type !== "execution:console")) {
63
- throw new Error(`invalid execution telemetry journal entry: ${name}`);
72
+ // ACK 可与目录扫描并发删除条目。
73
+ if (error.code === "ENOENT")
74
+ continue;
75
+ const invalid = new Error(`invalid execution telemetry journal entry: ${name}`, { cause: error });
76
+ if (!onInvalid)
77
+ throw invalid;
78
+ onInvalid(invalid);
64
79
  }
65
- frames.push(parsed.data);
66
80
  }
67
- return frames.sort((left, right) => left.at.localeCompare(right.at)
68
- || left.type.localeCompare(right.type) || left.seq - right.seq);
69
81
  }
70
82
  }
71
83
  export const createExecutionTelemetryJournal = (agentsRoot) => new ExecutionTelemetryJournal(agentsRoot);
@@ -0,0 +1,151 @@
1
+ import { dslog } from "./slog.js";
2
+ import {} from "./execution-telemetry-journal.js";
3
+ const MAX_IN_FLIGHT = 16;
4
+ const MAX_IN_FLIGHT_BYTES = 512 * 1_024;
5
+ const ACK_TIMEOUT_MS = 30_000;
6
+ const keyOf = (executionId, kind, seq) => `${executionId}.${kind}.${seq}`;
7
+ /** 复用 journal 与 event-ack。窗口只保留游标,帧内容按需从磁盘读取。 */
8
+ export class ExecutionTelemetrySender {
9
+ journal;
10
+ onError;
11
+ inFlight = new Map();
12
+ generation = 0;
13
+ running = false;
14
+ requested = false;
15
+ windowBlocked = false;
16
+ lastProgress = 0;
17
+ lastReportAt = 0;
18
+ lastErrorAt = -Infinity;
19
+ sentFrames = 0;
20
+ acknowledgedFrames = 0;
21
+ send;
22
+ timer;
23
+ constructor(journal, onError) {
24
+ this.journal = journal;
25
+ this.onError = onError;
26
+ }
27
+ start(send, onTimeout) {
28
+ this.stop();
29
+ this.send = send;
30
+ this.sentFrames = 0;
31
+ this.acknowledgedFrames = 0;
32
+ this.lastReportAt = Date.now();
33
+ this.logState("execution.telemetry_connected", "遥测补传连接已建立");
34
+ this.timer = setInterval(() => {
35
+ // 单个失败帧不能打断仍在正常确认的连接;只有整个窗口停滞才重连。
36
+ if (this.windowBlocked && Date.now() - this.lastProgress >= ACK_TIMEOUT_MS) {
37
+ this.logState("execution.telemetry_stalled", "遥测确认窗口停滞,准备重连", {
38
+ level: "WARN", stalled_ms: Date.now() - this.lastProgress,
39
+ });
40
+ this.stop();
41
+ onTimeout();
42
+ }
43
+ else if (this.inFlight.size > 0 && Date.now() - this.lastReportAt >= ACK_TIMEOUT_MS) {
44
+ this.lastReportAt = Date.now();
45
+ this.logState("execution.telemetry_progress", "遥测确认窗口状态");
46
+ }
47
+ }, 1_000);
48
+ this.timer.unref();
49
+ this.notify();
50
+ }
51
+ stop() {
52
+ if (this.send)
53
+ this.logState("execution.telemetry_disconnected", "遥测发送停止,未确认帧保留在 journal");
54
+ this.generation += 1;
55
+ this.send = undefined;
56
+ this.requested = false;
57
+ this.windowBlocked = false;
58
+ if (this.timer !== undefined)
59
+ clearInterval(this.timer);
60
+ this.timer = undefined;
61
+ this.inFlight.clear();
62
+ }
63
+ async acknowledge(executionId, kind, seq) {
64
+ const key = keyOf(executionId, kind, seq);
65
+ const entry = this.inFlight.get(key);
66
+ if (!entry)
67
+ return;
68
+ await this.journal.acknowledge(executionId, kind, seq);
69
+ // 重连会清空窗口;条目身份也能挡住旧连接和重复 ACK 的异步完成。
70
+ if (this.inFlight.get(key) !== entry)
71
+ return;
72
+ this.inFlight.delete(key);
73
+ this.acknowledgedFrames += 1;
74
+ this.lastProgress = Date.now();
75
+ this.notify();
76
+ }
77
+ notify() {
78
+ if (!this.send)
79
+ return;
80
+ this.requested = true;
81
+ if (!this.running)
82
+ void this.pump();
83
+ }
84
+ get bytes() {
85
+ let bytes = 0;
86
+ for (const entry of this.inFlight.values())
87
+ bytes += entry.bytes;
88
+ return bytes;
89
+ }
90
+ logState(event, message, fields = {}) {
91
+ dslog(event, message, {
92
+ generation: this.generation, in_flight_frames: this.inFlight.size,
93
+ in_flight_bytes: this.bytes, sent_frames: this.sentFrames,
94
+ acknowledged_frames: this.acknowledgedFrames, window_blocked: this.windowBlocked, ...fields,
95
+ });
96
+ }
97
+ reportError(error) {
98
+ // 同一个坏文件会被多次扫描;不要让错误日志形成新的无界积压。
99
+ if (Date.now() - this.lastErrorAt < ACK_TIMEOUT_MS)
100
+ return;
101
+ this.lastErrorAt = Date.now();
102
+ this.onError(error);
103
+ }
104
+ async pump() {
105
+ this.running = true;
106
+ const generation = this.generation;
107
+ try {
108
+ while (this.send && generation === this.generation && this.requested) {
109
+ this.requested = false;
110
+ let blocked = false;
111
+ // 本轮读取中的文件可能刚收到 ACK;不能把已删除游标的旧读取结果再发出去。
112
+ const alreadySent = new Set(this.inFlight.keys());
113
+ for await (const frame of this.journal.replay((error) => this.reportError(error))) {
114
+ if (!this.send || generation !== this.generation)
115
+ return;
116
+ const key = keyOf(frame.executionId, frame.type === "execution:activity" ? "activity" : "console", frame.seq);
117
+ if (alreadySent.has(key) || this.inFlight.has(key))
118
+ continue;
119
+ const bytes = Buffer.byteLength(JSON.stringify(frame));
120
+ if (this.inFlight.size >= MAX_IN_FLIGHT || this.bytes + bytes > MAX_IN_FLIGHT_BYTES) {
121
+ blocked = true;
122
+ break;
123
+ }
124
+ if (this.inFlight.size === 0)
125
+ this.lastProgress = Date.now();
126
+ this.inFlight.set(key, { bytes });
127
+ if (!this.send(frame)) {
128
+ this.inFlight.delete(key);
129
+ this.logState("execution.telemetry_send_failed", "遥测发送失败,帧保留在 journal", { level: "WARN" });
130
+ return;
131
+ }
132
+ this.sentFrames += 1;
133
+ }
134
+ if (!this.send || generation !== this.generation)
135
+ return;
136
+ blocked ||= this.inFlight.size >= MAX_IN_FLIGHT || this.bytes >= MAX_IN_FLIGHT_BYTES;
137
+ if (blocked && !this.windowBlocked)
138
+ this.lastProgress = Date.now();
139
+ this.windowBlocked = blocked;
140
+ }
141
+ }
142
+ catch (error) {
143
+ this.reportError(error);
144
+ }
145
+ finally {
146
+ this.running = false;
147
+ if (this.send && this.requested)
148
+ this.notify();
149
+ }
150
+ }
151
+ }
package/dist/main.js CHANGED
File without changes
package/dist/serve.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { ExecutionTelemetrySender } from "./execution-telemetry-sender.js";
1
2
  /** 常驻控制面桥接;按请求拉起本地 runtime。 */
2
3
  import { handleAgentEnvironmentFrame } from "./agent-environment-control.js";
3
4
  import { handleMachineEnvironmentFrame } from "./machine-environment-control.js";
@@ -80,6 +81,11 @@ export function serve(config, opts = {}) {
80
81
  const agentMemory = opts.execution?.dependencies?.agentMemory
81
82
  ?? (config.agentMemory === null ? undefined : createAgentMemoryBridge(config.agentMemory));
82
83
  const executionTelemetry = createExecutionTelemetryJournal(config.agentsRoot);
84
+ const telemetrySender = new ExecutionTelemetrySender(executionTelemetry, (error) => {
85
+ dslog("execution.telemetry_replay_failed", "execution telemetry 重放失败", {
86
+ level: "ERROR", error_message: error.message,
87
+ });
88
+ });
83
89
  const executeProtocol = opts.execution?.runExecution ?? runExecution;
84
90
  const localHome = opts.localHome ?? resolveDaemonLocalStateRootFromEnv();
85
91
  const hostCoordinator = opts.execution?.hostCoordinator
@@ -234,6 +240,8 @@ export function serve(config, opts = {}) {
234
240
  if (frame.type === "execution:activity" || frame.type === "execution:console") {
235
241
  try {
236
242
  await executionTelemetry.append(frame);
243
+ telemetrySender.notify();
244
+ return;
237
245
  }
238
246
  catch (error) {
239
247
  dslog("execution.telemetry_journal_failed", "execution telemetry 持久化失败", {
@@ -254,11 +262,6 @@ export function serve(config, opts = {}) {
254
262
  completionRetransmitter.track(entry.completion);
255
263
  }
256
264
  };
257
- const replayExecutionTelemetry = async () => {
258
- const frames = await executionTelemetry.replay();
259
- for (const frame of frames)
260
- safeExecutionSend(frame);
261
- };
262
265
  const snapshotEntry = (entry) => {
263
266
  if ((entry.state === "completed" || entry.state === "interrupted") && entry.completion !== null) {
264
267
  return { executionId: entry.executionId, state: entry.state, completion: entry.completion, updatedAt: entry.updatedAt };
@@ -374,11 +377,7 @@ export function serve(config, opts = {}) {
374
377
  completionRetransmitter.resume();
375
378
  }
376
379
  });
377
- void replayExecutionTelemetry().catch((error) => {
378
- dslog("execution.telemetry_replay_failed", "execution telemetry 重放失败", {
379
- level: "ERROR", error_message: error.message,
380
- });
381
- });
380
+ telemetrySender.start((frame) => ws === openedSocket && safeExecutionSend(frame), () => openedSocket.close(1013, "telemetry ACK timeout"));
382
381
  });
383
382
  connectionSocket.on("message", async (data) => {
384
383
  if (stopped || ws !== connectionSocket)
@@ -450,7 +449,7 @@ export function serve(config, opts = {}) {
450
449
  return;
451
450
  }
452
451
  if (frame.type === "execution:event-ack") {
453
- await executionTelemetry.acknowledge(frame.executionId, frame.kind, frame.seq).catch((error) => {
452
+ await telemetrySender.acknowledge(frame.executionId, frame.kind, frame.seq).catch((error) => {
454
453
  dslog("execution.telemetry_ack_failed", "execution telemetry ACK 处理失败", {
455
454
  level: "ERROR", execution_id: frame.executionId,
456
455
  error_message: error.message,
@@ -1263,6 +1262,7 @@ export function serve(config, opts = {}) {
1263
1262
  // server 能力随连接失效;下个连接的 ready 帧重新声明(防降级重连后沿用过期能力)。
1264
1263
  serverCapabilities = new Set();
1265
1264
  completionRetransmitter.pause();
1265
+ telemetrySender.stop();
1266
1266
  // 4001 = 控制面应用级「鉴权失败」关闭码(见 server control-plane.ts)。即使上面的
1267
1267
  // error 帧因 close 抢先而丢失,也能据关闭码识别这是凭证失效——退避拉满,不再每秒热循环。
1268
1268
  if (reportDaemonConnectionClose(code, log))
@@ -1323,6 +1323,7 @@ export function serve(config, opts = {}) {
1323
1323
  stopped = true;
1324
1324
  concurrencyTelemetry.stop();
1325
1325
  completionRetransmitter.stop();
1326
+ telemetrySender.stop();
1326
1327
  const deadline = createShutdownDeadline(shutdownTimeoutMs);
1327
1328
  if (reconnectTimer !== null) {
1328
1329
  clearTimeout(reconnectTimer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nowcrew/daemon",
3
- "version": "0.6.102-beta.4",
3
+ "version": "0.6.102-beta.5",
4
4
  "type": "module",
5
5
  "description": "crew daemon — 运行在用户机器:拉起/管理 agent 进程,注入 crew CLI,归一化 runtime 事件",
6
6
  "license": "Apache-2.0",
@@ -18,17 +18,9 @@
18
18
  "publishConfig": {
19
19
  "access": "public"
20
20
  },
21
- "scripts": {
22
- "daemon": "pnpm --filter @nowcrew/cli build && tsx src/main.ts",
23
- "build": "tsc -p tsconfig.json",
24
- "prepublishOnly": "pnpm build && node ../scripts/daemon-release-artifact.mjs --strict-registry",
25
- "test": "vitest run",
26
- "test:release-critical": "vitest run test/automatic-daemon-installer.test.ts test/automatic-install-target.test.ts test/automatic-service-migration.test.ts test/computer-cli.test.ts test/computer-service.test.ts test/computer-setup-client.test.ts test/computer-setup-orchestration.test.ts test/computer-setup.test.ts test/daemon-release-version.test.ts test/daemon-update-controller.test.ts test/daemon-update-eligibility.test.ts test/daemon-updater.test.ts test/execution-protocol.test.ts test/machine-info.test.ts --maxWorkers=2 --minWorkers=1",
27
- "typecheck": "tsc --noEmit"
28
- },
29
21
  "dependencies": {
30
22
  "@agentclientprotocol/sdk": "1.2.1",
31
- "@nowcrew/cli": "0.4.17",
23
+ "@nowcrew/cli": "0.4.19-beta.1",
32
24
  "cross-spawn": "^7.0.6",
33
25
  "semver": "7.8.5",
34
26
  "smol-toml": "1.8.0",
@@ -47,5 +39,12 @@
47
39
  "tsx": "^4.19.0",
48
40
  "typescript": "^5.6.0",
49
41
  "vitest": "^2.1.0"
42
+ },
43
+ "scripts": {
44
+ "daemon": "pnpm --filter @nowcrew/cli build && tsx src/main.ts",
45
+ "build": "tsc -p tsconfig.json",
46
+ "test": "vitest run",
47
+ "test:release-critical": "vitest run test/automatic-daemon-installer.test.ts test/automatic-install-target.test.ts test/automatic-service-migration.test.ts test/computer-cli.test.ts test/computer-service.test.ts test/computer-setup-client.test.ts test/computer-setup-orchestration.test.ts test/computer-setup.test.ts test/daemon-release-version.test.ts test/daemon-update-controller.test.ts test/daemon-update-eligibility.test.ts test/daemon-updater.test.ts test/execution-protocol.test.ts test/machine-info.test.ts --maxWorkers=2 --minWorkers=1",
48
+ "typecheck": "tsc --noEmit"
50
49
  }
51
- }
50
+ }