notesoup 0.0.0-stage → 0.40.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 +94 -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 +17 -0
  8. package/dist/src/boot/text.js +48 -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 +156 -0
  36. package/dist/src/history/history.js +212 -0
  37. package/dist/src/history/query.d.ts +63 -0
  38. package/dist/src/history/query.js +201 -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 +103 -0
  58. package/dist/src/notes/store.js +474 -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 +9 -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 +7 -0
  98. package/dist/src/pi/tool-names.js +7 -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 +58 -0
  108. package/dist/src/tools/history.js +210 -0
  109. package/dist/src/tools/notes.d.ts +250 -0
  110. package/dist/src/tools/notes.js +360 -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 +82 -0
  114. package/dist/src/tools/result.js +104 -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 +367 -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 +633 -0
  143. package/dist/test/host-neutral.test.d.ts +1 -0
  144. package/dist/test/host-neutral.test.js +274 -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 +515 -0
  149. package/dist/test/notes.test.d.ts +1 -0
  150. package/dist/test/notes.test.js +530 -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 +78 -0
  160. package/mods/notesoup/.claude-plugin/plugin.json +8 -0
  161. package/mods/notesoup/dist/claude/helper.js +9265 -0
  162. package/mods/notesoup/hooks/hooks.json +4 -0
  163. package/mods/notesoup/hooks/register.ts +240 -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 +58 -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 +286 -0
  188. package/src/history/query.ts +222 -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 +494 -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 +11 -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 +9 -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 +234 -0
  224. package/src/tools/notes.ts +381 -0
  225. package/src/tools/output.ts +41 -0
  226. package/src/tools/result.ts +144 -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,240 @@
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)",
21
+ remaining: "get_context_remaining (unavailable in Claude)", wipe: "/clear-memory (user command; no model reset tool in Claude)",
22
+ }
23
+
24
+ async function buildBoot($: Engine, identity: Identity, window: number) {
25
+ return run($, { op: "boot", identity, window, tools: BOOT_TOOLS }, identity.cwd)
26
+ }
27
+ function log($: Engine, text: string) {
28
+ try { $.ui.log(`notesoup: ${text}`) } catch { /* UI must not break a turn or reset. */ }
29
+ }
30
+
31
+ function errorResult(message: string): ToolCallResult { return { result: `notesoup: ${message}`, isError: true } }
32
+ /** The helper's tool result: the shared text the model reads, plus whether that outcome refused. */
33
+ function toolResultOf(value: unknown): { text: string; ok: boolean } | undefined {
34
+ if (typeof value !== "object" || value === null) return undefined
35
+ const { text, outcome } = value as { text?: unknown; outcome?: unknown }
36
+ if (typeof text !== "string" || typeof outcome !== "object" || outcome === null) return undefined
37
+ const ok = (outcome as { ok?: unknown }).ok
38
+ if (typeof ok !== "boolean") return undefined
39
+ return { text, ok }
40
+ }
41
+ function decode(result: ProcessRunResult): { ok: true; result: unknown } | { ok: false; error: string } {
42
+ if (result.exitCode !== 0) return { ok: false, error: "helper exited unsuccessfully" }
43
+ if (result.stdout.length > OUTPUT_LIMIT) return { ok: false, error: "helper output exceeded the limit" }
44
+ try {
45
+ const value = JSON.parse(result.stdout.trim()) as { ok?: unknown; result?: unknown; error?: unknown }
46
+ if (value.ok === true) return { ok: true, result: value.result }
47
+ return { ok: false, error: typeof value.error === "string" ? value.error : "helper returned an invalid error" }
48
+ } catch { return { ok: false, error: "helper returned invalid JSON" } }
49
+ }
50
+ async function run($: { process: { run: (argv: readonly string[], init?: { cwd?: string; stdin?: string; timeoutMs?: number }) => Promise<ProcessRunResult> } }, request: unknown, cwd: string) {
51
+ try { return decode(await $.process.run(["node", HELPER], { cwd, stdin: `${JSON.stringify(request)}\n`, timeoutMs: TIMEOUT_MS })) }
52
+ catch { return { ok: false as const, error: "helper execution failed" } }
53
+ }
54
+ function identityOf(e: { cwd: string }, sessionId: string, home: string, model: string): Identity {
55
+ return { home, sessionId, cwd: e.cwd, agent: "claude", model: model.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "claude" }
56
+ }
57
+
58
+ export function register(on: On) {
59
+ let identity: Identity | undefined
60
+ const manualSignal: { wake?: () => void } = {}
61
+ const state: ResetState = { window: 0, turnRunning: false, resetting: false, manualPending: false, headless: false, usageFailureLogged: false }
62
+ const registered = new Set<string>()
63
+ on("session.start", async ($, e, next) => {
64
+ const home = await $.env.get("PI_NOTES_HOME") ?? `${await $.env.get("HOME") ?? await $.env.get("USERPROFILE") ?? ""}/.agents/notes`
65
+ identity = identityOf(e, await $.session.id(), home, "claude")
66
+ state.identity = identity
67
+ state.window = 0
68
+ state.turnRunning = state.resetting = state.manualPending = state.usageFailureLogged = false
69
+ state.headless = !e.isInteractive
70
+ if (state.headless) log($, "reset skipped in headless (-p / SDK) session.")
71
+ state.preparedBoot = state.retainedBoot = undefined
72
+ // The engine's history view includes retained boots on resume/reload.
73
+ // Recover their ordinal without treating that view as inference context.
74
+ try { restoreWindow(state, await $.session.messages()) } catch { /* A fresh session starts at zero. */ }
75
+ registered.clear()
76
+ armManual($, state, manualSignal)
77
+ await $.command.register({ name: "clear-memory", description: "Reset context to a fresh notes-only window", immediate: true })
78
+ const schemas = await run($, { op: "schemas" }, e.cwd)
79
+ if (!schemas.ok || !Array.isArray(schemas.result)) return next(e)
80
+ for (const row of schemas.result as SchemaRow[]) {
81
+ if (!TOOL_NAMES.includes(row.name as typeof TOOL_NAMES[number])) continue
82
+ const full = await $.tool.register({ name: row.name, description: row.description, inputSchema: row.inputSchema, isDeferred: false } satisfies ToolSpec)
83
+ registered.add(full.tool)
84
+ }
85
+ return next(e)
86
+ })
87
+ on("tool.call", async ($, e, next) => {
88
+ if (!registered.has(e.tool) || e.agentId !== undefined || !identity) return next(e)
89
+ const { tool, tool_use_id: _toolUseId, agentId: _agentId, ...params } = e
90
+ const result = await run($, { op: "tool", tool: tool.slice(PREFIX.length), params, identity }, identity.cwd)
91
+ if (!result.ok) return errorResult(result.error)
92
+ // The outcome already decided success or refusal; Claude requires isError: true on errors and no discriminator on success.
93
+ const outcome = toolResultOf(result.result)
94
+ if (!outcome) return errorResult("helper returned an invalid tool result")
95
+ return outcome.ok ? { result: outcome.text } : { result: outcome.text, isError: true }
96
+ })
97
+ on("turn.start", async ($, e, next) => {
98
+ state.turnRunning = true
99
+ return next(e)
100
+ })
101
+ on("turn.complete", async ($, e, next) => {
102
+ const result = await next(e)
103
+ if (e.agentId !== undefined) return result
104
+ state.turnRunning = false
105
+ await maybeReset($, state, true)
106
+ return result
107
+ })
108
+ on("session.measure", async ($, e, next) => {
109
+ const result = await next(e)
110
+ await maybeReset($, state, false)
111
+ return result
112
+ })
113
+ on("command.run", async ($, e, next) => {
114
+ if (e.command !== "clear-memory") return next(e)
115
+ if (state.headless) return { text: "notesoup: reset skipped in headless (-p / SDK) session." }
116
+ if (state.resetting || state.manualPending) return { text: "notesoup: reset already pending." }
117
+ if (state.turnRunning) {
118
+ state.manualPending = true
119
+ return { text: "notesoup: reset queued until the turn completes." }
120
+ }
121
+ // command.run holds a turn even when idle. Start one hidden close-out
122
+ // turn after it returns; turn.complete is the verified self-hook path.
123
+ state.manualPending = true
124
+ manualSignal.wake?.()
125
+ return { text: "notesoup: reset requested; closing out the current window." }
126
+ })
127
+ on("session.compact", async ($, e, next) => {
128
+ // Do not take over native /compact or another plugin's compaction.
129
+ if (!state.resetting || e.trigger !== "plugin" || e.agentId !== undefined || !identity) return next(e)
130
+ const boot = await buildBoot($, identity, state.window + 1)
131
+ if (!boot.ok) return { skip: `notesoup: Notes boot failed: ${boot.error}` }
132
+ state.preparedBoot = String(boot.result)
133
+ return { messages: [{ role: "user", text: state.preparedBoot, toolUses: [] }] }
134
+ })
135
+ on("prompt.context", async ($, e, next) => {
136
+ const result = await next(e)
137
+ // The reset already retained its boot as the sole conversation message.
138
+ // Prompt context still recomputes, but must not inject a duplicate snapshot.
139
+ if (state.retainedBoot !== undefined) return { ...result, blocks: result.blocks.filter((block) => block.name !== "notesoup:boot") }
140
+ if (result.blocks.some((block) => block.name === "notesoup:boot")) return result
141
+ if (!identity) return result
142
+ const boot = await buildBoot($, identity, state.window)
143
+ if (!boot.ok) {
144
+ try {
145
+ await $.ui.notify("notesoup: Notes boot failed.")
146
+ } catch { /* Notification failure must not suppress the fallback block. */ }
147
+ }
148
+ const block = { name: "notesoup:boot", text: boot.ok ? String(boot.result) : `# notesoup\n${boot.error}` }
149
+ return { ...result, blocks: [...result.blocks, block] }
150
+ })
151
+
152
+ }
153
+
154
+ function restoreWindow(state: ResetState, messages: readonly { role: string; text: string }[]) {
155
+ if (!state.identity) return
156
+ const prefix = `Current context window id: claude:${state.identity.sessionId}:`
157
+ for (const message of messages) {
158
+ if (message.role !== "user" || !message.text.startsWith("<context_window>\nAgent name: claude ")) continue
159
+ const line = message.text.split("\n").find((value) => value.startsWith(prefix))
160
+ if (!line) continue
161
+ const suffix = line.slice(prefix.length)
162
+ const window = Number(suffix)
163
+ if (!/^\d+$/.test(suffix) || !Number.isSafeInteger(window) || window <= state.window) continue
164
+ state.window = window
165
+ state.retainedBoot = message.text
166
+ }
167
+ }
168
+
169
+ // The waiter is created in session.start, so its Promise continuation keeps
170
+ // that non-held host context. Timers started inside command.run inherit its
171
+ // held dispatch even after the command returns. No polling timer is needed.
172
+ function armManual($: Engine, state: ResetState, signal: { wake?: () => void }) {
173
+ const requested = new Promise<void>((resolve) => { signal.wake = resolve })
174
+ void requested.then(() => {
175
+ signal.wake = undefined
176
+ $.clock.after(0, () => finishManual($, state, signal))
177
+ })
178
+ }
179
+ async function finishManual($: Engine, state: ResetState, signal: { wake?: () => void }) {
180
+ try {
181
+ if (state.manualPending && !state.turnRunning) {
182
+ 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." })
183
+ }
184
+ } catch {
185
+ state.manualPending = false
186
+ log($, "manual reset skipped; the close-out turn could not start.")
187
+ } finally { armManual($, state, signal) }
188
+ }
189
+
190
+ // The sandbox permits $ forwarding only to file-top-level helpers.
191
+ async function maybeReset($: Engine, state: ResetState, complete: boolean) {
192
+ if (!state.identity || state.resetting || state.headless) return
193
+ let percent: number | undefined
194
+ try { percent = (await $.session.usage()).context?.percent }
195
+ catch {
196
+ if (!state.usageFailureLogged) {
197
+ state.usageFailureLogged = true
198
+ log($, "context usage unavailable; automatic reset skipped.")
199
+ }
200
+ }
201
+ if (state.turnRunning) return
202
+ if ((complete && state.manualPending) || (typeof percent === "number" && Number.isFinite(percent) && percent >= 85)) {
203
+ state.manualPending = false
204
+ await reset($, state)
205
+ }
206
+ }
207
+ async function reset($: Engine, state: ResetState): Promise<string> {
208
+ if (!state.identity) return "notesoup: reset skipped; notes identity unavailable."
209
+ if (state.headless) return "notesoup: reset skipped in headless (-p / SDK) session."
210
+ if (state.resetting) return "notesoup: reset already in progress."
211
+ state.resetting = true
212
+ state.preparedBoot = undefined
213
+ try {
214
+ const result = await $.session.compact({ instructions: RESET_INSTRUCTIONS })
215
+ if (result.skip !== undefined || state.preparedBoot === undefined) {
216
+ const text = `reset skipped: ${result.skip ?? "notes boot was not retained"}`
217
+ log($, text)
218
+ return `notesoup: ${text}`
219
+ }
220
+ state.retainedBoot = state.preparedBoot
221
+ state.window++
222
+ log($, `memory cleared · window ${state.window}`)
223
+ return `notesoup: memory cleared · window ${state.window}`
224
+ } catch (error) {
225
+ const text = String(error)
226
+ if (text.includes("headless (-p / SDK)")) {
227
+ state.headless = true
228
+ log($, "reset skipped in headless (-p / SDK) session.")
229
+ } else if (/a turn is (running|in flight)/.test(text)) {
230
+ state.manualPending = true
231
+ return "notesoup: reset queued until the turn completes."
232
+ } else {
233
+ log($, `reset failed; keeping the current window: ${text.replace(/[\r\n].*$/s, "")}`)
234
+ }
235
+ return state.headless ? "notesoup: reset skipped in headless (-p / SDK) session." : "notesoup: reset failed; keeping the current window."
236
+ } finally {
237
+ state.preparedBoot = undefined
238
+ state.resetting = false
239
+ }
240
+ }
@@ -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.