pi-weave 0.1.11 → 0.1.13
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 +148 -132
- package/package.json +1 -2
- package/src/core/cache/workspace.ts +26 -9
- package/src/core/concurrency.ts +3 -6
- package/src/core/frontmatter.ts +0 -53
- package/src/core/graph/build.ts +12 -8
- package/src/core/graph/current.ts +4 -6
- package/src/core/graph/model.ts +1 -1
- package/src/core/graph/wikilinks.ts +3 -3
- package/src/core/index.ts +26 -27
- package/src/core/paths.ts +0 -7
- package/src/core/slug.ts +13 -0
- package/src/core/types.ts +1 -0
- package/src/core/vault.ts +45 -467
- package/src/core/view/detail.ts +1 -1
- package/src/core/view/health.ts +1 -1
- package/src/core/view/tree.ts +15 -3
- package/src/pi/index.ts +6 -85
- package/src/pi/summarize.ts +2 -2
- package/src/pi/viewer/tui/bodyStore.ts +4 -7
- package/src/pi/viewer/tui/branding.ts +7 -148
- package/src/pi/viewer/tui/run.ts +3 -17
- package/src/pi/viewer/tui/surface/base.ts +24 -3
- package/src/pi/viewer/tui/surface/explore.ts +41 -6
- package/src/pi/viewer/tui/workspace.ts +23 -351
- package/src/pi/viewer/tui/workspaceRoot.ts +31 -172
- package/src/pi/viewer/web/run.ts +7 -117
- package/src/web/client/api.dom.ts +2 -2
- package/src/web/client/api.ts +14 -176
- package/src/web/client/bootstrap.ts +5 -14
- package/src/web/client/context/context.model.ts +9 -11
- package/src/web/client/dist/app.js +77 -167
- package/src/web/client/graph/dynamics.ts +5 -65
- package/src/web/client/graph/renderer.dom.ts +7 -8
- package/src/web/client/graph/renderer.ts +9 -35
- package/src/web/client/main.tsx +1 -1
- package/src/web/client/note/Note.tsx +37 -60
- package/src/web/client/note/note.model.ts +23 -0
- package/src/web/client/search/SearchPalette.tsx +45 -36
- package/src/web/client/search/search.model.ts +33 -454
- package/src/web/client/shell/Columns.tsx +13 -81
- package/src/web/client/shell/Header.tsx +2 -10
- package/src/web/client/shell/Shell.tsx +50 -123
- package/src/web/client/shell/StatusBar.tsx +1 -4
- package/src/web/client/shell/icons.model.ts +4 -7
- package/src/web/client/shell/keys.model.ts +5 -42
- package/src/web/client/shell/keys.ts +2 -2
- package/src/web/client/shell/shell.model.ts +10 -133
- package/src/web/client/shell/theme.model.ts +2 -2
- package/src/web/client/shell/theme.ts +33 -121
- package/src/web/client/state.ts +9 -89
- package/src/web/client/tree/Tree.tsx +24 -97
- package/src/web/client/tree/tree.model.ts +8 -56
- package/src/web/client/workspace.ts +72 -242
- package/src/web/server/page.ts +8 -10
- package/src/web/server/routes.ts +30 -415
- package/src/web/server/server.ts +6 -145
- package/src/web/shared/layout.ts +72 -624
- package/src/web/shared/wire.ts +10 -178
- package/src/core/sessions.ts +0 -929
- package/src/pi/sessionScan.ts +0 -104
- package/src/pi/viewer/tui/explorer.ts +0 -586
- package/src/web/client/live.model.ts +0 -275
- package/src/web/client/live.ts +0 -151
- package/src/web/client/note/Editor.tsx +0 -109
- package/src/web/client/note/editor.controller.ts +0 -151
- package/src/web/client/note/editor.model.ts +0 -686
- package/src/web/client/search/search.ts +0 -107
- package/src/web/client/shell/Divider.tsx +0 -44
- package/src/web/client/shell/cssvars.ts +0 -70
- package/src/web/client/shell/drag.model.ts +0 -170
- package/src/web/client/shell/layout.model.ts +0 -500
- package/src/web/client/shell/viewport.ts +0 -29
- package/src/web/server/sse.ts +0 -321
- package/src/web/server/watcher.ts +0 -507
package/src/core/vault.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import { existsSync
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
2
|
import { promises as fs } from "node:fs";
|
|
3
3
|
import { dirname, isAbsolute, join, relative } from "node:path";
|
|
4
4
|
import {
|
|
5
|
-
MANAGED_FRONT_MATTER_KEYS,
|
|
6
5
|
parseFrontMatter,
|
|
7
6
|
parseNoteFile,
|
|
8
|
-
quoteField,
|
|
9
7
|
serializeNote,
|
|
10
|
-
upsertFrontMatterFields,
|
|
11
8
|
} from "./frontmatter";
|
|
12
9
|
import { withMutationQueue } from "./mutex";
|
|
13
10
|
import { NOTES_DIR, OKF_MANIFEST } from "./paths";
|
|
@@ -74,18 +71,16 @@ function notePath(root: string, slug: string): string {
|
|
|
74
71
|
* Resolve a note slug to its on-disk path, or null when the slug is unsafe.
|
|
75
72
|
* Slugs arrive from tool parameters, so they are untrusted: `../x` and
|
|
76
73
|
* absolute escapes must never read or write outside `<vault>/notes/`
|
|
77
|
-
* (subdirectories *within* it are legitimate
|
|
74
|
+
* (subdirectories *within* it are legitimate for nested notes).
|
|
78
75
|
*/
|
|
79
76
|
export function resolveNotePath(root: string, slug: string): string | null {
|
|
80
77
|
if (slug.trim().length === 0) return null;
|
|
81
78
|
const notesDir = join(root, NOTES_DIR);
|
|
82
79
|
const candidate = join(notesDir, `${slug}.md`);
|
|
83
80
|
const rel = relative(notesDir, candidate);
|
|
84
|
-
// Slugs may nest
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
// outside notes/ and are rejected here, at the one door every read and
|
|
88
|
-
// write walks through.
|
|
81
|
+
// Slugs may nest, but they may never escape the collection: `..` segments
|
|
82
|
+
// and absolute paths resolve outside notes/ and are rejected here, at the
|
|
83
|
+
// one door every read and write walks through.
|
|
89
84
|
if (rel.startsWith("..") || isAbsolute(rel) || rel.length === 0) return null;
|
|
90
85
|
return candidate;
|
|
91
86
|
}
|
|
@@ -158,8 +153,8 @@ async function writeNote(
|
|
|
158
153
|
frontMatter: NoteFrontMatter | undefined,
|
|
159
154
|
): Promise<Note> {
|
|
160
155
|
const text = serializeNote(meta, body, frontMatter);
|
|
161
|
-
// Path-slugs
|
|
162
|
-
//
|
|
156
|
+
// Path-slugs may introduce a new subdirectory; every write path funnels
|
|
157
|
+
// through here, so this is the one mkdir that matters.
|
|
163
158
|
await fs.mkdir(dirname(path), { recursive: true });
|
|
164
159
|
await fs.writeFile(path, text, "utf8");
|
|
165
160
|
const parsed = parseNoteFile(text);
|
|
@@ -188,17 +183,9 @@ const LOCK_NS = "vault:note:";
|
|
|
188
183
|
/**
|
|
189
184
|
* Run `task` with exclusive access to every given note path.
|
|
190
185
|
*
|
|
191
|
-
* Paths are locked in a fixed
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* `b→a`) would otherwise be able to take one lock each and wait forever;
|
|
195
|
-
* with a total order on acquisition, one of them takes both and the other
|
|
196
|
-
* takes neither.
|
|
197
|
-
*
|
|
198
|
-
* Every mutation in this module goes through here, not just the new ones. A
|
|
199
|
-
* queue that half the writers ignore serializes nothing: an `appendToNote`
|
|
200
|
-
* racing an `updateNote` on the same file is a lost update whether or not
|
|
201
|
-
* the update took a lock.
|
|
186
|
+
* Paths are locked in a fixed order so callers that need multiple locks cannot
|
|
187
|
+
* deadlock. Every mutation in this module goes through here, keeping appends
|
|
188
|
+
* and finalization serialized with one another.
|
|
202
189
|
*/
|
|
203
190
|
function withNoteLocks<T>(paths: readonly string[], task: () => Promise<T>): Promise<T> {
|
|
204
191
|
const ordered = [...new Set(paths)].sort();
|
|
@@ -344,8 +331,8 @@ export async function finalizeNote(
|
|
|
344
331
|
const structured = input.body.trim();
|
|
345
332
|
// A note whose body carries no `## Raw` marker yet is treated as *all*
|
|
346
333
|
// raw: the entire pre-finalize body is preserved verbatim beneath the
|
|
347
|
-
// restructured body as a freshly created tail
|
|
348
|
-
//
|
|
334
|
+
// restructured body as a freshly created tail so the user's words are
|
|
335
|
+
// never silently destroyed by finalization.
|
|
349
336
|
const body =
|
|
350
337
|
structured +
|
|
351
338
|
(rawTail !== ""
|
|
@@ -358,447 +345,21 @@ export async function finalizeNote(
|
|
|
358
345
|
});
|
|
359
346
|
}
|
|
360
347
|
|
|
361
|
-
// ---------------------------------------------------------------------------
|
|
362
|
-
// Mutation APIs (weave-workspace §11 P5.2, P5.3)
|
|
363
|
-
// ---------------------------------------------------------------------------
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* A note plus the stamp that identifies the on-disk state it was read from.
|
|
367
|
-
*
|
|
368
|
-
* The read half of the conflict primitive (§11 P5.3). An editor reads this,
|
|
369
|
-
* holds `revision` for as long as the user is typing, and hands it back on
|
|
370
|
-
* save; a `revision` that no longer matches the file means someone else — a
|
|
371
|
-
* `weave_note` tool call, `$EDITOR`, an Obsidian sync — wrote in between.
|
|
372
|
-
*/
|
|
373
|
-
export interface RevisionedNote {
|
|
374
|
-
note: Note;
|
|
375
|
-
/**
|
|
376
|
-
* Opaque version stamp. **Compare it, do not interpret it.**
|
|
377
|
-
*
|
|
378
|
-
* It is currently `mtimeMs:size`, and the shape is deliberately not part
|
|
379
|
-
* of the contract: a caller that parses out the mtime is a caller that
|
|
380
|
-
* breaks when this becomes a content hash. Two fields rather than one
|
|
381
|
-
* because mtime alone has a real blind spot — the filesystem timestamp
|
|
382
|
-
* granularity. Two writes inside the same millisecond are indistinguishable
|
|
383
|
-
* by mtime, and "same millisecond" is not exotic when the writer is a
|
|
384
|
-
* program rather than a human; a size change catches the common case of
|
|
385
|
-
* such a pair differing in length.
|
|
386
|
-
*
|
|
387
|
-
* This does not make it a perfect detector. A same-millisecond write that
|
|
388
|
-
* preserves the byte count is invisible, which is the honest limitation of
|
|
389
|
-
* any stat-based scheme, and the reason this is typed as opaque: upgrading
|
|
390
|
-
* it to a digest is then a change here and nowhere else.
|
|
391
|
-
*/
|
|
392
|
-
revision: string;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
function revisionOf(st: { mtimeMs: number; size: number }): string {
|
|
396
|
-
return `${st.mtimeMs}:${st.size}`;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Read a note together with its {@link RevisionedNote.revision}.
|
|
401
|
-
*
|
|
402
|
-
* Stat-then-read rather than read-then-stat: if a writer lands between the
|
|
403
|
-
* two calls, the revision is of the *older* state than the content, so the
|
|
404
|
-
* save that follows sees a mismatch and is rejected. The opposite order
|
|
405
|
-
* yields a revision newer than the content, which would let a stale body be
|
|
406
|
-
* written back under a revision that looks current — failing safe versus
|
|
407
|
-
* failing silently.
|
|
408
|
-
*/
|
|
409
|
-
export async function getNoteWithRevision(root: string, slug: string): Promise<RevisionedNote | null> {
|
|
410
|
-
const path = resolveNotePath(root, slug);
|
|
411
|
-
if (!path) return null;
|
|
412
|
-
let revision: string;
|
|
413
|
-
try {
|
|
414
|
-
revision = revisionOf(await fs.stat(path));
|
|
415
|
-
} catch {
|
|
416
|
-
return null;
|
|
417
|
-
}
|
|
418
|
-
const note = await getNote(root, slug);
|
|
419
|
-
return note === null ? null : { note, revision };
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Why a mutation did not happen. The server maps these to status codes —
|
|
424
|
-
* `"missing"` → 404, `"conflict"` → 409, `"collision"` → 409 — but the
|
|
425
|
-
* mapping is the server's business; core reports the *situation* (§11 P5.3).
|
|
426
|
-
*/
|
|
427
|
-
export type MutationFailure =
|
|
428
|
-
/** No such note, or a slug that failed the traversal guard. */
|
|
429
|
-
| { ok: false; reason: "missing" }
|
|
430
|
-
/** The file moved since `expectedRevision` was read. */
|
|
431
|
-
| { ok: false; reason: "conflict"; current: RevisionedNote }
|
|
432
|
-
/** A rename whose destination slug is already taken. */
|
|
433
|
-
| { ok: false; reason: "collision"; slug: string };
|
|
434
|
-
|
|
435
|
-
export type MutationResult = { ok: true; note: Note } | MutationFailure;
|
|
436
|
-
|
|
437
|
-
/** A successful delete, or why it did not happen. */
|
|
438
|
-
export type DeleteResult = { ok: true } | { ok: false; reason: "missing" };
|
|
439
|
-
|
|
440
|
-
export interface UpdateNoteInput {
|
|
441
|
-
/** Replacement Markdown body. Omit to change only metadata. */
|
|
442
|
-
body?: string;
|
|
443
|
-
/**
|
|
444
|
-
* Metadata to merge over the note's current values. `created` is not
|
|
445
|
-
* accepted: it records when the note came into existence, and an edit is
|
|
446
|
-
* not a re-creation.
|
|
447
|
-
*/
|
|
448
|
-
meta?: Partial<Pick<NoteMeta, "title" | "tags" | "source">>;
|
|
449
|
-
/**
|
|
450
|
-
* The {@link RevisionedNote.revision} the caller last read. When supplied
|
|
451
|
-
* and no longer current, the write is refused with `reason: "conflict"`
|
|
452
|
-
* and the caller is handed the note as it now is, so a UI can offer
|
|
453
|
-
* reload-or-overwrite without a second round trip. Omit for
|
|
454
|
-
* last-write-wins.
|
|
455
|
-
*/
|
|
456
|
-
expectedRevision?: string;
|
|
457
|
-
/** Injectable clock for tests. */
|
|
458
|
-
now?: Date;
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Check `expectedRevision` against the file, inside the caller's lock.
|
|
463
|
-
*
|
|
464
|
-
* Returns the conflict to report, or null to proceed. Being inside the lock
|
|
465
|
-
* is the whole point: a check-then-write with the check outside the mutex is
|
|
466
|
-
* a race with a wider window than no check at all, because it looks like it
|
|
467
|
-
* is doing something.
|
|
468
|
-
*/
|
|
469
|
-
async function checkRevision(
|
|
470
|
-
root: string,
|
|
471
|
-
slug: string,
|
|
472
|
-
expected: string | undefined,
|
|
473
|
-
): Promise<MutationFailure | null> {
|
|
474
|
-
if (expected === undefined) return null;
|
|
475
|
-
const current = await getNoteWithRevision(root, slug);
|
|
476
|
-
if (current === null) return { ok: false, reason: "missing" };
|
|
477
|
-
return current.revision === expected ? null : { ok: false, reason: "conflict", current };
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Update a note's body and/or metadata in place.
|
|
482
|
-
*
|
|
483
|
-
* Preserves unknown front-matter keys (they ride on `note.frontMatter`
|
|
484
|
-
* through `writeNote`) and, when `body` is given, the append-only `## Raw`
|
|
485
|
-
* tail: the replacement body is treated as the *editorial* region above the
|
|
486
|
-
* tail, exactly as `finalizeNote` treats it. A caller replacing the body of
|
|
487
|
-
* a dictated note therefore cannot delete the user's verbatim scribbles by
|
|
488
|
-
* omitting them, which is the one thing `docs/notepad.md` §4 says must never
|
|
489
|
-
* happen. A body that already carries its own `## Raw` tail is written as
|
|
490
|
-
* given, so a round-trip through an editor that shows the whole file is not
|
|
491
|
-
* penalised with a duplicated tail.
|
|
492
|
-
*
|
|
493
|
-
* `updated` is bumped on every successful call, including a metadata-only
|
|
494
|
-
* one — a tag change is a change to the note.
|
|
495
|
-
*/
|
|
496
|
-
export async function updateNote(
|
|
497
|
-
root: string,
|
|
498
|
-
slug: string,
|
|
499
|
-
input: UpdateNoteInput,
|
|
500
|
-
now: Date = new Date(),
|
|
501
|
-
): Promise<MutationResult> {
|
|
502
|
-
const path = resolveNotePath(root, slug);
|
|
503
|
-
if (!path) return { ok: false, reason: "missing" };
|
|
504
|
-
return withNoteLocks([path], async () => {
|
|
505
|
-
const conflict = await checkRevision(root, slug, input.expectedRevision);
|
|
506
|
-
if (conflict) return conflict;
|
|
507
|
-
const note = await getNote(root, slug);
|
|
508
|
-
if (!note) return { ok: false, reason: "missing" };
|
|
509
|
-
|
|
510
|
-
const body = input.body === undefined ? note.body : preserveRawTail(note.body, input.body);
|
|
511
|
-
const meta: NoteMeta = {
|
|
512
|
-
...note,
|
|
513
|
-
...input.meta,
|
|
514
|
-
updated: (input.now ?? now).toISOString(),
|
|
515
|
-
};
|
|
516
|
-
return { ok: true, note: await writeNote(path, slug, meta, body, note.frontMatter) };
|
|
517
|
-
});
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
/** Re-attach the existing `## Raw` tail unless the replacement already has one. */
|
|
521
|
-
function preserveRawTail(currentBody: string, nextBody: string): string {
|
|
522
|
-
const tail = extractRawTail(currentBody);
|
|
523
|
-
if (tail === "" || extractRawTail(nextBody) !== "") return nextBody.trim();
|
|
524
|
-
return nextBody.trim() + `\n\n${tail}`;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
/**
|
|
528
|
-
* Rename a note: move `oldSlug.md` to `newSlug.md`.
|
|
529
|
-
*
|
|
530
|
-
* `newSlug` is passed through `slugify`, so a caller may hand over either a
|
|
531
|
-
* slug or a human title and get the same filesystem-safe result the rest of
|
|
532
|
-
* the vault uses.
|
|
533
|
-
*
|
|
534
|
-
* ## Inbound `[[wikilinks]]` are deliberately NOT rewritten
|
|
535
|
-
*
|
|
536
|
-
* A rename can break links from other notes, and there are two honest
|
|
537
|
-
* options. Rewriting every referring note is the bigger hammer, and it is
|
|
538
|
-
* the wrong one here:
|
|
539
|
-
*
|
|
540
|
-
* - **It is a multi-file write with no transaction.** Renaming one note
|
|
541
|
-
* would rewrite N others; a failure partway leaves the vault half-updated,
|
|
542
|
-
* and there is no rollback. Trading one dangling link for an unknown
|
|
543
|
-
* number of half-edited files is a bad trade.
|
|
544
|
-
* - **It edits prose to fix an index.** A wikilink lives in body text a
|
|
545
|
-
* human wrote, sometimes inside a quote, a code fence, or a `## Raw` tail
|
|
546
|
-
* that `docs/notepad.md` declares append-only and verbatim. A textual
|
|
547
|
-
* substitution across the vault cannot honour that; a rename would become
|
|
548
|
-
* the one operation allowed to modify preserved user input.
|
|
549
|
-
* - **The alternative is already visible, not silent.** Dangling targets are
|
|
550
|
-
* a first-class concept: `buildGraph` collects `danglingLinks`, the wire
|
|
551
|
-
* payload ships them as `dangling`, and the note column renders an
|
|
552
|
-
* unresolved wikilink as an unfollowable ghost. A stale link therefore
|
|
553
|
-
* shows up in the UI as something to fix, which is a better failure than a
|
|
554
|
-
* silent bulk edit the user cannot review.
|
|
555
|
-
*
|
|
556
|
-
* So: renaming leaves inbound links pointing at the old slug, where they
|
|
557
|
-
* render as dangling. If link-following-a-rename is wanted later, the right
|
|
558
|
-
* shape is an explicit, previewable "update N referring notes?" step — a
|
|
559
|
-
* separate operation the user opts into, not a side effect of this one.
|
|
560
|
-
*/
|
|
561
|
-
export async function renameNote(
|
|
562
|
-
root: string,
|
|
563
|
-
oldSlug: string,
|
|
564
|
-
newSlug: string,
|
|
565
|
-
now: Date = new Date(),
|
|
566
|
-
): Promise<MutationResult> {
|
|
567
|
-
const from = resolveNotePath(root, oldSlug);
|
|
568
|
-
if (!from) return { ok: false, reason: "missing" };
|
|
569
|
-
const target = slugify(newSlug);
|
|
570
|
-
const to = resolveNotePath(root, target);
|
|
571
|
-
// `slugify` cannot emit a traversing slug, but the guard is applied anyway:
|
|
572
|
-
// "this input is already safe" is exactly the assumption that stops being
|
|
573
|
-
// true when someone changes the other function.
|
|
574
|
-
if (!to) return { ok: false, reason: "missing" };
|
|
575
|
-
if (target === oldSlug) {
|
|
576
|
-
const note = await getNote(root, oldSlug);
|
|
577
|
-
return note === null ? { ok: false, reason: "missing" } : { ok: true, note };
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
return withNoteLocks([from, to], async () => {
|
|
581
|
-
const note = await getNote(root, oldSlug);
|
|
582
|
-
if (!note) return { ok: false, reason: "missing" };
|
|
583
|
-
// Refuse rather than uniquify: `addNote` may silently pick `decision-2`
|
|
584
|
-
// because nobody named a file there, but a rename onto an existing note
|
|
585
|
-
// is a user mistake, and quietly landing somewhere other than where they
|
|
586
|
-
// asked hides it. `fs.rename` would overwrite the destination outright.
|
|
587
|
-
if (await exists(to)) return { ok: false, reason: "collision", slug: target };
|
|
588
|
-
|
|
589
|
-
await fs.rename(from, to);
|
|
590
|
-
// The slug is the note's identity, so a rename is a change to the note.
|
|
591
|
-
const meta: NoteMeta = { ...note, updated: now.toISOString() };
|
|
592
|
-
return { ok: true, note: await writeNote(to, target, meta, note.body, note.frontMatter) };
|
|
593
|
-
});
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
/**
|
|
597
|
-
* Delete a note. **Hard delete** — the file is unlinked.
|
|
598
|
-
*
|
|
599
|
-
* No trash directory, and that is a deliberate omission rather than an
|
|
600
|
-
* oversight. A trash is a real feature: it needs a location that does not
|
|
601
|
-
* pollute `notes/` (everything there is indexed and graphed), a retention
|
|
602
|
-
* policy, a restore path, and an answer for what happens when a deleted slug
|
|
603
|
-
* is later reused. Inventing all of that as a side effect of "P5 needs a
|
|
604
|
-
* delete button" is how a vault grows a second, undocumented store of notes
|
|
605
|
-
* that the index does not know about — and AGENTS.md rule 5 says nothing in
|
|
606
|
-
* `.okf` may be the only copy of anything, which cuts both ways: a
|
|
607
|
-
* half-designed trash becomes exactly such a place.
|
|
608
|
-
*
|
|
609
|
-
* The vault is plain files in a directory most users keep under version
|
|
610
|
-
* control or a synced folder, so the recovery story is the one they already
|
|
611
|
-
* have and understand. If a trash is wanted, it should arrive as its own
|
|
612
|
-
* design decision with those questions answered.
|
|
613
|
-
*/
|
|
614
|
-
export async function deleteNote(root: string, slug: string): Promise<DeleteResult> {
|
|
615
|
-
const path = resolveNotePath(root, slug);
|
|
616
|
-
if (!path) return { ok: false, reason: "missing" };
|
|
617
|
-
return withNoteLocks([path], async () => {
|
|
618
|
-
try {
|
|
619
|
-
await fs.unlink(path);
|
|
620
|
-
return { ok: true };
|
|
621
|
-
} catch {
|
|
622
|
-
// Already gone, or never existed. Both are "there is no such note",
|
|
623
|
-
// which is the caller's question — not "the unlink syscall failed".
|
|
624
|
-
return { ok: false, reason: "missing" };
|
|
625
|
-
}
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
// ---------------------------------------------------------------------------
|
|
630
|
-
// Generated-note upsert (weave-scan sessions; docs/session-scan.md)
|
|
631
|
-
// ---------------------------------------------------------------------------
|
|
632
|
-
|
|
633
|
-
export interface UpsertNoteInput {
|
|
634
|
-
/** Desired slug (already slug-safe); uniquified (`-2`, `-3`…) when creating. */
|
|
635
|
-
slug: string;
|
|
636
|
-
title: string;
|
|
637
|
-
body: string;
|
|
638
|
-
tags?: string[];
|
|
639
|
-
/** Defaults to `"generated"` — the safe direction for AGENTS.md rule 4. */
|
|
640
|
-
source?: NoteSource;
|
|
641
|
-
/**
|
|
642
|
-
* Extra **owned scalar** front-matter fields (e.g. `session_hash`) upserted
|
|
643
|
-
* on every write. Managed keys and syntactically unsafe keys are silently
|
|
644
|
-
* dropped: the note engine owns those, and this function will not fight it.
|
|
645
|
-
*/
|
|
646
|
-
fields?: Record<string, string>;
|
|
647
|
-
/**
|
|
648
|
-
* Content identity of the generated note, when the slug alone must not
|
|
649
|
-
* decide ownership: on the create path, a candidate slug already occupied
|
|
650
|
-
* by a note carrying a **different** identity value is skipped (the slug
|
|
651
|
-
* uniquifies to `-2`, `-3`…), while a same-identity occupant is treated as
|
|
652
|
-
* ours. Without this, two generated artifacts that derive the same slug —
|
|
653
|
-
* two sessions that began with the same first message, say — would have
|
|
654
|
-
* the second silently overwrite the first, marker keys and all.
|
|
655
|
-
*/
|
|
656
|
-
identity?: { field: string; value: string };
|
|
657
|
-
/** Injectable clock for tests. */
|
|
658
|
-
now?: Date;
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
/**
|
|
662
|
-
* Idempotently create-or-update a note, for generated knowledge that is
|
|
663
|
-
* re-derivable from a source of truth (a session transcript, a scan) and
|
|
664
|
-
* keyed by content the note carries in its front matter.
|
|
665
|
-
*
|
|
666
|
-
* - **Create** (no file at `slug`): canonical managed block plus the extra
|
|
667
|
-
* fields, body as given, `created` = `updated` = now. The slug is passed
|
|
668
|
-
* through `uniqueSlug`, so a taken slug shifts to `-2` rather than
|
|
669
|
-
* overwriting a note the caller could not see.
|
|
670
|
-
* - **Update** (file exists): replace the body and the extra fields, bump
|
|
671
|
-
* `updated`, and change nothing else — `title`, `created`, `tags`, unknown
|
|
672
|
-
* front-matter keys, and the append-only `## Raw` tail all survive, per
|
|
673
|
-
* the vault's round-trip guarantees. Title and tags are deliberately
|
|
674
|
-
* creation-time values: the human may have retitled or retagged the note,
|
|
675
|
-
* and a re-scan must not clobber that.
|
|
676
|
-
*
|
|
677
|
-
* Both paths hold the notes-**directory** lock (like `addNote`): the create
|
|
678
|
-
* path runs a check-then-create slug allocation that must be atomic, and the
|
|
679
|
-
* update path's `getNote`-then-write is the same lost-update window.
|
|
680
|
-
*/
|
|
681
|
-
export async function upsertNote(root: string, input: UpsertNoteInput): Promise<Note> {
|
|
682
|
-
await ensureVault(root);
|
|
683
|
-
return withNoteLocks([join(root, NOTES_DIR)], async () => {
|
|
684
|
-
const now = (input.now ?? new Date()).toISOString();
|
|
685
|
-
const noteAt = (slug: string) => notePath(root, slug);
|
|
686
|
-
const identity = input.identity;
|
|
687
|
-
let existing = await getNote(root, input.slug);
|
|
688
|
-
if (
|
|
689
|
-
existing !== null &&
|
|
690
|
-
identity &&
|
|
691
|
-
fieldFromFrontMatter(existing.frontMatter, identity.field) !== identity.value
|
|
692
|
-
) {
|
|
693
|
-
// The note at this slug belongs to a different identity (or to no
|
|
694
|
-
// identity at all — a human note): never update it in place. Fall
|
|
695
|
-
// through to the create path, whose guard picks the next free slug.
|
|
696
|
-
existing = null;
|
|
697
|
-
}
|
|
698
|
-
if (existing === null) {
|
|
699
|
-
const slug = uniqueSlug(input.slug, (candidate) => {
|
|
700
|
-
if (!existsSync(noteAt(candidate))) return false; // free
|
|
701
|
-
if (!identity) return true; // slug ownership is the caller's problem
|
|
702
|
-
return occupantIdentity(root, candidate, identity.field) !== identity.value;
|
|
703
|
-
});
|
|
704
|
-
const meta: NoteMeta = {
|
|
705
|
-
title: input.title,
|
|
706
|
-
created: now,
|
|
707
|
-
updated: now,
|
|
708
|
-
tags: input.tags ?? [],
|
|
709
|
-
source: input.source ?? "generated",
|
|
710
|
-
};
|
|
711
|
-
// The managed lines below are re-rendered from `meta` by `serializeNote`
|
|
712
|
-
// (replayBlock substitutes rendered values for managed keys in place);
|
|
713
|
-
// spelling them here just fixes the block's key order.
|
|
714
|
-
const fields = sanitizeUpsertFields(input.fields);
|
|
715
|
-
const frontMatter = [
|
|
716
|
-
`title: ${quoteField(meta.title)}`,
|
|
717
|
-
`created: ${meta.created}`,
|
|
718
|
-
`updated: ${meta.updated}`,
|
|
719
|
-
`tags: [${meta.tags.map(quoteField).join(", ")}]`,
|
|
720
|
-
`source: ${meta.source}`,
|
|
721
|
-
...upsertFrontMatterFields([], fields),
|
|
722
|
-
];
|
|
723
|
-
return writeNote(noteAt(slug), slug, meta, input.body, frontMatter);
|
|
724
|
-
}
|
|
725
|
-
const meta: NoteMeta = { ...existing, updated: now };
|
|
726
|
-
const fields = sanitizeUpsertFields(input.fields);
|
|
727
|
-
const frontMatter = upsertFrontMatterFields(existing.frontMatter ?? [], fields);
|
|
728
|
-
const body = preserveRawTail(existing.body, input.body);
|
|
729
|
-
return writeNote(noteAt(input.slug), input.slug, meta, body, frontMatter);
|
|
730
|
-
});
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
/**
|
|
734
|
-
* The identity value carried in a note's own front-matter block, or null when
|
|
735
|
-
* absent — an absent identity never matches, so unmarked notes are never
|
|
736
|
-
* claimed as ours.
|
|
737
|
-
*/
|
|
738
|
-
function fieldFromFrontMatter(lines: NoteFrontMatter | undefined, field: string): string | null {
|
|
739
|
-
if (!lines) return null;
|
|
740
|
-
const parsed = parseFrontMatter(["---", ...lines, "---", ""].join("\n"));
|
|
741
|
-
if (!parsed) return null;
|
|
742
|
-
return parsed.fields.get(field) ?? null;
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
/**
|
|
746
|
-
* The identity value a note at `slug` carries in its front matter, or null
|
|
747
|
-
* when the file is missing, malformed, or does not declare the field — a
|
|
748
|
-
* null never equals a real identity value, so such a file blocks the slug.
|
|
749
|
-
*/
|
|
750
|
-
function occupantIdentity(root: string, slug: string, field: string): string | null {
|
|
751
|
-
let text: string;
|
|
752
|
-
try {
|
|
753
|
-
text = readFileSync(notePath(root, slug), "utf8");
|
|
754
|
-
} catch {
|
|
755
|
-
return null;
|
|
756
|
-
}
|
|
757
|
-
const parsed = parseFrontMatter(text);
|
|
758
|
-
if (!parsed) return null;
|
|
759
|
-
return parsed.fields.get(field) ?? null;
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
/**
|
|
763
|
-
* Drop fields this function has no business writing: managed keys (the note
|
|
764
|
-
* engine renders those from `NoteMeta`) and keys that are not plain scalar
|
|
765
|
-
* identifiers (a hostile key could smuggle newlines or `---` into the block).
|
|
766
|
-
* Values are guarded by `quoteField` at render time.
|
|
767
|
-
*/
|
|
768
|
-
function sanitizeUpsertFields(fields: Record<string, string> | undefined): Record<string, string> {
|
|
769
|
-
if (!fields) return {};
|
|
770
|
-
const managed = new Set<string>(MANAGED_FRONT_MATTER_KEYS);
|
|
771
|
-
const out: Record<string, string> = {};
|
|
772
|
-
for (const [key, value] of Object.entries(fields)) {
|
|
773
|
-
if (managed.has(key)) continue;
|
|
774
|
-
if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(key)) continue;
|
|
775
|
-
out[key] = value;
|
|
776
|
-
}
|
|
777
|
-
return out;
|
|
778
|
-
}
|
|
779
|
-
|
|
780
348
|
async function listNoteFiles(root: string): Promise<string[]> {
|
|
781
349
|
const dir = join(root, NOTES_DIR);
|
|
782
350
|
const out: string[] = [];
|
|
783
|
-
// Recursive by design: vault notes may nest (`sessions/<name>` — session
|
|
784
|
-
// memory lives in an inner folder of the graph, docs/session-scan.md), and
|
|
785
|
-
// every consumer above this function (list, search, graph, cache) speaks
|
|
786
|
-
// in slugs, so the relative path *is* the slug.
|
|
787
351
|
async function walk(prefix: string): Promise<void> {
|
|
788
352
|
let entries;
|
|
789
353
|
try {
|
|
790
354
|
entries = await fs.readdir(prefix.length > 0 ? join(dir, prefix) : dir, { withFileTypes: true });
|
|
791
355
|
} catch {
|
|
792
|
-
return;
|
|
356
|
+
return;
|
|
793
357
|
}
|
|
794
358
|
for (const entry of entries) {
|
|
795
359
|
if (entry.isDirectory()) {
|
|
796
360
|
await walk(prefix.length > 0 ? `${prefix}/${entry.name}` : entry.name);
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
if (entry.isFile() && entry.name.endsWith(".md")) {
|
|
800
|
-
const slug = prefix.length > 0 ? `${prefix}/${entry.name}` : entry.name;
|
|
801
|
-
out.push(slug);
|
|
361
|
+
} else if (entry.isFile() && entry.name.endsWith(".md")) {
|
|
362
|
+
out.push(prefix.length > 0 ? `${prefix}/${entry.name}` : entry.name);
|
|
802
363
|
}
|
|
803
364
|
}
|
|
804
365
|
}
|
|
@@ -806,17 +367,6 @@ async function listNoteFiles(root: string): Promise<string[]> {
|
|
|
806
367
|
return out.sort();
|
|
807
368
|
}
|
|
808
369
|
|
|
809
|
-
/**
|
|
810
|
-
* Derive the list-shaped summary of a note (drops the body, keeps its length).
|
|
811
|
-
*
|
|
812
|
-
* `frontMatter` is dropped along with the body, and explicitly rather than by
|
|
813
|
-
* omission: a spread is exempt from TypeScript's excess-property check, so
|
|
814
|
-
* carrying it would type-check fine and then ship every note's raw metadata
|
|
815
|
-
* block through `listNotes` into the search results and the wire payload —
|
|
816
|
-
* a field no consumer reads, on a shape the contract test pins.
|
|
817
|
-
* Preservation is a property of the *write* path re-reading the file, not of
|
|
818
|
-
* summaries carrying it around.
|
|
819
|
-
*/
|
|
820
370
|
export function summarizeNote(note: Note): NoteSummary {
|
|
821
371
|
const { body, frontMatter, ...rest } = note;
|
|
822
372
|
void frontMatter;
|
|
@@ -832,6 +382,28 @@ function byUpdatedDesc(a: { updated: string }, b: { updated: string }): number {
|
|
|
832
382
|
return b.updated.localeCompare(a.updated);
|
|
833
383
|
}
|
|
834
384
|
|
|
385
|
+
export async function listNoteFolders(root: string): Promise<string[]> {
|
|
386
|
+
const dir = join(root, NOTES_DIR);
|
|
387
|
+
const out: string[] = [];
|
|
388
|
+
async function walk(prefix: string): Promise<void> {
|
|
389
|
+
let entries;
|
|
390
|
+
try {
|
|
391
|
+
entries = await fs.readdir(prefix.length > 0 ? join(dir, prefix) : dir, { withFileTypes: true });
|
|
392
|
+
} catch {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
for (const entry of entries) {
|
|
396
|
+
if (entry.isDirectory() && !entry.name.startsWith(".")) {
|
|
397
|
+
const folder = prefix.length > 0 ? `${prefix}/${entry.name}` : entry.name;
|
|
398
|
+
out.push(folder);
|
|
399
|
+
await walk(folder);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
await walk("");
|
|
404
|
+
return out.sort();
|
|
405
|
+
}
|
|
406
|
+
|
|
835
407
|
/**
|
|
836
408
|
* Everything one pass over the vault can tell you: every readable note with
|
|
837
409
|
* its body, plus how many `.md` files exist.
|
|
@@ -848,18 +420,24 @@ export interface VaultSnapshot {
|
|
|
848
420
|
* `notes.length` can be smaller; this is the honest on-disk count.
|
|
849
421
|
*/
|
|
850
422
|
fileCount: number;
|
|
423
|
+
/** Subdirectories present in <vault>/notes/, including empty ones. */
|
|
424
|
+
folders?: string[];
|
|
851
425
|
}
|
|
852
426
|
|
|
853
427
|
/** Read the whole vault in one pass: one readdir, one read per note. */
|
|
854
428
|
export async function readVault(root: string): Promise<VaultSnapshot> {
|
|
855
|
-
const files = await listNoteFiles(root);
|
|
429
|
+
const [files, folders] = await Promise.all([listNoteFiles(root), listNoteFolders(root)]);
|
|
856
430
|
const notes: Note[] = [];
|
|
857
431
|
for (const file of files) {
|
|
858
432
|
const note = await getNote(root, file.slice(0, -".md".length));
|
|
859
433
|
if (!note) continue; // unreadable/malformed files are skipped, not fatal
|
|
860
434
|
notes.push(note);
|
|
861
435
|
}
|
|
862
|
-
return {
|
|
436
|
+
return {
|
|
437
|
+
notes: notes.sort(byUpdatedDesc),
|
|
438
|
+
fileCount: files.length,
|
|
439
|
+
...(folders.length > 0 ? { folders } : {}),
|
|
440
|
+
};
|
|
863
441
|
}
|
|
864
442
|
|
|
865
443
|
/** One note's identity and change-detection stamp, without reading its content. */
|
package/src/core/view/detail.ts
CHANGED
package/src/core/view/health.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* healthModel — staleness + link health, derived exclusively from the
|
|
3
|
-
* GraphModel (weave-
|
|
3
|
+
* GraphModel (weave-workspace §3).
|
|
4
4
|
*
|
|
5
5
|
* Zero new server/core fields: everything here is a projection of nodes,
|
|
6
6
|
* edges, and `model.staleness`.
|
package/src/core/view/tree.ts
CHANGED
|
@@ -138,12 +138,24 @@ function hiddenInternalKind(kind: NodeKind): boolean {
|
|
|
138
138
|
return kind === "gitState" || kind === "external" || kind === "package" || kind === "entryPoint";
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
function isFolderNode(node: GraphNode | undefined): boolean {
|
|
142
|
+
if (!node) return false;
|
|
143
|
+
return node.kind === "module" || node.id.startsWith("vfolder:");
|
|
144
|
+
}
|
|
145
|
+
|
|
141
146
|
function sortKids(ids: string[], byId: Map<string, GraphNode>): string[] {
|
|
142
147
|
return ids
|
|
143
148
|
.slice()
|
|
144
149
|
.sort((a, b) => {
|
|
145
|
-
const
|
|
146
|
-
const
|
|
150
|
+
const nodeA = byId.get(a);
|
|
151
|
+
const nodeB = byId.get(b);
|
|
152
|
+
const isFoldA = isFolderNode(nodeA);
|
|
153
|
+
const isFoldB = isFolderNode(nodeB);
|
|
154
|
+
if (isFoldA !== isFoldB) {
|
|
155
|
+
return isFoldA ? -1 : 1;
|
|
156
|
+
}
|
|
157
|
+
const la = listLabel(nodeA!);
|
|
158
|
+
const lb = listLabel(nodeB!);
|
|
147
159
|
return la.localeCompare(lb);
|
|
148
160
|
});
|
|
149
161
|
}
|
|
@@ -257,7 +269,7 @@ export function treeRows(model: GraphModel, state: TreeState): TreeRow[] {
|
|
|
257
269
|
return rows;
|
|
258
270
|
}
|
|
259
271
|
|
|
260
|
-
/** Empty-state hint for the tree surface
|
|
272
|
+
/** Empty-state hint for the tree surface. */
|
|
261
273
|
export function treeEmptyHint(model: GraphModel): string | null {
|
|
262
274
|
const vault = model.nodes.find((n) => n.kind === "vault");
|
|
263
275
|
if (!vault) return null;
|