@narumitw/pi-codex-imagegen 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +126 -2
- package/docs/architecture.md +66 -0
- package/docs/protocol.md +64 -0
- package/docs/verification.md +65 -0
- package/docs/workflows.md +65 -0
- package/package.json +43 -4
- package/src/artifacts.ts +480 -0
- package/src/codex-imagegen.ts +408 -0
- package/src/image-client.ts +304 -0
- package/src/index.ts +1 -0
- package/src/menu.ts +132 -0
- package/src/output-path-input.ts +110 -0
- package/src/prompt-editor.ts +107 -0
- package/src/safe-input-render.ts +54 -0
- package/src/settings-screen.ts +163 -0
- package/src/settings.ts +211 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CURSOR_MARKER, renderFakeCursor } from "@earendil-works/pi-tui";
|
|
2
|
+
import { sanitizeTerminalText } from "@narumitw/pi-tui-kit";
|
|
3
|
+
|
|
4
|
+
type NativeInput = { focused: boolean; render(width: number): string[] };
|
|
5
|
+
const SAMPLE = "__imagegen_cursor_cell__";
|
|
6
|
+
const [FAKE_START, FAKE_END] = renderFakeCursor(SAMPLE).split(SAMPLE);
|
|
7
|
+
|
|
8
|
+
/** Find only the marker added by native focus, not marker-shaped draft text. */
|
|
9
|
+
function cursorInsertion(focused: string, unfocused: string): number | undefined {
|
|
10
|
+
if (!FAKE_START || focused.length !== unfocused.length + CURSOR_MARKER.length) return undefined;
|
|
11
|
+
let prefix = 0;
|
|
12
|
+
while (prefix < unfocused.length && focused[prefix] === unfocused[prefix]) prefix++;
|
|
13
|
+
let suffix = 0;
|
|
14
|
+
while (suffix < unfocused.length && focused[focused.length - 1 - suffix] === unfocused[unfocused.length - 1 - suffix])
|
|
15
|
+
suffix++;
|
|
16
|
+
// Prefix/suffix bounds prove removal restores the unfocused rendering. Keep
|
|
17
|
+
// this linear even when a pasted draft contains many marker-shaped strings.
|
|
18
|
+
const first = unfocused.length - suffix;
|
|
19
|
+
// Adjacent draft markers can also satisfy the insertion proof. The native
|
|
20
|
+
// marker is the one immediately before its fake cursor, not an earlier copy.
|
|
21
|
+
const index = focused.indexOf(CURSOR_MARKER + FAKE_START, first);
|
|
22
|
+
return index >= first && index <= prefix ? index : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Sanitize all native content, then reconstruct only proven native cursor metadata. */
|
|
26
|
+
export function renderSafeInput(input: NativeInput, width: number): string[] {
|
|
27
|
+
const focused = input.focused;
|
|
28
|
+
let withoutCursor: string[];
|
|
29
|
+
let withCursor: string[];
|
|
30
|
+
try {
|
|
31
|
+
// Public focus changes only cursor metadata in Pi Input/Editor. Rendering both
|
|
32
|
+
// states proves provenance without reading private cursor/draft fields or
|
|
33
|
+
// changing editing, undo, paste, mouse geometry, or submission state.
|
|
34
|
+
input.focused = false;
|
|
35
|
+
withoutCursor = input.render(width);
|
|
36
|
+
input.focused = true;
|
|
37
|
+
withCursor = input.render(width);
|
|
38
|
+
} finally {
|
|
39
|
+
input.focused = focused;
|
|
40
|
+
}
|
|
41
|
+
return withCursor.map((line, row) => {
|
|
42
|
+
const original = withoutCursor[row];
|
|
43
|
+
const cursor = original === undefined ? undefined : cursorInsertion(line, original);
|
|
44
|
+
if (cursor === undefined || !FAKE_START || !FAKE_END) return sanitizeTerminalText(line);
|
|
45
|
+
const afterMarker = line.slice(cursor + CURSOR_MARKER.length);
|
|
46
|
+
const end = afterMarker.indexOf(FAKE_END, FAKE_START.length);
|
|
47
|
+
// Fail closed if a future native renderer changes its cursor protocol.
|
|
48
|
+
if (!afterMarker.startsWith(FAKE_START) || end < 0) return sanitizeTerminalText(line);
|
|
49
|
+
const before = sanitizeTerminalText(line.slice(0, cursor));
|
|
50
|
+
const cell = sanitizeTerminalText(afterMarker.slice(FAKE_START.length, end));
|
|
51
|
+
const after = sanitizeTerminalText(afterMarker.slice(end + FAKE_END.length));
|
|
52
|
+
return `${before}${focused ? CURSOR_MARKER : ""}${renderFakeCursor(cell)}${after}`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from "node:util";
|
|
2
|
+
import { type ExtensionCommandContext, getSettingsListTheme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { isKeyRelease, matchesKey, type SettingItem, SettingsList } from "@earendil-works/pi-tui";
|
|
4
|
+
import {
|
|
5
|
+
HorizontalRule,
|
|
6
|
+
renderBoundedFrameLayout,
|
|
7
|
+
runCustomInteraction,
|
|
8
|
+
sanitizeTerminalText,
|
|
9
|
+
} from "@narumitw/pi-tui-kit";
|
|
10
|
+
import type { ImagegenMenuOwner } from "./menu.js";
|
|
11
|
+
import type { ImagegenSettings } from "./settings.js";
|
|
12
|
+
|
|
13
|
+
export async function showImagegenSettings(
|
|
14
|
+
ctx: ExtensionCommandContext,
|
|
15
|
+
owner: ImagegenMenuOwner,
|
|
16
|
+
): Promise<"back" | "close" | "stale"> {
|
|
17
|
+
const result = await runCustomInteraction<"back" | "close">(ctx, {
|
|
18
|
+
signal: owner.signal,
|
|
19
|
+
isCurrent: owner.isCurrent.bind(owner),
|
|
20
|
+
onError: () => {
|
|
21
|
+
if (!owner.signal.aborted && owner.isCurrent()) ctx.ui.notify("Image generation settings failed.", "error");
|
|
22
|
+
},
|
|
23
|
+
create: ({ tui, theme, signal, complete }) => {
|
|
24
|
+
let disposed = false;
|
|
25
|
+
let closing = false;
|
|
26
|
+
const finish = (reason: "back" | "close") => {
|
|
27
|
+
closing = true;
|
|
28
|
+
complete(reason);
|
|
29
|
+
};
|
|
30
|
+
let pending = Promise.resolve();
|
|
31
|
+
const counts = new Map<string, number>();
|
|
32
|
+
let rows = new Map<number, number>();
|
|
33
|
+
let width = 0;
|
|
34
|
+
let height = 0;
|
|
35
|
+
const current = () => !owner.signal.aborted && owner.isCurrent();
|
|
36
|
+
const clearGeometry = () => {
|
|
37
|
+
rows.clear();
|
|
38
|
+
width = 0;
|
|
39
|
+
height = 0;
|
|
40
|
+
};
|
|
41
|
+
const rule = new HorizontalRule({ ruleStyle: (text) => theme.fg("border", text) });
|
|
42
|
+
const settings = owner.getSettings();
|
|
43
|
+
const items: SettingItem[] = [
|
|
44
|
+
{
|
|
45
|
+
id: "toolEnabled",
|
|
46
|
+
label: "Tool access",
|
|
47
|
+
currentValue: String(settings.toolEnabled),
|
|
48
|
+
values: ["false", "true"],
|
|
49
|
+
description:
|
|
50
|
+
"Allow model access at an idle boundary. Manual generation stays available. This changes model-visible tool definitions.",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
id: "transparentBackground",
|
|
54
|
+
label: "Transparent background",
|
|
55
|
+
currentValue: String(settings.transparentBackground),
|
|
56
|
+
values: ["false", "true"],
|
|
57
|
+
description: "Request a transparent background by default. The provider determines actual output support.",
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
// Retain the native control and theme contract, but use the callback theme.
|
|
61
|
+
// Hide native hard-coded key hints: they are not valid for remapped bindings.
|
|
62
|
+
const list = new SettingsList(
|
|
63
|
+
items,
|
|
64
|
+
items.length,
|
|
65
|
+
{
|
|
66
|
+
...getSettingsListTheme(),
|
|
67
|
+
label: (text, selected) => (selected ? theme.fg("accent", text) : text),
|
|
68
|
+
value: (text, selected) => theme.fg(selected ? "accent" : "muted", text),
|
|
69
|
+
description: (text) => theme.fg("muted", text),
|
|
70
|
+
get cursor() {
|
|
71
|
+
return theme.fg("accent", "→ ");
|
|
72
|
+
},
|
|
73
|
+
hint: () => "",
|
|
74
|
+
},
|
|
75
|
+
(id, value) => queue(id as keyof ImagegenSettings, value),
|
|
76
|
+
() => finish("back"),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
function queue(id: keyof ImagegenSettings, value: string): void {
|
|
80
|
+
const patch: Partial<ImagegenSettings> = { [id]: value === "true" };
|
|
81
|
+
list.updateValue(id, sanitizeTerminalText(value));
|
|
82
|
+
counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
83
|
+
// Register in action order, and recover the queue after each rejected save.
|
|
84
|
+
pending = pending
|
|
85
|
+
.then(async () => {
|
|
86
|
+
if (!current() || (disposed && !closing)) return;
|
|
87
|
+
try {
|
|
88
|
+
await owner.saveSettings(patch);
|
|
89
|
+
} catch {
|
|
90
|
+
if (current() && (!disposed || closing))
|
|
91
|
+
ctx.ui.notify("Settings were not saved. The previous value was restored.", "error");
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
.finally(() => {
|
|
95
|
+
const remaining = (counts.get(id) ?? 1) - 1;
|
|
96
|
+
counts.set(id, remaining);
|
|
97
|
+
if (current() && !disposed && remaining === 0) {
|
|
98
|
+
list.updateValue(id, sanitizeTerminalText(String(owner.getSettings()[id])));
|
|
99
|
+
list.invalidate();
|
|
100
|
+
clearGeometry();
|
|
101
|
+
tui.requestRender();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
render(availableWidth) {
|
|
108
|
+
const content = list.render(Math.max(1, availableWidth));
|
|
109
|
+
const focusedRow = Math.max(
|
|
110
|
+
0,
|
|
111
|
+
content.findIndex((line) => stripVTControlCharacters(line).startsWith("→")),
|
|
112
|
+
);
|
|
113
|
+
const terminalRows = Number.isFinite(tui.terminal.rows) ? Math.floor(tui.terminal.rows) : 24;
|
|
114
|
+
const layout = renderBoundedFrameLayout({
|
|
115
|
+
width: availableWidth,
|
|
116
|
+
maxRows: Math.max(1, terminalRows - 3),
|
|
117
|
+
rule: rule.render(availableWidth)[0] ?? "",
|
|
118
|
+
title: [theme.fg("accent", "Image Generation Settings")],
|
|
119
|
+
context: [
|
|
120
|
+
theme.fg("muted", `User scope: ${sanitizeTerminalText(owner.settingsPath)}`),
|
|
121
|
+
theme.fg("muted", "Changes save immediately. Closing does not undo saved changes."),
|
|
122
|
+
],
|
|
123
|
+
content,
|
|
124
|
+
focusedRow,
|
|
125
|
+
priorityRows: [focusedRow],
|
|
126
|
+
});
|
|
127
|
+
width = Math.max(0, Math.floor(availableWidth));
|
|
128
|
+
height = layout.lines.length;
|
|
129
|
+
rows = new Map(layout.contentRows.map(({ frameIndex, contentIndex }) => [frameIndex, contentIndex]));
|
|
130
|
+
return layout.lines;
|
|
131
|
+
},
|
|
132
|
+
handleInput(data) {
|
|
133
|
+
if (signal.aborted || isKeyRelease(data)) return;
|
|
134
|
+
clearGeometry();
|
|
135
|
+
if (matchesKey(data, "ctrl+c")) finish("close");
|
|
136
|
+
else list.handleInput(data);
|
|
137
|
+
tui.requestRender();
|
|
138
|
+
},
|
|
139
|
+
handleMouse(event) {
|
|
140
|
+
if (signal.aborted || event.x < 0 || event.x >= width || event.y < 0 || event.y >= height) return undefined;
|
|
141
|
+
const y = rows.get(event.y);
|
|
142
|
+
if (y === undefined) return undefined;
|
|
143
|
+
const handled = list.handleMouse({ ...event, y, height: list.render(width).length });
|
|
144
|
+
// Pi dispatches release and its synthesized click before the next render.
|
|
145
|
+
// Retain the last published geometry until a real layout change.
|
|
146
|
+
if (handled) tui.requestRender();
|
|
147
|
+
return handled;
|
|
148
|
+
},
|
|
149
|
+
invalidate() {
|
|
150
|
+
list.invalidate();
|
|
151
|
+
rule.invalidate();
|
|
152
|
+
clearGeometry();
|
|
153
|
+
},
|
|
154
|
+
waitForPending: () => pending,
|
|
155
|
+
dispose() {
|
|
156
|
+
disposed = true;
|
|
157
|
+
clearGeometry();
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
return result.kind === "completed" ? result.value : "stale";
|
|
163
|
+
}
|
package/src/settings.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
export interface ImagegenSettings {
|
|
7
|
+
toolEnabled: boolean;
|
|
8
|
+
transparentBackground: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function defaultSettings(): ImagegenSettings {
|
|
12
|
+
return {
|
|
13
|
+
toolEnabled: false,
|
|
14
|
+
transparentBackground: false,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type SaveOptions = {
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
isCurrent?: () => boolean;
|
|
21
|
+
// Must be synchronous and reversible, including after a partial failure.
|
|
22
|
+
apply?: (next: ImagegenSettings, previous: ImagegenSettings) => void;
|
|
23
|
+
};
|
|
24
|
+
type Document = Record<string, unknown>;
|
|
25
|
+
const keys = ["toolEnabled", "transparentBackground"] as const;
|
|
26
|
+
|
|
27
|
+
class SettingsError extends Error {}
|
|
28
|
+
|
|
29
|
+
function object(value: unknown): value is Document {
|
|
30
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function validate(document: Document): void {
|
|
34
|
+
for (const key of keys) {
|
|
35
|
+
if (!Object.hasOwn(document, key)) continue;
|
|
36
|
+
const value = document[key];
|
|
37
|
+
if (typeof value !== "boolean")
|
|
38
|
+
throw new SettingsError("Invalid image generation settings. Repair the file before saving.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function check(options: SaveOptions): void {
|
|
43
|
+
if (options.signal?.aborted || (options.isCurrent && !options.isCurrent()))
|
|
44
|
+
throw new SettingsError("Settings change cancelled.");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function readDocument(path: string): Promise<{ document: Document; bytes?: Buffer }> {
|
|
48
|
+
let bytes: Buffer;
|
|
49
|
+
try {
|
|
50
|
+
bytes = await fs.readFile(path);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (object(error) && error.code === "ENOENT") return { document: {} };
|
|
53
|
+
throw new SettingsError("Settings cannot be read. Repair the file before saving.");
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const document: unknown = JSON.parse(bytes.toString("utf8"));
|
|
57
|
+
if (!object(document)) throw new Error();
|
|
58
|
+
validate(document);
|
|
59
|
+
return { document, bytes };
|
|
60
|
+
} catch {
|
|
61
|
+
throw new SettingsError("Invalid image generation settings. Repair the file before saving.");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function applySynchronously(apply: SaveOptions["apply"], next: ImagegenSettings, previous: ImagegenSettings) {
|
|
66
|
+
// Copies prevent a callback from changing the store or the rollback snapshot.
|
|
67
|
+
const result: unknown = apply?.({ ...next }, { ...previous });
|
|
68
|
+
if (object(result) && typeof result.then === "function") {
|
|
69
|
+
void Promise.resolve(result).catch(() => {});
|
|
70
|
+
throw new SettingsError("Settings application must be synchronous.");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** User-only settings with in-process ordering; this is not a cross-process lock. */
|
|
75
|
+
export class SettingsStore {
|
|
76
|
+
readonly path: string;
|
|
77
|
+
private current: ImagegenSettings;
|
|
78
|
+
private readonly defaults: ImagegenSettings;
|
|
79
|
+
private tail: Promise<unknown> = Promise.resolve();
|
|
80
|
+
|
|
81
|
+
constructor(agentDir: string) {
|
|
82
|
+
this.path = join(agentDir, "pi-codex-imagegen.json");
|
|
83
|
+
this.defaults = defaultSettings();
|
|
84
|
+
this.current = { ...this.defaults };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get settings(): ImagegenSettings {
|
|
88
|
+
return { ...this.current };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private effective(document: Document): ImagegenSettings {
|
|
92
|
+
const next = { ...this.defaults };
|
|
93
|
+
for (const key of keys) {
|
|
94
|
+
if (Object.hasOwn(document, key)) Object.assign(next, { [key]: document[key] });
|
|
95
|
+
}
|
|
96
|
+
return next;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private enqueue<T>(operation: () => Promise<T>): Promise<T> {
|
|
100
|
+
// Register immediately: a per-instance deferred registration can reorder other stores' requests.
|
|
101
|
+
const task = withFileMutationQueue(this.path, operation).catch((error: unknown) => {
|
|
102
|
+
if (error instanceof SettingsError) throw error;
|
|
103
|
+
throw new SettingsError("Settings operation failed. Check file permissions and available disk space.");
|
|
104
|
+
});
|
|
105
|
+
// A queue-registration failure can settle before an earlier transaction.
|
|
106
|
+
this.tail = Promise.all([this.tail, task.catch(() => {})]);
|
|
107
|
+
return task;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
load(): Promise<{ settings: ImagegenSettings; warning?: string }> {
|
|
111
|
+
return this.enqueue(async () => {
|
|
112
|
+
try {
|
|
113
|
+
const { document } = await readDocument(this.path);
|
|
114
|
+
this.current = this.effective(document);
|
|
115
|
+
return { settings: this.settings };
|
|
116
|
+
} catch {
|
|
117
|
+
this.current = { ...this.defaults };
|
|
118
|
+
return {
|
|
119
|
+
settings: this.settings,
|
|
120
|
+
warning: "Image generation settings could not be loaded. Repair the file before saving.",
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
save(patch: Partial<ImagegenSettings>, options: SaveOptions = {}): Promise<ImagegenSettings> {
|
|
127
|
+
// Capture user action values, not a mutable object referenced by a queued task.
|
|
128
|
+
const captured = { ...patch };
|
|
129
|
+
return this.enqueue(async () => {
|
|
130
|
+
check(options);
|
|
131
|
+
if (!object(patch) || Object.keys(captured).some((key) => !keys.some((owned) => owned === key)))
|
|
132
|
+
throw new SettingsError("Invalid image generation settings change.");
|
|
133
|
+
validate(captured);
|
|
134
|
+
const { document, bytes } = await readDocument(this.path);
|
|
135
|
+
check(options);
|
|
136
|
+
const previous = this.settings;
|
|
137
|
+
const nextDocument = { ...document, ...captured };
|
|
138
|
+
const next = this.effective(nextDocument);
|
|
139
|
+
const owned = new Set<string>();
|
|
140
|
+
let published = false;
|
|
141
|
+
let applyAttempted = false;
|
|
142
|
+
const temporary = join(dirname(this.path), `.pi-codex-imagegen-${randomUUID()}.tmp`);
|
|
143
|
+
const backup = join(dirname(this.path), `.pi-codex-imagegen-${randomUUID()}.tmp`);
|
|
144
|
+
const stage = async (path: string, content: string | Buffer) => {
|
|
145
|
+
check(options);
|
|
146
|
+
const handle = await fs.open(path, "wx", 0o600);
|
|
147
|
+
owned.add(path);
|
|
148
|
+
try {
|
|
149
|
+
check(options);
|
|
150
|
+
await handle.writeFile(content);
|
|
151
|
+
check(options);
|
|
152
|
+
} finally {
|
|
153
|
+
await handle.close();
|
|
154
|
+
}
|
|
155
|
+
check(options);
|
|
156
|
+
};
|
|
157
|
+
try {
|
|
158
|
+
await fs.mkdir(dirname(this.path), { recursive: true, mode: 0o700 });
|
|
159
|
+
check(options);
|
|
160
|
+
await stage(temporary, `${JSON.stringify(nextDocument, null, 2)}\n`);
|
|
161
|
+
check(options);
|
|
162
|
+
if (bytes !== undefined) {
|
|
163
|
+
await stage(backup, bytes);
|
|
164
|
+
check(options);
|
|
165
|
+
}
|
|
166
|
+
await fs.rename(temporary, this.path);
|
|
167
|
+
published = true;
|
|
168
|
+
owned.delete(temporary);
|
|
169
|
+
check(options);
|
|
170
|
+
applyAttempted = true;
|
|
171
|
+
applySynchronously(options.apply, next, previous);
|
|
172
|
+
check(options);
|
|
173
|
+
// No await between runtime application and effective-state publication.
|
|
174
|
+
this.current = next;
|
|
175
|
+
return this.settings;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
let rollbackFailed = false;
|
|
178
|
+
if (applyAttempted) {
|
|
179
|
+
try {
|
|
180
|
+
applySynchronously(options.apply, previous, next);
|
|
181
|
+
} catch {
|
|
182
|
+
rollbackFailed = true;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
this.current = previous;
|
|
186
|
+
// Rollback must finish even when the request no longer owns the session.
|
|
187
|
+
if (published) {
|
|
188
|
+
try {
|
|
189
|
+
if (bytes === undefined) await fs.rm(this.path);
|
|
190
|
+
else {
|
|
191
|
+
await fs.rename(backup, this.path);
|
|
192
|
+
owned.delete(backup);
|
|
193
|
+
}
|
|
194
|
+
} catch {
|
|
195
|
+
rollbackFailed = true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (rollbackFailed) throw new SettingsError("Settings rollback failed. Reload and inspect the settings file.");
|
|
199
|
+
if (error instanceof SettingsError) throw error;
|
|
200
|
+
throw new SettingsError("Settings were not saved. Check file permissions and runtime application.");
|
|
201
|
+
} finally {
|
|
202
|
+
// This cleanup does not publish state and must also run after cancellation.
|
|
203
|
+
await Promise.all([...owned].map((path) => fs.rm(path, { force: true }).catch(() => {})));
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async flush(): Promise<void> {
|
|
209
|
+
await this.tail;
|
|
210
|
+
}
|
|
211
|
+
}
|