@nowcrew/daemon 0.6.88-beta.3 → 0.6.88-beta.30

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 (129) hide show
  1. package/README.md +89 -14
  2. package/dist/agent-ability/controller.js +11 -1
  3. package/dist/agent-ability/git-errors.js +22 -0
  4. package/dist/agent-ability/materializer.js +21 -10
  5. package/dist/agent-ability/resolver.js +12 -4
  6. package/dist/agent-environment-contract.js +55 -0
  7. package/dist/agent-environment-control.js +24 -0
  8. package/dist/agent-environment-execution.js +98 -0
  9. package/dist/agent-environments.js +275 -0
  10. package/dist/agent-memory/policy.js +3 -1
  11. package/dist/automatic-daemon-installer.js +51 -2
  12. package/dist/automatic-install-target.js +8 -0
  13. package/dist/computer-main.js +18 -0
  14. package/dist/computer-profile.js +17 -4
  15. package/dist/computer-setup-client.js +318 -0
  16. package/dist/computer-setup-orchestration.js +203 -0
  17. package/dist/computer-setup.js +244 -0
  18. package/dist/config.js +30 -12
  19. package/dist/console-formatter.js +3 -3
  20. package/dist/console.js +15 -2
  21. package/dist/daemon-update-eligibility.js +1 -1
  22. package/dist/execution-journal.js +9 -6
  23. package/dist/execution-protocol.js +2 -1
  24. package/dist/execution-runner.js +16 -10
  25. package/dist/i18n.js +19 -0
  26. package/dist/list-models.js +13 -11
  27. package/dist/local-execution-lease.js +22 -0
  28. package/dist/local-executor.js +88 -92
  29. package/dist/local-memory/candidate.js +43 -0
  30. package/dist/local-memory/index-budget.js +8 -0
  31. package/dist/local-memory/inventory-cli.js +61 -0
  32. package/dist/local-memory/inventory-preview.js +159 -0
  33. package/dist/local-memory/inventory-source.js +159 -0
  34. package/dist/local-memory/pending-match.js +29 -0
  35. package/dist/local-memory/pending-query.js +100 -0
  36. package/dist/local-memory/pending-schema.js +29 -0
  37. package/dist/local-memory/pending-source.js +126 -0
  38. package/dist/local-memory/pending.js +34 -84
  39. package/dist/local-memory/prompt.js +20 -12
  40. package/dist/local-memory/read-cli.js +97 -12
  41. package/dist/local-memory/rollout.js +82 -0
  42. package/dist/local-memory/runtime.js +24 -17
  43. package/dist/local-memory/work-log-execution.js +31 -0
  44. package/dist/local-memory/work-log.js +58 -0
  45. package/dist/local-memory/write.js +27 -58
  46. package/dist/local-runtime-environment.js +50 -0
  47. package/dist/machine-environment-cache.js +147 -0
  48. package/dist/machine-environment-contract.js +10 -0
  49. package/dist/machine-environment-control.js +27 -0
  50. package/dist/machine-environment-policy.js +75 -0
  51. package/dist/machine-environment-process.js +110 -0
  52. package/dist/machine-info.js +70 -25
  53. package/dist/machine-runtime-environment.js +189 -0
  54. package/dist/machine-shell-environment-capture.js +135 -0
  55. package/dist/machine-windows-environment-capture.js +215 -0
  56. package/dist/machine-windows-environment-script.js +158 -0
  57. package/dist/main.js +4 -69
  58. package/dist/marketplace-controller.js +75 -0
  59. package/dist/marketplace-protocol.js +38 -0
  60. package/dist/marketplace-runner.js +234 -0
  61. package/dist/marketplace-serve.js +17 -0
  62. package/dist/message-file-preview.js +246 -0
  63. package/dist/native-thread-context.js +59 -0
  64. package/dist/pre-admission-rejection.js +64 -0
  65. package/dist/project-skills/execution-adapter.js +3 -2
  66. package/dist/project-skills/runtime-launch.js +14 -0
  67. package/dist/prompt.js +15 -15
  68. package/dist/provider-env.js +26 -15
  69. package/dist/runner.js +31 -19
  70. package/dist/runtime-environment-merge.js +16 -0
  71. package/dist/runtime-health-handler.js +10 -0
  72. package/dist/runtime-health.js +3 -1
  73. package/dist/runtime-path.js +22 -12
  74. package/dist/runtime-probe.js +35 -6
  75. package/dist/runtimes/acp-probe-transport.js +69 -0
  76. package/dist/runtimes/claude-models.js +20 -9
  77. package/dist/runtimes/claude-session-start-hook.js +24 -0
  78. package/dist/runtimes/claude-thread-runner.js +266 -0
  79. package/dist/runtimes/claude.js +23 -4
  80. package/dist/runtimes/codex-app-server-runner.js +43 -4
  81. package/dist/runtimes/deepseek-harness-launch.js +10 -0
  82. package/dist/runtimes/deepseek-harness-profile.js +92 -0
  83. package/dist/runtimes/deepseek-harness-runner.js +226 -0
  84. package/dist/runtimes/deepseek-harness.js +76 -15
  85. package/dist/runtimes/harness-probe-retry.js +42 -0
  86. package/dist/runtimes/hermes-models.js +2 -1
  87. package/dist/runtimes/kimi-acp-runner.js +84 -15
  88. package/dist/runtimes/opencode-runner.js +7 -1
  89. package/dist/runtimes/opencode.js +2 -2
  90. package/dist/serve.js +95 -184
  91. package/dist/server-url.js +25 -0
  92. package/dist/session.js +4 -0
  93. package/dist/slog.js +2 -1
  94. package/dist/storage-identity.js +0 -3
  95. package/dist/storage-runtime.js +2 -2
  96. package/dist/storage-serve-adapter.js +6 -72
  97. package/dist/supervised-runtime.js +28 -18
  98. package/dist/thread-input-delivery.js +217 -0
  99. package/dist/token.js +0 -1
  100. package/dist/workspace-control-handler.js +27 -0
  101. package/dist/workspace-fs.js +66 -15
  102. package/dist/workspace.js +1 -1
  103. package/package.json +5 -4
  104. package/dist/codex-home-launch.js +0 -7
  105. package/dist/codex-home-mode-controller.js +0 -68
  106. package/dist/daemon-migration-controller.js +0 -211
  107. package/dist/daemon-migration-wiring.js +0 -22
  108. package/dist/runtimes/codex-home-migration-cli.js +0 -26
  109. package/dist/runtimes/codex-home-migration.js +0 -112
  110. package/dist/runtimes/codex-home.js +0 -236
  111. package/dist/storage-control.js +0 -212
  112. package/dist/storage-copy.js +0 -628
  113. package/dist/storage-credential-guard.js +0 -21
  114. package/dist/storage-doctor.js +0 -228
  115. package/dist/storage-manifest.js +0 -1027
  116. package/dist/storage-migration-controller.js +0 -440
  117. package/dist/storage-migration-manifest.js +0 -105
  118. package/dist/storage-migration-policy.js +0 -100
  119. package/dist/storage-runtime-controller.js +0 -381
  120. package/dist/storage-runtime-readiness.js +0 -240
  121. package/dist/storage-safe-fs.js +0 -302
  122. package/dist/storage-status-delivery.js +0 -224
  123. package/dist/storage-status-replay.js +0 -140
  124. package/dist/storage-target-confirmation.js +0 -129
  125. package/dist/storage-worker-command.js +0 -37
  126. package/dist/storage-worker-executor.js +0 -449
  127. package/dist/storage-worker-handoff.js +0 -151
  128. package/dist/storage-worker-operation.js +0 -294
  129. package/dist/storage-worker-status.js +0 -259
@@ -1,34 +1,11 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { lstat, mkdir, readdir, realpath, rename, rm, writeFile, readFile } from "node:fs/promises";
2
+ import { lstat, mkdir, realpath, rename, rm, writeFile } from "node:fs/promises";
3
3
  import { basename, dirname, join, relative, resolve, sep } from "node:path";
4
- import { z } from "zod";
4
+ import { isRecallablePending, parsePendingMemoryCandidate } from "./pending-schema.js";
5
+ import { pendingFileNames, pendingReadDirectory, PENDING_FILE_BYTES, readPendingSource } from "./pending-source.js";
6
+ import { pendingRecallScore } from "./pending-match.js";
5
7
  export const LOCAL_MEMORY_PENDING_SCAN_FILE_CAP = 128;
6
8
  export const LOCAL_MEMORY_PENDING_SCAN_BYTE_CAP = 1_000_000;
7
- const CandidateSchema = z.object({
8
- operation: z.enum(["create", "update", "supersede"]),
9
- memoryId: z.string().nullable(),
10
- baseRevision: z.number().int().nonnegative().nullable(),
11
- supersedesMemoryId: z.string().nullable(),
12
- title: z.string().min(1),
13
- kind: z.enum(["procedural", "semantic", "episodic", "preference"]),
14
- summary: z.string().min(1),
15
- triggers: z.array(z.string()).min(1),
16
- priority: z.enum(["high", "medium", "low"]),
17
- detailCategory: z.enum(["preferences", "project-rules", "testing", "debugging", "git", "incidents", "lessons"]),
18
- detailBody: z.string().min(1),
19
- reviewAfter: z.string().nullable(),
20
- }).strict();
21
- const PendingSchema = z.object({
22
- schemaVersion: z.literal(1),
23
- pendingId: z.string().regex(/^[A-Za-z0-9][A-Za-z0-9._-]*$/u),
24
- createdAt: z.string().min(1),
25
- reason: z.literal("index_over_budget"),
26
- source: z.object({
27
- executionId: z.string().min(1),
28
- pruneTraceId: z.string().min(1).nullable(),
29
- }).strict(),
30
- candidate: CandidateSchema,
31
- }).strict();
32
9
  function within(root, candidate) {
33
10
  const path = relative(root, candidate);
34
11
  return path === "" || (path !== ".." && !path.startsWith(`..${sep}`));
@@ -122,13 +99,11 @@ async function atomicWrite(path, content) {
122
99
  }
123
100
  }
124
101
  export async function persistPendingMemoryCandidate(input) {
125
- const parsed = PendingSchema.safeParse(input.candidate);
126
- if (!parsed.success)
127
- throw new Error("local_memory_pending_invalid");
102
+ const parsed = parsePendingMemoryCandidate(input.candidate);
128
103
  const directory = await pendingDirectory(input.agentDir);
129
- const target = join(directory, `${parsed.data.pendingId}.json`);
104
+ const target = join(directory, `${parsed.pendingId}.json`);
130
105
  await assertRegularFile(target);
131
- await atomicWrite(target, `${JSON.stringify(parsed.data, null, 2)}\n`);
106
+ await atomicWrite(target, `${JSON.stringify(parsed, null, 2)}\n`);
132
107
  return target;
133
108
  }
134
109
  export async function removePendingMemoryCandidate(input) {
@@ -140,73 +115,44 @@ export async function removePendingMemoryCandidate(input) {
140
115
  await rm(target, { force: true });
141
116
  }
142
117
  export async function readPendingMemoryCandidates(input) {
143
- const directory = await existingPendingDirectory(input.agentDir);
144
- if (directory === null)
145
- return [];
146
- const names = (await readdir(directory)).filter((name) => name.endsWith(".json")).sort();
118
+ const { directory } = await pendingReadDirectory(input.agentDir).catch((error) => {
119
+ if (error instanceof Error && error.message === "memory_pending_path_invalid") {
120
+ throw new Error("local_memory_pending_path_invalid");
121
+ }
122
+ throw error;
123
+ });
124
+ const names = await pendingFileNames(directory);
147
125
  const maxFiles = Math.max(0, input.maxFiles ?? LOCAL_MEMORY_PENDING_SCAN_FILE_CAP);
148
126
  const maxBytes = Math.max(0, input.maxBytes ?? LOCAL_MEMORY_PENDING_SCAN_BYTE_CAP);
149
127
  const result = [];
150
128
  let scannedBytes = 0;
151
- for (const name of names) {
152
- if (result.length >= maxFiles || scannedBytes >= maxBytes)
129
+ for (const name of names.slice(0, maxFiles)) {
130
+ if (scannedBytes >= maxBytes)
153
131
  break;
154
- const path = join(directory, name);
155
132
  try {
156
- await assertRegularFile(path);
157
- const stat = await lstat(path);
158
- if (!stat.isFile() || stat.isSymbolicLink())
159
- throw new Error("local_memory_pending_path_invalid");
160
- if (scannedBytes + stat.size > maxBytes)
161
- break;
162
- const raw = await readFile(path, "utf8");
163
- const size = Buffer.byteLength(raw, "utf8");
164
- if (scannedBytes + size > maxBytes)
165
- break;
166
- scannedBytes += size;
167
- const parsed = PendingSchema.safeParse(JSON.parse(raw));
168
- if (parsed.success && `${parsed.data.pendingId}.json` === name)
169
- result.push(parsed.data);
133
+ const source = await readPendingSource(input.agentDir, name, maxBytes - scannedBytes);
134
+ scannedBytes += source.bytes;
135
+ result.push(source.candidate);
170
136
  }
171
137
  catch {
172
- // A damaged pending item is diagnosable and must not hide other candidates.
138
+ // 失败读取也消耗预算,显式分页命令负责返回诊断和继续扫描。
139
+ scannedBytes += Math.min(PENDING_FILE_BYTES, maxBytes - scannedBytes);
173
140
  }
174
141
  }
175
142
  return result;
176
143
  }
177
- function normalize(value) {
178
- return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
179
- }
180
144
  function priorityRank(priority) {
181
145
  return priority === "high" ? 0 : priority === "medium" ? 1 : 2;
182
146
  }
183
- function candidateText(candidate) {
184
- return [
185
- candidate.candidate.title,
186
- candidate.candidate.summary,
187
- ...candidate.candidate.triggers,
188
- candidate.candidate.detailCategory,
189
- candidate.candidate.detailBody,
190
- ].join("\n");
191
- }
192
- function recallScore(candidate, query) {
193
- const normalizedQuery = normalize(query);
194
- if (normalizedQuery === "")
195
- return 1;
196
- const text = normalize(candidateText(candidate));
197
- if (text.includes(normalizedQuery))
198
- return 100;
199
- const terms = normalizedQuery.split(/[\s,,、。!?;::()[\]{}"'`]+/u).filter((term) => term.length > 0);
200
- return terms.reduce((score, term) => score + (text.includes(term) ? 1 : 0), 0);
201
- }
202
147
  export async function searchPendingMemoryCandidates(input) {
203
148
  const candidates = await readPendingMemoryCandidates({ agentDir: input.agentDir });
204
149
  const limit = Math.max(0, input.limit ?? 5);
205
150
  if (limit === 0)
206
151
  return [];
207
152
  return candidates
208
- .map((candidate) => ({ candidate, score: recallScore(candidate, input.query) }))
209
- .filter((item) => normalize(input.query) === "" || item.score > 0)
153
+ .filter(isRecallablePending)
154
+ .map((candidate) => ({ candidate, score: pendingRecallScore(candidate, input.query) }))
155
+ .filter((item) => item.score > 0)
210
156
  .sort((left, right) => right.score - left.score
211
157
  || priorityRank(left.candidate.candidate.priority) - priorityRank(right.candidate.candidate.priority)
212
158
  || right.candidate.createdAt.localeCompare(left.candidate.createdAt)
@@ -218,25 +164,29 @@ function truncateCodePoints(value, maxCodePoints) {
218
164
  return Array.from(value).slice(0, Math.max(0, maxCodePoints)).join("");
219
165
  }
220
166
  export function renderPendingMemoryContext(candidates, maxCodePoints = 2_000) {
221
- if (candidates.length === 0 || maxCodePoints <= 0)
167
+ const recallable = candidates.filter(isRecallablePending);
168
+ if (recallable.length === 0 || maxCodePoints <= 0)
222
169
  return "";
223
- const header = `\n\n## Archived memory candidates (daemon-managed)\n这些记忆已持久化,但尚未进入常驻热索引;只在当前请求相关时参考,必要时再由 memory-prune 提升。\n`;
170
+ const header = `\n\n## Archived memory candidates (daemon-managed)\n容量暂存的新增资料,仅为部分预览,不代表完整检索;可参考但不能覆盖现行规则,未包含内容请按需查询。\n`;
224
171
  let remaining = maxCodePoints - Array.from(header).length;
225
172
  if (remaining <= 0)
226
173
  return truncateCodePoints(header, maxCodePoints);
227
174
  const sections = [];
228
- for (const item of candidates) {
175
+ for (const item of recallable) {
229
176
  const change = item.candidate;
230
177
  const prefix = `\n### ${change.title}\n`
231
- + `- pending_file: .crew/memory/pending/${item.pendingId}.json\n`
178
+ + `- pending_id: ${item.pendingId}\n`
232
179
  + `- summary: ${change.summary}\n`
233
180
  + `- triggers: ${change.triggers.join(", ")}\n`
234
181
  + "- detail: ";
235
182
  const prefixLength = Array.from(prefix).length;
236
183
  if (prefixLength + 1 > remaining)
237
184
  break;
238
- const detail = truncateCodePoints(change.detailBody, remaining - prefixLength - 1);
239
- const section = `${prefix}${detail}\n`;
185
+ const notice = " [正文未完,请用 read-pending 分页读取]";
186
+ const available = remaining - prefixLength - 1;
187
+ const truncated = Array.from(change.detailBody).length > available;
188
+ const detail = truncateCodePoints(change.detailBody, truncated ? Math.max(0, available - Array.from(notice).length) : available);
189
+ const section = `${prefix}${detail}${truncated ? truncateCodePoints(notice, available - Array.from(detail).length) : ""}\n`;
240
190
  const length = Array.from(section).length;
241
191
  sections.push(section);
242
192
  remaining -= length;
@@ -1,6 +1,8 @@
1
1
  import { dslog } from "../slog.js";
2
2
  import { renderPendingMemoryContext, searchPendingMemoryCandidates } from "./pending.js";
3
- import { renderLocalMemoryResultInstructions } from "./runtime.js";
3
+ import { renderLocalMemoryResultInstructions, renderLocalMemoryV2WriteGuard } from "./runtime.js";
4
+ import { LOCAL_MEMORY_V2_DEFERRED_GUIDANCE } from "./rollout.js";
5
+ const PENDING_READ_GUIDANCE = "\n\nV2新增资料预览未命中不代表没有资料;先查有效索引,必要时用 crew-memory search-pending --query '关键词',按 nextCursor 续查;用 read-pending --id ID 分页读正文。容量暂存资料不能覆盖现行规则,未应用修改不作为记忆。用 crew-memory --help 查看参数。\n";
4
6
  export async function prepareLocalMemoryPrompt(input) {
5
7
  let pending = "";
6
8
  if (input.enabled) {
@@ -20,18 +22,24 @@ export async function prepareLocalMemoryPrompt(input) {
20
22
  }
21
23
  }
22
24
  const original = input.renderBase(0);
25
+ const guard = input.deferred ? LOCAL_MEMORY_V2_DEFERRED_GUIDANCE
26
+ : input.strictRollout && input.enabled && input.resultPath === null ? renderLocalMemoryV2WriteGuard() : "";
23
27
  const structured = input.resultPath === null ? "" : renderLocalMemoryResultInstructions(input.resultPath);
24
- const fits = (prompt) => input.maxBytes === undefined || Buffer.byteLength(prompt, "utf8") <= input.maxBytes;
25
- const render = (suffix) => (suffix === "" ? original : input.renderBase(Buffer.byteLength(suffix, "utf8"))) + suffix;
26
- // 后缀先占预算,可选预览重建;结构化契约优先于 pending,不能被预览挤掉。
27
- const complete = render(pending + structured);
28
- if (fits(complete))
29
- return { systemPrompt: complete, resultPath: input.resultPath };
30
- if (pending !== "") {
31
- const withoutPending = render(structured);
32
- if (fits(withoutPending))
33
- return { systemPrompt: withoutPending, resultPath: input.resultPath };
28
+ const guidance = input.enabled ? PENDING_READ_GUIDANCE : "";
29
+ // 短命令指引放在当前轮,独立于可舍弃预览;收尾契约仍保持系统与当前轮两份。
30
+ for (const choice of [
31
+ { suffix: pending + guard + structured, wake: guard + structured + guidance },
32
+ { suffix: guard + structured, wake: guard + structured + guidance },
33
+ { suffix: guard + structured, wake: guard + structured },
34
+ ]) {
35
+ const reserved = Buffer.byteLength(choice.suffix + choice.wake, "utf8");
36
+ const systemPrompt = (reserved === 0 ? original : input.renderBase(reserved)) + choice.suffix;
37
+ if (input.maxBytes === undefined || Buffer.byteLength(systemPrompt + choice.wake, "utf8") <= input.maxBytes) {
38
+ return { systemPrompt, resultPath: input.resultPath, wakePromptSuffix: choice.wake };
39
+ }
34
40
  }
41
+ if (input.strictRollout)
42
+ throw new Error("local_memory_v2_prompt_over_budget");
35
43
  // 强制基础指令与契约本身仍无法共存时,保留既有非结构化兼容回退,不裁基础指令。
36
- return { systemPrompt: original, resultPath: null };
44
+ return { systemPrompt: original, resultPath: null, wakePromptSuffix: "" };
37
45
  }
@@ -5,8 +5,13 @@ import { isAbsolute, relative, resolve, sep } from "node:path";
5
5
  import { pathToFileURL } from "node:url";
6
6
  import { parseArgs, TextDecoder } from "node:util";
7
7
  import { z } from "zod";
8
+ import { readPendingMemoryBody, searchPendingMemoryPage } from "./pending-query.js";
8
9
  const OUTPUT_BYTES = 12 * 1024;
9
10
  const FILE_BYTES = 32 * 1024 * 1024;
11
+ const READ_LIMIT_MAX = 4_000;
12
+ const READ_LIMIT_SAFE = 2_000;
13
+ const SEARCH_LIMIT_MAX = 40;
14
+ const SEARCH_LIMIT_SAFE = 10;
10
15
  const FileSchema = z.string().min(1).refine((file) => {
11
16
  if (isAbsolute(file) || /[\\\u0000-\u001f\u007f]/u.test(file))
12
17
  return false;
@@ -25,22 +30,43 @@ const CommandSchema = z.discriminatedUnion("command", [
25
30
  z.object({
26
31
  command: z.literal("search"), ...Common,
27
32
  query: z.string().trim().min(1).max(256),
28
- limit: z.coerce.number().int().min(1).max(40).default(10),
33
+ limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(10),
29
34
  afterLine: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
30
35
  }).strict(),
31
36
  z.object({
32
37
  command: z.literal("read"), ...Common,
33
38
  offset: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
34
- limit: z.coerce.number().int().min(1).max(4000).default(2000),
39
+ limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(2000),
40
+ }).strict(),
41
+ z.object({
42
+ command: z.literal("search-pending"),
43
+ query: z.string().trim().min(1).max(256),
44
+ cursor: z.string().min(1).max(4096).optional(),
45
+ limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(10),
46
+ }).strict(),
47
+ z.object({
48
+ command: z.literal("read-pending"),
49
+ id: z.string().min(1).max(250).regex(/^[A-Za-z0-9][A-Za-z0-9._-]*$/u),
50
+ ifHash: Common.ifHash,
51
+ offset: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
52
+ limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(2000),
35
53
  }).strict(),
36
54
  ]);
37
55
  const USAGE = `只读记忆工具,默认输出 JSON,不修改任何记忆文件。
38
56
  crew-memory search --query <关键词> [--file MEMORY.md|notes/文件.md] [--limit 10] [--after-line 0]
39
57
  crew-memory read [--file MEMORY.md|notes/文件.md] [--offset 0] [--limit 2000]
40
- 搜索关键词按空白分隔,同一行须全部命中;返回完整文件中的行号和字符偏移。
58
+ crew-memory search-pending --query <关键词> [--cursor <游标>] [--limit 10]
59
+ crew-memory read-pending --id <候选ID> [--offset 0] [--limit 2000] [--if-hash <sha256>]
60
+ read/read-pending 的有效页长为 1—4000,默认 2000;search/search-pending 的有效页长为 1—40,默认 10。超出上限时工具自动使用安全页长并返回 warning(recovered=true),不把它当作读取失败。
61
+ search 关键词按空白分隔,同一行须全部命中;返回完整文件中的行号和字符偏移。
41
62
  读取的 offset/limit 按 Unicode 字符计数;用 nextOffset 继续,eof=true 表示结束。
63
+ 读取的 --limit 超出范围时,工具会自动降到 2000 并在结果中标记 warning;保留原文件或候选 ID 及 offset,按 nextOffset 分页,不要一次读完整文件。
42
64
  后续页可传 --if-hash <前一页的 sha256>,文件变化时停止,避免混读不同版本。
43
- 搜索结果是原始资料定位,不代表候选已生效;按当前有效索引读取明细,不扫描 pending。`;
65
+ search/read 读取 MEMORY.md 和 notes 原文,不过滤失效段落,也不扫描 pending。
66
+ search-pending 只查因容量延期的 create 候选;有 nextCursor 就继续,即使本页 matches 为空。
67
+ complete=true 才表示全部候选已检索完成且无遗漏;warnings 或非零退出码表示读取未完成,不能当作零命中。
68
+ read-pending 只返回候选正文;offset 非零时必须传 --if-hash,update/supersede 候选不可读取。
69
+ 候选只供相关问题参考,未生效,不能覆盖有效规则;先读取当前有效索引与明细。`;
44
70
  function inside(root, path) {
45
71
  const value = relative(root, path);
46
72
  return value !== ".." && !value.startsWith(`..${sep}`) && !isAbsolute(value);
@@ -109,7 +135,15 @@ function originalLowercaseIndex(text, index) {
109
135
  }
110
136
  return text.length;
111
137
  }
112
- function searchSource(text, sha256, command) {
138
+ function normalizeLimit(requestedLimit, max, safe) {
139
+ if (requestedLimit <= max)
140
+ return { limit: requestedLimit };
141
+ return {
142
+ limit: safe,
143
+ warning: { code: "memory_limit_clamped", requestedLimit, appliedLimit: safe, recovered: true },
144
+ };
145
+ }
146
+ function searchSource(text, sha256, command, warning) {
113
147
  const terms = command.query.toLowerCase().split(/\s+/u);
114
148
  const matches = [];
115
149
  let line = 0;
@@ -150,10 +184,22 @@ function searchSource(text, sha256, command) {
150
184
  return {
151
185
  file: command.file, sha256, matches, matchCount, hasMore,
152
186
  nextAfterLine: hasMore ? matches.at(-1)?.line ?? null : null,
187
+ ...(warning === undefined ? {} : { warning, requestedLimit: warning.requestedLimit, limit: warning.appliedLimit }),
153
188
  };
154
189
  }
155
- function readPage(text, sha256, command) {
156
- const metadata = { file: command.file, sha256, offset: command.offset, nextOffset: Number.MAX_SAFE_INTEGER, eof: false, content: "" };
190
+ function readPage(text, source, command, warning) {
191
+ const metadata = {
192
+ ...source,
193
+ ...(warning === undefined ? {} : {
194
+ warning,
195
+ requestedLimit: warning.requestedLimit,
196
+ limit: warning.appliedLimit,
197
+ }),
198
+ offset: command.offset,
199
+ nextOffset: Number.MAX_SAFE_INTEGER,
200
+ eof: false,
201
+ content: "",
202
+ };
157
203
  let remaining = OUTPUT_BYTES - Buffer.byteLength(JSON.stringify(metadata), "utf8");
158
204
  let position = 0;
159
205
  let offset = 0;
@@ -175,8 +221,16 @@ function readPage(text, sha256, command) {
175
221
  count += 1;
176
222
  }
177
223
  const eof = position === text.length;
224
+ if (!eof && count === 0)
225
+ throw new Error("memory_output_too_large");
178
226
  return { ...metadata, content, eof, nextOffset: eof ? null : offset };
179
227
  }
228
+ function writeResult(result, io) {
229
+ const output = JSON.stringify(result);
230
+ if (Buffer.byteLength(output, "utf8") > OUTPUT_BYTES)
231
+ throw new Error("memory_output_too_large");
232
+ io.out(output);
233
+ }
180
234
  export async function runMemoryReadCommand(argv, io) {
181
235
  let command;
182
236
  try {
@@ -186,6 +240,7 @@ export async function runMemoryReadCommand(argv, io) {
186
240
  query: { type: "string", short: "q" }, file: { type: "string" },
187
241
  limit: { type: "string" }, offset: { type: "string" },
188
242
  "after-line": { type: "string" }, "if-hash": { type: "string" },
243
+ id: { type: "string" }, cursor: { type: "string" },
189
244
  json: { type: "boolean" }, help: { type: "boolean", short: "h" },
190
245
  },
191
246
  });
@@ -201,24 +256,54 @@ export async function runMemoryReadCommand(argv, io) {
201
256
  ...(afterLine === undefined ? {} : { afterLine }),
202
257
  ...(ifHash === undefined ? {} : { ifHash }),
203
258
  });
259
+ if (command.command === "read-pending" && command.offset > 0 && command.ifHash === undefined) {
260
+ throw new Error("memory_hash_required");
261
+ }
204
262
  }
205
263
  catch {
206
- io.err(`memory_arguments_invalid: 参数不合法。\n${USAGE}`);
264
+ io.err(`memory_arguments_invalid: 参数不合法,记忆读取未完成;请按以下参数范围修正后重试,不要当作没有记忆。\n${USAGE}`);
207
265
  return 2;
208
266
  }
209
267
  try {
268
+ if (!io.env.CREW_HOME)
269
+ throw new Error("memory_home_missing");
270
+ if (command.command === "search-pending") {
271
+ const normalized = normalizeLimit(command.limit, SEARCH_LIMIT_MAX, SEARCH_LIMIT_SAFE);
272
+ const result = await searchPendingMemoryPage({ agentDir: io.env.CREW_HOME, query: command.query,
273
+ ...(command.cursor === undefined ? {} : { cursor: command.cursor }), limit: normalized.limit });
274
+ writeResult({ ...result, ...(normalized.warning === undefined ? {} : {
275
+ warning: normalized.warning, requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit,
276
+ }) }, io);
277
+ if (result.warnings.length > 0 || (!result.hasMore && !result.complete)) {
278
+ throw new Error("memory_pending_search_incomplete");
279
+ }
280
+ return 0;
281
+ }
282
+ if (command.command === "read-pending") {
283
+ const normalized = normalizeLimit(command.limit, READ_LIMIT_MAX, READ_LIMIT_SAFE);
284
+ const { content, ...metadata } = await readPendingMemoryBody({ agentDir: io.env.CREW_HOME,
285
+ pendingId: command.id, ...(command.ifHash === undefined ? {} : { ifHash: command.ifHash }) });
286
+ writeResult(readPage(content, metadata, { ...command, limit: normalized.limit }, normalized.warning), io);
287
+ return 0;
288
+ }
210
289
  const source = await readSource(io.env.CREW_HOME, command.file);
211
290
  if (command.ifHash !== undefined && command.ifHash !== source.sha256)
212
291
  throw new Error("memory_file_changed");
213
292
  const result = command.command === "search"
214
- ? searchSource(source.text, source.sha256, command)
215
- : readPage(source.text, source.sha256, command);
216
- io.out(JSON.stringify(result));
293
+ ? (() => {
294
+ const normalized = normalizeLimit(command.limit, SEARCH_LIMIT_MAX, SEARCH_LIMIT_SAFE);
295
+ return searchSource(source.text, source.sha256, { ...command, limit: normalized.limit }, normalized.warning);
296
+ })()
297
+ : (() => {
298
+ const normalized = normalizeLimit(command.limit, READ_LIMIT_MAX, READ_LIMIT_SAFE);
299
+ return readPage(source.text, { file: command.file, sha256: source.sha256 }, { ...command, limit: normalized.limit }, normalized.warning);
300
+ })();
301
+ writeResult(result, io);
217
302
  return 0;
218
303
  }
219
304
  catch (error) {
220
305
  const code = error.code;
221
- const known = error instanceof Error && /^memory_[a-z_]+$/u.test(error.message) ? error.message : code ?? "memory_read_failed";
306
+ const known = error instanceof Error && /^(?:local_)?memory_[a-z_]+$/u.test(error.message) ? error.message : code ?? "memory_read_failed";
222
307
  io.err(JSON.stringify({ error: known, message: "记忆读取未完成;检查路径、文件版本或大小,不要将此错误当作没有记忆。" }));
223
308
  return 1;
224
309
  }
@@ -0,0 +1,82 @@
1
+ import { lstat, readdir, realpath } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import { localMemoryV2Environment } from "../config.js";
4
+ import { memorySeed } from "../workspace.js";
5
+ import { LOCAL_MEMORY_INDEX_CODE_POINT_CAP, LOCAL_MEMORY_INDEX_ENTRY_CAP, LOCAL_MEMORY_TOTAL_CODE_POINT_CAP } from "./index-budget.js";
6
+ import { inventoryLimits, readMemoryInventorySource } from "./inventory-source.js";
7
+ import { previewMemoryInventory } from "./inventory-preview.js";
8
+ import { parseLocalMemoryIndex } from "./write.js";
9
+ import { dslog } from "../slog.js";
10
+ export const LOCAL_MEMORY_V2_DEFERRED_GUIDANCE = `
11
+
12
+ ## 本轮本地记忆兼容状态
13
+ 开发环境已开放 V2,但该 Agent 的旧记忆尚未通过格式兼容检查,本轮未启用结构化 V2。
14
+ 本节覆盖前文和旧会话中直接改写共享记忆的要求:本轮 MEMORY.md 与 notes 只读,不得改写、迁移或清空。
15
+ 用户要求新增或更新长期规则时,内容和授权均只记录到本任务的 $CREW_TASK_LOG;保留日志,不要清空,不要宣称长期记忆已生效。旧资料兼容处理完成前,记忆收尾会明确报告需要处理。`;
16
+ export function reportLocalMemoryV2Deferred(executionId, handle, memoryPrune) {
17
+ dslog("local_memory.v2_deferred", "旧记忆尚未通过兼容检查,保留原资料和待处理日志", {
18
+ execution_id: executionId, agent_handle: handle, reason: "requires_migration"
19
+ });
20
+ if (memoryPrune)
21
+ throw new Error("local_memory_v2_requires_migration");
22
+ }
23
+ /** 只核对小型索引和其引用;不扫描全部历史任务、不迁移或生成新存储。 */
24
+ export async function resolveLocalMemoryV2Rollout(input) {
25
+ const environment = localMemoryV2Environment(input.serverUrl);
26
+ if (input.selfTraining || environment === "production")
27
+ return { enabled: false, deferred: false };
28
+ if (environment === "custom")
29
+ return { enabled: input.requested, deferred: false };
30
+ const deferred = { enabled: false, deferred: true };
31
+ const { workspace } = input;
32
+ if (Array.from(workspace.memory).length > LOCAL_MEMORY_TOTAL_CODE_POINT_CAP)
33
+ return deferred;
34
+ try {
35
+ const root = await realpath(workspace.dir);
36
+ const notesStat = await lstat(join(root, "notes"));
37
+ if (!notesStat.isDirectory() || notesStat.isSymbolicLink())
38
+ return deferred;
39
+ const memory = await readMemoryInventorySource(root, "MEMORY.md", inventoryLimits.fileBytes);
40
+ if (memory.content !== workspace.memory)
41
+ return deferred;
42
+ // 完整种子且无旧明细才可按空记忆接入;marker 不能替代这项核对。
43
+ const seededRole = /^## Role\n([\s\S]*?)\n\n## Core Goals\n/mu.exec(memory.content)?.[1];
44
+ if (seededRole !== undefined && memory.content === memorySeed(input.handle, seededRole)) {
45
+ return (await readdir(join(root, "notes"))).length === 0 ? { enabled: true, deferred: false } : deferred;
46
+ }
47
+ const generated = /<!-- NOWWORK:MEMORY_INDEX:BEGIN schema=1 -->[\s\S]*?<!-- NOWWORK:MEMORY_INDEX:END -->/u.exec(memory.content)?.[0];
48
+ if (!generated || Array.from(generated).length > LOCAL_MEMORY_INDEX_CODE_POINT_CAP)
49
+ return deferred;
50
+ const entries = parseLocalMemoryIndex(memory.content);
51
+ if (entries.length > LOCAL_MEMORY_INDEX_ENTRY_CAP)
52
+ return deferred;
53
+ const sources = [memory];
54
+ let remaining = inventoryLimits.totalBytes - memory.bytes;
55
+ for (const file of new Set(entries.map(entry => entry.file))) {
56
+ if (!/^notes\/(preferences|project-rules|testing|debugging|git|incidents|lessons)\.md$/u.test(file))
57
+ return deferred;
58
+ const source = await readMemoryInventorySource(root, file, Math.min(inventoryLimits.fileBytes, remaining));
59
+ sources.push(source);
60
+ remaining -= source.bytes;
61
+ }
62
+ const preview = previewMemoryInventory(sources, true, "existing_v2");
63
+ // 此处只放行现有V2;旧格式和未标记混合卡片都留待显式兼容处理。
64
+ if (preview.entries.some(entry => entry.format !== "managed")
65
+ || preview.issues.some(issue => !["managed_anchor_invalid", "projection_over_budget"].includes(issue.code)))
66
+ return deferred;
67
+ // 现有writer只改状态时不会重写正文块的revision;兼容门槛不能因此拒绝合法置顶。
68
+ for (const entry of entries) {
69
+ if (!/^[A-Za-z0-9_-]+$/u.test(entry.id))
70
+ return deferred;
71
+ const detail = sources.find(source => source.path === entry.file)?.content ?? "";
72
+ const begins = [...detail.matchAll(new RegExp(`<!-- NOWWORK:MEMORY_ENTRY:BEGIN id=${entry.id} revision=[1-9][0-9]* -->`, "gu"))];
73
+ const end = `<!-- NOWWORK:MEMORY_ENTRY:END id=${entry.id} -->`;
74
+ if (begins.length !== 1 || detail.split(end).length !== 2 || detail.indexOf(end) < begins[0].index)
75
+ return deferred;
76
+ }
77
+ return { enabled: true, deferred: false };
78
+ }
79
+ catch {
80
+ return deferred;
81
+ }
82
+ }
@@ -1,8 +1,9 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { constants } from "node:fs";
3
- import { lstat, mkdir, open, readFile, realpath, rm, writeFile } from "node:fs/promises";
4
- import { isAbsolute, join, relative, resolve, sep } from "node:path";
3
+ import { lstat, mkdir, open, readFile, rm, writeFile } from "node:fs/promises";
4
+ import { join } from "node:path";
5
5
  import { applyLocalMemoryResult, parseLocalMemoryResult } from "./write.js";
6
+ import { verifyWorkLogSource } from "./work-log.js";
6
7
  export function isLocalMemoryV2Eligible(input) {
7
8
  // The allowlist is the rollout boundary. The marker and seed/pre-existing
8
9
  // state are retained as diagnostics and for backward-compatible marker
@@ -51,6 +52,7 @@ export function renderLocalMemoryResultInstructions(resultPath) {
51
52
  return `
52
53
 
53
54
  ## Structured memory prune (daemon-managed)
55
+ 本节是本次执行唯一的记忆写入与清理协议,覆盖历史会话及本条消息前文中直接修改共享文件、清空工作日志或仅回复“已修正”的要求。任务目标与用户授权范围不变;只使用下面本轮提供的结果路径,不复用历史路径。
54
56
  本轮长期记忆收尾只提交候选,不要直接编辑 $CREW_HOME/MEMORY.md 或 $CREW_HOME/notes/。
55
57
  将一个合法 JSON 对象写入以下 daemon 提供的文件,写入完成后再结束本轮:
56
58
  ${resultPath}
@@ -68,8 +70,8 @@ outcome 只允许 no_change、proposed、conflict、failed。新增、更新、
68
70
  \`\`\`
69
71
 
70
72
  每个 changes 项须保留示例中的全部字段。operation=create|update|supersede;kind=procedural|semantic|episodic|preference;priority=high|medium|low;detailCategory=preferences|project-rules|testing|debugging|git|incidents|lessons。create 的 memoryId/baseRevision/supersedesMemoryId 必须为 null;update 按现有索引填写 memoryId/baseRevision,supersedesMemoryId 为 null;supersede 填 supersedesMemoryId/baseRevision,memoryId 为 null。
71
- 只提交可复用、与本次任务明确相关的少量候选;不要写入凭证、token、隐私或内部控制标记。daemon 会校验、去重、限制 MEMORY.md 大小,超出索引容量的候选会保存在 pending 归档中;后续相关任务会按需召回,索引腾出空间后可自动提升。
72
- 写入候选文件不等于已经记住,只有 daemon 校验并提交成功后记忆才生效。本结构化轮次覆盖前文提前清理工作日志的要求:保留 $CREW_TASK_LOG,不要清空或删除;daemon 将在提交成功后清理,失败时保留日志和候选用于排查或重试。
73
+ 只提交可复用、与本次任务明确相关的少量候选;不要写入凭证、token、隐私或内部控制标记。daemon 会校验、去重、限制 MEMORY.md 大小,超出索引容量的候选会保存在 pending 归档中。容量暂存的 create 可按需检索参考但不能覆盖现行规则;有空间的后续 proposed 提交可提升。未应用的 update/supersede 不进入普通召回,必须重新核对目标版本后处理。
74
+ 写入候选文件只表示提交候选,不能提前宣称已经记住或规则已经更新;daemon 在本轮结束后才处理,候选持久化与进入有效索引也不是同一结果。本结构化轮次覆盖前文提前清理工作日志的要求:保留 $CREW_TASK_LOG,不要清空或删除;daemon 将在处理成功后清理,失败时保留日志和候选用于排查或重试。
73
75
  `;
74
76
  }
75
77
  /** Prevent ordinary V2 turns from racing the daemon-managed memory writer. */
@@ -79,6 +81,11 @@ export function renderLocalMemoryV2WriteGuard() {
79
81
  ## V2 shared memory write safety
80
82
  本节覆盖前文关于普通任务直接更新共享记忆的例外规则。本 Agent 已启用结构化本地记忆 V2。普通任务不得直接编辑共享的 $CREW_HOME/MEMORY.md 或 $CREW_HOME/notes/ 文件;即使用户明确要求记住内容,也只把候选、用户授权和分类理由记录到本任务的 $CREW_TASK_LOG,留给任务结束后的 memory-prune 结构化收尾应用。只有 memory-prune 收尾提示明确要求写入 daemon 提供的 result 文件时,才按该协议提交候选。`;
81
83
  }
84
+ function workLogFailure(error) {
85
+ const reason = error instanceof Error ? error.message : "";
86
+ return { status: "failed", error: reason === "local_memory_work_log_source_changed"
87
+ || reason === "local_memory_work_log_version_missing" ? reason : "local_memory_work_log_cleanup_failed" };
88
+ }
82
89
  export async function commitLocalMemoryResultAfterExit(input) {
83
90
  if (input.exitCode !== 0)
84
91
  return { status: "skipped" };
@@ -96,6 +103,14 @@ export async function commitLocalMemoryResultAfterExit(input) {
96
103
  if (result.outcome === "conflict" || result.outcome === "failed") {
97
104
  return { status: "failed", error: `local_memory_result_${result.outcome}` };
98
105
  }
106
+ if (input.workLogPath !== undefined) {
107
+ try {
108
+ await verifyWorkLogSource(input.agentDir, input.workLogPath, input.expectedWorkLogSha256);
109
+ }
110
+ catch (error) {
111
+ return workLogFailure(error);
112
+ }
113
+ }
99
114
  await applyLocalMemoryResult({
100
115
  agentDir: input.agentDir,
101
116
  result,
@@ -108,20 +123,12 @@ export async function commitLocalMemoryResultAfterExit(input) {
108
123
  }
109
124
  if (input.workLogPath !== undefined) {
110
125
  try {
111
- const relativeLogPath = relative(resolve(input.agentDir), resolve(input.workLogPath));
112
- if (relativeLogPath === "" || relativeLogPath === ".." || relativeLogPath.startsWith(`..${sep}`)
113
- || isAbsolute(relativeLogPath))
114
- throw new Error("local_memory_work_log_path_invalid");
115
- const expectedLogPath = join(await realpath(input.agentDir), relativeLogPath);
116
- const before = await lstat(input.workLogPath);
117
- if (!before.isFile() || before.isSymbolicLink())
118
- throw new Error("local_memory_work_log_path_invalid");
119
- if (await realpath(input.workLogPath) !== expectedLogPath)
120
- throw new Error("local_memory_work_log_path_invalid");
126
+ const before = await verifyWorkLogSource(input.agentDir, input.workLogPath, input.expectedWorkLogSha256);
121
127
  const handle = await open(input.workLogPath, constants.O_WRONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
122
128
  try {
123
129
  const opened = await handle.stat();
124
- if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino) {
130
+ if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino
131
+ || opened.size !== before.size || opened.mtimeMs !== before.mtimeMs || opened.ctimeMs !== before.ctimeMs) {
125
132
  throw new Error("local_memory_work_log_path_changed");
126
133
  }
127
134
  await handle.truncate(0);
@@ -130,8 +137,8 @@ export async function commitLocalMemoryResultAfterExit(input) {
130
137
  await handle.close();
131
138
  }
132
139
  }
133
- catch {
134
- return { status: "failed", error: "local_memory_work_log_cleanup_failed" };
140
+ catch (error) {
141
+ return workLogFailure(error);
135
142
  }
136
143
  }
137
144
  try {
@@ -0,0 +1,31 @@
1
+ import { resolve } from "node:path";
2
+ import { withKeyedLease } from "../local-execution-lease.js";
3
+ // 只记录尚未结束的本机执行,最后一个执行结束即移除,不落盘。
4
+ const activeExecutions = new Map();
5
+ export function workLogExecutionKey(agentsRoot, handle, taskKey) {
6
+ return `work-log:${JSON.stringify([resolve(agentsRoot, handle), taskKey ?? "default"])}`;
7
+ }
8
+ export async function withWorkLogExecution(key, operation, cancellation) {
9
+ // 只在登记期间持锁;普通任务与收尾的模型调用仍可并行。
10
+ await withKeyedLease(key, async () => {
11
+ activeExecutions.set(key, (activeExecutions.get(key) ?? 0) + 1);
12
+ }, cancellation);
13
+ try {
14
+ return await operation();
15
+ }
16
+ finally {
17
+ const remaining = (activeExecutions.get(key) ?? 1) - 1;
18
+ if (remaining === 0)
19
+ activeExecutions.delete(key);
20
+ else
21
+ activeExecutions.set(key, remaining);
22
+ }
23
+ }
24
+ export async function withWorkLogCommit(key, operation, cancellation) {
25
+ return withKeyedLease(key, async () => {
26
+ // 已有同任务执行可能继续写日志,不能提交旧提案;新执行等短提交结束后再登记。
27
+ if (activeExecutions.get(key) !== 1)
28
+ throw new Error("local_memory_work_log_execution_conflict");
29
+ return operation();
30
+ }, cancellation);
31
+ }