Skip to content

fix(desktop): shallow clone repositories for repo-less cloud tasks - #76903

Merged
trunk-io[bot] merged 9 commits into
masterfrom
posthog-code/shallow-clone-repoless-tasks
Aug 5, 2026
Merged

fix(desktop): shallow clone repositories for repo-less cloud tasks#76903
trunk-io[bot] merged 9 commits into
masterfrom
posthog-code/shallow-clone-repoless-tasks

Conversation

@tatoalo

@tatoalo tatoalo commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Problem

Repo-less cloud tasks discover and clone repositories at runtime through the clone_repo tool, which did a full git clone. On large repositories that pulls years of history and tags into every sandbox. Sandbox-init clones are already shallow; agent-initiated ones were not.

Port of PostHog/code#4051, which went stale during the desktop migration. The list_repos / clone_repo tools came across but the shallow-clone half did not.

Changes

  • clone_repo clones with --depth 1 --single-branch --no-tags through a new execGit (raw execFile, next to execGh in @posthog/git) instead of the simple-git clone saga. It fetches a missing branch into an existing shallow clone and normalizes a retargeted or credential-carrying origin before fetching.
  • Auth rides in an http.extraHeader scoped to https://github.com/, so the token never lands in .git/config and is never sent to non-GitHub remotes.
  • parseGithubUrl rejects dot segments and unsafe characters in owner/repo. scp-style inputs skip WHATWG dot-segment normalization, so git@github.com:owner/.. would otherwise collapse the clone target onto the whole scratch tree, which the failure-path cleanup then deletes.
  • Concurrent clone_repo calls for the same repo serialize per target path, so a racing duplicate can't delete an in-progress checkout from its failure path.
  • Repo-less Claude, Codex and pi cloud sessions get list_repos / clone_repo. Multi-repo workspaces don't: channelMode requires no repositoryPath and no task repositories. The repo-less prompt tells the agent to deepen history in bounded steps only when a task needs older commits.
  • Auto mode auto-approves the two repository tools. All other modes still prompt.

How did you test this code?

Automated only, no manual app run:

  • clone-repo.test.ts drives the real tool against real git with no network (a temp GIT_CONFIG_GLOBAL maps the GitHub URL to a local fixture). Covers the shallow/single-branch/no-tags shape, token absent from the checkout config, auth header scoping verified through git config --get-urlmatch, origin normalization, missing-branch fetch, traversal slugs rejected without touching the workspace, failed clones cleaned up and concurrent clones serialized.
  • parseGithubUrl reject matrix gained the traversal and unsafe-character inputs.
  • Gating negatives: repo tools stay gated in Claude default mode, Codex still prompts outside hands-off modes and channelMode stays off for multi-repo tasks.
  • Full @posthog/agent and @posthog/git suites, typecheck and biome clean.

Automatic notifications

  • Publish to changelog?

Docs update

N/A

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Started from a Slack thread about monorepo clone times in repo-less sandboxes. Claude (PostHog Slack app) ported #4051, then dropped the original's CloneSaga route on review feedback: the saga layer is for the desktop app and going through it meant punching a hole in simple-git's GIT_CONFIG_* env guard, so the clone moved to raw execGit. A bot review flagged the unscoped auth header, which led to the github.com scoping and unconditional origin normalization. Claude Code then reviewed the branch and fixed what it found: path traversal in scp-style slugs reaching the failure-path rm -rf, unserialized concurrent clones and missing permission-gating negative tests. Skills invoked: /writing-tests, /writing-code-comments.


Created with PostHog from a Slack thread

@trunk-io

trunk-io Bot commented Aug 3, 2026

Copy link
Copy Markdown

😎 Merged successfully - details.

@tatoalo tatoalo self-assigned this Aug 3, 2026
@github-actions github-actions Bot added the feature/desktop Feature Tag: Desktop label Aug 3, 2026
@PostHog PostHog deleted a comment from github-actions Bot Aug 3, 2026
@trunk-io

trunk-io Bot commented Aug 3, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@charlesvien
charlesvien marked this pull request as ready for review August 3, 2026 21:19
@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
products/desktop/packages/agent/src/adapters/local-tools/tools/clone-repo.ts:135-136
**Existing branches remain stale**

When a retry, reconnected session, or later tool call requests a branch that already exists locally after its remote has advanced, `git checkout` succeeds and returns before the only fetch operation, causing the agent to edit stale code and potentially create a commit or pull request that omits newer remote changes.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "refactor(desktop): clone via execGit ins..." | Re-trigger Greptile

Comment thread products/desktop/packages/agent/src/adapters/local-tools/tools/clone-repo.ts Outdated
@veria-ai

veria-ai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 2 · PR risk: 0/10

Comment thread products/desktop/packages/agent/src/adapters/claude/tools.ts
@charlesvien

Copy link
Copy Markdown
Member

/trunk merge

@posthog

posthog Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

🦔 ReviewHog reviewed this pull request

Found 0 must fix, 0 should fix, 3 consider.

Published 3 findings (view the review).

@posthog

posthog Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

ReviewHog Alpha 🦔 If you find any issues helpful - please reply "valid", "invalid", etc., for evaluation purposes 🙏

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReviewHog Report

Changes

Issues: 3 issues

Files (11)
  • products/desktop/packages/agent/src/adapters/claude/claude-agent.ts
  • products/desktop/packages/agent/src/adapters/claude/tools.ts
  • products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts
  • products/desktop/packages/agent/src/adapters/local-tools/tools/clone-repo.ts
  • products/desktop/packages/agent/src/pi/repository-tools-extension.ts
  • products/desktop/packages/agent/src/pi/rpc-client.ts
  • products/desktop/packages/agent/src/pi/rpc-host.ts
  • products/desktop/packages/agent/src/server/agent-server.ts
  • products/desktop/packages/agent/src/server/pi-agent-server.ts
  • products/desktop/packages/git/src/git-exec.ts
  • products/desktop/packages/git/src/utils.ts

Comment thread products/desktop/packages/agent/src/adapters/local-tools/tools/clone-repo.ts Outdated
Comment thread products/desktop/packages/agent/src/adapters/local-tools/tools/clone-repo.ts Outdated
Comment thread products/desktop/packages/git/src/git-exec.ts
tatoalo and others added 7 commits August 4, 2026 17:30
Repo-less cloud tasks discover and clone a repository at runtime. CloneSaga ran a plain `git clone`, so picking up a large monorepo pulled its full history and tags before any work started.

- expose `list_repos` / `clone_repo` to repo-less Claude, Codex, and pi cloud sessions
- clone with `--depth 1 --single-branch --no-tags`, authenticating via `http.extraHeader` so the token never lands in `origin`
- tell the agent to use those tools and deepen history only when it needs to
- auto-allow the repository setup tools in Auto mode

Ported from PostHog/code#4051, which was open when the desktop app moved into this repo.

Generated-By: PostHog Code
Task-Id: 18ef3ffb-2064-465f-a2f8-496ba659e79a
clone_repo only ever runs inside agent-server, against a scratch checkout no
one else touches, so CloneSaga's repo locking and rollback bookkeeping buy it
nothing — and routing through simple-git meant opting out of that client's
guard against GIT_CONFIG_* in the child env just to pass http.extraHeader auth.

Adds execGit alongside execGh in @posthog/git and drives the clone, origin
repair, and branch fetch through it. CloneSaga and createGitClient go back to
their master state, leaving the desktop clone path untouched.

Origin inspection now reads remote.origin.url rather than `remote get-url`,
which resolves url.<base>.insteadOf and could mask a persisted credential.

Generated-By: PostHog Code
Task-Id: 18ef3ffb-2064-465f-a2f8-496ba659e79a
An unscoped `http.extraHeader` is attached to every HTTP remote git talks to.
The missing-branch path fetches from whatever `origin` the existing checkout
holds, and that origin was only rewritten when it carried embedded credentials,
so a checkout retargeted at another host would have been handed the live token.

Scopes the header to `https://github.com/`, verified against
`git config --get-urlmatch`, and normalizes any origin that isn't the canonical
clone URL rather than only the ones with credentials in them. Only this tool
writes to `repos/<owner>/<repo>`, so a mismatch always means the remote was
changed after the clone.

Generated-By: PostHog Code
Task-Id: 18ef3ffb-2064-465f-a2f8-496ba659e79a
@charlesvien
charlesvien force-pushed the posthog-code/shallow-clone-repoless-tasks branch from 5352106 to 542013d Compare August 5, 2026 00:47
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 9d0992d.

@charlesvien

Copy link
Copy Markdown
Member

/trunk merge

@trunk-io
trunk-io Bot merged commit c033564 into master Aug 5, 2026
195 checks passed
@trunk-io
trunk-io Bot deleted the posthog-code/shallow-clone-repoless-tasks branch August 5, 2026 01:43
@deployment-status-posthog

deployment-status-posthog Bot commented Aug 5, 2026

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-08-05 02:18 UTC Run
prod-us ✅ Deployed 2026-08-05 02:34 UTC Run
prod-eu ✅ Deployed 2026-08-05 02:34 UTC Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature/desktop Feature Tag: Desktop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants