@phren/cli 0.1.34 → 0.1.35

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.
@@ -47,6 +47,12 @@ export declare function sessionMarker(phrenPath: string, name: string): string;
47
47
  export declare function debugLog(msg: string): void;
48
48
  /** Always-on structured error log (no PHREN_DEBUG gate). */
49
49
  export declare function errorLog(tool: string, msg: string): void;
50
+ /**
51
+ * Truncate an append-only .jsonl to its last `keepLines` lines once it exceeds
52
+ * `maxBytes`. Best-effort: any IO error (including ENOENT) is ignored so callers
53
+ * never fail on rotation.
54
+ */
55
+ export declare function rotateJsonlIfLarge(filePath: string, maxBytes?: number, keepLines?: number): void;
50
56
  export declare function appendIndexEvent(phrenPath: string, event: Record<string, unknown>): void;
51
57
  /** Resolve the canonical findings file for a project directory. */
52
58
  export declare function resolveFindingsPath(projectDir: string): string | undefined;
@@ -307,9 +307,26 @@ export function errorLog(tool, msg) {
307
307
  // Logging must never throw
308
308
  }
309
309
  }
310
+ /**
311
+ * Truncate an append-only .jsonl to its last `keepLines` lines once it exceeds
312
+ * `maxBytes`. Best-effort: any IO error (including ENOENT) is ignored so callers
313
+ * never fail on rotation.
314
+ */
315
+ export function rotateJsonlIfLarge(filePath, maxBytes = 2_000_000, keepLines = 2000) {
316
+ try {
317
+ if (fs.statSync(filePath).size <= maxBytes)
318
+ return;
319
+ const lines = fs.readFileSync(filePath, "utf-8").split("\n");
320
+ fs.writeFileSync(filePath, lines.slice(-keepLines).join("\n"));
321
+ }
322
+ catch {
323
+ // no file yet or IO error — nothing to rotate
324
+ }
325
+ }
310
326
  export function appendIndexEvent(phrenPath, event) {
311
327
  try {
312
328
  const file = runtimeFile(phrenPath, "index-events.jsonl");
329
+ rotateJsonlIfLarge(file);
313
330
  fs.appendFileSync(file, JSON.stringify({ at: new Date().toISOString(), ...event }) + "\n");
314
331
  }
315
332
  catch (err) {
@@ -23,6 +23,20 @@ export declare function normalizeIndexedContent(content: string, type: string, p
23
23
  */
24
24
  export declare function updateFileInIndex(db: SqlJsDatabase, filePath: string, phrenPath: string): void;
25
25
  export declare function buildIndex(phrenPath: string, profile?: string): Promise<SqlJsDatabase>;
26
+ /**
27
+ * Non-blocking index loader for the UserPromptSubmit / PostToolUse hooks.
28
+ * Unlike buildIndex(), this NEVER blocks a hook on a 2-12s rebuild:
29
+ * - exact stat-hash cache hit → serve it immediately (fast path);
30
+ * - miss but a usable (possibly stale) cache exists → serve the stale snapshot
31
+ * now and kick a *detached* `background-reindex` (deduped by the rebuild
32
+ * lock) so the next prompt gets fresh results. A single prompt's context may
33
+ * lag one write — far better than freezing the prompt;
34
+ * - cold start (no cache at all) → block once on a full build so the very first
35
+ * prompt isn't empty.
36
+ * Freshness stays correct over time: an edit bumps mtime → new stat-hash → this
37
+ * misses → a rebuild is scheduled → the following prompt hits the new cache.
38
+ */
39
+ export declare function loadIndexForHook(phrenPath: string, profile?: string): Promise<SqlJsDatabase>;
26
40
  /** Find the FTS cache file for a specific phrenPath+profile. Returns exists + size. */
27
41
  export declare function findFtsCacheForPath(phrenPath: string, profile?: string): {
28
42
  exists: boolean;
@@ -71,6 +71,7 @@ import { logger } from "../logger.js";
71
71
  import { formatActorAttribution, parseSourceComment } from "../content/citation.js";
72
72
  import { beginUserFragmentBuildCache, endUserFragmentBuildCache, extractAndLinkFragments, ensureGlobalEntitiesTable, } from "./fragment-graph.js";
73
73
  import { bootstrapSqlJs } from "./sqljs.js";
74
+ import { spawnDetachedChild } from "./process.js";
74
75
  import { getProjectOwnershipMode, getProjectSourcePath, readProjectConfig } from "../project-config.js";
75
76
  import { buildSourceDocKey, decodeStringRow, getDocSourceKey, queryDocRows, queryRows, } from "../index-query.js";
76
77
  import { classifyTopicForText, readProjectTopics, } from "../project-topics.js";
@@ -1433,6 +1434,78 @@ export async function buildIndex(phrenPath, profile) {
1433
1434
  _lastBuildKey = buildKey;
1434
1435
  return db;
1435
1436
  }
1437
+ /** Resolve the per-user FTS cache directory (os.tmpdir()/phren-fts-<uid>). */
1438
+ function ftsCacheDir() {
1439
+ let userSuffix;
1440
+ try {
1441
+ userSuffix = String(os.userInfo().uid);
1442
+ }
1443
+ catch (err) {
1444
+ logger.debug("ftsCacheDir userInfo", errorMessage(err));
1445
+ userSuffix = crypto.createHash("sha1").update(homeDir()).digest("hex").slice(0, 12);
1446
+ }
1447
+ return path.join(os.tmpdir(), `phren-fts-${userSuffix}`);
1448
+ }
1449
+ /**
1450
+ * Non-blocking index loader for the UserPromptSubmit / PostToolUse hooks.
1451
+ * Unlike buildIndex(), this NEVER blocks a hook on a 2-12s rebuild:
1452
+ * - exact stat-hash cache hit → serve it immediately (fast path);
1453
+ * - miss but a usable (possibly stale) cache exists → serve the stale snapshot
1454
+ * now and kick a *detached* `background-reindex` (deduped by the rebuild
1455
+ * lock) so the next prompt gets fresh results. A single prompt's context may
1456
+ * lag one write — far better than freezing the prompt;
1457
+ * - cold start (no cache at all) → block once on a full build so the very first
1458
+ * prompt isn't empty.
1459
+ * Freshness stays correct over time: an edit bumps mtime → new stat-hash → this
1460
+ * misses → a rebuild is scheduled → the following prompt hits the new cache.
1461
+ */
1462
+ export async function loadIndexForHook(phrenPath, profile) {
1463
+ const cacheDir = ftsCacheDir();
1464
+ const globResult = globAllFiles(phrenPath, profile);
1465
+ const hash = computePhrenHash(phrenPath, profile, globResult.filePaths);
1466
+ const cacheFile = path.join(cacheDir, `${hash}.db`);
1467
+ const SQL = await bootstrapSqlJs();
1468
+ // Fast path: exact stat-hash hit (no file changed mtime/size since the build).
1469
+ if (fs.existsSync(cacheFile)) {
1470
+ try {
1471
+ const db = new SQL.Database(fs.readFileSync(cacheFile));
1472
+ const docCount = db.exec("SELECT COUNT(*) FROM docs")?.[0]?.values?.[0]?.[0] ?? 0;
1473
+ if (docCount > 0) {
1474
+ appendIndexEvent(phrenPath, { event: "build_index", cache: "hit", hash: hash.slice(0, 12), elapsedMs: 0, profile: profile || "" });
1475
+ return db;
1476
+ }
1477
+ db.close();
1478
+ }
1479
+ catch (err) {
1480
+ debugLog(`loadIndexForHook fast-path load failed: ${errorMessage(err)}`);
1481
+ }
1482
+ }
1483
+ // Miss. Serve stale immediately if any cache exists; else cold-build once.
1484
+ let hasStale = false;
1485
+ try {
1486
+ hasStale = fs.existsSync(cacheDir) && fs.readdirSync(cacheDir).some(f => f.endsWith(".db"));
1487
+ }
1488
+ catch (err) {
1489
+ logger.debug("loadIndexForHook staleScan", errorMessage(err));
1490
+ }
1491
+ if (!hasStale) {
1492
+ return buildIndex(phrenPath, profile);
1493
+ }
1494
+ // Schedule a detached rebuild for next time (rebuild lock dedups concurrent hooks).
1495
+ if (!isRebuildLockHeld(phrenPath)) {
1496
+ const entry = process.argv[1];
1497
+ if (entry && /index\.(c?js|mjs|ts)$/.test(entry) && fs.existsSync(entry)) {
1498
+ try {
1499
+ spawnDetachedChild([entry, "background-reindex"], { phrenPath }).unref();
1500
+ debugLog(`loadIndexForHook: scheduled detached background-reindex (stat-hash miss ${hash.slice(0, 8)})`);
1501
+ }
1502
+ catch (err) {
1503
+ debugLog(`loadIndexForHook: detached reindex spawn failed: ${errorMessage(err)}`);
1504
+ }
1505
+ }
1506
+ }
1507
+ return loadIndexSnapshotOrEmpty(phrenPath, profile);
1508
+ }
1436
1509
  async function _buildIndexGuarded(phrenPath, profile) {
1437
1510
  const lockTarget = runtimeFile(phrenPath, "index-rebuild");
1438
1511
  if (isRebuildLockHeld(phrenPath)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phren/cli",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
4
4
  "description": "Knowledge layer for AI agents — CLI, MCP server, and data layer",
5
5
  "type": "module",
6
6
  "bin": {