notesoup 0.41.0 → 0.42.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 (59) hide show
  1. package/dist/src/boot/text.d.ts +12 -0
  2. package/dist/src/boot/text.js +14 -2
  3. package/dist/src/budget/text.d.ts +12 -0
  4. package/dist/src/budget/text.js +12 -0
  5. package/dist/src/claude/helper.js +23 -2
  6. package/dist/src/claude/thresholds.d.ts +39 -0
  7. package/dist/src/claude/thresholds.js +149 -0
  8. package/dist/src/dream/doctor.js +1 -1
  9. package/dist/src/dream/jail.js +10 -10
  10. package/dist/src/history/history.js +1 -1
  11. package/dist/src/history/query.js +6 -6
  12. package/dist/src/notes/address.d.ts +1 -1
  13. package/dist/src/notes/address.js +12 -12
  14. package/dist/src/notes/store.js +16 -12
  15. package/dist/src/pi/reset/text.d.ts +2 -3
  16. package/dist/src/pi/reset/text.js +5 -4
  17. package/dist/src/pi/runtime.js +1 -1
  18. package/dist/src/pi/tool-result.js +2 -2
  19. package/dist/src/settings.d.ts +8 -0
  20. package/dist/src/settings.js +6 -1
  21. package/dist/src/tools/history.js +12 -12
  22. package/dist/src/tools/notes.d.ts +3 -3
  23. package/dist/src/tools/notes.js +12 -11
  24. package/dist/src/tools/output.d.ts +2 -0
  25. package/dist/src/tools/output.js +2 -0
  26. package/dist/src/tools/result.js +2 -2
  27. package/dist/src/tools/schema.js +3 -3
  28. package/dist/test/claude-mod.test.js +206 -16
  29. package/dist/test/dream.test.js +3 -3
  30. package/dist/test/history.integration.test.js +7 -8
  31. package/dist/test/host-neutral.test.js +1 -1
  32. package/dist/test/notes-library.test.js +1 -1
  33. package/dist/test/notes.integration.test.js +4 -4
  34. package/dist/test/notes.test.js +16 -16
  35. package/docs/claude-code-mod.md +10 -2
  36. package/mods/notesoup/.claude-plugin/plugin.json +1 -1
  37. package/mods/notesoup/dist/claude/helper.js +200 -41
  38. package/mods/notesoup/hooks/register.ts +193 -27
  39. package/mods/pi-context/dist/claude/helper.js +9324 -0
  40. package/package.json +1 -1
  41. package/src/boot/text.ts +20 -2
  42. package/src/budget/text.ts +17 -0
  43. package/src/claude/helper.ts +20 -3
  44. package/src/claude/thresholds.ts +170 -0
  45. package/src/dream/doctor.ts +1 -1
  46. package/src/dream/jail.ts +10 -10
  47. package/src/history/history.ts +1 -1
  48. package/src/history/query.ts +6 -6
  49. package/src/notes/address.ts +12 -12
  50. package/src/notes/store.ts +17 -12
  51. package/src/pi/reset/text.ts +6 -7
  52. package/src/pi/runtime.ts +1 -1
  53. package/src/pi/tool-result.ts +2 -2
  54. package/src/settings.ts +18 -2
  55. package/src/tools/history.ts +12 -12
  56. package/src/tools/notes.ts +14 -12
  57. package/src/tools/output.ts +2 -0
  58. package/src/tools/result.ts +2 -2
  59. package/src/tools/schema.ts +3 -3
package/src/settings.ts CHANGED
@@ -2,7 +2,18 @@ export const NOTESOUP_SETTINGS_KEY = "notesoup";
2
2
  /** Nested under "notesoup": the default dreamer model pattern, overridden by CLI --dreamer. */
3
3
  export const NOTESOUP_DREAMER_KEY = "dreamer";
4
4
 
5
- export type NotesoupSettings = { reminderMarginTokens?: unknown; dreamer?: unknown };
5
+ /**
6
+ * The one notesoup settings shape both hosts read. `reminderMarginTokens` is Pi's early
7
+ * reminder margin; `resetMarginTokens` and `warningRunwayTokens` are the Claude mod's
8
+ * margins against Claude's own auto-compact point (see src/claude/thresholds.ts). A host
9
+ * ignores the keys it does not use; every value is validated where it is consumed.
10
+ */
11
+ export type NotesoupSettings = {
12
+ reminderMarginTokens?: unknown;
13
+ resetMarginTokens?: unknown;
14
+ warningRunwayTokens?: unknown;
15
+ dreamer?: unknown;
16
+ };
6
17
 
7
18
  function isSettingsObject(value: unknown): value is Record<string, unknown> {
8
19
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -18,5 +29,10 @@ function notesoupSettings(settings: unknown): Record<string, unknown> {
18
29
  /** Merge the global and project "notesoup" objects per key; project wins, mirroring Pi's deep merge. */
19
30
  export function mergeNotesoupSettings(globalSettings: unknown, projectSettings: unknown): NotesoupSettings {
20
31
  const merged = { ...notesoupSettings(globalSettings), ...notesoupSettings(projectSettings) };
21
- return { reminderMarginTokens: merged.reminderMarginTokens, dreamer: merged.dreamer };
32
+ return {
33
+ reminderMarginTokens: merged.reminderMarginTokens,
34
+ resetMarginTokens: merged.resetMarginTokens,
35
+ warningRunwayTokens: merged.warningRunwayTokens,
36
+ dreamer: merged.dreamer,
37
+ };
22
38
  }
@@ -1,6 +1,6 @@
1
1
  /** Host-neutral history operations: one bounded payload for text and structured results. */
2
2
  import { Type, type Static } from "typebox";
3
- import { HISTORY_PREVIEW_CHARS } from "./output.js";
3
+ import { HISTORY_PREVIEW_CHARS, TOOL_OUTPUT_LIMIT } from "./output.js";
4
4
  import { failure, fitsResult, resultSchema, readTextWindow, renderOutcome, renderTextWindow, success, TextWindowRequestSchema, TextWindowSchema, type Operation, type Outcome } from "./result.js";
5
5
  import { historyRoles, searchQuery } from "./schema.js";
6
6
  import { HistoryPageSchema, selectPage, type HistoryPage } from "../history/query.js";
@@ -66,7 +66,7 @@ function renderFoldedRow(row: HistoryFoldedRow): string {
66
66
 
67
67
  function renderPage(page: HistoryPage): string {
68
68
  // An empty selection has no anchors either, so it would otherwise render as nothing at all.
69
- if (page.items.length === 0) return "no events match this selection";
69
+ if (page.items.length === 0) return "No events match these filters.";
70
70
  const body = page.items.map((item) => (isFoldedRow(item) ? renderFoldedRow(item) : renderPageItem(item))).join("\n\n");
71
71
  const cursors = [
72
72
  page.page_older === null ? "" : `→ older: ${JSON.stringify(page.page_older)}`,
@@ -94,7 +94,7 @@ const MAX_REPORTED_WINDOWS = 5;
94
94
 
95
95
  /** At most `MAX_REPORTED_WINDOWS` known ids, appended to an unknown_window_id message. */
96
96
  function knownWindowSuffix(known: string[]): string {
97
- return known.length === 0 ? "" : `; known: ${known.slice(0, MAX_REPORTED_WINDOWS).join(", ")}`;
97
+ return known.length === 0 ? "" : `; its windows: ${known.slice(0, MAX_REPORTED_WINDOWS).join(", ")}`;
98
98
  }
99
99
 
100
100
  export const PREVIOUS_WINDOW = "@previous";
@@ -105,13 +105,13 @@ function resolveWindowId(projection: HistoryProjection, windowId: string | undef
105
105
  const previous = projection.windows.at(-2);
106
106
  if (previous !== undefined) return { ok: true, window_id: previous.windowId };
107
107
  const known = projection.windows.map((current) => current.windowId);
108
- return { ok: false, outcome: failure("unknown_window_id", `unknown window_id "${PREVIOUS_WINDOW}": this session has no window before the current one${knownWindowSuffix(known)}`) };
108
+ return { ok: false, outcome: failure("unknown_window_id", `"${PREVIOUS_WINDOW}" has nothing to point at: this session has no window before the current one${knownWindowSuffix(known)}.`) };
109
109
  }
110
110
 
111
111
  function badWindowId(projection: HistoryProjection, params: HistoryFilter): Outcome<never> | undefined {
112
112
  const badWindow = unknownWindowId(projection, params);
113
113
  if (!badWindow) return undefined;
114
- return failure("unknown_window_id", `${badWindow.message}${knownWindowSuffix(badWindow.known)}`);
114
+ return failure("unknown_window_id", `${badWindow.message}${knownWindowSuffix(badWindow.known)}.`);
115
115
  }
116
116
 
117
117
  /* ------------------------------------------------------------------ history_windows */
@@ -139,14 +139,14 @@ export const historyWindows: Operation<typeof historyWindowsParameters, HistoryW
139
139
  data.windows.push(row);
140
140
  data.more--;
141
141
  }
142
- return fits(data) ? success(data) : failure("output_too_large", "The session identity does not fit one `history_windows` response.");
142
+ return fits(data) ? success(data) : failure("output_too_large", `The session id alone fills the ${TOOL_OUTPUT_LIMIT} a call can carry.`);
143
143
  },
144
144
  render: (result) => renderOutcome(result, renderWindows),
145
145
  };
146
146
 
147
147
  function renderWindows(data: HistoryWindowsData): string {
148
148
  const lines = [`session ${data.session_id}`, ...data.windows.map((window) => `window ${window.window_id} | seqs ${window.first_seq ?? "-"}-${window.last_seq ?? "-"}`)];
149
- if (data.more > 0) lines.push(`${data.more} more windows not shown; \`history_list\` also reports window ids on events`);
149
+ if (data.more > 0) lines.push(`${data.more} more windows not shown; every \`history_list\` event also names its window.`);
150
150
  return lines.join("\n");
151
151
  }
152
152
 
@@ -197,22 +197,22 @@ export const historyRead: Operation<typeof historyReadParameters, HistoryReadDat
197
197
  // Address checks run in one order: past the end of the file, off this branch, then
198
198
  // consumed by a pairing. Only the last two can mention what is at the address.
199
199
  if (item === undefined && params.seq > projection.highestSeq) {
200
- return failure("unknown_seq", `unknown seq ${params.seq}: this session's items run 1..${projection.highestSeq}`);
200
+ return failure("unknown_seq", `There is no seq ${params.seq}; this session runs from seq 1 to ${projection.highestSeq}.`);
201
201
  }
202
202
  if (!projection.branchSeqs.has(params.seq)) {
203
- return failure("not_on_branch", `seq ${params.seq} is on another branch and is not readable here`);
203
+ return failure("not_on_branch", `Seq ${params.seq} is on another branch of this session; it can't be read from here.`);
204
204
  }
205
205
  if (item === undefined) {
206
206
  const callSeq = projection.pairedResults.get(params.seq);
207
207
  if (callSeq !== undefined) {
208
- return failure("not_an_event", `seq ${params.seq} is a tool result already shown inside the call at seq ${callSeq}; read that seq instead`);
208
+ return failure("not_an_event", `Seq ${params.seq} is a tool result, shown together with its call; read seq ${callSeq} instead.`);
209
209
  }
210
- return failure("unknown_seq", `seq ${params.seq} is on this branch but carries no readable event`);
210
+ return failure("unknown_seq", `Seq ${params.seq} holds nothing to read.`);
211
211
  }
212
212
  const document = eventDocument(item);
213
213
  const totalChars = Array.from(document).length;
214
214
  if (typeof params.offset_chars === "number" && params.offset_chars > totalChars) {
215
- return failure("invalid_offset", `offset_chars ${params.offset_chars} is past the end: the item has ${totalChars} chars; the largest legal offset is ${totalChars} (an empty end-read)`);
215
+ return failure("invalid_offset", `This event ends at char ${totalChars}; offset_chars ${params.offset_chars} is past the last word.`);
216
216
  }
217
217
  return readTextWindow(
218
218
  document,
@@ -3,7 +3,7 @@ import { Type, type Static } from "typebox";
3
3
  import { localIso } from "../notes/frontmatter.js";
4
4
  import { isFilesystemError } from "../notes/fs-error.js";
5
5
  import { createNotesStore, NoteError, noteIdentity, type NoteChange, type NoteMeta, type NoteQueryStatus, type NoteRow, type Scope } from "../notes/index.js";
6
- import { prefixFit } from "./output.js";
6
+ import { prefixFit, TOOL_OUTPUT_LIMIT } from "./output.js";
7
7
  import { failure, fitsResult, resultSchema, readTextWindow, renderOutcome, renderTextWindow, structuredBytes, success, TextWindowRequestSchema, TextWindowSchema, type Outcome } from "./result.js";
8
8
  import { nullableString, InvalidQueryError, searchQueries, searchQuery } from "./schema.js";
9
9
 
@@ -171,7 +171,7 @@ export type NotesSearchData = Static<typeof NotesSearchDataSchema>;
171
171
  type Snapshot<T> = Omit<NotesListData, "files"> & { files: T[] };
172
172
 
173
173
  /** Keep identities intact. Only search previews may shrink to make the first row fit. */
174
- function snapshot<T>(
174
+ function snapshot<T extends { address: string }>(
175
175
  rows: T[],
176
176
  limit: number | undefined,
177
177
  status: NoteQueryStatus,
@@ -191,11 +191,11 @@ function snapshot<T>(
191
191
  const reduced = shrink(item, fits);
192
192
  if (fits(reduced)) files.push(reduced);
193
193
  }
194
- if (files.length === 0) return failure("output_too_large", "The first matching note cannot fit in a response without losing its identity.");
194
+ if (files.length === 0) return failure("output_too_large", `\`${item.address}\` won't fit on the list (${TOOL_OUTPUT_LIMIT} per call); narrow \`glob\` to leave it out.`);
195
195
  break;
196
196
  }
197
197
  const outcome = success(data(files));
198
- return fitsResult(outcome, render) ? outcome : failure("output_too_large", "The notes snapshot metadata exceeds the response budget.");
198
+ return fitsResult(outcome, render) ? outcome : failure("output_too_large", `The list of unreachable homes is itself over ${TOOL_OUTPUT_LIMIT}; narrow \`glob\` to fewer homes.`);
199
199
  }
200
200
 
201
201
  function listedFile(row: Pick<NoteRow, "address" | "scope" | "meta">, identity: NotesIdentity): NotesListData["files"][number] {
@@ -210,7 +210,7 @@ function renderWriteData(data: NotesWriteData): string {
210
210
 
211
211
  export const notesWrite = {
212
212
  name: "notes_write", label: "Notes write",
213
- description: `Create or replace a note's body, keeping metadata; uncrumples a crumpled note. Name it for what it holds and when to use it. Store what git and history can't recover. ${ADDRESS_DESCRIPTION}`,
213
+ description: `Create or replace a note. Name it for what it holds and when to use it. Store what git and history can't recover. ${ADDRESS_DESCRIPTION}`,
214
214
  parameters: notesWriteParameters, outputSchema: resultSchema(NotesWriteDataSchema), executionMode: "sequential",
215
215
  async execute(params: Static<typeof notesWriteParameters>, identity: NotesIdentity): Promise<Outcome<NotesWriteData>> {
216
216
  try {
@@ -246,18 +246,20 @@ function renderUpdate(result: Outcome<NotesUpdateData>): string {
246
246
  /** A shortened diff still says so in the receipt's own text, on both surfaces. */
247
247
  export const DIFF_SHORTENED_MARKER = "[diff truncated; update applied]";
248
248
 
249
+ const RECEIPT_TOO_LONG = `Saved, but the receipt is too long to hand back (${TOOL_OUTPUT_LIMIT} per call). Check the note with \`notes_read\`.`;
250
+
249
251
  /** Bound a mutation receipt on both surfaces; a huge diff is cut to the longest fitting prefix and marked. */
250
252
  function boundedUpdate(data: NotesUpdateData): Outcome<NotesUpdateData> {
251
253
  if (fitsResult(success(data), renderUpdate)) return success(data);
252
- if (data.diff === "") return failure("output_too_large", "The update was applied, but its receipt metadata exceeds the response budget.");
254
+ if (data.diff === "") return failure("output_too_large", RECEIPT_TOO_LONG);
253
255
  const diff = prefixFit(data.diff, (candidate) => fitsResult(success({ ...data, diff: `${candidate}\n${DIFF_SHORTENED_MARKER}` }), renderUpdate));
254
256
  const result = success({ ...data, diff: `${diff}\n${DIFF_SHORTENED_MARKER}` });
255
- return fitsResult(result, renderUpdate) ? result : failure("output_too_large", "The update was applied, but its receipt metadata exceeds the response budget.");
257
+ return fitsResult(result, renderUpdate) ? result : failure("output_too_large", RECEIPT_TOO_LONG);
256
258
  }
257
259
 
258
260
  export const notesUpdate = {
259
261
  name: "notes_update", label: "Notes update",
260
- description: `Edit a note's body, change its origin or crumpled state, or move it. Each \`oldText\` must match exactly once unless \`replace_all\` is true; frontmatter can't be edited. Returns a diff. ${ADDRESS_DESCRIPTION}`,
262
+ description: `Edit a note, change its origin or crumpled state, or move it. Each \`oldText\` must match exactly once unless \`replace_all\` is true. ${ADDRESS_DESCRIPTION}`,
261
263
  parameters: notesUpdateParameters, outputSchema: resultSchema(NotesUpdateDataSchema), executionMode: "sequential",
262
264
  async execute(params: Static<typeof notesUpdateParameters>, identity: NotesIdentity, renderDiff: (change: NoteChange) => string): Promise<Outcome<NotesUpdateData>> {
263
265
  try {
@@ -288,15 +290,15 @@ function renderReadData(data: NotesReadData): string {
288
290
 
289
291
  export const notesRead = {
290
292
  name: "notes_read", label: "Notes read",
291
- description: `Read a note's metadata and body, without frontmatter. ${ADDRESS_DESCRIPTION}`,
293
+ description: `Read a note. ${ADDRESS_DESCRIPTION}`,
292
294
  parameters: notesReadParameters, outputSchema: resultSchema(NotesReadDataSchema),
293
295
  async execute(params: Static<typeof notesReadParameters>, identity: NotesIdentity): Promise<Outcome<NotesReadData>> {
294
296
  try {
295
297
  const note = await createNotesStore(identity).read(params.address);
296
- if (!note) return failure("not_found", "note not found");
297
- const totalChars = Array.from(note.body).length;
298
- if (params.offset_chars !== undefined && params.offset_chars > totalChars) return failure("invalid_offset", `offset_chars ${params.offset_chars} is past the end of the body: the note has ${totalChars} body chars; the largest legal offset is ${totalChars} (an empty end-read)`);
299
298
  const resolved = noteIdentity(identity, params.address);
299
+ if (!note) return failure("not_found", `No note at ${resolved.address}.`);
300
+ const totalChars = Array.from(note.body).length;
301
+ if (params.offset_chars !== undefined && params.offset_chars > totalChars) return failure("invalid_offset", `This note ends at char ${totalChars}; offset_chars ${params.offset_chars} is past the last word.`);
300
302
  const metadata = noteMetadata(note.meta);
301
303
  return readTextWindow(note.body, params.offset_chars, params.max_chars, (window) => ({ ...resolved, metadata, ...window }), (result) => renderOutcome(result, renderReadData));
302
304
  } catch (error) { return failureOf(error); }
@@ -2,6 +2,8 @@
2
2
  export { earliestMatchOffsetChars } from "../text-match.js";
3
3
 
4
4
  export const TOOL_OUTPUT_MAX_BYTES = 32 * 1024;
5
+ /** The per-call limit as the model reads it in refusals. */
6
+ export const TOOL_OUTPUT_LIMIT = `${TOOL_OUTPUT_MAX_BYTES / 1024} KB`;
5
7
  export const DEFAULT_READ_WINDOW_CHARS = 12000;
6
8
  export const MAX_READ_WINDOW_CHARS = 50000;
7
9
  export const HISTORY_PREVIEW_CHARS = 1200;
@@ -1,6 +1,6 @@
1
1
  /** Internal outcomes, compact wire results, and bounded read windows. */
2
2
  import { Type, type Static, type TSchema } from "typebox";
3
- import { DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS, TOOL_OUTPUT_MAX_BYTES, withinTextBudget } from "./output.js";
3
+ import { DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS, TOOL_OUTPUT_LIMIT, TOOL_OUTPUT_MAX_BYTES, withinTextBudget } from "./output.js";
4
4
 
5
5
  /** The one refusal shape: `code` is a stable internal identifier, `message` the bounded reading. */
6
6
  export const OperationErrorSchema = Type.Object({
@@ -116,7 +116,7 @@ export function readTextWindow<T>(
116
116
  if (fits(mid)) low = mid;
117
117
  else high = mid - 1;
118
118
  }
119
- if (low === 0) return failure("output_too_large", "The response metadata leaves no room for a progressing read window.");
119
+ if (low === 0) return failure("output_too_large", `Its header alone fills the ${TOOL_OUTPUT_LIMIT} a call can carry; no room left for the text.`);
120
120
  return success(makeData(window(low)));
121
121
  }
122
122
 
@@ -24,8 +24,8 @@ export class InvalidQueryError extends Error {
24
24
  */
25
25
  export function searchQueries(query: unknown): string[] {
26
26
  const candidates = typeof query === "string" ? [query] : query;
27
- if (!Array.isArray(candidates) || candidates.length === 0) throw new InvalidQueryError("query must be a string or a non-empty array of strings");
28
- if (!candidates.every((candidate) => typeof candidate === "string")) throw new InvalidQueryError("query array elements must be strings");
29
- if (candidates.some((candidate) => candidate === "")) throw new InvalidQueryError("query strings must be non-empty: an empty query matches everything");
27
+ if (!Array.isArray(candidates) || candidates.length === 0) throw new InvalidQueryError("`query` must be a string or a non-empty list of strings.");
28
+ if (!candidates.every((candidate) => typeof candidate === "string")) throw new InvalidQueryError("Every item in the `query` list must be a string.");
29
+ if (candidates.some((candidate) => candidate === "")) throw new InvalidQueryError("`query` can't be an empty string; it would match everything.");
30
30
  return candidates as string[];
31
31
  }