Skip to content

Commit 3f79c4a

Browse files
committed
🧪 Halt a real export, and compare a real run against what it sealed
Three pieces of evidence #600 was missing, and one classification that was wrong. The cancellation is a cancellation. `exportArtifact` runs in a task of its own, its injected publication step captures the staging directory and then suspends without returning, and the test halts the task. What unwinds is Effection tearing a live scope down, so the staging resource's own backstop is what removes the directory — not `publish`'s caught-error path, which a thrown Error would have taken instead. Both output channels are captured and asserted empty, because a success line and a refusal are the two ways that case could have passed for the wrong reason. Disabling the backstop fails it. XE7 is one representative run compared family by family. It executes the document that is also its definition — a Repository cloned from a remote holding a committed symbolic link and an executable file, a branch, a written file, a staged commit and a Worktree — and exports it through `WorkflowLifecycle.export`. Every comparison has a source outside the artifact: the Workspace against the run's own DOFS filesystem walked path by path, the definition against the objects Git holds, the Repository and Worktree rows against the remote that was cloned and the commit the run's history records making, and the retained history against the journal the run committed. Dropping the worktrees, or carrying only the current root, fails it. A snapshot handed to `writeXmdArtifact()` would have proved the container and said nothing about what an export selects. WFX4-WFX6 are the command's own refusals, each as a process: a real executor holding the run's lock, a repository that is no longer one, and a repository that answers for the pinned commit with another object. Each reports failure, names why, and leaves neither the destination nor the staging directory beside it. Both export suites were listed as Bun-only exclusions on the claim that Node runs them. Node does not: every case takes the run's executor lock, and that lock is `Deno.openSync` through the `Deno` global. They were red in the Node shard from the commit that added them, so they move to the Deno-only list with the reason the source actually gives. Refs #600
1 parent 7146dba commit 3f79c4a

5 files changed

Lines changed: 698 additions & 35 deletions

File tree

‎packages/cli/tests/workflow-cli.test.ts‎

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { randomUUID } from "node:crypto";
1919
import { join } from "node:path";
2020
import { tmpdir } from "node:os";
2121
import { runCli } from "@executablemd/test-support/launch";
22-
import { workflowRunPath } from "@executablemd/workflow/deno";
22+
import { useWorkflowLifecycle, workflowRunPath } from "@executablemd/workflow/deno";
23+
import { WorkflowLifecycle } from "@executablemd/workflow";
2324

2425
interface Fixture {
2526
/** The repository the definition lives in. */
@@ -898,3 +899,164 @@ function digest(output: string, label: string): string | undefined {
898899
?.slice(label.length)
899900
.trim();
900901
}
902+
903+
/**
904+
* Tier WFX — the refusals the export command owns.
905+
*
906+
* Each is a condition a caller can actually arrive in, arranged with real
907+
* things: a real executor holding the run's lock, a repository that is no
908+
* longer there, and a repository that hands back an object the definition does
909+
* not name. The command is the subject, so each runs as a process and is
910+
* observed by its exit status, its diagnostics and what it left on disk.
911+
*
912+
* The provider's own refusals are the workflow package's suites. What is added
913+
* here is that the command turns each one into a failure a caller can see, and
914+
* that none of them puts anything at the destination — including the temporary
915+
* directory an export builds beside it.
916+
*/
917+
describe("Tier WFX — what xmd workflow export refuses", () => {
918+
it("WFX4: refuses a run an executor is holding, and writes nothing", function* () {
919+
yield* useFixture({ "flows/release.md": RELEASE }, function* (fixture) {
920+
const started = yield* xmd(fixture, [
921+
"workflow",
922+
"start",
923+
"--id=release-1",
924+
"flows/release.md",
925+
]).join();
926+
expect(started.code).toBe(0);
927+
928+
const target = join(fixture.repository, "busy.xmd");
929+
const refused = yield* scoped(function* () {
930+
// The acquisition `xmd workflow start` makes before it advances a run,
931+
// taken here and held across the command below. The lock is the
932+
// operating system's, so the exporting process meets the real one.
933+
yield* useWorkflowLifecycle({ root: fixture.runs });
934+
const acquired = yield* WorkflowLifecycle.operations.acquireExecutor("release-1");
935+
if (!acquired.ok) {
936+
throw acquired.error;
937+
}
938+
expect(acquired.value.kind).toBe("acquired");
939+
return yield* xmd(fixture, [
940+
"workflow",
941+
"export",
942+
"release-1",
943+
`--output=${target}`,
944+
]).join();
945+
});
946+
947+
expect(refused.code).toBe(1);
948+
// The dedicated refusal, not a generic failure: a caller is told the run
949+
// is busy and that nothing was written.
950+
expect(refused.stderr).toContain("is being executed right now");
951+
expect(refused.stderr).toContain("Nothing was written");
952+
yield* expectNothingPublished(fixture, target);
953+
});
954+
});
955+
956+
it("WFX5: refuses when the run's own source cannot be read back", function* () {
957+
yield* useFixture({ "flows/release.md": RELEASE }, function* (fixture) {
958+
const started = yield* xmd(fixture, [
959+
"workflow",
960+
"start",
961+
"--id=release-1",
962+
"flows/release.md",
963+
]).join();
964+
expect(started.code).toBe(0);
965+
966+
// The repository the run retains stops being one. Nothing about the run
967+
// changes: what is gone is the only place its definition's bytes were.
968+
yield* rm(join(fixture.repository, ".git"), { recursive: true, force: true });
969+
970+
const target = join(fixture.repository, "unreadable.xmd");
971+
const refused = yield* xmd(fixture, [
972+
"workflow",
973+
"export",
974+
"release-1",
975+
`--output=${target}`,
976+
]).join();
977+
978+
expect(refused.code).toBe(1);
979+
// Named, because "exited 1" is what a run this command never found would
980+
// also say: what refused is the reading of this run's own definition.
981+
expect(refused.stderr).toContain("this run's retained definition could not be loaded");
982+
yield* expectNothingPublished(fixture, target);
983+
});
984+
});
985+
986+
it("WFX6: refuses source the repository hands back that is not this run's", function* () {
987+
yield* useFixture(BUNDLE_FILES, function* (fixture) {
988+
const started = yield* xmd(fixture, [
989+
"workflow",
990+
"start",
991+
"--id=bundle-1",
992+
"flows/bundle.md",
993+
]).join();
994+
expect(started.code).toBe(0);
995+
const pinned = yield* revision(fixture, "HEAD");
996+
997+
// A second commit that says something else at the component's path, put
998+
// in front of the pinned one. The definition still names the commit it
999+
// named; the repository now answers for it with another object, which is
1000+
// exactly the case a host's own reading cannot notice.
1001+
yield* writeTextFile(join(fixture.repository, "flows/Stage.md"), "replaced.\n");
1002+
yield* commitAgain(fixture, "replacement");
1003+
const replacement = yield* revision(fixture, "HEAD");
1004+
yield* git(fixture.repository, ["replace", pinned, replacement]);
1005+
1006+
const target = join(fixture.repository, "mismatched.xmd");
1007+
const refused = yield* xmd(fixture, [
1008+
"workflow",
1009+
"export",
1010+
"bundle-1",
1011+
`--output=${target}`,
1012+
]).join();
1013+
1014+
expect(refused.code).toBe(1);
1015+
expect(refused.stderr).toContain("no longer the object this run's definition names");
1016+
yield* expectNothingPublished(fixture, target);
1017+
});
1018+
});
1019+
});
1020+
1021+
/** A root closed over one component, so a definition has an object to disagree about. */
1022+
const BUNDLE_FILES: Record<string, string> = {
1023+
"flows/bundle.md": [
1024+
"---",
1025+
"workflow:",
1026+
" components:",
1027+
" Stage: ./Stage.md",
1028+
"---",
1029+
"",
1030+
"# Bundle",
1031+
"",
1032+
"<Stage />",
1033+
"",
1034+
].join("\n"),
1035+
"flows/Stage.md": "staged.\n",
1036+
};
1037+
1038+
/** What one revision resolves to in the fixture's repository right now. */
1039+
function* revision(fixture: Fixture, name: string): Operation<string> {
1040+
const result = yield* exec("git", {
1041+
arguments: ["rev-parse", "--verify", name],
1042+
cwd: fixture.repository,
1043+
}).expect();
1044+
if (result.code !== 0) {
1045+
throw new Error(`git rev-parse ${name} failed: ${result.stderr}`);
1046+
}
1047+
return result.stdout.trim();
1048+
}
1049+
1050+
/**
1051+
* That a refusal left the destination alone, and no staging beside it.
1052+
*
1053+
* Both, because an export builds in a temporary directory next to the file a
1054+
* caller asked for: a refusal that removed the target and left that directory
1055+
* would still have put something where nobody wants one.
1056+
*/
1057+
function* expectNothingPublished(fixture: Fixture, target: string): Operation<void> {
1058+
expect(yield* exists(target)).toBe(false);
1059+
expect(
1060+
(yield* readdir(fixture.repository)).filter((name) => name.startsWith(".xmd-export-")),
1061+
).toEqual([]);
1062+
}

‎packages/cli/tests/workflow-export-publication.test.ts‎

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@
1111
* staging state too, so filesystem state on its own says nothing about whether
1212
* publication was ever attempted: a suite that only looked at the directory
1313
* would pass against a build where export never got that far.
14+
*
15+
* The cancellation is a cancellation. The export runs in a task of its own, its
16+
* publication step suspends and never returns, and the test halts the task —
17+
* so what unwinds is Effection tearing a live scope down, reaching the staging
18+
* resource's own backstop. A thrown error would take `publish`'s catch instead
19+
* and prove something else entirely, which is why that case asserts nothing was
20+
* written to either channel.
1421
*/
1522

1623
import { describe, it } from "@executablemd/test-support/bdd";
1724
import { expect } from "@executablemd/test-support/expect";
1825
import { ensureDir, exists, readdir, readTextFile, rm, writeTextFile } from "@effectionx/fs";
1926
import { exec } from "@effectionx/process";
20-
import { ensure, type Operation, scoped, until } from "effection";
27+
import { ensure, type Operation, scoped, spawn, suspend, until, withResolvers } from "effection";
2128
import { randomUUID } from "node:crypto";
2229
import { link } from "node:fs/promises";
2330
import process from "node:process";
24-
import { join } from "node:path";
31+
import { basename, dirname, join } from "node:path";
2532
import { tmpdir } from "node:os";
2633
import { fileURLToPath } from "node:url";
2734
import { runCli } from "@executablemd/test-support/launch";
@@ -226,30 +233,60 @@ describe("Tier WXP — what an export leaves behind", () => {
226233
});
227234
});
228235

229-
it("WXP4: a cancellation after staging leaves neither target nor staging", function* () {
236+
it("WXP4: halting a live export publishes nothing and leaves nothing", function* () {
230237
yield* useExportedRun(function* (fixture) {
231238
const target = join(fixture.repository, "cancelled.xmd");
232-
const staged: string[] = [];
233-
239+
// Publication is reached and then never returns. Nothing resolves this
240+
// suspend, so the only thing that can end the export is the halt below —
241+
// which is what makes this a cancellation rather than a thrown error
242+
// wearing the word.
243+
const publishing = withResolvers<string>();
234244
const recorder = recording({
235-
*link(_staging: string, _output: string): Operation<void> {
236-
// Staging exists by now: record what is on disk, then end the export
237-
// the way a cancellation ends it — without publishing.
238-
staged.push(...(yield* leftovers(fixture)));
239-
throw new Error("cancelled before publication completed");
245+
*link(staging: string, _output: string): Operation<void> {
246+
// The staging directory, taken while it is still there. After the
247+
// halt there is nothing left to learn its name from, so a case that
248+
// looked for it afterwards could not tell a removed directory from
249+
// one that was never created.
250+
publishing.resolve(dirname(staging));
251+
yield* suspend();
240252
},
241253
});
242-
const outcome = yield* withHost(fixture, function* () {
243-
return yield* exportArtifact("release-1", target, recorder.filesystem);
254+
255+
// Both channels, because the two ways this could pass for the wrong
256+
// reason are a success line and a refusal. A halt produces neither: it
257+
// does not run `publish`'s catch, and it never reaches the report.
258+
const said: string[] = [];
259+
const log = console.log;
260+
const error = console.error;
261+
yield* ensure(() => {
262+
console.log = log;
263+
console.error = error;
244264
});
265+
console.log = (...parts: unknown[]) => said.push(parts.map(String).join(" "));
266+
console.error = (...parts: unknown[]) => said.push(parts.map(String).join(" "));
245267

246-
// Staging was really there when the export was interrupted, and the
247-
// scope that owned it removed it on the way out.
268+
const exporting = yield* spawn(function* () {
269+
return yield* withHost(fixture, function* () {
270+
return yield* exportArtifact("release-1", target, recorder.filesystem);
271+
});
272+
});
273+
274+
const staging = yield* publishing.operation;
275+
// The export really is live and really is past staging: publication was
276+
// reached, and the directory it built is on disk at this moment.
277+
expect(recorder.log).toEqual(["link"]);
278+
expect(yield* exists(staging)).toBe(true);
279+
expect(yield* leftovers(fixture)).toEqual([basename(staging)]);
280+
281+
yield* exporting.halt();
282+
283+
// Nothing further ran, nothing was published, and the scope that owned
284+
// the staging directory took it with it.
248285
expect(recorder.log).toEqual(["link"]);
249-
expect(staged.length).toBe(1);
250-
expect(outcome.exitCode).toBe(1);
251286
expect(yield* exists(target)).toBe(false);
287+
expect(yield* exists(staging)).toBe(false);
252288
expect(yield* leftovers(fixture)).toEqual([]);
289+
expect(said).toEqual([]);
253290
});
254291
});
255292
});

‎packages/workflow/tests/support/git-remotes.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,16 @@ function environment(home: string): Record<string, string> {
7373
};
7474
}
7575

76-
/** Run one Git command, failing the suite with what Git said when it refuses. */
77-
export function git(args: readonly string[], cwd: string, home: string): string {
76+
/**
77+
* Run one Git command and answer with exactly what it printed.
78+
*
79+
* Untrimmed, because a fixture that reads a committed file back out of Git is
80+
* reading bytes: a trailing newline is part of the document, and a helper that
81+
* removed it would make "these are the exact bytes" a claim about something
82+
* else. `git()` trims, for the object ids and branch names every other caller
83+
* asks for.
84+
*/
85+
export function gitOutput(args: readonly string[], cwd: string, home: string): string {
7886
const output = spawnSync("git", [...args], {
7987
cwd,
8088
env: environment(home),
@@ -84,7 +92,12 @@ export function git(args: readonly string[], cwd: string, home: string): string
8492
if (output.status !== 0) {
8593
throw new Error(`git ${args.join(" ")} exited ${output.status} in ${cwd}: ${output.stderr}`);
8694
}
87-
return output.stdout.trim();
95+
return output.stdout;
96+
}
97+
98+
/** Run one Git command, failing the suite with what Git said when it refuses. */
99+
export function git(args: readonly string[], cwd: string, home: string): string {
100+
return gitOutput(args, cwd, home).trim();
88101
}
89102

90103
function* writeEntry(root: string, entry: RemoteEntry): Operation<void> {

0 commit comments

Comments
 (0)