@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.
- package/dist/bridge/browser-env.d.ts +5 -0
- package/dist/bridge/browser-env.js +86 -0
- package/dist/bridge/browser-protocol.d.ts +120 -0
- package/dist/bridge/browser-protocol.js +70 -0
- package/dist/bridge/browser-recordings.d.ts +63 -0
- package/dist/bridge/browser-recordings.js +148 -0
- package/dist/bridge/browser-registry.d.ts +64 -0
- package/dist/bridge/browser-registry.js +262 -0
- package/dist/bridge/browser-routes.d.ts +4 -0
- package/dist/bridge/browser-routes.js +39 -0
- package/dist/bridge/browser-shim/phren-playwright.cjs +184 -0
- package/dist/bridge/browser-socket.d.ts +2 -0
- package/dist/bridge/browser-socket.js +10 -0
- package/dist/bridge/browser-stream.d.ts +3 -0
- package/dist/bridge/browser-stream.js +129 -0
- package/dist/bridge/claude-question-dialog.js +32 -7
- package/dist/bridge/dispatch.d.ts +10 -0
- package/dist/bridge/dispatch.js +3 -1
- package/dist/bridge/dream-schedule.d.ts +133 -0
- package/dist/bridge/dream-schedule.js +322 -0
- package/dist/bridge/herdr.js +15 -6
- package/dist/bridge/install.js +4 -0
- package/dist/bridge/launch-brief.d.ts +11 -0
- package/dist/bridge/launch-brief.js +22 -0
- package/dist/bridge/pane-status.js +1 -1
- package/dist/bridge/server-routes.d.ts +3 -0
- package/dist/bridge/server-routes.js +15 -15
- package/dist/bridge/server-stream.d.ts +4 -0
- package/dist/bridge/server-stream.js +24 -5
- package/dist/bridge/server.js +23 -3
- package/dist/bridge/session-move.d.ts +10 -4
- package/dist/bridge/session-move.js +65 -34
- package/dist/bridge/simulator-stream.d.ts +2 -0
- package/dist/bridge/simulator-stream.js +2 -0
- package/dist/bridge/tab-activity.d.ts +2 -0
- package/dist/bridge/tab-activity.js +15 -2
- package/dist/bridge/task-routes.d.ts +20 -16
- package/dist/bridge/task-routes.js +1 -1
- package/dist/bridge/terminal-herdr.js +2 -1
- package/dist/bridge/terminal-tmux.js +2 -1
- package/dist/bridge/worker-reports.js +15 -3
- package/dist/bridge-hook.mjs +36819 -35219
- package/dist/capabilities/mcp.js +1 -1
- package/dist/cli/config-dreaming.d.ts +7 -0
- package/dist/cli/config-dreaming.js +72 -0
- package/dist/cli/config.js +6 -0
- package/dist/cli/dream.d.ts +59 -0
- package/dist/cli/dream.js +315 -0
- package/dist/cli/namespaces-tasks.js +14 -0
- package/dist/cli-registry.js +24 -1
- package/dist/config/dreaming.d.ts +82 -0
- package/dist/config/dreaming.js +195 -0
- package/dist/config/resolve.js +11 -0
- package/dist/config/schema.d.ts +4 -4
- package/dist/config/schema.js +157 -1
- package/dist/content/citation-freshness.js +6 -0
- package/dist/content/learning.d.ts +1 -0
- package/dist/content/learning.js +2 -0
- package/dist/data/access.d.ts +1 -1
- package/dist/data/access.js +3 -1
- package/dist/data/task-contract.d.ts +5 -4
- package/dist/data/tasks.d.ts +5 -0
- package/dist/data/tasks.js +28 -2
- package/dist/dream/apply.d.ts +69 -0
- package/dist/dream/apply.js +372 -0
- package/dist/dream/budget.d.ts +78 -0
- package/dist/dream/budget.js +180 -0
- package/dist/dream/collect.d.ts +86 -0
- package/dist/dream/collect.js +449 -0
- package/dist/dream/eval/dream-adapter.d.ts +16 -0
- package/dist/dream/eval/dream-adapter.js +21 -0
- package/dist/dream/eval/fixture.d.ts +53 -0
- package/dist/dream/eval/fixture.js +172 -0
- package/dist/dream/eval/index.d.ts +40 -0
- package/dist/dream/eval/index.js +78 -0
- package/dist/dream/eval/score.d.ts +34 -0
- package/dist/dream/eval/score.js +99 -0
- package/dist/dream/eval/scripted-model.d.ts +19 -0
- package/dist/dream/eval/scripted-model.js +70 -0
- package/dist/dream/pages.d.ts +68 -0
- package/dist/dream/pages.js +220 -0
- package/dist/dream/privacy.d.ts +3 -0
- package/dist/dream/privacy.js +18 -0
- package/dist/dream/prompt.d.ts +13 -0
- package/dist/dream/prompt.js +84 -0
- package/dist/dream/run.d.ts +98 -0
- package/dist/dream/run.js +317 -0
- package/dist/dream/types.d.ts +74 -0
- package/dist/dream/types.js +4 -0
- package/dist/finding/lifecycle.d.ts +2 -2
- package/dist/finding/lifecycle.js +5 -2
- package/dist/finding/versions.d.ts +4 -0
- package/dist/finding/versions.js +28 -0
- package/dist/index.js +1 -0
- package/dist/modules/registry.js +9 -7
- package/dist/project-config.d.ts +3 -0
- package/dist/task/merge-links.d.ts +38 -0
- package/dist/task/merge-links.js +45 -0
- package/dist/task/reconcile.d.ts +39 -0
- package/dist/task/reconcile.js +201 -0
- package/dist/task/report-links.d.ts +10 -0
- package/dist/task/report-links.js +47 -0
- package/dist/tools/config.js +46 -4
- package/dist/tools/dispatch.js +2 -1
- package/dist/tools/dream.d.ts +3 -0
- package/dist/tools/dream.js +123 -0
- package/dist/tools/tasks.js +4 -1
- package/package.json +1 -1
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
// Dream inputs: what actually happened in a project's sessions during the lookback window
|
|
2
|
+
// (trimmed transcripts from every harness Phren reads), next to what the project's memory
|
|
3
|
+
// currently says (findings with fids, truths, topics, pages), the repo's recent git log and
|
|
4
|
+
// the dream journal. Transcripts are budgeted newest first so a busy day can't crowd out
|
|
5
|
+
// the memory the model is meant to correct.
|
|
6
|
+
import { execFileSync } from "child_process";
|
|
7
|
+
import * as fs from "fs";
|
|
8
|
+
import * as path from "path";
|
|
9
|
+
import { claudeHomes } from "../bridge/claude-accounts.js";
|
|
10
|
+
import { materializedRoot } from "../bridge/codex-threads.js";
|
|
11
|
+
import { phrenStoreRoot } from "../bridge/transcripts.js";
|
|
12
|
+
import { redactSecretsForLog } from "../content/secrets.js";
|
|
13
|
+
import { readFindings } from "../data/access.js";
|
|
14
|
+
import { codexHome, copilotHome, homeDir } from "../home-paths.js";
|
|
15
|
+
import { getProjectSourcePath } from "../project-config.js";
|
|
16
|
+
import { readProjectTopics } from "../project-topics.js";
|
|
17
|
+
import { storeAwareProjectPath } from "../store-routing.js";
|
|
18
|
+
import { overBudget } from "./budget.js";
|
|
19
|
+
export const DEFAULT_TRANSCRIPT_BUDGET_CHARS = 120_000;
|
|
20
|
+
/** Bytes read from the end of a transcript; older turns of a huge file are outside any lookback anyway. */
|
|
21
|
+
const TAIL_BYTES = 8 * 1024 * 1024;
|
|
22
|
+
/** Bytes read from the start to find a session's cwd (Codex writes it once, in session_meta). */
|
|
23
|
+
const HEAD_BYTES = 64 * 1024;
|
|
24
|
+
const MESSAGE_MAX_CHARS = 1_500;
|
|
25
|
+
/** A session gets trimmed rather than dropped while at least this much budget remains. */
|
|
26
|
+
const MIN_PARTIAL_CHARS = 2_000;
|
|
27
|
+
const MAX_FILES = 400;
|
|
28
|
+
const JOURNAL_LINES = 20;
|
|
29
|
+
const GIT_LOG_COUNT = 20;
|
|
30
|
+
export function defaultTranscriptRoots() {
|
|
31
|
+
const roots = claudeHomes().map(home => ({ source: "claude", root: path.join(home.dir, "projects"), maxDepth: 1 }));
|
|
32
|
+
roots.push({ source: "codex", root: path.join(codexHome(), "sessions"), maxDepth: 3 });
|
|
33
|
+
roots.push({ source: "codex", root: materializedRoot(), maxDepth: 0 });
|
|
34
|
+
roots.push({ source: "copilot", root: path.join(copilotHome(), "session-state"), maxDepth: 1 });
|
|
35
|
+
roots.push({ source: "opencode", root: path.join(phrenStoreRoot(), ".runtime", "sessions"), maxDepth: 0 });
|
|
36
|
+
return roots;
|
|
37
|
+
}
|
|
38
|
+
export function projectStoreDir(phrenPath, project) {
|
|
39
|
+
return storeAwareProjectPath(phrenPath, project) ?? path.join(phrenPath, project);
|
|
40
|
+
}
|
|
41
|
+
const obj = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
42
|
+
const objs = (value) => Array.isArray(value) ? value.map(obj) : [];
|
|
43
|
+
const absolute = (value) => typeof value === "string" && value.length > 0 && value.length <= 4096 && path.isAbsolute(value) ? value : undefined;
|
|
44
|
+
function blocksText(content) {
|
|
45
|
+
if (typeof content === "string")
|
|
46
|
+
return content;
|
|
47
|
+
return objs(content).filter(block => ["text", "input_text", "output_text"].includes(String(block.type)) && typeof block.text === "string")
|
|
48
|
+
.map(block => block.text).join("\n");
|
|
49
|
+
}
|
|
50
|
+
/** Harness scaffolding that is not something the person or the agent said. */
|
|
51
|
+
function cleanText(text) {
|
|
52
|
+
const stripped = text.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "").trim();
|
|
53
|
+
if (/^<(environment_context|user_instructions|local-command-stdout|command-name|command-message)\b/.test(stripped))
|
|
54
|
+
return "";
|
|
55
|
+
if (stripped.startsWith("# AGENTS.md instructions"))
|
|
56
|
+
return "";
|
|
57
|
+
return stripped;
|
|
58
|
+
}
|
|
59
|
+
function messageOf(raw, source) {
|
|
60
|
+
let role, content;
|
|
61
|
+
if (source === "claude") {
|
|
62
|
+
if (raw.isCompactSummary === true || raw.isMeta === true)
|
|
63
|
+
return undefined;
|
|
64
|
+
const message = obj(raw.message);
|
|
65
|
+
role = message.role;
|
|
66
|
+
content = message.content;
|
|
67
|
+
}
|
|
68
|
+
else if (source === "codex") {
|
|
69
|
+
const payload = obj(raw.payload);
|
|
70
|
+
if (payload.type !== "message")
|
|
71
|
+
return undefined;
|
|
72
|
+
role = payload.role;
|
|
73
|
+
content = payload.content;
|
|
74
|
+
}
|
|
75
|
+
else if (source === "copilot") {
|
|
76
|
+
if (raw.type === "user.message")
|
|
77
|
+
role = "user";
|
|
78
|
+
else if (raw.type === "assistant.message")
|
|
79
|
+
role = "assistant";
|
|
80
|
+
else
|
|
81
|
+
return undefined;
|
|
82
|
+
content = obj(raw.data).content;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
if (raw.type === "user/message")
|
|
86
|
+
role = "user";
|
|
87
|
+
else if (raw.type === "assistant/message")
|
|
88
|
+
role = "assistant";
|
|
89
|
+
else
|
|
90
|
+
return undefined;
|
|
91
|
+
const message = obj(obj(raw.data).message);
|
|
92
|
+
role = message.role ?? role;
|
|
93
|
+
content = message.content;
|
|
94
|
+
}
|
|
95
|
+
if (role !== "user" && role !== "assistant")
|
|
96
|
+
return undefined;
|
|
97
|
+
const text = cleanText(blocksText(content));
|
|
98
|
+
return text ? { role, text } : undefined;
|
|
99
|
+
}
|
|
100
|
+
/** Tool names a row starts; the tool's input and output bodies are never kept. */
|
|
101
|
+
function toolsOf(raw, source) {
|
|
102
|
+
if (source === "claude") {
|
|
103
|
+
return objs(obj(raw.message).content).filter(block => block.type === "tool_use" && typeof block.name === "string").map(block => block.name);
|
|
104
|
+
}
|
|
105
|
+
if (source === "codex") {
|
|
106
|
+
const payload = obj(raw.payload);
|
|
107
|
+
if (["function_call", "custom_tool_call", "local_shell_call"].includes(String(payload.type)))
|
|
108
|
+
return [typeof payload.name === "string" ? payload.name : String(payload.type)];
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
if (source === "copilot") {
|
|
112
|
+
const name = obj(raw.data).toolName;
|
|
113
|
+
return raw.type === "tool.execution_start" && typeof name === "string" ? [name] : [];
|
|
114
|
+
}
|
|
115
|
+
const tool = obj(obj(raw.data).part);
|
|
116
|
+
return tool.type === "tool" && typeof tool.tool === "string" ? [tool.tool] : [];
|
|
117
|
+
}
|
|
118
|
+
function timestampOf(raw, source) {
|
|
119
|
+
const value = source === "opencode" ? raw.time ?? raw.timestamp : raw.timestamp;
|
|
120
|
+
const parsed = typeof value === "string" ? Date.parse(value) : typeof value === "number" ? value : NaN;
|
|
121
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
122
|
+
}
|
|
123
|
+
function cwdOf(raw, source) {
|
|
124
|
+
const direct = absolute(raw.cwd);
|
|
125
|
+
if (direct)
|
|
126
|
+
return direct;
|
|
127
|
+
const payload = obj(raw.payload), data = obj(raw.data);
|
|
128
|
+
if (source === "copilot")
|
|
129
|
+
return absolute(obj(data.context).cwd);
|
|
130
|
+
return absolute(payload.cwd) ?? absolute(data.cwd) ?? absolute(obj(data.context).cwd);
|
|
131
|
+
}
|
|
132
|
+
function sessionOf(source, file) {
|
|
133
|
+
const name = path.basename(file, ".jsonl");
|
|
134
|
+
if (source === "copilot")
|
|
135
|
+
return path.basename(path.dirname(file));
|
|
136
|
+
if (source === "opencode")
|
|
137
|
+
return name.replace(/^opencode-/, "").replace(/\.events$/, "");
|
|
138
|
+
const rollout = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i.exec(name);
|
|
139
|
+
return rollout ? rollout[1] : name;
|
|
140
|
+
}
|
|
141
|
+
function cap(text, max) {
|
|
142
|
+
if (text.length <= max)
|
|
143
|
+
return text;
|
|
144
|
+
const half = Math.floor((max - 5) / 2);
|
|
145
|
+
return `${text.slice(0, half)} […] ${text.slice(-half)}`;
|
|
146
|
+
}
|
|
147
|
+
function readChunk(file, start, length) {
|
|
148
|
+
const fd = fs.openSync(file, "r");
|
|
149
|
+
try {
|
|
150
|
+
const buffer = Buffer.alloc(length);
|
|
151
|
+
const read = fs.readSync(fd, buffer, 0, length, start);
|
|
152
|
+
return buffer.subarray(0, read).toString("utf8");
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
fs.closeSync(fd);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function parseLines(text, dropFirst) {
|
|
159
|
+
const lines = text.split("\n");
|
|
160
|
+
if (dropFirst)
|
|
161
|
+
lines.shift();
|
|
162
|
+
const rows = [];
|
|
163
|
+
for (const line of lines) {
|
|
164
|
+
if (!line.trim())
|
|
165
|
+
continue;
|
|
166
|
+
try {
|
|
167
|
+
rows.push(obj(JSON.parse(line)));
|
|
168
|
+
}
|
|
169
|
+
catch { /* A partial or foreign line. */ }
|
|
170
|
+
}
|
|
171
|
+
return rows;
|
|
172
|
+
}
|
|
173
|
+
export function underDirs(cwd, dirs) {
|
|
174
|
+
const resolved = path.resolve(cwd);
|
|
175
|
+
return dirs.some(dir => resolved === dir || resolved.startsWith(dir.endsWith(path.sep) ? dir : dir + path.sep));
|
|
176
|
+
}
|
|
177
|
+
/** One transcript trimmed to the window, or undefined when it is not the project's or has nothing in the window. */
|
|
178
|
+
export function readTranscript(source, file, dirs, from, to, metadataCwd) {
|
|
179
|
+
let size;
|
|
180
|
+
try {
|
|
181
|
+
size = fs.statSync(file).size;
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
if (size === 0)
|
|
187
|
+
return undefined;
|
|
188
|
+
const whole = size <= TAIL_BYTES;
|
|
189
|
+
const tailRows = parseLines(readChunk(file, whole ? 0 : size - TAIL_BYTES, whole ? size : TAIL_BYTES), !whole);
|
|
190
|
+
let cwd;
|
|
191
|
+
for (const row of whole ? tailRows : parseLines(readChunk(file, 0, Math.min(HEAD_BYTES, size)), false)) {
|
|
192
|
+
cwd = cwdOf(row, source);
|
|
193
|
+
if (cwd)
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
if (!cwd)
|
|
197
|
+
for (const row of tailRows) {
|
|
198
|
+
cwd = cwdOf(row, source);
|
|
199
|
+
if (cwd)
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
cwd ??= metadataCwd;
|
|
203
|
+
if (!cwd || !underDirs(cwd, dirs))
|
|
204
|
+
return undefined;
|
|
205
|
+
const lines = [];
|
|
206
|
+
let turn = 0, first, last;
|
|
207
|
+
let pendingTools = [];
|
|
208
|
+
const flushTools = () => {
|
|
209
|
+
if (!pendingTools.length)
|
|
210
|
+
return;
|
|
211
|
+
const counts = new Map();
|
|
212
|
+
for (const name of pendingTools)
|
|
213
|
+
counts.set(name, (counts.get(name) ?? 0) + 1);
|
|
214
|
+
lines.push(`[t${turn}] tools: ${[...counts].map(([name, n]) => n > 1 ? `${name}×${n}` : name).join(", ")}`);
|
|
215
|
+
pendingTools = [];
|
|
216
|
+
};
|
|
217
|
+
for (const row of tailRows) {
|
|
218
|
+
const at = timestampOf(row, source);
|
|
219
|
+
if (at !== undefined && (at < from || at > to))
|
|
220
|
+
continue;
|
|
221
|
+
const message = messageOf(row, source);
|
|
222
|
+
const tools = toolsOf(row, source);
|
|
223
|
+
if (!message && !tools.length)
|
|
224
|
+
continue;
|
|
225
|
+
if (at !== undefined) {
|
|
226
|
+
first ??= at;
|
|
227
|
+
last = at;
|
|
228
|
+
}
|
|
229
|
+
if (message) {
|
|
230
|
+
flushTools();
|
|
231
|
+
if (message.role === "user")
|
|
232
|
+
turn++;
|
|
233
|
+
lines.push(`[t${turn}] ${message.role}: ${cap(redactSecretsForLog(message.text).replace(/\s*\n\s*/g, " ⏎ "), MESSAGE_MAX_CHARS)}`);
|
|
234
|
+
}
|
|
235
|
+
pendingTools.push(...tools);
|
|
236
|
+
}
|
|
237
|
+
flushTools();
|
|
238
|
+
if (!lines.length || first === undefined || last === undefined)
|
|
239
|
+
return undefined;
|
|
240
|
+
return { source, session: sessionOf(source, file), cwd, first: new Date(first).toISOString(), last: new Date(last).toISOString(), turns: turn, text: lines.join("\n") };
|
|
241
|
+
}
|
|
242
|
+
function transcriptFiles(roots, from) {
|
|
243
|
+
const found = [];
|
|
244
|
+
const walk = (source, dir, depth) => {
|
|
245
|
+
let entries;
|
|
246
|
+
try {
|
|
247
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
for (const entry of entries) {
|
|
253
|
+
const full = path.join(dir, entry.name);
|
|
254
|
+
if (entry.isDirectory()) {
|
|
255
|
+
if (depth > 0)
|
|
256
|
+
walk(source, full, depth - 1);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (!entry.isFile() || !entry.name.endsWith(".jsonl"))
|
|
260
|
+
continue;
|
|
261
|
+
if (source === "copilot" && entry.name !== "events.jsonl")
|
|
262
|
+
continue;
|
|
263
|
+
if (source === "opencode" && !entry.name.startsWith("opencode-"))
|
|
264
|
+
continue;
|
|
265
|
+
try {
|
|
266
|
+
const mtime = fs.statSync(full).mtimeMs;
|
|
267
|
+
// Modified before the window opened: nothing in it can be in the window.
|
|
268
|
+
if (mtime >= from)
|
|
269
|
+
found.push({ source, file: full, mtime });
|
|
270
|
+
}
|
|
271
|
+
catch { /* Gone since listing. */ }
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
for (const root of roots)
|
|
275
|
+
walk(root.source, root.root, root.maxDepth);
|
|
276
|
+
return found.sort((a, b) => b.mtime - a.mtime).slice(0, MAX_FILES);
|
|
277
|
+
}
|
|
278
|
+
/** Fit transcripts into the budget, newest session first; the oldest turns of a session are trimmed before it is dropped. */
|
|
279
|
+
export function budgetTranscripts(all, budgetChars) {
|
|
280
|
+
const sorted = [...all].sort((a, b) => b.last.localeCompare(a.last));
|
|
281
|
+
const kept = [];
|
|
282
|
+
let used = 0, dropped = 0;
|
|
283
|
+
for (const transcript of sorted) {
|
|
284
|
+
const remaining = budgetChars - used;
|
|
285
|
+
const overhead = 200;
|
|
286
|
+
if (transcript.text.length + overhead <= remaining) {
|
|
287
|
+
kept.push(transcript);
|
|
288
|
+
used += transcript.text.length + overhead;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (remaining < MIN_PARTIAL_CHARS) {
|
|
292
|
+
dropped++;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const lines = transcript.text.split("\n");
|
|
296
|
+
const tail = [];
|
|
297
|
+
let size = overhead + 40;
|
|
298
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
299
|
+
if (size + lines[i].length + 1 > remaining)
|
|
300
|
+
break;
|
|
301
|
+
tail.unshift(lines[i]);
|
|
302
|
+
size += lines[i].length + 1;
|
|
303
|
+
}
|
|
304
|
+
if (!tail.length) {
|
|
305
|
+
dropped++;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
kept.push({ ...transcript, text: `[earlier turns trimmed]\n${tail.join("\n")}`, trimmed: true });
|
|
309
|
+
used += size;
|
|
310
|
+
}
|
|
311
|
+
return { kept, chars: kept.reduce((sum, t) => sum + t.text.length, 0), dropped };
|
|
312
|
+
}
|
|
313
|
+
// ── Memory and repo ──────────────────────────────────────────────────────────
|
|
314
|
+
function git(dir, args) {
|
|
315
|
+
return execFileSync("git", ["-C", dir, ...args], { encoding: "utf8", timeout: 5_000, stdio: ["ignore", "pipe", "ignore"] });
|
|
316
|
+
}
|
|
317
|
+
function defaultGitLog(dir) {
|
|
318
|
+
try {
|
|
319
|
+
return git(dir, ["log", `-${GIT_LOG_COUNT}`, "--date=short", "--format=%h %ad %s"]).split("\n").filter(Boolean);
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
return [];
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/** The repo's own folder plus its other worktrees, where agents also work on this project. */
|
|
326
|
+
export function projectDirsFor(phrenPath, project) {
|
|
327
|
+
const source = getProjectSourcePath(phrenPath, project);
|
|
328
|
+
if (!source)
|
|
329
|
+
return [];
|
|
330
|
+
const dirs = new Set([source]);
|
|
331
|
+
try {
|
|
332
|
+
for (const line of git(source, ["worktree", "list", "--porcelain"]).split("\n")) {
|
|
333
|
+
if (line.startsWith("worktree "))
|
|
334
|
+
dirs.add(path.resolve(line.slice("worktree ".length).trim()));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
catch { /* Not a git repo, or git missing. */ }
|
|
338
|
+
return [...dirs];
|
|
339
|
+
}
|
|
340
|
+
export function readTruths(phrenPath, project) {
|
|
341
|
+
const file = storeAwareProjectPath(phrenPath, project, "truths.md") ?? path.join(phrenPath, project, "truths.md");
|
|
342
|
+
try {
|
|
343
|
+
return fs.readFileSync(file, "utf8").split("\n").filter(line => line.startsWith("- "))
|
|
344
|
+
.map(line => line.slice(2).replace(/\s*_\(added \d{4}-\d{2}-\d{2}\)_\s*$/, "").trim()).filter(Boolean);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
return [];
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function readPages(projectDir) {
|
|
351
|
+
const dir = path.join(projectDir, "reference", "pages");
|
|
352
|
+
let names;
|
|
353
|
+
try {
|
|
354
|
+
names = fs.readdirSync(dir).filter(name => name.endsWith(".md")).sort();
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
return [];
|
|
358
|
+
}
|
|
359
|
+
return names.map(name => {
|
|
360
|
+
let head = "";
|
|
361
|
+
try {
|
|
362
|
+
head = readChunk(path.join(dir, name), 0, 2048);
|
|
363
|
+
}
|
|
364
|
+
catch { /* unreadable */ }
|
|
365
|
+
const field = (key) => new RegExp(`^${key}:\\s*(.+)$`, "m").exec(head)?.[1]?.trim().replace(/^["']|["']$/g, "") ?? "";
|
|
366
|
+
return { slug: name.slice(0, -3), title: field("title"), summary: field("summary") };
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
export function journalFile(phrenPath, project) {
|
|
370
|
+
return path.join(projectStoreDir(phrenPath, project), "dreams", "journal.md");
|
|
371
|
+
}
|
|
372
|
+
function readJournalTail(phrenPath, project) {
|
|
373
|
+
try {
|
|
374
|
+
return fs.readFileSync(journalFile(phrenPath, project), "utf8").split("\n").filter(line => line.startsWith("- ")).slice(-JOURNAL_LINES);
|
|
375
|
+
}
|
|
376
|
+
catch {
|
|
377
|
+
return [];
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const ACTIVE_STATUSES = new Set(["active", "contradicted", "stale", "invalid_citation"]);
|
|
381
|
+
/** OpenCode's existing session metadata supplies the directory absent from plugin rows.
|
|
382
|
+
* Never infer ownership from the transcript's filename or current process cwd. */
|
|
383
|
+
function opencodeDirectory(session) {
|
|
384
|
+
if (!/^ses_[a-zA-Z0-9]+$/.test(session))
|
|
385
|
+
return undefined;
|
|
386
|
+
const storage = path.join(process.env.XDG_DATA_HOME || path.join(homeDir(), ".local", "share"), "opencode", "storage", "session");
|
|
387
|
+
let projects;
|
|
388
|
+
try {
|
|
389
|
+
projects = fs.readdirSync(storage).slice(0, 400);
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
for (const project of projects) {
|
|
395
|
+
try {
|
|
396
|
+
const meta = obj(JSON.parse(fs.readFileSync(path.join(storage, project, `${session}.json`), "utf8")));
|
|
397
|
+
if (meta.id === session && absolute(meta.directory))
|
|
398
|
+
return meta.directory;
|
|
399
|
+
}
|
|
400
|
+
catch { /* A different project or unreadable metadata. */ }
|
|
401
|
+
}
|
|
402
|
+
return undefined;
|
|
403
|
+
}
|
|
404
|
+
export async function collect(phrenPath, project, options) {
|
|
405
|
+
const from = options.from.getTime(), to = options.to.getTime();
|
|
406
|
+
const dirs = (options.projectDirs ?? projectDirsFor(phrenPath, project)).map(dir => path.resolve(dir));
|
|
407
|
+
const transcripts = [];
|
|
408
|
+
let unresolvedTranscripts = 0;
|
|
409
|
+
if (dirs.length) {
|
|
410
|
+
for (const { source, file } of transcriptFiles(options.roots ?? defaultTranscriptRoots(), from)) {
|
|
411
|
+
const metadataCwd = source === "opencode" ? opencodeDirectory(sessionOf(source, file)) : undefined;
|
|
412
|
+
const transcript = readTranscript(source, file, dirs, from, to, metadataCwd);
|
|
413
|
+
if (source === "opencode" && !metadataCwd && !transcript)
|
|
414
|
+
unresolvedTranscripts++;
|
|
415
|
+
if (transcript)
|
|
416
|
+
transcripts.push(transcript);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const fitted = budgetTranscripts(transcripts, options.budgetChars ?? DEFAULT_TRANSCRIPT_BUDGET_CHARS);
|
|
420
|
+
const read = readFindings(phrenPath, project);
|
|
421
|
+
const findings = read.ok
|
|
422
|
+
? read.data.filter(item => item.stableId && ACTIVE_STATUSES.has(item.status))
|
|
423
|
+
.map(item => ({ fid: item.stableId, date: item.date, text: item.text, status: item.status, ...(item.citation ? { citation: item.citation } : {}) }))
|
|
424
|
+
: [];
|
|
425
|
+
const topics = readProjectTopics(phrenPath, project).topics.map(topic => ({ slug: topic.slug, label: topic.label }));
|
|
426
|
+
const repoDir = dirs[0];
|
|
427
|
+
const gitLog = options.gitLog === false || !repoDir ? [] : (options.gitLog ?? defaultGitLog)(repoDir);
|
|
428
|
+
const budgetBytes = Math.round((options.injectBudgetKB ?? 8) * 1024);
|
|
429
|
+
const budgetState = overBudget(phrenPath, project, options.injectBudgetKB ?? 8);
|
|
430
|
+
const sizeBytes = budgetState.bytes;
|
|
431
|
+
const candidates = budgetState.candidates.map(candidate => candidate.text);
|
|
432
|
+
return {
|
|
433
|
+
project,
|
|
434
|
+
from: options.from.toISOString(),
|
|
435
|
+
to: options.to.toISOString(),
|
|
436
|
+
...(repoDir ? { repoDir } : {}),
|
|
437
|
+
transcripts: fitted.kept,
|
|
438
|
+
transcriptChars: fitted.chars,
|
|
439
|
+
transcriptsDropped: fitted.dropped,
|
|
440
|
+
unresolvedTranscripts,
|
|
441
|
+
findings,
|
|
442
|
+
truths: readTruths(phrenPath, project),
|
|
443
|
+
topics,
|
|
444
|
+
pages: readPages(projectStoreDir(phrenPath, project)),
|
|
445
|
+
gitLog,
|
|
446
|
+
journal: readJournalTail(phrenPath, project),
|
|
447
|
+
budget: { sizeBytes, budgetBytes, over: sizeBytes > budgetBytes, candidates },
|
|
448
|
+
};
|
|
449
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { runHelper } from "../../bridge/helper.js";
|
|
2
|
+
import { type DreamRunner } from "../run.js";
|
|
3
|
+
export type { DreamChange, DreamEvidence, DreamChangeset } from "../types.js";
|
|
4
|
+
/** A `runHelper`-shaped runner (bridge/helper.ts). */
|
|
5
|
+
export type HelperRunner = typeof runHelper;
|
|
6
|
+
export interface EvalDreamOptions {
|
|
7
|
+
dryRun?: boolean;
|
|
8
|
+
mode?: "propose" | "apply";
|
|
9
|
+
now?: Date;
|
|
10
|
+
force?: boolean;
|
|
11
|
+
/** Omitted for a real-model run: the engine then uses the project's helper. */
|
|
12
|
+
runner?: HelperRunner;
|
|
13
|
+
}
|
|
14
|
+
export type DreamFn = (project: string, opts: EvalDreamOptions) => Promise<unknown>;
|
|
15
|
+
export declare function asDreamRunner(runner: HelperRunner): DreamRunner;
|
|
16
|
+
export declare const defaultDream: DreamFn;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Connects the eval harness to the dreaming engine. The harness scripts the
|
|
2
|
+
// model with a `runHelper`-shaped runner; the engine takes a DreamRunner.
|
|
3
|
+
import { helperFor } from "../../bridge/helper.js";
|
|
4
|
+
import { dream } from "../run.js";
|
|
5
|
+
export function asDreamRunner(runner) {
|
|
6
|
+
return async (request, project, phrenPath) => {
|
|
7
|
+
// A scripted runner needs no real helper account; a real one does.
|
|
8
|
+
let choice;
|
|
9
|
+
try {
|
|
10
|
+
choice = helperFor(phrenPath, project);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
choice = { project, account: "default", model: request.model || "scripted", home: { id: "default", dir: process.env.HOME ?? "", configFile: "", isDefault: true } };
|
|
14
|
+
}
|
|
15
|
+
if (request.model)
|
|
16
|
+
choice.model = request.model;
|
|
17
|
+
const result = await runner(choice, { prompt: request.prompt, cwd: request.cwd, timeoutSeconds: request.timeoutSeconds, signal: request.signal });
|
|
18
|
+
return { ok: result.ok, output: result.output, model: result.model, durationMs: result.durationMs };
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export const defaultDream = (project, { runner, ...rest }) => dream(project, { ...rest, ...(runner ? { runner: asDreamRunner(runner) } : {}) });
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dreaming eval's fixture: a small store and synthetic transcripts that
|
|
3
|
+
* contain exactly the situations a dream should handle.
|
|
4
|
+
*
|
|
5
|
+
* 1. A finding contradicted by a later transcript (the dev server moved off port 3000).
|
|
6
|
+
* 2. Two findings that say the same thing (migrate before tests).
|
|
7
|
+
* 3. A decision stated in a transcript and never saved (Postgres advisory locks
|
|
8
|
+
* instead of Redis for the rate limiter).
|
|
9
|
+
* 4. Truths over the injected budget, some recently cited and some stale.
|
|
10
|
+
* plus a correct finding nothing should touch (the control).
|
|
11
|
+
*
|
|
12
|
+
* Transcripts are written where phren's readers look for them under `home`:
|
|
13
|
+
* a Claude Code session (`.claude/projects/<cwd>/<id>.jsonl`) and a Codex
|
|
14
|
+
* rollout (`.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`), both with the
|
|
15
|
+
* project's checkout as their cwd.
|
|
16
|
+
*/
|
|
17
|
+
export declare const EVAL_PROJECT = "lantern";
|
|
18
|
+
/** Fixed clock for the fixture; transcripts fall inside the default 24 h lookback from here. */
|
|
19
|
+
export declare const EVAL_NOW: Date;
|
|
20
|
+
export declare const EVAL_BUDGET_KB = 8;
|
|
21
|
+
export declare const FIDS: {
|
|
22
|
+
readonly contradicted: "a1000001";
|
|
23
|
+
readonly dupA: "a1000002";
|
|
24
|
+
readonly dupB: "a1000003";
|
|
25
|
+
readonly control: "a1000004";
|
|
26
|
+
};
|
|
27
|
+
export declare const CLAUDE_SESSION = "5f0c2a7e-1b7d-4c55-9a52-0d9a6b3e7c11";
|
|
28
|
+
export declare const CODEX_SESSION = "0199a3c4-7d10-7b2e-9f61-2c8d4e5a6b70";
|
|
29
|
+
/** Phrases the scripted model and the scorer key on. */
|
|
30
|
+
export declare const FACTS: {
|
|
31
|
+
newPort: string;
|
|
32
|
+
decisionKeyword: string;
|
|
33
|
+
decisionPattern: RegExp;
|
|
34
|
+
decisionTopicPattern: RegExp;
|
|
35
|
+
};
|
|
36
|
+
export interface EvalFixture {
|
|
37
|
+
root: string;
|
|
38
|
+
/** The phren store (PHREN_PATH). */
|
|
39
|
+
store: string;
|
|
40
|
+
/** HOME for the run: where the transcripts live. */
|
|
41
|
+
home: string;
|
|
42
|
+
/** The project's checkout (sourcePath, and the transcripts' cwd). */
|
|
43
|
+
repo: string;
|
|
44
|
+
project: string;
|
|
45
|
+
now: Date;
|
|
46
|
+
budgetKB: number;
|
|
47
|
+
/** Truths nobody has used in months: the expected demotions. */
|
|
48
|
+
staleTruths: string[];
|
|
49
|
+
/** Truths cited in the last week: demoting them is a mistake. */
|
|
50
|
+
freshTruths: string[];
|
|
51
|
+
}
|
|
52
|
+
/** Builds the fixture under `root` (an empty directory the caller owns and removes). */
|
|
53
|
+
export declare function buildEvalFixture(root: string, now?: Date): EvalFixture;
|