Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Publish complete lifetime receipts before readers consume them (#6582)

Nick Gomez4fc28ae651b5
diff --git a/packages/app/tests/foundation/lifetime-receipt-publication.node.test.ts b/packages/app/tests/foundation/lifetime-receipt-publication.node.test.ts
new file mode 100644
index 000000000..58d745179
--- /dev/null
+++ b/packages/app/tests/foundation/lifetime-receipt-publication.node.test.ts
@@ -0,0 +1,69 @@
+import { EventEmitter } from 'node:events';
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import lifetimeGlobalSetup from '../stress/_helpers/port-ownership/lifetime-global-setup.ts';
+
+const publication = vi.hoisted(() => {
+ const observations: Array<string | undefined> = [];
+ return { runDir: '', observations };
+});
+
+vi.mock('node:child_process', async (importOriginal) => {
+ const childProcess = await importOriginal<typeof import('node:child_process')>();
+ return {
+ ...childProcess,
+ spawn: (_command: string, args: readonly string[]) => {
+ const child = new EventEmitter();
+ const receipt = { port: 43123, nonce: args[1], pid: 12345 };
+ vi.stubGlobal('fetch', async () => new Response(receipt.nonce));
+ queueMicrotask(() => child.emit('message', receipt));
+ return child;
+ },
+ };
+});
+
+vi.mock('node:fs', async (importOriginal) => {
+ const fs = await importOriginal<typeof import('node:fs')>();
+ return {
+ ...fs,
+ writeFileSync: (...args: Parameters<typeof fs.writeFileSync>) => {
+ const [path] = args;
+ if (typeof path === 'string' && dirname(path) === publication.runDir) {
+ fs.closeSync(fs.openSync(path, 'w'));
+ const receiptPath = join(publication.runDir, 'lifetime-receipt.json');
+ publication.observations.push(
+ fs.existsSync(receiptPath) ? fs.readFileSync(receiptPath, 'utf8') : undefined,
+ );
+ }
+ return fs.writeFileSync(...args);
+ },
+ };
+});
+
+describe('lifetime receipt publication', () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.unstubAllEnvs();
+ if (publication.runDir) rmSync(publication.runDir, { recursive: true, force: true });
+ publication.runDir = '';
+ publication.observations = [];
+ });
+
+ it('exposes the receipt only after its contents are complete', async () => {
+ publication.runDir = mkdtempSync(join(tmpdir(), 'ok-lifetime-publication-'));
+ vi.stubEnv('OK_PORT_OWNERSHIP_RUN_DIR', publication.runDir);
+ vi.stubEnv('OK_PORT_OWNERSHIP_CALLER', 'lifetime-normal');
+ await lifetimeGlobalSetup();
+
+ expect(publication.observations).toEqual([undefined]);
+ expect(
+ JSON.parse(readFileSync(join(publication.runDir, 'lifetime-receipt.json'), 'utf8')),
+ ).toMatchObject({
+ port: expect.any(Number),
+ nonce: expect.any(String),
+ pid: expect.any(Number),
+ });
+ });
+});
diff --git a/packages/app/tests/stress/_helpers/port-ownership/lifetime-global-setup.ts b/packages/app/tests/stress/_helpers/port-ownership/lifetime-global-setup.ts
index 9437e96b8..c83aa17f1 100644
--- a/packages/app/tests/stress/_helpers/port-ownership/lifetime-global-setup.ts
+++ b/packages/app/tests/stress/_helpers/port-ownership/lifetime-global-setup.ts
@@ -1,7 +1,7 @@
import { spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { once } from 'node:events';
-import { writeFileSync } from 'node:fs';
+import { renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -19,7 +19,10 @@ export default async function lifetimeGlobalSetup(): Promise<() => Promise<void>
const receipt = message as { port: number; nonce: string; pid: number };
const response = await fetch(`http://127.0.0.1:${receipt.port}/identity`);
if ((await response.text()) !== nonce) throw new Error('owned lifetime endpoint did not serve');
- writeFileSync(join(runDir, 'lifetime-receipt.json'), JSON.stringify(receipt));
+ const receiptPath = join(runDir, 'lifetime-receipt.json');
+ const temporaryReceiptPath = `${receiptPath}.tmp`;
+ writeFileSync(temporaryReceiptPath, JSON.stringify(receipt));
+ renameSync(temporaryReceiptPath, receiptPath);
if (process.env.OK_PORT_OWNERSHIP_CALLER !== 'lifetime-normal') {
await new Promise<never>(() => {});