notesoup 0.0.0-stage → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +91 -2
  3. package/dist/src/boot/render.d.ts +16 -0
  4. package/dist/src/boot/render.js +95 -0
  5. package/dist/src/boot/snapshot.d.ts +32 -0
  6. package/dist/src/boot/snapshot.js +36 -0
  7. package/dist/src/boot/text.d.ts +21 -0
  8. package/dist/src/boot/text.js +52 -0
  9. package/dist/src/budget/constants.d.ts +9 -0
  10. package/dist/src/budget/constants.js +9 -0
  11. package/dist/src/budget/policy.d.ts +19 -0
  12. package/dist/src/budget/policy.js +36 -0
  13. package/dist/src/budget/text.d.ts +8 -0
  14. package/dist/src/budget/text.js +10 -0
  15. package/dist/src/claude/helper.d.ts +15 -0
  16. package/dist/src/claude/helper.js +87 -0
  17. package/dist/src/dream/doctor.d.ts +2 -0
  18. package/dist/src/dream/doctor.js +160 -0
  19. package/dist/src/dream/gates.d.ts +10 -0
  20. package/dist/src/dream/gates.js +26 -0
  21. package/dist/src/dream/git.d.ts +21 -0
  22. package/dist/src/dream/git.js +71 -0
  23. package/dist/src/dream/jail.d.ts +11 -0
  24. package/dist/src/dream/jail.js +71 -0
  25. package/dist/src/dream/lock.d.ts +31 -0
  26. package/dist/src/dream/lock.js +99 -0
  27. package/dist/src/dream/playbook.d.ts +1 -0
  28. package/dist/src/dream/playbook.js +2 -0
  29. package/dist/src/dream/report.d.ts +7 -0
  30. package/dist/src/dream/report.js +43 -0
  31. package/dist/src/dream/result.d.ts +9 -0
  32. package/dist/src/dream/result.js +1 -0
  33. package/dist/src/dream/settings.d.ts +10 -0
  34. package/dist/src/dream/settings.js +14 -0
  35. package/dist/src/history/history.d.ts +151 -0
  36. package/dist/src/history/history.js +195 -0
  37. package/dist/src/history/query.d.ts +75 -0
  38. package/dist/src/history/query.js +143 -0
  39. package/dist/src/index.d.ts +1 -0
  40. package/dist/src/index.js +1 -0
  41. package/dist/src/notes/address.d.ts +31 -0
  42. package/dist/src/notes/address.js +123 -0
  43. package/dist/src/notes/constants.d.ts +3 -0
  44. package/dist/src/notes/constants.js +3 -0
  45. package/dist/src/notes/errors.d.ts +11 -0
  46. package/dist/src/notes/errors.js +13 -0
  47. package/dist/src/notes/frontmatter.d.ts +44 -0
  48. package/dist/src/notes/frontmatter.js +145 -0
  49. package/dist/src/notes/fs-error.d.ts +2 -0
  50. package/dist/src/notes/fs-error.js +5 -0
  51. package/dist/src/notes/identity.d.ts +10 -0
  52. package/dist/src/notes/identity.js +33 -0
  53. package/dist/src/notes/index.d.ts +5 -0
  54. package/dist/src/notes/index.js +3 -0
  55. package/dist/src/notes/paths.d.ts +21 -0
  56. package/dist/src/notes/paths.js +113 -0
  57. package/dist/src/notes/store.d.ts +104 -0
  58. package/dist/src/notes/store.js +478 -0
  59. package/dist/src/pi/boot.d.ts +24 -0
  60. package/dist/src/pi/boot.js +56 -0
  61. package/dist/src/pi/budget.d.ts +13 -0
  62. package/dist/src/pi/budget.js +178 -0
  63. package/dist/src/pi/dream/cli.d.ts +10 -0
  64. package/dist/src/pi/dream/cli.js +140 -0
  65. package/dist/src/pi/dream/runner.d.ts +27 -0
  66. package/dist/src/pi/dream/runner.js +90 -0
  67. package/dist/src/pi/dream/settings.d.ts +7 -0
  68. package/dist/src/pi/dream/settings.js +17 -0
  69. package/dist/src/pi/entries.d.ts +7 -0
  70. package/dist/src/pi/entries.js +7 -0
  71. package/dist/src/pi/estimate.d.ts +16 -0
  72. package/dist/src/pi/estimate.js +99 -0
  73. package/dist/src/pi/extension.d.ts +10 -0
  74. package/dist/src/pi/extension.js +24 -0
  75. package/dist/src/pi/history-tools.d.ts +3 -0
  76. package/dist/src/pi/history-tools.js +10 -0
  77. package/dist/src/pi/history.d.ts +4 -0
  78. package/dist/src/pi/history.js +150 -0
  79. package/dist/src/pi/notes/adapter.d.ts +10 -0
  80. package/dist/src/pi/notes/adapter.js +27 -0
  81. package/dist/src/pi/notes/tools.d.ts +3 -0
  82. package/dist/src/pi/notes/tools.js +15 -0
  83. package/dist/src/pi/reset/artifacts.d.ts +24 -0
  84. package/dist/src/pi/reset/artifacts.js +84 -0
  85. package/dist/src/pi/reset/committed.d.ts +7 -0
  86. package/dist/src/pi/reset/committed.js +28 -0
  87. package/dist/src/pi/reset/lifecycle.d.ts +101 -0
  88. package/dist/src/pi/reset/lifecycle.js +277 -0
  89. package/dist/src/pi/reset/text.d.ts +5 -0
  90. package/dist/src/pi/reset/text.js +6 -0
  91. package/dist/src/pi/runtime.d.ts +3 -0
  92. package/dist/src/pi/runtime.js +206 -0
  93. package/dist/src/pi/session-reader.d.ts +5 -0
  94. package/dist/src/pi/session-reader.js +1 -0
  95. package/dist/src/pi/thresholds.d.ts +17 -0
  96. package/dist/src/pi/thresholds.js +33 -0
  97. package/dist/src/pi/tool-names.d.ts +11 -0
  98. package/dist/src/pi/tool-names.js +11 -0
  99. package/dist/src/pi/tool-result.d.ts +15 -0
  100. package/dist/src/pi/tool-result.js +29 -0
  101. package/dist/src/pi/window.d.ts +41 -0
  102. package/dist/src/pi/window.js +127 -0
  103. package/dist/src/settings.d.ts +9 -0
  104. package/dist/src/settings.js +18 -0
  105. package/dist/src/text-match.d.ts +5 -0
  106. package/dist/src/text-match.js +16 -0
  107. package/dist/src/tools/history.d.ts +67 -0
  108. package/dist/src/tools/history.js +251 -0
  109. package/dist/src/tools/notes.d.ts +282 -0
  110. package/dist/src/tools/notes.js +378 -0
  111. package/dist/src/tools/output.d.ts +23 -0
  112. package/dist/src/tools/output.js +39 -0
  113. package/dist/src/tools/result.d.ts +79 -0
  114. package/dist/src/tools/result.js +130 -0
  115. package/dist/src/tools/schema.d.ts +18 -0
  116. package/dist/src/tools/schema.js +31 -0
  117. package/dist/test/agent-loop.test.d.ts +1 -0
  118. package/dist/test/agent-loop.test.js +1497 -0
  119. package/dist/test/boot.integration.test.d.ts +1 -0
  120. package/dist/test/boot.integration.test.js +294 -0
  121. package/dist/test/budget-settings.integration.test.d.ts +1 -0
  122. package/dist/test/budget-settings.integration.test.js +144 -0
  123. package/dist/test/claude-mod.test.d.ts +1 -0
  124. package/dist/test/claude-mod.test.js +404 -0
  125. package/dist/test/dependencies.test.d.ts +1 -0
  126. package/dist/test/dependencies.test.js +101 -0
  127. package/dist/test/doctor.test.d.ts +1 -0
  128. package/dist/test/doctor.test.js +37 -0
  129. package/dist/test/dream-skill.test.d.ts +1 -0
  130. package/dist/test/dream-skill.test.js +72 -0
  131. package/dist/test/dream.test.d.ts +1 -0
  132. package/dist/test/dream.test.js +136 -0
  133. package/dist/test/helpers/dependency-graph.d.ts +5 -0
  134. package/dist/test/helpers/dependency-graph.js +73 -0
  135. package/dist/test/helpers/extension-test-environment.d.ts +1 -0
  136. package/dist/test/helpers/extension-test-environment.js +9 -0
  137. package/dist/test/helpers/extension.d.ts +119 -0
  138. package/dist/test/helpers/extension.js +368 -0
  139. package/dist/test/helpers/notes.d.ts +6 -0
  140. package/dist/test/helpers/notes.js +13 -0
  141. package/dist/test/history.integration.test.d.ts +1 -0
  142. package/dist/test/history.integration.test.js +556 -0
  143. package/dist/test/host-neutral.test.d.ts +1 -0
  144. package/dist/test/host-neutral.test.js +300 -0
  145. package/dist/test/notes-library.test.d.ts +1 -0
  146. package/dist/test/notes-library.test.js +214 -0
  147. package/dist/test/notes.integration.test.d.ts +1 -0
  148. package/dist/test/notes.integration.test.js +497 -0
  149. package/dist/test/notes.test.d.ts +1 -0
  150. package/dist/test/notes.test.js +528 -0
  151. package/dist/test/reset-lifecycle.test.d.ts +1 -0
  152. package/dist/test/reset-lifecycle.test.js +605 -0
  153. package/docs/architecture.md +63 -0
  154. package/docs/claude-code-mod.md +9 -0
  155. package/docs/notes-library.md +74 -0
  156. package/docs/reset-lifecycle.md +75 -0
  157. package/docs/sdk-integration.md +39 -0
  158. package/docs/skills.md +30 -0
  159. package/docs/tool-results.md +88 -0
  160. package/mods/notesoup/.claude-plugin/plugin.json +8 -0
  161. package/mods/notesoup/dist/claude/helper.js +9324 -0
  162. package/mods/notesoup/hooks/hooks.json +4 -0
  163. package/mods/notesoup/hooks/register.ts +241 -0
  164. package/mods/notesoup/playbook.md +34 -0
  165. package/mods/notesoup/skills/dream/SKILL.md +12 -0
  166. package/mods/pi-context/dist/claude/helper.js +9324 -0
  167. package/package.json +89 -5
  168. package/playbook.md +34 -0
  169. package/skills/dream/SKILL.md +12 -0
  170. package/skills/memory/SKILL.md +59 -0
  171. package/src/boot/render.ts +115 -0
  172. package/src/boot/snapshot.ts +46 -0
  173. package/src/boot/text.ts +66 -0
  174. package/src/budget/constants.ts +9 -0
  175. package/src/budget/policy.ts +36 -0
  176. package/src/budget/text.ts +11 -0
  177. package/src/claude/helper.ts +94 -0
  178. package/src/dream/doctor.ts +117 -0
  179. package/src/dream/gates.ts +26 -0
  180. package/src/dream/git.ts +73 -0
  181. package/src/dream/jail.ts +62 -0
  182. package/src/dream/lock.ts +82 -0
  183. package/src/dream/playbook.ts +3 -0
  184. package/src/dream/report.ts +36 -0
  185. package/src/dream/result.ts +2 -0
  186. package/src/dream/settings.ts +16 -0
  187. package/src/history/history.ts +270 -0
  188. package/src/history/query.ts +147 -0
  189. package/src/index.ts +1 -0
  190. package/src/notes/address.ts +110 -0
  191. package/src/notes/constants.ts +3 -0
  192. package/src/notes/errors.ts +15 -0
  193. package/src/notes/frontmatter.ts +166 -0
  194. package/src/notes/fs-error.ts +5 -0
  195. package/src/notes/identity.ts +40 -0
  196. package/src/notes/index.ts +24 -0
  197. package/src/notes/paths.ts +109 -0
  198. package/src/notes/store.ts +497 -0
  199. package/src/pi/boot.ts +88 -0
  200. package/src/pi/budget.ts +178 -0
  201. package/src/pi/dream/cli.ts +92 -0
  202. package/src/pi/dream/runner.ts +91 -0
  203. package/src/pi/dream/settings.ts +17 -0
  204. package/src/pi/entries.ts +7 -0
  205. package/src/pi/estimate.ts +116 -0
  206. package/src/pi/extension.ts +27 -0
  207. package/src/pi/history-tools.ts +12 -0
  208. package/src/pi/history.ts +162 -0
  209. package/src/pi/notes/adapter.ts +33 -0
  210. package/src/pi/notes/tools.ts +18 -0
  211. package/src/pi/reset/artifacts.ts +100 -0
  212. package/src/pi/reset/committed.ts +26 -0
  213. package/src/pi/reset/lifecycle.ts +342 -0
  214. package/src/pi/reset/text.ts +10 -0
  215. package/src/pi/runtime.ts +200 -0
  216. package/src/pi/session-reader.ts +6 -0
  217. package/src/pi/thresholds.ts +50 -0
  218. package/src/pi/tool-names.ts +13 -0
  219. package/src/pi/tool-result.ts +41 -0
  220. package/src/pi/window.ts +133 -0
  221. package/src/settings.ts +22 -0
  222. package/src/text-match.ts +14 -0
  223. package/src/tools/history.ts +273 -0
  224. package/src/tools/notes.ts +394 -0
  225. package/src/tools/output.ts +41 -0
  226. package/src/tools/result.ts +169 -0
  227. package/src/tools/schema.ts +31 -0
@@ -0,0 +1,4 @@
1
+ {
2
+ "description": "Claude Code notes tools and a session-scoped notes boot section",
3
+ "modules": ["./register.ts"]
4
+ }
@@ -0,0 +1,241 @@
1
+ import type { EngineInterface, On, ProcessRunResult, ToolCallResult, ToolSpec } from "claude-code"
2
+
3
+ const PREFIX = "mcp__notesoup__"
4
+ const TOOL_NAMES = ["notes_write", "notes_update", "notes_read", "notes_list", "notes_search"] as const
5
+ // @ts-expect-error Claude's hook sandbox supplies import.meta.dir; Node's typings do not.
6
+ const HELPER = `${import.meta.dir}/../dist/claude/helper.js`
7
+ const TIMEOUT_MS = 10_000
8
+ const OUTPUT_LIMIT = 1024 * 1024
9
+
10
+ type Identity = { home: string; sessionId: string; cwd: string; agent: string; model: string }
11
+ type SchemaRow = { name: string; description: string; inputSchema: Record<string, unknown> }
12
+ type Engine = Pick<EngineInterface, "process" | "session" | "ui" | "clock" | "prompt">
13
+ type ResetState = {
14
+ identity?: Identity; window: number; turnRunning: boolean; resetting: boolean
15
+ manualPending: boolean; headless: boolean; usageFailureLogged: boolean; preparedBoot?: string; retainedBoot?: string
16
+ }
17
+ const RESET_INSTRUCTIONS = "notesoup: reset to a fresh notes-only context window."
18
+ const BOOT_TOOLS = {
19
+ notes: `${PREFIX}notes_*`, notesList: `${PREFIX}notes_list`,
20
+ history: "history_* (unavailable in Claude)", historyWindows: "history_windows (unavailable in Claude)", historyList: "history_list (unavailable in Claude)",
21
+ historySearch: "history_search (unavailable in Claude)", historyRead: "history_read (unavailable in Claude)",
22
+ remaining: "get_context_remaining (unavailable in Claude)", wipe: "/clear-memory (user command; no model reset tool in Claude)",
23
+ }
24
+
25
+ async function buildBoot($: Engine, identity: Identity, window: number) {
26
+ return run($, { op: "boot", identity, window, tools: BOOT_TOOLS }, identity.cwd)
27
+ }
28
+ function log($: Engine, text: string) {
29
+ try { $.ui.log(`notesoup: ${text}`) } catch { /* UI must not break a turn or reset. */ }
30
+ }
31
+
32
+ function errorResult(message: string): ToolCallResult { return { result: `notesoup: ${message}`, isError: true } }
33
+ /** The helper's tool result: the shared text the model reads, plus whether that outcome refused. */
34
+ function toolResultOf(value: unknown): { text: string; ok: boolean } | undefined {
35
+ if (typeof value !== "object" || value === null) return undefined
36
+ const { text, outcome } = value as { text?: unknown; outcome?: unknown }
37
+ if (typeof text !== "string" || typeof outcome !== "object" || outcome === null) return undefined
38
+ const ok = (outcome as { ok?: unknown }).ok
39
+ if (typeof ok !== "boolean") return undefined
40
+ return { text, ok }
41
+ }
42
+ function decode(result: ProcessRunResult): { ok: true; result: unknown } | { ok: false; error: string } {
43
+ if (result.exitCode !== 0) return { ok: false, error: "helper exited unsuccessfully" }
44
+ if (result.stdout.length > OUTPUT_LIMIT) return { ok: false, error: "helper output exceeded the limit" }
45
+ try {
46
+ const value = JSON.parse(result.stdout.trim()) as { ok?: unknown; result?: unknown; error?: unknown }
47
+ if (value.ok === true) return { ok: true, result: value.result }
48
+ return { ok: false, error: typeof value.error === "string" ? value.error : "helper returned an invalid error" }
49
+ } catch { return { ok: false, error: "helper returned invalid JSON" } }
50
+ }
51
+ async function run($: { process: { run: (argv: readonly string[], init?: { cwd?: string; stdin?: string; timeoutMs?: number }) => Promise<ProcessRunResult> } }, request: unknown, cwd: string) {
52
+ try { return decode(await $.process.run(["node", HELPER], { cwd, stdin: `${JSON.stringify(request)}\n`, timeoutMs: TIMEOUT_MS })) }
53
+ catch { return { ok: false as const, error: "helper execution failed" } }
54
+ }
55
+ function identityOf(e: { cwd: string }, sessionId: string, home: string, model: string): Identity {
56
+ return { home, sessionId, cwd: e.cwd, agent: "claude", model: model.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "claude" }
57
+ }
58
+
59
+ export function register(on: On) {
60
+ let identity: Identity | undefined
61
+ const manualSignal: { wake?: () => void } = {}
62
+ const state: ResetState = { window: 0, turnRunning: false, resetting: false, manualPending: false, headless: false, usageFailureLogged: false }
63
+ const registered = new Set<string>()
64
+ on("session.start", async ($, e, next) => {
65
+ const home = await $.env.get("PI_NOTES_HOME") ?? `${await $.env.get("HOME") ?? await $.env.get("USERPROFILE") ?? ""}/.agents/notes`
66
+ identity = identityOf(e, await $.session.id(), home, "claude")
67
+ state.identity = identity
68
+ state.window = 0
69
+ state.turnRunning = state.resetting = state.manualPending = state.usageFailureLogged = false
70
+ state.headless = !e.isInteractive
71
+ if (state.headless) log($, "reset skipped in headless (-p / SDK) session.")
72
+ state.preparedBoot = state.retainedBoot = undefined
73
+ // The engine's history view includes retained boots on resume/reload.
74
+ // Recover their ordinal without treating that view as inference context.
75
+ try { restoreWindow(state, await $.session.messages()) } catch { /* A fresh session starts at zero. */ }
76
+ registered.clear()
77
+ armManual($, state, manualSignal)
78
+ await $.command.register({ name: "clear-memory", description: "Reset context to a fresh notes-only window", immediate: true })
79
+ const schemas = await run($, { op: "schemas" }, e.cwd)
80
+ if (!schemas.ok || !Array.isArray(schemas.result)) return next(e)
81
+ for (const row of schemas.result as SchemaRow[]) {
82
+ if (!TOOL_NAMES.includes(row.name as typeof TOOL_NAMES[number])) continue
83
+ const full = await $.tool.register({ name: row.name, description: row.description, inputSchema: row.inputSchema, isDeferred: false } satisfies ToolSpec)
84
+ registered.add(full.tool)
85
+ }
86
+ return next(e)
87
+ })
88
+ on("tool.call", async ($, e, next) => {
89
+ if (!registered.has(e.tool) || e.agentId !== undefined || !identity) return next(e)
90
+ const { tool, tool_use_id: _toolUseId, agentId: _agentId, ...params } = e
91
+ const result = await run($, { op: "tool", tool: tool.slice(PREFIX.length), params, identity }, identity.cwd)
92
+ if (!result.ok) return errorResult(result.error)
93
+ // The outcome already decided success or refusal; Claude requires isError: true on errors and no discriminator on success.
94
+ const outcome = toolResultOf(result.result)
95
+ if (!outcome) return errorResult("helper returned an invalid tool result")
96
+ return outcome.ok ? { result: outcome.text } : { result: outcome.text, isError: true }
97
+ })
98
+ on("turn.start", async ($, e, next) => {
99
+ state.turnRunning = true
100
+ return next(e)
101
+ })
102
+ on("turn.complete", async ($, e, next) => {
103
+ const result = await next(e)
104
+ if (e.agentId !== undefined) return result
105
+ state.turnRunning = false
106
+ await maybeReset($, state, true)
107
+ return result
108
+ })
109
+ on("session.measure", async ($, e, next) => {
110
+ const result = await next(e)
111
+ await maybeReset($, state, false)
112
+ return result
113
+ })
114
+ on("command.run", async ($, e, next) => {
115
+ if (e.command !== "clear-memory") return next(e)
116
+ if (state.headless) return { text: "notesoup: reset skipped in headless (-p / SDK) session." }
117
+ if (state.resetting || state.manualPending) return { text: "notesoup: reset already pending." }
118
+ if (state.turnRunning) {
119
+ state.manualPending = true
120
+ return { text: "notesoup: reset queued until the turn completes." }
121
+ }
122
+ // command.run holds a turn even when idle. Start one hidden close-out
123
+ // turn after it returns; turn.complete is the verified self-hook path.
124
+ state.manualPending = true
125
+ manualSignal.wake?.()
126
+ return { text: "notesoup: reset requested; closing out the current window." }
127
+ })
128
+ on("session.compact", async ($, e, next) => {
129
+ // Do not take over native /compact or another plugin's compaction.
130
+ if (!state.resetting || e.trigger !== "plugin" || e.agentId !== undefined || !identity) return next(e)
131
+ const boot = await buildBoot($, identity, state.window + 1)
132
+ if (!boot.ok) return { skip: `notesoup: Notes boot failed: ${boot.error}` }
133
+ state.preparedBoot = String(boot.result)
134
+ return { messages: [{ role: "user", text: state.preparedBoot, toolUses: [] }] }
135
+ })
136
+ on("prompt.context", async ($, e, next) => {
137
+ const result = await next(e)
138
+ // The reset already retained its boot as the sole conversation message.
139
+ // Prompt context still recomputes, but must not inject a duplicate snapshot.
140
+ if (state.retainedBoot !== undefined) return { ...result, blocks: result.blocks.filter((block) => block.name !== "notesoup:boot") }
141
+ if (result.blocks.some((block) => block.name === "notesoup:boot")) return result
142
+ if (!identity) return result
143
+ const boot = await buildBoot($, identity, state.window)
144
+ if (!boot.ok) {
145
+ try {
146
+ await $.ui.notify("notesoup: Notes boot failed.")
147
+ } catch { /* Notification failure must not suppress the fallback block. */ }
148
+ }
149
+ const block = { name: "notesoup:boot", text: boot.ok ? String(boot.result) : `# notesoup\n${boot.error}` }
150
+ return { ...result, blocks: [...result.blocks, block] }
151
+ })
152
+
153
+ }
154
+
155
+ function restoreWindow(state: ResetState, messages: readonly { role: string; text: string }[]) {
156
+ if (!state.identity) return
157
+ const prefix = `Current context window id: claude:${state.identity.sessionId}:`
158
+ for (const message of messages) {
159
+ if (message.role !== "user" || !message.text.startsWith("<context_window>\nAgent name: claude ")) continue
160
+ const line = message.text.split("\n").find((value) => value.startsWith(prefix))
161
+ if (!line) continue
162
+ const suffix = line.slice(prefix.length)
163
+ const window = Number(suffix)
164
+ if (!/^\d+$/.test(suffix) || !Number.isSafeInteger(window) || window <= state.window) continue
165
+ state.window = window
166
+ state.retainedBoot = message.text
167
+ }
168
+ }
169
+
170
+ // The waiter is created in session.start, so its Promise continuation keeps
171
+ // that non-held host context. Timers started inside command.run inherit its
172
+ // held dispatch even after the command returns. No polling timer is needed.
173
+ function armManual($: Engine, state: ResetState, signal: { wake?: () => void }) {
174
+ const requested = new Promise<void>((resolve) => { signal.wake = resolve })
175
+ void requested.then(() => {
176
+ signal.wake = undefined
177
+ $.clock.after(0, () => finishManual($, state, signal))
178
+ })
179
+ }
180
+ async function finishManual($: Engine, state: ResetState, signal: { wake?: () => void }) {
181
+ try {
182
+ if (state.manualPending && !state.turnRunning) {
183
+ await $.prompt.submit({ text: "A context reset was requested. Save any essential checkpoint to session notes now, then end this turn. notesoup will reset to a fresh notes-only window after the turn completes." })
184
+ }
185
+ } catch {
186
+ state.manualPending = false
187
+ log($, "manual reset skipped; the close-out turn could not start.")
188
+ } finally { armManual($, state, signal) }
189
+ }
190
+
191
+ // The sandbox permits $ forwarding only to file-top-level helpers.
192
+ async function maybeReset($: Engine, state: ResetState, complete: boolean) {
193
+ if (!state.identity || state.resetting || state.headless) return
194
+ let percent: number | undefined
195
+ try { percent = (await $.session.usage()).context?.percent }
196
+ catch {
197
+ if (!state.usageFailureLogged) {
198
+ state.usageFailureLogged = true
199
+ log($, "context usage unavailable; automatic reset skipped.")
200
+ }
201
+ }
202
+ if (state.turnRunning) return
203
+ if ((complete && state.manualPending) || (typeof percent === "number" && Number.isFinite(percent) && percent >= 85)) {
204
+ state.manualPending = false
205
+ await reset($, state)
206
+ }
207
+ }
208
+ async function reset($: Engine, state: ResetState): Promise<string> {
209
+ if (!state.identity) return "notesoup: reset skipped; notes identity unavailable."
210
+ if (state.headless) return "notesoup: reset skipped in headless (-p / SDK) session."
211
+ if (state.resetting) return "notesoup: reset already in progress."
212
+ state.resetting = true
213
+ state.preparedBoot = undefined
214
+ try {
215
+ const result = await $.session.compact({ instructions: RESET_INSTRUCTIONS })
216
+ if (result.skip !== undefined || state.preparedBoot === undefined) {
217
+ const text = `reset skipped: ${result.skip ?? "notes boot was not retained"}`
218
+ log($, text)
219
+ return `notesoup: ${text}`
220
+ }
221
+ state.retainedBoot = state.preparedBoot
222
+ state.window++
223
+ log($, `memory cleared · window ${state.window}`)
224
+ return `notesoup: memory cleared · window ${state.window}`
225
+ } catch (error) {
226
+ const text = String(error)
227
+ if (text.includes("headless (-p / SDK)")) {
228
+ state.headless = true
229
+ log($, "reset skipped in headless (-p / SDK) session.")
230
+ } else if (/a turn is (running|in flight)/.test(text)) {
231
+ state.manualPending = true
232
+ return "notesoup: reset queued until the turn completes."
233
+ } else {
234
+ log($, `reset failed; keeping the current window: ${text.replace(/[\r\n].*$/s, "")}`)
235
+ }
236
+ return state.headless ? "notesoup: reset skipped in headless (-p / SDK) session." : "notesoup: reset failed; keeping the current window."
237
+ } finally {
238
+ state.preparedBoot = undefined
239
+ state.resetting = false
240
+ }
241
+ }
@@ -0,0 +1,34 @@
1
+ # Dream
2
+
3
+ I am reviewing my notes: keeping what is useful, connecting what belongs together, and leaving a clearer record for whoever comes next. Notes are record, not memory.
4
+
5
+ ## Scope
6
+
7
+ Work within the readable sources and writable homes established for this run. Do not broaden that scope while exploring. If a boundary is unclear, ask before crossing it.
8
+
9
+ A project dream draws from that project's material, project notes, and session notes identified as belonging to the project; it organizes only that project's notes. A store-wide dream may inspect across homes, but authorship and write permissions still apply. Unknown ownership is a coverage gap to report, not a reason to guess or repair ownership.
10
+
11
+ The homes serve different readers:
12
+
13
+ - Session notes are a trip's working record. Read them as sources where permitted; never edit them, even after promoting a useful finding.
14
+ - Project notes hold knowledge for whoever next works on that project.
15
+ - Human notes hold the human's cross-project preferences and standing rules. Do not turn project facts or your own lessons into rules in the human's voice.
16
+ - Agent notes belong to their author. Other agents' notes are read-only unless the human authorizes revision.
17
+ - Model notes describe the substrate's behavior. Other models' notes are read-only unless the human authorizes revision.
18
+
19
+ ## Review and consolidate
20
+
21
+ 1. **Leave stable notes alone.** Change a note to incorporate new evidence, resolve a verified error, merge a genuine duplicate, or split an oversized file—not merely to shorten or rephrase it. No change is a valid outcome.
22
+ 2. **Verify only what the proposed change depends on.** Before correcting or promoting a conclusion, check the relevant evidence as needed. Do not revalidate every stable note or turn dream into a source-code audit. An unavailable referent is unverified, not proof that it is dead; do not promote an unsupported claim as current fact.
23
+ 3. **Merge only genuine duplicates.** Supersede a note only when both notes have the same topic and kind (checkpoint, design, log…), and the survivor is strictly newer or more specific. Name the shared topic in the survivor. Otherwise keep both and record any unresolved conflict.
24
+ 4. **Relocate misfiled notes.** When a standing ruling or the note's own MAP already names its proper home, migrate it: copy it there preserving frontmatter with a one-line provenance note, crumple the original, and update both homes' maps. Crumple keeps the original readable by address, so relocation is fully reversible—do it, don't propose it. Propose only when the destination home itself is unclear.
25
+ 5. **Preserve meaning and provenance.** Keep facts, conditions, exceptions, uncertainty, authorship, and sources. When promoting a finding, cite its source. Do not relabel agent-authored material as human-authored. If its destination or authority is unclear, propose the promotion instead.
26
+ 6. **Keep notes manageable.** Aim below about 200 lines / 8KB per note. Split oversized notes by topic with one-line cross-links. Checkpoints may exceed the budget: trim prose, never facts.
27
+ 7. **Keep maps useful.** Each writable home's MAP.md lists its durable notes, one line per entry, with an unambiguous address and a short gist in your own words. Add promoted notes and drop entries that no longer hold. Maps follow the same size budget.
28
+ 8. **Preserve the record.** Never physically delete a live note; after merging, crumple the absorbed note only if it is writable. The wastebasket is the one exception: physically delete a crumpled note when its home is writable and both `crumpledAt` and its last access are more than 7 days old. Judge the clock from the raw frontmatter—`lastAccessed`, falling back to `crumpledAt` when `lastAccessed` is absent; if neither is readable, keep the note and report it. Never take the date through `notes_read`: reading refreshes `lastAccessed` and resets the very clock you are measuring. A venue with no way to delete files lists the doomed addresses in its report instead of blanking or truncating them. Preserve existing metadata and provenance. Map entry lines are yours to maintain within the writable scope.
29
+
30
+ ## Report
31
+
32
+ Give a concise report grouped by home: what you inspected, changed, promoted, deleted, and left unresolved. Mention coverage gaps and partial failures. If no notes changed, say so. Distinguish actual note changes from execution or audit artifacts; do not claim success for an incomplete run.
33
+
34
+ Put skill ideas and unresolved questions in the final message as proposals for the human, not new skill files.
@@ -0,0 +1,12 @@
1
+ ---
2
+ name: dream
3
+ description: Review and consolidate durable notes using notesoup's shared dream playbook. Use when the human asks to dream over notes, merge genuine duplicates, update maps, or promote durable learning.
4
+ ---
5
+
6
+ # Dream
7
+
8
+ Read `../../playbook.md` completely, resolving the path from this skill's directory. It is the shared guide to reviewing and organizing notes.
9
+
10
+ Dream in this agent's current session. Do not start another agent or model unless the human asks.
11
+
12
+ Work within the supplied readable and writable scope. If the scope or permission to write has not been established, ask rather than inventing it or setting up execution machinery yourself.