@nowcrew/daemon 0.6.98 → 0.6.99-beta.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.
Files changed (75) hide show
  1. package/README.md +43 -1
  2. package/dist/automatic-daemon-installer.js +41 -10
  3. package/dist/automatic-service-migration.js +7 -6
  4. package/dist/cloud-report-upload.js +1 -10
  5. package/dist/computer-service.js +40 -42
  6. package/dist/computer-setup-client.js +43 -9
  7. package/dist/computer-setup-existing.js +154 -0
  8. package/dist/computer-setup-orchestration.js +32 -6
  9. package/dist/computer-setup.js +37 -6
  10. package/dist/computer-token-setup.js +46 -0
  11. package/dist/execution-journal.js +3 -1
  12. package/dist/execution-protocol.js +1 -1
  13. package/dist/execution-runner.js +4 -1
  14. package/dist/i18n.js +16 -0
  15. package/dist/local-execution-prompt.js +8 -3
  16. package/dist/local-executor.js +64 -39
  17. package/dist/local-memory/commit-telemetry.js +30 -0
  18. package/dist/local-memory/execution-workspace.js +62 -0
  19. package/dist/local-memory/full-index-search.js +47 -0
  20. package/dist/local-memory/index-budget.js +9 -3
  21. package/dist/local-memory/initialization-identity.js +36 -0
  22. package/dist/local-memory/inventory-cli.js +52 -8
  23. package/dist/local-memory/inventory-source.js +15 -8
  24. package/dist/local-memory/inventory-v2.js +362 -0
  25. package/dist/local-memory/legacy-adapter.js +233 -0
  26. package/dist/local-memory/migration-access.js +242 -0
  27. package/dist/local-memory/migration-cli.js +54 -0
  28. package/dist/local-memory/migration-io.js +199 -0
  29. package/dist/local-memory/migration-pending.js +9 -0
  30. package/dist/local-memory/migration-plan.js +432 -0
  31. package/dist/local-memory/migration-schema.js +55 -0
  32. package/dist/local-memory/migration-state-invariants.js +65 -0
  33. package/dist/local-memory/migration-store.js +414 -0
  34. package/dist/local-memory/migration-tool-entry.js +21 -0
  35. package/dist/local-memory/pending-query.js +5 -2
  36. package/dist/local-memory/pending-schema.js +1 -1
  37. package/dist/local-memory/pending.js +16 -2
  38. package/dist/local-memory/prompt.js +21 -5
  39. package/dist/local-memory/read-cli.js +98 -76
  40. package/dist/local-memory/rollout.js +79 -11
  41. package/dist/local-memory/runtime.js +181 -13
  42. package/dist/local-memory/source-path.js +13 -0
  43. package/dist/local-memory/storage-format.js +249 -0
  44. package/dist/local-memory/write.js +189 -21
  45. package/dist/local-memory/writer-mode.js +13 -0
  46. package/dist/local-memory-diagnostics.js +96 -15
  47. package/dist/local-memory-telemetry.js +20 -3
  48. package/dist/machine-info.js +6 -3
  49. package/dist/marketplace-controller.js +8 -21
  50. package/dist/marketplace-protocol.js +1 -1
  51. package/dist/marketplace-remote.js +1 -1
  52. package/dist/marketplace-runner.js +1 -1
  53. package/dist/marketplace-serve.js +2 -4
  54. package/dist/memory-migration-controller.js +89 -0
  55. package/dist/memory-migration-inspect.js +108 -0
  56. package/dist/memory-migration-journal.js +14 -0
  57. package/dist/memory-migration-protocol.js +14 -0
  58. package/dist/memory-migration-runtime.js +305 -0
  59. package/dist/memory-prune-diagnostics.js +60 -4
  60. package/dist/message-file-preview.js +11 -1
  61. package/dist/npm-command.js +5 -5
  62. package/dist/powershell-command.js +12 -0
  63. package/dist/project-skills/machine-hello-publisher.js +95 -6
  64. package/dist/prompt.js +9 -17
  65. package/dist/runner.js +1 -1
  66. package/dist/runtimes/claude-thread-runner.js +25 -4
  67. package/dist/serve.js +92 -10
  68. package/dist/token.js +1 -0
  69. package/dist/windows-scheduled-task.js +3 -3
  70. package/dist/windows-task-principal.js +26 -0
  71. package/dist/workspace-control-handler.js +16 -3
  72. package/dist/workspace.js +13 -2
  73. package/package.json +1 -1
  74. package/dist/marketplace-maintenance-bridge.js +0 -79
  75. package/dist/marketplace-maintenance-protocol.js +0 -40
@@ -0,0 +1,305 @@
1
+ import { createHash } from "node:crypto";
2
+ import { mkdir, lstat, realpath, readFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { fileURLToPath, pathToFileURL } from "node:url";
5
+ import { createRequire } from "node:module";
6
+ import { z } from "zod";
7
+ import { ExecutionCompletedSchema } from "./execution-protocol.js";
8
+ import { startDormantSupervisor } from "./execution-supervisor.js";
9
+ import { MemoryMigrationStartSchema } from "./memory-migration-protocol.js";
10
+ import { MigrationHistorySchema, MigrationIdentitySchema, MigrationStateSchema } from "./local-memory/migration-schema.js";
11
+ import { readMemoryInventorySource, inventoryLimits } from "./local-memory/inventory-source.js";
12
+ const digest = (value) => createHash("sha256").update(value).digest("hex");
13
+ const protectedWindows = new Map();
14
+ const Operation = z.object({ id: z.string(), revision: z.number().int(), executionId: z.string().nullable(),
15
+ status: z.string(), sourceDigest: z.string().nullable(), planDigest: z.string().nullable(),
16
+ writeIntentConfirmedAt: z.string().nullable(), newAgentConfirmed: z.boolean().optional(),
17
+ cancelRequestedAt: z.string().nullable(),
18
+ }).passthrough();
19
+ const Summary = z.object({ result: z.enum(["success", "failed", "needs_review", "repair_required", "missing"]),
20
+ phase: z.string().nullable().optional(), issues: z.array(z.object({ path: z.string(), reason: z.string() }).passthrough()) }).passthrough();
21
+ /** 系统维护在普通执行槽之前分流;只有 server 持久确认意图后才运行活动替换。 */
22
+ export async function runMemoryMigration(raw, deps) {
23
+ const frame = MemoryMigrationStartSchema.parse(raw);
24
+ const common = { agentId: frame.agentId, migrationId: frame.migrationId, machineId: frame.machineId,
25
+ executionId: frame.executionId, attemptKey: frame.attemptKey };
26
+ const protectionKey = `${deps.agentsRoot}:${frame.migrationId}`;
27
+ const exclusive = protectedWindows.get(protectionKey) ?? await deps.coordinator.tryAcquireExclusiveExecution();
28
+ if (!exclusive) {
29
+ await deps.control("busy", { ...common, expectedRevision: frame.expectedRevision });
30
+ return { kind: "busy" };
31
+ }
32
+ let revision = frame.expectedRevision, intentConfirmed = false, acquired = false, acquisitionRequested = false, keepProtection = false;
33
+ let bound;
34
+ let identity, state;
35
+ let serverHistory;
36
+ let intentReceipt;
37
+ let lastPid = null, stopped = true, phaseCount = 0;
38
+ const startedAt = new Date().toISOString();
39
+ const dir = join(deps.agentsRoot, frame.handle);
40
+ const timeoutMs = deps.timeoutMs ?? 60_000;
41
+ const guard = () => ({ ...common, expectedRevision: revision, ...bound });
42
+ const control = async (step, extra = {}) => {
43
+ const response = await deps.control(step, { ...guard(), ...extra });
44
+ const parsed = z.object({ operation: Operation }).parse(response);
45
+ revision = parsed.operation.revision;
46
+ return parsed.operation;
47
+ };
48
+ const readKnownState = async (path) => {
49
+ const source = await readMemoryInventorySource(await realpath(dir), path, inventoryLimits.fileBytes);
50
+ return MigrationStateSchema.parse(JSON.parse(source.content));
51
+ };
52
+ const readState = () => readKnownState(".crew/memory/migration.v1.json");
53
+ const runTool = async (command, extra = {}) => {
54
+ if (!identity)
55
+ throw new Error("migration_identity_missing");
56
+ if (command === "recover" && deps.cancellation?.isRequested())
57
+ extra = { ...extra, cancelled: true };
58
+ const rollback = command === "recover" && extra.cancelled === true;
59
+ if (!rollback && deps.cancellation?.isRequested())
60
+ throw new Error("migration_cancelled");
61
+ if (phaseCount++ > 0) {
62
+ if (!stopped || !deps.journal.retry)
63
+ throw new Error("migration_stop_unproven");
64
+ await deps.journal.retry(frame.executionId);
65
+ }
66
+ const sourceMode = import.meta.url.endsWith(".ts");
67
+ const entry = fileURLToPath(new URL(`./local-memory/migration-tool-entry.${sourceMode ? "ts" : "js"}`, import.meta.url));
68
+ const args = sourceMode ? ["--import", pathToFileURL(createRequire(import.meta.url).resolve("tsx/esm")).href, entry] : [entry];
69
+ let stdout = "";
70
+ const launched = await deps.journal.startGuarded(frame.executionId, new Date().toISOString(), () => (deps.startSupervisor ?? startDormantSupervisor)({ command: process.execPath, args, cwd: dir,
71
+ env: { PATH: process.env.PATH ?? "", CREW_SLS_LOG: "off" },
72
+ stdinText: JSON.stringify({ command, agentDir: dir, identity, ...extra }),
73
+ }, sourceMode ? {
74
+ childEntry: fileURLToPath(new URL("./execution-supervisor-child.ts", import.meta.url)),
75
+ execArgv: ["--import", pathToFileURL(createRequire(import.meta.url).resolve("tsx/esm")).href],
76
+ } : {}), { beforeRelease: async ({ handle }) => {
77
+ lastPid = handle.pid;
78
+ stopped = false;
79
+ handle.stdout.on("data", chunk => { stdout += String(chunk); if (stdout.length > 2 * 1024 * 1024)
80
+ void handle.cancel(); });
81
+ handle.stderr.resume();
82
+ } });
83
+ if (launched.kind !== "started")
84
+ throw new Error("migration_execution_already_running");
85
+ const processHandle = launched.handle;
86
+ deps.send({ type: "execution:started", protocolVersion: 1, executionId: frame.executionId, at: new Date().toISOString() });
87
+ let timedOut = false;
88
+ const timer = setTimeout(() => { timedOut = true; void processHandle.cancel(); }, timeoutMs);
89
+ if (!rollback)
90
+ deps.cancellation?.register(async () => { if (!stopped)
91
+ await processHandle.cancel(); });
92
+ try {
93
+ await processHandle.exit;
94
+ await processHandle.cancel();
95
+ stopped = true;
96
+ if (timedOut)
97
+ throw new Error("migration_window_timeout");
98
+ return Summary.parse(JSON.parse(stdout.trim()));
99
+ }
100
+ finally {
101
+ clearTimeout(timer);
102
+ }
103
+ };
104
+ const finish = async (outcome, reason) => {
105
+ const completion = ExecutionCompletedSchema.parse({ type: "execution:completed", protocolVersion: 1,
106
+ executionId: frame.executionId, runtime: "memory_migration", resumed: false, outcome,
107
+ exitCode: outcome === "succeeded" ? 0 : 1, ...(reason ? { errorCode: reason, errorMessage: reason } : {}),
108
+ startedAt, finishedAt: new Date().toISOString() });
109
+ await deps.journal.complete(frame.executionId, completion);
110
+ deps.send(completion);
111
+ };
112
+ try {
113
+ const history = z.object({ operations: z.array(Operation), serverHistory: MigrationHistorySchema }).passthrough().parse(await deps.control("history", {}));
114
+ serverHistory = history.serverHistory;
115
+ const operation = history.operations.find(op => op.id === frame.migrationId);
116
+ if (!operation || operation.revision !== revision || operation.executionId !== frame.executionId)
117
+ throw new Error("migration_server_identity_mismatch");
118
+ intentConfirmed = operation.writeIntentConfirmedAt !== null;
119
+ await mkdir(dir, { recursive: true });
120
+ const root = await realpath(dir);
121
+ if ((await lstat(dir)).isSymbolicLink())
122
+ throw new Error("migration_root_invalid");
123
+ bound = { storageRootFingerprint: digest(root), ownerIdentity: String((await lstat(root)).uid) };
124
+ identity = MigrationIdentitySchema.parse({ agentId: frame.agentId, migrationId: frame.migrationId,
125
+ machineId: frame.machineId, executionId: frame.executionId, workspaceId: frame.workspaceId,
126
+ rootFingerprint: bound.storageRootFingerprint, ownerIdentity: bound.ownerIdentity,
127
+ sourceStorageVersion: frame.sourceStorageVersion, targetStorageVersion: 2 });
128
+ const { reqId: _transportRequest, ...stableFrame } = frame;
129
+ const accepted = await deps.journal.accept(frame.executionId, digest(JSON.stringify(stableFrame)), { runtime: "memory_migration" });
130
+ if (accepted.kind === "existing" && accepted.completion) {
131
+ deps.send(accepted.completion);
132
+ return { kind: "completed" };
133
+ }
134
+ deps.send({ type: "execution:accepted", protocolVersion: 1, executionId: frame.executionId,
135
+ state: "ready", effectivePermission: "workspace_write", at: new Date().toISOString() });
136
+ acquisitionRequested = true;
137
+ await control("acquired");
138
+ acquired = true;
139
+ if (frame.action === "recover") {
140
+ state = await readState().catch(() => undefined);
141
+ if (state && state.migrationId !== frame.migrationId && state.phase === "rolled_back"
142
+ && history.serverHistory.rolledBackOperations?.some(previous => previous.migrationId === state.migrationId
143
+ && previous.sourceDigest === state.sourceDigest && previous.planDigest === state.planDigest))
144
+ state = undefined;
145
+ if (state) {
146
+ if (state.migrationId !== frame.migrationId || state.identity.rootFingerprint !== bound.storageRootFingerprint
147
+ || state.identity.machineId !== frame.machineId || state.identity.agentId !== frame.agentId)
148
+ throw new Error("migration_state_identity_mismatch");
149
+ identity = state.identity;
150
+ }
151
+ if (!state) {
152
+ state = await readKnownState(`.crew/memory/migrations/${frame.migrationId}/terminal.json`).catch((e) => {
153
+ if (e.code !== "ENOENT")
154
+ throw e;
155
+ return readKnownState(`.crew/memory/migrations/${frame.migrationId}/manifest.json`);
156
+ });
157
+ identity = state.identity;
158
+ }
159
+ if (state.planDigest !== operation.planDigest || state.sourceDigest !== operation.sourceDigest)
160
+ throw new Error("migration_server_plan_mismatch");
161
+ const prior = await deps.journal.get(identity.executionId);
162
+ if (state.phase !== "committed" && (!prior || (prior.state !== "completed" && prior.state !== "interrupted"))) {
163
+ throw new Error("migration_stop_unproven");
164
+ }
165
+ const result = await runTool("recover", { serverHistory: history.serverHistory, cancelled: !intentConfirmed || operation.cancelRequestedAt !== null,
166
+ stoppedReceipt: { kind: "migration_process_stopped", migrationId: frame.migrationId, machineId: frame.machineId,
167
+ executionId: identity.executionId, ownerIdentity: identity.ownerIdentity, pid: null,
168
+ stoppedAt: new Date().toISOString(), proof: "journal_reconciled" } });
169
+ if (result.result !== "success")
170
+ throw new Error("migration_recovery_failed");
171
+ }
172
+ else {
173
+ const prepared = await runTool("prepare", { serverHistory: history.serverHistory,
174
+ ...(frame.recordMappings ? { recordMappings: frame.recordMappings } : {}) });
175
+ if (prepared.result === "needs_review") {
176
+ await control("needs-review", { issues: prepared.issues });
177
+ await finish("failed", "migration_needs_review");
178
+ return { kind: "needs_review" };
179
+ }
180
+ if (prepared.result !== "success")
181
+ throw new Error("migration_prepare_failed");
182
+ state = await readState();
183
+ const confirmed = await control("intent", { sourceDigest: state.sourceDigest, planDigest: state.planDigest,
184
+ minReaderCapabilities: ["memory_storage_v2", "memory_state_guard_v1"], minWriterCapabilities: ["memory_storage_v2", "memory_state_guard_v1"] });
185
+ intentConfirmed = true;
186
+ intentReceipt = { ...identity, kind: "server_write_intent", sourceDigest: state.sourceDigest,
187
+ planDigest: state.planDigest, intentVersion: confirmed.revision, confirmedAt: confirmed.writeIntentConfirmedAt };
188
+ const result = await runTool("apply", { intentReceipt });
189
+ if (result.result !== "success")
190
+ throw new Error("migration_apply_failed");
191
+ }
192
+ state = await readState();
193
+ if (state.phase !== "committed" && state.phase !== "rolled_back")
194
+ throw new Error("migration_terminal_unverified");
195
+ if (!intentConfirmed) {
196
+ if (state.phase !== "rolled_back")
197
+ throw new Error("migration_commit_without_intent");
198
+ await control("no-write", { sourceDigest: state.sourceDigest, planDigest: state.planDigest, verified: true });
199
+ }
200
+ else
201
+ await control("result", { outcome: state.phase, storageVersion: state.storageVersion,
202
+ sourceDigest: state.sourceDigest, planDigest: state.planDigest, origin: state.origin, verified: true });
203
+ await finish("succeeded");
204
+ return { kind: "completed", phase: state.phase };
205
+ }
206
+ catch (error) {
207
+ const reason = error instanceof Error && /^[a-z_]+$/u.test(error.message) ? error.message : "migration_execution_failed";
208
+ if (acquisitionRequested) {
209
+ try {
210
+ const latest = z.object({ operations: z.array(Operation) }).passthrough().parse(await deps.control("history", {}))
211
+ .operations.find(op => op.id === frame.migrationId);
212
+ if (!latest || latest.executionId !== frame.executionId)
213
+ throw new Error("migration_control_owner_changed");
214
+ revision = latest.revision;
215
+ intentConfirmed = latest.writeIntentConfirmedAt !== null;
216
+ acquired = !["needs_review", "rolled_back"].includes(latest.status);
217
+ if (intentConfirmed && identity && state && latest.sourceDigest === state.sourceDigest && latest.planDigest === state.planDigest) {
218
+ intentReceipt = { ...identity, kind: "server_write_intent", sourceDigest: state.sourceDigest, planDigest: state.planDigest,
219
+ intentVersion: latest.revision, confirmedAt: latest.writeIntentConfirmedAt };
220
+ }
221
+ }
222
+ catch {
223
+ keepProtection = true;
224
+ }
225
+ }
226
+ if (acquired) {
227
+ if (intentConfirmed) {
228
+ keepProtection = true;
229
+ if (stopped && identity && lastPid !== null) {
230
+ try {
231
+ const recovered = await runTool("recover", { cancelled: true, ...(intentReceipt ? { intentReceipt } : {}),
232
+ stoppedReceipt: { kind: "migration_process_stopped", migrationId: frame.migrationId, machineId: frame.machineId,
233
+ executionId: identity.executionId, ownerIdentity: identity.ownerIdentity, pid: lastPid,
234
+ stoppedAt: new Date().toISOString(), proof: "supervisor_exit" } });
235
+ state = await readState();
236
+ if (recovered.result === "success" && (state.phase === "committed" || state.phase === "rolled_back")) {
237
+ await control("result", { outcome: state.phase, storageVersion: state.storageVersion,
238
+ sourceDigest: state.sourceDigest, planDigest: state.planDigest, origin: state.origin, verified: true });
239
+ keepProtection = false;
240
+ }
241
+ }
242
+ catch { /* 未证明提交或完整回退时保留保护。 */ }
243
+ }
244
+ if (keepProtection)
245
+ await control("repair", { reason }).catch(() => undefined);
246
+ }
247
+ else if (stopped) {
248
+ let digests;
249
+ try {
250
+ let local = await readState().catch((error) => { if (error.code === "ENOENT")
251
+ return undefined; throw error; });
252
+ if (local?.migrationId !== frame.migrationId) {
253
+ local = await readKnownState(`.crew/memory/migrations/${frame.migrationId}/manifest.json`).catch(async (error) => {
254
+ if (error.code !== "ENOENT")
255
+ throw error;
256
+ const residue = await lstat(join(dir, ".crew/memory/migrations", frame.migrationId)).catch((statError) => {
257
+ if (statError.code === "ENOENT")
258
+ return undefined;
259
+ throw statError;
260
+ });
261
+ if (residue)
262
+ throw new Error("migration_prepare_residue_unverified");
263
+ return undefined;
264
+ });
265
+ }
266
+ if (local?.migrationId === frame.migrationId) {
267
+ if (!identity || lastPid === null || Object.keys(local.identity).some(key => local.identity[key] !== identity[key])
268
+ || local.intentReceipt || !["prepared", "rolled_back"].includes(local.phase))
269
+ throw new Error("migration_no_write_state_unverified");
270
+ digests = { sourceDigest: local.sourceDigest, planDigest: local.planDigest };
271
+ const recovered = await runTool("recover", { cancelled: true,
272
+ serverHistory: { kind: "existing", migrationId: frame.migrationId, writeIntentConfirmed: false,
273
+ ...(serverHistory?.rolledBackOperations ? { rolledBackOperations: serverHistory.rolledBackOperations } : {}) },
274
+ stoppedReceipt: { kind: "migration_process_stopped", migrationId: frame.migrationId, machineId: frame.machineId,
275
+ executionId: identity.executionId, ownerIdentity: identity.ownerIdentity, pid: lastPid,
276
+ stoppedAt: new Date().toISOString(), proof: "supervisor_exit" } });
277
+ state = await readState();
278
+ if (recovered.result !== "success" || state.phase !== "rolled_back")
279
+ throw new Error("migration_no_write_rollback_unverified");
280
+ digests = { sourceDigest: state.sourceDigest, planDigest: state.planDigest };
281
+ }
282
+ await control("no-write", { verified: true, ...digests });
283
+ keepProtection = false;
284
+ }
285
+ catch {
286
+ keepProtection = true;
287
+ await control("repair", { reason: "migration_no_write_rollback_unverified", ...digests }).catch(() => undefined);
288
+ }
289
+ }
290
+ else
291
+ keepProtection = true;
292
+ }
293
+ if (stopped)
294
+ await finish("failed", reason).catch(() => undefined);
295
+ return { kind: "failed", reason, processStopped: stopped, lastPid };
296
+ }
297
+ finally {
298
+ if (!keepProtection && stopped) {
299
+ protectedWindows.delete(protectionKey);
300
+ await exclusive.release();
301
+ }
302
+ else
303
+ protectedWindows.set(protectionKey, exclusive);
304
+ }
305
+ }
@@ -7,6 +7,7 @@ import { dslog } from "./slog.js";
7
7
  const TRACE_PATTERN = /\[memory-prune trace_id=([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\]/i;
8
8
  export const MEMORY_PRUNE_HASH_LIMIT_BYTES = 10 * 1024 * 1024;
9
9
  export const MEMORY_PRUNE_NOTES_MANIFEST_LIMIT = 128;
10
+ export const MEMORY_PRUNE_PENDING_MANIFEST_LIMIT = 128;
10
11
  export const MEMORY_PRUNE_NOTE_CHANGE_LIMIT = 128;
11
12
  function compareCodeUnits(left, right) {
12
13
  return left < right ? -1 : left > right ? 1 : 0;
@@ -192,6 +193,50 @@ export async function inspectMemoryPruneNotes(homeDir, signal) {
192
193
  ...(signal?.aborted ? { error: "ABORT_ERR" } : {}),
193
194
  };
194
195
  }
196
+ export async function inspectMemoryPrunePending(homeDir, signal) {
197
+ const pendingDir = join(homeDir, ".crew", "memory", "pending");
198
+ try {
199
+ const metadata = await lstat(pendingDir);
200
+ if (metadata.isSymbolicLink())
201
+ return { files: [], total_count: 0, omitted_count: 0, truncated: false, error: "symbolic_link" };
202
+ if (!metadata.isDirectory())
203
+ return { files: [], total_count: 0, omitted_count: 0, truncated: false, error: "ENOTDIR" };
204
+ }
205
+ catch (error) {
206
+ if (error.code === "ENOENT")
207
+ return { files: [], total_count: 0, omitted_count: 0, truncated: false };
208
+ return { files: [], total_count: 0, omitted_count: 0, truncated: false, error: diagnosticError(error) };
209
+ }
210
+ let names;
211
+ try {
212
+ names = (await readdir(pendingDir, { withFileTypes: true }))
213
+ .filter(entry => (entry.isFile() || entry.isSymbolicLink()) && entry.name.endsWith(".json"))
214
+ .map(entry => entry.name).sort(compareCodeUnits);
215
+ }
216
+ catch (error) {
217
+ return { files: [], total_count: 0, omitted_count: 0, truncated: false, error: diagnosticError(error) };
218
+ }
219
+ const selected = names.slice(0, MEMORY_PRUNE_PENDING_MANIFEST_LIMIT);
220
+ const files = [];
221
+ for (const name of selected) {
222
+ if (signal?.aborted)
223
+ break;
224
+ const path = join(pendingDir, name);
225
+ try {
226
+ if ((await lstat(path)).isSymbolicLink()) {
227
+ files.push({ relative_path: `.crew/memory/pending/${name}`, exists: false, error: "symbolic_link" });
228
+ continue;
229
+ }
230
+ }
231
+ catch (error) {
232
+ files.push({ relative_path: `.crew/memory/pending/${name}`, exists: false, error: diagnosticError(error) });
233
+ continue;
234
+ }
235
+ files.push({ relative_path: `.crew/memory/pending/${name}`, ...await inspectFile(path, false, undefined, signal) });
236
+ }
237
+ return { files, total_count: names.length, omitted_count: Math.max(0, names.length - selected.length),
238
+ truncated: names.length > selected.length, ...(signal?.aborted ? { error: "ABORT_ERR" } : {}) };
239
+ }
195
240
  async function inspectMemoryPruneFixedFiles(homeDir, runDir, workLogPath, signal) {
196
241
  const cwdIsHome = runDir === homeDir;
197
242
  const [memory, lessons, cwdMemory, cwdLessons, workLog] = await Promise.all([
@@ -210,15 +255,17 @@ async function inspectMemoryPruneFixedFiles(homeDir, runDir, workLogPath, signal
210
255
  };
211
256
  }
212
257
  export async function inspectMemoryPruneFiles(homeDir, runDir, workLogPath, signal) {
213
- const [fixedFiles, notesManifest] = await Promise.all([
258
+ const [fixedFiles, notesManifest, pendingManifest] = await Promise.all([
214
259
  inspectMemoryPruneFixedFiles(homeDir, runDir, workLogPath, signal),
215
260
  inspectMemoryPruneNotes(homeDir, signal),
261
+ inspectMemoryPrunePending(homeDir, signal),
216
262
  ]);
217
- return { ...fixedFiles, notes_manifest: notesManifest };
263
+ return { ...fixedFiles, notes_manifest: notesManifest, pending_manifest: pendingManifest };
218
264
  }
219
265
  export async function inspectMemoryPruneFilesWithinDeadline(homeDir, runDir, workLogPath, timeoutMs) {
220
266
  const fixedFiles = await withinMemoryPruneDiagnosticsDeadline((signal) => inspectMemoryPruneFixedFiles(homeDir, runDir, workLogPath, signal), timeoutMs);
221
267
  let notesManifest;
268
+ let pendingManifest;
222
269
  try {
223
270
  notesManifest = await withinMemoryPruneDiagnosticsDeadline((signal) => inspectMemoryPruneNotes(homeDir, signal), timeoutMs);
224
271
  }
@@ -231,7 +278,13 @@ export async function inspectMemoryPruneFilesWithinDeadline(homeDir, runDir, wor
231
278
  error: diagnosticError(error),
232
279
  };
233
280
  }
234
- return { ...fixedFiles, notes_manifest: notesManifest };
281
+ try {
282
+ pendingManifest = await withinMemoryPruneDiagnosticsDeadline(signal => inspectMemoryPrunePending(homeDir, signal), timeoutMs);
283
+ }
284
+ catch (error) {
285
+ pendingManifest = { files: [], total_count: 0, omitted_count: 0, truncated: false, error: diagnosticError(error) };
286
+ }
287
+ return { ...fixedFiles, notes_manifest: notesManifest, pending_manifest: pendingManifest };
235
288
  }
236
289
  export async function withinMemoryPruneDiagnosticsDeadline(operation, timeoutMs) {
237
290
  const controller = new AbortController();
@@ -361,6 +414,7 @@ export function evaluateMemoryPrunePostcondition(before, after, execution) {
361
414
  && (after.cwd_memory.lessons_reference_count ?? 0) > 0,
362
415
  cwd_lessons_changed: !after.cwd_lessons.same_as_shared_path
363
416
  && fileChanged(before.cwd_lessons, after.cwd_lessons),
417
+ pending_changed: diffMemoryPruneNotes(before.pending_manifest ?? { files: [], total_count: 0, omitted_count: 0, truncated: false }, after.pending_manifest ?? { files: [], total_count: 0, omitted_count: 0, truncated: false }).change_count > 0,
364
418
  };
365
419
  let outcome;
366
420
  if (!execution.executorCompleted)
@@ -388,6 +442,8 @@ export function evaluateMemoryPrunePostcondition(before, after, execution) {
388
442
  outcome = "promoted_without_index_change";
389
443
  else if (facts.shared_memory_changed)
390
444
  outcome = "shared_memory_updated";
445
+ else if (facts.pending_changed)
446
+ outcome = "pending_updated";
391
447
  else
392
448
  outcome = "no_files_changed";
393
449
  return { outcome, ...facts };
@@ -408,7 +464,7 @@ function memoryPruneFileFactFields(fileFacts) {
408
464
  }
409
465
  return fields;
410
466
  }
411
- export function memoryPruneSnapshotFields({ notes_manifest: _notesManifest, ...fileFacts }) {
467
+ export function memoryPruneSnapshotFields({ notes_manifest: _notesManifest, pending_manifest: _pendingManifest, ...fileFacts }) {
412
468
  return memoryPruneFileFactFields(fileFacts);
413
469
  }
414
470
  export function logMemoryPruneDiagnosticsFailure(input, traceId, phase, error) {
@@ -9,6 +9,7 @@ import { z } from "zod";
9
9
  import { isInternalEnvironmentPath } from "./workspace-fs.js";
10
10
  import { opaqueKey, safeKey } from "./workspace.js";
11
11
  import { AgentHandleSchema } from "./execution-protocol.js";
12
+ import { withMemoryAccess } from "./local-memory/migration-access.js";
12
13
  const MAX_BYTES = 256 * 1024;
13
14
  const MAX_BINARY_BYTES = 8 * 1024 * 1024;
14
15
  const MAX_CONCURRENT_READS = 4;
@@ -231,7 +232,16 @@ export async function readSharedMessageFile(agentsRoot, rawRequest, registry) {
231
232
  throw new SharedFileError("file_preview_busy");
232
233
  activeReads++;
233
234
  try {
234
- return await readCandidate(await resolveCandidate(agentsRoot, parsed.data, registry));
235
+ const candidate = await resolveCandidate(agentsRoot, parsed.data, registry);
236
+ const agentRoot = await realpath(resolve(agentsRoot, parsed.data.handle)).catch((error) => {
237
+ if (error.code === "ENOENT")
238
+ return undefined;
239
+ throw error;
240
+ });
241
+ // 记忆保护只用于该Agent根内资料,独立项目文件仍按原执行授权读取。
242
+ return await (agentRoot && inside(agentRoot, candidate.path)
243
+ ? withMemoryAccess(agentRoot, () => readCandidate(candidate))
244
+ : readCandidate(candidate));
235
245
  }
236
246
  catch (error) {
237
247
  if (error instanceof SharedFileError)
@@ -1,9 +1,9 @@
1
1
  import { posix, win32 } from "node:path";
2
- /** Execute POSIX npm through its owning Node binary so service PATH is irrelevant. */
2
+ /** Execute npm through its owning Node binary so PATH and cmd.exe quoting are irrelevant. */
3
3
  export function npmInvocation(platform, nodePath = process.execPath) {
4
4
  const paths = platform === "win32" ? win32 : posix;
5
- const npmPath = paths.join(paths.dirname(nodePath), platform === "win32" ? "npm.cmd" : "npm");
6
- return platform === "win32"
7
- ? { command: npmPath, argsPrefix: [] }
8
- : { command: nodePath, argsPrefix: [npmPath] };
5
+ const npmPath = platform === "win32"
6
+ ? paths.join(paths.dirname(nodePath), "node_modules", "npm", "bin", "npm-cli.js")
7
+ : paths.join(paths.dirname(nodePath), "npm");
8
+ return { command: nodePath, argsPrefix: [npmPath] };
9
9
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * powershell.exe -Command joins trailing argv into command text, rather than
3
+ * binding it to $args. Invoke a scriptblock with literal arguments inside one
4
+ * UTF-16LE encoded command. Encoding is transport, not encryption.
5
+ */
6
+ export function powershellCommandArgs(script, args = []) {
7
+ // PowerShell also treats typographic single quotes as string delimiters.
8
+ const literals = args.map((arg) => `'${arg.replace(/['\u2018-\u201b]/gu, (quote) => quote + quote)}'`).join(" ");
9
+ const command = "[Console]::OutputEncoding=[Text.UTF8Encoding]::new($false);\n"
10
+ + `& {\n${script}\n} ${literals}`;
11
+ return ["-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(command, "utf16le").toString("base64")];
12
+ }
@@ -1,20 +1,109 @@
1
1
  import { WebSocket } from "ws";
2
+ const HELLO_RETRY_DELAYS_MS = [1_000, 2_000, 4_000];
2
3
  /** 记住最近一次 hello 与 socket,便于存储身份变化后重发;Project Skills 状态恒为 ready。 */
3
4
  export function createMachineHelloPublisher(input) {
4
5
  let latest = null;
5
6
  let latestSocket = null;
6
- const republish = () => {
7
- if (latest === null || latestSocket?.readyState !== WebSocket.OPEN)
7
+ let acknowledged = false;
8
+ let attempts = 0;
9
+ // 发送代次:每开一轮新发送(attach/publish/republish)自增,随帧发出并要求 ack 回显。
10
+ let generation = 0;
11
+ let retryTimer;
12
+ const clearRetry = () => {
13
+ if (retryTimer !== undefined) {
14
+ clearTimeout(retryTimer);
15
+ retryTimer = undefined;
16
+ }
17
+ };
18
+ const scheduleRetry = () => {
19
+ clearRetry();
20
+ if (acknowledged || latest === null || latestSocket?.readyState !== WebSocket.OPEN)
21
+ return;
22
+ // 当前 server 不支持 ack → 不排重试(否则永远等不到确认,白跑满退避)。
23
+ if (input.ackSupported && !input.ackSupported())
24
+ return;
25
+ const delay = HELLO_RETRY_DELAYS_MS[attempts - 1];
26
+ if (delay === undefined) {
27
+ // 退避跑满仍未 ack:期待 ack(ackSupported 已声明支持)才升级为断连重连,避免旧行为下反复重连。
28
+ if (input.ackSupported !== undefined && latestSocket !== null)
29
+ input.onExhausted?.(latestSocket);
8
30
  return;
9
- const hello = input.effective({ ...latest, projectSkillsStatus: "ready" });
31
+ }
32
+ retryTimer = setTimeout(() => {
33
+ retryTimer = undefined;
34
+ sendCurrent();
35
+ }, delay);
36
+ };
37
+ function sendCurrent() {
38
+ if (latest === null || latestSocket?.readyState !== WebSocket.OPEN || acknowledged)
39
+ return;
40
+ // helloSeq 随帧发出;同一代次的多次重试复用同一 seq,ack 回显该 seq 即视为确认。
41
+ const hello = input.effective({ ...latest, projectSkillsStatus: "ready", helloSeq: generation });
42
+ const attempt = attempts + 1;
43
+ attempts = attempt;
10
44
  try {
11
45
  latestSocket.send(JSON.stringify(hello));
12
- input.onSent(hello);
46
+ input.onSent(hello, attempt);
47
+ }
48
+ catch (error) {
49
+ input.onSendFailed?.(hello, attempt, error);
13
50
  }
14
- catch { /* 非 OPEN,忽略 */ }
51
+ scheduleRetry();
52
+ }
53
+ const attach = (socket) => {
54
+ clearRetry();
55
+ latestSocket = socket;
56
+ acknowledged = false;
57
+ attempts = 0;
58
+ generation += 1;
59
+ sendCurrent();
60
+ };
61
+ const detach = (socket) => {
62
+ if (latestSocket !== socket)
63
+ return;
64
+ clearRetry();
65
+ latestSocket = null;
66
+ acknowledged = false;
67
+ attempts = 0;
68
+ };
69
+ const republish = () => {
70
+ if (latest === null || latestSocket === null)
71
+ return;
72
+ clearRetry();
73
+ acknowledged = false;
74
+ attempts = 0;
75
+ generation += 1;
76
+ sendCurrent();
77
+ };
78
+ const acknowledge = (socket, seq) => {
79
+ // 非当前 socket / 已确认 / 该 socket 已在关闭(close 后迟到的 ack)一律忽略:
80
+ // 后者会错误触发 onAck→清零强制重连计数,削弱重连上限。
81
+ if (latestSocket !== socket || latest === null || acknowledged || socket.readyState !== WebSocket.OPEN)
82
+ return;
83
+ // 代次校验:只接受回显当前代次的 ack,忽略上一代次迟到的 ack(否则会误停新代次的重试)。
84
+ // seq 缺省(老 server 不回显)时退回仅按 socket 匹配,保持兼容。
85
+ if (seq !== undefined && seq !== generation)
86
+ return;
87
+ acknowledged = true;
88
+ clearRetry();
89
+ input.onAck?.(input.effective({ ...latest, projectSkillsStatus: "ready" }));
15
90
  };
16
91
  return Object.freeze({
17
- publish: (hello, socket) => { latest = hello; latestSocket = socket; republish(); },
92
+ publish: (hello, socket) => {
93
+ latest = hello;
94
+ if (latestSocket !== socket) {
95
+ attach(socket);
96
+ return;
97
+ }
98
+ acknowledged = false;
99
+ attempts = 0;
100
+ generation += 1;
101
+ clearRetry();
102
+ sendCurrent();
103
+ },
104
+ attach,
105
+ detach,
18
106
  republish,
107
+ acknowledge,
19
108
  });
20
109
  }
package/dist/prompt.js CHANGED
@@ -5,6 +5,7 @@
5
5
  * claim-before-work、freshness/draft、"做完所有事再停"、inbox notice 语义、
6
6
  * 分层记忆与压缩安全、协作礼仪。措辞为本项目原创。
7
7
  */
8
+ import { detectLocalMemoryStorageVersion, selectLegacyMemoryPreview, selectLocalMemoryPreview, } from "./local-memory/storage-format.js";
8
9
  /** work-log 注入上限(字符):防冷启动注入随文件膨胀。超限保头(目标/结论)+尾(最近进展),
9
10
  * 截中段并提示读全文——全文仍在盘上,只限"默认推送量"不限"可获取量"。纯函数。 */
10
11
  export const WORKLOG_INJECT_CAP = 8000;
@@ -67,23 +68,14 @@ export function capWorkLogForInject(workLog, cap = WORKLOG_INJECT_CAP) {
67
68
  const tail = workLog.slice(-WORKLOG_TAIL);
68
69
  return `${head}\n…(work-log 过长已截去中段,全文用 Read 读 $CREW_TASK_LOG)\n${tail}`;
69
70
  }
70
- export function capMemoryForInject(memory, cap = MEMORY_INJECT_CAP, maxBytes = Number.POSITIVE_INFINITY) {
71
- if (memory.length <= cap && Buffer.byteLength(memory, "utf8") <= maxBytes)
72
- return memory;
73
- const notice = "[MEMORY.md preview truncated; use crew-memory search/read for more.]\n";
74
- const noticeBytes = Buffer.byteLength(notice, "utf8");
75
- if (notice.length > cap || noticeBytes > maxBytes)
76
- return "";
77
- let end = 0;
78
- let bytes = noticeBytes;
79
- for (const character of memory) {
80
- const size = Buffer.byteLength(character, "utf8");
81
- if (notice.length + end + character.length > cap || bytes + size > maxBytes)
82
- break;
83
- end += character.length;
84
- bytes += size;
71
+ export function capMemoryForInject(memory, cap = MEMORY_INJECT_CAP, maxBytes = Number.POSITIVE_INFINITY, query = "") {
72
+ if (detectLocalMemoryStorageVersion(memory) === 2) {
73
+ return selectLocalMemoryPreview(memory, query, { maxCodePoints: cap, maxBytes }).memory;
74
+ }
75
+ if (memory.length > cap || Buffer.byteLength(memory, "utf8") > maxBytes) {
76
+ return selectLegacyMemoryPreview(memory, query, { maxCodePoints: cap, maxBytes });
85
77
  }
86
- return notice + memory.slice(0, end);
78
+ return memory;
87
79
  }
88
80
  export function buildSystemPrompt(ctx) {
89
81
  const product = ctx.productName ?? "nowwork";
@@ -342,7 +334,7 @@ ${interactiveTaskRules}${freshnessRule}${externalReplyRule}${collaborationEtique
342
334
 
343
335
  ## Workspace 与分层记忆(CRITICAL — 索引+按需,配合上面的并行规则)
344
336
  你的持久记忆在 \`$CREW_HOME\`(跨你所有任务共享),按“常驻索引 + 功能明细 + 当前工作”分层:
345
- 1. **MEMORY.md = 常驻索引 + 角色**:提示词末尾可能只有它的有界预览,预算不足时也可能省略。预览未命中不等于没有记忆。索引条目包含 \`file/type/scope/summary/triggers/priority/updated\`,其中 file 是相对于 \`$CREW_HOME\` 的路径(例如 \`notes/testing.md\`);需要历史约束、经验或偏好时先检索完整索引,再按需分段读取。
337
+ 1. **MEMORY.md = 常驻索引 + 角色**:提示词末尾可能只有它的有界预览,预算不足时也可能省略。预览未命中不等于没有记忆。旧版索引可能包含 \`scope\`;V2 规范字段为 \`id/file/type/status/summary/triggers/priority/revision/anchor/updated\`(可选 \`review_after\`),不含 \`scope\`。其中 file 是相对于 \`$CREW_HOME\` 的路径(例如 \`notes/testing.md\`);需要历史约束、经验或偏好时先检索完整索引,再按需分段读取。
346
338
  2. **notes/<topic>.md = 功能明细**:按用户偏好、项目规则、测试、调试、Git、事故复盘等主题拆分。优先读取与当前任务匹配的文件;\`notes/lessons.md\` 只保留未分类经验和历史兼容内容,不是所有新经验的默认目的地。普通任务默认只读不写,但当前用户明确要求时可以按最小范围更新;任务结束后的专门收尾执行仍按收尾指令复核。
347
339
  - 对旧版 MEMORY.md,如果条目没有完整结构化字段但明确指向 \`notes/<file>.md\`,把它视为 legacy 索引;仍需结合条目标题/链接和当前请求判断相关性,不要因为缺少 summary/triggers 就假定正文已注入。
348
340
  3. **本任务 work-log = \`$CREW_TASK_LOG\`**:每任务一份(别的运行不会碰),记当前阶段/下一步/卡点/关键结论。系统已把它的当前内容注入末尾让你续上;有进展就更新它——这是你**唯一**该在任务中持续写的记忆文件。**work-log 是现状快照,不是流水账**:保持 ≤100 行,更新时重写全文替换过时内容,不要无限追加(过长会被注入截断)。