@staix/vault-mem 0.4.0 → 0.5.1
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/CHANGELOG.md +46 -0
- package/README.md +15 -7
- package/bin/vmem.js +9 -3
- package/package.json +2 -2
- package/src/analysis-commands.ts +22 -13
- package/src/analysis-input.ts +2 -2
- package/src/analysis-types.ts +2 -0
- package/src/cli-output.ts +140 -0
- package/src/cli.ts +75 -42
- package/src/commands-table.ts +85 -44
- package/src/config.ts +22 -3
- package/src/links.ts +2 -0
- package/src/log-commands.ts +10 -5
- package/src/mcp-spec.ts +5 -2
- package/src/mcp-vault.ts +6 -5
- package/src/org-commands.ts +5 -3
- package/src/org-promote.ts +3 -0
- package/src/promote-command.ts +6 -4
- package/src/read-commands.ts +12 -2
- package/src/read-output.ts +99 -0
- package/src/registry.ts +3 -2
- package/src/setup.ts +58 -12
- package/src/skills-mine.ts +3 -2
- package/src/spec-git.ts +7 -4
- package/src/spec-output.ts +119 -0
- package/src/sync.ts +62 -18
- package/src/text-output.ts +120 -0
- package/src/ui/inbox-source.ts +46 -0
- package/src/ui/inbox-worker.ts +8 -0
- package/src/ui/inbox.ts +134 -0
- package/src/ui/panel.css +1 -5
- package/src/ui/panel.js +60 -70
- package/src/ui/plugin.ts +12 -30
- package/src/vault-graph.ts +21 -2
- package/src/vault-index.ts +10 -4
- package/src/vault-lint.ts +8 -2
- package/src/vault-log.ts +97 -0
- package/src/workspace-graph.ts +9 -1
- package/src/write-commands.ts +5 -3
- package/src/write-operations.ts +25 -2
- package/src/write-output.ts +151 -0
- package/src/write-validation.ts +30 -8
package/src/ui/panel.js
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
1
|
// vmem console panels (acon api 1). Runs under the shell's strict CSP: data is rendered as text only.
|
|
2
|
-
const h = (tag, props = {}, ...kids) => {
|
|
3
|
-
const el = document.createElement(tag);
|
|
4
|
-
for (const [k, v] of Object.entries(props)) {
|
|
5
|
-
if (v === undefined || v === null || v === false) continue;
|
|
6
|
-
if (k === "text") el.textContent = String(v);
|
|
7
|
-
else if (k.startsWith("on")) el.addEventListener(k.slice(2), v);
|
|
8
|
-
else el.setAttribute(k, v === true ? "" : String(v));
|
|
9
|
-
}
|
|
10
|
-
el.append(...kids.flat().filter((k) => k !== null && k !== undefined && k !== false));
|
|
11
|
-
return el;
|
|
12
|
-
};
|
|
13
|
-
const empty = (title, hint) => h("div", { class: "empty" }, h("strong", { text: title }), hint ? h("span", { text: hint }) : null);
|
|
14
|
-
const err = (text) => h("p", { class: "notice", role: "alert", text });
|
|
15
|
-
const rows = (items, row) => (items.length ? h("ul", { class: "rows" }, items.map(row)) : null);
|
|
16
|
-
const row = (title, sub, onOpen, ...verbs) => h("li", { class: "row" },
|
|
17
|
-
h("button", { type: "button", class: "row-main", onclick: onOpen },
|
|
18
|
-
h("span", { class: "row-text" }, h("span", { class: "row-title", text: title }), sub ? h("span", { class: "row-sub", text: sub }) : null)),
|
|
19
|
-
verbs.length ? h("span", { class: "row-verbs" }, verbs) : null);
|
|
20
|
-
const btn = (label, onclick, cls = "btn") => h("button", { type: "button", class: cls, onclick, text: label });
|
|
21
|
-
const chip = (text, tone) => h("span", { class: `chip${tone ? ` ${tone}` : ""}`, text });
|
|
22
|
-
|
|
23
2
|
export default (host) => {
|
|
3
|
+
// Every element comes from the console panel kit (host.ui); this file defines no DOM helper of its own.
|
|
4
|
+
const ui = host.ui, h = ui.h;
|
|
24
5
|
const root = h("div", { class: "vmem" });
|
|
25
6
|
host.mount.append(root);
|
|
26
7
|
let panel = "overview", params = {};
|
|
27
8
|
const go = (p, extra = {}) => host.navigate(host.tool, p, { ...(params.vault ? { vault: params.vault } : {}), ...extra });
|
|
28
9
|
const openNote = (path) => go("note", { path });
|
|
10
|
+
const vaultParam = () => (params.vault ? { vault: params.vault } : {});
|
|
29
11
|
|
|
30
12
|
const searchForm = (q) => {
|
|
31
13
|
const input = h("input", { type: "search", name: "q", value: q ?? "", placeholder: "Search notes", "aria-label": "Search notes", class: "vmem-input" });
|
|
@@ -36,83 +18,91 @@ export default (host) => {
|
|
|
36
18
|
const views = {
|
|
37
19
|
overview(s) {
|
|
38
20
|
const vaults = s.status?.vaults ?? [];
|
|
39
|
-
if (!vaults.length) return [empty("No vault registered", "Run `vmem vaults add <id> <path>` to register one.")];
|
|
21
|
+
if (!vaults.length) return [ui.empty("No vault registered", "Run `vmem vaults add <id> <path>` to register one.")];
|
|
40
22
|
return [
|
|
41
23
|
searchForm(""),
|
|
42
|
-
h("div", { class: "cards" },
|
|
24
|
+
h("div", { class: "vmem-cards" },
|
|
43
25
|
h("section", { class: "card" }, h("h2", { text: "Vaults" }),
|
|
44
|
-
rows(vaults, (v) => row(v.id, `${v.kind} · write ${v.write}${v.health && v.health !== "ok" ? ` · ${v.health}` : ""}`, () => go("overview", { vault: v.id }), v.id === s.vault ? chip
|
|
26
|
+
ui.rows(vaults, (v) => ui.row({ title: v.id, sub: `${v.kind} · write ${v.write}${v.health && v.health !== "ok" ? ` · ${v.health}` : ""}`, onOpen: () => go("overview", { vault: v.id }), ...(v.id === s.vault ? { chip: { text: "shown", tone: "neutral" } } : {}) }))),
|
|
45
27
|
h("section", { class: "card" }, h("h2", { text: `${s.noteCount ?? 0}${s.noteCountCapped ? "+" : ""} notes · maps` }),
|
|
46
|
-
rows((s.mocs ?? []).slice(0, 8), (m) => row(m.title || m.path, `${m.count} links`, () => openNote(m.path))) ?? empty("No maps yet")),
|
|
28
|
+
ui.rows((s.mocs ?? []).slice(0, 8), (m) => ui.row({ title: m.title || m.path, sub: `${m.count} links`, onOpen: () => openNote(m.path) })) ?? ui.empty("No maps yet")),
|
|
47
29
|
h("section", { class: "card" }, h("h2", { text: "Recent log" }),
|
|
48
|
-
rows(s.log ?? [], (r) => row(r.raw ?? `${r.type} ${r.source}${r.dest ? ` → ${r.dest}` : ""}`, r.raw ?
|
|
30
|
+
ui.rows(s.log ?? [], (r) => ui.row({ title: r.raw ?? `${r.type} ${r.source}${r.dest ? ` → ${r.dest}` : ""}`, ...(r.raw ? {} : { sub: `${r.at} · ${r.project}${r.agent ? " · agent" : ""}` }), onOpen: () => go("log") })) ?? ui.empty("Log is empty"))),
|
|
49
31
|
];
|
|
50
32
|
},
|
|
51
33
|
notes(s) {
|
|
52
34
|
const list = s.notes ?? [];
|
|
53
|
-
return [searchForm(s.q), s.error ?
|
|
54
|
-
rows(list, (n) => row(n.title || n.path, [n.type, n.domain, n.description].filter(Boolean).join(" · "), () => openNote(n.path)))
|
|
55
|
-
?? empty(s.q ? "No notes match" : "No notes", s.q ? "Try fewer words; terms are combined with AND." :
|
|
35
|
+
return [searchForm(s.q), s.error ? ui.notice(s.error, "err") : null,
|
|
36
|
+
ui.rows(list, (n) => ui.row({ title: n.title || n.path, sub: [n.type, n.domain, n.description].filter(Boolean).join(" · "), onOpen: () => openNote(n.path) }))
|
|
37
|
+
?? ui.empty(s.q ? "No notes match" : "No notes", s.q ? "Try fewer words; terms are combined with AND." : undefined)];
|
|
56
38
|
},
|
|
57
39
|
note(s) {
|
|
58
|
-
if (s.error) return [
|
|
40
|
+
if (s.error) return [ui.notice(s.error, "err")];
|
|
59
41
|
const n = s.note;
|
|
60
|
-
if (!n) return [empty("No note selected", "Open a note from Search or the palette.")];
|
|
42
|
+
if (!n) return [ui.empty("No note selected", "Open a note from Search or the palette.")];
|
|
61
43
|
const fm = n.frontmatter ?? {};
|
|
62
|
-
const title = fm.title || n.path;
|
|
63
44
|
const moveInput = h("input", { type: "text", name: "to", class: "vmem-input", "aria-label": "New stem", placeholder: "new-stem" });
|
|
64
45
|
const orgs = s.orgVaults ?? [];
|
|
65
|
-
const orgSelect = orgs.length ? h("select", { name: "org", class: "vmem-input", "aria-label": "Org vault" }, orgs.map((o) => h("option", { value: o, text: o }))) : null;
|
|
46
|
+
const orgSelect = orgs.length ? h("select", { name: "org", class: "vmem-input", "aria-label": "Org vault" }, ...orgs.map((o) => h("option", { value: o, text: o }))) : null;
|
|
66
47
|
const out = s.links?.out ?? [], back = s.links?.in ?? [], dead = s.links?.dead ?? [];
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
h("
|
|
71
|
-
|
|
72
|
-
h("form", { class: "vmem-inline", onsubmit: (e) => { e.preventDefault(); if (moveInput.value.trim()) host.action({ action: "move", from: n.path, to: moveInput.value.trim(), ...(params.vault ? { vault: params.vault } : {}) }); } },
|
|
73
|
-
moveInput, h("button", { type: "submit", class: "btn", text: "Move…" })),
|
|
74
|
-
orgSelect ? (() => {
|
|
75
|
-
const topics = h("input", { type: "text", name: "orgTopics", class: "vmem-input", "aria-label": "Org topics (comma separated, optional)", placeholder: "org topics (optional)" });
|
|
76
|
-
const again = h("input", { type: "checkbox", name: "again", "aria-label": "Promote again to another org" });
|
|
77
|
-
return h("form", { class: "vmem-inline", onsubmit: (e) => { e.preventDefault(); host.action({ action: "promote", path: n.path, to: orgSelect.value, ...(topics.value.trim() ? { topics: topics.value.trim() } : {}), ...(again.checked ? { again: true } : {}) }); } },
|
|
78
|
-
orgSelect, topics, h("label", { class: "vmem-check" }, again, h("span", { text: "again" })), h("button", { type: "submit", class: "btn", text: "Promote to org…" }));
|
|
79
|
-
})() : null),
|
|
80
|
-
h("details", { open: true }, h("summary", { text: "Body" }), h("pre", { class: "vmem-body", text: n.body ?? "" }), n.truncated ? err("Truncated; open the file for the rest.") : null),
|
|
81
|
-
h("details", {}, h("summary", { text: `Links (${out.length} out, ${back.length} in${dead.length ? `, ${dead.length} dead` : ""})` }),
|
|
82
|
-
rows([...out.map((l) => ({ path: l.resolved, label: `→ ${l.resolved}` })), ...back.map((l) => ({ path: l.source, label: `← ${l.source}` })), ...dead.map((l) => ({ label: `✕ ${l.target}` }))],
|
|
83
|
-
(l) => row(l.label, "", () => l.path && openNote(l.path))) ?? empty("No links")),
|
|
84
|
-
h("p", { class: "vmem-mono", text: `${n.path} · sha256 ${String(n.sha256 ?? "").slice(0, 12)}` }),
|
|
48
|
+
const links = [...out.map((l) => ({ path: l.resolved, label: `→ ${l.resolved}` })), ...back.map((l) => ({ path: l.source, label: `← ${l.source}` })), ...dead.map((l) => ({ label: `✕ ${l.target}` }))];
|
|
49
|
+
const actions = [
|
|
50
|
+
ui.button("Delete…", () => host.action({ action: "delete", path: n.path, ...vaultParam() }), { tone: "danger" }),
|
|
51
|
+
h("form", { class: "vmem-inline", onsubmit: (e) => { e.preventDefault(); if (moveInput.value.trim()) host.action({ action: "move", from: n.path, to: moveInput.value.trim(), ...vaultParam() }); } },
|
|
52
|
+
moveInput, h("button", { type: "submit", class: "btn", text: "Move…" })),
|
|
85
53
|
];
|
|
54
|
+
if (orgSelect) {
|
|
55
|
+
const topics = h("input", { type: "text", name: "orgTopics", class: "vmem-input", "aria-label": "Org topics (comma separated, optional)", placeholder: "org topics (optional)" });
|
|
56
|
+
const again = h("input", { type: "checkbox", name: "again", "aria-label": "Promote again to another org" });
|
|
57
|
+
actions.push(h("form", { class: "vmem-inline", onsubmit: (e) => { e.preventDefault(); host.action({ action: "promote", path: n.path, to: orgSelect.value, ...(topics.value.trim() ? { topics: topics.value.trim() } : {}), ...(again.checked ? { again: true } : {}) }); } },
|
|
58
|
+
orgSelect, topics, h("label", { class: "vmem-check" }, again, h("span", { text: "again" })), h("button", { type: "submit", class: "btn", text: "Promote to org…" })));
|
|
59
|
+
}
|
|
60
|
+
return [ui.detail({
|
|
61
|
+
kind: fm.type || "note", title: fm.title || n.path,
|
|
62
|
+
...(fm.status ? { state: { text: fm.status, ...(fm.status === "superseded" ? { tone: "warn" } : {}) } } : {}),
|
|
63
|
+
actions, ...(fm.description ? { summary: fm.description } : {}),
|
|
64
|
+
sections: [
|
|
65
|
+
{ title: "Body", open: true, body: h("div", {}, h("pre", { class: "vmem-body", text: n.body ?? "" }), n.truncated ? ui.notice("Truncated; open the file for the rest.", "warn") : null) },
|
|
66
|
+
{ title: `Links (${out.length} out, ${back.length} in${dead.length ? `, ${dead.length} dead` : ""})`,
|
|
67
|
+
body: ui.rows(links, (l) => ui.row({ title: l.label, ...(l.path ? { onOpen: () => openNote(l.path) } : {}) })) ?? ui.empty("No links") },
|
|
68
|
+
],
|
|
69
|
+
footer: [`${n.path} · sha256 ${String(n.sha256 ?? "").slice(0, 12)}`],
|
|
70
|
+
})];
|
|
86
71
|
},
|
|
87
72
|
promote(s) {
|
|
88
73
|
const projects = s.projects ?? [];
|
|
89
74
|
const all = projects.flatMap((p) => p.candidates);
|
|
90
75
|
const picked = all.find((c) => c.id === s.selected);
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
76
|
+
const form = picked ? h("section", { class: "card vmem-form" }, h("h2", { text: `Promote: ${picked.title || picked.kind}` }),
|
|
77
|
+
ui.form([
|
|
78
|
+
{ name: "domain", label: "domain (required)", required: true },
|
|
79
|
+
{ name: "topics", label: "topics, comma separated (required)", required: true },
|
|
80
|
+
{ name: "type", label: "type (optional)" },
|
|
81
|
+
{ name: "description", label: "description (optional)" },
|
|
82
|
+
{ name: "stem", label: "file stem (needed when the title has no ASCII letters)" },
|
|
83
|
+
], { label: "Preview promotion…", primary: true }, (values) => {
|
|
84
|
+
const meta = Object.fromEntries(Object.entries(values).map(([k, v]) => [k, v.trim()]).filter(([, v]) => v));
|
|
85
|
+
host.action({ action: "promote-from-amem", id: picked.id, project: picked.project, ...meta, ...vaultParam() });
|
|
86
|
+
})) : null;
|
|
87
|
+
return [s.unavailable ? ui.notice(s.unavailable, "err") : null, form,
|
|
88
|
+
ui.rows(all, (c) => ui.row({ title: c.title || c.kind, sub: `${c.kind} · ${c.project} · ${new Date(c.at).toLocaleString()}`, onOpen: () => go("promote", { id: c.id, project: c.project }),
|
|
89
|
+
...(s.selected === c.id ? { chip: { text: "selected", tone: "neutral" } } : { verbs: [ui.button("Select", () => go("promote", { id: c.id, project: c.project }))] }) }))
|
|
90
|
+
?? ui.empty("No promotion candidates", "Durable agent memories appear here when agent-mem marks them for the vault.")];
|
|
104
91
|
},
|
|
105
92
|
lint(s) {
|
|
106
93
|
const f = s.findings ?? [];
|
|
107
94
|
return [h("p", { class: "vmem-sub", text: s.summary ?? "" }),
|
|
108
|
-
rows(f, (x) => row(x.message, `${x.code}${x.path ? ` · ${x.path}` : ""}`, (
|
|
109
|
-
?? empty("No lint findings", "The vault passes every check.")];
|
|
95
|
+
ui.rows(f, (x) => ui.row({ title: x.message, sub: `${x.code}${x.path ? ` · ${x.path}` : ""}`, ...(x.path ? { onOpen: () => openNote(x.path) } : {}), chip: { text: x.severity, tone: x.severity === "error" ? "err" : "warn" } }))
|
|
96
|
+
?? ui.empty("No lint findings", "The vault passes every check.")];
|
|
110
97
|
},
|
|
111
98
|
log(s) {
|
|
112
99
|
const r = s.rows ?? [];
|
|
113
|
-
return [s.error ?
|
|
114
|
-
rows(r, (x) =>
|
|
115
|
-
|
|
100
|
+
return [s.error ? ui.notice(s.error, "err") : null,
|
|
101
|
+
ui.rows(r, (x) => {
|
|
102
|
+
const p = x.dest ?? x.source;
|
|
103
|
+
return ui.row({ title: x.raw ?? `${x.type} ${x.source}${x.dest ? ` → ${x.dest}` : ""}`, sub: x.raw ? "unparsed line" : `${x.at} · ${x.project} · ${x.note}${x.agent ? " · agent" : ""}`,
|
|
104
|
+
...(typeof p === "string" && p.startsWith("notes/") ? { onOpen: () => openNote(p) } : {}) });
|
|
105
|
+
}) ?? ui.empty("Log is empty")];
|
|
116
106
|
},
|
|
117
107
|
};
|
|
118
108
|
|
|
@@ -125,7 +115,7 @@ export default (host) => {
|
|
|
125
115
|
// Rule: every form control in this file has a stable name, or its state is lost on the next poll.
|
|
126
116
|
const values = new Map([...root.querySelectorAll("input[name], select[name]")].map((el) => [el.name, el.type === "checkbox" ? el.checked : el.value]));
|
|
127
117
|
const focused = root.contains(document.activeElement) ? document.activeElement.getAttribute("name") : null;
|
|
128
|
-
root.replaceChildren(...(views[panel]?.(snap) ?? [empty("Unknown panel")]).filter(Boolean));
|
|
118
|
+
root.replaceChildren(...(views[panel]?.(snap) ?? [ui.empty("Unknown panel")]).filter(Boolean));
|
|
129
119
|
for (const el of root.querySelectorAll("input[name], select[name]")) if (values.has(el.name)) el[el.type === "checkbox" ? "checked" : "value"] = values.get(el.name);
|
|
130
120
|
if (focused) root.querySelector(`[name="${focused}"]`)?.focus();
|
|
131
121
|
});
|
package/src/ui/plugin.ts
CHANGED
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
* The provider reuses the `vmem mcp` handlers in process, so the console acts with exactly the MCP rights and checks.
|
|
4
4
|
*/
|
|
5
5
|
import { readFileSync } from "node:fs";
|
|
6
|
-
import type { ActionResult, Change, ConsolePlugin, ConsoleProvider
|
|
6
|
+
import type { ActionResult, Change, ConsolePlugin, ConsoleProvider } from "@staix/agent-console/contract";
|
|
7
7
|
import { analysisInput } from "../analysis-input.ts";
|
|
8
8
|
import { resolveHome } from "../home.ts";
|
|
9
9
|
import { createVaultMcp } from "../mcp-vault.ts";
|
|
10
10
|
import { validArguments } from "../mcp.ts";
|
|
11
11
|
import { listPromotionCandidates } from "../promote.ts";
|
|
12
12
|
import { loadRegistry } from "../registry.ts";
|
|
13
|
+
import { createInboxSnapshot } from "./inbox.ts";
|
|
13
14
|
import { lintVault } from "../vault-lint.ts";
|
|
15
|
+
import { readLogSince } from "../vault-log.ts";
|
|
14
16
|
import type { LintCode } from "../analysis-types.ts";
|
|
15
17
|
|
|
16
18
|
const asset = (name: string) => readFileSync(new URL(name, import.meta.url), "utf8");
|
|
@@ -120,7 +122,7 @@ export const plugin: ConsolePlugin = {
|
|
|
120
122
|
return validArguments(out, schema ?? { type: "object" }) ? out : `invalid input for ${tool}`;
|
|
121
123
|
};
|
|
122
124
|
|
|
123
|
-
//
|
|
125
|
+
// The Lint panel reuses its report for 60 s; inbox collection has its own off-thread cache.
|
|
124
126
|
const lintCache = new Map<string, { at: number; report: ReturnType<typeof lintVault> }>();
|
|
125
127
|
const lint = (id: string) => {
|
|
126
128
|
const hit = lintCache.get(id);
|
|
@@ -140,30 +142,7 @@ export const plugin: ConsolePlugin = {
|
|
|
140
142
|
catch (error) { ctx.log(`vmem: promotion candidates unavailable: ${error instanceof Error ? error.message : String(error)}`); return { projects: [], unavailable: "agent-mem candidates are unavailable; see System" }; }
|
|
141
143
|
};
|
|
142
144
|
|
|
143
|
-
|
|
144
|
-
const firstSeen = new Map<string, string>();
|
|
145
|
-
const since = (id: string) => { const at = firstSeen.get(id) ?? new Date().toISOString(); firstSeen.set(id, at); return at; };
|
|
146
|
-
async function inbox(): Promise<InboxItem[]> {
|
|
147
|
-
const items: InboxItem[] = [];
|
|
148
|
-
for (const p of (await candidates()).projects) for (const c of p.candidates) items.push({
|
|
149
|
-
id: `mem:${c.id}`, group: "candidate", kind: "promotion", title: c.title ?? c.kind, detail: `${c.kind} memory to promote into a vault note`,
|
|
150
|
-
project: c.project, at: new Date(c.at).toISOString(),
|
|
151
|
-
// Promotion needs a domain and topics, so the item opens the Promote panel instead of offering a one-key verb.
|
|
152
|
-
verbs: [],
|
|
153
|
-
link: { panel: "promote", params: { id: c.id, project: c.project } },
|
|
154
|
-
});
|
|
155
|
-
for (const v of mcp.vaults.filter(x => x.kind === "personal")) {
|
|
156
|
-
let report;
|
|
157
|
-
try { report = lint(v.id); } catch (error) { ctx.log(`vmem: lint ${v.id} failed: ${error instanceof Error ? error.message : String(error)}`); continue; }
|
|
158
|
-
if (report.errors > 0) items.push({ id: `lint:${v.id}`, group: "alert", kind: "lint", title: `${report.errors} lint errors in ${v.id}`, detail: report.summary, at: since(`lint:${v.id}`),
|
|
159
|
-
chip: { text: `${report.errors} errors`, tone: "err" }, verbs: [], link: { panel: "lint", params: { vault: v.id } } });
|
|
160
|
-
const stale = report.findings.L11 ?? [];
|
|
161
|
-
if (stale.length) items.push({ id: `stale:${v.id}`, group: "alert", kind: "lint", title: `Graph report is stale in ${v.id}`, detail: stale[0]!, at: since(`stale:${v.id}`),
|
|
162
|
-
chip: { text: "stale", tone: "warn" }, verbs: [], link: { panel: "lint", params: { vault: v.id } } });
|
|
163
|
-
}
|
|
164
|
-
for (const id of firstSeen.keys()) if (!items.some(i => i.id === id)) firstSeen.delete(id);
|
|
165
|
-
return items;
|
|
166
|
-
}
|
|
145
|
+
const inbox = createInboxSnapshot(home, mcp.vaults, ctx.log);
|
|
167
146
|
|
|
168
147
|
/** One two-step operation: preview, and with the console's confirmation recompute and write only on the same fingerprint. */
|
|
169
148
|
async function twoStep(tool: string, args: Args | string, confirmed: boolean, fingerprint: string | undefined, describe: (preview: Args) => { verb: string; preview: string[]; changes: Change[]; warnings: string[] }): Promise<ActionResult> {
|
|
@@ -175,16 +154,17 @@ export const plugin: ConsolePlugin = {
|
|
|
175
154
|
return { ok: false, needsConfirm: true, fingerprint: preview.fingerprint, verb: d.verb, preview: d.preview, changes: d.changes, ...(d.warnings.length ? { warnings: d.warnings } : {}) };
|
|
176
155
|
}
|
|
177
156
|
const done = await call(tool, { ...args, confirm: true, fingerprint });
|
|
178
|
-
if (done.applied === true) lintCache.clear();
|
|
157
|
+
if (done.applied === true) { lintCache.clear(); inbox.invalidate(); }
|
|
179
158
|
return confirmedOutcome(done, describe(preview).verb);
|
|
180
159
|
}
|
|
181
160
|
|
|
182
161
|
return {
|
|
162
|
+
async close() { await inbox.close(); },
|
|
183
163
|
assets: { js: asset("./panel.js"), css: asset("./panel.css") },
|
|
184
164
|
async snapshot(input) {
|
|
185
165
|
const args = input as Args;
|
|
186
166
|
switch (input.panel) {
|
|
187
|
-
case "inbox": return
|
|
167
|
+
case "inbox": return inbox.snapshot();
|
|
188
168
|
case "search": {
|
|
189
169
|
const q = str(args.q);
|
|
190
170
|
if (!q) return { hits: [], total: 0 };
|
|
@@ -227,8 +207,10 @@ export const plugin: ConsolePlugin = {
|
|
|
227
207
|
case "log": {
|
|
228
208
|
const vault = vaultOf(args);
|
|
229
209
|
if (!vault) return { rows: [] };
|
|
230
|
-
|
|
231
|
-
|
|
210
|
+
// `since` is the `cursor` of the previous answer: only newer rows come back, or the latest with `reset: true`.
|
|
211
|
+
// The console owns the numeric `after` (its server refuses any other type), so the log cursor travels as `since`.
|
|
212
|
+
const r = readLogSince(home, { vault: vault.id, limit: 100, ...(str(args.type) ? { type: str(args.type) } : {}), ...(str(args.since) ? { after: str(args.since) } : {}) });
|
|
213
|
+
return { vault: vault.id, rows: r.lines.map(parseLogLine).reverse(), cursor: r.cursor, ...(r.reset ? { reset: true } : {}), ...(r.more ? { more: true } : {}), ...(r.ok ? {} : { error: firstIssue(r as unknown as Args) }) };
|
|
232
214
|
}
|
|
233
215
|
default: return { error: `unknown panel ${input.panel}` };
|
|
234
216
|
}
|
package/src/vault-graph.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { AnalysisSnapshot } from "./analysis-types.ts";
|
|
2
|
+
import { LINK_RELATIONS } from "./links.ts";
|
|
3
|
+
export { LINK_RELATIONS };
|
|
2
4
|
import { AnalysisYamlUnsupported, parseAnalysisYaml, scalarText, yamlTruthy } from "./analysis-yaml.ts";
|
|
3
5
|
|
|
4
6
|
export interface GraphNode {
|
|
@@ -336,6 +338,16 @@ export const codePointCompare = compare;
|
|
|
336
338
|
export function labelCommunities(ids: readonly string[], edges: readonly (readonly [string, string])[]): Map<string, number> {
|
|
337
339
|
return leidenCommunities(ids, edges).membership;
|
|
338
340
|
}
|
|
341
|
+
/** The typed relation of a `- [[x]] (relation)` list line, or undefined for any other wiki link. */
|
|
342
|
+
function linkRelation(body: string, index: number, length: number): string | undefined {
|
|
343
|
+
// Only `- [[` can be typed; check the two characters before the link before scanning back to the line start,
|
|
344
|
+
// so a long line of plain links stays linear. The scan back then crosses only whitespace.
|
|
345
|
+
if (index < 2 || body[index - 1] !== " " || body[index - 2] !== "-") return undefined;
|
|
346
|
+
let start = index - 2;
|
|
347
|
+
while (start > 0 && body[start - 1] !== "\n") { if (!/\s/.test(body[start - 1]!)) return undefined; start--; }
|
|
348
|
+
const after = /^ \(([a-z-]+)\)(?::|\s*(?:\n|$))/.exec(body.slice(index + length, index + length + 40));
|
|
349
|
+
return after && (LINK_RELATIONS as readonly string[]).includes(after[1]!) ? after[1] : undefined;
|
|
350
|
+
}
|
|
339
351
|
/** Pure transformation of the derived-index snapshot, preserving the reference wiki graph semantics. */
|
|
340
352
|
export function buildGraph(snapshot: AnalysisSnapshot): VaultGraph {
|
|
341
353
|
if (snapshot.notes.length > MAX_NODES) throw new Error(`graph: exceeds ${MAX_NODES} notes`);
|
|
@@ -354,7 +366,9 @@ export function buildGraph(snapshot: AnalysisSnapshot): VaultGraph {
|
|
|
354
366
|
const target = match[1]!.trim().toLowerCase().replaceAll(" ", "-");
|
|
355
367
|
if (!stems.has(target)) continue;
|
|
356
368
|
const [source, destination] = [id, target].sort(compare) as [string, string];
|
|
357
|
-
|
|
369
|
+
const key = JSON.stringify([source, destination]), rel = linkRelation(body, match.index, match[0].length), prior = edges.get(key);
|
|
370
|
+
const kept = prior?.rel ?? rel;
|
|
371
|
+
edges.set(key, { source, target: destination, relation: "wiki_link", ...(kept ? { rel: kept } : {}), confidence_tag: "EXTRACTED", confidence: 1 });
|
|
358
372
|
if (edges.size > MAX_EDGES) throw new Error(`graph: exceeds ${MAX_EDGES} edges`);
|
|
359
373
|
}
|
|
360
374
|
}
|
|
@@ -365,9 +379,14 @@ export function buildGraph(snapshot: AnalysisSnapshot): VaultGraph {
|
|
|
365
379
|
communities(graph);
|
|
366
380
|
return graph;
|
|
367
381
|
}
|
|
368
|
-
|
|
382
|
+
/** An edge's link relation: its typed `rel`, else the edge relation (`wiki_link` for a plain link). */
|
|
383
|
+
export function edgeRelation(edge: GraphEdge): string { return edge.relation === "wiki_link" ? edge.rel ?? "wiki_link" : edge.relation; }
|
|
384
|
+
export function graphNeighbors(graph: VaultGraph, start: string, hops: 1 | 2, limit: number, relation?: string): GraphNeighborhood {
|
|
369
385
|
if (hops !== 1 && hops !== 2) throw new Error("graph neighbors: hops must be 1 or 2");
|
|
370
386
|
if (!Number.isInteger(limit) || limit < 1 || limit > 500) throw new Error("graph neighbors: limit must be 1..500");
|
|
387
|
+
if (relation !== undefined && relation !== "wiki_link" && !(LINK_RELATIONS as readonly string[]).includes(relation))
|
|
388
|
+
throw new Error(`graph neighbors: unknown relation ${JSON.stringify(relation)}; use wiki_link or ${LINK_RELATIONS.join(", ")}`);
|
|
389
|
+
if (relation !== undefined) graph = { ...graph, edges: graph.edges.filter(edge => edgeRelation(edge) === relation) };
|
|
371
390
|
const neighbors = adjacency(graph);
|
|
372
391
|
if (!neighbors.has(start)) throw new Error("graph neighbors: unknown note");
|
|
373
392
|
const distances = new Map([[start, 0]]), queue = [start];
|
package/src/vault-index.ts
CHANGED
|
@@ -16,8 +16,8 @@ export const MAX_RESULTS = 500;
|
|
|
16
16
|
* share the unversioned `<digest>.sqlite`; the first run of a newer version builds its own file once.
|
|
17
17
|
*/
|
|
18
18
|
export const LINK_RESOLUTION = 2;
|
|
19
|
-
/** Bump when tables or FTS columns change. Named per value like LINK_RESOLUTION. 2 = CJK bigram table. */
|
|
20
|
-
export const INDEX_SCHEMA =
|
|
19
|
+
/** Bump when tables or FTS columns change. Named per value like LINK_RESOLUTION. 2 = CJK bigram table; 3 = reference identifiers in FTS. */
|
|
20
|
+
export const INDEX_SCHEMA = 3;
|
|
21
21
|
const indexName = (root: string) => `${digest(root)}-links${LINK_RESOLUTION}-schema${INDEX_SCHEMA}.sqlite`;
|
|
22
22
|
/**
|
|
23
23
|
* unicode61 keeps each Hangul/CJK run as one token, so `사업단` misses `사업단은`. A side table holds the
|
|
@@ -221,7 +221,7 @@ export class VaultIndex<Source extends VaultSpec | SpecSourceSpec = VaultSpec> {
|
|
|
221
221
|
if (size.count > 10000 || size.bytes > 64 * 1024 * 1024) throw new Error("analysis: cached snapshot exceeds bound");
|
|
222
222
|
for (const row of disk.query<Omit<StoredNote,"aliases"> & {aliases:string}, []>("SELECT * FROM notes").all()) {
|
|
223
223
|
this.db.query("INSERT INTO notes VALUES (?,?,?,?,?,?,?,?,?,?)").run(row.path,row.hash,row.metadata,row.title,row.description,row.body,row.type,row.domain,row.aliases,row.source);
|
|
224
|
-
this.indexText(row.path, row.title, row.description, row.body);
|
|
224
|
+
this.indexText(row.path, row.title, row.description, !this.adapter && row.type === "reference" ? this.searchBody(row.body, parseFrontmatter(row.source).data) : row.body);
|
|
225
225
|
}
|
|
226
226
|
for (const row of disk.query<{source:string;ordinal:number;data:string}, []>("SELECT * FROM links").all()) this.db.query("INSERT INTO links VALUES (?,?,?)").run(row.source,row.ordinal,row.data);
|
|
227
227
|
this.db.run(`PRAGMA user_version = ${Number(disk.query<{ user_version: number }, []>("PRAGMA user_version").get()?.user_version ?? 0)}`);
|
|
@@ -242,6 +242,12 @@ export class VaultIndex<Source extends VaultSpec | SpecSourceSpec = VaultSpec> {
|
|
|
242
242
|
}
|
|
243
243
|
close(): void { this.db.close(); }
|
|
244
244
|
// ponytail: grams stores its bigram text (about 2.3x the CJK bytes); contentless with stable rowids would save it.
|
|
245
|
+
/** Keep the stored body and link/analysis input unchanged; identifiers join only the FTS text. */
|
|
246
|
+
private searchBody(body: string, data: Record<string, unknown>): string {
|
|
247
|
+
if (this.adapter || data.type !== "reference") return body;
|
|
248
|
+
const identifiers = [text(data.doi), text(data.citekey)].filter(Boolean);
|
|
249
|
+
return identifiers.length ? body + "\n" + identifiers.join("\n") : body;
|
|
250
|
+
}
|
|
245
251
|
private indexText(path: string, title: string, description: string, body: string): void {
|
|
246
252
|
this.db.query("INSERT INTO fts VALUES (?,?,?,?)").run(path, title, description, body);
|
|
247
253
|
this.db.query("INSERT INTO grams VALUES (?,?,?,?)").run(path, grams(title), grams(description), grams(body));
|
|
@@ -350,7 +356,7 @@ export class VaultIndex<Source extends VaultSpec | SpecSourceSpec = VaultSpec> {
|
|
|
350
356
|
this.db.query("INSERT OR REPLACE INTO notes VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(path, hash, stamp,
|
|
351
357
|
title, description, body, text(data.type), text(data.domain), JSON.stringify(aliases(data.aliases ?? data.alias)), source);
|
|
352
358
|
this.unindex(path);
|
|
353
|
-
this.indexText(path, title, description, body);
|
|
359
|
+
this.indexText(path, title, description, this.searchBody(body, data));
|
|
354
360
|
changed++;
|
|
355
361
|
}
|
|
356
362
|
for (const row of this.db.query<{path:string}, []>("SELECT path FROM analysis_notes").all()) if (!present.has(row.path)) this.db.query("DELETE FROM analysis_notes WHERE path=?").run(row.path);
|
package/src/vault-lint.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BUILTIN_DOMAINS } from "./config.ts";
|
|
1
2
|
import { parseAnalysisYaml, scalarText, yamlTruthy, AnalysisYamlUnsupported } from "./analysis-yaml.ts";
|
|
2
3
|
import type { AnalysisSnapshot, LintCode, LintReport } from "./analysis-types.ts";
|
|
3
4
|
const TYPES = ["decision", "insight", "method", "moc", "observation", "person", "project", "reference"];
|
|
@@ -70,7 +71,9 @@ function graphChecks(snapshot: AnalysisSnapshot): { l11: string[]; l12: string[]
|
|
|
70
71
|
singles.sort();
|
|
71
72
|
return { l11, l12, info: `nodes ${graph.nodes?.length ?? 0} / edges ${edges.length} / communities ${communities.size}; singleton ${singles.length}개${singles.length ? ": " + singles.slice(0,10).map(s => `\`${s}\``).join(", ") : ""}` };
|
|
72
73
|
}
|
|
73
|
-
export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport {
|
|
74
|
+
export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport { return lintDetails(snapshot, only).report; }
|
|
75
|
+
/** The report plus what only its markdown carries: the note count, check titles and per-check info lines, for the CLI text view. */
|
|
76
|
+
export function lintDetails(snapshot: AnalysisSnapshot, only?: string): { report: LintReport; notes: number; titles: Record<LintCode, string>; info: Partial<Record<LintCode, string>> } {
|
|
74
77
|
const findings: Record<LintCode,string[]> = {L01:[],L02:[],L03:[],L09:[],L10:[],L11:[],L12:[],L13:[],L14:[]};
|
|
75
78
|
const notes = [...snapshot.notes].sort((a,b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0).map(n => ({...n,...legacyDocument(n.source),stem: n.path.replace(/^notes\//, "").slice(0,-3)}));
|
|
76
79
|
const stems = new Set(notes.map(n => n.stem)), inlinks = new Map(notes.map(n => [n.stem,0]));
|
|
@@ -88,6 +91,9 @@ export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport
|
|
|
88
91
|
if (fm === null) { findings.L02.push(prefix + "frontmatter 없음/파싱 실패"); continue; }
|
|
89
92
|
if (fm.status === "superseded") superseded++;
|
|
90
93
|
if (!TYPES.includes(String(fm.type))) findings.L02.push(prefix + `\`type: ${py(fm.type)}\` 허용값 아님 (${TYPES.join(" | ")})`);
|
|
94
|
+
if (fm.domain !== undefined && (typeof fm.domain !== "string" || !(snapshot.domains ?? BUILTIN_DOMAINS).includes(fm.domain))) {
|
|
95
|
+
findings.L02.push(prefix + `\`domain: ${py(fm.domain)}\` 허용값 아님 (${(snapshot.domains ?? BUILTIN_DOMAINS).join(" | ")}); vmem.config.json의 이 vault \`domains\`에 id 추가 가능`);
|
|
96
|
+
}
|
|
91
97
|
if (fm.type === "moc") { if (!truth(fm.description)) findings.L02.push(prefix + "`type: moc` 노트는 `description` 필수"); }
|
|
92
98
|
else if (!truth(fm.topics)) findings.L02.push(prefix + "`topics` 없음/빈 배열");
|
|
93
99
|
for (const [key,allowed] of [["confidence",CONFIDENCE],["status",STATUS]] as const) if (fm[key] !== undefined && fm[key] !== null && !allowed.includes(py(fm[key]))) findings.L02.push(prefix + `\`${key}: ${py(fm[key])}\` 허용값 아님 (${allowed.join(" | ")})${key === "status" ? " (status는 노트 생애주기이지 사업 진행 상태가 아님 — 작업 상태는 본문에)" : ""}`);
|
|
@@ -131,5 +137,5 @@ export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport
|
|
|
131
137
|
if (extra[code]) lines.push("",extra[code]!);
|
|
132
138
|
lines.push("");
|
|
133
139
|
}
|
|
134
|
-
return { findings, errors, warnings, markdown: lines.join("\n"), summary };
|
|
140
|
+
return { report: { findings, errors, warnings, markdown: lines.join("\n"), summary }, notes: notes.length, titles: CODE_TITLES, info: extra };
|
|
135
141
|
}
|
package/src/vault-log.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { compileLogFilter } from "./log-filter.ts";
|
|
|
2
2
|
import { closeSync, constants, fstatSync, fsyncSync, ftruncateSync, lstatSync, openSync, readSync, realpathSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
5
6
|
import { loadRegistry, syncDirectory } from "./registry.ts";
|
|
6
7
|
import { canonicalPath, isDenied } from "./platform.ts";
|
|
7
8
|
import { formatLogEvent, LOG_TYPES } from "./log-format.ts";
|
|
@@ -262,3 +263,99 @@ export function readLog(home: string, options: LogReadOptions): LogReadResult {
|
|
|
262
263
|
}
|
|
263
264
|
return result;
|
|
264
265
|
}
|
|
266
|
+
|
|
267
|
+
/** Bytes a cursor poll may read forward; a larger gap answers with the latest rows and `reset: true`. */
|
|
268
|
+
export const MAX_LOG_CURSOR_GAP = 1024 * 1024;
|
|
269
|
+
/** Bytes read from the end of the log for a first call or a reset. */
|
|
270
|
+
const LOG_TAIL_WINDOW = 256 * 1024;
|
|
271
|
+
const CURSOR_ANCHOR = 256;
|
|
272
|
+
export interface LogSinceResult { ok: boolean; lines: string[]; cursor: string; reset: boolean; more: boolean; warnings: string[]; issues: WriteIssue[] }
|
|
273
|
+
/**
|
|
274
|
+
* Incremental tail for pollers. The cursor is opaque: base64url of `v1.<offset>.<sha256 of up to 256 bytes before offset>`,
|
|
275
|
+
* where offset is the end of the last complete line read. Rows come from the byte range after it, so identical lines
|
|
276
|
+
* are kept apart; a cursor past the end, with a different anchor (rewritten or truncated log) or more than
|
|
277
|
+
* MAX_LOG_CURSOR_GAP behind answers with the latest rows and `reset: true`. Every call reads a bounded range.
|
|
278
|
+
*/
|
|
279
|
+
export function readLogSince(home: string, options: { vault: string; after?: string; type?: string; limit?: number }): LogSinceResult {
|
|
280
|
+
const result: LogSinceResult = { ok: false, lines: [], cursor: "", reset: false, more: false, warnings: [], issues: [] };
|
|
281
|
+
let fd: number | undefined;
|
|
282
|
+
try {
|
|
283
|
+
const limit = options.limit ?? 100;
|
|
284
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 200) refuse("L09", "log limit must be an integer 1..200");
|
|
285
|
+
if (options.type !== undefined && !(LOG_TYPES as readonly string[]).includes(options.type)) refuse("L09", "unknown log type filter");
|
|
286
|
+
if (options.after !== undefined && (typeof options.after !== "string" || options.after.length > 256)) refuse("L09", "invalid log cursor");
|
|
287
|
+
const ctx = context(home, options.vault, undefined, false), file = join(ctx.vault.path, "log");
|
|
288
|
+
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
|
|
289
|
+
catch (caught) { if ((caught as NodeJS.ErrnoException).code === "ENOENT") { result.ok = true; result.cursor = encodeCursor(0, Buffer.alloc(0)); result.reset = options.after !== undefined && options.after !== result.cursor; return result; } throw caught; }
|
|
290
|
+
const stat = fstatSync(fd);
|
|
291
|
+
if (!stat.isFile()) refuse("L09", "log must be a plain file");
|
|
292
|
+
const size = stat.size, read = (start: number, end: number) => {
|
|
293
|
+
const out = Buffer.alloc(end - start);
|
|
294
|
+
for (let done = 0; done < out.length;) { const n = readSync(fd!, out, done, out.length - done, start + done); if (!n) refuse("LOG_READ", "log changed while reading"); done += n; }
|
|
295
|
+
return out;
|
|
296
|
+
};
|
|
297
|
+
const anchor = (offset: number) => read(Math.max(0, offset - CURSOR_ANCHOR), offset);
|
|
298
|
+
let start: number | undefined;
|
|
299
|
+
if (options.after !== undefined) {
|
|
300
|
+
const at = decodeCursor(options.after);
|
|
301
|
+
if (at && at.offset <= size && size - at.offset <= MAX_LOG_CURSOR_GAP && hashOf(anchor(at.offset)) === at.hash) start = at.offset;
|
|
302
|
+
else result.reset = true;
|
|
303
|
+
}
|
|
304
|
+
let skipFirst = false;
|
|
305
|
+
if (start === undefined) { start = Math.max(0, size - LOG_TAIL_WINDOW); skipFirst = start > 0 && read(start - 1, start)[0] !== 0x0a; }
|
|
306
|
+
const bytes = read(start, size);
|
|
307
|
+
let from = 0;
|
|
308
|
+
if (skipFirst) {
|
|
309
|
+
const nl = bytes.indexOf(0x0a);
|
|
310
|
+
if (nl < 0) {
|
|
311
|
+
// The whole window is inside one line: point the cursor at the newline before it (scanning back at most
|
|
312
|
+
// MAX_LOG_CURSOR_GAP), or at offset 0, which is a line start and, that far behind, always answers with a reset.
|
|
313
|
+
let boundary = 0;
|
|
314
|
+
for (let at = start; at > 0 && start - at < MAX_LOG_CURSOR_GAP;) {
|
|
315
|
+
const chunk = read(Math.max(0, at - 64 * 1024), at), i = chunk.lastIndexOf(0x0a);
|
|
316
|
+
if (i >= 0) { boundary = at - chunk.length + i + 1; break; }
|
|
317
|
+
at -= chunk.length;
|
|
318
|
+
}
|
|
319
|
+
result.warnings.push("historical log line over 4096 bytes omitted");
|
|
320
|
+
result.cursor = encodeCursor(boundary, anchor(boundary));
|
|
321
|
+
result.ok = true;
|
|
322
|
+
return result;
|
|
323
|
+
}
|
|
324
|
+
from = nl + 1;
|
|
325
|
+
}
|
|
326
|
+
const lastNl = bytes.lastIndexOf(0x0a), end = lastNl >= from ? lastNl + 1 : from;
|
|
327
|
+
if (end < bytes.length) result.warnings.push("incomplete final log line not yet read");
|
|
328
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
329
|
+
// A poll from a valid cursor delivers the oldest `limit` new rows and stops its cursor after the last one, so the
|
|
330
|
+
// next poll continues there; a first call or a reset shows the newest rows and moves the cursor to the end.
|
|
331
|
+
const incremental = options.after !== undefined && !result.reset;
|
|
332
|
+
let cursorAt = start + end;
|
|
333
|
+
for (let at = from; at < end;) {
|
|
334
|
+
const nl = bytes.indexOf(0x0a, at), next = nl + 1;
|
|
335
|
+
const raw = decoder.decode(bytes.subarray(at, nl)), line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
336
|
+
at = next;
|
|
337
|
+
if (!line.trim() || line.trimStart().startsWith("#")) continue;
|
|
338
|
+
if (Buffer.byteLength(line) > MAX_READ_LINE_BYTES) { if (!result.warnings.includes("historical log line over 4096 bytes omitted")) result.warnings.push("historical log line over 4096 bytes omitted"); continue; }
|
|
339
|
+
const type = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}\s+(\S+)/.exec(line)?.[1];
|
|
340
|
+
if (options.type && options.type !== type) continue;
|
|
341
|
+
if (incremental && result.lines.length === limit) { cursorAt = start + bytes.lastIndexOf(0x0a, at - 2) + 1; result.more = true; result.warnings.push(`more rows follow; poll again with the returned cursor`); break; }
|
|
342
|
+
result.lines.push(line);
|
|
343
|
+
}
|
|
344
|
+
if (result.lines.length > limit) { result.lines = result.lines.slice(-limit); result.warnings.push(`only the newest ${limit} rows returned`); }
|
|
345
|
+
result.cursor = encodeCursor(cursorAt, anchor(cursorAt));
|
|
346
|
+
result.ok = true;
|
|
347
|
+
} catch (caught) {
|
|
348
|
+
result.lines = [];
|
|
349
|
+
if (caught instanceof LogRefused) result.issues.push(...caught.issues);
|
|
350
|
+
else result.issues.push({ code: "LOG_READ", severity: "error", message: "cannot read plain UTF-8 log" });
|
|
351
|
+
} finally {
|
|
352
|
+
if (fd !== undefined) { try { closeSync(fd); } catch { result.ok = false; result.issues.push({ code: "LOG_CLOSE", severity: "error", message: "log descriptor close failed" }); } }
|
|
353
|
+
}
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
const hashOf = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex");
|
|
357
|
+
function encodeCursor(offset: number, before: Buffer): string { return Buffer.from(`v1.${offset}.${hashOf(before)}`).toString("base64url"); }
|
|
358
|
+
function decodeCursor(cursor: string): { offset: number; hash: string } | undefined {
|
|
359
|
+
const m = /^v1\.(\d{1,15})\.([0-9a-f]{64})$/.exec(Buffer.from(cursor, "base64url").toString("latin1"));
|
|
360
|
+
return m ? { offset: Number(m[1]), hash: m[2]! } : undefined;
|
|
361
|
+
}
|
package/src/workspace-graph.ts
CHANGED
|
@@ -16,6 +16,13 @@ const FRONTMATTER = /^---\s*\n([\s\S]*?)\n---\s*\n/;
|
|
|
16
16
|
const WIKI = /\[\[([^\]|]+?)(?:\|[^\]]+?)?\]\]/g;
|
|
17
17
|
const PRIORITY = { wiki_link: 0, fm_ref: 1, bu_member: 2, source_ref: 3, related: 4, supersedes: 5 };
|
|
18
18
|
export type WorkCounters = Record<"work_docs" | "yaml_fail" | "template_skip" | "related_unresolved" | "related_ambiguous" | "related_dir_skip" | "related_root_escape" | "related_home_escape" | "source_stub" | "source_dead" | "source_nonmd" | "source_skiproot" | "collisions" | "dup_edges", number>;
|
|
19
|
+
/**
|
|
20
|
+
* Edge attributes `scripts/workspace-parity.ts` compares with `build-graph.py --work-root`. The Python reference puts no
|
|
21
|
+
* `rel` on wiki_link edges; vmem keeps the `vmem link --relation` type there, so that `rel` is outside parity.
|
|
22
|
+
*/
|
|
23
|
+
export function workspaceParityEdgeKey(e: Record<string, unknown>): string {
|
|
24
|
+
return (["relation", "field", "rel"] as const).map(a => e[a] === undefined || (a === "rel" && e.relation === "wiki_link") ? "" : String(e[a])).join("|");
|
|
25
|
+
}
|
|
19
26
|
export function workSkipped(path: string, roots: string[]): boolean {
|
|
20
27
|
const normalized = path.normalize("NFC").replace(/^\/+|\/+$/g, "");
|
|
21
28
|
return roots.some(root => normalized === root || normalized.startsWith(root + "/"));
|
|
@@ -129,7 +136,8 @@ export function buildWorkspaceGraph(snapshot: AnalysisSnapshot, work: WorkspaceS
|
|
|
129
136
|
if (!current || PRIORITY[edge.relation] > PRIORITY[current.relation]) best.set(key, edge);
|
|
130
137
|
}
|
|
131
138
|
const edges = new Map(graph.edges.map(edge => [pair(edge), edge]));
|
|
132
|
-
|
|
139
|
+
// A vault wiki link's `rel` belongs to that relation only: a higher-priority work edge on the pair drops it.
|
|
140
|
+
for (const [key, edge] of best) { const prior = edges.get(key); edges.set(key, prior && prior.relation === edge.relation ? { ...prior, ...edge } : edge); }
|
|
133
141
|
graph.edges = [...edges.values()];
|
|
134
142
|
if (graph.nodes.length > 20000 || graph.edges.length > 100000) throw new Error("workspace graph: node or edge bound exceeded");
|
|
135
143
|
finalizeGraph(graph);
|
package/src/write-commands.ts
CHANGED
|
@@ -10,6 +10,8 @@ import type { LogEvent } from "./log-types.ts";
|
|
|
10
10
|
import { LOG_EVENT_SCHEMA } from "./log-format.ts";
|
|
11
11
|
import { argumentError } from "./mcp.ts";
|
|
12
12
|
import { parseTidyMap, tidyVault, TIDY_RULES } from "./tidy.ts";
|
|
13
|
+
import { PLAIN, type TextOptions } from "./text-output.ts";
|
|
14
|
+
import { renderTidy, renderWriteReceipt } from "./write-output.ts";
|
|
13
15
|
const COMMANDS = new Set(["new", "patch", "append", "frontmatter", "link", "supersede", "move", "delete", "tidy", "report", "check"]);
|
|
14
16
|
export function isWriteCommand(command: string): boolean { return COMMANDS.has(command); }
|
|
15
17
|
function object(text: string, label: string): Record<string, unknown> {
|
|
@@ -41,7 +43,7 @@ function logOption(text: string | undefined): { log?: LogEvent } {
|
|
|
41
43
|
if (error) throw new Error(`${error}; expected {"type", "project", "source", "note"[, "dest"]} as for log append`);
|
|
42
44
|
return { log: value as LogEvent };
|
|
43
45
|
}
|
|
44
|
-
export function runWriteCommand(command: string, args: string[], home: string, env: NodeJS.ProcessEnv = process.env): { text: string; code: number } {
|
|
46
|
+
export function runWriteCommand(command: string, args: string[], home: string, env: NodeJS.ProcessEnv = process.env, text: TextOptions = PLAIN): { text: string; code: number } {
|
|
45
47
|
const valueOptions: Record<string, string[]> = {
|
|
46
48
|
new: ["--type", "--domain", "--topics", "--description", "--title", "--source", "--confidence", "--status", "--extra", "--body"],
|
|
47
49
|
patch: ["--old", "--new", "--edits", "--expected-sha"], append: ["--section", "--markdown", "--expected-sha"],
|
|
@@ -81,7 +83,7 @@ export function runWriteCommand(command: string, args: string[], home: string, e
|
|
|
81
83
|
if (flags["--dry-run"] && flags["--confirm"]) throw new Error("tidy: --dry-run and --confirm are exclusive");
|
|
82
84
|
const mapText = optional("--map") !== undefined ? input(required("--map"), home) : undefined;
|
|
83
85
|
const result = tidyVault(home, { vault, rules, ...(mapText !== undefined ? { map: parseTidyMap(mapText), mapText } : {}), confirm: flags["--confirm"] === true && !flags["--dry-run"], fingerprint: optional("--fingerprint"), channel: "cli" }, { env });
|
|
84
|
-
return { text: JSON.stringify(result,
|
|
86
|
+
return { text: flags["--json"] ? JSON.stringify(result) : renderTidy(result, text, { argv: [command, ...args], positional }), code: result.ok ? 0 : 1 };
|
|
85
87
|
}
|
|
86
88
|
let operation: WriteOp;
|
|
87
89
|
switch (command) {
|
|
@@ -115,5 +117,5 @@ export function runWriteCommand(command: string, args: string[], home: string, e
|
|
|
115
117
|
}
|
|
116
118
|
const request: WriteRequest = { vault, operation, channel: "cli", dryRun: flags["--dry-run"] === true, confirm: flags["--confirm"] === true, fingerprint: optional("--fingerprint"), ...logOption(optional("--log")) };
|
|
117
119
|
const result = applyWrite(home, request, { env });
|
|
118
|
-
return { text: JSON.stringify(result,
|
|
120
|
+
return { text: flags["--json"] ? JSON.stringify(result) : renderWriteReceipt(result, text, { argv: [command, ...args], positional }), code: result.ok && result.log?.ok !== false ? 0 : 1 };
|
|
119
121
|
}
|