Skip to content

Commit 8c0aaa0

Browse files
committed
fix(skills): sync proposal generation files
1 parent 8661aec commit 8c0aaa0

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

‎src/skills/workshop/proposal-generation.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ export async function stageSkillProposalGeneration(params: {
6060
const generationDir = path.join(generationsDir, generationId);
6161
try {
6262
await stateRoot.mkdir(stagingDir);
63-
await stateRoot.create(path.join(stagingDir, PROPOSAL_DRAFT_FILE), params.content, {
64-
encoding: "utf8",
65-
});
63+
await createDurableGenerationFile(
64+
stateRoot,
65+
path.join(stagingDir, PROPOSAL_DRAFT_FILE),
66+
params.content,
67+
);
6668
for (const file of params.supportFiles ?? []) {
67-
await stateRoot.create(path.join(stagingDir, file.path), file.content, {
68-
encoding: "utf8",
69-
mkdir: true,
70-
});
69+
await createDurableGenerationFile(stateRoot, path.join(stagingDir, file.path), file.content);
7170
}
7271
// The record only names the destination after this same-filesystem move, so
7372
// readers can never observe a partially populated generation.
@@ -80,6 +79,20 @@ export async function stageSkillProposalGeneration(params: {
8079
}
8180
}
8281

82+
async function createDurableGenerationFile(
83+
stateRoot: Awaited<ReturnType<typeof root>>,
84+
relativePath: string,
85+
content: string,
86+
): Promise<void> {
87+
await stateRoot.create(relativePath, content, { encoding: "utf8", mkdir: true });
88+
const opened = await stateRoot.openWritable(relativePath, { writeMode: "update" });
89+
try {
90+
await opened.handle.sync();
91+
} finally {
92+
await opened.handle.close();
93+
}
94+
}
95+
8396
export async function discardSkillProposalGeneration(
8497
record: SkillProposalRecord,
8598
store?: SkillWorkshopStoreOptions,

‎src/skills/workshop/revision-atomicity.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ vi.mock("../../infra/fs-safe.js", async (importOriginal) => {
4242
revisionFault.trip("generation exclusive create");
4343
};
4444
}
45+
if (property === "openWritable") {
46+
return async (...openArgs: Parameters<typeof target.openWritable>) => {
47+
const writable = await target.openWritable(...openArgs);
48+
const handle = new Proxy(writable.handle, {
49+
get(handleTarget, handleProperty, handleReceiver) {
50+
if (handleProperty === "sync") {
51+
return async () => {
52+
await handleTarget.sync();
53+
revisionFault.trip("generation file durability sync");
54+
};
55+
}
56+
const handleValue = Reflect.get(
57+
handleTarget,
58+
handleProperty,
59+
handleReceiver,
60+
) as unknown;
61+
return typeof handleValue === "function"
62+
? handleValue.bind(handleTarget)
63+
: handleValue;
64+
},
65+
});
66+
return { ...writable, handle };
67+
};
68+
}
4569
if (property === "move") {
4670
return async (...moveArgs: Parameters<typeof target.move>) => {
4771
await target.move(...moveArgs);
@@ -192,6 +216,7 @@ describe("Skill Workshop revision generation atomicity", () => {
192216
it.each([
193217
"generation staging start",
194218
"generation exclusive create",
219+
"generation file durability sync",
195220
"generation finalization",
196221
"generation parent durability sync",
197222
"CAS before commit",

0 commit comments

Comments
 (0)