Skip to content

Commit 071baee

Browse files
committed
🔒 Attach a checkout only when it is the one that was retained
Attachment asked whether Git could read the checkout at the retained path. Any repository answers yes. A review probe made that concrete: create Repository A, write a valid clone of unrelated Repository B over A's authoritative Workspace checkout, keep A's metadata and journal, remove the root Close, and partial replay completed — restoring A's recorded reads against B's content, with every later live effect landing in B. Readable is a property of any repository, so the three things the record actually claims are now read back out of the checkout itself: the locator it was cloned from, compared by fingerprint against the retained one; the algorithm it names objects with; and whether the commit it was created at is present. A Worktree additionally proves its checkout is a worktree of the Repository it belongs to, which Git answers directly — `--git-common-dir` resolves through the administration pair to the repository a worktree is linked to, so a repository of its own standing in a worktree's place answers with a different directory. What is deliberately not asked is where HEAD or the current branch is. #294 moves those transactionally, and requiring them to equal creation state would make an ordinary commit look like corruption. A regression pins that too: the same repository, checked out on another branch at another commit, still attaches. Five regressions, each differing from the retained checkout in exactly one dimension of identity, so a passing test says which check caught it. The substitution one is the review's probe: it proves the failure is fatal past `<PrintErrors>`, that no child or later sibling ran, that nothing was cloned, fetched or checked out, and that the retained record, the Workspace frontier and the composition events are exactly what they were. Refs #293
1 parent 62f8506 commit 071baee

5 files changed

Lines changed: 468 additions & 20 deletions

File tree

‎packages/workflow/src/deno/composition/git.ts‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,18 @@ export function currentBranch(git: GitSession, directory: string): Operation<str
9090
return git.read(["symbolic-ref", "--quiet", "--short", "HEAD"], directory);
9191
}
9292

93-
export function* objectFormat(git: GitSession, directory: string): Operation<GitObjectFormat> {
93+
/** The object format a checkout reports, or `undefined` when it reports none. */
94+
export function* readObjectFormat(
95+
git: GitSession,
96+
directory: string,
97+
): Operation<GitObjectFormat | undefined> {
9498
const reported = yield* git.read(["rev-parse", "--show-object-format"], directory);
95-
if (reported !== "sha1" && reported !== "sha256") {
99+
return reported === "sha1" || reported === "sha256" ? reported : undefined;
100+
}
101+
102+
export function* objectFormat(git: GitSession, directory: string): Operation<GitObjectFormat> {
103+
const reported = yield* readObjectFormat(git, directory);
104+
if (reported === undefined) {
96105
throw new GitRefusal("unusable-repository");
97106
}
98107
return reported;
@@ -303,13 +312,61 @@ function* resolveWorktreeBase(
303312
/**
304313
* Whether a materialized checkout is one Git can still read.
305314
*
306-
* The question attachment asks: not whether HEAD is where creation left it —
307-
* a later Git effect may have moved it transactionally — but whether the
308-
* administration the Workspace retained still describes a repository at all.
315+
* Necessary and nowhere near sufficient. It says the administration describes a
316+
* repository; it says nothing about *which* repository, which is why the checks
317+
* below exist beside it. Deliberately not a question about where HEAD is: a
318+
* later Git effect may have moved it transactionally, and creation identity is
319+
* not a shadow of the current branch.
309320
*/
310321
export function* checkoutReadable(
311322
git: GitSession,
312323
directory: string,
313324
): Operation<string | undefined> {
314325
return yield* resolveCommit(git, directory, "HEAD");
315326
}
327+
328+
/**
329+
* The locator a checkout says it came from.
330+
*
331+
* `remote.origin.url` is what a clone writes and what this provider's clone
332+
* wrote. Reading it back is how the bytes in the Workspace are tied to the
333+
* identity the record claims for them: a checkout of another repository sitting
334+
* at the retained path answers with another locator, and no amount of HEAD
335+
* being readable would have said so.
336+
*/
337+
export function* originLocator(git: GitSession, directory: string): Operation<string | undefined> {
338+
return yield* git.read(["config", "--get", "remote.origin.url"], directory);
339+
}
340+
341+
/** Whether this object id names a commit that is present in this checkout. */
342+
export function* commitPresent(
343+
git: GitSession,
344+
directory: string,
345+
commit: string,
346+
): Operation<boolean> {
347+
return (yield* resolveCommit(git, directory, commit)) !== undefined;
348+
}
349+
350+
/**
351+
* The shared `.git` directory a checkout belongs to.
352+
*
353+
* For a primary checkout this is its own `.git`; for a linked worktree it is the
354+
* repository the worktree is a worktree *of*. Comparing it with the place the
355+
* enclosing Repository was materialized is what proves a retained worktree
356+
* belongs to the retained Repository it claims, rather than to some other
357+
* repository whose administration happens to resolve.
358+
*/
359+
export function* commonDirectory(
360+
git: GitSession,
361+
directory: string,
362+
): Operation<string | undefined> {
363+
// Without `--path-format=absolute`, which not every supported Git has: a
364+
// linked worktree already answers absolutely, and a primary checkout answers
365+
// `.git` relative to itself. Resolving the relative case here keeps the
366+
// comparison one string equality on every version.
367+
const reported = yield* git.read(["rev-parse", "--git-common-dir"], directory);
368+
if (reported === undefined) {
369+
return undefined;
370+
}
371+
return reported.startsWith("/") ? reported : `${directory}/${reported}`;
372+
}

‎packages/workflow/src/deno/composition/provider.ts‎

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
sameRepositoryRecord,
6262
sameWorktreeRecord,
6363
worktreeRecordJson,
64+
type GitObjectFormat,
6465
type RepositoryCreationRequest,
6566
type RepositoryRecord,
6667
type WorktreeCreationRequest,
@@ -80,6 +81,10 @@ import {
8081
checkoutPrimary,
8182
checkoutReadable,
8283
clone,
84+
commitPresent,
85+
commonDirectory,
86+
originLocator,
87+
readObjectFormat,
8388
GitRefusal,
8489
gitSession,
8590
objectFormat,
@@ -592,7 +597,11 @@ type StaleReason =
592597
| "administration"
593598
| "repository"
594599
| "placement"
595-
| "locator";
600+
| "locator"
601+
| "origin"
602+
| "object-format"
603+
| "creation-commit"
604+
| "linkage";
596605

597606
const STALE_SENTENCES: ReadonlyMap<StaleReason, string> = new Map([
598607
["metadata", "the retained record naming it is gone or names something else"],
@@ -601,6 +610,10 @@ const STALE_SENTENCES: ReadonlyMap<StaleReason, string> = new Map([
601610
["repository", "the repository it belongs to is no longer retained"],
602611
["placement", "the path it recorded is not the one this run's placement gives its identity"],
603612
["locator", "the url retained for it is not the one its identity names"],
613+
["origin", "the checkout it holds came from a different repository"],
614+
["object-format", "the checkout it holds names its objects with a different algorithm"],
615+
["creation-commit", "the commit it was created at is not in the checkout it holds"],
616+
["linkage", "the checkout it holds is not a worktree of the repository it belongs to"],
604617
]);
605618

606619
function stale(subject: string, reason: StaleReason): RepositoryStaleStateError {
@@ -676,12 +689,27 @@ function worktreeSubject(name: string): string {
676689
return `worktree ${JSON.stringify(name)}`;
677690
}
678691

692+
/**
693+
* A materialized checkout, and what it has to turn out to be.
694+
*
695+
* `repositoryDirectory` is where the Repository this checkout belongs to was
696+
* materialized — for a primary checkout, itself — and `repository` is that
697+
* Repository's retained record. Both travel with the directory because
698+
* verification is a comparison, and neither half of it can be read off the host
699+
* tree alone.
700+
*/
701+
interface Attached {
702+
readonly directory: string;
703+
readonly repositoryDirectory: string;
704+
readonly repository: RepositoryRecord;
705+
}
706+
679707
function* prepareRepositoryAttachment(
680708
workspace: PrivateWorkspaceTransaction,
681709
root: string,
682710
record: RepositoryRecord,
683711
subject: string,
684-
): Operation<string> {
712+
): Operation<Attached> {
685713
const stored = workspace.metadata.readRepository(record.name);
686714
if (stored === undefined || !sameRepositoryRecord(stored.record, record)) {
687715
throw stale(subject, "metadata");
@@ -695,15 +723,15 @@ function* prepareRepositoryAttachment(
695723
}
696724
const directory = yield* exportTree(workspace.filesystem, root, checkoutPath);
697725
localizeAdministration(root, directory, []);
698-
return directory;
726+
return { directory, repositoryDirectory: directory, repository: record };
699727
}
700728

701729
function* prepareWorktreeAttachment(
702730
workspace: PrivateWorkspaceTransaction,
703731
root: string,
704732
record: WorktreeRecord,
705733
subject: string,
706-
): Operation<string> {
734+
): Operation<Attached> {
707735
const stored = workspace.metadata.readWorktree(record.repositoryName, record.name);
708736
if (stored === undefined || !sameWorktreeRecord(stored, record)) {
709737
throw stale(subject, "metadata");
@@ -727,20 +755,103 @@ function* prepareWorktreeAttachment(
727755
const repositoryDirectory = yield* exportTree(workspace.filesystem, root, repositoryPath);
728756
const directory = yield* exportTree(workspace.filesystem, root, checkoutPath);
729757
localizeAdministration(root, repositoryDirectory, [directory]);
730-
return directory;
758+
return { directory, repositoryDirectory, repository: repository.record };
759+
}
760+
761+
/**
762+
* What the checkout at a retained path has to turn out to be, or what disagrees.
763+
*
764+
* Readable is where this used to stop, and readable is a property of *any*
765+
* repository. A clone of an unrelated repository written over the retained path
766+
* is perfectly readable, so attachment that asked only that question accepted
767+
* one identity's history against another identity's bytes — and the document
768+
* carried on, restoring recorded reads that no longer describe what is there
769+
* while every later live effect ran against the substitute.
770+
*
771+
* So the three things the record actually claims are read back out of the
772+
* checkout: where it came from, how it names objects, and whether the commit it
773+
* was created at is in it. Each is creation identity, and creation identity does
774+
* not move. What is deliberately *not* asked is where HEAD or the current branch
775+
* is: #294 moves those transactionally, and requiring them to equal creation
776+
* state would make an ordinary commit look like corruption.
777+
*
778+
* The origin is compared by fingerprint rather than by bytes. The fingerprint is
779+
* what every other comparison in this provider uses, and it is the one form of
780+
* the locator that is safe to hold beside a diagnostic.
781+
*/
782+
function* repositoryDisagreement(
783+
git: GitSession,
784+
attached: Attached,
785+
objectFormat: GitObjectFormat,
786+
creationCommit: string,
787+
): Operation<StaleReason | undefined> {
788+
if ((yield* checkoutReadable(git, attached.directory)) === undefined) {
789+
return "administration";
790+
}
791+
792+
const origin = yield* originLocator(git, attached.repositoryDirectory);
793+
if (
794+
origin === undefined ||
795+
locatorFingerprint(origin) !== attached.repository.locatorFingerprint
796+
) {
797+
return "origin";
798+
}
799+
if ((yield* readObjectFormat(git, attached.repositoryDirectory)) !== objectFormat) {
800+
return "object-format";
801+
}
802+
if (!(yield* commitPresent(git, attached.directory, creationCommit))) {
803+
return "creation-commit";
804+
}
805+
return undefined;
806+
}
807+
808+
/**
809+
* The same questions for a linked worktree, plus the one only it raises.
810+
*
811+
* A worktree's identity is its Repository's identity and its own place inside
812+
* it, so the origin and the object format are read through the shared
813+
* administration — and *that* is the thing worth proving: the checkout retained
814+
* for this worktree has to be a worktree of the Repository this run says it
815+
* belongs to. Git answers that directly, because `--git-common-dir` resolves
816+
* through the administration pair to the repository the worktree is linked to.
817+
* A worktree of some other repository, or a whole repository of its own sitting
818+
* at the retained path, answers with a different directory.
819+
*/
820+
function* worktreeDisagreement(
821+
git: GitSession,
822+
attached: Attached,
823+
record: WorktreeRecord,
824+
): Operation<StaleReason | undefined> {
825+
if ((yield* checkoutReadable(git, attached.directory)) === undefined) {
826+
return "administration";
827+
}
828+
if (
829+
(yield* commonDirectory(git, attached.directory)) !== `${attached.repositoryDirectory}/.git`
830+
) {
831+
return "linkage";
832+
}
833+
return yield* repositoryDisagreement(
834+
git,
835+
attached,
836+
attached.repository.objectFormat,
837+
record.creationCommit,
838+
);
731839
}
732840

733841
/**
734-
* Rebuild a live checkout from the Workspace and prove Git can still read it.
842+
* Rebuild a live checkout from the Workspace and prove it is the retained one.
735843
*
736844
* The transaction is held only for the export: Git runs afterwards, against
737-
* files, so a subprocess never keeps the run's database locked.
845+
* files, so a subprocess never keeps the run's database locked. Nothing here
846+
* writes, clones or repairs — a disagreement is reported and the Workspace is
847+
* left exactly as it was found.
738848
*/
739849
function* attach(
740850
database: WorkflowRunDatabase,
741851
host: RepositoryHost,
742852
subject: string,
743-
prepare: (workspace: PrivateWorkspaceTransaction, root: string) => Operation<string>,
853+
prepare: (workspace: PrivateWorkspaceTransaction, root: string) => Operation<Attached>,
854+
disagreement: (git: GitSession, attached: Attached) => Operation<StaleReason | undefined>,
744855
): Operation<void> {
745856
const root = yield* host.useDirectory();
746857
const prepared = yield* transactWorkspaceRoots(database, (workspace) => prepare(workspace, root));
@@ -749,8 +860,9 @@ function* attach(
749860
}
750861

751862
const git: GitSession = gitSession(host, root);
752-
if ((yield* checkoutReadable(git, prepared.value)) === undefined) {
753-
throw stale(subject, "administration");
863+
const found = yield* disagreement(git, prepared.value);
864+
if (found !== undefined) {
865+
throw stale(subject, found);
754866
}
755867
}
756868

@@ -802,6 +914,8 @@ export function useRepositoryComposition(
802914
record,
803915
`repository ${JSON.stringify(record.name)}`,
804916
),
917+
(git, attached) =>
918+
repositoryDisagreement(git, attached, record.objectFormat, record.creationCommit),
805919
);
806920
},
807921

@@ -834,6 +948,7 @@ export function useRepositoryComposition(
834948
record,
835949
`worktree ${JSON.stringify(record.name)}`,
836950
),
951+
(git, attached) => worktreeDisagreement(git, attached, record),
837952
);
838953
},
839954
},

0 commit comments

Comments
 (0)