@fungi.computer/darkroom 0.11.11 → 0.11.13
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/README.md +13 -0
- package/examples/standalone/README.md +52 -0
- package/examples/standalone/worker.ts +87 -0
- package/examples/standalone/wrangler.jsonc +26 -0
- package/package.json +8 -2
- package/src/app-media-contracts.ts +142 -0
- package/src/audio-providers/elevenlabs.ts +108 -0
- package/src/audio-reference-types.ts +47 -0
- package/src/audio-references.ts +107 -0
- package/src/browser-failure.ts +73 -0
- package/src/browser.ts +7 -0
- package/src/cloudflare/provider-reason.ts +89 -0
- package/src/cloudflare/provider.ts +405 -0
- package/src/cloudflare/r2-destination.ts +130 -0
- package/src/cloudflare/reference-images.ts +193 -0
- package/src/cloudflare.ts +38 -0
- package/src/cost-estimate.ts +129 -0
- package/src/daily-media-meter.ts +163 -0
- package/src/effect.ts +2 -0
- package/src/execution-error.ts +98 -0
- package/src/failure.ts +13 -0
- package/src/gateway-models.ts +304 -0
- package/src/gateway-wire.ts +36 -0
- package/src/index.ts +1454 -0
- package/src/media-binding.ts +260 -0
- package/src/media-content-type.ts +60 -0
- package/src/media-generation.ts +171 -0
- package/src/media-operation-contract.ts +173 -0
- package/src/media-operation-do.ts +257 -0
- package/src/media-operation-owner.ts +1786 -0
- package/src/media-operation-storage.ts +110 -0
- package/src/media-operation.ts +454 -0
- package/src/media-path.ts +8 -0
- package/src/media-quote.ts +34 -0
- package/src/media-run-lifetime.ts +5 -0
- package/src/media-written.ts +38 -0
- package/src/model-catalog.ts +1574 -0
- package/src/neurons.ts +6 -0
- package/src/node-webpmux.d.ts +12 -0
- package/src/provenance-bytes.ts +22 -0
- package/src/provenance-export.ts +126 -0
- package/src/provenance-jpeg.ts +83 -0
- package/src/provenance-png.ts +26 -0
- package/src/provenance-signed.ts +7 -0
- package/src/provenance-xmp.ts +57 -0
- package/src/provenance.ts +87 -0
- package/src/reference-image-size.ts +67 -0
- package/src/reference-read.ts +50 -0
- package/src/server.ts +711 -0
- package/src/video-providers/index.ts +18 -0
- package/src/video-providers/minimax.ts +96 -0
- package/src/video-providers/reference-encoding.ts +32 -0
- package/src/video-providers/types.ts +47 -0
- package/src/video-providers/unified.ts +68 -0
- package/test/audio-models.test.ts +428 -0
- package/test/audio-references.test.ts +428 -0
- package/test/custom-catalogue.test.ts +257 -0
- package/test/daily-media-meter.test.ts +287 -0
- package/test/failure.test.ts +190 -0
- package/test/fixtures/cloudflare-workers.fixture.ts +10 -0
- package/test/fixtures/media-operation.fixture.ts +134 -0
- package/test/fixtures/media-team-prompts.ts +7 -0
- package/test/fixtures/native-sqlite.fixture.ts +40 -0
- package/test/gateway-models.test.ts +712 -0
- package/test/media-operation-do.test.ts +1565 -0
- package/test/media-quote.test.ts +81 -0
- package/test/media-retention.test.ts +597 -0
- package/test/media-service.test.ts +549 -0
- package/test/media-team-prompts.test.ts +92 -0
- package/test/model-facts.test.ts +126 -0
- package/test/prompt-limit.test.ts +53 -0
- package/test/provenance-export.test.ts +144 -0
- package/test/provenance-images.fixture.mjs +17 -0
- package/test/provenance.test.ts +179 -0
- package/test/provider-gateway.test.ts +114 -0
- package/test/provider-reasons.test.ts +133 -0
- package/test/r2-destination.test.ts +166 -0
- package/test/reference-image-size.test.ts +73 -0
- package/test/reference-images-billing.test.ts +40 -0
- package/test/reference-images.test.ts +399 -0
- package/test/standalone.cloudflare.test.mjs +298 -0
- package/test/standalone.worker.mjs +70 -0
- package/test/transform-models.test.ts +400 -0
- package/test/video-capabilities.test.ts +160 -0
- package/test/video-contract.test.ts +533 -0
- package/test/video-failure-lifecycle.test.ts +177 -0
- package/test/video-provider.test.ts +136 -0
- package/test/video-wire.test.ts +251 -0
- package/vitest.cloudflare.config.ts +22 -0
- package/vitest.node.config.ts +14 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
MediaSettingsError,
|
|
4
|
+
validateMediaGenerationSettings,
|
|
5
|
+
} from "../src/index.js";
|
|
6
|
+
import { estimate } from "../src/cloudflare/provider.js";
|
|
7
|
+
import { mediaUnits } from "../src/gateway-models.js";
|
|
8
|
+
import { quoteMediaGeneration } from "../src/media-quote.js";
|
|
9
|
+
|
|
10
|
+
const noReferences = {
|
|
11
|
+
read: () => Promise.reject(new Error("no reference expected")),
|
|
12
|
+
};
|
|
13
|
+
const png = (width: number, height: number) => ({
|
|
14
|
+
bytes: new Uint8Array([1]),
|
|
15
|
+
format: "png" as const,
|
|
16
|
+
width,
|
|
17
|
+
height,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("quoteMediaGeneration", () => {
|
|
21
|
+
it("quotes a video as its admission estimate and billed seconds", async () => {
|
|
22
|
+
const raw = {
|
|
23
|
+
model: "bytedance/seedance-2.0",
|
|
24
|
+
prompt: "fern fronds unfurling",
|
|
25
|
+
width: 1280,
|
|
26
|
+
height: 720,
|
|
27
|
+
durationSeconds: 5,
|
|
28
|
+
generateAudio: true,
|
|
29
|
+
};
|
|
30
|
+
const quote = await quoteMediaGeneration(raw, noReferences);
|
|
31
|
+
const settings = validateMediaGenerationSettings(raw);
|
|
32
|
+
expect(quote).toEqual({
|
|
33
|
+
unit: "video_second",
|
|
34
|
+
units: 5,
|
|
35
|
+
providerCostMilliunits: estimate(settings, []),
|
|
36
|
+
});
|
|
37
|
+
expect(quote.providerCostMilliunits).toBeGreaterThan(0);
|
|
38
|
+
const longer = await quoteMediaGeneration(
|
|
39
|
+
{ ...raw, durationSeconds: 10 },
|
|
40
|
+
noReferences,
|
|
41
|
+
);
|
|
42
|
+
expect(longer.units).toBe(10);
|
|
43
|
+
expect(longer.providerCostMilliunits).toBeGreaterThan(
|
|
44
|
+
quote.providerCostMilliunits,
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("reads references so their size counts, as admission does", async () => {
|
|
49
|
+
const raw = {
|
|
50
|
+
model: "openai/gpt-image-2",
|
|
51
|
+
prompt: "a seed catalogue cover",
|
|
52
|
+
width: 1024,
|
|
53
|
+
height: 1024,
|
|
54
|
+
references: [{ path: "/refs/a.png", computerId: "c" }],
|
|
55
|
+
};
|
|
56
|
+
const quote = await quoteMediaGeneration(raw, {
|
|
57
|
+
read: () => Promise.resolve(png(1024, 1024)),
|
|
58
|
+
});
|
|
59
|
+
const settings = validateMediaGenerationSettings(raw);
|
|
60
|
+
expect(quote).toEqual({
|
|
61
|
+
...mediaUnits(settings),
|
|
62
|
+
providerCostMilliunits: estimate(settings, [{ ...png(1024, 1024) }]),
|
|
63
|
+
});
|
|
64
|
+
const bare = await quoteMediaGeneration(
|
|
65
|
+
{ ...raw, references: undefined },
|
|
66
|
+
noReferences,
|
|
67
|
+
);
|
|
68
|
+
expect(quote.providerCostMilliunits).toBeGreaterThan(
|
|
69
|
+
bare.providerCostMilliunits,
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("refuses settings admission would refuse", async () => {
|
|
74
|
+
await expect(
|
|
75
|
+
quoteMediaGeneration(
|
|
76
|
+
{ model: "openai/gpt-image-2", prompt: "x", width: 7, height: 7 },
|
|
77
|
+
noReferences,
|
|
78
|
+
),
|
|
79
|
+
).rejects.toBeInstanceOf(MediaSettingsError);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import sharp from "sharp";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { executeMediaOperation } from "../src/media-operation.js";
|
|
5
|
+
import {
|
|
6
|
+
MediaOperationOwner,
|
|
7
|
+
type MediaOperationCoordinates,
|
|
8
|
+
type MediaOperationRequest,
|
|
9
|
+
} from "../src/media-operation-owner.js";
|
|
10
|
+
import {
|
|
11
|
+
eraseMediaOperation,
|
|
12
|
+
readRetainedMedia,
|
|
13
|
+
retainedMedia,
|
|
14
|
+
removeMediaIfWritten,
|
|
15
|
+
type MediaRemoval,
|
|
16
|
+
type MediaServiceHost,
|
|
17
|
+
} from "../src/server.js";
|
|
18
|
+
import { mediaOperationFixture as fixture } from "./fixtures/media-operation.fixture.js";
|
|
19
|
+
|
|
20
|
+
const coordinates: MediaOperationCoordinates = {
|
|
21
|
+
namespace: "team-a",
|
|
22
|
+
owner: "installation-a",
|
|
23
|
+
};
|
|
24
|
+
const request: MediaOperationRequest = {
|
|
25
|
+
model: "@cf/black-forest-labs/flux-2-klein-4b",
|
|
26
|
+
prompt: "a private greenhouse",
|
|
27
|
+
width: 1_024,
|
|
28
|
+
height: 768,
|
|
29
|
+
computerId: "computer-a",
|
|
30
|
+
path: "generated/greenhouse.png",
|
|
31
|
+
};
|
|
32
|
+
const outputKey = "media-output/v1/media:request-a";
|
|
33
|
+
const generated = new Uint8Array([1, 2, 3]);
|
|
34
|
+
|
|
35
|
+
function make(outputs = new Map<string, Uint8Array>()) {
|
|
36
|
+
return fixture(":memory:", outputs, undefined, "media:request-a");
|
|
37
|
+
}
|
|
38
|
+
function admit(owner: MediaOperationOwner) {
|
|
39
|
+
return owner.admit("actor-a", coordinates, "request-a", request);
|
|
40
|
+
}
|
|
41
|
+
async function generating(owner: MediaOperationOwner) {
|
|
42
|
+
admit(owner);
|
|
43
|
+
const claimed = await owner.claim();
|
|
44
|
+
if (claimed.leaseId === undefined) throw new Error("claim failed");
|
|
45
|
+
owner.recordEstimate(claimed.leaseId, 125);
|
|
46
|
+
return claimed.leaseId;
|
|
47
|
+
}
|
|
48
|
+
async function image(format: "png" | "jpeg" | "webp") {
|
|
49
|
+
return new Uint8Array(
|
|
50
|
+
await sharp({
|
|
51
|
+
create: {
|
|
52
|
+
width: 16,
|
|
53
|
+
height: 16,
|
|
54
|
+
channels: 3,
|
|
55
|
+
background: { r: 40, g: 90, b: 140 },
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
.toFormat(format)
|
|
59
|
+
.toBuffer(),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
function run(
|
|
63
|
+
owner: MediaOperationOwner,
|
|
64
|
+
bytes: Uint8Array,
|
|
65
|
+
format: string,
|
|
66
|
+
extra: {
|
|
67
|
+
embedPrompt?: boolean | undefined;
|
|
68
|
+
metering?: MediaServiceHost["metering"];
|
|
69
|
+
write?: (bytes: Uint8Array) => Promise<void>;
|
|
70
|
+
remove?: ((sha256: string) => Promise<MediaRemoval>) | undefined;
|
|
71
|
+
},
|
|
72
|
+
) {
|
|
73
|
+
return Effect.runPromise(
|
|
74
|
+
executeMediaOperation({
|
|
75
|
+
operation: owner,
|
|
76
|
+
actorUserId: "actor-a",
|
|
77
|
+
coordinates,
|
|
78
|
+
requestId: "request-a",
|
|
79
|
+
request,
|
|
80
|
+
metering: extra.metering ?? "unmetered",
|
|
81
|
+
embedPrompt: extra.embedPrompt,
|
|
82
|
+
destination: {
|
|
83
|
+
authorize: () => Promise.resolve(),
|
|
84
|
+
write: extra.write ?? (() => Promise.resolve()),
|
|
85
|
+
remove: extra.remove ?? (() => Promise.resolve("absent")),
|
|
86
|
+
},
|
|
87
|
+
generation: {
|
|
88
|
+
prepare: () =>
|
|
89
|
+
Promise.resolve({
|
|
90
|
+
costMilliunits: 125,
|
|
91
|
+
references: [],
|
|
92
|
+
audioReferences: [],
|
|
93
|
+
run: () => Promise.resolve({ bytes, format }),
|
|
94
|
+
}),
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
describe("private prompt metadata through a generation", () => {
|
|
101
|
+
for (const format of ["png", "jpeg", "webp"] as const) {
|
|
102
|
+
it(`${format}: saved file omits the prompt by default and carries it on opt-in`, async () => {
|
|
103
|
+
for (const embedPrompt of [undefined, false, true]) {
|
|
104
|
+
const test = make();
|
|
105
|
+
try {
|
|
106
|
+
const saved: Uint8Array[] = [];
|
|
107
|
+
await run(test.owner, await image(format), format, {
|
|
108
|
+
embedPrompt,
|
|
109
|
+
write: (bytes) => {
|
|
110
|
+
saved.push(bytes);
|
|
111
|
+
return Promise.resolve();
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
const xmp = new TextDecoder().decode(
|
|
115
|
+
(await sharp(saved[0]).metadata()).xmp,
|
|
116
|
+
);
|
|
117
|
+
expect(xmp).toContain("fungi:printId");
|
|
118
|
+
if (embedPrompt === true) {
|
|
119
|
+
expect(xmp).toContain("fungi:prompt");
|
|
120
|
+
expect(xmp).toContain("a private greenhouse");
|
|
121
|
+
} else {
|
|
122
|
+
expect(xmp).not.toContain("fungi:prompt");
|
|
123
|
+
expect(xmp).not.toContain("private greenhouse");
|
|
124
|
+
}
|
|
125
|
+
} finally {
|
|
126
|
+
test.close();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
function host(
|
|
134
|
+
test: ReturnType<typeof make>,
|
|
135
|
+
files: Map<string, Uint8Array>,
|
|
136
|
+
metering: MediaServiceHost["metering"] = "unmetered",
|
|
137
|
+
namespace = "team-a",
|
|
138
|
+
) {
|
|
139
|
+
return {
|
|
140
|
+
scope: {
|
|
141
|
+
actorId: "actor-a",
|
|
142
|
+
namespace,
|
|
143
|
+
owner: "installation-a",
|
|
144
|
+
destinationId: "computer-a",
|
|
145
|
+
},
|
|
146
|
+
operations: { getByName: () => test.owner },
|
|
147
|
+
metering,
|
|
148
|
+
destination: {
|
|
149
|
+
authorize: () => Promise.resolve(),
|
|
150
|
+
write: (path: string, bytes: Uint8Array) => {
|
|
151
|
+
files.set(path, bytes);
|
|
152
|
+
return Promise.resolve();
|
|
153
|
+
},
|
|
154
|
+
remove: (path: string, written: { sha256: string }) =>
|
|
155
|
+
removeMediaIfWritten(
|
|
156
|
+
{
|
|
157
|
+
read: () => Promise.resolve(files.get(path) ?? null),
|
|
158
|
+
delete: () => {
|
|
159
|
+
files.delete(path);
|
|
160
|
+
return Promise.resolve();
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
written.sha256,
|
|
164
|
+
),
|
|
165
|
+
},
|
|
166
|
+
} satisfies Pick<
|
|
167
|
+
MediaServiceHost,
|
|
168
|
+
"scope" | "operations" | "destination" | "metering"
|
|
169
|
+
>;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
describe("erasing retained results", () => {
|
|
173
|
+
beforeEach(() => {
|
|
174
|
+
vi.useFakeTimers();
|
|
175
|
+
vi.setSystemTime(1_000);
|
|
176
|
+
});
|
|
177
|
+
afterEach(() => vi.useRealTimers());
|
|
178
|
+
|
|
179
|
+
it("erases an operation that is still generating and refuses its later output", async () => {
|
|
180
|
+
const outputs = new Map<string, Uint8Array>();
|
|
181
|
+
const test = make(outputs);
|
|
182
|
+
try {
|
|
183
|
+
const lease = await generating(test.owner);
|
|
184
|
+
expect(await test.owner.erase("team-a", "request-a")).toEqual({
|
|
185
|
+
clear: true,
|
|
186
|
+
destinationPath: "generated/greenhouse.png",
|
|
187
|
+
writtenSha256: null,
|
|
188
|
+
abandonedGeneration: true,
|
|
189
|
+
});
|
|
190
|
+
await expect(
|
|
191
|
+
test.owner.recordGenerated(lease, generated, "png"),
|
|
192
|
+
).rejects.toThrow("not_found");
|
|
193
|
+
expect(() => {
|
|
194
|
+
test.owner.recordWriteIntent(lease, "a".repeat(64));
|
|
195
|
+
}).toThrow("not_found");
|
|
196
|
+
expect(outputs.size).toBe(0);
|
|
197
|
+
} finally {
|
|
198
|
+
test.close();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("keeps the write digest through recovery of an expired writing lease", async () => {
|
|
203
|
+
const test = make();
|
|
204
|
+
try {
|
|
205
|
+
const lease = await generating(test.owner);
|
|
206
|
+
await test.owner.recordGenerated(lease, generated, "png");
|
|
207
|
+
const sha256 = "b".repeat(64);
|
|
208
|
+
test.owner.recordWriteIntent(lease, sha256);
|
|
209
|
+
// The write landed, then the worker died before recording it.
|
|
210
|
+
vi.setSystemTime(Date.now() + 10 * 60 * 1_000);
|
|
211
|
+
expect((await test.owner.claim()).kind).toBe("claimed");
|
|
212
|
+
expect(
|
|
213
|
+
(await test.owner.erase("team-a", "request-a")).writtenSha256,
|
|
214
|
+
).toBe(sha256);
|
|
215
|
+
} finally {
|
|
216
|
+
test.close();
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("releases the hold a worker opened after erasure already settled", async () => {
|
|
221
|
+
const test = make();
|
|
222
|
+
const settle = vi.fn(() => Promise.resolve());
|
|
223
|
+
try {
|
|
224
|
+
await expect(
|
|
225
|
+
run(test.owner, await image("png"), "png", {
|
|
226
|
+
metering: {
|
|
227
|
+
// Erasure lands while the worker is admitting.
|
|
228
|
+
admit: async () => {
|
|
229
|
+
await test.owner.erase("team-a", "request-a");
|
|
230
|
+
return { priceMilliunits: 125 };
|
|
231
|
+
},
|
|
232
|
+
settle,
|
|
233
|
+
},
|
|
234
|
+
}),
|
|
235
|
+
).rejects.toThrow();
|
|
236
|
+
expect(settle).toHaveBeenCalledWith({
|
|
237
|
+
operationId: "media:request-a",
|
|
238
|
+
units: 0,
|
|
239
|
+
});
|
|
240
|
+
} finally {
|
|
241
|
+
test.close();
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("erases an operation whose lease expired mid-generation instead of holding it", async () => {
|
|
246
|
+
const test = make();
|
|
247
|
+
try {
|
|
248
|
+
await generating(test.owner);
|
|
249
|
+
vi.setSystemTime(Date.now() + 10 * 60 * 1_000);
|
|
250
|
+
expect((await test.owner.erase("team-a", "request-a")).clear).toBe(true);
|
|
251
|
+
await expect(test.owner.claim()).rejects.toThrow("not_found");
|
|
252
|
+
} finally {
|
|
253
|
+
test.close();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("refuses an active operation atomically when the host asks to wait for inactivity", async () => {
|
|
258
|
+
const test = make();
|
|
259
|
+
try {
|
|
260
|
+
const lease = await generating(test.owner);
|
|
261
|
+
await expect(
|
|
262
|
+
test.owner.erase("team-a", "request-a", "refuse-active"),
|
|
263
|
+
).rejects.toThrow("still active");
|
|
264
|
+
// Nothing was marked: the generation continues.
|
|
265
|
+
await test.owner.recordGenerated(lease, generated, "png");
|
|
266
|
+
expect(test.owner.read()?.state).toBe("writing");
|
|
267
|
+
} finally {
|
|
268
|
+
test.close();
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("keeps the erasure marker and destination facts through a crash mid-cleanup", async () => {
|
|
273
|
+
const test = make();
|
|
274
|
+
try {
|
|
275
|
+
await generating(test.owner);
|
|
276
|
+
vi.spyOn(test.context.storage, "deleteKeys").mockRejectedValueOnce(
|
|
277
|
+
new Error("process died"),
|
|
278
|
+
);
|
|
279
|
+
await expect(test.owner.erase("team-a", "request-a")).rejects.toThrow(
|
|
280
|
+
"process died",
|
|
281
|
+
);
|
|
282
|
+
// A restart reads the durable marker: the operation stays erased, so the
|
|
283
|
+
// same request id cannot generate and charge again.
|
|
284
|
+
const restarted = new MediaOperationOwner(test.context, {
|
|
285
|
+
APP_ARTIFACTS: test.bucket,
|
|
286
|
+
});
|
|
287
|
+
await Promise.resolve();
|
|
288
|
+
expect(() => admit(restarted)).toThrow("not_found");
|
|
289
|
+
await expect(restarted.claim()).rejects.toThrow("not_found");
|
|
290
|
+
expect(await restarted.erase("team-a", "request-a")).toEqual({
|
|
291
|
+
clear: true,
|
|
292
|
+
destinationPath: request.path,
|
|
293
|
+
writtenSha256: null,
|
|
294
|
+
abandonedGeneration: true,
|
|
295
|
+
});
|
|
296
|
+
// Nothing, not even a completed erase, removes the marker.
|
|
297
|
+
const again = new MediaOperationOwner(test.context, {
|
|
298
|
+
APP_ARTIFACTS: test.bucket,
|
|
299
|
+
});
|
|
300
|
+
await Promise.resolve();
|
|
301
|
+
expect(() => admit(again)).toThrow("not_found");
|
|
302
|
+
expect((await again.erase("team-a", "request-a")).destinationPath).toBe(
|
|
303
|
+
request.path,
|
|
304
|
+
);
|
|
305
|
+
} finally {
|
|
306
|
+
test.close();
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("reports not clear while a put is in flight, then deletes what landed on a repeat", async () => {
|
|
311
|
+
const outputs = new Map<string, Uint8Array>();
|
|
312
|
+
const test = make(outputs);
|
|
313
|
+
let entered = false;
|
|
314
|
+
let release: () => void = () => undefined;
|
|
315
|
+
const gate = new Promise<void>((resolve) => {
|
|
316
|
+
release = resolve;
|
|
317
|
+
});
|
|
318
|
+
const slow = {
|
|
319
|
+
...test.bucket,
|
|
320
|
+
put: async (...input: Parameters<typeof test.bucket.put>) => {
|
|
321
|
+
entered = true;
|
|
322
|
+
await gate;
|
|
323
|
+
return test.bucket.put(...input);
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
try {
|
|
327
|
+
await Promise.resolve();
|
|
328
|
+
const owner = new MediaOperationOwner(test.context, {
|
|
329
|
+
APP_ARTIFACTS: slow,
|
|
330
|
+
});
|
|
331
|
+
const lease = await generating(owner);
|
|
332
|
+
const late = owner.recordGenerated(lease, generated, "png");
|
|
333
|
+
const settled = expect(late).rejects.toThrow("not_found");
|
|
334
|
+
await vi.waitFor(() => {
|
|
335
|
+
expect(entered).toBe(true);
|
|
336
|
+
});
|
|
337
|
+
expect((await owner.erase("team-a", "request-a")).clear).toBe(false);
|
|
338
|
+
release();
|
|
339
|
+
await settled;
|
|
340
|
+
expect((await owner.erase("team-a", "request-a")).clear).toBe(true);
|
|
341
|
+
expect(outputs.size).toBe(0);
|
|
342
|
+
} finally {
|
|
343
|
+
test.close();
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("remembers a failed late delete and retries it on the next erase", async () => {
|
|
348
|
+
const outputs = new Map<string, Uint8Array>();
|
|
349
|
+
const test = make(outputs);
|
|
350
|
+
let entered = false;
|
|
351
|
+
let release: () => void = () => undefined;
|
|
352
|
+
const gate = new Promise<void>((resolve) => {
|
|
353
|
+
release = resolve;
|
|
354
|
+
});
|
|
355
|
+
const slow = {
|
|
356
|
+
...test.bucket,
|
|
357
|
+
put: async (...input: Parameters<typeof test.bucket.put>) => {
|
|
358
|
+
entered = true;
|
|
359
|
+
await gate;
|
|
360
|
+
return test.bucket.put(...input);
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
try {
|
|
364
|
+
await Promise.resolve();
|
|
365
|
+
const owner = new MediaOperationOwner(test.context, {
|
|
366
|
+
APP_ARTIFACTS: slow,
|
|
367
|
+
});
|
|
368
|
+
const lease = await generating(owner);
|
|
369
|
+
const late = owner.recordGenerated(lease, generated, "png");
|
|
370
|
+
await vi.waitFor(() => {
|
|
371
|
+
expect(entered).toBe(true);
|
|
372
|
+
});
|
|
373
|
+
await owner.erase("team-a", "request-a");
|
|
374
|
+
// Keep the late bytes: the first delete after the put lands fails.
|
|
375
|
+
vi.spyOn(slow, "delete").mockRejectedValueOnce(new Error("r2 down"));
|
|
376
|
+
release();
|
|
377
|
+
await expect(late).rejects.toThrow("r2 down");
|
|
378
|
+
expect(outputs.has(outputKey)).toBe(true);
|
|
379
|
+
expect((await owner.readErasure()).clear).toBe(false);
|
|
380
|
+
expect((await owner.erase("team-a", "request-a")).clear).toBe(true);
|
|
381
|
+
expect(outputs.size).toBe(0);
|
|
382
|
+
} finally {
|
|
383
|
+
test.close();
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("deletes background reference copies a dispatch was writing during erasure", async () => {
|
|
388
|
+
const outputs = new Map<string, Uint8Array>();
|
|
389
|
+
const test = make(outputs);
|
|
390
|
+
try {
|
|
391
|
+
test.owner.admit("actor-a", coordinates, "request-a", {
|
|
392
|
+
...request,
|
|
393
|
+
references: [{ computerId: "computer-a", path: "/Pictures/panel.png" }],
|
|
394
|
+
});
|
|
395
|
+
const claimed = await test.owner.claim();
|
|
396
|
+
const lease = claimed.leaseId ?? "";
|
|
397
|
+
test.owner.recordEstimate(lease, 125);
|
|
398
|
+
await test.owner.dispatchInBackground(
|
|
399
|
+
lease,
|
|
400
|
+
[
|
|
401
|
+
{
|
|
402
|
+
bytes: generated,
|
|
403
|
+
format: "png",
|
|
404
|
+
width: 1,
|
|
405
|
+
height: 1,
|
|
406
|
+
filename: "panel.png",
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
[],
|
|
410
|
+
);
|
|
411
|
+
expect(outputs.size).toBe(1);
|
|
412
|
+
expect((await test.owner.erase("team-a", "request-a")).clear).toBe(true);
|
|
413
|
+
expect(outputs.size).toBe(0);
|
|
414
|
+
} finally {
|
|
415
|
+
test.close();
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("deletes the destination file when erasure wins the race with its write", async () => {
|
|
420
|
+
const test = make();
|
|
421
|
+
const files = new Map<string, Uint8Array>();
|
|
422
|
+
const removed: string[] = [];
|
|
423
|
+
try {
|
|
424
|
+
await expect(
|
|
425
|
+
run(test.owner, await image("png"), "png", {
|
|
426
|
+
write: async (bytes) => {
|
|
427
|
+
files.set(request.path, bytes);
|
|
428
|
+
await test.owner.erase("team-a", "request-a");
|
|
429
|
+
},
|
|
430
|
+
remove: (sha256) => {
|
|
431
|
+
removed.push(sha256);
|
|
432
|
+
files.delete(request.path);
|
|
433
|
+
return Promise.resolve("removed");
|
|
434
|
+
},
|
|
435
|
+
}),
|
|
436
|
+
).rejects.toThrow();
|
|
437
|
+
expect(files.size).toBe(0);
|
|
438
|
+
expect(removed).toHaveLength(1);
|
|
439
|
+
// The digest recorded before the write is the one kept for the host.
|
|
440
|
+
expect(
|
|
441
|
+
(await test.owner.erase("team-a", "request-a")).writtenSha256,
|
|
442
|
+
).toBe(removed[0]);
|
|
443
|
+
} finally {
|
|
444
|
+
test.close();
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
describe("recovering and erasing by operation id", () => {
|
|
450
|
+
it("lists and reads output a crash left unrecorded, then erases it", async () => {
|
|
451
|
+
const outputs = new Map<string, Uint8Array>();
|
|
452
|
+
const test = make(outputs);
|
|
453
|
+
const files = new Map<string, Uint8Array>();
|
|
454
|
+
try {
|
|
455
|
+
await generating(test.owner);
|
|
456
|
+
// The R2 write committed, then the process died before SQL recorded it.
|
|
457
|
+
outputs.set(outputKey, generated);
|
|
458
|
+
const h = host(test, files);
|
|
459
|
+
expect(await retainedMedia(h, "request-a")).toEqual({
|
|
460
|
+
requestId: "request-a",
|
|
461
|
+
state: "generating",
|
|
462
|
+
destinationPath: request.path,
|
|
463
|
+
output: { bytes: 3, format: "png", contentType: "image/png" },
|
|
464
|
+
});
|
|
465
|
+
expect(await readRetainedMedia(h, "request-a")).toEqual({
|
|
466
|
+
bytes: generated,
|
|
467
|
+
format: "png",
|
|
468
|
+
});
|
|
469
|
+
expect(await eraseMediaOperation(h, "request-a")).toMatchObject({
|
|
470
|
+
clear: true,
|
|
471
|
+
destinationPath: request.path,
|
|
472
|
+
destinationFile: null,
|
|
473
|
+
});
|
|
474
|
+
expect(outputs.size).toBe(0);
|
|
475
|
+
expect(await retainedMedia(h, "request-a")).toBeNull();
|
|
476
|
+
expect(await readRetainedMedia(h, "request-a")).toBeNull();
|
|
477
|
+
} finally {
|
|
478
|
+
test.close();
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it("refuses to list or read another namespace's operation", async () => {
|
|
483
|
+
const test = make();
|
|
484
|
+
try {
|
|
485
|
+
admit(test.owner);
|
|
486
|
+
const other = host(test, new Map(), "unmetered", "team-b");
|
|
487
|
+
await expect(retainedMedia(other, "request-a")).rejects.toThrow(
|
|
488
|
+
"scope mismatch",
|
|
489
|
+
);
|
|
490
|
+
await expect(readRetainedMedia(other, "request-a")).rejects.toThrow(
|
|
491
|
+
"scope mismatch",
|
|
492
|
+
);
|
|
493
|
+
} finally {
|
|
494
|
+
test.close();
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it("deletes the saved file only while it still holds what the operation wrote", async () => {
|
|
499
|
+
for (const reused of [false, true]) {
|
|
500
|
+
const test = make();
|
|
501
|
+
const files = new Map<string, Uint8Array>();
|
|
502
|
+
try {
|
|
503
|
+
const h = host(test, files);
|
|
504
|
+
await run(test.owner, await image("png"), "png", {
|
|
505
|
+
write: (bytes) => h.destination.write(request.path, bytes),
|
|
506
|
+
});
|
|
507
|
+
const other = new Uint8Array([9, 9, 9]);
|
|
508
|
+
if (reused) files.set(request.path, other);
|
|
509
|
+
const erased = await eraseMediaOperation(h, "request-a");
|
|
510
|
+
expect(erased.destinationFile).toBe(reused ? "not-ours" : "removed");
|
|
511
|
+
expect(files.get(request.path)).toEqual(reused ? other : undefined);
|
|
512
|
+
// A retry reports the same, from the marker kept through erasure.
|
|
513
|
+
expect(
|
|
514
|
+
(await eraseMediaOperation(h, "request-a")).destinationFile,
|
|
515
|
+
).toBe(reused ? "not-ours" : "absent");
|
|
516
|
+
} finally {
|
|
517
|
+
test.close();
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it("keeps the destination facts so a failed file delete can be retried", async () => {
|
|
523
|
+
const test = make();
|
|
524
|
+
const files = new Map<string, Uint8Array>();
|
|
525
|
+
try {
|
|
526
|
+
const h = host(test, files);
|
|
527
|
+
await run(test.owner, await image("png"), "png", {
|
|
528
|
+
write: (bytes) => h.destination.write(request.path, bytes),
|
|
529
|
+
});
|
|
530
|
+
const remove = vi
|
|
531
|
+
.spyOn(h.destination, "remove")
|
|
532
|
+
.mockRejectedValueOnce(new Error("bucket unavailable"));
|
|
533
|
+
await expect(eraseMediaOperation(h, "request-a")).rejects.toThrow(
|
|
534
|
+
"bucket unavailable",
|
|
535
|
+
);
|
|
536
|
+
expect(files.size).toBe(1);
|
|
537
|
+
expect(await eraseMediaOperation(h, "request-a")).toMatchObject({
|
|
538
|
+
clear: true,
|
|
539
|
+
destinationFile: "removed",
|
|
540
|
+
});
|
|
541
|
+
expect(remove).toHaveBeenCalledTimes(2);
|
|
542
|
+
expect(files.size).toBe(0);
|
|
543
|
+
} finally {
|
|
544
|
+
test.close();
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it("settles an abandoned generation's billing hold at zero, and never a finished one", async () => {
|
|
549
|
+
for (const finished of [false, true]) {
|
|
550
|
+
const test = make();
|
|
551
|
+
const settle = vi.fn(() => Promise.resolve());
|
|
552
|
+
const metering = {
|
|
553
|
+
admit: () => Promise.resolve({ priceMilliunits: 125 }),
|
|
554
|
+
settle,
|
|
555
|
+
};
|
|
556
|
+
try {
|
|
557
|
+
const h = host(test, new Map(), metering);
|
|
558
|
+
if (finished)
|
|
559
|
+
await run(test.owner, await image("png"), "png", {
|
|
560
|
+
write: () => Promise.resolve(),
|
|
561
|
+
});
|
|
562
|
+
else await generating(test.owner);
|
|
563
|
+
const erased = await eraseMediaOperation(h, "request-a");
|
|
564
|
+
expect(erased.abandonedGeneration).toBe(!finished);
|
|
565
|
+
if (finished) expect(settle).not.toHaveBeenCalled();
|
|
566
|
+
else
|
|
567
|
+
expect(settle).toHaveBeenCalledWith({
|
|
568
|
+
operationId: "media:request-a",
|
|
569
|
+
units: 0,
|
|
570
|
+
});
|
|
571
|
+
} finally {
|
|
572
|
+
test.close();
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("resumes an erasure that stopped after the marker, with the operation still refusing work", async () => {
|
|
578
|
+
const outputs = new Map<string, Uint8Array>();
|
|
579
|
+
const test = make(outputs);
|
|
580
|
+
try {
|
|
581
|
+
const lease = await generating(test.owner);
|
|
582
|
+
await test.owner.recordGenerated(lease, generated, "png");
|
|
583
|
+
const del = vi
|
|
584
|
+
.spyOn(test.bucket, "delete")
|
|
585
|
+
.mockRejectedValueOnce(new Error("r2 down"));
|
|
586
|
+
await expect(test.owner.erase("team-a", "request-a")).rejects.toThrow(
|
|
587
|
+
"r2 down",
|
|
588
|
+
);
|
|
589
|
+
await expect(test.owner.claim()).rejects.toThrow("not_found");
|
|
590
|
+
expect((await test.owner.erase("team-a", "request-a")).clear).toBe(true);
|
|
591
|
+
expect(del).toHaveBeenCalled();
|
|
592
|
+
expect(outputs.size).toBe(0);
|
|
593
|
+
} finally {
|
|
594
|
+
test.close();
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
});
|