@joe-broadhead/opencode-mem 0.9.2 → 0.9.12

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +1605 -0
  2. package/README.md +135 -43
  3. package/docs/agent-protocol.md +29 -28
  4. package/docs/permissions.md +148 -0
  5. package/package.json +277 -16
  6. package/skills/mem.pages/SKILL.md +35 -16
  7. package/skills/mem.sharing/SKILL.md +39 -6
  8. package/skills/mem.sources/SKILL.md +41 -10
  9. package/skills/mem.usage/SKILL.md +47 -16
  10. package/src/backend/records.js +413 -50
  11. package/src/backend/v3.js +15 -10
  12. package/src/call-shapes.js +18 -25
  13. package/src/capture-pause.js +136 -0
  14. package/src/capture.js +7 -4
  15. package/src/collector-service.js +212 -35
  16. package/src/collector.js +186 -47
  17. package/src/commands.js +37 -0
  18. package/src/compaction.js +4 -2
  19. package/src/config.js +162 -1
  20. package/src/connect.js +353 -0
  21. package/src/copies.js +28 -3
  22. package/src/credentials.js +35 -7
  23. package/src/curation.js +33 -14
  24. package/src/events.js +144 -36
  25. package/src/forget-purge.js +377 -0
  26. package/src/forgotten.js +356 -0
  27. package/src/hooks.js +9 -4
  28. package/src/index.js +25 -6
  29. package/src/permissions.js +116 -38
  30. package/src/protocol.js +21 -11
  31. package/src/records.js +4 -1
  32. package/src/runtime.js +207 -16
  33. package/src/shapes.js +63 -0
  34. package/src/sources.js +183 -12
  35. package/src/spaces.js +58 -1
  36. package/src/spool.js +442 -64
  37. package/src/stated.js +334 -0
  38. package/src/storable.js +477 -0
  39. package/src/tools/aliases.js +36 -147
  40. package/src/tools/commitments.js +69 -12
  41. package/src/tools/held-write.js +9 -1
  42. package/src/tools/index.js +29 -44
  43. package/src/tools/ingest.js +33 -128
  44. package/src/tools/memory.js +594 -901
  45. package/src/tools/page-sections.js +147 -0
  46. package/src/tools/page.js +314 -82
  47. package/src/tools/recall-browse.js +173 -0
  48. package/src/tools/recall.js +79 -19
  49. package/src/tools/share.js +494 -131
  50. package/src/tools/shared.js +97 -19
  51. package/src/tools/spaces.js +215 -104
  52. package/src/tools/status.js +31 -3
  53. package/src/tools/wiki.js +5 -13
  54. package/src/util.js +10 -20
  55. package/src/views/contracts.js +137 -6
  56. package/src/views/overdue.js +125 -0
  57. package/src/views/profile.js +154 -0
  58. package/src/views/records-reads.js +132 -33
  59. package/src/views/rpc.js +63 -12
  60. package/src/views/shape.js +49 -0
  61. package/views/commitments.html +31 -4
  62. package/views/home.html +22 -3
  63. package/views/memory.html +126 -24
  64. package/src/erase.js +0 -36
@@ -0,0 +1,147 @@
1
+ import { createHash } from "node:crypto"
2
+
3
+ /**
4
+ * The `## sections` of a page body, read the way the records store's
5
+ * `records.append` reads them (records/src/text.js appendToSection): a level-2
6
+ * heading starts a section, the next heading of level 1 or 2 ends it, and
7
+ * headings inside code fences are ignored. mem_page `replace` uses them to
8
+ * change one section without rewriting the page.
9
+ */
10
+
11
+ const HEADING = /^(#{1,6})\s+(.*?)\s*#*\s*$/
12
+
13
+ /** Every level-2 section: its heading text, and the line range of its text. */
14
+ export function sectionsOf(body) {
15
+ const lines = String(body ?? "").split("\n")
16
+ const sections = []
17
+ let fence = false
18
+ let open = null
19
+ for (let i = 0; i < lines.length; i += 1) {
20
+ if (/^\s*(```|~~~)/.test(lines[i])) fence = !fence
21
+ if (fence) continue
22
+ const match = HEADING.exec(lines[i])
23
+ if (!match || match[1].length > 2) continue
24
+ if (open) {
25
+ open.end = i
26
+ sections.push(open)
27
+ open = null
28
+ }
29
+ if (match[1].length === 2) open = { heading: match[2].trim(), line: i, end: lines.length }
30
+ }
31
+ if (open) sections.push(open)
32
+ return { lines, sections }
33
+ }
34
+
35
+ /** A section name as given: trimmed, without leading #s. */
36
+ export function sectionName(name) {
37
+ return String(name ?? "").trim().replace(/^#+\s*/, "")
38
+ }
39
+
40
+ /**
41
+ * Which section a name means, for append and replace alike: the one heading
42
+ * that equals it (case aside), else the one heading that starts with it and a
43
+ * space or "(" ("Status" finds "Status (as of 2026-11-12)"). `ambiguous` lists
44
+ * every match, with its 1-based line, when the name fits more than one; a
45
+ * name that fits none is `{ missing: true }` (append and replace add it).
46
+ *
47
+ * @returns {{ section?: { heading: string, line: number, end: number, text: string }, ambiguous?: { heading: string, line: number }[], missing?: boolean }}
48
+ */
49
+ export function resolveSection(body, name) {
50
+ const { lines, sections } = sectionsOf(body)
51
+ const want = sectionName(name).toLowerCase()
52
+ if (!want) return { missing: true }
53
+ const exact = sections.filter((section) => section.heading.toLowerCase() === want)
54
+ const prefixed = sections.filter((section) => {
55
+ const heading = section.heading.toLowerCase()
56
+ return heading.startsWith(`${want} `) || heading.startsWith(`${want}(`)
57
+ })
58
+ const matches = exact.length > 0 ? exact : prefixed
59
+ if (matches.length > 1) return { ambiguous: matches.map((section) => ({ heading: section.heading, line: section.line + 1 })) }
60
+ if (matches.length === 0) return { missing: true }
61
+ const found = matches[0]
62
+ return { section: { ...found, text: lines.slice(found.line + 1, found.end).join("\n").trim() } }
63
+ }
64
+
65
+ /** The one section a name means, or null (none, or more than one). */
66
+ export function findSection(body, name) {
67
+ return resolveSection(body, name).section ?? null
68
+ }
69
+
70
+ /** The refusal for a name that fits more than one section. */
71
+ export function ambiguousMessage(verb, pagePath, name, matches) {
72
+ const listed = matches.map((match) => `"## ${match.heading}" at line ${match.line}`).join(", ")
73
+ return `not ${verb}: "${sectionName(name)}" matches ${matches.length} sections of ${pagePath}: ${listed}. Nothing was written. Rename or merge one with write (read the page first), then ${verb === "appended" ? "append" : "replace"} again.`
74
+ }
75
+
76
+ /** True when text opens a code fence it does not close: the next heading would be hidden inside it. */
77
+ export function unbalancedFence(text) {
78
+ return String(text ?? "").split("\n").filter((line) => /^\s*(```|~~~)/.test(line)).length % 2 === 1
79
+ }
80
+
81
+ /** A short fingerprint of one section's text, or of its absence. */
82
+ export function sectionHash(section) {
83
+ return createHash("sha256").update(section ? `${section.heading.toLowerCase()}\n${section.text}` : "\u0000none").digest("hex").slice(0, 16)
84
+ }
85
+
86
+ /** The fingerprint of every section, keyed by lower-case heading, for a later replace's check. */
87
+ export function sectionHashes(body) {
88
+ const { sections } = sectionsOf(body)
89
+ const out = {}
90
+ for (const section of sections) {
91
+ const key = section.heading.toLowerCase()
92
+ out[key] = sectionHash(findSection(body, section.heading))
93
+ }
94
+ return out
95
+ }
96
+
97
+ /**
98
+ * The body with one section's text replaced by `text`, its heading line
99
+ * kept. A missing section is added at the end, as an append adds it.
100
+ */
101
+ export function replaceSection(body, name, text) {
102
+ const value = String(text ?? "").replace(/\s+$/, "")
103
+ const resolved = resolveSection(body, name)
104
+ if (resolved.ambiguous) return { ambiguous: resolved.ambiguous }
105
+ const found = resolved.section
106
+ if (!found) {
107
+ const trimmed = String(body ?? "").replace(/\s+$/, "")
108
+ return { body: `${trimmed ? `${trimmed}\n\n` : ""}## ${sectionName(name)}\n${value}\n`, heading: sectionName(name), created: true }
109
+ }
110
+ const { lines } = sectionsOf(body)
111
+ const after = lines.slice(found.end)
112
+ const next = [...lines.slice(0, found.line + 1), ...value.split("\n"), ...(after.length > 0 && after.some((line) => line.trim()) ? ["", ...after] : [])]
113
+ const joined = next.join("\n").replace(/\s+$/, "")
114
+ return { body: `${joined}\n`, heading: found.heading, created: false }
115
+ }
116
+
117
+ /**
118
+ * A routine's run-stats line: `Run daily@v1 · 3 sources · 4,120 tokens`, or
119
+ * the journal routine's own `_daily@v1 · 3 threads · … · run <id>_`.
120
+ */
121
+ const RUN_STATS = /^(?:Run\s+\S.*·.*|_\S.*·.*_)$/
122
+
123
+ /**
124
+ * A page's closing routine footer (FIX-0912): its last "---" line and the run
125
+ * stats under it, with no heading in between, as the journal routine writes
126
+ * them. A person's own "---" is not one: the line after it must be run stats
127
+ * (the caller also asks for a routine page), so a plain rule and the text
128
+ * under it stay ordinary section content. `main` is the body before it;
129
+ * `footer` is the block, to keep at the end.
130
+ *
131
+ * @param {string} body
132
+ * @returns {{ main: string, footer: string } | null}
133
+ */
134
+ export function splitFooter(body) {
135
+ const lines = String(body ?? "").replace(/\s+$/, "").split("\n")
136
+ let last = lines.length - 1
137
+ while (last >= 0 && !lines[last].trim()) last -= 1
138
+ if (last < 0 || !RUN_STATS.test(lines[last].trim())) return null
139
+ let at = last
140
+ while (at >= 0) {
141
+ if (/^#{1,6}\s/.test(lines[at])) return null
142
+ if (lines[at].trim() === "---") break
143
+ at -= 1
144
+ }
145
+ if (at < 0) return null
146
+ return { main: lines.slice(0, at).join("\n").replace(/\s+$/, ""), footer: lines.slice(at).join("\n") }
147
+ }