Skip to content

Session pages miss pull requests from other branches and show wrong changes once main moves #155

Closed

Description

@fctl-sh

The fctl session https://app.fctl.sh/s/ses_01M5DCMP24RSTE8CGHSVH4A4A9 (fctl#91 in the app) opened two pull requests in this repository, #151 and #152. Its page lists only #151, under Pull requests and in the header. Its Changes section says "No changes yet." over "+0 −0 · 1 commit · pushed". Yet the session changed web/src/components/ui/select.tsx (+3) and select.test.tsx (+31) for #151, now merged, and form.tsx and form.test.tsx for #152.

Three causes, all in fctl-agent's git state (crates/fctl-agent/src/workspace.rs). Everything the page shows about repositories comes from that state (#132).

How it works now

1. Pull requests are looked up only for the checked-out branch

workspace::sync asks gh pr view <branch> (workspace::pull_request) for the branch checked out in the session's checkout. In the clones of the project's other repositories (sync_clone), it asks only when the clone is on the session branch. The control plane learns pull requests only from these reports: SessionObject::apply_git → fctl_domain::pulls::found and merge (crates/fctl-control/src/do_session.rs). No webhook or other source adds one.

#152 came from fctl/91-pick-in-composer. The agent made that branch in a git worktree beside the checkout (git worktree add ~/composer-fix -b fctl/91-pick-in-composer origin/main), pushed it from there and ran gh pr create --head fctl/91-pick-in-composer. Refresh noticed the gh pr create (opens_pr in crates/fctl-agent/src/refresh.rs). The refresh that followed asked again for the checked-out branch's pull request. From the session's journal (~/.fctl/state/journal.jsonl, times in UTC):

20:01:17  gh pr create --head fctl/91-pick-in-composer prints https://github.com/hyperstructure/fctl/pull/152
20:01:21  git event: branch=fctl/91-on-mobile-don-t-know-on-desktop-i-can-t pushed=true pull_request=#151

The same goes for any pull request whose branch isn't checked out when fctl looks: gh pr create --head <branch>, a branch that was pushed and then switched away from, a worktree, or a second clone. fctl_domain::pulls and its test sessions_keep_every_pull_request_they_found handle a second branch only once it is checked out in the session's checkout. Work outside the tracked checkouts (#152's worktree) never shows under Changes either.

2. Changes are a diff against the moving tip of origin/<base>

workspace::state_at lists changed files with git diff --numstat -M origin/<base> and git diff --name-status -M origin/<base>. That compares the working tree with the base branch's tip as the clone last fetched it, not with the commit the session's branch started from. ahead and behind count origin/<base>...HEAD, so they are right.

origin/<base> moves whenever the clone fetches. fctl-agent's prepare runs git fetch --prune origin each time a session resumes on its checkout ("Reusing the checkout of …"). Agents fetch too: the pull-requests skill has them run git fetch origin to keep a branch current, and AGENTS.md has them check releases with git tag --contains.

  • After a squash merge, the base holds the session's changes, so the diff is empty: "No changes yet." and "+0 −0". ahead stays 1, because the squash commit is a different commit. In the session, a git fetch at 20:04:30 brought in fix(web): close a dropdown when you tap it again on iPhone and iPad #151's merge (cceac24), and the git event at 20:04:36 has files=[]. In its checkout now:

    $ git rev-list --left-right --count origin/main...HEAD
    1	1
    $ git diff --numstat -M origin/main               # what fctl-agent reports: nothing
    $ git diff --numstat -M --merge-base origin/main  # what the branch changed
    31	0	web/src/components/ui/select.test.tsx
    3	0	web/src/components/ui/select.tsx
    
  • After someone else's commits land on the base, once the clone fetches them, their changes show among the session's, reversed: a file they added appears as deleted by the session. The session lists' counts change with them, because apply_git sets stats.files_changed, additions and deletions from the same files.

3. A merged branch is pushed again after a resume

This repository deletes a pull request's branch when it is merged (delete_branch_on_merge). On the next resume, prepare's git fetch --prune removes origin/<branch>, so pushed turns false while ahead is still 1. Changes then reads "1 commit · not pushed yet". When the next turn ends, sync pushes the branch again (st.ahead > 0 && !st.pushed), which recreates it on GitHub. The session's journal shows the first half: at 20:24:37 "Reusing the checkout of hyperstructure/fctl", then at 20:25:08 a git event with pushed=false. The test below shows the push.

Reproductions

These tests go in the tests module of crates/fctl-agent/src/workspace.rs and use its helpers (sandbox, spec, fake_github). They print what happens today. A stand-in gh on PATH logs its calls and gives every branch it knows a pull request:

Tests and the stand-in gh
    fn changes(st: &GitState) -> Vec<String> {
        st.files
            .iter()
            .map(|f| format!("{} {} +{:?} -{:?}", f.status, f.path, f.additions, f.deletions))
            .collect()
    }

    async fn commit(dir: &Path, home: &Path, file: &str, text: &str, msg: &str) -> Result<()> {
        std::fs::write(dir.join(file), text)?;
        git_ok_in(dir, home, &["add", file]).await?;
        git_ok_in(dir, home, &["commit", "-q", "-m", msg]).await?;
        Ok(())
    }

    #[tokio::test]
    async fn repro_changes_after_a_squash_merge() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let p = sandbox(dir.path())?;
        let spec = spec()?;
        configure_git(&p, &spec.git_author).await?;
        let origin = fake_github(dir.path(), &p.home, "acme/widgets").await?;
        let (log, _logs) = mpsc::unbounded_channel();
        prepare(&p, &spec, &log).await?;
        commit(&p.workdir, &p.home, "a.txt", "one\ntwo\n", "Add a.txt").await?;
        let first = sync(&p, &spec, true, Moment::TurnEnded, None).await?;
        let st = &first.state;
        println!("REPRO pushed and opened: files={:?} ahead={} behind={} pushed={} pr={:?} logs={:?}",
            changes(st), st.ahead, st.behind, st.pushed, st.pull_request.as_ref().map(|p| p.number), first.logs);
        // On GitHub: the pull request is squash-merged and its branch deleted.
        for args in [
            &["merge", "-q", "--squash", "fctl/1-test"][..],
            &["commit", "-q", "-m", "Add a.txt (#1)"],
            &["branch", "-q", "-D", "fctl/1-test"],
        ] {
            let o = run(&origin, &p.home, "git", args).await?;
            assert!(o.ok, "{}", o.stderr);
        }
        // The session resumes on its checkout: prepare fetches with --prune.
        prepare(&p, &spec, &log).await?;
        let st = state(&p, &spec).await?;
        println!("REPRO after the merge and a resume: files={:?} ahead={} behind={} pushed={}",
            changes(&st), st.ahead, st.behind, st.pushed);
        let next = sync(&p, &spec, true, Moment::TurnEnded, Some(&st)).await?;
        let back = run(&origin, &p.home, "git", &["rev-parse", "--verify", "--quiet", "refs/heads/fctl/1-test"]).await?.ok;
        println!("REPRO the next turn's end: logs={:?}; fctl/1-test is on GitHub again: {back}", next.logs);
        Ok(())
    }

    #[tokio::test]
    async fn repro_changes_when_main_moves_on() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let p = sandbox(dir.path())?;
        let spec = spec()?;
        configure_git(&p, &spec.git_author).await?;
        let origin = fake_github(dir.path(), &p.home, "acme/widgets").await?;
        let (log, _logs) = mpsc::unbounded_channel();
        prepare(&p, &spec, &log).await?;
        commit(&p.workdir, &p.home, "a.txt", "one\ntwo\n", "Add a.txt").await?;
        // Someone else's change lands on main, and the clone fetches it.
        commit(&origin, &p.home, "theirs.txt", "1\n2\n3\n", "Someone else's change").await?;
        git_ok(&p, &["fetch", "-q", "origin"]).await?;
        let st = state(&p, &spec).await?;
        println!("REPRO after main moved on: files={:?} ahead={} behind={}", changes(&st), st.ahead, st.behind);
        Ok(())
    }

    #[tokio::test]
    async fn repro_a_second_pull_request_from_a_worktree() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let p = sandbox(dir.path())?;
        let spec = spec()?;
        configure_git(&p, &spec.git_author).await?;
        fake_github(dir.path(), &p.home, "acme/widgets").await?;
        let (log, _logs) = mpsc::unbounded_channel();
        prepare(&p, &spec, &log).await?;
        commit(&p.workdir, &p.home, "a.txt", "a\n", "First").await?;
        let first = sync(&p, &spec, true, Moment::TurnEnded, None).await?.state;
        // A second branch in a worktree beside the checkout, pushed, with its own pull request.
        let wt = dir.path().join("second");
        let wt_arg = wt.display().to_string();
        git_ok(&p, &["worktree", "add", "-q", "-b", "fctl/1-second", &wt_arg, "origin/main"]).await?;
        commit(&wt, &p.home, "b.txt", "b\n", "Second").await?;
        git_ok_in(&wt, &p.home, &["push", "-q", "-u", "origin", "fctl/1-second"]).await?;
        // `gh pr create --head fctl/1-second` ran: the refresh that follows, then the turn's end.
        let working = sync(&p, &spec, true, Moment::Working { opened_pr: true }, Some(&first)).await?.state;
        let ended = sync(&p, &spec, true, Moment::TurnEnded, Some(&working)).await?.state;
        let found: Vec<u64> = fctl_domain::pulls::found(&ended).iter().map(|p| p.number).collect();
        println!("REPRO pull requests found: {found:?}; Changes: {:?}", changes(&ended));
        let calls = std::fs::read_to_string(std::env::var("FAKE_GH_LOG")?)?;
        println!("REPRO gh calls:\n{calls}");
        Ok(())
    }
#!/bin/sh
# A stand-in for gh: logs each call; every pushed branch has a pull request.
echo "gh $*" >> "$FAKE_GH_LOG"
if [ "$1" = pr ] && [ "$2" = view ]; then
  case "$3" in
    fctl/1-test) echo '{"number":1,"url":"https://github.com/acme/widgets/pull/1","title":"First","state":"OPEN","isDraft":false}' ;;
    fctl/1-second) echo '{"number":2,"url":"https://github.com/acme/widgets/pull/2","title":"Second","state":"OPEN","isDraft":false}' ;;
    *) exit 1 ;;
  esac
  exit 0
fi
exit 1
mkdir -p /tmp/fakegh && cp gh /tmp/fakegh/ && chmod +x /tmp/fakegh/gh
PATH=/tmp/fakegh:$PATH FAKE_GH_LOG=/tmp/fake-gh.log \
  cargo test -p fctl-agent repro_ -- --nocapture --test-threads=1

Output on main at cceac24:

pushed and opened: files=["added a.txt +Some(2) -Some(0)"] ahead=1 behind=0 pushed=true pr=Some(1)
after the merge and a resume: files=[] ahead=1 behind=1 pushed=false
the next turn's end: logs=[(Info, "Pushed fctl/1-test")]; fctl/1-test is on GitHub again: true
after main moved on: files=["added a.txt +Some(2) -Some(0)", "deleted theirs.txt +Some(0) -Some(3)"] ahead=1 behind=1
pull requests found: [1]; Changes: ["added a.txt +Some(1) -Some(0)"]
gh calls: `gh pr view fctl/1-test …` three times, never fctl/1-second

What should happen

Each part can be its own pull request.

  • Every pull request the session opens is listed, whatever branch, worktree or clone it came from, and its state stays current (open, merged, closed). One way: Refresh already sees the gh pr create call, and its tool result holds the URL gh prints (https://github.com/<owner>/<repo>/pull/<n>). fctl-agent can look that URL up (gh pr view <url> --json number,url,title,state,isDraft). It can keep the URLs it found where they survive a restart (the sandbox's state directory), and look them up again when a turn ends, as it does the checked-out branches' pull requests. pulls::found then includes them.
  • Changes list what the session's branch changed since it left its base. Diff against the merge base, git diff --merge-base origin/<base>, for both --numstat and --name-status. That needs Git 2.30 or newer; the sandbox image has 2.39. Keep today's fallback to the empty tree when there is no base. Uncommitted and untracked work counts as now. GitState::files's doc says "relative to the base"; make it say since the branch left the base.
  • fctl-agent doesn't recreate a branch that GitHub deleted when its pull request merged. After a resume, the agent no longer has its last lookup, but it has two signals. The branch's upstream is set (it pushes with -u) but gone after fetch --prune (git status -sb shows [gone]). And gh pr view <branch> says merged.
  • The reproductions above become tests that assert the fixed behavior: after the merge and a resume, files lists a.txt; after main moved on, only a.txt; the next turn's end doesn't push; and the worktree's pull request is found.

Compatibility

Parts 2 and 3 change only fctl-agent, which reaches sandboxes with hosts' releases; no protocol change. Part 1 needs a way to report more than one pull request per repository. Today GitState has one pull_request (the checked-out branch's), and each RepoGitState has one. An optional list on GitState, such as pull_requests: Option<Vec<PullRequestRef>>, fits: GitState is #[serde(default)] with skip_serializing_none. Change crates/fctl-proto/src/event.rs first, then run cargo xtask gen, and have pulls::found read the list. The control plane, which is never older than agents, takes the field before agents send it. Older agents just leave it out.

Decisions for a person

  • Whether Changes should also cover worktrees and clones an agent makes outside the session's checkout and ~/repos, where fix(web): stop Home's composer taking presses in its project list #152's work was. The alternative is to tell agents, in crates/fctl-agent/src/prompt.rs, to make a second pull request's branch in the session's checkout (git switch -c), which fctl already follows. Decision: use your recommended approach. As long as it works.
  • What happens when a merged and deleted branch gets new commits: fctl pushes it again so a new pull request can come from it, or leaves the commits unpushed and says so. Decision: It should likely be shown if possible.
  • What Changes shows once the session's pull request is merged: the merged work, as GitHub's Files changed does for a merged pull request, or a "merged" note in place of "1 commit · not pushed yet". Decision: would be great if it was shown as merged if the agent can see that it was merged, with the merged work that the agent did.

Session: https://app.fctl.sh/s/ses_01M5DCMP24RSTE8CGHSVH4A4A9

Activity

  1. added
    bug
    agents
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentsbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions