@phren/cli 0.2.2 → 0.2.3

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.
@@ -43,10 +43,15 @@ export declare function digestTopic(slug: string, file: string, bullets: Archive
43
43
  export declare function structuralNow(d: TopicDigest): string;
44
44
  /** A prose paragraph over the newest bullets, when a model is configured; "" otherwise. */
45
45
  export declare function proseNow(d: TopicDigest, bullets: ArchivedBullet[], signal?: AbortSignal): Promise<string>;
46
+ /** Fingerprint of everything in a topic file except its own Now block. */
47
+ export declare function contentFingerprint(content: string, start: string, end: string): string;
46
48
  /** Insert or replace a marked block. Topic files: before the first section; summary: at the end. */
47
49
  export declare function upsertBlock(content: string, start: string, end: string, rendered: string, where: "top" | "bottom"): string;
48
- /** The stamp inside an existing block, so unchanged files are not rewritten. */
49
- export declare function blockStamp(content: string, start: string): string | null;
50
+ /** The stamp and fingerprint inside an existing block, so unchanged files are not rewritten. */
51
+ export declare function blockMeta(content: string, start: string): {
52
+ at: string;
53
+ hash: string | null;
54
+ } | null;
50
55
  export interface SummarizeOptions {
51
56
  /** Ask a configured model for prose; falls back to structural when none answers. */
52
57
  llm?: boolean;
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import * as fs from "fs";
17
17
  import * as path from "path";
18
+ import { createHash } from "crypto";
18
19
  import { callLlm } from "./dedup.js";
19
20
  import { readProjectTopics, topicReferencePath } from "../project-topics.js";
20
21
  import { storeAwareProjectPath } from "../store-routing.js";
@@ -151,8 +152,15 @@ export async function proseNow(d, bullets, signal) {
151
152
  return "";
152
153
  }
153
154
  }
154
- function block(start, end, heading, body, stamp) {
155
- return `${start.replace("-->", `at=${stamp} -->`)}\n${heading}\n\n${body}\n${end}`;
155
+ function block(start, end, heading, body, stamp, hash = "") {
156
+ return `${start.replace("-->", `at=${stamp}${hash ? ` hash=${hash}` : ""} -->`)}\n${heading}\n\n${body}\n${end}`;
157
+ }
158
+ /** Fingerprint of everything in a topic file except its own Now block. */
159
+ export function contentFingerprint(content, start, end) {
160
+ const s = content.indexOf(start.replace("-->", ""));
161
+ const e = content.indexOf(end);
162
+ const rest = s !== -1 && e !== -1 && e > s ? content.slice(0, s) + content.slice(e + end.length) : content;
163
+ return createHash("sha1").update(rest.replace(/\s+/g, " ").trim()).digest("hex").slice(0, 12);
156
164
  }
157
165
  /** Insert or replace a marked block. Topic files: before the first section; summary: at the end. */
158
166
  export function upsertBlock(content, start, end, rendered, where) {
@@ -170,10 +178,10 @@ export function upsertBlock(content, start, end, rendered, where) {
170
178
  }
171
179
  return `${content.trimEnd()}\n\n${rendered}\n`;
172
180
  }
173
- /** The stamp inside an existing block, so unchanged files are not rewritten. */
174
- export function blockStamp(content, start) {
175
- const m = new RegExp(`${start.replace("-->", "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}at=([^ ]+) -->`).exec(content);
176
- return m ? m[1] : null;
181
+ /** The stamp and fingerprint inside an existing block, so unchanged files are not rewritten. */
182
+ export function blockMeta(content, start) {
183
+ const m = new RegExp(`${start.replace("-->", "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}at=([^ ]+)(?: hash=([0-9a-f]+))? -->`).exec(content);
184
+ return m ? { at: m[1], hash: m[2] ?? null } : null;
177
185
  }
178
186
  /**
179
187
  * Move whole "## Archived <date>" sections, oldest first, into <topic>.older.md
@@ -210,13 +218,14 @@ export function splitTopicFile(filePath, maxBullets = TOPIC_SPLIT_AT) {
210
218
  export async function summarizeTopicFile(filePath, slug, opts = {}) {
211
219
  const split = splitTopicFile(filePath);
212
220
  const content = fs.readFileSync(filePath, "utf8");
213
- const stat = fs.statSync(filePath);
214
221
  const stamp = (opts.now ?? (() => new Date()))().toISOString();
215
- const prev = blockStamp(content, NOW_START);
222
+ const prev = blockMeta(content, NOW_START);
223
+ const fingerprint = contentFingerprint(content, NOW_START, NOW_END);
216
224
  const bullets = parseTopicBullets(content);
217
225
  const digest = digestTopic(slug, filePath, bullets);
218
- // The block records when it was written; a file untouched since then is left alone.
219
- if (!opts.force && prev && !split && new Date(prev).getTime() >= stat.mtimeMs - 1000) {
226
+ // The block carries a fingerprint of the rest of the file; unchanged bullets
227
+ // mean an unchanged summary, whatever the clock says.
228
+ if (!opts.force && prev?.hash === fingerprint && !split) {
220
229
  const existing = content.slice(content.indexOf(NOW_START.replace("-->", "")), content.indexOf(NOW_END));
221
230
  return { slug, file: filePath, bullets: bullets.length, updated: false, now: existing.split("\n").slice(2).join("\n").trim() };
222
231
  }
@@ -225,7 +234,7 @@ export async function summarizeTopicFile(filePath, slug, opts = {}) {
225
234
  now = await proseNow(digest, bullets, AbortSignal.timeout(25_000));
226
235
  if (!now)
227
236
  now = structuralNow(digest);
228
- const next = upsertBlock(content, NOW_START, NOW_END, block(NOW_START, NOW_END, "## Now", now, stamp), "top");
237
+ const next = upsertBlock(content, NOW_START, NOW_END, block(NOW_START, NOW_END, "## Now", now, stamp, fingerprint), "top");
229
238
  if (next !== content)
230
239
  atomicWriteText(filePath, next);
231
240
  return { slug, file: filePath, bullets: bullets.length, updated: next !== content, split: split ?? undefined, now };
@@ -298,7 +307,7 @@ export async function summarizeProject(phrenPath, project, opts = {}) {
298
307
  const rendered = block(KNOWS_START, KNOWS_END, "## What phren knows", lines.join("\n"), stamp);
299
308
  const existing = fs.existsSync(summaryPath) ? fs.readFileSync(summaryPath, "utf8") : `# ${project}\n`;
300
309
  const next = upsertBlock(existing, KNOWS_START, KNOWS_END, rendered, "bottom");
301
- const stripStamp = (s) => s.replace(/ at=[^ ]+ -->/g, " -->");
310
+ const stripStamp = (s) => s.replace(/ at=[^ ]+( hash=[0-9a-f]+)? -->/g, " -->");
302
311
  const changed = stripStamp(next) !== stripStamp(existing);
303
312
  if (changed)
304
313
  atomicWriteText(summaryPath, next);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phren/cli",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Knowledge layer for AI agents — CLI, MCP server, and data layer",
5
5
  "type": "module",
6
6
  "bin": {