Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/gates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ jobs:
cache-key: >-
${{ hashFiles('.github/workflows/gates.yml', 'bind/python/pyproject.toml', 'bind/typescript/build.ts') }}
cache-save-if: ${{ github.ref == 'refs/heads/main' }}
# The bun:ffi of bun 1.3 builds its native callbacks with TinyCC in pages of the heap of Windows, which lose
# their right to run code while the process lives, and a call into OpenTUI then crashes. From bun 1.4 on, the
# engine builds them.
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: "1.3.14"
bun-version: "1.4.2"
- run: uv python install
- run: uv sync --locked
- run: bun install --frozen-lockfile
Expand Down
4 changes: 3 additions & 1 deletion bind/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ chain that holds some. Work that a pause of the operator holds is not in `world.
wake of the operator. A command that an earlier World started and did not end runs again at that wake, once,
and what it told before stands in its door. Wait deadlines and partial streams live in the record's
`.world.json` companion. File snapshots append to `.changes.jsonl`; `world.changes.read` loads a page of them.
Keep both companions with the JSONL record.
Keep both companions with the JSONL record. The World saves `.world.json` whole with `saveFile(path, text)`,
which writes `<path>.tmp` and gives it the name of the file. A save that fails throws an error that names the file,
with the error of the system as its cause. The TUI saves its own files with it.

`inspectRecord(path, models)` reads pending work through the same native replay without taking a record lock,
writing files, or starting a model or command. The TUI runs this inspection in its own worker. `World.activity` holds
Expand Down
2 changes: 1 addition & 1 deletion bind/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export {
imageReferences,
imageType,
} from "./images.js";
export { furbDirectory } from "./project.js";
export { furbDirectory, saveFile } from "./project.js";
export { shell } from "./shell.js";
export type { Entry, Fact, OperatorPrompt, Paragraph, Turn, Usage } from "./types.js";
export {
Expand Down
15 changes: 14 additions & 1 deletion bind/typescript/src/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, writeFileSync } from "node:fs";
import { mkdirSync, renameSync, writeFileSync } from "node:fs";
import { join } from "node:path";

/** The directory `.furb/<parts>` of a project, made when it is absent. Records, file contents, shares and images
Expand All @@ -15,3 +15,16 @@ export function furbDirectory(project: string, ...parts: string[]): string {
mkdirSync(directory, { recursive: true });
return directory;
}

/** Save a file whole: the text goes to `<path>.tmp`, which then takes the name of the file, so no reader sees a part
* of it. A save that fails throws an error that names its file, with the error of the system as its cause, since
* the error of the runtime does not always name the file: bun 1.4 on Windows says only "write". */
export function saveFile(path: string, text: string): void {
const draft = `${path}.tmp`;
try {
writeFileSync(draft, text, { mode: 0o600 });
renameSync(draft, path);
} catch (error) {
throw new Error(`Could not save ${path} through ${draft}: ${(error as Error).message}`, { cause: error });
}
}
7 changes: 3 additions & 4 deletions bind/typescript/src/world.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChildProcessWithoutNullStreams, spawn, spawnSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import {
type Api,
Expand All @@ -20,7 +20,7 @@ import { Activity, type RunState } from "./activity.js";
import { FileChanges } from "./changes.js";
import { type Ears, WorldAdapter, type WorldHandler, type WorldRequest } from "./ears.js";
import { attachImage, type ImageAttachment, ImageCache, turnImages } from "./images.js";
import { furbDirectory } from "./project.js";
import { furbDirectory, saveFile } from "./project.js";
import { RecordFile } from "./record.js";
import { shell } from "./shell.js";
import {
Expand Down Expand Up @@ -555,8 +555,7 @@ export class World extends EventEmitter {
deadlines: [...this.deadlines],
streams: [...this.streams],
};
writeFileSync(`${path}.tmp`, JSON.stringify(saved), { mode: 0o600 });
renameSync(`${path}.tmp`, path);
saveFile(path, JSON.stringify(saved));
}

/** Start the pending work: a wake of each chain that holds some, which the engine answers by starting each
Expand Down
6 changes: 3 additions & 3 deletions tui/src/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { mkdirSync, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { saveFile } from "@furb/engine";
import { defaultTheme, palettes, type ThemeName } from "./theme.ts";

export class Preferences {
Expand Down Expand Up @@ -52,8 +53,7 @@ export class Preferences {
const data = this.serialize(theme);
if (data === this.saved) return;
mkdirSync(dirname(this.path), { recursive: true });
writeFileSync(`${this.path}.tmp`, data, { mode: 0o600 });
renameSync(`${this.path}.tmp`, this.path);
saveFile(this.path, data);
this.theme = theme;
this.saved = data;
this.notice = "";
Expand Down
6 changes: 3 additions & 3 deletions tui/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
import { readFileSync, renameSync, writeFileSync } from "node:fs";
import { readFileSync } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { basename, dirname, join, resolve } from "node:path";
import type { Fact, ImageAttachment, LiveAct, Turn, Usage } from "@furb/engine";
Expand All @@ -11,6 +11,7 @@ import {
imagePath,
imageReference,
imageReferences,
saveFile,
shapes,
} from "@furb/engine";
import type { FileChange } from "@furb/engine/world";
Expand Down Expand Up @@ -846,8 +847,7 @@ export class Session extends EventEmitter {
...Object.fromEntries(kept.map((key) => [key, this[key]])),
cost: this.savedCost,
};
writeFileSync(`${path}.tmp`, JSON.stringify(view), { mode: 0o600 });
renameSync(`${path}.tmp`, path);
saveFile(path, JSON.stringify(view));
}
/** Save the view, then end the World whatever the save came to. */
async dispose(): Promise<void> {
Expand Down
7 changes: 3 additions & 4 deletions tui/src/workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { existsSync, mkdirSync, readFileSync } from "node:fs";
import { mkdir, readdir, realpath, rename, stat, writeFile } from "node:fs/promises";
import { basename, dirname, join, resolve } from "node:path";
import { furbDirectory, RecordLock } from "@furb/engine";
import { furbDirectory, RecordLock, saveFile } from "@furb/engine";
import { openEngine } from "./bridge.ts";
import { expandHome } from "./files.ts";
import type { EngineOptions } from "./models.ts";
Expand Down Expand Up @@ -132,8 +132,7 @@ export class Workspaces extends EventEmitter {
if (change.add && !group.records.includes(change.add)) group.records.unshift(change.add);
if (change.remove) group.records = group.records.filter((record) => record !== change.remove);
mkdirSync(dirname(this.path), { recursive: true });
writeFileSync(`${this.path}.tmp`, JSON.stringify({ workspaces: list }), { mode: 0o600 });
renameSync(`${this.path}.tmp`, this.path);
saveFile(this.path, JSON.stringify({ workspaces: list }));
if (this.damaged) {
this.damaged = false;
this.notice = "";
Expand Down
18 changes: 18 additions & 0 deletions tui/test/preferences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "bun:test";
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Preferences } from "../src/preferences.ts";

test("a save of the preferences that fails names its file, and the preferences stay as they were", async () => {
const directory = await mkdtemp(join(tmpdir(), "furb-preferences-"));
try {
const preferences = new Preferences(join(directory, "ui.json"));
// A directory where the save writes its file makes the save fail on every system.
await mkdir(join(directory, "ui.json.tmp"));
expect(() => preferences.save("paper")).toThrow("ui.json.tmp");
expect(preferences.theme).toBe("github");
} finally {
await rm(directory, { recursive: true, force: true });
}
});
16 changes: 16 additions & 0 deletions tui/test/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ test("the workspace list keeps what each instance saves, and a list that cannot
}
}, 30000);

test("a save of the workspace list that fails names its file", async () => {
const directory = await mkdtemp(join(tmpdir(), "furb-list-save-"));
const library = new Workspaces(new Preferences(join(directory, "config/ui.json")), { demo: true });
try {
await mkdir(join(directory, "project"));
// A directory where the save writes its file makes the save fail on every system.
await mkdir(join(directory, "config/workspaces.json.tmp"), { recursive: true });
expect(String(await library.add(join(directory, "project")).catch((error: unknown) => error))).toContain(
"workspaces.json.tmp",
);
} finally {
await library.dispose();
await rm(directory, { recursive: true, force: true });
}
});

test("the .furb that the TUI makes in a project keeps itself out of version control", async () => {
const directory = await mkdtemp(join(tmpdir(), "furb-ignore-"));
const library = new Workspaces(new Preferences(join(directory, "config/ui.json")), { demo: true });
Expand Down