@phren/cli 0.3.49 → 0.3.50

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.
@@ -1,3 +1,4 @@
1
+ import { type CitationFreshness } from "../content/citation-freshness.js";
1
2
  import { type FindingItem } from "../data/access.js";
2
3
  export declare const PROJECT_MEMORY_ROUTE: RegExp;
3
4
  export declare const MEMORY_LIMITS: {
@@ -12,6 +13,7 @@ export interface MemoryFinding {
12
13
  type: string | null;
13
14
  status: FindingItem["status"];
14
15
  created: string | null;
16
+ citationFreshness?: CitationFreshness;
15
17
  citation: {
16
18
  file: string | null;
17
19
  line: number | null;
@@ -1,3 +1,4 @@
1
+ import { cachedCitationFreshness, refreshCitationFreshness } from "../content/citation-freshness.js";
1
2
  import * as fs from "node:fs";
2
3
  import * as path from "node:path";
3
4
  import { readFindings } from "../data/access.js";
@@ -24,7 +25,11 @@ const TAG = new RegExp(`^\\[(${FINDING_TAGS.join("|")})\\]\\s*`, "i");
24
25
  const DATE = /^\d{4}-\d{2}-\d{2}/;
25
26
  /** Text that trips phren's credential detector is withheld whole, never cut. */
26
27
  const safe = (text) => redactSecretsForLog(text);
27
- function finding(item) {
28
+ function freshnessOptions(root, project) {
29
+ return { phrenPath: root, projectRemote: getProjectRemote(root, project, readProjectConfig(root, project)) };
30
+ }
31
+ function finding(item, opts) {
32
+ const citationFreshness = item.citationData ? cachedCitationFreshness(item.citationData, opts) : undefined;
28
33
  const tag = TAG.exec(item.text);
29
34
  const cite = item.citationData;
30
35
  const citation = cite && (cite.file || cite.line || cite.commit || cite.symbol)
@@ -36,6 +41,7 @@ function finding(item) {
36
41
  text: safe(tag ? item.text.slice(tag[0].length) : item.text),
37
42
  type: tag ? tag[1].toLowerCase() : null,
38
43
  status: item.status, created, citation,
44
+ ...(citationFreshness ? { citationFreshness } : {}),
39
45
  };
40
46
  }
41
47
  function task(item) {
@@ -66,10 +72,14 @@ function locate(base, rawProject) {
66
72
  return { project, root, storeId };
67
73
  }
68
74
  /** Newest first, so a cap drops the oldest. Undated findings sort last. */
69
- function sortedFindings(root, project) {
75
+ function sortedFindings(root, project, opts) {
70
76
  const result = readFindings(root, project);
71
77
  if (!result.ok)
72
78
  throw new BridgeError(404, "Project not found.");
79
+ const citations = result.data.flatMap(item => item.citationData ? [item.citationData] : []);
80
+ // One batch per poll, after the response has been shaped. Refresh deduplicates overlapping polls.
81
+ if (citations.length)
82
+ setImmediate(() => { void refreshCitationFreshness(citations, { ...opts, budgetMs: 1500 }); });
73
83
  return result.data.map((item, index) => ({ item, index, date: DATE.test(item.date) ? item.date : "" }))
74
84
  .sort((a, b) => b.date.localeCompare(a.date) || a.index - b.index).map(entry => entry.item);
75
85
  }
@@ -79,7 +89,8 @@ function readTruths(root, project) {
79
89
  }
80
90
  export function projectMemory(base, rawProject) {
81
91
  const { project, root, storeId } = locate(base, rawProject);
82
- const allFindings = sortedFindings(root, project);
92
+ const opts = freshnessOptions(root, project);
93
+ const allFindings = sortedFindings(root, project, opts);
83
94
  const truths = readTruths(root, project).map(text => ({ text: safe(text) }));
84
95
  // Tasks are their own module; with it off the sections are empty.
85
96
  const taskDoc = readTasks(root, project);
@@ -87,8 +98,8 @@ export function projectMemory(base, rawProject) {
87
98
  const truncated = allFindings.length > MEMORY_LIMITS.findings || items.Active.length > MEMORY_LIMITS.active
88
99
  || items.Queue.length > MEMORY_LIMITS.queue || items.Done.length > MEMORY_LIMITS.done;
89
100
  return {
90
- project, store_id: storeId, remote: getProjectRemote(root, project, readProjectConfig(root, project)), truncated,
91
- findings: allFindings.slice(0, MEMORY_LIMITS.findings).map(finding),
101
+ project, store_id: storeId, remote: opts.projectRemote ?? null, truncated,
102
+ findings: allFindings.slice(0, MEMORY_LIMITS.findings).map(item => finding(item, opts)),
92
103
  truths,
93
104
  tasks: {
94
105
  active: items.Active.slice(0, MEMORY_LIMITS.active).map(task),
@@ -102,11 +113,12 @@ export function projectMemory(base, rawProject) {
102
113
  * type or variable the code index places in one of them. */
103
114
  export async function memoryForFiles(base, rawProject, paths) {
104
115
  const { project, root, storeId } = locate(base, rawProject);
116
+ const opts = freshnessOptions(root, project);
105
117
  if (paths.length < 1 || paths.length > MAX_FILE_PATHS || !paths.every(isRepoPath)) {
106
118
  throw new BridgeError(400, `Send 1 to ${MAX_FILE_PATHS} repository-relative paths.`);
107
119
  }
108
120
  const wanted = new Set(paths);
109
- const all = sortedFindings(root, project);
121
+ const all = sortedFindings(root, project, opts);
110
122
  const citedFile = (item) => item.citationData?.file?.replace(/^\.\//, "");
111
123
  const named = all.filter(item => item.citationData?.symbol && !wanted.has(citedFile(item) ?? ""));
112
124
  let symbols = "unavailable";
@@ -125,9 +137,9 @@ export async function memoryForFiles(base, rawProject, paths) {
125
137
  for (const item of all) {
126
138
  const file = citedFile(item), name = item.citationData?.symbol;
127
139
  if (file && wanted.has(file))
128
- matches.push({ ...finding(item), match: "file" });
140
+ matches.push({ ...finding(item, opts), match: "file" });
129
141
  else if (name && files[name] && wanted.has(files[name]))
130
- matches.push({ ...finding(item), match: "symbol" });
142
+ matches.push({ ...finding(item, opts), match: "symbol" });
131
143
  }
132
144
  return { project, store_id: storeId, symbols, truncated: matches.length > MEMORY_LIMITS.findings,
133
145
  findings: matches.slice(0, MEMORY_LIMITS.findings) };
@@ -142,12 +154,13 @@ export async function memoryForFiles(base, rawProject, paths) {
142
154
  */
143
155
  export function memorySearch(base, rawProject, q, limit) {
144
156
  const { project, root } = locate(base, rawProject);
157
+ const opts = freshnessOptions(root, project);
145
158
  if (!isSearchText(q))
146
159
  throw new BridgeError(400, "q must be 1 to 1000 characters of text.");
147
160
  if (!Number.isInteger(limit) || limit < 1 || limit > MAX_SEARCH_LIMIT)
148
161
  throw new BridgeError(400, `limit must be 1 to ${MAX_SEARCH_LIMIT}.`);
149
162
  const entries = [
150
- ...sortedFindings(root, project).map(item => ({ kind: "finding", text: item.text, item })),
163
+ ...sortedFindings(root, project, opts).map(item => ({ kind: "finding", text: item.text, item })),
151
164
  ...readTruths(root, project).map(text => ({ kind: "truth", text, item: undefined })),
152
165
  ];
153
166
  const tokens = tokenizeForOverlap(q, 64);
@@ -170,7 +183,7 @@ export function memorySearch(base, rawProject, q, limit) {
170
183
  continue;
171
184
  const inactive = entry.item && entry.item.status !== "active";
172
185
  const score = Math.round((best.rarity / weight) * (inactive ? 0.5 : 1) * 1000) / 1000;
173
- const shaped = entry.item ? finding(entry.item) : null;
186
+ const shaped = entry.item ? finding(entry.item, opts) : null;
174
187
  hits.push({ kind: entry.kind, score, matched: best.distinct, text: shaped ? shaped.text : safe(entry.text), finding: shaped });
175
188
  }
176
189
  hits.sort((a, b) => b.score - a.score || b.matched - a.matched);
@@ -209,7 +222,8 @@ let writes = Promise.resolve();
209
222
  * project's write permission, duplicate and conflict checks, the secret
210
223
  * scan and citation validation against the code index. */
211
224
  export async function saveFinding(base, rawProject, input) {
212
- const { project } = locate(base, rawProject);
225
+ const { project, root } = locate(base, rawProject);
226
+ const opts = freshnessOptions(root, project);
213
227
  if (Buffer.byteLength(JSON.stringify(input ?? null)) > MAX_FINDING_BODY)
214
228
  throw new BridgeError(413, `The body must be at most ${MAX_FINDING_BODY} bytes.`);
215
229
  const body = findingWriteSchema.parse(input);
@@ -229,9 +243,9 @@ export async function saveFinding(base, rawProject, input) {
229
243
  throw new BridgeError(/permission|denied|not allowed/i.test(error) ? 403 : 400, safe(error));
230
244
  }
231
245
  const status = result.data?.status === "skipped" ? "duplicate" : "saved";
232
- const saved = status === "saved" ? sortedFindings(locate(base, rawProject).root, project)
246
+ const saved = status === "saved" ? sortedFindings(root, project, opts)
233
247
  .find(item => item.text === (result.data?.finding ?? "").replace(/^-\s+/, "").trim()) : undefined;
234
- return { project, status, finding: saved ? finding(saved) : null };
248
+ return { project, status, finding: saved ? finding(saved, opts) : null };
235
249
  }
236
250
  // ── POST /tasks/complete ─────────────────────────────────────────────────────
237
251
  /** Mark one open task done, by the stable id `/tasks?branch=` gave gitboy when