@@ -21,7 +21,16 @@ import { open } from "node:fs/promises";
2121import { tmpdir } from "node:os" ;
2222import { scoped , spawn , suspend , until , withResolvers } from "effection" ;
2323import type { Operation } from "effection" ;
24- import { GitOperationProtocolError } from "../src/composition/errors.ts" ;
24+ import { GitOperationError , GitOperationProtocolError } from "../src/composition/errors.ts" ;
25+ import { DivergenceError } from "@executablemd/durable-streams" ;
26+ import { RepositoryContext } from "../src/composition/context.ts" ;
27+ import type { RepositoryRecord } from "../src/composition/records.ts" ;
28+ import { gitOperationFingerprint } from "../src/deno/composition/operations.ts" ;
29+ import { withWorkflowWorkspace } from "../src/deno/workspace/host.ts" ;
30+ import type { WorkflowWorkspaceOptions } from "../src/deno/workspace/host.ts" ;
31+ import type { WorkflowRunDatabase } from "../src/storage/api.ts" ;
32+ import { collect , execute , inlineSource } from "@executablemd/core" ;
33+ import type { Json } from "@executablemd/durable-streams" ;
2534import { WORKSPACE_GIT_SWITCH } from "../src/deno/composition/provider.ts" ;
2635import { denoRepositoryHost } from "../src/deno/composition/host.ts" ;
2736import type { GitInvocation , GitOutcome } from "../src/deno/composition/host.ts" ;
@@ -96,6 +105,14 @@ function isProtocolFailure(value: unknown): value is GitOperationProtocolError {
96105 return value instanceof GitOperationProtocolError ;
97106}
98107
108+ function isDivergence ( value : unknown ) : value is DivergenceError {
109+ return value instanceof DivergenceError ;
110+ }
111+
112+ function isGitFailure ( value : unknown ) : value is GitOperationError {
113+ return value instanceof GitOperationError ;
114+ }
115+
99116/** Every table this run's database holds, as another connection sees them. */
100117function tableNames ( path : string ) : string [ ] {
101118 const database = new DatabaseSync ( path ) ;
@@ -138,6 +155,43 @@ function damageSwitchResult(path: string, damage: (record: Record<string, unknow
138155 }
139156}
140157
158+ /**
159+ * One `<Git.Switch>` inside the checkout, under a supplied Repository context.
160+ *
161+ * A self-closing `<Repository>` retains a checkout and installs no context, so
162+ * the record the component observes is exactly the one a caller supplies here —
163+ * which is what a replaced context is, and what makes the two runs below differ
164+ * by nothing but that record.
165+ */
166+ function observedSource ( locator : string ) : string {
167+ return [
168+ `<Repository name="project" url="${ locator } " as="repository" />` ,
169+ "<Dir path={repository}>" ,
170+ `<Git.Switch branch="release" />` ,
171+ "</Dir>" ,
172+ ] . join ( "\n" ) ;
173+ }
174+
175+ function runObserved (
176+ database : WorkflowRunDatabase ,
177+ record : RepositoryRecord ,
178+ locator : string ,
179+ options : WorkflowWorkspaceOptions ,
180+ ) : Operation < Json > {
181+ return scoped ( function * ( ) {
182+ return yield * withWorkflowWorkspace (
183+ database ,
184+ scoped ( function * ( ) {
185+ yield * RepositoryContext . around ( { current : ( ) => record } , { at : "min" } ) ;
186+ return yield * collect (
187+ yield * execute ( { ...inlineSource ( observedSource ( locator ) ) , stream : database . journal } ) ,
188+ ) ;
189+ } ) ,
190+ options ,
191+ ) ;
192+ } ) ;
193+ }
194+
141195describe ( "workflow Git.Switch durability" , ( ) => {
142196 it ( "replays without moving a branch or appending a second transition" , function * ( ) {
143197 const root = yield * useStorageRoot ( ) ;
@@ -349,6 +403,69 @@ describe("workflow Git.Switch durability", () => {
349403 } ) ;
350404 } ) ;
351405
406+ /**
407+ * A recorded transition belongs to the observation it was authorized for.
408+ *
409+ * Durable identity is type and name, and the name is where the observation
410+ * lives, so the encoding behind it has to be injective: two records that
411+ * digested alike would let a replay hand back a transition authorized for one
412+ * of them to the other, on the path where nothing is authenticated because
413+ * nothing is executed.
414+ *
415+ * `requestedBase` is the demonstration. A record that never supplied a base
416+ * retains `null`; a replaced context can supply the string a sentinel-based
417+ * encoding used for absence, and the two must still be different effects.
418+ */
419+ it ( "refuses to replay a transition recorded for a different Repository record" , function * ( ) {
420+ const root = yield * useStorageRoot ( ) ;
421+ const remote = yield * useBareRemote ( REMOTE ) ;
422+ const path = runPath ( root , "release-1.4" ) ;
423+
424+ yield * withStorage ( root , function * ( ) {
425+ // What this fixture retains, learned from a run of its own: creation
426+ // identity is a function of the name, the url and the base.
427+ const learning = yield * createRun ( { runId : "learning" } ) ;
428+ yield * runDocument ( learning , `<Repository name="project" url="${ remote . locator } " as="r" />` ) ;
429+ const [ learned ] = yield * retainedRepositories ( learning ) ;
430+ const record = learned ?. record ;
431+ if ( record === undefined || record . requestedBase !== null ) {
432+ throw new Error ( "the fixture did not retain a Repository with no requested base" ) ;
433+ }
434+
435+ const database = yield * createRun ( ) ;
436+ const first = countingHost ( ) ;
437+ yield * runObserved ( database , record , remote . locator , countingOptions ( first ) ) ;
438+ expect ( subcommands ( first . counters ) ) . toContain ( "switch" ) ;
439+ const recorded = yield * gitEvents ( database ) ;
440+ expect ( recorded ) . toHaveLength ( 1 ) ;
441+ const published = publishedRoots ( path ) ;
442+
443+ dropRootClose ( path ) ;
444+
445+ // The same expansion, under a context differing in one member only.
446+ const second = countingHost ( ) ;
447+ const failure = yield * raised (
448+ runObserved (
449+ database ,
450+ { ...record , requestedBase : "\u0000" } ,
451+ remote . locator ,
452+ countingOptions ( second ) ,
453+ ) ,
454+ ) ;
455+
456+ // Two different effects, so the recorded one is not this one's to take:
457+ // the journal says so at the position it reaches, before anything runs.
458+ // A collision would instead have handed this observation a transition
459+ // recorded for another, on the path where nothing is authenticated
460+ // because nothing is executed.
461+ expect ( causedBy ( failure , isDivergence ) ) . toBeInstanceOf ( DivergenceError ) ;
462+ expect ( causedBy ( failure , isGitFailure ) ) . toBe ( undefined ) ;
463+ expect ( subcommands ( second . counters ) ) . not . toContain ( "switch" ) ;
464+ expect ( yield * gitEvents ( database ) ) . toHaveLength ( recorded . length ) ;
465+ expect ( publishedRoots ( path ) ) . toBe ( published ) ;
466+ } ) ;
467+ } ) ;
468+
352469 it ( "leaves the frontier untouched when a blocked switch is halted" , function * ( ) {
353470 const root = yield * useStorageRoot ( ) ;
354471 const remote = yield * useBareRemote ( REMOTE ) ;
@@ -449,6 +566,50 @@ describe("workflow Git.Switch durability", () => {
449566 * it returns only once a reader has the pipe open, so the child is provably
450567 * running at the moment the scope is torn down.
451568 */
569+ /**
570+ * The encoding behind a Git operation's durable identity, on its own.
571+ *
572+ * The replay above proves one collision cannot happen; this proves the shape of
573+ * the encoding, which is what makes every other arrangement of values safe too.
574+ * Absence, a separator, a length prefix and the empty string are all ordinary
575+ * content somewhere — a branch may be named `1:a`, a base may contain any
576+ * character — so none of them may be readable as structure.
577+ */
578+ describe ( "workflow Git operation identity" , ( ) => {
579+ it ( "gives distinct values distinct fingerprints" , function * ( ) {
580+ const arrangements : ( string | null ) [ ] [ ] = [
581+ [ ] ,
582+ [ null ] ,
583+ [ "" ] ,
584+ [ "\u0000" ] ,
585+ [ "\u0001" ] ,
586+ [ "-" ] ,
587+ [ null , null ] ,
588+ [ null , "" ] ,
589+ [ "" , null ] ,
590+ [ "a" , "b" ] ,
591+ [ "a\u0001b" ] ,
592+ [ "ab" , "" ] ,
593+ [ "" , "ab" ] ,
594+ [ "1:a" ] ,
595+ [ "1" , "a" ] ,
596+ [ "2:ab" , "c" ] ,
597+ [ "2:ab c" ] ,
598+ [ "main" , null ] ,
599+ [ null , "main" ] ,
600+ ] ;
601+
602+ const fingerprints = arrangements . map ( ( values ) => gitOperationFingerprint ( values ) ) ;
603+ expect ( new Set ( fingerprints ) . size ) . toBe ( arrangements . length ) ;
604+ } ) ;
605+
606+ it ( "gives one value one fingerprint" , function * ( ) {
607+ expect ( gitOperationFingerprint ( [ "main" , null , "a\u0001b" ] ) ) . toBe (
608+ gitOperationFingerprint ( [ "main" , null , "a\u0001b" ] ) ,
609+ ) ;
610+ } ) ;
611+ } ) ;
612+
452613describe ( "workflow Git operation teardown" , ( ) => {
453614 it ( "kills the Git child it spawned when the scope is halted" , function * ( ) {
454615 const directory = yield * useTempDirectory ( "xmd-git-teardown-" ) ;
0 commit comments