Skip to content
Myr

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

7 files+494 added−21 removed

modified:apps/cli/src/local/check.ts+27 added-11 removed
203 unmodified lines
204 )205 );206 });207208/** 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 */215const 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 });228229 if (Option.isSome(given)) {216 return given.value;230 return { claimBase: held, given: given.value };231 }232233 if (Option.isNone(claim)) {220 return "main";234 return { claimBase: held, given: "main" };235 }236223 const { base } = claim.value;224 const held = yield* Effect.exit(commitOf(root, base));225226 if (Exit.isSuccess(held)) {227 return base;237 if (Option.isSome(held)) {238 return { claimBase: held, given: held.value };239 }240241 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 );246236 return "main";247 return { claimBase: held, given: "main" };248 });249250const syntheticClaim = (input: {251 readonly ask: Ask;
320 unmodified lines
572 ]),573 () => SOMEONE574 );575565 const base = yield* baseOf(context.root, flags.base, claim);576 const { claimBase, given: base } = yield* baseOf(577 context.root,578 flags.base,579 claim580 );581582 const bundler = yield* Effect.serviceOption(ConfigBundler);583584 const policy = yield* localPolicy(context.root, base, {
43 unmodified lines
628 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 lines
modified:apps/cli/src/local/facts.ts+27 added-6 removed
2 unmodified lines
3import { ObjectId, RepoHost } from "@repo/artifacts/repo-host";4import type { RepoNotFound } from "@repo/artifacts/repo-host";5import { RepoHostLocal } from "@repo/artifacts/repo-host/local";6import type { PushFacts, PushTarget } from "@repo/gate/facts";7import { answerBase } from "@repo/git/answer-base";8import { addedLines, treeDiff } from "@repo/git/tree-diff";9import type { Change } from "@repo/git/tree-diff";10import type { Ask } from "@repo/protocol/domain/ask";11import type { ClaimView } from "@repo/protocol/domain/claim";12import { RefNameText } from "@repo/protocol/domain/common";13import type { Policy } from "@repo/protocol/domain/policy";13import { CommitSha, RepoName, TreeSha } from "@repo/protocol/ids";14import { CommitSha, LandedSha, RepoName, TreeSha } from "@repo/protocol/ids";15import type { Principal } from "@repo/protocol/principal";16import {17 Effect,18 FileSystem,
90 unmodified lines
109 ? sha110 : yield* new UnknownRevision({ revision, root });111 });112112const mergeBase = (root: string, base: string, head: CommitSha) =>113const 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();117118 return ran.exitCode === 0 && Schema.is(CommitSha)(sha)119 ? sha
101 unmodified lines
221 });222223export 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 lines
291> =>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);296297 const claimBase = yield* Option.match(input.claimBase, {298 onNone: () => mergeBase(input.root, landed, head),299 onSome: Effect.succeed,300 });301302 const opened = yield* openWorking(input.root, [landed, claimBase, head]);303304 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 });313314 const changes = yield* treeDiff(315 opened.name,316 yield* treeOf(opened.name, base),317 yield* treeOf(opened.name, head)
22 unmodified lines
modified:apps/cli/test/local/facts.test.ts+2 added
107 unmodified lines
108 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 lines
145 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 lines
modified:apps/platform/test/gateway.test.ts+125 added
46 unmodified lines
47const notesRef = `refs/notes/${Brand.slug}`;4849const parseIso = AskId.make("parse-iso");5051/** A sibling ask whose opening lands under `packages/b`, moving main under a claim. */52const parseB = AskId.make("parse-b");5354const fresh = ProjectName.make("alice/fresh");5556const 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);6162const askOf = (id: AskId) =>
285 unmodified lines
348 30_000349 );350351 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`;358359 // The sibling lands after the claim, so the claim's base stays main₀.360 const { landed: moved } = yield* openAsk(api, landed, askOf(parseB));361362 yield* gitOk(363 root,364 ...as(tokens.alice),365 "clone",366 "--quiet",367 urlOf(claim.fork),368 clone369 );370371 const own = yield* commit(clone, "parse the year", {372 "packages/parser/src/parse.ts":373 "export const parse = (s) => ({ year: 2026 });\n",374 });375376 yield* gitOk(377 clone,378 "pull",379 "--quiet",380 "--no-rebase",381 urlOf(datekit),382 "main"383 );384385 const head = CommitSha.make(yield* gitOk(clone, "rev-parse", "HEAD"));386387 const pushed = yield* git(388 clone,389 ...as(tokens.alice),390 "push",391 "origin",392 "HEAD:main"393 );394395 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);403404 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 );421422 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`;429430 yield* openAsk(api, landed, askOf(parseB));431432 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 );453454 const pushed = yield* git(455 clone,456 ...as(tokens.alice),457 "push",458 "origin",459 "HEAD:main"460 );461462 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 );471472 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 lines
added:packages/git/src/answer-base.ts+119 added
1import { RepoHost, Revision } from "@repo/artifacts/repo-host";2import type { RepoNotFound } from "@repo/artifacts/repo-host";3import type { CommitSha, LandedSha, RepoName } from "@repo/protocol/ids";4import { Effect, Option } from "effect";56/** How far back from `landed` main's landings are read for the base, in one log. */7const LANDINGS_READ = 256;89/** How many of the head's own commits are read before the walk gives up. */10const COMMITS_READ = 512;1112const READS_AT_ONCE = 8;1314export 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}2425/**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 */35export 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 }4243 const host = yield* RepoHost;4445 const chain = yield* host.log(46 input.canonical,47 Revision.Commit({ sha: input.landed }),48 LANDINGS_READ49 );5051 // 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]));5657 if (landings.length === 0) {58 return input.base;59 }6061 const seen = new Set<CommitSha>([input.head]);62 let frontier: readonly CommitSha[] = [input.head];63 let best = Option.none<number>();64 let reads = 0;6566 // 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[] = [];7071 for (const sha of frontier) {72 const found = rank.get(sha);7374 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 }8586 if (Option.contains(best, 0)) {87 return input.landed;88 }8990 const read = yield* Effect.forEach(91 unread,92 (sha) => host.readCommit(input.repo, sha),93 { concurrency: READS_AT_ONCE }94 );9596 reads += unread.length;9798 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 }106107 seen.add(parent);108109 return true;110 }),111 })112 );113 }114115 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
1import { describe, expect, it } from "@effect/vitest";2import { RefName, RepoHost, RepoName } from "@repo/artifacts/repo-host";3import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";4import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";5import { CommitSha, LandedSha } from "@repo/protocol/ids";6import { gitClient } from "@repo/testkit/git-client";7import type { Files } from "@repo/testkit/git-client";8import { Effect, Option } from "effect";910import { answerBase } from "../src/answer-base.ts";1112const canonical = RepoName.make("datekit");1314const fork = RepoName.make("datekit--parse-iso--alice");1516const main = RefName.make("refs/heads/main");1718const 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];2223const 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];2728const 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];3233const 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];3738/**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 */42const 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]);4849 yield* repos.create(canonical);50 yield* repos.create(fork);5152 const push = (repo: RepoName, to: CommitSha) =>53 repos.push(repo, client.source, [{ from: Option.none(), ref: main, to }]);5455 yield* push(canonical, main1);5657 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 );7374 return { chosen, client, main0, main1, own };75});7677describe("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;8182 expect(yield* chosen({ base: main0, head: own, landed: main1 })).toBe(83 main084 );85 }).pipe(Effect.provide(RepoHostMemory))86 );8788 it.effect("is the moved main once the answer merges it", () =>89 Effect.gen(function* () {90 const { chosen, client, main0, main1, own } = yield* history;9192 const merged = yield* client.commit("merge main", bothMoved, [93 own,94 main1,95 ]);9697 expect(yield* chosen({ base: main0, head: merged, landed: main1 })).toBe(98 main199 );100 }).pipe(Effect.provide(RepoHostMemory))101 );102103 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]);107108 expect(yield* chosen({ base: main0, head: rebased, landed: main1 })).toBe(109 main1110 );111 }).pipe(Effect.provide(RepoHostMemory))112 );113114 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]);121122 yield* repos.push(canonical, client.source, [123 { from: Option.some(main1), ref: main, to: main2 },124 ]);125126 const merged = yield* client.commit("merge main", bothMoved, [127 own,128 main1,129 ]);130131 expect(132 yield* chosen({ base: main0, head: merged, landed: main2 })133 ).toBe(main1);134 }).pipe(Effect.provide(RepoHostMemory))135 );136137 it.effect("reads nothing while main has not moved since the claim", () =>138 Effect.gen(function* () {139 const { chosen, main0, own } = yield* history;140141 expect(yield* chosen({ base: main0, head: own, landed: main0 })).toBe(142 main0143 );144 }).pipe(Effect.provide(RepoHostMemory))145 );146147 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);154155 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 lines
44import { pushStages, refusingStage, runGate } from "@repo/gate/gate";45import type { StageRefusal } from "@repo/gate/gate";46import { scan } from "@repo/gate/stages/shape";47import { verdictOf } from "@repo/gate/verdict";48import { answerBase } from "@repo/git/answer-base";49import { patchId } from "@repo/git/patch-id";50import { formatLanding } from "@repo/git/trailers";51import { HarnessAudit } from "@repo/protocol/audit";52import {
4560 unmodified lines
4613 onSome: ({ row, state }) =>4614 viewOf(h.policy, ask, state, row.pinned),4615 });46164616 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 );46244625 // 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 );46374638 const refused = yield* gateOf(4639 withLayerFacts(4640 h,4641 writes,
179 unmodified lines
4821 onSome: ({ row, state }) =>4822 viewOf(h.policy, ask, state, row.pinned),4823 });48244825 // 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)));48374838 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 lines
4855 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 lines
4882 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