pi-optchat 0.7.1 → 0.8.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.
package/src/inspector.ts CHANGED
@@ -60,6 +60,8 @@ export class Inspector implements Component, Focusable {
60
60
  private lineCount = 0;
61
61
  private hintLines = 1;
62
62
  private range: UsageRange = 'This session';
63
+ private confirmDelete?: string;
64
+ private notice = '';
63
65
  private ended = false;
64
66
  private readonly unsubscribe: (() => void)[];
65
67
  private readonly timer: ReturnType<typeof setInterval>;
@@ -87,6 +89,7 @@ export class Inspector implements Component, Focusable {
87
89
  handleInput(data: string) {
88
90
  if (this.ended) return;
89
91
  if (matchesKey(data, 'escape') || matchesKey(data, 'ctrl+c')) return this.finish();
92
+ const deleting = this.confirmDelete; this.confirmDelete = undefined; this.notice = '';
90
93
  if (matchesKey(data, 'tab')) {
91
94
  this.page = nextPage(this.page); this.scroll = 0;
92
95
  } else if (this.page === 'activity') this.scrollInput(data);
@@ -103,10 +106,22 @@ export class Inspector implements Component, Focusable {
103
106
  else if (matchesKey(data, 'home')) this.select(-Infinity);
104
107
  else if (matchesKey(data, 'end')) this.select(Infinity);
105
108
  else if (data === 'm') return this.finish('model');
109
+ else if ((data === 'd' || matchesKey(data, 'delete')) && this.selected) {
110
+ if (deleting === this.selected) this.remove(this.selected);
111
+ else this.confirmDelete = this.selected;
112
+ }
106
113
  else if (matchesKey(data, 'return') && this.selected) return this.finish({ open: this.selected });
107
114
  }
108
115
  this.options.redraw();
109
116
  }
117
+ /** Once the run is gone, the cursor moves to the next row that is not, else the one above. */
118
+ private remove(id: string) {
119
+ const { children } = this.options, list = children.history.list(), doomed = new Set([id, ...children.history.descendants(id).map(r => r.id)]);
120
+ const index = list.findIndex(r => r.id === id), next = list.slice(index).find(r => !doomed.has(r.id)) ?? list.slice(0, index).findLast(r => !doomed.has(r.id));
121
+ children.remove(id).then(() => { if (!children.history.records.has(this.selected ?? '')) this.selected = next?.id; },
122
+ (error: unknown) => { this.notice = `Not deleted: ${error instanceof Error ? error.message : String(error)}`; })
123
+ .finally(() => { if (!this.ended) this.options.redraw(); });
124
+ }
110
125
  private scrollInput(data: string) {
111
126
  const delta = matchesKey(data, 'up') ? -1 : matchesKey(data, 'down') ? 1 : matchesKey(data, 'pageUp') ? -this.height : matchesKey(data, 'pageDown') ? this.height : 0;
112
127
  if (delta || matchesKey(data, 'home') || matchesKey(data, 'end')) {
@@ -191,10 +206,12 @@ export class Inspector implements Component, Focusable {
191
206
  });
192
207
  if (!body.length) body.push(color('muted', 'No agents yet. Ask the main agent to delegate a task.'));
193
208
  info = `${list.filter(isActiveRun).length} active · ${list.length} saved${list.length ? ` · ${cursor + 1}/${list.length}` : ''}`;
194
- hint = '↑↓ select · Enter open · m model · Tab usage · Esc close';
209
+ hint = this.confirmDelete ? `Press d again to ${children.live(this.confirmDelete) ? 'stop and delete' : 'delete'} this agent and the agents it started`
210
+ : this.notice || '↑↓ select · Enter open · m model · d delete · Tab usage · Esc close';
195
211
  }
196
212
  title = fit(title, Math.max(1, inner - visibleWidth(info) - 2));
197
- const footer = wrapTextWithAnsi(hint, inner).map(line => color('dim', line));
213
+ const tone = this.page !== 'agents' ? 'dim' : this.confirmDelete ? 'warning' : this.notice ? 'error' : 'dim';
214
+ const footer = wrapTextWithAnsi(hint, inner).map(line => color(tone, line));
198
215
  this.hintLines = footer.length;
199
216
  const rule = color('border', '─'.repeat(Math.max(1, width)));
200
217
  // Same layout as Pi's own selectors: rules above and below, content indented by one column.
package/src/memory.ts CHANGED
@@ -1,19 +1,25 @@
1
- import { closeSync, existsSync, fstatSync, fsyncSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, statSync, writeSync } from 'node:fs';
2
- import { dirname, join } from 'node:path';
1
+ import { closeSync, existsSync, fstatSync, fsyncSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, statSync, writeFileSync, writeSync } from 'node:fs';
2
+ import { dirname, join, resolve } from 'node:path';
3
3
  import { EventEmitter } from 'node:events';
4
4
 
5
5
  export const NODE = 512;
6
6
  export const VIEW = 128_000;
7
7
  export const CAP = 30_000;
8
- export type Kind = 'user' | 'talk' | 'tool' | 'echo' | 'note';
9
- export interface Origin { source: 'claude' | 'claude-memory' | 'codex' | 'chatgpt'; conversation: string; message: string; title: string; project?: string }
8
+ /** The most characters one zoom into a message returns, so a page stays under the tool output CAP instead of losing its middle. */
9
+ export const PAGE = 25_000;
10
+ export const KINDS = ['user', 'talk', 'tool', 'echo', 'note', 'work'] as const;
11
+ export type Kind = typeof KINDS[number];
12
+ export interface Origin { source: 'claude' | 'claude-memory' | 'codex' | 'pi' | 'chatgpt'; conversation: string; message: string; title: string; project?: string }
10
13
  export interface Entry { i: number; kind: Kind; text: string; size: number; date: string; receipt?: string; origin?: Origin }
11
14
  export interface Part { l: number; i: number }
12
15
  export interface Summary extends Part { text: string; size: number }
13
- export interface Compression { context: string; source: string; merge: boolean; historical?: boolean }
16
+ /** `part` is the node to build: a message's leaf, or the merge of its two halves. */
17
+ export interface Compression { context: string; source: string; part: Part; historical?: boolean }
14
18
  export type Compressor = (input: Compression, signal: AbortSignal) => Promise<string>;
15
19
  const key = ({ l, i }: Part) => l * 2 ** 40 + i;
16
20
  const UNBUILT = '(not summarized yet: zoom it)';
21
+ /** A message's node starts once fewer than this many lines before it are unbuilt (recipe §4). */
22
+ const AHEAD = 8;
17
23
  export const start = ({ l, i }: Part) => i * 2 ** l;
18
24
  export const end = (part: Part) => start(part) + 2 ** part.l;
19
25
  export const bytes = (s: string) => Buffer.byteLength(s, 'utf8');
@@ -33,6 +39,63 @@ export function cap(text: string, limit = CAP) {
33
39
  export function localDay(date = new Date()) {
34
40
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
35
41
  }
42
+ export function atomicWrite(file: string, text: string | Uint8Array) {
43
+ mkdirSync(resolve(file, '..'), { recursive: true, mode: 0o700 });
44
+ const temporary = `${file}.${process.pid}.tmp`;
45
+ // One read-write handle: Windows refuses fsync on a read-only one, and it cannot open a directory at all, so there the file flush is the whole guarantee.
46
+ const fd = openSync(temporary, 'w+', 0o600);
47
+ try { writeFileSync(fd, text); fsyncSync(fd); } finally { closeSync(fd); }
48
+ renameSync(temporary, file);
49
+ if (process.platform === 'win32') return;
50
+ const parent = openSync(resolve(file, '..'), 'r');
51
+ try { fsyncSync(parent); } finally { closeSync(parent); }
52
+ }
53
+ /** The index of the view's most due pair of sibling lines whose parent is built, or -1 (recipe §3.2). A pair is as due as
54
+ * long ago it ended, in its own line size; equal pairs go oldest first. This is the order of Taelin's rollback push. */
55
+ export function mostDue(view: readonly Part[], total: number, built: (part: Part) => boolean) {
56
+ let best = -1, due = -Infinity;
57
+ for (let j = 0; j + 1 < view.length; j++) {
58
+ const a = view[j], b = view[j + 1];
59
+ if (a.l !== b.l || a.i % 2 || b.i !== a.i + 1) continue;
60
+ const age = (total - (end(b) - 1)) / 2 ** a.l;
61
+ if (age > due && built({ l: a.l + 1, i: a.i / 2 })) { best = j; due = age; }
62
+ }
63
+ return best;
64
+ }
65
+ /** A view (recipe §3.2): lines that tile the log, oldest first. It grows a line per message and, once over `high` bytes or when
66
+ * forced, merges its most due pairs in one batch down to `low`, merging what it can at each change until then. */
67
+ class Sawtooth {
68
+ readonly parts: Part[] = [];
69
+ private measured = 0;
70
+ private merging = false;
71
+ constructor(private readonly high: number, private readonly low: number, private readonly node: (part: Part) => Summary | undefined) {}
72
+ private lineSize(part: Part) { return this.node(part)?.size ?? UNBUILT_BYTES; }
73
+ get size() { return this.measured; }
74
+ push(part: Part) { this.parts.push(part); this.measured += this.lineSize(part); }
75
+ /** The line covering message `at`. The lines tile the log in order, so a binary search finds it. */
76
+ covering(at: number): Part | undefined {
77
+ for (let lo = 0, hi = this.parts.length - 1; lo <= hi;) {
78
+ const mid = (lo + hi) >> 1, p = this.parts[mid];
79
+ if (end(p) <= at) lo = mid + 1;
80
+ else if (start(p) > at) hi = mid - 1;
81
+ else return p;
82
+ }
83
+ }
84
+ /** A message got its node. Its line is usually still shown, but after a damaged tree file a saved parent can already hide it. */
85
+ built(i: number) { if (this.covering(i)?.l === 0) this.measured += this.lineSize({ l: 0, i }) - UNBUILT_BYTES; }
86
+ /** Returns whether any lines merged. */
87
+ fit(total: number, force = false) {
88
+ const lines = this.parts.length;
89
+ if (force || this.measured > this.high) this.merging = true;
90
+ for (let best: number; this.merging && this.measured > this.low && (best = mostDue(this.parts, total, part => !!this.node(part))) >= 0;) {
91
+ const a = this.parts[best], b = this.parts[best + 1], parent = { l: a.l + 1, i: a.i / 2 };
92
+ this.measured += this.lineSize(parent) - this.lineSize(a) - this.lineSize(b);
93
+ this.parts.splice(best, 2, parent);
94
+ }
95
+ if (this.measured <= this.low) this.merging = false;
96
+ return this.parts.length < lines;
97
+ }
98
+ }
36
99
  const otherWriter = (file: string) => new Error(`Another process wrote ${file}. Close every other Pi on this profile and restart Pi; nothing was written.`);
37
100
  /** With `size`, refuses to append unless the file still has that size. A last line left without its newline is ended first. Returns the new size. */
38
101
  export function appendJson(file: string, value: unknown, size?: number) {
@@ -65,7 +128,7 @@ function object(value: unknown): value is Record<string, unknown> {
65
128
  }
66
129
  export function isEntry(value: unknown): value is Entry {
67
130
  return object(value) && Number.isSafeInteger(value.i) && typeof value.kind === 'string'
68
- && ['user', 'talk', 'tool', 'echo', 'note'].includes(value.kind)
131
+ && KINDS.some(kind => kind === value.kind)
69
132
  && typeof value.text === 'string' && typeof value.date === 'string';
70
133
  }
71
134
  function isSummary(value: unknown): value is Summary {
@@ -73,31 +136,36 @@ function isSummary(value: unknown): value is Summary {
73
136
  && typeof value.text === 'string';
74
137
  }
75
138
 
76
- /** The log is authoritative. The tree and monotonically coarsening view follow recipe §§2–6. */
139
+ /** The log is authoritative. The tree and the view follow the recipe: the view grows a line per message and, once it passes
140
+ * `budget`, merges its most due pairs in one batch down to half of it, so between batches it only grows at its end.
141
+ * Compactions get their own view (recipe §4): the chat's merged further, to between an eighth and a quarter of the budget,
142
+ * and again whenever the chat's merges, so parallel compactions read it from the cache. */
77
143
  export class Memory {
78
144
  readonly root: Entry[] = [];
79
145
  readonly tree = new Map<number, Summary>();
80
- readonly view: Part[] = [];
81
146
  private readonly events = new EventEmitter();
82
147
  private readonly controller = new AbortController();
83
148
  private readonly busy = new Map<number, Promise<void>>();
84
149
  private readonly retryAt = new Map<number, number>();
85
150
  private readonly reported = new Set<number>();
86
151
  private readonly lastSeenBytes = new Map<string, number>();
87
- private viewBytes = 0;
88
- private leaves = 0;
89
- /** Per level, every node below this index is built. */
90
- private readonly low: number[] = [];
152
+ /** Messages without their node, and merges whose halves are built, oldest first, so the scheduler never scans the tree for work (recipe §4). */
153
+ private readonly unbuilt = new Set<number>();
154
+ private readonly merges = new Map<number, Part>();
91
155
  private retryTimer?: ReturnType<typeof setTimeout>;
92
156
  /** The most summaries owed at once since none were last owed. */
93
157
  private peak = 0;
94
158
  private scheduled = false;
95
159
  private stopped = false;
160
+ private readonly chat: Sawtooth;
161
+ private readonly compaction: Sawtooth;
96
162
  lastError?: string;
97
163
 
98
164
  constructor(readonly directory: string, private readonly compress: Compressor,
99
165
  private readonly warn: (s: string) => void = console.error,
100
166
  readonly budget = VIEW, private readonly jobs = 8, private readonly retryMs = 10_000) {
167
+ this.chat = new Sawtooth(budget, budget / 2, part => this.node(part));
168
+ this.compaction = new Sawtooth(budget / 4, budget / 8, part => this.node(part));
101
169
  for (const sub of ['main', 'tree']) mkdirSync(join(directory, sub), { recursive: true, mode: 0o700 });
102
170
  const main = join(directory, 'main'), log = records(main, warn);
103
171
  for (const name of readdirSync(main).filter(n => n.endsWith('.jsonl'))) this.lastSeenBytes.set(join(main, name), statSync(join(main, name)).size);
@@ -107,12 +175,16 @@ export class Memory {
107
175
  for (const value of records(join(directory, 'tree'), warn)) {
108
176
  if (!isSummary(value) || value.l < 0 || value.i < 0 || end(value) > this.root.length)
109
177
  throw new Error('Invalid OptChat summary record.');
110
- if (value.l === 0 && !this.tree.has(key(value))) this.leaves++;
111
178
  this.tree.set(key(value), { ...value, size: lineBytes(value.text) });
112
179
  }
113
- // Fold history in order; do not retile the entire log on each turn.
114
- for (let i = 0; i < this.root.length; i++) { this.push(i); this.fit(i + 1); }
115
- this.schedule();
180
+ for (const node of this.tree.values()) this.queueParent(node);
181
+ for (let i = 0; i < this.root.length; i++) if (!this.node({ l: 0, i })) this.unbuilt.add(i);
182
+ // Refolding the log gives a different view than the live one, and every cache entry would miss, so the view is saved.
183
+ const loaded = this.load();
184
+ for (const part of this.view) this.compaction.push(part);
185
+ this.compaction.fit(loaded, true);
186
+ for (let i = loaded; i < this.root.length; i++) { this.push(i); this.fit(i + 1); }
187
+ this.fit(); this.save(); this.schedule();
116
188
  }
117
189
  private checkLog(next: string) {
118
190
  const main = dirname(next), names = readdirSync(main).filter(n => n.endsWith('.jsonl'));
@@ -124,18 +196,48 @@ export class Memory {
124
196
  const file = join(this.directory, 'main', `${localDay()}.jsonl`);
125
197
  if (!this.lastSeenBytes.has(file)) this.checkLog(file);
126
198
  this.lastSeenBytes.set(file, appendJson(file, entry, this.lastSeenBytes.get(file) ?? 0));
127
- this.root.push(entry); this.push(entry.i); this.fit(); this.schedule();
199
+ this.root.push(entry); this.push(entry.i); if (this.fit()) this.save(); this.schedule();
128
200
  return entry;
129
201
  }
130
202
  node(part: Part) { return this.tree.get(key(part)); }
131
203
  private text(part: Part) { return this.node(part)?.text ?? UNBUILT; }
132
- private partBytes(part: Part) { return this.node(part)?.size ?? UNBUILT_BYTES; }
133
- private push(i: number) { const part = { l: 0, i }; this.view.push(part); this.viewBytes += this.partBytes(part); }
204
+ /** Takes the saved view if it still tiles the log from the start, and returns how many messages it covers. */
205
+ private load() {
206
+ const file = join(this.directory, 'view.json');
207
+ if (!existsSync(file)) return 0;
208
+ const rebuild = () => { this.warn(`Rebuilt the memory view: ${file} does not match the log.`); return 0; };
209
+ let saved: unknown;
210
+ try { saved = JSON.parse(readFileSync(file, 'utf8')); } catch { return rebuild(); }
211
+ if (!Array.isArray(saved)) return rebuild();
212
+ const parts: Part[] = [];
213
+ let covered = 0;
214
+ for (const value of saved) {
215
+ const [l, i] = Array.isArray(value) ? value : [];
216
+ const part = { l, i };
217
+ if (!Number.isSafeInteger(l) || !Number.isSafeInteger(i) || l < 0 || start(part) !== covered || end(part) > this.root.length || (l > 0 && !this.node(part)))
218
+ return rebuild();
219
+ parts.push(part); covered = end(part);
220
+ }
221
+ for (const part of parts) this.chat.push(part);
222
+ return covered;
223
+ }
224
+ /** Saved when it merges: a view that only grew is the saved one plus a line per later message, as `load` replays it.
225
+ * The log stays authoritative, so a failed save only warns. */
226
+ private save() {
227
+ try { atomicWrite(join(this.directory, 'view.json'), JSON.stringify(this.view.map(p => [p.l, p.i]))); }
228
+ catch (error) { this.warn(`Could not save the memory view: ${error instanceof Error ? error.message : String(error)}`); }
229
+ }
230
+ private push(i: number) {
231
+ const part = { l: 0, i };
232
+ this.chat.push(part); this.compaction.push(part);
233
+ if (!this.node(part)) this.unbuilt.add(i);
234
+ }
235
+ get view(): readonly Part[] { return this.chat.parts; }
134
236
  render() { return `${VIEW_OPEN}${this.view.map(p => `${start(p)}+${2 ** p.l}|${flat(this.text(p))}`).join('\n')}\n</chat>`; }
135
237
  get ready() { return this.view.every(p => this.node(p)); }
136
- get pending() { return this.root.length - this.leaves; }
238
+ get pending() { return this.unbuilt.size; }
137
239
  get active() { return this.busy.size; }
138
- get size() { return this.viewBytes; }
240
+ get size() { return this.chat.size; }
139
241
  /** Summaries built out of the backlog since it was last empty, and when the next failed one is retried. */
140
242
  progress(now = Date.now()) {
141
243
  const due = Math.min(...[...this.retryAt.values()].filter(t => t > now));
@@ -143,34 +245,16 @@ export class Memory {
143
245
  }
144
246
  private owed() { return this.expectedNodes() - this.tree.size; }
145
247
  onChange(listener: () => void) { this.events.on('change', listener); return () => { this.events.off('change', listener); }; }
146
- /** The view line covering message `at`. The view tiles the log in order, so a binary search finds it. */
147
- covering(at: number): Part | undefined {
148
- for (let lo = 0, hi = this.view.length - 1; lo <= hi;) {
149
- const mid = (lo + hi) >> 1, p = this.view[mid];
150
- if (end(p) <= at) lo = mid + 1;
151
- else if (start(p) > at) hi = mid - 1;
152
- else return p;
153
- }
154
- }
155
- private visible(part: Part) { const p = this.covering(start(part)); return p?.l === part.l && p.i === part.i; }
248
+ /** The view line covering message `at`. */
249
+ covering(at: number) { return this.chat.covering(at); }
250
+ /** Returns whether the chat's view merged. */
156
251
  private fit(total = this.root.length) {
157
- while (this.viewBytes > this.budget) {
158
- let best = -1; let due = -Infinity;
159
- for (let j = 0; j + 1 < this.view.length; j++) {
160
- const a = this.view[j], b = this.view[j + 1];
161
- if (a.l !== b.l || a.i % 2 || b.i !== a.i + 1) continue;
162
- const age = (total - start(a)) / 2 ** (a.l + 2);
163
- if (age > due && this.node({ l: a.l + 1, i: a.i / 2 })) { best = j; due = age; }
164
- }
165
- if (best < 0) break;
166
- const a = this.view[best], b = this.view[best + 1];
167
- const parent = { l: a.l + 1, i: a.i / 2 };
168
- this.viewBytes += this.partBytes(parent) - this.partBytes(a) - this.partBytes(b);
169
- this.view.splice(best, 2, parent);
170
- }
252
+ const merged = this.chat.fit(total);
253
+ this.compaction.fit(total, merged);
171
254
  const owed = this.owed();
172
255
  this.peak = owed > 0 ? Math.max(this.peak, owed) : 0;
173
256
  this.events.emit('change');
257
+ return merged;
174
258
  }
175
259
  private schedule() {
176
260
  if (this.scheduled || this.stopped) return;
@@ -181,54 +265,72 @@ export class Memory {
181
265
  if (this.stopped) return;
182
266
  // One clock reading: skipping a part and arming its retry timer must agree on what is due.
183
267
  const now = Date.now();
184
- const total = this.root.length;
185
- const first = this.view.find(p => !this.node(p));
186
- const boundary = first ? start(first) : total;
187
- for (let l = 0; 2 ** l <= total; l++) {
188
- let low = this.low[l] ?? 0;
189
- while (this.node({ l, i: low })) low++;
190
- this.low[l] = low;
191
- for (let i = low; (i + 1) * 2 ** l <= total; i++) {
192
- if (this.busy.size >= this.jobs) return;
193
- const part = { l, i }, id = key(part);
194
- if ((l === 0 ? i : end(part)) > boundary) break;
195
- if (this.node(part) || this.busy.has(id) || (this.retryAt.get(id) ?? 0) > now) continue;
196
- if (l && (!this.node({ l: l - 1, i: 2 * i }) || !this.node({ l: l - 1, i: 2 * i + 1 }))) continue;
197
- const promise = this.build(part).catch(error => {
198
- if (this.stopped) return;
199
- this.lastError = error instanceof Error ? error.message : String(error);
200
- if (!this.reported.has(id)) { this.reported.add(id); this.warn(`Compactor ${start(part)}+${2 ** l}: ${this.lastError}`); }
201
- this.retryAt.set(id, Date.now() + this.retryMs);
202
- if (!this.retryTimer) this.retryTimer = setTimeout(() => { this.retryTimer = undefined; this.schedule(); }, this.retryMs);
203
- }).finally(() => { this.busy.delete(id); this.events.emit('change'); this.schedule(); });
204
- this.busy.set(id, promise);
205
- }
268
+ const run = (part: Part) => {
269
+ const id = key(part);
270
+ if (this.busy.has(id) || (this.retryAt.get(id) ?? 0) > now) return;
271
+ const promise = this.build(part).catch(error => {
272
+ if (this.stopped) return;
273
+ this.lastError = error instanceof Error ? error.message : String(error);
274
+ if (!this.reported.has(id)) { this.reported.add(id); this.warn(`Compactor ${start(part)}+${2 ** part.l}: ${this.lastError}`); }
275
+ this.retryAt.set(id, Date.now() + this.retryMs);
276
+ if (!this.retryTimer) this.retryTimer = setTimeout(() => { this.retryTimer = undefined; this.schedule(); }, this.retryMs);
277
+ // Schedule before telling waiters, so a turn never mistakes the gap before the next pump for a stall.
278
+ }).finally(() => { this.busy.delete(id); this.schedule(); this.events.emit('change'); });
279
+ this.busy.set(id, promise);
280
+ };
281
+ // A message's node starts once fewer than AHEAD lines before it are unbuilt; a merge, once both halves are built.
282
+ let ahead = 0;
283
+ for (const i of this.unbuilt) {
284
+ if (ahead++ === AHEAD) break;
285
+ if (this.busy.size >= this.jobs) return;
286
+ run({ l: 0, i });
287
+ }
288
+ for (const part of this.merges.values()) {
289
+ if (this.busy.size >= this.jobs) return;
290
+ run(part);
206
291
  }
207
292
  // A later failure can have a later deadline than the timer installed by the first.
208
293
  const deadlines = [...this.retryAt.values()].filter(t => t > now);
209
294
  if (deadlines.length && !this.retryTimer) this.retryTimer = setTimeout(() => { this.retryTimer = undefined; this.schedule(); }, Math.max(1, Math.min(...deadlines) - now));
295
+ if (this.stalled) this.events.emit('change');
210
296
  }
297
+ /** Nothing is being built, nothing more can start, and something failed: only a retry can make progress. */
298
+ private get stalled() { return this.busy.size === 0 && !this.scheduled && this.retryAt.size > 0; }
211
299
  private async build(part: Part) {
212
300
  const source = part.l === 0 ? `${this.root[part.i].kind}: ${this.root[part.i].text}`
213
301
  : [0, 1].map(offset => this.text({ l: part.l - 1, i: 2 * part.i + offset })).join('\n');
214
- const boundary = part.l === 0 ? start(part) : end(part);
215
- const context = `<chat>\n${this.view.filter(p => end(p) <= boundary).map(p => flat(this.text(p))).join('\n')}\n</chat>`;
216
302
  const mergeSource = part.l > 0 ? [0, 1].map(offset => flat(this.text({ l: part.l - 1, i: 2 * part.i + offset }))).join('\n') : source;
217
- const text = bytes(source) <= NODE ? source : (await this.compress({ context, source: mergeSource, merge: part.l > 0,
303
+ const text = bytes(source) <= NODE ? source : (await this.compress({ context: this.compactionView(part), source: mergeSource, part,
218
304
  historical: this.root.slice(start(part), end(part)).some(entry => !!entry.origin) }, this.controller.signal)).trim();
219
305
  if (this.stopped) return;
220
306
  if (!text) throw new Error('Compactor returned an empty summary.');
221
307
  const node = { ...part, text, size: lineBytes(text) };
222
308
  appendJson(join(this.directory, 'tree', `${localDay()}.jsonl`), node);
223
- this.tree.set(key(part), node); this.retryAt.delete(key(part));
224
- // A leaf is usually still in the view when built, but after a damaged tree file a saved parent can already hide it.
225
- if (part.l === 0) { this.leaves++; if (this.visible(part)) this.viewBytes += node.size - UNBUILT_BYTES; }
309
+ this.tree.set(key(part), node); this.retryAt.delete(key(part)); this.merges.delete(key(part)); this.queueParent(part);
310
+ if (!part.l) { this.unbuilt.delete(part.i); this.chat.built(part.i); this.compaction.built(part.i); }
226
311
  if (!this.retryAt.size) this.lastError = undefined;
227
- this.fit();
312
+ if (this.fit()) this.save();
228
313
  }
229
- /** Waits until every part of the view is built. As in the recipe, the view may run over budget until pending merges land. */
230
- async settle(signal?: AbortSignal, all = false): Promise<void> {
231
- const done = () => this.ready && (!all || (this.busy.size === 0 && this.tree.size === this.expectedNodes()));
314
+ /** The compactions' view up to the node. A line still being built shows as a placeholder instead of ending the view: an A/B on
315
+ * real turns (#101) found that cutting there also hid the built lines after it, such as an echo's own tool call. */
316
+ private compactionView(part: Part) {
317
+ const boundary = part.l === 0 ? start(part) : end(part), lines: string[] = [];
318
+ for (const p of this.compaction.parts) { if (end(p) > boundary) break; lines.push(`${start(p)}+${2 ** p.l}|${flat(this.text(p))}`); }
319
+ return `${VIEW_OPEN}${lines.join('\n')}\n</chat>`;
320
+ }
321
+ private queueParent({ l, i }: Part) {
322
+ const parent = { l: l + 1, i: Math.floor(i / 2) };
323
+ if (!this.node(parent) && this.node({ l, i: i % 2 ? i - 1 : i + 1 })) this.merges.set(key(parent), parent);
324
+ }
325
+ /** A 'turn' waits until every part of the view is built, or until everything pending has failed: it then goes on with placeholders
326
+ * for the missing lines, `lastError` says why, and the failed nodes are still retried in the background. 'tree' waits for every
327
+ * node, through failures. As in the recipe, the view may run over budget until pending merges land.
328
+ * 'ahead' (imports) waits until a new message's node would start at once. Due merges count too: messages go first, so a steady
329
+ * stream of them would otherwise take every worker and the views could never merge. */
330
+ async settle(signal?: AbortSignal, until: 'turn' | 'tree' | 'ahead' = 'turn'): Promise<void> {
331
+ const done = () => until === 'tree' ? this.ready && this.busy.size === 0 && this.tree.size === this.expectedNodes()
332
+ : until === 'ahead' ? this.unbuilt.size + this.merges.size < AHEAD
333
+ : this.ready || this.stalled;
232
334
  if (done()) return;
233
335
  if (this.stopped || signal?.aborted) throw new Error('Memory wait cancelled.');
234
336
  await new Promise<void>((resolve, reject) => {
@@ -243,10 +345,26 @@ export class Memory {
243
345
  for (let n = this.root.length; n > 0; n = Math.floor(n / 2)) count += n;
244
346
  return count;
245
347
  }
246
- zoom(id: number, n: number) {
348
+ /** n = 1 gives the message whole, or, when it is longer than `limit` or `offset` is given, the page of up to `limit` characters from `offset`.
349
+ * Paging a message that fits in one page is allowed, but the page says so. */
350
+ zoom(id: number, n: number, offset?: number, limit?: number) {
247
351
  if (!Number.isSafeInteger(id) || id < 0 || !Number.isSafeInteger(n) || n < 1 || !Number.isInteger(Math.log2(n)) || id % n || id + n > this.root.length)
248
352
  throw new Error(`No line ${id}+${n}.`);
249
- if (n === 1) { const entry = this.root[id]; return `${id}+0|${entry.kind}: ${entry.text}`; }
353
+ if (n === 1) {
354
+ const { kind, text } = this.root[id], page = Math.min(limit ?? PAGE, PAGE);
355
+ if (!Number.isSafeInteger(page) || page < 1) throw new Error('limit must be a positive integer.');
356
+ if (offset === undefined && text.length <= page) return `${id}+0|${kind}: ${text}`;
357
+ if (offset !== undefined && (!Number.isSafeInteger(offset) || offset < 0 || offset >= Math.max(1, text.length)))
358
+ throw new Error(`Message ${id} has ${text.length} characters; offset must be 0 to ${Math.max(0, text.length - 1)}.`);
359
+ // Never split a surrogate pair: a page starts on its first half and ends after its second.
360
+ const from = /[\udc00-\udfff]/.test(text[offset ?? 0] ?? '') ? (offset ?? 0) - 1 : offset ?? 0;
361
+ let to = Math.min(text.length, from + page);
362
+ // A one-unit page on a pair takes the whole pair, so the next offset always moves forward.
363
+ if (to < text.length && /[\ud800-\udbff]/.test(text[to - 1])) to += to - 1 === from ? 1 : -1;
364
+ const hint = text.length <= PAGE ? `\n[note: this message is only ${text.length.toLocaleString('en-US')} characters and fits in one zoom; offset/limit are for messages over ${PAGE.toLocaleString('en-US')}]` : '';
365
+ return `${id}+0|${kind}: ${text.slice(from, to)}\n[showing characters ${from}-${to} of ${text.length}${to < text.length ? `; next page: offset ${to}` : ''}]${hint}`;
366
+ }
367
+ if (offset !== undefined) throw new Error('offset and limit page one message: use them with n = 1.');
250
368
  const l = Math.log2(n) - 1, i = 2 * id / n;
251
369
  return [0, 1].map(offset => {
252
370
  const part = { l, i: i + offset }, node = this.node(part);
package/src/profiles.ts CHANGED
@@ -1,11 +1,16 @@
1
- import { chmodSync, closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
1
+ import { createHash } from 'node:crypto';
2
+ import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, unlinkSync, writeFileSync, type Stats } from 'node:fs';
2
3
  import { createConnection, createServer } from 'node:net';
3
4
  import { homedir } from 'node:os';
4
5
  import { join, resolve } from 'node:path';
5
6
  import type { ModelChoice } from './compactor.ts';
6
7
  import { record } from './cache.ts';
8
+ import { atomicWrite } from './memory.ts';
7
9
  import { DEFAULT_SETTINGS, readSettings, type Settings } from './settings.ts';
8
10
 
11
+ export const isWindows = process.platform === 'win32';
12
+ const PIPE_PREFIX = '\\\\.\\pipe\\';
13
+
9
14
  export const dataHome = () => resolve(process.env.OPTCHAT_HOME ?? join(homedir(), '.optchat'));
10
15
  export interface ProfileConfig extends Settings { compactor: ModelChoice; subagent: ModelChoice }
11
16
  export const defaults: ProfileConfig = {
@@ -22,22 +27,21 @@ export function listProfiles() {
22
27
  const root = join(dataHome(), 'profiles');
23
28
  return existsSync(root) ? readdirSync(root, { withFileTypes: true }).filter(f => f.isDirectory() && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(f.name)).map(f => f.name).sort() : [];
24
29
  }
25
- export function atomicWrite(file: string, text: string) {
26
- mkdirSync(resolve(file, '..'), { recursive: true, mode: 0o700 });
27
- const temporary = `${file}.${process.pid}.tmp`;
28
- writeFileSync(temporary, text, { mode: 0o600 });
29
- const fd = openSync(temporary, 'r');
30
- try { fsyncSync(fd); } finally { closeSync(fd); }
31
- renameSync(temporary, file);
32
- const parent = openSync(resolve(file, '..'), 'r');
33
- try { fsyncSync(parent); } finally { closeSync(parent); }
34
- }
35
30
  export function createProfile(name: string) {
36
31
  const dir = profilePath(name);
37
32
  if (existsSync(dir)) throw new Error(`Profile already exists: ${name}`);
38
- mkdirSync(dir, { recursive: true, mode: 0o700 });
39
- saveConfig(dir, defaults);
40
- atomicWrite(join(dir, 'AGENTS.md'), `# ${name}\n\nThis is the ${name} profile.\nAdd your personal instructions here.\n`);
33
+ mkdirSync(resolve(dir, '..'), { recursive: true, mode: 0o700 });
34
+ // Claim the directory exclusively, so a competing creator cannot have its
35
+ // profile overwritten or removed by this attempt's cleanup.
36
+ mkdirSync(dir, { mode: 0o700 });
37
+ try {
38
+ saveConfig(dir, defaults);
39
+ atomicWrite(join(dir, 'AGENTS.md'), `# ${name}\n\nThis is the ${name} profile.\nAdd your personal instructions here.\n`);
40
+ } catch (error) {
41
+ try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); }
42
+ catch { throw new Error(`Failed to create profile ${name}, and could not remove its incomplete directory: ${dir}`, { cause: error }); }
43
+ throw error;
44
+ }
41
45
  }
42
46
  export function saveConfig(dir: string, config: ProfileConfig) { atomicWrite(join(dir, 'config.json'), JSON.stringify(config, null, 2) + '\n'); }
43
47
  function modelChoice(value: unknown): value is ModelChoice {
@@ -60,24 +64,41 @@ export function rememberProfile(name: string) { atomicWrite(join(dataHome(), 'la
60
64
  export class ProfileBusyError extends Error {
61
65
  constructor(readonly owner: string) { super(`Profile already running: ${owner}`); }
62
66
  }
63
- /** Lives in the profile itself, so every Pi on this profile finds the same socket whatever its TMPDIR. Git skips sockets, so checkpoints never see it. */
64
- export const profileSocket = (dir: string, purpose: 'lock' | 'windows' = 'lock') => join(dir, `${purpose}.sock`);
67
+ /** POSIX keeps a socket file in the profile itself, so every Pi on the profile finds the same one whatever its TMPDIR, and Git skips it. Windows has no Unix sockets, so the profile path is hashed into a named pipe in the machine-wide pipe namespace. */
68
+ export function profileSocket(dir: string, purpose: 'lock' | 'windows' = 'lock') {
69
+ if (!isWindows) return join(dir, `${purpose}.sock`);
70
+ // Hash the directory's real identity, so another casing, a junction or a symlink to the same profile finds the same pipe.
71
+ let identity = resolve(dir); try { identity = realpathSync.native(identity); } catch {}
72
+ return `${PIPE_PREFIX}optchat-${createHash('sha256').update(identity.toLowerCase()).digest('hex').slice(0, 16)}-${purpose}`;
73
+ }
65
74
 
66
- // sun_path is 104 bytes on macOS and 108 elsewhere, both including the NUL. Node 22 binds a truncated path instead of failing, so the length is checked before listen.
67
- export const SOCKET_PATH_LIMIT = process.platform === 'darwin' ? 103 : 107;
75
+ // sun_path is 104 bytes on macOS and 108 elsewhere, both including the NUL; a Windows named pipe path is capped at 259 characters, which for the ASCII names OptChat generates is also its byte length. Node 22 binds a truncated socket instead of failing, so the length is checked before listen.
76
+ export const SOCKET_PATH_LIMIT = process.platform === 'darwin' ? 103 : isWindows ? 259 : 107;
68
77
  export function checkSocketPath(path: string) {
69
78
  const length = Buffer.byteLength(path);
70
79
  if (length > SOCKET_PATH_LIMIT) throw new Error(`Cannot listen on the profile socket: its path is ${length} bytes, over this system's limit of ${SOCKET_PATH_LIMIT}. Set OPTCHAT_HOME to a shorter directory: ${path}`);
71
80
  }
72
81
 
73
- /** A socket that may be replaced; a file that only shares its name is refused, never deleted. */
74
- function existingSocket(path: string) {
82
+ /** A socket that may be replaced; a file that only shares its name is refused, never deleted. A Windows pipe has no filesystem entry, so there is nothing to check or remove. */
83
+ function existingSocket(path: string): { stat: Stats; removable: true } | { removable: false } | undefined {
84
+ if (isWindows) return { removable: false };
75
85
  let stat; try { stat = lstatSync(path); } catch { return undefined; }
76
86
  if (!stat.isSocket()) throw new Error(`${path} is not an OptChat socket. Move it out of the profile and try again.`);
77
- return stat;
87
+ return { stat, removable: true };
78
88
  }
79
89
  /** Removes a dead socket left by a previous owner. */
80
- export function removeStaleSocket(path: string) { if (existingSocket(path)) unlinkSync(path); }
90
+ export function removeStaleSocket(path: string) { if (existingSocket(path)?.removable) unlinkSync(path); }
91
+
92
+ /** Asks the current owner who holds the socket; nothing answering means the entry is stale. A Windows pipe that has disappeared reports ENOENT instead of ECONNREFUSED. */
93
+ function readOwner(path: string) {
94
+ return new Promise<string | undefined>((resolve, reject) => {
95
+ const socket = createConnection(path); let message = '';
96
+ socket.setTimeout(1500, () => { socket.destroy(); reject(new Error('Profile lock did not respond; refusing to steal it.')); });
97
+ socket.on('data', data => { message += data.toString(); });
98
+ socket.on('end', () => resolve(message || 'another Pi instance'));
99
+ socket.on('error', e => { if ('code' in e && (e.code === 'ECONNREFUSED' || isWindows && e.code === 'ENOENT')) resolve(undefined); else reject(e); });
100
+ });
101
+ }
81
102
 
82
103
  /** OS-owned socket lifetime, no timeout-based stealing of a busy profile. */
83
104
  export async function lockProfile(dir: string, description: string) {
@@ -93,18 +114,16 @@ export async function lockProfile(dir: string, description: string) {
93
114
  if (!(error instanceof Error) || !('code' in error) || error.code !== 'EADDRINUSE') throw error;
94
115
  const before = existingSocket(socketPath);
95
116
  if (!before) throw new Error('Profile lock changed; try again.');
96
- const owner = await new Promise<string | undefined>((resolve, reject) => {
97
- const socket = createConnection(socketPath); let message = '';
98
- socket.setTimeout(1500, () => { socket.destroy(); reject(new Error('Profile lock did not respond; refusing to steal it.')); });
99
- socket.on('data', data => { message += data.toString(); });
100
- socket.on('end', () => resolve(message || 'another Pi instance'));
101
- socket.on('error', e => { if ('code' in e && e.code === 'ECONNREFUSED') resolve(undefined); else reject(e); });
102
- });
117
+ const owner = await readOwner(socketPath);
103
118
  if (owner !== undefined) throw new ProfileBusyError(owner);
104
- if (statSync(socketPath).ino !== before.ino) throw new Error('Profile lock changed; try again.');
105
- unlinkSync(socketPath); await listen();
119
+ // Nothing answered, so the entry is stale. POSIX leaves a file behind that can be removed; a Windows pipe disappears with its owner, so rebinding is the whole cleanup.
120
+ if (before.removable) {
121
+ if (statSync(socketPath).ino !== before.stat.ino) throw new Error('Profile lock changed; try again.');
122
+ unlinkSync(socketPath);
123
+ }
124
+ await listen();
106
125
  }
107
126
  const unlock = () => new Promise<void>(resolve => server.close(() => resolve()));
108
- try { chmodSync(socketPath, 0o600); } catch (error) { await unlock(); throw error; }
127
+ if (!isWindows) { try { chmodSync(socketPath, 0o600); } catch (error) { await unlock(); throw error; } }
109
128
  return unlock;
110
129
  }