paseo-follow-up 2.0.2-beta.1 → 2.0.2-beta.2

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 (2) hide show
  1. package/package.json +1 -1
  2. package/server/inbox.ts +64 -2
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "paseo-follow-up",
3
3
  "private": false,
4
- "version": "2.0.2-beta.1",
4
+ "version": "2.0.2-beta.2",
5
5
  "description": "A Paseo plugin that follows every workspace until the work is done: one Home for what needs you, questions you answer anywhere, and repairs that send themselves.",
6
6
  "type": "module",
7
7
  "files": [
package/server/inbox.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { prLink } from "../shared/presentation";
2
2
  import { DEVELOPER, ledgerBy, onBehalfLine, type Actor } from "../shared/actor";
3
- import { mkdir, open, readFile, rename, unlink } from "node:fs/promises";
3
+ import { mkdir, open, readFile, rename, unlink, type FileHandle } from "node:fs/promises";
4
4
  import { join } from "node:path";
5
5
  import { randomUUID } from "node:crypto";
6
6
  import { realTimers, stableMessageId, withDeadline, type Timers } from "./delivery";
@@ -149,12 +149,74 @@ export class InboxStore {
149
149
  const temporary = join(this.directory, `inbox.${randomUUID()}.tmp`);
150
150
  try {
151
151
  const file = await open(temporary, "wx", 0o600);
152
- try { await file.writeFile(JSON.stringify(state)); await file.sync(); } finally { await file.close(); }
152
+ try { await writeParts(file, inboxJsonParts(state)); await file.sync(); } finally { await file.close(); }
153
153
  await rename(temporary, this.path);
154
154
  this.written = { at: this.now() }; this.unsaved = null; this.saved = deepFreeze(state); this.writes++;
155
155
  } finally { await unlink(temporary).catch(() => undefined); }
156
156
  }
157
157
  }
158
+ /**
159
+ * a-mutation-costs-what-it-changes, for the write: inbox.json is JSON.stringify(state), byte for byte, written as the
160
+ * UTF-8 of each part in order (writev), from a cached buffer per saved (frozen) value: each entry, and each top-level
161
+ * field (briefOrigins is 26 KB). The saved state is deep-frozen and a frozen object never changes, so its bytes stay
162
+ * right for as long as the object lives, and a write encodes only what changed since the last one. Cached strings
163
+ * wouldn't do: joining them back into one 133 KB string per write allocated twice what JSON.stringify did. 2026-11-14
164
+ * (2.0.2-beta.1, live mix): stringifying the whole inbox, ~20 writes a minute, was 26.5 of the inbox's 76 MB per 5 min.
165
+ */
166
+ const frozenBytes = new WeakMap<object, Buffer>();
167
+ const keyBytes = new Map<string, Buffer>();
168
+ const OPEN = Buffer.from("{"), CLOSE = Buffer.from("}"), COMMA = Buffer.from(",");
169
+ function valueBytes(value: unknown): Buffer | undefined {
170
+ const frozen = value !== null && typeof value === "object" && Object.isFrozen(value);
171
+ const cached = frozen ? frozenBytes.get(value) : undefined;
172
+ if (cached) return cached;
173
+ const text = JSON.stringify(value);
174
+ if (text === undefined) return undefined;
175
+ const bytes = Buffer.from(text);
176
+ if (frozen) frozenBytes.set(value, bytes);
177
+ return bytes;
178
+ }
179
+ function memberKey(key: string): Buffer {
180
+ let bytes = keyBytes.get(key);
181
+ if (!bytes) { bytes = Buffer.from(`${JSON.stringify(key)}:`); keyBytes.set(key, bytes); }
182
+ return bytes;
183
+ }
184
+ /** JSON.stringify of a plain object as parts: members in insertion order, undefined, functions and symbols left out. */
185
+ function objectParts(value: Record<string, unknown>, out: Buffer[], member: (v: unknown, out: Buffer[]) => boolean): void {
186
+ out.push(OPEN); let first = true;
187
+ for (const key of Object.keys(value)) {
188
+ const at = out.length;
189
+ if (!first) out.push(COMMA);
190
+ out.push(memberKey(key));
191
+ if (member(value[key], out)) first = false; else out.length = at;
192
+ }
193
+ out.push(CLOSE);
194
+ }
195
+ const pushValue = (v: unknown, out: Buffer[]): boolean => { const b = valueBytes(v); if (b) out.push(b); return !!b; };
196
+ /** inbox.json's bytes, in parts: the same bytes as Buffer.from(JSON.stringify(state)). */
197
+ export function inboxJsonParts(state: InboxState): Buffer[] {
198
+ const out: Buffer[] = [];
199
+ objectParts(state as Record<string, unknown>, out, (v, o) => {
200
+ if (v !== state.entries) return pushValue(v, o);
201
+ objectParts(state.entries as Record<string, unknown>, o, pushValue);
202
+ return true;
203
+ });
204
+ // Agent ids come and go: keep the key cache to the keys this inbox has.
205
+ if (keyBytes.size > 4 * (Object.keys(state.entries).length + 32)) keyBytes.clear();
206
+ return out;
207
+ }
208
+ /** Writes every part, however the OS splits a vectored write. */
209
+ async function writeParts(file: FileHandle, parts: Buffer[]): Promise<void> {
210
+ let rest = parts;
211
+ while (rest.length) {
212
+ const { bytesWritten } = await file.writev(rest.length > 512 ? rest.slice(0, 512) : rest);
213
+ let n = bytesWritten, i = 0;
214
+ while (i < rest.length && n >= rest[i]!.length) n -= rest[i++]!.length;
215
+ rest = rest.slice(i);
216
+ if (n > 0) rest = [rest[0]!.subarray(n), ...rest.slice(1)];
217
+ if (bytesWritten === 0 && rest.length) throw new Error("inbox.json: the write made no progress");
218
+ }
219
+ }
158
220
  /**
159
221
  * a-mutation-costs-what-it-changes: entries draft per entry; briefOrigins, read whole and replaced whole, drafts as the
160
222
  * saved frozen array (2026-11-14: each turn end structuredCloned, shadow-walked and re-validated all 500 origins,