@nowcrew/daemon 0.6.87 → 0.6.88-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.
package/README.md CHANGED
@@ -418,6 +418,28 @@ Local policy can reduce server-requested access and limits; it cannot grant more
418
418
  Provider credentials and configured environment are prepared locally and never carried in execution
419
419
  control frames.
420
420
 
421
+ ## 完整本地记忆读取
422
+
423
+ 运行时注入的 `MEMORY.md` 是有界预览,未命中不代表完整文件没有相关记忆。
424
+ 普通 Agent 工作区会在现有 PATH 目录中提供只读 `crew-memory` 工具,无需 Server 凭证:
425
+
426
+ ```bash
427
+ crew-memory search --query '提交前检查'
428
+ crew-memory search --query '提交前检查' --after-line 120
429
+ crew-memory read --offset 2400 --limit 2000
430
+ crew-memory read --file notes/testing.md --offset 0 --limit 2000
431
+ ```
432
+
433
+ 命令默认返回 JSON。搜索遍历完整指定文件,返回 `line`、`offset`、命中摘要和
434
+ `nextAfterLine`;读取按 Unicode 字符偏移分页,使用 `nextOffset` 继续直到 `eof=true`。
435
+ 后续页可传 `--if-hash <上一页的sha256>`,文件变化时明确停止,不混读不同版本。
436
+ 每页 JSON 有 12 KiB 上限,输入文件超过 32 MiB 时明确报错,不将未完成搜索伪装为零命中。
437
+
438
+ 工具仅支持 `MEMORY.md` 和 `notes/` 下明确指定的 Markdown 文件,不遍历 pending,
439
+ 不写入或迁移记忆。搜索结果是原始资料定位,读取明细前仍需确认当前有效索引和正文片段。
440
+ 这些修改不改变现有 V2 写入上限、候选状态或归档策略,也不提供自动语义召回保证。
441
+ 工具随 daemon 包发布;旧 daemon 没有该入口时,Agent 使用原生文件搜索与分段读取回退。
442
+
421
443
  ## Optional TencentDB Agent Memory Bridge
422
444
 
423
445
  Execution protocol v1 can opt NowWork Agents into the private TencentDB Agent Memory Panel.
@@ -13,22 +13,17 @@ const fileFingerprint = async (path) => {
13
13
  const stat = await lstat(path);
14
14
  return `${stat.size}:${stat.mtimeMs}`;
15
15
  };
16
- function isReportFile(root, path) {
16
+ function isReportFile(root, path, kind) {
17
17
  const parts = relative(root, path).split(sep).filter(Boolean);
18
18
  const filename = parts.at(-1) ?? "";
19
- if (parts[0] === "tasks" && parts.length >= 3) {
20
- return REPORT_FILENAMES.has(filename) || extname(filename).toLowerCase() === ".html";
21
- }
22
- if (parts[0] !== "qa-report-server-v2")
23
- return false;
24
- if (parts[1] === "static" && parts.length === 3)
25
- return extname(filename).toLowerCase() === ".html";
26
- if (parts[1] === "qa-results" && parts.length >= 4) {
19
+ if (kind === "static")
20
+ return parts.length === 1 && extname(filename).toLowerCase() === ".html";
21
+ if (parts.length >= 2) {
27
22
  return REPORT_FILENAMES.has(filename) || extname(filename).toLowerCase() === ".html";
28
23
  }
29
24
  return false;
30
25
  }
31
- async function walkReports(root, directory, depth, output) {
26
+ async function walkReports(root, directory, depth, output, kind) {
32
27
  if (depth > MAX_DEPTH || output.length >= MAX_FILES)
33
28
  return;
34
29
  const entries = await readdir(directory, { withFileTypes: true }).catch(() => []);
@@ -39,28 +34,26 @@ async function walkReports(root, directory, depth, output) {
39
34
  continue;
40
35
  const path = join(directory, entry.name);
41
36
  if (entry.isDirectory()) {
42
- await walkReports(root, path, depth + 1, output);
37
+ await walkReports(root, path, depth + 1, output, kind);
43
38
  }
44
- else if (entry.isFile() && isReportFile(root, path)) {
39
+ else if (entry.isFile() && isReportFile(root, path, kind)) {
45
40
  output.push(resolve(path));
46
41
  }
47
42
  }
48
43
  }
49
- export async function snapshotReportFiles(root) {
44
+ export async function snapshotReportFiles(root, qaReportRoot) {
50
45
  const files = [];
51
46
  const workspaceRoot = resolve(root);
52
- for (const directory of [
53
- join(workspaceRoot, "tasks"),
54
- join(workspaceRoot, "qa-report-server-v2", "qa-results"),
55
- join(workspaceRoot, "qa-report-server-v2", "static"),
56
- ]) {
57
- await walkReports(workspaceRoot, directory, 0, files);
47
+ const hub = resolve(qaReportRoot ?? join(workspaceRoot, "qa-report-server-v2"));
48
+ for (const kind of ["tasks", "qa-results", "static"]) {
49
+ const directory = join(kind === "tasks" ? workspaceRoot : hub, kind);
50
+ await walkReports(directory, directory, 0, files, kind);
58
51
  }
59
52
  const entries = await Promise.all(files.map(async (path) => [path, await fileFingerprint(path)]));
60
53
  return new Map(entries);
61
54
  }
62
- export async function discoverReportFiles(root, before) {
63
- const after = await snapshotReportFiles(root);
55
+ export async function discoverReportFiles(root, before, qaReportRoot) {
56
+ const after = await snapshotReportFiles(root, qaReportRoot);
64
57
  return [...after.entries()]
65
58
  .filter(([path, fingerprint]) => before.get(path) !== fingerprint)
66
59
  .map(([path]) => path)
@@ -97,7 +90,7 @@ async function verifyOssUrl(url) {
97
90
  }
98
91
  }
99
92
  export async function uploadCloudReports(input) {
100
- const files = await discoverReportFiles(input.root, input.before);
93
+ const files = await discoverReportFiles(input.root, input.before, input.qaReportRoot);
101
94
  if (files.length === 0)
102
95
  return { urls: [], failures: [] };
103
96
  const run = input.execFile ?? (async (file, args) => {
@@ -131,10 +124,10 @@ export async function uploadCloudReports(input) {
131
124
  }
132
125
  return { urls, failures };
133
126
  }
134
- export async function prepareCloudReportSnapshot(facts, root) {
127
+ export async function prepareCloudReportSnapshot(facts, root, qaReportRoot) {
135
128
  if (!facts.enabled)
136
129
  return null;
137
- return snapshotReportFiles(root);
130
+ return snapshotReportFiles(root, qaReportRoot);
138
131
  }
139
132
  export async function prepareCloudReportExecution(input) {
140
133
  const facts = cloudReportUploadFacts(input.env);
@@ -147,7 +140,7 @@ export async function prepareCloudReportExecution(input) {
147
140
  return { facts, snapshot: null };
148
141
  }
149
142
  try {
150
- return { facts, snapshot: await prepareCloudReportSnapshot(facts, input.root) };
143
+ return { facts, snapshot: await prepareCloudReportSnapshot(facts, input.root, input.qaReportRoot) };
151
144
  }
152
145
  catch (error) {
153
146
  dslog("cloud_report_upload.snapshot_failed", "报告文件快照失败", {
@@ -177,6 +170,7 @@ export async function deliverCloudReports(input) {
177
170
  const result = await (input.upload ?? uploadCloudReports)({
178
171
  script: input.facts.ossUploadScript,
179
172
  root: input.root,
173
+ ...(input.qaReportRoot === undefined ? {} : { qaReportRoot: input.qaReportRoot }),
180
174
  before: input.before,
181
175
  });
182
176
  if (result.failures.length > 0) {
@@ -185,6 +179,14 @@ export async function deliverCloudReports(input) {
185
179
  uploaded_count: result.urls.length, failed_count: result.failures.length,
186
180
  });
187
181
  }
182
+ else {
183
+ dslog(result.urls.length === 0 ? "cloud_report_upload.no_files" : "cloud_report_upload.completed", result.urls.length === 0 ? "未发现新增或变化的报告文件" : "报告上传完成", {
184
+ execution_id: input.executionId, agent_handle: input.agentHandle,
185
+ uploaded_count: result.urls.length,
186
+ report_root: input.root,
187
+ qa_report_root: input.qaReportRoot ?? join(input.root, "qa-report-server-v2"),
188
+ });
189
+ }
188
190
  return {
189
191
  finalText: input.captureFinal && result.urls.length > 0
190
192
  ? appendReportLinks(input.finalText ?? "", result.urls)
@@ -834,9 +834,12 @@ export async function runExecution(config, input, dependencies) {
834
834
  ...(selfTraining ? { isolateLocalMemory: true } : {}),
835
835
  collaborationTools: config.codexCollaborationTools && spec.runtime.name === "codex",
836
836
  });
837
- const boundedSystemPrompt = (context) => appendAgentMemoryContext(typeof systemPromptWithLocalFacts === "string"
838
- ? systemPromptWithLocalFacts
839
- : systemPromptWithLocalFacts(context), recalledMemory, baseSystemPromptBudget);
837
+ const boundedSystemPrompt = (context) => {
838
+ const base = typeof systemPromptWithLocalFacts === "string"
839
+ ? systemPromptWithLocalFacts
840
+ : systemPromptWithLocalFacts(context);
841
+ return appendAgentMemoryContext(base, recalledMemory, Math.max(Buffer.byteLength(base, "utf8"), baseSystemPromptBudget - (context.reservedPromptBytes ?? 0)));
842
+ };
840
843
  const localInput = {
841
844
  executionId: spec.executionId,
842
845
  handle: spec.agent.handle,
@@ -34,45 +34,39 @@ export function truncateUtf8(value, maxBytes) {
34
34
  }
35
35
  return value.slice(0, end);
36
36
  }
37
- function takeCompletePromptSections(sections, maxBytes) {
38
- return sections.reduce((state, section) => {
39
- if (!state.full)
40
- return state;
41
- const sectionBytes = Buffer.byteLength(section, "utf8");
42
- if (state.bytes + sectionBytes > maxBytes)
43
- return { ...state, full: false };
44
- return { value: `${state.value}${section}`, bytes: state.bytes + sectionBytes, full: true };
45
- }, { value: "", bytes: 0, full: true }).value;
46
- }
47
37
  /** v1 server instructions opt in to daemon-owned paths and bounded local workspace context. */
48
38
  export function withLocalExecutionFacts(serverPrompt, maxBytes, options = {}) {
49
- return ({ workspace, resuming, projectContext, trainingProjectContext }) => {
50
- const memory = options.isolateLocalMemory ? "" : capMemoryForInject(workspace.memory);
39
+ return ({ workspace, resuming, projectContext, trainingProjectContext, reservedPromptBytes = 0 }) => {
51
40
  const workLog = resuming ? "" : capWorkLogForInject(workspace.workLog);
52
41
  const collaborationPrompt = options.collaborationTools === true ? buildCollaborationToolsPrompt() : "";
53
42
  const mandatoryWorkspacePrompt = `${buildRuntimeWorkspacePrompt(projectContext)}${buildTrainingGitProjectPrompt(trainingProjectContext)}`;
54
43
  const crewHome = options.isolateLocalMemory ? workspace.runDir : workspace.dir;
55
- const localFactSections = [`\n\n## Local execution facts
44
+ const localFacts = `\n\n## Local execution facts
56
45
  - $CREW_HOME: ${crewHome}
57
- - $CREW_TASK_LOG: ${workspace.workLogPath}`,
58
- ...(memory ? [`\n\n## Injected MEMORY.md (bounded local context)\n${memory}`] : []),
59
- ...(workLog ? [`\n\n## Injected work-log (bounded local context)\n${workLog}`] : []),
60
- ];
46
+ - $CREW_TASK_LOG: ${workspace.workLogPath}`;
61
47
  const serverPromptBytes = Buffer.byteLength(serverPrompt, "utf8");
62
48
  const collaborationBytes = Buffer.byteLength(collaborationPrompt, "utf8");
63
49
  const mandatoryBytes = Buffer.byteLength(mandatoryWorkspacePrompt, "utf8");
64
50
  const requiredBytes = serverPromptBytes + collaborationBytes + mandatoryBytes;
65
- // 必需预算与 projectContext 无关:协作段落永远参与;无协作时与旧实现逐字节等价。
51
+ // 先保留全部必需内容,再按实际剩余字节一次性构造预览。
66
52
  if (collaborationPrompt !== "" && requiredBytes > maxBytes) {
67
53
  throw new Error("collaboration_tools_prompt_budget_exceeded");
68
54
  }
69
- if (mandatoryWorkspacePrompt !== "") {
70
- if (requiredBytes > maxBytes)
71
- throw new Error("project_workspace_prompt_budget_exceeded");
72
- const optionalFacts = takeCompletePromptSections(localFactSections, maxBytes - requiredBytes);
73
- return `${serverPrompt}${collaborationPrompt}${optionalFacts}${mandatoryWorkspacePrompt}`;
55
+ if (mandatoryWorkspacePrompt !== "" && requiredBytes > maxBytes) {
56
+ throw new Error("project_workspace_prompt_budget_exceeded");
74
57
  }
75
- const remaining = maxBytes - requiredBytes;
76
- return `${serverPrompt}${collaborationPrompt}${truncateUtf8(localFactSections.join(""), remaining)}`;
58
+ const remaining = maxBytes - requiredBytes - Math.max(0, reservedPromptBytes);
59
+ const factsBytes = Buffer.byteLength(localFacts, "utf8");
60
+ if (factsBytes > remaining)
61
+ return `${serverPrompt}${collaborationPrompt}${mandatoryWorkspacePrompt}`;
62
+ const memoryHeading = "\n\n## Injected MEMORY.md (bounded local context)\n";
63
+ const memory = options.isolateLocalMemory ? "" : capMemoryForInject(workspace.memory, undefined, remaining - factsBytes - Buffer.byteLength(memoryHeading, "utf8"));
64
+ const memorySection = memory === "" ? "" : memoryHeading + memory;
65
+ const workLogSection = workLog === "" ? "" : `\n\n## Injected work-log (bounded local context)\n${workLog}`;
66
+ const workLogBudget = remaining - factsBytes - Buffer.byteLength(memorySection, "utf8");
67
+ const boundedWorkLog = mandatoryWorkspacePrompt === ""
68
+ ? truncateUtf8(workLogSection, workLogBudget)
69
+ : Buffer.byteLength(workLogSection, "utf8") <= workLogBudget ? workLogSection : "";
70
+ return `${serverPrompt}${collaborationPrompt}${localFacts}${memorySection}${boundedWorkLog}${mandatoryWorkspacePrompt}`;
77
71
  };
78
72
  }
@@ -36,8 +36,8 @@ import { CodexStartupStageParser } from "./codex-startup-stage.js";
36
36
  import { formatRuntimeStartupError } from "./runtime-startup-error.js";
37
37
  import { machinePackageManagerEnv, packageManagerEnvFacts } from "./package-manager-env.js";
38
38
  import { sanitizeEnvVars } from "./runtime-env.js";
39
- import { commitLocalMemoryResultAfterExit, initializeLocalMemoryV2ForWorkspace, isLocalMemoryV2Eligible, localMemoryResultPath as makeLocalMemoryResultPath, renderLocalMemoryResultInstructions, } from "./local-memory/runtime.js";
40
- import { renderPendingMemoryContext, searchPendingMemoryCandidates, } from "./local-memory/pending.js";
39
+ import { commitLocalMemoryResultAfterExit, initializeLocalMemoryV2ForWorkspace, isLocalMemoryV2Eligible, localMemoryResultPath as makeLocalMemoryResultPath, } from "./local-memory/runtime.js";
40
+ import { prepareLocalMemoryPrompt } from "./local-memory/prompt.js";
41
41
  export class RuntimeRunningCallbackTimeoutError extends Error {
42
42
  constructor(runtime, timeoutMs) {
43
43
  super(`${runtime} runtime-running callback timed out after ${timeoutMs}ms`);
@@ -254,7 +254,11 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
254
254
  let executionWorkspace = workspace;
255
255
  let localMemoryTelemetry = null;
256
256
  const inheritedEnv = { ...process.env };
257
- const cloudReportPreparation = await prepareCloudReportExecution({ env: inheritedEnv, root: workspace.dir, executionId: input.executionId, agentHandle: input.handle });
257
+ const reportRoots = {
258
+ root: workspace.dir,
259
+ ...(input.launch.qaReportRoot === undefined ? {} : { qaReportRoot: input.launch.qaReportRoot }),
260
+ };
261
+ const cloudReportPreparation = await prepareCloudReportExecution({ env: inheritedEnv, ...reportRoots, executionId: input.executionId, agentHandle: input.handle });
258
262
  const { facts: cloudReportUpload, snapshot: reportSnapshot } = cloudReportPreparation;
259
263
  for (const key of Object.keys(inheritedEnv)) {
260
264
  if (key.startsWith("CREW_AGENT_MEMORY_") || ["CREW_CLOUD_PROVIDER", "CREW_OSS_UPLOAD_SCRIPT", "CREW_OSS_REPORT_UPLOAD"].includes(key))
@@ -382,26 +386,6 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
382
386
  }
383
387
  const attachmentPlan = routeRuntimeAttachments(runtime.name, materialized?.attachments ?? []);
384
388
  const wakePrompt = `${resolvePrompt(input.wakePrompt, promptContext)}${attachmentPlan.promptSuffix}`;
385
- let pendingMemoryContext = "";
386
- if (input.localMemoryV2Enabled === true) {
387
- try {
388
- const pendingCandidates = await searchPendingMemoryCandidates({
389
- agentDir: executionWorkspace.dir,
390
- query: wakePrompt,
391
- limit: 5,
392
- });
393
- pendingMemoryContext = renderPendingMemoryContext(pendingCandidates);
394
- }
395
- catch (error) {
396
- dslog("local_memory.pending_recall_failed", "本地归档记忆召回失败", {
397
- level: "WARN",
398
- execution_id: input.executionId,
399
- agent_handle: input.handle,
400
- error_name: error instanceof Error ? error.name : "unknown",
401
- error_code: error?.code,
402
- });
403
- }
404
- }
405
389
  memoryPruneTraceId = parseMemoryPruneTraceId(wakePrompt);
406
390
  if (input.localMemoryV2Enabled === true && memoryPruneTraceId !== null
407
391
  && isLocalMemoryV2Eligible({
@@ -413,26 +397,14 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
413
397
  })) {
414
398
  localMemoryResultPath = makeLocalMemoryResultPath(executionWorkspace.runDir, input.executionId);
415
399
  }
416
- const promptWithoutPending = resolvePrompt(input.systemPrompt, promptContext);
417
- let baseSystemPrompt = `${promptWithoutPending}${pendingMemoryContext}`;
418
- const structuredMemoryInstructions = localMemoryResultPath === null
419
- ? null
420
- : renderLocalMemoryResultInstructions(localMemoryResultPath);
421
- const fitsPromptBudget = (prompt) => structuredMemoryInstructions === null
422
- || input.maxSystemPromptBytes === undefined
423
- || Buffer.byteLength(prompt, "utf8") + Buffer.byteLength(structuredMemoryInstructions, "utf8")
424
- <= input.maxSystemPromptBytes;
425
- // The structured prune contract is mandatory for V2. Drop optional archived
426
- // recall before dropping the daemon result file when the prompt budget is tight.
427
- if (!fitsPromptBudget(baseSystemPrompt) && pendingMemoryContext !== "") {
428
- baseSystemPrompt = promptWithoutPending;
429
- }
430
- const structuredPromptFits = fitsPromptBudget(baseSystemPrompt);
431
- if (!structuredPromptFits)
432
- localMemoryResultPath = null;
433
- const systemPrompt = localMemoryResultPath === null
434
- ? baseSystemPrompt
435
- : `${baseSystemPrompt}${structuredMemoryInstructions ?? ""}`;
400
+ const memoryPrompt = await prepareLocalMemoryPrompt({
401
+ agentDir: executionWorkspace.dir, executionId: input.executionId, agentHandle: input.handle,
402
+ enabled: input.localMemoryV2Enabled === true, query: wakePrompt,
403
+ resultPath: localMemoryResultPath, maxBytes: input.maxSystemPromptBytes,
404
+ renderBase: (reservedPromptBytes) => resolvePrompt(input.systemPrompt, { ...promptContext, reservedPromptBytes }),
405
+ });
406
+ localMemoryResultPath = memoryPrompt.resultPath;
407
+ const systemPrompt = memoryPrompt.systemPrompt;
436
408
  if (!isolatedMaintenance)
437
409
  try {
438
410
  localMemoryTelemetry = createLocalMemoryTelemetry({
@@ -1018,7 +990,7 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
1018
990
  }
1019
991
  const cloudReportResult = exitCode === 0
1020
992
  ? await deliverCloudReports({
1021
- facts: cloudReportUpload, root: workspace.dir, before: reportSnapshot, finalText,
993
+ facts: cloudReportUpload, ...reportRoots, before: reportSnapshot, finalText,
1022
994
  externalAnswer: externalOutput.lastClosedBlock
1023
995
  ?? (finalText === null ? null : extractExternalAnswer(finalText)),
1024
996
  captureFinal: input.captureFinal, executionId: input.executionId, agentHandle: input.handle,
@@ -0,0 +1,37 @@
1
+ import { dslog } from "../slog.js";
2
+ import { renderPendingMemoryContext, searchPendingMemoryCandidates } from "./pending.js";
3
+ import { renderLocalMemoryResultInstructions } from "./runtime.js";
4
+ export async function prepareLocalMemoryPrompt(input) {
5
+ let pending = "";
6
+ if (input.enabled) {
7
+ try {
8
+ pending = renderPendingMemoryContext(await searchPendingMemoryCandidates({
9
+ agentDir: input.agentDir, query: input.query, limit: 5,
10
+ }));
11
+ }
12
+ catch (error) {
13
+ dslog("local_memory.pending_recall_failed", "本地归档记忆召回失败", {
14
+ level: "WARN",
15
+ execution_id: input.executionId,
16
+ agent_handle: input.agentHandle,
17
+ error_name: error instanceof Error ? error.name : "unknown",
18
+ error_code: error?.code,
19
+ });
20
+ }
21
+ }
22
+ const original = input.renderBase(0);
23
+ 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 };
34
+ }
35
+ // 强制基础指令与契约本身仍无法共存时,保留既有非结构化兼容回退,不裁基础指令。
36
+ return { systemPrompt: original, resultPath: null };
37
+ }
@@ -0,0 +1,232 @@
1
+ import { constants } from "node:fs";
2
+ import { open, realpath } from "node:fs/promises";
3
+ import { createHash } from "node:crypto";
4
+ import { isAbsolute, relative, resolve, sep } from "node:path";
5
+ import { pathToFileURL } from "node:url";
6
+ import { parseArgs, TextDecoder } from "node:util";
7
+ import { z } from "zod";
8
+ const OUTPUT_BYTES = 12 * 1024;
9
+ const FILE_BYTES = 32 * 1024 * 1024;
10
+ const FileSchema = z.string().min(1).refine((file) => {
11
+ if (isAbsolute(file) || /[\\\u0000-\u001f\u007f]/u.test(file))
12
+ return false;
13
+ if (Buffer.byteLength(file, "utf8") > 1024)
14
+ return false;
15
+ const parts = file.split("/");
16
+ if (parts.some((part) => part === "" || part === "." || part === ".."))
17
+ return false;
18
+ return file === "MEMORY.md" || (parts[0] === "notes" && parts.length > 1 && file.endsWith(".md"));
19
+ }, "memory_path_invalid");
20
+ const Common = {
21
+ file: FileSchema.default("MEMORY.md"),
22
+ ifHash: z.string().regex(/^[a-f0-9]{64}$/u).optional(),
23
+ };
24
+ const CommandSchema = z.discriminatedUnion("command", [
25
+ z.object({
26
+ command: z.literal("search"), ...Common,
27
+ query: z.string().trim().min(1).max(256),
28
+ limit: z.coerce.number().int().min(1).max(40).default(10),
29
+ afterLine: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
30
+ }).strict(),
31
+ z.object({
32
+ command: z.literal("read"), ...Common,
33
+ 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),
35
+ }).strict(),
36
+ ]);
37
+ const USAGE = `只读记忆工具,默认输出 JSON,不修改任何记忆文件。
38
+ crew-memory search --query <关键词> [--file MEMORY.md|notes/文件.md] [--limit 10] [--after-line 0]
39
+ crew-memory read [--file MEMORY.md|notes/文件.md] [--offset 0] [--limit 2000]
40
+ 搜索关键词按空白分隔,同一行须全部命中;返回完整文件中的行号和字符偏移。
41
+ 读取的 offset/limit 按 Unicode 字符计数;用 nextOffset 继续,eof=true 表示结束。
42
+ 后续页可传 --if-hash <前一页的 sha256>,文件变化时停止,避免混读不同版本。
43
+ 搜索结果是原始资料定位,不代表候选已生效;按当前有效索引读取明细,不扫描 pending。`;
44
+ function inside(root, path) {
45
+ const value = relative(root, path);
46
+ return value !== ".." && !value.startsWith(`..${sep}`) && !isAbsolute(value);
47
+ }
48
+ async function readSource(home, file) {
49
+ if (!home)
50
+ throw new Error("memory_home_missing");
51
+ const root = await realpath(home);
52
+ const path = await realpath(resolve(root, file));
53
+ if (!inside(root, path) || !FileSchema.safeParse(relative(root, path).split(sep).join("/")).success) {
54
+ throw new Error("memory_path_invalid");
55
+ }
56
+ const handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
57
+ try {
58
+ const stat = await handle.stat();
59
+ if (!stat.isFile())
60
+ throw new Error("memory_file_not_regular");
61
+ if (stat.size > FILE_BYTES)
62
+ throw new Error("memory_file_too_large");
63
+ // 允许读完整来源,但绝不能在文件并发增长时无界分配内存。
64
+ const chunks = [];
65
+ const chunk = Buffer.allocUnsafe(64 * 1024);
66
+ let bytes = 0;
67
+ for (;;) {
68
+ const { bytesRead } = await handle.read(chunk, 0, Math.min(chunk.length, FILE_BYTES + 1 - bytes), null);
69
+ if (bytesRead === 0)
70
+ break;
71
+ bytes += bytesRead;
72
+ if (bytes > FILE_BYTES)
73
+ throw new Error("memory_file_too_large");
74
+ chunks.push(Buffer.from(chunk.subarray(0, bytesRead)));
75
+ }
76
+ const buffer = Buffer.concat(chunks);
77
+ const text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(buffer);
78
+ return { text, sha256: createHash("sha256").update(buffer).digest("hex") };
79
+ }
80
+ finally {
81
+ await handle.close();
82
+ }
83
+ }
84
+ function characterCount(text) {
85
+ let count = 0;
86
+ for (const _character of text)
87
+ count += 1;
88
+ return count;
89
+ }
90
+ function excerpt(text, limit) {
91
+ let value = "";
92
+ let count = 0;
93
+ for (const character of text) {
94
+ if (count++ >= limit)
95
+ return `${value}...`;
96
+ value += character;
97
+ }
98
+ return value;
99
+ }
100
+ function originalLowercaseIndex(text, index) {
101
+ let originalIndex = 0;
102
+ let lowerIndex = 0;
103
+ for (const character of text) {
104
+ // 逐字符只累计小写长度;匹配仍用整行小写,保留长度不变的词尾 sigma 上下文规则。
105
+ lowerIndex += character.toLowerCase().length;
106
+ if (index < lowerIndex)
107
+ return originalIndex;
108
+ originalIndex += character.length;
109
+ }
110
+ return text.length;
111
+ }
112
+ function searchSource(text, sha256, command) {
113
+ const terms = command.query.toLowerCase().split(/\s+/u);
114
+ const matches = [];
115
+ let line = 0;
116
+ let offset = 0;
117
+ let heading = "";
118
+ let matchCount = 0;
119
+ let hasMore = false;
120
+ let remaining = OUTPUT_BYTES - Buffer.byteLength(JSON.stringify({ file: command.file, sha256 })) - 512;
121
+ for (const match of text.matchAll(/[^\n]*(?:\n|$)/gu)) {
122
+ if (match[0] === "")
123
+ break;
124
+ line += 1;
125
+ const raw = match[0];
126
+ const content = raw.replace(/\r?\n$/u, "");
127
+ const title = /^#{1,6}\s+(.+?)\s*$/u.exec(content)?.[1];
128
+ if (title !== undefined)
129
+ heading = excerpt(title, 96);
130
+ const lower = content.toLowerCase();
131
+ if (terms.every((term) => lower.includes(term))) {
132
+ matchCount += 1;
133
+ if (line > command.afterLine) {
134
+ let start = Math.max(0, originalLowercaseIndex(content, lower.indexOf(terms[0])) - 60);
135
+ if (start > 0 && /[\uDC00-\uDFFF]/u.test(content[start]))
136
+ start -= 1;
137
+ const item = { line, offset, heading, text: `${start > 0 ? "..." : ""}${excerpt(content.slice(start), 240)}` };
138
+ const bytes = Buffer.byteLength(JSON.stringify(item), "utf8") + 1;
139
+ if (hasMore || matches.length >= command.limit || bytes > remaining) {
140
+ hasMore = true;
141
+ }
142
+ else {
143
+ matches.push(item);
144
+ remaining -= bytes;
145
+ }
146
+ }
147
+ }
148
+ offset += characterCount(raw);
149
+ }
150
+ return {
151
+ file: command.file, sha256, matches, matchCount, hasMore,
152
+ nextAfterLine: hasMore ? matches.at(-1)?.line ?? null : null,
153
+ };
154
+ }
155
+ function readPage(text, sha256, command) {
156
+ const metadata = { file: command.file, sha256, offset: command.offset, nextOffset: Number.MAX_SAFE_INTEGER, eof: false, content: "" };
157
+ let remaining = OUTPUT_BYTES - Buffer.byteLength(JSON.stringify(metadata), "utf8");
158
+ let position = 0;
159
+ let offset = 0;
160
+ while (position < text.length && offset < command.offset) {
161
+ position += text.codePointAt(position) > 0xffff ? 2 : 1;
162
+ offset += 1;
163
+ }
164
+ let content = "";
165
+ let count = 0;
166
+ while (position < text.length && count < command.limit) {
167
+ const character = String.fromCodePoint(text.codePointAt(position));
168
+ const bytes = Buffer.byteLength(JSON.stringify(character), "utf8") - 2;
169
+ if (bytes > remaining)
170
+ break;
171
+ remaining -= bytes;
172
+ content += character;
173
+ position += character.length;
174
+ offset += 1;
175
+ count += 1;
176
+ }
177
+ const eof = position === text.length;
178
+ return { ...metadata, content, eof, nextOffset: eof ? null : offset };
179
+ }
180
+ export async function runMemoryReadCommand(argv, io) {
181
+ let command;
182
+ try {
183
+ const { values, positionals } = parseArgs({
184
+ args: [...argv], allowPositionals: true,
185
+ options: {
186
+ query: { type: "string", short: "q" }, file: { type: "string" },
187
+ limit: { type: "string" }, offset: { type: "string" },
188
+ "after-line": { type: "string" }, "if-hash": { type: "string" },
189
+ json: { type: "boolean" }, help: { type: "boolean", short: "h" },
190
+ },
191
+ });
192
+ if (values.help) {
193
+ io.out(USAGE);
194
+ return 0;
195
+ }
196
+ if (positionals.length !== 1)
197
+ throw new Error("memory_command_invalid");
198
+ const { help: _help, json: _json, "after-line": afterLine, "if-hash": ifHash, ...options } = values;
199
+ command = CommandSchema.parse({
200
+ command: positionals[0], ...options,
201
+ ...(afterLine === undefined ? {} : { afterLine }),
202
+ ...(ifHash === undefined ? {} : { ifHash }),
203
+ });
204
+ }
205
+ catch {
206
+ io.err(`memory_arguments_invalid: 参数不合法。\n${USAGE}`);
207
+ return 2;
208
+ }
209
+ try {
210
+ const source = await readSource(io.env.CREW_HOME, command.file);
211
+ if (command.ifHash !== undefined && command.ifHash !== source.sha256)
212
+ throw new Error("memory_file_changed");
213
+ 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));
217
+ return 0;
218
+ }
219
+ catch (error) {
220
+ const code = error.code;
221
+ const known = error instanceof Error && /^memory_[a-z_]+$/u.test(error.message) ? error.message : code ?? "memory_read_failed";
222
+ io.err(JSON.stringify({ error: known, message: "记忆读取未完成;检查路径、文件版本或大小,不要将此错误当作没有记忆。" }));
223
+ return 1;
224
+ }
225
+ }
226
+ if (process.argv[1] !== undefined && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
227
+ void runMemoryReadCommand(process.argv.slice(2), {
228
+ env: process.env,
229
+ out: (text) => process.stdout.write(`${text}\n`),
230
+ err: (text) => process.stderr.write(`${text}\n`),
231
+ }).then((code) => { process.exitCode = code; });
232
+ }
@@ -1,6 +1,7 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { readFile, stat } from "node:fs/promises";
3
3
  import { isAbsolute, join, normalize, relative, resolve } from "node:path";
4
+ import { parseArgs } from "node:util";
4
5
  import { capMemoryForInject, MEMORY_INJECT_CAP } from "./prompt.js";
5
6
  const INJECTION_MARKERS = [
6
7
  "## Injected MEMORY.md (bounded local context)\n",
@@ -172,10 +173,58 @@ function commandOperation(command) {
172
173
  ? "read"
173
174
  : null;
174
175
  }
176
+ function crewMemoryReadAccess(command, paths) {
177
+ if (!/^\s*crew-memory(?:\s|$)/u.test(command))
178
+ return null;
179
+ const args = [];
180
+ let remaining = command.trim();
181
+ // Only complete literal arguments are supported; shell expansion and concatenated quoting are not inferred.
182
+ while (remaining !== "") {
183
+ const match = /^(?:"([^"\\$`]*)"|'([^']*)'|([^\s"'\\$`<>;|&()\[\]{}*?!#]+))(?=\s|$)\s*/u.exec(remaining);
184
+ if (match === null)
185
+ return null;
186
+ args.push(match[1] ?? match[2] ?? match[3]);
187
+ remaining = remaining.slice(match[0].length);
188
+ }
189
+ try {
190
+ const { values, positionals } = parseArgs({
191
+ args: args.slice(1),
192
+ allowPositionals: true,
193
+ options: {
194
+ file: { type: "string" }, query: { type: "string", short: "q" },
195
+ limit: { type: "string" }, offset: { type: "string" },
196
+ "after-line": { type: "string" }, "if-hash": { type: "string" },
197
+ json: { type: "boolean" }, help: { type: "boolean", short: "h" },
198
+ },
199
+ });
200
+ if (values.help || positionals.length !== 1)
201
+ return null;
202
+ const operation = positionals[0];
203
+ if (operation === "search") {
204
+ if (!values.query?.trim() || values.offset !== undefined)
205
+ return null;
206
+ }
207
+ else if (operation !== "read" || values.query !== undefined || values["after-line"] !== undefined) {
208
+ return null;
209
+ }
210
+ const file = values.file ?? "MEMORY.md";
211
+ if (file !== "MEMORY.md" && !/^notes\/[^/\\\u0000-\u001f\u007f]+\.md$/u.test(file))
212
+ return null;
213
+ const classified = classifyExplicitPath(resolve(paths.homeDir, file), paths);
214
+ return classified === null ? null : { ...classified, operation: "read" };
215
+ }
216
+ catch {
217
+ return null;
218
+ }
219
+ }
175
220
  function commandAccesses(command, paths) {
176
221
  if (/[;|&\n]/.test(command))
177
222
  return [];
178
223
  const accesses = new Map();
224
+ const memoryRead = crewMemoryReadAccess(command, paths);
225
+ if (memoryRead !== null) {
226
+ accesses.set(`${memoryRead.target}:${memoryRead.relative_path ?? ""}:read`, memoryRead);
227
+ }
179
228
  const expanded = command
180
229
  .replaceAll("${CREW_HOME}", paths.homeDir)
181
230
  .replaceAll("$CREW_HOME", paths.homeDir)
@@ -243,26 +292,41 @@ export function createLocalMemoryAccessObserver(options) {
243
292
  for (const block of blocks) {
244
293
  if (block.type !== "tool_use" || typeof block.id !== "string" || typeof block.name !== "string")
245
294
  continue;
295
+ if (block.name === "Bash") {
296
+ const input = block.input && typeof block.input === "object" && !Array.isArray(block.input)
297
+ ? block.input
298
+ : null;
299
+ const accesses = typeof input?.command === "string" ? commandAccesses(input.command, paths) : [];
300
+ if (accesses.length > 0)
301
+ pending.set(block.id, { accesses, evidence: "shell_command" });
302
+ continue;
303
+ }
246
304
  const operation = toolOperation(block.name);
247
305
  const path = toolPath(block.input);
248
306
  const classified = operation === null || path === null ? null : classifyExplicitPath(path, paths);
249
- if (operation !== null && classified !== null)
250
- pending.set(block.id, { operation, ...classified });
307
+ if (operation !== null && classified !== null) {
308
+ pending.set(block.id, {
309
+ accesses: [{ operation, ...classified }],
310
+ evidence: "structured_tool",
311
+ });
312
+ }
251
313
  }
252
314
  }
253
315
  if (record.type === "user") {
254
316
  for (const block of blocks) {
255
317
  if (block.type !== "tool_result" || typeof block.tool_use_id !== "string")
256
318
  continue;
257
- const access = pending.get(block.tool_use_id);
319
+ const toolAccess = pending.get(block.tool_use_id);
258
320
  pending.delete(block.tool_use_id);
259
- if (access === undefined)
321
+ if (toolAccess === undefined)
260
322
  continue;
261
- recordObservation(observations, counts, {
262
- ...access,
263
- outcome: block.is_error === true ? "failed" : "succeeded",
264
- evidence: "structured_tool",
265
- });
323
+ for (const access of toolAccess.accesses) {
324
+ recordObservation(observations, counts, {
325
+ ...access,
326
+ outcome: block.is_error === true ? "failed" : "succeeded",
327
+ evidence: toolAccess.evidence,
328
+ });
329
+ }
266
330
  }
267
331
  }
268
332
  if (record.type === "kimi.acp.tool_call" && typeof record.id === "string") {
@@ -277,15 +341,18 @@ export function createLocalMemoryAccessObserver(options) {
277
341
  ? null
278
342
  : classifyExplicitPath(path, paths);
279
343
  if (fallbackOperation !== null && classified !== null) {
280
- pending.set(record.id, { operation: fallbackOperation, ...classified });
344
+ pending.set(record.id, {
345
+ accesses: [{ operation: fallbackOperation, ...classified }],
346
+ evidence: "structured_tool",
347
+ });
281
348
  }
282
349
  }
283
350
  if (record.type === "kimi.acp.tool_result" && typeof record.id === "string") {
284
351
  if (record.status !== "completed" && record.status !== "failed")
285
352
  return observations;
286
- const access = pending.get(record.id);
353
+ const toolAccess = pending.get(record.id);
287
354
  pending.delete(record.id);
288
- if (access !== undefined) {
355
+ for (const access of toolAccess?.accesses ?? []) {
289
356
  recordObservation(observations, counts, {
290
357
  ...access,
291
358
  outcome: record.status === "failed" ? "failed" : "succeeded",
package/dist/prompt.js CHANGED
@@ -67,10 +67,23 @@ export function capWorkLogForInject(workLog, cap = WORKLOG_INJECT_CAP) {
67
67
  const tail = workLog.slice(-WORKLOG_TAIL);
68
68
  return `${head}\n…(work-log 过长已截去中段,全文用 Read 读 $CREW_TASK_LOG)\n${tail}`;
69
69
  }
70
- export function capMemoryForInject(memory, cap = MEMORY_INJECT_CAP) {
71
- return memory.length > cap
72
- ? `${memory.slice(0, cap)}\n…(MEMORY.md 过长已截断,详情用 Read 读 $CREW_HOME/MEMORY.md;功能明细仅按索引中的 file 读取)`
73
- : memory;
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;
85
+ }
86
+ return notice + memory.slice(0, end);
74
87
  }
75
88
  export function buildSystemPrompt(ctx) {
76
89
  const product = ctx.productName ?? "nowwork";
@@ -99,7 +112,7 @@ export function buildSystemPrompt(ctx) {
99
112
  // scheduled 换成静默版,不出现任何"先确认/先回复"的措辞。
100
113
  const startupSequence = alwaysReport
101
114
  ? `## 启动序列(每轮汇报定时任务)
102
- 1. 使用本提示词末尾已注入的 MEMORY.md 索引;命中相关条目时,再按索引中的 file 从 $CREW_HOME 读取明细。
115
+ 1. 参考本提示词中的 MEMORY.md 预览;需要历史约束或经验时检索完整索引,再按索引中的 file 从 $CREW_HOME 读取明细。
103
116
  2. 执行唤醒提示词里给出的定时指令。
104
117
  3. Return exactly one self-contained final report as your runtime final response.
105
118
  4. Do not call crew message send; the daemon delivers the final response.
@@ -107,14 +120,14 @@ export function buildSystemPrompt(ctx) {
107
120
  6. 只有存在需要后续跟进的具体事项时,才用 \`crew task create\` 创建任务。`
108
121
  : scheduled
109
122
  ? `## 启动序列(静默定时任务)
110
- 1. 使用本提示词末尾已注入的 MEMORY.md 索引;命中相关条目时,再按索引中的 file 从 $CREW_HOME 读取明细。
123
+ 1. 参考本提示词中的 MEMORY.md 预览;需要历史约束或经验时检索完整索引,再按索引中的 file 从 $CREW_HOME 读取明细。
111
124
  2. 执行唤醒提示词里给出的定时指令。**默认零输出**:不判断"是否需要先确认/声明接手",本轮没有人在等你回复。
112
125
  3. 只有发现异常、需要人处理、或指令明确要求汇报时,才用 \`crew message send --send-draft\` 输出到频道(用法见上面"通信"一节)。
113
126
  4. 只有存在需要后续跟进的具体事项时,才用 \`crew task create\` 创建任务。
114
127
  5. 无异常时直接结束,不需要任何输出,不必读频道历史(除非指令要求)。`
115
128
  : `## 启动序列
116
129
  1. 若本轮来信可以直接回答,不要先发确认消息;处理完后只发一条完整回复。只有需要澄清、调用工具、协调他人或较长时间处理时,才先发有信息量的确认/进度消息。
117
- 2. 使用本提示词末尾已注入的 MEMORY.md 索引;命中相关条目时,再按索引中的 file 从 $CREW_HOME 读取明细。
130
+ 2. 参考本提示词中的 MEMORY.md 预览;需要历史约束或经验时检索完整索引,再按索引中的 file 从 $CREW_HOME 读取明细。
118
131
  3. 若本轮只有"有未读"的 inbox notice、没有正文:notice 表示存在你尚未看到的消息(正文被暂时省略以免刷屏,不是没有内容)。是否读、何时读由你判断,可用 \`crew message check\` / \`crew message read\` 拉取。**绝不能仅凭一条 content-free notice 就断定"没有工作"**;若选择暂不读,要诚实当作 defer。
119
132
  4. 收到消息就处理,并用 \`crew message send\` 回复。
120
133
  5. **做完你负责的所有事再停。** 多步任务(调研/改码/测试)要全部完成、汇报结果后再停。新消息会在你存活期间自动送达,无需轮询等待。`;
@@ -329,13 +342,15 @@ ${interactiveTaskRules}${freshnessRule}${externalReplyRule}${collaborationEtique
329
342
 
330
343
  ## Workspace 与分层记忆(CRITICAL — 索引+按需,配合上面的并行规则)
331
344
  你的持久记忆在 \`$CREW_HOME\`(跨你所有任务共享),按“常驻索引 + 功能明细 + 当前工作”分层:
332
- 1. **MEMORY.md = 常驻索引 + 角色**:系统每次已把它(截断索引版)注入本提示词末尾。索引条目包含 \`file/type/scope/summary/triggers/priority/updated\`,其中 file 是相对于 \`$CREW_HOME\` 的路径(例如 \`notes/testing.md\`),先用这些字段判断当前问题是否相关;通常不必再读整份 MEMORY.md。
345
+ 1. **MEMORY.md = 常驻索引 + 角色**:提示词末尾可能只有它的有界预览,预算不足时也可能省略。预览未命中不等于没有记忆。索引条目包含 \`file/type/scope/summary/triggers/priority/updated\`,其中 file 是相对于 \`$CREW_HOME\` 的路径(例如 \`notes/testing.md\`);需要历史约束、经验或偏好时先检索完整索引,再按需分段读取。
333
346
  2. **notes/<topic>.md = 功能明细**:按用户偏好、项目规则、测试、调试、Git、事故复盘等主题拆分。优先读取与当前任务匹配的文件;\`notes/lessons.md\` 只保留未分类经验和历史兼容内容,不是所有新经验的默认目的地。普通任务默认只读不写,但当前用户明确要求时可以按最小范围更新;任务结束后的专门收尾执行仍按收尾指令复核。
334
347
  - 对旧版 MEMORY.md,如果条目没有完整结构化字段但明确指向 \`notes/<file>.md\`,把它视为 legacy 索引;仍需结合条目标题/链接和当前请求判断相关性,不要因为缺少 summary/triggers 就假定正文已注入。
335
348
  3. **本任务 work-log = \`$CREW_TASK_LOG\`**:每任务一份(别的运行不会碰),记当前阶段/下一步/卡点/关键结论。系统已把它的当前内容注入末尾让你续上;有进展就更新它——这是你**唯一**该在任务中持续写的记忆文件。**work-log 是现状快照,不是流水账**:保持 ≤100 行,更新时重写全文替换过时内容,不要无限追加(过长会被注入截断)。
336
349
 
337
350
  **按需读取规则(提示词引导,不是 Daemon 自动召回保证)**:
338
- - 当前请求命中 MEMORY.md 某条目的 \`summary\` 或 \`triggers\`,或用户明确询问历史经验/偏好时,在最终回答或执行动作前用 \`Read $CREW_HOME/<file>\` 读取对应明细;不要再次拼接 \`notes/\`。
351
+ - 需要历史约束、经验或偏好时,在最终回答或执行动作前先用只读命令 \`crew-memory search --query '关键词' [--after-line N]\` 搜索完整 \`$CREW_HOME/MEMORY.md\`;默认返回 JSON,有后续结果时按返回游标继续,不要把预览中未命中当成全文无结果。
352
+ - 用 \`crew-memory read [--file MEMORY.md|notes/xxx.md] [--offset N] [--limit N]\` 读取命中范围,再按索引中的 file 从 $CREW_HOME 读取明细;默认返回 JSON。以当前有效索引及其指向的正文为依据,不自动读取 pending,也不把未决或已替代片段当成有效记忆。
353
+ - 旧 daemon 没有 \`crew-memory\` 时,回退到 runtime 原生文件搜索和分段 Read:先搜索完整 \`$CREW_HOME/MEMORY.md\`,再用 \`Read $CREW_HOME/<file>\` 读取命中范围;不要再次拼接 \`notes/\`。工具提示截断或返回下一页时继续分页,不要一次读取整份大文件。
339
354
  - 未命中时不要为了“保险”读取全部 notes;只读取能直接影响当前任务的文件。MEMORY.md 注入的是索引,不会自动让 note 正文可用。
340
355
 
341
356
  **沉淀与瘦身由系统触发,不要平时自行扩展范围**:任务被改为 done/closed 时,系统会单独唤醒你收尾——那时按功能分类把可复用经验提炼进合适的 \`notes/<topic>.md\`,更新 MEMORY.md 索引。普通执行已经按用户明确要求写入时,收尾先检查、后修改,只在明确缺漏时做最小必要修正,不得重复追加或只为优化措辞而改写。最终结论必须且只能归类为:无变化、已修正、冲突、失败;无变化或已修正时必须满足对应后置条件并清空 work-log,冲突或失败时保留 work-log,任一记忆写入失败则保留 work-log。不要默认全部写入 \`notes/lessons.md\`。默认不要改 MEMORY.md/notes,但不能覆盖当前用户明确授权的最小范围更新。
package/dist/workspace.js CHANGED
@@ -11,6 +11,8 @@
11
11
  import { mkdir, writeFile, readFile, chmod, access, lstat } from "node:fs/promises";
12
12
  import { createHash, randomUUID } from "node:crypto";
13
13
  import { dirname, join } from "node:path";
14
+ import { fileURLToPath, pathToFileURL } from "node:url";
15
+ import { createRequire } from "node:module";
14
16
  import { dslog } from "./slog.js";
15
17
  /** 文件系统安全的 taskKey:仅留 [\w.-],其余转 _,截断,避免路径穿越/超长。 */
16
18
  export function safeKey(key) {
@@ -96,6 +98,19 @@ export async function prepareWorkspace(input) {
96
98
  await chmod(wrapper, 0o755);
97
99
  // win32 侧 wrapper:cmd/PowerShell 不认 shebang 脚本,并存 crew.cmd(unix 下无害不会被命中)
98
100
  await writeFile(join(crewDir, "crew.cmd"), `@echo off\r\nnode ${JSON.stringify(input.cliPath)} %*\r\n`, "utf8");
101
+ if (!input.skipLocalMemory) {
102
+ const sourceMode = import.meta.url.endsWith(".ts");
103
+ const reader = fileURLToPath(new URL(`./local-memory/read-cli.${sourceMode ? "ts" : "js"}`, import.meta.url));
104
+ const readerArgs = sourceMode
105
+ ? ["--import", pathToFileURL(createRequire(import.meta.url).resolve("tsx/esm")).href, reader]
106
+ : [reader];
107
+ const readerWrapper = join(crewDir, "crew-memory");
108
+ const posixArgs = readerArgs.map((arg) => `'${arg.replace(/'/gu, "'\\''")}'`).join(" ");
109
+ const windowsArgs = readerArgs.map((arg) => `"${arg.replace(/%/gu, "%%")}"`).join(" ");
110
+ await writeFile(readerWrapper, `#!/bin/sh\nexec node ${posixArgs} "$@"\n`, "utf8");
111
+ await chmod(readerWrapper, 0o755);
112
+ await writeFile(join(crewDir, "crew-memory.cmd"), `@echo off\r\nnode ${windowsArgs} %*\r\n`, "utf8");
113
+ }
99
114
  // 每个 agent 独立的 XDG 配置根:隔离第三方 CLI 凭证,避免 agent 之间互相串号
100
115
  const homeDir = join(dir, ".home");
101
116
  await mkdir(join(homeDir, ".config"), { recursive: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nowcrew/daemon",
3
- "version": "0.6.87",
3
+ "version": "0.6.88-beta.1",
4
4
  "type": "module",
5
5
  "description": "crew daemon — 运行在用户机器:拉起/管理 agent 进程,注入 crew CLI,归一化 runtime 事件",
6
6
  "license": "Apache-2.0",