feat(context branching): add context branching for threads#2091
feat(context branching): add context branching for threads#2091William-zgx wants to merge 10 commits intobytedance:mainfrom
Conversation
|
@William-zgx, thanks for your contribution. As it's a new feature, please create an issue with all your designs to disucss with the community first. |
(cherry picked from commit 517ea1c)
f106ae3 to
eb04ffd
Compare
There was a problem hiding this comment.
Pull request overview
Adds minimal “context branching” support for threads by introducing backend lineage metadata + a branch creation endpoint, then wiring frontend APIs, routing helpers, and sidebar rendering to create and navigate branch threads.
Changes:
- Backend: add
POST /api/threads/{thread_id}/branchesto fork a new thread from a checkpoint, copy selected thread-local files, and persist branch lineage metadata. - Frontend: add thread branch APIs/hooks and UI actions (create branch / back-to-parent), plus branch-aware routing.
- Frontend: render recent chats as a parent/child tree using branch metadata.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/app/gateway/routers/threads.py | Adds branch create request/response models, file-copy helpers, LangGraph thread registration helpers, and the /branches endpoint. |
| backend/tests/test_threads_router.py | Adds unit tests covering branching behavior, checkpoint selection, file copying, and rollback on failure. |
| frontend/src/core/threads/types.ts | Introduces branch metadata types and request/response types for branching + thread records. |
| frontend/src/core/threads/utils.ts | Adds agent-name normalization and branch metadata helpers; improves thread path resolution. |
| frontend/src/core/threads/api.ts | Adds fetchThreadRecord and createThreadBranch API calls. |
| frontend/src/core/threads/hooks.ts | Adds useThreadRecord and useCreateThreadBranch; updates rename to invalidate thread detail queries. |
| frontend/src/core/threads/index.ts | Re-exports new thread API and utils. |
| frontend/src/components/workspace/thread-branch-actions.tsx | Adds UI for creating branches and returning to parent thread. |
| frontend/src/components/workspace/recent-chat-list.tsx | Builds and renders a branch tree in the sidebar recent chats list. |
| frontend/src/app/workspace/chats/[thread_id]/page.tsx | Adds branch actions to the default chat header. |
| frontend/src/app/workspace/agents/[agent_name]/chats/[thread_id]/page.tsx | Adds branch actions + redirects /agents/default/... back to default chat routes. |
| frontend/src/core/i18n/locales/en-US.ts | Adds branching UI strings (English). |
| frontend/src/core/i18n/locales/zh-CN.ts | Adds branching UI strings (Chinese). |
| frontend/src/core/i18n/locales/types.ts | Adds branching translation schema. |
| frontend/next.config.js | Adds gateway rewrites for additional /api/* endpoints including /api/threads/:path*. |
|
@William-zgx, thanks for your contribution. Please take a look at the review comments. |
|
Addressed in d95980f. I added defensive branch-depth parsing, rollback on copy/store failures, skipped symlinks during branch file copy, cleared artifacts when outputs are not copied, aligned the timestamp description, and added the missing /api/threads rewrite. |
@WillemJiang Thanks, I’ve addressed the review comments, updated the PR, and resolved the related threads. |
|
@WillemJiang could you please take a look when you have a moment? I've addressed the review comments and updated the PR. Thanks! |
# Conflicts: # frontend/next.config.js # frontend/src/app/workspace/agents/[agent_name]/chats/[thread_id]/page.tsx # frontend/src/app/workspace/chats/[thread_id]/page.tsx
Summary
Scope
Testing
backend/.venv/bin/pytest backend/tests/test_threads_router.py -qbackend/.venv/bin/pytest backend/tests/test_provisioner_kubeconfig.py backend/tests/test_docker_sandbox_mode_detection.py -qpnpm --dir frontend typecheckpnpm --dir frontend exec node --test src/core/threads/utils.test.ts