Repository navigation
fix(server): pull_request_read shows the commit the agent just pushed #46224
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
Changes from all commits
453f2fe
c2e71c6
97ca96e
4e3e62a
e852877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ import { | |
| type ProjectId, | ||
| PROVIDER_DISPLAY_NAMES, | ||
| ProviderDriverKind, | ||
| type PullRequestDetail, | ||
| type PullRequestRef, | ||
| SourceControlProviderError, | ||
| type SourceControlWritingStyleSettings, | ||
| type ThreadId, | ||
|
|
@@ -203,6 +205,14 @@ export class GitManager extends Context.Service< | |
| readonly branch: string; | ||
| readonly url: string; | ||
| }) => Effect.Effect<boolean>; | ||
| /** | ||
| * A pull request's detail, read again when its cached head is one this checkout's own push | ||
| * left behind. A shell `git push` clears no cache, but it moves the remote-tracking ref. | ||
| */ | ||
| readonly pullRequestDetail: (input: { | ||
| readonly cwd: string; | ||
| readonly reference: PullRequestRef; | ||
| }) => Effect.Effect<PullRequestDetail, PullRequestService.PullRequestError>; | ||
| readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>; | ||
| readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>; | ||
| readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>; | ||
|
|
@@ -3722,6 +3732,57 @@ export const make = Effect.gen(function* () { | |
| return checkedOutUrl === url; | ||
| }); | ||
|
|
||
| // Whether this checkout holds `commit` and last saw its remotes hold `branch` elsewhere. A | ||
| // commit it never fetched is someone else's push, which is newer than the checkout's view. | ||
| const remoteBranchLeftCommit = Effect.fn("GitManager.remoteBranchLeftCommit")( | ||
| function* (cwd: string, branch: string, commit: string) { | ||
| if (!/^[0-9a-f]{7,64}$/i.test(commit)) return false; | ||
| const remoteNames = yield* readLookupRemotes( | ||
| cwd, | ||
| "GitManager.remoteBranchLeftCommit.remotes", | ||
| ); | ||
| if (remoteNames.length === 0) return false; | ||
| const refs = new Set(remoteNames.map((name) => `refs/remotes/${name}/${branch}`)); | ||
| const listed = yield* gitCore.execute({ | ||
| operation: "GitManager.remoteBranchLeftCommit", | ||
| cwd, | ||
| args: ["for-each-ref", "--format=%(refname)%00%(objectname)", ...refs], | ||
| timeoutMs: 5_000, | ||
| }); | ||
| // for-each-ref also lists refs below a pattern, such as origin/feature/x for feature. | ||
| const tracked = listed.stdout | ||
| .split("\n") | ||
| .map((line) => trimAsciiWhitespace(line).split("\u0000")) | ||
| .flatMap(([ref, oid]) => (ref !== undefined && oid && refs.has(ref) ? [oid] : [])); | ||
| if (tracked.length === 0 || tracked.some((oid) => oid.startsWith(commit.toLowerCase()))) { | ||
| return false; | ||
| } | ||
| yield* gitCore.resolveCommit({ cwd, revision: commit }); | ||
| return true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not treat an older tracking ref as evidence of a push. If cached head B exists locally but Exclude tracking refs that are provably behind
|
||
| }, | ||
| Effect.orElseSucceed(() => false), | ||
| ); | ||
|
|
||
| const pullRequestDetail: GitManager["Service"]["pullRequestDetail"] = Effect.fn( | ||
| "GitManager.pullRequestDetail", | ||
| )(function* ({ cwd, reference }) { | ||
| const startedAt = yield* Clock.currentTimeMillis; | ||
| // Agents pin merges to this headSha, so it must not come from a stale snapshot. | ||
| const read = pullRequests.detail({ ...reference, allowStale: false }); | ||
| const detail = yield* read; | ||
| if ( | ||
| detail.state !== "open" || | ||
| detail.headSha === undefined || | ||
| detail.observedAt === undefined || | ||
| detail.observedAt >= startedAt || | ||
| !(yield* remoteBranchLeftCommit(cwd, detail.headBranch, detail.headSha)) | ||
| ) { | ||
| return detail; | ||
| } | ||
| yield* pullRequests.invalidate({ reference }); | ||
| return yield* read; | ||
| }); | ||
|
|
||
| const invalidateLocalStatus: GitManager["Service"]["invalidateLocalStatus"] = Effect.fn( | ||
| "invalidateLocalStatus", | ||
| )(function* (cwd) { | ||
|
|
@@ -4476,6 +4537,7 @@ export const make = Effect.gen(function* () { | |
| status, | ||
| branchPullRequest, | ||
| checkedOutPullRequest, | ||
| pullRequestDetail, | ||
| invalidateLocalStatus, | ||
| invalidateRemoteStatus, | ||
| invalidateStatus, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,7 +216,12 @@ const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* ( | |
| addressedProject: () => Effect.succeed(project ?? undefined), | ||
| ...options.pullRequests, | ||
| }), | ||
| Layer.mock(GitManager.GitManager)(options.git ?? {}), | ||
| Layer.mock(GitManager.GitManager)({ | ||
| pullRequestDetail: ({ reference }) => | ||
| options.pullRequests?.detail?.({ ...reference, allowStale: false }) ?? | ||
| Effect.die("pullRequests.detail is not mocked"), | ||
| ...options.git, | ||
| }), | ||
| Layer.succeed(SecureRandom.SecureRandom, testCrypto), | ||
| ServerSettings.layerTest({}, options.managedPolicy), | ||
| McpToolAccessTestkit.liveThreadsLayer, | ||
|
|
@@ -954,6 +959,20 @@ describe("source control tools", () => { | |
| }), | ||
| ); | ||
|
|
||
| it.effect("reads the detail through the thread's checkout", () => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only checks mocked checkout-path wiring; the GitManager test already covers the changed read behavior. Could you delete this callback-wiring test? Posted via Macroscope — Test Signal |
||
| Effect.gen(function* () { | ||
| const harness = yield* makeHarness({ | ||
| thread: branchThread, | ||
| pullRequests: { activity: () => Effect.succeed(activity) }, | ||
| git: { | ||
| pullRequestDetail: ({ cwd }) => | ||
| Effect.succeed({ ...detail, headSha: cwd === "/workspace/worktree" ? "def456" : "" }), | ||
| }, | ||
| }); | ||
| expect((yield* harness.call("pull_request_read", {})).headSha).toBe("def456"); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("reads an Azure DevOps PR by number at its organization and project", () => | ||
| Effect.gen(function* () { | ||
| const asked: Array<PullRequestRef> = []; | ||
|
|
||
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.
Use only refs from the PR head repository as refresh evidence.
If a fork PR's head A exists locally but its fork has no configured remote, an upstream
featurebranch at divergent B makes this helper returntrue. The host can still correctly report A. Each later read then invalidates the cache and repeats the host requests.Restrict the compared refs to the verified PR head repository. If none exist, retain normal TTL caching. A verified head ref that advances or is rewritten must still trigger refresh. Excluding ancestor refs alone does not fix this divergent-branch case.