An all-in-one notes manager for an Obsidian second brain: writes the daily note itself (changes and why, decisions, discussions, research and lookups), queues…

A Claude Code mod that keeps your Obsidian second brain up to date by itself. You work with Claude; it takes the notes.
Every session writes itself into today's daily note: what changed and why, what you decided (in your own words), what was discussed but not settled, and what was researched or looked up, with sources. When work in one project affects another, it leaves a signal so nothing slips between projects. It survives crashes, works from your phone, and never needs you to remember anything.
Why this exists, in one line: a second brain is only as good as what gets written into it, and the moment you most need a note (mid-task, deep in a session) is the moment you are least likely to stop and write one.
A block like this lands in today's daily note, updated after every turn:
## Session 2 (14:05): contact form spam
*auto-written by vault-scribe · session `abcdef12` · own project: Website · cwd `C:/code/website` · updated 14:40*
### What Got Done
- added a honeypot field to the contact form (why: bots were submitting it daily)
- returned 422 for spam submissions (why: the blog embed needs a clear failure)
### Decisions Made
- use a honeypot, not a captcha (Alex: "no captcha, people hate them")
### Discussions & Open Questions
- weighed rate limiting per IP, deferred until spam volume is measured
- proposed, not confirmed: move the form to its own page
### Research & Lookups
- searched honeypot field effectiveness: catches most simple bots (source: https://example.com/honeypot)
- read the blog's embed code: it posts to the same endpoint, so it needed the 422 handling
### What's Still In Progress
- measure spam volume for a week
### Notes Touched
- [[Projects/Website/memory/forms|forms]]
### Signals for Review
- 1 queued for review → [[vault-scribe/signals/2026-10-08|signals 2026-10-08]]: `SIG-2026-10-08-abcdef12-1`
What each section is for:
map.md:128), an edit that shifts lines tells Claude exactly which citations elsewhere now point at the wrong line.Requires Claude Code 2.1.287 or newer (mods), in the terminal or the desktop app's Code tab.
/plugin install vault-scribe --marketplace saladin1980/vault-scribe
Then:
vault-scribe/config.md inside your vault.vault_root option (/config, or pluginConfigs in ~/.claude/settings.json)./scribe to check it found everything.Full walkthrough: docs/setup.md.
/scribe: status (owner, project, what's captured, signals, heads-up)/scribe flush: summarize and write now. Optional: it happens on its own.It reads your vault and the tools Claude runs in your session. Summaries go through your own Claude session's model (the one in scribe_model). Secrets that look like tokens, keys and passwords are redacted before anything is stored, summarized or written. Everything it writes stays in your vault. Details: docs/how-it-works.md.
claude plugin validate .
claude plugin test .
MIT. Built by saladin1980 to run a personal second brain, and shared in case it helps yours.
hooks/register.tsx 562 lines1// vault-scribe: an all-in-one notes manager for an Obsidian "second brain".
2// Writes the daily note itself (changes and why, decisions, discussions, research and lookups), queues
3// cross-project signals for a reviewer, shows a heads-up band, and guards a few dangerous writes.
4// Configured by one note in the vault: see README.md and docs/configuration.md.
5import { atom, read, update } from 'claude-code'
6import type { EngineInterface, Register } from 'claude-code'
7
8import type { HeadsUp, HeadsUpItem } from '../types'
9import {
10 N8N_WHY, approvesPush, checkQuotes, clip, fwd, commandDenyReason, matchPushRule, parseGitPush, countSessions, dailyNotePath, digestText, extractCitations, firstDiffLine,
11 isN8nDeleteTool, isPending, isTrivial, isUnder, lineCount, mergeSummary, newDailyNote, newSignalsFile, norm,
12 ownProject, parseProjectMap, parseSignals, parseSummary, pathDenyReason, redact, renderBlock, renderSignal,
13 countNames, shouldQueue, signalsPath, staleCitations, stampOf, summaryPrompt, tagText, touchName, upsertSession, vaultRel,
14} from './lib'
15import type { Agg, Citation, GuardOptions, ProjectMap, Signal, Summary, TurnRec } from './lib'
16
17type $T = EngineInterface
18
19const headsUp = atom({ plugin: 'vault-scribe', key: 'headsUp' } as const, null)
20const isDismissed = atom({ plugin: 'vault-scribe', key: 'isDismissed' } as const, false)
21
22const HUMAN = new Set(['composer', 'bridge'])
23const IDLE_MS = 8 * 60 * 1000
24const HEADS_UP_EVERY_MS = 10 * 60 * 1000
25const HEADS_UP_DAYS = 14
26const KEEP_SESSIONS_MS = 3 * 24 * 3600 * 1000
27const RECOVER_AFTER_MS = 30 * 60 * 1000
28
29
30// Module state: a hot reload drops it; the session's aggregate lives in $.store too.
31let root = ''
32let configNote = 'vault-scribe/config.md'
33let map: ProjectMap = parseProjectMap('')
34let agg: Agg | null = null
35let current: TurnRec = { actions: [] }
36let citations: Citation[] | null = null
37let mdPaths = new Set<string>()
38let nameCounts = new Map<string, number>()
39let headsUpAt = 0
40let idle: { cancel: () => void } | null = null
41let chain: Promise<unknown> = Promise.resolve()
42
43function serial<T>(fn: () => Promise<T>): Promise<T> {
44 const run = chain.then(fn, fn)
45 chain = run.catch(() => undefined)
46 return run
47}
48function log($: $T, text: string) {
49 try { $.ui.log(`vault-scribe: ${text}`) } catch { /* nothing draws */ }
50}
51
52async function loadMap($: $T) {
53 try {
54 map = parseProjectMap(await $.fs.read(`${root}/${configNote}`))
55 } catch {
56 log($, `config note ${configNote} unreadable; using defaults, project tagging is off until it is back`)
57 }
58 return map
59}
60
61async function freshAgg($: $T, id: string, cwd: string): Promise<Agg> {
62 const now = await $.clock.now()
63 const s = stampOf(now, map.timeZone)
64 return {
65 sessionId: id, cwd: fwd(cwd), own: ownProject(cwd, map.projects, root), sessionNo: 0, date: s.date, startedAt: s.time,
66 updatedAtMs: now, ctx: [], isNudged: false, isHeadsUpSent: false, topic: '', indexLine: '', done: [],
67 decisions: [], inProgress: [], discussions: [], research: [], notes: [], signalIds: [], digest: [], humanTurns: 0,
68 isReps: false,
69 }
70}
71
72async function ensureAgg($: $T): Promise<Agg> {
73 const id = await $.session.id()
74 if (agg && agg.sessionId === id) return agg
75 const stored = (await $.store.get(`sess:${id}`)) as Agg | undefined
76 agg = stored && stored.sessionId === id ? stored : await freshAgg($, id, await $.session.root())
77 // Records made by an older build lack the newer fields.
78 agg.discussions ??= []
79 agg.research ??= []
80 return agg
81}
82
83async function saveAgg($: $T, a: Agg) {
84 a.updatedAtMs = await $.clock.now()
85 await $.store.set(`sess:${a.sessionId}`, a)
86}
87
88async function save($: $T) {
89 if (agg) await saveAgg($, agg)
90}
91
92// ---------- heads-up ----------
93
94async function refreshHeadsUp($: $T) {
95 headsUpAt = await $.clock.now()
96 const a = await ensureAgg($)
97 const dir = `${root}/${map.signalsDir}`
98 if (!(await $.fs.exists(dir))) return
99 const cutoff = stampOf(headsUpAt - HEADS_UP_DAYS * 24 * 3600 * 1000, map.timeZone).date
100 const files = (await $.fs.list(dir)).filter(f => f.kind === 'file' && /^\d{4}-\d{2}-\d{2}\.md$/.test(f.name) && f.name.slice(0, 10) >= cutoff)
101 const id8 = a.sessionId.slice(0, 8)
102 const items: HeadsUpItem[] = []
103 for (const f of files) {
104 const sigs = parseSignals(await $.fs.read(`${dir}/${f.name}`).catch(() => ''))
105 for (const s of sigs) {
106 if (!isPending(s) || s.sessionId === id8) continue
107 const touchesOwn = a.own === 'vault' || s.touches.some(t => touchName(t) === a.own)
108 if (touchesOwn) items.push({ id: s.id, what: s.what, kind: s.kind, from: s.from.replace(/ · cwd.*$/, '') })
109 }
110 }
111 const next: HeadsUp | null = items.length === 0 ? null : { project: a.own, items: items.slice(-5), total: items.length }
112 await update($, headsUp, () => next)
113 $.ui.status(`📓 scribe · ${a.own}${next ? ` · ⚠ ${next.total}` : ''}`)
114}
115
116// ---------- citation index (vault .md files citing file.md:NNN) ----------
117
118async function buildCitations($: $T) {
119 const out: Citation[] = []
120 const paths = new Set<string>()
121 const walk = async (dir: string, depth: number) => {
122 if (depth > 8) return
123 const entries = await $.fs.list(dir).catch(() => [])
124 for (const en of entries) {
125 if (en.kind === 'dir' && !map.skipDirs.includes(en.name) && !en.name.startsWith('.')) await walk(`${dir}/${en.name}`, depth + 1)
126 else if (en.kind === 'file' && en.name.toLowerCase().endsWith('.md')) {
127 const path = `${dir}/${en.name}`
128 paths.add(norm(path))
129 if (en.size >= 2_000_000) continue
130 const text = await $.fs.read(path).catch(() => '')
131 if (/\.md:\d/.test(text)) out.push(...extractCitations(path, text))
132 }
133 }
134 }
135 await walk(root, 0)
136 mdPaths = paths
137 nameCounts = countNames([...paths])
138 citations = out
139}
140
141function reindex(path: string, text: string) {
142 if (!citations) return
143 citations = citations.filter(c => norm(c.file) !== norm(path)).concat(extractCitations(path, text))
144 if (!mdPaths.has(norm(path))) {
145 mdPaths.add(norm(path))
146 nameCounts = countNames([...mdPaths])
147 }
148}
149
150// ---------- flush: summary → signals → daily note ----------
151
152/** The live session's flush. `summarize` false = the cheap per-turn write: no model call, digest kept. */
153async function flush($: $T, why: string, summarize = true) {
154 return serial(async () => flushAgg($, await ensureAgg($), why, summarize))
155}
156
157async function flushAgg($: $T, a: Agg, why: string, summarize: boolean) {
158 {
159 if (a.digest.length === 0 && a.notes.length === 0 && a.done.length === 0) return
160 if (summarize && a.digest.length === 0 && a.sessionNo > 0) return
161 await loadMap($)
162 const names = map.projects.map(p => p.name)
163 const now = await $.clock.now()
164 const stamp = stampOf(now, map.timeZone)
165 a.discussions ??= []
166 a.research ??= []
167
168 let summary: Summary | null = null
169 if (summarize && a.digest.length > 0) {
170 const { system, prompt } = summaryPrompt(digestText(a.digest), map.projects, a.own, map.owner)
171 const r = await $.model.complete({ model: map.model, system, prompt, maxTokens: 2500, effort: 'low' }).catch(() => null)
172 if (r && r.isAnswered) summary = parseSummary(r.text, names)
173 // A decision only counts when it quotes words the owner actually typed.
174 const ownerWords = a.digest.map(t => t.prompt ?? '').filter(x => x !== '')
175 if (summary) summary = checkQuotes(summary, ownerWords)
176 if (!summary) log($, `summary unavailable (${r ? (r.isAnswered ? 'unparseable reply' : r.reason) : 'request refused'}); wrote the deterministic part only`)
177 }
178
179 // Deterministic tags from what the session actually touched.
180 const det = new Map<string, string[]>()
181 for (const t of a.digest) for (const act of t.actions) {
182 // Only changes count: edits, writes and non-read-only shell commands. Reads, lookups and reports don't.
183 if (!/^(edit|write) |^(Bash|PowerShell): /.test(act)) continue
184 // An edit is evidence by its path alone, never by the text it changed.
185 const subject = /^(edit|write) /.test(act) ? actionPath(act) : act
186 // Only a real path or host is evidence of touching a project; a keyword inside command text is not
187 // (a heredoc full of project names tagged seven projects on 2026-10-08).
188 for (const [p, ev] of tagText(subject, map.projects)) {
189 const hard = ev.filter(x => !x.startsWith('keyword '))
190 if (hard.length > 0) det.set(p, [...new Set([...(det.get(p) ?? []), ...hard])])
191 }
192 }
193
194 const found: { kind: Signal['kind']; what: string; why: string; touches: Map<string, string[]> }[] = []
195 for (const s of summary?.signals ?? []) {
196 const touches = new Map<string, string[]>()
197 for (const p of s.projects) touches.set(p, ['named by summary'])
198 for (const [p, ev] of tagText(`${s.what} ${s.why}`, map.projects)) touches.set(p, [...(touches.get(p) ?? []), ...ev])
199 found.push({ kind: s.kind, what: s.what, why: s.why, touches })
200 }
201 for (const [p, ev] of det) {
202 if (p === a.own || found.some(f => f.touches.has(p))) continue
203 found.push({
204 kind: 'change',
205 what: `This session touched ${p} while working in ${a.own}: ${ev.slice(0, 3).join(', ')}`,
206 why: 'Detected from the tools the session ran; no summarized signal covered it. Check what changed and whether its notes need it.',
207 touches: new Map([[p, ev]]),
208 })
209 }
210
211 const queued: Signal[] = []
212 const id8 = a.sessionId.slice(0, 8)
213 if (summarize && !a.isReps) {
214 for (const f of found) {
215 const projects = [...f.touches.keys()]
216 if (!shouldQueue(f.kind, projects, a.own)) continue
217 const touches = projects.map(p => `${p}${p === a.own ? ' (own project)' : ''} (${clip(f.touches.get(p)!.join(', '), 160)})`)
218 queued.push({
219 id: `SIG-${stamp.date}-${id8}-${a.signalIds.length + queued.length + 1}`, status: '⏳ pending', kind: f.kind,
220 what: redact(f.what), why: redact(f.why), sessionId: id8,
221 from: `session ${id8} · cwd \`${a.cwd}\` · own project: ${a.own} · ${stamp.date} ${stamp.time}`,
222 touches: touches.length > 0 ? touches : [`${a.own} (own project)`],
223 })
224 }
225 if (queued.length > 0) {
226 const path = signalsPath(root, stamp.date, map.signalsDir)
227 const text = await $.fs.read(path).catch(() => newSignalsFile(stamp.date))
228 await $.fs.write(path, text.replace(/\s*$/, '\n\n') + queued.map(renderSignal).join('\n'))
229 a.signalIds.push(...queued.map(q => q.id))
230 }
231 }
232
233 // Notes for the model, delivered with the owner's next prompt.
234 for (const q of queued) {
235 if (q.kind === 'decision' || q.kind === 'correction') {
236 a.ctx.push(`vault-scribe: captured a ${q.kind}: "${clip(q.what, 200)}" (${q.id}, queued for review). Before calling it applied, search the WHOLE vault for its subject, not just the file at hand: a ruling applied to one file tends to leave contradicting lines elsewhere.`)
237 }
238 }
239 const ownRow = map.projects.find(p => p.name === a.own)
240 const touchedOwnMemory = ownRow ? a.notes.some(n => isUnder(n, `${root}/${ownRow.memory}`)) : true
241 const editedSomething = a.digest.some(t => t.actions.some(x => /^(edit|write) /.test(x)))
242 if (summarize && ownRow && !touchedOwnMemory && !a.isNudged && editedSomething && a.humanTurns >= 3) {
243 a.isNudged = true
244 a.ctx.push(`vault-scribe: this session has been changing ${a.own}, and nothing in its memory folder (${ownRow.memory}) has been updated yet. If a fact a future session needs changed, update that project's memory notes now while the context is here; the daily note is already handled.`)
245 }
246
247 if (summary) Object.assign(a, mergeSummary(a, summary))
248 if (summarize && !summary && a.digest.length > 0) {
249 const acts = a.digest.flatMap(t => t.actions).filter(x => /^(edit|write) /.test(x)).map(actionPath)
250 if (acts.length > 0) a.done.push(`edited ${acts.length} file(s): ${clip([...new Set(acts)].slice(0, 4).join(', '), 220)}`)
251 }
252 if (summarize) a.digest = []
253
254 // Daily note: this session's block + Index bullet only, the file re-read just before the write.
255 const notePath = dailyNotePath(root, stampOfDate(a.date), map.dailyNotePattern)
256 const raw = await $.fs.read(notePath).catch(() => newDailyNote(stampOfDate(a.date), map.dailyFrontmatter))
257 const isCrlf = raw.includes('\r\n')
258 if (a.sessionNo === 0) a.sessionNo = countSessions(raw) + 1
259 const sigLink = a.signalIds.length > 0 ? `[[${map.signalsDir.replace(/^[\\/]+|[\\/]+$/g, '')}/${a.date}|signals ${a.date}]]` : null
260 const indexLine = a.indexLine || `**${a.topic || 'Session'}**: in progress`
261 const out = upsertSession(raw, a.sessionId, renderBlock(a, root, sigLink, stamp.time), indexLine)
262 await $.fs.write(notePath, isCrlf ? out.replace(/\n/g, '\r\n') : out)
263 await saveAgg($, a)
264 if (why === 'command') log($, `daily note updated (${notePath})`)
265 if (why === 'recover') log($, `recovered session ${id8} (it ended without writing up its last turns)`)
266 }
267}
268
269/** Sessions that died (crash, closed window) with turns never written up: write them now. */
270async function recoverOrphans($: $T) {
271 const now = await $.clock.now()
272 const live = agg?.sessionId
273 for (const k of await $.store.keys()) {
274 if (!k.startsWith('sess:') || k === `sess:${live}`) continue
275 const v = (await $.store.get(k)) as Agg | undefined
276 if (!v) continue
277 const idleFor = now - (v.updatedAtMs ?? 0)
278 // A live session writes up its digest within IDLE_MS; one still holding turns well past that is dead.
279 if (v.digest && v.digest.length > 0 && idleFor > RECOVER_AFTER_MS) {
280 await serial(() => flushAgg($, v, 'recover', true)).catch(() => undefined)
281 } else if (idleFor > KEEP_SESSIONS_MS && (!v.digest || v.digest.length === 0)) {
282 await $.store.delete(k)
283 }
284 }
285}
286
287/** `edit D:/x/y.md: "old" -> "new"` / `write D:/x/y.md (12 lines)` → `D:/x/y.md` */
288function actionPath(act: string): string {
289 return act.replace(/^(edit|write) /, '').split(/: "| \(/)[0] ?? ''
290}
291
292const oneLine = (t: string) => t.replace(/\s+/g, ' ').trim()
293
294/** What a tool call contributes to the work log, or null for calls not worth noting. */
295function describeCall(tool: string, args: Record<string, unknown>, isReadOnly: boolean): string | null {
296 const s = (k: string) => (typeof args[k] === 'string' ? (args[k] as string) : '')
297 const path = fwd(s('file_path') || s('notebook_path'))
298 if (tool === 'Edit' && path) {
299 return `edit ${path}: "${clip(redact(oneLine(s('old_string'))), 90)}" -> "${clip(redact(oneLine(s('new_string'))), 90)}"`
300 }
301 if (tool === 'MultiEdit' && path) return `edit ${path} (${Array.isArray(args.edits) ? args.edits.length : 1} edits)`
302 if (tool === 'NotebookEdit' && path) return `edit ${path} (notebook cell)`
303 if (tool === 'Write' && path) return `write ${path} (${s('content').split(/\r?\n/).length} lines)`
304 if (tool === 'Bash' || tool === 'PowerShell') {
305 const cmd = s('command')
306 return cmd ? `${isReadOnly ? 'read ' : ''}${tool}: ${clip(redact(oneLine(cmd)), 300)}` : null
307 }
308 if (tool === 'Read' && path) return `read Read: ${path}`
309 if (tool === 'Grep') return `read Grep: "${clip(s('pattern'), 80)}" in ${fwd(s('path')) || '.'}`
310 if (tool === 'Glob') return `read Glob: ${clip(s('pattern'), 80)} in ${fwd(s('path')) || '.'}`
311 if (tool === 'WebSearch' || (/web[_-]?search/i.test(tool) && s('query'))) return `web search: ${clip(s('query'), 200)}`
312 if (tool === 'WebFetch' || (/fetch/i.test(tool) && s('url'))) return `web fetch: ${clip(s('url'), 300)}`
313 if (/navigate$/i.test(tool) && s('url')) return `web open: ${clip(s('url'), 300)}`
314 if (tool === 'Agent' || tool === 'Task') return `research agent: ${clip(oneLine(s('description') || s('prompt')), 160)}`
315 return null
316}
317
318/**
319 * The push guard: what the config note's push policy says about this `git push`, judged before it runs.
320 * A held push goes through only when the owner's own prompt this turn approves it ("ship it", "push it").
321 */
322async function pushDecision($: $T, command: string): Promise<{ deny?: string; note?: string }> {
323 const p = parseGitPush(command)
324 if (!p || (map.pushPolicy.length === 0 && !map.guardForcePush)) return {}
325 const cwd = fwd(await $.session.cwd())
326 const dir = p.dir === null ? cwd : /^[a-z]:|^\//i.test(fwd(p.dir)) ? fwd(p.dir) : `${cwd}/${fwd(p.dir)}`
327 const git = async (...args: string[]) => {
328 const r = await $.process.run(['git', '-C', dir, ...args], { timeoutMs: 5000 })
329 return r.exitCode === 0 ? r.stdout.trim() : ''
330 }
331 const url = await git('remote', 'get-url', p.remote)
332 const branch = p.branch ?? (await git('rev-parse', '--abbrev-ref', 'HEAD'))
333 const rule = matchPushRule(map.pushPolicy, url, dir, branch)
334 const said = current.prompt ?? ''
335 const where = `${url || dir} (${branch || 'current branch'})`
336 if (p.isForce && map.guardForcePush && !approvesPush(said, true)) {
337 return { deny: `vault-scribe held this push: it is a FORCE push to ${where}, which can wipe history. Ask ${map.owner} first; it goes through when their own message approves a force push (e.g. "force push it").` }
338 }
339 if (rule?.policy === 'hold' && !approvesPush(said, p.isForce)) {
340 return {
341 deny: [
342 `vault-scribe held this push to ${where}: ${rule.deploysTo || 'the push policy holds it'}.`,
343 rule.test ? `Before asking: ${rule.test}.` : '',
344 `Get ${map.owner}'s explicit sign-off, then push again. It goes through when their own message approves it ("ship it", "push it", "go ahead and push").`,
345 ].filter(x => x !== '').join(' '),
346 }
347 }
348 if (rule?.policy === 'announce') {
349 return { note: `vault-scribe: that push to ${where} deploys: ${rule.deploysTo}. Tell ${map.owner} it is on its way and what to test${rule.test ? ` (${rule.test})` : ''}.` }
350 }
351 return {}
352}
353
354function stampOfDate(date: string) {
355 return stampOf(Date.parse(`${date}T12:00:00Z`))
356}
357
358function armIdle($: $T) {
359 idle?.cancel()
360 idle = $.clock.after(IDLE_MS, () => { void flush($, 'idle').catch(() => undefined) })
361}
362
363export const register: Register = (on, options) => {
364 // ---------- hooks ----------
365
366 on('session.start', async ($, e, next) => {
367 // Vault: the vault_root option, else VAULT_SCRIBE_ROOT, else the folder the session started in.
368 const opt = (k: string) => (typeof options[k] === 'string' ? (options[k] as string).trim() : '')
369 root = (opt('vault_root') || (await $.env.get('VAULT_SCRIBE_ROOT')) || (await $.session.root()))
370 .replace(/\\/g, '/').replace(/\/+$/, '')
371 configNote = (opt('config_note') || (await $.env.get('VAULT_SCRIBE_CONFIG')) || 'vault-scribe/config.md').replace(/\\/g, '/')
372 await loadMap($)
373 agg = null
374 const a = await ensureAgg($)
375 // The map may have changed since this session's record was made (or an older build computed it).
376 const startDir = await $.session.root()
377 a.cwd = fwd(startDir)
378 a.own = ownProject(startDir, map.projects, root)
379 await save($)
380 current = { actions: [] }
381 // A resumed or reloaded session with turns never written up: write them now, summarize in the background.
382 if (a.digest.length > 0) {
383 await flush($, 'resume', false).catch(() => undefined)
384 $.clock.after(1000, () => { void flush($, 'resume', true).catch(() => undefined) })
385 }
386 await $.command.register({ name: 'scribe', description: 'vault-scribe: status, or `/scribe flush` to write the daily note now', argumentHint: '[flush]' })
387 await refreshHeadsUp($).catch(() => undefined)
388 // Background: prune old session records, build the citation index.
389 $.clock.after(1500, () => {
390 void (async () => {
391 await recoverOrphans($)
392 await buildCitations($)
393 })().catch(() => undefined)
394 })
395 return next(e)
396 })
397
398 on('session.end', async ($, e, next) => {
399 await flush($, 'end').catch(() => undefined)
400 return next(e)
401 })
402
403 on('prompt.submit', async ($, e, next) => {
404 const a = await ensureAgg($).catch(() => null)
405 if (!a) return next(e)
406 const text = e.text.trim()
407 const kind = e.origin.kind
408 const rc = map.reviewerCommand
409 if (rc !== '' && (text.startsWith(`/${rc}`) || (kind === 'scheduled-trigger' && text.includes(rc)))) a.isReps = true
410 if (HUMAN.has(kind) && !text.startsWith('/') && !isTrivial(text)) {
411 current.prompt = clip(redact(text), 1500)
412 } else if (HUMAN.has(kind) && isTrivial(text)) {
413 current.prompt = current.prompt ?? text
414 }
415
416 const extra: string[] = []
417 const hu = await read($, headsUp)
418 if (hu && !a.isHeadsUpSent && HUMAN.has(kind) && a.own !== 'vault') {
419 a.isHeadsUpSent = true
420 extra.push([
421 `vault-scribe heads-up: ${hu.total} change(s) from other sessions touch ${hu.project} and have not been reviewed yet. Treat the notes they affect as possibly stale:`,
422 ...hu.items.map(i => `- [${i.kind}] ${i.what} (${i.from}; ${i.id})`),
423 `Mention this to ${map.owner} once if it bears on what they asked. Do not fix other projects' notes from here; the reviewer does that.`,
424 ].join('\n'))
425 }
426 if (a.ctx.length > 0 && HUMAN.has(kind)) extra.push(...a.ctx.splice(0))
427 if ((await $.clock.now()) - headsUpAt > HEADS_UP_EVERY_MS) $.clock.after(50, () => { void refreshHeadsUp($).catch(() => undefined) })
428 await save($)
429 return extra.length > 0 ? next({ ...e, context: [...(e.context ?? []), ...extra] }) : next(e)
430 }).catch(($, e, next) => next(e))
431
432 on('tool.call', async ($, e, next) => {
433 const tool = String(e.tool)
434 const args = e as unknown as Record<string, unknown>
435 const path = typeof args.file_path === 'string' ? args.file_path : typeof args.notebook_path === 'string' ? args.notebook_path : null
436 const command = (tool === 'Bash' || tool === 'PowerShell') && typeof args.command === 'string' ? args.command : null
437 const isWrite = tool === 'Edit' || tool === 'Write' || tool === 'MultiEdit' || tool === 'NotebookEdit'
438
439 // Hard rules: refuse before anything runs.
440 const g: GuardOptions = { vaultRoot: root, autoMemory: map.guardAutoMemory, paths: map.guardPaths, n8n: map.guardN8nDelete }
441 const reason = (isWrite && path ? pathDenyReason(path, g) : null)
442 ?? (command ? commandDenyReason(command, g) : null)
443 ?? (map.guardN8nDelete && isN8nDeleteTool(tool) ? N8N_WHY : null)
444 if (reason) return { deny: `vault-scribe refused this: ${reason}` }
445 const push = command ? await pushDecision($, command).catch(() => ({} as { deny?: string; note?: string })) : {}
446 if (push.deny) return { deny: push.deny }
447
448 // Soft rules: look before, act after.
449 const isVaultMd = isWrite && path !== null && isUnder(path, root) && /\.md$/i.test(path)
450 const before = isVaultMd ? await $.fs.read(path!).catch(() => null) : null
451 const mf = map.memoryFolderName.toLowerCase()
452 const newMemoryDir = isWrite && path !== null && mf !== '' && norm(path).split('/').includes(mf)
453 ? !(await $.fs.exists(path.replace(/[\\/][^\\/]*$/, '')).catch(() => true))
454 : false
455
456 const ran = await next(e)
457 if (ran.deny !== undefined || ran.isError === true) return ran
458
459 // Read-only commands (the engine's own check) are context for the summary, never evidence of a change.
460 const described = describeCall(tool, args, ran.isReadOnly === true)
461 if (described) current.actions.push(described)
462
463 const notes: string[] = []
464 if (push.note) notes.push(push.note)
465 if (agg && isVaultMd && path) {
466 if (!agg.notes.some(n => norm(n) === norm(path))) agg.notes.push(path)
467 if (map.reviewDir !== '' && isUnder(path, `${root}/${map.reviewDir}`)) agg.isReps = true
468 }
469 if (isVaultMd && path) {
470 const after = await $.fs.read(path).catch(() => null)
471 if (after !== null) {
472 if (before !== null && citations) {
473 const delta = lineCount(after) - lineCount(before)
474 const from = firstDiffLine(before, after)
475 const stale = staleCitations(citations, path, from, delta, nameCounts)
476 if (stale.length > 0) {
477 notes.push([
478 `vault-scribe: this edit changed ${vaultRel(path, root)}.md's line count by ${delta > 0 ? '+' : ''}${delta} from line ${from}. These citations into it now point ${Math.abs(delta)} line(s) off. Fix them now (or make the edit line-neutral):`,
479 ...stale.slice(0, 15).map(c => `- ${vaultRel(c.file, root) ?? c.file}.md:${c.line} cites :${c.cited} → now :${c.cited + delta}`),
480 ...(stale.length > 15 ? [`- …and ${stale.length - 15} more`] : []),
481 ].join('\n'))
482 }
483 }
484 reindex(path, after)
485 }
486 }
487 if (newMemoryDir && path) {
488 notes.push([
489 `vault-scribe: this created a new ${map.memoryFolderName}/ folder (${fwd(path).replace(/\/[^/]*$/, '')}), so a new project. Update these together, in this session (the config note's new project checklist):`,
490 ...map.checklist.map(m => `- ${m}`),
491 ].join('\n'))
492 }
493 return notes.length > 0 ? { ...ran, context: [...(ran.context ?? []), ...notes] } : ran
494 }).catch(($, e, next) => next(e))
495
496 on('turn.complete', async ($, e, next) => {
497 // A subagent's final report is research the main session acted on: keep it for the summary.
498 if (e.agentId !== undefined && e.answer) current.actions.push(`research report: ${clip(redact(oneLine(e.answer)), 1200)}`)
499 if (e.agentId === undefined) {
500 const a = await ensureAgg($).catch(() => null)
501 if (a) {
502 if (e.answer) current.answer = clip(redact(e.answer), 1500)
503 if (current.prompt || current.actions.length > 0) {
504 a.digest.push(current)
505 if (current.prompt) a.humanTurns++
506 }
507 current = { actions: [] }
508 await save($)
509 // The note is written here, inside the hook: no timer a reload or crash could drop.
510 await flush($, 'turn', false).catch(() => undefined)
511 const prompts = a.digest.filter(t => t.prompt).length
512 if (prompts >= map.summarizeEvery) $.clock.after(10, () => { void flush($, 'turns', true).catch(() => undefined) })
513 armIdle($)
514 }
515 }
516 return next(e)
517 })
518
519 on('command.run', { command: 'scribe' }, async ($, e) => {
520 const a = await ensureAgg($)
521 if (e.args.trim() === 'flush') {
522 if (current.prompt || current.actions.length > 0) { a.digest.push(current); current = { actions: [] } }
523 await flush($, 'command')
524 return { text: `vault-scribe: daily note written for session ${a.sessionId.slice(0, 8)} (${a.signalIds.length} signal(s) queued so far).` }
525 }
526 await refreshHeadsUp($).catch(() => undefined)
527 const hu = await read($, headsUp)
528 return {
529 text: [
530 `vault-scribe · owner: ${map.owner} · own project: ${a.own} · cwd ${a.cwd} · config: ${configNote}`,
531 `captured: ${a.humanTurns} prompt(s), ${a.digest.length} turn(s) waiting for the next summary, ${a.notes.length} vault note(s) touched, ${(a.discussions ?? []).length} discussion(s), ${(a.research ?? []).length} research item(s)`,
532 `daily note: ${a.sessionNo > 0 ? `Session ${a.sessionNo} in ${a.date}.md` : 'not written yet'} · summary model: ${map.model} every ${map.summarizeEvery} prompt(s)`,
533 `signals queued by this session: ${a.signalIds.length}${a.isReps ? ' (reviewer run: queueing is off)' : ''}`,
534 `heads-up: ${hu ? `${hu.total} pending signal(s) touch ${hu.project}` : 'nothing pending for this project'}`,
535 `citation index: ${citations ? `${citations.length} citations` : 'building'}`,
536 ].join('\n'),
537 }
538 })
539
540 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
541 const hu = await read($, headsUp)
542 if (!hu || e.props.hasSurvey || (await read($, isDismissed))) return next(e)
543 const { Box, Text, Button } = $.ui.resolve(e)
544 const cols = Math.max(30, (e.props.bodyColumns ?? e.viewport?.columns ?? 80) - 4)
545 return (
546 <Box flexDirection="column">
547 <Text color="warning" bold>
548 ⚠ {hu.total} change{hu.total === 1 ? '' : 's'} from other sessions touch {hu.project}, pending review
549 </Text>
550 {hu.items.slice(-3).map(i => (
551 <Text dimColor wrap="truncate-end">
552 {clip(`· ${i.what} (${i.from})`, cols)}
553 </Text>
554 ))}
555 <Box>
556 <Button key="vs-dismiss" label="Dismiss" onPress={() => update($, isDismissed, () => true)} />
557 </Box>
558 </Box>
559 )
560 })
561}
562hooks/lib.ts 747 lines1// vault-scribe: pure logic, no `$`. Everything here is testable on its own.
2// See README.md and docs/ for what each piece is for.
3
4export type Project = { name: string; memory: string; paths: string[]; hosts: string[]; keywords: string[] }
5/** Everything a vault owner can tune, read from the config note's frontmatter at runtime. */
6export type ProjectMap = {
7 projects: Project[]
8 model: string
9 summarizeEvery: number
10 /** Who the sessions belong to, as the summaries should name them. */
11 owner: string
12 timeZone: string
13 /** Vault-relative; tokens {YYYY} {MM} {DD} {Month}. */
14 dailyNotePattern: string
15 signalsDir: string
16 /** Edits under this vault folder mark a session as the reviewer's own run (no signals queued). */
17 reviewDir: string
18 /** A slash command that runs your reviewer (e.g. `review-vault`); its sessions queue no signals. */
19 reviewerCommand: string
20 /** Folder name that marks a project's memory (e.g. `claude-memory`); creating a new one shows the checklist. */
21 memoryFolderName: string
22 /** Folder names the citation index never walks into. */
23 skipDirs: string[]
24 /** Extra frontmatter lines for a daily note the scribe creates. */
25 dailyFrontmatter: string[]
26 /** Refuse writes into Claude Code's per-project auto-memory (~/.claude/projects/<x>/memory), except the vault's own. */
27 guardAutoMemory: boolean
28 /** Refuse writes whose path contains any of these fragments (case-insensitive, forward slashes). */
29 guardPaths: string[]
30 guardN8nDelete: boolean
31 /** What to update when a new project memory folder appears ("## New project checklist" in the config note). */
32 checklist: string[]
33 /** Per repo and branch: what a push does and whether it is held ("## Push policy" in the config note). */
34 pushPolicy: PushRule[]
35 /** Hold every force push, whatever the policy says. */
36 guardForcePush: boolean
37}
38
39export type PushPolicyKind = 'hold' | 'announce' | 'go'
40export type PushRule = { repo: string; branch: string; policy: PushPolicyKind; deploysTo: string; test: string }
41
42export const DEFAULT_DAILY_NOTE = '{YYYY}-{MM}-{DD}.md'
43export const DEFAULT_SIGNALS_DIR = 'vault-scribe/signals'
44export const DEFAULT_CHECKLIST = ['the vault-scribe config note: a project-table row so the new project gets tagged']
45
46export type SignalKind = 'change' | 'decision' | 'correction' | 'fact' | 'discussion'
47export type Signal = {
48 id: string
49 status: string
50 kind: SignalKind
51 what: string
52 why: string
53 from: string
54 touches: string[]
55 sessionId: string
56}
57
58export type TurnRec = { prompt?: string; actions: string[]; answer?: string }
59
60export type Agg = {
61 sessionId: string
62 cwd: string
63 own: string
64 sessionNo: number
65 /** The note's date (the session's start day), and its HH:MM start. */
66 date: string
67 startedAt: string
68 updatedAtMs: number
69 /** Notes for the model, flushed into the next prompt's context. */
70 ctx: string[]
71 isNudged: boolean
72 isHeadsUpSent: boolean
73 topic: string
74 indexLine: string
75 done: string[]
76 decisions: string[]
77 inProgress: string[]
78 /** Options weighed, proposals waiting on the owner, ideas rejected and why. */
79 discussions: string[]
80 /** What was read or looked up, the finding, and what it informed (with sources). */
81 research: string[]
82 notes: string[]
83 signalIds: string[]
84 digest: TurnRec[]
85 humanTurns: number
86 isReps: boolean
87}
88
89export type Summary = {
90 topic: string
91 indexLine: string
92 done: string[]
93 decisions: string[]
94 inProgress: string[]
95 discussions: string[]
96 research: string[]
97 signals: { kind: SignalKind; what: string; why: string; projects: string[]; quote: string }[]
98}
99
100// ---------- paths ----------
101
102/** Display form of a path: forward slashes. */
103export const fwd = (p: string) => p.replace(/\\/g, '/')
104
105export const norm = (p: string) => p.replace(/\\/g, '/').replace(/\/+$/, '').toLowerCase()
106
107export const isUnder = (path: string, root: string) => {
108 const p = norm(path)
109 const r = norm(root)
110 return p === r || p.startsWith(r + '/')
111}
112
113/** `C:\Vault\Projects\x.md` → `Projects/x` (null outside the vault). */
114export function vaultRel(path: string, vaultRoot: string): string | null {
115 if (!isUnder(path, vaultRoot)) return null
116 const p = path.replace(/\\/g, '/')
117 const rel = p.slice(vaultRoot.replace(/\\/g, '/').replace(/\/+$/, '').length + 1)
118 return rel.replace(/\.md$/i, '')
119}
120
121export function noteLink(path: string, vaultRoot: string): string | null {
122 const rel = vaultRel(path, vaultRoot)
123 if (rel === null || !/\.md$/i.test(path)) return null
124 const base = rel.split('/').pop() ?? rel
125 return `[[${rel}|${base}]]`
126}
127
128// ---------- project map ----------
129
130export function parseFrontmatter(text: string): Record<string, string> {
131 const m = /^---\r?\n([\s\S]*?)\r?\n---/.exec(text)
132 const out: Record<string, string> = {}
133 if (!m) return out
134 for (const line of (m[1] ?? '').split(/\r?\n/)) {
135 const kv = /^([A-Za-z_][\w-]*)\s*:\s*(.*)$/.exec(line)
136 if (kv && kv[1]) out[kv[1]] = (kv[2] ?? '').trim()
137 }
138 return out
139}
140
141const cells = (s: string) =>
142 s.split(',').map(x => x.trim()).filter(x => x !== '')
143
144export function parseProjectMap(text: string): ProjectMap {
145 const fm = parseFrontmatter(text)
146 const projects: Project[] = []
147 let inTable = false
148 for (const raw of text.split(/\r?\n/)) {
149 const line = raw.trim()
150 if (/^\|\s*Project\s*\|/i.test(line)) { inTable = true; continue }
151 if (!inTable) continue
152 if (!line.startsWith('|')) { if (projects.length > 0) break; continue }
153 if (/^\|[\s|:-]+\|$/.test(line)) continue
154 const c = line.slice(1, line.endsWith('|') ? -1 : undefined).split('|').map(x => x.trim())
155 const [name = '', memory = '', paths = '', hosts = '', keywords = ''] = c
156 if (c.length < 5 || name === '') continue
157 projects.push({ name, memory, paths: cells(paths), hosts: cells(hosts), keywords: cells(keywords) })
158 }
159 const every = Number.parseInt(fm.summarize_every ?? '', 10)
160 const flag = (v: string | undefined, d: boolean) => (v === undefined || v === '' ? d : !/^(false|no|off|0)$/i.test(v))
161 const text_ = (v: string | undefined, d: string) => (v ?? '').trim().replace(/^["']|["']$/g, '') || d
162 const list = (v: string | undefined, d: string[]) => {
163 const l = (v ?? '').split(',').map(x => x.trim().replace(/^["']|["']$/g, '')).filter(x => x !== '')
164 return l.length > 0 ? l : d
165 }
166 const checklist: string[] = []
167 const pushPolicy: PushRule[] = []
168 let inPush = false
169 for (const raw of text.split(/\r?\n/)) {
170 const line = raw.trim()
171 if (/^#{1,6}\s+push policy\s*$/i.test(line)) { inPush = true; continue }
172 if (inPush && /^#{1,6}\s/.test(line)) break
173 if (!inPush || !line.startsWith('|') || /^\|[\s|:-]+\|$/.test(line) || /^\|\s*repo\s*\|/i.test(line)) continue
174 const [repo = '', branch = '', policy = '', deploysTo = '', test = ''] = line.slice(1, line.endsWith('|') ? -1 : undefined).split('|').map(x => x.trim())
175 const kind = policy.toLowerCase()
176 if (repo !== '' && (kind === 'hold' || kind === 'announce' || kind === 'go')) {
177 pushPolicy.push({ repo, branch: branch || '*', policy: kind, deploysTo, test })
178 }
179 }
180 let inChecklist = false
181 for (const raw of text.split(/\r?\n/)) {
182 if (/^#{1,6}\s+new project checklist\s*$/i.test(raw.trim())) { inChecklist = true; continue }
183 if (inChecklist && /^#{1,6}\s/.test(raw.trim())) break
184 const b = inChecklist ? /^\s*[-*]\s+(.+)$/.exec(raw) : null
185 if (b?.[1]) checklist.push(b[1].trim())
186 }
187 return {
188 projects,
189 model: text_(fm.scribe_model, 'haiku'),
190 summarizeEvery: Number.isFinite(every) && every > 0 ? every : 3,
191 owner: text_(fm.owner, 'the user'),
192 timeZone: text_(fm.timezone, 'America/New_York'),
193 dailyNotePattern: text_(fm.daily_note_path, DEFAULT_DAILY_NOTE),
194 signalsDir: text_(fm.signals_dir, DEFAULT_SIGNALS_DIR),
195 reviewDir: text_(fm.review_dir, ''),
196 reviewerCommand: text_(fm.reviewer_command, '').replace(/^\//, ''),
197 memoryFolderName: text_(fm.memory_folder_name, ''),
198 skipDirs: list(fm.skip_dirs, ['.obsidian', '.git', '.trash', 'node_modules']),
199 dailyFrontmatter: list((fm.daily_note_frontmatter ?? '').replace(/;/g, ','), []),
200 // Every guard is off unless the vault owner turns it on: what is off limits is specific to one setup.
201 guardAutoMemory: flag(fm.guard_auto_memory ?? fm.guard_retired_memory, false),
202 guardPaths: list(fm.guard_paths, []),
203 guardN8nDelete: flag(fm.guard_n8n_delete, false),
204 checklist: checklist.length > 0 ? checklist : DEFAULT_CHECKLIST,
205 pushPolicy,
206 guardForcePush: flag(fm.guard_force_push, false),
207 }
208}
209
210/** The project whose paths hold the cwd (longest match). Vault root is `vault`; nothing matched is `unmapped`. */
211export function ownProject(cwd: string, projects: Project[], vaultRoot: string): string {
212 if (norm(cwd) === norm(vaultRoot)) return 'vault'
213 let best = ''
214 let bestLen = -1
215 for (const pr of projects) for (const p of pr.paths) {
216 if (isUnder(cwd, p) && norm(p).length > bestLen) { best = pr.name; bestLen = norm(p).length }
217 }
218 if (best) return best
219 return isUnder(cwd, vaultRoot) ? 'vault' : 'unmapped'
220}
221
222const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
223const wordRe = (w: string) => new RegExp(`(^|[^a-z0-9_.-])${escapeRe(w.toLowerCase())}($|[^a-z0-9_-])`, 'i')
224
225/** Every project a piece of text points at, with the evidence for each tag. */
226export function tagText(text: string, projects: Project[]): Map<string, string[]> {
227 const out = new Map<string, string[]>()
228 const t = text.replace(/\\/g, '/').toLowerCase()
229 const add = (name: string, ev: string) => {
230 const list = out.get(name) ?? []
231 if (!list.includes(ev)) list.push(ev)
232 out.set(name, list)
233 }
234 // Paths: a path that is a prefix of a longer matched path loses (`C:/code/site/blog` beats `C:/code/site`).
235 const hits: { name: string; path: string }[] = []
236 for (const pr of projects) for (const p of pr.paths) {
237 const np = norm(p)
238 if (t.includes(np + '/') || t.endsWith(np) || t.includes(np + ' ') || t.includes(np + '"') || t.includes(np + "'")) {
239 hits.push({ name: pr.name, path: p })
240 }
241 }
242 const occ = (s: string) => t.split(s).length - 1
243 for (const h of hits) {
244 // Shadowed only when every occurrence of the short path is inside a longer, other project's path.
245 const longer = hits.filter(o => o.name !== h.name && norm(o.path).startsWith(norm(h.path) + '/'))
246 const inLonger = longer.reduce((n, o) => n + occ(norm(o.path)), 0)
247 if (longer.length === 0 || occ(norm(h.path)) > inLonger) add(h.name, `path ${h.path}`)
248 }
249 for (const pr of projects) {
250 for (const h of pr.hosts) if (wordRe(h).test(t)) add(pr.name, `host ${h}`)
251 for (const k of pr.keywords) if (wordRe(k).test(t)) add(pr.name, `keyword "${k}"`)
252 }
253 return out
254}
255
256// ---------- guardrails (each one off unless the config note turns it on) ----------
257
258/** How Claude Code names a folder under ~/.claude/projects/: `D:\My Vault` → `d--my-vault`. */
259export const projectDirName = (dir: string) => dir.replace(/[\\/:\s]/g, '-').replace(/-+$/, '').toLowerCase()
260
261export type GuardOptions = { vaultRoot: string; autoMemory: boolean; paths: string[]; n8n: boolean }
262
263/** A write into Claude Code's per-project auto-memory, other than the vault's own folder. */
264export function isAutoMemoryPath(path: string, vaultRoot: string): boolean {
265 const m = /\/\.claude\/projects\/([^/]+)\/memory(\/|$)/.exec(norm(path))
266 return m !== null && m[1] !== projectDirName(vaultRoot)
267}
268
269const guardedFragment = (text: string, paths: string[]) =>
270 paths.find(f => f.trim() !== '' && text.includes(f.replace(/\\/g, '/').toLowerCase()))
271
272const WRITE_VERB =
273 /(\b(set-content|add-content|out-file|new-item|copy-item|move-item|remove-item|rename-item|rm|mv|cp|tee|del|erase|mkdir|touch|rmdir)\b|\bsed\s+-i)/i
274/** Redirect targets (`> file`, `>> "file"`); `2>&1` and fd duplications are not targets. */
275const REDIRECT = /(?:^|[^0-9&>])>{1,2}\s*(?!&)(["']?)([^"'\s|;&]+(?:\s[^"'\s|;&>]+)*?)\1(?=\s|$|[|;&])/g
276
277const AUTO_MEMORY = /\/\.claude\/projects\/([^/\s'"]+)\/memory(\/|\b)/g
278
279const AUTO_MEMORY_WHY = 'that writes into Claude Code\'s per-project auto-memory (~/.claude/projects/<x>/memory/). This vault keeps project memory in the vault itself; write it there.'
280const guardPathWhy = (f: string) => `that writes under "${f}", which this vault's config marks off limits (guard_paths).`
281export const N8N_WHY = 'n8n workflows are archive only here, never deleted. Archive the workflow instead (POST /api/v1/workflows/{id}/archive).'
282
283/** Why a shell command must be refused, or null. */
284export function commandDenyReason(cmd: string, g: GuardOptions): string | null {
285 const c = cmd.replace(/\\/g, '/')
286 if (g.n8n && /\/workflows?\b/i.test(c) &&
287 /(-X\s*['"]?DELETE|--request\s+['"]?DELETE|-Method\s+['"]?Delete|method\s*[:=]\s*['"]DELETE)/i.test(c)) {
288 return N8N_WHY
289 }
290 // A write verb anywhere puts the whole command in scope; a bare redirect only its target.
291 const scope = WRITE_VERB.test(c) ? c.toLowerCase() : [...c.matchAll(REDIRECT)].map(m => m[2] ?? '').join(' ').toLowerCase()
292 if (scope === '') return null
293 if (g.autoMemory) {
294 const own = projectDirName(g.vaultRoot)
295 if ([...scope.matchAll(AUTO_MEMORY)].some(m => m[1] !== own)) return AUTO_MEMORY_WHY
296 }
297 const f = guardedFragment(scope, g.paths)
298 return f ? guardPathWhy(f) : null
299}
300
301/** Why an Edit/Write to this path must be refused, or null. */
302export function pathDenyReason(path: string, g: GuardOptions): string | null {
303 if (g.autoMemory && isAutoMemoryPath(path, g.vaultRoot)) return AUTO_MEMORY_WHY
304 const f = guardedFragment(norm(path), g.paths)
305 return f ? guardPathWhy(f) : null
306}
307
308export const isN8nDeleteTool = (tool: string) => /n8n/i.test(tool) && /delete/i.test(tool)
309
310// ---------- push guard ----------
311
312export type GitPush = { dir: string | null; remote: string; branch: string | null; isForce: boolean }
313
314const unquote = (t: string) => t.replace(/^["']|["']$/g, '')
315const ARG = /"[^"]*"|'[^']*'|[^\s;&|]+/g
316
317/**
318 * The `git push` in a shell command, or null: its folder (`git -C x`, or a `cd x` / `Set-Location x` before it),
319 * remote, branch (from a refspec) and whether it forces.
320 */
321export function parseGitPush(cmd: string): GitPush | null {
322 const c = cmd.replace(/\r?\n/g, ' ')
323 const m = /(?:^|[;&|]\s*|\bthen\s+)\s*git((?:\s+-C\s+(?:"[^"]*"|'[^']*'|\S+)|\s+-c\s+\S+)*)\s+push\b([^;&|]*)/i.exec(c)
324 if (!m) return null
325 const before = c.slice(0, m.index)
326 const cds = [...before.matchAll(/\b(?:cd|set-location|push-location|sl|pushd)\s+(?:-path\s+|-literalpath\s+)?("[^"]*"|'[^']*'|[^\s;&|]+)/gi)]
327 const gitC = /-C\s+("[^"]*"|'[^']*'|\S+)/.exec(m[1] ?? '')
328 const dir = gitC?.[1] ? unquote(gitC[1]) : cds.length > 0 ? unquote(cds[cds.length - 1]?.[1] ?? '') : null
329 const args = [...(m[2] ?? '').matchAll(ARG)].map(a => unquote(a[0]))
330 const positional = args.filter(a => !a.startsWith('-'))
331 const isForce = args.some(a => /^(-f|--force|--force-with-lease(=.*)?|--force-if-includes|--mirror|--delete|-d)$/.test(a)) ||
332 positional.slice(1).some(r => r.startsWith('+') || r.startsWith(':'))
333 const ref = positional[1] ? positional[1].replace(/^\+/, '') : null
334 const branch = ref === null ? null : (ref.includes(':') ? ref.split(':').pop() ?? '' : ref).replace(/^refs\/heads\//, '') || null
335 return { dir: dir || null, remote: positional[0] ?? 'origin', branch: branch === 'HEAD' ? null : branch, isForce }
336}
337
338/** `https://github.com/me/site.git` and `git@github.com:me/site.git` both become `github.com/me/site`. */
339export const normRemote = (url: string) =>
340 url.trim().toLowerCase().replace(/^git@([^:]+):/, '$1/').replace(/^[a-z+]+:\/\/([^@/]*@)?/, '').replace(/\.git$/, '').replace(/\/+$/, '')
341
342/** The most specific rule for this repo and branch: an exact branch beats `*`. A repo is a remote suffix or a path prefix. */
343export function matchPushRule(rules: PushRule[], remoteUrl: string, dir: string, branch: string): PushRule | null {
344 const r = normRemote(remoteUrl)
345 const isRepo = (rule: PushRule) => {
346 const want = rule.repo.toLowerCase().replace(/\.git$/, '').replace(/\/+$/, '')
347 if (/^[a-z]:|^\//i.test(rule.repo)) return isUnder(dir, rule.repo)
348 return r === want || r.endsWith('/' + want)
349 }
350 const hits = rules.filter(x => isRepo(x) && (x.branch === '*' || x.branch.toLowerCase() === branch.toLowerCase()))
351 return hits.find(x => x.branch !== '*') ?? hits[0] ?? null
352}
353
354const APPROVE_PUSH =
355 /\b(ship\s+it|push\s+it|push\s+(?:it\s+)?(?:now|away|live)|deploy\s+it|go\s+ahead\s+and\s+(?:push|deploy|ship)|(?:yes|ok|okay|yep)[,!.\s]+(?:push|deploy|ship)|approved?\s+(?:to\s+)?(?:push|deploy)|send\s+it)\b/i
356
357/** Whether the owner's own prompt approves a held push. A force push also needs the word "force". */
358export const approvesPush = (prompt: string, isForce: boolean) =>
359 APPROVE_PUSH.test(prompt) && (!isForce || /\bforce\b/i.test(prompt))
360
361// ---------- prompts ----------
362
363const TRIVIAL =
364 /^(y|yes|yep|yeah|no|nope|ok|okay|k|go|go on|go ahead|do it|continue|proceed|next|done|thanks|thank you|ty|great|nice|cool|lgtm|ship it|\d{1,3}|[a-e])[\s.!)]*$/i
365export const isTrivial = (text: string) => TRIVIAL.test(text.trim())
366
367const SECRET = [
368 /\b(sk|pk|rk|ghp|gho|ghs|github_pat|xox[abprs]|glpat)[-_][A-Za-z0-9_-]{8,}/gi,
369 /\bAKIA[0-9A-Z]{16}\b/g,
370 /\bAIza[0-9A-Za-z_-]{20,}/g,
371 /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}(\.[A-Za-z0-9_-]+)?/g,
372 /discord(app)?\.com\/api\/webhooks\/\d+\/[A-Za-z0-9_-]+/gi,
373 /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g,
374]
375/** `password: hunter2` keeps its label: `password: [redacted]`. */
376const LABELLED = /\b(password|passwd|passphrase|secret|token|api[ _-]?key)(\s*(?:\bis\b|:|=)\s*)\S{4,}/gi
377
378export function redact(text: string): string {
379 let t = text
380 for (const re of SECRET) t = t.replace(re, '[redacted]')
381 return t.replace(LABELLED, '$1$2[redacted]')
382}
383
384export const clip = (s: string, n: number) => (s.length <= n ? s : s.slice(0, n - 1) + '…')
385
386// ---------- time ----------
387
388export type Stamp = { date: string; time: string; monthFolder: string; dayTitle: string }
389
390const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
391const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
392
393/** Local date/time in America/New_York (the vault's rule), whatever zone the host runs in. */
394export function stampOf(epochMs: number, timeZone = 'America/New_York'): Stamp {
395 let y: number, mo: number, d: number, hh: number, mm: number, wd: number
396 try {
397 const parts = new Intl.DateTimeFormat('en-US', {
398 timeZone, year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', hourCycle: 'h23', weekday: 'short',
399 }).formatToParts(new Date(epochMs))
400 const get = (t: string) => parts.find(p => p.type === t)?.value ?? ''
401 y = Number(get('year')); mo = Number(get('month')); d = Number(get('day'))
402 hh = Number(get('hour')) % 24; mm = Number(get('minute'))
403 wd = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'].indexOf(get('weekday'))
404 if (!y || !mo || !d || wd < 0) throw new Error('parts')
405 } catch {
406 const dt = new Date(epochMs)
407 y = dt.getFullYear(); mo = dt.getMonth() + 1; d = dt.getDate(); hh = dt.getHours(); mm = dt.getMinutes(); wd = dt.getDay()
408 }
409 const p2 = (n: number) => String(n).padStart(2, '0')
410 return {
411 date: `${y}-${p2(mo)}-${p2(d)}`,
412 time: `${p2(hh)}:${p2(mm)}`,
413 monthFolder: `${p2(mo)} - ${MONTHS[mo - 1]} ${y}`,
414 dayTitle: `${DAYS[wd]}, ${MONTHS[mo - 1]} ${d}, ${y}`,
415 }
416}
417
418// ---------- daily note ----------
419
420export function dailyNotePath(vaultRoot: string, s: Stamp, pattern = DEFAULT_DAILY_NOTE): string {
421 const [y = '', mo = '', d = ''] = s.date.split('-')
422 const rel = pattern
423 .replace(/\{YYYY\}/g, y).replace(/\{MM\}/g, mo).replace(/\{DD\}/g, d)
424 .replace(/\{Month\}/g, MONTHS[Number(mo) - 1] ?? mo)
425 return `${vaultRoot.replace(/[\\/]+$/, '')}/${rel.replace(/^[\\/]+/, '')}`
426}
427
428export function newDailyNote(s: Stamp, frontmatter: string[] = []): string {
429 return [
430 '---', ...frontmatter, `created: ${s.date}`, '---', '',
431 `# ${s.dayTitle}`, '', '## Index', '', '---', '',
432 ].join('\n')
433}
434
435export const blockStart = (id: string) => `<!-- vault-scribe:session:${id} -->`
436export const blockEnd = (id: string) => `<!-- /vault-scribe:session:${id} -->`
437export const indexTag = (id: string) => `<!-- vs:${id.slice(0, 8)} -->`
438
439export const countSessions = (note: string) => (note.match(/^## Session \d+/gm) ?? []).length
440
441const bullets = (items: string[]) => (items.length === 0 ? ['-'] : items.map(i => `- ${i}`))
442
443export function renderBlock(a: Agg, vaultRoot: string, signalsLink: string | null, nowTime: string): string {
444 const notes = a.notes.map(n => noteLink(n, vaultRoot)).filter((x): x is string => x !== null)
445 const sig = a.signalIds.length === 0
446 ? ['- none queued']
447 : [`- ${a.signalIds.length} queued for review${signalsLink ? ` → ${signalsLink}` : ''}: ${a.signalIds.map(i => `\`${i}\``).join(', ')}`]
448 return [
449 blockStart(a.sessionId),
450 `## Session ${a.sessionNo} (${a.startedAt}): ${a.topic || 'session'}`,
451 `*auto-written by vault-scribe · session \`${a.sessionId.slice(0, 8)}\` · own project: ${a.own} · cwd \`${fwd(a.cwd)}\` · updated ${nowTime}*`,
452 '',
453 '### What Got Done', ...bullets([...a.done, ...(a.digest.length > 0 ? [`_${a.digest.length} recent turn(s) captured, summary on the next pass_`] : [])]), '',
454 '### Decisions Made', ...bullets(a.decisions), '',
455 '### Discussions & Open Questions', ...bullets(a.discussions ?? []), '',
456 '### Research & Lookups', ...bullets(a.research ?? []), '',
457 "### What's Still In Progress", ...bullets(a.inProgress), '',
458 '### Notes Touched', ...bullets(notes), '',
459 '### Signals for Review', ...sig,
460 blockEnd(a.sessionId),
461 ].join('\n')
462}
463
464/** Puts this session's block and Index bullet into the note, leaving every other byte alone. */
465export function upsertSession(note: string, sessionId: string, block: string, indexLine: string): string {
466 let text = note.replace(/\r\n/g, '\n')
467 const tag = indexTag(sessionId)
468 const bullet = `- ${indexLine} ${tag}`
469
470 // Index bullet
471 const lines = text.split('\n')
472 const at = lines.findIndex(l => l.includes(tag))
473 if (at >= 0) {
474 lines[at] = bullet
475 } else {
476 let idx = lines.findIndex(l => /^## Index\s*$/.test(l))
477 if (idx < 0) {
478 const h1 = lines.findIndex(l => /^# /.test(l))
479 lines.splice(h1 + 1, 0, '', '## Index', '')
480 idx = h1 + 2
481 }
482 // Drop the template's placeholder bullet (and the double blank it leaves).
483 const ph = lines.findIndex((l, i) => i > idx && /^- \*\*Session topic\*\* (—|:|-)/.test(l))
484 if (ph >= 0) {
485 lines.splice(ph, 1)
486 if ((lines[ph - 1] ?? '').trim() === '' && (lines[ph] ?? '').trim() === '') lines.splice(ph, 1)
487 }
488 let end = idx + 1
489 while (end < lines.length && !/^#{1,2} /.test(lines[end] ?? '') && (lines[end] ?? '').trim() !== '---') end++
490 let lastBullet = -1
491 for (let i = idx + 1; i < end; i++) if (/^\s*- /.test(lines[i] ?? '')) lastBullet = i
492 if (lastBullet >= 0) {
493 lines.splice(lastBullet + 1, 0, bullet)
494 } else {
495 let ins = end
496 while (ins > idx + 1 && (lines[ins - 1] ?? '').trim() === '') ins--
497 const add = [(lines[ins - 1] ?? '').trim() === '' ? null : '', bullet].filter((x): x is string => x !== null)
498 if ((lines[ins] ?? '').trim() !== '') add.push('')
499 lines.splice(ins, 0, ...add)
500 }
501 }
502 text = lines.join('\n')
503
504 // Session block
505 const s = text.indexOf(blockStart(sessionId))
506 const e = text.indexOf(blockEnd(sessionId))
507 if (s >= 0 && e > s) {
508 text = text.slice(0, s) + block + text.slice(e + blockEnd(sessionId).length)
509 } else {
510 text = text.replace(/\s*$/, '') + '\n\n' + block + '\n\n---\n'
511 }
512 return text
513}
514
515// ---------- signals ----------
516
517export const signalsPath = (vaultRoot: string, date: string, dir = DEFAULT_SIGNALS_DIR) =>
518 `${vaultRoot.replace(/[\\/]+$/, '')}/${dir.replace(/^[\\/]+|[\\/]+$/g, '')}/${date}.md`
519
520export function newSignalsFile(date: string): string {
521 return [
522 '---', 'status: active', 'project: meta', 'type: log', `created: ${date}`, '---', '',
523 `# vault-scribe signals: ${date}`, '',
524 'Cross-project changes, decisions, corrections and discussions captured live by the vault-scribe mod, queued for review.',
525 'The reviewer flips each heading from `⏳ pending` to `✅ processed <date>: ...`, `🔁 routed <date>: ...` or `✖ dropped <date>: ...` on the same line, so the edit is line-neutral.', '',
526 ].join('\n')
527}
528
529export function renderSignal(s: Signal): string {
530 return [
531 `### ${s.id} · ${s.status}`,
532 `- kind: ${s.kind}`,
533 `- what: ${s.what}`,
534 `- why: ${s.why || '-'}`,
535 `- from: ${s.from}`,
536 `- touches: ${s.touches.join('; ')}`,
537 '',
538 ].join('\n')
539}
540
541export function parseSignals(text: string): Signal[] {
542 const out: Signal[] = []
543 const blocks = text.replace(/\r\n/g, '\n').split(/^### /m).slice(1)
544 for (const b of blocks) {
545 const [head = '', ...rest] = b.split('\n')
546 const hm = /^(SIG-\d{4}-\d{2}-\d{2}-([0-9a-z]{8})-\d+)\s*·\s*(.*)$/i.exec(head.trim())
547 if (!hm || !hm[1] || !hm[2]) continue
548 const field = (k: string) => rest.find(l => l.startsWith(`- ${k}:`))?.slice(k.length + 3).trim() ?? ''
549 const touches = field('touches').split(';').map(x => x.trim()).filter(Boolean)
550 out.push({
551 id: hm[1], sessionId: hm[2], status: (hm[3] ?? '').trim(),
552 kind: (field('kind') || 'change') as SignalKind,
553 what: field('what'), why: field('why'), from: field('from'), touches,
554 })
555 }
556 return out
557}
558
559/** The project name of a `touches` entry: `Website (path C:/code/site)` → `Website`. */
560export const touchName = (t: string) => t.replace(/\s*\(.*$/, '').trim()
561
562export const isPending = (s: Signal) => s.status.startsWith('⏳')
563
564// ---------- summary ----------
565
566export function digestText(turns: TurnRec[], maxChars = 14000): string {
567 const parts: string[] = []
568 turns.forEach((t, i) => {
569 parts.push(`## Turn ${i + 1}`)
570 if (t.prompt) parts.push(`USER: ${clip(t.prompt, 1500)}`)
571 for (const a of t.actions.slice(0, 40)) parts.push(`ACTION: ${clip(a, a.startsWith('research report') ? 1200 : 300)}`)
572 if (t.actions.length > 40) parts.push(`ACTION: …and ${t.actions.length - 40} more`)
573 if (t.answer) parts.push(`ASSISTANT: ${clip(t.answer, 1500)}`)
574 })
575 const all = parts.join('\n')
576 return all.length <= maxChars ? all : all.slice(all.length - maxChars)
577}
578
579export function summaryPrompt(digest: string, projects: Project[], own: string, owner = 'the user'): { system: string; prompt: string } {
580 const list = projects.map(p => `- ${p.name}: memory ${p.memory}; paths ${p.paths.join(', ')}; hosts ${p.hosts.join(', ') || 'none'}; keywords ${p.keywords.join(', ')}`).join('\n')
581 const system = [
582 `You keep the work log for ${owner}'s Claude Code sessions, part of their second brain. You read a digest of recent turns and answer with ONE JSON object and nothing else.`,
583 `In the digest, USER lines are ${owner}'s own words. ACTION lines are what the assistant did: edits with before/after, writes, shell commands, reads (lines starting "read"), web searches/fetches/pages opened, and research-agent reports. ASSISTANT lines are its answers.`,
584 'Write past tense, terse, factual. Never invent: only what the digest shows. Never include secrets, passwords or tokens.',
585 'Style: never use em-dashes (use a colon, comma or period instead) and write every file path with forward slashes.',
586 'Fields:',
587 '- topic: 2-6 words naming what this session is about',
588 '- index_line: one line, "**Topic**: what happened/was decided" (past tense, under 160 chars)',
589 '- done: up to 8 bullets of concrete changes completed in THESE turns. Each bullet names WHAT changed (file or system) and WHY, as "what (why: reason)". The why comes from the USER\'s words or the reason the assistant gave.',
590 `- decisions: up to 4 bullets, ONLY decisions or rulings ${owner} made. Each MUST end with the exact words from a USER line that state or accept it, as (${owner}: "exact words"). A plan the assistant proposed that the USER has not clearly accepted is NOT a decision: put it under discussions.`,
591 '- discussions: up to 5 bullets that could affect LATER choices, so they are not lost: options weighed, proposals still waiting on the USER, ideas rejected and why, trade-offs, contradictions found.',
592 '- research: up to 6 bullets for reads and lookups that informed a choice: what was read or looked up (files, a whole project\'s notes, web searches, pages, agent reports), the key finding, and what it informed. Keep URLs as sources, e.g. "searched X: finding (source: https://...)". Skip trivial reads that informed nothing.',
593 '- in_progress: up to 4 bullets still open at the end of these turns',
594 '- signals: things another project\'s notes would need to know. Each {"kind":"change|decision|correction|fact|discussion","what":"...","why":"...","projects":["<exact project names from the list>"],"quote":"<for decision/correction: the exact USER words, else empty>"}. Use change/fact/discussion when it affects a project OTHER than the session\'s own, even indirectly (shared API, host, database, webhook, rule, convention, or research one project did that another project needs). Use decision/correction only with a USER quote. Empty list when nothing qualifies.',
595 `The session's own project is: ${own}.`,
596 'Projects:',
597 list,
598 ].join('\n')
599 const prompt = `<digest>\n${digest}\n</digest>\nAnswer with the JSON object only.`
600 return { system, prompt }
601}
602
603/** House style: no em-dashes, forward-slash paths. */
604const tidy = (s: string) => s.replace(/\s*\u2014\s*/g, ': ').replace(/\\/g, '/')
605const str = (v: unknown, n: number) => (typeof v === 'string' ? clip(tidy(redact(v.trim())), n) : '')
606const strs = (v: unknown, max: number, n: number) =>
607 Array.isArray(v) ? v.map(x => str(x, n)).filter(x => x !== '').slice(0, max) : []
608
609export function parseSummary(text: string, projectNames: string[]): Summary | null {
610 const json = /\{[\s\S]*\}/.exec(text)?.[0]
611 if (!json) return null
612 let o: Record<string, unknown>
613 try { o = JSON.parse(json) as Record<string, unknown> } catch { return null }
614 const names = new Map(projectNames.map(n => [n.toLowerCase(), n]))
615 const kinds: SignalKind[] = ['change', 'decision', 'correction', 'fact', 'discussion']
616 const signals = Array.isArray(o.signals) ? o.signals.flatMap(s => {
617 if (typeof s !== 'object' || s === null) return []
618 const r = s as Record<string, unknown>
619 const kind = kinds.includes(r.kind as SignalKind) ? (r.kind as SignalKind) : 'change'
620 const what = str(r.what, 300)
621 if (what === '') return []
622 const projects = Array.isArray(r.projects)
623 ? [...new Set(r.projects.map(p => names.get(String(p).toLowerCase())).filter((x): x is string => x !== undefined))]
624 : []
625 return [{ kind, what, why: str(r.why, 300), projects, quote: str(r.quote, 300) }]
626 }).slice(0, 8) : []
627 return {
628 topic: str(o.topic, 60),
629 indexLine: str(o.index_line, 200),
630 done: strs(o.done, 8, 300),
631 decisions: strs(o.decisions, 4, 300),
632 inProgress: strs(o.in_progress, 4, 240),
633 discussions: strs(o.discussions, 5, 300),
634 research: strs(o.research, 6, 360),
635 signals,
636 }
637}
638
639const dedupe = (xs: string[], max: number) => {
640 const seen = new Set<string>()
641 const out: string[] = []
642 for (const x of xs) { const k = x.toLowerCase(); if (!seen.has(k)) { seen.add(k); out.push(x) } }
643 return out.slice(-max)
644}
645
646const squash = (t: string) => t.toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim()
647
648/** True when one of the quoted strings in `text` really appears in what the owner typed. */
649export function hasVerifiedQuote(text: string, ownerWords: string[]): boolean {
650 const said = squash(ownerWords.join(' \n '))
651 const quotes = [...text.matchAll(/["\u201c]([^"\u201d]{4,})["\u201d]/g)].map(m => squash(m[1] ?? ''))
652 return quotes.some(q => q.length >= 4 && said.includes(q))
653}
654
655/**
656 * A decision only counts when it quotes the owner. Unverifiable decisions become discussions, and an
657 * unverifiable decision/correction signal becomes a discussion signal (2026-10-08: proposals were logged as rulings).
658 */
659export function checkQuotes(s: Summary, ownerWords: string[]): Summary {
660 const kept: string[] = []
661 const moved: string[] = []
662 for (const d of s.decisions) (hasVerifiedQuote(d, ownerWords) ? kept : moved).push(d)
663 return {
664 ...s,
665 decisions: kept,
666 discussions: [...s.discussions, ...moved.map(m => `proposed, not confirmed: ${m}`)],
667 signals: s.signals.map(x =>
668 (x.kind === 'decision' || x.kind === 'correction') && !hasVerifiedQuote(`"${x.quote}"`, ownerWords)
669 ? { ...x, kind: 'discussion' as const }
670 : x),
671 }
672}
673
674export function mergeSummary(a: Agg, s: Summary): Agg {
675 return {
676 ...a,
677 topic: a.topic || s.topic,
678 indexLine: s.indexLine || a.indexLine,
679 done: dedupe([...a.done, ...s.done], 20),
680 decisions: dedupe([...a.decisions, ...s.decisions], 12),
681 discussions: dedupe([...(a.discussions ?? []), ...s.discussions], 15),
682 research: dedupe([...(a.research ?? []), ...s.research], 20),
683 inProgress: s.inProgress.length > 0 ? s.inProgress : a.inProgress,
684 }
685}
686
687/** Signals worth queueing for review: anything touching another project, plus every decision/correction. */
688export function shouldQueue(kind: SignalKind, projects: string[], own: string): boolean {
689 if (kind === 'decision' || kind === 'correction') return true
690 return projects.some(p => p !== own)
691}
692
693// ---------- citations ----------
694
695export type Citation = { file: string; line: number; target: string; cited: number }
696
697const CITE = /([^\s`'"()[\]|<>*]+\.md):(\d+)(?:-(\d+))?/g
698
699/** Every `name.md:NNN` citation in a note. `target` is the cited path as written (lowercase, forward slashes, leading ./ and ../ dropped). */
700export function extractCitations(file: string, text: string): Citation[] {
701 const out: Citation[] = []
702 text.split(/\r?\n/).forEach((l, i) => {
703 for (const m of l.matchAll(CITE)) {
704 const target = norm(m[1] ?? '').replace(/^(\.\.?\/)+/, '')
705 out.push({ file, line: i + 1, target, cited: Number(m[2]) })
706 }
707 })
708 return out
709}
710
711const baseName = (p: string) => norm(p).split('/').pop() ?? ''
712const dirName = (p: string) => norm(p).split('/').slice(0, -1).join('/')
713
714/**
715 * Does citation `c` point at `editedFile`? A cited path with a folder in it must match the
716 * edited path's tail, folder by folder. A bare name means the citing note's own folder,
717 * else the vault-wide file of that name, but only when `nameCounts` says it is unique.
718 */
719export function citesFile(c: Citation, editedFile: string, nameCounts?: Map<string, number>): boolean {
720 const edited = norm(editedFile)
721 if (c.target.includes('/')) return edited === c.target || edited.endsWith(`/${c.target}`)
722 if (baseName(edited) !== c.target) return false
723 return dirName(edited) === dirName(c.file) || nameCounts?.get(c.target) === 1
724}
725
726export function firstDiffLine(before: string, after: string): number {
727 const a = before.split(/\r?\n/)
728 const b = after.split(/\r?\n/)
729 const n = Math.min(a.length, b.length)
730 for (let i = 0; i < n; i++) if (a[i] !== b[i]) return i + 1
731 return n + 1
732}
733
734export const lineCount = (t: string) => t.split(/\r?\n/).length
735
736/** Lowercase basename → how many vault files carry it; feeds the bare-name fallback in `citesFile`. */
737export function countNames(paths: string[]): Map<string, number> {
738 const counts = new Map<string, number>()
739 for (const p of paths) counts.set(baseName(p), (counts.get(baseName(p)) ?? 0) + 1)
740 return counts
741}
742
743export function staleCitations(index: Citation[], editedFile: string, fromLine: number, delta: number, nameCounts?: Map<string, number>): Citation[] {
744 if (delta === 0 || baseName(editedFile) === '') return []
745 return index.filter(c => c.cited >= fromLine && norm(c.file) !== norm(editedFile) && citesFile(c, editedFile, nameCounts))
746}
747types/index.d.ts 9 lines1export type HeadsUpItem = { id: string; what: string; from: string; kind: string }
2export type HeadsUp = { project: string; items: HeadsUpItem[]; total: number }
3
4declare module 'claude-code' {
5 interface PluginState {
6 'vault-scribe': { headsUp: HeadsUp | null; isDismissed: boolean }
7 }
8}
9