repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

fix(bak): use strong ETags and the real session directory

d181cfb39439f0c2b2b5fd608fc17e1e218e9dd1

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Strip weak ETag prefixes on read, write, and if-match, so conditional catalog writes work against stores that answer with weak validators.
Request identity encoding on GET, because a re-encoded body returns an ETag that no longer matches the stored object.
List the default session store when Pi reports no session directory, instead of scanning the working directory.
Add a real-bucket E2E suite behind the PI_BAK_S3_ environment that skips itself when unconfigured, plus an in-memory S3 unit test.

Changed files (9)

extensions/bak/README.md

Modified
46 46 `/bak backup` uploads all locally discoverable sessions.
47 47 Completed agent runs back up their current session asynchronously.
48 48 Restore defaults to the current configured session directory and never silently overwrites differing files.
50 +## Tests
52 +`mise run //extensions/bak:test` covers the archive logic against an in-memory store.
54 +`mise run //extensions/bak:e2e` drives the real bucket named by the `PI_BAK_S3_*` environment.
55 +It skips itself when those variables are missing.
56 +Every test host uses a throwaway `e2e-*` alias and deletes its objects and catalog entry afterwards.

extensions/bak/__e2e__/commands.test.ts

Added
new file mode 100644
1 +import { mkdir, mkdtemp, readdir, readFile, rm } from "node:fs/promises";
2 +import { tmpdir } from "node:os";
3 +import { join } from "node:path";
4 +import type { SessionInfo } from "@earendil-works/pi-coding-agent";
5 +import { afterAll, beforeAll, describe, expect, it } from "vitest";
6 +import { BakArchive } from "../archive.js";
7 +import bakExtension from "../index.js";
8 +import { type S3Config, S3Store } from "../s3.js";
9 +import { type HostIdentity, statePaths } from "../state.js";
10 +import {
11 + removeHosts,
12 + storageConfig,
13 + testAlias,
14 + writeSession,
15 +} from "./support.js";
17 +const config = storageConfig;
19 +interface Note {
20 + message: string;
21 + level: string;
24 +type CommandHandler = (args: string, ctx: unknown) => Promise<void> | void;
25 +type EventHandler = (event: unknown, ctx: unknown) => Promise<void> | void;
27 +const aliases: string[] = [];
28 +let workspace: string;
29 +let sessionDir: string;
30 +let notes: Note[] = [];
31 +let run: (args: string) => Promise<void>;
32 +let emit: (event: string) => Promise<void>;
33 +let context: Record<string, unknown>;
34 +let currentSession: SessionInfo | undefined;
36 +/**
37 + * Registers the extension exactly like Pi does, but against a throwaway state
38 + * directory and session store so real backups stay untouched.
39 + */
40 +function loadExtension(): void {
41 + const handlers = new Map<string, EventHandler[]>();
42 + let command: { handler: CommandHandler } | undefined;
43 + const pi = {
44 + on(event: string, handler: EventHandler) {
45 + handlers.set(event, [...(handlers.get(event) ?? []), handler]);
46 + },
47 + registerCommand(_name: string, definition: { handler: CommandHandler }) {
48 + command = definition;
49 + },
50 + };
51 + bakExtension(pi as never);
52 + if (!command) throw new Error("bak command was not registered");
53 + const registered = command;
54 + run = async (args: string) => {
55 + await registered.handler(args, context);
56 + };
57 + emit = async (event: string) => {
58 + for (const handler of handlers.get(event) ?? [])
59 + await handler({ type: event }, context);
60 + };
63 +function sessionContext(): Record<string, unknown> {
64 + return {
65 + hasUI: true,
66 + ui: {
67 + notify: (message: string, level = "info") => {
68 + notes.push({ message, level });
69 + },
70 + setStatus: () => undefined,
71 + theme: { fg: (_style: string, text: string) => text },
72 + },
73 + sessionManager: {
74 + getSessionDir: () => sessionDir,
75 + usesDefaultSessionDir: () => false,
76 + getSessionFile: () => currentSession?.path,
77 + getHeader: () =>
78 + currentSession
79 + ? {
80 + id: currentSession.id,
81 + cwd: currentSession.cwd,
82 + timestamp: currentSession.created.toISOString(),
83 + }
84 + : undefined,
85 + getSessionName: () => currentSession?.name,
86 + getEntries: () => [],
87 + },
88 + };
91 +function lastNote(): Note {
92 + const note = notes.at(-1);
93 + if (!note) throw new Error("no bak notification recorded");
94 + return note;
97 +async function archivedIds(alias: string): Promise<string[]> {
98 + const cache = await sandboxArchive().refresh();
99 + return (
100 + cache.indexes
101 + .find((index) => index.alias === alias)
102 + ?.sessions.map((record) => record.id) ?? []
103 + );
106 +async function waitForArchived(alias: string, id: string): Promise<void> {
107 + for (let attempt = 0; attempt < 30; attempt++) {
108 + if ((await archivedIds(alias)).includes(id)) return;
109 + await new Promise((resolve) => setTimeout(resolve, 500));
111 + throw new Error(`session ${id} never reached the archive`);
114 +function sandboxArchive(): BakArchive {
115 + return new BakArchive(
116 + new S3Store(config as S3Config),
117 + statePaths(process.env),
118 + );
121 +describe.skipIf(!config)("bak commands against real S3 storage", () => {
122 + const original = {
123 + state: process.env.XDG_STATE_HOME,
124 + cache: process.env.XDG_CACHE_HOME,
125 + };
127 + beforeAll(async () => {
128 + workspace = await mkdtemp(join(tmpdir(), "bak-cmd-e2e-"));
129 + process.env.XDG_STATE_HOME = join(workspace, "state");
130 + process.env.XDG_CACHE_HOME = join(workspace, "cache");
131 + sessionDir = join(workspace, "sessions");
132 + await mkdir(sessionDir, { recursive: true });
133 + context = sessionContext();
134 + loadExtension();
135 + });
137 + afterAll(async () => {
138 + if (config && aliases.length > 0) {
139 + const catalog = await new S3Store(config).get("catalog/hosts.json");
140 + const hosts = catalog
141 + ? (
142 + JSON.parse(new TextDecoder().decode(catalog.body)) as {
143 + hosts: HostIdentity[];
145 + ).hosts
146 + : [];
147 + await removeHosts(
148 + config,
149 + hosts
150 + .filter((host) => aliases.includes(host.alias))
151 + .map((host) => host.hostId),
152 + );
154 + process.env.XDG_STATE_HOME = original.state;
155 + process.env.XDG_CACHE_HOME = original.cache;
156 + if (original.state === undefined) delete process.env.XDG_STATE_HOME;
157 + if (original.cache === undefined) delete process.env.XDG_CACHE_HOME;
158 + await rm(workspace, { recursive: true, force: true });
159 + });
161 + it("reports an uninitialized host", async () => {
162 + notes = [];
163 + await run("status");
165 + expect(lastNote().message).toContain("not initialized");
166 + expect(lastNote().message).toContain("0 pending");
167 + });
169 + it("initializes, backs up local sessions, and reports status", async () => {
170 + const alias = testAlias();
171 + aliases.push(alias);
172 + notes = [];
174 + await run(`init ${alias}`);
175 + expect(lastNote().message).toBe(`bak: initialized as ${alias}`);
176 + const identity = JSON.parse(
177 + await readFile(statePaths(process.env).identity, "utf8"),
178 + ) as HostIdentity;
179 + expect(identity.alias).toBe(alias);
181 + const first = await writeSession(sessionDir, { name: "command session" });
182 + const second = await writeSession(sessionDir, { messages: ["x", "y"] });
183 + await run("backup");
184 + expect(lastNote().message).toBe("bak: backed up 2; 2 indexed");
186 + await run("status");
187 + expect(lastNote().message).toContain(alias);
188 + expect(lastNote().message).toContain("ready");
189 + // The counter spans every catalog host, including unrelated real hosts.
190 + expect(lastNote().message).toMatch(/[1-9]\d* indexed/);
191 + expect(lastNote().message).toContain("0 pending");
192 + expect(lastNote().message).not.toContain("error");
194 + const cache = await sandboxArchive().refresh();
195 + const ids =
196 + cache.indexes
197 + .find((index) => index.alias === alias)
198 + ?.sessions.map((record) => record.id) ?? [];
199 + expect(ids.sort()).toEqual([first.id, second.id].sort());
200 + });
202 + it("refreshes the catalog and restores by alias prefix", async () => {
203 + const alias = aliases.at(-1) as string;
204 + notes = [];
205 + await run("refresh");
206 + expect(lastNote().message).toMatch(/refreshed \d+ host\(s\)/);
208 + const cache = await sandboxArchive().refresh();
209 + const record = cache.indexes.find((index) => index.alias === alias)
210 + ?.sessions[0];
211 + expect(record).toBeDefined();
212 + const target = join(workspace, "restore-target");
214 + await run(`restore ${alias}/${record?.id.slice(0, 8)} ${target}`);
215 + expect(lastNote().message).toBe("bak: restored 1, skipped 0, conflicts 0");
216 + expect(await readdir(target)).toHaveLength(1);
218 + await run(`restore ${alias}/${record?.id.slice(0, 8)} ${target}`);
219 + expect(lastNote().message).toBe("bak: restored 0, skipped 1, conflicts 0");
220 + });
222 + it("restores every archived session of the host", async () => {
223 + const alias = aliases.at(-1) as string;
224 + const target = join(workspace, "restore-all");
225 + notes = [];
227 + await run(`restore all ${target}`);
228 + expect(lastNote().message).toMatch(/restored \d+, skipped \d+/);
230 + const cache = await sandboxArchive().refresh();
231 + const own =
232 + cache.indexes.find((index) => index.alias === alias)?.sessions.length ??
233 + 0;
234 + expect((await readdir(target)).length).toBeGreaterThanOrEqual(own);
235 + });
237 + it("keeps working after session lifecycle events", async () => {
238 + notes = [];
239 + await emit("session_start");
240 + await writeSession(sessionDir, { messages: ["after restart"] });
241 + await run("backup");
242 + expect(lastNote().message).toBe("bak: backed up 3; 3 indexed");
244 + await run("status");
245 + expect(lastNote().message).toMatch(/[1-9]\d* indexed/);
246 + expect(lastNote().message).toContain("0 pending");
247 + expect(lastNote().message).not.toContain("error");
248 + });
250 + it("backs up the active session when the agent settles", async () => {
251 + const alias = aliases.at(-1) as string;
252 + notes = [];
253 + currentSession = await writeSession(sessionDir, {
254 + messages: ["settled"],
255 + name: "settled session",
256 + });
258 + await emit("agent_settled");
259 + await waitForArchived(alias, currentSession.id);
261 + expect(notes.filter((note) => note.level !== "info")).toEqual([]);
262 + const markers = await readdir(statePaths(process.env).pendingDir);
263 + expect(markers).toEqual([]);
264 + });
266 + it("retains a pending retry after a failed backup and recovers", async () => {
267 + const alias = aliases.at(-1) as string;
268 + const bucket = process.env.PI_BAK_S3_BUCKET;
269 + notes = [];
270 + currentSession = await writeSession(sessionDir, { messages: ["pending"] });
272 + process.env.PI_BAK_S3_BUCKET = `${(config as S3Config).bucket}-missing`;
273 + loadExtension();
274 + await run("backup");
275 + expect(lastNote().level).toBe("error");
276 + expect(await readdir(statePaths(process.env).pendingDir)).not.toEqual([]);
278 + process.env.PI_BAK_S3_BUCKET = bucket;
279 + if (bucket === undefined) delete process.env.PI_BAK_S3_BUCKET;
280 + loadExtension();
281 + await emit("session_start");
282 + await emit("agent_settled");
283 + await waitForArchived(alias, currentSession.id);
285 + for (let attempt = 0; attempt < 30; attempt++) {
286 + if ((await readdir(statePaths(process.env).pendingDir)).length === 0)
287 + break;
288 + await new Promise((resolve) => setTimeout(resolve, 500));
290 + expect(await readdir(statePaths(process.env).pendingDir)).toEqual([]);
291 + });
293 + it("reports usage and initialization errors", async () => {
294 + notes = [];
295 + await run("bogus");
296 + expect(lastNote().level).toBe("error");
297 + expect(lastNote().message).toContain("Usage:");
299 + await run(`init ${testAlias()}`);
300 + expect(lastNote().level).toBe("error");
301 + expect(lastNote().message).toMatch(/Already initialized as/);
302 + });
303 +});

extensions/bak/__e2e__/r2.test.ts

Added
new file mode 100644
1 +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2 +import { tmpdir } from "node:os";
3 +import { dirname, join } from "node:path";
4 +import { afterAll, beforeAll, describe, expect, it } from "vitest";
5 +import { BakArchive, sessionInfoFromPath } from "../archive.js";
6 +import { type S3Config, S3Store } from "../s3.js";
7 +import type { StatePaths } from "../state.js";
8 +import {
9 + appendMessage,
10 + removeHosts,
11 + sessionBody,
12 + storageConfig,
13 + testAlias,
14 + writeSession,
15 +} from "./support.js";
17 +const config = storageConfig;
18 +const hostIds: string[] = [];
19 +let workspace: string;
21 +function statePathsIn(name: string): StatePaths {
22 + const root = join(workspace, name);
23 + return {
24 + identity: join(root, "identity.json"),
25 + cache: join(root, "catalog.json"),
26 + pendingDir: join(root, "pending"),
27 + };
30 +function newArchive(paths: StatePaths): BakArchive {
31 + return new BakArchive(new S3Store(config as S3Config), paths);
34 +async function initHost(name: string, alias: string) {
35 + const paths = statePathsIn(name);
36 + const archive = newArchive(paths);
37 + const identity = await archive.init(alias);
38 + hostIds.push(identity.hostId);
39 + return { archive, paths, identity };
42 +async function sessionDir(name: string): Promise<string> {
43 + const directory = join(workspace, name);
44 + await mkdir(directory, { recursive: true });
45 + return directory;
48 +describe.skipIf(!config)("bak archive against real S3 storage", () => {
49 + beforeAll(async () => {
50 + workspace = await mkdtemp(join(tmpdir(), "bak-e2e-"));
51 + });
53 + afterAll(async () => {
54 + if (config) await removeHosts(config, hostIds);
55 + await rm(workspace, { recursive: true, force: true });
56 + });
58 + it("registers a host and creates an empty index", async () => {
59 + const alias = testAlias();
60 + const { archive, identity } = await initHost("alpha", alias);
62 + expect(identity.alias).toBe(alias);
63 + const cache = await archive.refresh();
64 + const index = cache.indexes.find(
65 + (entry) => entry.hostId === identity.hostId,
66 + );
67 + expect(cache.catalog.hosts).toContainEqual(identity);
68 + expect(index?.sessions).toEqual([]);
69 + });
71 + it("rejects a duplicate alias and rolls back the local identity", async () => {
72 + const alias = testAlias();
73 + const first = await initHost("collide-a", alias);
74 + const paths = statePathsIn("collide-b");
75 + const archive = newArchive(paths);
77 + await expect(archive.init(alias)).rejects.toThrow(/Alias already exists/);
78 + await expect(readFile(paths.identity, "utf8")).rejects.toMatchObject({
79 + code: "ENOENT",
80 + });
81 + expect(first.identity.alias).toBe(alias);
82 + });
84 + it("uploads sessions, indexes them, and stays idempotent", async () => {
85 + const { archive, identity } = await initHost("backup", testAlias());
86 + const directory = await sessionDir("backup-sessions");
87 + const first = await writeSession(directory, { name: "first session" });
88 + const second = await writeSession(directory, { messages: ["a", "b"] });
90 + expect(await archive.backup([first, second])).toEqual({
91 + uploaded: 2,
92 + indexed: 2,
93 + });
94 + expect(await archive.backup([first, second])).toEqual({
95 + uploaded: 2,
96 + indexed: 2,
97 + });
99 + const cache = await archive.refresh();
100 + const index = cache.indexes.find(
101 + (entry) => entry.hostId === identity.hostId,
102 + );
103 + expect(index?.sessions).toHaveLength(2);
104 + expect(index?.sessions.find((record) => record.id === first.id)?.name).toBe(
105 + "first session",
106 + );
107 + expect(index?.sessions.map((record) => record.id).sort()).toEqual(
108 + [first.id, second.id].sort(),
109 + );
110 + });
112 + it("extends an appended session and rejects divergent rewrites", async () => {
113 + const { archive } = await initHost("append", testAlias());
114 + const directory = await sessionDir("append-sessions");
115 + const info = await writeSession(directory, { messages: ["one"] });
116 + await archive.backup([info]);
118 + const grown = await appendMessage(info.path, "two");
119 + expect(await archive.backup([grown])).toEqual({ uploaded: 1, indexed: 1 });
121 + await writeFile(
122 + info.path,
123 + sessionBody(info.id, info.created.toISOString(), info.cwd, ["other"]),
124 + );
125 + const diverged = await sessionInfoFromPath(info.path);
126 + await expect(archive.backup([diverged])).rejects.toThrow(
127 + /Archived session diverged/,
128 + );
129 + });
131 + it("restores, skips identical files, and reports conflicts", async () => {
132 + const { archive, identity } = await initHost("restore", testAlias());
133 + const directory = await sessionDir("restore-source");
134 + const info = await writeSession(directory, { messages: ["restore me"] });
135 + await archive.backup([info]);
136 + const cache = await archive.refresh();
137 + const records =
138 + cache.indexes.find((entry) => entry.hostId === identity.hostId)
139 + ?.sessions ?? [];
140 + expect(records).toHaveLength(1);
142 + const target = join(workspace, "restore-target");
143 + expect(await archive.restore(records, target)).toEqual({
144 + restored: 1,
145 + skipped: 0,
146 + conflicts: 0,
147 + });
148 + expect(await archive.restore(records, target)).toEqual({
149 + restored: 0,
150 + skipped: 1,
151 + conflicts: 0,
152 + });
154 + const conflictDir = await sessionDir("restore-conflict");
155 + await writeFile(
156 + join(conflictDir, `conflict_${info.id}.jsonl`),
157 + sessionBody(info.id, info.created.toISOString(), info.cwd, ["local"]),
158 + );
159 + expect(await archive.restore(records, conflictDir)).toEqual({
160 + restored: 0,
161 + skipped: 0,
162 + conflicts: 1,
163 + });
164 + });
166 + it("survives concurrent index updates from two archives", async () => {
167 + const { archive, identity, paths } = await initHost("cas-a", testAlias());
168 + const secondPaths = statePathsIn("cas-b");
169 + await mkdir(dirname(secondPaths.identity), { recursive: true });
170 + await writeFile(secondPaths.identity, `${JSON.stringify(identity)}\n`);
171 + const other = newArchive(secondPaths);
172 + const directory = await sessionDir("cas-sessions");
173 + const left = await writeSession(directory, { messages: ["left"] });
174 + const right = await writeSession(directory, { messages: ["right"] });
176 + const [a, b] = await Promise.all([
177 + archive.backup([left]),
178 + other.backup([right]),
179 + ]);
181 + expect(a.uploaded).toBe(1);
182 + expect(b.uploaded).toBe(1);
183 + const cache = await newArchive(paths).refresh();
184 + const index = cache.indexes.find(
185 + (entry) => entry.hostId === identity.hostId,
186 + );
187 + expect(index?.sessions.map((record) => record.id).sort()).toEqual(
188 + [left.id, right.id].sort(),
189 + );
190 + });
191 +});

extensions/bak/__e2e__/support.ts

Added
new file mode 100644
1 +import { randomUUID } from "node:crypto";
2 +import { readFile, writeFile } from "node:fs/promises";
3 +import { join } from "node:path";
4 +import type { SessionInfo } from "@earendil-works/pi-coding-agent";
5 +import { AwsClient } from "aws4fetch";
6 +import { sessionInfoFromPath } from "../archive.js";
7 +import { type S3Config, S3Store, s3Config } from "../s3.js";
8 +import type { HostIdentity } from "../state.js";
10 +const decoder = new TextDecoder();
12 +export const CATALOG_KEY = "catalog/hosts.json";
13 +export const storageConfig = s3Config().config;
15 +export function testAlias(): string {
16 + return `e2e-${randomUUID().slice(0, 8)}`;
19 +function signedClient(config: S3Config): AwsClient {
20 + return new AwsClient({
21 + accessKeyId: config.accessKeyId,
22 + secretAccessKey: config.secretAccessKey,
23 + service: "s3",
24 + region: config.region,
25 + retries: 0,
26 + });
29 +function objectUrl(config: S3Config, key: string): string {
30 + const endpoint = config.endpoint.replace(/\/+$/, "");
31 + const path = [config.bucket, ...key.split("/")]
32 + .map((part) => encodeURIComponent(part))
33 + .join("/");
34 + return `${endpoint}/${path}`;
37 +export async function listKeys(
38 + config: S3Config,
39 + prefix: string,
40 +): Promise<string[]> {
41 + const url = `${config.endpoint.replace(/\/+$/, "")}/${encodeURIComponent(config.bucket)}?list-type=2&max-keys=1000&prefix=${encodeURIComponent(prefix)}`;
42 + const response = await signedClient(config).fetch(url, { method: "GET" });
43 + const xml = await response.text();
44 + return [...xml.matchAll(/<Key>([^<]+)<\/Key>/g)].map(
45 + (match) => match[1] as string,
46 + );
49 +export async function deleteKey(
50 + config: S3Config,
51 + key: string,
52 +): Promise<number> {
53 + const response = await signedClient(config).fetch(objectUrl(config, key), {
54 + method: "DELETE",
55 + });
56 + return response.status;
59 +/**
60 + * The bucket is shared with real backups, so tests only ever remove the
61 + * throwaway hosts they created themselves.
62 + */
63 +export async function removeHosts(
64 + config: S3Config,
65 + hostIds: string[],
66 +): Promise<void> {
67 + if (hostIds.length === 0) return;
68 + for (const hostId of hostIds) {
69 + for (const key of await listKeys(config, `sessions/${hostId}/`))
70 + await deleteKey(config, key);
71 + await deleteKey(config, `hosts/${hostId}.json`);
72 + }
73 + const store = new S3Store(config);
74 + for (let attempt = 0; attempt < 5; attempt++) {
75 + const current = await store.get(CATALOG_KEY);
76 + if (!current?.etag) return;
77 + const catalog = JSON.parse(decoder.decode(current.body)) as {
78 + version: number;
79 + hosts: HostIdentity[];
80 + };
81 + const hosts = catalog.hosts.filter(
82 + (host) => !hostIds.includes(host.hostId),
83 + );
84 + if (hosts.length === catalog.hosts.length) return;
85 + try {
86 + await store.put(
87 + CATALOG_KEY,
88 + `${JSON.stringify({ ...catalog, hosts })}\n`,
89 + {
90 + contentType: "application/json",
91 + ifMatch: current.etag,
92 + },
93 + );
94 + return;
95 + } catch {
96 + // Concurrent catalog writer; retry the read-modify-write cycle.
97 + }
98 + }
101 +export function sessionBody(
102 + id: string,
103 + created: string,
104 + cwd: string,
105 + messages: string[],
106 + name?: string,
107 +): string {
108 + // Entries mirror the real Pi session schema; Pi skips files it cannot parse.
109 + const lines = [
110 + JSON.stringify({
111 + type: "session",
112 + version: 3,
113 + id,
114 + timestamp: created,
115 + cwd,
116 + }),
117 + ];
118 + let parentId: string | null = null;
119 + for (const text of messages) {
120 + const entryId = randomUUID().slice(0, 8);
121 + lines.push(
122 + JSON.stringify({
123 + type: "message",
124 + id: entryId,
125 + parentId,
126 + timestamp: created,
127 + message: { role: "user", content: [{ type: "text", text }] },
128 + }),
129 + );
130 + parentId = entryId;
132 + if (name)
133 + lines.push(
134 + JSON.stringify({
135 + type: "session_info",
136 + id: randomUUID().slice(0, 8),
137 + parentId,
138 + timestamp: created,
139 + name,
140 + }),
141 + );
142 + return `${lines.join("\n")}\n`;
145 +export async function writeSession(
146 + directory: string,
147 + options: {
148 + id?: string;
149 + created?: string;
150 + cwd?: string;
151 + messages?: string[];
152 + name?: string;
153 + } = {},
154 +): Promise<SessionInfo> {
155 + const id = options.id ?? randomUUID();
156 + const created = options.created ?? new Date().toISOString();
157 + const path = join(directory, `${created.replace(/[:.]/g, "-")}_${id}.jsonl`);
158 + await writeFile(
159 + path,
160 + sessionBody(
161 + id,
162 + created,
163 + options.cwd ?? directory,
164 + options.messages ?? ["hello"],
165 + options.name,
166 + ),
167 + );
168 + return sessionInfoFromPath(path);
171 +export async function appendMessage(
172 + path: string,
173 + text: string,
174 +): Promise<SessionInfo> {
175 + const body = await readFile(path, "utf8");
176 + await writeFile(
177 + path,
178 + `${body}${JSON.stringify({ type: "message", role: "assistant", content: text })}\n`,
179 + );
180 + return sessionInfoFromPath(path);

extensions/bak/__tests__/s3.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { S3Store } from "../s3.js";
4 +const config = {
5 + endpoint: "https://example.invalid",
6 + bucket: "bucket",
7 + region: "auto",
8 + accessKeyId: "id",
9 + secretAccessKey: "secret",
10 +};
12 +function recorder(response: () => Response) {
13 + const calls: { url: string; init: RequestInit }[] = [];
14 + const client = {
15 + fetch: (input: RequestInfo | URL, init?: RequestInit) => {
16 + calls.push({ url: String(input), init: init ?? {} });
17 + return Promise.resolve(response());
18 + },
19 + } as unknown as Pick<import("aws4fetch").AwsClient, "fetch">;
20 + return { calls, client };
23 +function header(init: RequestInit, name: string): string | null {
24 + return new Headers(init.headers).get(name);
27 +describe("S3Store weak ETag handling", () => {
28 + it("requests identity encoding and strips weak validators on get", async () => {
29 + const { calls, client } = recorder(
30 + () =>
31 + new Response("body", {
32 + status: 200,
33 + headers: { etag: 'W/"abc"' },
34 + }),
35 + );
36 + const store = new S3Store(config, client);
38 + const object = await store.get("hosts/host.json");
40 + expect(object?.etag).toBe('"abc"');
41 + expect(header(calls[0]?.init ?? {}, "accept-encoding")).toBe("identity");
42 + });
44 + it("sends a strong validator when given a weak ifMatch", async () => {
45 + const { calls, client } = recorder(
46 + () => new Response(null, { status: 200, headers: { etag: 'W/"def"' } }),
47 + );
48 + const store = new S3Store(config, client);
50 + const etag = await store.put("hosts/host.json", "body", {
51 + contentType: "application/json",
52 + ifMatch: 'W/"abc"',
53 + });
55 + expect(header(calls[0]?.init ?? {}, "if-match")).toBe('"abc"');
56 + expect(etag).toBe('"def"');
57 + });
58 +});

extensions/bak/index.ts

Modified
93 93 const manager = ctx.sessionManager as typeof ctx.sessionManager & {
94 94 usesDefaultSessionDir?: () => boolean;
96 - return manager.usesDefaultSessionDir?.()
96 + // Non-persistent sessions report an empty directory; scanning it would read
97 + // the working directory instead of the session store.
98 + const directory = ctx.sessionManager.getSessionDir();
99 + return !directory || manager.usesDefaultSessionDir?.()
97 100 ? SessionManager.listAll()
98 - : SessionManager.listAll(ctx.sessionManager.getSessionDir());
101 + : SessionManager.listAll(directory);
101 104 export default function bakExtension(pi: ExtensionAPI) {

extensions/bak/mise.toml

Modified
1 +[tasks.e2e]
2 +description = "Run bak real S3 storage E2E"
3 +depends = ["//:install"]
4 +run = "npx vitest run --config vitest.e2e.config.ts"
1 6 [tasks.fmt]
2 7 description = "Format bak extension"
3 8 depends = ["//:install"]

extensions/bak/s3.ts

Modified
41 41 return status === 408 || status === 429 || status >= 500;
44 +function strongEtag(value: string | null): string | undefined {
45 + const etag = value?.trim().replace(/^W\//, "");
46 + return etag || undefined;
44 49 async function boundedError(response: Response): Promise<string> {
45 50 const text = (await response.text()).replace(/\s+/g, " ").trim();
46 51 return (
99 104 key: string,
100 105 options: { signal?: AbortSignal } = {},
101 106 ): Promise<StoredObject | undefined> {
102 - const response = await this.request(key, { method: "GET" }, options.signal);
107 + const response = await this.request(
108 + key,
109 + { method: "GET", headers: { "accept-encoding": "identity" } },
110 + options.signal,
111 + );
103 112 if (response.status === 404) return undefined;
104 113 if (!response.ok)
105 114 throw new S3Error(response.status, await boundedError(response));
106 115 return {
107 116 body: new Uint8Array(await response.arrayBuffer()),
108 - etag: response.headers.get("etag") ?? undefined,
117 + etag: strongEtag(response.headers.get("etag")),
122 131 const headers = new Headers({
123 132 "content-type": options.contentType ?? "application/octet-stream",
125 - if (options.ifMatch) headers.set("if-match", options.ifMatch);
134 + const ifMatch = strongEtag(options.ifMatch ?? null);
135 + if (ifMatch) headers.set("if-match", ifMatch);
126 136 if (options.ifNoneMatch) headers.set("if-none-match", options.ifNoneMatch);
127 137 const requestBody =
128 138 typeof body === "string" ? body : Uint8Array.from(body).buffer;
138 148 if (!response.ok)
139 149 throw new S3Error(response.status, await boundedError(response));
140 - return response.headers.get("etag") ?? undefined;
150 + return strongEtag(response.headers.get("etag"));

extensions/bak/vitest.e2e.config.ts

Added
new file mode 100644
1 +import { defineConfig } from "vitest/config";
3 +export default defineConfig({
4 + test: {
5 + include: ["__e2e__/**/*.test.ts"],
6 + exclude: ["node_modules/**"],
7 + fileParallelism: false,
8 + testTimeout: 120_000,
9 + hookTimeout: 120_000,
10 + },
11 +});