selflore 0.0.2 → 0.2.0
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/README.md +80 -17
- package/dist/cli.js +87 -17
- package/dist/hooks/capture-worker.js +27 -0
- package/dist/hooks/io.js +6 -1
- package/dist/hooks/session-start.js +2 -8
- package/dist/hooks/stop.js +3 -21
- package/dist/hooks/user-prompt-submit.js +10 -0
- package/dist/init.js +84 -18
- package/dist/lib/background.js +173 -0
- package/dist/lib/capture-prompt.js +39 -0
- package/dist/lib/commands.js +321 -67
- package/dist/lib/config.js +84 -5
- package/dist/lib/entries.js +82 -3
- package/dist/lib/heat.js +141 -0
- package/dist/lib/hook-logic.js +107 -0
- package/dist/lib/index-cache.js +46 -0
- package/dist/lib/install.js +2 -3
- package/dist/lib/links.js +58 -0
- package/dist/lib/logo.js +6 -0
- package/dist/lib/notes-graph.js +585 -0
- package/dist/lib/notes-html.js +148 -330
- package/dist/lib/notes-page-css.js +279 -0
- package/dist/lib/notes-page-js.js +959 -0
- package/dist/lib/notes.js +19 -6
- package/dist/lib/nudge.js +8 -1
- package/dist/lib/paths.js +6 -0
- package/dist/lib/question.js +19 -0
- package/dist/lib/queue.js +6 -4
- package/dist/lib/recall.js +149 -0
- package/dist/lib/results.js +11 -1
- package/dist/lib/retag.js +150 -0
- package/dist/lib/server-launch.js +73 -0
- package/dist/lib/server.js +120 -0
- package/dist/lib/session.js +27 -1
- package/dist/lib/settings.js +5 -1
- package/dist/lib/signals.js +51 -0
- package/dist/lib/stats.js +72 -40
- package/dist/lib/struggle.js +120 -0
- package/dist/lib/surface.js +75 -0
- package/dist/lib/transcript.js +205 -37
- package/dist/lib/ui.js +127 -0
- package/dist/notes-server.js +39 -0
- package/dist/sl.js +33 -14
- package/dist/vendor/graph3d.js +5402 -0
- package/package.json +6 -3
- package/skill/SKILL.md +29 -14
- package/skill/references/capture.md +16 -12
- package/skill/references/log.md +2 -2
- package/skill/references/recall.md +44 -0
- package/skill/references/stats.md +5 -1
- package/skill/references/test.md +49 -31
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { effectiveFocus, readConfig } from "./config.js";
|
|
6
|
+
import { GENERIC_TOPICS, listEntries, normalizeTopics, ORIGINS, validateEntry } from "./entries.js";
|
|
7
|
+
import { capturePrompt } from "./capture-prompt.js";
|
|
8
|
+
import { projectInfo, storeEntry } from "./commands.js";
|
|
9
|
+
import { paths } from "./paths.js";
|
|
10
|
+
/** Env flag set on everything selflore spawns, so its own hooks never fire inside a capture. */
|
|
11
|
+
export const BG_ENV = "SELFLORE_BG";
|
|
12
|
+
function gitRoot(cwd) {
|
|
13
|
+
try {
|
|
14
|
+
return execFileSync("git", ["rev-parse", "--show-toplevel"], { cwd, stdio: ["ignore", "pipe", "ignore"] }).toString().trim();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isTracked(root, file) {
|
|
21
|
+
try {
|
|
22
|
+
execFileSync("git", ["ls-files", "--error-unmatch", file], { cwd: root, stdio: "ignore" });
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Filter for large writes: drop files outside the project, and one-off scripts
|
|
31
|
+
* (untracked, sitting in the repo root, written once and never touched again).
|
|
32
|
+
*/
|
|
33
|
+
export function keepWriteFor(cwd, tracked = isTracked) {
|
|
34
|
+
const root = gitRoot(cwd) || cwd;
|
|
35
|
+
return ({ file, editCount }) => {
|
|
36
|
+
const abs = path.resolve(cwd, file);
|
|
37
|
+
const rel = path.relative(root, abs);
|
|
38
|
+
if (rel.startsWith("..") || path.isAbsolute(rel))
|
|
39
|
+
return false;
|
|
40
|
+
const inRoot = !rel.includes(path.sep);
|
|
41
|
+
if (inRoot && editCount <= 1 && !tracked(root, rel))
|
|
42
|
+
return false;
|
|
43
|
+
return true;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Resolve the model flag for the capture call. "session" uses the model the session itself used. */
|
|
47
|
+
export function captureModel(config, sessionModel) {
|
|
48
|
+
if (config.capture_model === "sonnet" || config.capture_model === "haiku")
|
|
49
|
+
return config.capture_model;
|
|
50
|
+
return sessionModel;
|
|
51
|
+
}
|
|
52
|
+
/** Start the capture worker fully detached; the Stop hook returns immediately. */
|
|
53
|
+
export const spawnWorker = (a) => {
|
|
54
|
+
try {
|
|
55
|
+
const worker = path.join(paths.app(), "hooks", "capture-worker.js");
|
|
56
|
+
const child = spawn(process.execPath, [worker, a.session, a.transcript, String(a.from), String(a.to), a.cwd], {
|
|
57
|
+
detached: true,
|
|
58
|
+
stdio: "ignore",
|
|
59
|
+
env: { ...process.env, [BG_ENV]: "1" },
|
|
60
|
+
});
|
|
61
|
+
child.on("error", () => { });
|
|
62
|
+
child.unref();
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
/** Pull the JSON array out of the model's reply (tolerates stray prose or code fences). */
|
|
70
|
+
export function parseCaptureReply(reply) {
|
|
71
|
+
const start = reply.indexOf("[");
|
|
72
|
+
const end = reply.lastIndexOf("]");
|
|
73
|
+
if (start < 0 || end <= start)
|
|
74
|
+
return [];
|
|
75
|
+
try {
|
|
76
|
+
const parsed = JSON.parse(reply.slice(start, end + 1));
|
|
77
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Convert one model-produced object into an entry draft (body = prose + fenced snippet). */
|
|
84
|
+
export function toDraft(raw, focusMode) {
|
|
85
|
+
if (!raw || typeof raw !== "object")
|
|
86
|
+
return null;
|
|
87
|
+
const r = raw;
|
|
88
|
+
const snippet = typeof r.snippet === "string" && r.snippet.trim() ? r.snippet.replace(/\s+$/, "") : "";
|
|
89
|
+
const lang = typeof r.snippet_lang === "string" ? r.snippet_lang.replace(/[^\w+-]/g, "") : "";
|
|
90
|
+
const draft = {
|
|
91
|
+
kind: r.kind,
|
|
92
|
+
level: r.level,
|
|
93
|
+
topics: normalizeTopics(r.topics, r.skill),
|
|
94
|
+
...(ORIGINS.includes(r.origin) ? { origin: r.origin } : {}), // anything else: inferred from the kind
|
|
95
|
+
paths: Array.isArray(r.paths) ? r.paths.filter((p) => typeof p === "string") : [],
|
|
96
|
+
testable: typeof r.testable === "string" ? r.testable.trim() : "",
|
|
97
|
+
body: `${typeof r.body === "string" ? r.body.trim() : ""}${snippet ? `\n\n\`\`\`${lang}\n${snippet}\n\`\`\`` : ""}`,
|
|
98
|
+
};
|
|
99
|
+
if (validateEntry(draft).length)
|
|
100
|
+
return null;
|
|
101
|
+
// A single-level focus is a hard filter, in case the model drifted.
|
|
102
|
+
if (focusMode === "details" && draft.level !== "detail")
|
|
103
|
+
return null;
|
|
104
|
+
if (focusMode === "design" && draft.level !== "design")
|
|
105
|
+
return null;
|
|
106
|
+
return draft;
|
|
107
|
+
}
|
|
108
|
+
export function askClaude(systemPrompt, input, model) {
|
|
109
|
+
const bin = process.env.SELFLORE_CLAUDE_BIN || "claude";
|
|
110
|
+
const args = ["-p", "--output-format", "json", "--tools", "", "--no-session-persistence", "--system-prompt", systemPrompt];
|
|
111
|
+
if (model)
|
|
112
|
+
args.push("--model", model);
|
|
113
|
+
// Run from a scratch dir so no project CLAUDE.md is picked up.
|
|
114
|
+
const out = execFileSync(bin, args, {
|
|
115
|
+
input,
|
|
116
|
+
cwd: os.tmpdir(),
|
|
117
|
+
env: { ...process.env, [BG_ENV]: "1" },
|
|
118
|
+
timeout: 5 * 60 * 1000,
|
|
119
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
120
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
121
|
+
}).toString();
|
|
122
|
+
try {
|
|
123
|
+
const json = JSON.parse(out);
|
|
124
|
+
return json.is_error ? "" : (json.result ?? "");
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Topics worth reusing, so one subject keeps one name: what this project's entries already use
|
|
132
|
+
* (most used first), then what other projects use. Generic words are never offered.
|
|
133
|
+
*/
|
|
134
|
+
export function knownTopics(project, entries = listEntries(), limit = 30) {
|
|
135
|
+
const mine = new Map();
|
|
136
|
+
const others = new Map();
|
|
137
|
+
for (const e of entries)
|
|
138
|
+
for (const t of e.topics) {
|
|
139
|
+
if (GENERIC_TOPICS.has(t))
|
|
140
|
+
continue;
|
|
141
|
+
const counts = e.project === project ? mine : others;
|
|
142
|
+
counts.set(t, (counts.get(t) ?? 0) + 1);
|
|
143
|
+
}
|
|
144
|
+
const byUse = (m) => [...m.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([t]) => t);
|
|
145
|
+
return [...new Set([...byUse(mine), ...byUse(others)])].slice(0, limit);
|
|
146
|
+
}
|
|
147
|
+
/** Ask the capture model for entries from an excerpt and store them. Returns how many were stored. */
|
|
148
|
+
export function runCapture(opts) {
|
|
149
|
+
if (!opts.excerpt.trim())
|
|
150
|
+
return 0;
|
|
151
|
+
const config = readConfig();
|
|
152
|
+
const project = projectInfo(opts.cwd).project;
|
|
153
|
+
const focus = effectiveFocus(config, project);
|
|
154
|
+
const reply = (opts.ask ?? askClaude)(capturePrompt(config, focus, knownTopics(project)), `Session excerpt (most recent work):\n\n${opts.excerpt}`, captureModel(config, opts.sessionModel));
|
|
155
|
+
let stored = 0;
|
|
156
|
+
for (const raw of parseCaptureReply(reply)) {
|
|
157
|
+
const draft = toDraft(raw, focus.mode);
|
|
158
|
+
if (!draft)
|
|
159
|
+
continue;
|
|
160
|
+
storeEntry(draft, { session: opts.session, cwd: opts.cwd, now: opts.now });
|
|
161
|
+
stored++;
|
|
162
|
+
}
|
|
163
|
+
return stored;
|
|
164
|
+
}
|
|
165
|
+
export function logWorkerError(err) {
|
|
166
|
+
try {
|
|
167
|
+
fs.mkdirSync(paths.state(), { recursive: true });
|
|
168
|
+
fs.appendFileSync(path.join(paths.state(), "capture-errors.log"), `${new Date().toISOString()} ${err instanceof Error ? err.message : String(err)}\n`);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
// nothing else we can do
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describeFocus } from "./config.js";
|
|
2
|
+
const INTENSITY_BAR = {
|
|
3
|
+
light: "Only the biggest moments: a significant bug root-caused, a major design decision, or something the developer asked to have explained at length. Most chunks yield 0 entries.",
|
|
4
|
+
normal: "Something the developer asked to have explained, a bug root-caused and fixed, a non-obvious tradeoff with a stated reason, a new library/API/pattern, or the developer correcting the agent. A module the agent wrote counts only when there is a real decision in it. Routine edits yield 0 entries, and most chunks yield 0 or 1.",
|
|
5
|
+
thorough: "Anything the developer would benefit from understanding, including smaller gotchas, edge cases and API behaviour discovered along the way. Still skip renames, formatting and config tweaks.",
|
|
6
|
+
};
|
|
7
|
+
function focusRule(focus) {
|
|
8
|
+
if (focus.mode === "details")
|
|
9
|
+
return 'Capture only level "detail": language, library and API specifics — how something behaves, edge cases, gotchas, what a snippet does.';
|
|
10
|
+
if (focus.mode === "design")
|
|
11
|
+
return 'Capture only level "design": architecture, data flow, tradeoffs, failure modes, why it is built this way, what breaks at scale.';
|
|
12
|
+
const lean = { design: "Lean toward design (about 70/30).", balanced: "Aim for a balance of both.", details: "Lean toward detail (about 70/30)." }[focus.balance];
|
|
13
|
+
return `Capture both levels, tagging each honestly ("detail" = language/library specifics; "design" = architecture and tradeoffs). ${lean}`;
|
|
14
|
+
}
|
|
15
|
+
/** System prompt for the headless background capture call. Output: a JSON array, nothing else. */
|
|
16
|
+
export function capturePrompt(config, focus, knownTopics = []) {
|
|
17
|
+
const reuse = knownTopics.length
|
|
18
|
+
? ` Reuse these when they fit, spelled exactly: ${knownTopics.join(", ")}. Only add a new one when none does.`
|
|
19
|
+
: "";
|
|
20
|
+
return `You are selflore's capture step. You read an excerpt of a Claude Code session in which an AI agent did work for a developer, and you extract "lore": moments the developer should understand but probably doesn't, because the agent did the work. Each entry is a dated note the developer will ask about later ("why does this retry twice?"), and a question they can be tested on.
|
|
21
|
+
|
|
22
|
+
Bar for logging (intensity: ${config.capture_intensity}): ${INTENSITY_BAR[config.capture_intensity]}
|
|
23
|
+
Focus (${describeFocus(focus)}): ${focusRule(focus)}
|
|
24
|
+
The developer wants to keep sharp: ${config.keep_sharp.join(", ") || "anything"}. Prefer moments in those areas.
|
|
25
|
+
Self-rating ${config.rating}/10: ${config.rating <= 3 ? "favour fundamentals" : config.rating >= 8 ? "favour tradeoffs and failure modes" : "balanced depth"}.
|
|
26
|
+
|
|
27
|
+
Rules:
|
|
28
|
+
- The "testable" question is the gate. It must test understanding (why / what breaks / what happens if / what does this print and why), never trivia like names or file locations. If you cannot write one sharp question, skip the moment.
|
|
29
|
+
- The strongest signal is the developer asking how or why something works and the agent explaining it: they have just said what they didn't know. Capture it, with origin "asked". But skip a question whose answer is a one-line fact (what a flag does, where a file lives, what a command is called).
|
|
30
|
+
- Prefer one deep entry over several shallow ones. Do not log the same idea twice.
|
|
31
|
+
- Skip anything the developer wrote themselves, reports/docs the agent wrote for them to read, and throwaway one-off scripts.
|
|
32
|
+
- "body" is the note itself and the answer key, at most ~120 words. Lead with what the code alone can't tell anyone: why it was done this way, what was rejected, which bug or constraint forced it. What the code does comes second, and only as much as the why needs.
|
|
33
|
+
- "origin": "asked" (the developer asked for the explanation), "bugfix" (something failed and was root-caused and fixed), "dependency" (a library or API was added or changed), or "write" (anything else the agent built).
|
|
34
|
+
- "snippet" (optional, max 40 lines): the essential code as it is now. For a bug, the fixed code; name the original bug in the body.
|
|
35
|
+
- "topics": 1 to 3, lowercase. The first is the group the moment sits in: the technology, framework, service or subsystem ("fastapi", "postgres", "stripe", "notes server"). Entries are grouped by it, so keep it broad and reuse an existing one wherever possible. Then the concept ("retry logic", "idempotency", "race condition"). Never a kind of work: not "architecture", "debugging", "fundamentals", "design" or "bug".${reuse}
|
|
36
|
+
|
|
37
|
+
Output ONLY a JSON array (possibly empty, []), no prose, no code fences. Each element:
|
|
38
|
+
{"kind": "bug"|"decision"|"concept"|"pattern", "level": "detail"|"design", "origin": "asked"|"bugfix"|"dependency"|"write", "topics": ["<technology>", "<concept>"], "paths": ["relative/path"], "testable": "<question>", "body": "<answer key>", "snippet": "<code or omit>", "snippet_lang": "<ts|py|…|omit>"}`;
|
|
39
|
+
}
|