-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(bot): harden automated candidate delivery #2429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| export type GitTreeMode = "100644" | "100755" | "120000"; | ||
|
|
||
| export interface CandidateChange { | ||
| readonly path: string; | ||
| readonly mode: GitTreeMode; | ||
| /** Null deletes the path from the candidate tree. */ | ||
| readonly content: Uint8Array | null; | ||
| } | ||
|
|
||
| export interface CandidateSnapshot { | ||
| readonly baseCommitSha: string; | ||
| readonly treeSha: string; | ||
| readonly changes: readonly CandidateChange[]; | ||
| } | ||
|
|
||
| export interface CandidateGitHub { | ||
| getBranchSha(branch: string): Promise<string | null>; | ||
| getCommit(sha: string): Promise<{ treeSha: string; message: string }>; | ||
| createBlob(content: Uint8Array): Promise<string>; | ||
| createTree(baseTreeSha: string, entries: readonly GitTreeEntry[]): Promise<string>; | ||
| createCommit(message: string, treeSha: string, parentSha: string): Promise<string>; | ||
| createBranch(branch: string, commitSha: string): Promise<void>; | ||
| updateBranch(branch: string, commitSha: string): Promise<void>; | ||
| } | ||
|
|
||
| export interface GitTreeEntry { | ||
| readonly path: string; | ||
| readonly mode: GitTreeMode; | ||
| readonly type: "blob"; | ||
| readonly sha: string | null; | ||
| } | ||
|
|
||
| export interface CandidatePublication { | ||
| readonly branch: string; | ||
| readonly commitSha: string; | ||
| readonly files: string[]; | ||
| } | ||
|
|
||
| export interface PublishCandidateInput { | ||
| readonly branch: string; | ||
| readonly runId: string; | ||
| readonly commitMessage: string; | ||
| readonly expectedPreviousSha: string | null; | ||
| readonly snapshot: CandidateSnapshot; | ||
| } | ||
|
|
||
| export function requireCandidatePublication( | ||
| claimed: boolean, | ||
| publication: CandidatePublication | null, | ||
| ): void { | ||
| if (claimed && !publication) { | ||
| throw new Error("publish_candidate must complete before reporting a published change"); | ||
| } | ||
| } | ||
|
|
||
| export async function publishCandidate( | ||
| input: PublishCandidateInput, | ||
| github: CandidateGitHub, | ||
| ): Promise<CandidatePublication> { | ||
| if (input.snapshot.changes.length === 0) throw new Error("candidate has no changes to publish"); | ||
| const files = input.snapshot.changes.map((change) => change.path); | ||
| const runMarker = `EmDash-Run: ${input.runId}`; | ||
| const liveBefore = await github.getBranchSha(input.branch); | ||
| if (liveBefore !== input.expectedPreviousSha) { | ||
| if (liveBefore) { | ||
| const liveCommit = await github.getCommit(liveBefore); | ||
| if (liveCommit.message.includes(runMarker)) { | ||
| return { branch: input.branch, commitSha: liveBefore, files }; | ||
| } | ||
| } | ||
| throw new Error( | ||
| `candidate branch changed since this run started (expected ${input.expectedPreviousSha ?? "absent"}, found ${liveBefore ?? "absent"})`, | ||
| ); | ||
| } | ||
|
|
||
| const baseCommit = await github.getCommit(input.snapshot.baseCommitSha); | ||
| const entries: GitTreeEntry[] = []; | ||
| for (const change of input.snapshot.changes) { | ||
| entries.push({ | ||
| path: change.path, | ||
| mode: change.mode, | ||
| type: "blob", | ||
| sha: change.content === null ? null : await github.createBlob(change.content), | ||
| }); | ||
| } | ||
| const treeSha = await github.createTree(baseCommit.treeSha, entries); | ||
| if (treeSha !== input.snapshot.treeSha) { | ||
| throw new Error( | ||
| `GitHub created tree ${treeSha}, which does not match the verified candidate ${input.snapshot.treeSha}`, | ||
| ); | ||
| } | ||
| const message = `${input.commitMessage.trim()}\n\n${runMarker}`; | ||
| const parentSha = input.expectedPreviousSha ?? input.snapshot.baseCommitSha; | ||
| const commitSha = await github.createCommit(message, treeSha, parentSha); | ||
|
|
||
| const liveAtUpdate = await github.getBranchSha(input.branch); | ||
| if (liveAtUpdate !== input.expectedPreviousSha) { | ||
| throw new Error("candidate branch changed while the publication was being prepared"); | ||
| } | ||
| if (liveAtUpdate === null) await github.createBranch(input.branch, commitSha); | ||
| else await github.updateBranch(input.branch, commitSha); | ||
|
|
||
| const publishedSha = await github.getBranchSha(input.branch); | ||
| if (publishedSha !== commitSha) { | ||
| throw new Error( | ||
| `candidate branch verification failed (expected ${commitSha}, found ${publishedSha ?? "absent"})`, | ||
| ); | ||
| } | ||
| return { branch: input.branch, commitSha, files }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[needs fixing] The idempotency branch returns the live commit as soon as its message contains the run marker, without checking whether that commit's tree matches the current snapshot. If a single run calls
publish_candidatemore than once after making additional edits, the second snapshot will have a differenttreeShathan the commit already on the branch, but this branch silently returns the stale commit. The agent then reportsimplemented: true/fixed: trueagainst a publication that does not contain the verified candidate tree.