notesoup 0.51.2 → 0.51.4

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.
@@ -3,7 +3,7 @@ import { notesWrite, notesUpdate, notesRead, notesList, notesSearch } from "../.
3
3
  import { notesIdentityFromPi } from "./adapter.js";
4
4
  import { NOTES_NAMESPACE, registerOperation } from "../tool-result.js";
5
5
  function renderDiff(change) {
6
- return change.kind === "none" ? "" : generateDiffString(change.before, change.after).diff;
6
+ return change.kind === "none" ? "" : generateDiffString(change.before, change.after, 1).diff;
7
7
  }
8
8
  /** Register the five shared note operations; each one supplies Pi's session identity and nothing else. */
9
9
  export function registerNotesTools(pi) {
@@ -16,12 +16,6 @@ type ResetOptions = {
16
16
  };
17
17
  export type ResetRequest = {
18
18
  readonly phase: "none";
19
- } | {
20
- readonly phase: "stop-pending";
21
- readonly windowId: string;
22
- readonly tool?: {
23
- readonly lastWord?: string;
24
- };
25
19
  } | {
26
20
  readonly phase: "pending";
27
21
  readonly windowId: string;
@@ -36,6 +30,8 @@ export type ResetRequest = {
36
30
  export type ResetOverflowPhase = "idle" | "pending" | "pending-spent" | "spent";
37
31
  export interface ResetControlState {
38
32
  readonly request: ResetRequest;
33
+ /** Await settlement after a safety reset; retain its intent to retry if it never committed. */
34
+ readonly stopAfterReset?: ResetBoundaryIntent;
39
35
  readonly overflow: ResetOverflowPhase;
40
36
  }
41
37
  export declare function initialResetControl(): ResetControlState;
@@ -79,15 +75,34 @@ export type ResetControlEvent = {
79
75
  readonly type: "before_settle";
80
76
  readonly facts: ResetBeforeSettleFacts;
81
77
  };
82
- export type ResetControlEffect = "none" | "close-out-armed" | "already-pending" | "commit-boundary" | "commit-boundary-stop" | "stop" | "recover-overflow";
83
- export interface ResetControlResult {
84
- readonly state: ResetControlState;
85
- readonly effect: ResetControlEffect;
86
- readonly lastWord?: string;
87
- /** The origin of the boundary this decision commits. Absent means no `empty_brain` call was recorded, so forced. */
88
- readonly origin?: ResetOrigin;
78
+ /** Construction intent, not final state: handled inputs can still arrive while notes load. Only `turn-end`
79
+ * may latch a stop; overflow kinds spend recovery; only `overflow-recovery` carries passive run inputs. */
80
+ export interface ResetBoundaryIntent {
81
+ readonly effect: "build-boundary";
82
+ readonly boundary: "turn-end" | "settle" | "command-overflow" | "overflow-recovery";
83
+ readonly windowId: string;
84
+ readonly continueAfterReset: boolean;
85
+ readonly origin: ResetOrigin;
86
+ readonly tool?: {
87
+ readonly lastWord?: string;
88
+ };
89
89
  }
90
+ export type ResetControlResult = ResetBoundaryIntent | {
91
+ readonly state: ResetControlState;
92
+ readonly effect: "none" | "close-out-armed" | "already-pending" | "stop";
93
+ };
94
+ /** Resolve once, after a successful current build. No delivery policy is left to the adapter. */
95
+ export declare function finalizeResetBoundary(intent: ResetBoundaryIntent, request: ResetRequest, passiveInputs?: readonly CarriedInput[]): {
96
+ state: ResetControlState;
97
+ inputs: readonly (string | (import("@earendil-works/pi-ai").TextContent | import("@earendil-works/pi-ai").ImageContent)[])[];
98
+ resumeModel: boolean;
99
+ };
90
100
  /** Pure transitions: command policy schedules a reset without disguising actual tool calls. */
101
+ export declare function reduceResetControl(state: ResetControlState, event: Extract<ResetControlEvent, {
102
+ type: "input" | "command" | "tool_request";
103
+ }>): Extract<ResetControlResult, {
104
+ state: ResetControlState;
105
+ }>;
91
106
  export declare function reduceResetControl(state: ResetControlState, event: ResetControlEvent): ResetControlResult;
92
107
  /**
93
108
  * Tool-only requests commit at turn_end; command policy waits for settlement.
@@ -95,7 +110,7 @@ export declare function reduceResetControl(state: ResetControlState, event: Rese
95
110
  * Budget warnings carry no lifecycle state. Overflow recovery remains bounded.
96
111
  */
97
112
  export declare function registerResetLifecycle(pi: ExtensionAPI, options: ResetOptions): {
98
- command(windowId: string, continueAfterReset: boolean, input?: CarriedInput): ResetControlEffect;
113
+ command(windowId: string, continueAfterReset: boolean, input?: CarriedInput): "none" | "stop" | "close-out-armed" | "already-pending";
99
114
  request(windowId: string, lastWord?: string): "rollover_already_pending" | "rollover_requested";
100
115
  clear: (preserveInputs?: boolean) => void;
101
116
  };
@@ -21,25 +21,26 @@ export function initialResetControl() {
21
21
  return { request: NO_REQUEST, overflow: "idle" };
22
22
  }
23
23
  function requestForWindow(request, windowId) {
24
- return request.phase === "stop-pending" || (request.phase !== "none" && request.windowId === windowId) ? request : NO_REQUEST;
24
+ return request.phase === "pending" && request.windowId === windowId ? request : NO_REQUEST;
25
25
  }
26
- function commandContinues(request) {
27
- return request.command?.continueAfterReset === true || (request.command?.inputs?.length ?? 0) > 0;
26
+ const spendsOverflow = (boundary) => boundary === "command-overflow" || boundary === "overflow-recovery";
27
+ function boundaryIntent(request, windowId, boundary) {
28
+ const tool = request.phase === "none" ? undefined : request.tool;
29
+ const continueAfterReset = request.phase === "pending" ? request.command?.continueAfterReset ?? true : true;
30
+ return { effect: "build-boundary", boundary, windowId, tool, continueAfterReset, origin: spendsOverflow(boundary) || !tool ? "forced" : "normal" };
28
31
  }
29
- function commandCommit(request, overflow = false) {
30
- return {
31
- state: { request: NO_REQUEST, overflow: overflow ? "spent" : "idle" },
32
- effect: commandContinues(request) ? "commit-boundary" : "commit-boundary-stop",
33
- ...requestDetails(request),
34
- ...(overflow ? { origin: "forced" } : {}),
32
+ /** Resolve once, after a successful current build. No delivery policy is left to the adapter. */
33
+ export function finalizeResetBoundary(intent, request, passiveInputs = []) {
34
+ const inputs = request.phase === "pending" && request.command ? request.command.inputs ?? []
35
+ : intent.boundary === "overflow-recovery" ? passiveInputs : [];
36
+ const resumeModel = intent.continueAfterReset || inputs.length > 0;
37
+ const state = {
38
+ request: NO_REQUEST,
39
+ ...(intent.boundary === "turn-end" && !resumeModel ? { stopAfterReset: intent } : {}),
40
+ overflow: spendsOverflow(intent.boundary) ? "spent" : "idle",
35
41
  };
42
+ return { state, inputs, resumeModel };
36
43
  }
37
- /** The tool request itself is the origin fact; the stop latch retains it for construction retry. */
38
- function requestDetails(request) {
39
- const tool = request.phase === "none" ? undefined : request.tool;
40
- return { origin: tool ? "normal" : "forced", ...tool };
41
- }
42
- /** Pure transitions: command policy schedules a reset without disguising actual tool calls. */
43
44
  export function reduceResetControl(state, event) {
44
45
  switch (event.type) {
45
46
  case "input": {
@@ -50,9 +51,9 @@ export function reduceResetControl(state, event) {
50
51
  }
51
52
  case "command":
52
53
  case "tool_request": {
53
- const current = requestForWindow(state.request, event.windowId);
54
- if (current.phase === "stop-pending")
54
+ if (state.stopAfterReset)
55
55
  return { state, effect: "already-pending" };
56
+ const current = requestForWindow(state.request, event.windowId);
56
57
  const request = current.phase === "pending" ? current : { phase: "pending", windowId: event.windowId };
57
58
  if (event.type === "command") {
58
59
  if (request.command)
@@ -70,8 +71,8 @@ export function reduceResetControl(state, event) {
70
71
  const request = requestForWindow(state.request, facts.windowId);
71
72
  if (facts.aborted)
72
73
  return { state: initialResetControl(), effect: "none" };
73
- if (request.phase === "stop-pending")
74
- return { state: { request, overflow: "idle" }, effect: "none" };
74
+ if (state.stopAfterReset)
75
+ return { state: { ...state, overflow: "idle" }, effect: "none" };
75
76
  const manual = request.phase === "pending" && request.command !== undefined;
76
77
  if (facts.overflow) {
77
78
  const pending = manual ? facts.enabled : !facts.queued && facts.enabled && facts.automaticResetEnabled;
@@ -89,11 +90,7 @@ export function reduceResetControl(state, event) {
89
90
  // A direct tool request commits after the whole tool batch. The budget cutoff is
90
91
  // a separate hard-reserve safety path; ordinary close-out waits for settlement.
91
92
  if ((request.phase === "pending" && request.tool && !manual) || facts.hardReserveDue) {
92
- if (manual && commandContinues(request))
93
- return commandCommit(request);
94
- if (manual)
95
- return { state: { request: { phase: "stop-pending", windowId: facts.windowId, ...(request.tool ? { tool: request.tool } : {}) }, overflow: "idle" }, effect: "commit-boundary-stop", ...requestDetails(request) };
96
- return { state: { request: NO_REQUEST, overflow: "idle" }, effect: "commit-boundary", ...requestDetails(request) };
93
+ return boundaryIntent(request, facts.windowId, "turn-end");
97
94
  }
98
95
  return { state: { request, overflow: "idle" }, effect: "none" };
99
96
  }
@@ -102,14 +99,15 @@ export function reduceResetControl(state, event) {
102
99
  if (facts.aborted)
103
100
  return { state: initialResetControl(), effect: "none" };
104
101
  const request = requestForWindow(state.request, facts.windowId);
105
- if (request.phase === "stop-pending") {
102
+ if (state.stopAfterReset) {
106
103
  if (facts.queued)
107
104
  return { state, effect: "none" };
108
- // Only retry construction if the safety boundary never committed. Otherwise
109
- // stop without a second wipe, even if a queued turn subsequently failed.
110
- if (request.windowId !== facts.windowId)
105
+ // A changed window confirms commit: stop without wiping twice. Otherwise retry.
106
+ if (state.stopAfterReset.windowId !== facts.windowId)
111
107
  return { state: initialResetControl(), effect: "stop" };
112
- return { state: initialResetControl(), effect: facts.failed || !facts.enabled ? "none" : "commit-boundary-stop", ...requestDetails(request) };
108
+ if (facts.failed || !facts.enabled)
109
+ return { state: initialResetControl(), effect: "none" };
110
+ return { ...state.stopAfterReset, boundary: "settle" };
113
111
  }
114
112
  const manual = request.phase === "pending" && request.command !== undefined;
115
113
  if (state.overflow === "pending" || state.overflow === "pending-spent") {
@@ -119,19 +117,21 @@ export function reduceResetControl(state, event) {
119
117
  // An explicit manual wipe is not an automatic recovery/retry. It must still
120
118
  // clear on overflow, including when automatic reset is disabled.
121
119
  if (manual && facts.enabled)
122
- return commandCommit(request, true);
120
+ return boundaryIntent(request, facts.windowId, "command-overflow");
123
121
  const spent = state.overflow === "pending-spent";
124
122
  if (!facts.enabled || !facts.automaticResetEnabled) {
125
123
  return { state: { request: facts.failed ? NO_REQUEST : request, overflow: spent ? "spent" : "idle" }, effect: "none" };
126
124
  }
127
- return { state: { request: NO_REQUEST, overflow: "spent" }, effect: spent ? "none" : "recover-overflow", ...requestDetails(request) };
125
+ if (spent)
126
+ return { state: { request: NO_REQUEST, overflow: "spent" }, effect: "none" };
127
+ return boundaryIntent(request, facts.windowId, "overflow-recovery");
128
128
  }
129
129
  if (facts.failed || !facts.enabled || !manual) {
130
130
  return { state: { ...state, request: NO_REQUEST }, effect: "none" };
131
131
  }
132
132
  if (facts.queued)
133
133
  return { state: { ...state, request }, effect: "none" };
134
- return commandCommit(request);
134
+ return boundaryIntent(request, facts.windowId, "settle");
135
135
  }
136
136
  }
137
137
  }
@@ -160,17 +160,16 @@ export function registerResetLifecycle(pi, options) {
160
160
  runInputs.push(...inputs);
161
161
  };
162
162
  const clear = (preserveInputs = true) => {
163
+ lifecycleGeneration++;
163
164
  if (preserveInputs)
164
165
  restoreCommandInputs();
165
166
  control = initialResetControl();
166
167
  runInputs = [];
167
168
  commandId = undefined;
168
169
  };
169
- const resetBoundaryResult = async (entries, ctx, decision) => {
170
+ const resetBoundaryResult = async (entries, ctx, intent) => {
170
171
  // Keep command state live while notes load: input arriving during this await must
171
172
  // join this very boundary even if its steer cannot reach the closing model.
172
- const passiveInputs = decision.effect === "recover-overflow" ? runInputs : [];
173
- const origin = decision.origin ?? "forced";
174
173
  const generation = lifecycleGeneration;
175
174
  const outerGeneration = options.getLifecycleGeneration?.();
176
175
  const sessionId = ctx.sessionManager.getSessionId();
@@ -182,7 +181,7 @@ export function registerResetLifecycle(pi, options) {
182
181
  const firstSeq = nextHistorySeq(ctx, entries) + 1;
183
182
  let resetDrafts;
184
183
  try {
185
- resetDrafts = await options.buildReset(ctx, isCurrent, decision.lastWord, firstSeq, origin);
184
+ resetDrafts = await options.buildReset(ctx, isCurrent, intent.tool?.lastWord, firstSeq, intent.origin);
186
185
  }
187
186
  catch (error) {
188
187
  if (isCurrent())
@@ -191,14 +190,11 @@ export function registerResetLifecycle(pi, options) {
191
190
  }
192
191
  if (!isCurrent())
193
192
  return entries.length > 0 ? { entries } : undefined;
194
- const request = control.request;
195
- const inputs = request.phase === "pending" && request.command ? request.command.inputs ?? [] : passiveInputs;
196
- control = decision.state;
197
- if (inputs.length > 0 && control.request.phase === "stop-pending")
198
- control = { ...control, request: NO_REQUEST };
193
+ const finalized = finalizeResetBoundary(intent, control.request, runInputs);
194
+ control = finalized.state;
199
195
  runInputs = [];
200
196
  options.onResetReady?.(ctx, resetDrafts);
201
- return { entries: [...entries, ...resetDrafts, ...carriedInputDrafts(inputs)], continue: inputs.length > 0 || decision.effect !== "commit-boundary-stop" };
197
+ return { entries: [...entries, ...resetDrafts, ...carriedInputDrafts(finalized.inputs)], continue: finalized.resumeModel };
202
198
  };
203
199
  // Pi may start multiple low-level loops while draining follow-ups/recovering.
204
200
  // Keep the logical run's inputs until settlement or a successful boundary.
@@ -237,10 +233,11 @@ export function registerResetLifecycle(pi, options) {
237
233
  request.windowId === currentWindowId(ctx) && details?.windowId === request.windowId && commandId !== undefined && details?.commandId === commandId;
238
234
  });
239
235
  const filtered = messages.length === event.messages.length ? undefined : { messages };
240
- if (!sessionActive || request.phase !== "stop-pending" || ctx.hasPendingMessages())
236
+ const stop = control.stopAfterReset;
237
+ if (!sessionActive || !stop || ctx.hasPendingMessages())
241
238
  return filtered;
242
239
  const marker = currentReset(ctx);
243
- if (!marker || marker.data.windowId === request.windowId || !resetBoundaryCommitted(ctx, marker.id, marker.data.windowId))
240
+ if (!marker || marker.data.windowId === stop.windowId || !resetBoundaryCommitted(ctx, marker.id, marker.data.windowId))
244
241
  return filtered;
245
242
  const branch = ctx.sessionManager.getBranch();
246
243
  const markerIndex = branch.findIndex((entry) => entry.id === marker.id);
@@ -270,7 +267,7 @@ export function registerResetLifecycle(pi, options) {
270
267
  get hardReserveDue() { return options.budget.hardReserveDue(ctx); },
271
268
  },
272
269
  });
273
- if (decision.effect === "commit-boundary" || decision.effect === "commit-boundary-stop")
270
+ if (decision.effect === "build-boundary")
274
271
  return await resetBoundaryResult(entries, ctx, decision);
275
272
  if (aborted || (event.outcome === "error" && decision.state.request.phase === "none"))
276
273
  restoreCommandInputs();
@@ -291,7 +288,7 @@ export function registerResetLifecycle(pi, options) {
291
288
  failed: event.outcome === "error",
292
289
  },
293
290
  });
294
- if (decision.effect === "commit-boundary" || decision.effect === "commit-boundary-stop" || decision.effect === "recover-overflow")
291
+ if (decision.effect === "build-boundary")
295
292
  return await resetBoundaryResult(event.entries, ctx, decision);
296
293
  if (decision.state.request.phase === "none")
297
294
  restoreCommandInputs();
@@ -321,9 +318,9 @@ export function registerResetLifecycle(pi, options) {
321
318
  pi.on("agent_settled", () => {
322
319
  clear();
323
320
  });
324
- pi.on("session_start", () => { lifecycleGeneration++; clear(false); sessionActive = true; });
325
- pi.on("session_tree", () => { lifecycleGeneration++; clear(false); });
326
- pi.on("session_shutdown", () => { lifecycleGeneration++; clear(false); options.budget.clear(); sessionActive = false; });
321
+ pi.on("session_start", () => { clear(false); sessionActive = true; });
322
+ pi.on("session_tree", () => { clear(false); });
323
+ pi.on("session_shutdown", () => { clear(false); options.budget.clear(); sessionActive = false; });
327
324
  return {
328
325
  command(windowId, continueAfterReset, input) {
329
326
  const decision = reduceResetControl(control, { type: "command", windowId, continueAfterReset, ...(input !== undefined ? { input } : {}) });