Merge project/claim-base-moves into main
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by oscabriel · committed by oscabriel · parents 3c2c19c, d0d1021 · Browse files · 7 files·+494 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+494 added−21 removed
- tests:+290 added−0 removed
- other:+204 added−21 removed
Provenance
- Commit
db02276- Tree
e780d0c- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent 1
3c2c19c- Parent 2
d0d1021
7 files changed+494 added−21 removed
modified:apps/cli/src/local/check.ts+27 added-11 removed
203 unmodified lines204 )205 );206 });207 208-/** The gateway diffs a fork push from the claim's base, so an answer committed on the clone's own main is still seen. */208+/**209+ * The gateway diffs a fork push from the claim's base, so an answer committed210+ * on the clone's own main is still seen; from the newest landing the answer211+ * contains since, so one that merged a moved main is judged on its own212+ * change. `--base` names main as the caller has it; without it, the claim's213+ * base stands for main, and the diff starts there.214+ */215 const baseOf = (216 root: string,217 given: Option.Option<string>,218 claim: Option.Option<ClaimView>219 ) =>220 Effect.gen(function* () {221+ const held = yield* Option.match(claim, {222+ onNone: () => Effect.succeedNone,223+ onSome: ({ base }) =>224+ Effect.map(Effect.exit(commitOf(root, base)), (exit) =>225+ Exit.isSuccess(exit) ? Option.some(base) : Option.none()226+ ),227+ });228+229 if (Option.isSome(given)) {216- return given.value;230+ return { claimBase: held, given: given.value };231 }232 233 if (Option.isNone(claim)) {220- return "main";234+ return { claimBase: held, given: "main" };235 }236 223- const { base } = claim.value;224- const held = yield* Effect.exit(commitOf(root, base));225-226- if (Exit.isSuccess(held)) {227- return base;237+ if (Option.isSome(held)) {238+ return { claimBase: held, given: held.value };239 }240 241 yield* Output.use((output) =>242 output.note(232- `Your claim was made at ${base}, which this repo doesn't have, so the diff starts at main; fetch the project's main, or pass --base.`243+ `Your claim was made at ${claim.value.base}, which this repo doesn't have, so the diff starts at main; fetch the project's main, or pass --base.`244 )245 );246 236- return "main";247+ return { claimBase: held, given: "main" };248 });249 250 const syntheticClaim = (input: {251 readonly ask: Ask;320 unmodified lines572 ]),573 () => SOMEONE574 );575 565- const base = yield* baseOf(context.root, flags.base, claim);576+ const { claimBase, given: base } = yield* baseOf(577+ context.root,578+ flags.base,579+ claim580+ );581 582 const bundler = yield* Effect.serviceOption(ConfigBundler);583 584 const policy = yield* localPolicy(context.root, base, {43 unmodified lines628 policy,629 })630 )631 : claim,632+ claimBase,633 claimsHeld: Option.match(claims, {634 onNone: () => 1,635 onSome: (held) => held.filter(isLive).length,636 }),173 unmodified linesmodified:apps/cli/src/local/facts.ts+27 added-6 removed
2 unmodified lines3 import { ObjectId, RepoHost } from "@repo/artifacts/repo-host";4 import type { RepoNotFound } from "@repo/artifacts/repo-host";5 import { RepoHostLocal } from "@repo/artifacts/repo-host/local";6 import type { PushFacts, PushTarget } from "@repo/gate/facts";7+import { answerBase } from "@repo/git/answer-base";8 import { addedLines, treeDiff } from "@repo/git/tree-diff";9 import type { Change } from "@repo/git/tree-diff";10 import type { Ask } from "@repo/protocol/domain/ask";11 import type { ClaimView } from "@repo/protocol/domain/claim";12 import { RefNameText } from "@repo/protocol/domain/common";13 import type { Policy } from "@repo/protocol/domain/policy";13-import { CommitSha, RepoName, TreeSha } from "@repo/protocol/ids";14+import { CommitSha, LandedSha, RepoName, TreeSha } from "@repo/protocol/ids";15 import type { Principal } from "@repo/protocol/principal";16 import {17 Effect,18 FileSystem,90 unmodified lines109 ? sha110 : yield* new UnknownRevision({ revision, root });111 });112 112-const mergeBase = (root: string, base: string, head: CommitSha) =>113+const mergeBase = (root: string, base: CommitSha, head: CommitSha) =>114 Effect.gen(function* () {114- const from = yield* commitOf(root, base);115- const ran = yield* runGit(root, ["merge-base", from, head]);115+ const ran = yield* runGit(root, ["merge-base", base, head]);116 const sha = ran.stdout.trim();117 118 return ran.exitCode === 0 && Schema.is(CommitSha)(sha)119 ? sha101 unmodified lines221 });222 223 export interface LocalFactsInput {224 readonly root: string;225+ /** Main as the caller has it: the newest landing the answer may be judged from. */226 readonly base: string;227+ /**228+ * The claim's base, when the repo has it: where the answer left main. Absent,229+ * the merge base of `base` and `head` stands for it.230+ */231+ readonly claimBase: Option.Option<CommitSha>;232 readonly head: string;233 readonly policy: Policy;234 readonly ask: Option.Option<Ask>;235 readonly claim: Option.Option<ClaimView>;55 unmodified lines291 > =>292 Effect.scoped(293 Effect.gen(function* () {294 const head = yield* commitOf(input.root, input.head);289- const base = yield* mergeBase(input.root, input.base, head);290- const opened = yield* openWorking(input.root, [base, head]);295+ const landed = yield* commitOf(input.root, input.base);296 297+ const claimBase = yield* Option.match(input.claimBase, {298+ onNone: () => mergeBase(input.root, landed, head),299+ onSome: Effect.succeed,300+ });301+302+ const opened = yield* openWorking(input.root, [landed, claimBase, head]);303+304 return yield* Effect.gen(function* () {305+ // As the gateway chooses it: the newest landing the head contains.306+ const base = yield* answerBase({307+ base: claimBase,308+ canonical: opened.name,309+ head,310+ landed: LandedSha.make(landed),311+ repo: opened.name,312+ });313+314 const changes = yield* treeDiff(315 opened.name,316 yield* treeOf(opened.name, base),317 yield* treeOf(opened.name, head)22 unmodified linesmodified:apps/cli/test/local/facts.test.ts+2 added
107 unmodified lines108 const facts = yield* localFacts({109 ask: Option.none(),110 base: "main",111 claim: Option.none(),112+ claimBase: Option.none(),113 claimsHeld: 0,114 head: "HEAD",115 policy,116 pusher,28 unmodified lines145 const facts = yield* localFacts({146 ask: Option.none(),147 base: "origin/main",148 claim: Option.none(),149+ claimBase: Option.none(),150 claimsHeld: 0,151 head: "HEAD",152 policy,153 pusher,245 unmodified linesmodified:apps/platform/test/gateway.test.ts+125 added
46 unmodified lines47 const notesRef = `refs/notes/${Brand.slug}`;48 49 const parseIso = AskId.make("parse-iso");50 51+/** A sibling ask whose opening lands under `packages/b`, moving main under a claim. */52+const parseB = AskId.make("parse-b");53+54 const fresh = ProjectName.make("alice/fresh");55 56 const scenario = aProject({ name: datekit, people: ["bob"] }).pipe(57 withAsk({ id: parseIso, territory: "packages/parser" }),58+ withAsk({ id: parseB, territory: "packages/b" }),59 withVouches(["bob"])60 );61 62 const askOf = (id: AskId) =>285 unmodified lines348 30_000349 );350 351 it.live(352+ "5a. an answer that merged a main moved by a sibling's landing is judged on its own change: the push passes, and its base is that main",353+ () =>354+ Effect.gen(function* () {355+ const { api, landed, openAsk, root, urlOf } = yield* withDatekit;356+ const { claim } = yield* api.claim(alice, parseIso).pipe(Effect.orDie);357+ const clone = `${root}/alices`;358+359+ // The sibling lands after the claim, so the claim's base stays main₀.360+ const { landed: moved } = yield* openAsk(api, landed, askOf(parseB));361+362+ yield* gitOk(363+ root,364+ ...as(tokens.alice),365+ "clone",366+ "--quiet",367+ urlOf(claim.fork),368+ clone369+ );370+371+ const own = yield* commit(clone, "parse the year", {372+ "packages/parser/src/parse.ts":373+ "export const parse = (s) => ({ year: 2026 });\n",374+ });375+376+ yield* gitOk(377+ clone,378+ "pull",379+ "--quiet",380+ "--no-rebase",381+ urlOf(datekit),382+ "main"383+ );384+385+ const head = CommitSha.make(yield* gitOk(clone, "rev-parse", "HEAD"));386+387+ const pushed = yield* git(388+ clone,389+ ...as(tokens.alice),390+ "push",391+ "origin",392+ "HEAD:main"393+ );394+395+ const receipt = yield* api396+ .submitAnswer(alice, {397+ ask: parseIso,398+ base: claim.base,399+ head,400+ requestId: pushRequestId(alice.handle, head),401+ })402+ .pipe(Effect.orDie);403+404+ expect({405+ base: claim.base,406+ exitCode: pushed.exitCode,407+ merged: head !== own,408+ remote: remoteLines(pushed.stderr),409+ }).toStrictEqual({410+ base: landed,411+ exitCode: 0,412+ merged: true,413+ remote: [414+ `remote: checking ${parseIso} at ${head.slice(0, 7)}; follow with ${Brand.cli} status`,415+ ],416+ });417+ expect(receipt.base).toBe(moved);418+ }).pipe(Effect.provide(NodeServices.layer)),419+ 30_000420+ );421+422+ it.live(423+ "5b. an answer that merged the moved main and edits the sibling's territory too is OutOfScope for its own path alone",424+ () =>425+ Effect.gen(function* () {426+ const { api, landed, openAsk, root, urlOf } = yield* withDatekit;427+ const { claim } = yield* api.claim(alice, parseIso).pipe(Effect.orDie);428+ const clone = `${root}/alices`;429+430+ yield* openAsk(api, landed, askOf(parseB));431+432+ yield* gitOk(433+ root,434+ ...as(tokens.alice),435+ "clone",436+ "--quiet",437+ urlOf(claim.fork),438+ clone439+ );440+ yield* commit(clone, "parse the year, and b too", {441+ "packages/b/src/b.ts": "export const b = 2;\n",442+ "packages/parser/src/parse.ts":443+ "export const parse = (s) => ({ year: 2026 });\n",444+ });445+ yield* gitOk(446+ clone,447+ "pull",448+ "--quiet",449+ "--no-rebase",450+ urlOf(datekit),451+ "main"452+ );453+454+ const pushed = yield* git(455+ clone,456+ ...as(tokens.alice),457+ "push",458+ "origin",459+ "HEAD:main"460+ );461+462+ expect(pushed.exitCode).toBe(1);463+ expect(remoteLines(pushed.stderr)).toStrictEqual(464+ explain([465+ new OutOfScope({ paths: [RepoPath.make("packages/b/src/b.ts")] }),466+ ]).map((line) => `remote: ${line}`)467+ );468+ }).pipe(Effect.provide(NodeServices.layer)),469+ 30_000470+ );471+472+ it.live(473 "6. an unknown token is a 401 asking for Basic, and so is a push with no credential",474 () =>475 Effect.gen(function* () {476 const { origin, root, urlOf } = yield* withDatekit;486 unmodified linesadded:packages/git/src/answer-base.ts+119 added
1+import { RepoHost, Revision } from "@repo/artifacts/repo-host";2+import type { RepoNotFound } from "@repo/artifacts/repo-host";3+import type { CommitSha, LandedSha, RepoName } from "@repo/protocol/ids";4+import { Effect, Option } from "effect";5+6+/** How far back from `landed` main's landings are read for the base, in one log. */7+const LANDINGS_READ = 256;8+9+/** How many of the head's own commits are read before the walk gives up. */10+const COMMITS_READ = 512;11+12+const READS_AT_ONCE = 8;13+14+export interface AnswerBaseInput {15+ /** The repo `landed` and main's history live in. */16+ readonly canonical: RepoName;17+ /** The repo `head` lives in: the answer's fork, or the canonical repo itself. */18+ readonly repo: RepoName;19+ /** Where the answer's history left main's, as the claim's grant recorded it. */20+ readonly base: CommitSha;21+ readonly head: CommitSha;22+ readonly landed: LandedSha;23+}24+25+/**26+ * The base an answer is judged from: the newest of main's landings its head27+ * contains, from `landed` back to the claim's `base`. An answer that merged28+ * or rebased onto a main that moved under it is diffed from that main, so a29+ * sibling's landed paths are not its own; one that stayed on its base is30+ * diffed from the base, as before. The gateway, a submit and the local check31+ * choose the base here, so the same change keeps the same patch id wherever32+ * it is judged. A head that contains none of the landings, or whose history33+ * is too long to walk, keeps the claim's base, as it did before.34+ */35+export const answerBase = (36+ input: AnswerBaseInput37+): Effect.Effect<CommitSha, RepoNotFound, RepoHost> =>38+ Effect.gen(function* () {39+ if (input.base === input.landed) {40+ return input.base;41+ }42+43+ const host = yield* RepoHost;44+45+ const chain = yield* host.log(46+ input.canonical,47+ Revision.Commit({ sha: input.landed }),48+ LANDINGS_READ49+ );50+51+ // Newest first, cut before the claim's base: a chain the base is not on52+ // within the limit offers nothing, so the base stands.53+ const since = chain.findIndex((commit) => commit.sha === input.base);54+ const landings = since === -1 ? [] : chain.slice(0, since);55+ const rank = new Map(landings.map((commit, index) => [commit.sha, index]));56+57+ if (landings.length === 0) {58+ return input.base;59+ }60+61+ const seen = new Set<CommitSha>([input.head]);62+ let frontier: readonly CommitSha[] = [input.head];63+ let best = Option.none<number>();64+ let reads = 0;65+66+ // Breadth first over every parent, since a merge of main is a second67+ // parent; a landing or the base ends its own branch of the walk.68+ while (frontier.length > 0 && reads < COMMITS_READ) {69+ const unread: CommitSha[] = [];70+71+ for (const sha of frontier) {72+ const found = rank.get(sha);73+74+ if (found !== undefined) {75+ best = Option.some(76+ Option.match(best, {77+ onNone: () => found,78+ onSome: (held) => Math.min(held, found),79+ })80+ );81+ } else if (sha !== input.base) {82+ unread.push(sha);83+ }84+ }85+86+ if (Option.contains(best, 0)) {87+ return input.landed;88+ }89+90+ const read = yield* Effect.forEach(91+ unread,92+ (sha) => host.readCommit(input.repo, sha),93+ { concurrency: READS_AT_ONCE }94+ );95+96+ reads += unread.length;97+98+ frontier = read.flatMap((commit) =>99+ Option.match(commit, {100+ onNone: () => [],101+ onSome: ({ parents }) =>102+ parents.filter((parent) => {103+ if (seen.has(parent)) {104+ return false;105+ }106+107+ seen.add(parent);108+109+ return true;110+ }),111+ })112+ );113+ }114+115+ return Option.match(best, {116+ onNone: () => input.base,117+ onSome: (index) => landings[index]?.sha ?? input.base,118+ });119+ });added:packages/git/test/answer-base.test.ts+163 added
1+import { describe, expect, it } from "@effect/vitest";2+import { RefName, RepoHost, RepoName } from "@repo/artifacts/repo-host";3+import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";4+import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";5+import { CommitSha, LandedSha } from "@repo/protocol/ids";6+import { gitClient } from "@repo/testkit/git-client";7+import type { Files } from "@repo/testkit/git-client";8+import { Effect, Option } from "effect";9+10+import { answerBase } from "../src/answer-base.ts";11+12+const canonical = RepoName.make("datekit");13+14+const fork = RepoName.make("datekit--parse-iso--alice");15+16+const main = RefName.make("refs/heads/main");17+18+const start: Files = [19+ ["packages/a/src/a.ts", "export const a = 1;\n"],20+ ["packages/b/src/b.ts", "export const b = 1;\n"],21+];22+23+const bMoved: Files = [24+ ["packages/a/src/a.ts", "export const a = 1;\n"],25+ ["packages/b/src/b.ts", "export const b = 2;\n"],26+];27+28+const aMoved: Files = [29+ ["packages/a/src/a.ts", "export const a = 2;\n"],30+ ["packages/b/src/b.ts", "export const b = 1;\n"],31+];32+33+const bothMoved: Files = [34+ ["packages/a/src/a.ts", "export const a = 2;\n"],35+ ["packages/b/src/b.ts", "export const b = 2;\n"],36+];37+38+/**39+ * main₀ is the claim's base; a sibling lands main₁, touching `packages/b`;40+ * the answer's own commit changes `packages/a` on main₀.41+ */42+const history = Effect.gen(function* () {43+ const repos = yield* RepoHost;44+ const client = yield* gitClient(memoryFs());45+ const main0 = yield* client.commit("start", start);46+ const main1 = yield* client.commit("b lands", bMoved, [main0]);47+ const own = yield* client.commit("a moves", aMoved, [main0]);48+49+ yield* repos.create(canonical);50+ yield* repos.create(fork);51+52+ const push = (repo: RepoName, to: CommitSha) =>53+ repos.push(repo, client.source, [{ from: Option.none(), ref: main, to }]);54+55+ yield* push(canonical, main1);56+57+ const chosen = (input: {58+ readonly base: CommitSha;59+ readonly head: CommitSha;60+ readonly landed: CommitSha;61+ }) =>62+ push(fork, input.head).pipe(63+ Effect.andThen(64+ answerBase({65+ base: input.base,66+ canonical,67+ head: input.head,68+ landed: LandedSha.make(input.landed),69+ repo: fork,70+ })71+ )72+ );73+74+ return { chosen, client, main0, main1, own };75+});76+77+describe("the base an answer is judged from", () => {78+ it.effect("is the claim's base while the answer stays on it", () =>79+ Effect.gen(function* () {80+ const { chosen, main0, main1, own } = yield* history;81+82+ expect(yield* chosen({ base: main0, head: own, landed: main1 })).toBe(83+ main084+ );85+ }).pipe(Effect.provide(RepoHostMemory))86+ );87+88+ it.effect("is the moved main once the answer merges it", () =>89+ Effect.gen(function* () {90+ const { chosen, client, main0, main1, own } = yield* history;91+92+ const merged = yield* client.commit("merge main", bothMoved, [93+ own,94+ main1,95+ ]);96+97+ expect(yield* chosen({ base: main0, head: merged, landed: main1 })).toBe(98+ main199+ );100+ }).pipe(Effect.provide(RepoHostMemory))101+ );102+103+ it.effect("is the moved main once the answer is rebased onto it", () =>104+ Effect.gen(function* () {105+ const { chosen, client, main0, main1 } = yield* history;106+ const rebased = yield* client.commit("a moves", bothMoved, [main1]);107+108+ expect(yield* chosen({ base: main0, head: rebased, landed: main1 })).toBe(109+ main1110+ );111+ }).pipe(Effect.provide(RepoHostMemory))112+ );113+114+ it.effect(115+ "is the newest landing the head contains, not main's tip, when main moved on again",116+ () =>117+ Effect.gen(function* () {118+ const { chosen, client, main0, main1, own } = yield* history;119+ const repos = yield* RepoHost;120+ const main2 = yield* client.commit("b lands again", bMoved, [main1]);121+122+ yield* repos.push(canonical, client.source, [123+ { from: Option.some(main1), ref: main, to: main2 },124+ ]);125+126+ const merged = yield* client.commit("merge main", bothMoved, [127+ own,128+ main1,129+ ]);130+131+ expect(132+ yield* chosen({ base: main0, head: merged, landed: main2 })133+ ).toBe(main1);134+ }).pipe(Effect.provide(RepoHostMemory))135+ );136+137+ it.effect("reads nothing while main has not moved since the claim", () =>138+ Effect.gen(function* () {139+ const { chosen, main0, own } = yield* history;140+141+ expect(yield* chosen({ base: main0, head: own, landed: main0 })).toBe(142+ main0143+ );144+ }).pipe(Effect.provide(RepoHostMemory))145+ );146+147+ it.effect(148+ "keeps the claim's base when main's history does not reach it, or the head shares nothing with main",149+ () =>150+ Effect.gen(function* () {151+ const { chosen, client, main0, main1, own } = yield* history;152+ const elsewhere = CommitSha.make("9".repeat(40));153+ const orphan = yield* client.commit("unrelated", aMoved);154+155+ expect(156+ yield* chosen({ base: elsewhere, head: own, landed: main1 })157+ ).toBe(elsewhere);158+ expect(159+ yield* chosen({ base: main0, head: orphan, landed: main1 })160+ ).toBe(main0);161+ }).pipe(Effect.provide(RepoHostMemory))162+ );163+});modified:packages/project/src/project.ts+31 added-4 removed
43 unmodified lines44 import { pushStages, refusingStage, runGate } from "@repo/gate/gate";45 import type { StageRefusal } from "@repo/gate/gate";46 import { scan } from "@repo/gate/stages/shape";47 import { verdictOf } from "@repo/gate/verdict";48+import { answerBase } from "@repo/git/answer-base";49 import { patchId } from "@repo/git/patch-id";50 import { formatLanding } from "@repo/git/trailers";51 import { HarnessAudit } from "@repo/protocol/audit";52 import {4560 unmodified lines4613 onSome: ({ row, state }) =>4614 viewOf(h.policy, ask, state, row.pinned),4615 });4616 4616- const base = Option.getOrElse(4617+ const claimBase = Option.getOrElse(4618 Option.map(4619 Option.flatMap(claimed, ({ state }) => claimOf(state)),4620 (claim) => claim.base4621 ),4622 () => live.value.landed4623 );4624 4625+ // A fork the host lacks keeps the claim's base; the facts refuse it.4626+ const base = yield* withPorts(4627+ answerBase({4628+ base: claimBase,4629+ canonical: repo,4630+ head: input.after,4631+ landed: live.value.landed,4632+ repo: input.repo,4633+ })4634+ ).pipe(4635+ Effect.catchTag("RepoNotFound", () => Effect.succeed(claimBase))4636+ );4637+4638 const refused = yield* gateOf(4639 withLayerFacts(4640 h,4641 writes,179 unmodified lines4821 onSome: ({ row, state }) =>4822 viewOf(h.policy, ask, state, row.pinned),4823 });4824 4825+ // The base given is where the answer left main (the gateway gives4826+ // the claim's); it is judged from the newest landing its head4827+ // contains since, so the same change keeps the same patch id.4828+ const base = yield* withPorts(4829+ answerBase({4830+ base: input.base,4831+ canonical: repo,4832+ head: input.head,4833+ landed: live.value.landed,4834+ repo: fork,4835+ })4836+ ).pipe(Effect.catchTag("RepoNotFound", () => Effect.fail(refused)));4837+4838 const facts = yield* withLayerFacts(4839 h,4840 writes,4841 pushFacts({4842 ask,4816- base: input.base,4843+ base,4844 claim: view,4845 claimsHeld: heldBy(h, caller.handle),4846 head: input.head,4847 landed: live.value.landed,7 unmodified lines4855 Effect.catchTags({4856 MissingCommit: () =>4857 Effect.fail(4858 new StaleBase({4832- base: input.base,4859+ base,4860 conflicts: [],4861 main: live.value.landed,4862 })4863 ),18 unmodified lines4882 assisted: disclosed(4883 h.policy.value,4884 Option.map(own, ({ row }) => row)4885 ),4859- base: input.base,4886+ base,4887 credits: creditsFor(ask.value, caller.handle),4888 fork,4889 head: input.head,4890 project: name,660 unmodified lines