Skip to content

Commit dcf4ea5

Browse files
committed
🧷 Ask a publication which wait it stands behind, and whether it carried a value
Two ways a snapshot still reached a canonical identity it had not earned. A request describes its wait twice, in `name` and in `suspensionId`, and the structural pairing only compared the first. A pair agreeing about the name and disagreeing about the identity names two waits, and neither of them is one the other could have ended. Both fields are now compared, and asked of every publication rather than only of one some retained row points at — inherited history legitimately arrives as a request and its publication with no row behind them, which is exactly where nothing else was looking. A successful publication was read through `result.value ?? null`, so a result carrying no value at all read as a publication of `null`. A wait that ended with nothing and a wait whose retained answer is null are different histories, and one was standing in for the other. The member is now asked for by name, and only once it is there is the value compared. Both regressions are writer-path, because neither can be made by damaging a sealed file — a sealed file is one this build already accepted. They are snapshots offered to `writeXmdArtifact()`, which has to refuse them before a container exists, and the test asserts nothing is left at the destination. At the previous commit each of them is written and given an identity. Removing either check alone puts that back. Refs #602
1 parent 046176e commit dcf4ea5

2 files changed

Lines changed: 113 additions & 10 deletions

File tree

‎packages/workflow/src/deno/artifact/records.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,13 +1479,17 @@ function* verifySuspensions(
14791479
// it: an answer is published directly behind the request it answers, so one
14801480
// standing anywhere else answers nothing this artifact holds.
14811481
const behind = byPosition.get(positionKey(entry.coroutineId, entry.index - 1));
1482-
if (
1483-
behind === undefined ||
1484-
behind.description.type !== SUSPENSION_REQUEST ||
1485-
behind.description["name"] !== named
1486-
) {
1482+
if (behind === undefined || behind.description.type !== SUSPENSION_REQUEST) {
14871483
reject("an answer is published somewhere other than behind the request it answers");
14881484
}
1485+
// Both fields, not just the one this publication happens to be read by. A
1486+
// request describes its wait twice over, and a pair that agrees about one
1487+
// and not the other names two waits — which is not a wait either of them
1488+
// could have ended. Asked of every publication, including one that
1489+
// inherited history legitimately retains no answer row for.
1490+
if (behind.description["name"] !== named || behind.description["suspensionId"] !== named) {
1491+
reject("an answer is published behind a request that names a different wait");
1492+
}
14891493
published.set(named, Object.freeze({ suspensionId: named, at: entry }));
14901494
}
14911495

@@ -1544,12 +1548,21 @@ function* verifySuspensions(
15441548
) {
15451549
reject("a consumed suspension answer was published away from the wait it ended");
15461550
}
1547-
if (publication.at.result.status !== "ok") {
1551+
const result = publication.at.result;
1552+
if (result.status !== "ok") {
15481553
reject("a consumed suspension answer was published without a value that ended the wait");
15491554
}
1550-
if (
1551-
canonicalJsonText(publication.at.result.value ?? null) !== canonicalJsonText(answer.answer)
1552-
) {
1555+
// Presence, and never normalized. A publication carrying no value at all is
1556+
// not a publication of `null`: reading it as one would let a wait that
1557+
// ended with nothing stand as evidence for a wait whose retained answer is
1558+
// null, and those are different histories. The member is asked for by name
1559+
// — a record parsed out of JSON cannot hold an explicit `undefined`, so
1560+
// absence is the only way past this.
1561+
const ended = "value" in result ? result.value : undefined;
1562+
if (ended === undefined) {
1563+
reject("a consumed suspension answer was published without a value that ended the wait");
1564+
}
1565+
if (canonicalJsonText(ended) !== canonicalJsonText(answer.answer)) {
15531566
reject("a consumed suspension answer was published carrying a different value");
15541567
}
15551568
}

‎packages/workflow/tests/xmd-artifact.test.ts‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ import { XMD_ARTIFACT_APPLICATION_ID } from "../src/deno/artifact/schema.ts";
3838
import { APPLICATION_ID as LIVE_RUN_APPLICATION_ID } from "../src/deno/schema.ts";
3939
import * as publishedDeno from "../deno.ts";
4040
import * as publishedRoot from "../mod.ts";
41-
import { richArtifact, SUSPENSIONS } from "./support/artifact-fixture.ts";
41+
import { richArtifact, SUSPENSIONS, wait } from "./support/artifact-fixture.ts";
42+
import { serializeDurableEvent } from "@executablemd/durable-streams";
43+
import type { DurableEvent } from "@executablemd/durable-streams";
44+
import { SUSPENSION_ANSWER } from "../src/suspension/answer.ts";
4245

4346
const encoder = new TextEncoder();
4447

@@ -133,6 +136,59 @@ function storedRecord(path: string, kind: string, identity: string): JsonObject
133136
);
134137
}
135138

139+
/** The same snapshot, with some journal records replaced by event id. */
140+
function withRecords(
141+
contents: DetachedXmdArtifact,
142+
replacements: Readonly<Record<string, DurableEvent>>,
143+
): DetachedXmdArtifact {
144+
return {
145+
...contents,
146+
journal: contents.journal.map((row) => {
147+
const replacement = replacements[row.eventId];
148+
return replacement === undefined
149+
? row
150+
: { ...row, record: serializeDurableEvent(replacement) };
151+
}),
152+
};
153+
}
154+
155+
/**
156+
* The answered wait, re-cut so its answer is `null` and its publication carries
157+
* no value member.
158+
*
159+
* The wait's schema is rebuilt to accept null, and its fingerprint with it, so
160+
* what refuses this snapshot is the missing member rather than a value the
161+
* schema would have rejected first.
162+
*/
163+
function valuelessConsumedAnswer(): DetachedXmdArtifact {
164+
const contents = richArtifact();
165+
const nullWait = wait(
166+
SUSPENSIONS.consumed.id,
167+
{ kind: "approval", release: "1.4" },
168+
{
169+
type: "null",
170+
},
171+
);
172+
const valueless: DurableEvent = {
173+
type: "yield",
174+
coroutineId: "root",
175+
description: {
176+
type: SUSPENSION_ANSWER,
177+
name: nullWait.id,
178+
suspensionId: nullWait.id,
179+
},
180+
result: { status: "ok" },
181+
};
182+
return {
183+
...withRecords(contents, { "event-5": nullWait.request, "event-6": valueless }),
184+
answers: contents.answers.map((row) =>
185+
row.suspensionId === nullWait.id
186+
? { ...row, answer: null, requestFingerprint: nullWait.fingerprint }
187+
: row,
188+
),
189+
};
190+
}
191+
136192
/** One member of a parsed record, as the object it has to be. */
137193
function parsedObject(value: unknown): JsonObject {
138194
return parseJsonObject(value, "$", (reason) => new Error(`the member ${reason}`));
@@ -865,6 +921,40 @@ describe("XMD artifact container version 1", () => {
865921
closureHash,
866922
closureMembership,
867923
];
924+
// Two the writer must refuse outright. Neither can be produced by damaging
925+
// a sealed file, because a sealed file is one this build already accepted —
926+
// these are snapshots that would otherwise be written and given a canonical
927+
// identity, so the refusal has to happen before any container exists.
928+
const unwritable: readonly (readonly [string, DetachedXmdArtifact])[] = [
929+
// An inherited request agreeing with its adjacent publication about the
930+
// wait's name and disagreeing about its identity. No answer row points at
931+
// it, so only the structural pairing can catch it.
932+
[
933+
"inherited-identity.xmd",
934+
withRecords(richArtifact(), {
935+
"event-1": {
936+
...SUSPENSIONS.inherited.request,
937+
description: {
938+
...SUSPENSIONS.inherited.request.description,
939+
suspensionId: "9".repeat(32),
940+
},
941+
},
942+
}),
943+
],
944+
// A consumed answer of `null`, published as a success that carries no
945+
// value at all. Normalizing the absent member to null would read these
946+
// two different histories as one.
947+
["valueless-publication.xmd", valuelessConsumedAnswer()],
948+
];
949+
950+
for (const [name, snapshot] of unwritable) {
951+
const written = yield* writeXmdArtifact(join(directory, name), snapshot);
952+
expect(written.ok).toBe(false);
953+
expect(written.ok ? "" : written.error.name).toBe("XmdArtifactInventoryError");
954+
// Refused before a container is accepted, so nothing is left behind.
955+
expect(yield* exists(join(directory, name))).toBe(false);
956+
}
957+
868958
// Each refusal is named, because the manifest comparison that runs after
869959
// these gates would refuse every one of them too — and a suite that only
870960
// asked "did it fail" could not tell which check was doing the work.

0 commit comments

Comments
 (0)