Skip to content

Commit 368aa6d

Browse files
tarasclaude
andauthored
🚨 Bind a Git observation to the route and the turn it was read on (#940)
Four corrections both #940 reviewers reproduced against 94496999. **A directory is a directory under either spelling.** `<Git.File path="dir/" />` answered with the blob at `dir/inside.txt` under a path claiming to be `dir/`. The argument Git receives is a pathspec rather than a lookup: `dir` matches the directory entry and `dir/` matches the things inside it, and the read took the first record without checking whose path it was. It now resolves the entry at exactly the path asked for; a path that matched records but is not one of them is the directory it is, and refuses. **Trusted admission holds a saved route to the history that made it.** A fully shaped observation whose checkout named a root nothing selected was admitted, and its completed terminal reused with no Git at all — the body that checks the live registry is never entered for a completed terminal. Admission now requires the selection and the checkout to be ones a preceding placement in that same history made, before completed or unfinished reuse, decided from parsed historical facts with no canonicalization and no `git`. A cold reuse whose checkout has since been deleted still answers, and a history holding no observation record is unaffected. **One entry's reads and mutations share one native turn.** The reads keyed it `observation <commonDirectory>` while the transitions and the publication keyed it `git <commonDirectory>`, so sharing the serializer shared nothing: a same-entry `<Git.Add>` reached native Git while a `<Git.Diff>` still owned its multi-command read. The key is now named once, `nativeGitTurn()`, and all three callers take it — held through joined teardown and the acknowledged append. Cross-entry independence is untouched: that is a mutation lease, not this queue. **The journey's two missing checkpoint-2 reads and its per-phase immutability evidence** are completed in `sidekick-journey-completion.md` through the real terminal and ACP Sidekick, with paired before/after fingerprints around every phase. The original journey's proven portions stand. Its host count is corrected to 95 of 447 with 8 `config` calls, and its unverifiable tree metadata is withdrawn for a tree object this repository holds. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent e34cac3 commit 368aa6d

12 files changed

Lines changed: 530 additions & 60 deletions

File tree

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

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -832,20 +832,21 @@ function* observeFile(
832832
if (listed.code !== 0) {
833833
return unreadable(view, `the tree of commit ${commit}`);
834834
}
835-
const entry = treeEntry(listed.stdout);
836-
if (entry === undefined) {
835+
const entry = treeEntry(listed.stdout, request.path);
836+
if (entry.kind === "absent") {
837837
return refuse(
838838
view,
839839
"absent-path",
840840
`read commit ${commit}, which holds nothing at ${JSON.stringify(request.path)}. Read a ` +
841841
"path that commit holds, or list them with <Git.Files>.",
842842
);
843843
}
844-
if (entry.type !== "blob") {
844+
if (entry.kind === "directory" || entry.type !== "blob") {
845+
const what = entry.kind === "directory" ? "tree" : entry.type;
845846
return refuse(
846847
view,
847848
"nontext-blob",
848-
`read commit ${commit}, where ${JSON.stringify(request.path)} is a ${entry.type} rather ` +
849+
`read commit ${commit}, where ${JSON.stringify(request.path)} is a ${what} rather ` +
849850
"than a file. A directory and a nested checkout's recorded commit are not text to read.",
850851
);
851852
}
@@ -891,28 +892,47 @@ function* observeFile(
891892
});
892893
}
893894

895+
/** What the commit holds at the exact path a read asked for. */
896+
type TreeEntry =
897+
/** The entry at that path, with the object and type Git reported for it. */
898+
| { readonly kind: "entry"; readonly type: string; readonly object: string }
899+
/** A directory: the path names no entry, and entries beneath it exist. */
900+
| { readonly kind: "directory" }
901+
| { readonly kind: "absent" };
902+
894903
/**
895-
* The entry one `ls-tree` record describes, or `undefined` for no record.
904+
* The entry this commit holds at exactly the path that was asked for.
905+
*
906+
* Each record is `<mode> <type> <object>`, a tab, then the path. The path is
907+
* what decides the answer, because the argument Git received is a *pathspec*
908+
* rather than a lookup: `dir` matches the directory entry itself and `dir/`
909+
* matches the things inside it. Taking the first record would answer a read of
910+
* `dir/` with the blob at `dir/inside.txt` — a file the caller never named,
911+
* under a path the result would claim was a directory.
896912
*
897-
* `<mode> <type> <object>` then a tab and the path. The path is not read back:
898-
* what the entry is asked for is the type and the object, and the path this
899-
* read answers with is the admitted spelling the caller wrote.
913+
* So a path that matched records but is not one of them is a directory, which
914+
* is refused as the tree it is; a path that matched nothing is absent.
900915
*/
901-
function treeEntry(
902-
output: string,
903-
): { readonly type: string; readonly object: string } | undefined {
904-
const record = output.split("\u0000")[0] ?? "";
905-
const tab = record.indexOf("\t");
906-
if (tab < 0) {
907-
return undefined;
908-
}
909-
const fields = record.slice(0, tab).split(" ");
910-
const type = fields[1];
911-
const object = fields[2];
912-
if (type === undefined || object === undefined) {
913-
return undefined;
916+
function treeEntry(output: string, path: string): TreeEntry {
917+
let matched = false;
918+
for (const record of output.split("\u0000")) {
919+
const tab = record.indexOf("\t");
920+
if (tab < 0) {
921+
continue;
922+
}
923+
matched = true;
924+
if (record.slice(tab + 1) !== path) {
925+
continue;
926+
}
927+
const fields = record.slice(0, tab).split(" ");
928+
const type = fields[1];
929+
const object = fields[2];
930+
if (type === undefined || object === undefined) {
931+
continue;
932+
}
933+
return { kind: "entry", type, object };
914934
}
915-
return { type, object };
935+
return matched ? { kind: "directory" } : { kind: "absent" };
916936
}
917937

918938
/**

‎packages/git/src/deno/retained-repository/git.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ import type { SlotKind } from "../run-composition/leases.ts";
122122
import type { SelectionRegistry } from "../selections.ts";
123123
import { holdManagedPlacement } from "./leases.ts";
124124
import { retainedRecordAhead } from "./local.ts";
125-
import { placementSubject } from "./selections.ts";
125+
import { nativeGitTurn, placementSubject } from "./selections.ts";
126126
import type { ReplPlacementRegistration } from "./selections.ts";
127127

128128
/**
@@ -420,7 +420,7 @@ function* live(
420420
): Operation<unknown> {
421421
const target = yield* routing();
422422
return yield* entry.serialize(
423-
`git ${target.registration.placement.commonDirectory}`,
423+
nativeGitTurn(target.registration.placement.commonDirectory),
424424
function* () {
425425
yield* own(entry, target);
426426
return yield* once();

‎packages/git/src/deno/retained-repository/observations.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import { performGitObservation } from "../composition/observations.ts";
9898
import type { ObservationPlace } from "../composition/observations.ts";
9999
import type { RepositoryHost } from "../composition/host.ts";
100100
import { selectCheckout } from "../run-composition/operations.ts";
101+
import { nativeGitTurn } from "./selections.ts";
101102
import type { SlotKind } from "../run-composition/leases.ts";
102103
import type { SelectionRegistry } from "../selections.ts";
103104
import { holdManagedPlacement } from "./leases.ts";
@@ -358,6 +359,19 @@ function readback(
358359
* slot another entry holds a fatal diagnostic the person can act on rather than
359360
* a durable outcome — and keeps temporary contention out of the journal
360361
* entirely.
362+
*
363+
* The turn is `nativeGitTurn()`, the same one this entry's Add, Switch, Commit
364+
* and publication take for the same shared Git directory. A read runs several
365+
* Git commands against one private index copy and one pair of resolved commit
366+
* ids, and a mutation of this entry's own entering between them would move the
367+
* repository underneath a sequence already in progress. A key of this read's
368+
* own would be a second queue: the serializer is shared, but a turn is the key.
369+
*
370+
* It is held for the whole of `once()` — the native work, its joined teardown
371+
* and the coordinator's acknowledged append — so the turn ends when the
372+
* observation is recorded rather than when Git stops running. Cross-entry
373+
* independence is unaffected: this queue is per entry, and another entry's
374+
* mutation lease is a different mechanism.
361375
*/
362376
function* live(
363377
entry: ReplObservationEntry,
@@ -366,7 +380,7 @@ function* live(
366380
): Operation<unknown> {
367381
const target = yield* routing();
368382
return yield* entry.serialize(
369-
`observation ${target.registration.placement.commonDirectory}`,
383+
nativeGitTurn(target.registration.placement.commonDirectory),
370384
function* () {
371385
yield* own(entry, target);
372386
return yield* once();

‎packages/git/src/deno/retained-repository/push.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ import type { SelectionRegistry } from "../selections.ts";
135135
import { holdManagedPlacement } from "./leases.ts";
136136
import type { ReplGitCheckout } from "./git.ts";
137137
import { retainedRecordAhead } from "./local.ts";
138-
import { placementSubject } from "./selections.ts";
138+
import { nativeGitTurn, placementSubject } from "./selections.ts";
139139
import type { ReplPlacementRegistration } from "./selections.ts";
140140

141141
/**
@@ -416,7 +416,7 @@ function* resolve(entry: ReplPushEntry, received: ReplPushPlace): Operation<Repl
416416
// `<Git.Commit>` of the same repository would describe a moment neither of
417417
// them is at. The turn is held for these two short reads and released before
418418
// anything is published: a network round trip never holds it.
419-
const read = yield* entry.serialize(`git ${placement.commonDirectory}`, function* () {
419+
const read = yield* entry.serialize(nativeGitTurn(placement.commonDirectory), function* () {
420420
const checkout = liveCheckout(git, routed.checkout, received.workingDirectory);
421421
const branch = yield* currentBranch(checkout.git, checkout.directory);
422422
if (branch === undefined) {

‎packages/git/src/deno/retained-repository/selections.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,20 @@ const placementCoordinator: LiveDurableOperationCoordinator = {
807807
* rather than stranding it, which is the one case the two cleanups have to tell
808808
* apart.
809809
*/
810+
/**
811+
* The one turn an entry's native Git for one shared Git directory takes.
812+
*
813+
* Named here rather than spelled at each caller, because the key *is* the
814+
* exclusion: every operation of this entry that runs `git` against one common
815+
* directory — Add, Switch, Commit, the publication's observation, and the four
816+
* reads — has to name the same string. A caller that spelled its own would be
817+
* handed its own queue by a serializer the two were sharing, which looks like
818+
* participation and is not.
819+
*/
820+
export function nativeGitTurn(commonDirectory: string): string {
821+
return `git ${commonDirectory}`;
822+
}
823+
810824
export function slotTurns(): <T>(key: string, work: () => Operation<T>) => Operation<T> {
811825
interface Waiting {
812826
readonly granted: WithResolvers<void>;

‎packages/git/src/retained-repository/admission.ts‎

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
replPlacementRecordName,
4848
REPL_REPOSITORY_PLACEMENT,
4949
} from "./placement-records.ts";
50+
import type { ReplPlacement } from "./placement-records.ts";
5051
import {
5152
parseReplGitRecord,
5253
replGitFingerprint,
@@ -57,6 +58,8 @@ import {
5758
parseReplObservationRecord,
5859
replObservationFingerprint,
5960
replObservationRecordName,
61+
routedByPlacement,
62+
selectedByPlacement,
6063
REPL_REPOSITORY_OBSERVATION,
6164
} from "./observation-records.ts";
6265

@@ -76,7 +79,9 @@ export type ReplRepositoryRefusal =
7679
/** A record stating a version this host does not implement. */
7780
| "unknown-version"
7881
/** A record whose name is not the name of the request it carries. */
79-
| "request-fingerprint";
82+
| "request-fingerprint"
83+
/** An observation naming a route no preceding placement in it selected. */
84+
| "unrouted-observation";
8085

8186
/**
8287
* A history this host cannot continue from.
@@ -104,6 +109,10 @@ export class ReplRepositoryHistoryError extends StaleInputError {
104109
* feature's type that does not parse is, and refuses.
105110
*/
106111
export function* admitReplRepositoryHistory(retained: readonly DurableEvent[]): Operation<void> {
112+
// The placements this history has selected *so far*. An observation names the
113+
// checkout it read, and nothing outside this snapshot may authorize one: the
114+
// route has to be a placement a preceding record in the same history made.
115+
const placements: ReplPlacement[] = [];
107116
for (const event of retained) {
108117
if (event.type !== "yield") {
109118
continue;
@@ -137,15 +146,18 @@ export function* admitReplRepositoryHistory(retained: readonly DurableEvent[]):
137146
continue;
138147
}
139148
if (type === REPL_REPOSITORY_PLACEMENT) {
140-
yield* admitPlacement(payload, event.description.name);
149+
const placement = yield* admitPlacement(payload, event.description.name);
150+
if (placement !== undefined) {
151+
placements.push(placement);
152+
}
141153
continue;
142154
}
143155
if (type === REPL_REPOSITORY_GIT) {
144156
yield* admitGit(payload, event.description.name);
145157
continue;
146158
}
147159
if (type === REPL_REPOSITORY_OBSERVATION) {
148-
yield* admitObservation(payload, event.description.name);
160+
yield* admitObservation(payload, event.description.name, placements);
149161
continue;
150162
}
151163
yield* admitLocal(payload, event.description.name);
@@ -179,19 +191,32 @@ function* admitGit(payload: unknown, name: string): Operation<void> {
179191
}
180192

181193
/**
182-
* Hold one recorded observation to the request it carries.
194+
* Hold one recorded observation to the request it carries, and to the route
195+
* this history actually selected.
183196
*
184-
* The same two questions the other records answer, over a record whose success
185-
* side is one page: a saved page that does not describe the request beside it —
197+
* The two questions the other records answer, over a record whose success side
198+
* is one page: a saved page that does not describe the request beside it —
186199
* another query, another comparison, a continuation on another commit, more
187200
* records than the limit asked for — does not parse, so it is refused here
188201
* rather than binding a listing on replay.
189202
*
190-
* It runs no `git` and opens nothing. Whether the paths a record names are
191-
* still tracked is not this reader's question, and not a question a
192-
* continuation asks either.
203+
* And one more, which the other record types do not need. An observation names
204+
* the checkout it was read from, and a parseable record can name any checkout
205+
* at all: every required member can be present, the request, name and
206+
* fingerprint untouched, and the root somewhere this entry never selected.
207+
* Nothing downstream would catch it — a completed terminal is reused before the
208+
* body that checks the live registry is ever entered — so a route no preceding
209+
* placement authorizes is refused here, before completed or unfinished reuse.
210+
*
211+
* It is decided from this history alone. No checkout is opened, no path is
212+
* canonicalized and no `git` runs: a continuation whose checkout has since been
213+
* deleted still answers, which is the whole point of retaining the page.
193214
*/
194-
function* admitObservation(payload: unknown, name: string): Operation<void> {
215+
function* admitObservation(
216+
payload: unknown,
217+
name: string,
218+
placements: readonly ReplPlacement[],
219+
): Operation<void> {
195220
const record = parseReplObservationRecord(payload);
196221
if (record === undefined) {
197222
throw malformed(REPL_REPOSITORY_OBSERVATION);
@@ -203,6 +228,18 @@ function* admitObservation(payload: unknown, name: string): Operation<void> {
203228
if (name !== replObservationRecordName({ operation: record.operation, fingerprint })) {
204229
throw misnamed(REPL_REPOSITORY_OBSERVATION);
205230
}
231+
if (!selectedByPlacement(placements, record.request)) {
232+
throw unrouted(
233+
"names a selection no placement in this history made, so this history does not say " +
234+
"which checkout the read was routed to",
235+
);
236+
}
237+
if (!routedByPlacement(placements, record.request.repository, record.checkout)) {
238+
throw unrouted(
239+
"names a checkout no placement in this history selected, so this history does not say " +
240+
"where the read ran",
241+
);
242+
}
206243
}
207244

208245
function* admitLocal(payload: unknown, name: string): Operation<void> {
@@ -235,7 +272,10 @@ function* admitLocal(payload: unknown, name: string): Operation<void> {
235272
* It opens nothing. Whether the checkout a placement names is still there is
236273
* not this reader's question and is not a question a continuation asks.
237274
*/
238-
function* admitPlacement(payload: unknown, name: string): Operation<void> {
275+
function* admitPlacement(
276+
payload: unknown,
277+
name: string,
278+
): Operation<ReplPlacement | undefined> {
239279
const record = parseReplPlacementRecord(payload);
240280
if (record === undefined) {
241281
throw malformed(REPL_REPOSITORY_PLACEMENT);
@@ -247,6 +287,8 @@ function* admitPlacement(payload: unknown, name: string): Operation<void> {
247287
if (name !== replPlacementRecordName({ kind: record.kind, fingerprint })) {
248288
throw misnamed(REPL_REPOSITORY_PLACEMENT);
249289
}
290+
// A refused selection placed nothing, so it authorizes no route.
291+
return record.outcome.ok ? record.outcome.value : undefined;
250292
}
251293

252294
function* admitSelection(payload: unknown, name: string): Operation<void> {
@@ -277,6 +319,13 @@ function changed(type: string): ReplRepositoryHistoryError {
277319
);
278320
}
279321

322+
function unrouted(sentence: string): ReplRepositoryHistoryError {
323+
return refusal(
324+
"unrouted-observation",
325+
`a retained ${REPL_REPOSITORY_OBSERVATION} ${sentence}.`,
326+
);
327+
}
328+
280329
function misnamed(type: string): ReplRepositoryHistoryError {
281330
return refusal(
282331
"request-fingerprint",

0 commit comments

Comments
 (0)