@phren/cli 0.3.70 → 0.3.72

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 (108) hide show
  1. package/dist/bridge/browser-env.d.ts +5 -0
  2. package/dist/bridge/browser-env.js +86 -0
  3. package/dist/bridge/browser-protocol.d.ts +120 -0
  4. package/dist/bridge/browser-protocol.js +70 -0
  5. package/dist/bridge/browser-recordings.d.ts +63 -0
  6. package/dist/bridge/browser-recordings.js +148 -0
  7. package/dist/bridge/browser-registry.d.ts +64 -0
  8. package/dist/bridge/browser-registry.js +262 -0
  9. package/dist/bridge/browser-routes.d.ts +4 -0
  10. package/dist/bridge/browser-routes.js +39 -0
  11. package/dist/bridge/browser-shim/phren-playwright.cjs +184 -0
  12. package/dist/bridge/browser-socket.d.ts +2 -0
  13. package/dist/bridge/browser-socket.js +10 -0
  14. package/dist/bridge/browser-stream.d.ts +3 -0
  15. package/dist/bridge/browser-stream.js +129 -0
  16. package/dist/bridge/claude-question-dialog.js +32 -7
  17. package/dist/bridge/dispatch.d.ts +10 -0
  18. package/dist/bridge/dispatch.js +3 -1
  19. package/dist/bridge/dream-schedule.d.ts +133 -0
  20. package/dist/bridge/dream-schedule.js +322 -0
  21. package/dist/bridge/herdr.js +15 -6
  22. package/dist/bridge/install.js +4 -0
  23. package/dist/bridge/launch-brief.d.ts +11 -0
  24. package/dist/bridge/launch-brief.js +22 -0
  25. package/dist/bridge/pane-status.js +1 -1
  26. package/dist/bridge/server-routes.d.ts +3 -0
  27. package/dist/bridge/server-routes.js +15 -15
  28. package/dist/bridge/server-stream.d.ts +4 -0
  29. package/dist/bridge/server-stream.js +24 -5
  30. package/dist/bridge/server.js +23 -3
  31. package/dist/bridge/session-move.d.ts +10 -4
  32. package/dist/bridge/session-move.js +65 -34
  33. package/dist/bridge/simulator-stream.d.ts +2 -0
  34. package/dist/bridge/simulator-stream.js +2 -0
  35. package/dist/bridge/tab-activity.d.ts +2 -0
  36. package/dist/bridge/tab-activity.js +15 -2
  37. package/dist/bridge/task-routes.d.ts +20 -16
  38. package/dist/bridge/task-routes.js +1 -1
  39. package/dist/bridge/terminal-herdr.js +2 -1
  40. package/dist/bridge/terminal-tmux.js +2 -1
  41. package/dist/bridge/worker-reports.js +15 -3
  42. package/dist/bridge-hook.mjs +36819 -35219
  43. package/dist/capabilities/mcp.js +1 -1
  44. package/dist/cli/config-dreaming.d.ts +7 -0
  45. package/dist/cli/config-dreaming.js +72 -0
  46. package/dist/cli/config.js +6 -0
  47. package/dist/cli/dream.d.ts +59 -0
  48. package/dist/cli/dream.js +315 -0
  49. package/dist/cli/namespaces-tasks.js +14 -0
  50. package/dist/cli-registry.js +24 -1
  51. package/dist/config/dreaming.d.ts +82 -0
  52. package/dist/config/dreaming.js +195 -0
  53. package/dist/config/resolve.js +11 -0
  54. package/dist/config/schema.d.ts +4 -4
  55. package/dist/config/schema.js +157 -1
  56. package/dist/content/citation-freshness.js +6 -0
  57. package/dist/content/learning.d.ts +1 -0
  58. package/dist/content/learning.js +2 -0
  59. package/dist/data/access.d.ts +1 -1
  60. package/dist/data/access.js +3 -1
  61. package/dist/data/task-contract.d.ts +5 -4
  62. package/dist/data/tasks.d.ts +5 -0
  63. package/dist/data/tasks.js +28 -2
  64. package/dist/dream/apply.d.ts +69 -0
  65. package/dist/dream/apply.js +372 -0
  66. package/dist/dream/budget.d.ts +78 -0
  67. package/dist/dream/budget.js +180 -0
  68. package/dist/dream/collect.d.ts +86 -0
  69. package/dist/dream/collect.js +449 -0
  70. package/dist/dream/eval/dream-adapter.d.ts +16 -0
  71. package/dist/dream/eval/dream-adapter.js +21 -0
  72. package/dist/dream/eval/fixture.d.ts +53 -0
  73. package/dist/dream/eval/fixture.js +172 -0
  74. package/dist/dream/eval/index.d.ts +40 -0
  75. package/dist/dream/eval/index.js +78 -0
  76. package/dist/dream/eval/score.d.ts +34 -0
  77. package/dist/dream/eval/score.js +99 -0
  78. package/dist/dream/eval/scripted-model.d.ts +19 -0
  79. package/dist/dream/eval/scripted-model.js +70 -0
  80. package/dist/dream/pages.d.ts +68 -0
  81. package/dist/dream/pages.js +220 -0
  82. package/dist/dream/privacy.d.ts +3 -0
  83. package/dist/dream/privacy.js +18 -0
  84. package/dist/dream/prompt.d.ts +13 -0
  85. package/dist/dream/prompt.js +84 -0
  86. package/dist/dream/run.d.ts +98 -0
  87. package/dist/dream/run.js +317 -0
  88. package/dist/dream/types.d.ts +74 -0
  89. package/dist/dream/types.js +4 -0
  90. package/dist/finding/lifecycle.d.ts +2 -2
  91. package/dist/finding/lifecycle.js +5 -2
  92. package/dist/finding/versions.d.ts +4 -0
  93. package/dist/finding/versions.js +28 -0
  94. package/dist/index.js +1 -0
  95. package/dist/modules/registry.js +9 -7
  96. package/dist/project-config.d.ts +3 -0
  97. package/dist/task/merge-links.d.ts +38 -0
  98. package/dist/task/merge-links.js +45 -0
  99. package/dist/task/reconcile.d.ts +39 -0
  100. package/dist/task/reconcile.js +201 -0
  101. package/dist/task/report-links.d.ts +10 -0
  102. package/dist/task/report-links.js +47 -0
  103. package/dist/tools/config.js +46 -4
  104. package/dist/tools/dispatch.js +2 -1
  105. package/dist/tools/dream.d.ts +3 -0
  106. package/dist/tools/dream.js +123 -0
  107. package/dist/tools/tasks.js +4 -1
  108. package/package.json +1 -1
@@ -0,0 +1,180 @@
1
+ /**
2
+ * The injected layer's size, and what to demote when it is too big.
3
+ *
4
+ * What phren puts in front of every session for a project regardless of the
5
+ * prompt is (see `shared/retrieval.ts` and `cli/hooks.ts`):
6
+ * - the whole of the project's `truths.md` (canonical rows are prepended to
7
+ * every search for the detected project), and
8
+ * - the "What phren knows" block from `summary.md`, once per session, capped
9
+ * at 520 characters by the hook.
10
+ * Everything else the hook injects is chosen by relevance and already sits
11
+ * under its own token budget. Errors in the always-on part spread to every
12
+ * session, so dreaming keeps it under `dreaming.injectBudgetKB`.
13
+ */
14
+ import * as fs from "fs";
15
+ import { readKnowsBlock } from "../content/summarize.js";
16
+ import { lookupEventsLogFile } from "../phren-paths.js";
17
+ import { storeAwareProjectPath } from "../store-routing.js";
18
+ /** The hook trims the knows block to this many characters (cli/hooks.ts). */
19
+ const KNOWS_INJECT_CHARS = 520;
20
+ /** Read at most this much of the lookup log's tail when looking for citations. */
21
+ const LOOKUP_TAIL_BYTES = 4 * 1024 * 1024;
22
+ const ADDED_RE = /\s*_\(added (\d{4}-\d{2}-\d{2})\)_\s*$/;
23
+ function byteLength(text) {
24
+ return Buffer.byteLength(text, "utf8");
25
+ }
26
+ function normalize(text) {
27
+ return text.replace(/\s+/g, " ").trim().toLowerCase();
28
+ }
29
+ export function parseTruths(content) {
30
+ const truths = [];
31
+ for (const line of content.split("\n")) {
32
+ if (!line.startsWith("- "))
33
+ continue;
34
+ const raw = line.slice(2);
35
+ const added = raw.match(ADDED_RE)?.[1];
36
+ truths.push({ text: raw.replace(ADDED_RE, "").trim(), line, added, bytes: byteLength(line) + 1 });
37
+ }
38
+ return truths;
39
+ }
40
+ function truthsPath(store, project) {
41
+ return storeAwareProjectPath(store, project, "truths.md");
42
+ }
43
+ export function injectedSize(store, project) {
44
+ const file = truthsPath(store, project);
45
+ let content = "";
46
+ try {
47
+ if (file)
48
+ content = fs.readFileSync(file, "utf8");
49
+ }
50
+ catch {
51
+ content = "";
52
+ }
53
+ let knowsBytes = 0;
54
+ try {
55
+ const knows = readKnowsBlock(store, project);
56
+ if (knows) {
57
+ const text = knows.text.length > KNOWS_INJECT_CHARS ? `${knows.text.slice(0, KNOWS_INJECT_CHARS - 1)}…` : knows.text;
58
+ knowsBytes = byteLength(text);
59
+ }
60
+ }
61
+ catch {
62
+ knowsBytes = 0;
63
+ }
64
+ const truthsBytes = byteLength(content);
65
+ const bytes = truthsBytes + knowsBytes;
66
+ return { project, truthsBytes, knowsBytes, bytes, kb: Math.round((bytes / 1024) * 100) / 100, truths: parseTruths(content) };
67
+ }
68
+ /**
69
+ * Latest lookup-log time at which each truth showed up in a hit's snippet for
70
+ * this project. The always-inject path logs the whole truths file, so only
71
+ * search hits that matched a truth's own words are a useful signal; a truth
72
+ * nobody searched into stays uncited and is demoted first.
73
+ */
74
+ function lookupCitations(store, project, truths) {
75
+ const result = new Map();
76
+ const file = lookupEventsLogFile(store);
77
+ let text = "";
78
+ try {
79
+ const size = fs.statSync(file).size;
80
+ const start = Math.max(0, size - LOOKUP_TAIL_BYTES);
81
+ const fd = fs.openSync(file, "r");
82
+ try {
83
+ const buf = Buffer.alloc(size - start);
84
+ fs.readSync(fd, buf, 0, buf.length, start);
85
+ text = buf.toString("utf8");
86
+ }
87
+ finally {
88
+ fs.closeSync(fd);
89
+ }
90
+ }
91
+ catch {
92
+ return result;
93
+ }
94
+ const keys = truths.map((t) => ({ text: t.text, key: normalize(t.text).slice(0, 60) })).filter((k) => k.key.length >= 12);
95
+ for (const line of text.split("\n")) {
96
+ if (!line.includes(project))
97
+ continue;
98
+ let event;
99
+ try {
100
+ event = JSON.parse(line);
101
+ }
102
+ catch {
103
+ continue;
104
+ }
105
+ if (event.project !== project || event.type !== "canonical" || !event.at || !event.snippet)
106
+ continue;
107
+ // A snippet carrying most of the file is the always-inject path, not a citation.
108
+ const snippet = normalize(event.snippet);
109
+ const hits = keys.filter((k) => snippet.includes(k.key));
110
+ if (!hits.length || (keys.length > 2 && hits.length > keys.length / 2))
111
+ continue;
112
+ for (const hit of hits) {
113
+ const prev = result.get(hit.text);
114
+ if (!prev || event.at > prev)
115
+ result.set(hit.text, event.at);
116
+ }
117
+ }
118
+ return result;
119
+ }
120
+ function newer(a, b) {
121
+ if (!a)
122
+ return b;
123
+ if (!b)
124
+ return a;
125
+ return a > b ? a : b;
126
+ }
127
+ export function rankTruths(store, project, truths, opts = {}) {
128
+ const looked = opts.skipLookupLog ? new Map() : lookupCitations(store, project, truths);
129
+ const extra = opts.citedAt instanceof Map ? opts.citedAt : new Map(Object.entries(opts.citedAt ?? {}));
130
+ const extraByNorm = new Map([...extra].map(([k, v]) => [normalize(k), v]));
131
+ return truths
132
+ .map((t, index) => ({ t, index, lastCited: newer(newer(t.added, looked.get(t.text)), extraByNorm.get(normalize(t.text))) }))
133
+ .sort((a, b) => (a.lastCited ?? "").localeCompare(b.lastCited ?? "") || b.t.bytes - a.t.bytes || a.index - b.index)
134
+ .map(({ t, lastCited }) => ({ ...t, lastCited }));
135
+ }
136
+ export function overBudget(store, project, budgetKB, opts = {}) {
137
+ const size = injectedSize(store, project);
138
+ const budgetBytes = Math.max(0, Math.floor(budgetKB * 1024));
139
+ const ranking = rankTruths(store, project, size.truths, opts);
140
+ const excessBytes = Math.max(0, size.bytes - budgetBytes);
141
+ const candidates = [];
142
+ let freed = 0;
143
+ for (const truth of ranking) {
144
+ if (freed >= excessBytes)
145
+ break;
146
+ candidates.push(truth);
147
+ freed += truth.bytes;
148
+ }
149
+ return { project, budgetBytes, bytes: size.bytes, over: excessBytes > 0, excessBytes, candidates, ranking };
150
+ }
151
+ /**
152
+ * Bytes a new truth costs once `upsertCanonical` has written it: a new file is
153
+ * the header plus "- text _(added YYYY-MM-DD)_\n"; an existing one gets the
154
+ * line plus a blank-line separator inserted under "## Truths".
155
+ */
156
+ export function truthCost(project, text, fileExists) {
157
+ const line = byteLength(`- ${text.replace(/^- /, "")} _(added 0000-00-00)_`);
158
+ return fileExists ? line + 2 : byteLength(`# ${project} Truths\n\n## Truths\n\n`) + line + 1;
159
+ }
160
+ /**
161
+ * Whether promoting `text` to a truth keeps the injected layer under budget,
162
+ * counting the truths the same changeset demotes. `apply` refuses a
163
+ * promote_truth that does not fit.
164
+ */
165
+ export function checkPromotion(store, project, budgetKB, text, demoted = []) {
166
+ const size = injectedSize(store, project);
167
+ const budgetBytes = Math.max(0, Math.floor(budgetKB * 1024));
168
+ const gone = new Set(demoted.map((d) => normalize(d.replace(/^- /, "").replace(ADDED_RE, ""))));
169
+ const freed = size.truths.filter((t) => gone.has(normalize(t.text))).reduce((sum, t) => sum + t.bytes, 0);
170
+ const bytesAfter = size.bytes - freed + truthCost(project, text, size.truthsBytes > 0);
171
+ return { fits: bytesAfter <= budgetBytes, bytesBefore: size.bytes, bytesAfter, budgetBytes };
172
+ }
173
+ /** One line for the dreaming prompt: where the injected layer stands. */
174
+ export function describeBudget(state) {
175
+ const kb = (n) => (n / 1024).toFixed(1);
176
+ if (!state.over)
177
+ return `Injected layer: ${kb(state.bytes)} KB of ${kb(state.budgetBytes)} KB budget (${state.ranking.length} truths).`;
178
+ return `Injected layer: ${kb(state.bytes)} KB, over the ${kb(state.budgetBytes)} KB budget by ${kb(state.excessBytes)} KB. `
179
+ + `Least recently cited truths: ${state.candidates.map((c) => JSON.stringify(c.text)).join("; ")}.`;
180
+ }
@@ -0,0 +1,86 @@
1
+ export type DreamTranscriptSource = "claude" | "codex" | "copilot" | "opencode";
2
+ export declare const DEFAULT_TRANSCRIPT_BUDGET_CHARS = 120000;
3
+ export interface DreamTranscriptRoot {
4
+ source: DreamTranscriptSource;
5
+ root: string;
6
+ maxDepth: number;
7
+ }
8
+ export interface DreamTranscript {
9
+ source: DreamTranscriptSource;
10
+ session: string;
11
+ cwd: string;
12
+ first: string;
13
+ last: string;
14
+ turns: number;
15
+ /** Trimmed rendering: `[t<n>] user|assistant|tools: ...` lines. */
16
+ text: string;
17
+ trimmed?: true;
18
+ }
19
+ export interface DreamFindingInput {
20
+ fid: string;
21
+ date: string;
22
+ text: string;
23
+ status: string;
24
+ citation?: string;
25
+ }
26
+ export interface DreamPageInput {
27
+ slug: string;
28
+ title: string;
29
+ summary: string;
30
+ }
31
+ export interface DreamBudgetState {
32
+ sizeBytes: number;
33
+ budgetBytes: number;
34
+ over: boolean;
35
+ candidates: string[];
36
+ }
37
+ export interface DreamInputs {
38
+ project: string;
39
+ from: string;
40
+ to: string;
41
+ repoDir?: string;
42
+ transcripts: DreamTranscript[];
43
+ transcriptChars: number;
44
+ /** Sessions in the window left out entirely for budget. */
45
+ transcriptsDropped: number;
46
+ /** Unattributed OpenCode sessions hold the watermark back. */
47
+ unresolvedTranscripts?: number;
48
+ findings: DreamFindingInput[];
49
+ truths: string[];
50
+ topics: Array<{
51
+ slug: string;
52
+ label: string;
53
+ }>;
54
+ pages: DreamPageInput[];
55
+ gitLog: string[];
56
+ journal: string[];
57
+ budget: DreamBudgetState;
58
+ }
59
+ export interface CollectOptions {
60
+ from: Date;
61
+ to: Date;
62
+ budgetChars?: number;
63
+ injectBudgetKB?: number;
64
+ /** Transcript locations; defaults to every harness Phren reads on this computer. */
65
+ roots?: DreamTranscriptRoot[];
66
+ /** Folders whose sessions belong to the project; defaults to its source path and that repo's worktrees. */
67
+ projectDirs?: string[];
68
+ /** Replace the git reader (tests); false skips git. */
69
+ gitLog?: false | ((dir: string) => string[]);
70
+ }
71
+ export declare function defaultTranscriptRoots(): DreamTranscriptRoot[];
72
+ export declare function projectStoreDir(phrenPath: string, project: string): string;
73
+ export declare function underDirs(cwd: string, dirs: string[]): boolean;
74
+ /** One transcript trimmed to the window, or undefined when it is not the project's or has nothing in the window. */
75
+ export declare function readTranscript(source: DreamTranscriptSource, file: string, dirs: string[], from: number, to: number, metadataCwd?: string): DreamTranscript | undefined;
76
+ /** Fit transcripts into the budget, newest session first; the oldest turns of a session are trimmed before it is dropped. */
77
+ export declare function budgetTranscripts(all: DreamTranscript[], budgetChars: number): {
78
+ kept: DreamTranscript[];
79
+ chars: number;
80
+ dropped: number;
81
+ };
82
+ /** The repo's own folder plus its other worktrees, where agents also work on this project. */
83
+ export declare function projectDirsFor(phrenPath: string, project: string): string[];
84
+ export declare function readTruths(phrenPath: string, project: string): string[];
85
+ export declare function journalFile(phrenPath: string, project: string): string;
86
+ export declare function collect(phrenPath: string, project: string, options: CollectOptions): Promise<DreamInputs>;