@devries-cameron20 / t3code-1 / commits / 6d7fe2eeb7
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Introduce pluggable VCS driver foundation (#2435)

Co-authored-by: Julius Marminge <julius@macmini.local> Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: codex <codex@users.noreply.github.com> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>

Julius Marminge committed May 2, 2026 at 16:08 UTC 6d7fe2eeb7d2731da3f7f69988056ff7a5d3b921
131 files changed +9175 -5767
.plans/19-version-control-phase-1-vcs-driver-foundation.md new
+216
@@ -0,0 +1,216 @@
1 +# Version Control Phase 1: VCS Driver Foundation
2 +
3 +## Goal
4 +
5 +Introduce a provider-neutral VCS layer and rewrite the local Git implementation as an Effect-native driver. This phase should preserve user-visible behavior while replacing the Git-first service boundary with an abstraction that can support Git, Jujutsu, and later Sapling or another viable VCS.
6 +
7 +The existing `GitCore` implementation is a behavior reference and source of regression tests, not the target architecture. New code should follow the newer package style used by `effect-acp` and `effect-codex-app-server`: typed service tags, schema-backed tagged errors, scoped process usage, explicit decode boundaries, and no Promise-based process helper as the core execution primitive.
8 +
9 +## Scope
10 +
11 +- Add VCS-domain contracts in `packages/contracts/src/vcs.ts`.
12 +- Add shared runtime parsing helpers in `packages/shared/src/vcs/*` only when they are useful to both server and web.
13 +- Add server services under `apps/server/src/vcs`:
14 + - `Services/VcsDriver.ts`
15 + - `Services/VcsRepositoryResolver.ts`
16 + - `Services/VcsProcess.ts`
17 + - `Layers/GitVcsDriver.ts`
18 + - `errors.ts`
19 +- Migrate server callers from Git-specific terms where the operation is actually VCS-generic.
20 +- Update active consumers to the new VCS APIs in the same phase; do not add backwards-compatible export shims.
21 +- Leave source-control hosting providers out of this phase except for remote metadata needed to describe repository status.
22 +
23 +## Non-Goals
24 +
25 +- No GitLab, Azure DevOps, or GitHub provider rewrite yet.
26 +- No Jujutsu driver yet, but every interface must be designed so a Jujutsu driver does not have to pretend to be Git.
27 +- No T3 Review implementation yet.
28 +- No broad UI redesign.
29 +
30 +## Driver Model
31 +
32 +Use provider-neutral nouns in new APIs:
33 +
34 +- `VcsDriver`: local repository mechanics.
35 +- `RepositoryIdentity`: detected VCS kind, root path, common metadata path when available, remotes.
36 +- `WorkingCopyStatus`: dirty state, changed files, aggregate insertions/deletions, current branch/bookmark/change name.
37 +- `ChangeSet`: a committed or pending unit of change, not necessarily a Git commit.
38 +- `RefName`: branch, bookmark, tag, or provider-specific ref.
39 +
40 +The initial driver capabilities should be explicit:
41 +
42 +```ts
43 +export interface VcsDriverCapabilities {
44 + readonly kind: "git" | "jj" | "sapling" | "unknown";
45 + readonly supportsWorktrees: boolean;
46 + readonly supportsBookmarks: boolean;
47 + readonly supportsAtomicSnapshot: boolean;
48 + readonly supportsPushDefaultRemote: boolean;
49 +}
50 +```
51 +
52 +Do not model Jujutsu as `GitCoreShape extends ...`. The Git driver can expose Git-specific implementation details internally, but the public VCS layer should describe operations by intent:
53 +
54 +- `detectRepository(cwd)`
55 +- `status(cwd, options)`
56 +- `listRefs(cwd, query/pagination)`
57 +- `checkoutRef(cwd, ref)`
58 +- `createRef(cwd, ref, from?)`
59 +- `createWorkspace(cwd, ref, path?)`
60 +- `removeWorkspace(path)`
61 +- `prepareChangeContext(cwd, filePaths?)`
62 +- `createChange(cwd, message, options)`
63 +- `push(cwd, target?)`
64 +- `rangeContext(cwd, base, head)`
65 +- `listWorkspaceFiles(cwd, options)`
66 +
67 +## Effect Process Layer
68 +
69 +Create a small reusable `VcsProcess` service instead of using `runProcess`.
70 +
71 +Requirements:
72 +
73 +- Implement with `ChildProcess` and `ChildProcessSpawner` from `effect/unstable/process`.
74 +- Support scoped acquisition/release for long-running commands and interruption.
75 +- Support bounded stdout/stderr collection with truncation markers.
76 + - DO not eagerly consume full stdout/stderr, return stream apis and expose helpers for consumers so we don't consume streams to memory unnecessarily...
77 +- Support stdin.
78 +- Support timeout through Effect scheduling/interruption, not ad-hoc timers.
79 +- Stream output lines to progress callbacks as Effects.
80 +- Return a typed `ProcessOutput` value for successful execution.
81 +- Fail with typed errors, not generic thrown exceptions.
82 +
83 +Errors should be schema-backed tagged classes, for example:
84 +
85 +- `VcsProcessSpawnError`
86 +- `VcsProcessExitError`
87 +- `VcsProcessTimeoutError`
88 +- `VcsOutputDecodeError`
89 +- `VcsRepositoryDetectionError`
90 +- `VcsUnsupportedOperationError`
91 +
92 +Every error should carry operation name, command display string, cwd when applicable, exit code when applicable, stderr/stdout tails when useful, and original cause where available. Override `message` for user readable messages that provides meaning and hints where appropriate. Errors are schema backed so the full error details will be persisted and serialized properly when stored to DB/Logfiles.
93 +
94 +## Git Driver Rewrite
95 +
96 +Rewrite Git support against `VcsProcess`.
97 +
98 +Carry forward current behavior from:
99 +
100 +- `apps/server/src/git/Layers/GitCore.ts`
101 +- `apps/server/src/git/Layers/GitCore.test.ts`
102 +- current Git status/branch/worktree contracts
103 +
104 +But split the implementation into smaller modules:
105 +
106 +- command execution and hardening config
107 +- repository detection
108 +- status parsing
109 +- branch/ref parsing
110 +- worktree operations
111 +- commit/range context generation
112 +- push/pull operations
113 +
114 +Keep parsing deterministic. Prefer Git porcelain formats, null-separated output, and schema decoding for JSON-like command output. Avoid regex parsing where Git gives a structured format.
115 +
116 +## Freshness and Local Caching
117 +
118 +Define freshness rules in the VCS layer before adding more providers. Local VCS status is cheap enough to refresh often; network-backed status is not.
119 +
120 +Treat these as live/local:
121 +
122 +- repository detection for the active cwd
123 +- working copy dirty state
124 +- staged/unstaged/untracked file summaries
125 +- current branch/bookmark/change name
126 +- local branch/bookmark lists
127 +- local worktree/workspace lists
128 +
129 +These may run on user-visible polling, but should still be debounced and coalesced per repository root. Prefer filesystem-triggered invalidation where available, with a short fallback poll interval. Concurrent requests for the same repository/status shape should share one in-flight Effect.
130 +
131 +Treat these as cached or explicit-refresh only:
132 +
133 +- remote tracking branch refreshes
134 +- ahead/behind counts that require network fetches
135 +- default branch discovery from a remote provider
136 +- remote branch lists beyond locally known refs
137 +
138 +The VCS driver should expose freshness metadata with status results:
139 +
140 +```ts
141 +export interface VcsFreshness {
142 + readonly source: "live-local" | "cached-local" | "cached-remote" | "explicit-remote";
143 + readonly observedAt: string;
144 + readonly expiresAt?: string;
145 +}
146 +```
147 +
148 +Remote refreshes should be opt-in per operation, for example `refresh: "local-only" | "allow-cached-remote" | "force-remote"`. The default for background status should be `local-only`.
149 +
150 +Use Effect `Cache` for repository identity and expensive local metadata:
151 +
152 +- key by resolved repository root plus VCS kind
153 +- invalidate on cwd/root changes and workspace mutation operations
154 +- use short TTLs for local status caches when filesystem events are unavailable
155 +- never hide command failures behind stale values unless the caller explicitly accepts stale data
156 +
157 +## Cutover Policy
158 +
159 +Prefer direct migration and deletion over compatibility wrappers.
160 +
161 +Rules:
162 +
163 +- Update consumers to call `VcsDriver`/`VcsRepositoryResolver` directly as soon as the new API exists.
164 +- Delete migrated `GitCore` service methods and tests in the same PR that moves their consumers.
165 +- Do not keep backwards-compatible export shims, barrel aliases, or old service names for convenience.
166 +- Transitional modules are allowed only when a caller group is too complex or risky to migrate in the same PR.
167 +- Every transitional module must have a narrow owner, a removal checklist, and a test proving it delegates to the new implementation.
168 +- No new feature work may depend on transitional modules.
169 +
170 +Expected transitional candidates:
171 +
172 +- The highest-level `GitManager` orchestration can be migrated in slices if doing the full Commit + PR flow in one PR is too risky.
173 +- WebSocket payload compatibility can remain only where changing it would require a coordinated UI/server protocol migration. Internal server code should still use the new VCS contracts.
174 +
175 +## Tests
176 +
177 +Add integration-style tests with real temporary Git repositories for the new Git driver:
178 +
179 +- non-repository detection
180 +- status for clean/dirty/untracked/staged states
181 +- branch/ref list with pagination
182 +- checkout/create branch
183 +- worktree create/remove
184 +- commit context generation with file filters
185 +- commit creation with hook progress events
186 +- push behavior against a local bare remote
187 +- status polling does not perform remote network refresh by default
188 +- concurrent duplicate status requests are coalesced
189 +- bounded output/truncation
190 +- timeout/interruption
191 +- typed error shape for command failure and missing executable
192 +
193 +Move or duplicate only the tests needed to prove behavior, then delete the old service tests in the same migration slice.
194 +
195 +## Migration Steps
196 +
197 +1. Add `vcs` contracts and tagged errors.
198 +2. Add `VcsProcess` and unit tests around process execution semantics.
199 +3. Add `VcsDriver` and `VcsRepositoryResolver` service contracts.
200 +4. Implement `GitVcsDriver` with real Git command integration tests.
201 +5. Move `GitStatusBroadcaster` and branch/worktree flows to the VCS service directly.
202 +6. Move commit/range/push callers to the VCS service directly.
203 +7. Delete migrated `GitCore` internals and tests as each caller group moves.
204 +8. Add a transitional adapter only for any remaining `GitManager` path that is explicitly too complex to cut over safely in one PR.
205 +9. Remove every transitional adapter before starting Phase 2 unless the adapter is documented as blocking on the provider cutover.
206 +
207 +## Acceptance Criteria
208 +
209 +- Current Git branch/status/worktree/commit behavior remains intact.
210 +- New Git implementation does not depend on `processRunner.ts`.
211 +- New errors are typed and inspectable by tests.
212 +- VCS interfaces contain no GitHub/GitLab/Azure concepts.
213 +- Active consumers use the new VCS APIs directly; any remaining transitional module has a written removal checklist and no compatibility export shim.
214 +- Background status refresh is local-only by default and cannot hit provider rate limits.
215 +- Jujutsu can be added by implementing a real driver instead of conforming to Git command semantics.
216 +- `bun fmt`, `bun lint`, and `bun typecheck` pass.
.plans/20-version-control-phase-2-source-control-provider-foundation.md new
+268
@@ -0,0 +1,268 @@
1 +# Version Control Phase 2: Source Control Provider Foundation
2 +
3 +## Goal
4 +
5 +Introduce a pluggable source-control provider layer and rewrite GitHub support as an Effect-native provider. This phase should preserve the existing GitHub Commit + PR flow while making GitLab and Azure DevOps additive drivers rather than branches inside GitHub-oriented code.
6 +
7 +The existing `GitHubCli` service and GitHub-specific `GitManager` paths are behavior references. The new provider layer should use detailed tagged errors, schema decode boundaries, `effect/unstable/process`, capability flags, and provider-neutral change-request types.
8 +
9 +## Scope
10 +
11 +- Add provider-domain contracts in `packages/contracts/src/sourceControl.ts`.
12 +- Add provider URL/reference parsing helpers in `packages/shared/src/sourceControl/*`.
13 +- Add server services under `apps/server/src/sourceControl`:
14 + - `Services/SourceControlProvider.ts`
15 + - `Services/SourceControlProviderRegistry.ts`
16 + - `Services/SourceControlProcess.ts`
17 + - `Layers/GitHubSourceControlProvider.ts`
18 + - `errors.ts`
19 +- Migrate PR creation, PR lookup, default-branch lookup, clone URL lookup, and PR checkout through the provider layer.
20 +- Update active consumers to the provider APIs directly; do not add backwards-compatible `GitHubCli` export shims.
21 +- Keep GitHub as the only production provider at the end of this phase, but make GitLab and Azure implementation paths obvious and bounded.
22 +
23 +## Non-Goals
24 +
25 +- No GitLab implementation in this phase, except fixtures/contracts that prove the abstraction can represent merge requests.
26 +- No Azure DevOps implementation in this phase, except URL/reference parser test cases if cheap.
27 +- No in-app review UI yet.
28 +- No hard dependency on one CLI forever. The first GitHub driver may use `gh`, but the interface should support REST/GraphQL implementations later.
29 +
30 +## Provider Model
31 +
32 +Use provider-neutral names:
33 +
34 +- `SourceControlProvider`: hosted repository and change-request mechanics.
35 +- `ChangeRequest`: GitHub pull request, GitLab merge request, Azure pull request.
36 +- `ChangeRequestThread`: review or discussion thread.
37 +- `ChangeRequestComment`: top-level or inline comment.
38 +- `ProviderRepository`: owner/project/repo identity plus clone URLs.
39 +
40 +Core provider operations:
41 +
42 +- `detectRemote(remoteUrl)`
43 +- `checkAuth(cwd)`
44 +- `getRepository(cwd | remoteUrl)`
45 +- `getDefaultTargetRef(repository)`
46 +- `listChangeRequests(repository, filters)`
47 +- `getChangeRequest(repository, reference)`
48 +- `createChangeRequest(repository, input)`
49 +- `checkoutChangeRequest(cwd, changeRequest, options)`
50 +- `getCloneUrls(repository)`
51 +
52 +Review-facing operations should be designed now, even if unimplemented:
53 +
54 +- `listReviewThreads(changeRequest)`
55 +- `createReviewComment(changeRequest, input)`
56 +- `replyToReviewThread(thread, input)`
57 +- `resolveReviewThread(thread)`
58 +- `submitReview(changeRequest, input)`
59 +
60 +Each operation should be guarded by capabilities:
61 +
62 +```ts
63 +export interface SourceControlProviderCapabilities {
64 + readonly kind: "github" | "gitlab" | "azure-devops" | "unknown";
65 + readonly supportsCreateChangeRequest: boolean;
66 + readonly supportsCheckoutChangeRequest: boolean;
67 + readonly supportsReviewThreads: boolean;
68 + readonly supportsInlineComments: boolean;
69 + readonly supportsDraftChangeRequests: boolean;
70 +}
71 +```
72 +
73 +## Provider Registry
74 +
75 +Add a registry that resolves a provider from repository remotes and explicit user input.
76 +
77 +Rules:
78 +
79 +- Detection should be pure where possible and testable without spawning CLIs.
80 +- Remote URL parsing belongs in `packages/shared`, not server-only provider layers.
81 +- Unknown providers should return explicit unsupported-operation errors, not silently fall back to GitHub.
82 +- Provider selection should be stable per operation and logged with enough context to debug bad remote detection.
83 +
84 +The registry should support multiple provider implementations at runtime, not a single dispatcher file with inline provider branches.
85 +
86 +## Rate Limits and Provider Caching
87 +
88 +Design the provider layer around a strict freshness budget. Provider API and CLI calls must not be part of frequent background polling unless the operation is explicitly marked safe and cached.
89 +
90 +Default behavior:
91 +
92 +- Pure URL/remote parsing is always live because it is local.
93 +- Provider detection from local remotes is live-local.
94 +- Authentication checks are cached.
95 +- Repository metadata is cached.
96 +- Default branch metadata is cached.
97 +- Change-request lists are cached and refreshed on explicit user actions or coarse intervals.
98 +- Full review threads, comments, file diffs, and timeline data are fetched only when the user opens the relevant review surface or explicitly refreshes it.
99 +- Create/update operations invalidate affected cache keys immediately after success.
100 +
101 +The provider API should make freshness explicit:
102 +
103 +```ts
104 +export interface SourceControlFreshness {
105 + readonly source: "live-local" | "cached-provider" | "live-provider";
106 + readonly observedAt: string;
107 + readonly expiresAt?: string;
108 + readonly stale?: boolean;
109 +}
110 +
111 +export type ProviderRefreshPolicy =
112 + | "cache-first"
113 + | "stale-while-revalidate"
114 + | "force-refresh"
115 + | "local-only";
116 +```
117 +
118 +Every read operation that can touch a provider should accept a refresh policy. Background UI reads should default to `cache-first` or `stale-while-revalidate`; direct user actions like pressing refresh can use `force-refresh`.
119 +
120 +Use Effect `Cache` for provider data:
121 +
122 +- auth status: key by provider kind, hostname, workspace identity, and account if known; TTL around minutes, not seconds
123 +- repository metadata/default branch: key by provider repository stable ID or normalized remote URL; TTL around tens of minutes
124 +- change-request summary lists: key by provider repository, state/filter, source ref, target ref; short TTL with stale-while-revalidate
125 +- individual change-request summaries: key by provider repository and provider CR ID; short TTL, invalidated after create/update/comment operations
126 +- review threads/comments/diffs: key by provider CR ID and head SHA/version when available; fetch on demand for T3 Review
127 +
128 +Provider drivers should surface rate-limit signals when available:
129 +
130 +- remaining quota
131 +- reset time
132 +- retry-after duration
133 +- whether the limit is primary, secondary/abuse, or unknown
134 +
135 +Rate-limit errors should be typed, retryable when the provider gives a reset/retry time, and visible enough for the UI to avoid repeatedly retrying a blocked operation.
136 +
137 +Avoid rate-limit footguns:
138 +
139 +- no provider calls from render loops or fast status polling
140 +- no listing all PRs/MRs across all repos to infer one branch state
141 +- no silent GitHub fallback for unknown providers
142 +- no unbounded cache cardinality for branch names or free-form search queries
143 +- no per-thread duplicate provider refresh when multiple views observe the same repository
144 +
145 +## GitHub Provider Rewrite
146 +
147 +Rewrite GitHub support as `GitHubSourceControlProvider`.
148 +
149 +Carry forward behavior from:
150 +
151 +- `apps/server/src/git/Layers/GitHubCli.ts`
152 +- `apps/server/src/git/Layers/GitHubCli.test.ts`
153 +- `apps/server/src/git/githubPullRequests.ts`
154 +- GitHub-specific `GitManager` PR paths
155 +
156 +Implementation requirements:
157 +
158 +- Use `SourceControlProcess` built on `effect/unstable/process`, not `runProcess`.
159 +- Decode `gh api` and `gh pr --json` responses with Effect Schema.
160 +- Use typed errors for auth failure, missing CLI, command failure, output decode failure, unsupported reference, and provider mismatch.
161 +- Keep stdout/stderr bounded.
162 +- Avoid global mutable auth caches unless they are Effect `Cache` values with explicit keys, TTLs, and invalidation behavior.
163 +- Parse provider rate-limit headers or CLI/API error payloads when available and map them to typed rate-limit errors.
164 +- Keep GitHub nouns inside the GitHub driver; convert to `ChangeRequest` at the provider boundary.
165 +
166 +## GitManager Cutover
167 +
168 +Refactor `GitManager` so it coordinates three independent services:
169 +
170 +- `VcsDriver` for local repository mechanics.
171 +- `SourceControlProviderRegistry` for hosted provider selection.
172 +- `TextGeneration` for message/body generation.
173 +
174 +`GitManager` should stop depending directly on GitHub services. User-visible step labels should be provider-neutral unless the selected provider is known and the label is intentionally provider-specific.
175 +
176 +The Commit + PR flow should become:
177 +
178 +1. Resolve VCS repository and local status.
179 +2. Resolve source-control provider from remotes.
180 +3. Generate commit content through the existing text generation service.
181 +4. Create local change through `VcsDriver`.
182 +5. Push through `VcsDriver` or a narrow provider push helper only if the VCS requires provider-specific target syntax.
183 +6. Generate change-request title/body.
184 +7. Create the change request through `SourceControlProvider`.
185 +
186 +## Cutover Policy
187 +
188 +This phase should aggressively remove old GitHub-specific internals.
189 +
190 +Rules:
191 +
192 +- Move each active consumer directly to `SourceControlProviderRegistry` or a concrete provider test layer.
193 +- Delete migrated `GitHubCli` methods, tests, and GitHub-specific helper exports in the same PR that moves their final consumer.
194 +- Do not add compatibility export shims from `apps/server/src/git` to `apps/server/src/sourceControl`.
195 +- Transitional modules are allowed only for a bounded `GitManager` slice that cannot move safely with the rest of the provider cutover.
196 +- Every transitional module must have an owner comment, a removal checklist, and no public exports consumed by new code.
197 +- Provider-neutral web parsing should replace GitHub-only parsing directly; do not keep parallel parser stacks unless a route still requires both during a single PR.
198 +
199 +## GitLab and Azure Readiness
200 +
201 +Use the triaged references as implementation inputs, not merge targets:
202 +
203 +- GitLab PR #592 is useful for `glab mr` command mapping and JSON normalization.
204 +- Azure issue #1138 defines a good first Azure slice: remote/URL detection and change-request thread setup for same-repo URLs.
205 +
206 +The abstraction should let Phase 3 add:
207 +
208 +- `GitLabSourceControlProvider` using `glab`.
209 +- `AzureDevOpsSourceControlProvider` using `az repos pr` or REST APIs.
210 +
211 +No provider should need to edit GitHub code to join the registry.
212 +
213 +## T3 Review Design Constraint
214 +
215 +Do not optimize only for creation/checkout. The provider layer must be able to support a future in-app review surface.
216 +
217 +That means contracts should include stable IDs and enough metadata for:
218 +
219 +- file-level diffs
220 +- inline review threads
221 +- resolved/unresolved state
222 +- top-level discussion comments
223 +- pending review submission
224 +- provider URL back-links
225 +
226 +Provider-specific fields can live in a metadata bag, but core review behavior should not require the UI to know whether the backing service is GitHub, GitLab, or Azure DevOps.
227 +
228 +## Tests
229 +
230 +Add tests at three levels:
231 +
232 +- Pure parser tests for GitHub, GitLab, and Azure remote URLs and change-request references.
233 +- Provider unit tests with fake `SourceControlProcess` output and schema decode failures.
234 +- Integration-style GitHub CLI tests only where they can run hermetically or be skipped without hiding unit coverage.
235 +
236 +Required cases:
237 +
238 +- GitHub PR URL, number, and branch-ish references.
239 +- GitLab MR URL/reference parsing.
240 +- Azure DevOps PR URL parsing for same-repo URLs.
241 +- unknown provider returns unsupported-operation errors.
242 +- missing CLI and auth failures produce distinct typed errors.
243 +- invalid CLI JSON fails at decode boundary with useful context.
244 +
245 +## Migration Steps
246 +
247 +1. Add `sourceControl` contracts and provider-neutral schemas.
248 +2. Add shared remote/reference parser helpers and tests.
249 +3. Add `SourceControlProcess` and provider errors.
250 +4. Add provider registry with GitHub-only registration.
251 +5. Implement `GitHubSourceControlProvider` from scratch against the new process layer.
252 +6. Cut GitHub PR operations in `GitManager` over to the provider registry.
253 +7. Replace web PR-reference parsing with provider-neutral parser output while keeping current GitHub UX.
254 +8. Add provider cache metrics and tests for cache hit, stale refresh, invalidation, and rate-limit error mapping.
255 +9. Delete the migrated `GitHubCli` implementation, tests, and GitHub-specific helper exports unless an explicit transitional checklist remains.
256 +
257 +## Acceptance Criteria
258 +
259 +- Existing GitHub Commit + PR and PR checkout flows still work.
260 +- `GitManager` no longer imports or depends on `GitHubCli`.
261 +- Active consumers use source-control provider APIs directly; any remaining transitional module has a written removal checklist and no compatibility export shim.
262 +- Source-control contracts can represent GitHub PRs, GitLab MRs, and Azure DevOps PRs.
263 +- Unknown/unsupported providers fail explicitly and visibly.
264 +- GitHub command execution does not depend on `processRunner.ts`.
265 +- Background provider reads are cached/coalesced and do not consume provider API quota on every status refresh.
266 +- Rate-limit responses become typed errors with retry/reset metadata where available.
267 +- The provider API includes the review operations needed by future T3 Review work, even if they are capability-gated.
268 +- `bun fmt`, `bun lint`, and `bun typecheck` pass.
.plans/README.md
+2
@@ -10,3 +10,5 @@
10 8. `08-precommit-format-and-lint.md`
11 9. `09-event-state-test-expansion.md`
12 10. `10-unify-process-session-abstraction.md`
13 +19. `19-version-control-phase-1-vcs-driver-foundation.md`
14 +20. `20-version-control-phase-2-source-control-provider-foundation.md`
apps/server/integration/OrchestrationEngineHarness.integration.ts
+17 -14
@@ -25,10 +25,7 @@ import {
25
26 import { CheckpointStoreLive } from "../src/checkpointing/Layers/CheckpointStore.ts";
27 import { CheckpointStore } from "../src/checkpointing/Services/CheckpointStore.ts";
28 -import { GitCoreLive } from "../src/git/Layers/GitCore.ts";
29 -import { GitCore, type GitCoreShape } from "../src/git/Services/GitCore.ts";
30 -import { GitStatusBroadcaster } from "../src/git/Services/GitStatusBroadcaster.ts";
31 -import { TextGeneration, type TextGenerationShape } from "../src/git/Services/TextGeneration.ts";
28 +import { TextGeneration, type TextGenerationShape } from "../src/textGeneration/TextGeneration.ts";
29 import { OrchestrationCommandReceiptRepositoryLive } from "../src/persistence/Layers/OrchestrationCommandReceipts.ts";
30 import { OrchestrationEventStoreLive } from "../src/persistence/Layers/OrchestrationEventStore.ts";
31 import { ProjectionCheckpointRepositoryLive } from "../src/persistence/Layers/ProjectionCheckpoints.ts";
@@ -77,6 +74,11 @@ import {
74 import { deriveServerPaths, ServerConfig } from "../src/config.ts";
75 import { WorkspaceEntriesLive } from "../src/workspace/Layers/WorkspaceEntries.ts";
76 import { WorkspacePathsLive } from "../src/workspace/Layers/WorkspacePaths.ts";
77 +import * as GitVcsDriver from "../src/vcs/GitVcsDriver.ts";
78 +import * as VcsDriverRegistry from "../src/vcs/VcsDriverRegistry.ts";
79 +import { VcsStatusBroadcaster } from "../src/vcs/VcsStatusBroadcaster.ts";
80 +import { GitWorkflowService } from "../src/git/GitWorkflowService.ts";
81 +import * as VcsProcess from "../src/vcs/VcsProcess.ts";
82
83 function runGit(cwd: string, args: ReadonlyArray<string>) {
84 return execFileSync("git", args, {
@@ -291,7 +293,7 @@ export const makeOrchestrationIntegrationHarness = (
293 Layer.provide(providerEventLoggersLayer),
294 );
295
294 - const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(GitCoreLive));
296 + const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(VcsDriverRegistry.layer));
297 const projectionSnapshotQueryLayer = OrchestrationProjectionSnapshotQueryLive;
298 const runtimeServicesLayer = Layer.mergeAll(
299 projectionSnapshotQueryLayer,
@@ -307,31 +309,31 @@ export const makeOrchestrationIntegrationHarness = (
309 Layer.provideMerge(runtimeServicesLayer),
310 Layer.provideMerge(serverSettingsLayer),
311 );
310 - const gitCoreLayer = Layer.succeed(GitCore, {
311 - renameBranch: (input: Parameters<GitCoreShape["renameBranch"]>[0]) =>
312 + const gitWorkflowLayer = Layer.mock(GitWorkflowService)({
313 + renameBranch: (input: Parameters<GitVcsDriver.GitVcsDriverShape["renameBranch"]>[0]) =>
314 Effect.succeed({ branch: input.newBranch }),
313 - } as unknown as GitCoreShape);
315 + });
316 const textGenerationLayer = Layer.succeed(TextGeneration, {
317 generateBranchName: () => Effect.succeed({ branch: "update" }),
318 generateThreadTitle: () => Effect.succeed({ title: "New thread" }),
319 } as unknown as TextGenerationShape);
320 const providerCommandReactorLayer = ProviderCommandReactorLive.pipe(
321 Layer.provideMerge(runtimeServicesLayer),
320 - Layer.provideMerge(gitCoreLayer),
322 + Layer.provideMerge(gitWorkflowLayer),
323 Layer.provideMerge(textGenerationLayer),
324 Layer.provideMerge(serverSettingsLayer),
325 );
326 const checkpointReactorLayer = CheckpointReactorLive.pipe(
327 Layer.provideMerge(runtimeServicesLayer),
328 Layer.provideMerge(
327 - Layer.succeed(GitStatusBroadcaster, {
329 + Layer.succeed(VcsStatusBroadcaster, {
330 getStatus: () => Effect.die("getStatus should not be called in this test"),
331 refreshLocalStatus: () =>
332 Effect.succeed({
333 isRepo: true,
332 - hasOriginRemote: false,
333 - isDefaultBranch: true,
334 - branch: "main",
334 + hasPrimaryRemote: false,
335 + isDefaultRef: true,
336 + refName: "main",
337 hasWorkingTreeChanges: false,
338 workingTree: { files: [], insertions: 0, deletions: 0 },
339 }),
@@ -342,11 +344,12 @@ export const makeOrchestrationIntegrationHarness = (
344 Layer.provideMerge(
345 WorkspaceEntriesLive.pipe(
346 Layer.provide(WorkspacePathsLive),
345 - Layer.provideMerge(gitCoreLayer),
347 + Layer.provideMerge(VcsDriverRegistry.layer),
348 Layer.provide(NodeServices.layer),
349 ),
350 ),
351 Layer.provideMerge(WorkspacePathsLive),
352 + Layer.provideMerge(VcsProcess.layer),
353 );
354 const orchestrationReactorLayer = OrchestrationReactorLive.pipe(
355 Layer.provideMerge(runtimeIngestionLayer),
apps/server/src/checkpointing/Errors.ts
+2 -5
@@ -1,6 +1,6 @@
1 import { Schema } from "effect";
2 import type { ProjectionRepositoryError } from "../persistence/Errors.ts";
3 -import { GitCommandError } from "@t3tools/contracts";
3 +import type { VcsError } from "@t3tools/contracts";
4
5 /**
6 * CheckpointUnavailableError - Expected checkpoint does not exist.
@@ -35,9 +35,6 @@ export class CheckpointInvariantError extends Schema.TaggedErrorClass<Checkpoint
35 }
36 }
37
38 -export type CheckpointStoreError =
39 - | GitCommandError
40 - | CheckpointInvariantError
41 - | CheckpointUnavailableError;
38 +export type CheckpointStoreError = VcsError | CheckpointInvariantError | CheckpointUnavailableError;
39
40 export type CheckpointServiceError = CheckpointStoreError | ProjectionRepositoryError;
apps/server/src/checkpointing/Layers/CheckpointStore.test.ts
+20 -17
@@ -8,24 +8,27 @@ import { describe, expect } from "vitest";
8 import { checkpointRefForThreadTurn } from "../Utils.ts";
9 import { CheckpointStoreLive } from "./CheckpointStore.ts";
10 import { CheckpointStore } from "../Services/CheckpointStore.ts";
11 -import { GitCoreLive } from "../../git/Layers/GitCore.ts";
12 -import { GitCore } from "../../git/Services/GitCore.ts";
13 -import { GitCommandError } from "@t3tools/contracts";
11 +import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts";
12 +import * as VcsProcess from "../../vcs/VcsProcess.ts";
13 +import type { VcsError } from "@t3tools/contracts";
14 import { ServerConfig } from "../../config.ts";
15 import { ThreadId } from "@t3tools/contracts";
16
17 const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
18 prefix: "t3-checkpoint-store-test-",
19 });
20 -const GitCoreTestLayer = GitCoreLive.pipe(
21 - Layer.provide(ServerConfigLayer),
22 - Layer.provide(NodeServices.layer),
23 -);
20 +const VcsProcessTestLayer = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
21 +const VcsDriverTestLayer = VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcessTestLayer));
22 const CheckpointStoreTestLayer = CheckpointStoreLive.pipe(
25 - Layer.provide(GitCoreTestLayer),
26 - Layer.provide(NodeServices.layer),
23 + Layer.provideMerge(VcsDriverTestLayer),
24 + Layer.provideMerge(NodeServices.layer),
25 +);
26 +const TestLayer = CheckpointStoreTestLayer.pipe(
27 + Layer.provideMerge(VcsProcessTestLayer),
28 + Layer.provideMerge(VcsDriverTestLayer),
29 + Layer.provideMerge(ServerConfigLayer),
30 + Layer.provideMerge(NodeServices.layer),
31 );
28 -const TestLayer = Layer.mergeAll(NodeServices.layer, GitCoreTestLayer, CheckpointStoreTestLayer);
32
33 function makeTmpDir(
34 prefix = "checkpoint-store-test-",
@@ -49,11 +52,12 @@ function writeTextFile(
52 function git(
53 cwd: string,
54 args: ReadonlyArray<string>,
52 -): Effect.Effect<string, GitCommandError, GitCore> {
55 +): Effect.Effect<string, VcsError, VcsProcess.VcsProcess> {
56 return Effect.gen(function* () {
54 - const gitCore = yield* GitCore;
55 - const result = yield* gitCore.execute({
57 + const process = yield* VcsProcess.VcsProcess;
58 + const result = yield* process.run({
59 operation: "CheckpointStore.test.git",
60 + command: "git",
61 cwd,
62 args,
63 timeoutMs: 10_000,
@@ -66,12 +70,11 @@ function initRepoWithCommit(
70 cwd: string,
71 ): Effect.Effect<
72 void,
69 - GitCommandError | PlatformError.PlatformError,
70 - GitCore | FileSystem.FileSystem
73 + VcsError | PlatformError.PlatformError,
74 + VcsProcess.VcsProcess | FileSystem.FileSystem
75 > {
76 return Effect.gen(function* () {
73 - const core = yield* GitCore;
74 - yield* core.initRepo({ cwd });
77 + yield* git(cwd, ["init"]);
78 yield* git(cwd, ["config", "user.email", "test@test.com"]);
79 yield* git(cwd, ["config", "user.name", "Test"]);
80 yield* writeTextFile(path.join(cwd, "README.md"), "# test\n");
apps/server/src/checkpointing/Layers/CheckpointStore.ts
+47 -31
@@ -14,8 +14,8 @@ import { randomUUID } from "node:crypto";
14 import { Effect, Layer, FileSystem, Path } from "effect";
15
16 import { CheckpointInvariantError } from "../Errors.ts";
17 -import { GitCommandError } from "@t3tools/contracts";
18 -import { GitCore } from "../../git/Services/GitCore.ts";
17 +import { VcsProcessExitError } from "@t3tools/contracts";
18 +import { VcsDriverRegistry } from "../../vcs/VcsDriverRegistry.ts";
19 import { CheckpointStore, type CheckpointStoreShape } from "../Services/CheckpointStore.ts";
20 import { CheckpointRef } from "@t3tools/contracts";
21
@@ -24,10 +24,26 @@ const CHECKPOINT_DIFF_MAX_OUTPUT_BYTES = 10_000_000;
24 const makeCheckpointStore = Effect.gen(function* () {
25 const fs = yield* FileSystem.FileSystem;
26 const path = yield* Path.Path;
27 - const git = yield* GitCore;
28 -
29 - const resolveHeadCommit = (cwd: string): Effect.Effect<string | null, GitCommandError> =>
30 - git
27 + const vcsRegistry = yield* VcsDriverRegistry;
28 + const vcs = {
29 + execute: (input: {
30 + readonly operation: string;
31 + readonly cwd: string;
32 + readonly args: ReadonlyArray<string>;
33 + readonly stdin?: string;
34 + readonly env?: NodeJS.ProcessEnv;
35 + readonly allowNonZeroExit?: boolean;
36 + readonly timeoutMs?: number;
37 + readonly maxOutputBytes?: number;
38 + readonly truncateOutputAtMaxBytes?: boolean;
39 + }) =>
40 + vcsRegistry
41 + .resolve({ cwd: input.cwd, requestedKind: "git" })
42 + .pipe(Effect.flatMap((handle) => handle.driver.execute(input))),
43 + };
44 +
45 + const resolveHeadCommit = (cwd: string) =>
46 + vcs
47 .execute({
48 operation: "CheckpointStore.resolveHeadCommit",
49 cwd,
@@ -36,7 +52,7 @@ const makeCheckpointStore = Effect.gen(function* () {
52 })
53 .pipe(
54 Effect.map((result) => {
39 - if (result.code !== 0) {
55 + if (result.exitCode !== 0) {
56 return null;
57 }
58 const commit = result.stdout.trim();
@@ -44,21 +60,18 @@ const makeCheckpointStore = Effect.gen(function* () {
60 }),
61 );
62
47 - const hasHeadCommit = (cwd: string): Effect.Effect<boolean, GitCommandError> =>
48 - git
63 + const hasHeadCommit = (cwd: string) =>
64 + vcs
65 .execute({
66 operation: "CheckpointStore.hasHeadCommit",
67 cwd,
68 args: ["rev-parse", "--verify", "HEAD"],
69 allowNonZeroExit: true,
70 })
55 - .pipe(Effect.map((result) => result.code === 0));
71 + .pipe(Effect.map((result) => result.exitCode === 0));
72
57 - const resolveCheckpointCommit = (
58 - cwd: string,
59 - checkpointRef: CheckpointRef,
60 - ): Effect.Effect<string | null, GitCommandError> =>
61 - git
73 + const resolveCheckpointCommit = (cwd: string, checkpointRef: CheckpointRef) =>
74 + vcs
75 .execute({
76 operation: "CheckpointStore.resolveCheckpointCommit",
77 cwd,
@@ -67,7 +80,7 @@ const makeCheckpointStore = Effect.gen(function* () {
80 })
81 .pipe(
82 Effect.map((result) => {
70 - if (result.code !== 0) {
83 + if (result.exitCode !== 0) {
84 return null;
85 }
86 const commit = result.stdout.trim();
@@ -76,7 +89,7 @@ const makeCheckpointStore = Effect.gen(function* () {
89 );
90
91 const isGitRepository: CheckpointStoreShape["isGitRepository"] = (cwd) =>
79 - git
92 + vcs
93 .execute({
94 operation: "CheckpointStore.isGitRepository",
95 cwd,
@@ -84,7 +97,7 @@ const makeCheckpointStore = Effect.gen(function* () {
97 allowNonZeroExit: true,
98 })
99 .pipe(
87 - Effect.map((result) => result.code === 0 && result.stdout.trim() === "true"),
100 + Effect.map((result) => result.exitCode === 0 && result.stdout.trim() === "true"),
101 Effect.catch(() => Effect.succeed(false)),
102 );
103
@@ -108,7 +121,7 @@ const makeCheckpointStore = Effect.gen(function* () {
121
122 const headExists = yield* hasHeadCommit(input.cwd);
123 if (headExists) {
111 - yield* git.execute({
124 + yield* vcs.execute({
125 operation,
126 cwd: input.cwd,
127 args: ["read-tree", "HEAD"],
@@ -116,14 +129,14 @@ const makeCheckpointStore = Effect.gen(function* () {
129 });
130 }
131
119 - yield* git.execute({
132 + yield* vcs.execute({
133 operation,
134 cwd: input.cwd,
135 args: ["add", "-A", "--", "."],
136 env: commitEnv,
137 });
138
126 - const writeTreeResult = yield* git.execute({
139 + const writeTreeResult = yield* vcs.execute({
140 operation,
141 cwd: input.cwd,
142 args: ["write-tree"],
@@ -131,16 +144,17 @@ const makeCheckpointStore = Effect.gen(function* () {
144 });
145 const treeOid = writeTreeResult.stdout.trim();
146 if (treeOid.length === 0) {
134 - return yield* new GitCommandError({
147 + return yield* new VcsProcessExitError({
148 operation,
149 command: "git write-tree",
150 cwd: input.cwd,
151 + exitCode: 0,
152 detail: "git write-tree returned an empty tree oid.",
153 });
154 }
155
156 const message = `t3 checkpoint ref=${input.checkpointRef}`;
143 - const commitTreeResult = yield* git.execute({
157 + const commitTreeResult = yield* vcs.execute({
158 operation,
159 cwd: input.cwd,
160 args: ["commit-tree", treeOid, "-m", message],
@@ -148,15 +162,16 @@ const makeCheckpointStore = Effect.gen(function* () {
162 });
163 const commitOid = commitTreeResult.stdout.trim();
164 if (commitOid.length === 0) {
151 - return yield* new GitCommandError({
165 + return yield* new VcsProcessExitError({
166 operation,
167 command: "git commit-tree",
168 cwd: input.cwd,
169 + exitCode: 0,
170 detail: "git commit-tree returned an empty commit oid.",
171 });
172 }
173
159 - yield* git.execute({
174 + yield* vcs.execute({
175 operation,
176 cwd: input.cwd,
177 args: ["update-ref", input.checkpointRef, commitOid],
@@ -197,12 +212,12 @@ const makeCheckpointStore = Effect.gen(function* () {
212 return false;
213 }
214
200 - yield* git.execute({
215 + yield* vcs.execute({
216 operation,
217 cwd: input.cwd,
218 args: ["restore", "--source", commitOid, "--worktree", "--staged", "--", "."],
219 });
205 - yield* git.execute({
220 + yield* vcs.execute({
221 operation,
222 cwd: input.cwd,
223 args: ["clean", "-fd", "--", "."],
@@ -210,7 +225,7 @@ const makeCheckpointStore = Effect.gen(function* () {
225
226 const headExists = yield* hasHeadCommit(input.cwd);
227 if (headExists) {
213 - yield* git.execute({
228 + yield* vcs.execute({
229 operation,
230 cwd: input.cwd,
231 args: ["reset", "--quiet", "--", "."],
@@ -235,15 +250,16 @@ const makeCheckpointStore = Effect.gen(function* () {
250 }
251
252 if (!fromCommitOid || !toCommitOid) {
238 - return yield* new GitCommandError({
253 + return yield* new VcsProcessExitError({
254 operation,
255 command: "git diff",
256 cwd: input.cwd,
257 + exitCode: 1,
258 detail: "Checkpoint ref is unavailable for diff operation.",
259 });
260 }
261
246 - const result = yield* git.execute({
262 + const result = yield* vcs.execute({
263 operation,
264 cwd: input.cwd,
265 args: ["diff", "--patch", "--minimal", "--no-color", fromCommitOid, toCommitOid],
@@ -262,7 +278,7 @@ const makeCheckpointStore = Effect.gen(function* () {
278 yield* Effect.forEach(
279 input.checkpointRefs,
280 (checkpointRef) =>
265 - git.execute({
281 + vcs.execute({
282 operation,
283 cwd: input.cwd,
284 args: ["update-ref", "-d", checkpointRef],
apps/server/src/git/GitManager.test.ts renamed
+147 -95
@@ -5,6 +5,7 @@ import { spawnSync } from "node:child_process";
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import { it } from "@effect/vitest";
7 import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect";
8 +import { ChildProcessSpawner } from "effect/unstable/process";
9 import { expect } from "vitest";
10 import type {
11 GitActionProgressEvent,
@@ -14,23 +15,25 @@ import type {
15 } from "@t3tools/contracts";
16
17 import { GitCommandError, GitHubCliError, TextGenerationError } from "@t3tools/contracts";
17 -import { type GitManagerShape } from "../Services/GitManager.ts";
18 +import { type GitManagerShape } from "./GitManager.ts";
19 import {
20 type GitHubCliShape,
21 type GitHubPullRequestSummary,
22 GitHubCli,
22 -} from "../Services/GitHubCli.ts";
23 -import { type TextGenerationShape, TextGeneration } from "../Services/TextGeneration.ts";
24 -import { GitCoreLive } from "./GitCore.ts";
25 -import { GitCore } from "../Services/GitCore.ts";
23 +} from "../sourceControl/GitHubCli.ts";
24 +import { type TextGenerationShape, TextGeneration } from "../textGeneration/TextGeneration.ts";
25 +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
26 +import * as VcsProcess from "../vcs/VcsProcess.ts";
27 +import * as GitHubSourceControlProvider from "../sourceControl/GitHubSourceControlProvider.ts";
28 +import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
29 import { makeGitManager } from "./GitManager.ts";
27 -import { ServerConfig } from "../../config.ts";
28 -import { ServerSettingsService } from "../../serverSettings.ts";
30 +import { ServerConfig } from "../config.ts";
31 +import { ServerSettingsService } from "../serverSettings.ts";
32 import {
33 ProjectSetupScriptRunner,
34 type ProjectSetupScriptRunnerInput,
35 type ProjectSetupScriptRunnerShape,
33 -} from "../../project/Services/ProjectSetupScriptRunner.ts";
36 +} from "../project/Services/ProjectSetupScriptRunner.ts";
37
38 interface FakeGhScenario {
39 prListSequence?: string[];
@@ -53,6 +56,16 @@ interface FakeGhScenario {
56 failWith?: GitHubCliError;
57 }
58
59 +function fakeGhOutput(stdout: string): VcsProcess.VcsProcessOutput {
60 + return {
61 + exitCode: ChildProcessSpawner.ExitCode(0),
62 + stdout,
63 + stderr: "",
64 + stdoutTruncated: false,
65 + stderrTruncated: false,
66 + };
67 +}
68 +
69 interface FakeGitTextGeneration {
70 generateCommitMessage: (input: {
71 cwd: string;
@@ -209,18 +222,27 @@ function runGit(
222 args: readonly string[],
223 allowNonZeroExit = false,
224 ): Effect.Effect<
212 - { readonly code: number; readonly stdout: string; readonly stderr: string },
225 + {
226 + readonly exitCode: GitVcsDriver.ExecuteGitResult["exitCode"];
227 + readonly stdout: string;
228 + readonly stderr: string;
229 + },
230 GitCommandError,
214 - GitCore
231 + GitVcsDriver.GitVcsDriver
232 > {
233 return Effect.gen(function* () {
217 - const gitCore = yield* GitCore;
218 - return yield* gitCore.execute({
234 + const git = yield* GitVcsDriver.GitVcsDriver;
235 + const result = yield* git.execute({
236 operation: "GitManager.test.runGit",
237 cwd,
238 args,
239 allowNonZeroExit,
240 });
241 + return {
242 + exitCode: result.exitCode,
243 + stdout: result.stdout,
244 + stderr: result.stderr,
245 + };
246 });
247 }
248
@@ -229,7 +251,7 @@ function initRepo(
251 ): Effect.Effect<
252 void,
253 PlatformError.PlatformError | GitCommandError,
232 - FileSystem.FileSystem | Scope.Scope | GitCore
254 + FileSystem.FileSystem | Scope.Scope | GitVcsDriver.GitVcsDriver
255 > {
256 return Effect.gen(function* () {
257 const fs = yield* FileSystem.FileSystem;
@@ -245,7 +267,7 @@ function initRepo(
267 function createBareRemote(): Effect.Effect<
268 string,
269 PlatformError.PlatformError | GitCommandError,
248 - FileSystem.FileSystem | Scope.Scope | GitCore
270 + FileSystem.FileSystem | Scope.Scope | GitVcsDriver.GitVcsDriver
271 > {
272 return Effect.gen(function* () {
273 const remoteDir = yield* makeTempDir("t3code-git-remote-");
@@ -259,7 +281,7 @@ function configureRemote(
281 remoteName: string,
282 remotePath: string,
283 fetchNamespace: string,
262 -): Effect.Effect<void, GitCommandError, GitCore> {
284 +): Effect.Effect<void, GitCommandError, GitVcsDriver.GitVcsDriver> {
285 return Effect.gen(function* () {
286 yield* runGit(cwd, ["config", `remote.${remoteName}.url`, remotePath]);
287 yield* runGit(cwd, [
@@ -379,24 +401,15 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
401 ? scenario.prListByHeadSelector?.[headSelector]
402 : undefined;
403 const stdout = (mappedQueue ?? mappedStdout ?? prListQueue.shift() ?? "[]") + "\n";
382 - return Effect.succeed({
383 - stdout,
384 - stderr: "",
385 - code: 0,
386 - signal: null,
387 - timedOut: false,
388 - });
404 + return Effect.succeed(fakeGhOutput(stdout));
405 }
406
407 if (args[0] === "pr" && args[1] === "create") {
392 - return Effect.succeed({
393 - stdout:
408 + return Effect.succeed(
409 + fakeGhOutput(
410 (scenario.createdPrUrl ?? "https://github.com/pingdotgg/codething-mvp/pull/101") + "\n",
395 - stderr: "",
396 - code: 0,
397 - signal: null,
398 - timedOut: false,
399 - });
411 + ),
412 + );
413 }
414
415 if (args[0] === "pr" && args[1] === "view") {
@@ -408,8 +421,8 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
421 headRefName: "feature/pull-request",
422 state: "open",
423 };
411 - return Effect.succeed({
412 - stdout:
424 + return Effect.succeed(
425 + fakeGhOutput(
426 JSON.stringify({
427 ...pullRequest,
428 ...(pullRequest.headRepositoryNameWithOwner
@@ -427,11 +440,8 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
440 }
441 : {}),
442 }) + "\n",
430 - stderr: "",
431 - code: 0,
432 - signal: null,
433 - timedOut: false,
434 - });
443 + ),
444 + );
445 }
446
447 if (args[0] === "pr" && args[1] === "checkout") {
@@ -453,13 +463,7 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
463 runGitSyncForFakeGh(input.cwd, ["checkout", "-b", headBranch]);
464 }
465 }
456 - return {
457 - stdout: "",
458 - stderr: "",
459 - code: 0,
460 - signal: null,
461 - timedOut: false,
462 - };
466 + return fakeGhOutput("");
467 },
468 catch: (error) =>
469 isGitHubCliError(error)
@@ -486,26 +490,17 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
490 }),
491 );
492 }
489 - return Effect.succeed({
490 - stdout:
493 + return Effect.succeed(
494 + fakeGhOutput(
495 JSON.stringify({
496 nameWithOwner: repository,
497 url: cloneUrls.url,
498 sshUrl: cloneUrls.sshUrl,
499 }) + "\n",
496 - stderr: "",
497 - code: 0,
498 - signal: null,
499 - timedOut: false,
500 - });
500 + ),
501 + );
502 }
502 - return Effect.succeed({
503 - stdout: `${scenario.defaultBranch ?? "main"}\n`,
504 - stderr: "",
505 - code: 0,
506 - signal: null,
507 - timedOut: false,
508 - });
503 + return Effect.succeed(fakeGhOutput(`${scenario.defaultBranch ?? "main"}\n`));
504 }
505
506 return Effect.fail(
@@ -639,13 +634,26 @@ function makeManager(input?: {
634
635 const serverSettingsLayer = ServerSettingsService.layerTest();
636
642 - const gitCoreLayer = GitCoreLive.pipe(
637 + const vcsDriverLayer = GitVcsDriver.layer.pipe(
638 + Layer.provideMerge(VcsProcess.layer),
639 Layer.provideMerge(NodeServices.layer),
640 Layer.provideMerge(ServerConfigLayer),
641 );
642 + const sourceControlRegistryLayer = Layer.effect(
643 + SourceControlProviderRegistry.SourceControlProviderRegistry,
644 + GitHubSourceControlProvider.make().pipe(
645 + Effect.map((provider) =>
646 + SourceControlProviderRegistry.SourceControlProviderRegistry.of({
647 + get: () => Effect.succeed(provider),
648 + resolveHandle: () => Effect.succeed({ provider, context: null }),
649 + resolve: () => Effect.succeed(provider),
650 + }),
651 + ),
652 + Effect.provide(Layer.succeed(GitHubCli, gitHubCli)),
653 + ),
654 + );
655
656 const managerLayer = Layer.mergeAll(
648 - Layer.succeed(GitHubCli, gitHubCli),
657 Layer.succeed(TextGeneration, textGeneration),
658 Layer.succeed(
659 ProjectSetupScriptRunner,
@@ -653,9 +661,9 @@ function makeManager(input?: {
661 runForThread: () => Effect.succeed({ status: "no-script" as const }),
662 },
663 ),
656 - gitCoreLayer,
664 + vcsDriverLayer,
665 serverSettingsLayer,
658 - ).pipe(Layer.provideMerge(NodeServices.layer));
666 + ).pipe(Layer.provideMerge(sourceControlRegistryLayer), Layer.provideMerge(NodeServices.layer));
667
668 return makeGitManager().pipe(
669 Effect.provide(managerLayer),
@@ -665,8 +673,9 @@ function makeManager(input?: {
673
674 const asThreadId = (threadId: string) => threadId as ThreadId;
675
668 -const GitManagerTestLayer = GitCoreLive.pipe(
676 +const GitManagerTestLayer = GitVcsDriver.layer.pipe(
677 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-git-manager-test-" })),
678 + Layer.provideMerge(VcsProcess.layer),
679 Layer.provideMerge(NodeServices.layer),
680 );
681
@@ -698,15 +707,15 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
707
708 const status = yield* manager.status({ cwd: repoDir });
709 expect(status.isRepo).toBe(true);
701 - expect(status.hasOriginRemote).toBe(true);
702 - expect(status.isDefaultBranch).toBe(false);
703 - expect(status.branch).toBe("feature/status-open-pr");
710 + expect(status.hasPrimaryRemote).toBe(true);
711 + expect(status.isDefaultRef).toBe(false);
712 + expect(status.refName).toBe("feature/status-open-pr");
713 expect(status.pr).toEqual({
714 number: 13,
715 title: "Existing PR",
716 url: "https://github.com/pingdotgg/codething-mvp/pull/13",
708 - baseBranch: "main",
709 - headBranch: "feature/status-open-pr",
717 + baseRef: "main",
718 + headRef: "feature/status-open-pr",
719 state: "open",
720 });
721 }),
@@ -743,8 +752,8 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
752 number: 14,
753 title: "Existing PR title",
754 url: "https://github.com/pingdotgg/codething-mvp/pull/14",
746 - baseBranch: "main",
747 - headBranch: "feature/status-trimmed-pr",
755 + baseRef: "main",
756 + headRef: "feature/status-trimmed-pr",
757 state: "open",
758 });
759 }),
@@ -794,8 +803,8 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
803 number: 15,
804 title: "Valid PR title",
805 url: "https://github.com/pingdotgg/codething-mvp/pull/15",
797 - baseBranch: "main",
798 - headBranch: "feature/status-valid-pr-entry",
806 + baseRef: "main",
807 + headRef: "feature/status-valid-pr-entry",
808 state: "open",
809 });
810 }),
@@ -843,8 +852,8 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
852 number: 17,
853 title: "Merged PR",
854 url: "https://github.com/pingdotgg/codething-mvp/pull/17",
846 - baseBranch: "main",
847 - headBranch: "feature/status-lowercase-state",
855 + baseRef: "main",
856 + headRef: "feature/status-lowercase-state",
857 state: "merged",
858 });
859 }),
@@ -859,9 +868,9 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
868
869 expect(status).toEqual({
870 isRepo: false,
862 - hasOriginRemote: false,
863 - isDefaultBranch: false,
864 - branch: null,
871 + hasPrimaryRemote: false,
872 + isDefaultRef: false,
873 + refName: null,
874 hasWorkingTreeChanges: false,
875 workingTree: {
876 files: [],
@@ -888,9 +897,9 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
897
898 expect(status).toEqual({
899 isRepo: false,
891 - hasOriginRemote: false,
892 - isDefaultBranch: false,
893 - branch: null,
900 + hasPrimaryRemote: false,
901 + isDefaultRef: false,
902 + refName: null,
903 hasWorkingTreeChanges: false,
904 workingTree: {
905 files: [],
@@ -972,7 +981,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
981 });
982
983 const status = yield* manager.status({ cwd: repoDir });
975 - expect(status.branch).toBe("main");
984 + expect(status.refName).toBe("main");
985 expect(status.pr).toBeNull();
986 }),
987 );
@@ -1026,13 +1035,13 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1035 });
1036
1037 const status = yield* manager.status({ cwd: repoDir });
1029 - expect(status.branch).toBe("t3code/pr-488/statemachine");
1038 + expect(status.refName).toBe("t3code/pr-488/statemachine");
1039 expect(status.pr).toEqual({
1040 number: 488,
1041 title: "Rebase this PR on latest main",
1042 url: "https://github.com/pingdotgg/codething-mvp/pull/488",
1034 - baseBranch: "main",
1035 - headBranch: "statemachine",
1043 + baseRef: "main",
1044 + headRef: "statemachine",
1045 state: "open",
1046 });
1047 expect(ghCalls).toContain(
@@ -1126,13 +1135,13 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1135 });
1136
1137 const status = yield* manager.status({ cwd: repoDir });
1129 - expect(status.branch).toBe("upstream/effect-atom");
1138 + expect(status.refName).toBe("upstream/effect-atom");
1139 expect(status.pr).toEqual({
1140 number: 1618,
1141 title: "Correct PR",
1142 url: "https://github.com/pingdotgg/t3code/pull/1618",
1134 - baseBranch: "main",
1135 - headBranch: "effect-atom",
1143 + baseRef: "main",
1144 + headRef: "effect-atom",
1145 state: "open",
1146 });
1147 expect(ghCalls.some((call) => call.includes("pr list --head upstream/effect-atom "))).toBe(
@@ -1176,13 +1185,13 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1185 });
1186
1187 const status = yield* manager.status({ cwd: repoDir });
1179 - expect(status.branch).toBe("feature/status-merged-pr");
1188 + expect(status.refName).toBe("feature/status-merged-pr");
1189 expect(status.pr).toEqual({
1190 number: 22,
1191 title: "Merged PR",
1192 url: "https://github.com/pingdotgg/codething-mvp/pull/22",
1184 - baseBranch: "main",
1185 - headBranch: "feature/status-merged-pr",
1193 + baseRef: "main",
1194 + headRef: "feature/status-merged-pr",
1195 state: "merged",
1196 });
1197 }),
@@ -1213,7 +1222,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1222 });
1223
1224 const status = yield* manager.status({ cwd: repoDir });
1216 - expect(status.branch).toBe("main");
1225 + expect(status.refName).toBe("main");
1226 expect(status.pr).toBeNull();
1227 }),
1228 );
@@ -1253,13 +1262,13 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1262 });
1263
1264 const status = yield* manager.status({ cwd: repoDir });
1256 - expect(status.branch).toBe("feature/status-open-over-merged");
1265 + expect(status.refName).toBe("feature/status-open-over-merged");
1266 expect(status.pr).toEqual({
1267 number: 46,
1268 title: "Open PR",
1269 url: "https://github.com/pingdotgg/codething-mvp/pull/46",
1261 - baseBranch: "main",
1262 - headBranch: "feature/status-open-over-merged",
1270 + baseRef: "main",
1271 + headRef: "feature/status-open-over-merged",
1272 state: "open",
1273 });
1274 }),
@@ -1284,7 +1293,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1293 });
1294
1295 const status = yield* manager.status({ cwd: repoDir });
1287 - expect(status.branch).toBe("feature/status-no-gh");
1296 + expect(status.refName).toBe("feature/status-no-gh");
1297 expect(status.pr).toBeNull();
1298 }),
1299 );
@@ -1708,6 +1717,49 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1717 }),
1718 );
1719
1720 + it.effect("create_pr falls back to main when source control provider detection fails", () =>
1721 + Effect.gen(function* () {
1722 + const repoDir = yield* makeTempDir("t3code-git-manager-");
1723 + yield* initRepo(repoDir);
1724 + yield* runGit(repoDir, ["checkout", "-b", "feature/provider-fallback"]);
1725 + fs.writeFileSync(path.join(repoDir, "provider-fallback.txt"), "fallback\n");
1726 + yield* runGit(repoDir, ["add", "provider-fallback.txt"]);
1727 + yield* runGit(repoDir, ["commit", "-m", "Provider fallback"]);
1728 + const remoteDir = yield* createBareRemote();
1729 + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
1730 +
1731 + const { manager, ghCalls } = yield* makeManager({
1732 + ghScenario: {
1733 + prListSequence: [
1734 + "[]",
1735 + JSON.stringify([
1736 + {
1737 + number: 404,
1738 + title: "Provider fallback",
1739 + url: "https://github.com/pingdotgg/codething-mvp/pull/404",
1740 + baseRefName: "main",
1741 + headRefName: "feature/provider-fallback",
1742 + },
1743 + ]),
1744 + ],
1745 + },
1746 + });
1747 +
1748 + const result = yield* runStackedAction(manager, {
1749 + cwd: repoDir,
1750 + action: "create_pr",
1751 + });
1752 +
1753 + expect(result.pr.status).toBe("created");
1754 + expect(result.pr.number).toBe(404);
1755 + expect(
1756 + ghCalls.some((call) =>
1757 + call.includes("pr create --base main --head feature/provider-fallback"),
1758 + ),
1759 + ).toBe(true);
1760 + }),
1761 + );
1762 +
1763 it.effect("returns existing PR metadata for commit/push/pr action", () =>
1764 Effect.gen(function* () {
1765 const repoDir = yield* makeTempDir("t3code-git-manager-");
@@ -3119,7 +3171,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
3171 expect.objectContaining({
3172 kind: "phase_started",
3173 phase: "pr",
3122 - label: "Creating GitHub pull request...",
3174 + label: "Creating pull request...",
3175 }),
3176 ]);
3177 }),
apps/server/src/git/GitManager.ts renamed
+183 -154
@@ -1,55 +1,96 @@
1 import { randomUUID } from "node:crypto";
2 -import { realpathSync } from "node:fs";
2
3 import {
4 + Array as Arr,
5 Cache,
6 + Context,
7 + DateTime,
8 Duration,
9 Effect,
10 Exit,
11 FileSystem,
12 Layer,
13 Option,
14 + Order,
15 Path,
16 Ref,
14 - Result,
17 } from "effect";
18 import {
19 GitActionProgressEvent,
20 GitActionProgressPhase,
21 GitCommandError,
22 + GitPreparePullRequestThreadInput,
23 + GitPreparePullRequestThreadResult,
24 + GitPullRequestRefInput,
25 + GitResolvePullRequestResult,
26 + GitRunStackedActionInput,
27 GitRunStackedActionResult,
28 GitStackedAction,
22 - type GitStatusLocalResult,
23 - type GitStatusRemoteResult,
29 + VcsStatusInput,
30 + type VcsStatusLocalResult,
31 + type VcsStatusRemoteResult,
32 + VcsStatusResult,
33 ModelSelection,
34 } from "@t3tools/contracts";
35 import {
27 - detectGitHostingProviderFromRemoteUrl,
36 + detectSourceControlProviderFromGitRemoteUrl,
37 mergeGitStatusParts,
38 resolveAutoFeatureBranchName,
39 sanitizeBranchFragment,
40 sanitizeFeatureBranchName,
41 } from "@t3tools/shared/git";
42 +import {
43 + getChangeRequestTerminologyForKind,
44 + type ChangeRequestTerminology,
45 +} from "@t3tools/shared/sourceControl";
46
47 import { GitManagerError } from "@t3tools/contracts";
35 -import {
36 - GitManager,
37 - type GitActionProgressReporter,
38 - type GitManagerShape,
39 - type GitRunStackedActionOptions,
40 -} from "../Services/GitManager.ts";
41 -import { GitCore } from "../Services/GitCore.ts";
42 -import type { GitStatusDetails } from "../Services/GitCore.ts";
43 -import { GitHubCli, type GitHubPullRequestSummary } from "../Services/GitHubCli.ts";
44 -import { TextGeneration } from "../Services/TextGeneration.ts";
45 -import { ProjectSetupScriptRunner } from "../../project/Services/ProjectSetupScriptRunner.ts";
46 -import { extractBranchNameFromRemoteRef } from "../remoteRefs.ts";
47 -import { ServerSettingsService } from "../../serverSettings.ts";
48 +import { TextGeneration } from "../textGeneration/TextGeneration.ts";
49 +import { ProjectSetupScriptRunner } from "../project/Services/ProjectSetupScriptRunner.ts";
50 +import { extractBranchNameFromRemoteRef } from "./remoteRefs.ts";
51 +import { ServerSettingsService } from "../serverSettings.ts";
52 import type { GitManagerServiceError } from "@t3tools/contracts";
49 -import {
50 - decodeGitHubPullRequestListJson,
51 - formatGitHubJsonDecodeError,
52 -} from "../githubPullRequests.ts";
53 +import { GitVcsDriver, type GitStatusDetails } from "../vcs/GitVcsDriver.ts";
54 +import { SourceControlProviderRegistry } from "../sourceControl/SourceControlProviderRegistry.ts";
55 +import type { ChangeRequest } from "@t3tools/contracts";
56 +
57 +export interface GitActionProgressReporter {
58 + readonly publish: (event: GitActionProgressEvent) => Effect.Effect<void, never>;
59 +}
60 +
61 +export interface GitRunStackedActionOptions {
62 + readonly actionId?: string;
63 + readonly progressReporter?: GitActionProgressReporter;
64 +}
65 +
66 +export interface GitManagerShape {
67 + readonly status: (
68 + input: VcsStatusInput,
69 + ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
70 + readonly localStatus: (
71 + input: VcsStatusInput,
72 + ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
73 + readonly remoteStatus: (
74 + input: VcsStatusInput,
75 + ) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
76 + readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
77 + readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
78 + readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
79 + readonly resolvePullRequest: (
80 + input: GitPullRequestRefInput,
81 + ) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
82 + readonly preparePullRequestThread: (
83 + input: GitPreparePullRequestThreadInput,
84 + ) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;
85 + readonly runStackedAction: (
86 + input: GitRunStackedActionInput,
87 + options?: GitRunStackedActionOptions,
88 + ) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
89 +}
90 +
91 +export class GitManager extends Context.Service<GitManager, GitManagerShape>()(
92 + "t3/git/GitManager",
93 +) {}
94
95 const COMMIT_TIMEOUT_MS = 10 * 60_000;
96 const MAX_PROGRESS_TEXT_LENGTH = 500;
@@ -75,9 +116,14 @@ interface OpenPrInfo {
116
117 interface PullRequestInfo extends OpenPrInfo, PullRequestHeadRemoteInfo {
118 state: "open" | "closed" | "merged";
78 - updatedAt: string | null;
119 + updatedAt: Option.Option<DateTime.Utc>;
120 }
121
122 +const pullRequestUpdatedAtDescOrder: Order.Order<PullRequestInfo> = Order.mapInput(
123 + Order.flip(Option.makeOrder(DateTime.Order)),
124 + (pullRequest) => pullRequest.updatedAt,
125 +);
126 +
127 interface ResolvedPullRequest {
128 number: number;
129 title: string;
@@ -88,9 +134,9 @@ interface ResolvedPullRequest {
134 }
135
136 interface PullRequestHeadRemoteInfo {
91 - isCrossRepository?: boolean;
92 - headRepositoryNameWithOwner?: string | null;
93 - headRepositoryOwnerLogin?: string | null;
137 + isCrossRepository?: boolean | undefined;
138 + headRepositoryNameWithOwner?: string | null | undefined;
139 + headRepositoryOwnerLogin?: string | null | undefined;
140 }
141
142 interface BranchHeadContext {
@@ -256,7 +302,7 @@ function matchesBranchHeadContext(
302 return true;
303 }
304
259 -function toPullRequestInfo(summary: GitHubPullRequestSummary): PullRequestInfo {
305 +function toPullRequestInfo(summary: ChangeRequest): PullRequestInfo {
306 return {
307 number: summary.number,
308 title: summary.title,
@@ -264,7 +310,7 @@ function toPullRequestInfo(summary: GitHubPullRequestSummary): PullRequestInfo {
310 baseRefName: summary.baseRefName,
311 headRefName: summary.headRefName,
312 state: summary.state ?? "open",
267 - updatedAt: null,
313 + updatedAt: summary.updatedAt,
314 ...(summary.isCrossRepository !== undefined
315 ? { isCrossRepository: summary.isCrossRepository }
316 : {}),
@@ -311,13 +357,14 @@ function withDescription(title: string, description: string | undefined) {
357
358 function summarizeGitActionResult(
359 result: Pick<GitRunStackedActionResult, "commit" | "push" | "pr">,
360 + terms: ChangeRequestTerminology,
361 ): {
362 title: string;
363 description?: string;
364 } {
365 if (result.pr.status === "created" || result.pr.status === "opened_existing") {
366 const prNumber = result.pr.number ? ` #${result.pr.number}` : "";
320 - const title = `${result.pr.status === "created" ? "Created PR" : "Opened PR"}${prNumber}`;
367 + const title = `${result.pr.status === "created" ? "Created" : "Opened"} ${terms.shortLabel}${prNumber}`;
368 return withDescription(title, truncateText(result.pr.title));
369 }
370
@@ -422,16 +469,16 @@ function toStatusPr(pr: PullRequestInfo): {
469 number: number;
470 title: string;
471 url: string;
425 - baseBranch: string;
426 - headBranch: string;
472 + baseRef: string;
473 + headRef: string;
474 state: "open" | "closed" | "merged";
475 } {
476 return {
477 number: pr.number,
478 title: pr.title,
479 url: pr.url,
433 - baseBranch: pr.baseRefName,
434 - headBranch: pr.headRefName,
480 + baseRef: pr.baseRefName,
481 + headRef: pr.headRefName,
482 state: pr.state,
483 };
484 }
@@ -442,14 +489,6 @@ function normalizePullRequestReference(reference: string): string {
489 return hashNumber?.[1] ?? trimmed;
490 }
491
445 -function canonicalizeExistingPath(value: string): string {
446 - try {
447 - return realpathSync.native(value);
448 - } catch {
449 - return value;
450 - }
451 -}
452 -
492 function toResolvedPullRequest(pr: {
493 number: number;
494 title: string;
@@ -475,9 +514,9 @@ function shouldPreferSshRemote(url: string | null): boolean {
514 }
515
516 function toPullRequestHeadRemoteInfo(pr: {
478 - isCrossRepository?: boolean;
479 - headRepositoryNameWithOwner?: string | null;
480 - headRepositoryOwnerLogin?: string | null;
517 + isCrossRepository?: boolean | undefined;
518 + headRepositoryNameWithOwner?: string | null | undefined;
519 + headRepositoryOwnerLogin?: string | null | undefined;
520 }): PullRequestHeadRemoteInfo {
521 return {
522 ...(pr.isCrossRepository !== undefined ? { isCrossRepository: pr.isCrossRepository } : {}),
@@ -491,10 +530,12 @@ function toPullRequestHeadRemoteInfo(pr: {
530 }
531
532 export const makeGitManager = Effect.fn("makeGitManager")(function* () {
494 - const gitCore = yield* GitCore;
495 - const gitHubCli = yield* GitHubCli;
533 + const gitCore = yield* GitVcsDriver;
534 + const sourceControlProviders = yield* SourceControlProviderRegistry;
535 const textGeneration = yield* TextGeneration;
536 const projectSetupScriptRunner = yield* ProjectSetupScriptRunner;
537 +
538 + const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd });
539 const serverSettingsService = yield* ServerSettingsService;
540
541 const createProgressEmitter = (
@@ -531,7 +572,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
572 return;
573 }
574
534 - const cloneUrls = yield* gitHubCli.getRepositoryCloneUrls({
575 + const cloneUrls = yield* (yield* sourceControlProvider(cwd)).getRepositoryCloneUrls({
576 cwd,
577 repository: repositoryNameWithOwner,
578 });
@@ -586,7 +627,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
627 return;
628 }
629
589 - const cloneUrls = yield* gitHubCli.getRepositoryCloneUrls({
630 + const cloneUrls = yield* (yield* sourceControlProvider(cwd)).getRepositoryCloneUrls({
631 cwd,
632 repository: repositoryNameWithOwner,
633 });
@@ -635,7 +676,9 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
676 const path = yield* Path.Path;
677
678 const tempDir = process.env.TMPDIR ?? process.env.TEMP ?? process.env.TMP ?? "/tmp";
638 - const normalizeStatusCacheKey = (cwd: string) => canonicalizeExistingPath(cwd);
679 + const canonicalizeExistingPath = (value: string) =>
680 + fileSystem.realPath(value).pipe(Effect.catch(() => Effect.succeed(value)));
681 + const normalizeStatusCacheKey = canonicalizeExistingPath;
682 const nonRepositoryStatusDetails = {
683 isRepo: false,
684 hasOriginRemote: false,
@@ -660,20 +703,22 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
703
704 return {
705 isRepo: details.isRepo,
663 - ...(hostingProvider ? { hostingProvider } : {}),
664 - hasOriginRemote: details.hasOriginRemote,
665 - isDefaultBranch: details.isDefaultBranch,
666 - branch: details.branch,
706 + ...(hostingProvider ? { sourceControlProvider: hostingProvider } : {}),
707 + hasPrimaryRemote: details.hasOriginRemote,
708 + isDefaultRef: details.isDefaultBranch,
709 + refName: details.branch,
710 hasWorkingTreeChanges: details.hasWorkingTreeChanges,
711 workingTree: details.workingTree,
669 - } satisfies GitStatusLocalResult;
712 + } satisfies VcsStatusLocalResult;
713 });
714 const localStatusResultCache = yield* Cache.makeWith(readLocalStatus, {
715 capacity: STATUS_RESULT_CACHE_CAPACITY,
716 timeToLive: (exit) => (Exit.isSuccess(exit) ? STATUS_RESULT_CACHE_TTL : Duration.zero),
717 });
718 const invalidateLocalStatusResultCache = (cwd: string) =>
676 - Cache.invalidate(localStatusResultCache, normalizeStatusCacheKey(cwd));
719 + normalizeStatusCacheKey(cwd).pipe(
720 + Effect.flatMap((cacheKey) => Cache.invalidate(localStatusResultCache, cacheKey)),
721 + );
722 const readRemoteStatus = Effect.fn("readRemoteStatus")(function* (cwd: string) {
723 const details = yield* gitCore
724 .statusDetails(cwd)
@@ -704,14 +749,16 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
749 aheadCount: details.aheadCount,
750 behindCount: details.behindCount,
751 pr,
707 - } satisfies GitStatusRemoteResult;
752 + } satisfies VcsStatusRemoteResult;
753 });
754 const remoteStatusResultCache = yield* Cache.makeWith(readRemoteStatus, {
755 capacity: STATUS_RESULT_CACHE_CAPACITY,
756 timeToLive: (exit) => (Exit.isSuccess(exit) ? STATUS_RESULT_CACHE_TTL : Duration.zero),
757 });
758 const invalidateRemoteStatusResultCache = (cwd: string) =>
714 - Cache.invalidate(remoteStatusResultCache, normalizeStatusCacheKey(cwd));
759 + normalizeStatusCacheKey(cwd).pipe(
760 + Effect.flatMap((cacheKey) => Cache.invalidate(remoteStatusResultCache, cacheKey)),
761 + );
762
763 const readConfigValueNullable = (cwd: string, key: string) =>
764 gitCore.readConfigValue(cwd, key).pipe(Effect.catch(() => Effect.succeed(null)));
@@ -728,7 +775,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
775 (yield* readConfigValueNullable(cwd, `remote.${preferredRemoteName}.url`)) ??
776 (yield* readConfigValueNullable(cwd, "remote.origin.url"));
777
731 - return remoteUrl ? detectGitHostingProviderFromRemoteUrl(remoteUrl) : null;
778 + return remoteUrl ? detectSourceControlProviderFromGitRemoteUrl(remoteUrl) : null;
779 });
780
781 const resolveRemoteRepositoryContext = Effect.fn("resolveRemoteRepositoryContext")(function* (
@@ -833,9 +880,10 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
880 >,
881 ) {
882 for (const headSelector of headContext.headSelectors) {
836 - const pullRequests = yield* gitHubCli.listOpenPullRequests({
883 + const pullRequests = yield* (yield* sourceControlProvider(cwd)).listChangeRequests({
884 cwd,
885 headSelector,
886 + state: "open",
887 limit: 1,
888 });
889 const normalizedPullRequests = pullRequests.map(toPullRequestInfo);
@@ -851,7 +899,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
899 baseRefName: firstPullRequest.baseRefName,
900 headRefName: firstPullRequest.headRefName,
901 state: "open",
854 - updatedAt: null,
902 + updatedAt: Option.none(),
903 } satisfies PullRequestInfo;
904 }
905 }
@@ -867,46 +915,14 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
915 const parsedByNumber = new Map<number, PullRequestInfo>();
916
917 for (const headSelector of headContext.headSelectors) {
870 - const stdout = yield* gitHubCli
871 - .execute({
872 - cwd,
873 - args: [
874 - "pr",
875 - "list",
876 - "--head",
877 - headSelector,
878 - "--state",
879 - "all",
880 - "--limit",
881 - "20",
882 - "--json",
883 - "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
884 - ],
885 - })
886 - .pipe(Effect.map((result) => result.stdout));
887 -
888 - const raw = stdout.trim();
889 - if (raw.length === 0) {
890 - continue;
891 - }
892 -
893 - const pullRequests = yield* Effect.sync(() => decodeGitHubPullRequestListJson(raw)).pipe(
894 - Effect.flatMap((decoded) => {
895 - if (!Result.isSuccess(decoded)) {
896 - return Effect.fail(
897 - gitManagerError(
898 - "findLatestPr",
899 - `GitHub CLI returned invalid PR list JSON: ${formatGitHubJsonDecodeError(decoded.failure)}`,
900 - decoded.failure,
901 - ),
902 - );
903 - }
904 -
905 - return Effect.succeed(decoded.success);
906 - }),
907 - );
918 + const pullRequests = yield* (yield* sourceControlProvider(cwd)).listChangeRequests({
919 + cwd,
920 + headSelector,
921 + state: "all",
922 + limit: 20,
923 + });
924
909 - for (const pr of pullRequests) {
925 + for (const pr of pullRequests.map(toPullRequestInfo)) {
926 if (!matchesBranchHeadContext(pr, headContext)) {
927 continue;
928 }
@@ -914,11 +930,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
930 }
931 }
932
917 - const parsed = Array.from(parsedByNumber.values()).toSorted((a, b) => {
918 - const left = a.updatedAt ? Date.parse(a.updatedAt) : 0;
919 - const right = b.updatedAt ? Date.parse(b.updatedAt) : 0;
920 - return right - left;
921 - });
933 + const parsed = Arr.sort(parsedByNumber.values(), pullRequestUpdatedAtDescOrder);
934
935 const latestOpenPr = parsed.find((pr) => pr.state === "open");
936 if (latestOpenPr) {
@@ -931,7 +943,11 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
943 cwd: string,
944 result: Pick<GitRunStackedActionResult, "action" | "branch" | "commit" | "push" | "pr">,
945 ) {
934 - const summary = summarizeGitActionResult(result);
946 + const terms = yield* sourceControlProvider(cwd).pipe(
947 + Effect.map((provider) => getChangeRequestTerminologyForKind(provider.kind)),
948 + Effect.catch(() => Effect.succeed(getChangeRequestTerminologyForKind("unknown"))),
949 + );
950 + const summary = summarizeGitActionResult(result, terms);
951 let latestOpenPr: PullRequestInfo | null = null;
952 let currentBranchIsDefault = false;
953 let finalBranchContext: {
@@ -996,7 +1012,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1012 result.pr.status === "opened_existing")
1013 ? {
1014 kind: "open_pr" as const,
999 - label: "View PR",
1015 + label: `View ${terms.shortLabel}`,
1016 url: openPr.url,
1017 }
1018 : (result.action === "push" || result.action === "commit_push") &&
@@ -1004,7 +1020,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1020 !currentBranchIsDefault
1021 ? {
1022 kind: "run_action" as const,
1007 - label: "Create PR",
1023 + label: `Create ${terms.shortLabel}`,
1024 action: { kind: "create_pr" as const },
1025 }
1026 : {
@@ -1035,11 +1051,12 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1051 }
1052 }
1053
1038 - const defaultFromGh = yield* gitHubCli
1039 - .getDefaultBranch({ cwd })
1040 - .pipe(Effect.catch(() => Effect.succeed(null)));
1041 - if (defaultFromGh) {
1042 - return defaultFromGh;
1054 + const defaultFromProvider = yield* sourceControlProvider(cwd).pipe(
1055 + Effect.flatMap((provider) => provider.getDefaultBranch({ cwd })),
1056 + Effect.catch(() => Effect.succeed(null)),
1057 + );
1058 + if (defaultFromProvider) {
1059 + return defaultFromProvider;
1060 }
1061
1062 return "main";
@@ -1211,6 +1228,8 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1228 fallbackBranch: string | null,
1229 emit: GitActionProgressEmitter,
1230 ) {
1231 + const provider = yield* sourceControlProvider(cwd);
1232 + const terms = getChangeRequestTerminologyForKind(provider.kind);
1233 const details = yield* gitCore.statusDetails(cwd);
1234 const branch = details.branch ?? fallbackBranch;
1235 if (!branch) {
@@ -1247,7 +1266,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1266 yield* emit({
1267 kind: "phase_started",
1268 phase: "pr",
1250 - label: "Generating PR content...",
1269 + label: `Generating ${terms.shortLabel} content...`,
1270 });
1271 const rangeContext = yield* gitCore.readRangeContext(cwd, baseBranch);
1272
@@ -1272,12 +1291,12 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1291 yield* emit({
1292 kind: "phase_started",
1293 phase: "pr",
1275 - label: "Creating GitHub pull request...",
1294 + label: `Creating ${terms.singular}...`,
1295 });
1277 - yield* gitHubCli
1278 - .createPullRequest({
1296 + yield* provider
1297 + .createChangeRequest({
1298 cwd,
1280 - baseBranch,
1299 + baseRefName: baseBranch,
1300 headSelector: headContext.preferredHeadSelector,
1301 title: generated.title,
1302 bodyFile,
@@ -1305,11 +1324,13 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1324 });
1325
1326 const localStatus: GitManagerShape["localStatus"] = Effect.fn("localStatus")(function* (input) {
1308 - return yield* Cache.get(localStatusResultCache, normalizeStatusCacheKey(input.cwd));
1327 + const cacheKey = yield* normalizeStatusCacheKey(input.cwd);
1328 + return yield* Cache.get(localStatusResultCache, cacheKey);
1329 });
1330 const remoteStatus: GitManagerShape["remoteStatus"] = Effect.fn("remoteStatus")(
1331 function* (input) {
1312 - return yield* Cache.get(remoteStatusResultCache, normalizeStatusCacheKey(input.cwd));
1332 + const cacheKey = yield* normalizeStatusCacheKey(input.cwd);
1333 + return yield* Cache.get(remoteStatusResultCache, cacheKey);
1334 },
1335 );
1336 const status: GitManagerShape["status"] = Effect.fn("status")(function* (input) {
@@ -1335,8 +1356,8 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1356
1357 const resolvePullRequest: GitManagerShape["resolvePullRequest"] = Effect.fn("resolvePullRequest")(
1358 function* (input) {
1338 - const pullRequest = yield* gitHubCli
1339 - .getPullRequest({
1359 + const pullRequest = yield* (yield* sourceControlProvider(input.cwd))
1360 + .getChangeRequest({
1361 cwd: input.cwd,
1362 reference: normalizePullRequestReference(input.reference),
1363 })
@@ -1369,15 +1390,15 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1390 };
1391 return yield* Effect.gen(function* () {
1392 const normalizedReference = normalizePullRequestReference(input.reference);
1372 - const rootWorktreePath = canonicalizeExistingPath(input.cwd);
1373 - const pullRequestSummary = yield* gitHubCli.getPullRequest({
1393 + const rootWorktreePath = yield* canonicalizeExistingPath(input.cwd);
1394 + const pullRequestSummary = yield* (yield* sourceControlProvider(input.cwd)).getChangeRequest({
1395 cwd: input.cwd,
1396 reference: normalizedReference,
1397 });
1398 const pullRequest = toResolvedPullRequest(pullRequestSummary);
1399
1400 if (input.mode === "local") {
1380 - yield* gitHubCli.checkoutPullRequest({
1401 + yield* (yield* sourceControlProvider(input.cwd)).checkoutChangeRequest({
1402 cwd: input.cwd,
1403 reference: normalizedReference,
1404 force: true,
@@ -1419,33 +1440,35 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1440 const localPullRequestBranch =
1441 resolvePullRequestWorktreeLocalBranchName(pullRequestWithRemoteInfo);
1442
1422 - const findLocalHeadBranch = (cwd: string) =>
1423 - gitCore.listBranches({ cwd }).pipe(
1424 - Effect.map((result) => {
1425 - const localBranch = result.branches.find(
1426 - (branch) => !branch.isRemote && branch.name === localPullRequestBranch,
1427 - );
1428 - if (localBranch) {
1429 - return localBranch;
1430 - }
1431 - if (localPullRequestBranch === pullRequest.headBranch) {
1432 - return null;
1433 - }
1434 - return (
1435 - result.branches.find(
1436 - (branch) =>
1437 - !branch.isRemote &&
1438 - branch.name === pullRequest.headBranch &&
1439 - branch.worktreePath !== null &&
1440 - canonicalizeExistingPath(branch.worktreePath) !== rootWorktreePath,
1441 - ) ?? null
1442 - );
1443 - }),
1443 + const findLocalHeadBranch = Effect.fn("findLocalHeadBranch")(function* (cwd: string) {
1444 + const result = yield* gitCore.listRefs({ cwd });
1445 + const localBranch = result.refs.find(
1446 + (branch) => !branch.isRemote && branch.name === localPullRequestBranch,
1447 );
1448 + if (localBranch) {
1449 + return localBranch;
1450 + }
1451 + if (localPullRequestBranch === pullRequest.headBranch) {
1452 + return null;
1453 + }
1454 +
1455 + for (const branch of result.refs) {
1456 + if (branch.isRemote || branch.name !== pullRequest.headBranch || !branch.worktreePath) {
1457 + continue;
1458 + }
1459 +
1460 + const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);
1461 + if (worktreePath !== rootWorktreePath) {
1462 + return branch;
1463 + }
1464 + }
1465 +
1466 + return null;
1467 + });
1468
1469 const existingBranchBeforeFetch = yield* findLocalHeadBranch(input.cwd);
1470 const existingBranchBeforeFetchPath = existingBranchBeforeFetch?.worktreePath
1448 - ? canonicalizeExistingPath(existingBranchBeforeFetch.worktreePath)
1471 + ? yield* canonicalizeExistingPath(existingBranchBeforeFetch.worktreePath)
1472 : null;
1473 if (
1474 existingBranchBeforeFetch?.worktreePath &&
@@ -1473,7 +1496,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1496
1497 const existingBranchAfterFetch = yield* findLocalHeadBranch(input.cwd);
1498 const existingBranchAfterFetchPath = existingBranchAfterFetch?.worktreePath
1476 - ? canonicalizeExistingPath(existingBranchAfterFetch.worktreePath)
1499 + ? yield* canonicalizeExistingPath(existingBranchAfterFetch.worktreePath)
1500 : null;
1501 if (
1502 existingBranchAfterFetch?.worktreePath &&
@@ -1495,7 +1518,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1518
1519 const worktree = yield* gitCore.createWorktree({
1520 cwd: input.cwd,
1498 - branch: localPullRequestBranch,
1521 + refName: localPullRequestBranch,
1522 path: null,
1523 });
1524 yield* ensureExistingWorktreeUpstream(worktree.worktree.path);
@@ -1503,7 +1526,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1526
1527 return {
1528 pullRequest,
1506 - branch: worktree.worktree.branch,
1529 + branch: worktree.worktree.refName,
1530 worktreePath: worktree.worktree.path,
1531 };
1532 }).pipe(Effect.ensuring(invalidateStatus(input.cwd)));
@@ -1535,8 +1558,8 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1558 const existingBranchNames = yield* gitCore.listLocalBranchNames(cwd);
1559 const resolvedBranch = resolveAutoFeatureBranchName(existingBranchNames, preferredBranch);
1560
1538 - yield* gitCore.createBranch({ cwd, branch: resolvedBranch });
1539 - yield* Effect.scoped(gitCore.checkoutBranch({ cwd, branch: resolvedBranch }));
1561 + yield* gitCore.createRef({ cwd, refName: resolvedBranch });
1562 + yield* Effect.scoped(gitCore.switchRef({ cwd, refName: resolvedBranch }));
1563
1564 return {
1565 branchStep: { status: "created" as const, name: resolvedBranch },
@@ -1639,6 +1662,12 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1662
1663 const currentBranch = branchStep.name ?? initialStatus.branch;
1664 const commitAction = isCommitAction(input.action) ? input.action : null;
1665 + const changeRequestTerms = wantsPr
1666 + ? yield* sourceControlProvider(input.cwd).pipe(
1667 + Effect.map((provider) => getChangeRequestTerminologyForKind(provider.kind)),
1668 + Effect.catch(() => Effect.succeed(getChangeRequestTerminologyForKind("unknown"))),
1669 + )
1670 + : null;
1671
1672 const commit = commitAction
1673 ? yield* Ref.set(currentPhase, Option.some("commit")).pipe(
@@ -1676,7 +1705,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1705 .emit({
1706 kind: "phase_started",
1707 phase: "pr",
1679 - label: "Preparing PR...",
1708 + label: `Preparing ${changeRequestTerms?.shortLabel ?? "PR"}...`,
1709 })
1710 .pipe(
1711 Effect.tap(() => Ref.set(currentPhase, Option.some("pr"))),
@@ -1737,4 +1766,4 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
1766 } satisfies GitManagerShape;
1767 });
1768
1740 -export const GitManagerLive = Layer.effect(GitManager, makeGitManager());
1769 +export const layer = Layer.effect(GitManager, makeGitManager());
apps/server/src/git/GitWorkflowService.test.ts new
+132
@@ -0,0 +1,132 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { Effect, Layer } from "effect";
3 +import { describe, vi } from "vitest";
4 +
5 +import { GitManager } from "./GitManager.ts";
6 +import { GitWorkflowService, layer as GitWorkflowServiceLayer } from "./GitWorkflowService.ts";
7 +import { GitVcsDriver } from "../vcs/GitVcsDriver.ts";
8 +import { VcsDriverRegistry, type VcsDriverRegistryShape } from "../vcs/VcsDriverRegistry.ts";
9 +
10 +function makeLayer(input: { readonly detect: VcsDriverRegistryShape["detect"] }) {
11 + return GitWorkflowServiceLayer.pipe(
12 + Layer.provide(
13 + Layer.mock(VcsDriverRegistry)({
14 + detect: input.detect,
15 + }),
16 + ),
17 + Layer.provide(Layer.mock(GitVcsDriver)({})),
18 + Layer.provide(Layer.mock(GitManager)({})),
19 + );
20 +}
21 +
22 +describe("GitWorkflowService", () => {
23 + it.effect("returns an empty local status when no VCS repository is detected", () =>
24 + Effect.gen(function* () {
25 + const workflow = yield* GitWorkflowService;
26 + const status = yield* workflow.localStatus({ cwd: "/not-a-repo" });
27 +
28 + assert.deepStrictEqual(status, {
29 + isRepo: false,
30 + hasPrimaryRemote: false,
31 + isDefaultRef: false,
32 + refName: null,
33 + hasWorkingTreeChanges: false,
34 + workingTree: {
35 + files: [],
36 + insertions: 0,
37 + deletions: 0,
38 + },
39 + });
40 + }).pipe(
41 + Effect.provide(
42 + makeLayer({
43 + detect: () => Effect.succeed(null),
44 + }),
45 + ),
46 + ),
47 + );
48 +
49 + it.effect("returns an empty full status when no VCS repository is detected", () =>
50 + Effect.gen(function* () {
51 + const workflow = yield* GitWorkflowService;
52 + const status = yield* workflow.status({ cwd: "/not-a-repo" });
53 +
54 + assert.deepStrictEqual(status, {
55 + isRepo: false,
56 + hasPrimaryRemote: false,
57 + isDefaultRef: false,
58 + refName: null,
59 + hasWorkingTreeChanges: false,
60 + workingTree: {
61 + files: [],
62 + insertions: 0,
63 + deletions: 0,
64 + },
65 + hasUpstream: false,
66 + aheadCount: 0,
67 + behindCount: 0,
68 + pr: null,
69 + });
70 + }).pipe(
71 + Effect.provide(
72 + makeLayer({
73 + detect: () => Effect.succeed(null),
74 + }),
75 + ),
76 + ),
77 + );
78 +
79 + it.effect("does not call GitManager status methods when no VCS repository is detected", () => {
80 + const localStatus = vi.fn();
81 + const remoteStatus = vi.fn();
82 + const status = vi.fn();
83 +
84 + const testLayer = GitWorkflowServiceLayer.pipe(
85 + Layer.provide(
86 + Layer.mock(VcsDriverRegistry)({
87 + detect: () => Effect.succeed(null),
88 + }),
89 + ),
90 + Layer.provide(Layer.mock(GitVcsDriver)({})),
91 + Layer.provide(
92 + Layer.mock(GitManager)({
93 + localStatus,
94 + remoteStatus,
95 + status,
96 + }),
97 + ),
98 + );
99 +
100 + return Effect.gen(function* () {
101 + const workflow = yield* GitWorkflowService;
102 + yield* workflow.localStatus({ cwd: "/not-a-repo" });
103 + yield* workflow.remoteStatus({ cwd: "/not-a-repo" });
104 + yield* workflow.status({ cwd: "/not-a-repo" });
105 +
106 + assert.equal(localStatus.mock.calls.length, 0);
107 + assert.equal(remoteStatus.mock.calls.length, 0);
108 + assert.equal(status.mock.calls.length, 0);
109 + }).pipe(Effect.provide(testLayer));
110 + });
111 +
112 + it.effect("returns an empty ref list when no VCS repository is detected", () =>
113 + Effect.gen(function* () {
114 + const workflow = yield* GitWorkflowService;
115 + const refs = yield* workflow.listRefs({ cwd: "/not-a-repo" });
116 +
117 + assert.deepStrictEqual(refs, {
118 + refs: [],
119 + isRepo: false,
120 + hasPrimaryRemote: false,
121 + nextCursor: null,
122 + totalCount: 0,
123 + });
124 + }).pipe(
125 + Effect.provide(
126 + makeLayer({
127 + detect: () => Effect.succeed(null),
128 + }),
129 + ),
130 + ),
131 + );
132 +});
apps/server/src/git/GitWorkflowService.ts new
+313
@@ -0,0 +1,313 @@
1 +import { Context, Effect, Layer } from "effect";
2 +
3 +import {
4 + GitManagerError,
5 + GitCommandError,
6 + type VcsSwitchRefInput,
7 + type VcsSwitchRefResult,
8 + type VcsCreateRefInput,
9 + type VcsCreateRefResult,
10 + type VcsCreateWorktreeInput,
11 + type VcsCreateWorktreeResult,
12 + type VcsListRefsInput,
13 + type VcsListRefsResult,
14 + type GitManagerServiceError,
15 + type GitPreparePullRequestThreadInput,
16 + type GitPreparePullRequestThreadResult,
17 + type GitPullRequestRefInput,
18 + type VcsPullResult,
19 + type VcsRemoveWorktreeInput,
20 + type GitResolvePullRequestResult,
21 + type GitRunStackedActionInput,
22 + type GitRunStackedActionResult,
23 + type VcsStatusInput,
24 + type VcsStatusLocalResult,
25 + type VcsStatusRemoteResult,
26 + type VcsStatusResult,
27 +} from "@t3tools/contracts";
28 +
29 +import { GitManager, type GitRunStackedActionOptions } from "./GitManager.ts";
30 +import { GitVcsDriver } from "../vcs/GitVcsDriver.ts";
31 +import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
32 +
33 +export interface GitWorkflowServiceShape {
34 + readonly status: (
35 + input: VcsStatusInput,
36 + ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
37 + readonly localStatus: (
38 + input: VcsStatusInput,
39 + ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
40 + readonly remoteStatus: (
41 + input: VcsStatusInput,
42 + ) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
43 + readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
44 + readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
45 + readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
46 + readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
47 + readonly runStackedAction: (
48 + input: GitRunStackedActionInput,
49 + options?: GitRunStackedActionOptions,
50 + ) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
51 + readonly resolvePullRequest: (
52 + input: GitPullRequestRefInput,
53 + ) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
54 + readonly preparePullRequestThread: (
55 + input: GitPreparePullRequestThreadInput,
56 + ) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;
57 + readonly listRefs: (input: VcsListRefsInput) => Effect.Effect<VcsListRefsResult, GitCommandError>;
58 + readonly createWorktree: (
59 + input: VcsCreateWorktreeInput,
60 + ) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
61 + readonly removeWorktree: (input: VcsRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
62 + readonly createRef: (
63 + input: VcsCreateRefInput,
64 + ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
65 + readonly switchRef: (
66 + input: VcsSwitchRefInput,
67 + ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
68 + readonly renameBranch: (input: {
69 + readonly cwd: string;
70 + readonly oldBranch: string;
71 + readonly newBranch: string;
72 + }) => Effect.Effect<{ readonly branch: string }, GitManagerServiceError>;
73 +}
74 +
75 +export class GitWorkflowService extends Context.Service<
76 + GitWorkflowService,
77 + GitWorkflowServiceShape
78 +>()("t3/git/GitWorkflowService") {}
79 +
80 +const unsupportedGitWorkflow = (operation: string, cwd: string, detail: string) =>
81 + new GitManagerError({
82 + operation,
83 + detail: `${detail} (${cwd})`,
84 + });
85 +
86 +const unsupportedGitCommand = (operation: string, cwd: string, detail: string) =>
87 + new GitCommandError({
88 + operation,
89 + command: "vcs-route",
90 + cwd,
91 + detail,
92 + });
93 +
94 +function nonRepositoryLocalStatus(): VcsStatusLocalResult {
95 + return {
96 + isRepo: false,
97 + hasPrimaryRemote: false,
98 + isDefaultRef: false,
99 + refName: null,
100 + hasWorkingTreeChanges: false,
101 + workingTree: {
102 + files: [],
103 + insertions: 0,
104 + deletions: 0,
105 + },
106 + };
107 +}
108 +
109 +function nonRepositoryStatus(): VcsStatusResult {
110 + return {
111 + ...nonRepositoryLocalStatus(),
112 + hasUpstream: false,
113 + aheadCount: 0,
114 + behindCount: 0,
115 + pr: null,
116 + };
117 +}
118 +
119 +function nonRepositoryListRefs(): VcsListRefsResult {
120 + return {
121 + refs: [],
122 + isRepo: false,
123 + hasPrimaryRemote: false,
124 + nextCursor: null,
125 + totalCount: 0,
126 + };
127 +}
128 +
129 +export const make = Effect.fn("makeGitWorkflowService")(function* () {
130 + const registry = yield* VcsDriverRegistry;
131 + const git = yield* GitVcsDriver;
132 + const gitManager = yield* GitManager;
133 +
134 + const ensureGit = Effect.fn("GitWorkflowService.ensureGit")(function* (
135 + operation: string,
136 + cwd: string,
137 + ) {
138 + const handle = yield* registry
139 + .resolve({ cwd })
140 + .pipe(
141 + Effect.mapError((error) =>
142 + unsupportedGitWorkflow(
143 + operation,
144 + cwd,
145 + error instanceof Error ? error.message : String(error),
146 + ),
147 + ),
148 + );
149 + if (handle.kind !== "git") {
150 + return yield* unsupportedGitWorkflow(
151 + operation,
152 + cwd,
153 + `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
154 + );
155 + }
156 + });
157 +
158 + const ensureGitCommand = Effect.fn("GitWorkflowService.ensureGitCommand")(function* (
159 + operation: string,
160 + cwd: string,
161 + ) {
162 + const handle = yield* registry
163 + .resolve({ cwd })
164 + .pipe(
165 + Effect.mapError((error) =>
166 + unsupportedGitCommand(
167 + operation,
168 + cwd,
169 + error instanceof Error ? error.message : String(error),
170 + ),
171 + ),
172 + );
173 + if (handle.kind !== "git") {
174 + return yield* unsupportedGitCommand(
175 + operation,
176 + cwd,
177 + `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
178 + );
179 + }
180 + });
181 +
182 + const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
183 + function* (operation: string, cwd: string) {
184 + const handle = yield* registry
185 + .detect({ cwd })
186 + .pipe(
187 + Effect.mapError((error) =>
188 + unsupportedGitWorkflow(
189 + operation,
190 + cwd,
191 + error instanceof Error ? error.message : String(error),
192 + ),
193 + ),
194 + );
195 + if (!handle) {
196 + return false;
197 + }
198 + if (handle.kind !== "git") {
199 + return yield* unsupportedGitWorkflow(
200 + operation,
201 + cwd,
202 + `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
203 + );
204 + }
205 + return true;
206 + },
207 + );
208 +
209 + const detectGitRepositoryForCommand = Effect.fn(
210 + "GitWorkflowService.detectGitRepositoryForCommand",
211 + )(function* (operation: string, cwd: string) {
212 + const handle = yield* registry
213 + .detect({ cwd })
214 + .pipe(
215 + Effect.mapError((error) =>
216 + unsupportedGitCommand(
217 + operation,
218 + cwd,
219 + error instanceof Error ? error.message : String(error),
220 + ),
221 + ),
222 + );
223 + if (!handle) {
224 + return false;
225 + }
226 + if (handle.kind !== "git") {
227 + return yield* unsupportedGitCommand(
228 + operation,
229 + cwd,
230 + `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
231 + );
232 + }
233 + return true;
234 + });
235 +
236 + const routeGitManager =
237 + <Input extends { readonly cwd: string }, Output>(
238 + operation: string,
239 + run: (input: Input) => Effect.Effect<Output, GitManagerServiceError>,
240 + ) =>
241 + (input: Input) =>
242 + ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
243 +
244 + return GitWorkflowService.of({
245 + status: (input) =>
246 + detectGitRepositoryForStatus("GitWorkflowService.status", input.cwd).pipe(
247 + Effect.flatMap((isGitRepository) =>
248 + isGitRepository ? gitManager.status(input) : Effect.succeed(nonRepositoryStatus()),
249 + ),
250 + ),
251 + localStatus: (input) =>
252 + detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe(
253 + Effect.flatMap((isGitRepository) =>
254 + isGitRepository
255 + ? gitManager.localStatus(input)
256 + : Effect.succeed(nonRepositoryLocalStatus()),
257 + ),
258 + ),
259 + remoteStatus: (input) =>
260 + detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
261 + Effect.flatMap((isGitRepository) =>
262 + isGitRepository ? gitManager.remoteStatus(input) : Effect.succeed(null),
263 + ),
264 + ),
265 + invalidateLocalStatus: gitManager.invalidateLocalStatus,
266 + invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
267 + invalidateStatus: gitManager.invalidateStatus,
268 + pullCurrentBranch: (cwd) =>
269 + ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
270 + Effect.andThen(git.pullCurrentBranch(cwd)),
271 + ),
272 + runStackedAction: (input, options) =>
273 + ensureGit("GitWorkflowService.runStackedAction", input.cwd).pipe(
274 + Effect.andThen(gitManager.runStackedAction(input, options)),
275 + ),
276 + resolvePullRequest: routeGitManager(
277 + "GitWorkflowService.resolvePullRequest",
278 + gitManager.resolvePullRequest,
279 + ),
280 + preparePullRequestThread: routeGitManager(
281 + "GitWorkflowService.preparePullRequestThread",
282 + gitManager.preparePullRequestThread,
283 + ),
284 + listRefs: (input) =>
285 + detectGitRepositoryForCommand("GitWorkflowService.listRefs", input.cwd).pipe(
286 + Effect.flatMap((isGitRepository) =>
287 + isGitRepository ? git.listRefs(input) : Effect.succeed(nonRepositoryListRefs()),
288 + ),
289 + ),
290 + createWorktree: (input) =>
291 + ensureGitCommand("GitWorkflowService.createWorktree", input.cwd).pipe(
292 + Effect.andThen(git.createWorktree(input)),
293 + ),
294 + removeWorktree: (input) =>
295 + ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
296 + Effect.andThen(git.removeWorktree(input)),
297 + ),
298 + createRef: (input) =>
299 + ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
300 + Effect.andThen(git.createRef(input)),
301 + ),
302 + switchRef: (input) =>
303 + ensureGitCommand("GitWorkflowService.switchRef", input.cwd).pipe(
304 + Effect.andThen(Effect.scoped(git.switchRef(input))),
305 + ),
306 + renameBranch: (input) =>
307 + ensureGit("GitWorkflowService.renameBranch", input.cwd).pipe(
308 + Effect.andThen(git.renameBranch(input)),
309 + ),
310 + });
311 +});
312 +
313 +export const layer = Layer.effect(GitWorkflowService, make());
apps/server/src/git/Layers/GitCore.test.ts deleted
-2333
@@ -1,2333 +0,0 @@
1 -import { existsSync } from "node:fs";
2 -import path from "node:path";
3 -
4 -import * as NodeServices from "@effect/platform-node/NodeServices";
5 -import { it } from "@effect/vitest";
6 -import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect";
7 -import { describe, expect, vi } from "vitest";
8 -
9 -import { GitCoreLive, makeGitCore } from "./GitCore.ts";
10 -import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";
11 -import { GitCommandError } from "@t3tools/contracts";
12 -import { type ProcessRunResult, runProcess } from "../../processRunner.ts";
13 -import { ServerConfig } from "../../config.ts";
14 -
15 -// ── Helpers ──
16 -
17 -const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), { prefix: "t3-git-core-test-" });
18 -const GitCoreTestLayer = GitCoreLive.pipe(
19 - Layer.provide(ServerConfigLayer),
20 - Layer.provide(NodeServices.layer),
21 -);
22 -const TestLayer = Layer.mergeAll(NodeServices.layer, GitCoreTestLayer);
23 -
24 -function makeTmpDir(
25 - prefix = "git-test-",
26 -): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> {
27 - return Effect.gen(function* () {
28 - const fileSystem = yield* FileSystem.FileSystem;
29 - return yield* fileSystem.makeTempDirectoryScoped({ prefix });
30 - });
31 -}
32 -
33 -function writeTextFile(
34 - filePath: string,
35 - contents: string,
36 -): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
37 - return Effect.gen(function* () {
38 - const fileSystem = yield* FileSystem.FileSystem;
39 - yield* fileSystem.writeFileString(filePath, contents);
40 - });
41 -}
42 -
43 -function removePath(
44 - targetPath: string,
45 -): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
46 - return Effect.gen(function* () {
47 - const fileSystem = yield* FileSystem.FileSystem;
48 - yield* fileSystem.remove(targetPath, { recursive: true, force: true });
49 - });
50 -}
51 -
52 -function makeDirectory(
53 - dirPath: string,
54 -): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
55 - return Effect.gen(function* () {
56 - const fileSystem = yield* FileSystem.FileSystem;
57 - yield* fileSystem.makeDirectory(dirPath, { recursive: true });
58 - });
59 -}
60 -
61 -/** Run a raw git command for test setup (not under test). */
62 -function git(
63 - cwd: string,
64 - args: ReadonlyArray<string>,
65 - env?: NodeJS.ProcessEnv,
66 -): Effect.Effect<string, GitCommandError, GitCore> {
67 - return Effect.gen(function* () {
68 - const gitCore = yield* GitCore;
69 - const result = yield* gitCore.execute({
70 - operation: "GitCore.test.git",
71 - cwd,
72 - args,
73 - ...(env ? { env } : {}),
74 - timeoutMs: 10_000,
75 - });
76 - return result.stdout.trim();
77 - });
78 -}
79 -
80 -function configureRemote(
81 - cwd: string,
82 - remoteName: string,
83 - remotePath: string,
84 - fetchNamespace: string,
85 -): Effect.Effect<string, GitCommandError, GitCore> {
86 - return Effect.gen(function* () {
87 - yield* git(cwd, ["config", `remote.${remoteName}.url`, remotePath]);
88 - return yield* git(cwd, [
89 - "config",
90 - "--replace-all",
91 - `remote.${remoteName}.fetch`,
92 - `+refs/heads/*:refs/remotes/${fetchNamespace}/*`,
93 - ]);
94 - });
95 -}
96 -
97 -function runShellCommand(input: {
98 - command: string;
99 - cwd: string;
100 - timeoutMs?: number;
101 - maxOutputBytes?: number;
102 -}): Effect.Effect<ProcessRunResult, Error> {
103 - return Effect.promise(() => {
104 - const shellPath =
105 - process.platform === "win32"
106 - ? (process.env.ComSpec ?? "cmd.exe")
107 - : (process.env.SHELL ?? "/bin/sh");
108 -
109 - const args =
110 - process.platform === "win32" ? ["/d", "/s", "/c", input.command] : ["-lc", input.command];
111 -
112 - return runProcess(shellPath, args, {
113 - cwd: input.cwd,
114 - timeoutMs: input.timeoutMs ?? 30_000,
115 - allowNonZeroExit: true,
116 - maxBufferBytes: input.maxOutputBytes ?? 1_000_000,
117 - outputMode: "truncate",
118 - });
119 - });
120 -}
121 -
122 -const makeIsolatedGitCore = (executeOverride: GitCoreShape["execute"]) =>
123 - makeGitCore({ executeOverride }).pipe(
124 - Effect.provide(Layer.provideMerge(ServerConfigLayer, NodeServices.layer)),
125 - );
126 -
127 -/** Create a repo with an initial commit so branches work. */
128 -function initRepoWithCommit(
129 - cwd: string,
130 -): Effect.Effect<
131 - { initialBranch: string },
132 - GitCommandError | PlatformError.PlatformError,
133 - GitCore | FileSystem.FileSystem
134 -> {
135 - return Effect.gen(function* () {
136 - const core = yield* GitCore;
137 - yield* core.initRepo({ cwd });
138 - yield* git(cwd, ["config", "user.email", "test@test.com"]);
139 - yield* git(cwd, ["config", "user.name", "Test"]);
140 - yield* writeTextFile(path.join(cwd, "README.md"), "# test\n");
141 - yield* git(cwd, ["add", "."]);
142 - yield* git(cwd, ["commit", "-m", "initial commit"]);
143 - const initialBranch = yield* git(cwd, ["branch", "--show-current"]);
144 - return { initialBranch };
145 - });
146 -}
147 -
148 -function commitWithDate(
149 - cwd: string,
150 - fileName: string,
151 - fileContents: string,
152 - dateIsoString: string,
153 - message: string,
154 -): Effect.Effect<
155 - void,
156 - GitCommandError | PlatformError.PlatformError,
157 - GitCore | FileSystem.FileSystem
158 -> {
159 - return Effect.gen(function* () {
160 - yield* writeTextFile(path.join(cwd, fileName), fileContents);
161 - yield* git(cwd, ["add", fileName]);
162 - yield* git(cwd, ["commit", "-m", message], {
163 - ...process.env,
164 - GIT_AUTHOR_DATE: dateIsoString,
165 - GIT_COMMITTER_DATE: dateIsoString,
166 - });
167 - });
168 -}
169 -
170 -function buildLargeText(lineCount = 20_000): string {
171 - return Array.from({ length: lineCount }, (_, index) => `line ${String(index).padStart(5, "0")}`)
172 - .join("\n")
173 - .concat("\n");
174 -}
175 -
176 -function splitNullSeparatedPaths(input: string): string[] {
177 - return input
178 - .split("\0")
179 - .map((value) => value.trim())
180 - .filter((value) => value.length > 0);
181 -}
182 -
183 -// ── Tests ──
184 -
185 -it.layer(TestLayer)("git integration", (it) => {
186 - describe("shell process execution", () => {
187 - it.effect("caps captured output when maxOutputBytes is exceeded", () =>
188 - Effect.gen(function* () {
189 - const result = yield* runShellCommand({
190 - command: `node -e "process.stdout.write('x'.repeat(2000))"`,
191 - cwd: process.cwd(),
192 - timeoutMs: 10_000,
193 - maxOutputBytes: 128,
194 - });
195 -
196 - expect(result.code).toBe(0);
197 - expect(result.stdout.length).toBeLessThanOrEqual(128);
198 - expect(result.stdoutTruncated || result.stderrTruncated).toBe(true);
199 - }),
200 - );
201 - });
202 -
203 - // ── initGitRepo ──
204 -
205 - describe("initGitRepo", () => {
206 - it.effect("creates a valid git repo", () =>
207 - Effect.gen(function* () {
208 - const tmp = yield* makeTmpDir();
209 - yield* (yield* GitCore).initRepo({ cwd: tmp });
210 - expect(existsSync(path.join(tmp, ".git"))).toBe(true);
211 - }),
212 - );
213 -
214 - it.effect("listGitBranches reports isRepo: true after init + commit", () =>
215 - Effect.gen(function* () {
216 - const tmp = yield* makeTmpDir();
217 - yield* initRepoWithCommit(tmp);
218 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
219 - expect(result.isRepo).toBe(true);
220 - expect(result.hasOriginRemote).toBe(false);
221 - expect(result.branches.length).toBeGreaterThanOrEqual(1);
222 - }),
223 - );
224 - });
225 -
226 - describe("workspace helpers", () => {
227 - it.effect("filterIgnoredPaths chunks large path lists and preserves kept paths", () =>
228 - Effect.gen(function* () {
229 - const cwd = "/virtual/repo";
230 - const relativePaths = Array.from({ length: 340 }, (_, index) => {
231 - const prefix = index % 3 === 0 ? "ignored" : "kept";
232 - return `${prefix}/segment-${String(index).padStart(4, "0")}/${"x".repeat(900)}.ts`;
233 - });
234 - const expectedPaths = relativePaths.filter(
235 - (relativePath) => !relativePath.startsWith("ignored/"),
236 - );
237 -
238 - const seenChunks: string[][] = [];
239 - const core = yield* makeIsolatedGitCore((input) => {
240 - if (
241 - input.args.join(" ") !==
242 - "-c core.fsmonitor=false -c core.untrackedCache=false check-ignore --no-index -z --stdin"
243 - ) {
244 - return Effect.fail(
245 - new GitCommandError({
246 - operation: input.operation,
247 - command: `git ${input.args.join(" ")}`,
248 - cwd: input.cwd,
249 - detail: "unexpected git command in chunking test",
250 - }),
251 - );
252 - }
253 -
254 - const chunkPaths = splitNullSeparatedPaths(input.stdin ?? "");
255 - seenChunks.push(chunkPaths);
256 - const ignoredPaths = chunkPaths.filter((relativePath) =>
257 - relativePath.startsWith("ignored/"),
258 - );
259 -
260 - return Effect.succeed({
261 - code: ignoredPaths.length > 0 ? 0 : 1,
262 - stdout: ignoredPaths.length > 0 ? `${ignoredPaths.join("\0")}\0` : "",
263 - stderr: "",
264 - stdoutTruncated: false,
265 - stderrTruncated: false,
266 - });
267 - });
268 -
269 - const result = yield* core.filterIgnoredPaths(cwd, relativePaths);
270 -
271 - expect(seenChunks.length).toBeGreaterThan(1);
272 - expect(seenChunks.flat()).toEqual(relativePaths);
273 - expect(result).toEqual(expectedPaths);
274 - }),
275 - );
276 -
277 - it.effect("listWorkspaceFiles disables fsmonitor and untracked cache helpers", () =>
278 - Effect.gen(function* () {
279 - const core = yield* makeIsolatedGitCore((input) => {
280 - expect(input.args).toEqual([
281 - "-c",
282 - "core.fsmonitor=false",
283 - "-c",
284 - "core.untrackedCache=false",
285 - "ls-files",
286 - "--cached",
287 - "--others",
288 - "--exclude-standard",
289 - "-z",
290 - ]);
291 - return Effect.succeed({
292 - code: 0,
293 - stdout: "src/index.ts\0README.md\0",
294 - stderr: "",
295 - stdoutTruncated: false,
296 - stderrTruncated: false,
297 - });
298 - });
299 -
300 - const result = yield* core.listWorkspaceFiles("/virtual/repo");
301 - expect(result.paths).toEqual(["src/index.ts", "README.md"]);
302 - expect(result.truncated).toBe(false);
303 - }),
304 - );
305 - });
306 -
307 - // ── listGitBranches ──
308 -
309 - describe("listGitBranches", () => {
310 - it.effect("returns isRepo: false for non-git directory", () =>
311 - Effect.gen(function* () {
312 - const tmp = yield* makeTmpDir();
313 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
314 - expect(result.isRepo).toBe(false);
315 - expect(result.hasOriginRemote).toBe(false);
316 - expect(result.branches).toEqual([]);
317 - }),
318 - );
319 -
320 - it.effect("returns isRepo: false for deleted directories", () =>
321 - Effect.gen(function* () {
322 - const tmp = yield* makeTmpDir();
323 - const deletedDir = path.join(tmp, "deleted-repo");
324 - yield* makeDirectory(deletedDir);
325 - yield* removePath(deletedDir);
326 -
327 - const result = yield* (yield* GitCore).listBranches({ cwd: deletedDir });
328 -
329 - expect(result.isRepo).toBe(false);
330 - expect(result.hasOriginRemote).toBe(false);
331 - expect(result.branches).toEqual([]);
332 - }),
333 - );
334 -
335 - it.effect("returns the current branch with current: true", () =>
336 - Effect.gen(function* () {
337 - const tmp = yield* makeTmpDir();
338 - yield* initRepoWithCommit(tmp);
339 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
340 - const current = result.branches.find((b) => b.current);
341 - expect(current).toBeDefined();
342 - expect(current!.current).toBe(true);
343 - }),
344 - );
345 -
346 - it.effect("does not include detached HEAD pseudo-refs as branches", () =>
347 - Effect.gen(function* () {
348 - const tmp = yield* makeTmpDir();
349 - yield* initRepoWithCommit(tmp);
350 - yield* git(tmp, ["checkout", "--detach", "HEAD"]);
351 -
352 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
353 - expect(result.branches.some((branch) => branch.name.startsWith("("))).toBe(false);
354 - expect(result.branches.some((branch) => branch.current)).toBe(false);
355 - }),
356 - );
357 -
358 - it.effect("keeps current branch first and sorts the remaining branches by recency", () =>
359 - Effect.gen(function* () {
360 - const tmp = yield* makeTmpDir();
361 - yield* initRepoWithCommit(tmp);
362 - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
363 - (branch) => branch.current,
364 - )!.name;
365 -
366 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "older-branch" });
367 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "older-branch" });
368 - yield* commitWithDate(
369 - tmp,
370 - "older.txt",
371 - "older branch change\n",
372 - "Thu, 1 Jan 2037 00:00:00 +0000",
373 - "older branch change",
374 - );
375 -
376 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: initialBranch });
377 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "newer-branch" });
378 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "newer-branch" });
379 - yield* commitWithDate(
380 - tmp,
381 - "newer.txt",
382 - "newer branch change\n",
383 - "Fri, 1 Jan 2038 00:00:00 +0000",
384 - "newer branch change",
385 - );
386 -
387 - // Switch away to show current branch is pinned, then remaining branches are recency-sorted.
388 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "older-branch" });
389 -
390 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
391 - expect(result.branches[0]!.name).toBe("older-branch");
392 - expect(result.branches[1]!.name).toBe("newer-branch");
393 - }),
394 - );
395 -
396 - it.effect("keeps default branch right after current branch", () =>
397 - Effect.gen(function* () {
398 - const tmp = yield* makeTmpDir();
399 - const remote = yield* makeTmpDir();
400 - yield* initRepoWithCommit(tmp);
401 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
402 - (branch) => branch.current,
403 - )!.name;
404 -
405 - yield* git(remote, ["init", "--bare"]);
406 - yield* git(tmp, ["remote", "add", "origin", remote]);
407 - yield* git(tmp, ["push", "-u", "origin", defaultBranch]);
408 - yield* git(tmp, ["remote", "set-head", "origin", defaultBranch]);
409 -
410 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "current-branch" });
411 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "current-branch" });
412 - yield* commitWithDate(
413 - tmp,
414 - "current.txt",
415 - "current change\n",
416 - "Thu, 1 Jan 2037 00:00:00 +0000",
417 - "current change",
418 - );
419 -
420 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: defaultBranch });
421 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "newer-branch" });
422 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "newer-branch" });
423 - yield* commitWithDate(
424 - tmp,
425 - "newer.txt",
426 - "newer change\n",
427 - "Fri, 1 Jan 2038 00:00:00 +0000",
428 - "newer change",
429 - );
430 -
431 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "current-branch" });
432 -
433 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
434 - expect(result.branches[0]!.name).toBe("current-branch");
435 - expect(result.branches[1]!.name).toBe(defaultBranch);
436 - expect(result.branches[2]!.name).toBe("newer-branch");
437 - }),
438 - );
439 -
440 - it.effect("lists multiple branches after creating them", () =>
441 - Effect.gen(function* () {
442 - const tmp = yield* makeTmpDir();
443 - yield* initRepoWithCommit(tmp);
444 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-a" });
445 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-b" });
446 -
447 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
448 - const names = result.branches.map((b) => b.name);
449 - expect(names).toContain("feature-a");
450 - expect(names).toContain("feature-b");
451 - }),
452 - );
453 -
454 - it.effect("paginates branch results and returns paging metadata", () =>
455 - Effect.gen(function* () {
456 - const tmp = yield* makeTmpDir();
457 - const { initialBranch } = yield* initRepoWithCommit(tmp);
458 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-a" });
459 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-b" });
460 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-c" });
461 -
462 - const firstPage = yield* (yield* GitCore).listBranches({ cwd: tmp, limit: 2 });
463 - expect(firstPage.totalCount).toBe(4);
464 - expect(firstPage.nextCursor).toBe(2);
465 - expect(firstPage.branches.map((branch) => branch.name)).toEqual([
466 - initialBranch,
467 - "feature-a",
468 - ]);
469 -
470 - const secondPage = yield* (yield* GitCore).listBranches({
471 - cwd: tmp,
472 - cursor: firstPage.nextCursor ?? 0,
473 - limit: 2,
474 - });
475 - expect(secondPage.totalCount).toBe(4);
476 - expect(secondPage.nextCursor).toBeNull();
477 - expect(secondPage.branches.map((branch) => branch.name)).toEqual([
478 - "feature-b",
479 - "feature-c",
480 - ]);
481 - }),
482 - );
483 -
484 - it.effect("parses separate branch names when column.ui is always enabled", () =>
485 - Effect.gen(function* () {
486 - const tmp = yield* makeTmpDir();
487 - const { initialBranch } = yield* initRepoWithCommit(tmp);
488 - const createdBranchNames = [
489 - "go-bin",
490 - "copilot/rewrite-cli-in-go",
491 - "copilot/rewrite-cli-in-rust",
492 - ] as const;
493 - for (const branchName of createdBranchNames) {
494 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: branchName });
495 - }
496 - yield* git(tmp, ["config", "column.ui", "always"]);
497 -
498 - const rawBranchOutput = yield* git(tmp, ["branch", "--no-color"], {
499 - ...process.env,
500 - COLUMNS: "120",
501 - });
502 - expect(
503 - rawBranchOutput
504 - .split("\n")
505 - .some(
506 - (line) =>
507 - createdBranchNames.filter((branchName) => line.includes(branchName)).length >= 2,
508 - ),
509 - ).toBe(true);
510 -
511 - const realGitCore = yield* GitCore;
512 - const core = yield* makeIsolatedGitCore((input) =>
513 - realGitCore.execute(
514 - input.args[0] === "branch"
515 - ? {
516 - ...input,
517 - env: { ...input.env, COLUMNS: "120" },
518 - }
519 - : input,
520 - ),
521 - );
522 -
523 - const result = yield* core.listBranches({ cwd: tmp });
524 - const localBranchNames = result.branches
525 - .filter((branch) => !branch.isRemote)
526 - .map((branch) => branch.name);
527 -
528 - expect(localBranchNames).toHaveLength(4);
529 - expect(localBranchNames).toEqual(
530 - expect.arrayContaining([initialBranch, ...createdBranchNames]),
531 - );
532 - expect(
533 - localBranchNames.some(
534 - (branchName) =>
535 - createdBranchNames.filter((createdBranch) => branchName.includes(createdBranch))
536 - .length >= 2,
537 - ),
538 - ).toBe(false);
539 - }),
540 - );
541 -
542 - it.effect("isDefault is false when no remote exists", () =>
543 - Effect.gen(function* () {
544 - const tmp = yield* makeTmpDir();
545 - yield* initRepoWithCommit(tmp);
546 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
547 - expect(result.branches.every((b) => b.isDefault === false)).toBe(true);
548 - }),
549 - );
550 -
551 - it.effect("lists local branches first and remote branches last", () =>
552 - Effect.gen(function* () {
553 - const remote = yield* makeTmpDir();
554 - const tmp = yield* makeTmpDir();
555 -
556 - yield* git(remote, ["init", "--bare"]);
557 - yield* initRepoWithCommit(tmp);
558 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
559 - (branch) => branch.current,
560 - )!.name;
561 -
562 - yield* git(tmp, ["remote", "add", "origin", remote]);
563 - yield* git(tmp, ["push", "-u", "origin", defaultBranch]);
564 -
565 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/local-only" });
566 -
567 - const remoteOnlyBranch = "feature/remote-only";
568 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: defaultBranch });
569 - yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]);
570 - yield* git(tmp, ["push", "-u", "origin", remoteOnlyBranch]);
571 - yield* git(tmp, ["checkout", defaultBranch]);
572 - yield* git(tmp, ["branch", "-D", remoteOnlyBranch]);
573 -
574 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
575 - const firstRemoteIndex = result.branches.findIndex((branch) => branch.isRemote);
576 -
577 - expect(result.hasOriginRemote).toBe(true);
578 - expect(firstRemoteIndex).toBeGreaterThan(0);
579 - expect(result.branches.slice(0, firstRemoteIndex).every((branch) => !branch.isRemote)).toBe(
580 - true,
581 - );
582 - expect(result.branches.slice(firstRemoteIndex).every((branch) => branch.isRemote)).toBe(
583 - true,
584 - );
585 - expect(
586 - result.branches.some(
587 - (branch) => branch.name === "feature/local-only" && !branch.isRemote,
588 - ),
589 - ).toBe(true);
590 - expect(
591 - result.branches.some(
592 - (branch) => branch.name === "origin/feature/remote-only" && branch.isRemote,
593 - ),
594 - ).toBe(true);
595 - }),
596 - );
597 -
598 - it.effect("includes remoteName metadata for remotes with slash in the name", () =>
599 - Effect.gen(function* () {
600 - const remote = yield* makeTmpDir();
601 - const tmp = yield* makeTmpDir();
602 - const remoteName = "my-org/upstream";
603 -
604 - yield* git(remote, ["init", "--bare"]);
605 - yield* initRepoWithCommit(tmp);
606 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
607 - (branch) => branch.current,
608 - )!.name;
609 -
610 - yield* git(tmp, ["remote", "add", remoteName, remote]);
611 - yield* git(tmp, ["push", "-u", remoteName, defaultBranch]);
612 -
613 - const remoteOnlyBranch = "feature/remote-with-remote-name";
614 - yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]);
615 - yield* git(tmp, ["push", "-u", remoteName, remoteOnlyBranch]);
616 - yield* git(tmp, ["checkout", defaultBranch]);
617 - yield* git(tmp, ["branch", "-D", remoteOnlyBranch]);
618 -
619 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
620 - const remoteBranch = result.branches.find(
621 - (branch) => branch.name === `${remoteName}/${remoteOnlyBranch}`,
622 - );
623 -
624 - expect(remoteBranch).toBeDefined();
625 - expect(remoteBranch?.isRemote).toBe(true);
626 - expect(remoteBranch?.remoteName).toBe(remoteName);
627 - }),
628 - );
629 -
630 - it.effect(
631 - "filters branch queries before pagination and dedupes origin refs with local matches",
632 - () =>
633 - Effect.gen(function* () {
634 - const remote = yield* makeTmpDir();
635 - const tmp = yield* makeTmpDir();
636 -
637 - yield* git(remote, ["init", "--bare"]);
638 - const { initialBranch } = yield* initRepoWithCommit(tmp);
639 - yield* git(tmp, ["remote", "add", "origin", remote]);
640 - yield* git(tmp, ["push", "-u", "origin", initialBranch]);
641 -
642 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/demo" });
643 - yield* git(tmp, ["push", "-u", "origin", "feature/demo"]);
644 -
645 - yield* git(tmp, ["checkout", "-b", "feature/remote-only"]);
646 - yield* git(tmp, ["push", "-u", "origin", "feature/remote-only"]);
647 - yield* git(tmp, ["checkout", initialBranch]);
648 - yield* git(tmp, ["branch", "-D", "feature/remote-only"]);
649 -
650 - const result = yield* (yield* GitCore).listBranches({
651 - cwd: tmp,
652 - query: "feature/",
653 - limit: 10,
654 - });
655 -
656 - expect(result.totalCount).toBe(2);
657 - expect(result.nextCursor).toBeNull();
658 - expect(result.branches.map((branch) => branch.name)).toEqual([
659 - "feature/demo",
660 - "origin/feature/remote-only",
661 - ]);
662 - }),
663 - );
664 - });
665 -
666 - // ── checkoutGitBranch ──
667 -
668 - describe("checkoutGitBranch", () => {
669 - it.effect("checks out an existing branch", () =>
670 - Effect.gen(function* () {
671 - const tmp = yield* makeTmpDir();
672 - yield* initRepoWithCommit(tmp);
673 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature" });
674 -
675 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature" });
676 -
677 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
678 - const current = result.branches.find((b) => b.current);
679 - expect(current!.name).toBe("feature");
680 - }),
681 - );
682 -
683 - it.effect("refreshes upstream behind count after checkout when remote branch advanced", () =>
684 - Effect.gen(function* () {
685 - const context = yield* Effect.context<never>();
686 - const runPromise = Effect.runPromiseWith(context);
687 -
688 - const remote = yield* makeTmpDir();
689 - const source = yield* makeTmpDir();
690 - const clone = yield* makeTmpDir();
691 - yield* git(remote, ["init", "--bare"]);
692 -
693 - yield* initRepoWithCommit(source);
694 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find(
695 - (branch) => branch.current,
696 - )!.name;
697 - yield* git(source, ["remote", "add", "origin", remote]);
698 - yield* git(source, ["push", "-u", "origin", defaultBranch]);
699 -
700 - const featureBranch = "feature-behind";
701 - yield* (yield* GitCore).createBranch({ cwd: source, branch: featureBranch });
702 - yield* (yield* GitCore).checkoutBranch({ cwd: source, branch: featureBranch });
703 - yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
704 - yield* git(source, ["add", "feature.txt"]);
705 - yield* git(source, ["commit", "-m", "feature base"]);
706 - yield* git(source, ["push", "-u", "origin", featureBranch]);
707 - yield* (yield* GitCore).checkoutBranch({ cwd: source, branch: defaultBranch });
708 -
709 - yield* git(clone, ["clone", remote, "."]);
710 - yield* git(clone, ["config", "user.email", "test@test.com"]);
711 - yield* git(clone, ["config", "user.name", "Test"]);
712 - yield* git(clone, ["checkout", "-b", featureBranch, "--track", `origin/${featureBranch}`]);
713 - yield* writeTextFile(path.join(clone, "feature.txt"), "feature from remote\n");
714 - yield* git(clone, ["add", "feature.txt"]);
715 - yield* git(clone, ["commit", "-m", "remote feature update"]);
716 - yield* git(clone, ["push", "origin", featureBranch]);
717 -
718 - yield* (yield* GitCore).checkoutBranch({ cwd: source, branch: featureBranch });
719 - const core = yield* GitCore;
720 - yield* Effect.promise(() =>
721 - vi.waitFor(
722 - async () => {
723 - const details = await runPromise(core.statusDetails(source));
724 - expect(details.branch).toBe(featureBranch);
725 - expect(details.aheadCount).toBe(0);
726 - expect(details.behindCount).toBe(1);
727 - },
728 - {
729 - timeout: 10_000,
730 - interval: 100,
731 - },
732 - ),
733 - );
734 - }),
735 - );
736 -
737 - it.effect("statusDetails remains successful when upstream refresh fails after checkout", () =>
738 - Effect.gen(function* () {
739 - const remote = yield* makeTmpDir();
740 - const source = yield* makeTmpDir();
741 - yield* git(remote, ["init", "--bare"]);
742 -
743 - yield* initRepoWithCommit(source);
744 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find(
745 - (branch) => branch.current,
746 - )!.name;
747 - yield* git(source, ["remote", "add", "origin", remote]);
748 - yield* git(source, ["push", "-u", "origin", defaultBranch]);
749 -
750 - const featureBranch = "feature-refresh-failure";
751 - yield* git(source, ["branch", featureBranch]);
752 - yield* git(source, ["checkout", featureBranch]);
753 - yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
754 - yield* git(source, ["add", "feature.txt"]);
755 - yield* git(source, ["commit", "-m", "feature base"]);
756 - yield* git(source, ["push", "-u", "origin", featureBranch]);
757 - yield* git(source, ["checkout", defaultBranch]);
758 -
759 - const realGitCore = yield* GitCore;
760 - let refreshFetchAttempts = 0;
761 - const core = yield* makeIsolatedGitCore((input) => {
762 - if (input.args[0] === "--git-dir" && input.args[2] === "fetch") {
763 - refreshFetchAttempts += 1;
764 - return Effect.fail(
765 - new GitCommandError({
766 - operation: "git.test.refreshFailure",
767 - command: `git ${input.args.join(" ")}`,
768 - cwd: input.cwd,
769 - detail: "simulated fetch timeout",
770 - }),
771 - );
772 - }
773 - return realGitCore.execute(input);
774 - });
775 - yield* core.checkoutBranch({ cwd: source, branch: featureBranch });
776 - const status = yield* core.statusDetails(source);
777 - expect(refreshFetchAttempts).toBe(1);
778 - expect(status.branch).toBe(featureBranch);
779 - expect(status.upstreamRef).toBe(`origin/${featureBranch}`);
780 - expect(yield* git(source, ["branch", "--show-current"])).toBe(featureBranch);
781 - }),
782 - );
783 -
784 - it.effect("defers upstream refresh until statusDetails is requested", () =>
785 - Effect.gen(function* () {
786 - const remote = yield* makeTmpDir();
787 - const source = yield* makeTmpDir();
788 - yield* git(remote, ["init", "--bare"]);
789 -
790 - yield* initRepoWithCommit(source);
791 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find(
792 - (branch) => branch.current,
793 - )!.name;
794 - yield* git(source, ["remote", "add", "origin", remote]);
795 - yield* git(source, ["push", "-u", "origin", defaultBranch]);
796 -
797 - const featureBranch = "feature/scoped-fetch";
798 - yield* git(source, ["checkout", "-b", featureBranch]);
799 - yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
800 - yield* git(source, ["add", "feature.txt"]);
801 - yield* git(source, ["commit", "-m", "feature base"]);
802 - yield* git(source, ["push", "-u", "origin", featureBranch]);
803 - yield* git(source, ["checkout", defaultBranch]);
804 -
805 - const realGitCore = yield* GitCore;
806 - let refreshFetchAttempts = 0;
807 - const core = yield* makeIsolatedGitCore((input) => {
808 - if (input.args[0] === "--git-dir" && input.args[2] === "fetch") {
809 - refreshFetchAttempts += 1;
810 - return Effect.succeed({
811 - code: 0,
812 - stdout: "",
813 - stderr: "",
814 - stdoutTruncated: false,
815 - stderrTruncated: false,
816 - });
817 - }
818 - return realGitCore.execute(input);
819 - });
820 - yield* core.checkoutBranch({ cwd: source, branch: featureBranch });
821 - yield* Effect.promise(() => new Promise<void>((resolve) => setTimeout(resolve, 50)));
822 - expect(refreshFetchAttempts).toBe(0);
823 - const status = yield* core.statusDetails(source);
824 - expect(status.branch).toBe(featureBranch);
825 - expect(refreshFetchAttempts).toBe(1);
826 - }),
827 - );
828 -
829 - it.effect("coalesces upstream refreshes across sibling worktrees on the same remote", () =>
830 - Effect.gen(function* () {
831 - const ok = (stdout = "") =>
832 - Effect.succeed({
833 - code: 0,
834 - stdout,
835 - stderr: "",
836 - stdoutTruncated: false,
837 - stderrTruncated: false,
838 - });
839 -
840 - let fetchCount = 0;
841 - const core = yield* makeIsolatedGitCore((input) => {
842 - if (
843 - input.args[0] === "rev-parse" &&
844 - input.args[1] === "--abbrev-ref" &&
845 - input.args[2] === "--symbolic-full-name" &&
846 - input.args[3] === "@{upstream}"
847 - ) {
848 - return ok(
849 - input.cwd === "/repo/worktrees/pr-123" ? "origin/feature/pr-123\n" : "origin/main\n",
850 - );
851 - }
852 - if (input.args[0] === "remote") {
853 - return ok("origin\n");
854 - }
855 - if (input.args[0] === "rev-parse" && input.args[1] === "--git-common-dir") {
856 - return ok("/repo/.git\n");
857 - }
858 - if (input.args[0] === "--git-dir" && input.args[2] === "fetch") {
859 - fetchCount += 1;
860 - expect(input.cwd).toBe("/repo");
861 - expect(input.args).toEqual([
862 - "--git-dir",
863 - "/repo/.git",
864 - "fetch",
865 - "--quiet",
866 - "--no-tags",
867 - "origin",
868 - ]);
869 - return ok();
870 - }
871 - if (input.operation === "GitCore.statusDetails.status") {
872 - return ok(
873 - input.cwd === "/repo/worktrees/pr-123"
874 - ? "# branch.head feature/pr-123\n# branch.upstream origin/feature/pr-123\n# branch.ab +0 -0\n"
875 - : "# branch.head main\n# branch.upstream origin/main\n# branch.ab +0 -0\n",
876 - );
877 - }
878 - if (
879 - input.operation === "GitCore.statusDetails.unstagedNumstat" ||
880 - input.operation === "GitCore.statusDetails.stagedNumstat"
881 - ) {
882 - return ok();
883 - }
884 - if (input.operation === "GitCore.statusDetails.defaultRef") {
885 - return ok("refs/remotes/origin/main\n");
886 - }
887 - return Effect.fail(
888 - new GitCommandError({
889 - operation: input.operation,
890 - command: `git ${input.args.join(" ")}`,
891 - cwd: input.cwd,
892 - detail: "Unexpected git command in shared refresh cache test.",
893 - }),
894 - );
895 - });
896 -
897 - yield* core.statusDetails("/repo/worktrees/main");
898 - yield* core.statusDetails("/repo/worktrees/pr-123");
899 - expect(fetchCount).toBe(1);
900 - }),
901 - );
902 -
903 - it.effect(
904 - "briefly backs off failed upstream refreshes across sibling worktrees on one remote",
905 - () =>
906 - Effect.gen(function* () {
907 - const ok = (stdout = "") =>
908 - Effect.succeed({
909 - code: 0,
910 - stdout,
911 - stderr: "",
912 - stdoutTruncated: false,
913 - stderrTruncated: false,
914 - });
915 -
916 - let fetchCount = 0;
917 - const core = yield* makeIsolatedGitCore((input) => {
918 - if (
919 - input.args[0] === "rev-parse" &&
920 - input.args[1] === "--abbrev-ref" &&
921 - input.args[2] === "--symbolic-full-name" &&
922 - input.args[3] === "@{upstream}"
923 - ) {
924 - return ok(
925 - input.cwd === "/repo/worktrees/pr-123"
926 - ? "origin/feature/pr-123\n"
927 - : "origin/main\n",
928 - );
929 - }
930 - if (input.args[0] === "remote") {
931 - return ok("origin\n");
932 - }
933 - if (input.args[0] === "rev-parse" && input.args[1] === "--git-common-dir") {
934 - return ok("/repo/.git\n");
935 - }
936 - if (input.args[0] === "--git-dir" && input.args[2] === "fetch") {
937 - fetchCount += 1;
938 - return Effect.fail(
939 - new GitCommandError({
940 - operation: input.operation,
941 - command: `git ${input.args.join(" ")}`,
942 - cwd: input.cwd,
943 - detail: "simulated fetch timeout",
944 - }),
945 - );
946 - }
947 - if (input.operation === "GitCore.statusDetails.status") {
948 - return ok(
949 - input.cwd === "/repo/worktrees/pr-123"
950 - ? "# branch.head feature/pr-123\n# branch.upstream origin/feature/pr-123\n# branch.ab +0 -0\n"
951 - : "# branch.head main\n# branch.upstream origin/main\n# branch.ab +0 -0\n",
952 - );
953 - }
954 - if (
955 - input.operation === "GitCore.statusDetails.unstagedNumstat" ||
956 - input.operation === "GitCore.statusDetails.stagedNumstat"
957 - ) {
958 - return ok();
959 - }
960 - if (input.operation === "GitCore.statusDetails.defaultRef") {
961 - return ok("refs/remotes/origin/main\n");
962 - }
963 - return Effect.fail(
964 - new GitCommandError({
965 - operation: input.operation,
966 - command: `git ${input.args.join(" ")}`,
967 - cwd: input.cwd,
968 - detail: "Unexpected git command in refresh failure cooldown test.",
969 - }),
970 - );
971 - });
972 -
973 - yield* core.statusDetails("/repo/worktrees/main");
974 - yield* core.statusDetails("/repo/worktrees/pr-123");
975 - expect(fetchCount).toBe(1);
976 - }),
977 - );
978 -
979 - it.effect("throws when branch does not exist", () =>
980 - Effect.gen(function* () {
981 - const tmp = yield* makeTmpDir();
982 - yield* initRepoWithCommit(tmp);
983 - const result = yield* Effect.result(
984 - (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "nonexistent" }),
985 - );
986 - expect(result._tag).toBe("Failure");
987 - }),
988 - );
989 -
990 - it.effect("does not silently checkout a local branch when a remote ref no longer exists", () =>
991 - Effect.gen(function* () {
992 - const remote = yield* makeTmpDir();
993 - const source = yield* makeTmpDir();
994 - yield* git(remote, ["init", "--bare"]);
995 -
996 - yield* initRepoWithCommit(source);
997 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find(
998 - (branch) => branch.current,
999 - )!.name;
1000 - yield* git(source, ["remote", "add", "origin", remote]);
1001 - yield* git(source, ["push", "-u", "origin", defaultBranch]);
1002 -
1003 - yield* (yield* GitCore).createBranch({ cwd: source, branch: "feature" });
1004 -
1005 - const checkoutResult = yield* Effect.result(
1006 - (yield* GitCore).checkoutBranch({ cwd: source, branch: "origin/feature" }),
1007 - );
1008 - expect(checkoutResult._tag).toBe("Failure");
1009 - expect(yield* git(source, ["branch", "--show-current"])).toBe(defaultBranch);
1010 - }),
1011 - );
1012 -
1013 - it.effect("checks out a remote tracking branch when remote name contains slashes", () =>
1014 - Effect.gen(function* () {
1015 - const remote = yield* makeTmpDir();
1016 - const prefixRemote = yield* makeTmpDir();
1017 - const source = yield* makeTmpDir();
1018 - const prefixFetchNamespace = "prefix-my-org";
1019 - const prefixRemoteName = "my-org";
1020 - const remoteName = "my-org/upstream";
1021 - const featureBranch = "feature";
1022 - yield* git(remote, ["init", "--bare"]);
1023 - yield* git(prefixRemote, ["init", "--bare"]);
1024 -
1025 - yield* initRepoWithCommit(source);
1026 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find(
1027 - (branch) => branch.current,
1028 - )!.name;
1029 - yield* configureRemote(source, prefixRemoteName, prefixRemote, prefixFetchNamespace);
1030 - yield* configureRemote(source, remoteName, remote, remoteName);
1031 - yield* git(source, ["push", "-u", remoteName, defaultBranch]);
1032 -
1033 - yield* git(source, ["checkout", "-b", featureBranch]);
1034 - yield* writeTextFile(path.join(source, "feature.txt"), "feature content\n");
1035 - yield* git(source, ["add", "feature.txt"]);
1036 - yield* git(source, ["commit", "-m", "feature commit"]);
1037 - yield* git(source, ["push", "-u", remoteName, featureBranch]);
1038 - yield* git(source, ["checkout", defaultBranch]);
1039 - yield* git(source, ["branch", "-D", featureBranch]);
1040 -
1041 - const checkoutResult = yield* (yield* GitCore).checkoutBranch({
1042 - cwd: source,
1043 - branch: `${remoteName}/${featureBranch}`,
1044 - });
1045 -
1046 - expect(checkoutResult.branch).toBe("upstream/feature");
1047 - expect(yield* git(source, ["branch", "--show-current"])).toBe("upstream/feature");
1048 - const realGitCore = yield* GitCore;
1049 - let fetchArgs: readonly string[] | null = null;
1050 - const core = yield* makeIsolatedGitCore((input) => {
1051 - if (input.args[0] === "--git-dir" && input.args[2] === "fetch") {
1052 - fetchArgs = [...input.args];
1053 - return Effect.succeed({
1054 - code: 0,
1055 - stdout: "",
1056 - stderr: "",
1057 - stdoutTruncated: false,
1058 - stderrTruncated: false,
1059 - });
1060 - }
1061 - return realGitCore.execute(input);
1062 - });
1063 -
1064 - const status = yield* core.statusDetails(source);
1065 - expect(status.branch).toBe("upstream/feature");
1066 - expect(status.upstreamRef).toBe(`${remoteName}/${featureBranch}`);
1067 - expect(fetchArgs).toEqual([
1068 - "--git-dir",
1069 - path.join(source, ".git"),
1070 - "fetch",
1071 - "--quiet",
1072 - "--no-tags",
1073 - remoteName,
1074 - ]);
1075 - }),
1076 - );
1077 -
1078 - it.effect(
1079 - "falls back to detached checkout when --track would conflict with an existing local branch",
1080 - () =>
1081 - Effect.gen(function* () {
1082 - const remote = yield* makeTmpDir();
1083 - const source = yield* makeTmpDir();
1084 - yield* git(remote, ["init", "--bare"]);
1085 -
1086 - yield* initRepoWithCommit(source);
1087 - const defaultBranch = (yield* (yield* GitCore).listBranches({
1088 - cwd: source,
1089 - })).branches.find((branch) => branch.current)!.name;
1090 - yield* git(source, ["remote", "add", "origin", remote]);
1091 - yield* git(source, ["push", "-u", "origin", defaultBranch]);
1092 -
1093 - // Keep local branch but remove tracking so `--track origin/<branch>`
1094 - // would attempt to create an already-existing local branch.
1095 - yield* git(source, ["branch", "--unset-upstream"]);
1096 -
1097 - yield* (yield* GitCore).checkoutBranch({
1098 - cwd: source,
1099 - branch: `origin/${defaultBranch}`,
1100 - });
1101 -
1102 - const core = yield* GitCore;
1103 - const status = yield* core.statusDetails(source);
1104 - expect(status.branch).toBeNull();
1105 - }),
1106 - );
1107 -
1108 - it.effect("throws when checkout would overwrite uncommitted changes", () =>
1109 - Effect.gen(function* () {
1110 - const tmp = yield* makeTmpDir();
1111 - yield* initRepoWithCommit(tmp);
1112 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "other" });
1113 -
1114 - // Create a conflicting change: modify README on current branch
1115 - yield* writeTextFile(path.join(tmp, "README.md"), "modified\n");
1116 - yield* git(tmp, ["add", "README.md"]);
1117 -
1118 - // First, checkout other branch cleanly
1119 - yield* git(tmp, ["stash"]);
1120 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "other" });
1121 - yield* writeTextFile(path.join(tmp, "README.md"), "other content\n");
1122 - yield* git(tmp, ["add", "."]);
1123 - yield* git(tmp, ["commit", "-m", "other change"]);
1124 -
1125 - // Go back to default branch
1126 - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1127 - (b) => !b.current,
1128 - )!.name;
1129 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: defaultBranch });
1130 -
1131 - // Make uncommitted changes to the same file
1132 - yield* writeTextFile(path.join(tmp, "README.md"), "conflicting local\n");
1133 -
1134 - // Checkout should fail due to uncommitted changes
1135 - const result = yield* Effect.result(
1136 - (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "other" }),
1137 - );
1138 - expect(result._tag).toBe("Failure");
1139 - }),
1140 - );
1141 - });
1142 -
1143 - // ── createGitBranch ──
1144 -
1145 - describe("createGitBranch", () => {
1146 - it.effect("creates a new branch visible in listGitBranches", () =>
1147 - Effect.gen(function* () {
1148 - const tmp = yield* makeTmpDir();
1149 - yield* initRepoWithCommit(tmp);
1150 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "new-feature" });
1151 -
1152 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
1153 - expect(result.branches.some((b) => b.name === "new-feature")).toBe(true);
1154 - }),
1155 - );
1156 -
1157 - it.effect("throws when branch already exists", () =>
1158 - Effect.gen(function* () {
1159 - const tmp = yield* makeTmpDir();
1160 - yield* initRepoWithCommit(tmp);
1161 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "dupe" });
1162 - const result = yield* Effect.result(
1163 - (yield* GitCore).createBranch({ cwd: tmp, branch: "dupe" }),
1164 - );
1165 - expect(result._tag).toBe("Failure");
1166 - }),
1167 - );
1168 - });
1169 -
1170 - // ── renameGitBranch ──
1171 -
1172 - describe("renameGitBranch", () => {
1173 - it.effect("renames the current branch", () =>
1174 - Effect.gen(function* () {
1175 - const tmp = yield* makeTmpDir();
1176 - yield* initRepoWithCommit(tmp);
1177 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/old-name" });
1178 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature/old-name" });
1179 -
1180 - const renamed = yield* (yield* GitCore).renameBranch({
1181 - cwd: tmp,
1182 - oldBranch: "feature/old-name",
1183 - newBranch: "feature/new-name",
1184 - });
1185 -
1186 - expect(renamed.branch).toBe("feature/new-name");
1187 -
1188 - const branches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1189 - expect(branches.branches.some((branch) => branch.name === "feature/old-name")).toBe(false);
1190 - const current = branches.branches.find((branch) => branch.current);
1191 - expect(current?.name).toBe("feature/new-name");
1192 - }),
1193 - );
1194 -
1195 - it.effect("returns success without git invocation when old/new names match", () =>
1196 - Effect.gen(function* () {
1197 - const tmp = yield* makeTmpDir();
1198 - yield* initRepoWithCommit(tmp);
1199 - const current = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1200 - (b) => b.current,
1201 - )!;
1202 -
1203 - const renamed = yield* (yield* GitCore).renameBranch({
1204 - cwd: tmp,
1205 - oldBranch: current.name,
1206 - newBranch: current.name,
1207 - });
1208 -
1209 - expect(renamed.branch).toBe(current.name);
1210 - }),
1211 - );
1212 -
1213 - it.effect("appends numeric suffix when target branch already exists", () =>
1214 - Effect.gen(function* () {
1215 - const tmp = yield* makeTmpDir();
1216 - yield* initRepoWithCommit(tmp);
1217 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/feat/session" });
1218 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/tmp-working" });
1219 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "t3code/tmp-working" });
1220 -
1221 - const renamed = yield* (yield* GitCore).renameBranch({
1222 - cwd: tmp,
1223 - oldBranch: "t3code/tmp-working",
1224 - newBranch: "t3code/feat/session",
1225 - });
1226 -
1227 - expect(renamed.branch).toBe("t3code/feat/session-1");
1228 - const branches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1229 - expect(branches.branches.some((branch) => branch.name === "t3code/feat/session")).toBe(
1230 - true,
1231 - );
1232 - expect(branches.branches.some((branch) => branch.name === "t3code/feat/session-1")).toBe(
1233 - true,
1234 - );
1235 - const current = branches.branches.find((branch) => branch.current);
1236 - expect(current?.name).toBe("t3code/feat/session-1");
1237 - }),
1238 - );
1239 -
1240 - it.effect("increments suffix until it finds an available branch name", () =>
1241 - Effect.gen(function* () {
1242 - const tmp = yield* makeTmpDir();
1243 - yield* initRepoWithCommit(tmp);
1244 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/feat/session" });
1245 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/feat/session-1" });
1246 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/tmp-working" });
1247 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "t3code/tmp-working" });
1248 -
1249 - const renamed = yield* (yield* GitCore).renameBranch({
1250 - cwd: tmp,
1251 - oldBranch: "t3code/tmp-working",
1252 - newBranch: "t3code/feat/session",
1253 - });
1254 -
1255 - expect(renamed.branch).toBe("t3code/feat/session-2");
1256 - }),
1257 - );
1258 -
1259 - it.effect("uses '--' separator for branch rename arguments", () =>
1260 - Effect.gen(function* () {
1261 - const tmp = yield* makeTmpDir();
1262 - yield* initRepoWithCommit(tmp);
1263 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/old-name" });
1264 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature/old-name" });
1265 -
1266 - const realGitCore = yield* GitCore;
1267 - let renameArgs: ReadonlyArray<string> | null = null;
1268 - const core = yield* makeIsolatedGitCore((input) => {
1269 - if (input.args[0] === "branch" && input.args[1] === "-m") {
1270 - renameArgs = [...input.args];
1271 - }
1272 - return realGitCore.execute(input);
1273 - });
1274 -
1275 - const renamed = yield* core.renameBranch({
1276 - cwd: tmp,
1277 - oldBranch: "feature/old-name",
1278 - newBranch: "feature/new-name",
1279 - });
1280 -
1281 - expect(renamed.branch).toBe("feature/new-name");
1282 - expect(renameArgs).toEqual(["branch", "-m", "--", "feature/old-name", "feature/new-name"]);
1283 - }),
1284 - );
1285 - });
1286 -
1287 - // ── createGitWorktree + removeGitWorktree ──
1288 -
1289 - describe("createGitWorktree", () => {
1290 - it.effect("creates a worktree with a new branch from the base branch", () =>
1291 - Effect.gen(function* () {
1292 - const tmp = yield* makeTmpDir();
1293 - yield* initRepoWithCommit(tmp);
1294 -
1295 - const wtPath = path.join(tmp, "worktree-out");
1296 - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1297 - (b) => b.current,
1298 - )!.name;
1299 -
1300 - const result = yield* (yield* GitCore).createWorktree({
1301 - cwd: tmp,
1302 - branch: currentBranch,
1303 - newBranch: "wt-branch",
1304 - path: wtPath,
1305 - });
1306 -
1307 - expect(result.worktree.path).toBe(wtPath);
1308 - expect(result.worktree.branch).toBe("wt-branch");
1309 - expect(existsSync(wtPath)).toBe(true);
1310 - expect(existsSync(path.join(wtPath, "README.md"))).toBe(true);
1311 -
1312 - // Clean up worktree before tmp dir disposal
1313 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath });
1314 - }),
1315 - );
1316 -
1317 - it.effect("worktree has the new branch checked out", () =>
1318 - Effect.gen(function* () {
1319 - const tmp = yield* makeTmpDir();
1320 - yield* initRepoWithCommit(tmp);
1321 -
1322 - const wtPath = path.join(tmp, "wt-check-dir");
1323 - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1324 - (b) => b.current,
1325 - )!.name;
1326 -
1327 - yield* (yield* GitCore).createWorktree({
1328 - cwd: tmp,
1329 - branch: currentBranch,
1330 - newBranch: "wt-check",
1331 - path: wtPath,
1332 - });
1333 -
1334 - // Verify the worktree is on the new branch
1335 - const branchOutput = yield* git(wtPath, ["branch", "--show-current"]);
1336 - expect(branchOutput).toBe("wt-check");
1337 -
1338 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath });
1339 - }),
1340 - );
1341 -
1342 - it.effect("creates a worktree for an existing branch when newBranch is omitted", () =>
1343 - Effect.gen(function* () {
1344 - const tmp = yield* makeTmpDir();
1345 - yield* initRepoWithCommit(tmp);
1346 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/existing-worktree" });
1347 -
1348 - const wtPath = path.join(tmp, "wt-existing");
1349 - const result = yield* (yield* GitCore).createWorktree({
1350 - cwd: tmp,
1351 - branch: "feature/existing-worktree",
1352 - path: wtPath,
1353 - });
1354 -
1355 - expect(result.worktree.path).toBe(wtPath);
1356 - expect(result.worktree.branch).toBe("feature/existing-worktree");
1357 - const branchOutput = yield* git(wtPath, ["branch", "--show-current"]);
1358 - expect(branchOutput).toBe("feature/existing-worktree");
1359 -
1360 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath });
1361 - }),
1362 - );
1363 -
1364 - it.effect("throws when new branch name already exists", () =>
1365 - Effect.gen(function* () {
1366 - const tmp = yield* makeTmpDir();
1367 - yield* initRepoWithCommit(tmp);
1368 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "existing" });
1369 -
1370 - const wtPath = path.join(tmp, "wt-conflict");
1371 - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1372 - (b) => b.current,
1373 - )!.name;
1374 -
1375 - const result = yield* Effect.result(
1376 - (yield* GitCore).createWorktree({
1377 - cwd: tmp,
1378 - branch: currentBranch,
1379 - newBranch: "existing",
1380 - path: wtPath,
1381 - }),
1382 - );
1383 - expect(result._tag).toBe("Failure");
1384 - }),
1385 - );
1386 -
1387 - it.effect("listGitBranches from worktree cwd reports worktree branch as current", () =>
1388 - Effect.gen(function* () {
1389 - const tmp = yield* makeTmpDir();
1390 - yield* initRepoWithCommit(tmp);
1391 -
1392 - const wtPath = path.join(tmp, "wt-list-dir");
1393 - const mainBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1394 - (b) => b.current,
1395 - )!.name;
1396 -
1397 - yield* (yield* GitCore).createWorktree({
1398 - cwd: tmp,
1399 - branch: mainBranch,
1400 - newBranch: "wt-list",
1401 - path: wtPath,
1402 - });
1403 -
1404 - // listGitBranches from the worktree should show wt-list as current
1405 - const wtBranches = yield* (yield* GitCore).listBranches({ cwd: wtPath });
1406 - expect(wtBranches.isRepo).toBe(true);
1407 - const wtCurrent = wtBranches.branches.find((b) => b.current);
1408 - expect(wtCurrent!.name).toBe("wt-list");
1409 -
1410 - // Main repo should still show the original branch as current
1411 - const mainBranches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1412 - const mainCurrent = mainBranches.branches.find((b) => b.current);
1413 - expect(mainCurrent!.name).toBe(mainBranch);
1414 -
1415 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath });
1416 - }),
1417 - );
1418 -
1419 - it.effect("removeGitWorktree cleans up the worktree", () =>
1420 - Effect.gen(function* () {
1421 - const tmp = yield* makeTmpDir();
1422 - yield* initRepoWithCommit(tmp);
1423 -
1424 - const wtPath = path.join(tmp, "wt-remove-dir");
1425 - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1426 - (b) => b.current,
1427 - )!.name;
1428 -
1429 - yield* (yield* GitCore).createWorktree({
1430 - cwd: tmp,
1431 - branch: currentBranch,
1432 - newBranch: "wt-remove",
1433 - path: wtPath,
1434 - });
1435 - expect(existsSync(wtPath)).toBe(true);
1436 -
1437 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath });
1438 - expect(existsSync(wtPath)).toBe(false);
1439 - }),
1440 - );
1441 -
1442 - it.effect("removeGitWorktree force removes a dirty worktree", () =>
1443 - Effect.gen(function* () {
1444 - const tmp = yield* makeTmpDir();
1445 - yield* initRepoWithCommit(tmp);
1446 -
1447 - const wtPath = path.join(tmp, "wt-dirty-dir");
1448 - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1449 - (b) => b.current,
1450 - )!.name;
1451 -
1452 - yield* (yield* GitCore).createWorktree({
1453 - cwd: tmp,
1454 - branch: currentBranch,
1455 - newBranch: "wt-dirty",
1456 - path: wtPath,
1457 - });
1458 - expect(existsSync(wtPath)).toBe(true);
1459 -
1460 - yield* writeTextFile(path.join(wtPath, "README.md"), "dirty change\n");
1461 -
1462 - const failedRemove = yield* Effect.result(
1463 - (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }),
1464 - );
1465 - expect(failedRemove._tag).toBe("Failure");
1466 - expect(existsSync(wtPath)).toBe(true);
1467 -
1468 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath, force: true });
1469 - expect(existsSync(wtPath)).toBe(false);
1470 - }),
1471 - );
1472 - });
1473 -
1474 - // ── Full flow: local branch checkout ──
1475 -
1476 - describe("full flow: local branch checkout", () => {
1477 - it.effect("init → commit → create branch → checkout → verify current", () =>
1478 - Effect.gen(function* () {
1479 - const tmp = yield* makeTmpDir();
1480 - yield* initRepoWithCommit(tmp);
1481 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-login" });
1482 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature-login" });
1483 -
1484 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
1485 - const current = result.branches.find((b) => b.current);
1486 - expect(current!.name).toBe("feature-login");
1487 - }),
1488 - );
1489 - });
1490 -
1491 - // ── Full flow: worktree creation from base branch ──
1492 -
1493 - describe("full flow: worktree creation", () => {
1494 - it.effect("creates worktree with new branch from current branch", () =>
1495 - Effect.gen(function* () {
1496 - const tmp = yield* makeTmpDir();
1497 - yield* initRepoWithCommit(tmp);
1498 -
1499 - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1500 - (b) => b.current,
1501 - )!.name;
1502 -
1503 - const wtPath = path.join(tmp, "my-worktree");
1504 - const result = yield* (yield* GitCore).createWorktree({
1505 - cwd: tmp,
1506 - branch: currentBranch,
1507 - newBranch: "feature-wt",
1508 - path: wtPath,
1509 - });
1510 -
1511 - // Worktree exists
1512 - expect(existsSync(result.worktree.path)).toBe(true);
1513 -
1514 - // Main repo still on original branch
1515 - const mainBranches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1516 - const mainCurrent = mainBranches.branches.find((b) => b.current);
1517 - expect(mainCurrent!.name).toBe(currentBranch);
1518 -
1519 - // Worktree is on the new branch
1520 - const wtBranch = yield* git(wtPath, ["branch", "--show-current"]);
1521 - expect(wtBranch).toBe("feature-wt");
1522 -
1523 - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath });
1524 - }),
1525 - );
1526 - });
1527 -
1528 - describe("fetchPullRequestBranch", () => {
1529 - it.effect("fetches a GitHub pull request ref into a local branch without checkout", () =>
1530 - Effect.gen(function* () {
1531 - const tmp = yield* makeTmpDir();
1532 - const { initialBranch } = yield* initRepoWithCommit(tmp);
1533 - const remoteDir = yield* makeTmpDir("git-remote-");
1534 - yield* git(remoteDir, ["init", "--bare"]);
1535 - yield* git(tmp, ["remote", "add", "origin", remoteDir]);
1536 - yield* git(tmp, ["push", "-u", "origin", initialBranch]);
1537 - yield* git(tmp, ["checkout", "-b", "feature/pr-fetch"]);
1538 - yield* writeTextFile(path.join(tmp, "pr-fetch.txt"), "fetch me\n");
1539 - yield* git(tmp, ["add", "pr-fetch.txt"]);
1540 - yield* git(tmp, ["commit", "-m", "Add PR fetch branch"]);
1541 - yield* git(tmp, ["push", "-u", "origin", "feature/pr-fetch"]);
1542 - yield* git(tmp, ["push", "origin", "HEAD:refs/pull/55/head"]);
1543 - yield* git(tmp, ["checkout", initialBranch]);
1544 -
1545 - yield* (yield* GitCore).fetchPullRequestBranch({
1546 - cwd: tmp,
1547 - prNumber: 55,
1548 - branch: "feature/pr-fetch",
1549 - });
1550 -
1551 - const localBranches = yield* git(tmp, ["branch", "--list", "feature/pr-fetch"]);
1552 - expect(localBranches).toContain("feature/pr-fetch");
1553 - const currentBranch = yield* git(tmp, ["branch", "--show-current"]);
1554 - expect(currentBranch).toBe(initialBranch);
1555 - }),
1556 - );
1557 - });
1558 -
1559 - // ── Full flow: thread switching simulation ──
1560 -
1561 - describe("full flow: thread switching (checkout toggling)", () => {
1562 - it.effect("checkout a → checkout b → checkout a → current matches", () =>
1563 - Effect.gen(function* () {
1564 - const tmp = yield* makeTmpDir();
1565 - yield* initRepoWithCommit(tmp);
1566 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "branch-a" });
1567 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "branch-b" });
1568 -
1569 - // Simulate switching to thread A's branch
1570 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "branch-a" });
1571 - let branches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1572 - expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a");
1573 -
1574 - // Simulate switching to thread B's branch
1575 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "branch-b" });
1576 - branches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1577 - expect(branches.branches.find((b) => b.current)!.name).toBe("branch-b");
1578 -
1579 - // Switch back to thread A
1580 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "branch-a" });
1581 - branches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1582 - expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a");
1583 - }),
1584 - );
1585 - });
1586 -
1587 - // ── Full flow: checkout conflict ──
1588 -
1589 - describe("full flow: checkout conflict", () => {
1590 - it.effect("uncommitted changes prevent checkout to a diverged branch", () =>
1591 - Effect.gen(function* () {
1592 - const tmp = yield* makeTmpDir();
1593 - yield* initRepoWithCommit(tmp);
1594 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "diverged" });
1595 -
1596 - // Make diverged branch have different file content
1597 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "diverged" });
1598 - yield* writeTextFile(path.join(tmp, "README.md"), "diverged content\n");
1599 - yield* git(tmp, ["add", "."]);
1600 - yield* git(tmp, ["commit", "-m", "diverge"]);
1601 -
1602 - // Actually, let's just get back to the initial branch explicitly
1603 - const allBranches = yield* (yield* GitCore).listBranches({ cwd: tmp });
1604 - const initialBranch = allBranches.branches.find((b) => b.name !== "diverged")!.name;
1605 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: initialBranch });
1606 -
1607 - // Make local uncommitted changes to the same file
1608 - yield* writeTextFile(path.join(tmp, "README.md"), "local uncommitted\n");
1609 -
1610 - // Attempt checkout should fail
1611 - const failedCheckout = yield* Effect.result(
1612 - (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "diverged" }),
1613 - );
1614 - expect(failedCheckout._tag).toBe("Failure");
1615 -
1616 - // Current branch should still be the initial one
1617 - const result = yield* (yield* GitCore).listBranches({ cwd: tmp });
1618 - expect(result.branches.find((b) => b.current)!.name).toBe(initialBranch);
1619 - }),
1620 - );
1621 - });
1622 -
1623 - describe("GitCore", () => {
1624 - it.effect("supports branch lifecycle operations through the service API", () =>
1625 - Effect.gen(function* () {
1626 - const tmp = yield* makeTmpDir();
1627 - const core = yield* GitCore;
1628 -
1629 - yield* core.initRepo({ cwd: tmp });
1630 - yield* git(tmp, ["config", "user.email", "test@test.com"]);
1631 - yield* git(tmp, ["config", "user.name", "Test"]);
1632 - yield* writeTextFile(path.join(tmp, "README.md"), "# test\n");
1633 - yield* git(tmp, ["add", "."]);
1634 - yield* git(tmp, ["commit", "-m", "initial commit"]);
1635 -
1636 - yield* core.createBranch({ cwd: tmp, branch: "feature/service-api" });
1637 - yield* core.checkoutBranch({ cwd: tmp, branch: "feature/service-api" });
1638 - const branches = yield* core.listBranches({ cwd: tmp });
1639 -
1640 - expect(branches.isRepo).toBe(true);
1641 - expect(
1642 - branches.branches.find((branch: { current: boolean; name: string }) => branch.current)
1643 - ?.name,
1644 - ).toBe("feature/service-api");
1645 - }),
1646 - );
1647 -
1648 - it.effect(
1649 - "reuses an existing remote when the target URL only differs by a trailing slash after .git",
1650 - () =>
1651 - Effect.gen(function* () {
1652 - const tmp = yield* makeTmpDir();
1653 - yield* initRepoWithCommit(tmp);
1654 - const core = yield* GitCore;
1655 -
1656 - yield* git(tmp, ["remote", "add", "origin", "git@github.com:pingdotgg/t3code.git"]);
1657 -
1658 - const remoteName = yield* core.ensureRemote({
1659 - cwd: tmp,
1660 - preferredName: "origin",
1661 - url: "git@github.com:pingdotgg/t3code.git/",
1662 - });
1663 -
1664 - expect(remoteName).toBe("origin");
1665 - expect((yield* git(tmp, ["remote"])).split("\n").filter(Boolean)).toEqual(["origin"]);
1666 - }),
1667 - );
1668 -
1669 - it.effect("reports status details and dirty state", () =>
1670 - Effect.gen(function* () {
1671 - const tmp = yield* makeTmpDir();
1672 - yield* initRepoWithCommit(tmp);
1673 - const core = yield* GitCore;
1674 -
1675 - const clean = yield* core.status({ cwd: tmp });
1676 - expect(clean.hasWorkingTreeChanges).toBe(false);
1677 - expect(clean.branch).toBeTruthy();
1678 -
1679 - yield* writeTextFile(path.join(tmp, "README.md"), "updated\n");
1680 - const dirty = yield* core.statusDetails(tmp);
1681 - expect(dirty.hasWorkingTreeChanges).toBe(true);
1682 - }),
1683 - );
1684 -
1685 - it.effect("returns a non-repo status for deleted directories", () =>
1686 - Effect.gen(function* () {
1687 - const tmp = yield* makeTmpDir();
1688 - const deletedDir = path.join(tmp, "deleted-repo");
1689 - yield* makeDirectory(deletedDir);
1690 - yield* removePath(deletedDir);
1691 - const core = yield* GitCore;
1692 -
1693 - const status = yield* core.statusDetails(deletedDir);
1694 - const localStatus = yield* core.statusDetailsLocal(deletedDir);
1695 -
1696 - expect(status).toEqual({
1697 - isRepo: false,
1698 - hasOriginRemote: false,
1699 - isDefaultBranch: false,
1700 - branch: null,
1701 - upstreamRef: null,
1702 - hasWorkingTreeChanges: false,
1703 - workingTree: {
1704 - files: [],
1705 - insertions: 0,
1706 - deletions: 0,
1707 - },
1708 - hasUpstream: false,
1709 - aheadCount: 0,
1710 - behindCount: 0,
1711 - });
1712 - expect(localStatus).toEqual(status);
1713 - }),
1714 - );
1715 -
1716 - it.effect("computes ahead count against base branch when no upstream is configured", () =>
1717 - Effect.gen(function* () {
1718 - const tmp = yield* makeTmpDir();
1719 - yield* initRepoWithCommit(tmp);
1720 - const core = yield* GitCore;
1721 -
1722 - yield* core.createBranch({ cwd: tmp, branch: "feature/no-upstream-ahead" });
1723 - yield* core.checkoutBranch({ cwd: tmp, branch: "feature/no-upstream-ahead" });
1724 - yield* writeTextFile(path.join(tmp, "feature.txt"), "ahead of base\n");
1725 - yield* git(tmp, ["add", "feature.txt"]);
1726 - yield* git(tmp, ["commit", "-m", "feature commit"]);
1727 -
1728 - const details = yield* core.statusDetails(tmp);
1729 - expect(details.branch).toBe("feature/no-upstream-ahead");
1730 - expect(details.hasUpstream).toBe(false);
1731 - expect(details.aheadCount).toBe(1);
1732 - expect(details.behindCount).toBe(0);
1733 - }),
1734 - );
1735 -
1736 - it.effect(
1737 - "computes ahead count against origin/default when local default branch is missing",
1738 - () =>
1739 - Effect.gen(function* () {
1740 - const remote = yield* makeTmpDir();
1741 - const source = yield* makeTmpDir();
1742 - yield* git(remote, ["init", "--bare"]);
1743 -
1744 - yield* initRepoWithCommit(source);
1745 - const initialBranch = (yield* (yield* GitCore).listBranches({
1746 - cwd: source,
1747 - })).branches.find((branch) => branch.current)!.name;
1748 - yield* git(source, ["remote", "add", "origin", remote]);
1749 - yield* git(source, ["push", "-u", "origin", initialBranch]);
1750 - yield* git(source, ["checkout", "-b", "feature/remote-base-only"]);
1751 - yield* writeTextFile(
1752 - path.join(source, "feature.txt"),
1753 - `ahead of origin/${initialBranch}\n`,
1754 - );
1755 - yield* git(source, ["add", "feature.txt"]);
1756 - yield* git(source, ["commit", "-m", "feature commit"]);
1757 - yield* git(source, ["branch", "-D", initialBranch]);
1758 -
1759 - const core = yield* GitCore;
1760 - const details = yield* core.statusDetails(source);
1761 - expect(details.branch).toBe("feature/remote-base-only");
1762 - expect(details.hasUpstream).toBe(false);
1763 - expect(details.aheadCount).toBe(1);
1764 - expect(details.behindCount).toBe(0);
1765 - }),
1766 - );
1767 -
1768 - it.effect(
1769 - "computes ahead count against a non-origin remote-prefixed gh-merge-base candidate",
1770 - () =>
1771 - Effect.gen(function* () {
1772 - const remote = yield* makeTmpDir();
1773 - const source = yield* makeTmpDir();
1774 - const remoteName = "fork-seed";
1775 - yield* git(remote, ["init", "--bare"]);
1776 -
1777 - yield* initRepoWithCommit(source);
1778 - const initialBranch = (yield* (yield* GitCore).listBranches({
1779 - cwd: source,
1780 - })).branches.find((branch) => branch.current)!.name;
1781 - yield* git(source, ["remote", "add", remoteName, remote]);
1782 - yield* git(source, ["push", "-u", remoteName, initialBranch]);
1783 - yield* git(source, ["checkout", "-b", "feature/non-origin-merge-base"]);
1784 - yield* git(source, [
1785 - "config",
1786 - "branch.feature/non-origin-merge-base.gh-merge-base",
1787 - `${remoteName}/${initialBranch}`,
1788 - ]);
1789 - yield* writeTextFile(
1790 - path.join(source, "feature.txt"),
1791 - `ahead of ${remoteName}/${initialBranch}\n`,
1792 - );
1793 - yield* git(source, ["add", "feature.txt"]);
1794 - yield* git(source, ["commit", "-m", "feature commit"]);
1795 - yield* git(source, ["branch", "-D", initialBranch]);
1796 -
1797 - const core = yield* GitCore;
1798 - const details = yield* core.statusDetails(source);
1799 - expect(details.branch).toBe("feature/non-origin-merge-base");
1800 - expect(details.hasUpstream).toBe(false);
1801 - expect(details.aheadCount).toBe(1);
1802 - expect(details.behindCount).toBe(0);
1803 - }),
1804 - );
1805 -
1806 - it.effect("skips push when no upstream is configured and branch is not ahead of base", () =>
1807 - Effect.gen(function* () {
1808 - const tmp = yield* makeTmpDir();
1809 - yield* initRepoWithCommit(tmp);
1810 - const core = yield* GitCore;
1811 -
1812 - yield* core.createBranch({ cwd: tmp, branch: "feature/no-upstream-no-ahead" });
1813 - yield* core.checkoutBranch({ cwd: tmp, branch: "feature/no-upstream-no-ahead" });
1814 -
1815 - const pushed = yield* core.pushCurrentBranch(tmp, null);
1816 - expect(pushed.status).toBe("skipped_up_to_date");
1817 - expect(pushed.branch).toBe("feature/no-upstream-no-ahead");
1818 - expect(pushed.setUpstream).toBeUndefined();
1819 - }),
1820 - );
1821 -
1822 - it.effect("pushes with upstream setup when no comparable base branch exists", () =>
1823 - Effect.gen(function* () {
1824 - const tmp = yield* makeTmpDir();
1825 - const remote = yield* makeTmpDir();
1826 - yield* git(tmp, ["init", "--initial-branch=trunk"]);
1827 - yield* git(tmp, ["config", "user.email", "test@test.com"]);
1828 - yield* git(tmp, ["config", "user.name", "Test"]);
1829 - yield* writeTextFile(path.join(tmp, "README.md"), "hello\n");
1830 - yield* git(tmp, ["add", "README.md"]);
1831 - yield* git(tmp, ["commit", "-m", "initial"]);
1832 - yield* git(remote, ["init", "--bare"]);
1833 - yield* git(tmp, ["remote", "add", "origin", remote]);
1834 - yield* git(tmp, ["checkout", "-b", "feature/no-base"]);
1835 -
1836 - const core = yield* GitCore;
1837 - const pushed = yield* core.pushCurrentBranch(tmp, null);
1838 - expect(pushed.status).toBe("pushed");
1839 - expect(pushed.setUpstream).toBe(true);
1840 - expect(pushed.upstreamBranch).toBe("origin/feature/no-base");
1841 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
1842 - "origin/feature/no-base",
1843 - );
1844 - }),
1845 - );
1846 -
1847 - it.effect("pushes with upstream setup to the only configured non-origin remote", () =>
1848 - Effect.gen(function* () {
1849 - const tmp = yield* makeTmpDir();
1850 - const remote = yield* makeTmpDir();
1851 - yield* git(tmp, ["init", "--initial-branch=main"]);
1852 - yield* git(tmp, ["config", "user.email", "test@test.com"]);
1853 - yield* git(tmp, ["config", "user.name", "Test"]);
1854 - yield* writeTextFile(path.join(tmp, "README.md"), "hello\n");
1855 - yield* git(tmp, ["add", "README.md"]);
1856 - yield* git(tmp, ["commit", "-m", "initial"]);
1857 - yield* git(remote, ["init", "--bare"]);
1858 - yield* git(tmp, ["remote", "add", "fork", remote]);
1859 - yield* git(tmp, ["checkout", "-b", "feature/fork-only"]);
1860 -
1861 - const core = yield* GitCore;
1862 - const pushed = yield* core.pushCurrentBranch(tmp, null);
1863 - expect(pushed.status).toBe("pushed");
1864 - expect(pushed.setUpstream).toBe(true);
1865 - expect(pushed.upstreamBranch).toBe("fork/feature/fork-only");
1866 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
1867 - "fork/feature/fork-only",
1868 - );
1869 - }),
1870 - );
1871 -
1872 - it.effect(
1873 - "pushes with upstream setup when comparable base exists but remote branch is missing",
1874 - () =>
1875 - Effect.gen(function* () {
1876 - const tmp = yield* makeTmpDir();
1877 - const remote = yield* makeTmpDir();
1878 - yield* git(remote, ["init", "--bare"]);
1879 -
1880 - yield* initRepoWithCommit(tmp);
1881 - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1882 - (branch) => branch.current,
1883 - )!.name;
1884 - yield* git(tmp, ["remote", "add", "origin", remote]);
1885 - yield* git(tmp, ["push", "-u", "origin", initialBranch]);
1886 -
1887 - yield* writeTextFile(path.join(tmp, "default-ahead.txt"), "ahead on default\n");
1888 - yield* git(tmp, ["add", "default-ahead.txt"]);
1889 - yield* git(tmp, ["commit", "-m", "default ahead"]);
1890 -
1891 - const featureBranch = "feature/publish-no-upstream";
1892 - yield* git(tmp, ["checkout", "-b", featureBranch]);
1893 -
1894 - const core = yield* GitCore;
1895 - const pushed = yield* core.pushCurrentBranch(tmp, null);
1896 - expect(pushed.status).toBe("pushed");
1897 - expect(pushed.setUpstream).toBe(true);
1898 - expect(pushed.upstreamBranch).toBe(`origin/${featureBranch}`);
1899 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
1900 - `origin/${featureBranch}`,
1901 - );
1902 - expect(yield* git(tmp, ["ls-remote", "--heads", "origin", featureBranch])).toContain(
1903 - featureBranch,
1904 - );
1905 - }),
1906 - );
1907 -
1908 - it.effect("prefers branch pushRemote over origin when setting upstream", () =>
1909 - Effect.gen(function* () {
1910 - const tmp = yield* makeTmpDir();
1911 - const origin = yield* makeTmpDir();
1912 - const fork = yield* makeTmpDir();
1913 - yield* git(origin, ["init", "--bare"]);
1914 - yield* git(fork, ["init", "--bare"]);
1915 -
1916 - yield* initRepoWithCommit(tmp);
1917 - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find(
1918 - (branch) => branch.current,
1919 - )!.name;
1920 - yield* git(tmp, ["remote", "add", "origin", origin]);
1921 - yield* git(tmp, ["remote", "add", "fork", fork]);
1922 - yield* git(tmp, ["push", "-u", "origin", initialBranch]);
1923 -
1924 - const featureBranch = "feature/push-remote";
1925 - yield* git(tmp, ["checkout", "-b", featureBranch]);
1926 - yield* git(tmp, ["config", `branch.${featureBranch}.pushRemote`, "fork"]);
1927 - yield* writeTextFile(path.join(tmp, "feature.txt"), "push to fork\n");
1928 - yield* git(tmp, ["add", "feature.txt"]);
1929 - yield* git(tmp, ["commit", "-m", "feature commit"]);
1930 -
1931 - const core = yield* GitCore;
1932 - const pushed = yield* core.pushCurrentBranch(tmp, null);
1933 - expect(pushed.status).toBe("pushed");
1934 - expect(pushed.setUpstream).toBe(true);
1935 - expect(pushed.upstreamBranch).toBe(`fork/${featureBranch}`);
1936 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
1937 - `fork/${featureBranch}`,
1938 - );
1939 - expect(yield* git(tmp, ["ls-remote", "--heads", "fork", featureBranch])).toContain(
1940 - featureBranch,
1941 - );
1942 - }),
1943 - );
1944 -
1945 - it.effect(
1946 - "pushes renamed PR worktree branches to their tracked upstream branch even when push.default is current",
1947 - () =>
1948 - Effect.gen(function* () {
1949 - const tmp = yield* makeTmpDir();
1950 - const fork = yield* makeTmpDir();
1951 - yield* git(fork, ["init", "--bare"]);
1952 -
1953 - const { initialBranch } = yield* initRepoWithCommit(tmp);
1954 - yield* git(tmp, ["remote", "add", "jasonLaster", fork]);
1955 - yield* git(tmp, ["checkout", "-b", "statemachine"]);
1956 - yield* writeTextFile(path.join(tmp, "fork.txt"), "fork branch\n");
1957 - yield* git(tmp, ["add", "fork.txt"]);
1958 - yield* git(tmp, ["commit", "-m", "fork branch"]);
1959 - yield* git(tmp, ["push", "-u", "jasonLaster", "statemachine"]);
1960 - yield* git(tmp, ["checkout", initialBranch]);
1961 - yield* git(tmp, ["branch", "-D", "statemachine"]);
1962 - yield* git(tmp, [
1963 - "checkout",
1964 - "-b",
1965 - "t3code/pr-488/statemachine",
1966 - "--track",
1967 - "jasonLaster/statemachine",
1968 - ]);
1969 - yield* git(tmp, ["config", "push.default", "current"]);
1970 - yield* writeTextFile(path.join(tmp, "fork.txt"), "updated fork branch\n");
1971 - yield* git(tmp, ["add", "fork.txt"]);
1972 - yield* git(tmp, ["commit", "-m", "update reviewed PR branch"]);
1973 -
1974 - const core = yield* GitCore;
1975 - const pushed = yield* core.pushCurrentBranch(tmp, null);
1976 -
1977 - expect(pushed.status).toBe("pushed");
1978 - expect(pushed.setUpstream).toBe(false);
1979 - expect(pushed.upstreamBranch).toBe("jasonLaster/statemachine");
1980 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
1981 - "jasonLaster/statemachine",
1982 - );
1983 - expect(
1984 - yield* git(tmp, ["ls-remote", "--heads", "jasonLaster", "statemachine"]),
1985 - ).toContain("statemachine");
1986 - expect(
1987 - yield* git(tmp, ["ls-remote", "--heads", "jasonLaster", "t3code/pr-488/statemachine"]),
1988 - ).toBe("");
1989 - }),
1990 - );
1991 -
1992 - it.effect("pushes to the tracked upstream when the remote name contains slashes", () =>
1993 - Effect.gen(function* () {
1994 - const tmp = yield* makeTmpDir();
1995 - const remote = yield* makeTmpDir();
1996 - const prefixRemote = yield* makeTmpDir();
1997 - const prefixFetchNamespace = "prefix-my-org";
1998 - const prefixRemoteName = "my-org";
1999 - const remoteName = "my-org/upstream";
2000 - const featureBranch = "feature/slash-remote-push";
2001 - yield* git(remote, ["init", "--bare"]);
2002 - yield* git(prefixRemote, ["init", "--bare"]);
2003 -
2004 - const { initialBranch } = yield* initRepoWithCommit(tmp);
2005 - yield* configureRemote(tmp, prefixRemoteName, prefixRemote, prefixFetchNamespace);
2006 - yield* configureRemote(tmp, remoteName, remote, remoteName);
2007 - yield* git(tmp, ["push", "-u", remoteName, initialBranch]);
2008 -
2009 - yield* git(tmp, ["checkout", "-b", featureBranch]);
2010 - yield* writeTextFile(path.join(tmp, "feature.txt"), "first revision\n");
2011 - yield* git(tmp, ["add", "feature.txt"]);
2012 - yield* git(tmp, ["commit", "-m", "feature base"]);
2013 - yield* git(tmp, ["push", "-u", remoteName, featureBranch]);
2014 -
2015 - yield* writeTextFile(path.join(tmp, "feature.txt"), "second revision\n");
2016 - yield* git(tmp, ["add", "feature.txt"]);
2017 - yield* git(tmp, ["commit", "-m", "feature update"]);
2018 -
2019 - const core = yield* GitCore;
2020 - const pushed = yield* core.pushCurrentBranch(tmp, null);
2021 - expect(pushed.status).toBe("pushed");
2022 - expect(pushed.setUpstream).toBe(false);
2023 - expect(pushed.upstreamBranch).toBe(`${remoteName}/${featureBranch}`);
2024 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
2025 - `${remoteName}/${featureBranch}`,
2026 - );
2027 - expect(yield* git(tmp, ["ls-remote", "--heads", remoteName, featureBranch])).toContain(
2028 - featureBranch,
2029 - );
2030 - }),
2031 - );
2032 -
2033 - it.effect("includes command context when worktree removal fails", () =>
2034 - Effect.gen(function* () {
2035 - const tmp = yield* makeTmpDir();
2036 - yield* initRepoWithCommit(tmp);
2037 - const core = yield* GitCore;
2038 - const missingWorktreePath = path.join(tmp, "missing-worktree");
2039 -
2040 - const removeResult = yield* Effect.result(
2041 - core.removeWorktree({ cwd: tmp, path: missingWorktreePath }),
2042 - );
2043 - expect(removeResult._tag).toBe("Failure");
2044 - if (removeResult._tag !== "Failure") {
2045 - return;
2046 - }
2047 - const message = removeResult.failure.message;
2048 - expect(message).toContain("git worktree remove");
2049 - expect(message).toContain(`cwd: ${tmp}`);
2050 - expect(message).toContain(missingWorktreePath);
2051 - }),
2052 - );
2053 -
2054 - it.effect(
2055 - "refreshes upstream before statusDetails so behind count reflects remote updates",
2056 - () =>
2057 - Effect.gen(function* () {
2058 - const remote = yield* makeTmpDir();
2059 - const source = yield* makeTmpDir();
2060 - const clone = yield* makeTmpDir();
2061 - yield* git(remote, ["init", "--bare"]);
2062 -
2063 - yield* initRepoWithCommit(source);
2064 - const initialBranch = (yield* (yield* GitCore).listBranches({
2065 - cwd: source,
2066 - })).branches.find((branch) => branch.current)!.name;
2067 - yield* git(source, ["remote", "add", "origin", remote]);
2068 - yield* git(source, ["push", "-u", "origin", initialBranch]);
2069 -
2070 - yield* git(clone, ["clone", remote, "."]);
2071 - yield* git(clone, ["config", "user.email", "test@test.com"]);
2072 - yield* git(clone, ["config", "user.name", "Test"]);
2073 - yield* git(clone, [
2074 - "checkout",
2075 - "-B",
2076 - initialBranch,
2077 - "--track",
2078 - `origin/${initialBranch}`,
2079 - ]);
2080 - yield* writeTextFile(path.join(clone, "CHANGELOG.md"), "remote change\n");
2081 - yield* git(clone, ["add", "CHANGELOG.md"]);
2082 - yield* git(clone, ["commit", "-m", "remote update"]);
2083 - yield* git(clone, ["push", "origin", initialBranch]);
2084 -
2085 - const core = yield* GitCore;
2086 - const details = yield* core.statusDetails(source);
2087 - expect(details.branch).toBe(initialBranch);
2088 - expect(details.aheadCount).toBe(0);
2089 - expect(details.behindCount).toBe(1);
2090 - }),
2091 - );
2092 -
2093 - it.effect("prepares commit context by auto-staging and creates commit", () =>
2094 - Effect.gen(function* () {
2095 - const tmp = yield* makeTmpDir();
2096 - yield* initRepoWithCommit(tmp);
2097 - const core = yield* GitCore;
2098 -
2099 - yield* writeTextFile(path.join(tmp, "README.md"), "new content\n");
2100 - const context = yield* core.prepareCommitContext(tmp);
2101 - expect(context).not.toBeNull();
2102 - expect(context!.stagedSummary.length).toBeGreaterThan(0);
2103 - expect(context!.stagedPatch.length).toBeGreaterThan(0);
2104 -
2105 - const created = yield* core.commit(tmp, "Add README update", "- include updated content");
2106 - expect(created.commitSha.length).toBeGreaterThan(0);
2107 - expect(yield* git(tmp, ["log", "-1", "--pretty=%s"])).toBe("Add README update");
2108 - }),
2109 - );
2110 -
2111 - it.effect("prepareCommitContext stages only selected files when filePaths provided", () =>
2112 - Effect.gen(function* () {
2113 - const tmp = yield* makeTmpDir();
2114 - yield* initRepoWithCommit(tmp);
2115 - const core = yield* GitCore;
2116 -
2117 - yield* writeTextFile(path.join(tmp, "a.txt"), "file a\n");
2118 - yield* writeTextFile(path.join(tmp, "b.txt"), "file b\n");
2119 -
2120 - const context = yield* core.prepareCommitContext(tmp, ["a.txt"]);
2121 - expect(context).not.toBeNull();
2122 - expect(context!.stagedSummary).toContain("a.txt");
2123 - expect(context!.stagedSummary).not.toContain("b.txt");
2124 -
2125 - yield* core.commit(tmp, "Add only a.txt", "");
2126 -
2127 - // b.txt should still be untracked after commit
2128 - const statusAfter = yield* git(tmp, ["status", "--porcelain"]);
2129 - expect(statusAfter).toContain("b.txt");
2130 - expect(statusAfter).not.toContain("a.txt");
2131 - }),
2132 - );
2133 -
2134 - it.effect("prepareCommitContext stages everything when filePaths is undefined", () =>
2135 - Effect.gen(function* () {
2136 - const tmp = yield* makeTmpDir();
2137 - yield* initRepoWithCommit(tmp);
2138 - const core = yield* GitCore;
2139 -
2140 - yield* writeTextFile(path.join(tmp, "a.txt"), "file a\n");
2141 - yield* writeTextFile(path.join(tmp, "b.txt"), "file b\n");
2142 -
2143 - const context = yield* core.prepareCommitContext(tmp);
2144 - expect(context).not.toBeNull();
2145 - expect(context!.stagedSummary).toContain("a.txt");
2146 - expect(context!.stagedSummary).toContain("b.txt");
2147 - }),
2148 - );
2149 -
2150 - it.effect("prepareCommitContext truncates oversized staged patches instead of failing", () =>
2151 - Effect.gen(function* () {
2152 - const tmp = yield* makeTmpDir();
2153 - yield* initRepoWithCommit(tmp);
2154 - const core = yield* GitCore;
2155 -
2156 - yield* writeTextFile(path.join(tmp, "README.md"), buildLargeText());
2157 -
2158 - const context = yield* core.prepareCommitContext(tmp);
2159 - expect(context).not.toBeNull();
2160 - expect(context!.stagedSummary).toContain("README.md");
2161 - expect(context!.stagedPatch).toContain("[truncated]");
2162 - }),
2163 - );
2164 -
2165 - it.effect("readRangeContext truncates oversized diff patches instead of failing", () =>
2166 - Effect.gen(function* () {
2167 - const tmp = yield* makeTmpDir();
2168 - const { initialBranch } = yield* initRepoWithCommit(tmp);
2169 - const core = yield* GitCore;
2170 -
2171 - yield* core.createBranch({ cwd: tmp, branch: "feature/large-range-context" });
2172 - yield* core.checkoutBranch({ cwd: tmp, branch: "feature/large-range-context" });
2173 - yield* writeTextFile(path.join(tmp, "large.txt"), buildLargeText());
2174 - yield* git(tmp, ["add", "large.txt"]);
2175 - yield* git(tmp, ["commit", "-m", "Add large range context"]);
2176 -
2177 - const rangeContext = yield* core.readRangeContext(tmp, initialBranch);
2178 - expect(rangeContext.commitSummary).toContain("Add large range context");
2179 - expect(rangeContext.diffSummary).toContain("large.txt");
2180 - expect(rangeContext.diffPatch).toContain("[truncated]");
2181 - }),
2182 - );
2183 -
2184 - it.effect("pushes with upstream setup and then skips when up to date", () =>
2185 - Effect.gen(function* () {
2186 - const tmp = yield* makeTmpDir();
2187 - const remote = yield* makeTmpDir();
2188 - yield* initRepoWithCommit(tmp);
2189 - yield* git(remote, ["init", "--bare"]);
2190 - yield* git(tmp, ["remote", "add", "origin", remote]);
2191 - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/core-push" });
2192 - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature/core-push" });
2193 -
2194 - yield* writeTextFile(path.join(tmp, "feature.txt"), "push me\n");
2195 - const core = yield* GitCore;
2196 - const context = yield* core.prepareCommitContext(tmp);
2197 - expect(context).not.toBeNull();
2198 - yield* core.commit(tmp, "Add feature file", "");
2199 -
2200 - const pushed = yield* core.pushCurrentBranch(tmp, null);
2201 - expect(pushed.status).toBe("pushed");
2202 - expect(pushed.setUpstream).toBe(true);
2203 - expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
2204 - "origin/feature/core-push",
2205 - );
2206 -
2207 - const skipped = yield* core.pushCurrentBranch(tmp, null);
2208 - expect(skipped.status).toBe("skipped_up_to_date");
2209 - }),
2210 - );
2211 -
2212 - it.effect("pulls behind branch and then reports up-to-date", () =>
2213 - Effect.gen(function* () {
2214 - const remote = yield* makeTmpDir();
2215 - const source = yield* makeTmpDir();
2216 - const clone = yield* makeTmpDir();
2217 - yield* git(remote, ["init", "--bare"]);
2218 -
2219 - yield* initRepoWithCommit(source);
2220 - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find(
2221 - (branch) => branch.current,
2222 - )!.name;
2223 - yield* git(source, ["remote", "add", "origin", remote]);
2224 - yield* git(source, ["push", "-u", "origin", initialBranch]);
2225 -
2226 - yield* git(clone, ["clone", remote, "."]);
2227 - yield* git(clone, ["config", "user.email", "test@test.com"]);
2228 - yield* git(clone, ["config", "user.name", "Test"]);
2229 - yield* writeTextFile(path.join(clone, "CHANGELOG.md"), "remote change\n");
2230 - yield* git(clone, ["add", "CHANGELOG.md"]);
2231 - yield* git(clone, ["commit", "-m", "remote update"]);
2232 - yield* git(clone, ["push", "origin", initialBranch]);
2233 -
2234 - const core = yield* GitCore;
2235 - const pulled = yield* core.pullCurrentBranch(source);
2236 - expect(pulled.status).toBe("pulled");
2237 - expect((yield* core.statusDetails(source)).behindCount).toBe(0);
2238 -
2239 - const skipped = yield* core.pullCurrentBranch(source);
2240 - expect(skipped.status).toBe("skipped_up_to_date");
2241 - }),
2242 - );
2243 -
2244 - it.effect("top-level pullGitBranch rejects when no upstream exists", () =>
2245 - Effect.gen(function* () {
2246 - const tmp = yield* makeTmpDir();
2247 - yield* initRepoWithCommit(tmp);
2248 - const result = yield* Effect.result((yield* GitCore).pullCurrentBranch(tmp));
2249 - expect(result._tag).toBe("Failure");
2250 - if (result._tag === "Failure") {
2251 - expect(result.failure.message.toLowerCase()).toContain("no upstream");
2252 - }
2253 - }),
2254 - );
2255 -
2256 - it.effect("lists branches when recency lookup fails", () =>
2257 - Effect.gen(function* () {
2258 - const tmp = yield* makeTmpDir();
2259 - yield* initRepoWithCommit(tmp);
2260 - const realGitCore = yield* GitCore;
2261 - let didFailRecency = false;
2262 - const core = yield* makeIsolatedGitCore((input) => {
2263 - if (!didFailRecency && input.args[0] === "for-each-ref") {
2264 - didFailRecency = true;
2265 - return Effect.fail(
2266 - new GitCommandError({
2267 - operation: "git.test.listBranchesRecency",
2268 - command: `git ${input.args.join(" ")}`,
2269 - cwd: input.cwd,
2270 - detail: "timeout",
2271 - }),
2272 - );
2273 - }
2274 - return realGitCore.execute(input);
2275 - });
2276 -
2277 - const result = yield* core.listBranches({ cwd: tmp });
2278 -
2279 - expect(result.isRepo).toBe(true);
2280 - expect(result.branches.length).toBeGreaterThan(0);
2281 - expect(result.branches[0]?.current).toBe(true);
2282 - expect(didFailRecency).toBe(true);
2283 - }),
2284 - );
2285 -
2286 - it.effect("falls back to empty remote branch data when remote lookups fail", () =>
2287 - Effect.gen(function* () {
2288 - const tmp = yield* makeTmpDir();
2289 - const remote = yield* makeTmpDir();
2290 - yield* initRepoWithCommit(tmp);
2291 - yield* git(remote, ["init", "--bare"]);
2292 - yield* git(tmp, ["remote", "add", "origin", remote]);
2293 -
2294 - const realGitCore = yield* GitCore;
2295 - let didFailRemoteBranches = false;
2296 - let didFailRemoteNames = false;
2297 - const core = yield* makeIsolatedGitCore((input) => {
2298 - if (input.args.join(" ") === "branch --no-color --no-column --remotes") {
2299 - didFailRemoteBranches = true;
2300 - return Effect.fail(
2301 - new GitCommandError({
2302 - operation: "git.test.listBranchesRemoteBranches",
2303 - command: `git ${input.args.join(" ")}`,
2304 - cwd: input.cwd,
2305 - detail: "remote unavailable",
2306 - }),
2307 - );
2308 - }
2309 - if (input.args.join(" ") === "remote") {
2310 - didFailRemoteNames = true;
2311 - return Effect.fail(
2312 - new GitCommandError({
2313 - operation: "git.test.listBranchesRemoteNames",
2314 - command: `git ${input.args.join(" ")}`,
2315 - cwd: input.cwd,
2316 - detail: "remote unavailable",
2317 - }),
2318 - );
2319 - }
2320 - return realGitCore.execute(input);
2321 - });
2322 -
2323 - const result = yield* core.listBranches({ cwd: tmp });
2324 -
2325 - expect(result.isRepo).toBe(true);
2326 - expect(result.branches.length).toBeGreaterThan(0);
2327 - expect(result.branches.every((branch) => !branch.isRemote)).toBe(true);
2328 - expect(didFailRemoteBranches).toBe(true);
2329 - expect(didFailRemoteNames).toBe(true);
2330 - }),
2331 - );
2332 - });
2333 -});
apps/server/src/git/Layers/GitHubCli.test.ts deleted
-227
@@ -1,227 +0,0 @@
1 -import { assert, it } from "@effect/vitest";
2 -import { Effect } from "effect";
3 -import { afterEach, expect, vi } from "vitest";
4 -
5 -vi.mock("../../processRunner", () => ({
6 - runProcess: vi.fn(),
7 -}));
8 -
9 -import { runProcess } from "../../processRunner.ts";
10 -import { GitHubCli } from "../Services/GitHubCli.ts";
11 -import { GitHubCliLive } from "./GitHubCli.ts";
12 -
13 -const mockedRunProcess = vi.mocked(runProcess);
14 -const layer = it.layer(GitHubCliLive);
15 -
16 -afterEach(() => {
17 - mockedRunProcess.mockReset();
18 -});
19 -
20 -layer("GitHubCliLive", (it) => {
21 - it.effect("parses pull request view output", () =>
22 - Effect.gen(function* () {
23 - mockedRunProcess.mockResolvedValueOnce({
24 - stdout: JSON.stringify({
25 - number: 42,
26 - title: "Add PR thread creation",
27 - url: "https://github.com/pingdotgg/codething-mvp/pull/42",
28 - baseRefName: "main",
29 - headRefName: "feature/pr-threads",
30 - state: "OPEN",
31 - mergedAt: null,
32 - isCrossRepository: true,
33 - headRepository: {
34 - nameWithOwner: "octocat/codething-mvp",
35 - },
36 - headRepositoryOwner: {
37 - login: "octocat",
38 - },
39 - }),
40 - stderr: "",
41 - code: 0,
42 - signal: null,
43 - timedOut: false,
44 - });
45 -
46 - const result = yield* Effect.gen(function* () {
47 - const gh = yield* GitHubCli;
48 - return yield* gh.getPullRequest({
49 - cwd: "/repo",
50 - reference: "#42",
51 - });
52 - });
53 -
54 - assert.deepStrictEqual(result, {
55 - number: 42,
56 - title: "Add PR thread creation",
57 - url: "https://github.com/pingdotgg/codething-mvp/pull/42",
58 - baseRefName: "main",
59 - headRefName: "feature/pr-threads",
60 - state: "open",
61 - isCrossRepository: true,
62 - headRepositoryNameWithOwner: "octocat/codething-mvp",
63 - headRepositoryOwnerLogin: "octocat",
64 - });
65 - expect(mockedRunProcess).toHaveBeenCalledWith(
66 - "gh",
67 - [
68 - "pr",
69 - "view",
70 - "#42",
71 - "--json",
72 - "number,title,url,baseRefName,headRefName,state,mergedAt,isCrossRepository,headRepository,headRepositoryOwner",
73 - ],
74 - expect.objectContaining({ cwd: "/repo" }),
75 - );
76 - }),
77 - );
78 -
79 - it.effect("trims pull request fields decoded from gh json", () =>
80 - Effect.gen(function* () {
81 - mockedRunProcess.mockResolvedValueOnce({
82 - stdout: JSON.stringify({
83 - number: 42,
84 - title: " Add PR thread creation \n",
85 - url: " https://github.com/pingdotgg/codething-mvp/pull/42 ",
86 - baseRefName: " main ",
87 - headRefName: "\tfeature/pr-threads\t",
88 - state: "OPEN",
89 - mergedAt: null,
90 - isCrossRepository: true,
91 - headRepository: {
92 - nameWithOwner: " octocat/codething-mvp ",
93 - },
94 - headRepositoryOwner: {
95 - login: " octocat ",
96 - },
97 - }),
98 - stderr: "",
99 - code: 0,
100 - signal: null,
101 - timedOut: false,
102 - });
103 -
104 - const result = yield* Effect.gen(function* () {
105 - const gh = yield* GitHubCli;
106 - return yield* gh.getPullRequest({
107 - cwd: "/repo",
108 - reference: "#42",
109 - });
110 - });
111 -
112 - assert.deepStrictEqual(result, {
113 - number: 42,
114 - title: "Add PR thread creation",
115 - url: "https://github.com/pingdotgg/codething-mvp/pull/42",
116 - baseRefName: "main",
117 - headRefName: "feature/pr-threads",
118 - state: "open",
119 - isCrossRepository: true,
120 - headRepositoryNameWithOwner: "octocat/codething-mvp",
121 - headRepositoryOwnerLogin: "octocat",
122 - });
123 - }),
124 - );
125 -
126 - it.effect("skips invalid entries when parsing pr lists", () =>
127 - Effect.gen(function* () {
128 - mockedRunProcess.mockResolvedValueOnce({
129 - stdout: JSON.stringify([
130 - {
131 - number: 0,
132 - title: "invalid",
133 - url: "https://github.com/pingdotgg/codething-mvp/pull/0",
134 - baseRefName: "main",
135 - headRefName: "feature/invalid",
136 - },
137 - {
138 - number: 43,
139 - title: " Valid PR ",
140 - url: " https://github.com/pingdotgg/codething-mvp/pull/43 ",
141 - baseRefName: " main ",
142 - headRefName: " feature/pr-list ",
143 - headRepository: {
144 - nameWithOwner: " ",
145 - },
146 - headRepositoryOwner: {
147 - login: " ",
148 - },
149 - },
150 - ]),
151 - stderr: "",
152 - code: 0,
153 - signal: null,
154 - timedOut: false,
155 - });
156 -
157 - const result = yield* Effect.gen(function* () {
158 - const gh = yield* GitHubCli;
159 - return yield* gh.listOpenPullRequests({
160 - cwd: "/repo",
161 - headSelector: "feature/pr-list",
162 - });
163 - });
164 -
165 - assert.deepStrictEqual(result, [
166 - {
167 - number: 43,
168 - title: "Valid PR",
169 - url: "https://github.com/pingdotgg/codething-mvp/pull/43",
170 - baseRefName: "main",
171 - headRefName: "feature/pr-list",
172 - state: "open",
173 - },
174 - ]);
175 - }),
176 - );
177 -
178 - it.effect("reads repository clone URLs", () =>
179 - Effect.gen(function* () {
180 - mockedRunProcess.mockResolvedValueOnce({
181 - stdout: JSON.stringify({
182 - nameWithOwner: "octocat/codething-mvp",
183 - url: "https://github.com/octocat/codething-mvp",
184 - sshUrl: "git@github.com:octocat/codething-mvp.git",
185 - }),
186 - stderr: "",
187 - code: 0,
188 - signal: null,
189 - timedOut: false,
190 - });
191 -
192 - const result = yield* Effect.gen(function* () {
193 - const gh = yield* GitHubCli;
194 - return yield* gh.getRepositoryCloneUrls({
195 - cwd: "/repo",
196 - repository: "octocat/codething-mvp",
197 - });
198 - });
199 -
200 - assert.deepStrictEqual(result, {
201 - nameWithOwner: "octocat/codething-mvp",
202 - url: "https://github.com/octocat/codething-mvp",
203 - sshUrl: "git@github.com:octocat/codething-mvp.git",
204 - });
205 - }),
206 - );
207 -
208 - it.effect("surfaces a friendly error when the pull request is not found", () =>
209 - Effect.gen(function* () {
210 - mockedRunProcess.mockRejectedValueOnce(
211 - new Error(
212 - "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
213 - ),
214 - );
215 -
216 - const error = yield* Effect.gen(function* () {
217 - const gh = yield* GitHubCli;
218 - return yield* gh.getPullRequest({
219 - cwd: "/repo",
220 - reference: "4888",
221 - });
222 - }).pipe(Effect.flip);
223 -
224 - assert.equal(error.message.includes("Pull request not found"), true);
225 - }),
226 - );
227 -});
apps/server/src/git/Layers/GitStatusBroadcaster.ts deleted
-311
@@ -1,311 +0,0 @@
1 -import { realpathSync } from "node:fs";
2 -
3 -import {
4 - Duration,
5 - Effect,
6 - Exit,
7 - Fiber,
8 - Layer,
9 - PubSub,
10 - Ref,
11 - Scope,
12 - Stream,
13 - SynchronizedRef,
14 -} from "effect";
15 -import type {
16 - GitStatusInput,
17 - GitStatusLocalResult,
18 - GitStatusRemoteResult,
19 - GitStatusStreamEvent,
20 -} from "@t3tools/contracts";
21 -import { mergeGitStatusParts } from "@t3tools/shared/git";
22 -
23 -import {
24 - GitStatusBroadcaster,
25 - type GitStatusBroadcasterShape,
26 -} from "../Services/GitStatusBroadcaster.ts";
27 -import { GitManager } from "../Services/GitManager.ts";
28 -
29 -const GIT_STATUS_REFRESH_INTERVAL = Duration.seconds(30);
30 -
31 -interface GitStatusChange {
32 - readonly cwd: string;
33 - readonly event: GitStatusStreamEvent;
34 -}
35 -
36 -interface CachedValue<T> {
37 - readonly fingerprint: string;
38 - readonly value: T;
39 -}
40 -
41 -interface CachedGitStatus {
42 - readonly local: CachedValue<GitStatusLocalResult> | null;
43 - readonly remote: CachedValue<GitStatusRemoteResult | null> | null;
44 -}
45 -
46 -interface ActiveRemotePoller {
47 - readonly fiber: Fiber.Fiber<void, never>;
48 - readonly subscriberCount: number;
49 -}
50 -
51 -function normalizeCwd(cwd: string): string {
52 - try {
53 - return realpathSync.native(cwd);
54 - } catch {
55 - return cwd;
56 - }
57 -}
58 -
59 -function fingerprintStatusPart(status: unknown): string {
60 - return JSON.stringify(status);
61 -}
62 -
63 -export const GitStatusBroadcasterLive = Layer.effect(
64 - GitStatusBroadcaster,
65 - Effect.gen(function* () {
66 - const gitManager = yield* GitManager;
67 - const changesPubSub = yield* Effect.acquireRelease(
68 - PubSub.unbounded<GitStatusChange>(),
69 - (pubsub) => PubSub.shutdown(pubsub),
70 - );
71 - const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
72 - Scope.close(scope, Exit.void),
73 - );
74 - const cacheRef = yield* Ref.make(new Map<string, CachedGitStatus>());
75 - const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
76 -
77 - const getCachedStatus = Effect.fn("getCachedStatus")(function* (cwd: string) {
78 - return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null));
79 - });
80 -
81 - const updateCachedLocalStatus = Effect.fn("updateCachedLocalStatus")(function* (
82 - cwd: string,
83 - local: GitStatusLocalResult,
84 - options?: { publish?: boolean },
85 - ) {
86 - const nextLocal = {
87 - fingerprint: fingerprintStatusPart(local),
88 - value: local,
89 - } satisfies CachedValue<GitStatusLocalResult>;
90 - const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
91 - const previous = cache.get(cwd) ?? { local: null, remote: null };
92 - const nextCache = new Map(cache);
93 - nextCache.set(cwd, {
94 - ...previous,
95 - local: nextLocal,
96 - });
97 - return [previous.local?.fingerprint !== nextLocal.fingerprint, nextCache] as const;
98 - });
99 -
100 - if (options?.publish && shouldPublish) {
101 - yield* PubSub.publish(changesPubSub, {
102 - cwd,
103 - event: {
104 - _tag: "localUpdated",
105 - local,
106 - },
107 - });
108 - }
109 -
110 - return local;
111 - });
112 -
113 - const updateCachedRemoteStatus = Effect.fn("updateCachedRemoteStatus")(function* (
114 - cwd: string,
115 - remote: GitStatusRemoteResult | null,
116 - options?: { publish?: boolean },
117 - ) {
118 - const nextRemote = {
119 - fingerprint: fingerprintStatusPart(remote),
120 - value: remote,
121 - } satisfies CachedValue<GitStatusRemoteResult | null>;
122 - const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
123 - const previous = cache.get(cwd) ?? { local: null, remote: null };
124 - const nextCache = new Map(cache);
125 - nextCache.set(cwd, {
126 - ...previous,
127 - remote: nextRemote,
128 - });
129 - return [previous.remote?.fingerprint !== nextRemote.fingerprint, nextCache] as const;
130 - });
131 -
132 - if (options?.publish && shouldPublish) {
133 - yield* PubSub.publish(changesPubSub, {
134 - cwd,
135 - event: {
136 - _tag: "remoteUpdated",
137 - remote,
138 - },
139 - });
140 - }
141 -
142 - return remote;
143 - });
144 -
145 - const loadLocalStatus = Effect.fn("loadLocalStatus")(function* (cwd: string) {
146 - const local = yield* gitManager.localStatus({ cwd });
147 - return yield* updateCachedLocalStatus(cwd, local);
148 - });
149 -
150 - const loadRemoteStatus = Effect.fn("loadRemoteStatus")(function* (cwd: string) {
151 - const remote = yield* gitManager.remoteStatus({ cwd });
152 - return yield* updateCachedRemoteStatus(cwd, remote);
153 - });
154 -
155 - const getOrLoadLocalStatus = Effect.fn("getOrLoadLocalStatus")(function* (cwd: string) {
156 - const cached = yield* getCachedStatus(cwd);
157 - if (cached?.local) {
158 - return cached.local.value;
159 - }
160 - return yield* loadLocalStatus(cwd);
161 - });
162 -
163 - const getOrLoadRemoteStatus = Effect.fn("getOrLoadRemoteStatus")(function* (cwd: string) {
164 - const cached = yield* getCachedStatus(cwd);
165 - if (cached?.remote) {
166 - return cached.remote.value;
167 - }
168 - return yield* loadRemoteStatus(cwd);
169 - });
170 -
171 - const getStatus: GitStatusBroadcasterShape["getStatus"] = Effect.fn("getStatus")(function* (
172 - input: GitStatusInput,
173 - ) {
174 - const normalizedCwd = normalizeCwd(input.cwd);
175 - const [local, remote] = yield* Effect.all([
176 - getOrLoadLocalStatus(normalizedCwd),
177 - getOrLoadRemoteStatus(normalizedCwd),
178 - ]);
179 - return mergeGitStatusParts(local, remote);
180 - });
181 -
182 - const refreshLocalStatus: GitStatusBroadcasterShape["refreshLocalStatus"] = Effect.fn(
183 - "refreshLocalStatus",
184 - )(function* (cwd) {
185 - const normalizedCwd = normalizeCwd(cwd);
186 - yield* gitManager.invalidateLocalStatus(normalizedCwd);
187 - const local = yield* gitManager.localStatus({ cwd: normalizedCwd });
188 - return yield* updateCachedLocalStatus(normalizedCwd, local, { publish: true });
189 - });
190 -
191 - const refreshRemoteStatus = Effect.fn("refreshRemoteStatus")(function* (cwd: string) {
192 - yield* gitManager.invalidateRemoteStatus(cwd);
193 - const remote = yield* gitManager.remoteStatus({ cwd });
194 - return yield* updateCachedRemoteStatus(cwd, remote, { publish: true });
195 - });
196 -
197 - const refreshStatus: GitStatusBroadcasterShape["refreshStatus"] = Effect.fn("refreshStatus")(
198 - function* (cwd) {
199 - const normalizedCwd = normalizeCwd(cwd);
200 - const [local, remote] = yield* Effect.all([
201 - refreshLocalStatus(normalizedCwd),
202 - refreshRemoteStatus(normalizedCwd),
203 - ]);
204 - return mergeGitStatusParts(local, remote);
205 - },
206 - );
207 -
208 - const makeRemoteRefreshLoop = (cwd: string) => {
209 - const logRefreshFailure = (error: Error) =>
210 - Effect.logWarning("git remote status refresh failed", {
211 - cwd,
212 - detail: error.message,
213 - });
214 -
215 - return refreshRemoteStatus(cwd).pipe(
216 - Effect.catch(logRefreshFailure),
217 - Effect.andThen(
218 - Effect.forever(
219 - Effect.sleep(GIT_STATUS_REFRESH_INTERVAL).pipe(
220 - Effect.andThen(refreshRemoteStatus(cwd).pipe(Effect.catch(logRefreshFailure))),
221 - ),
222 - ),
223 - ),
224 - );
225 - };
226 -
227 - const retainRemotePoller = Effect.fn("retainRemotePoller")(function* (cwd: string) {
228 - yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => {
229 - const existing = activePollers.get(cwd);
230 - if (existing) {
231 - const nextPollers = new Map(activePollers);
232 - nextPollers.set(cwd, {
233 - ...existing,
234 - subscriberCount: existing.subscriberCount + 1,
235 - });
236 - return Effect.succeed([undefined, nextPollers] as const);
237 - }
238 -
239 - return makeRemoteRefreshLoop(cwd).pipe(
240 - Effect.forkIn(broadcasterScope),
241 - Effect.map((fiber) => {
242 - const nextPollers = new Map(activePollers);
243 - nextPollers.set(cwd, {
244 - fiber,
245 - subscriberCount: 1,
246 - });
247 - return [undefined, nextPollers] as const;
248 - }),
249 - );
250 - });
251 - });
252 -
253 - const releaseRemotePoller = Effect.fn("releaseRemotePoller")(function* (cwd: string) {
254 - const pollerToInterrupt = yield* SynchronizedRef.modify(pollersRef, (activePollers) => {
255 - const existing = activePollers.get(cwd);
256 - if (!existing) {
257 - return [null, activePollers] as const;
258 - }
259 -
260 - if (existing.subscriberCount > 1) {
261 - const nextPollers = new Map(activePollers);
262 - nextPollers.set(cwd, {
263 - ...existing,
264 - subscriberCount: existing.subscriberCount - 1,
265 - });
266 - return [null, nextPollers] as const;
267 - }
268 -
269 - const nextPollers = new Map(activePollers);
270 - nextPollers.delete(cwd);
271 - return [existing.fiber, nextPollers] as const;
272 - });
273 -
274 - if (pollerToInterrupt) {
275 - yield* Fiber.interrupt(pollerToInterrupt).pipe(Effect.ignore);
276 - }
277 - });
278 -
279 - const streamStatus: GitStatusBroadcasterShape["streamStatus"] = (input) =>
280 - Stream.unwrap(
281 - Effect.gen(function* () {
282 - const normalizedCwd = normalizeCwd(input.cwd);
283 - const subscription = yield* PubSub.subscribe(changesPubSub);
284 - const initialLocal = yield* getOrLoadLocalStatus(normalizedCwd);
285 - const initialRemote = (yield* getCachedStatus(normalizedCwd))?.remote?.value ?? null;
286 - yield* retainRemotePoller(normalizedCwd);
287 -
288 - const release = releaseRemotePoller(normalizedCwd).pipe(Effect.ignore, Effect.asVoid);
289 -
290 - return Stream.concat(
291 - Stream.make({
292 - _tag: "snapshot" as const,
293 - local: initialLocal,
294 - remote: initialRemote,
295 - }),
296 - Stream.fromSubscription(subscription).pipe(
297 - Stream.filter((event) => event.cwd === normalizedCwd),
298 - Stream.map((event) => event.event),
299 - ),
300 - ).pipe(Stream.ensuring(release));
301 - }),
302 - );
303 -
304 - return {
305 - getStatus,
306 - refreshLocalStatus,
307 - refreshStatus,
308 - streamStatus,
309 - } satisfies GitStatusBroadcasterShape;
310 - }),
311 -);
apps/server/src/git/Layers/TextGenerationLive.ts deleted
-101
@@ -1,101 +0,0 @@
1 -/**
2 - * TextGenerationLive — registry-backed implementation of the `TextGeneration`
3 - * service tag.
4 - *
5 - * The `TextGeneration` tag is kept as a thin facade over
6 - * `ProviderInstanceRegistry`. Every op pulls `modelSelection.instanceId`,
7 - * looks up the matching `ProviderInstance`, and delegates to that instance's
8 - * own `textGeneration` closure (built by its driver's `create()`).
9 - *
10 - * There is deliberately no per-driver dispatch here — the registry already
11 - * knows which driver backs each instance, and each `ProviderInstance`
12 - * carries the fully-bound `TextGenerationShape` produced by its driver.
13 - * That means:
14 - *
15 - * - Multiple instances of the same driver (e.g. `codex_personal`,
16 - * `codex_work`) each get their own text-generation closure bound to
17 - * their own settings — the routing is by instance, not by driver.
18 - * - Unknown or disabled instances surface a `TextGenerationError` with
19 - * the missing `instanceId`, instead of silently falling back to a
20 - * default.
21 - *
22 - * This replaces the old `RoutingTextGenerationLive`, which tried to route
23 - * by driver-kind and misused `modelSelection.instanceId` as a driver-id
24 - * literal.
25 - *
26 - * @module git/Layers/TextGenerationLive
27 - */
28 -import { Effect, Layer } from "effect";
29 -
30 -import { TextGenerationError } from "@t3tools/contracts";
31 -import type { ProviderInstanceId } from "@t3tools/contracts";
32 -
33 -import {
34 - ProviderInstanceRegistry,
35 - type ProviderInstanceRegistryShape,
36 -} from "../../provider/Services/ProviderInstanceRegistry.ts";
37 -import type { ProviderInstance } from "../../provider/ProviderDriver.ts";
38 -import { TextGeneration, type TextGenerationShape } from "../Services/TextGeneration.ts";
39 -
40 -type TextGenerationOp =
41 - | "generateCommitMessage"
42 - | "generatePrContent"
43 - | "generateBranchName"
44 - | "generateThreadTitle";
45 -
46 -const resolveInstance = (
47 - registry: ProviderInstanceRegistryShape,
48 - operation: TextGenerationOp,
49 - instanceId: ProviderInstanceId,
50 -): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
51 - registry.getInstance(instanceId).pipe(
52 - Effect.flatMap((instance) =>
53 - instance
54 - ? Effect.succeed(instance.textGeneration)
55 - : Effect.fail(
56 - new TextGenerationError({
57 - operation,
58 - detail: `No provider instance registered for id '${instanceId}'.`,
59 - }),
60 - ),
61 - ),
62 - );
63 -
64 -/**
65 - * Build a `TextGenerationShape` that routes every call through the
66 - * registry. Exposed separately from the Layer so tests can construct it
67 - * against a stub registry without layering gymnastics.
68 - */
69 -export const makeTextGenerationFromRegistry = (
70 - registry: ProviderInstanceRegistryShape,
71 -): TextGenerationShape => ({
72 - generateCommitMessage: (input) =>
73 - resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
74 - Effect.flatMap((tg) => tg.generateCommitMessage(input)),
75 - ),
76 - generatePrContent: (input) =>
77 - resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
78 - Effect.flatMap((tg) => tg.generatePrContent(input)),
79 - ),
80 - generateBranchName: (input) =>
81 - resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
82 - Effect.flatMap((tg) => tg.generateBranchName(input)),
83 - ),
84 - generateThreadTitle: (input) =>
85 - resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
86 - Effect.flatMap((tg) => tg.generateThreadTitle(input)),
87 - ),
88 -});
89 -
90 -/**
91 - * `TextGeneration` Layer wired to the `ProviderInstanceRegistry`. The rest
92 - * of the server keeps using `yield* TextGeneration` — only the underlying
93 - * wiring changed from kind-based routing to instance-based routing.
94 - */
95 -export const TextGenerationLive = Layer.effect(
96 - TextGeneration,
97 - Effect.gen(function* () {
98 - const registry = yield* ProviderInstanceRegistry;
99 - return makeTextGenerationFromRegistry(registry);
100 - }),
101 -);
apps/server/src/git/Services/GitCore.ts deleted
-313
@@ -1,313 +0,0 @@
1 -/**
2 - * GitCore - Effect service contract for low-level Git operations.
3 - *
4 - * Wraps core repository primitives used by higher-level orchestration
5 - * services and WebSocket routes.
6 - *
7 - * @module GitCore
8 - */
9 -import { Context } from "effect";
10 -import type { Effect } from "effect";
11 -import type {
12 - GitCheckoutInput,
13 - GitCheckoutResult,
14 - GitCreateBranchInput,
15 - GitCreateBranchResult,
16 - GitCreateWorktreeInput,
17 - GitCreateWorktreeResult,
18 - GitInitInput,
19 - GitListBranchesInput,
20 - GitListBranchesResult,
21 - GitPullResult,
22 - GitRemoveWorktreeInput,
23 - GitStatusInput,
24 - GitStatusResult,
25 -} from "@t3tools/contracts";
26 -
27 -import type { GitCommandError } from "@t3tools/contracts";
28 -
29 -export interface ExecuteGitInput {
30 - readonly operation: string;
31 - readonly cwd: string;
32 - readonly args: ReadonlyArray<string>;
33 - readonly stdin?: string;
34 - readonly env?: NodeJS.ProcessEnv;
35 - readonly allowNonZeroExit?: boolean;
36 - readonly timeoutMs?: number;
37 - readonly maxOutputBytes?: number;
38 - readonly truncateOutputAtMaxBytes?: boolean;
39 - readonly progress?: ExecuteGitProgress;
40 -}
41 -
42 -export interface ExecuteGitResult {
43 - readonly code: number;
44 - readonly stdout: string;
45 - readonly stderr: string;
46 - readonly stdoutTruncated: boolean;
47 - readonly stderrTruncated: boolean;
48 -}
49 -
50 -export interface GitStatusDetails extends Omit<GitStatusResult, "pr"> {
51 - upstreamRef: string | null;
52 -}
53 -
54 -export interface GitPreparedCommitContext {
55 - stagedSummary: string;
56 - stagedPatch: string;
57 -}
58 -
59 -export interface ExecuteGitProgress {
60 - readonly onStdoutLine?: (line: string) => Effect.Effect<void, never>;
61 - readonly onStderrLine?: (line: string) => Effect.Effect<void, never>;
62 - readonly onHookStarted?: (hookName: string) => Effect.Effect<void, never>;
63 - readonly onHookFinished?: (input: {
64 - hookName: string;
65 - exitCode: number | null;
66 - durationMs: number | null;
67 - }) => Effect.Effect<void, never>;
68 -}
69 -
70 -export interface GitCommitProgress {
71 - readonly onOutputLine?: (input: {
72 - stream: "stdout" | "stderr";
73 - text: string;
74 - }) => Effect.Effect<void, never>;
75 - readonly onHookStarted?: (hookName: string) => Effect.Effect<void, never>;
76 - readonly onHookFinished?: (input: {
77 - hookName: string;
78 - exitCode: number | null;
79 - durationMs: number | null;
80 - }) => Effect.Effect<void, never>;
81 -}
82 -
83 -export interface GitCommitOptions {
84 - readonly timeoutMs?: number;
85 - readonly progress?: GitCommitProgress;
86 -}
87 -
88 -export interface GitPushResult {
89 - status: "pushed" | "skipped_up_to_date";
90 - branch: string;
91 - upstreamBranch?: string | undefined;
92 - setUpstream?: boolean | undefined;
93 -}
94 -
95 -export interface GitRangeContext {
96 - commitSummary: string;
97 - diffSummary: string;
98 - diffPatch: string;
99 -}
100 -
101 -export interface GitListWorkspaceFilesResult {
102 - readonly paths: ReadonlyArray<string>;
103 - readonly truncated: boolean;
104 -}
105 -
106 -export interface GitRenameBranchInput {
107 - cwd: string;
108 - oldBranch: string;
109 - newBranch: string;
110 -}
111 -
112 -export interface GitRenameBranchResult {
113 - branch: string;
114 -}
115 -
116 -export interface GitFetchPullRequestBranchInput {
117 - cwd: string;
118 - prNumber: number;
119 - branch: string;
120 -}
121 -
122 -export interface GitEnsureRemoteInput {
123 - cwd: string;
124 - preferredName: string;
125 - url: string;
126 -}
127 -
128 -export interface GitFetchRemoteBranchInput {
129 - cwd: string;
130 - remoteName: string;
131 - remoteBranch: string;
132 - localBranch: string;
133 -}
134 -
135 -export interface GitSetBranchUpstreamInput {
136 - cwd: string;
137 - branch: string;
138 - remoteName: string;
139 - remoteBranch: string;
140 -}
141 -
142 -/**
143 - * GitCoreShape - Service API for low-level Git repository interactions.
144 - */
145 -export interface GitCoreShape {
146 - /**
147 - * Execute a raw Git command.
148 - */
149 - readonly execute: (input: ExecuteGitInput) => Effect.Effect<ExecuteGitResult, GitCommandError>;
150 -
151 - /**
152 - * Read Git status for a repository.
153 - */
154 - readonly status: (input: GitStatusInput) => Effect.Effect<GitStatusResult, GitCommandError>;
155 -
156 - /**
157 - * Read detailed working tree / branch status for a repository.
158 - */
159 - readonly statusDetails: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
160 -
161 - /**
162 - * Read detailed working tree / branch status without refreshing remote tracking refs.
163 - */
164 - readonly statusDetailsLocal: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
165 -
166 - /**
167 - * Build staged change context for commit generation.
168 - */
169 - readonly prepareCommitContext: (
170 - cwd: string,
171 - filePaths?: readonly string[],
172 - ) => Effect.Effect<GitPreparedCommitContext | null, GitCommandError>;
173 -
174 - /**
175 - * Create a commit with provided subject/body.
176 - */
177 - readonly commit: (
178 - cwd: string,
179 - subject: string,
180 - body: string,
181 - options?: GitCommitOptions,
182 - ) => Effect.Effect<{ commitSha: string }, GitCommandError>;
183 -
184 - /**
185 - * Push current branch, setting upstream if needed.
186 - */
187 - readonly pushCurrentBranch: (
188 - cwd: string,
189 - fallbackBranch: string | null,
190 - ) => Effect.Effect<GitPushResult, GitCommandError>;
191 -
192 - /**
193 - * Collect commit/diff context between base branch and current HEAD.
194 - */
195 - readonly readRangeContext: (
196 - cwd: string,
197 - baseBranch: string,
198 - ) => Effect.Effect<GitRangeContext, GitCommandError>;
199 -
200 - /**
201 - * Read a Git config value from the local repository.
202 - */
203 - readonly readConfigValue: (
204 - cwd: string,
205 - key: string,
206 - ) => Effect.Effect<string | null, GitCommandError>;
207 -
208 - /**
209 - * Determine whether the provided cwd is inside a git work tree.
210 - */
211 - readonly isInsideWorkTree: (cwd: string) => Effect.Effect<boolean, GitCommandError>;
212 -
213 - /**
214 - * List tracked and untracked workspace file paths relative to cwd.
215 - */
216 - readonly listWorkspaceFiles: (
217 - cwd: string,
218 - ) => Effect.Effect<GitListWorkspaceFilesResult, GitCommandError>;
219 -
220 - /**
221 - * Remove gitignored paths from a relative path list.
222 - */
223 - readonly filterIgnoredPaths: (
224 - cwd: string,
225 - relativePaths: ReadonlyArray<string>,
226 - ) => Effect.Effect<ReadonlyArray<string>, GitCommandError>;
227 -
228 - /**
229 - * List local + remote branches and branch metadata.
230 - */
231 - readonly listBranches: (
232 - input: GitListBranchesInput,
233 - ) => Effect.Effect<GitListBranchesResult, GitCommandError>;
234 -
235 - /**
236 - * Pull current branch from upstream using fast-forward only.
237 - */
238 - readonly pullCurrentBranch: (cwd: string) => Effect.Effect<GitPullResult, GitCommandError>;
239 -
240 - /**
241 - * Create a worktree and branch from a base branch.
242 - */
243 - readonly createWorktree: (
244 - input: GitCreateWorktreeInput,
245 - ) => Effect.Effect<GitCreateWorktreeResult, GitCommandError>;
246 -
247 - /**
248 - * Materialize a GitHub pull request head as a local branch without switching checkout.
249 - */
250 - readonly fetchPullRequestBranch: (
251 - input: GitFetchPullRequestBranchInput,
252 - ) => Effect.Effect<void, GitCommandError>;
253 -
254 - /**
255 - * Ensure a named remote exists for the provided URL, returning the reused or created remote name.
256 - */
257 - readonly ensureRemote: (input: GitEnsureRemoteInput) => Effect.Effect<string, GitCommandError>;
258 -
259 - /**
260 - * Fetch a remote branch into a local branch without checkout.
261 - */
262 - readonly fetchRemoteBranch: (
263 - input: GitFetchRemoteBranchInput,
264 - ) => Effect.Effect<void, GitCommandError>;
265 -
266 - /**
267 - * Set the upstream tracking branch for a local branch.
268 - */
269 - readonly setBranchUpstream: (
270 - input: GitSetBranchUpstreamInput,
271 - ) => Effect.Effect<void, GitCommandError>;
272 -
273 - /**
274 - * Remove an existing worktree.
275 - */
276 - readonly removeWorktree: (input: GitRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
277 -
278 - /**
279 - * Rename an existing local branch.
280 - */
281 - readonly renameBranch: (
282 - input: GitRenameBranchInput,
283 - ) => Effect.Effect<GitRenameBranchResult, GitCommandError>;
284 -
285 - /**
286 - * Create a local branch.
287 - */
288 - readonly createBranch: (
289 - input: GitCreateBranchInput,
290 - ) => Effect.Effect<GitCreateBranchResult, GitCommandError>;
291 -
292 - /**
293 - * Checkout an existing branch and refresh its upstream metadata in background.
294 - */
295 - readonly checkoutBranch: (
296 - input: GitCheckoutInput,
297 - ) => Effect.Effect<GitCheckoutResult, GitCommandError>;
298 -
299 - /**
300 - * Initialize a repository in the provided directory.
301 - */
302 - readonly initRepo: (input: GitInitInput) => Effect.Effect<void, GitCommandError>;
303 -
304 - /**
305 - * List local branch names (short format).
306 - */
307 - readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
308 -}
309 -
310 -/**
311 - * GitCore - Service tag for low-level Git repository operations.
312 - */
313 -export class GitCore extends Context.Service<GitCore, GitCoreShape>()("t3/git/Services/GitCore") {}
apps/server/src/git/Services/GitHubCli.ts deleted
-103
@@ -1,103 +0,0 @@
1 -/**
2 - * GitHubCli - Effect service contract for `gh` process interactions.
3 - *
4 - * Provides thin command execution helpers used by Git workflow orchestration.
5 - *
6 - * @module GitHubCli
7 - */
8 -import { Context } from "effect";
9 -import type { Effect } from "effect";
10 -
11 -import type { ProcessRunResult } from "../../processRunner.ts";
12 -import type { GitHubCliError } from "@t3tools/contracts";
13 -
14 -export interface GitHubPullRequestSummary {
15 - readonly number: number;
16 - readonly title: string;
17 - readonly url: string;
18 - readonly baseRefName: string;
19 - readonly headRefName: string;
20 - readonly state?: "open" | "closed" | "merged";
21 - readonly isCrossRepository?: boolean;
22 - readonly headRepositoryNameWithOwner?: string | null;
23 - readonly headRepositoryOwnerLogin?: string | null;
24 -}
25 -
26 -export interface GitHubRepositoryCloneUrls {
27 - readonly nameWithOwner: string;
28 - readonly url: string;
29 - readonly sshUrl: string;
30 -}
31 -
32 -/**
33 - * GitHubCliShape - Service API for executing GitHub CLI commands.
34 - */
35 -export interface GitHubCliShape {
36 - /**
37 - * Execute a GitHub CLI command and return full process output.
38 - */
39 - readonly execute: (input: {
40 - readonly cwd: string;
41 - readonly args: ReadonlyArray<string>;
42 - readonly timeoutMs?: number;
43 - }) => Effect.Effect<ProcessRunResult, GitHubCliError>;
44 -
45 - /**
46 - * List open pull requests for a head branch.
47 - */
48 - readonly listOpenPullRequests: (input: {
49 - readonly cwd: string;
50 - readonly headSelector: string;
51 - readonly limit?: number;
52 - }) => Effect.Effect<ReadonlyArray<GitHubPullRequestSummary>, GitHubCliError>;
53 -
54 - /**
55 - * Resolve a pull request by URL, number, or branch-ish identifier.
56 - */
57 - readonly getPullRequest: (input: {
58 - readonly cwd: string;
59 - readonly reference: string;
60 - }) => Effect.Effect<GitHubPullRequestSummary, GitHubCliError>;
61 -
62 - /**
63 - * Resolve clone URLs for a GitHub repository.
64 - */
65 - readonly getRepositoryCloneUrls: (input: {
66 - readonly cwd: string;
67 - readonly repository: string;
68 - }) => Effect.Effect<GitHubRepositoryCloneUrls, GitHubCliError>;
69 -
70 - /**
71 - * Create a pull request from branch context and body file.
72 - */
73 - readonly createPullRequest: (input: {
74 - readonly cwd: string;
75 - readonly baseBranch: string;
76 - readonly headSelector: string;
77 - readonly title: string;
78 - readonly bodyFile: string;
79 - }) => Effect.Effect<void, GitHubCliError>;
80 -
81 - /**
82 - * Resolve repository default branch through GitHub metadata.
83 - */
84 - readonly getDefaultBranch: (input: {
85 - readonly cwd: string;
86 - }) => Effect.Effect<string | null, GitHubCliError>;
87 -
88 - /**
89 - * Checkout a pull request into the current repository worktree.
90 - */
91 - readonly checkoutPullRequest: (input: {
92 - readonly cwd: string;
93 - readonly reference: string;
94 - readonly force?: boolean;
95 - }) => Effect.Effect<void, GitHubCliError>;
96 -}
97 -
98 -/**
99 - * GitHubCli - Service tag for GitHub CLI process execution.
100 - */
101 -export class GitHubCli extends Context.Service<GitHubCli, GitHubCliShape>()(
102 - "t3/git/Services/GitHubCli",
103 -) {}
apps/server/src/git/Services/GitManager.ts deleted
-104
@@ -1,104 +0,0 @@
1 -/**
2 - * GitManager - Effect service contract for stacked Git workflows.
3 - *
4 - * Orchestrates status inspection and commit/push/PR flows by composing
5 - * lower-level Git and external tool services.
6 - *
7 - * @module GitManager
8 - */
9 -import {
10 - GitActionProgressEvent,
11 - GitPreparePullRequestThreadInput,
12 - GitPreparePullRequestThreadResult,
13 - GitPullRequestRefInput,
14 - GitResolvePullRequestResult,
15 - GitRunStackedActionInput,
16 - GitRunStackedActionResult,
17 - GitStatusLocalResult,
18 - GitStatusRemoteResult,
19 - GitStatusInput,
20 - GitStatusResult,
21 -} from "@t3tools/contracts";
22 -import { Context } from "effect";
23 -import type { Effect } from "effect";
24 -import type { GitManagerServiceError } from "@t3tools/contracts";
25 -
26 -export interface GitActionProgressReporter {
27 - readonly publish: (event: GitActionProgressEvent) => Effect.Effect<void, never>;
28 -}
29 -
30 -export interface GitRunStackedActionOptions {
31 - readonly actionId?: string;
32 - readonly progressReporter?: GitActionProgressReporter;
33 -}
34 -
35 -/**
36 - * GitManagerShape - Service API for high-level Git workflow actions.
37 - */
38 -export interface GitManagerShape {
39 - /**
40 - * Read current repository Git status plus open PR metadata when available.
41 - */
42 - readonly status: (
43 - input: GitStatusInput,
44 - ) => Effect.Effect<GitStatusResult, GitManagerServiceError>;
45 -
46 - /**
47 - * Read local repository status without remote hosting enrichment.
48 - */
49 - readonly localStatus: (
50 - input: GitStatusInput,
51 - ) => Effect.Effect<GitStatusLocalResult, GitManagerServiceError>;
52 -
53 - /**
54 - * Read remote tracking / PR status for a repository.
55 - */
56 - readonly remoteStatus: (
57 - input: GitStatusInput,
58 - ) => Effect.Effect<GitStatusRemoteResult | null, GitManagerServiceError>;
59 -
60 - /**
61 - * Clear any cached local status snapshot for a repository.
62 - */
63 - readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
64 -
65 - /**
66 - * Clear any cached remote status snapshot for a repository.
67 - */
68 - readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
69 -
70 - /**
71 - * Clear any cached status snapshot for a repository so the next read is fresh.
72 - */
73 - readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
74 -
75 - /**
76 - * Resolve a pull request by URL/number against the current repository.
77 - */
78 - readonly resolvePullRequest: (
79 - input: GitPullRequestRefInput,
80 - ) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
81 -
82 - /**
83 - * Prepare a new thread workspace from a pull request in local or worktree mode.
84 - */
85 - readonly preparePullRequestThread: (
86 - input: GitPreparePullRequestThreadInput,
87 - ) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;
88 -
89 - /**
90 - * Run a Git action (`commit`, `push`, `create_pr`, `commit_push`, `commit_push_pr`).
91 - * When `featureBranch` is set, creates and checks out a feature branch first.
92 - */
93 - readonly runStackedAction: (
94 - input: GitRunStackedActionInput,
95 - options?: GitRunStackedActionOptions,
96 - ) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
97 -}
98 -
99 -/**
100 - * GitManager - Service tag for stacked Git workflow orchestration.
101 - */
102 -export class GitManager extends Context.Service<GitManager, GitManagerShape>()(
103 - "t3/git/Services/GitManager",
104 -) {}
apps/server/src/git/Services/GitStatusBroadcaster.ts deleted
-27
@@ -1,27 +0,0 @@
1 -import { Context } from "effect";
2 -import type { Effect, Stream } from "effect";
3 -import type {
4 - GitManagerServiceError,
5 - GitStatusInput,
6 - GitStatusLocalResult,
7 - GitStatusResult,
8 - GitStatusStreamEvent,
9 -} from "@t3tools/contracts";
10 -
11 -export interface GitStatusBroadcasterShape {
12 - readonly getStatus: (
13 - input: GitStatusInput,
14 - ) => Effect.Effect<GitStatusResult, GitManagerServiceError>;
15 - readonly refreshLocalStatus: (
16 - cwd: string,
17 - ) => Effect.Effect<GitStatusLocalResult, GitManagerServiceError>;
18 - readonly refreshStatus: (cwd: string) => Effect.Effect<GitStatusResult, GitManagerServiceError>;
19 - readonly streamStatus: (
20 - input: GitStatusInput,
21 - ) => Stream.Stream<GitStatusStreamEvent, GitManagerServiceError>;
22 -}
23 -
24 -export class GitStatusBroadcaster extends Context.Service<
25 - GitStatusBroadcaster,
26 - GitStatusBroadcasterShape
27 ->()("t3/git/Services/GitStatusBroadcaster") {}
apps/server/src/git/Utils.ts
-165
@@ -1,171 +1,6 @@
1 -/**
2 - * Shared utilities for text generation layers (Codex, Claude, etc.).
3 - *
4 - * @module textGenerationUtils
5 - */
6 -import { Schema } from "effect";
7 -
8 -import { TextGenerationError } from "@t3tools/contracts";
9 -
1 import { existsSync } from "node:fs";
2 import { join } from "node:path";
3
4 export function isGitRepository(cwd: string): boolean {
5 return existsSync(join(cwd, ".git"));
6 }
16 -
17 -/** Convert an Effect Schema to a flat JSON Schema object, inlining `$defs` when present. */
18 -export function toJsonSchemaObject(schema: Schema.Top): unknown {
19 - const document = Schema.toJsonSchemaDocument(schema);
20 - if (document.definitions && Object.keys(document.definitions).length > 0) {
21 - return { ...document.schema, $defs: document.definitions };
22 - }
23 - return document.schema;
24 -}
25 -
26 -/** Truncate a text section to `maxChars`, appending a `[truncated]` marker when needed. */
27 -export function limitSection(value: string, maxChars: number): string {
28 - if (value.length <= maxChars) return value;
29 - const truncated = value.slice(0, maxChars);
30 - return `${truncated}\n\n[truncated]`;
31 -}
32 -
33 -export function extractJsonObject(raw: string): string {
34 - const trimmed = raw.trim();
35 - if (trimmed.length === 0) {
36 - return trimmed;
37 - }
38 -
39 - const start = trimmed.indexOf("{");
40 - if (start < 0) {
41 - return trimmed;
42 - }
43 -
44 - let depth = 0;
45 - let inString = false;
46 - let escaping = false;
47 - for (let index = start; index < trimmed.length; index += 1) {
48 - const char = trimmed[index];
49 - if (inString) {
50 - if (escaping) {
51 - escaping = false;
52 - } else if (char === "\\") {
53 - escaping = true;
54 - } else if (char === '"') {
55 - inString = false;
56 - }
57 - continue;
58 - }
59 -
60 - if (char === '"') {
61 - inString = true;
62 - continue;
63 - }
64 -
65 - if (char === "{") {
66 - depth += 1;
67 - continue;
68 - }
69 -
70 - if (char === "}") {
71 - depth -= 1;
72 - if (depth === 0) {
73 - return trimmed.slice(start, index + 1);
74 - }
75 - }
76 - }
77 -
78 - return trimmed.slice(start);
79 -}
80 -
81 -/** Normalise a raw commit subject to imperative-mood, ≤72 chars, no trailing period. */
82 -export function sanitizeCommitSubject(raw: string): string {
83 - const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
84 - const withoutTrailingPeriod = singleLine.replace(/[.]+$/g, "").trim();
85 - if (withoutTrailingPeriod.length === 0) {
86 - return "Update project files";
87 - }
88 -
89 - if (withoutTrailingPeriod.length <= 72) {
90 - return withoutTrailingPeriod;
91 - }
92 - return withoutTrailingPeriod.slice(0, 72).trimEnd();
93 -}
94 -
95 -/** Normalise a raw PR title to a single line with a sensible fallback. */
96 -export function sanitizePrTitle(raw: string): string {
97 - const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
98 - if (singleLine.length > 0) {
99 - return singleLine;
100 - }
101 - return "Update project changes";
102 -}
103 -
104 -/** Normalise a raw thread title to a compact single-line sidebar-safe label. */
105 -export function sanitizeThreadTitle(raw: string): string {
106 - const normalized = raw
107 - .trim()
108 - .split(/\r?\n/g)[0]
109 - ?.trim()
110 - .replace(/^['"`]+|['"`]+$/g, "")
111 - .trim()
112 - .replace(/\s+/g, " ");
113 -
114 - if (!normalized || normalized.trim().length === 0) {
115 - return "New thread";
116 - }
117 -
118 - if (normalized.length <= 50) {
119 - return normalized;
120 - }
121 -
122 - return `${normalized.slice(0, 47).trimEnd()}...`;
123 -}
124 -
125 -/** CLI name to human-readable label, e.g. "codex" → "Codex CLI (`codex`)" */
126 -function cliLabel(cliName: string): string {
127 - const capitalized = cliName.charAt(0).toUpperCase() + cliName.slice(1);
128 - return `${capitalized} CLI (\`${cliName}\`)`;
129 -}
130 -
131 -/**
132 - * Normalize an unknown error from a CLI text generation process into a
133 - * typed `TextGenerationError`. Parameterized by CLI name so both Codex
134 - * and Claude (and future providers) can share the same logic.
135 - */
136 -export function normalizeCliError(
137 - cliName: string,
138 - operation: string,
139 - error: unknown,
140 - fallback: string,
141 -): TextGenerationError {
142 - if (Schema.is(TextGenerationError)(error)) {
143 - return error;
144 - }
145 -
146 - if (error instanceof Error) {
147 - const lower = error.message.toLowerCase();
148 - if (
149 - error.message.includes(`Command not found: ${cliName}`) ||
150 - lower.includes(`spawn ${cliName}`) ||
151 - lower.includes("enoent")
152 - ) {
153 - return new TextGenerationError({
154 - operation,
155 - detail: `${cliLabel(cliName)} is required but not available on PATH.`,
156 - cause: error,
157 - });
158 - }
159 - return new TextGenerationError({
160 - operation,
161 - detail: `${fallback}: ${error.message}`,
162 - cause: error,
163 - });
164 - }
165 -
166 - return new TextGenerationError({
167 - operation,
168 - detail: fallback,
169 - cause: error,
170 - });
171 -}
apps/server/src/git/githubPullRequests.ts
+4 -5
@@ -1,4 +1,4 @@
1 -import { Cause, Exit, Result, Schema } from "effect";
1 +import { Cause, DateTime, Exit, Option, Result, Schema } from "effect";
2 import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts";
3 import { decodeJsonResult, formatSchemaError } from "@t3tools/shared/schemaJson";
4
@@ -9,7 +9,7 @@ export interface NormalizedGitHubPullRequestRecord {
9 readonly baseRefName: string;
10 readonly headRefName: string;
11 readonly state: "open" | "closed" | "merged";
12 - readonly updatedAt: string | null;
12 + readonly updatedAt: Option.Option<DateTime.Utc>;
13 readonly isCrossRepository?: boolean;
14 readonly headRepositoryNameWithOwner?: string | null;
15 readonly headRepositoryOwnerLogin?: string | null;
@@ -23,7 +23,7 @@ const GitHubPullRequestSchema = Schema.Struct({
23 headRefName: TrimmedNonEmptyString,
24 state: Schema.optional(Schema.NullOr(Schema.String)),
25 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
26 - updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
26 + updatedAt: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
27 isCrossRepository: Schema.optional(Schema.Boolean),
28 headRepository: Schema.optional(
29 Schema.NullOr(
@@ -80,8 +80,7 @@ function normalizeGitHubPullRequestRecord(
80 baseRefName: raw.baseRefName,
81 headRefName: raw.headRefName,
82 state: normalizeGitHubPullRequestState(raw),
83 - updatedAt:
84 - typeof raw.updatedAt === "string" && raw.updatedAt.trim().length > 0 ? raw.updatedAt : null,
83 + updatedAt: raw.updatedAt ?? Option.none(),
84 ...(typeof raw.isCrossRepository === "boolean"
85 ? { isCrossRepository: raw.isCrossRepository }
86 : {}),
apps/server/src/orchestration/Layers/CheckpointReactor.test.ts
+16 -10
@@ -24,8 +24,9 @@ import { afterEach, describe, expect, it, vi } from "vitest";
24
25 import { CheckpointStoreLive } from "../../checkpointing/Layers/CheckpointStore.ts";
26 import { CheckpointStore } from "../../checkpointing/Services/CheckpointStore.ts";
27 -import { GitCoreLive } from "../../git/Layers/GitCore.ts";
28 -import { GitStatusBroadcaster } from "../../git/Services/GitStatusBroadcaster.ts";
27 +import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts";
28 +import * as VcsProcess from "../../vcs/VcsProcess.ts";
29 +import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
30 import { RepositoryIdentityResolverLive } from "../../project/Layers/RepositoryIdentityResolver.ts";
31 import { CheckpointReactorLive } from "./CheckpointReactor.ts";
32 import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
@@ -282,7 +283,7 @@ describe("CheckpointReactor", () => {
283 const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
284 prefix: "t3-checkpoint-reactor-test-",
285 });
285 - const gitStatusBroadcasterLayer = Layer.succeed(GitStatusBroadcaster, {
286 + const vcsStatusBroadcasterLayer = Layer.succeed(VcsStatusBroadcaster, {
287 getStatus: () => Effect.die("getStatus should not be called in this test"),
288 refreshLocalStatus: (cwd: string) =>
289 Effect.sync(() => {
@@ -290,9 +291,9 @@ describe("CheckpointReactor", () => {
291 }).pipe(
292 Effect.as({
293 isRepo: true,
293 - hasOriginRemote: false,
294 - isDefaultBranch: true,
295 - branch: "main",
294 + hasPrimaryRemote: false,
295 + isDefaultRef: true,
296 + refName: "main",
297 hasWorkingTreeChanges: false,
298 workingTree: { files: [], insertions: 0, deletions: 0 },
299 }),
@@ -305,11 +306,16 @@ describe("CheckpointReactor", () => {
306 Layer.provideMerge(orchestrationLayer),
307 Layer.provideMerge(RuntimeReceiptBusLive),
308 Layer.provideMerge(Layer.succeed(ProviderService, provider.service)),
308 - Layer.provideMerge(gitStatusBroadcasterLayer),
309 - Layer.provideMerge(CheckpointStoreLive),
310 - Layer.provideMerge(WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive))),
309 + Layer.provideMerge(vcsStatusBroadcasterLayer),
310 + Layer.provideMerge(CheckpointStoreLive.pipe(Layer.provide(VcsDriverRegistry.layer))),
311 + Layer.provideMerge(
312 + WorkspaceEntriesLive.pipe(
313 + Layer.provide(WorkspacePathsLive),
314 + Layer.provideMerge(VcsDriverRegistry.layer),
315 + ),
316 + ),
317 Layer.provideMerge(WorkspacePathsLive),
312 - Layer.provideMerge(GitCoreLive),
318 + Layer.provideMerge(VcsProcess.layer),
319 Layer.provideMerge(ServerConfigLayer),
320 Layer.provideMerge(NodeServices.layer),
321 );
apps/server/src/orchestration/Layers/CheckpointReactor.ts
+3 -3
@@ -24,7 +24,7 @@ import { RuntimeReceiptBus } from "../Services/RuntimeReceiptBus.ts";
24 import type { CheckpointStoreError } from "../../checkpointing/Errors.ts";
25 import type { OrchestrationDispatchError } from "../Errors.ts";
26 import { isGitRepository } from "../../git/Utils.ts";
27 -import { GitStatusBroadcaster } from "../../git/Services/GitStatusBroadcaster.ts";
27 +import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
28 import { WorkspaceEntries } from "../../workspace/Services/WorkspaceEntries.ts";
29
30 type ReactorInput =
@@ -70,7 +70,7 @@ const make = Effect.gen(function* () {
70 const checkpointStore = yield* CheckpointStore;
71 const receiptBus = yield* RuntimeReceiptBus;
72 const workspaceEntries = yield* WorkspaceEntries;
73 - const gitStatusBroadcaster = yield* GitStatusBroadcaster;
73 + const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
74
75 const appendRevertFailureActivity = (input: {
76 readonly threadId: ThreadId;
@@ -508,7 +508,7 @@ const make = Effect.gen(function* () {
508 return;
509 }
510
511 - yield* gitStatusBroadcaster.refreshLocalStatus(sessionRuntime.value.cwd).pipe(
511 + yield* vcsStatusBroadcaster.refreshLocalStatus(sessionRuntime.value.cwd).pipe(
512 Effect.catch((error) =>
513 Effect.logWarning("failed to refresh local git status after turn completion", {
514 threadId: event.threadId,
apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
+13 -12
@@ -33,12 +33,7 @@ import {
33 ProviderService,
34 type ProviderServiceShape,
35 } from "../../provider/Services/ProviderService.ts";
36 -import { GitCore, type GitCoreShape } from "../../git/Services/GitCore.ts";
37 -import {
38 - GitStatusBroadcaster,
39 - type GitStatusBroadcasterShape,
40 -} from "../../git/Services/GitStatusBroadcaster.ts";
41 -import { TextGeneration, type TextGenerationShape } from "../../git/Services/TextGeneration.ts";
36 +import { TextGeneration, type TextGenerationShape } from "../../textGeneration/TextGeneration.ts";
37 import { RepositoryIdentityResolverLive } from "../../project/Layers/RepositoryIdentityResolver.ts";
38 import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
39 import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
@@ -52,6 +47,8 @@ import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
47 import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
48 import * as NodeServices from "@effect/platform-node/NodeServices";
49 import { ServerSettingsService } from "../../serverSettings.ts";
50 +import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
51 +import { GitWorkflowService, type GitWorkflowServiceShape } from "../../git/GitWorkflowService.ts";
52
53 const asProjectId = (value: string): ProjectId => ProjectId.make(value);
54 const asApprovalRequestId = (value: string): ApprovalRequestId => ApprovalRequestId.make(value);
@@ -243,9 +240,9 @@ describe("ProviderCommandReactor", () => {
240 const refreshStatus = vi.fn((_: string) =>
241 Effect.succeed({
242 isRepo: true,
246 - hasOriginRemote: true,
247 - isDefaultBranch: false,
248 - branch: "renamed-branch",
243 + hasPrimaryRemote: true,
244 + isDefaultRef: false,
245 + refName: "renamed-branch",
246 hasWorkingTreeChanges: false,
247 workingTree: {
248 files: [],
@@ -324,15 +321,19 @@ describe("ProviderCommandReactor", () => {
321 const layer = ProviderCommandReactorLive.pipe(
322 Layer.provideMerge(orchestrationLayer),
323 Layer.provideMerge(Layer.succeed(ProviderService, service)),
327 - Layer.provideMerge(Layer.succeed(GitCore, { renameBranch } as unknown as GitCoreShape)),
324 Layer.provideMerge(
329 - Layer.succeed(GitStatusBroadcaster, {
325 + Layer.mock(GitWorkflowService)({
326 + renameBranch,
327 + } satisfies Partial<GitWorkflowServiceShape>),
328 + ),
329 + Layer.provideMerge(
330 + Layer.succeed(VcsStatusBroadcaster, {
331 getStatus: () => Effect.die("getStatus should not be called in this test"),
332 refreshLocalStatus: () =>
333 Effect.die("refreshLocalStatus should not be called in this test"),
334 refreshStatus,
335 streamStatus: () => Stream.die("streamStatus should not be called in this test"),
335 - } satisfies GitStatusBroadcasterShape),
336 + }),
337 ),
338 Layer.provideMerge(
339 Layer.mock(TextGeneration, {
apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
+7 -7
@@ -16,12 +16,10 @@ import { Cache, Cause, Duration, Effect, Equal, Layer, Option, Schema, Stream }
16 import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
17
18 import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts";
19 -import { GitCore } from "../../git/Services/GitCore.ts";
20 -import { GitStatusBroadcaster } from "../../git/Services/GitStatusBroadcaster.ts";
19 import { increment, orchestrationEventsProcessedTotal } from "../../observability/Metrics.ts";
20 import { ProviderAdapterRequestError } from "../../provider/Errors.ts";
21 import type { ProviderServiceError } from "../../provider/Errors.ts";
24 -import { TextGeneration } from "../../git/Services/TextGeneration.ts";
22 +import { TextGeneration } from "../../textGeneration/TextGeneration.ts";
23 import { ProviderService } from "../../provider/Services/ProviderService.ts";
24 import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
25 import {
@@ -29,6 +27,8 @@ import {
27 type ProviderCommandReactorShape,
28 } from "../Services/ProviderCommandReactor.ts";
29 import { ServerSettingsService } from "../../serverSettings.ts";
30 +import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
31 +import { GitWorkflowService } from "../../git/GitWorkflowService.ts";
32
33 type ProviderIntentEvent = Extract<
34 OrchestrationEvent,
@@ -168,8 +168,8 @@ function buildGeneratedWorktreeBranchName(raw: string): string {
168 const make = Effect.gen(function* () {
169 const orchestrationEngine = yield* OrchestrationEngineService;
170 const providerService = yield* ProviderService;
171 - const git = yield* GitCore;
172 - const gitStatusBroadcaster = yield* GitStatusBroadcaster;
171 + const gitWorkflow = yield* GitWorkflowService;
172 + const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
173 const textGeneration = yield* TextGeneration;
174 const serverSettingsService = yield* ServerSettingsService;
175 const handledTurnStartKeys = yield* Cache.make<string, true>({
@@ -587,7 +587,7 @@ const make = Effect.gen(function* () {
587 const targetBranch = buildGeneratedWorktreeBranchName(generated.branch);
588 if (targetBranch === oldBranch) return;
589
590 - const renamed = yield* git.renameBranch({ cwd, oldBranch, newBranch: targetBranch });
590 + const renamed = yield* gitWorkflow.renameBranch({ cwd, oldBranch, newBranch: targetBranch });
591 yield* orchestrationEngine.dispatch({
592 type: "thread.meta.update",
593 commandId: serverCommandId("worktree-branch-rename"),
@@ -595,7 +595,7 @@ const make = Effect.gen(function* () {
595 branch: renamed.branch,
596 worktreePath: cwd,
597 });
598 - yield* gitStatusBroadcaster.refreshStatus(cwd).pipe(Effect.ignoreCause({ log: true }));
598 + yield* vcsStatusBroadcaster.refreshStatus(cwd).pipe(Effect.ignoreCause({ log: true }));
599 }).pipe(
600 Effect.catchCause((cause) =>
601 Effect.logWarning("provider command reactor failed to generate or rename worktree branch", {
apps/server/src/project/Layers/RepositoryIdentityResolver.ts
+6 -3
@@ -1,6 +1,9 @@
1 import type { RepositoryIdentity } from "@t3tools/contracts";
2 import { Cache, Duration, Effect, Exit, Layer } from "effect";
3 -import { detectGitHostingProviderFromRemoteUrl, normalizeGitRemoteUrl } from "@t3tools/shared/git";
3 +import {
4 + detectSourceControlProviderFromGitRemoteUrl,
5 + normalizeGitRemoteUrl,
6 +} from "@t3tools/shared/git";
7
8 import { runProcess } from "../../processRunner.ts";
9 import {
@@ -45,7 +48,7 @@ function buildRepositoryIdentity(input: {
48 readonly rootPath: string;
49 }): RepositoryIdentity {
50 const canonicalKey = normalizeGitRemoteUrl(input.remoteUrl);
48 - const hostingProvider = detectGitHostingProviderFromRemoteUrl(input.remoteUrl);
51 + const sourceControlProvider = detectSourceControlProviderFromGitRemoteUrl(input.remoteUrl);
52 const repositoryPath = canonicalKey.split("/").slice(1).join("/");
53 const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0);
54 const [owner] = repositoryPathSegments;
@@ -60,7 +63,7 @@ function buildRepositoryIdentity(input: {
63 },
64 rootPath: input.rootPath,
65 ...(repositoryPath ? { displayName: repositoryPath } : {}),
63 - ...(hostingProvider ? { provider: hostingProvider.kind } : {}),
66 + ...(sourceControlProvider ? { provider: sourceControlProvider.kind } : {}),
67 ...(owner ? { owner } : {}),
68 ...(repositoryName ? { name: repositoryName } : {}),
69 };
apps/server/src/provider/Drivers/ClaudeDriver.ts
+1 -1
@@ -16,7 +16,7 @@ import { ClaudeSettings, ProviderDriverKind, type ServerProvider } from "@t3tool
16 import { Cache, Duration, Effect, FileSystem, Path, Schema, Stream } from "effect";
17 import { ChildProcessSpawner } from "effect/unstable/process";
18
19 -import { makeClaudeTextGeneration } from "../../git/Layers/ClaudeTextGeneration.ts";
19 +import { makeClaudeTextGeneration } from "../../textGeneration/ClaudeTextGeneration.ts";
20 import { ServerConfig } from "../../config.ts";
21 import { ProviderDriverError } from "../Errors.ts";
22 import { makeClaudeAdapter } from "../Layers/ClaudeAdapter.ts";
apps/server/src/provider/Drivers/CodexDriver.ts
+1 -1
@@ -25,7 +25,7 @@ import { CodexSettings, ProviderDriverKind, type ServerProvider } from "@t3tools
25 import { Duration, Effect, FileSystem, Path, Schema, Stream } from "effect";
26 import { ChildProcessSpawner } from "effect/unstable/process";
27
28 -import { makeCodexTextGeneration } from "../../git/Layers/CodexTextGeneration.ts";
28 +import { makeCodexTextGeneration } from "../../textGeneration/CodexTextGeneration.ts";
29 import { ServerConfig } from "../../config.ts";
30 import { ProviderDriverError } from "../Errors.ts";
31 import { makeCodexAdapter } from "../Layers/CodexAdapter.ts";
apps/server/src/provider/Drivers/CursorDriver.ts
+1 -1
@@ -17,7 +17,7 @@ import { Duration, Effect, FileSystem, Path, Schema, Stream } from "effect";
17 import { ChildProcessSpawner } from "effect/unstable/process";
18
19 import { ServerConfig } from "../../config.ts";
20 -import { makeCursorTextGeneration } from "../../git/Layers/CursorTextGeneration.ts";
20 +import { makeCursorTextGeneration } from "../../textGeneration/CursorTextGeneration.ts";
21 import { ProviderDriverError } from "../Errors.ts";
22 import { makeCursorAdapter } from "../Layers/CursorAdapter.ts";
23 import {
apps/server/src/provider/Drivers/OpenCodeDriver.ts
+1 -1
@@ -16,7 +16,7 @@ import { OpenCodeSettings, ProviderDriverKind, type ServerProvider } from "@t3to
16 import { Duration, Effect, FileSystem, Path, Schema, Stream } from "effect";
17 import { ChildProcessSpawner } from "effect/unstable/process";
18
19 -import { makeOpenCodeTextGeneration } from "../../git/Layers/OpenCodeTextGeneration.ts";
19 +import { makeOpenCodeTextGeneration } from "../../textGeneration/OpenCodeTextGeneration.ts";
20 import { ServerConfig } from "../../config.ts";
21 import { ProviderDriverError } from "../Errors.ts";
22 import { makeOpenCodeAdapter } from "../Layers/OpenCodeAdapter.ts";
apps/server/src/provider/Layers/ProviderAdapterRegistry.test.ts
+1 -1
@@ -14,7 +14,7 @@ import type { OpenCodeAdapterShape } from "../Services/OpenCodeAdapter.ts";
14 import { ProviderAdapterRegistry } from "../Services/ProviderAdapterRegistry.ts";
15 import { ProviderInstanceRegistry } from "../Services/ProviderInstanceRegistry.ts";
16 import type { ProviderInstance } from "../ProviderDriver.ts";
17 -import type { TextGenerationShape } from "../../git/Services/TextGeneration.ts";
17 +import type { TextGenerationShape } from "../../textGeneration/TextGeneration.ts";
18 import { ProviderAdapterRegistryLive } from "./ProviderAdapterRegistry.ts";
19 import * as NodeServices from "@effect/platform-node/NodeServices";
20
apps/server/src/provider/ProviderDriver.ts
+1 -1
@@ -28,7 +28,7 @@ import type {
28 } from "@t3tools/contracts";
29 import type { Effect, Schema, Scope } from "effect";
30
31 -import type { TextGenerationShape } from "../git/Services/TextGeneration.ts";
31 +import type { TextGenerationShape } from "../textGeneration/TextGeneration.ts";
32 import type { ProviderAdapterError, ProviderDriverError } from "./Errors.ts";
33 import type { ProviderAdapterShape } from "./Services/ProviderAdapter.ts";
34 import type { ServerProviderShape } from "./Services/ServerProvider.ts";
apps/server/src/server.test.ts
+242 -125
@@ -29,6 +29,7 @@ import { assert, it } from "@effect/vitest";
29 import { assertFailure, assertInclude, assertTrue } from "@effect/vitest/utils";
30 import {
31 Deferred,
32 + DateTime,
33 Duration,
34 Effect,
35 FileSystem,
@@ -38,6 +39,7 @@ import {
39 Path,
40 Stream,
41 } from "effect";
42 +import { ChildProcessSpawner } from "effect/unstable/process";
43 import {
44 FetchHttpClient,
45 HttpBody,
@@ -50,6 +52,8 @@ import { RpcClient, RpcSerialization } from "effect/unstable/rpc";
52 import * as Socket from "effect/unstable/socket/Socket";
53 import { vi } from "vitest";
54
55 +const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
56 +
57 import type { ServerConfigShape } from "./config.ts";
58 import { deriveServerPaths, ServerConfig } from "./config.ts";
59 import { makeRoutesLayer } from "./server.ts";
@@ -58,13 +62,7 @@ import {
62 CheckpointDiffQuery,
63 type CheckpointDiffQueryShape,
64 } from "./checkpointing/Services/CheckpointDiffQuery.ts";
61 -import { GitCore, type GitCoreShape } from "./git/Services/GitCore.ts";
62 -import { GitManager, type GitManagerShape } from "./git/Services/GitManager.ts";
63 -import { GitStatusBroadcasterLive } from "./git/Layers/GitStatusBroadcaster.ts";
64 -import {
65 - GitStatusBroadcaster,
66 - type GitStatusBroadcasterShape,
67 -} from "./git/Services/GitStatusBroadcaster.ts";
65 +import { GitManager, type GitManagerShape } from "./git/GitManager.ts";
66 import { Keybindings, type KeybindingsShape } from "./keybindings.ts";
67 import { Open, type OpenShape } from "./open.ts";
68 import {
@@ -105,6 +103,20 @@ import {
103 import { WorkspaceEntriesLive } from "./workspace/Layers/WorkspaceEntries.ts";
104 import { WorkspaceFileSystemLive } from "./workspace/Layers/WorkspaceFileSystem.ts";
105 import { WorkspacePathsLive } from "./workspace/Layers/WorkspacePaths.ts";
106 +import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
107 +import type { VcsDriverShape } from "./vcs/VcsDriver.ts";
108 +import {
109 + VcsStatusBroadcaster,
110 + type VcsStatusBroadcasterShape,
111 + layer as VcsStatusBroadcasterLayer,
112 +} from "./vcs/VcsStatusBroadcaster.ts";
113 +import {
114 + VcsDriverRegistry,
115 + type VcsDriverRegistryShape,
116 + type VcsDriverHandle,
117 +} from "./vcs/VcsDriverRegistry.ts";
118 +import { layer as VcsProvisioningServiceLayer } from "./vcs/VcsProvisioningService.ts";
119 +import { layer as GitWorkflowServiceLayer } from "./git/GitWorkflowService.ts";
120 import { ServerSecretStoreLive } from "./auth/Layers/ServerSecretStore.ts";
121 import { ServerAuthLive } from "./auth/Layers/ServerAuth.ts";
122
@@ -195,16 +207,6 @@ const makeDefaultOrchestrationThreadShell = (
207 };
208 };
209
198 -const workspaceAndProjectServicesLayer = Layer.mergeAll(
199 - WorkspacePathsLive,
200 - WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive)),
201 - WorkspaceFileSystemLive.pipe(
202 - Layer.provide(WorkspacePathsLive),
203 - Layer.provide(WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive))),
204 - ),
205 - ProjectFaviconResolverLive,
206 -);
207 -
210 const browserOtlpTracingLayer = Layer.mergeAll(
211 FetchHttpClient.layer,
212 OtlpSerialization.layerJson,
@@ -324,9 +326,11 @@ const buildAppUnderTest = (options?: {
326 providerRegistry?: Partial<ProviderRegistryShape>;
327 serverSettings?: Partial<ServerSettingsShape>;
328 open?: Partial<OpenShape>;
327 - gitCore?: Partial<GitCoreShape>;
329 + vcsDriver?: Partial<VcsDriverShape>;
330 + vcsDriverRegistry?: Partial<VcsDriverRegistryShape>;
331 + gitVcsDriver?: Partial<GitVcsDriver.GitVcsDriverShape>;
332 gitManager?: Partial<GitManagerShape>;
329 - gitStatusBroadcaster?: Partial<GitStatusBroadcasterShape>;
333 + vcsStatusBroadcaster?: Partial<VcsStatusBroadcasterShape>;
334 projectSetupScriptRunner?: Partial<ProjectSetupScriptRunnerShape>;
335 terminalManager?: Partial<TerminalManagerShape>;
336 orchestrationEngine?: Partial<OrchestrationEngineShape>;
@@ -372,22 +376,110 @@ const buildAppUnderTest = (options?: {
376 ...options?.config,
377 };
378 const layerConfig = Layer.succeed(ServerConfig, config);
375 - const gitCoreLayer = Layer.mock(GitCore)({
379 + const defaultVcsDriver: VcsDriverShape = {
380 + capabilities: {
381 + kind: "git",
382 + supportsWorktrees: true,
383 + supportsBookmarks: false,
384 + supportsAtomicSnapshot: false,
385 + supportsPushDefaultRemote: true,
386 + ignoreClassifier: "native",
387 + },
388 + execute: () =>
389 + Effect.succeed({
390 + exitCode: ChildProcessSpawner.ExitCode(0),
391 + stdout: "",
392 + stderr: "",
393 + stdoutTruncated: false,
394 + stderrTruncated: false,
395 + }),
396 + detectRepository: () => Effect.succeed(null),
397 isInsideWorkTree: () => Effect.succeed(false),
398 listWorkspaceFiles: () =>
399 Effect.succeed({
400 paths: [],
401 truncated: false,
402 + freshness: {
403 + source: "live-local",
404 + observedAt: TEST_EPOCH,
405 + expiresAt: Option.none(),
406 + },
407 + }),
408 + listRemotes: () =>
409 + Effect.succeed({
410 + remotes: [],
411 + freshness: {
412 + source: "live-local",
413 + observedAt: TEST_EPOCH,
414 + expiresAt: Option.none(),
415 + },
416 }),
417 filterIgnoredPaths: (_cwd, relativePaths) => Effect.succeed(relativePaths),
383 - ...options?.layers?.gitCore,
418 + initRepository: () => Effect.void,
419 + ...options?.layers?.vcsDriver,
420 + };
421 + const vcsDriverRegistryLayer = Layer.mock(VcsDriverRegistry)({
422 + get: () => Effect.succeed(defaultVcsDriver),
423 + detect: (input) =>
424 + defaultVcsDriver.detectRepository(input.cwd).pipe(
425 + Effect.flatMap((repository) =>
426 + repository
427 + ? Effect.succeed(repository)
428 + : defaultVcsDriver.isInsideWorkTree(input.cwd).pipe(
429 + Effect.map((isInsideWorkTree) =>
430 + isInsideWorkTree
431 + ? {
432 + kind: "git" as const,
433 + rootPath: input.cwd,
434 + metadataPath: null,
435 + freshness: {
436 + source: "live-local" as const,
437 + observedAt: TEST_EPOCH,
438 + expiresAt: Option.none(),
439 + },
440 + }
441 + : null,
442 + ),
443 + ),
444 + ),
445 + Effect.map((repository) =>
446 + repository
447 + ? ({
448 + kind: repository.kind,
449 + repository,
450 + driver: defaultVcsDriver,
451 + } satisfies VcsDriverHandle)
452 + : null,
453 + ),
454 + ),
455 + resolve: (input) =>
456 + Effect.succeed({
457 + kind:
458 + input.requestedKind === "auto" || !input.requestedKind ? "git" : input.requestedKind,
459 + repository: {
460 + kind:
461 + input.requestedKind === "auto" || !input.requestedKind ? "git" : input.requestedKind,
462 + rootPath: input.cwd,
463 + metadataPath: null,
464 + freshness: {
465 + source: "live-local",
466 + observedAt: TEST_EPOCH,
467 + expiresAt: Option.none(),
468 + },
469 + },
470 + driver: defaultVcsDriver,
471 + }),
472 + ...options?.layers?.vcsDriverRegistry,
473 + });
474 + const gitVcsDriverLayer = Layer.mock(GitVcsDriver.GitVcsDriver)({
475 + ...options?.layers?.gitVcsDriver,
476 });
477 const gitManagerLayer = Layer.mock(GitManager)({
478 ...options?.layers?.gitManager,
479 });
480 const workspaceEntriesLayer = WorkspaceEntriesLive.pipe(
481 Layer.provide(WorkspacePathsLive),
390 - Layer.provideMerge(gitCoreLayer),
482 + Layer.provideMerge(vcsDriverRegistryLayer),
483 );
484 const workspaceAndProjectServicesLayer = Layer.mergeAll(
485 WorkspacePathsLive,
@@ -398,11 +490,19 @@ const buildAppUnderTest = (options?: {
490 ),
491 ProjectFaviconResolverLive,
492 );
401 - const gitStatusBroadcasterLayer = options?.layers?.gitStatusBroadcaster
402 - ? Layer.mock(GitStatusBroadcaster)({
403 - ...options.layers.gitStatusBroadcaster,
493 + const gitWorkflowLayer = GitWorkflowServiceLayer.pipe(
494 + Layer.provideMerge(vcsDriverRegistryLayer),
495 + Layer.provideMerge(gitVcsDriverLayer),
496 + Layer.provideMerge(gitManagerLayer),
497 + );
498 + const vcsProvisioningLayer = VcsProvisioningServiceLayer.pipe(
499 + Layer.provide(vcsDriverRegistryLayer),
500 + );
501 + const vcsStatusBroadcasterLayer = options?.layers?.vcsStatusBroadcaster
502 + ? Layer.mock(VcsStatusBroadcaster)({
503 + ...options.layers.vcsStatusBroadcaster,
504 })
405 - : GitStatusBroadcasterLive.pipe(Layer.provide(gitManagerLayer));
505 + : VcsStatusBroadcasterLayer.pipe(Layer.provide(gitWorkflowLayer));
506
507 const servedRoutesLayer = HttpRouter.serve(makeRoutesLayer, {
508 disableListenLog: true,
@@ -441,9 +541,11 @@ const buildAppUnderTest = (options?: {
541 ...options?.layers?.open,
542 }),
543 ),
444 - Layer.provide(gitCoreLayer),
544 Layer.provide(gitManagerLayer),
446 - Layer.provideMerge(gitStatusBroadcasterLayer),
545 + Layer.provide(gitVcsDriverLayer),
546 + Layer.provide(gitWorkflowLayer),
547 + Layer.provide(vcsProvisioningLayer),
548 + Layer.provideMerge(vcsStatusBroadcasterLayer),
549 Layer.provide(
550 Layer.mock(ProjectSetupScriptRunner)({
551 runForThread: () => Effect.succeed({ status: "no-script" as const }),
@@ -2068,12 +2170,17 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2170
2171 yield* buildAppUnderTest({
2172 layers: {
2071 - gitCore: {
2173 + vcsDriver: {
2174 isInsideWorkTree: () => Effect.succeed(true),
2175 listWorkspaceFiles: () =>
2176 Effect.succeed({
2177 paths: ["src/tracked.ts"],
2178 truncated: false,
2179 + freshness: {
2180 + source: "live-local",
2181 + observedAt: TEST_EPOCH,
2182 + expiresAt: Option.none(),
2183 + },
2184 }),
2185 filterIgnoredPaths: (_cwd, relativePaths) =>
2186 Effect.succeed(
@@ -2273,9 +2380,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2380 localStatus: () =>
2381 Effect.succeed({
2382 isRepo: true,
2276 - hasOriginRemote: true,
2277 - isDefaultBranch: true,
2278 - branch: "main",
2383 + hasPrimaryRemote: true,
2384 + isDefaultRef: true,
2385 + refName: "main",
2386 hasWorkingTreeChanges: false,
2387 workingTree: { files: [], insertions: 0, deletions: 0 },
2388 }),
@@ -2289,9 +2396,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2396 status: () =>
2397 Effect.succeed({
2398 isRepo: true,
2292 - hasOriginRemote: true,
2293 - isDefaultBranch: true,
2294 - branch: "main",
2399 + hasPrimaryRemote: true,
2400 + isDefaultRef: true,
2401 + refName: "main",
2402 hasWorkingTreeChanges: false,
2403 workingTree: { files: [], insertions: 0, deletions: 0 },
2404 hasUpstream: true,
@@ -2372,16 +2479,16 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2479 worktreePath: null,
2480 }),
2481 },
2375 - gitCore: {
2482 + gitVcsDriver: {
2483 pullCurrentBranch: () =>
2484 Effect.succeed({
2485 status: "pulled",
2379 - branch: "main",
2380 - upstreamBranch: "origin/main",
2486 + refName: "main",
2487 + upstreamRef: "origin/main",
2488 }),
2382 - listBranches: () =>
2489 + listRefs: () =>
2490 Effect.succeed({
2384 - branches: [
2491 + refs: [
2492 {
2493 name: "main",
2494 current: true,
@@ -2390,18 +2497,20 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2497 },
2498 ],
2499 isRepo: true,
2393 - hasOriginRemote: true,
2500 + hasPrimaryRemote: true,
2501 nextCursor: null,
2502 totalCount: 1,
2503 }),
2504 createWorktree: () =>
2505 Effect.succeed({
2399 - worktree: { path: "/tmp/wt", branch: "feature/demo" },
2506 + worktree: { path: "/tmp/wt", refName: "feature/demo" },
2507 }),
2508 removeWorktree: () => Effect.void,
2402 - createBranch: (input) => Effect.succeed({ branch: input.branch }),
2403 - checkoutBranch: (input) => Effect.succeed({ branch: input.branch }),
2404 - initRepo: () => Effect.void,
2509 + createRef: (input) => Effect.succeed({ refName: input.refName }),
2510 + switchRef: (input) => Effect.succeed({ refName: input.refName }),
2511 + },
2512 + vcsDriver: {
2513 + isInsideWorkTree: () => Effect.succeed(true),
2514 },
2515 },
2516 });
@@ -2409,13 +2518,13 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2518 const wsUrl = yield* getWsServerUrl("/ws");
2519
2520 const pull = yield* Effect.scoped(
2412 - withWsRpcClient(wsUrl, (client) => client[WS_METHODS.gitPull]({ cwd: "/tmp/repo" })),
2521 + withWsRpcClient(wsUrl, (client) => client[WS_METHODS.vcsPull]({ cwd: "/tmp/repo" })),
2522 );
2523 assert.equal(pull.status, "pulled");
2524
2525 const refreshedStatus = yield* Effect.scoped(
2526 withWsRpcClient(wsUrl, (client) =>
2418 - client[WS_METHODS.gitRefreshStatus]({ cwd: "/tmp/repo" }),
2527 + client[WS_METHODS.vcsRefreshStatus]({ cwd: "/tmp/repo" }),
2528 ),
2529 );
2530 assert.equal(refreshedStatus.isRepo, true);
@@ -2459,27 +2568,25 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2568 );
2569 assert.equal(prepared.branch, "feature/demo");
2570
2462 - const branches = yield* Effect.scoped(
2463 - withWsRpcClient(wsUrl, (client) =>
2464 - client[WS_METHODS.gitListBranches]({ cwd: "/tmp/repo" }),
2465 - ),
2571 + const refs = yield* Effect.scoped(
2572 + withWsRpcClient(wsUrl, (client) => client[WS_METHODS.vcsListRefs]({ cwd: "/tmp/repo" })),
2573 );
2467 - assert.equal(branches.branches[0]?.name, "main");
2574 + assert.equal(refs.refs[0]?.name, "main");
2575
2576 const worktree = yield* Effect.scoped(
2577 withWsRpcClient(wsUrl, (client) =>
2471 - client[WS_METHODS.gitCreateWorktree]({
2578 + client[WS_METHODS.vcsCreateWorktree]({
2579 cwd: "/tmp/repo",
2473 - branch: "main",
2580 + refName: "main",
2581 path: null,
2582 }),
2583 ),
2584 );
2478 - assert.equal(worktree.worktree.branch, "feature/demo");
2585 + assert.equal(worktree.worktree.refName, "feature/demo");
2586
2587 yield* Effect.scoped(
2588 withWsRpcClient(wsUrl, (client) =>
2482 - client[WS_METHODS.gitRemoveWorktree]({
2589 + client[WS_METHODS.vcsRemoveWorktree]({
2590 cwd: "/tmp/repo",
2591 path: "/tmp/wt",
2592 }),
@@ -2488,25 +2595,25 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2595
2596 yield* Effect.scoped(
2597 withWsRpcClient(wsUrl, (client) =>
2491 - client[WS_METHODS.gitCreateBranch]({
2598 + client[WS_METHODS.vcsCreateRef]({
2599 cwd: "/tmp/repo",
2493 - branch: "feature/new",
2600 + refName: "feature/new",
2601 }),
2602 ),
2603 );
2604
2605 yield* Effect.scoped(
2606 withWsRpcClient(wsUrl, (client) =>
2500 - client[WS_METHODS.gitCheckout]({
2607 + client[WS_METHODS.vcsSwitchRef]({
2608 cwd: "/tmp/repo",
2502 - branch: "main",
2609 + refName: "main",
2610 }),
2611 ),
2612 );
2613
2614 yield* Effect.scoped(
2615 withWsRpcClient(wsUrl, (client) =>
2509 - client[WS_METHODS.gitInit]({
2616 + client[WS_METHODS.vcsInit]({
2617 cwd: "/tmp/repo",
2618 }),
2619 ),
@@ -2526,7 +2633,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2633 let statusCalls = 0;
2634 yield* buildAppUnderTest({
2635 layers: {
2529 - gitCore: {
2636 + gitVcsDriver: {
2637 pullCurrentBranch: () => Effect.fail(gitError),
2638 },
2639 gitManager: {
@@ -2545,9 +2652,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2652 localStatus: () =>
2653 Effect.succeed({
2654 isRepo: true,
2548 - hasOriginRemote: true,
2549 - isDefaultBranch: true,
2550 - branch: "main",
2655 + hasPrimaryRemote: true,
2656 + isDefaultRef: true,
2657 + refName: "main",
2658 hasWorkingTreeChanges: true,
2659 workingTree: { files: [], insertions: 0, deletions: 0 },
2660 }),
@@ -2566,9 +2673,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2673 statusCalls += 1;
2674 return {
2675 isRepo: true,
2569 - hasOriginRemote: true,
2570 - isDefaultBranch: true,
2571 - branch: "main",
2676 + hasPrimaryRemote: true,
2677 + isDefaultRef: true,
2678 + refName: "main",
2679 hasWorkingTreeChanges: true,
2680 workingTree: { files: [], insertions: 0, deletions: 0 },
2681 hasUpstream: true,
@@ -2583,7 +2690,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2690
2691 const wsUrl = yield* getWsServerUrl("/ws");
2692 const result = yield* Effect.scoped(
2586 - withWsRpcClient(wsUrl, (client) => client[WS_METHODS.gitPull]({ cwd: "/tmp/repo" })).pipe(
2693 + withWsRpcClient(wsUrl, (client) => client[WS_METHODS.vcsPull]({ cwd: "/tmp/repo" })).pipe(
2694 Effect.result,
2695 ),
2696 );
@@ -2622,9 +2729,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2729 localStatus: () =>
2730 Effect.succeed({
2731 isRepo: true,
2625 - hasOriginRemote: true,
2626 - isDefaultBranch: false,
2627 - branch: "feature/demo",
2732 + hasPrimaryRemote: true,
2733 + isDefaultRef: false,
2734 + refName: "feature/demo",
2735 hasWorkingTreeChanges: true,
2736 workingTree: { files: [], insertions: 0, deletions: 0 },
2737 }),
@@ -2643,9 +2750,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2750 statusCalls += 1;
2751 return {
2752 isRepo: true,
2646 - hasOriginRemote: true,
2647 - isDefaultBranch: false,
2648 - branch: "feature/demo",
2753 + hasPrimaryRemote: true,
2754 + isDefaultRef: false,
2755 + refName: "feature/demo",
2756 hasWorkingTreeChanges: true,
2757 workingTree: { files: [], insertions: 0, deletions: 0 },
2758 hasUpstream: true,
@@ -2680,12 +2787,12 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2787 Effect.gen(function* () {
2788 yield* buildAppUnderTest({
2789 layers: {
2683 - gitCore: {
2790 + gitVcsDriver: {
2791 pullCurrentBranch: () =>
2792 Effect.succeed({
2793 status: "pulled" as const,
2687 - branch: "main",
2688 - upstreamBranch: "origin/main",
2794 + refName: "main",
2795 + upstreamRef: "origin/main",
2796 }),
2797 },
2798 gitManager: {
@@ -2694,9 +2801,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2801 localStatus: () =>
2802 Effect.succeed({
2803 isRepo: true,
2697 - hasOriginRemote: true,
2698 - isDefaultBranch: true,
2699 - branch: "main",
2804 + hasPrimaryRemote: true,
2805 + isDefaultRef: true,
2806 + refName: "main",
2807 hasWorkingTreeChanges: false,
2808 workingTree: { files: [], insertions: 0, deletions: 0 },
2809 }),
@@ -2716,7 +2823,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2823 const wsUrl = yield* getWsServerUrl("/ws");
2824 const startedAt = Date.now();
2825 const result = yield* Effect.scoped(
2719 - withWsRpcClient(wsUrl, (client) => client[WS_METHODS.gitPull]({ cwd: "/tmp/repo" })),
2826 + withWsRpcClient(wsUrl, (client) => client[WS_METHODS.vcsPull]({ cwd: "/tmp/repo" })),
2827 );
2828 const elapsedMs = Date.now() - startedAt;
2829
@@ -2731,15 +2838,18 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2838 Effect.gen(function* () {
2839 yield* buildAppUnderTest({
2840 layers: {
2841 + vcsDriver: {
2842 + isInsideWorkTree: () => Effect.succeed(true),
2843 + },
2844 gitManager: {
2845 invalidateLocalStatus: () => Effect.void,
2846 invalidateRemoteStatus: () => Effect.void,
2847 localStatus: () =>
2848 Effect.succeed({
2849 isRepo: true,
2740 - hasOriginRemote: true,
2741 - isDefaultBranch: false,
2742 - branch: "feature/demo",
2850 + hasPrimaryRemote: true,
2851 + isDefaultRef: false,
2852 + refName: "feature/demo",
2853 hasWorkingTreeChanges: false,
2854 workingTree: { files: [], insertions: 0, deletions: 0 },
2855 }),
@@ -2804,6 +2914,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2914
2915 yield* buildAppUnderTest({
2916 layers: {
2917 + vcsDriver: {
2918 + isInsideWorkTree: () => Effect.succeed(true),
2919 + },
2920 gitManager: {
2921 invalidateLocalStatus: () => Effect.void,
2922 invalidateRemoteStatus: () => Effect.void,
@@ -2813,9 +2926,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
2926 Effect.andThen(
2927 Effect.succeed({
2928 isRepo: true,
2816 - hasOriginRemote: true,
2817 - isDefaultBranch: false,
2818 - branch: "feature/demo",
2929 + hasPrimaryRemote: true,
2930 + isDefaultRef: false,
2931 + refName: "feature/demo",
2932 hasWorkingTreeChanges: false,
2933 workingTree: { files: [], insertions: 0, deletions: 0 },
2934 }),
@@ -3479,9 +3592,9 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3592 const refreshStatus = vi.fn((_: string) =>
3593 Effect.succeed({
3594 isRepo: true,
3482 - hasOriginRemote: true,
3483 - isDefaultBranch: false,
3484 - branch: "t3code/bootstrap-branch",
3595 + hasPrimaryRemote: true,
3596 + isDefaultRef: false,
3597 + refName: "t3code/bootstrap-refName",
3598 hasWorkingTreeChanges: false,
3599 workingTree: {
3600 files: [],
@@ -3494,13 +3607,14 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3607 pr: null,
3608 }),
3609 );
3497 - const createWorktree = vi.fn((_: Parameters<GitCoreShape["createWorktree"]>[0]) =>
3498 - Effect.succeed({
3499 - worktree: {
3500 - branch: "t3code/bootstrap-branch",
3501 - path: "/tmp/bootstrap-worktree",
3502 - },
3503 - }),
3610 + const createWorktree = vi.fn(
3611 + (_: Parameters<GitVcsDriver.GitVcsDriverShape["createWorktree"]>[0]) =>
3612 + Effect.succeed({
3613 + worktree: {
3614 + refName: "t3code/bootstrap-refName",
3615 + path: "/tmp/bootstrap-worktree",
3616 + },
3617 + }),
3618 );
3619 const runForThread = vi.fn(
3620 (_: Parameters<ProjectSetupScriptRunnerShape["runForThread"]>[0]) =>
@@ -3515,10 +3629,10 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3629
3630 yield* buildAppUnderTest({
3631 layers: {
3518 - gitCore: {
3632 + gitVcsDriver: {
3633 createWorktree,
3634 },
3521 - gitStatusBroadcaster: {
3635 + vcsStatusBroadcaster: {
3636 refreshStatus,
3637 },
3638 orchestrationEngine: {
@@ -3566,7 +3680,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3680 prepareWorktree: {
3681 projectCwd: "/tmp/project",
3682 baseBranch: "main",
3569 - branch: "t3code/bootstrap-branch",
3683 + branch: "t3code/bootstrap-refName",
3684 },
3685 runSetupScript: true,
3686 },
@@ -3588,8 +3702,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3702 );
3703 assert.deepEqual(createWorktree.mock.calls[0]?.[0], {
3704 cwd: "/tmp/project",
3591 - branch: "main",
3592 - newBranch: "t3code/bootstrap-branch",
3705 + refName: "main",
3706 + newRefName: "t3code/bootstrap-refName",
3707 path: null,
3708 });
3709 assert.deepEqual(runForThread.mock.calls[0]?.[0], {
@@ -3619,13 +3733,14 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3733 it.effect("records setup-script failures without aborting bootstrap turn start", () =>
3734 Effect.gen(function* () {
3735 const dispatchedCommands: Array<OrchestrationCommand> = [];
3622 - const createWorktree = vi.fn((_: Parameters<GitCoreShape["createWorktree"]>[0]) =>
3623 - Effect.succeed({
3624 - worktree: {
3625 - branch: "t3code/bootstrap-branch",
3626 - path: "/tmp/bootstrap-worktree",
3627 - },
3628 - }),
3736 + const createWorktree = vi.fn(
3737 + (_: Parameters<GitVcsDriver.GitVcsDriverShape["createWorktree"]>[0]) =>
3738 + Effect.succeed({
3739 + worktree: {
3740 + refName: "t3code/bootstrap-refName",
3741 + path: "/tmp/bootstrap-worktree",
3742 + },
3743 + }),
3744 );
3745 const runForThread = vi.fn(
3746 (_: Parameters<ProjectSetupScriptRunnerShape["runForThread"]>[0]) =>
@@ -3634,7 +3749,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3749
3750 yield* buildAppUnderTest({
3751 layers: {
3637 - gitCore: {
3752 + gitVcsDriver: {
3753 createWorktree,
3754 },
3755 orchestrationEngine: {
@@ -3682,7 +3797,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3797 prepareWorktree: {
3798 projectCwd: "/tmp/project",
3799 baseBranch: "main",
3685 - branch: "t3code/bootstrap-branch",
3800 + branch: "t3code/bootstrap-refName",
3801 },
3802 runSetupScript: true,
3803 },
@@ -3712,13 +3827,14 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3827 it.effect("does not misattribute setup activity dispatch failures as setup launch failures", () =>
3828 Effect.gen(function* () {
3829 const dispatchedCommands: Array<OrchestrationCommand> = [];
3715 - const createWorktree = vi.fn((_: Parameters<GitCoreShape["createWorktree"]>[0]) =>
3716 - Effect.succeed({
3717 - worktree: {
3718 - branch: "t3code/bootstrap-branch",
3719 - path: "/tmp/bootstrap-worktree",
3720 - },
3721 - }),
3830 + const createWorktree = vi.fn(
3831 + (_: Parameters<GitVcsDriver.GitVcsDriverShape["createWorktree"]>[0]) =>
3832 + Effect.succeed({
3833 + worktree: {
3834 + refName: "t3code/bootstrap-refName",
3835 + path: "/tmp/bootstrap-worktree",
3836 + },
3837 + }),
3838 );
3839 const runForThread = vi.fn(
3840 (_: Parameters<ProjectSetupScriptRunnerShape["runForThread"]>[0]) =>
@@ -3734,7 +3850,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3850
3851 yield* buildAppUnderTest({
3852 layers: {
3737 - gitCore: {
3853 + gitVcsDriver: {
3854 createWorktree,
3855 },
3856 orchestrationEngine: {
@@ -3798,7 +3914,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3914 prepareWorktree: {
3915 projectCwd: "/tmp/project",
3916 baseBranch: "main",
3801 - branch: "t3code/bootstrap-branch",
3917 + branch: "t3code/bootstrap-refName",
3918 },
3919 runSetupScript: true,
3920 },
@@ -3830,13 +3946,14 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3946 it.effect("cleans up created bootstrap threads when worktree creation defects", () =>
3947 Effect.gen(function* () {
3948 const dispatchedCommands: Array<OrchestrationCommand> = [];
3833 - const createWorktree = vi.fn((_: Parameters<GitCoreShape["createWorktree"]>[0]) =>
3834 - Effect.die(new Error("worktree exploded")),
3949 + const createWorktree = vi.fn(
3950 + (_: Parameters<GitVcsDriver.GitVcsDriverShape["createWorktree"]>[0]) =>
3951 + Effect.die(new Error("worktree exploded")),
3952 );
3953
3954 yield* buildAppUnderTest({
3955 layers: {
3839 - gitCore: {
3956 + gitVcsDriver: {
3957 createWorktree,
3958 },
3959 orchestrationEngine: {
@@ -3881,7 +3998,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
3998 prepareWorktree: {
3999 projectCwd: "/tmp/project",
4000 baseBranch: "main",
3884 - branch: "t3code/bootstrap-branch",
4001 + branch: "t3code/bootstrap-refName",
4002 },
4003 runSetupScript: false,
4004 },
apps/server/src/server.ts
+49 -18
@@ -25,13 +25,11 @@ import { ProviderSessionReaperLive } from "./provider/Layers/ProviderSessionReap
25 import { OpenCodeRuntimeLive } from "./provider/opencodeRuntime.ts";
26 import { CheckpointDiffQueryLive } from "./checkpointing/Layers/CheckpointDiffQuery.ts";
27 import { CheckpointStoreLive } from "./checkpointing/Layers/CheckpointStore.ts";
28 -import { GitCoreLive } from "./git/Layers/GitCore.ts";
29 -import { GitHubCliLive } from "./git/Layers/GitHubCli.ts";
30 -import { GitStatusBroadcasterLive } from "./git/Layers/GitStatusBroadcaster.ts";
31 -import { TextGenerationLive } from "./git/Layers/TextGenerationLive.ts";
28 +import * as GitHubCli from "./sourceControl/GitHubCli.ts";
29 +import * as TextGeneration from "./textGeneration/TextGeneration.ts";
30 import { ProviderInstanceRegistryHydrationLive } from "./provider/Layers/ProviderInstanceRegistryHydration.ts";
31 import { TerminalManagerLive } from "./terminal/Layers/Manager.ts";
34 -import { GitManagerLive } from "./git/Layers/GitManager.ts";
32 +import * as GitManager from "./git/GitManager.ts";
33 import { KeybindingsLive } from "./keybindings.ts";
34 import { ServerRuntimeStartup, ServerRuntimeStartupLive } from "./serverRuntimeStartup.ts";
35 import { OrchestrationReactorLive } from "./orchestration/Layers/OrchestrationReactor.ts";
@@ -47,6 +45,14 @@ import { RepositoryIdentityResolverLive } from "./project/Layers/RepositoryIdent
45 import { WorkspaceEntriesLive } from "./workspace/Layers/WorkspaceEntries.ts";
46 import { WorkspaceFileSystemLive } from "./workspace/Layers/WorkspaceFileSystem.ts";
47 import { WorkspacePathsLive } from "./workspace/Layers/WorkspacePaths.ts";
48 +import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
49 +import * as VcsDriverRegistry from "./vcs/VcsDriverRegistry.ts";
50 +import * as VcsProjectConfig from "./vcs/VcsProjectConfig.ts";
51 +import * as VcsProcess from "./vcs/VcsProcess.ts";
52 +import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts";
53 +import * as VcsStatusBroadcaster from "./vcs/VcsStatusBroadcaster.ts";
54 +import * as GitWorkflowService from "./git/GitWorkflowService.ts";
55 +import * as SourceControlProviderRegistry from "./sourceControl/SourceControlProviderRegistry.ts";
56 import { ProjectSetupScriptRunnerLive } from "./project/Layers/ProjectSetupScriptRunner.ts";
57 import { ObservabilityLive } from "./observability/Layers/Observability.ts";
58 import { ServerEnvironmentLive } from "./environment/Layers/ServerEnvironment.ts";
@@ -133,11 +139,6 @@ const ReactorLayerLive = Layer.empty.pipe(
139 Layer.provideMerge(RuntimeReceiptBusLive),
140 );
141
136 -const CheckpointingLayerLive = Layer.empty.pipe(
137 - Layer.provideMerge(CheckpointDiffQueryLive),
138 - Layer.provideMerge(CheckpointStoreLive),
139 -);
140 -
142 const ProviderSessionDirectoryLayerLive = ProviderSessionDirectoryLive.pipe(
143 Layer.provide(ProviderSessionRuntimeRepositoryLive),
144 );
@@ -155,24 +156,50 @@ const ProviderLayerLive = ProviderServiceLive.pipe(
156
157 const PersistenceLayerLive = Layer.empty.pipe(Layer.provideMerge(SqlitePersistenceLayerLive));
158
158 -const GitManagerLayerLive = GitManagerLive.pipe(
159 +const VcsDriverRegistryLayerLive = VcsDriverRegistry.layer.pipe(
160 + Layer.provide(VcsProjectConfig.layer),
161 +);
162 +
163 +const GitManagerLayerLive = GitManager.layer.pipe(
164 Layer.provideMerge(ProjectSetupScriptRunnerLive),
160 - Layer.provideMerge(GitCoreLive),
161 - Layer.provideMerge(GitHubCliLive),
162 - Layer.provideMerge(TextGenerationLive),
165 + Layer.provideMerge(GitVcsDriver.layer),
166 + Layer.provideMerge(
167 + SourceControlProviderRegistry.layer.pipe(
168 + Layer.provide(GitHubCli.layer),
169 + Layer.provideMerge(VcsDriverRegistryLayerLive),
170 + ),
171 + ),
172 + Layer.provideMerge(TextGeneration.layer),
173 );
174
175 const GitLayerLive = Layer.empty.pipe(
176 Layer.provideMerge(GitManagerLayerLive),
167 - Layer.provideMerge(GitStatusBroadcasterLive.pipe(Layer.provide(GitManagerLayerLive))),
168 - Layer.provideMerge(GitCoreLive),
177 + Layer.provideMerge(GitVcsDriver.layer),
178 +);
179 +
180 +const GitWorkflowLayerLive = GitWorkflowService.layer.pipe(
181 + Layer.provideMerge(VcsDriverRegistryLayerLive),
182 + Layer.provideMerge(GitLayerLive),
183 +);
184 +
185 +const VcsLayerLive = Layer.empty.pipe(
186 + Layer.provideMerge(VcsProjectConfig.layer),
187 + Layer.provideMerge(VcsDriverRegistryLayerLive),
188 + Layer.provideMerge(VcsProvisioningService.layer.pipe(Layer.provide(VcsDriverRegistryLayerLive))),
189 + Layer.provideMerge(GitWorkflowLayerLive),
190 + Layer.provideMerge(VcsStatusBroadcaster.layer.pipe(Layer.provide(GitWorkflowLayerLive))),
191 +);
192 +
193 +const CheckpointingLayerLive = Layer.empty.pipe(
194 + Layer.provideMerge(CheckpointDiffQueryLive),
195 + Layer.provideMerge(CheckpointStoreLive.pipe(Layer.provide(VcsDriverRegistryLayerLive))),
196 );
197
198 const TerminalLayerLive = TerminalManagerLive.pipe(Layer.provide(PtyAdapterLive));
199
200 const WorkspaceEntriesLayerLive = WorkspaceEntriesLive.pipe(
201 Layer.provide(WorkspacePathsLive),
175 - Layer.provideMerge(GitCoreLive),
202 + Layer.provideMerge(VcsDriverRegistryLayerLive),
203 );
204
205 const WorkspaceFileSystemLayerLive = WorkspaceFileSystemLive.pipe(
@@ -196,10 +223,11 @@ const ProviderRuntimeLayerLive = ProviderSessionReaperLive.pipe(
223 Layer.provideMerge(OrchestrationLayerLive),
224 );
225
199 -const RuntimeDependenciesLive = ReactorLayerLive.pipe(
226 +const RuntimeCoreDependenciesLive = ReactorLayerLive.pipe(
227 // Core Services
228 Layer.provideMerge(CheckpointingLayerLive),
229 Layer.provideMerge(GitLayerLive),
230 + Layer.provideMerge(VcsLayerLive),
231 Layer.provideMerge(ProviderRuntimeLayerLive),
232 Layer.provideMerge(TerminalLayerLive),
233 Layer.provideMerge(PersistenceLayerLive),
@@ -229,7 +257,9 @@ const RuntimeDependenciesLive = ReactorLayerLive.pipe(
257 Layer.provideMerge(RepositoryIdentityResolverLive),
258 Layer.provideMerge(ServerEnvironmentLive),
259 Layer.provideMerge(AuthLayerLive),
260 +);
261
262 +const RuntimeDependenciesLive = RuntimeCoreDependenciesLive.pipe(
263 // Misc.
264 Layer.provideMerge(AnalyticsServiceLayerLive),
265 Layer.provideMerge(OpenLive),
@@ -310,6 +340,7 @@ export const makeServerLayer = Layer.unwrap(
340 Layer.provideMerge(HttpServerLive),
341 Layer.provide(ObservabilityLive),
342 Layer.provideMerge(FetchHttpClient.layer),
343 + Layer.provideMerge(VcsProcess.layer),
344 Layer.provideMerge(PlatformServicesLive),
345 );
346 }),
apps/server/src/sourceControl/GitHubCli.test.ts new
+240
@@ -0,0 +1,240 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { Effect, Layer } from "effect";
3 +import { ChildProcessSpawner } from "effect/unstable/process";
4 +import { VcsProcessExitError, type VcsError } from "@t3tools/contracts";
5 +import { afterEach, describe, expect, vi } from "vitest";
6 +
7 +import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "../vcs/VcsProcess.ts";
8 +import * as GitHubCli from "./GitHubCli.ts";
9 +
10 +const processOutput = (stdout: string): VcsProcessOutput => ({
11 + exitCode: ChildProcessSpawner.ExitCode(0),
12 + stdout,
13 + stderr: "",
14 + stdoutTruncated: false,
15 + stderrTruncated: false,
16 +});
17 +
18 +const mockRun = vi.fn<(input: VcsProcessInput) => Effect.Effect<VcsProcessOutput, VcsError>>();
19 +
20 +const layer = GitHubCli.layer.pipe(
21 + Layer.provide(
22 + Layer.mock(VcsProcess)({
23 + run: mockRun,
24 + }),
25 + ),
26 +);
27 +
28 +afterEach(() => {
29 + mockRun.mockReset();
30 +});
31 +
32 +describe("GitHubCli.layer", () => {
33 + it.effect("parses pull request view output", () =>
34 + Effect.gen(function* () {
35 + mockRun.mockReturnValueOnce(
36 + Effect.succeed(
37 + processOutput(
38 + JSON.stringify({
39 + number: 42,
40 + title: "Add PR thread creation",
41 + url: "https://github.com/pingdotgg/codething-mvp/pull/42",
42 + baseRefName: "main",
43 + headRefName: "feature/pr-threads",
44 + state: "OPEN",
45 + mergedAt: null,
46 + isCrossRepository: true,
47 + headRepository: {
48 + nameWithOwner: "octocat/codething-mvp",
49 + },
50 + headRepositoryOwner: {
51 + login: "octocat",
52 + },
53 + }),
54 + ),
55 + ),
56 + );
57 +
58 + const gh = yield* GitHubCli.GitHubCli;
59 + const result = yield* gh.getPullRequest({
60 + cwd: "/repo",
61 + reference: "#42",
62 + });
63 +
64 + assert.deepStrictEqual(result, {
65 + number: 42,
66 + title: "Add PR thread creation",
67 + url: "https://github.com/pingdotgg/codething-mvp/pull/42",
68 + baseRefName: "main",
69 + headRefName: "feature/pr-threads",
70 + state: "open",
71 + isCrossRepository: true,
72 + headRepositoryNameWithOwner: "octocat/codething-mvp",
73 + headRepositoryOwnerLogin: "octocat",
74 + });
75 + expect(mockRun).toHaveBeenCalledWith({
76 + operation: "GitHubCli.execute",
77 + command: "gh",
78 + args: [
79 + "pr",
80 + "view",
81 + "#42",
82 + "--json",
83 + "number,title,url,baseRefName,headRefName,state,mergedAt,isCrossRepository,headRepository,headRepositoryOwner",
84 + ],
85 + cwd: "/repo",
86 + timeoutMs: 30_000,
87 + });
88 + }).pipe(Effect.provide(layer)),
89 + );
90 +
91 + it.effect("trims pull request fields decoded from gh json", () =>
92 + Effect.gen(function* () {
93 + mockRun.mockReturnValueOnce(
94 + Effect.succeed(
95 + processOutput(
96 + JSON.stringify({
97 + number: 42,
98 + title: " Add PR thread creation \n",
99 + url: " https://github.com/pingdotgg/codething-mvp/pull/42 ",
100 + baseRefName: " main ",
101 + headRefName: "\tfeature/pr-threads\t",
102 + state: "OPEN",
103 + mergedAt: null,
104 + isCrossRepository: true,
105 + headRepository: {
106 + nameWithOwner: " octocat/codething-mvp ",
107 + },
108 + headRepositoryOwner: {
109 + login: " octocat ",
110 + },
111 + }),
112 + ),
113 + ),
114 + );
115 +
116 + const gh = yield* GitHubCli.GitHubCli;
117 + const result = yield* gh.getPullRequest({
118 + cwd: "/repo",
119 + reference: "#42",
120 + });
121 +
122 + assert.deepStrictEqual(result, {
123 + number: 42,
124 + title: "Add PR thread creation",
125 + url: "https://github.com/pingdotgg/codething-mvp/pull/42",
126 + baseRefName: "main",
127 + headRefName: "feature/pr-threads",
128 + state: "open",
129 + isCrossRepository: true,
130 + headRepositoryNameWithOwner: "octocat/codething-mvp",
131 + headRepositoryOwnerLogin: "octocat",
132 + });
133 + }).pipe(Effect.provide(layer)),
134 + );
135 +
136 + it.effect("skips invalid entries when parsing pr lists", () =>
137 + Effect.gen(function* () {
138 + mockRun.mockReturnValueOnce(
139 + Effect.succeed(
140 + processOutput(
141 + JSON.stringify([
142 + {
143 + number: 0,
144 + title: "invalid",
145 + url: "https://github.com/pingdotgg/codething-mvp/pull/0",
146 + baseRefName: "main",
147 + headRefName: "feature/invalid",
148 + },
149 + {
150 + number: 43,
151 + title: " Valid PR ",
152 + url: " https://github.com/pingdotgg/codething-mvp/pull/43 ",
153 + baseRefName: " main ",
154 + headRefName: " feature/pr-list ",
155 + headRepository: {
156 + nameWithOwner: " ",
157 + },
158 + headRepositoryOwner: {
159 + login: " ",
160 + },
161 + },
162 + ]),
163 + ),
164 + ),
165 + );
166 +
167 + const gh = yield* GitHubCli.GitHubCli;
168 + const result = yield* gh.listOpenPullRequests({
169 + cwd: "/repo",
170 + headSelector: "feature/pr-list",
171 + });
172 +
173 + assert.deepStrictEqual(result, [
174 + {
175 + number: 43,
176 + title: "Valid PR",
177 + url: "https://github.com/pingdotgg/codething-mvp/pull/43",
178 + baseRefName: "main",
179 + headRefName: "feature/pr-list",
180 + state: "open",
181 + },
182 + ]);
183 + }).pipe(Effect.provide(layer)),
184 + );
185 +
186 + it.effect("reads repository clone URLs", () =>
187 + Effect.gen(function* () {
188 + mockRun.mockReturnValueOnce(
189 + Effect.succeed(
190 + processOutput(
191 + JSON.stringify({
192 + nameWithOwner: "octocat/codething-mvp",
193 + url: "https://github.com/octocat/codething-mvp",
194 + sshUrl: "git@github.com:octocat/codething-mvp.git",
195 + }),
196 + ),
197 + ),
198 + );
199 +
200 + const gh = yield* GitHubCli.GitHubCli;
201 + const result = yield* gh.getRepositoryCloneUrls({
202 + cwd: "/repo",
203 + repository: "octocat/codething-mvp",
204 + });
205 +
206 + assert.deepStrictEqual(result, {
207 + nameWithOwner: "octocat/codething-mvp",
208 + url: "https://github.com/octocat/codething-mvp",
209 + sshUrl: "git@github.com:octocat/codething-mvp.git",
210 + });
211 + }).pipe(Effect.provide(layer)),
212 + );
213 +
214 + it.effect("surfaces a friendly error when the pull request is not found", () =>
215 + Effect.gen(function* () {
216 + mockRun.mockReturnValueOnce(
217 + Effect.fail(
218 + new VcsProcessExitError({
219 + operation: "GitHubCli.execute",
220 + command: "gh pr view",
221 + cwd: "/repo",
222 + exitCode: 1,
223 + detail:
224 + "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
225 + }),
226 + ),
227 + );
228 +
229 + const gh = yield* GitHubCli.GitHubCli;
230 + const error = yield* gh
231 + .getPullRequest({
232 + cwd: "/repo",
233 + reference: "4888",
234 + })
235 + .pipe(Effect.flip);
236 +
237 + assert.equal(error.message.includes("Pull request not found"), true);
238 + }).pipe(Effect.provide(layer)),
239 + );
240 +});
apps/server/src/sourceControl/GitHubCli.ts renamed
+130 -62
@@ -1,68 +1,135 @@
1 -import { Effect, Layer, Result, Schema, SchemaIssue } from "effect";
2 -import { TrimmedNonEmptyString } from "@t3tools/contracts";
1 +import { Context, Effect, Layer, Result, Schema, SchemaIssue } from "effect";
2
4 -import { runProcess } from "../../processRunner.ts";
5 -import { GitHubCliError } from "@t3tools/contracts";
6 -import {
7 - GitHubCli,
8 - type GitHubRepositoryCloneUrls,
9 - type GitHubCliShape,
10 -} from "../Services/GitHubCli.ts";
3 +import { GitHubCliError, TrimmedNonEmptyString, type VcsError } from "@t3tools/contracts";
4 +
5 +import { VcsProcess, type VcsProcessOutput } from "../vcs/VcsProcess.ts";
6 import {
7 decodeGitHubPullRequestJson,
8 decodeGitHubPullRequestListJson,
9 formatGitHubJsonDecodeError,
15 -} from "../githubPullRequests.ts";
10 +} from "../git/githubPullRequests.ts";
11
12 const DEFAULT_TIMEOUT_MS = 30_000;
13
19 -function normalizeGitHubCliError(operation: "execute" | "stdout", error: unknown): GitHubCliError {
20 - if (error instanceof Error) {
21 - if (error.message.includes("Command not found: gh")) {
22 - return new GitHubCliError({
23 - operation,
24 - detail: "GitHub CLI (`gh`) is required but not available on PATH.",
25 - cause: error,
26 - });
27 - }
28 -
29 - const lower = error.message.toLowerCase();
30 - if (
31 - lower.includes("authentication failed") ||
32 - lower.includes("not logged in") ||
33 - lower.includes("gh auth login") ||
34 - lower.includes("no oauth token")
35 - ) {
36 - return new GitHubCliError({
37 - operation,
38 - detail: "GitHub CLI is not authenticated. Run `gh auth login` and retry.",
39 - cause: error,
40 - });
41 - }
42 -
43 - if (
44 - lower.includes("could not resolve to a pullrequest") ||
45 - lower.includes("repository.pullrequest") ||
46 - lower.includes("no pull requests found for branch") ||
47 - lower.includes("pull request not found")
48 - ) {
49 - return new GitHubCliError({
50 - operation,
51 - detail: "Pull request not found. Check the PR number or URL and try again.",
52 - cause: error,
53 - });
54 - }
14 +export interface GitHubPullRequestSummary {
15 + readonly number: number;
16 + readonly title: string;
17 + readonly url: string;
18 + readonly baseRefName: string;
19 + readonly headRefName: string;
20 + readonly state?: "open" | "closed" | "merged";
21 + readonly isCrossRepository?: boolean;
22 + readonly headRepositoryNameWithOwner?: string | null;
23 + readonly headRepositoryOwnerLogin?: string | null;
24 +}
25 +
26 +export interface GitHubRepositoryCloneUrls {
27 + readonly nameWithOwner: string;
28 + readonly url: string;
29 + readonly sshUrl: string;
30 +}
31 +
32 +export interface GitHubCliShape {
33 + readonly execute: (input: {
34 + readonly cwd: string;
35 + readonly args: ReadonlyArray<string>;
36 + readonly timeoutMs?: number;
37 + }) => Effect.Effect<VcsProcessOutput, GitHubCliError>;
38 +
39 + readonly listOpenPullRequests: (input: {
40 + readonly cwd: string;
41 + readonly headSelector: string;
42 + readonly limit?: number;
43 + }) => Effect.Effect<ReadonlyArray<GitHubPullRequestSummary>, GitHubCliError>;
44 +
45 + readonly getPullRequest: (input: {
46 + readonly cwd: string;
47 + readonly reference: string;
48 + }) => Effect.Effect<GitHubPullRequestSummary, GitHubCliError>;
49 +
50 + readonly getRepositoryCloneUrls: (input: {
51 + readonly cwd: string;
52 + readonly repository: string;
53 + }) => Effect.Effect<GitHubRepositoryCloneUrls, GitHubCliError>;
54 +
55 + readonly createPullRequest: (input: {
56 + readonly cwd: string;
57 + readonly baseBranch: string;
58 + readonly headSelector: string;
59 + readonly title: string;
60 + readonly bodyFile: string;
61 + }) => Effect.Effect<void, GitHubCliError>;
62 +
63 + readonly getDefaultBranch: (input: {
64 + readonly cwd: string;
65 + }) => Effect.Effect<string | null, GitHubCliError>;
66 +
67 + readonly checkoutPullRequest: (input: {
68 + readonly cwd: string;
69 + readonly reference: string;
70 + readonly force?: boolean;
71 + }) => Effect.Effect<void, GitHubCliError>;
72 +}
73 +
74 +export class GitHubCli extends Context.Service<GitHubCli, GitHubCliShape>()(
75 + "t3/source-control/GitHubCli",
76 +) {}
77 +
78 +function errorText(error: VcsError | unknown): string {
79 + if (typeof error === "object" && error !== null) {
80 + const tag = "_tag" in error && typeof error._tag === "string" ? error._tag : "";
81 + const detail = "detail" in error && typeof error.detail === "string" ? error.detail : "";
82 + const message = "message" in error && typeof error.message === "string" ? error.message : "";
83 + return [tag, detail, message].filter(Boolean).join("\n");
84 + }
85 +
86 + return String(error);
87 +}
88 +
89 +function normalizeGitHubCliError(
90 + operation: "execute" | "stdout",
91 + error: VcsError | unknown,
92 +): GitHubCliError {
93 + const text = errorText(error);
94 + const lower = text.toLowerCase();
95
96 + if (lower.includes("command not found: gh") || lower.includes("enoent")) {
97 return new GitHubCliError({
98 operation,
58 - detail: `GitHub CLI command failed: ${error.message}`,
99 + detail: "GitHub CLI (`gh`) is required but not available on PATH.",
100 + cause: error,
101 + });
102 + }
103 +
104 + if (
105 + lower.includes("authentication failed") ||
106 + lower.includes("not logged in") ||
107 + lower.includes("gh auth login") ||
108 + lower.includes("no oauth token")
109 + ) {
110 + return new GitHubCliError({
111 + operation,
112 + detail: "GitHub CLI is not authenticated. Run `gh auth login` and retry.",
113 + cause: error,
114 + });
115 + }
116 +
117 + if (
118 + lower.includes("could not resolve to a pullrequest") ||
119 + lower.includes("repository.pullrequest") ||
120 + lower.includes("no pull requests found for branch") ||
121 + lower.includes("pull request not found")
122 + ) {
123 + return new GitHubCliError({
124 + operation,
125 + detail: "Pull request not found. Check the PR number or URL and try again.",
126 cause: error,
127 });
128 }
129
130 return new GitHubCliError({
131 operation,
65 - detail: "GitHub CLI command failed.",
132 + detail: text,
133 cause: error,
134 });
135 }
@@ -101,18 +168,21 @@ function decodeGitHubJson<S extends Schema.Top>(
168 );
169 }
170
104 -const makeGitHubCli = Effect.sync(() => {
171 +export const make = Effect.fn("makeGitHubCli")(function* () {
172 + const process = yield* VcsProcess;
173 +
174 const execute: GitHubCliShape["execute"] = (input) =>
106 - Effect.tryPromise({
107 - try: () =>
108 - runProcess("gh", input.args, {
109 - cwd: input.cwd,
110 - timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
111 - }),
112 - catch: (error) => normalizeGitHubCliError("execute", error),
113 - });
175 + process
176 + .run({
177 + operation: "GitHubCli.execute",
178 + command: "gh",
179 + args: input.args,
180 + cwd: input.cwd,
181 + timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
182 + })
183 + .pipe(Effect.mapError((error) => normalizeGitHubCliError("execute", error)));
184
115 - const service = {
185 + return GitHubCli.of({
186 execute,
187 listOpenPullRequests: (input) =>
188 execute({
@@ -232,9 +302,7 @@ const makeGitHubCli = Effect.sync(() => {
302 cwd: input.cwd,
303 args: ["pr", "checkout", input.reference, ...(input.force ? ["--force"] : [])],
304 }).pipe(Effect.asVoid),
235 - } satisfies GitHubCliShape;
236 -
237 - return service;
305 + });
306 });
307
240 -export const GitHubCliLive = Layer.effect(GitHubCli, makeGitHubCli);
308 +export const layer = Layer.effect(GitHubCli, make());
apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts new
+154
@@ -0,0 +1,154 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { DateTime, Effect, Layer, Option } from "effect";
3 +import { ChildProcessSpawner } from "effect/unstable/process";
4 +
5 +import { GitHubCli, type GitHubCliShape } from "./GitHubCli.ts";
6 +import type { VcsProcessOutput } from "../vcs/VcsProcess.ts";
7 +import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
8 +
9 +const processResult = (stdout: string): VcsProcessOutput => ({
10 + exitCode: ChildProcessSpawner.ExitCode(0),
11 + stdout,
12 + stderr: "",
13 + stdoutTruncated: false,
14 + stderrTruncated: false,
15 +});
16 +
17 +function makeProvider(github: Partial<GitHubCliShape>) {
18 + return GitHubSourceControlProvider.make().pipe(Effect.provide(Layer.mock(GitHubCli)(github)));
19 +}
20 +
21 +it.effect("maps GitHub PR summaries into provider-neutral change requests", () =>
22 + Effect.gen(function* () {
23 + const provider = yield* makeProvider({
24 + getPullRequest: () =>
25 + Effect.succeed({
26 + number: 42,
27 + title: "Add GitHub provider",
28 + url: "https://github.com/pingdotgg/t3code/pull/42",
29 + baseRefName: "main",
30 + headRefName: "feature/source-control",
31 + state: "open",
32 + isCrossRepository: true,
33 + headRepositoryNameWithOwner: "fork/t3code",
34 + headRepositoryOwnerLogin: "fork",
35 + }),
36 + });
37 +
38 + const changeRequest = yield* provider.getChangeRequest({
39 + cwd: "/repo",
40 + reference: "42",
41 + });
42 +
43 + assert.deepStrictEqual(changeRequest, {
44 + provider: "github",
45 + number: 42,
46 + title: "Add GitHub provider",
47 + url: "https://github.com/pingdotgg/t3code/pull/42",
48 + baseRefName: "main",
49 + headRefName: "feature/source-control",
50 + state: "open",
51 + updatedAt: Option.none(),
52 + isCrossRepository: true,
53 + headRepositoryNameWithOwner: "fork/t3code",
54 + headRepositoryOwnerLogin: "fork",
55 + });
56 + }),
57 +);
58 +
59 +it.effect("uses gh json listing for non-open change request state queries", () =>
60 + Effect.gen(function* () {
61 + let executeArgs: ReadonlyArray<string> = [];
62 + const provider = yield* makeProvider({
63 + execute: (input) => {
64 + executeArgs = input.args;
65 + return Effect.succeed(
66 + processResult(
67 + JSON.stringify([
68 + {
69 + number: 7,
70 + title: "Merged work",
71 + url: "https://github.com/pingdotgg/t3code/pull/7",
72 + baseRefName: "main",
73 + headRefName: "feature/merged",
74 + state: "merged",
75 + updatedAt: "2026-01-02T00:00:00.000Z",
76 + },
77 + ]),
78 + ),
79 + );
80 + },
81 + });
82 +
83 + const changeRequests = yield* provider.listChangeRequests({
84 + cwd: "/repo",
85 + headSelector: "feature/merged",
86 + state: "all",
87 + limit: 10,
88 + });
89 +
90 + assert.deepStrictEqual(executeArgs, [
91 + "pr",
92 + "list",
93 + "--head",
94 + "feature/merged",
95 + "--state",
96 + "all",
97 + "--limit",
98 + "10",
99 + "--json",
100 + "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
101 + ]);
102 + assert.strictEqual(changeRequests[0]?.provider, "github");
103 + assert.strictEqual(changeRequests[0]?.state, "merged");
104 + assert.deepStrictEqual(
105 + changeRequests[0]?.updatedAt,
106 + Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")),
107 + );
108 + }),
109 +);
110 +
111 +it.effect("treats empty non-open change request listing output as no results", () =>
112 + Effect.gen(function* () {
113 + const provider = yield* makeProvider({
114 + execute: () => Effect.succeed(processResult("")),
115 + });
116 +
117 + const changeRequests = yield* provider.listChangeRequests({
118 + cwd: "/repo",
119 + headSelector: "feature/empty",
120 + state: "all",
121 + limit: 10,
122 + });
123 +
124 + assert.deepStrictEqual(changeRequests, []);
125 + }),
126 +);
127 +
128 +it.effect("creates GitHub PRs through provider-neutral input names", () =>
129 + Effect.gen(function* () {
130 + let createInput: Parameters<GitHubCliShape["createPullRequest"]>[0] | null = null;
131 + const provider = yield* makeProvider({
132 + createPullRequest: (input) => {
133 + createInput = input;
134 + return Effect.void;
135 + },
136 + });
137 +
138 + yield* provider.createChangeRequest({
139 + cwd: "/repo",
140 + baseRefName: "main",
141 + headSelector: "owner:feature/provider",
142 + title: "Provider PR",
143 + bodyFile: "/tmp/body.md",
144 + });
145 +
146 + assert.deepStrictEqual(createInput, {
147 + cwd: "/repo",
148 + baseBranch: "main",
149 + headSelector: "owner:feature/provider",
150 + title: "Provider PR",
151 + bodyFile: "/tmp/body.md",
152 + });
153 + }),
154 +);
apps/server/src/sourceControl/GitHubSourceControlProvider.ts new
+145
@@ -0,0 +1,145 @@
1 +import { Effect, Layer, Option, Result, Schema } from "effect";
2 +import {
3 + SourceControlProviderError,
4 + type ChangeRequest,
5 + type ChangeRequestState,
6 + type GitHubCliError,
7 +} from "@t3tools/contracts";
8 +
9 +import { GitHubCli, type GitHubPullRequestSummary } from "./GitHubCli.ts";
10 +import { decodeGitHubPullRequestListJson } from "../git/githubPullRequests.ts";
11 +import { SourceControlProvider, type SourceControlProviderShape } from "./SourceControlProvider.ts";
12 +
13 +function providerError(operation: string, cause: GitHubCliError): SourceControlProviderError {
14 + return new SourceControlProviderError({
15 + provider: "github",
16 + operation,
17 + detail: cause.detail,
18 + cause,
19 + });
20 +}
21 +
22 +function toChangeRequest(summary: GitHubPullRequestSummary): ChangeRequest {
23 + return {
24 + provider: "github",
25 + number: summary.number,
26 + title: summary.title,
27 + url: summary.url,
28 + baseRefName: summary.baseRefName,
29 + headRefName: summary.headRefName,
30 + state: summary.state ?? "open",
31 + updatedAt: Option.none(),
32 + ...(summary.isCrossRepository !== undefined
33 + ? { isCrossRepository: summary.isCrossRepository }
34 + : {}),
35 + ...(summary.headRepositoryNameWithOwner !== undefined
36 + ? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner }
37 + : {}),
38 + ...(summary.headRepositoryOwnerLogin !== undefined
39 + ? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin }
40 + : {}),
41 + };
42 +}
43 +
44 +export const make = Effect.fn("makeGitHubSourceControlProvider")(function* () {
45 + const github = yield* GitHubCli;
46 +
47 + const listChangeRequests: SourceControlProviderShape["listChangeRequests"] = (input) => {
48 + if (input.state === "open") {
49 + return github
50 + .listOpenPullRequests({
51 + cwd: input.cwd,
52 + headSelector: input.headSelector,
53 + ...(input.limit !== undefined ? { limit: input.limit } : {}),
54 + })
55 + .pipe(
56 + Effect.map((items) => items.map(toChangeRequest)),
57 + Effect.mapError((error) => providerError("listChangeRequests", error)),
58 + );
59 + }
60 +
61 + const stateArg: ChangeRequestState | "all" = input.state;
62 + return github
63 + .execute({
64 + cwd: input.cwd,
65 + args: [
66 + "pr",
67 + "list",
68 + "--head",
69 + input.headSelector,
70 + "--state",
71 + stateArg,
72 + "--limit",
73 + String(input.limit ?? 20),
74 + "--json",
75 + "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
76 + ],
77 + })
78 + .pipe(
79 + Effect.flatMap((result) => {
80 + const raw = result.stdout.trim();
81 + if (raw.length === 0) {
82 + return Effect.succeed([]);
83 + }
84 + return Effect.sync(() => decodeGitHubPullRequestListJson(raw)).pipe(
85 + Effect.flatMap((decoded) =>
86 + Result.isSuccess(decoded)
87 + ? Effect.succeed(
88 + decoded.success.map((item) => ({
89 + ...toChangeRequest(item),
90 + updatedAt: item.updatedAt,
91 + })),
92 + )
93 + : Effect.fail(
94 + new SourceControlProviderError({
95 + provider: "github",
96 + operation: "listChangeRequests",
97 + detail: "GitHub CLI returned invalid change request JSON.",
98 + cause: decoded.failure,
99 + }),
100 + ),
101 + ),
102 + );
103 + }),
104 + Effect.mapError((error) =>
105 + Schema.is(SourceControlProviderError)(error)
106 + ? error
107 + : providerError("listChangeRequests", error),
108 + ),
109 + );
110 + };
111 +
112 + return SourceControlProvider.of({
113 + kind: "github",
114 + listChangeRequests,
115 + getChangeRequest: (input) =>
116 + github.getPullRequest(input).pipe(
117 + Effect.map(toChangeRequest),
118 + Effect.mapError((error) => providerError("getChangeRequest", error)),
119 + ),
120 + createChangeRequest: (input) =>
121 + github
122 + .createPullRequest({
123 + cwd: input.cwd,
124 + baseBranch: input.baseRefName,
125 + headSelector: input.headSelector,
126 + title: input.title,
127 + bodyFile: input.bodyFile,
128 + })
129 + .pipe(Effect.mapError((error) => providerError("createChangeRequest", error))),
130 + getRepositoryCloneUrls: (input) =>
131 + github
132 + .getRepositoryCloneUrls(input)
133 + .pipe(Effect.mapError((error) => providerError("getRepositoryCloneUrls", error))),
134 + getDefaultBranch: (input) =>
135 + github
136 + .getDefaultBranch(input)
137 + .pipe(Effect.mapError((error) => providerError("getDefaultBranch", error))),
138 + checkoutChangeRequest: (input) =>
139 + github
140 + .checkoutPullRequest(input)
141 + .pipe(Effect.mapError((error) => providerError("checkoutChangeRequest", error))),
142 + });
143 +});
144 +
145 +export const layer = Layer.effect(SourceControlProvider, make());
apps/server/src/sourceControl/SourceControlDiscovery.test.ts new
+214
@@ -0,0 +1,214 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as NodeServices from "@effect/platform-node/NodeServices";
3 +import { Effect, Layer, Option } from "effect";
4 +import { ChildProcessSpawner } from "effect/unstable/process";
5 +import { VcsProcessSpawnError } from "@t3tools/contracts";
6 +
7 +import { ServerConfig } from "../config.ts";
8 +import * as VcsProcess from "../vcs/VcsProcess.ts";
9 +import { SourceControlDiscovery, layer } from "./SourceControlDiscovery.ts";
10 +
11 +const processOutput = (
12 + stdout: string,
13 + options?: {
14 + readonly stderr?: string;
15 + readonly exitCode?: ChildProcessSpawner.ExitCode;
16 + },
17 +): VcsProcess.VcsProcessOutput => ({
18 + exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
19 + stdout,
20 + stderr: options?.stderr ?? "",
21 + stdoutTruncated: false,
22 + stderrTruncated: false,
23 +});
24 +
25 +it.effect("reports implemented tools separately from locally available CLIs", () => {
26 + const testLayer = layer.pipe(
27 + Layer.provide(
28 + ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-discovery-" }),
29 + ),
30 + Layer.provide(
31 + Layer.mock(VcsProcess.VcsProcess)({
32 + run: (input) => {
33 + if (input.command === "git") {
34 + return Effect.succeed(processOutput("git version 2.51.0\n"));
35 + }
36 + if (input.command === "gh" && input.args[0] === "--version") {
37 + return Effect.succeed(processOutput("gh version 2.83.0\n"));
38 + }
39 + if (input.command === "gh" && input.args.join(" ") === "auth status") {
40 + return Effect.succeed(
41 + processOutput(`github.com
42 +Logged in to github.com account juliusmarminge (keyring)
43 +- Active account: true
44 +- Git operations protocol: ssh
45 +- Token: gho_************************************
46 +- Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo'
47 +`),
48 + );
49 + }
50 + return Effect.fail(
51 + new VcsProcessSpawnError({
52 + operation: input.operation,
53 + command: input.command,
54 + cwd: input.cwd,
55 + cause: new Error(`${input.command} not found`),
56 + }),
57 + );
58 + },
59 + }),
60 + ),
61 + Layer.provideMerge(NodeServices.layer),
62 + );
63 +
64 + return Effect.gen(function* () {
65 + const discovery = yield* SourceControlDiscovery;
66 + const result = yield* discovery.discover;
67 +
68 + assert.deepStrictEqual(
69 + result.versionControlSystems.map((item) => ({
70 + kind: item.kind,
71 + implemented: item.implemented,
72 + status: item.status,
73 + })),
74 + [
75 + { kind: "git", implemented: true, status: "available" },
76 + { kind: "jj", implemented: false, status: "missing" },
77 + ],
78 + );
79 + assert.deepStrictEqual(
80 + result.sourceControlProviders.map((item) => ({
81 + kind: item.kind,
82 + implemented: item.implemented,
83 + status: item.status,
84 + auth: item.auth.status,
85 + account: item.auth.account,
86 + })),
87 + [
88 + {
89 + kind: "github",
90 + implemented: true,
91 + status: "available",
92 + auth: "authenticated",
93 + account: Option.some("juliusmarminge"),
94 + },
95 + {
96 + kind: "gitlab",
97 + implemented: false,
98 + status: "missing",
99 + auth: "unknown",
100 + account: Option.none(),
101 + },
102 + {
103 + kind: "azure-devops",
104 + implemented: false,
105 + status: "missing",
106 + auth: "unknown",
107 + account: Option.none(),
108 + },
109 + {
110 + kind: "bitbucket",
111 + implemented: false,
112 + status: "missing",
113 + auth: "unknown",
114 + account: Option.none(),
115 + },
116 + ],
117 + );
118 + }).pipe(Effect.provide(testLayer));
119 +});
120 +
121 +it.effect("probes provider authentication without exposing token details", () => {
122 + const testLayer = layer.pipe(
123 + Layer.provide(
124 + ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-auth-discovery-" }),
125 + ),
126 + Layer.provide(
127 + Layer.mock(VcsProcess.VcsProcess)({
128 + run: (input) => {
129 + if (input.args[0] === "--version") {
130 + return Effect.succeed(processOutput(`${input.command} version test\n`));
131 + }
132 + if (input.command === "gh" && input.args.join(" ") === "auth status") {
133 + return Effect.succeed(
134 + processOutput(`github.com
135 +Logged in to github.com account octocat (keyring)
136 +- Token: gho_************************************
137 +- Token scopes: 'repo'
138 +`),
139 + );
140 + }
141 + if (input.command === "glab" && input.args.join(" ") === "auth status") {
142 + return Effect.succeed(
143 + processOutput(`gitlab.com
144 +Logged in to gitlab.com as gitlab-user
145 +`),
146 + );
147 + }
148 + if (
149 + input.command === "az" &&
150 + input.args.join(" ") === "account show --query user.name -o tsv"
151 + ) {
152 + return Effect.succeed(processOutput("azure-user@example.com\n"));
153 + }
154 + if (input.command === "bb" && input.args.join(" ") === "auth status") {
155 + return Effect.succeed(
156 + processOutput(`bitbucket.org
157 +Logged in as bitbucket-user
158 +`),
159 + );
160 + }
161 + return Effect.fail(
162 + new VcsProcessSpawnError({
163 + operation: input.operation,
164 + command: input.command,
165 + cwd: input.cwd,
166 + cause: new Error(`${input.command} not found`),
167 + }),
168 + );
169 + },
170 + }),
171 + ),
172 + Layer.provideMerge(NodeServices.layer),
173 + );
174 +
175 + return Effect.gen(function* () {
176 + const discovery = yield* SourceControlDiscovery;
177 + const result = yield* discovery.discover;
178 +
179 + assert.deepStrictEqual(
180 + result.sourceControlProviders.map((item) => ({
181 + kind: item.kind,
182 + auth: item.auth.status,
183 + account: item.auth.account,
184 + detail: item.auth.detail,
185 + })),
186 + [
187 + {
188 + kind: "github",
189 + auth: "authenticated",
190 + account: Option.some("octocat"),
191 + detail: Option.none(),
192 + },
193 + {
194 + kind: "gitlab",
195 + auth: "authenticated",
196 + account: Option.some("gitlab-user"),
197 + detail: Option.none(),
198 + },
199 + {
200 + kind: "azure-devops",
201 + auth: "authenticated",
202 + account: Option.some("azure-user@example.com"),
203 + detail: Option.none(),
204 + },
205 + {
206 + kind: "bitbucket",
207 + auth: "authenticated",
208 + account: Option.some("bitbucket-user"),
209 + detail: Option.none(),
210 + },
211 + ],
212 + );
213 + }).pipe(Effect.provide(testLayer));
214 +});
apps/server/src/sourceControl/SourceControlDiscovery.ts new
+412
@@ -0,0 +1,412 @@
1 +import {
2 + type SourceControlProviderAuth,
3 + type SourceControlDiscoveryResult,
4 + type SourceControlProviderDiscoveryItem,
5 + type SourceControlProviderKind,
6 + type VcsDiscoveryItem,
7 + type VcsDriverKind,
8 +} from "@t3tools/contracts";
9 +import { Context, Effect, Layer, Option } from "effect";
10 +
11 +import { ServerConfig } from "../config.ts";
12 +import * as VcsProcess from "../vcs/VcsProcess.ts";
13 +
14 +interface DiscoveryProbe {
15 + readonly label: string;
16 + readonly executable: string;
17 + readonly versionArgs: ReadonlyArray<string>;
18 + readonly implemented: boolean;
19 + readonly installHint: string;
20 +}
21 +
22 +type VcsProbe = DiscoveryProbe & {
23 + readonly kind: VcsDriverKind;
24 +};
25 +
26 +type ProviderProbe = DiscoveryProbe & {
27 + readonly kind: SourceControlProviderKind;
28 + readonly authArgs: ReadonlyArray<string>;
29 + readonly parseAuth: (input: AuthProbeInput) => SourceControlProviderAuth;
30 +};
31 +
32 +interface AuthProbeInput {
33 + readonly stdout: string;
34 + readonly stderr: string;
35 + readonly exitCode: VcsProcess.VcsProcessOutput["exitCode"];
36 +}
37 +
38 +interface DiscoveryProbeResult<Kind extends string> {
39 + readonly kind: Kind;
40 + readonly label: string;
41 + readonly executable: string;
42 + readonly implemented: boolean;
43 + readonly status: "available" | "missing";
44 + readonly version: Option.Option<string>;
45 + readonly installHint: string;
46 + readonly detail: Option.Option<string>;
47 +}
48 +
49 +const VCS_PROBES: ReadonlyArray<VcsProbe> = [
50 + {
51 + kind: "git",
52 + label: "Git",
53 + executable: "git",
54 + versionArgs: ["--version"],
55 + implemented: true,
56 + installHint: "Install Git from https://git-scm.com/downloads or with your package manager.",
57 + },
58 + {
59 + kind: "jj",
60 + label: "Jujutsu",
61 + executable: "jj",
62 + versionArgs: ["--version"],
63 + implemented: false,
64 + installHint: "Install Jujutsu with `brew install jj` or from https://github.com/jj-vcs/jj.",
65 + },
66 +];
67 +
68 +const SOURCE_CONTROL_PROVIDER_PROBES: ReadonlyArray<ProviderProbe> = [
69 + {
70 + kind: "github",
71 + label: "GitHub",
72 + executable: "gh",
73 + versionArgs: ["--version"],
74 + authArgs: ["auth", "status"],
75 + parseAuth: parseGitHubAuth,
76 + implemented: true,
77 + installHint: "Install GitHub CLI with `brew install gh` or from https://cli.github.com/.",
78 + },
79 + {
80 + kind: "gitlab",
81 + label: "GitLab",
82 + executable: "glab",
83 + versionArgs: ["--version"],
84 + authArgs: ["auth", "status"],
85 + parseAuth: parseGitLabAuth,
86 + implemented: false,
87 + installHint:
88 + "Install GitLab CLI with `brew install glab` or from https://gitlab.com/gitlab-org/cli.",
89 + },
90 + {
91 + kind: "azure-devops",
92 + label: "Azure DevOps",
93 + executable: "az",
94 + versionArgs: ["--version"],
95 + authArgs: ["account", "show", "--query", "user.name", "-o", "tsv"],
96 + parseAuth: parseAzureAuth,
97 + implemented: false,
98 + installHint:
99 + "Install Azure CLI with `brew install azure-cli`, then add Azure DevOps support with `az extension add --name azure-devops`.",
100 + },
101 + {
102 + kind: "bitbucket",
103 + label: "Bitbucket",
104 + executable: "bb",
105 + versionArgs: ["--version"],
106 + authArgs: ["auth", "status"],
107 + parseAuth: parseBitbucketAuth,
108 + implemented: false,
109 + installHint: "Install a Bitbucket CLI (`bb`) and authenticate it for your Bitbucket workspace.",
110 + },
111 +];
112 +
113 +function firstNonEmptyLine(text: string): Option.Option<string> {
114 + const line = text
115 + .split(/\r?\n/)
116 + .map((entry) => entry.trim())
117 + .find((entry) => entry.length > 0);
118 + return line === undefined ? Option.none() : Option.some(line);
119 +}
120 +
121 +function detailFromCause(cause: unknown): Option.Option<string> {
122 + if (cause instanceof Error && cause.message.trim().length > 0) {
123 + return Option.some(cause.message.trim());
124 + }
125 + return Option.none();
126 +}
127 +
128 +function authAccount(account: string | undefined): Option.Option<string> {
129 + const trimmed = account?.trim();
130 + return trimmed === undefined || trimmed.length === 0 ? Option.none() : Option.some(trimmed);
131 +}
132 +
133 +function authHost(host: string | undefined): Option.Option<string> {
134 + const trimmed = host?.trim();
135 + return trimmed === undefined || trimmed.length === 0 ? Option.none() : Option.some(trimmed);
136 +}
137 +
138 +function authDetail(detail: string | undefined): Option.Option<string> {
139 + const trimmed = detail?.trim();
140 + return trimmed === undefined || trimmed.length === 0 ? Option.none() : Option.some(trimmed);
141 +}
142 +
143 +function providerAuth(input: {
144 + readonly status: SourceControlProviderAuth["status"];
145 + readonly account?: string | undefined;
146 + readonly host?: string | undefined;
147 + readonly detail?: string | undefined;
148 +}): SourceControlProviderAuth {
149 + return {
150 + status: input.status,
151 + account: authAccount(input.account),
152 + host: authHost(input.host),
153 + detail: authDetail(input.detail),
154 + };
155 +}
156 +
157 +function unknownAuth(detail?: string): SourceControlProviderAuth {
158 + return providerAuth({ status: "unknown", detail });
159 +}
160 +
161 +function combinedAuthOutput(input: AuthProbeInput): string {
162 + return [input.stdout, input.stderr].filter((entry) => entry.trim().length > 0).join("\n");
163 +}
164 +
165 +function sanitizedAuthLines(text: string): ReadonlyArray<string> {
166 + return text
167 + .split(/\r?\n/)
168 + .map((entry) => entry.trim())
169 + .filter((entry) => entry.length > 0)
170 + .filter((entry) => !/^[-\s]*token(?:\s+scopes?)?:/iu.test(entry));
171 +}
172 +
173 +function firstSafeAuthLine(text: string): string | undefined {
174 + return sanitizedAuthLines(text)[0];
175 +}
176 +
177 +function parseCliHost(text: string): string | undefined {
178 + return sanitizedAuthLines(text)
179 + .map((line) => line.replace(/^[^a-z0-9]+/iu, ""))
180 + .find((line) => /^[a-z0-9][a-z0-9.-]*(?::\d+)?$/iu.test(line));
181 +}
182 +
183 +function matchFirst(text: string, patterns: ReadonlyArray<RegExp>): string | undefined {
184 + for (const pattern of patterns) {
185 + const match = pattern.exec(text);
186 + const value = match?.[1]?.trim();
187 + if (value && value.length > 0) return value;
188 + }
189 + return undefined;
190 +}
191 +
192 +function parseGitHubAuth(input: AuthProbeInput): SourceControlProviderAuth {
193 + const output = combinedAuthOutput(input);
194 + const account = matchFirst(output, [
195 + /Logged in to .* account\s+([^\s(]+)/iu,
196 + /Logged in to .* as\s+([^\s(]+)/iu,
197 + ]);
198 + const host = parseCliHost(output);
199 +
200 + if (input.exitCode !== 0) {
201 + return providerAuth({
202 + status: "unauthenticated",
203 + host,
204 + detail: firstSafeAuthLine(output) ?? "Run `gh auth login` to authenticate GitHub CLI.",
205 + });
206 + }
207 +
208 + if (account) {
209 + return providerAuth({ status: "authenticated", account, host });
210 + }
211 +
212 + return providerAuth({
213 + status: "unknown",
214 + host,
215 + detail: firstSafeAuthLine(output) ?? "GitHub CLI auth status could not be parsed.",
216 + });
217 +}
218 +
219 +function parseGitLabAuth(input: AuthProbeInput): SourceControlProviderAuth {
220 + const output = combinedAuthOutput(input);
221 + const account = matchFirst(output, [
222 + /Logged in to .* as\s+([^\s(]+)/iu,
223 + /Logged in to .* account\s+([^\s(]+)/iu,
224 + /account:\s*([^\s(]+)/iu,
225 + ]);
226 + const host = parseCliHost(output);
227 +
228 + if (input.exitCode !== 0) {
229 + return providerAuth({
230 + status: "unauthenticated",
231 + host,
232 + detail: firstSafeAuthLine(output) ?? "Run `glab auth login` to authenticate GitLab CLI.",
233 + });
234 + }
235 +
236 + if (account) {
237 + return providerAuth({ status: "authenticated", account, host });
238 + }
239 +
240 + return providerAuth({
241 + status: "unknown",
242 + host,
243 + detail: firstSafeAuthLine(output) ?? "GitLab CLI auth status could not be parsed.",
244 + });
245 +}
246 +
247 +function parseAzureAuth(input: AuthProbeInput): SourceControlProviderAuth {
248 + const account = input.stdout.trim().split(/\r?\n/)[0]?.trim();
249 +
250 + if (input.exitCode !== 0) {
251 + return providerAuth({
252 + status: "unauthenticated",
253 + detail:
254 + firstSafeAuthLine(combinedAuthOutput(input)) ?? "Run `az login` to authenticate Azure CLI.",
255 + });
256 + }
257 +
258 + if (account && account.length > 0) {
259 + return providerAuth({ status: "authenticated", account, host: "dev.azure.com" });
260 + }
261 +
262 + return providerAuth({
263 + status: "unknown",
264 + host: "dev.azure.com",
265 + detail: "Azure CLI account status could not be parsed.",
266 + });
267 +}
268 +
269 +function parseBitbucketAuth(input: AuthProbeInput): SourceControlProviderAuth {
270 + const output = combinedAuthOutput(input);
271 + const account = matchFirst(output, [
272 + /Logged in to .* as\s+([^\s(]+)/iu,
273 + /Logged in as\s+([^\s(]+)/iu,
274 + /account:\s*([^\s(]+)/iu,
275 + /user:\s*([^\s(]+)/iu,
276 + /username:\s*([^\s(]+)/iu,
277 + ]);
278 + const host = parseCliHost(output) ?? "bitbucket.org";
279 +
280 + if (input.exitCode !== 0) {
281 + return providerAuth({
282 + status: "unauthenticated",
283 + host,
284 + detail:
285 + firstSafeAuthLine(output) ?? "Authenticate the Bitbucket CLI before enabling Bitbucket.",
286 + });
287 + }
288 +
289 + if (account) {
290 + return providerAuth({ status: "authenticated", account, host });
291 + }
292 +
293 + return providerAuth({
294 + status: "unknown",
295 + host,
296 + detail: firstSafeAuthLine(output) ?? "Bitbucket CLI auth status could not be parsed.",
297 + });
298 +}
299 +
300 +export interface SourceControlDiscoveryShape {
301 + readonly discover: Effect.Effect<SourceControlDiscoveryResult>;
302 +}
303 +
304 +export class SourceControlDiscovery extends Context.Service<
305 + SourceControlDiscovery,
306 + SourceControlDiscoveryShape
307 +>()("t3/source-control/SourceControlDiscovery") {}
308 +
309 +export const layer = Layer.effect(
310 + SourceControlDiscovery,
311 + Effect.gen(function* () {
312 + const config = yield* ServerConfig;
313 + const process = yield* VcsProcess.VcsProcess;
314 +
315 + const probe = <Kind extends VcsDriverKind | SourceControlProviderKind>(
316 + input: DiscoveryProbe & { readonly kind: Kind },
317 + ): Effect.Effect<DiscoveryProbeResult<Kind>> =>
318 + process
319 + .run({
320 + operation: "source-control.discovery.probe",
321 + command: input.executable,
322 + args: input.versionArgs,
323 + cwd: config.cwd,
324 + timeoutMs: 5_000,
325 + maxOutputBytes: 8_000,
326 + truncateOutputAtMaxBytes: true,
327 + })
328 + .pipe(
329 + Effect.map(
330 + (result) =>
331 + ({
332 + kind: input.kind,
333 + label: input.label,
334 + executable: input.executable,
335 + implemented: input.implemented,
336 + status: "available" as const,
337 + version: Option.orElse(firstNonEmptyLine(result.stdout), () =>
338 + firstNonEmptyLine(result.stderr),
339 + ),
340 + installHint: input.installHint,
341 + detail: Option.none<string>(),
342 + }) satisfies DiscoveryProbeResult<Kind>,
343 + ),
344 + Effect.catch((cause) =>
345 + Effect.succeed({
346 + kind: input.kind,
347 + label: input.label,
348 + executable: input.executable,
349 + implemented: input.implemented,
350 + status: "missing" as const,
351 + version: Option.none<string>(),
352 + installHint: input.installHint,
353 + detail: detailFromCause(cause),
354 + } satisfies DiscoveryProbeResult<Kind>),
355 + ),
356 + );
357 +
358 + const probeProvider = (input: ProviderProbe) =>
359 + probe(input).pipe(
360 + Effect.flatMap((item) => {
361 + if (item.status !== "available") {
362 + return Effect.succeed({
363 + ...item,
364 + auth: unknownAuth("CLI is not installed."),
365 + } satisfies SourceControlProviderDiscoveryItem);
366 + }
367 +
368 + return process
369 + .run({
370 + operation: "source-control.discovery.auth",
371 + command: input.executable,
372 + args: input.authArgs,
373 + cwd: config.cwd,
374 + allowNonZeroExit: true,
375 + timeoutMs: 5_000,
376 + maxOutputBytes: 8_000,
377 + truncateOutputAtMaxBytes: true,
378 + })
379 + .pipe(
380 + Effect.map(
381 + (result) =>
382 + ({
383 + ...item,
384 + auth: input.parseAuth(result),
385 + }) satisfies SourceControlProviderDiscoveryItem,
386 + ),
387 + Effect.catch((cause) =>
388 + Effect.succeed({
389 + ...item,
390 + auth: unknownAuth(Option.getOrUndefined(detailFromCause(cause))),
391 + } satisfies SourceControlProviderDiscoveryItem),
392 + ),
393 + );
394 + }),
395 + );
396 +
397 + return SourceControlDiscovery.of({
398 + discover: Effect.all({
399 + versionControlSystems: Effect.all(
400 + VCS_PROBES.map((entry) => probe(entry)) as ReadonlyArray<Effect.Effect<VcsDiscoveryItem>>,
401 + { concurrency: "unbounded" },
402 + ),
403 + sourceControlProviders: Effect.all(
404 + SOURCE_CONTROL_PROVIDER_PROBES.map((entry) => probeProvider(entry)) as ReadonlyArray<
405 + Effect.Effect<SourceControlProviderDiscoveryItem>
406 + >,
407 + { concurrency: "unbounded" },
408 + ),
409 + }),
410 + });
411 + }),
412 +);
apps/server/src/sourceControl/SourceControlProvider.ts new
+68
@@ -0,0 +1,68 @@
1 +import { Context, Effect } from "effect";
2 +import type {
3 + ChangeRequest,
4 + ChangeRequestState,
5 + SourceControlProviderError,
6 + SourceControlProviderInfo,
7 + SourceControlProviderKind,
8 + SourceControlRepositoryCloneUrls,
9 +} from "@t3tools/contracts";
10 +
11 +export interface SourceControlProviderContext {
12 + readonly provider: SourceControlProviderInfo;
13 + readonly remoteName: string;
14 + readonly remoteUrl: string;
15 +}
16 +
17 +export interface SourceControlRefSelector {
18 + readonly refName: string;
19 + readonly owner?: string;
20 + readonly repository?: string;
21 +}
22 +
23 +export interface SourceControlProviderShape {
24 + readonly kind: SourceControlProviderKind;
25 + readonly listChangeRequests: (input: {
26 + readonly cwd: string;
27 + readonly context?: SourceControlProviderContext;
28 + readonly source?: SourceControlRefSelector;
29 + readonly headSelector: string;
30 + readonly state: ChangeRequestState | "all";
31 + readonly limit?: number;
32 + }) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
33 + readonly getChangeRequest: (input: {
34 + readonly cwd: string;
35 + readonly context?: SourceControlProviderContext;
36 + readonly reference: string;
37 + }) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
38 + readonly createChangeRequest: (input: {
39 + readonly cwd: string;
40 + readonly context?: SourceControlProviderContext;
41 + readonly source?: SourceControlRefSelector;
42 + readonly target?: SourceControlRefSelector;
43 + readonly baseRefName: string;
44 + readonly headSelector: string;
45 + readonly title: string;
46 + readonly bodyFile: string;
47 + }) => Effect.Effect<void, SourceControlProviderError>;
48 + readonly getRepositoryCloneUrls: (input: {
49 + readonly cwd: string;
50 + readonly context?: SourceControlProviderContext;
51 + readonly repository: string;
52 + }) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
53 + readonly getDefaultBranch: (input: {
54 + readonly cwd: string;
55 + readonly context?: SourceControlProviderContext;
56 + }) => Effect.Effect<string | null, SourceControlProviderError>;
57 + readonly checkoutChangeRequest: (input: {
58 + readonly cwd: string;
59 + readonly context?: SourceControlProviderContext;
60 + readonly reference: string;
61 + readonly force?: boolean;
62 + }) => Effect.Effect<void, SourceControlProviderError>;
63 +}
64 +
65 +export class SourceControlProvider extends Context.Service<
66 + SourceControlProvider,
67 + SourceControlProviderShape
68 +>()("t3/source-control/SourceControlProvider") {}
apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts new
+114
@@ -0,0 +1,114 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { DateTime, Effect, Layer, Option } from "effect";
3 +
4 +import { GitHubCli } from "./GitHubCli.ts";
5 +import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
6 +import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
7 +import type { VcsDriverShape } from "../vcs/VcsDriver.ts";
8 +
9 +const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
10 +
11 +function makeRegistry(input: {
12 + readonly remotes: ReadonlyArray<{
13 + readonly name: string;
14 + readonly url: string;
15 + }>;
16 +}) {
17 + const driver = {
18 + listRemotes: () =>
19 + Effect.succeed({
20 + remotes: input.remotes.map((remote) => ({
21 + ...remote,
22 + pushUrl: Option.none(),
23 + isPrimary: remote.name === "origin",
24 + })),
25 + freshness: {
26 + source: "live-local" as const,
27 + observedAt: TEST_EPOCH,
28 + expiresAt: Option.none(),
29 + },
30 + }),
31 + } satisfies Partial<VcsDriverShape>;
32 +
33 + const registryLayer = Layer.mock(VcsDriverRegistry)({
34 + get: () => Effect.succeed(driver as unknown as VcsDriverShape),
35 + resolve: () =>
36 + Effect.succeed({
37 + kind: "git",
38 + repository: {
39 + kind: "git",
40 + rootPath: "/repo",
41 + metadataPath: null,
42 + freshness: {
43 + source: "live-local" as const,
44 + observedAt: TEST_EPOCH,
45 + expiresAt: Option.none(),
46 + },
47 + },
48 + driver: driver as unknown as VcsDriverShape,
49 + }),
50 + });
51 +
52 + return SourceControlProviderRegistry.make().pipe(
53 + Effect.provide(Layer.mergeAll(registryLayer, Layer.mock(GitHubCli)({}))),
54 + );
55 +}
56 +
57 +it.effect("routes GitHub remotes to the GitHub provider", () =>
58 + Effect.gen(function* () {
59 + const registry = yield* makeRegistry({
60 + remotes: [{ name: "origin", url: "git@github.com:pingdotgg/t3code.git" }],
61 + });
62 +
63 + const provider = yield* registry.resolve({ cwd: "/repo" });
64 +
65 + assert.strictEqual(provider.kind, "github");
66 + }),
67 +);
68 +
69 +it.effect("routes directly by provider kind for remote-first workflows", () =>
70 + Effect.gen(function* () {
71 + const registry = yield* makeRegistry({
72 + remotes: [],
73 + });
74 +
75 + const provider = yield* registry.get("github");
76 +
77 + assert.strictEqual(provider.kind, "github");
78 + }),
79 +);
80 +
81 +it.effect(
82 + "detects GitLab remotes and returns an unsupported provider until one is registered",
83 + () =>
84 + Effect.gen(function* () {
85 + const registry = yield* makeRegistry({
86 + remotes: [{ name: "origin", url: "git@gitlab.com:group/project.git" }],
87 + });
88 +
89 + const provider = yield* registry.resolve({ cwd: "/repo" });
90 +
91 + assert.strictEqual(provider.kind, "gitlab");
92 + const error = yield* Effect.flip(
93 + provider.listChangeRequests({
94 + cwd: "/repo",
95 + headSelector: "feature/source-control",
96 + state: "open",
97 + }),
98 + );
99 +
100 + assert.strictEqual(error.provider, "gitlab");
101 + }),
102 +);
103 +
104 +it.effect("falls back to a non-origin remote when origin is not configured", () =>
105 + Effect.gen(function* () {
106 + const registry = yield* makeRegistry({
107 + remotes: [{ name: "upstream", url: "https://dev.azure.com/acme/project/_git/repo" }],
108 + });
109 +
110 + const provider = yield* registry.resolve({ cwd: "/repo" });
111 +
112 + assert.strictEqual(provider.kind, "azure-devops");
113 + }),
114 +);
apps/server/src/sourceControl/SourceControlProviderRegistry.ts new
+162
@@ -0,0 +1,162 @@
1 +import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
2 +import { SourceControlProviderError } from "@t3tools/contracts";
3 +import type { SourceControlProviderKind } from "@t3tools/contracts";
4 +import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
5 +
6 +import {
7 + SourceControlProvider,
8 + type SourceControlProviderContext,
9 + type SourceControlProviderShape,
10 +} from "./SourceControlProvider.ts";
11 +import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
12 +import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
13 +
14 +const PROVIDER_DETECTION_CACHE_CAPACITY = 2_048;
15 +const PROVIDER_DETECTION_CACHE_TTL = Duration.seconds(5);
16 +
17 +export interface SourceControlProviderRegistration {
18 + readonly kind: SourceControlProviderKind;
19 + readonly provider: SourceControlProviderShape;
20 +}
21 +
22 +export interface SourceControlProviderHandle {
23 + readonly provider: SourceControlProviderShape;
24 + readonly context: SourceControlProviderContext | null;
25 +}
26 +
27 +export interface SourceControlProviderRegistryShape {
28 + readonly get: (
29 + kind: SourceControlProviderKind,
30 + ) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
31 + readonly resolveHandle: (input: {
32 + readonly cwd: string;
33 + }) => Effect.Effect<SourceControlProviderHandle, SourceControlProviderError>;
34 + readonly resolve: (input: {
35 + readonly cwd: string;
36 + }) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
37 +}
38 +
39 +export class SourceControlProviderRegistry extends Context.Service<
40 + SourceControlProviderRegistry,
41 + SourceControlProviderRegistryShape
42 +>()("t3/source-control/SourceControlProviderRegistry") {}
43 +
44 +function unsupportedProvider(kind: SourceControlProviderKind): SourceControlProviderShape {
45 + const unsupported = (operation: string) =>
46 + Effect.fail(
47 + new SourceControlProviderError({
48 + provider: kind,
49 + operation,
50 + detail: `No ${kind} source control provider is registered.`,
51 + }),
52 + );
53 +
54 + return SourceControlProvider.of({
55 + kind,
56 + listChangeRequests: () => unsupported("listChangeRequests"),
57 + getChangeRequest: () => unsupported("getChangeRequest"),
58 + createChangeRequest: () => unsupported("createChangeRequest"),
59 + getRepositoryCloneUrls: () => unsupported("getRepositoryCloneUrls"),
60 + getDefaultBranch: () => unsupported("getDefaultBranch"),
61 + checkoutChangeRequest: () => unsupported("checkoutChangeRequest"),
62 + });
63 +}
64 +
65 +function providerDetectionError(operation: string, cwd: string, cause: unknown) {
66 + return new SourceControlProviderError({
67 + provider: "unknown",
68 + operation,
69 + detail: `Failed to detect source control provider for ${cwd}.`,
70 + cause,
71 + });
72 +}
73 +
74 +function selectProviderContext(
75 + remotes: ReadonlyArray<{
76 + readonly name: string;
77 + readonly url: string;
78 + }>,
79 +): SourceControlProviderContext | null {
80 + const candidates = remotes
81 + .map((remote) => {
82 + const provider = detectSourceControlProviderFromRemoteUrl(remote.url);
83 + return provider
84 + ? {
85 + provider,
86 + remoteName: remote.name,
87 + remoteUrl: remote.url,
88 + }
89 + : null;
90 + })
91 + .filter((value): value is SourceControlProviderContext => value !== null);
92 +
93 + return (
94 + candidates.find((candidate) => candidate.remoteName === "origin") ??
95 + candidates.find((candidate) => candidate.provider.kind !== "unknown") ??
96 + candidates[0] ??
97 + null
98 + );
99 +}
100 +
101 +export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWithProviders")(
102 + function* (registrations: ReadonlyArray<SourceControlProviderRegistration>) {
103 + const vcsRegistry = yield* VcsDriverRegistry;
104 + const providers = new Map<SourceControlProviderKind, SourceControlProviderShape>(
105 + registrations.map((registration) => [registration.kind, registration.provider]),
106 + );
107 +
108 + const get: SourceControlProviderRegistryShape["get"] = (kind) =>
109 + Effect.succeed(providers.get(kind) ?? unsupportedProvider(kind));
110 +
111 + const detectProviderContext = Effect.fn("SourceControlProviderRegistry.detectProviderContext")(
112 + function* (cwd: string) {
113 + const handle = yield* vcsRegistry
114 + .resolve({ cwd })
115 + .pipe(Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)));
116 + const remotes = yield* handle.driver
117 + .listRemotes(cwd)
118 + .pipe(Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)));
119 +
120 + return selectProviderContext(remotes.remotes);
121 + },
122 + );
123 +
124 + const providerContextCache = yield* Cache.makeWith<
125 + string,
126 + SourceControlProviderContext | null,
127 + SourceControlProviderError
128 + >(detectProviderContext, {
129 + capacity: PROVIDER_DETECTION_CACHE_CAPACITY,
130 + timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero),
131 + });
132 +
133 + const resolveHandle: SourceControlProviderRegistryShape["resolveHandle"] = (input) =>
134 + Cache.get(providerContextCache, input.cwd).pipe(
135 + Effect.map((context) => {
136 + const kind = context?.provider.kind ?? "unknown";
137 + return {
138 + provider: providers.get(kind) ?? unsupportedProvider(kind),
139 + context,
140 + } satisfies SourceControlProviderHandle;
141 + }),
142 + );
143 +
144 + return SourceControlProviderRegistry.of({
145 + get,
146 + resolveHandle,
147 + resolve: (input) => resolveHandle(input).pipe(Effect.map((handle) => handle.provider)),
148 + });
149 + },
150 +);
151 +
152 +export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () {
153 + const github = yield* GitHubSourceControlProvider.make();
154 + return yield* makeWithProviders([
155 + {
156 + kind: "github",
157 + provider: github,
158 + },
159 + ]);
160 +});
161 +
162 +export const layer = Layer.effect(SourceControlProviderRegistry, make());
apps/server/src/textGeneration/ClaudeTextGeneration.test.ts renamed
+4 -4
@@ -5,9 +5,9 @@ import { Effect, FileSystem, Layer, Path, Schema } from "effect";
5 import { createModelSelection } from "@t3tools/shared/model";
6 import { expect } from "vitest";
7
8 -import { ServerConfig } from "../../config.ts";
9 -import { type TextGenerationShape } from "../Services/TextGeneration.ts";
10 -import { sanitizeThreadTitle } from "../Utils.ts";
8 +import { ServerConfig } from "../config.ts";
9 +import { type TextGenerationShape } from "./TextGeneration.ts";
10 +import { sanitizeThreadTitle } from "./TextGenerationUtils.ts";
11 import { makeClaudeTextGeneration } from "./ClaudeTextGeneration.ts";
12
13 const ClaudeTextGenerationTestLayer = ServerConfig.layerTest(process.cwd(), {
@@ -184,7 +184,7 @@ function withFakeClaudeEnv<A, E, R>(
184 }).pipe(Effect.scoped);
185 }
186
187 -it.layer(ClaudeTextGenerationTestLayer)("ClaudeTextGenerationLive", (it) => {
187 +it.layer(ClaudeTextGenerationTestLayer)("ClaudeTextGeneration", (it) => {
188 it.effect("forwards Claude thinking settings for Haiku without passing effort", () =>
189 withFakeClaudeEnv(
190 {
apps/server/src/textGeneration/ClaudeTextGeneration.ts renamed
+5 -5
@@ -14,20 +14,20 @@ import { type ClaudeSettings, type ModelSelection } from "@t3tools/contracts";
14 import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
15
16 import { TextGenerationError } from "@t3tools/contracts";
17 -import { type TextGenerationShape } from "../Services/TextGeneration.ts";
17 +import { type TextGenerationShape } from "./TextGeneration.ts";
18 import {
19 buildBranchNamePrompt,
20 buildCommitMessagePrompt,
21 buildPrContentPrompt,
22 buildThreadTitlePrompt,
23 -} from "../Prompts.ts";
23 +} from "./TextGenerationPrompts.ts";
24 import {
25 normalizeCliError,
26 sanitizeCommitSubject,
27 sanitizePrTitle,
28 sanitizeThreadTitle,
29 toJsonSchemaObject,
30 -} from "../Utils.ts";
30 +} from "./TextGenerationUtils.ts";
31 import {
32 getModelSelectionStringOptionValue,
33 getProviderOptionDescriptors,
@@ -37,8 +37,8 @@ import {
37 normalizeClaudeCliEffort,
38 resolveClaudeApiModelId,
39 resolveClaudeEffort,
40 -} from "../../provider/Layers/ClaudeProvider.ts";
41 -import { makeClaudeEnvironment } from "../../provider/Drivers/ClaudeHome.ts";
40 +} from "../provider/Layers/ClaudeProvider.ts";
41 +import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts";
42
43 const CLAUDE_TIMEOUT_MS = 180_000;
44
apps/server/src/textGeneration/CodexTextGeneration.test.ts renamed
+3 -3
@@ -6,8 +6,8 @@ import { expect } from "vitest";
6
7 import { CodexSettings, ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
8
9 -import { ServerConfig } from "../../config.ts";
10 -import { type TextGenerationShape } from "../Services/TextGeneration.ts";
9 +import { ServerConfig } from "../config.ts";
10 +import { type TextGenerationShape } from "./TextGeneration.ts";
11 import { makeCodexTextGeneration } from "./CodexTextGeneration.ts";
12
13 const DEFAULT_TEST_MODEL_SELECTION = createModelSelection(
@@ -168,7 +168,7 @@ function withFakeCodexEnv<A, E, R>(
168 }).pipe(Effect.scoped);
169 }
170
171 -it.layer(CodexTextGenerationTestLayer)("CodexTextGenerationLive", (it) => {
171 +it.layer(CodexTextGenerationTestLayer)("CodexTextGeneration", (it) => {
172 it.effect("generates and sanitizes commit messages without branch by default", () =>
173 withFakeCodexEnv(
174 {
apps/server/src/textGeneration/CodexTextGeneration.ts renamed
+6 -10
@@ -4,28 +4,28 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
4 import { type CodexSettings, type ModelSelection } from "@t3tools/contracts";
5 import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
6
7 -import { resolveAttachmentPath } from "../../attachmentStore.ts";
8 -import { ServerConfig } from "../../config.ts";
9 -import { expandHomePath } from "../../pathExpansion.ts";
7 +import { resolveAttachmentPath } from "../attachmentStore.ts";
8 +import { ServerConfig } from "../config.ts";
9 +import { expandHomePath } from "../pathExpansion.ts";
10 import { TextGenerationError } from "@t3tools/contracts";
11 import {
12 type BranchNameGenerationInput,
13 type ThreadTitleGenerationResult,
14 type TextGenerationShape,
15 -} from "../Services/TextGeneration.ts";
15 +} from "./TextGeneration.ts";
16 import {
17 buildBranchNamePrompt,
18 buildCommitMessagePrompt,
19 buildPrContentPrompt,
20 buildThreadTitlePrompt,
21 -} from "../Prompts.ts";
21 +} from "./TextGenerationPrompts.ts";
22 import {
23 normalizeCliError,
24 sanitizeCommitSubject,
25 sanitizePrTitle,
26 sanitizeThreadTitle,
27 toJsonSchemaObject,
28 -} from "../Utils.ts";
28 +} from "./TextGenerationUtils.ts";
29 import {
30 getModelSelectionBooleanOptionValue,
31 getModelSelectionStringOptionValue,
@@ -386,7 +386,3 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
386 generateThreadTitle,
387 } satisfies TextGenerationShape;
388 });
389 -
390 -// NOTE: `CodexTextGenerationLive` (the singleton Layer) has been removed.
391 -// `makeCodexTextGeneration(codexConfig)` is now invoked directly by
392 -// `CodexDriver.create()` for each configured instance.
apps/server/src/textGeneration/CursorTextGeneration.test.ts renamed
+8 -8
@@ -11,12 +11,12 @@ import { expect } from "vitest";
11
12 import { CursorSettings, ProviderInstanceId } from "@t3tools/contracts";
13
14 -import { ServerConfig } from "../../config.ts";
15 -import { type TextGenerationShape } from "../Services/TextGeneration.ts";
14 +import { ServerConfig } from "../config.ts";
15 +import { type TextGenerationShape } from "./TextGeneration.ts";
16 import { makeCursorTextGeneration } from "./CursorTextGeneration.ts";
17
18 const __dirname = path.dirname(fileURLToPath(import.meta.url));
19 -const mockAgentPath = path.join(__dirname, "../../../scripts/acp-mock-agent.ts");
19 +const mockAgentPath = path.join(__dirname, "../../scripts/acp-mock-agent.ts");
20
21 function shellSingleQuote(value: string): string {
22 return `'${value.replaceAll("'", `'"'"'`)}'`;
@@ -82,7 +82,7 @@ function waitForFileContent(path: string): Effect.Effect<string> {
82 });
83 }
84
85 -it.layer(CursorTextGenerationTestLayer)("CursorTextGenerationLive", (it) => {
85 +it.layer(CursorTextGenerationTestLayer)("CursorTextGeneration", (it) => {
86 it.effect("uses ACP model config options instead of raw CLI model ids", () => {
87 const requestLogDir = mkdtempSync(path.join(os.tmpdir(), "t3code-cursor-text-log-"));
88 const requestLogPath = path.join(requestLogDir, "requests.ndjson");
@@ -100,9 +100,9 @@ it.layer(CursorTextGenerationTestLayer)("CursorTextGenerationLive", (it) => {
100 const generated = yield* textGeneration.generateCommitMessage({
101 cwd: process.cwd(),
102 branch: "feature/cursor-text-generation",
103 - stagedSummary: "M apps/server/src/git/Layers/CursorTextGeneration.ts",
103 + stagedSummary: "M apps/server/src/textGeneration/CursorTextGeneration.ts",
104 stagedPatch:
105 - "diff --git a/apps/server/src/git/Layers/CursorTextGeneration.ts b/apps/server/src/git/Layers/CursorTextGeneration.ts",
105 + "diff --git a/apps/server/src/textGeneration/CursorTextGeneration.ts b/apps/server/src/textGeneration/CursorTextGeneration.ts",
106 modelSelection: {
107 ...createModelSelection(ProviderInstanceId.make("cursor"), "gpt-5.4", [
108 { id: "reasoning", value: "xhigh" },
@@ -243,9 +243,9 @@ it.layer(CursorTextGenerationTestLayer)("CursorTextGenerationLive", (it) => {
243 const generated = yield* textGeneration.generateCommitMessage({
244 cwd: process.cwd(),
245 branch: "feature/cursor-runtime-close",
246 - stagedSummary: "M apps/server/src/git/Layers/CursorTextGeneration.ts",
246 + stagedSummary: "M apps/server/src/textGeneration/CursorTextGeneration.ts",
247 stagedPatch:
248 - "diff --git a/apps/server/src/git/Layers/CursorTextGeneration.ts b/apps/server/src/git/Layers/CursorTextGeneration.ts",
248 + "diff --git a/apps/server/src/textGeneration/CursorTextGeneration.ts b/apps/server/src/textGeneration/CursorTextGeneration.ts",
249 modelSelection: {
250 instanceId: ProviderInstanceId.make("cursor"),
251 model: "composer-2",
apps/server/src/textGeneration/CursorTextGeneration.ts renamed
+4 -11
@@ -5,26 +5,23 @@ import { type CursorSettings, type ModelSelection } from "@t3tools/contracts";
5 import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
6
7 import { TextGenerationError } from "@t3tools/contracts";
8 -import {
9 - type ThreadTitleGenerationResult,
10 - type TextGenerationShape,
11 -} from "../Services/TextGeneration.ts";
8 +import { type ThreadTitleGenerationResult, type TextGenerationShape } from "./TextGeneration.ts";
9 import {
10 buildBranchNamePrompt,
11 buildCommitMessagePrompt,
12 buildPrContentPrompt,
13 buildThreadTitlePrompt,
17 -} from "../Prompts.ts";
14 +} from "./TextGenerationPrompts.ts";
15 import {
16 extractJsonObject,
17 sanitizeCommitSubject,
18 sanitizePrTitle,
19 sanitizeThreadTitle,
23 -} from "../Utils.ts";
20 +} from "./TextGenerationUtils.ts";
21 import {
22 applyCursorAcpModelSelection,
23 makeCursorAcpRuntime,
27 -} from "../../provider/acp/CursorAcpSupport.ts";
24 +} from "../provider/acp/CursorAcpSupport.ts";
25
26 const CURSOR_TIMEOUT_MS = 180_000;
27
@@ -277,7 +274,3 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu
274 generateThreadTitle,
275 } satisfies TextGenerationShape;
276 });
280 -
281 -// NOTE: `CursorTextGenerationLive` (the singleton Layer) has been removed.
282 -// `makeCursorTextGeneration(cursorConfig)` is now invoked directly by
283 -// `CursorDriver.create()` so each provider instance owns its own closure.
apps/server/src/textGeneration/OpenCodeTextGeneration.test.ts renamed
+5 -5
@@ -6,13 +6,13 @@ import { TestClock } from "effect/testing";
6 import { NetService } from "@t3tools/shared/Net";
7 import { beforeEach, expect } from "vitest";
8
9 -import { ServerConfig } from "../../config.ts";
9 +import { ServerConfig } from "../config.ts";
10 import {
11 OpenCodeRuntime,
12 OpenCodeRuntimeError,
13 type OpenCodeRuntimeShape,
14 -} from "../../provider/opencodeRuntime.ts";
15 -import { type TextGenerationShape } from "../Services/TextGeneration.ts";
14 +} from "../provider/opencodeRuntime.ts";
15 +import { type TextGenerationShape } from "./TextGeneration.ts";
16 import { makeOpenCodeTextGeneration } from "./OpenCodeTextGeneration.ts";
17
18 const runtimeMock = {
@@ -158,7 +158,7 @@ const advanceIdleClock = Effect.gen(function* () {
158 yield* Effect.yieldNow;
159 });
160
161 -it.layer(OpenCodeTextGenerationTestLayer)("OpenCodeTextGenerationLive", (it) => {
161 +it.layer(OpenCodeTextGenerationTestLayer)("OpenCodeTextGeneration", (it) => {
162 it.effect("reuses a warm server across back-to-back requests and closes it after idling", () =>
163 withOpenCodeTextGeneration(DEFAULT_OPENCODE_SETTINGS, (textGeneration) =>
164 Effect.gen(function* () {
@@ -306,7 +306,7 @@ it.layer(OpenCodeTextGenerationTestLayer)("OpenCodeTextGenerationLive", (it) =>
306 });
307
308 it.layer(OpenCodeTextGenerationExistingServerTestLayer)(
309 - "OpenCodeTextGenerationLive with configured server URL",
309 + "OpenCodeTextGeneration with configured server URL",
310 (it) => {
311 it.effect("reuses a configured OpenCode server URL without spawning or applying idle TTL", () =>
312 withOpenCodeTextGeneration(EXISTING_SERVER_OPENCODE_SETTINGS, (textGeneration) =>
apps/server/src/textGeneration/OpenCodeTextGeneration.ts renamed
+6 -6
@@ -10,21 +10,21 @@ import {
10 import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
11 import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
12
13 -import { ServerConfig } from "../../config.ts";
14 -import { resolveAttachmentPath } from "../../attachmentStore.ts";
13 +import { ServerConfig } from "../config.ts";
14 +import { resolveAttachmentPath } from "../attachmentStore.ts";
15 import {
16 buildBranchNamePrompt,
17 buildCommitMessagePrompt,
18 buildPrContentPrompt,
19 buildThreadTitlePrompt,
20 -} from "../Prompts.ts";
21 -import { type TextGenerationShape } from "../Services/TextGeneration.ts";
20 +} from "./TextGenerationPrompts.ts";
21 +import { type TextGenerationShape } from "./TextGeneration.ts";
22 import {
23 extractJsonObject,
24 sanitizeCommitSubject,
25 sanitizePrTitle,
26 sanitizeThreadTitle,
27 -} from "../Utils.ts";
27 +} from "./TextGenerationUtils.ts";
28 import {
29 OpenCodeRuntime,
30 type OpenCodeServerConnection,
@@ -32,7 +32,7 @@ import {
32 openCodeRuntimeErrorDetail,
33 parseOpenCodeModelSlug,
34 toOpenCodeFileParts,
35 -} from "../../provider/opencodeRuntime.ts";
35 +} from "../provider/opencodeRuntime.ts";
36
37 const OPENCODE_TEXT_GENERATION_IDLE_TTL = "30 seconds";
38
apps/server/src/textGeneration/TextGeneration.test.ts renamed
+4 -4
@@ -5,11 +5,11 @@ import { describe, expect } from "vitest";
5 import { ProviderInstanceId } from "@t3tools/contracts";
6 import { createModelSelection } from "@t3tools/shared/model";
7
8 -import type { ProviderInstance } from "../../provider/ProviderDriver.ts";
9 -import type { ProviderInstanceRegistryShape } from "../../provider/Services/ProviderInstanceRegistry.ts";
10 -import type { TextGenerationShape } from "../Services/TextGeneration.ts";
8 +import type { ProviderInstance } from "../provider/ProviderDriver.ts";
9 +import type { ProviderInstanceRegistryShape } from "../provider/Services/ProviderInstanceRegistry.ts";
10 +import type { TextGenerationShape } from "./TextGeneration.ts";
11
12 -import { makeTextGenerationFromRegistry } from "./TextGenerationLive.ts";
12 +import { makeTextGenerationFromRegistry } from "./TextGeneration.ts";
13
14 const makeStubTextGeneration = (overrides: Partial<TextGenerationShape>): TextGenerationShape => ({
15 generateCommitMessage: () =>
apps/server/src/textGeneration/TextGeneration.ts renamed
+62 -14
@@ -1,18 +1,13 @@
1 -/**
2 - * TextGeneration - Effect service contract for AI-generated Git content.
3 - *
4 - * Generates commit messages and pull request titles/bodies from repository
5 - * context prepared by Git services.
6 - *
7 - * @module TextGeneration
8 - */
9 -import { Context } from "effect";
10 -import type { Effect } from "effect";
11 -import type { ChatAttachment, ModelSelection } from "@t3tools/contracts";
1 +import { Context, Effect, Layer } from "effect";
2 +import type { ChatAttachment, ModelSelection, ProviderInstanceId } from "@t3tools/contracts";
3 +import { TextGenerationError } from "@t3tools/contracts";
4
13 -import type { TextGenerationError } from "@t3tools/contracts";
5 +import {
6 + ProviderInstanceRegistry,
7 + type ProviderInstanceRegistryShape,
8 +} from "../provider/Services/ProviderInstanceRegistry.ts";
9 +import type { ProviderInstance } from "../provider/ProviderDriver.ts";
10
15 -/** Providers that support git text generation (commit messages, PR content, branch names). */
11 export type TextGenerationProvider = "codex" | "claudeAgent" | "cursor" | "opencode";
12
13 export interface CommitMessageGenerationInput {
@@ -119,5 +114,58 @@ export interface TextGenerationShape {
114 * TextGeneration - Service tag for commit and PR text generation.
115 */
116 export class TextGeneration extends Context.Service<TextGeneration, TextGenerationShape>()(
122 - "t3/git/Services/TextGeneration",
117 + "t3/text-generation/TextGeneration",
118 ) {}
119 +
120 +type TextGenerationOp =
121 + | "generateCommitMessage"
122 + | "generatePrContent"
123 + | "generateBranchName"
124 + | "generateThreadTitle";
125 +
126 +const resolveInstance = (
127 + registry: ProviderInstanceRegistryShape,
128 + operation: TextGenerationOp,
129 + instanceId: ProviderInstanceId,
130 +): Effect.Effect<ProviderInstance["textGeneration"], TextGenerationError> =>
131 + registry.getInstance(instanceId).pipe(
132 + Effect.flatMap((instance) =>
133 + instance
134 + ? Effect.succeed(instance.textGeneration)
135 + : Effect.fail(
136 + new TextGenerationError({
137 + operation,
138 + detail: `No provider instance registered for id '${instanceId}'.`,
139 + }),
140 + ),
141 + ),
142 + );
143 +
144 +export const makeTextGenerationFromRegistry = (
145 + registry: ProviderInstanceRegistryShape,
146 +): TextGenerationShape => ({
147 + generateCommitMessage: (input) =>
148 + resolveInstance(registry, "generateCommitMessage", input.modelSelection.instanceId).pipe(
149 + Effect.flatMap((textGeneration) => textGeneration.generateCommitMessage(input)),
150 + ),
151 + generatePrContent: (input) =>
152 + resolveInstance(registry, "generatePrContent", input.modelSelection.instanceId).pipe(
153 + Effect.flatMap((textGeneration) => textGeneration.generatePrContent(input)),
154 + ),
155 + generateBranchName: (input) =>
156 + resolveInstance(registry, "generateBranchName", input.modelSelection.instanceId).pipe(
157 + Effect.flatMap((textGeneration) => textGeneration.generateBranchName(input)),
158 + ),
159 + generateThreadTitle: (input) =>
160 + resolveInstance(registry, "generateThreadTitle", input.modelSelection.instanceId).pipe(
161 + Effect.flatMap((textGeneration) => textGeneration.generateThreadTitle(input)),
162 + ),
163 +});
164 +
165 +export const layer = Layer.effect(
166 + TextGeneration,
167 + Effect.gen(function* () {
168 + const registry = yield* ProviderInstanceRegistry;
169 + return makeTextGenerationFromRegistry(registry);
170 + }),
171 +);
apps/server/src/textGeneration/TextGenerationPolicy.ts new
+19
@@ -0,0 +1,19 @@
1 +import { Schema } from "effect";
2 +
3 +export const TextGenerationPolicyKind = Schema.Literals([
4 + "default",
5 + "conventional_commits",
6 + "repo_conventions",
7 + "custom",
8 +]);
9 +export type TextGenerationPolicyKind = typeof TextGenerationPolicyKind.Type;
10 +
11 +export const TextGenerationPolicy = Schema.Struct({
12 + kind: TextGenerationPolicyKind,
13 + commitInstructions: Schema.optional(Schema.String),
14 + changeRequestInstructions: Schema.optional(Schema.String),
15 + branchInstructions: Schema.optional(Schema.String),
16 + threadTitleInstructions: Schema.optional(Schema.String),
17 + inferRepositoryConventions: Schema.Boolean,
18 +});
19 +export type TextGenerationPolicy = typeof TextGenerationPolicy.Type;
apps/server/src/textGeneration/TextGenerationPresets.ts new
+41
@@ -0,0 +1,41 @@
1 +import type { TextGenerationPolicy, TextGenerationPolicyKind } from "./TextGenerationPolicy.ts";
2 +
3 +export const defaultTextGenerationPolicy: TextGenerationPolicy = {
4 + kind: "default",
5 + inferRepositoryConventions: false,
6 +};
7 +
8 +export const conventionalCommitsTextGenerationPolicy: TextGenerationPolicy = {
9 + kind: "conventional_commits",
10 + commitInstructions:
11 + "Use Conventional Commits when generating commit subjects. Prefer the narrowest accurate type and include a scope only when it is obvious from the diff.",
12 + changeRequestInstructions:
13 + "Keep the change request title concise. Do not force Conventional Commit syntax into the title unless the repository already uses it.",
14 + inferRepositoryConventions: false,
15 +};
16 +
17 +export const repositoryConventionsTextGenerationPolicy: TextGenerationPolicy = {
18 + kind: "repo_conventions",
19 + commitInstructions:
20 + "Follow the repository's established commit message style when examples are available.",
21 + changeRequestInstructions:
22 + "Follow the repository's established change request title and body style when examples are available.",
23 + inferRepositoryConventions: true,
24 +};
25 +
26 +export const customTextGenerationPolicy = (
27 + overrides: Omit<Partial<TextGenerationPolicy>, "kind">,
28 +): TextGenerationPolicy => ({
29 + kind: "custom",
30 + inferRepositoryConventions: false,
31 + ...overrides,
32 +});
33 +
34 +export const textGenerationPresets: Record<
35 + Exclude<TextGenerationPolicyKind, "custom">,
36 + TextGenerationPolicy
37 +> = {
38 + default: defaultTextGenerationPolicy,
39 + conventional_commits: conventionalCommitsTextGenerationPolicy,
40 + repo_conventions: repositoryConventionsTextGenerationPolicy,
41 +};
apps/server/src/textGeneration/TextGenerationPrompts.test.ts renamed
+2 -2
@@ -5,8 +5,8 @@ import {
5 buildCommitMessagePrompt,
6 buildPrContentPrompt,
7 buildThreadTitlePrompt,
8 -} from "./Prompts.ts";
9 -import { normalizeCliError, sanitizeThreadTitle } from "./Utils.ts";
8 +} from "./TextGenerationPrompts.ts";
9 +import { normalizeCliError, sanitizeThreadTitle } from "./TextGenerationUtils.ts";
10 import { TextGenerationError } from "@t3tools/contracts";
11
12 describe("buildCommitMessagePrompt", () => {
apps/server/src/textGeneration/TextGenerationPrompts.ts renamed
+17 -1
@@ -9,7 +9,13 @@
9 import { Schema } from "effect";
10 import type { ChatAttachment } from "@t3tools/contracts";
11
12 -import { limitSection } from "./Utils.ts";
12 +import { limitSection } from "./TextGenerationUtils.ts";
13 +import type { TextGenerationPolicy } from "./TextGenerationPolicy.ts";
14 +
15 +function policyInstruction(instruction: string | undefined): ReadonlyArray<string> {
16 + const trimmed = instruction?.trim();
17 + return trimmed ? ["", "Additional instructions:", limitSection(trimmed, 4_000)] : [];
18 +}
19
20 // ---------------------------------------------------------------------------
21 // Commit message
@@ -20,6 +26,7 @@ export interface CommitMessagePromptInput {
26 stagedSummary: string;
27 stagedPatch: string;
28 includeBranch: boolean;
29 + policy?: TextGenerationPolicy | undefined;
30 }
31
32 export function buildCommitMessagePrompt(input: CommitMessagePromptInput) {
@@ -37,6 +44,7 @@ export function buildCommitMessagePrompt(input: CommitMessagePromptInput) {
44 ? ["- branch must be a short semantic git branch fragment for this change"]
45 : []),
46 "- capture the primary user-visible or developer-visible change",
47 + ...policyInstruction(input.policy?.commitInstructions),
48 "",
49 `Branch: ${input.branch ?? "(detached)"}`,
50 "",
@@ -77,6 +85,7 @@ export interface PrContentPromptInput {
85 commitSummary: string;
86 diffSummary: string;
87 diffPatch: string;
88 + policy?: TextGenerationPolicy | undefined;
89 }
90
91 export function buildPrContentPrompt(input: PrContentPromptInput) {
@@ -88,6 +97,7 @@ export function buildPrContentPrompt(input: PrContentPromptInput) {
97 "- body must be markdown and include headings '## Summary' and '## Testing'",
98 "- under Summary, provide short bullet points",
99 "- under Testing, include bullet points with concrete checks or 'Not run' where appropriate",
100 + ...policyInstruction(input.policy?.changeRequestInstructions),
101 "",
102 `Base branch: ${input.baseBranch}`,
103 `Head branch: ${input.headBranch}`,
@@ -117,6 +127,7 @@ export function buildPrContentPrompt(input: PrContentPromptInput) {
127 export interface BranchNamePromptInput {
128 message: string;
129 attachments?: ReadonlyArray<ChatAttachment> | undefined;
130 + policy?: TextGenerationPolicy | undefined;
131 }
132
133 interface PromptFromMessageInput {
@@ -125,6 +136,7 @@ interface PromptFromMessageInput {
136 rules: ReadonlyArray<string>;
137 message: string;
138 attachments?: ReadonlyArray<ChatAttachment> | undefined;
139 + additionalInstructions?: string | undefined;
140 }
141
142 function buildPromptFromMessage(input: PromptFromMessageInput): string {
@@ -140,6 +152,7 @@ function buildPromptFromMessage(input: PromptFromMessageInput): string {
152 "",
153 "User message:",
154 limitSection(input.message, 8_000),
155 + ...policyInstruction(input.additionalInstructions),
156 ];
157 if (attachmentLines.length > 0) {
158 promptSections.push(
@@ -164,6 +177,7 @@ export function buildBranchNamePrompt(input: BranchNamePromptInput) {
177 ],
178 message: input.message,
179 attachments: input.attachments,
180 + additionalInstructions: input.policy?.branchInstructions,
181 });
182 const outputSchema = Schema.Struct({
183 branch: Schema.String,
@@ -179,6 +193,7 @@ export function buildBranchNamePrompt(input: BranchNamePromptInput) {
193 export interface ThreadTitlePromptInput {
194 message: string;
195 attachments?: ReadonlyArray<ChatAttachment> | undefined;
196 + policy?: TextGenerationPolicy | undefined;
197 }
198
199 export function buildThreadTitlePrompt(input: ThreadTitlePromptInput) {
@@ -193,6 +208,7 @@ export function buildThreadTitlePrompt(input: ThreadTitlePromptInput) {
208 ],
209 message: input.message,
210 attachments: input.attachments,
211 + additionalInstructions: input.policy?.threadTitleInstructions,
212 });
213 const outputSchema = Schema.Struct({
214 title: Schema.String,
apps/server/src/textGeneration/TextGenerationUtils.ts new
+159
@@ -0,0 +1,159 @@
1 +import { Schema } from "effect";
2 +
3 +import { TextGenerationError } from "@t3tools/contracts";
4 +
5 +/** Convert an Effect Schema to a flat JSON Schema object, inlining `$defs` when present. */
6 +export function toJsonSchemaObject(schema: Schema.Top): unknown {
7 + const document = Schema.toJsonSchemaDocument(schema);
8 + if (document.definitions && Object.keys(document.definitions).length > 0) {
9 + return { ...document.schema, $defs: document.definitions };
10 + }
11 + return document.schema;
12 +}
13 +
14 +/** Truncate a text section to `maxChars`, appending a `[truncated]` marker when needed. */
15 +export function limitSection(value: string, maxChars: number): string {
16 + if (value.length <= maxChars) return value;
17 + const truncated = value.slice(0, maxChars);
18 + return `${truncated}\n\n[truncated]`;
19 +}
20 +
21 +export function extractJsonObject(raw: string): string {
22 + const trimmed = raw.trim();
23 + if (trimmed.length === 0) {
24 + return trimmed;
25 + }
26 +
27 + const start = trimmed.indexOf("{");
28 + if (start < 0) {
29 + return trimmed;
30 + }
31 +
32 + let depth = 0;
33 + let inString = false;
34 + let escaping = false;
35 + for (let index = start; index < trimmed.length; index += 1) {
36 + const char = trimmed[index];
37 + if (inString) {
38 + if (escaping) {
39 + escaping = false;
40 + } else if (char === "\\") {
41 + escaping = true;
42 + } else if (char === '"') {
43 + inString = false;
44 + }
45 + continue;
46 + }
47 +
48 + if (char === '"') {
49 + inString = true;
50 + continue;
51 + }
52 +
53 + if (char === "{") {
54 + depth += 1;
55 + continue;
56 + }
57 +
58 + if (char === "}") {
59 + depth -= 1;
60 + if (depth === 0) {
61 + return trimmed.slice(start, index + 1);
62 + }
63 + }
64 + }
65 +
66 + return trimmed.slice(start);
67 +}
68 +
69 +/** Normalise a raw commit subject to imperative-mood, ≤72 chars, no trailing period. */
70 +export function sanitizeCommitSubject(raw: string): string {
71 + const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
72 + const withoutTrailingPeriod = singleLine.replace(/[.]+$/g, "").trim();
73 + if (withoutTrailingPeriod.length === 0) {
74 + return "Update project files";
75 + }
76 +
77 + if (withoutTrailingPeriod.length <= 72) {
78 + return withoutTrailingPeriod;
79 + }
80 + return withoutTrailingPeriod.slice(0, 72).trimEnd();
81 +}
82 +
83 +/** Normalise a raw PR title to a single line with a sensible fallback. */
84 +export function sanitizePrTitle(raw: string): string {
85 + const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
86 + if (singleLine.length > 0) {
87 + return singleLine;
88 + }
89 + return "Update project changes";
90 +}
91 +
92 +/** Normalise a raw thread title to a compact single-line sidebar-safe label. */
93 +export function sanitizeThreadTitle(raw: string): string {
94 + const normalized = raw
95 + .trim()
96 + .split(/\r?\n/g)[0]
97 + ?.trim()
98 + .replace(/^['"`]+|['"`]+$/g, "")
99 + .trim()
100 + .replace(/\s+/g, " ");
101 +
102 + if (!normalized || normalized.trim().length === 0) {
103 + return "New thread";
104 + }
105 +
106 + if (normalized.length <= 50) {
107 + return normalized;
108 + }
109 +
110 + return `${normalized.slice(0, 47).trimEnd()}...`;
111 +}
112 +
113 +/** CLI name to human-readable label, e.g. "codex" → "Codex CLI (`codex`)" */
114 +function cliLabel(cliName: string): string {
115 + const capitalized = cliName.charAt(0).toUpperCase() + cliName.slice(1);
116 + return `${capitalized} CLI (\`${cliName}\`)`;
117 +}
118 +
119 +/**
120 + * Normalize an unknown error from a CLI text generation process into a
121 + * typed `TextGenerationError`. Parameterized by CLI name so both Codex
122 + * and Claude (and future providers) can share the same logic.
123 + */
124 +export function normalizeCliError(
125 + cliName: string,
126 + operation: string,
127 + error: unknown,
128 + fallback: string,
129 +): TextGenerationError {
130 + if (Schema.is(TextGenerationError)(error)) {
131 + return error;
132 + }
133 +
134 + if (error instanceof Error) {
135 + const lower = error.message.toLowerCase();
136 + if (
137 + error.message.includes(`Command not found: ${cliName}`) ||
138 + lower.includes(`spawn ${cliName}`) ||
139 + lower.includes("enoent")
140 + ) {
141 + return new TextGenerationError({
142 + operation,
143 + detail: `${cliLabel(cliName)} is required but not available on PATH.`,
144 + cause: error,
145 + });
146 + }
147 + return new TextGenerationError({
148 + operation,
149 + detail: `${fallback}: ${error.message}`,
150 + cause: error,
151 + });
152 + }
153 +
154 + return new TextGenerationError({
155 + operation,
156 + detail: fallback,
157 + cause: error,
158 + });
159 +}
apps/server/src/vcs/GitVcsDriver.test.ts new
+99
@@ -0,0 +1,99 @@
1 +import * as NodeServices from "@effect/platform-node/NodeServices";
2 +import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
3 +import { ChildProcessSpawner } from "effect/unstable/process";
4 +import { assert, it } from "@effect/vitest";
5 +
6 +import { GitCommandError } from "@t3tools/contracts";
7 +import { ServerConfig } from "../config.ts";
8 +import * as GitVcsDriver from "./GitVcsDriver.ts";
9 +import * as VcsProcess from "./VcsProcess.ts";
10 +import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
11 +
12 +const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
13 + prefix: "t3-git-vcs-contract-",
14 +});
15 +const GitContractLayer = Layer.mergeAll(GitVcsDriver.vcsLayer, GitVcsDriver.layer).pipe(
16 + Layer.provide(ServerConfigLayer),
17 + Layer.provideMerge(VcsProcess.layer),
18 + Layer.provideMerge(NodeServices.layer),
19 +);
20 +
21 +const runGit = (cwd: string, args: ReadonlyArray<string>) =>
22 + Effect.gen(function* () {
23 + const driver = yield* GitVcsDriver.GitVcsDriver;
24 + yield* driver.execute({
25 + operation: "GitVcsDriver.contract.git",
26 + cwd,
27 + args,
28 + timeoutMs: 10_000,
29 + });
30 + });
31 +
32 +type GitContractError = GitCommandError | PlatformError.PlatformError;
33 +
34 +runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
35 + name: "Git",
36 + kind: "git",
37 + layer: GitContractLayer,
38 + fixture: {
39 + createRepo: (cwd) =>
40 + Effect.gen(function* () {
41 + yield* runGit(cwd, ["init"]);
42 + yield* runGit(cwd, ["config", "user.email", "test@test.com"]);
43 + yield* runGit(cwd, ["config", "user.name", "Test"]);
44 + }),
45 + writeFile: (cwd, relativePath, contents) =>
46 + Effect.gen(function* () {
47 + const fileSystem = yield* FileSystem.FileSystem;
48 + const path = yield* Path.Path;
49 + const absolutePath = path.join(cwd, relativePath);
50 + yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
51 + yield* fileSystem.writeFileString(absolutePath, contents);
52 + }),
53 + trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
54 + commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
55 + ignorePath: (cwd, pattern) =>
56 + Effect.gen(function* () {
57 + const fileSystem = yield* FileSystem.FileSystem;
58 + const path = yield* Path.Path;
59 + yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
60 + }),
61 + },
62 +});
63 +
64 +it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
65 + let observedEnv: NodeJS.ProcessEnv | undefined;
66 +
67 + return Effect.gen(function* () {
68 + const driver = yield* GitVcsDriver.makeVcsDriverShape();
69 +
70 + yield* driver.execute({
71 + operation: "GitVcsDriver.test.env",
72 + cwd: "/repo",
73 + args: ["status"],
74 + env: {
75 + GIT_INDEX_FILE: "/tmp/t3-index",
76 + },
77 + });
78 +
79 + assert.deepStrictEqual(observedEnv, {
80 + GIT_INDEX_FILE: "/tmp/t3-index",
81 + });
82 + }).pipe(
83 + Effect.provide(
84 + Layer.mock(VcsProcess.VcsProcess)({
85 + run: (input) =>
86 + Effect.sync(() => {
87 + observedEnv = input.env;
88 + return {
89 + exitCode: ChildProcessSpawner.ExitCode(0),
90 + stdout: "",
91 + stderr: "",
92 + stdoutTruncated: false,
93 + stderrTruncated: false,
94 + };
95 + }),
96 + }),
97 + ),
98 + );
99 +});
apps/server/src/vcs/GitVcsDriver.ts new
+543
@@ -0,0 +1,543 @@
1 +import { Context, DateTime, Effect, Layer, Option } from "effect";
2 +import { ChildProcessSpawner } from "effect/unstable/process";
3 +
4 +import {
5 + GitCommandError,
6 + VcsProcessExitError,
7 + type VcsSwitchRefInput,
8 + type VcsSwitchRefResult,
9 + type VcsCreateRefInput,
10 + type VcsCreateRefResult,
11 + type VcsCreateWorktreeInput,
12 + type VcsCreateWorktreeResult,
13 + type VcsInitInput,
14 + type VcsListRefsInput,
15 + type VcsListRefsResult,
16 + type VcsPullResult,
17 + type VcsRemoveWorktreeInput,
18 + type VcsStatusInput,
19 + type VcsStatusResult,
20 +} from "@t3tools/contracts";
21 +import { makeGitVcsDriverCore } from "./GitVcsDriverCore.ts";
22 +import { VcsDriver, type VcsDriverShape } from "./VcsDriver.ts";
23 +import { VcsProcess, type VcsProcessShape } from "./VcsProcess.ts";
24 +
25 +export interface ExecuteGitInput {
26 + readonly operation: string;
27 + readonly cwd: string;
28 + readonly args: ReadonlyArray<string>;
29 + readonly stdin?: string;
30 + readonly env?: NodeJS.ProcessEnv;
31 + readonly allowNonZeroExit?: boolean;
32 + readonly timeoutMs?: number;
33 + readonly maxOutputBytes?: number;
34 + readonly truncateOutputAtMaxBytes?: boolean;
35 + readonly progress?: ExecuteGitProgress;
36 +}
37 +
38 +export interface ExecuteGitResult {
39 + readonly exitCode: ChildProcessSpawner.ExitCode;
40 + readonly stdout: string;
41 + readonly stderr: string;
42 + readonly stdoutTruncated: boolean;
43 + readonly stderrTruncated: boolean;
44 +}
45 +
46 +export interface GitStatusDetails {
47 + isRepo: boolean;
48 + sourceControlProvider?: VcsStatusResult["sourceControlProvider"];
49 + hasOriginRemote: boolean;
50 + isDefaultBranch: boolean;
51 + branch: string | null;
52 + upstreamRef: string | null;
53 + hasWorkingTreeChanges: boolean;
54 + workingTree: VcsStatusResult["workingTree"];
55 + hasUpstream: boolean;
56 + aheadCount: number;
57 + behindCount: number;
58 +}
59 +
60 +export interface GitPreparedCommitContext {
61 + stagedSummary: string;
62 + stagedPatch: string;
63 +}
64 +
65 +export interface ExecuteGitProgress {
66 + readonly onStdoutLine?: (line: string) => Effect.Effect<void, never>;
67 + readonly onStderrLine?: (line: string) => Effect.Effect<void, never>;
68 + readonly onHookStarted?: (hookName: string) => Effect.Effect<void, never>;
69 + readonly onHookFinished?: (input: {
70 + hookName: string;
71 + exitCode: number | null;
72 + durationMs: number | null;
73 + }) => Effect.Effect<void, never>;
74 +}
75 +
76 +export interface GitCommitProgress {
77 + readonly onOutputLine?: (input: {
78 + stream: "stdout" | "stderr";
79 + text: string;
80 + }) => Effect.Effect<void, never>;
81 + readonly onHookStarted?: (hookName: string) => Effect.Effect<void, never>;
82 + readonly onHookFinished?: (input: {
83 + hookName: string;
84 + exitCode: number | null;
85 + durationMs: number | null;
86 + }) => Effect.Effect<void, never>;
87 +}
88 +
89 +export interface GitCommitOptions {
90 + readonly timeoutMs?: number;
91 + readonly progress?: GitCommitProgress;
92 +}
93 +
94 +export interface GitPushResult {
95 + status: "pushed" | "skipped_up_to_date";
96 + branch: string;
97 + upstreamBranch?: string | undefined;
98 + setUpstream?: boolean | undefined;
99 +}
100 +
101 +export interface GitRangeContext {
102 + commitSummary: string;
103 + diffSummary: string;
104 + diffPatch: string;
105 +}
106 +
107 +export interface GitRenameBranchInput {
108 + cwd: string;
109 + oldBranch: string;
110 + newBranch: string;
111 +}
112 +
113 +export interface GitRenameBranchResult {
114 + branch: string;
115 +}
116 +
117 +export interface GitFetchPullRequestBranchInput {
118 + cwd: string;
119 + prNumber: number;
120 + branch: string;
121 +}
122 +
123 +export interface GitEnsureRemoteInput {
124 + cwd: string;
125 + preferredName: string;
126 + url: string;
127 +}
128 +
129 +export interface GitFetchRemoteBranchInput {
130 + cwd: string;
131 + remoteName: string;
132 + remoteBranch: string;
133 + localBranch: string;
134 +}
135 +
136 +export interface GitSetBranchUpstreamInput {
137 + cwd: string;
138 + branch: string;
139 + remoteName: string;
140 + remoteBranch: string;
141 +}
142 +
143 +export interface GitVcsDriverShape {
144 + readonly execute: (input: ExecuteGitInput) => Effect.Effect<ExecuteGitResult, GitCommandError>;
145 + readonly status: (input: VcsStatusInput) => Effect.Effect<VcsStatusResult, GitCommandError>;
146 + readonly statusDetails: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
147 + readonly statusDetailsLocal: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
148 + readonly prepareCommitContext: (
149 + cwd: string,
150 + filePaths?: readonly string[],
151 + ) => Effect.Effect<GitPreparedCommitContext | null, GitCommandError>;
152 + readonly commit: (
153 + cwd: string,
154 + subject: string,
155 + body: string,
156 + options?: GitCommitOptions,
157 + ) => Effect.Effect<{ commitSha: string }, GitCommandError>;
158 + readonly pushCurrentBranch: (
159 + cwd: string,
160 + fallbackBranch: string | null,
161 + ) => Effect.Effect<GitPushResult, GitCommandError>;
162 + readonly readRangeContext: (
163 + cwd: string,
164 + baseRef: string,
165 + ) => Effect.Effect<GitRangeContext, GitCommandError>;
166 + readonly readConfigValue: (
167 + cwd: string,
168 + key: string,
169 + ) => Effect.Effect<string | null, GitCommandError>;
170 + readonly listRefs: (input: VcsListRefsInput) => Effect.Effect<VcsListRefsResult, GitCommandError>;
171 + readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
172 + readonly createWorktree: (
173 + input: VcsCreateWorktreeInput,
174 + ) => Effect.Effect<VcsCreateWorktreeResult, GitCommandError>;
175 + readonly fetchPullRequestBranch: (
176 + input: GitFetchPullRequestBranchInput,
177 + ) => Effect.Effect<void, GitCommandError>;
178 + readonly ensureRemote: (input: GitEnsureRemoteInput) => Effect.Effect<string, GitCommandError>;
179 + readonly fetchRemoteBranch: (
180 + input: GitFetchRemoteBranchInput,
181 + ) => Effect.Effect<void, GitCommandError>;
182 + readonly setBranchUpstream: (
183 + input: GitSetBranchUpstreamInput,
184 + ) => Effect.Effect<void, GitCommandError>;
185 + readonly removeWorktree: (input: VcsRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
186 + readonly renameBranch: (
187 + input: GitRenameBranchInput,
188 + ) => Effect.Effect<GitRenameBranchResult, GitCommandError>;
189 + readonly createRef: (
190 + input: VcsCreateRefInput,
191 + ) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
192 + readonly switchRef: (
193 + input: VcsSwitchRefInput,
194 + ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
195 + readonly initRepo: (input: VcsInitInput) => Effect.Effect<void, GitCommandError>;
196 + readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
197 +}
198 +
199 +export class GitVcsDriver extends Context.Service<GitVcsDriver, GitVcsDriverShape>()(
200 + "t3/vcs/GitVcsDriver",
201 +) {}
202 +
203 +const WORKSPACE_FILES_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
204 +const GIT_CHECK_IGNORE_MAX_STDIN_BYTES = 256 * 1024;
205 +const WORKSPACE_GIT_HARDENED_CONFIG_ARGS = [
206 + "-c",
207 + "core.fsmonitor=false",
208 + "-c",
209 + "core.untrackedCache=false",
210 +] as const;
211 +
212 +const nowFreshness = Effect.fn("GitVcsDriver.nowFreshness")(function* () {
213 + const now = yield* DateTime.now;
214 + return {
215 + source: "live-local" as const,
216 + observedAt: now,
217 + expiresAt: Option.none(),
218 + };
219 +});
220 +
221 +function splitNullSeparatedPaths(input: string, truncated: boolean): string[] {
222 + const parts = input.split("\0");
223 + if (parts.length === 0) return [];
224 +
225 + if (truncated && parts[parts.length - 1]?.length) {
226 + parts.pop();
227 + }
228 +
229 + return parts.filter((value) => value.length > 0);
230 +}
231 +
232 +function chunkPathsForGitCheckIgnore(relativePaths: ReadonlyArray<string>): string[][] {
233 + const chunks: string[][] = [];
234 + let chunk: string[] = [];
235 + let chunkBytes = 0;
236 +
237 + for (const relativePath of relativePaths) {
238 + const relativePathBytes = Buffer.byteLength(relativePath) + 1;
239 + if (chunk.length > 0 && chunkBytes + relativePathBytes > GIT_CHECK_IGNORE_MAX_STDIN_BYTES) {
240 + chunks.push(chunk);
241 + chunk = [];
242 + chunkBytes = 0;
243 + }
244 +
245 + chunk.push(relativePath);
246 + chunkBytes += relativePathBytes;
247 +
248 + if (chunkBytes >= GIT_CHECK_IGNORE_MAX_STDIN_BYTES) {
249 + chunks.push(chunk);
250 + chunk = [];
251 + chunkBytes = 0;
252 + }
253 + }
254 +
255 + if (chunk.length > 0) {
256 + chunks.push(chunk);
257 + }
258 +
259 + return chunks;
260 +}
261 +
262 +function parseGitRemoteVerboseOutput(
263 + output: string,
264 +): Map<string, { url?: string; pushUrl?: string }> {
265 + const remotes = new Map<string, { url?: string; pushUrl?: string }>();
266 + for (const line of output.split("\n")) {
267 + const trimmed = line.trim();
268 + if (trimmed.length === 0) {
269 + continue;
270 + }
271 +
272 + const match = /^(\S+)\s+(\S+)\s+\((fetch|push)\)$/.exec(trimmed);
273 + if (!match) {
274 + continue;
275 + }
276 +
277 + const name = match[1];
278 + const url = match[2];
279 + const direction = match[3];
280 + if (!name || !url || !direction) {
281 + continue;
282 + }
283 + const remote = remotes.get(name) ?? {};
284 + if (direction === "fetch") {
285 + remote.url = url;
286 + } else {
287 + remote.pushUrl = url;
288 + }
289 + remotes.set(name, remote);
290 + }
291 + return remotes;
292 +}
293 +
294 +const gitCommand = (
295 + process: VcsProcessShape,
296 + operation: string,
297 + cwd: string,
298 + args: ReadonlyArray<string>,
299 + options?: {
300 + readonly stdin?: string;
301 + readonly env?: NodeJS.ProcessEnv;
302 + readonly allowNonZeroExit?: boolean;
303 + readonly timeoutMs?: number;
304 + readonly maxOutputBytes?: number;
305 + readonly truncateOutputAtMaxBytes?: boolean;
306 + },
307 +) =>
308 + process.run({
309 + operation,
310 + command: "git",
311 + args,
312 + cwd,
313 + ...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
314 + ...(options?.env !== undefined ? { env: options.env } : {}),
315 + ...(options?.allowNonZeroExit !== undefined
316 + ? { allowNonZeroExit: options.allowNonZeroExit }
317 + : {}),
318 + ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
319 + ...(options?.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
320 + ...(options?.truncateOutputAtMaxBytes !== undefined
321 + ? { truncateOutputAtMaxBytes: options.truncateOutputAtMaxBytes }
322 + : {}),
323 + });
324 +
325 +export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* () {
326 + const process = yield* VcsProcess;
327 + const capabilities = {
328 + kind: "git" as const,
329 + supportsWorktrees: true,
330 + supportsBookmarks: false,
331 + supportsAtomicSnapshot: false,
332 + supportsPushDefaultRemote: true,
333 + ignoreClassifier: "native" as const,
334 + };
335 +
336 + const isInsideWorkTree: VcsDriverShape["isInsideWorkTree"] = (cwd) =>
337 + gitCommand(
338 + process,
339 + "GitVcsDriver.isInsideWorkTree",
340 + cwd,
341 + ["rev-parse", "--is-inside-work-tree"],
342 + {
343 + allowNonZeroExit: true,
344 + timeoutMs: 5_000,
345 + maxOutputBytes: 4_096,
346 + },
347 + ).pipe(Effect.map((result) => result.exitCode === 0 && result.stdout.trim() === "true"));
348 +
349 + const execute: VcsDriverShape["execute"] = (input) =>
350 + gitCommand(process, input.operation, input.cwd, input.args, {
351 + ...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
352 + ...(input.env !== undefined ? { env: input.env } : {}),
353 + ...(input.allowNonZeroExit !== undefined ? { allowNonZeroExit: input.allowNonZeroExit } : {}),
354 + ...(input.timeoutMs !== undefined ? { timeoutMs: input.timeoutMs } : {}),
355 + ...(input.maxOutputBytes !== undefined ? { maxOutputBytes: input.maxOutputBytes } : {}),
356 + ...(input.truncateOutputAtMaxBytes !== undefined
357 + ? { truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes }
358 + : {}),
359 + });
360 +
361 + const detectRepository: VcsDriverShape["detectRepository"] = Effect.fn("detectRepository")(
362 + function* (cwd) {
363 + if (!(yield* isInsideWorkTree(cwd))) {
364 + return null;
365 + }
366 +
367 + const root = yield* gitCommand(process, "GitVcsDriver.detectRepository.root", cwd, [
368 + "rev-parse",
369 + "--show-toplevel",
370 + ]);
371 + const gitCommonDir = yield* gitCommand(
372 + process,
373 + "GitVcsDriver.detectRepository.commonDir",
374 + cwd,
375 + ["rev-parse", "--git-common-dir"],
376 + ).pipe(Effect.catch(() => Effect.succeed(null)));
377 +
378 + return {
379 + kind: "git" as const,
380 + rootPath: root.stdout.trim(),
381 + metadataPath: gitCommonDir?.stdout.trim() || null,
382 + freshness: yield* nowFreshness(),
383 + };
384 + },
385 + );
386 +
387 + const listWorkspaceFiles: VcsDriverShape["listWorkspaceFiles"] = (cwd) =>
388 + gitCommand(
389 + process,
390 + "GitVcsDriver.listWorkspaceFiles",
391 + cwd,
392 + [
393 + ...WORKSPACE_GIT_HARDENED_CONFIG_ARGS,
394 + "ls-files",
395 + "--cached",
396 + "--others",
397 + "--exclude-standard",
398 + "-z",
399 + ],
400 + {
401 + allowNonZeroExit: true,
402 + timeoutMs: 20_000,
403 + maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
404 + truncateOutputAtMaxBytes: true,
405 + },
406 + ).pipe(
407 + Effect.flatMap((result) =>
408 + result.exitCode === 0
409 + ? Effect.gen(function* () {
410 + const freshness = yield* nowFreshness();
411 + return {
412 + paths: splitNullSeparatedPaths(result.stdout, result.stdoutTruncated),
413 + truncated: result.stdoutTruncated,
414 + freshness,
415 + };
416 + })
417 + : Effect.fail(
418 + new VcsProcessExitError({
419 + operation: "GitVcsDriver.listWorkspaceFiles",
420 + command: "git ls-files",
421 + cwd,
422 + exitCode: result.exitCode,
423 + detail: result.stderr.trim() || "git ls-files failed",
424 + }),
425 + ),
426 + ),
427 + );
428 +
429 + const listRemotes: VcsDriverShape["listRemotes"] = Effect.fn("listRemotes")(function* (cwd) {
430 + const result = yield* gitCommand(process, "GitVcsDriver.listRemotes", cwd, ["remote", "-v"], {
431 + allowNonZeroExit: true,
432 + timeoutMs: 5_000,
433 + maxOutputBytes: 64 * 1024,
434 + });
435 +
436 + if (result.exitCode !== 0) {
437 + return yield* new VcsProcessExitError({
438 + operation: "GitVcsDriver.listRemotes",
439 + command: "git remote -v",
440 + cwd,
441 + exitCode: result.exitCode,
442 + detail: result.stderr.trim() || "git remote -v failed",
443 + });
444 + }
445 +
446 + const parsed = parseGitRemoteVerboseOutput(result.stdout);
447 + const remotes = Array.from(parsed.entries()).flatMap(([name, remote]) => {
448 + if (!remote.url) {
449 + return [];
450 + }
451 + return [
452 + {
453 + name,
454 + url: remote.url,
455 + pushUrl: remote.pushUrl ? Option.some(remote.pushUrl) : Option.none(),
456 + isPrimary: name === "origin",
457 + },
458 + ];
459 + });
460 +
461 + return {
462 + remotes,
463 + freshness: yield* nowFreshness(),
464 + };
465 + });
466 +
467 + const filterIgnoredPaths: VcsDriverShape["filterIgnoredPaths"] = Effect.fn("filterIgnoredPaths")(
468 + function* (cwd, relativePaths) {
469 + if (relativePaths.length === 0) {
470 + return relativePaths;
471 + }
472 +
473 + const ignoredPaths = new Set<string>();
474 + const chunks = chunkPathsForGitCheckIgnore(relativePaths);
475 +
476 + for (const chunk of chunks) {
477 + const result = yield* gitCommand(
478 + process,
479 + "GitVcsDriver.filterIgnoredPaths",
480 + cwd,
481 + [...WORKSPACE_GIT_HARDENED_CONFIG_ARGS, "check-ignore", "--no-index", "-z", "--stdin"],
482 + {
483 + stdin: `${chunk.join("\0")}\0`,
484 + allowNonZeroExit: true,
485 + timeoutMs: 20_000,
486 + maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
487 + truncateOutputAtMaxBytes: true,
488 + },
489 + );
490 +
491 + if (result.exitCode !== 0 && result.exitCode !== 1) {
492 + return yield* new VcsProcessExitError({
493 + operation: "GitVcsDriver.filterIgnoredPaths",
494 + command: "git check-ignore",
495 + cwd,
496 + exitCode: result.exitCode,
497 + detail: result.stderr.trim() || "git check-ignore failed",
498 + });
499 + }
500 +
501 + for (const ignoredPath of splitNullSeparatedPaths(result.stdout, result.stdoutTruncated)) {
502 + ignoredPaths.add(ignoredPath);
503 + }
504 + }
505 +
506 + if (ignoredPaths.size === 0) {
507 + return relativePaths;
508 + }
509 +
510 + return relativePaths.filter((relativePath) => !ignoredPaths.has(relativePath));
511 + },
512 + );
513 +
514 + const initRepository: VcsDriverShape["initRepository"] = (input) =>
515 + gitCommand(process, "GitVcsDriver.initRepository", input.cwd, ["init"], {
516 + timeoutMs: 10_000,
517 + maxOutputBytes: 64 * 1024,
518 + }).pipe(Effect.asVoid);
519 +
520 + return {
521 + capabilities,
522 + execute,
523 + detectRepository,
524 + isInsideWorkTree,
525 + listWorkspaceFiles,
526 + listRemotes,
527 + filterIgnoredPaths,
528 + initRepository,
529 + } satisfies VcsDriverShape;
530 +});
531 +
532 +export const makeVcsDriver = Effect.fn("makeGitVcsDriver")(function* () {
533 + const driver = yield* makeVcsDriverShape();
534 + return VcsDriver.of(driver);
535 +});
536 +
537 +export const make = Effect.fn("makeGitVcsDriverService")(function* () {
538 + const git = yield* makeGitVcsDriverCore();
539 + return GitVcsDriver.of(git);
540 +});
541 +
542 +export const vcsLayer = Layer.effect(VcsDriver, makeVcsDriver());
543 +export const layer = Layer.effect(GitVcsDriver, make());
apps/server/src/vcs/GitVcsDriverCore.test.ts new
+246
@@ -0,0 +1,246 @@
1 +import * as NodeServices from "@effect/platform-node/NodeServices";
2 +import { assert, it } from "@effect/vitest";
3 +import { Effect, FileSystem, Layer, Path, PlatformError, Scope } from "effect";
4 +import { describe } from "vitest";
5 +
6 +import { GitCommandError } from "@t3tools/contracts";
7 +import { ServerConfig } from "../config.ts";
8 +import * as GitVcsDriver from "./GitVcsDriver.ts";
9 +
10 +const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
11 + prefix: "t3-git-vcs-driver-test-",
12 +});
13 +const TestLayer = GitVcsDriver.layer.pipe(
14 + Layer.provide(ServerConfigLayer),
15 + Layer.provideMerge(NodeServices.layer),
16 +);
17 +
18 +const makeTmpDir = (
19 + prefix = "git-vcs-driver-test-",
20 +): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
21 + Effect.gen(function* () {
22 + const fileSystem = yield* FileSystem.FileSystem;
23 + return yield* fileSystem.makeTempDirectoryScoped({ prefix });
24 + });
25 +
26 +const writeTextFile = (
27 + cwd: string,
28 + relativePath: string,
29 + contents: string,
30 +): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> =>
31 + Effect.gen(function* () {
32 + const fileSystem = yield* FileSystem.FileSystem;
33 + const pathService = yield* Path.Path;
34 + const filePath = pathService.join(cwd, relativePath);
35 + yield* fileSystem.makeDirectory(pathService.dirname(filePath), { recursive: true });
36 + yield* fileSystem.writeFileString(filePath, contents);
37 + });
38 +
39 +const git = (
40 + cwd: string,
41 + args: ReadonlyArray<string>,
42 + env?: NodeJS.ProcessEnv,
43 +): Effect.Effect<string, GitCommandError, GitVcsDriver.GitVcsDriver> =>
44 + Effect.gen(function* () {
45 + const driver = yield* GitVcsDriver.GitVcsDriver;
46 + const result = yield* driver.execute({
47 + operation: "GitVcsDriver.test.git",
48 + cwd,
49 + args,
50 + ...(env ? { env } : {}),
51 + timeoutMs: 10_000,
52 + });
53 + return result.stdout.trim();
54 + });
55 +
56 +const initRepoWithCommit = (
57 + cwd: string,
58 +): Effect.Effect<
59 + { readonly initialBranch: string },
60 + GitCommandError | PlatformError.PlatformError,
61 + GitVcsDriver.GitVcsDriver | FileSystem.FileSystem | Path.Path
62 +> =>
63 + Effect.gen(function* () {
64 + const driver = yield* GitVcsDriver.GitVcsDriver;
65 + yield* driver.initRepo({ cwd });
66 + yield* git(cwd, ["config", "user.email", "test@test.com"]);
67 + yield* git(cwd, ["config", "user.name", "Test"]);
68 + yield* writeTextFile(cwd, "README.md", "# test\n");
69 + yield* git(cwd, ["add", "."]);
70 + yield* git(cwd, ["commit", "-m", "initial commit"]);
71 + const initialBranch = yield* git(cwd, ["branch", "--show-current"]);
72 + return { initialBranch };
73 + });
74 +
75 +it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
76 + describe("repository status", () => {
77 + it.effect("reports non-repository directories without failing", () =>
78 + Effect.gen(function* () {
79 + const cwd = yield* makeTmpDir();
80 + const driver = yield* GitVcsDriver.GitVcsDriver;
81 +
82 + const refs = yield* driver.listRefs({ cwd });
83 + assert.equal(refs.isRepo, false);
84 + assert.deepStrictEqual(refs.refs, []);
85 + }),
86 + );
87 +
88 + it.effect("reports refName and dirty state for a repository", () =>
89 + Effect.gen(function* () {
90 + const cwd = yield* makeTmpDir();
91 + const { initialBranch } = yield* initRepoWithCommit(cwd);
92 + yield* writeTextFile(cwd, "feature.ts", "export const value = 1;\n");
93 +
94 + const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetails(cwd);
95 +
96 + assert.equal(status.isRepo, true);
97 + assert.equal(status.branch, initialBranch);
98 + assert.equal(status.hasWorkingTreeChanges, true);
99 + assert.include(
100 + status.workingTree.files.map((file) => file.path),
101 + "feature.ts",
102 + );
103 + }),
104 + );
105 + });
106 +
107 + describe("refName operations", () => {
108 + it.effect("creates, checks out, renames, and lists refs", () =>
109 + Effect.gen(function* () {
110 + const cwd = yield* makeTmpDir();
111 + yield* initRepoWithCommit(cwd);
112 + const driver = yield* GitVcsDriver.GitVcsDriver;
113 +
114 + yield* driver.createRef({ cwd, refName: "feature/original" });
115 + const switchRef = yield* driver.switchRef({ cwd, refName: "feature/original" });
116 + assert.equal(switchRef.refName, "feature/original");
117 +
118 + const renamed = yield* driver.renameBranch({
119 + cwd,
120 + oldBranch: "feature/original",
121 + newBranch: "feature/renamed",
122 + });
123 + assert.equal(renamed.branch, "feature/renamed");
124 + assert.equal(yield* git(cwd, ["branch", "--show-current"]), "feature/renamed");
125 +
126 + const refs = yield* driver.listRefs({ cwd });
127 + assert.equal(
128 + refs.refs.find((refName) => refName.name === "feature/renamed")?.current,
129 + true,
130 + );
131 + }),
132 + );
133 +
134 + it.effect("returns the existing refName when rename source and target match", () =>
135 + Effect.gen(function* () {
136 + const cwd = yield* makeTmpDir();
137 + yield* initRepoWithCommit(cwd);
138 + const driver = yield* GitVcsDriver.GitVcsDriver;
139 +
140 + const current = yield* git(cwd, ["branch", "--show-current"]);
141 + const result = yield* driver.renameBranch({
142 + cwd,
143 + oldBranch: current,
144 + newBranch: current,
145 + });
146 +
147 + assert.equal(result.branch, current);
148 + }),
149 + );
150 + });
151 +
152 + describe("worktree operations", () => {
153 + it.effect("creates and removes a worktree for a new refName", () =>
154 + Effect.gen(function* () {
155 + const cwd = yield* makeTmpDir();
156 + const { initialBranch } = yield* initRepoWithCommit(cwd);
157 + const pathService = yield* Path.Path;
158 + const worktreePath = pathService.join(
159 + yield* makeTmpDir("git-worktrees-"),
160 + "feature-worktree",
161 + );
162 + const driver = yield* GitVcsDriver.GitVcsDriver;
163 +
164 + const created = yield* driver.createWorktree({
165 + cwd,
166 + path: worktreePath,
167 + refName: initialBranch,
168 + newRefName: "feature/worktree",
169 + });
170 +
171 + assert.equal(created.worktree.path, worktreePath);
172 + assert.equal(created.worktree.refName, "feature/worktree");
173 + assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), "feature/worktree");
174 +
175 + yield* driver.removeWorktree({ cwd, path: worktreePath });
176 + const fileSystem = yield* FileSystem.FileSystem;
177 + assert.equal(yield* fileSystem.exists(worktreePath), false);
178 + }),
179 + );
180 + });
181 +
182 + describe("commit context", () => {
183 + it.effect("stages selected files and commits only those files", () =>
184 + Effect.gen(function* () {
185 + const cwd = yield* makeTmpDir();
186 + yield* initRepoWithCommit(cwd);
187 + const driver = yield* GitVcsDriver.GitVcsDriver;
188 +
189 + yield* writeTextFile(cwd, "a.txt", "a\n");
190 + yield* writeTextFile(cwd, "b.txt", "b\n");
191 +
192 + const context = yield* driver.prepareCommitContext(cwd, ["a.txt"]);
193 + assert.include(context?.stagedSummary ?? "", "a.txt");
194 + assert.notInclude(context?.stagedSummary ?? "", "b.txt");
195 +
196 + const commit = yield* driver.commit(cwd, "Add a", "");
197 + assert.match(commit.commitSha, /^[a-f0-9]{40}$/);
198 + assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "Add a");
199 +
200 + const status = yield* git(cwd, ["status", "--porcelain"]);
201 + assert.include(status, "?? b.txt");
202 + assert.notInclude(status, "a.txt");
203 + }),
204 + );
205 + });
206 +
207 + describe("remote operations", () => {
208 + it.effect("pushes with upstream setup and skips when already up to date", () =>
209 + Effect.gen(function* () {
210 + const cwd = yield* makeTmpDir();
211 + const remote = yield* makeTmpDir("git-remote-");
212 + yield* initRepoWithCommit(cwd);
213 + yield* git(remote, ["init", "--bare"]);
214 + yield* git(cwd, ["remote", "add", "origin", remote]);
215 + yield* (yield* GitVcsDriver.GitVcsDriver).createRef({
216 + cwd,
217 + refName: "feature/push",
218 + });
219 + yield* (yield* GitVcsDriver.GitVcsDriver).switchRef({
220 + cwd,
221 + refName: "feature/push",
222 + });
223 + yield* writeTextFile(cwd, "feature.txt", "feature\n");
224 + yield* (yield* GitVcsDriver.GitVcsDriver).prepareCommitContext(cwd);
225 + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add feature", "");
226 +
227 + const pushed = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
228 + assert.deepInclude(pushed, {
229 + status: "pushed",
230 + branch: "feature/push",
231 + setUpstream: true,
232 + });
233 + assert.equal(
234 + yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
235 + "origin/feature/push",
236 + );
237 +
238 + const skipped = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
239 + assert.deepInclude(skipped, {
240 + status: "skipped_up_to_date",
241 + branch: "feature/push",
242 + });
243 + }),
244 + );
245 + });
246 +});
apps/server/src/vcs/GitVcsDriverCore.ts renamed
+367 -486
@@ -1,11 +1,11 @@
1 import {
2 Cache,
3 Data,
4 + DateTime,
5 Duration,
6 Effect,
7 Exit,
8 FileSystem,
8 - Layer,
9 Option,
10 Path,
11 PlatformError,
@@ -18,25 +18,24 @@ import {
18 } from "effect";
19 import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
20
21 -import { GitCommandError, type GitBranch } from "@t3tools/contracts";
21 +import { GitCommandError, type VcsRef } from "@t3tools/contracts";
22 import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
23 -import { compactTraceAttributes } from "../../observability/Attributes.ts";
24 -import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../../observability/Metrics.ts";
25 -import {
26 - GitCore,
27 - type ExecuteGitProgress,
28 - type GitCommitOptions,
29 - type GitCoreShape,
30 - type GitStatusDetails,
31 - type ExecuteGitInput,
32 - type ExecuteGitResult,
33 -} from "../Services/GitCore.ts";
23 +import { compactTraceAttributes } from "../observability/Attributes.ts";
24 +import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
25 +import type {
26 + ExecuteGitProgress,
27 + GitCommitOptions,
28 + GitVcsDriverShape,
29 + GitStatusDetails,
30 + ExecuteGitInput,
31 + ExecuteGitResult,
32 +} from "./GitVcsDriver.ts";
33 import {
34 parseRemoteNames,
35 parseRemoteNamesInGitOrder,
36 parseRemoteRefWithRemoteNames,
38 -} from "../remoteRefs.ts";
39 -import { ServerConfig } from "../../config.ts";
37 +} from "../git/remoteRefs.ts";
38 +import { ServerConfig } from "../config.ts";
39 import { decodeJsonResult } from "@t3tools/shared/schemaJson";
40
41 const DEFAULT_TIMEOUT_MS = 30_000;
@@ -46,14 +45,6 @@ const PREPARED_COMMIT_PATCH_MAX_OUTPUT_BYTES = 49_000;
45 const RANGE_COMMIT_SUMMARY_MAX_OUTPUT_BYTES = 19_000;
46 const RANGE_DIFF_SUMMARY_MAX_OUTPUT_BYTES = 19_000;
47 const RANGE_DIFF_PATCH_MAX_OUTPUT_BYTES = 59_000;
49 -const WORKSPACE_FILES_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
50 -const GIT_CHECK_IGNORE_MAX_STDIN_BYTES = 256 * 1024;
51 -const WORKSPACE_GIT_HARDENED_CONFIG_ARGS = [
52 - "-c",
53 - "core.fsmonitor=false",
54 - "-c",
55 - "core.untrackedCache=false",
56 -] as const;
48 const STATUS_UPSTREAM_REFRESH_INTERVAL = Duration.seconds(15);
49 const STATUS_UPSTREAM_REFRESH_TIMEOUT = Duration.seconds(5);
50 const STATUS_UPSTREAM_REFRESH_FAILURE_COOLDOWN = Duration.seconds(5);
@@ -126,47 +117,6 @@ function parseNumstatEntries(
117 return entries;
118 }
119
129 -function splitNullSeparatedPaths(input: string, truncated: boolean): string[] {
130 - const parts = input.split("\0");
131 - if (parts.length === 0) return [];
132 -
133 - if (truncated && parts[parts.length - 1]?.length) {
134 - parts.pop();
135 - }
136 -
137 - return parts.filter((value) => value.length > 0);
138 -}
139 -
140 -function chunkPathsForGitCheckIgnore(relativePaths: readonly string[]): string[][] {
141 - const chunks: string[][] = [];
142 - let chunk: string[] = [];
143 - let chunkBytes = 0;
144 -
145 - for (const relativePath of relativePaths) {
146 - const relativePathBytes = Buffer.byteLength(relativePath) + 1;
147 - if (chunk.length > 0 && chunkBytes + relativePathBytes > GIT_CHECK_IGNORE_MAX_STDIN_BYTES) {
148 - chunks.push(chunk);
149 - chunk = [];
150 - chunkBytes = 0;
151 - }
152 -
153 - chunk.push(relativePath);
154 - chunkBytes += relativePathBytes;
155 -
156 - if (chunkBytes >= GIT_CHECK_IGNORE_MAX_STDIN_BYTES) {
157 - chunks.push(chunk);
158 - chunk = [];
159 - chunkBytes = 0;
160 - }
161 - }
162 -
163 - if (chunk.length > 0) {
164 - chunks.push(chunk);
165 - }
166 -
167 - return chunks;
168 -}
169 -
120 function parsePorcelainPath(line: string): string | null {
121 if (line.startsWith("? ") || line.startsWith("! ")) {
122 const simple = line.slice(2).trim();
@@ -205,34 +155,34 @@ function parseBranchLine(line: string): { name: string; current: boolean } | nul
155 }
156
157 function filterBranchesForListQuery(
208 - branches: ReadonlyArray<GitBranch>,
158 + refs: ReadonlyArray<VcsRef>,
159 query?: string,
210 -): ReadonlyArray<GitBranch> {
160 +): ReadonlyArray<VcsRef> {
161 if (!query) {
212 - return branches;
162 + return refs;
163 }
164
165 const normalizedQuery = query.toLowerCase();
216 - return branches.filter((branch) => branch.name.toLowerCase().includes(normalizedQuery));
166 + return refs.filter((refName) => refName.name.toLowerCase().includes(normalizedQuery));
167 }
168
169 function paginateBranches(input: {
220 - branches: ReadonlyArray<GitBranch>;
170 + refs: ReadonlyArray<VcsRef>;
171 cursor?: number | undefined;
172 limit?: number | undefined;
173 }): {
224 - branches: ReadonlyArray<GitBranch>;
174 + refs: ReadonlyArray<VcsRef>;
175 nextCursor: number | null;
176 totalCount: number;
177 } {
178 const cursor = input.cursor ?? 0;
179 const limit = input.limit ?? GIT_LIST_BRANCHES_DEFAULT_LIMIT;
230 - const totalCount = input.branches.length;
231 - const branches = input.branches.slice(cursor, cursor + limit);
232 - const nextCursor = cursor + branches.length < totalCount ? cursor + branches.length : null;
180 + const totalCount = input.refs.length;
181 + const refs = input.refs.slice(cursor, cursor + limit);
182 + const nextCursor = cursor + refs.length < totalCount ? cursor + refs.length : null;
183
184 return {
235 - branches,
185 + refs,
186 nextCursor,
187 totalCount,
188 };
@@ -273,7 +223,7 @@ function parseRemoteFetchUrls(stdout: string): Map<string, string> {
223 function parseUpstreamRefWithRemoteNames(
224 upstreamRef: string,
225 remoteNames: ReadonlyArray<string>,
276 -): { upstreamRef: string; remoteName: string; upstreamBranch: string } | null {
226 +): { upstreamRef: string; remoteName: string; branchName: string } | null {
227 const parsed = parseRemoteRefWithRemoteNames(upstreamRef, remoteNames);
228 if (!parsed) {
229 return null;
@@ -282,28 +232,28 @@ function parseUpstreamRefWithRemoteNames(
232 return {
233 upstreamRef,
234 remoteName: parsed.remoteName,
285 - upstreamBranch: parsed.branchName,
235 + branchName: parsed.branchName,
236 };
237 }
238
239 function parseUpstreamRefByFirstSeparator(
240 upstreamRef: string,
291 -): { upstreamRef: string; remoteName: string; upstreamBranch: string } | null {
241 +): { upstreamRef: string; remoteName: string; branchName: string } | null {
242 const separatorIndex = upstreamRef.indexOf("/");
243 if (separatorIndex <= 0 || separatorIndex === upstreamRef.length - 1) {
244 return null;
245 }
246
247 const remoteName = upstreamRef.slice(0, separatorIndex).trim();
298 - const upstreamBranch = upstreamRef.slice(separatorIndex + 1).trim();
299 - if (remoteName.length === 0 || upstreamBranch.length === 0) {
248 + const branchName = upstreamRef.slice(separatorIndex + 1).trim();
249 + if (remoteName.length === 0 || branchName.length === 0) {
250 return null;
251 }
252
253 return {
254 upstreamRef,
255 remoteName,
306 - upstreamBranch,
256 + branchName,
257 };
258 }
259
@@ -315,11 +265,11 @@ function parseTrackingBranchByUpstreamRef(stdout: string, upstreamRef: string):
265 }
266 const [branchNameRaw, upstreamBranchRaw = ""] = trimmedLine.split("\t");
267 const branchName = branchNameRaw?.trim() ?? "";
318 - const upstreamBranch = upstreamBranchRaw.trim();
319 - if (branchName.length === 0 || upstreamBranch.length === 0) {
268 + const candidateUpstreamRef = upstreamBranchRaw.trim();
269 + if (branchName.length === 0 || candidateUpstreamRef.length === 0) {
270 continue;
271 }
322 - if (upstreamBranch === upstreamRef) {
272 + if (candidateUpstreamRef === upstreamRef) {
273 return branchName;
274 }
275 }
@@ -346,8 +296,8 @@ function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string):
296 if (!trimmed.startsWith(prefix)) {
297 return null;
298 }
349 - const branch = trimmed.slice(prefix.length).trim();
350 - return branch.length > 0 ? branch : null;
299 + const refName = trimmed.slice(prefix.length).trim();
300 + return refName.length > 0 ? refName : null;
301 }
302
303 function createGitCommandError(
@@ -401,17 +351,18 @@ interface Trace2Monitor {
351 readonly flush: Effect.Effect<void, never>;
352 }
353
404 -const nowUnixNano = (): bigint => BigInt(Date.now()) * 1_000_000n;
354 +const nowUnixNano = DateTime.now.pipe(
355 + Effect.map((now) => BigInt(DateTime.toEpochMillis(now)) * 1_000_000n),
356 +);
357
358 const addCurrentSpanEvent = (name: string, attributes: Record<string, unknown>) =>
407 - Effect.currentSpan.pipe(
408 - Effect.tap((span) =>
409 - Effect.sync(() => {
410 - span.event(name, nowUnixNano(), compactTraceAttributes(attributes));
411 - }),
412 - ),
413 - Effect.catch(() => Effect.void),
414 - );
359 + Effect.gen(function* () {
360 + const span = yield* Effect.currentSpan;
361 + const timestamp = yield* nowUnixNano;
362 + yield* Effect.sync(() => {
363 + span.event(name, timestamp, compactTraceAttributes(attributes));
364 + });
365 + }).pipe(Effect.catch(() => Effect.void));
366
367 function trace2ChildKey(record: Record<string, unknown>): string | null {
368 const childId = record.child_id;
@@ -460,7 +411,7 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* (
411 const traceRecord = decodeJsonResult(Trace2Record)(trimmedLine);
412 if (Result.isFailure(traceRecord)) {
413 yield* Effect.logDebug(
463 - `GitCore.trace2: failed to parse trace line for ${quoteGitCommand(input.args)} in ${input.cwd}`,
414 + `GitVcsDriver.trace2: failed to parse trace line for ${quoteGitCommand(input.args)} in ${input.cwd}`,
415 traceRecord.failure,
416 );
417 return;
@@ -484,7 +435,8 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* (
435 }
436
437 if (event === "child_start") {
487 - hookStartByChildKey.set(childKey, { hookName, startedAtMs: Date.now() });
438 + const now = yield* DateTime.now;
439 + hookStartByChildKey.set(childKey, { hookName, startedAtMs: DateTime.toEpochMillis(now) });
440 yield* addCurrentSpanEvent("git.hook.started", {
441 hookName,
442 });
@@ -496,9 +448,12 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* (
448
449 if (event === "child_exit") {
450 hookStartByChildKey.delete(childKey);
499 - const code = traceRecord.success.code;
451 + const code = traceRecord.success.exitCode;
452 const exitCode = typeof code === "number" && Number.isInteger(code) ? code : null;
501 - const durationMs = started ? Math.max(0, Date.now() - started.startedAtMs) : null;
453 + const now = yield* DateTime.now;
454 + const durationMs = started
455 + ? Math.max(0, DateTime.toEpochMillis(now) - started.startedAtMs)
456 + : null;
457 yield* addCurrentSpanEvent("git.hook.finished", {
458 hookName: started?.hookName ?? hookName,
459 exitCode,
@@ -653,14 +608,14 @@ const collectOutput = Effect.fn("collectOutput")(function* <E>(
608 };
609 });
610
656 -export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
657 - executeOverride?: GitCoreShape["execute"];
611 +export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* (options?: {
612 + executeOverride?: GitVcsDriverShape["execute"];
613 }) {
614 const fileSystem = yield* FileSystem.FileSystem;
615 const path = yield* Path.Path;
616 const { worktreesDir } = yield* ServerConfig;
617
663 - let executeRaw: GitCoreShape["execute"];
618 + let executeRaw: GitVcsDriverShape["execute"];
619
620 if (options?.executeOverride) {
621 executeRaw = options.executeOverride;
@@ -711,7 +666,6 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
666 input.progress?.onStderrLine,
667 ),
668 child.exitCode.pipe(
714 - Effect.map((value) => Number(value)),
669 Effect.mapError(toGitCommandError(commandInput, "failed to report exit code.")),
670 ),
671 input.stdin === undefined
@@ -738,7 +692,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
692 }
693
694 return {
741 - code: exitCode,
695 + exitCode,
696 stdout: stdout.text,
697 stderr: stderr.text,
698 stdoutTruncated: stdout.truncated,
@@ -767,7 +721,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
721 });
722 }
723
770 - const execute: GitCoreShape["execute"] = (input) =>
724 + const execute: GitVcsDriverShape["execute"] = (input) =>
725 executeRaw(input).pipe(
726 withMetrics({
727 counter: gitCommandsTotal,
@@ -806,7 +760,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
760 ...(options.progress ? { progress: options.progress } : {}),
761 }).pipe(
762 Effect.flatMap((result) => {
809 - if (options.allowNonZeroExit || result.code === 0) {
763 + if (options.allowNonZeroExit || result.exitCode === 0) {
764 return Effect.succeed(result);
765 }
766 const stderr = result.stderr.trim();
@@ -823,7 +777,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
777 operation,
778 cwd,
779 args,
826 - `${commandLabel(args)} failed: code=${result.code ?? "null"}`,
780 + `${commandLabel(args)} failed: code=${result.exitCode ?? "null"}`,
781 ),
782 );
783 }),
@@ -859,16 +813,16 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
813 ),
814 );
815
862 - const branchExists = (cwd: string, branch: string): Effect.Effect<boolean, GitCommandError> =>
816 + const branchExists = (cwd: string, refName: string): Effect.Effect<boolean, GitCommandError> =>
817 executeGit(
864 - "GitCore.branchExists",
818 + "GitVcsDriver.branchExists",
819 cwd,
866 - ["show-ref", "--verify", "--quiet", `refs/heads/${branch}`],
820 + ["show-ref", "--verify", "--quiet", `refs/heads/${refName}`],
821 {
822 allowNonZeroExit: true,
823 timeoutMs: 5_000,
824 },
871 - ).pipe(Effect.map((result) => result.code === 0));
825 + ).pipe(Effect.map((result) => result.exitCode === 0));
826
827 const resolveAvailableBranchName = Effect.fn("resolveAvailableBranchName")(function* (
828 cwd: string,
@@ -888,7 +842,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
842 }
843
844 return yield* createGitCommandError(
891 - "GitCore.renameBranch",
845 + "GitVcsDriver.renameBranch",
846 cwd,
847 ["branch", "-m", "--", desiredBranch],
848 `Could not find an available branch name for '${desiredBranch}'.`,
@@ -897,7 +851,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
851
852 const resolveCurrentUpstream = Effect.fn("resolveCurrentUpstream")(function* (cwd: string) {
853 const upstreamRef = yield* runGitStdout(
900 - "GitCore.resolveCurrentUpstream",
854 + "GitVcsDriver.resolveCurrentUpstream",
855 cwd,
856 ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"],
857 true,
@@ -907,7 +861,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
861 return null;
862 }
863
910 - const remoteNames = yield* runGitStdout("GitCore.listRemoteNames", cwd, ["remote"]).pipe(
864 + const remoteNames = yield* runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe(
865 Effect.map(parseRemoteNames),
866 Effect.catch(() => Effect.succeed<ReadonlyArray<string>>([])),
867 );
@@ -924,7 +878,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
878 const fetchCwd =
879 path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
880 return executeGit(
927 - "GitCore.fetchRemoteForStatus",
881 + "GitVcsDriver.fetchRemoteForStatus",
882 fetchCwd,
883 ["--git-dir", gitCommonDir, "fetch", "--quiet", "--no-tags", remoteName],
884 {
@@ -935,7 +889,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
889 };
890
891 const resolveGitCommonDir = Effect.fn("resolveGitCommonDir")(function* (cwd: string) {
938 - const gitCommonDir = yield* runGitStdout("GitCore.resolveGitCommonDir", cwd, [
892 + const gitCommonDir = yield* runGitStdout("GitVcsDriver.resolveGitCommonDir", cwd, [
893 "rev-parse",
894 "--git-common-dir",
895 ]).pipe(Effect.map((stdout) => stdout.trim()));
@@ -978,13 +932,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
932 remoteName: string,
933 ): Effect.Effect<string | null, GitCommandError> =>
934 executeGit(
981 - "GitCore.resolveDefaultBranchName",
935 + "GitVcsDriver.resolveDefaultBranchName",
936 cwd,
937 ["symbolic-ref", `refs/remotes/${remoteName}/HEAD`],
938 { allowNonZeroExit: true },
939 ).pipe(
940 Effect.map((result) => {
987 - if (result.code !== 0) {
941 + if (result.exitCode !== 0) {
942 return null;
943 }
944 return parseDefaultBranchFromRemoteHeadRef(result.stdout, remoteName);
@@ -994,24 +948,24 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
948 const remoteBranchExists = (
949 cwd: string,
950 remoteName: string,
997 - branch: string,
951 + refName: string,
952 ): Effect.Effect<boolean, GitCommandError> =>
953 executeGit(
1000 - "GitCore.remoteBranchExists",
954 + "GitVcsDriver.remoteBranchExists",
955 cwd,
1002 - ["show-ref", "--verify", "--quiet", `refs/remotes/${remoteName}/${branch}`],
956 + ["show-ref", "--verify", "--quiet", `refs/remotes/${remoteName}/${refName}`],
957 {
958 allowNonZeroExit: true,
959 },
1006 - ).pipe(Effect.map((result) => result.code === 0));
960 + ).pipe(Effect.map((result) => result.exitCode === 0));
961
962 const originRemoteExists = (cwd: string): Effect.Effect<boolean, GitCommandError> =>
1009 - executeGit("GitCore.originRemoteExists", cwd, ["remote", "get-url", "origin"], {
963 + executeGit("GitVcsDriver.originRemoteExists", cwd, ["remote", "get-url", "origin"], {
964 allowNonZeroExit: true,
1011 - }).pipe(Effect.map((result) => result.code === 0));
965 + }).pipe(Effect.map((result) => result.exitCode === 0));
966
967 const listRemoteNames = (cwd: string): Effect.Effect<ReadonlyArray<string>, GitCommandError> =>
1014 - runGitStdout("GitCore.listRemoteNames", cwd, ["remote"]).pipe(
968 + runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe(
969 Effect.map(parseRemoteNamesInGitOrder),
970 );
971
@@ -1025,7 +979,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
979 return firstRemote;
980 }
981 return yield* createGitCommandError(
1028 - "GitCore.resolvePrimaryRemoteName",
982 + "GitVcsDriver.resolvePrimaryRemoteName",
983 cwd,
984 ["remote"],
985 "No git remote is configured for this repository.",
@@ -1034,12 +988,12 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
988
989 const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* (
990 cwd: string,
1037 - branch: string,
991 + refName: string,
992 ) {
993 const branchPushRemote = yield* runGitStdout(
1040 - "GitCore.resolvePushRemoteName.branchPushRemote",
994 + "GitVcsDriver.resolvePushRemoteName.branchPushRemote",
995 cwd,
1042 - ["config", "--get", `branch.${branch}.pushRemote`],
996 + ["config", "--get", `branch.${refName}.pushRemote`],
997 true,
998 ).pipe(Effect.map((stdout) => stdout.trim()));
999 if (branchPushRemote.length > 0) {
@@ -1047,7 +1001,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1001 }
1002
1003 const pushDefaultRemote = yield* runGitStdout(
1050 - "GitCore.resolvePushRemoteName.remotePushDefault",
1004 + "GitVcsDriver.resolvePushRemoteName.remotePushDefault",
1005 cwd,
1006 ["config", "--get", "remote.pushDefault"],
1007 true,
@@ -1059,39 +1013,47 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1013 return yield* resolvePrimaryRemoteName(cwd).pipe(Effect.catch(() => Effect.succeed(null)));
1014 });
1015
1062 - const ensureRemote: GitCoreShape["ensureRemote"] = Effect.fn("ensureRemote")(function* (input) {
1063 - const preferredName = sanitizeRemoteName(input.preferredName);
1064 - const normalizedTargetUrl = normalizeRemoteUrl(input.url);
1065 - const remoteFetchUrls = yield* runGitStdout("GitCore.ensureRemote.listRemoteUrls", input.cwd, [
1066 - "remote",
1067 - "-v",
1068 - ]).pipe(Effect.map((stdout) => parseRemoteFetchUrls(stdout)));
1069 -
1070 - for (const [remoteName, remoteUrl] of remoteFetchUrls.entries()) {
1071 - if (normalizeRemoteUrl(remoteUrl) === normalizedTargetUrl) {
1072 - return remoteName;
1016 + const ensureRemote: GitVcsDriverShape["ensureRemote"] = Effect.fn("ensureRemote")(
1017 + function* (input) {
1018 + const preferredName = sanitizeRemoteName(input.preferredName);
1019 + const normalizedTargetUrl = normalizeRemoteUrl(input.url);
1020 + const remoteFetchUrls = yield* runGitStdout(
1021 + "GitVcsDriver.ensureRemote.listRemoteUrls",
1022 + input.cwd,
1023 + ["remote", "-v"],
1024 + ).pipe(Effect.map((stdout) => parseRemoteFetchUrls(stdout)));
1025 +
1026 + for (const [remoteName, remoteUrl] of remoteFetchUrls.entries()) {
1027 + if (normalizeRemoteUrl(remoteUrl) === normalizedTargetUrl) {
1028 + return remoteName;
1029 + }
1030 }
1074 - }
1031
1076 - let remoteName = preferredName;
1077 - let suffix = 1;
1078 - while (remoteFetchUrls.has(remoteName)) {
1079 - remoteName = `${preferredName}-${suffix}`;
1080 - suffix += 1;
1081 - }
1032 + let remoteName = preferredName;
1033 + let suffix = 1;
1034 + while (remoteFetchUrls.has(remoteName)) {
1035 + remoteName = `${preferredName}-${suffix}`;
1036 + suffix += 1;
1037 + }
1038
1083 - yield* runGit("GitCore.ensureRemote.add", input.cwd, ["remote", "add", remoteName, input.url]);
1084 - return remoteName;
1085 - });
1039 + yield* runGit("GitVcsDriver.ensureRemote.add", input.cwd, [
1040 + "remote",
1041 + "add",
1042 + remoteName,
1043 + input.url,
1044 + ]);
1045 + return remoteName;
1046 + },
1047 + );
1048
1049 const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* (
1050 cwd: string,
1089 - branch: string,
1051 + refName: string,
1052 ) {
1053 const configuredBaseBranch = yield* runGitStdout(
1092 - "GitCore.resolveBaseBranchForNoUpstream.config",
1054 + "GitVcsDriver.resolveBaseBranchForNoUpstream.config",
1055 cwd,
1094 - ["config", "--get", `branch.${branch}.gh-merge-base`],
1056 + ["config", "--get", `branch.${refName}.gh-merge-base`],
1057 true,
1058 ).pipe(Effect.map((stdout) => stdout.trim()));
1059
@@ -1118,7 +1080,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1080 : remotePrefix && candidate.startsWith(remotePrefix)
1081 ? candidate.slice(remotePrefix.length)
1082 : candidate;
1121 - if (normalizedCandidate.length === 0 || normalizedCandidate === branch) {
1083 + if (normalizedCandidate.length === 0 || normalizedCandidate === refName) {
1084 continue;
1085 }
1086
@@ -1139,20 +1101,20 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1101
1102 const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* (
1103 cwd: string,
1142 - branch: string,
1104 + refName: string,
1105 ) {
1144 - const baseBranch = yield* resolveBaseBranchForNoUpstream(cwd, branch);
1145 - if (!baseBranch) {
1106 + const baseRef = yield* resolveBaseBranchForNoUpstream(cwd, refName);
1107 + if (!baseRef) {
1108 return 0;
1109 }
1110
1111 const result = yield* executeGit(
1150 - "GitCore.computeAheadCountAgainstBase",
1112 + "GitVcsDriver.computeAheadCountAgainstBase",
1113 cwd,
1152 - ["rev-list", "--count", `${baseBranch}..HEAD`],
1114 + ["rev-list", "--count", `${baseRef}..HEAD`],
1115 { allowNonZeroExit: true },
1116 );
1155 - if (result.code !== 0) {
1117 + if (result.exitCode !== 0) {
1118 return 0;
1119 }
1120
@@ -1162,7 +1124,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1124
1125 const readBranchRecency = Effect.fn("readBranchRecency")(function* (cwd: string) {
1126 const branchRecency = yield* executeGit(
1165 - "GitCore.readBranchRecency",
1127 + "GitVcsDriver.readBranchRecency",
1128 cwd,
1129 [
1130 "for-each-ref",
@@ -1177,7 +1139,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1139 );
1140
1141 const branchLastCommit = new Map<string, number>();
1180 - if (branchRecency.code !== 0) {
1142 + if (branchRecency.exitCode !== 0) {
1143 return branchLastCommit;
1144 }
1145
@@ -1198,7 +1160,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1160
1161 const readStatusDetailsLocal = Effect.fn("readStatusDetailsLocal")(function* (cwd: string) {
1162 const statusResult = yield* executeGit(
1201 - "GitCore.statusDetails.status",
1163 + "GitVcsDriver.statusDetails.status",
1164 cwd,
1165 ["status", "--porcelain=2", "--branch"],
1166 {
@@ -1210,27 +1172,27 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1172 return NON_REPOSITORY_STATUS_DETAILS;
1173 }
1174
1213 - if (statusResult.code !== 0) {
1175 + if (statusResult.exitCode !== 0) {
1176 const stderr = statusResult.stderr.trim();
1177 return yield* createGitCommandError(
1216 - "GitCore.statusDetails.status",
1178 + "GitVcsDriver.statusDetails.status",
1179 cwd,
1180 ["status", "--porcelain=2", "--branch"],
1181 stderr || "git status failed",
1182 );
1183 }
1184
1223 - const [unstagedNumstatStdout, stagedNumstatStdout, defaultRefResult, hasOriginRemote] =
1185 + const [unstagedNumstatStdout, stagedNumstatStdout, defaultRefResult, hasPrimaryRemote] =
1186 yield* Effect.all(
1187 [
1226 - runGitStdout("GitCore.statusDetails.unstagedNumstat", cwd, ["diff", "--numstat"]),
1227 - runGitStdout("GitCore.statusDetails.stagedNumstat", cwd, [
1188 + runGitStdout("GitVcsDriver.statusDetails.unstagedNumstat", cwd, ["diff", "--numstat"]),
1189 + runGitStdout("GitVcsDriver.statusDetails.stagedNumstat", cwd, [
1190 "diff",
1191 "--cached",
1192 "--numstat",
1193 ]),
1194 executeGit(
1233 - "GitCore.statusDetails.defaultRef",
1195 + "GitVcsDriver.statusDetails.defaultRef",
1196 cwd,
1197 ["symbolic-ref", "refs/remotes/origin/HEAD"],
1198 {
@@ -1243,11 +1205,11 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1205 );
1206 const statusStdout = statusResult.stdout;
1207 const defaultBranch =
1246 - defaultRefResult.code === 0
1208 + defaultRefResult.exitCode === 0
1209 ? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
1210 : null;
1211
1250 - let branch: string | null = null;
1212 + let refName: string | null = null;
1213 let upstreamRef: string | null = null;
1214 let aheadCount = 0;
1215 let behindCount = 0;
@@ -1257,7 +1219,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1219 for (const line of statusStdout.split(/\r?\n/g)) {
1220 if (line.startsWith("# branch.head ")) {
1221 const value = line.slice("# branch.head ".length).trim();
1260 - branch = value.startsWith("(") ? null : value;
1222 + refName = value.startsWith("(") ? null : value;
1223 continue;
1224 }
1225 if (line.startsWith("# branch.upstream ")) {
@@ -1279,8 +1241,8 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1241 }
1242 }
1243
1282 - if (!upstreamRef && branch) {
1283 - aheadCount = yield* computeAheadCountAgainstBase(cwd, branch).pipe(
1244 + if (!upstreamRef && refName) {
1245 + aheadCount = yield* computeAheadCountAgainstBase(cwd, refName).pipe(
1246 Effect.catch(() => Effect.succeed(0)),
1247 );
1248 behindCount = 0;
@@ -1314,12 +1276,12 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1276
1277 return {
1278 isRepo: true,
1317 - hasOriginRemote,
1279 + hasOriginRemote: hasPrimaryRemote,
1280 isDefaultBranch:
1319 - branch !== null &&
1320 - (branch === defaultBranch ||
1321 - (defaultBranch === null && (branch === "main" || branch === "master"))),
1322 - branch,
1281 + refName !== null &&
1282 + (refName === defaultBranch ||
1283 + (defaultBranch === null && (refName === "main" || refName === "master"))),
1284 + branch: refName,
1285 upstreamRef,
1286 hasWorkingTreeChanges,
1287 workingTree: {
@@ -1333,27 +1295,29 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1295 };
1296 });
1297
1336 - const statusDetailsLocal: GitCoreShape["statusDetailsLocal"] = Effect.fn("statusDetailsLocal")(
1298 + const statusDetailsLocal: GitVcsDriverShape["statusDetailsLocal"] = Effect.fn(
1299 + "statusDetailsLocal",
1300 + )(function* (cwd) {
1301 + return yield* readStatusDetailsLocal(cwd);
1302 + });
1303 +
1304 + const statusDetails: GitVcsDriverShape["statusDetails"] = Effect.fn("statusDetails")(
1305 function* (cwd) {
1306 + yield* refreshStatusUpstreamIfStale(cwd).pipe(
1307 + Effect.catchIf(isMissingGitCwdError, () => Effect.void),
1308 + Effect.ignoreCause({ log: true }),
1309 + );
1310 return yield* readStatusDetailsLocal(cwd);
1311 },
1312 );
1313
1342 - const statusDetails: GitCoreShape["statusDetails"] = Effect.fn("statusDetails")(function* (cwd) {
1343 - yield* refreshStatusUpstreamIfStale(cwd).pipe(
1344 - Effect.catchIf(isMissingGitCwdError, () => Effect.void),
1345 - Effect.ignoreCause({ log: true }),
1346 - );
1347 - return yield* readStatusDetailsLocal(cwd);
1348 - });
1349 -
1350 - const status: GitCoreShape["status"] = (input) =>
1314 + const status: GitVcsDriverShape["status"] = (input) =>
1315 statusDetails(input.cwd).pipe(
1316 Effect.map((details) => ({
1317 isRepo: details.isRepo,
1354 - hasOriginRemote: details.hasOriginRemote,
1355 - isDefaultBranch: details.isDefaultBranch,
1356 - branch: details.branch,
1318 + hasPrimaryRemote: details.hasOriginRemote,
1319 + isDefaultRef: details.isDefaultBranch,
1320 + refName: details.branch,
1321 hasWorkingTreeChanges: details.hasWorkingTreeChanges,
1322 workingTree: details.workingTree,
1323 hasUpstream: details.hasUpstream,
@@ -1363,34 +1327,34 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1327 })),
1328 );
1329
1366 - const prepareCommitContext: GitCoreShape["prepareCommitContext"] = Effect.fn(
1330 + const prepareCommitContext: GitVcsDriverShape["prepareCommitContext"] = Effect.fn(
1331 "prepareCommitContext",
1332 )(function* (cwd, filePaths) {
1333 if (filePaths && filePaths.length > 0) {
1370 - yield* runGit("GitCore.prepareCommitContext.reset", cwd, ["reset"]).pipe(
1334 + yield* runGit("GitVcsDriver.prepareCommitContext.reset", cwd, ["reset"]).pipe(
1335 Effect.catch(() => Effect.void),
1336 );
1373 - yield* runGit("GitCore.prepareCommitContext.addSelected", cwd, [
1337 + yield* runGit("GitVcsDriver.prepareCommitContext.addSelected", cwd, [
1338 "add",
1339 "-A",
1340 "--",
1341 ...filePaths,
1342 ]);
1343 } else {
1380 - yield* runGit("GitCore.prepareCommitContext.addAll", cwd, ["add", "-A"]);
1344 + yield* runGit("GitVcsDriver.prepareCommitContext.addAll", cwd, ["add", "-A"]);
1345 }
1346
1383 - const stagedSummary = yield* runGitStdout("GitCore.prepareCommitContext.stagedSummary", cwd, [
1384 - "diff",
1385 - "--cached",
1386 - "--name-status",
1387 - ]).pipe(Effect.map((stdout) => stdout.trim()));
1347 + const stagedSummary = yield* runGitStdout(
1348 + "GitVcsDriver.prepareCommitContext.stagedSummary",
1349 + cwd,
1350 + ["diff", "--cached", "--name-status"],
1351 + ).pipe(Effect.map((stdout) => stdout.trim()));
1352 if (stagedSummary.length === 0) {
1353 return null;
1354 }
1355
1356 const stagedPatch = yield* runGitStdoutWithOptions(
1393 - "GitCore.prepareCommitContext.stagedPatch",
1357 + "GitVcsDriver.prepareCommitContext.stagedPatch",
1358 cwd,
1359 ["diff", "--cached", "--patch", "--minimal"],
1360 {
@@ -1405,7 +1369,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1369 };
1370 });
1371
1408 - const commit: GitCoreShape["commit"] = Effect.fn("commit")(function* (
1372 + const commit: GitVcsDriverShape["commit"] = Effect.fn("commit")(function* (
1373 cwd,
1374 subject,
1375 body,
@@ -1426,11 +1390,11 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1390 onStderrLine: (line: string) =>
1391 options.progress?.onOutputLine?.({ stream: "stderr", text: line }) ?? Effect.void,
1392 };
1429 - yield* executeGit("GitCore.commit.commit", cwd, args, {
1393 + yield* executeGit("GitVcsDriver.commit.commit", cwd, args, {
1394 ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
1395 ...(progress ? { progress } : {}),
1396 }).pipe(Effect.asVoid);
1433 - const commitSha = yield* runGitStdout("GitCore.commit.revParseHead", cwd, [
1397 + const commitSha = yield* runGitStdout("GitVcsDriver.commit.revParseHead", cwd, [
1398 "rev-parse",
1399 "HEAD",
1400 ]).pipe(Effect.map((stdout) => stdout.trim()));
@@ -1438,13 +1402,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1402 return { commitSha };
1403 });
1404
1441 - const pushCurrentBranch: GitCoreShape["pushCurrentBranch"] = Effect.fn("pushCurrentBranch")(
1405 + const pushCurrentBranch: GitVcsDriverShape["pushCurrentBranch"] = Effect.fn("pushCurrentBranch")(
1406 function* (cwd, fallbackBranch) {
1407 const details = yield* statusDetails(cwd);
1408 const branch = details.branch ?? fallbackBranch;
1409 if (!branch) {
1410 return yield* createGitCommandError(
1447 - "GitCore.pushCurrentBranch",
1411 + "GitVcsDriver.pushCurrentBranch",
1412 cwd,
1413 ["push"],
1414 "Cannot push from detached HEAD.",
@@ -1491,13 +1455,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1455 const publishRemoteName = yield* resolvePushRemoteName(cwd, branch);
1456 if (!publishRemoteName) {
1457 return yield* createGitCommandError(
1494 - "GitCore.pushCurrentBranch",
1458 + "GitVcsDriver.pushCurrentBranch",
1459 cwd,
1460 ["push"],
1461 "Cannot push because no git remote is configured for this repository.",
1462 );
1463 }
1500 - yield* runGit("GitCore.pushCurrentBranch.pushWithUpstream", cwd, [
1464 + yield* runGit("GitVcsDriver.pushCurrentBranch.pushWithUpstream", cwd, [
1465 "push",
1466 "-u",
1467 publishRemoteName,
@@ -1515,10 +1479,10 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1479 Effect.catch(() => Effect.succeed(null)),
1480 );
1481 if (currentUpstream) {
1518 - yield* runGit("GitCore.pushCurrentBranch.pushUpstream", cwd, [
1482 + yield* runGit("GitVcsDriver.pushCurrentBranch.pushUpstream", cwd, [
1483 "push",
1484 currentUpstream.remoteName,
1521 - `HEAD:${currentUpstream.upstreamBranch}`,
1485 + `HEAD:${currentUpstream.upstreamRef}`,
1486 ]);
1487 return {
1488 status: "pushed" as const,
@@ -1528,7 +1492,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1492 };
1493 }
1494
1531 - yield* runGit("GitCore.pushCurrentBranch.push", cwd, ["push"]);
1495 + yield* runGit("GitVcsDriver.pushCurrentBranch.push", cwd, ["push"]);
1496 return {
1497 status: "pushed" as const,
1498 branch,
@@ -1538,13 +1502,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1502 },
1503 );
1504
1541 - const pullCurrentBranch: GitCoreShape["pullCurrentBranch"] = Effect.fn("pullCurrentBranch")(
1505 + const pullCurrentBranch: GitVcsDriverShape["pullCurrentBranch"] = Effect.fn("pullCurrentBranch")(
1506 function* (cwd) {
1507 const details = yield* statusDetails(cwd);
1544 - const branch = details.branch;
1545 - if (!branch) {
1508 + const refName = details.branch;
1509 + if (!refName) {
1510 return yield* createGitCommandError(
1547 - "GitCore.pullCurrentBranch",
1511 + "GitVcsDriver.pullCurrentBranch",
1512 cwd,
1513 ["pull", "--ff-only"],
1514 "Cannot pull from detached HEAD.",
@@ -1552,24 +1516,24 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1516 }
1517 if (!details.hasUpstream) {
1518 return yield* createGitCommandError(
1555 - "GitCore.pullCurrentBranch",
1519 + "GitVcsDriver.pullCurrentBranch",
1520 cwd,
1521 ["pull", "--ff-only"],
1522 "Current branch has no upstream configured. Push with upstream first.",
1523 );
1524 }
1525 const beforeSha = yield* runGitStdout(
1562 - "GitCore.pullCurrentBranch.beforeSha",
1526 + "GitVcsDriver.pullCurrentBranch.beforeSha",
1527 cwd,
1528 ["rev-parse", "HEAD"],
1529 true,
1530 ).pipe(Effect.map((stdout) => stdout.trim()));
1567 - yield* executeGit("GitCore.pullCurrentBranch.pull", cwd, ["pull", "--ff-only"], {
1531 + yield* executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["pull", "--ff-only"], {
1532 timeoutMs: 30_000,
1533 fallbackErrorMessage: "git pull failed",
1534 });
1535 const afterSha = yield* runGitStdout(
1572 - "GitCore.pullCurrentBranch.afterSha",
1536 + "GitVcsDriver.pullCurrentBranch.afterSha",
1537 cwd,
1538 ["rev-parse", "HEAD"],
1539 true,
@@ -1578,19 +1542,19 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1542 const refreshed = yield* statusDetails(cwd);
1543 return {
1544 status: beforeSha.length > 0 && beforeSha === afterSha ? "skipped_up_to_date" : "pulled",
1581 - branch,
1582 - upstreamBranch: refreshed.upstreamRef,
1545 + refName,
1546 + upstreamRef: refreshed.upstreamRef,
1547 };
1548 },
1549 );
1550
1587 - const readRangeContext: GitCoreShape["readRangeContext"] = Effect.fn("readRangeContext")(
1588 - function* (cwd, baseBranch) {
1589 - const range = `${baseBranch}..HEAD`;
1551 + const readRangeContext: GitVcsDriverShape["readRangeContext"] = Effect.fn("readRangeContext")(
1552 + function* (cwd, baseRef) {
1553 + const range = `${baseRef}..HEAD`;
1554 const [commitSummary, diffSummary, diffPatch] = yield* Effect.all(
1555 [
1556 runGitStdoutWithOptions(
1593 - "GitCore.readRangeContext.log",
1557 + "GitVcsDriver.readRangeContext.log",
1558 cwd,
1559 ["log", "--oneline", range],
1560 {
@@ -1599,7 +1563,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1563 },
1564 ),
1565 runGitStdoutWithOptions(
1602 - "GitCore.readRangeContext.diffStat",
1566 + "GitVcsDriver.readRangeContext.diffStat",
1567 cwd,
1568 ["diff", "--stat", range],
1569 {
@@ -1608,7 +1572,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1572 },
1573 ),
1574 runGitStdoutWithOptions(
1611 - "GitCore.readRangeContext.diffPatch",
1575 + "GitVcsDriver.readRangeContext.diffPatch",
1576 cwd,
1577 ["diff", "--patch", "--minimal", range],
1578 {
@@ -1628,112 +1592,18 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1592 },
1593 );
1594
1631 - const readConfigValue: GitCoreShape["readConfigValue"] = (cwd, key) =>
1632 - runGitStdout("GitCore.readConfigValue", cwd, ["config", "--get", key], true).pipe(
1595 + const readConfigValue: GitVcsDriverShape["readConfigValue"] = (cwd, key) =>
1596 + runGitStdout("GitVcsDriver.readConfigValue", cwd, ["config", "--get", key], true).pipe(
1597 Effect.map((stdout) => stdout.trim()),
1598 Effect.map((trimmed) => (trimmed.length > 0 ? trimmed : null)),
1599 );
1600
1637 - const isInsideWorkTree: GitCoreShape["isInsideWorkTree"] = (cwd) =>
1638 - executeGit("GitCore.isInsideWorkTree", cwd, ["rev-parse", "--is-inside-work-tree"], {
1639 - allowNonZeroExit: true,
1640 - timeoutMs: 5_000,
1641 - maxOutputBytes: 4_096,
1642 - }).pipe(Effect.map((result) => result.code === 0 && result.stdout.trim() === "true"));
1643 -
1644 - const listWorkspaceFiles: GitCoreShape["listWorkspaceFiles"] = (cwd) =>
1645 - executeGit(
1646 - "GitCore.listWorkspaceFiles",
1647 - cwd,
1648 - [
1649 - ...WORKSPACE_GIT_HARDENED_CONFIG_ARGS,
1650 - "ls-files",
1651 - "--cached",
1652 - "--others",
1653 - "--exclude-standard",
1654 - "-z",
1655 - ],
1656 - {
1657 - allowNonZeroExit: true,
1658 - timeoutMs: 20_000,
1659 - maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
1660 - truncateOutputAtMaxBytes: true,
1661 - },
1662 - ).pipe(
1663 - Effect.flatMap((result) =>
1664 - result.code === 0
1665 - ? Effect.succeed({
1666 - paths: splitNullSeparatedPaths(result.stdout, result.stdoutTruncated),
1667 - truncated: result.stdoutTruncated,
1668 - })
1669 - : Effect.fail(
1670 - createGitCommandError(
1671 - "GitCore.listWorkspaceFiles",
1672 - cwd,
1673 - [
1674 - ...WORKSPACE_GIT_HARDENED_CONFIG_ARGS,
1675 - "ls-files",
1676 - "--cached",
1677 - "--others",
1678 - "--exclude-standard",
1679 - "-z",
1680 - ],
1681 - result.stderr.trim().length > 0 ? result.stderr.trim() : "git ls-files failed",
1682 - ),
1683 - ),
1684 - ),
1685 - );
1686 -
1687 - const filterIgnoredPaths: GitCoreShape["filterIgnoredPaths"] = (cwd, relativePaths) =>
1688 - Effect.gen(function* () {
1689 - if (relativePaths.length === 0) {
1690 - return relativePaths;
1691 - }
1692 -
1693 - const ignoredPaths = new Set<string>();
1694 - const chunks = chunkPathsForGitCheckIgnore(relativePaths);
1695 -
1696 - for (const chunk of chunks) {
1697 - const result = yield* executeGit(
1698 - "GitCore.filterIgnoredPaths",
1699 - cwd,
1700 - [...WORKSPACE_GIT_HARDENED_CONFIG_ARGS, "check-ignore", "--no-index", "-z", "--stdin"],
1701 - {
1702 - stdin: `${chunk.join("\0")}\0`,
1703 - allowNonZeroExit: true,
1704 - timeoutMs: 20_000,
1705 - maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
1706 - truncateOutputAtMaxBytes: true,
1707 - },
1708 - );
1709 -
1710 - if (result.code !== 0 && result.code !== 1) {
1711 - return yield* createGitCommandError(
1712 - "GitCore.filterIgnoredPaths",
1713 - cwd,
1714 - [...WORKSPACE_GIT_HARDENED_CONFIG_ARGS, "check-ignore", "--no-index", "-z", "--stdin"],
1715 - result.stderr.trim().length > 0 ? result.stderr.trim() : "git check-ignore failed",
1716 - );
1717 - }
1718 -
1719 - for (const ignoredPath of splitNullSeparatedPaths(result.stdout, result.stdoutTruncated)) {
1720 - ignoredPaths.add(ignoredPath);
1721 - }
1722 - }
1723 -
1724 - if (ignoredPaths.size === 0) {
1725 - return relativePaths;
1726 - }
1727 -
1728 - return relativePaths.filter((relativePath) => !ignoredPaths.has(relativePath));
1729 - });
1730 -
1731 - const listBranches: GitCoreShape["listBranches"] = Effect.fn("listBranches")(function* (input) {
1601 + const listRefs: GitVcsDriverShape["listRefs"] = Effect.fn("listRefs")(function* (input) {
1602 const branchRecencyPromise = readBranchRecency(input.cwd).pipe(
1603 Effect.catch(() => Effect.succeed(new Map<string, number>())),
1604 );
1605 const localBranchResult = yield* executeGit(
1736 - "GitCore.listBranches.branchNoColor",
1606 + "GitVcsDriver.listRefs.branchNoColor",
1607 input.cwd,
1608 ["branch", "--no-color", "--no-column"],
1609 {
@@ -1743,7 +1613,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1613 ).pipe(
1614 Effect.catchIf(isMissingGitCwdError, () =>
1615 Effect.succeed({
1746 - code: 128,
1616 + exitCode: ChildProcessSpawner.ExitCode(128),
1617 stdout: "",
1618 stderr: "fatal: not a git repository",
1619 stdoutTruncated: false,
@@ -1752,19 +1622,19 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1622 ),
1623 );
1624
1755 - if (localBranchResult.code !== 0) {
1625 + if (localBranchResult.exitCode !== 0) {
1626 const stderr = localBranchResult.stderr.trim();
1627 if (stderr.toLowerCase().includes("not a git repository")) {
1628 return {
1759 - branches: [],
1629 + refs: [],
1630 isRepo: false,
1761 - hasOriginRemote: false,
1631 + hasPrimaryRemote: false,
1632 nextCursor: null,
1633 totalCount: 0,
1634 };
1635 }
1636 return yield* createGitCommandError(
1767 - "GitCore.listBranches",
1637 + "GitVcsDriver.listRefs",
1638 input.cwd,
1639 ["branch", "--no-color", "--no-column"],
1640 stderr || "git branch failed",
@@ -1772,7 +1642,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1642 }
1643
1644 const remoteBranchResultEffect = executeGit(
1775 - "GitCore.listBranches.remoteBranches",
1645 + "GitVcsDriver.listRefs.remoteBranches",
1646 input.cwd,
1647 ["branch", "--no-color", "--no-column", "--remotes"],
1648 {
@@ -1782,13 +1652,21 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1652 ).pipe(
1653 Effect.catch((error) =>
1654 Effect.logWarning(
1785 - `GitCore.listBranches: remote branch lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote branch list.`,
1786 - ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })),
1655 + `GitVcsDriver.listRefs: remote refName lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote refName list.`,
1656 + ).pipe(
1657 + Effect.as({
1658 + exitCode: ChildProcessSpawner.ExitCode(1),
1659 + stdout: "",
1660 + stderr: "",
1661 + stdoutTruncated: false,
1662 + stderrTruncated: false,
1663 + } satisfies ExecuteGitResult),
1664 + ),
1665 ),
1666 );
1667
1668 const remoteNamesResultEffect = executeGit(
1791 - "GitCore.listBranches.remoteNames",
1669 + "GitVcsDriver.listRefs.remoteNames",
1670 input.cwd,
1671 ["remote"],
1672 {
@@ -1798,8 +1676,16 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1676 ).pipe(
1677 Effect.catch((error) =>
1678 Effect.logWarning(
1801 - `GitCore.listBranches: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`,
1802 - ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })),
1679 + `GitVcsDriver.listRefs: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`,
1680 + ).pipe(
1681 + Effect.as({
1682 + exitCode: ChildProcessSpawner.ExitCode(1),
1683 + stdout: "",
1684 + stderr: "",
1685 + stdoutTruncated: false,
1686 + stderrTruncated: false,
1687 + } satisfies ExecuteGitResult),
1688 + ),
1689 ),
1690 );
1691
@@ -1807,7 +1693,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1693 yield* Effect.all(
1694 [
1695 executeGit(
1810 - "GitCore.listBranches.defaultRef",
1696 + "GitVcsDriver.listRefs.defaultRef",
1697 input.cwd,
1698 ["symbolic-ref", "refs/remotes/origin/HEAD"],
1699 {
@@ -1816,7 +1702,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1702 },
1703 ),
1704 executeGit(
1819 - "GitCore.listBranches.worktreeList",
1705 + "GitVcsDriver.listRefs.worktreeList",
1706 input.cwd,
1707 ["worktree", "list", "--porcelain"],
1708 {
@@ -1832,25 +1718,25 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1718 );
1719
1720 const remoteNames =
1835 - remoteNamesResult.code === 0 ? parseRemoteNames(remoteNamesResult.stdout) : [];
1836 - if (remoteBranchResult.code !== 0 && remoteBranchResult.stderr.trim().length > 0) {
1721 + remoteNamesResult.exitCode === 0 ? parseRemoteNames(remoteNamesResult.stdout) : [];
1722 + if (remoteBranchResult.exitCode !== 0 && remoteBranchResult.stderr.trim().length > 0) {
1723 yield* Effect.logWarning(
1838 - `GitCore.listBranches: remote branch lookup returned code ${remoteBranchResult.code} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote branch list.`,
1724 + `GitVcsDriver.listRefs: remote refName lookup returned code ${remoteBranchResult.exitCode} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote refName list.`,
1725 );
1726 }
1841 - if (remoteNamesResult.code !== 0 && remoteNamesResult.stderr.trim().length > 0) {
1727 + if (remoteNamesResult.exitCode !== 0 && remoteNamesResult.stderr.trim().length > 0) {
1728 yield* Effect.logWarning(
1843 - `GitCore.listBranches: remote name lookup returned code ${remoteNamesResult.code} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`,
1729 + `GitVcsDriver.listRefs: remote name lookup returned code ${remoteNamesResult.exitCode} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`,
1730 );
1731 }
1732
1733 const defaultBranch =
1848 - defaultRef.code === 0
1734 + defaultRef.exitCode === 0
1735 ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
1736 : null;
1737
1738 const worktreeMap = new Map<string, string>();
1853 - if (worktreeList.code === 0) {
1739 + if (worktreeList.exitCode === 0) {
1740 let currentPath: string | null = null;
1741 for (const line of worktreeList.stdout.split("\n")) {
1742 if (line.startsWith("worktree ")) {
@@ -1871,13 +1757,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1757 const localBranches = localBranchResult.stdout
1758 .split("\n")
1759 .map(parseBranchLine)
1874 - .filter((branch): branch is { name: string; current: boolean } => branch !== null)
1875 - .map((branch) => ({
1876 - name: branch.name,
1877 - current: branch.current,
1760 + .filter((refName): refName is { name: string; current: boolean } => refName !== null)
1761 + .map((refName) => ({
1762 + name: refName.name,
1763 + current: refName.current,
1764 isRemote: false,
1879 - isDefault: branch.name === defaultBranch,
1880 - worktreePath: worktreeMap.get(branch.name) ?? null,
1765 + isDefault: refName.name === defaultBranch,
1766 + worktreePath: worktreeMap.get(refName.name) ?? null,
1767 }))
1768 .toSorted((a, b) => {
1769 const aPriority = a.current ? 0 : a.isDefault ? 1 : 2;
@@ -1891,13 +1777,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1777 });
1778
1779 const remoteBranches =
1894 - remoteBranchResult.code === 0
1780 + remoteBranchResult.exitCode === 0
1781 ? remoteBranchResult.stdout
1782 .split("\n")
1783 .map(parseBranchLine)
1898 - .filter((branch): branch is { name: string; current: boolean } => branch !== null)
1899 - .map((branch) => {
1900 - const parsedRemoteRef = parseRemoteRefWithRemoteNames(branch.name, remoteNames);
1784 + .filter((refName): refName is { name: string; current: boolean } => refName !== null)
1785 + .map((refName) => {
1786 + const parsedRemoteRef = parseRemoteRefWithRemoteNames(refName.name, remoteNames);
1787 const remoteBranch: {
1788 name: string;
1789 current: boolean;
@@ -1906,7 +1792,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1792 isDefault: boolean;
1793 worktreePath: string | null;
1794 } = {
1909 - name: branch.name,
1795 + name: refName.name,
1796 current: false,
1797 isRemote: true,
1798 isDefault: false,
@@ -1925,8 +1811,8 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1811 })
1812 : [];
1813
1928 - const branches = paginateBranches({
1929 - branches: filterBranchesForListQuery(
1814 + const refs = paginateBranches({
1815 + refs: filterBranchesForListQuery(
1816 dedupeRemoteBranchesWithLocalMatches([...localBranches, ...remoteBranches]),
1817 input.query,
1818 ),
@@ -1935,43 +1821,43 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1821 });
1822
1823 return {
1938 - branches: [...branches.branches],
1824 + refs: [...refs.refs],
1825 isRepo: true,
1940 - hasOriginRemote: remoteNames.includes("origin"),
1941 - nextCursor: branches.nextCursor,
1942 - totalCount: branches.totalCount,
1826 + hasPrimaryRemote: remoteNames.includes("origin"),
1827 + nextCursor: refs.nextCursor,
1828 + totalCount: refs.totalCount,
1829 };
1830 });
1831
1946 - const createWorktree: GitCoreShape["createWorktree"] = Effect.fn("createWorktree")(
1832 + const createWorktree: GitVcsDriverShape["createWorktree"] = Effect.fn("createWorktree")(
1833 function* (input) {
1948 - const targetBranch = input.newBranch ?? input.branch;
1834 + const targetBranch = input.newRefName ?? input.refName;
1835 const sanitizedBranch = targetBranch.replace(/\//g, "-");
1836 const repoName = path.basename(input.cwd);
1837 const worktreePath = input.path ?? path.join(worktreesDir, repoName, sanitizedBranch);
1952 - const args = input.newBranch
1953 - ? ["worktree", "add", "-b", input.newBranch, worktreePath, input.branch]
1954 - : ["worktree", "add", worktreePath, input.branch];
1838 + const args = input.newRefName
1839 + ? ["worktree", "add", "-b", input.newRefName, worktreePath, input.refName]
1840 + : ["worktree", "add", worktreePath, input.refName];
1841
1956 - yield* executeGit("GitCore.createWorktree", input.cwd, args, {
1842 + yield* executeGit("GitVcsDriver.createWorktree", input.cwd, args, {
1843 fallbackErrorMessage: "git worktree add failed",
1844 });
1845
1846 return {
1847 worktree: {
1848 path: worktreePath,
1963 - branch: targetBranch,
1849 + refName: targetBranch,
1850 },
1851 };
1852 },
1853 );
1854
1969 - const fetchPullRequestBranch: GitCoreShape["fetchPullRequestBranch"] = Effect.fn(
1855 + const fetchPullRequestBranch: GitVcsDriverShape["fetchPullRequestBranch"] = Effect.fn(
1856 "fetchPullRequestBranch",
1857 )(function* (input) {
1858 const remoteName = yield* resolvePrimaryRemoteName(input.cwd);
1859 yield* executeGit(
1974 - "GitCore.fetchPullRequestBranch",
1860 + "GitVcsDriver.fetchPullRequestBranch",
1861 input.cwd,
1862 [
1863 "fetch",
@@ -1986,9 +1872,9 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1872 );
1873 });
1874
1989 - const fetchRemoteBranch: GitCoreShape["fetchRemoteBranch"] = Effect.fn("fetchRemoteBranch")(
1875 + const fetchRemoteBranch: GitVcsDriverShape["fetchRemoteBranch"] = Effect.fn("fetchRemoteBranch")(
1876 function* (input) {
1991 - yield* runGit("GitCore.fetchRemoteBranch.fetch", input.cwd, [
1877 + yield* runGit("GitVcsDriver.fetchRemoteBranch.fetch", input.cwd, [
1878 "fetch",
1879 "--quiet",
1880 "--no-tags",
@@ -1999,7 +1885,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1885 const localBranchAlreadyExists = yield* branchExists(input.cwd, input.localBranch);
1886 const targetRef = `${input.remoteName}/${input.remoteBranch}`;
1887 yield* runGit(
2002 - "GitCore.fetchRemoteBranch.materialize",
1888 + "GitVcsDriver.fetchRemoteBranch.materialize",
1889 input.cwd,
1890 localBranchAlreadyExists
1891 ? ["branch", "--force", input.localBranch, targetRef]
@@ -2008,28 +1894,28 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1894 },
1895 );
1896
2011 - const setBranchUpstream: GitCoreShape["setBranchUpstream"] = (input) =>
2012 - runGit("GitCore.setBranchUpstream", input.cwd, [
1897 + const setBranchUpstream: GitVcsDriverShape["setBranchUpstream"] = (input) =>
1898 + runGit("GitVcsDriver.setBranchUpstream", input.cwd, [
1899 "branch",
1900 "--set-upstream-to",
1901 `${input.remoteName}/${input.remoteBranch}`,
1902 input.branch,
1903 ]);
1904
2019 - const removeWorktree: GitCoreShape["removeWorktree"] = Effect.fn("removeWorktree")(
1905 + const removeWorktree: GitVcsDriverShape["removeWorktree"] = Effect.fn("removeWorktree")(
1906 function* (input) {
1907 const args = ["worktree", "remove"];
1908 if (input.force) {
1909 args.push("--force");
1910 }
1911 args.push(input.path);
2026 - yield* executeGit("GitCore.removeWorktree", input.cwd, args, {
1912 + yield* executeGit("GitVcsDriver.removeWorktree", input.cwd, args, {
1913 timeoutMs: 15_000,
1914 fallbackErrorMessage: "git worktree remove failed",
1915 }).pipe(
1916 Effect.mapError((error) =>
1917 createGitCommandError(
2032 - "GitCore.removeWorktree",
1918 + "GitVcsDriver.removeWorktree",
1919 input.cwd,
1920 args,
1921 `${commandLabel(args)} failed (cwd: ${input.cwd}): ${error.message}`,
@@ -2040,127 +1926,127 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
1926 },
1927 );
1928
2043 - const renameBranch: GitCoreShape["renameBranch"] = Effect.fn("renameBranch")(function* (input) {
2044 - if (input.oldBranch === input.newBranch) {
2045 - return { branch: input.newBranch };
2046 - }
2047 - const targetBranch = yield* resolveAvailableBranchName(input.cwd, input.newBranch);
1929 + const renameBranch: GitVcsDriverShape["renameBranch"] = Effect.fn("renameBranch")(
1930 + function* (input) {
1931 + if (input.oldBranch === input.newBranch) {
1932 + return { branch: input.newBranch };
1933 + }
1934 + const targetBranch = yield* resolveAvailableBranchName(input.cwd, input.newBranch);
1935
2049 - yield* executeGit(
2050 - "GitCore.renameBranch",
2051 - input.cwd,
2052 - ["branch", "-m", "--", input.oldBranch, targetBranch],
2053 - {
2054 - timeoutMs: 10_000,
2055 - fallbackErrorMessage: "git branch rename failed",
2056 - },
2057 - );
1936 + yield* executeGit(
1937 + "GitVcsDriver.renameBranch",
1938 + input.cwd,
1939 + ["branch", "-m", "--", input.oldBranch, targetBranch],
1940 + {
1941 + timeoutMs: 10_000,
1942 + fallbackErrorMessage: "git branch rename failed",
1943 + },
1944 + );
1945
2059 - return { branch: targetBranch };
2060 - });
1946 + return { branch: targetBranch };
1947 + },
1948 + );
1949
2062 - const checkoutBranch: GitCoreShape["checkoutBranch"] = Effect.fn("checkoutBranch")(
2063 - function* (input) {
2064 - const [localInputExists, remoteExists] = yield* Effect.all(
2065 - [
2066 - executeGit(
2067 - "GitCore.checkoutBranch.localInputExists",
2068 - input.cwd,
2069 - ["show-ref", "--verify", "--quiet", `refs/heads/${input.branch}`],
2070 - {
2071 - timeoutMs: 5_000,
2072 - allowNonZeroExit: true,
2073 - },
2074 - ).pipe(Effect.map((result) => result.code === 0)),
2075 - executeGit(
2076 - "GitCore.checkoutBranch.remoteExists",
2077 - input.cwd,
2078 - ["show-ref", "--verify", "--quiet", `refs/remotes/${input.branch}`],
2079 - {
2080 - timeoutMs: 5_000,
2081 - allowNonZeroExit: true,
2082 - },
2083 - ).pipe(Effect.map((result) => result.code === 0)),
2084 - ],
2085 - { concurrency: "unbounded" },
2086 - );
1950 + const switchRef: GitVcsDriverShape["switchRef"] = Effect.fn("switchRef")(function* (input) {
1951 + const [localInputExists, remoteExists] = yield* Effect.all(
1952 + [
1953 + executeGit(
1954 + "GitVcsDriver.switchRef.localInputExists",
1955 + input.cwd,
1956 + ["show-ref", "--verify", "--quiet", `refs/heads/${input.refName}`],
1957 + {
1958 + timeoutMs: 5_000,
1959 + allowNonZeroExit: true,
1960 + },
1961 + ).pipe(Effect.map((result) => result.exitCode === 0)),
1962 + executeGit(
1963 + "GitVcsDriver.switchRef.remoteExists",
1964 + input.cwd,
1965 + ["show-ref", "--verify", "--quiet", `refs/remotes/${input.refName}`],
1966 + {
1967 + timeoutMs: 5_000,
1968 + allowNonZeroExit: true,
1969 + },
1970 + ).pipe(Effect.map((result) => result.exitCode === 0)),
1971 + ],
1972 + { concurrency: "unbounded" },
1973 + );
1974
2088 - const localTrackingBranch = remoteExists
1975 + const localTrackingBranch = remoteExists
1976 + ? yield* executeGit(
1977 + "GitVcsDriver.switchRef.localTrackingBranch",
1978 + input.cwd,
1979 + ["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
1980 + {
1981 + timeoutMs: 5_000,
1982 + allowNonZeroExit: true,
1983 + },
1984 + ).pipe(
1985 + Effect.map((result) =>
1986 + result.exitCode === 0
1987 + ? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
1988 + : null,
1989 + ),
1990 + )
1991 + : null;
1992 +
1993 + const localTrackedBranchCandidate = deriveLocalBranchNameFromRemoteRef(input.refName);
1994 + const localTrackedBranchTargetExists =
1995 + remoteExists && localTrackedBranchCandidate
1996 ? yield* executeGit(
2090 - "GitCore.checkoutBranch.localTrackingBranch",
1997 + "GitVcsDriver.switchRef.localTrackedBranchTargetExists",
1998 input.cwd,
2092 - ["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
1999 + ["show-ref", "--verify", "--quiet", `refs/heads/${localTrackedBranchCandidate}`],
2000 {
2001 timeoutMs: 5_000,
2002 allowNonZeroExit: true,
2003 },
2097 - ).pipe(
2098 - Effect.map((result) =>
2099 - result.code === 0
2100 - ? parseTrackingBranchByUpstreamRef(result.stdout, input.branch)
2101 - : null,
2102 - ),
2103 - )
2104 - : null;
2105 -
2106 - const localTrackedBranchCandidate = deriveLocalBranchNameFromRemoteRef(input.branch);
2107 - const localTrackedBranchTargetExists =
2108 - remoteExists && localTrackedBranchCandidate
2109 - ? yield* executeGit(
2110 - "GitCore.checkoutBranch.localTrackedBranchTargetExists",
2111 - input.cwd,
2112 - ["show-ref", "--verify", "--quiet", `refs/heads/${localTrackedBranchCandidate}`],
2113 - {
2114 - timeoutMs: 5_000,
2115 - allowNonZeroExit: true,
2116 - },
2117 - ).pipe(Effect.map((result) => result.code === 0))
2118 - : false;
2119 -
2120 - const checkoutArgs = localInputExists
2121 - ? ["checkout", input.branch]
2122 - : remoteExists && !localTrackingBranch && localTrackedBranchTargetExists
2123 - ? ["checkout", input.branch]
2124 - : remoteExists && !localTrackingBranch
2125 - ? ["checkout", "--track", input.branch]
2126 - : remoteExists && localTrackingBranch
2127 - ? ["checkout", localTrackingBranch]
2128 - : ["checkout", input.branch];
2129 -
2130 - yield* executeGit("GitCore.checkoutBranch.checkout", input.cwd, checkoutArgs, {
2131 - timeoutMs: 10_000,
2132 - fallbackErrorMessage: "git checkout failed",
2133 - });
2004 + ).pipe(Effect.map((result) => result.exitCode === 0))
2005 + : false;
2006 +
2007 + const checkoutArgs = localInputExists
2008 + ? ["checkout", input.refName]
2009 + : remoteExists && !localTrackingBranch && localTrackedBranchTargetExists
2010 + ? ["checkout", input.refName]
2011 + : remoteExists && !localTrackingBranch
2012 + ? ["checkout", "--track", input.refName]
2013 + : remoteExists && localTrackingBranch
2014 + ? ["checkout", localTrackingBranch]
2015 + : ["checkout", input.refName];
2016 +
2017 + yield* executeGit("GitVcsDriver.switchRef.checkout", input.cwd, checkoutArgs, {
2018 + timeoutMs: 10_000,
2019 + fallbackErrorMessage: "git checkout failed",
2020 + });
2021
2135 - const branch = yield* runGitStdout("GitCore.checkoutBranch.currentBranch", input.cwd, [
2136 - "branch",
2137 - "--show-current",
2138 - ]).pipe(Effect.map((stdout) => stdout.trim() || null));
2022 + const refName = yield* runGitStdout("GitVcsDriver.switchRef.currentBranch", input.cwd, [
2023 + "branch",
2024 + "--show-current",
2025 + ]).pipe(Effect.map((stdout) => stdout.trim() || null));
2026
2140 - return { branch };
2141 - },
2142 - );
2027 + return { refName };
2028 + });
2029
2144 - const createBranch: GitCoreShape["createBranch"] = Effect.fn("createBranch")(function* (input) {
2145 - yield* executeGit("GitCore.createBranch", input.cwd, ["branch", input.branch], {
2030 + const createRef: GitVcsDriverShape["createRef"] = Effect.fn("createRef")(function* (input) {
2031 + yield* executeGit("GitVcsDriver.createRef", input.cwd, ["branch", input.refName], {
2032 timeoutMs: 10_000,
2033 fallbackErrorMessage: "git branch create failed",
2034 });
2149 - if (input.checkout) {
2150 - yield* checkoutBranch({ cwd: input.cwd, branch: input.branch });
2035 + if (input.switchRef) {
2036 + yield* switchRef({ cwd: input.cwd, refName: input.refName });
2037 }
2038
2153 - return { branch: input.branch };
2039 + return { refName: input.refName };
2040 });
2041
2156 - const initRepo: GitCoreShape["initRepo"] = (input) =>
2157 - executeGit("GitCore.initRepo", input.cwd, ["init"], {
2042 + const initRepo: GitVcsDriverShape["initRepo"] = (input) =>
2043 + executeGit("GitVcsDriver.initRepo", input.cwd, ["init"], {
2044 timeoutMs: 10_000,
2045 fallbackErrorMessage: "git init failed",
2046 }).pipe(Effect.asVoid);
2047
2162 - const listLocalBranchNames: GitCoreShape["listLocalBranchNames"] = (cwd) =>
2163 - runGitStdout("GitCore.listLocalBranchNames", cwd, [
2048 + const listLocalBranchNames: GitVcsDriverShape["listLocalBranchNames"] = (cwd) =>
2049 + runGitStdout("GitVcsDriver.listLocalBranchNames", cwd, [
2050 "branch",
2051 "--list",
2052 "--no-column",
@@ -2185,10 +2071,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
2071 pullCurrentBranch,
2072 readRangeContext,
2073 readConfigValue,
2188 - isInsideWorkTree,
2189 - listWorkspaceFiles,
2190 - filterIgnoredPaths,
2191 - listBranches,
2074 + listRefs,
2075 createWorktree,
2076 fetchPullRequestBranch,
2077 ensureRemote,
@@ -2196,11 +2079,9 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
2079 setBranchUpstream,
2080 removeWorktree,
2081 renameBranch,
2199 - createBranch,
2200 - checkoutBranch,
2082 + createRef,
2083 + switchRef,
2084 initRepo,
2085 listLocalBranchNames,
2203 - } satisfies GitCoreShape;
2086 + } satisfies GitVcsDriverShape;
2087 });
2205 -
2206 -export const GitCoreLive = Layer.effect(GitCore, makeGitCore());
apps/server/src/vcs/VcsDriver.ts new
+31
@@ -0,0 +1,31 @@
1 +import { Context, type Effect } from "effect";
2 +
3 +import type {
4 + VcsDriverCapabilities,
5 + VcsError,
6 + VcsInitInput,
7 + VcsListRemotesResult,
8 + VcsListWorkspaceFilesResult,
9 + VcsRepositoryIdentity,
10 +} from "@t3tools/contracts";
11 +import type { VcsProcessInput, VcsProcessOutput } from "./VcsProcess.ts";
12 +
13 +export interface VcsDriverShape {
14 + readonly capabilities: VcsDriverCapabilities;
15 + readonly execute: (
16 + input: Omit<VcsProcessInput, "command">,
17 + ) => Effect.Effect<VcsProcessOutput, VcsError>;
18 + readonly detectRepository: (cwd: string) => Effect.Effect<VcsRepositoryIdentity | null, VcsError>;
19 + readonly isInsideWorkTree: (cwd: string) => Effect.Effect<boolean, VcsError>;
20 + readonly listWorkspaceFiles: (
21 + cwd: string,
22 + ) => Effect.Effect<VcsListWorkspaceFilesResult, VcsError>;
23 + readonly listRemotes: (cwd: string) => Effect.Effect<VcsListRemotesResult, VcsError>;
24 + readonly filterIgnoredPaths: (
25 + cwd: string,
26 + relativePaths: ReadonlyArray<string>,
27 + ) => Effect.Effect<ReadonlyArray<string>, VcsError>;
28 + readonly initRepository: (input: VcsInitInput) => Effect.Effect<void, VcsError>;
29 +}
30 +
31 +export class VcsDriver extends Context.Service<VcsDriver, VcsDriverShape>()("t3/vcs/VcsDriver") {}
apps/server/src/vcs/VcsDriverRegistry.test.ts new
+87
@@ -0,0 +1,87 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { Effect, Layer } from "effect";
3 +import { ChildProcessSpawner } from "effect/unstable/process";
4 +import { describe } from "vitest";
5 +
6 +import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "./VcsProcess.ts";
7 +import { VcsProjectConfig } from "./VcsProjectConfig.ts";
8 +import { VcsDriverRegistry, make as makeVcsDriverRegistry } from "./VcsDriverRegistry.ts";
9 +
10 +const processOutput = (stdout: string): VcsProcessOutput => ({
11 + exitCode: ChildProcessSpawner.ExitCode(0),
12 + stdout,
13 + stderr: "",
14 + stdoutTruncated: false,
15 + stderrTruncated: false,
16 +});
17 +
18 +describe("VcsDriverRegistry", () => {
19 + it.effect("routes directly by VCS driver kind for non-repository workflows", () => {
20 + const layer = Layer.effect(VcsDriverRegistry, makeVcsDriverRegistry()).pipe(
21 + Layer.provide(
22 + Layer.mock(VcsProjectConfig)({
23 + resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
24 + }),
25 + ),
26 + Layer.provide(
27 + Layer.mock(VcsProcess)({
28 + run: () => Effect.succeed(processOutput("")),
29 + }),
30 + ),
31 + );
32 +
33 + return Effect.gen(function* () {
34 + const registry = yield* VcsDriverRegistry;
35 + const driver = yield* registry.get("git");
36 +
37 + assert.strictEqual(driver.capabilities.kind, "git");
38 + }).pipe(Effect.provide(layer));
39 + });
40 +
41 + it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => {
42 + const calls: VcsProcessInput[] = [];
43 + const layer = Layer.effect(VcsDriverRegistry, makeVcsDriverRegistry()).pipe(
44 + Layer.provide(
45 + Layer.mock(VcsProjectConfig)({
46 + resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
47 + }),
48 + ),
49 + Layer.provide(
50 + Layer.mock(VcsProcess)({
51 + run: (input) =>
52 + Effect.sync(() => {
53 + calls.push(input);
54 + const command = input.args.join(" ");
55 + if (command === "rev-parse --is-inside-work-tree") {
56 + return processOutput("true\n");
57 + }
58 + if (command === "rev-parse --show-toplevel") {
59 + return processOutput("/repo\n");
60 + }
61 + if (command === "rev-parse --git-common-dir") {
62 + return processOutput("/repo/.git\n");
63 + }
64 + return processOutput("");
65 + }),
66 + }),
67 + ),
68 + );
69 +
70 + return Effect.gen(function* () {
71 + const registry = yield* VcsDriverRegistry;
72 + const first = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
73 + const second = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
74 +
75 + assert.equal(first.repository.rootPath, "/repo");
76 + assert.equal(second.repository.rootPath, "/repo");
77 + assert.deepStrictEqual(
78 + calls.map((call) => call.args.join(" ")),
79 + [
80 + "rev-parse --is-inside-work-tree",
81 + "rev-parse --show-toplevel",
82 + "rev-parse --git-common-dir",
83 + ],
84 + );
85 + }).pipe(Effect.provide(layer));
86 + });
87 +});
apps/server/src/vcs/VcsDriverRegistry.ts new
+155
@@ -0,0 +1,155 @@
1 +import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
2 +
3 +import type { VcsDriverKind, VcsError, VcsRepositoryIdentity } from "@t3tools/contracts";
4 +import { VcsUnsupportedOperationError } from "@t3tools/contracts";
5 +import * as GitVcsDriver from "./GitVcsDriver.ts";
6 +import * as VcsProjectConfig from "./VcsProjectConfig.ts";
7 +import type { VcsDriverShape } from "./VcsDriver.ts";
8 +
9 +const DETECTION_CACHE_CAPACITY = 2_048;
10 +const DETECTION_CACHE_TTL = Duration.seconds(2);
11 +
12 +export interface VcsDriverResolveInput {
13 + readonly cwd: string;
14 + readonly requestedKind?: VcsDriverKind | "auto";
15 +}
16 +
17 +export interface VcsDriverHandle {
18 + readonly kind: VcsDriverKind;
19 + readonly repository: VcsRepositoryIdentity;
20 + readonly driver: VcsDriverShape;
21 +}
22 +
23 +export interface VcsDriverRegistryShape {
24 + readonly get: (kind: VcsDriverKind) => Effect.Effect<VcsDriverShape, VcsError>;
25 + readonly detect: (
26 + input: VcsDriverResolveInput,
27 + ) => Effect.Effect<VcsDriverHandle | null, VcsError>;
28 + readonly resolve: (input: VcsDriverResolveInput) => Effect.Effect<VcsDriverHandle, VcsError>;
29 +}
30 +
31 +export class VcsDriverRegistry extends Context.Service<VcsDriverRegistry, VcsDriverRegistryShape>()(
32 + "t3/vcs/VcsDriverRegistry",
33 +) {}
34 +
35 +const unsupported = (operation: string, kind: VcsDriverKind, detail: string) =>
36 + new VcsUnsupportedOperationError({
37 + operation,
38 + kind,
39 + detail,
40 + });
41 +
42 +function detectionCacheKey(input: {
43 + readonly cwd: string;
44 + readonly requestedKind: VcsDriverKind | "auto";
45 +}): string {
46 + return `${input.requestedKind}\0${input.cwd}`;
47 +}
48 +
49 +function parseDetectionCacheKey(key: string): {
50 + readonly cwd: string;
51 + readonly requestedKind: VcsDriverKind | "auto";
52 +} {
53 + const separatorIndex = key.indexOf("\0");
54 + if (separatorIndex === -1) {
55 + return {
56 + cwd: key,
57 + requestedKind: "auto",
58 + };
59 + }
60 + return {
61 + requestedKind: key.slice(0, separatorIndex) as VcsDriverKind | "auto",
62 + cwd: key.slice(separatorIndex + 1),
63 + };
64 +}
65 +
66 +export const make = Effect.fn("makeVcsDriverRegistry")(function* () {
67 + const projectConfig = yield* VcsProjectConfig.VcsProjectConfig;
68 + const git = yield* GitVcsDriver.makeVcsDriverShape();
69 + const drivers: Partial<Record<VcsDriverKind, VcsDriverShape>> = {
70 + git,
71 + };
72 +
73 + const get: VcsDriverRegistryShape["get"] = (kind) => {
74 + const driver = drivers[kind];
75 + if (!driver) {
76 + return Effect.fail(
77 + unsupported("VcsDriverRegistry.get", kind, `No ${kind} VCS driver is registered.`),
78 + );
79 + }
80 + return Effect.succeed(driver);
81 + };
82 +
83 + const detectWithDriver = Effect.fn("VcsDriverRegistry.detectWithDriver")(function* (
84 + kind: VcsDriverKind,
85 + driver: VcsDriverShape,
86 + cwd: string,
87 + ) {
88 + const repository = yield* driver.detectRepository(cwd);
89 + if (!repository) {
90 + return null;
91 + }
92 + return {
93 + kind,
94 + repository,
95 + driver,
96 + } satisfies VcsDriverHandle;
97 + });
98 +
99 + const detectResolvedKind = Effect.fn("VcsDriverRegistry.detectResolvedKind")(function* (input: {
100 + readonly cwd: string;
101 + readonly requestedKind: VcsDriverKind | "auto";
102 + }) {
103 + const requestedKind = input.requestedKind;
104 +
105 + if (requestedKind !== "auto" && requestedKind !== "unknown") {
106 + const driver = yield* get(requestedKind);
107 + return yield* detectWithDriver(requestedKind, driver, input.cwd);
108 + }
109 +
110 + return yield* detectWithDriver("git", git, input.cwd);
111 + });
112 +
113 + const detectionCache = yield* Cache.makeWith<string, VcsDriverHandle | null, VcsError>(
114 + (key) => detectResolvedKind(parseDetectionCacheKey(key)),
115 + {
116 + capacity: DETECTION_CACHE_CAPACITY,
117 + timeToLive: (exit) => (Exit.isSuccess(exit) ? DETECTION_CACHE_TTL : Duration.zero),
118 + },
119 + );
120 +
121 + const detect: VcsDriverRegistryShape["detect"] = Effect.fn("VcsDriverRegistry.detect")(
122 + function* (input) {
123 + const requestedKind = yield* projectConfig.resolveKind(input);
124 + return yield* Cache.get(detectionCache, detectionCacheKey({ cwd: input.cwd, requestedKind }));
125 + },
126 + );
127 +
128 + const resolve: VcsDriverRegistryShape["resolve"] = Effect.fn("VcsDriverRegistry.resolve")(
129 + function* (input) {
130 + const detected = yield* detect(input);
131 + if (detected) {
132 + return detected;
133 + }
134 +
135 + const requestedKind = input.requestedKind ?? "auto";
136 + return yield* unsupported(
137 + "VcsDriverRegistry.resolve",
138 + requestedKind === "auto" ? "unknown" : requestedKind,
139 + requestedKind === "auto"
140 + ? `No supported VCS repository was detected at ${input.cwd}.`
141 + : `No ${requestedKind} repository was detected at ${input.cwd}.`,
142 + );
143 + },
144 + );
145 +
146 + return VcsDriverRegistry.of({
147 + get,
148 + detect,
149 + resolve,
150 + });
151 +});
152 +
153 +export const layer = Layer.effect(VcsDriverRegistry, make()).pipe(
154 + Layer.provide(VcsProjectConfig.layer),
155 +);
apps/server/src/vcs/VcsProcess.ts new
+259
@@ -0,0 +1,259 @@
1 +import { Duration, Context, Effect, Layer, Option, PlatformError, Sink, Stream } from "effect";
2 +import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
3 +
4 +import {
5 + VcsOutputDecodeError,
6 + type VcsError,
7 + VcsProcessExitError,
8 + VcsProcessSpawnError,
9 + VcsProcessTimeoutError,
10 +} from "@t3tools/contracts";
11 +
12 +export interface VcsProcessInput {
13 + readonly operation: string;
14 + readonly command: string;
15 + readonly args: ReadonlyArray<string>;
16 + readonly cwd: string;
17 + readonly stdin?: string;
18 + readonly env?: NodeJS.ProcessEnv;
19 + readonly allowNonZeroExit?: boolean;
20 + readonly timeoutMs?: number;
21 + readonly maxOutputBytes?: number;
22 + readonly truncateOutputAtMaxBytes?: boolean;
23 +}
24 +
25 +export interface VcsProcessOutput {
26 + readonly exitCode: ChildProcessSpawner.ExitCode;
27 + readonly stdout: string;
28 + readonly stderr: string;
29 + readonly stdoutTruncated: boolean;
30 + readonly stderrTruncated: boolean;
31 +}
32 +
33 +export interface VcsProcessCollectedText {
34 + readonly text: string;
35 + readonly truncated: boolean;
36 +}
37 +
38 +export interface VcsProcessHandle {
39 + readonly pid: ChildProcessSpawner.ProcessId;
40 + readonly stdin: Sink.Sink<void, Uint8Array, never, VcsError>;
41 + readonly stdout: Stream.Stream<Uint8Array, VcsError>;
42 + readonly stderr: Stream.Stream<Uint8Array, VcsError>;
43 + readonly exitCode: Effect.Effect<ChildProcessSpawner.ExitCode, VcsError>;
44 + readonly writeStdin: (input: string) => Effect.Effect<void, VcsError>;
45 +}
46 +
47 +export interface VcsProcessShape {
48 + readonly withProcess: <A, E, R>(
49 + input: VcsProcessInput,
50 + use: (handle: VcsProcessHandle) => Effect.Effect<A, E, R>,
51 + ) => Effect.Effect<A, E | VcsError, R>;
52 + readonly run: (input: VcsProcessInput) => Effect.Effect<VcsProcessOutput, VcsError>;
53 +}
54 +
55 +export class VcsProcess extends Context.Service<VcsProcess, VcsProcessShape>()(
56 + "t3/vcs/VcsProcess",
57 +) {}
58 +
59 +const DEFAULT_TIMEOUT_MS = 30_000;
60 +const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
61 +const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]";
62 +
63 +function commandLabel(command: string, args: ReadonlyArray<string>): string {
64 + return [command, ...args].join(" ");
65 +}
66 +
67 +function outputDecodeError(
68 + input: VcsProcessInput,
69 + detail: string,
70 + cause: unknown,
71 +): VcsOutputDecodeError {
72 + return new VcsOutputDecodeError({
73 + operation: input.operation,
74 + command: commandLabel(input.command, input.args),
75 + cwd: input.cwd,
76 + detail,
77 + cause,
78 + });
79 +}
80 +
81 +export const collectText = Effect.fn("VcsProcess.collectText")(function* (input: {
82 + readonly operation: string;
83 + readonly command: string;
84 + readonly cwd: string;
85 + readonly stream: Stream.Stream<Uint8Array, VcsError>;
86 + readonly maxOutputBytes?: number;
87 + readonly truncateOutputAtMaxBytes?: boolean;
88 +}) {
89 + const decoder = new TextDecoder();
90 + let text = "";
91 + let bytes = 0;
92 + let truncated = false;
93 + const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
94 + const truncateOutputAtMaxBytes = input.truncateOutputAtMaxBytes ?? false;
95 +
96 + yield* Stream.runForEach(input.stream, (chunk) =>
97 + Effect.sync(() => {
98 + if (truncated) return;
99 +
100 + const remainingBytes = maxOutputBytes - bytes;
101 + if (remainingBytes <= 0) {
102 + truncated = true;
103 + if (truncateOutputAtMaxBytes) {
104 + text += OUTPUT_TRUNCATED_MARKER;
105 + }
106 + return;
107 + }
108 +
109 + const nextChunk = chunk.byteLength > remainingBytes ? chunk.slice(0, remainingBytes) : chunk;
110 + text += decoder.decode(nextChunk, { stream: true });
111 + bytes += nextChunk.byteLength;
112 +
113 + if (chunk.byteLength > remainingBytes) {
114 + truncated = true;
115 + if (truncateOutputAtMaxBytes) {
116 + text += OUTPUT_TRUNCATED_MARKER;
117 + }
118 + }
119 + }),
120 + );
121 +
122 + if (!truncated) {
123 + text += decoder.decode();
124 + }
125 +
126 + return { text, truncated } satisfies VcsProcessCollectedText;
127 +});
128 +
129 +export const make = Effect.fn("makeVcsProcess")(function* () {
130 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
131 +
132 + const spawn = Effect.fn("VcsProcess.spawn")(function* (input: VcsProcessInput) {
133 + const label = commandLabel(input.command, input.args);
134 + const child = yield* spawner
135 + .spawn(
136 + ChildProcess.make(input.command, [...input.args], {
137 + cwd: input.cwd,
138 + env: {
139 + ...process.env,
140 + ...input.env,
141 + },
142 + }),
143 + )
144 + .pipe(
145 + Effect.mapError(
146 + (cause) =>
147 + new VcsProcessSpawnError({
148 + operation: input.operation,
149 + command: label,
150 + cwd: input.cwd,
151 + cause,
152 + }),
153 + ),
154 + );
155 + yield* Effect.addFinalizer(() => child.kill().pipe(Effect.ignore));
156 +
157 + const mapStreamError = (streamName: "stdout" | "stderr") =>
158 + Stream.mapError((cause: PlatformError.PlatformError) =>
159 + outputDecodeError(input, `failed to read process ${streamName}`, cause),
160 + );
161 + const mapEffectError = (detail: string) =>
162 + Effect.mapError((cause: PlatformError.PlatformError) =>
163 + outputDecodeError(input, detail, cause),
164 + );
165 + const writeStdin = (stdin: string) =>
166 + Stream.run(Stream.encodeText(Stream.make(stdin)), child.stdin).pipe(
167 + mapEffectError("failed to write process stdin"),
168 + );
169 +
170 + return {
171 + pid: child.pid,
172 + stdin: child.stdin.pipe(
173 + Sink.mapError((cause) => outputDecodeError(input, "failed to write process stdin", cause)),
174 + ),
175 + stdout: child.stdout.pipe(mapStreamError("stdout")),
176 + stderr: child.stderr.pipe(mapStreamError("stderr")),
177 + exitCode: child.exitCode.pipe(mapEffectError("failed to read process exit code")),
178 + writeStdin,
179 + } satisfies VcsProcessHandle;
180 + });
181 +
182 + const withProcess: VcsProcessShape["withProcess"] = (input, use) =>
183 + Effect.scoped(spawn(input).pipe(Effect.flatMap(use)));
184 +
185 + const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) {
186 + const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
187 + const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
188 + const label = commandLabel(input.command, input.args);
189 +
190 + const runProcess = Effect.gen(function* () {
191 + const [stdout, stderr, exitCode] = yield* withProcess(input, (child) =>
192 + Effect.all(
193 + [
194 + collectText({
195 + operation: input.operation,
196 + command: label,
197 + cwd: input.cwd,
198 + stream: child.stdout,
199 + maxOutputBytes,
200 + truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false,
201 + }),
202 + collectText({
203 + operation: input.operation,
204 + command: label,
205 + cwd: input.cwd,
206 + stream: child.stderr,
207 + maxOutputBytes,
208 + truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false,
209 + }),
210 + child.exitCode,
211 + input.stdin === undefined ? Effect.void : child.writeStdin(input.stdin),
212 + ],
213 + { concurrency: "unbounded" },
214 + ),
215 + ).pipe(Effect.map(([stdout, stderr, exitCode]) => [stdout, stderr, exitCode] as const));
216 +
217 + if (!input.allowNonZeroExit && exitCode !== 0) {
218 + return yield* new VcsProcessExitError({
219 + operation: input.operation,
220 + command: label,
221 + cwd: input.cwd,
222 + exitCode,
223 + detail: stderr.text.trim() || `${label} exited with code ${exitCode}.`,
224 + });
225 + }
226 +
227 + return {
228 + exitCode,
229 + stdout: stdout.text,
230 + stderr: stderr.text,
231 + stdoutTruncated: stdout.truncated,
232 + stderrTruncated: stderr.truncated,
233 + } satisfies VcsProcessOutput;
234 + });
235 +
236 + return yield* runProcess.pipe(
237 + Effect.scoped,
238 + Effect.timeoutOption(Duration.millis(timeoutMs)),
239 + Effect.flatMap((result) =>
240 + Option.match(result, {
241 + onSome: Effect.succeed,
242 + onNone: () =>
243 + Effect.fail(
244 + new VcsProcessTimeoutError({
245 + operation: input.operation,
246 + command: label,
247 + cwd: input.cwd,
248 + timeoutMs,
249 + }),
250 + ),
251 + }),
252 + ),
253 + );
254 + });
255 +
256 + return VcsProcess.of({ withProcess, run });
257 +});
258 +
259 +export const layer = Layer.effect(VcsProcess, make());
apps/server/src/vcs/VcsProjectConfig.test.ts new
+67
@@ -0,0 +1,67 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as NodeServices from "@effect/platform-node/NodeServices";
3 +import { Effect, FileSystem, Layer, Path } from "effect";
4 +import { describe } from "vitest";
5 +
6 +import { VcsProjectConfig, layer as VcsProjectConfigLayer } from "./VcsProjectConfig.ts";
7 +
8 +const TestLayer = VcsProjectConfigLayer.pipe(
9 + Layer.provide(NodeServices.layer),
10 + Layer.provideMerge(NodeServices.layer),
11 +);
12 +
13 +describe("VcsProjectConfig", () => {
14 + it.layer(TestLayer)("uses an explicit requested VCS kind before config", (it) => {
15 + it.effect("returns the requested kind", () =>
16 + Effect.gen(function* () {
17 + const config = yield* VcsProjectConfig;
18 + const kind = yield* config.resolveKind({
19 + cwd: "/repo",
20 + requestedKind: "jj",
21 + });
22 +
23 + assert.equal(kind, "jj");
24 + }),
25 + );
26 + });
27 +
28 + it.layer(TestLayer)("discovers .t3code/vcs.json from nested workspaces", (it) => {
29 + it.effect("returns the configured kind", () =>
30 + Effect.gen(function* () {
31 + const fileSystem = yield* FileSystem.FileSystem;
32 + const path = yield* Path.Path;
33 + const root = yield* fileSystem.makeTempDirectoryScoped({
34 + prefix: "t3-vcs-config-test-",
35 + });
36 + const configDir = path.join(root, ".t3code");
37 + const nested = path.join(root, "packages", "app");
38 + yield* fileSystem.makeDirectory(configDir, { recursive: true });
39 + yield* fileSystem.makeDirectory(nested, { recursive: true });
40 + yield* fileSystem.writeFileString(
41 + path.join(configDir, "vcs.json"),
42 + JSON.stringify({ vcs: { kind: "jj" } }),
43 + );
44 +
45 + const config = yield* VcsProjectConfig;
46 + const kind = yield* config.resolveKind({ cwd: nested });
47 +
48 + assert.equal(kind, "jj");
49 + }),
50 + );
51 + });
52 +
53 + it.layer(TestLayer)("falls back to auto when no config exists", (it) => {
54 + it.effect("returns auto", () =>
55 + Effect.gen(function* () {
56 + const fileSystem = yield* FileSystem.FileSystem;
57 + const root = yield* fileSystem.makeTempDirectoryScoped({
58 + prefix: "t3-vcs-config-test-",
59 + });
60 + const config = yield* VcsProjectConfig;
61 + const kind = yield* config.resolveKind({ cwd: root });
62 +
63 + assert.equal(kind, "auto");
64 + }),
65 + );
66 + });
67 +});
apps/server/src/vcs/VcsProjectConfig.ts new
+117
@@ -0,0 +1,117 @@
1 +import { Context, Effect, FileSystem, Layer, Path, Schema } from "effect";
2 +
3 +import { VcsDriverKind, type VcsDriverKind as VcsDriverKindType } from "@t3tools/contracts";
4 +
5 +const ProjectVcsConfig = Schema.Struct({
6 + vcs: Schema.optional(
7 + Schema.Struct({
8 + kind: Schema.optional(VcsDriverKind),
9 + }),
10 + ),
11 + vcsKind: Schema.optional(VcsDriverKind),
12 +});
13 +
14 +interface ProjectVcsConfigFile {
15 + readonly vcs?:
16 + | {
17 + readonly kind?: VcsDriverKindType | undefined;
18 + }
19 + | undefined;
20 + readonly vcsKind?: VcsDriverKindType | undefined;
21 +}
22 +
23 +export interface VcsProjectConfigResolveInput {
24 + readonly cwd: string;
25 + readonly requestedKind?: VcsDriverKindType | "auto";
26 +}
27 +
28 +export interface VcsProjectConfigShape {
29 + readonly resolveKind: (
30 + input: VcsProjectConfigResolveInput,
31 + ) => Effect.Effect<VcsDriverKindType | "auto">;
32 +}
33 +
34 +export class VcsProjectConfig extends Context.Service<VcsProjectConfig, VcsProjectConfigShape>()(
35 + "t3/vcs/VcsProjectConfig",
36 +) {}
37 +
38 +function configuredKind(config: ProjectVcsConfigFile): VcsDriverKindType | "auto" {
39 + return config.vcs?.kind ?? config.vcsKind ?? "auto";
40 +}
41 +
42 +function parseConfig(raw: string): ProjectVcsConfigFile | null {
43 + try {
44 + const parsed = JSON.parse(raw) as unknown;
45 + return Schema.is(ProjectVcsConfig)(parsed) ? parsed : null;
46 + } catch {
47 + return null;
48 + }
49 +}
50 +
51 +export const make = Effect.fn("makeVcsProjectConfig")(function* () {
52 + const fileSystem = yield* FileSystem.FileSystem;
53 + const path = yield* Path.Path;
54 +
55 + const findConfigPath = Effect.fn("VcsProjectConfig.findConfigPath")(function* (cwd: string) {
56 + let current = cwd;
57 + while (true) {
58 + const candidate = path.join(current, ".t3code", "vcs.json");
59 + if (yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))) {
60 + return candidate;
61 + }
62 +
63 + const parent = path.dirname(current);
64 + if (parent === current) {
65 + return null;
66 + }
67 + current = parent;
68 + }
69 + });
70 +
71 + const readConfiguredKind = Effect.fn("VcsProjectConfig.readConfiguredKind")(function* (
72 + configPath: string,
73 + ) {
74 + const raw = yield* fileSystem.readFileString(configPath).pipe(
75 + Effect.catch((error) =>
76 + Effect.logWarning("failed to read VCS project config", {
77 + configPath,
78 + error,
79 + }).pipe(Effect.as(null)),
80 + ),
81 + );
82 + if (raw === null) {
83 + return "auto" as const;
84 + }
85 +
86 + const parsed = parseConfig(raw);
87 + if (parsed === null) {
88 + yield* Effect.logWarning("invalid VCS project config", {
89 + configPath,
90 + });
91 + return "auto" as const;
92 + }
93 +
94 + return configuredKind(parsed);
95 + });
96 +
97 + const resolveKind: VcsProjectConfigShape["resolveKind"] = Effect.fn(
98 + "VcsProjectConfig.resolveKind",
99 + )(function* (input) {
100 + if (input.requestedKind !== undefined && input.requestedKind !== "auto") {
101 + return input.requestedKind;
102 + }
103 +
104 + const configPath = yield* findConfigPath(input.cwd);
105 + if (configPath === null) {
106 + return "auto";
107 + }
108 +
109 + return yield* readConfiguredKind(configPath);
110 + });
111 +
112 + return VcsProjectConfig.of({
113 + resolveKind,
114 + });
115 +});
116 +
117 +export const layer = Layer.effect(VcsProjectConfig, make());
apps/server/src/vcs/VcsProvisioningService.test.ts new
+94
@@ -0,0 +1,94 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { DateTime, Effect, Layer, Option } from "effect";
3 +import { ChildProcessSpawner } from "effect/unstable/process";
4 +
5 +import type { VcsDriverShape } from "./VcsDriver.ts";
6 +import { VcsDriverRegistry } from "./VcsDriverRegistry.ts";
7 +import { VcsProvisioningService, layer } from "./VcsProvisioningService.ts";
8 +
9 +const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
10 +
11 +function makeDriver(calls: string[]): VcsDriverShape {
12 + return {
13 + capabilities: {
14 + kind: "git",
15 + supportsWorktrees: true,
16 + supportsBookmarks: false,
17 + supportsAtomicSnapshot: false,
18 + supportsPushDefaultRemote: true,
19 + ignoreClassifier: "native",
20 + },
21 + execute: () =>
22 + Effect.succeed({
23 + exitCode: ChildProcessSpawner.ExitCode(0),
24 + stdout: "",
25 + stderr: "",
26 + stdoutTruncated: false,
27 + stderrTruncated: false,
28 + }),
29 + detectRepository: () => Effect.succeed(null),
30 + isInsideWorkTree: () => Effect.succeed(false),
31 + listWorkspaceFiles: () =>
32 + Effect.succeed({
33 + paths: [],
34 + truncated: false,
35 + freshness: {
36 + source: "live-local",
37 + observedAt: TEST_EPOCH,
38 + expiresAt: Option.none(),
39 + },
40 + }),
41 + listRemotes: () =>
42 + Effect.succeed({
43 + remotes: [],
44 + freshness: {
45 + source: "live-local",
46 + observedAt: TEST_EPOCH,
47 + expiresAt: Option.none(),
48 + },
49 + }),
50 + filterIgnoredPaths: (_cwd, relativePaths) => Effect.succeed(relativePaths),
51 + initRepository: (input) =>
52 + Effect.sync(() => {
53 + calls.push(`${input.kind ?? "default"}:${input.cwd}`);
54 + }),
55 + };
56 +}
57 +
58 +it.effect("routes repository initialization through an explicit VCS driver kind", () => {
59 + const calls: string[] = [];
60 + const driver = makeDriver(calls);
61 + const testLayer = layer.pipe(
62 + Layer.provide(
63 + Layer.mock(VcsDriverRegistry)({
64 + get: (kind) => (kind === "git" ? Effect.succeed(driver) : Effect.die("unexpected kind")),
65 + }),
66 + ),
67 + );
68 +
69 + return Effect.gen(function* () {
70 + const provisioning = yield* VcsProvisioningService;
71 + yield* provisioning.initRepository({ cwd: "/repo", kind: "git" });
72 +
73 + assert.deepStrictEqual(calls, ["git:/repo"]);
74 + }).pipe(Effect.provide(testLayer));
75 +});
76 +
77 +it.effect("defaults repository initialization to Git until callers choose a VCS kind", () => {
78 + const calls: string[] = [];
79 + const driver = makeDriver(calls);
80 + const testLayer = layer.pipe(
81 + Layer.provide(
82 + Layer.mock(VcsDriverRegistry)({
83 + get: (kind) => (kind === "git" ? Effect.succeed(driver) : Effect.die("unexpected kind")),
84 + }),
85 + ),
86 + );
87 +
88 + return Effect.gen(function* () {
89 + const provisioning = yield* VcsProvisioningService;
90 + yield* provisioning.initRepository({ cwd: "/repo" });
91 +
92 + assert.deepStrictEqual(calls, ["default:/repo"]);
93 + }).pipe(Effect.provide(testLayer));
94 +});
apps/server/src/vcs/VcsProvisioningService.ts new
+54
@@ -0,0 +1,54 @@
1 +import { Context, Effect, Layer } from "effect";
2 +
3 +import {
4 + type VcsDriverKind,
5 + type VcsError,
6 + type VcsInitInput,
7 + VcsUnsupportedOperationError,
8 +} from "@t3tools/contracts";
9 +import { VcsDriverRegistry } from "./VcsDriverRegistry.ts";
10 +
11 +export interface VcsProvisioningServiceShape {
12 + readonly initRepository: (input: VcsInitInput) => Effect.Effect<void, VcsError>;
13 +}
14 +
15 +export class VcsProvisioningService extends Context.Service<
16 + VcsProvisioningService,
17 + VcsProvisioningServiceShape
18 +>()("t3/vcs/VcsProvisioningService") {}
19 +
20 +function resolveRequestedKind(
21 + kind: VcsDriverKind | undefined,
22 +): Effect.Effect<VcsDriverKind, VcsUnsupportedOperationError> {
23 + if (kind === undefined) {
24 + return Effect.succeed("git");
25 + }
26 + if (kind === "unknown") {
27 + return Effect.fail(
28 + new VcsUnsupportedOperationError({
29 + operation: "VcsProvisioningService.resolveRequestedKind",
30 + kind,
31 + detail: "A concrete VCS driver kind is required for repository provisioning.",
32 + }),
33 + );
34 + }
35 + return Effect.succeed(kind);
36 +}
37 +
38 +export const make = Effect.fn("makeVcsProvisioningService")(function* () {
39 + const registry = yield* VcsDriverRegistry;
40 +
41 + const initRepository: VcsProvisioningServiceShape["initRepository"] = Effect.fn(
42 + "VcsProvisioningService.initRepository",
43 + )(function* (input) {
44 + const kind = yield* resolveRequestedKind(input.kind);
45 + const driver = yield* registry.get(kind);
46 + return yield* driver.initRepository(input);
47 + });
48 +
49 + return VcsProvisioningService.of({
50 + initRepository,
51 + });
52 +});
53 +
54 +export const layer = Layer.effect(VcsProvisioningService, make());
apps/server/src/vcs/VcsStatusBroadcaster.test.ts renamed
+124 -72
@@ -1,83 +1,82 @@
1 import { assert, it } from "@effect/vitest";
2 -import { Deferred, Effect, Exit, Layer, Option, Scope, Stream } from "effect";
2 +import * as NodeServices from "@effect/platform-node/NodeServices";
3 +import { Deferred, Effect, Exit, FileSystem, Layer, Option, Path, Scope, Stream } from "effect";
4 import type {
4 - GitStatusLocalResult,
5 - GitStatusRemoteResult,
6 - GitStatusResult,
7 - GitStatusStreamEvent,
5 + VcsStatusLocalResult,
6 + VcsStatusRemoteResult,
7 + VcsStatusResult,
8 + VcsStatusStreamEvent,
9 } from "@t3tools/contracts";
10 import { describe } from "vitest";
11
11 -import { GitStatusBroadcaster } from "../Services/GitStatusBroadcaster.ts";
12 -import { GitStatusBroadcasterLive } from "./GitStatusBroadcaster.ts";
13 -import { type GitManagerShape, GitManager } from "../Services/GitManager.ts";
12 +import {
13 + VcsStatusBroadcaster,
14 + layer as VcsStatusBroadcasterLayer,
15 +} from "./VcsStatusBroadcaster.ts";
16 +import { GitWorkflowService, type GitWorkflowServiceShape } from "../git/GitWorkflowService.ts";
17
15 -const baseLocalStatus: GitStatusLocalResult = {
18 +const baseLocalStatus: VcsStatusLocalResult = {
19 isRepo: true,
17 - hostingProvider: {
20 + sourceControlProvider: {
21 kind: "github",
22 name: "GitHub",
23 baseUrl: "https://github.com",
24 },
22 - hasOriginRemote: true,
23 - isDefaultBranch: false,
24 - branch: "feature/status-broadcast",
25 + hasPrimaryRemote: true,
26 + isDefaultRef: false,
27 + refName: "feature/status-broadcast",
28 hasWorkingTreeChanges: false,
29 workingTree: { files: [], insertions: 0, deletions: 0 },
30 };
31
29 -const baseRemoteStatus: GitStatusRemoteResult = {
32 +const baseRemoteStatus: VcsStatusRemoteResult = {
33 hasUpstream: true,
34 aheadCount: 0,
35 behindCount: 0,
36 pr: null,
37 };
38
36 -const baseStatus: GitStatusResult = {
39 +const baseStatus: VcsStatusResult = {
40 ...baseLocalStatus,
41 ...baseRemoteStatus,
42 };
43
44 function makeTestLayer(state: {
42 - currentLocalStatus: GitStatusLocalResult;
43 - currentRemoteStatus: GitStatusRemoteResult | null;
45 + currentLocalStatus: VcsStatusLocalResult;
46 + currentRemoteStatus: VcsStatusRemoteResult | null;
47 localStatusCalls: number;
48 remoteStatusCalls: number;
49 localInvalidationCalls: number;
50 remoteInvalidationCalls: number;
51 }) {
49 - const gitManager: GitManagerShape = {
50 - localStatus: () =>
51 - Effect.sync(() => {
52 - state.localStatusCalls += 1;
53 - return state.currentLocalStatus;
52 + return VcsStatusBroadcasterLayer.pipe(
53 + Layer.provide(
54 + Layer.mock(GitWorkflowService)({
55 + localStatus: () =>
56 + Effect.sync(() => {
57 + state.localStatusCalls += 1;
58 + return state.currentLocalStatus;
59 + }),
60 + remoteStatus: () =>
61 + Effect.sync(() => {
62 + state.remoteStatusCalls += 1;
63 + return state.currentRemoteStatus;
64 + }),
65 + invalidateLocalStatus: () =>
66 + Effect.sync(() => {
67 + state.localInvalidationCalls += 1;
68 + }),
69 + invalidateRemoteStatus: () =>
70 + Effect.sync(() => {
71 + state.remoteInvalidationCalls += 1;
72 + }),
73 }),
55 - remoteStatus: () =>
56 - Effect.sync(() => {
57 - state.remoteStatusCalls += 1;
58 - return state.currentRemoteStatus;
59 - }),
60 - status: () => Effect.die("status should not be called in this test"),
61 - invalidateLocalStatus: () =>
62 - Effect.sync(() => {
63 - state.localInvalidationCalls += 1;
64 - }),
65 - invalidateRemoteStatus: () =>
66 - Effect.sync(() => {
67 - state.remoteInvalidationCalls += 1;
68 - }),
69 - invalidateStatus: () => Effect.die("invalidateStatus should not be called in this test"),
70 - resolvePullRequest: () => Effect.die("resolvePullRequest should not be called in this test"),
71 - preparePullRequestThread: () =>
72 - Effect.die("preparePullRequestThread should not be called in this test"),
73 - runStackedAction: () => Effect.die("runStackedAction should not be called in this test"),
74 - };
75 -
76 - return GitStatusBroadcasterLive.pipe(Layer.provide(Layer.succeed(GitManager, gitManager)));
74 + ),
75 + );
76 }
77
79 -describe("GitStatusBroadcasterLive", () => {
80 - it.effect("reuses the cached git status across repeated reads", () => {
78 +describe("VcsStatusBroadcaster", () => {
79 + it.effect("reuses the cached VCS status across repeated reads", () => {
80 const state = {
81 currentLocalStatus: baseLocalStatus,
82 currentRemoteStatus: baseRemoteStatus,
@@ -88,7 +87,7 @@ describe("GitStatusBroadcasterLive", () => {
87 };
88
89 return Effect.gen(function* () {
91 - const broadcaster = yield* GitStatusBroadcaster;
90 + const broadcaster = yield* VcsStatusBroadcaster;
91
92 const first = yield* broadcaster.getStatus({ cwd: "/repo" });
93 const second = yield* broadcaster.getStatus({ cwd: "/repo" });
@@ -113,12 +112,12 @@ describe("GitStatusBroadcasterLive", () => {
112 };
113
114 return Effect.gen(function* () {
116 - const broadcaster = yield* GitStatusBroadcaster;
115 + const broadcaster = yield* VcsStatusBroadcaster;
116 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
117
118 state.currentLocalStatus = {
119 ...baseLocalStatus,
121 - branch: "feature/updated-status",
120 + refName: "feature/updated-status",
121 };
122 state.currentRemoteStatus = {
123 ...baseRemoteStatus,
@@ -154,12 +153,12 @@ describe("GitStatusBroadcasterLive", () => {
153 };
154
155 return Effect.gen(function* () {
157 - const broadcaster = yield* GitStatusBroadcaster;
156 + const broadcaster = yield* VcsStatusBroadcaster;
157 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
158
159 state.currentLocalStatus = {
160 ...baseLocalStatus,
162 - branch: "feature/local-only-refresh",
161 + refName: "feature/local-only-refresh",
162 hasWorkingTreeChanges: true,
163 };
164
@@ -179,6 +178,66 @@ describe("GitStatusBroadcasterLive", () => {
178 }).pipe(Effect.provide(makeTestLayer(state)));
179 });
180
181 + it.effect("normalizes symlinked CWDs before cache lookup and workflow calls", () => {
182 + const seenCwds: string[] = [];
183 + const state = {
184 + currentLocalStatus: baseLocalStatus,
185 + currentRemoteStatus: baseRemoteStatus,
186 + localStatusCalls: 0,
187 + remoteStatusCalls: 0,
188 + localInvalidationCalls: 0,
189 + remoteInvalidationCalls: 0,
190 + };
191 + const testLayer = VcsStatusBroadcasterLayer.pipe(
192 + Layer.provide(
193 + Layer.mock(GitWorkflowService)({
194 + localStatus: (input) =>
195 + Effect.sync(() => {
196 + seenCwds.push(input.cwd);
197 + state.localStatusCalls += 1;
198 + return state.currentLocalStatus;
199 + }),
200 + remoteStatus: (input) =>
201 + Effect.sync(() => {
202 + seenCwds.push(input.cwd);
203 + state.remoteStatusCalls += 1;
204 + return state.currentRemoteStatus;
205 + }),
206 + invalidateLocalStatus: () =>
207 + Effect.sync(() => {
208 + state.localInvalidationCalls += 1;
209 + }),
210 + invalidateRemoteStatus: () =>
211 + Effect.sync(() => {
212 + state.remoteInvalidationCalls += 1;
213 + }),
214 + } satisfies Partial<GitWorkflowServiceShape>),
215 + ),
216 + );
217 +
218 + return Effect.gen(function* () {
219 + const fileSystem = yield* FileSystem.FileSystem;
220 + const path = yield* Path.Path;
221 + const realDir = yield* fileSystem.makeTempDirectoryScoped({
222 + prefix: "t3-vcs-status-real-",
223 + });
224 + const linkParent = yield* fileSystem.makeTempDirectoryScoped({
225 + prefix: "t3-vcs-status-link-",
226 + });
227 + const linkDir = path.join(linkParent, "repo-link");
228 + yield* fileSystem.symlink(realDir, linkDir);
229 + const realPath = yield* fileSystem.realPath(realDir);
230 +
231 + const broadcaster = yield* VcsStatusBroadcaster;
232 + yield* broadcaster.getStatus({ cwd: linkDir });
233 + yield* broadcaster.getStatus({ cwd: realDir });
234 +
235 + assert.deepStrictEqual(seenCwds, [realPath, realPath]);
236 + assert.equal(state.localStatusCalls, 1);
237 + assert.equal(state.remoteStatusCalls, 1);
238 + }).pipe(Effect.provide(Layer.mergeAll(testLayer, NodeServices.layer)));
239 + });
240 +
241 it.effect("streams a local snapshot first and remote updates later", () => {
242 const state = {
243 currentLocalStatus: baseLocalStatus,
@@ -190,9 +249,9 @@ describe("GitStatusBroadcasterLive", () => {
249 };
250
251 return Effect.gen(function* () {
193 - const broadcaster = yield* GitStatusBroadcaster;
194 - const snapshotDeferred = yield* Deferred.make<GitStatusStreamEvent>();
195 - const remoteUpdatedDeferred = yield* Deferred.make<GitStatusStreamEvent>();
252 + const broadcaster = yield* VcsStatusBroadcaster;
253 + const snapshotDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
254 + const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
255 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => {
256 if (event._tag === "snapshot") {
257 return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);
@@ -211,11 +270,11 @@ describe("GitStatusBroadcasterLive", () => {
270 _tag: "snapshot",
271 local: baseLocalStatus,
272 remote: null,
214 - } satisfies GitStatusStreamEvent);
273 + } satisfies VcsStatusStreamEvent);
274 assert.deepStrictEqual(remoteUpdated, {
275 _tag: "remoteUpdated",
276 remote: baseRemoteStatus,
218 - } satisfies GitStatusStreamEvent);
277 + } satisfies VcsStatusStreamEvent);
278 }).pipe(Effect.provide(makeTestLayer(state)));
279 });
280
@@ -230,9 +289,9 @@ describe("GitStatusBroadcasterLive", () => {
289 };
290 let remoteInterruptedDeferred: Deferred.Deferred<void, never> | null = null;
291 let remoteStartedDeferred: Deferred.Deferred<void, never> | null = null;
233 - const testLayer = GitStatusBroadcasterLive.pipe(
292 + const testLayer = VcsStatusBroadcasterLayer.pipe(
293 Layer.provide(
235 - Layer.succeed(GitManager, {
294 + Layer.mock(GitWorkflowService)({
295 localStatus: () =>
296 Effect.sync(() => {
297 state.localStatusCalls += 1;
@@ -247,14 +306,13 @@ describe("GitStatusBroadcasterLive", () => {
306 ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore)
307 : Effect.void,
308 ),
250 - Effect.andThen(Effect.never as Effect.Effect<GitStatusRemoteResult | null, never>),
309 + Effect.andThen(Effect.never as Effect.Effect<VcsStatusRemoteResult | null, never>),
310 Effect.onInterrupt(() =>
311 remoteInterruptedDeferred
312 ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore)
313 : Effect.void,
314 ),
315 ),
257 - status: () => Effect.die("status should not be called in this test"),
316 invalidateLocalStatus: () =>
317 Effect.sync(() => {
318 state.localInvalidationCalls += 1;
@@ -263,13 +321,7 @@ describe("GitStatusBroadcasterLive", () => {
321 Effect.sync(() => {
322 state.remoteInvalidationCalls += 1;
323 }),
266 - invalidateStatus: () => Effect.die("invalidateStatus should not be called in this test"),
267 - resolvePullRequest: () =>
268 - Effect.die("resolvePullRequest should not be called in this test"),
269 - preparePullRequestThread: () =>
270 - Effect.die("preparePullRequestThread should not be called in this test"),
271 - runStackedAction: () => Effect.die("runStackedAction should not be called in this test"),
272 - } satisfies GitManagerShape),
324 + } satisfies Partial<GitWorkflowServiceShape>),
325 ),
326 );
327
@@ -279,9 +331,9 @@ describe("GitStatusBroadcasterLive", () => {
331 remoteInterruptedDeferred = remoteInterrupted;
332 remoteStartedDeferred = remoteStarted;
333
282 - const broadcaster = yield* GitStatusBroadcaster;
283 - const firstSnapshot = yield* Deferred.make<GitStatusStreamEvent>();
284 - const secondSnapshot = yield* Deferred.make<GitStatusStreamEvent>();
334 + const broadcaster = yield* VcsStatusBroadcaster;
335 + const firstSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();
336 + const secondSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();
337 const firstScope = yield* Scope.make();
338 const secondScope = yield* Scope.make();
339 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
@@ -302,11 +354,11 @@ describe("GitStatusBroadcasterLive", () => {
354 assert.equal(state.remoteStatusCalls, 1);
355
356 yield* Scope.close(firstScope, Exit.void);
305 - assert.equal(Option.isNone(yield* Deferred.poll(remoteInterrupted)), true);
357 + assert.isTrue(Option.isNone(yield* Deferred.poll(remoteInterrupted)));
358
359 yield* Scope.close(secondScope, Exit.void).pipe(Effect.forkScoped);
360 yield* Deferred.await(remoteInterrupted);
309 - assert.equal(Option.isSome(yield* Deferred.poll(remoteInterrupted)), true);
361 + assert.isTrue(Option.isSome(yield* Deferred.poll(remoteInterrupted)));
362 }).pipe(Effect.provide(testLayer));
363 });
364 });
apps/server/src/vcs/VcsStatusBroadcaster.ts new
+344
@@ -0,0 +1,344 @@
1 +import { realpathSync } from "node:fs";
2 +
3 +import {
4 + Duration,
5 + Effect,
6 + Exit,
7 + Fiber,
8 + Layer,
9 + PubSub,
10 + Ref,
11 + Scope,
12 + Stream,
13 + SynchronizedRef,
14 +} from "effect";
15 +import type {
16 + GitManagerServiceError,
17 + VcsStatusInput,
18 + VcsStatusLocalResult,
19 + VcsStatusRemoteResult,
20 + VcsStatusResult,
21 + VcsStatusStreamEvent,
22 +} from "@t3tools/contracts";
23 +import { Context } from "effect";
24 +import { mergeGitStatusParts } from "@t3tools/shared/git";
25 +
26 +import { GitWorkflowService } from "../git/GitWorkflowService.ts";
27 +
28 +const VCS_STATUS_REFRESH_INTERVAL = Duration.seconds(30);
29 +
30 +interface VcsStatusChange {
31 + readonly cwd: string;
32 + readonly event: VcsStatusStreamEvent;
33 +}
34 +
35 +interface CachedValue<T> {
36 + readonly fingerprint: string;
37 + readonly value: T;
38 +}
39 +
40 +interface CachedVcsStatus {
41 + readonly local: CachedValue<VcsStatusLocalResult> | null;
42 + readonly remote: CachedValue<VcsStatusRemoteResult | null> | null;
43 +}
44 +
45 +interface ActiveRemotePoller {
46 + readonly fiber: Fiber.Fiber<void, never>;
47 + readonly subscriberCount: number;
48 +}
49 +
50 +export interface VcsStatusBroadcasterShape {
51 + readonly getStatus: (
52 + input: VcsStatusInput,
53 + ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
54 + readonly refreshLocalStatus: (
55 + cwd: string,
56 + ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
57 + readonly refreshStatus: (cwd: string) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
58 + readonly streamStatus: (
59 + input: VcsStatusInput,
60 + ) => Stream.Stream<VcsStatusStreamEvent, GitManagerServiceError>;
61 +}
62 +
63 +export class VcsStatusBroadcaster extends Context.Service<
64 + VcsStatusBroadcaster,
65 + VcsStatusBroadcasterShape
66 +>()("t3/vcs/VcsStatusBroadcaster") {}
67 +
68 +function fingerprintStatusPart(status: unknown): string {
69 + return JSON.stringify(status);
70 +}
71 +
72 +function normalizeCwd(cwd: string): string {
73 + try {
74 + return realpathSync.native(cwd);
75 + } catch {
76 + return cwd;
77 + }
78 +}
79 +
80 +export const layer = Layer.effect(
81 + VcsStatusBroadcaster,
82 + Effect.gen(function* () {
83 + const workflow = yield* GitWorkflowService;
84 + const changesPubSub = yield* Effect.acquireRelease(
85 + PubSub.unbounded<VcsStatusChange>(),
86 + (pubsub) => PubSub.shutdown(pubsub),
87 + );
88 + const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
89 + Scope.close(scope, Exit.void),
90 + );
91 + const cacheRef = yield* Ref.make(new Map<string, CachedVcsStatus>());
92 + const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
93 +
94 + const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (
95 + cwd: string,
96 + ) {
97 + return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null));
98 + });
99 +
100 + const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")(
101 + function* (cwd: string, local: VcsStatusLocalResult, options?: { publish?: boolean }) {
102 + const nextLocal = {
103 + fingerprint: fingerprintStatusPart(local),
104 + value: local,
105 + } satisfies CachedValue<VcsStatusLocalResult>;
106 + const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
107 + const previous = cache.get(cwd) ?? { local: null, remote: null };
108 + const nextCache = new Map(cache);
109 + nextCache.set(cwd, {
110 + ...previous,
111 + local: nextLocal,
112 + });
113 + return [previous.local?.fingerprint !== nextLocal.fingerprint, nextCache] as const;
114 + });
115 +
116 + if (options?.publish && shouldPublish) {
117 + yield* PubSub.publish(changesPubSub, {
118 + cwd,
119 + event: {
120 + _tag: "localUpdated",
121 + local,
122 + },
123 + });
124 + }
125 +
126 + return local;
127 + },
128 + );
129 +
130 + const updateCachedRemoteStatus = Effect.fn("VcsStatusBroadcaster.updateCachedRemoteStatus")(
131 + function* (
132 + cwd: string,
133 + remote: VcsStatusRemoteResult | null,
134 + options?: { publish?: boolean },
135 + ) {
136 + const nextRemote = {
137 + fingerprint: fingerprintStatusPart(remote),
138 + value: remote,
139 + } satisfies CachedValue<VcsStatusRemoteResult | null>;
140 + const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
141 + const previous = cache.get(cwd) ?? { local: null, remote: null };
142 + const nextCache = new Map(cache);
143 + nextCache.set(cwd, {
144 + ...previous,
145 + remote: nextRemote,
146 + });
147 + return [previous.remote?.fingerprint !== nextRemote.fingerprint, nextCache] as const;
148 + });
149 +
150 + if (options?.publish && shouldPublish) {
151 + yield* PubSub.publish(changesPubSub, {
152 + cwd,
153 + event: {
154 + _tag: "remoteUpdated",
155 + remote,
156 + },
157 + });
158 + }
159 +
160 + return remote;
161 + },
162 + );
163 +
164 + const loadLocalStatus = Effect.fn("VcsStatusBroadcaster.loadLocalStatus")(function* (
165 + cwd: string,
166 + ) {
167 + const local = yield* workflow.localStatus({ cwd });
168 + return yield* updateCachedLocalStatus(cwd, local);
169 + });
170 +
171 + const loadRemoteStatus = Effect.fn("VcsStatusBroadcaster.loadRemoteStatus")(function* (
172 + cwd: string,
173 + ) {
174 + const remote = yield* workflow.remoteStatus({ cwd });
175 + return yield* updateCachedRemoteStatus(cwd, remote);
176 + });
177 +
178 + const getOrLoadLocalStatus = Effect.fn("VcsStatusBroadcaster.getOrLoadLocalStatus")(function* (
179 + cwd: string,
180 + ) {
181 + const cached = yield* getCachedStatus(cwd);
182 + if (cached?.local) {
183 + return cached.local.value;
184 + }
185 + return yield* loadLocalStatus(cwd);
186 + });
187 +
188 + const getOrLoadRemoteStatus = Effect.fn("VcsStatusBroadcaster.getOrLoadRemoteStatus")(
189 + function* (cwd: string) {
190 + const cached = yield* getCachedStatus(cwd);
191 + if (cached?.remote) {
192 + return cached.remote.value;
193 + }
194 + return yield* loadRemoteStatus(cwd);
195 + },
196 + );
197 +
198 + const getStatus: VcsStatusBroadcasterShape["getStatus"] = Effect.fn(
199 + "VcsStatusBroadcaster.getStatus",
200 + )(function* (input) {
201 + const cwd = normalizeCwd(input.cwd);
202 + const [local, remote] = yield* Effect.all([
203 + getOrLoadLocalStatus(cwd),
204 + getOrLoadRemoteStatus(cwd),
205 + ]);
206 + return mergeGitStatusParts(local, remote);
207 + });
208 +
209 + const refreshLocalStatus: VcsStatusBroadcasterShape["refreshLocalStatus"] = Effect.fn(
210 + "VcsStatusBroadcaster.refreshLocalStatus",
211 + )(function* (rawCwd) {
212 + const cwd = normalizeCwd(rawCwd);
213 + yield* workflow.invalidateLocalStatus(cwd);
214 + const local = yield* workflow.localStatus({ cwd });
215 + return yield* updateCachedLocalStatus(cwd, local, { publish: true });
216 + });
217 +
218 + const refreshRemoteStatus = Effect.fn("VcsStatusBroadcaster.refreshRemoteStatus")(function* (
219 + cwd: string,
220 + ) {
221 + yield* workflow.invalidateRemoteStatus(cwd);
222 + const remote = yield* workflow.remoteStatus({ cwd });
223 + return yield* updateCachedRemoteStatus(cwd, remote, { publish: true });
224 + });
225 +
226 + const refreshStatus: VcsStatusBroadcasterShape["refreshStatus"] = Effect.fn(
227 + "VcsStatusBroadcaster.refreshStatus",
228 + )(function* (rawCwd) {
229 + const cwd = normalizeCwd(rawCwd);
230 + const [local, remote] = yield* Effect.all([
231 + refreshLocalStatus(cwd),
232 + refreshRemoteStatus(cwd),
233 + ]);
234 + return mergeGitStatusParts(local, remote);
235 + });
236 +
237 + const makeRemoteRefreshLoop = (cwd: string) => {
238 + const logRefreshFailure = (error: Error) =>
239 + Effect.logWarning("VCS remote status refresh failed", {
240 + cwd,
241 + detail: error.message,
242 + });
243 +
244 + return refreshRemoteStatus(cwd).pipe(
245 + Effect.catch(logRefreshFailure),
246 + Effect.andThen(
247 + Effect.forever(
248 + Effect.sleep(VCS_STATUS_REFRESH_INTERVAL).pipe(
249 + Effect.andThen(refreshRemoteStatus(cwd).pipe(Effect.catch(logRefreshFailure))),
250 + ),
251 + ),
252 + ),
253 + );
254 + };
255 +
256 + const retainRemotePoller = Effect.fn("VcsStatusBroadcaster.retainRemotePoller")(function* (
257 + cwd: string,
258 + ) {
259 + yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => {
260 + const existing = activePollers.get(cwd);
261 + if (existing) {
262 + const nextPollers = new Map(activePollers);
263 + nextPollers.set(cwd, {
264 + ...existing,
265 + subscriberCount: existing.subscriberCount + 1,
266 + });
267 + return Effect.succeed([undefined, nextPollers] as const);
268 + }
269 +
270 + return makeRemoteRefreshLoop(cwd).pipe(
271 + Effect.forkIn(broadcasterScope),
272 + Effect.map((fiber) => {
273 + const nextPollers = new Map(activePollers);
274 + nextPollers.set(cwd, {
275 + fiber,
276 + subscriberCount: 1,
277 + });
278 + return [undefined, nextPollers] as const;
279 + }),
280 + );
281 + });
282 + });
283 +
284 + const releaseRemotePoller = Effect.fn("VcsStatusBroadcaster.releaseRemotePoller")(function* (
285 + cwd: string,
286 + ) {
287 + const pollerToInterrupt = yield* SynchronizedRef.modify(pollersRef, (activePollers) => {
288 + const existing = activePollers.get(cwd);
289 + if (!existing) {
290 + return [null, activePollers] as const;
291 + }
292 +
293 + if (existing.subscriberCount > 1) {
294 + const nextPollers = new Map(activePollers);
295 + nextPollers.set(cwd, {
296 + ...existing,
297 + subscriberCount: existing.subscriberCount - 1,
298 + });
299 + return [null, nextPollers] as const;
300 + }
301 +
302 + const nextPollers = new Map(activePollers);
303 + nextPollers.delete(cwd);
304 + return [existing.fiber, nextPollers] as const;
305 + });
306 +
307 + if (pollerToInterrupt) {
308 + yield* Fiber.interrupt(pollerToInterrupt).pipe(Effect.ignore);
309 + }
310 + });
311 +
312 + const streamStatus: VcsStatusBroadcasterShape["streamStatus"] = (input) =>
313 + Stream.unwrap(
314 + Effect.gen(function* () {
315 + const cwd = normalizeCwd(input.cwd);
316 + const subscription = yield* PubSub.subscribe(changesPubSub);
317 + const initialLocal = yield* getOrLoadLocalStatus(cwd);
318 + const initialRemote = (yield* getCachedStatus(cwd))?.remote?.value ?? null;
319 + yield* retainRemotePoller(cwd);
320 +
321 + const release = releaseRemotePoller(cwd).pipe(Effect.ignore, Effect.asVoid);
322 +
323 + return Stream.concat(
324 + Stream.make({
325 + _tag: "snapshot" as const,
326 + local: initialLocal,
327 + remote: initialRemote,
328 + }),
329 + Stream.fromSubscription(subscription).pipe(
330 + Stream.filter((event) => event.cwd === cwd),
331 + Stream.map((event) => event.event),
332 + ),
333 + ).pipe(Stream.ensuring(release));
334 + }),
335 + );
336 +
337 + return VcsStatusBroadcaster.of({
338 + getStatus,
339 + refreshLocalStatus,
340 + refreshStatus,
341 + streamStatus,
342 + });
343 + }),
344 +);
apps/server/src/vcs/testing/VcsDriverContractHarness.ts new
+152
@@ -0,0 +1,152 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { DateTime, Option } from "effect";
3 +import { Effect, FileSystem, Layer, Path, type PlatformError, type Scope } from "effect";
4 +import { describe } from "vitest";
5 +
6 +import type { VcsDriverKind } from "@t3tools/contracts";
7 +import { VcsDriver } from "../VcsDriver.ts";
8 +
9 +export interface VcsDriverFixture<R, E> {
10 + readonly createRepo: (cwd: string) => Effect.Effect<void, E, R>;
11 + readonly writeFile: (
12 + cwd: string,
13 + relativePath: string,
14 + contents: string,
15 + ) => Effect.Effect<void, E, R | FileSystem.FileSystem | Path.Path>;
16 + readonly trackFile?: (cwd: string, relativePath: string) => Effect.Effect<void, E, R>;
17 + readonly commit?: (cwd: string, message: string) => Effect.Effect<void, E, R>;
18 + readonly ignorePath: (
19 + cwd: string,
20 + pattern: string,
21 + ) => Effect.Effect<void, E, R | FileSystem.FileSystem | Path.Path>;
22 +}
23 +
24 +export interface VcsDriverContractSuiteInput<R, E> {
25 + readonly name: string;
26 + readonly kind: VcsDriverKind;
27 + readonly layer: Layer.Layer<VcsDriver | R | FileSystem.FileSystem | Path.Path, E, never>;
28 + readonly fixture: VcsDriverFixture<R, E>;
29 +}
30 +
31 +export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInput<R, E>) {
32 + const makeTmpDir = (
33 + prefix = `t3-${input.kind}-vcs-contract-`,
34 + ): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
35 + Effect.gen(function* () {
36 + const fileSystem = yield* FileSystem.FileSystem;
37 + return yield* fileSystem.makeTempDirectoryScoped({ prefix });
38 + });
39 +
40 + it.layer(input.layer)(`${input.name} VCS driver contract`, (it) => {
41 + describe("repository detection", () => {
42 + it.effect("returns null outside a repository", () =>
43 + Effect.gen(function* () {
44 + const cwd = yield* makeTmpDir();
45 + const driver = yield* VcsDriver;
46 +
47 + assert.equal(yield* driver.detectRepository(cwd), null);
48 + assert.equal(yield* driver.isInsideWorkTree(cwd), false);
49 + }),
50 + );
51 +
52 + it.effect("detects repository identity inside a repository and nested directories", () =>
53 + Effect.gen(function* () {
54 + const cwd = yield* makeTmpDir();
55 + const driver = yield* VcsDriver;
56 +
57 + yield* input.fixture.createRepo(cwd);
58 + yield* input.fixture.writeFile(cwd, "src/index.ts", "export const value = 1;\n");
59 + const identity = yield* driver.detectRepository(cwd);
60 + assert.equal(identity?.kind, input.kind);
61 + assert.isTrue(identity?.rootPath.endsWith(cwd));
62 + assert.equal(identity?.freshness.source, "live-local");
63 + assert.isTrue(DateTime.isDateTime(identity?.freshness.observedAt));
64 + assert.isTrue(Option.isNone(identity?.freshness.expiresAt ?? Option.none()));
65 + assert.equal(yield* driver.isInsideWorkTree(cwd), true);
66 +
67 + const path = yield* Path.Path;
68 + const nestedDir = path.join(cwd, "src");
69 + const nestedIdentity = yield* driver.detectRepository(nestedDir);
70 + assert.equal(nestedIdentity?.rootPath, identity?.rootPath);
71 + assert.equal(yield* driver.isInsideWorkTree(nestedDir), true);
72 + }),
73 + );
74 + });
75 +
76 + describe("workspace files", () => {
77 + it.effect("lists tracked and untracked non-ignored files", () =>
78 + Effect.gen(function* () {
79 + const cwd = yield* makeTmpDir();
80 + const driver = yield* VcsDriver;
81 +
82 + yield* input.fixture.createRepo(cwd);
83 + yield* input.fixture.writeFile(cwd, "tracked.ts", "export const tracked = true;\n");
84 + if (input.fixture.trackFile && input.fixture.commit) {
85 + yield* input.fixture.trackFile(cwd, "tracked.ts");
86 + yield* input.fixture.commit(cwd, "Track file");
87 + }
88 + yield* input.fixture.writeFile(cwd, "untracked.ts", "export const untracked = true;\n");
89 +
90 + const result = yield* driver.listWorkspaceFiles(cwd);
91 +
92 + assert.include(result.paths, "tracked.ts");
93 + assert.include(result.paths, "untracked.ts");
94 + assert.equal(result.truncated, false);
95 + assert.equal(result.freshness.source, "live-local");
96 + assert.isTrue(DateTime.isDateTime(result.freshness.observedAt));
97 + assert.isTrue(Option.isNone(result.freshness.expiresAt));
98 + }),
99 + );
100 +
101 + it.effect("excludes ignored files from workspace listing", () =>
102 + Effect.gen(function* () {
103 + const cwd = yield* makeTmpDir();
104 + const driver = yield* VcsDriver;
105 +
106 + yield* input.fixture.createRepo(cwd);
107 + yield* input.fixture.ignorePath(cwd, "*.log");
108 + yield* input.fixture.writeFile(cwd, "included.ts", "export const included = true;\n");
109 + yield* input.fixture.writeFile(cwd, "debug.log", "ignore me\n");
110 + yield* input.fixture.writeFile(cwd, "nested/error.log", "ignore me too\n");
111 +
112 + const result = yield* driver.listWorkspaceFiles(cwd);
113 +
114 + assert.include(result.paths, "included.ts");
115 + assert.notInclude(result.paths, "debug.log");
116 + assert.notInclude(result.paths, "nested/error.log");
117 + }),
118 + );
119 + });
120 +
121 + describe("ignored path filtering", () => {
122 + it.effect("filters ignored paths", () =>
123 + Effect.gen(function* () {
124 + const cwd = yield* makeTmpDir();
125 + const driver = yield* VcsDriver;
126 +
127 + yield* input.fixture.createRepo(cwd);
128 + yield* input.fixture.ignorePath(cwd, "*.log");
129 +
130 + const result = yield* driver.filterIgnoredPaths(cwd, [
131 + "keep.ts",
132 + "debug.log",
133 + "nested/error.log",
134 + ]);
135 +
136 + assert.deepStrictEqual(result, ["keep.ts"]);
137 + }),
138 + );
139 +
140 + it.effect("returns empty input unchanged", () =>
141 + Effect.gen(function* () {
142 + const cwd = yield* makeTmpDir();
143 + const driver = yield* VcsDriver;
144 +
145 + yield* input.fixture.createRepo(cwd);
146 +
147 + assert.deepStrictEqual(yield* driver.filterIgnoredPaths(cwd, []), []);
148 + }),
149 + );
150 + });
151 + });
152 +}
apps/server/src/workspace/Layers/WorkspaceEntries.test.ts
+8 -7
@@ -5,8 +5,8 @@ import { it, afterEach, describe, expect, vi } from "@effect/vitest";
5 import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
6
7 import { ServerConfig } from "../../config.ts";
8 -import { GitCoreLive } from "../../git/Layers/GitCore.ts";
9 -import { GitCore } from "../../git/Services/GitCore.ts";
8 +import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts";
9 +import * as VcsProcess from "../../vcs/VcsProcess.ts";
10 import { WorkspaceEntries } from "../Services/WorkspaceEntries.ts";
11 import { WorkspaceEntriesLive } from "./WorkspaceEntries.ts";
12 import { WorkspacePathsLive } from "./WorkspacePaths.ts";
@@ -14,7 +14,8 @@ import { WorkspacePathsLive } from "./WorkspacePaths.ts";
14 const TestLayer = Layer.empty.pipe(
15 Layer.provideMerge(WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive))),
16 Layer.provideMerge(WorkspacePathsLive),
17 - Layer.provideMerge(GitCoreLive),
17 + Layer.provideMerge(VcsProcess.layer),
18 + Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
19 Layer.provide(
20 ServerConfig.layerTest(process.cwd(), {
21 prefix: "t3-workspace-entries-test-",
@@ -25,12 +26,11 @@ const TestLayer = Layer.empty.pipe(
26
27 const makeTempDir = Effect.fn(function* (opts?: { prefix?: string; git?: boolean }) {
28 const fileSystem = yield* FileSystem.FileSystem;
28 - const gitCore = yield* GitCore;
29 const dir = yield* fileSystem.makeTempDirectoryScoped({
30 prefix: opts?.prefix ?? "t3code-workspace-entries-",
31 });
32 if (opts?.git) {
33 - yield* gitCore.initRepo({ cwd: dir });
33 + yield* git(dir, ["init"]);
34 }
35 return dir;
36 });
@@ -51,9 +51,10 @@ function writeTextFile(
51
52 const git = (cwd: string, args: ReadonlyArray<string>, env?: NodeJS.ProcessEnv) =>
53 Effect.gen(function* () {
54 - const gitCore = yield* GitCore;
55 - const result = yield* gitCore.execute({
54 + const process = yield* VcsProcess.VcsProcess;
55 + const result = yield* process.run({
56 operation: "WorkspaceEntries.test.git",
57 + command: "git",
58 cwd,
59 args,
60 ...(env ? { env } : {}),
apps/server/src/workspace/Layers/WorkspaceEntries.ts
+38 -32
@@ -2,7 +2,7 @@ import * as OS from "node:os";
2 import fsPromises from "node:fs/promises";
3 import type { Dirent } from "node:fs";
4
5 -import { Cache, Duration, Effect, Exit, Layer, Option, Path } from "effect";
5 +import { Cache, DateTime, Duration, Effect, Exit, Layer, Path } from "effect";
6
7 import { type FilesystemBrowseInput, type ProjectEntry } from "@t3tools/contracts";
8 import { isExplicitRelativePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
@@ -13,7 +13,7 @@ import {
13 type RankedSearchResult,
14 } from "@t3tools/shared/searchRanking";
15
16 -import { GitCore } from "../../git/Services/GitCore.ts";
16 +import { VcsDriverRegistry } from "../../vcs/VcsDriverRegistry.ts";
17 import {
18 WorkspaceEntries,
19 WorkspaceEntriesBrowseError,
@@ -174,38 +174,39 @@ const resolveBrowseTarget = (
174
175 export const makeWorkspaceEntries = Effect.gen(function* () {
176 const path = yield* Path.Path;
177 - const gitOption = yield* Effect.serviceOption(GitCore);
177 + const vcsRegistry = yield* VcsDriverRegistry;
178 const workspacePaths = yield* WorkspacePaths;
179
180 - const isInsideGitWorkTree = (cwd: string): Effect.Effect<boolean> =>
181 - Option.match(gitOption, {
182 - onSome: (git) => git.isInsideWorkTree(cwd).pipe(Effect.catch(() => Effect.succeed(false))),
183 - onNone: () => Effect.succeed(false),
184 - });
180 + const isInsideVcsWorkTree = (cwd: string): Effect.Effect<boolean> =>
181 + vcsRegistry.detect({ cwd }).pipe(
182 + Effect.map((handle) => handle !== null),
183 + Effect.catch(() => Effect.succeed(false)),
184 + );
185
186 - const filterGitIgnoredPaths = (
186 + const filterVcsIgnoredPaths = (
187 cwd: string,
188 relativePaths: string[],
189 ): Effect.Effect<string[], never> =>
190 - Option.match(gitOption, {
191 - onSome: (git) =>
192 - git.filterIgnoredPaths(cwd, relativePaths).pipe(
193 - Effect.map((paths) => [...paths]),
194 - Effect.catch(() => Effect.succeed(relativePaths)),
195 - ),
196 - onNone: () => Effect.succeed(relativePaths),
197 - });
198 -
199 - const buildWorkspaceIndexFromGit = Effect.fn("WorkspaceEntries.buildWorkspaceIndexFromGit")(
190 + vcsRegistry.detect({ cwd }).pipe(
191 + Effect.flatMap((handle) =>
192 + handle
193 + ? handle.driver.filterIgnoredPaths(cwd, relativePaths).pipe(
194 + Effect.map((paths) => [...paths]),
195 + Effect.catch(() => Effect.succeed(relativePaths)),
196 + )
197 + : Effect.succeed(relativePaths),
198 + ),
199 + Effect.catch(() => Effect.succeed(relativePaths)),
200 + );
201 +
202 + const buildWorkspaceIndexFromVcs = Effect.fn("WorkspaceEntries.buildWorkspaceIndexFromVcs")(
203 function* (cwd: string) {
201 - if (Option.isNone(gitOption)) {
202 - return null;
203 - }
204 - if (!(yield* isInsideGitWorkTree(cwd))) {
204 + const vcs = yield* vcsRegistry.detect({ cwd }).pipe(Effect.catch(() => Effect.succeed(null)));
205 + if (!vcs) {
206 return null;
207 }
208
208 - const listedFiles = yield* gitOption.value
209 + const listedFiles = yield* vcs.driver
210 .listWorkspaceFiles(cwd)
211 .pipe(Effect.catch(() => Effect.succeed(null)));
212
@@ -216,7 +217,10 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
217 const listedPaths = [...listedFiles.paths]
218 .map((entry) => toPosixPath(entry))
219 .filter((entry) => entry.length > 0 && !isPathInIgnoredDirectory(entry));
219 - const filePaths = yield* filterGitIgnoredPaths(cwd, listedPaths);
220 + const filePaths = yield* vcs.driver.filterIgnoredPaths(cwd, listedPaths).pipe(
221 + Effect.map((paths) => [...paths]),
222 + Effect.catch(() => filterVcsIgnoredPaths(cwd, listedPaths)),
223 + );
224
225 const directorySet = new Set<string>();
226 for (const filePath of filePaths) {
@@ -248,9 +252,10 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
252 )
253 .map(toSearchableWorkspaceEntry);
254
255 + const now = yield* DateTime.now;
256 const entries = [...directoryEntries, ...fileEntries];
257 return {
253 - scannedAt: Date.now(),
258 + scannedAt: now.epochMilliseconds,
259 entries: entries.slice(0, WORKSPACE_INDEX_MAX_ENTRIES),
260 truncated: listedFiles.truncated || entries.length > WORKSPACE_INDEX_MAX_ENTRIES,
261 };
@@ -288,7 +293,7 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
293 const buildWorkspaceIndexFromFilesystem = Effect.fn(
294 "WorkspaceEntries.buildWorkspaceIndexFromFilesystem",
295 )(function* (cwd: string): Effect.fn.Return<WorkspaceIndex, WorkspaceEntriesError> {
291 - const shouldFilterWithGitIgnore = yield* isInsideGitWorkTree(cwd);
296 + const shouldFilterWithGitIgnore = yield* isInsideVcsWorkTree(cwd);
297
298 let pendingDirectories: string[] = [""];
299 const entries: SearchableWorkspaceEntry[] = [];
@@ -336,7 +341,7 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
341 candidateEntries.map((entry) => entry.relativePath),
342 );
343 const allowedPathSet = shouldFilterWithGitIgnore
339 - ? new Set(yield* filterGitIgnoredPaths(cwd, candidatePaths))
344 + ? new Set(yield* filterVcsIgnoredPaths(cwd, candidatePaths))
345 : null;
346
347 for (const candidateEntries of candidateEntriesByDirectory) {
@@ -368,8 +373,9 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
373 }
374 }
375
376 + const now = yield* DateTime.now;
377 return {
372 - scannedAt: Date.now(),
378 + scannedAt: now.epochMilliseconds,
379 entries,
380 truncated,
381 };
@@ -378,9 +384,9 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
384 const buildWorkspaceIndex = Effect.fn("WorkspaceEntries.buildWorkspaceIndex")(function* (
385 cwd: string,
386 ): Effect.fn.Return<WorkspaceIndex, WorkspaceEntriesError> {
381 - const gitIndexed = yield* buildWorkspaceIndexFromGit(cwd);
382 - if (gitIndexed) {
383 - return gitIndexed;
387 + const vcsIndexed = yield* buildWorkspaceIndexFromVcs(cwd);
388 + if (vcsIndexed) {
389 + return vcsIndexed;
390 }
391 return yield* buildWorkspaceIndexFromFilesystem(cwd);
392 });
apps/server/src/workspace/Layers/WorkspaceFileSystem.test.ts
+3 -2
@@ -3,7 +3,8 @@ import { it, describe, expect } from "@effect/vitest";
3 import { Effect, FileSystem, Layer, Path } from "effect";
4
5 import { ServerConfig } from "../../config.ts";
6 -import { GitCoreLive } from "../../git/Layers/GitCore.ts";
6 +import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts";
7 +import * as VcsProcess from "../../vcs/VcsProcess.ts";
8 import { WorkspaceEntries } from "../Services/WorkspaceEntries.ts";
9 import { WorkspaceFileSystem } from "../Services/WorkspaceFileSystem.ts";
10 import { WorkspaceEntriesLive } from "./WorkspaceEntries.ts";
@@ -19,7 +20,7 @@ const TestLayer = Layer.empty.pipe(
20 Layer.provideMerge(ProjectLayer),
21 Layer.provideMerge(WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive))),
22 Layer.provideMerge(WorkspacePathsLive),
22 - Layer.provideMerge(GitCoreLive),
23 + Layer.provideMerge(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))),
24 Layer.provide(
25 ServerConfig.layerTest(process.cwd(), {
26 prefix: "t3-workspace-files-test-",
apps/server/src/ws.ts
+74 -54
@@ -29,9 +29,6 @@ import { RpcSerialization, RpcServer } from "effect/unstable/rpc";
29
30 import { CheckpointDiffQuery } from "./checkpointing/Services/CheckpointDiffQuery.ts";
31 import { ServerConfig } from "./config.ts";
32 -import { GitCore } from "./git/Services/GitCore.ts";
33 -import { GitManager } from "./git/Services/GitManager.ts";
34 -import { GitStatusBroadcaster } from "./git/Services/GitStatusBroadcaster.ts";
32 import { Keybindings } from "./keybindings.ts";
33 import { Open, resolveAvailableEditors } from "./open.ts";
34 import { normalizeDispatchCommand } from "./orchestration/Normalizer.ts";
@@ -50,10 +47,15 @@ import { TerminalManager } from "./terminal/Services/Manager.ts";
47 import { WorkspaceEntries } from "./workspace/Services/WorkspaceEntries.ts";
48 import { WorkspaceFileSystem } from "./workspace/Services/WorkspaceFileSystem.ts";
49 import { WorkspacePathOutsideRootError } from "./workspace/Services/WorkspacePaths.ts";
50 +import { VcsStatusBroadcaster } from "./vcs/VcsStatusBroadcaster.ts";
51 +import { VcsProvisioningService } from "./vcs/VcsProvisioningService.ts";
52 +import { GitWorkflowService } from "./git/GitWorkflowService.ts";
53 import { ProjectSetupScriptRunner } from "./project/Services/ProjectSetupScriptRunner.ts";
54 import { RepositoryIdentityResolver } from "./project/Services/RepositoryIdentityResolver.ts";
55 import { ServerEnvironment } from "./environment/Services/ServerEnvironment.ts";
56 import { ServerAuth } from "./auth/Services/ServerAuth.ts";
57 +import * as SourceControlDiscoveryLayer from "./sourceControl/SourceControlDiscovery.ts";
58 +import * as VcsProcess from "./vcs/VcsProcess.ts";
59 import {
60 BootstrapCredentialService,
61 type BootstrapCredentialChange,
@@ -136,9 +138,9 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
138 const checkpointDiffQuery = yield* CheckpointDiffQuery;
139 const keybindings = yield* Keybindings;
140 const open = yield* Open;
139 - const gitManager = yield* GitManager;
140 - const git = yield* GitCore;
141 - const gitStatusBroadcaster = yield* GitStatusBroadcaster;
141 + const gitWorkflow = yield* GitWorkflowService;
142 + const vcsProvisioning = yield* VcsProvisioningService;
143 + const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
144 const terminalManager = yield* TerminalManager;
145 const providerRegistry = yield* ProviderRegistry;
146 const config = yield* ServerConfig;
@@ -151,6 +153,7 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
153 const repositoryIdentityResolver = yield* RepositoryIdentityResolver;
154 const serverEnvironment = yield* ServerEnvironment;
155 const serverAuth = yield* ServerAuth;
156 + const sourceControlDiscovery = yield* SourceControlDiscoveryLayer.SourceControlDiscovery;
157 const bootstrapCredentials = yield* BootstrapCredentialService;
158 const sessions = yield* SessionCredentialService;
159 const serverCommandId = (tag: string) =>
@@ -453,10 +456,10 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
456 }
457
458 if (bootstrap?.prepareWorktree) {
456 - const worktree = yield* git.createWorktree({
459 + const worktree = yield* gitWorkflow.createWorktree({
460 cwd: bootstrap.prepareWorktree.projectCwd,
458 - branch: bootstrap.prepareWorktree.baseBranch,
459 - newBranch: bootstrap.prepareWorktree.branch,
461 + refName: bootstrap.prepareWorktree.baseBranch,
462 + newRefName: bootstrap.prepareWorktree.branch,
463 path: null,
464 });
465 targetWorktreePath = worktree.worktree.path;
@@ -464,7 +467,7 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
467 type: "thread.meta.update",
468 commandId: serverCommandId("bootstrap-thread-meta-update"),
469 threadId: command.threadId,
467 - branch: worktree.worktree.branch,
470 + branch: worktree.worktree.refName,
471 worktreePath: targetWorktreePath,
472 });
473 yield* refreshGitStatus(targetWorktreePath);
@@ -540,7 +543,7 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
543 });
544
545 const refreshGitStatus = (cwd: string) =>
543 - gitStatusBroadcaster
546 + vcsStatusBroadcaster
547 .refreshStatus(cwd)
548 .pipe(Effect.ignoreCause({ log: true }), Effect.forkDetach, Effect.asVoid);
549
@@ -783,6 +786,14 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
786 "rpc.aggregate": "server",
787 },
788 ),
789 + [WS_METHODS.serverDiscoverSourceControl]: (_input) =>
790 + observeRpcEffect(
791 + WS_METHODS.serverDiscoverSourceControl,
792 + sourceControlDiscovery.discover,
793 + {
794 + "rpc.aggregate": "server",
795 + },
796 + ),
797 [WS_METHODS.projectsSearchEntries]: (input) =>
798 observeRpcEffect(
799 WS_METHODS.projectsSearchEntries,
@@ -831,26 +842,26 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
842 ),
843 { "rpc.aggregate": "workspace" },
844 ),
834 - [WS_METHODS.subscribeGitStatus]: (input) =>
845 + [WS_METHODS.subscribeVcsStatus]: (input) =>
846 observeRpcStream(
836 - WS_METHODS.subscribeGitStatus,
837 - gitStatusBroadcaster.streamStatus(input),
847 + WS_METHODS.subscribeVcsStatus,
848 + vcsStatusBroadcaster.streamStatus(input),
849 {
839 - "rpc.aggregate": "git",
850 + "rpc.aggregate": "vcs",
851 },
852 ),
842 - [WS_METHODS.gitRefreshStatus]: (input) =>
853 + [WS_METHODS.vcsRefreshStatus]: (input) =>
854 observeRpcEffect(
844 - WS_METHODS.gitRefreshStatus,
845 - gitStatusBroadcaster.refreshStatus(input.cwd),
855 + WS_METHODS.vcsRefreshStatus,
856 + vcsStatusBroadcaster.refreshStatus(input.cwd),
857 {
847 - "rpc.aggregate": "git",
858 + "rpc.aggregate": "vcs",
859 },
860 ),
850 - [WS_METHODS.gitPull]: (input) =>
861 + [WS_METHODS.vcsPull]: (input) =>
862 observeRpcEffect(
852 - WS_METHODS.gitPull,
853 - git.pullCurrentBranch(input.cwd).pipe(
863 + WS_METHODS.vcsPull,
864 + gitWorkflow.pullCurrentBranch(input.cwd).pipe(
865 Effect.matchCauseEffect({
866 onFailure: (cause) => Effect.failCause(cause),
867 onSuccess: (result) =>
@@ -863,7 +874,7 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
874 observeRpcStream(
875 WS_METHODS.gitRunStackedAction,
876 Stream.callback<GitActionProgressEvent, GitManagerServiceError>((queue) =>
866 - gitManager
877 + gitWorkflow
878 .runStackedAction(input, {
879 actionId: input.actionId,
880 progressReporter: {
@@ -880,55 +891,59 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
891 }),
892 ),
893 ),
883 - { "rpc.aggregate": "git" },
894 + { "rpc.aggregate": "vcs" },
895 ),
896 [WS_METHODS.gitResolvePullRequest]: (input) =>
886 - observeRpcEffect(WS_METHODS.gitResolvePullRequest, gitManager.resolvePullRequest(input), {
887 - "rpc.aggregate": "git",
888 - }),
897 + observeRpcEffect(
898 + WS_METHODS.gitResolvePullRequest,
899 + gitWorkflow.resolvePullRequest(input),
900 + {
901 + "rpc.aggregate": "git",
902 + },
903 + ),
904 [WS_METHODS.gitPreparePullRequestThread]: (input) =>
905 observeRpcEffect(
906 WS_METHODS.gitPreparePullRequestThread,
892 - gitManager
907 + gitWorkflow
908 .preparePullRequestThread(input)
909 .pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
910 { "rpc.aggregate": "git" },
911 ),
897 - [WS_METHODS.gitListBranches]: (input) =>
898 - observeRpcEffect(WS_METHODS.gitListBranches, git.listBranches(input), {
899 - "rpc.aggregate": "git",
912 + [WS_METHODS.vcsListRefs]: (input) =>
913 + observeRpcEffect(WS_METHODS.vcsListRefs, gitWorkflow.listRefs(input), {
914 + "rpc.aggregate": "vcs",
915 }),
901 - [WS_METHODS.gitCreateWorktree]: (input) =>
916 + [WS_METHODS.vcsCreateWorktree]: (input) =>
917 observeRpcEffect(
903 - WS_METHODS.gitCreateWorktree,
904 - git.createWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
905 - { "rpc.aggregate": "git" },
918 + WS_METHODS.vcsCreateWorktree,
919 + gitWorkflow.createWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
920 + { "rpc.aggregate": "vcs" },
921 ),
907 - [WS_METHODS.gitRemoveWorktree]: (input) =>
922 + [WS_METHODS.vcsRemoveWorktree]: (input) =>
923 observeRpcEffect(
909 - WS_METHODS.gitRemoveWorktree,
910 - git.removeWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
911 - { "rpc.aggregate": "git" },
924 + WS_METHODS.vcsRemoveWorktree,
925 + gitWorkflow.removeWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
926 + { "rpc.aggregate": "vcs" },
927 ),
913 - [WS_METHODS.gitCreateBranch]: (input) =>
928 + [WS_METHODS.vcsCreateRef]: (input) =>
929 observeRpcEffect(
915 - WS_METHODS.gitCreateBranch,
916 - git.createBranch(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
917 - { "rpc.aggregate": "git" },
930 + WS_METHODS.vcsCreateRef,
931 + gitWorkflow.createRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
932 + { "rpc.aggregate": "vcs" },
933 ),
919 - [WS_METHODS.gitCheckout]: (input) =>
934 + [WS_METHODS.vcsSwitchRef]: (input) =>
935 observeRpcEffect(
921 - WS_METHODS.gitCheckout,
922 - Effect.scoped(git.checkoutBranch(input)).pipe(
923 - Effect.tap(() => refreshGitStatus(input.cwd)),
924 - ),
925 - { "rpc.aggregate": "git" },
936 + WS_METHODS.vcsSwitchRef,
937 + gitWorkflow.switchRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
938 + { "rpc.aggregate": "vcs" },
939 ),
927 - [WS_METHODS.gitInit]: (input) =>
940 + [WS_METHODS.vcsInit]: (input) =>
941 observeRpcEffect(
929 - WS_METHODS.gitInit,
930 - git.initRepo(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
931 - { "rpc.aggregate": "git" },
942 + WS_METHODS.vcsInit,
943 + vcsProvisioning
944 + .initRepository(input)
945 + .pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
946 + { "rpc.aggregate": "vcs" },
947 ),
948 [WS_METHODS.terminalOpen]: (input) =>
949 observeRpcEffect(WS_METHODS.terminalOpen, terminalManager.open(input), {
@@ -1084,7 +1099,12 @@ export const websocketRpcRouteLayer = Layer.unwrap(
1099 },
1100 }).pipe(
1101 Effect.provide(
1087 - makeWsRpcLayer(session.sessionId).pipe(Layer.provideMerge(RpcSerialization.layerJson)),
1102 + makeWsRpcLayer(session.sessionId).pipe(
1103 + Layer.provideMerge(RpcSerialization.layerJson),
1104 + Layer.provide(
1105 + SourceControlDiscoveryLayer.layer.pipe(Layer.provide(VcsProcess.layer)),
1106 + ),
1107 + ),
1108 ),
1109 );
1110 return yield* Effect.acquireUseRelease(
apps/web/src/components/BranchToolbar.logic.test.ts
+26 -26
@@ -1,4 +1,4 @@
1 -import { EnvironmentId, type GitBranch } from "@t3tools/contracts";
1 +import { EnvironmentId, type VcsRef } from "@t3tools/contracts";
2 import { describe, expect, it } from "vitest";
3 import {
4 dedupeRemoteBranchesWithLocalMatches,
@@ -28,7 +28,7 @@ describe("resolveDraftEnvModeAfterBranchChange", () => {
28 ).toBe("local");
29 });
30
31 - it("keeps new-worktree mode when selecting a base branch before worktree creation", () => {
31 + it("keeps new-worktree mode when selecting a base ref before worktree creation", () => {
32 expect(
33 resolveDraftEnvModeAfterBranchChange({
34 nextWorktreePath: null,
@@ -38,7 +38,7 @@ describe("resolveDraftEnvModeAfterBranchChange", () => {
38 ).toBe("worktree");
39 });
40
41 - it("uses worktree mode when selecting a branch already attached to a worktree", () => {
41 + it("uses worktree mode when selecting a ref already attached to a worktree", () => {
42 expect(
43 resolveDraftEnvModeAfterBranchChange({
44 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
@@ -50,7 +50,7 @@ describe("resolveDraftEnvModeAfterBranchChange", () => {
50 });
51
52 describe("resolveBranchToolbarValue", () => {
53 - it("defaults new-worktree mode to current git branch when no explicit base branch is set", () => {
53 + it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {
54 expect(
55 resolveBranchToolbarValue({
56 envMode: "worktree",
@@ -61,7 +61,7 @@ describe("resolveBranchToolbarValue", () => {
61 ).toBe("main");
62 });
63
64 - it("keeps an explicitly selected worktree base branch", () => {
64 + it("keeps an explicitly selected worktree base ref", () => {
65 expect(
66 resolveBranchToolbarValue({
67 envMode: "worktree",
@@ -72,7 +72,7 @@ describe("resolveBranchToolbarValue", () => {
72 ).toBe("feature/base");
73 });
74
75 - it("shows the actual checked-out branch when not selecting a new worktree base", () => {
75 + it("shows the actual checked-out ref when not selecting a new worktree base", () => {
76 expect(
77 resolveBranchToolbarValue({
78 envMode: "local",
@@ -186,8 +186,8 @@ describe("deriveLocalBranchNameFromRemoteRef", () => {
186 });
187
188 describe("dedupeRemoteBranchesWithLocalMatches", () => {
189 - it("hides remote refs when the matching local branch exists", () => {
190 - const input: GitBranch[] = [
189 + it("hides remote refs when the matching local ref exists", () => {
190 + const input: VcsRef[] = [
191 {
192 name: "feature/demo",
193 current: false,
@@ -212,14 +212,14 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => {
212 },
213 ];
214
215 - expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
215 + expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
216 "feature/demo",
217 "origin/feature/remote-only",
218 ]);
219 });
220
221 it("keeps all entries when no local match exists for a remote ref", () => {
222 - const input: GitBranch[] = [
222 + const input: VcsRef[] = [
223 {
224 name: "feature/local",
225 current: false,
@@ -236,14 +236,14 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => {
236 },
237 ];
238
239 - expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
239 + expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
240 "feature/local",
241 "origin/feature/remote-only",
242 ]);
243 });
244
245 - it("keeps non-origin remote refs visible even when a matching local branch exists", () => {
246 - const input: GitBranch[] = [
245 + it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
246 + const input: VcsRef[] = [
247 {
248 name: "feature/demo",
249 current: false,
@@ -260,14 +260,14 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => {
260 },
261 ];
262
263 - expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
263 + expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
264 "feature/demo",
265 "my-org/upstream/feature/demo",
266 ]);
267 });
268
269 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
270 - const input: GitBranch[] = [
270 + const input: VcsRef[] = [
271 {
272 name: "upstream/feature",
273 current: false,
@@ -284,7 +284,7 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => {
284 },
285 ];
286
287 - expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
287 + expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
288 "upstream/feature",
289 "my-org/upstream/feature",
290 ]);
@@ -292,12 +292,12 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => {
292 });
293
294 describe("resolveBranchSelectionTarget", () => {
295 - it("reuses an existing secondary worktree for the selected branch", () => {
295 + it("reuses an existing secondary worktree for the selected ref", () => {
296 expect(
297 resolveBranchSelectionTarget({
298 activeProjectCwd: "/repo",
299 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
300 - branch: {
300 + refName: {
301 isDefault: false,
302 worktreePath: "/repo/.t3/worktrees/feature-b",
303 },
@@ -309,12 +309,12 @@ describe("resolveBranchSelectionTarget", () => {
309 });
310 });
311
312 - it("switches back to the main repo when the branch already lives there", () => {
312 + it("switches back to the main repo when the ref already lives there", () => {
313 expect(
314 resolveBranchSelectionTarget({
315 activeProjectCwd: "/repo",
316 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
317 - branch: {
317 + refName: {
318 isDefault: true,
319 worktreePath: "/repo",
320 },
@@ -326,12 +326,12 @@ describe("resolveBranchSelectionTarget", () => {
326 });
327 });
328
329 - it("checks out the default branch in the main repo when leaving a secondary worktree", () => {
329 + it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
330 expect(
331 resolveBranchSelectionTarget({
332 activeProjectCwd: "/repo",
333 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
334 - branch: {
334 + refName: {
335 isDefault: true,
336 worktreePath: null,
337 },
@@ -343,12 +343,12 @@ describe("resolveBranchSelectionTarget", () => {
343 });
344 });
345
346 - it("keeps checkout in the current worktree for non-default branches", () => {
346 + it("keeps checkout in the current worktree for non-default refs", () => {
347 expect(
348 resolveBranchSelectionTarget({
349 activeProjectCwd: "/repo",
350 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
351 - branch: {
351 + refName: {
352 isDefault: false,
353 worktreePath: null,
354 },
@@ -373,7 +373,7 @@ describe("shouldIncludeBranchPickerItem", () => {
373 ).toBe(true);
374 });
375
376 - it("keeps the synthetic create-branch item visible for arbitrary branch input", () => {
376 + it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
377 expect(
378 shouldIncludeBranchPickerItem({
379 itemValue: "__create_new_branch__:feature/demo",
@@ -384,7 +384,7 @@ describe("shouldIncludeBranchPickerItem", () => {
384 ).toBe(true);
385 });
386
387 - it("still filters ordinary branch items by query text", () => {
387 + it("still filters ordinary ref items by query text", () => {
388 expect(
389 shouldIncludeBranchPickerItem({
390 itemValue: "main",
apps/web/src/components/BranchToolbar.logic.ts
+7 -7
@@ -1,4 +1,4 @@
1 -import type { EnvironmentId, GitBranch, ProjectId } from "@t3tools/contracts";
1 +import type { EnvironmentId, VcsRef, ProjectId } from "@t3tools/contracts";
2 import { Schema } from "effect";
3 export {
4 dedupeRemoteBranchesWithLocalMatches,
@@ -100,24 +100,24 @@ export function resolveBranchToolbarValue(input: {
100 export function resolveBranchSelectionTarget(input: {
101 activeProjectCwd: string;
102 activeWorktreePath: string | null;
103 - branch: Pick<GitBranch, "isDefault" | "worktreePath">;
103 + refName: Pick<VcsRef, "isDefault" | "worktreePath">;
104 }): {
105 checkoutCwd: string;
106 nextWorktreePath: string | null;
107 reuseExistingWorktree: boolean;
108 } {
109 - const { activeProjectCwd, activeWorktreePath, branch } = input;
109 + const { activeProjectCwd, activeWorktreePath, refName } = input;
110
111 - if (branch.worktreePath) {
111 + if (refName.worktreePath) {
112 return {
113 - checkoutCwd: branch.worktreePath,
114 - nextWorktreePath: branch.worktreePath === activeProjectCwd ? null : branch.worktreePath,
113 + checkoutCwd: refName.worktreePath,
114 + nextWorktreePath: refName.worktreePath === activeProjectCwd ? null : refName.worktreePath,
115 reuseExistingWorktree: true,
116 };
117 }
118
119 const nextWorktreePath =
120 - activeWorktreePath !== null && branch.isDefault ? null : activeWorktreePath;
120 + activeWorktreePath !== null && refName.isDefault ? null : activeWorktreePath;
121
122 return {
123 checkoutCwd: nextWorktreePath ?? activeProjectCwd,
apps/web/src/components/BranchToolbarBranchSelector.tsx
+61 -50
@@ -1,5 +1,5 @@
1 import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime";
2 -import type { EnvironmentId, GitBranch, ThreadId } from "@t3tools/contracts";
2 +import type { EnvironmentId, VcsRef, ThreadId } from "@t3tools/contracts";
3 import { useInfiniteQuery, useQueryClient } from "@tanstack/react-query";
4 import { LegendList, type LegendListRef } from "@legendapp/list/react";
5 import { ChevronDownIcon } from "lucide-react";
@@ -21,6 +21,7 @@ import { useGitStatus } from "../lib/gitStatusState";
21 import { newCommandId } from "../lib/utils";
22 import { cn } from "../lib/utils";
23 import { parsePullRequestReference } from "../pullRequestReference";
24 +import { getSourceControlPresentation } from "../sourceControlPresentation";
25 import { useStore } from "../store";
26 import { createProjectSelectorByRef, createThreadSelectorByRef } from "../storeSelectors";
27 import {
@@ -53,7 +54,7 @@ interface BranchToolbarBranchSelectorProps {
54 envLocked: boolean;
55 effectiveEnvModeOverride?: "local" | "worktree";
56 activeThreadBranchOverride?: string | null;
56 - onActiveThreadBranchOverrideChange?: (branch: string | null) => void;
57 + onActiveThreadBranchOverrideChange?: (refName: string | null) => void;
58 onCheckoutPullRequestRequest?: (reference: string) => void;
59 onComposerFocusRequest?: () => void;
60 }
@@ -69,7 +70,7 @@ function getBranchTriggerLabel(input: {
70 }): string {
71 const { activeWorktreePath, effectiveEnvMode, resolvedActiveBranch } = input;
72 if (!resolvedActiveBranch) {
72 - return "Select branch";
73 + return "Select ref";
74 }
75 if (effectiveEnvMode === "worktree" && !activeWorktreePath) {
76 return `From ${resolvedActiveBranch}`;
@@ -193,7 +194,7 @@ export function BranchToolbarBranchSelector({
194 );
195
196 // ---------------------------------------------------------------------------
196 - // Git branch queries
197 + // Git ref queries
198 // ---------------------------------------------------------------------------
199 const queryClient = useQueryClient();
200 const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);
@@ -224,22 +225,27 @@ export function BranchToolbarBranchSelector({
225 query: deferredTrimmedBranchQuery,
226 }),
227 );
227 - const branches = useMemo(
228 - () => branchesSearchData?.pages.flatMap((page) => page.branches) ?? [],
228 + const refs = useMemo(
229 + () => branchesSearchData?.pages.flatMap((page) => page.refs) ?? [],
230 [branchesSearchData?.pages],
231 );
232 const currentGitBranch =
232 - branchStatusQuery.data?.branch ?? branches.find((branch) => branch.current)?.name ?? null;
233 + branchStatusQuery.data?.refName ?? refs.find((refName) => refName.current)?.name ?? null;
234 + const sourceControlPresentation = useMemo(
235 + () => getSourceControlPresentation(branchStatusQuery.data?.sourceControlProvider),
236 + [branchStatusQuery.data?.sourceControlProvider],
237 + );
238 + const SourceControlIcon = sourceControlPresentation.Icon;
239 const canonicalActiveBranch = resolveBranchToolbarValue({
240 envMode: effectiveEnvMode,
241 activeWorktreePath,
242 activeThreadBranch,
243 currentGitBranch,
244 });
239 - const branchNames = useMemo(() => branches.map((branch) => branch.name), [branches]);
245 + const branchNames = useMemo(() => refs.map((refName) => refName.name), [refs]);
246 const branchByName = useMemo(
241 - () => new Map(branches.map((branch) => [branch.name, branch] as const)),
242 - [branches],
247 + () => new Map(refs.map((refName) => [refName.name, refName] as const)),
248 + [refs],
249 );
250 const normalizedDeferredBranchQuery = deferredTrimmedBranchQuery.toLowerCase();
251 const prReference = parsePullRequestReference(trimmedBranchQuery);
@@ -289,11 +295,11 @@ export function BranchToolbarBranchSelector({
295 const shouldVirtualizeBranchList = filteredBranchPickerItems.length > 40;
296 const totalBranchCount = branchesSearchData?.pages[0]?.totalCount ?? 0;
297 const branchStatusText = isBranchesSearchPending
292 - ? "Loading branches..."
298 + ? "Loading refs..."
299 : isFetchingNextPage
294 - ? "Loading more branches..."
300 + ? "Loading more refs..."
301 : hasNextPage
296 - ? `Showing ${branches.length} of ${totalBranchCount} branches`
302 + ? `Showing ${refs.length} of ${totalBranchCount} refs`
303 : null;
304
305 // ---------------------------------------------------------------------------
@@ -303,17 +309,17 @@ export function BranchToolbarBranchSelector({
309 startBranchActionTransition(async () => {
310 await action().catch(() => undefined);
311 await queryClient
306 - .invalidateQueries({ queryKey: gitQueryKeys.branches(environmentId, branchCwd) })
312 + .invalidateQueries({ queryKey: gitQueryKeys.refs(environmentId, branchCwd) })
313 .catch(() => undefined);
314 });
315 };
316
311 - const selectBranch = (branch: GitBranch) => {
317 + const selectBranch = (refName: VcsRef) => {
318 const api = readEnvironmentApi(environmentId);
319 if (!api || !branchCwd || !activeProjectCwd || isBranchActionPending) return;
320
321 if (isSelectingWorktreeBase) {
316 - setThreadBranch(branch.name, null);
322 + setThreadBranch(refName.name, null);
323 setIsBranchMenuOpen(false);
324 onComposerFocusRequest?.();
325 return;
@@ -322,19 +328,19 @@ export function BranchToolbarBranchSelector({
328 const selectionTarget = resolveBranchSelectionTarget({
329 activeProjectCwd,
330 activeWorktreePath,
325 - branch,
331 + refName,
332 });
333
334 if (selectionTarget.reuseExistingWorktree) {
329 - setThreadBranch(branch.name, selectionTarget.nextWorktreePath);
335 + setThreadBranch(refName.name, selectionTarget.nextWorktreePath);
336 setIsBranchMenuOpen(false);
337 onComposerFocusRequest?.();
338 return;
339 }
340
335 - const selectedBranchName = branch.isRemote
336 - ? deriveLocalBranchNameFromRemoteRef(branch.name)
337 - : branch.name;
341 + const selectedBranchName = refName.isRemote
342 + ? deriveLocalBranchNameFromRemoteRef(refName.name)
343 + : refName.name;
344
345 setIsBranchMenuOpen(false);
346 onComposerFocusRequest?.();
@@ -343,12 +349,12 @@ export function BranchToolbarBranchSelector({
349 const previousBranch = resolvedActiveBranch;
350 setOptimisticBranch(selectedBranchName);
351 try {
346 - const checkoutResult = await api.git.checkout({
352 + const checkoutResult = await api.vcs.switchRef({
353 cwd: selectionTarget.checkoutCwd,
348 - branch: branch.name,
354 + refName: refName.name,
355 });
350 - const nextBranchName = branch.isRemote
351 - ? (checkoutResult.branch ?? selectedBranchName)
356 + const nextBranchName = refName.isRemote
357 + ? (checkoutResult.refName ?? selectedBranchName)
358 : selectedBranchName;
359 setOptimisticBranch(nextBranchName);
360 setThreadBranch(nextBranchName, selectionTarget.nextWorktreePath);
@@ -357,7 +363,7 @@ export function BranchToolbarBranchSelector({
363 toastManager.add(
364 stackedThreadToast({
365 type: "error",
360 - title: "Failed to checkout branch.",
366 + title: "Failed to switch ref.",
367 description: toBranchActionErrorMessage(error),
368 }),
369 );
@@ -365,7 +371,7 @@ export function BranchToolbarBranchSelector({
371 });
372 };
373
368 - const createBranch = (rawName: string) => {
374 + const createRef = (rawName: string) => {
375 const name = rawName.trim();
376 const api = readEnvironmentApi(environmentId);
377 if (!api || !branchCwd || !name || isBranchActionPending) return;
@@ -377,19 +383,19 @@ export function BranchToolbarBranchSelector({
383 const previousBranch = resolvedActiveBranch;
384 setOptimisticBranch(name);
385 try {
380 - const createBranchResult = await api.git.createBranch({
386 + const createBranchResult = await api.vcs.createRef({
387 cwd: branchCwd,
382 - branch: name,
383 - checkout: true,
388 + refName: name,
389 + switchRef: true,
390 });
385 - setOptimisticBranch(createBranchResult.branch);
386 - setThreadBranch(createBranchResult.branch, activeWorktreePath);
391 + setOptimisticBranch(createBranchResult.refName);
392 + setThreadBranch(createBranchResult.refName, activeWorktreePath);
393 } catch (error) {
394 setOptimisticBranch(previousBranch);
395 toastManager.add(
396 stackedThreadToast({
397 type: "error",
392 - title: "Failed to create and checkout branch.",
398 + title: "Failed to create and switch ref.",
399 description: toBranchActionErrorMessage(error),
400 }),
401 );
@@ -420,7 +426,7 @@ export function BranchToolbarBranchSelector({
426 return;
427 }
428 void queryClient.invalidateQueries({
423 - queryKey: gitQueryKeys.branches(environmentId, branchCwd),
429 + queryKey: gitQueryKeys.refs(environmentId, branchCwd),
430 });
431 },
432 [branchCwd, environmentId, queryClient],
@@ -482,7 +488,7 @@ export function BranchToolbarBranchSelector({
488 useEffect(() => {
489 if (shouldVirtualizeBranchList) return;
490 maybeFetchNextBranchPage();
485 - }, [branches.length, maybeFetchNextBranchPage, shouldVirtualizeBranchList]);
491 + }, [refs.length, maybeFetchNextBranchPage, shouldVirtualizeBranchList]);
492
493 const triggerLabel = getBranchTriggerLabel({
494 activeWorktreePath,
@@ -508,9 +514,14 @@ export function BranchToolbarBranchSelector({
514 onCheckoutPullRequestRequest(prReference);
515 }}
516 >
511 - <div className="flex min-w-0 flex-col items-start py-1">
512 - <span className="truncate font-medium">Checkout Pull Request</span>
513 - <span className="truncate text-muted-foreground text-xs">{prReference}</span>
517 + <div className="flex min-w-0 items-center gap-2 py-1">
518 + <SourceControlIcon className="size-3.5 shrink-0 text-muted-foreground" />
519 + <span className="flex min-w-0 flex-col items-start">
520 + <span className="truncate font-medium">
521 + Checkout {sourceControlPresentation.terminology.singular}
522 + </span>
523 + <span className="truncate text-muted-foreground text-xs">{prReference}</span>
524 + </span>
525 </div>
526 </ComboboxItem>
527 );
@@ -522,25 +533,25 @@ export function BranchToolbarBranchSelector({
533 key={itemValue}
534 index={index}
535 value={itemValue}
525 - onClick={() => createBranch(trimmedBranchQuery)}
536 + onClick={() => createRef(trimmedBranchQuery)}
537 >
527 - <span className="truncate">Create new branch &quot;{trimmedBranchQuery}&quot;</span>
538 + <span className="truncate">Create new ref &quot;{trimmedBranchQuery}&quot;</span>
539 </ComboboxItem>
540 );
541 }
542
532 - const branch = branchByName.get(itemValue);
533 - if (!branch) return null;
543 + const refName = branchByName.get(itemValue);
544 + if (!refName) return null;
545
546 const hasSecondaryWorktree =
536 - branch.worktreePath && activeProjectCwd && branch.worktreePath !== activeProjectCwd;
537 - const badge = branch.current
547 + refName.worktreePath && activeProjectCwd && refName.worktreePath !== activeProjectCwd;
548 + const badge = refName.current
549 ? "current"
550 : hasSecondaryWorktree
551 ? "worktree"
541 - : branch.isRemote
552 + : refName.isRemote
553 ? "remote"
543 - : branch.isDefault
554 + : refName.isDefault
555 ? "default"
556 : null;
557 return (
@@ -549,7 +560,7 @@ export function BranchToolbarBranchSelector({
560 key={itemValue}
561 index={index}
562 value={itemValue}
552 - onClick={() => selectBranch(branch)}
563 + onClick={() => selectBranch(refName)}
564 >
565 <div className="flex w-full items-center justify-between gap-2">
566 <span className="truncate">{itemValue}</span>
@@ -581,7 +592,7 @@ export function BranchToolbarBranchSelector({
592 <ComboboxTrigger
593 render={<Button variant="ghost" size="xs" />}
594 className={cn("min-w-0 text-muted-foreground/70 hover:text-foreground/80", className)}
584 - disabled={(isBranchesSearchPending && branches.length === 0) || isBranchActionPending}
595 + disabled={(isBranchesSearchPending && refs.length === 0) || isBranchActionPending}
596 >
597 <span className="min-w-0 max-w-[240px] truncate">{triggerLabel}</span>
598 <ChevronDownIcon className="shrink-0" />
@@ -591,14 +602,14 @@ export function BranchToolbarBranchSelector({
602 <ComboboxInput
603 className="[&_input]:font-sans rounded-md"
604 inputClassName="ring-0"
594 - placeholder="Search branches..."
605 + placeholder="Search refs..."
606 showTrigger={false}
607 size="sm"
608 value={branchQuery}
609 onChange={(event) => setBranchQuery(event.target.value)}
610 />
611 </div>
601 - <ComboboxEmpty>No branches found.</ComboboxEmpty>
612 + <ComboboxEmpty>No refs found.</ComboboxEmpty>
613
614 {shouldVirtualizeBranchList ? (
615 <ComboboxListVirtualized>
apps/web/src/components/ChatView.browser.tsx
+26 -25
@@ -206,6 +206,7 @@ function createMockEnvironmentApi(input: {
206 filesystem: {
207 browse: input.browse,
208 },
209 + vcs: {} as EnvironmentApi["vcs"],
210 git: {} as EnvironmentApi["git"],
211 orchestration: {
212 dispatchCommand: input.dispatchCommand,
@@ -951,13 +952,13 @@ function resolveWsRpc(body: NormalizedWsRpcRequestBody): unknown {
952 if (tag === WS_METHODS.serverGetConfig) {
953 return fixture.serverConfig;
954 }
954 - if (tag === WS_METHODS.gitListBranches) {
955 + if (tag === WS_METHODS.vcsListRefs) {
956 return {
957 isRepo: true,
957 - hasOriginRemote: true,
958 + hasPrimaryRemote: true,
959 nextCursor: null,
960 totalCount: 1,
960 - branches: [
961 + refs: [
962 {
963 name: "main",
964 current: true,
@@ -2287,16 +2288,16 @@ describe("ChatView timeline estimator parity (full app)", () => {
2288 branchButton.click();
2289
2290 const branchInput = await waitForElement(
2290 - () => document.querySelector<HTMLInputElement>('input[placeholder="Search branches..."]'),
2291 - "Unable to find branch search input.",
2291 + () => document.querySelector<HTMLInputElement>('input[placeholder="Search refs..."]'),
2292 + "Unable to find ref search input.",
2293 );
2294 branchInput.focus();
2294 - await page.getByPlaceholder("Search branches...").fill("1359");
2295 + await page.getByPlaceholder("Search refs...").fill("1359");
2296
2297 const checkoutItem = await waitForElement(
2298 () =>
2299 Array.from(document.querySelectorAll("span")).find(
2299 - (element) => element.textContent?.trim() === "Checkout Pull Request",
2300 + (element) => element.textContent?.trim() === "Checkout pull request",
2301 ) as HTMLSpanElement | null,
2302 "Unable to find checkout pull request option.",
2303 );
@@ -2425,7 +2426,7 @@ describe("ChatView timeline estimator parity (full app)", () => {
2426 { timeout: 8_000, interval: 16 },
2427 );
2428
2428 - expect(wsRequests.some((request) => request._tag === WS_METHODS.gitCreateWorktree)).toBe(
2429 + expect(wsRequests.some((request) => request._tag === WS_METHODS.vcsCreateWorktree)).toBe(
2430 false,
2431 );
2432 expect(
@@ -2572,13 +2573,13 @@ describe("ChatView timeline estimator parity (full app)", () => {
2573 ),
2574 },
2575 resolveRpc: (body) => {
2575 - if (body._tag === WS_METHODS.gitListBranches) {
2576 + if (body._tag === WS_METHODS.vcsListRefs) {
2577 return {
2578 isRepo: true,
2578 - hasOriginRemote: true,
2579 + hasPrimaryRemote: true,
2580 nextCursor: null,
2581 totalCount: 1,
2581 - branches: [
2582 + refs: [
2583 {
2584 name: "main",
2585 current: true,
@@ -2665,13 +2666,13 @@ describe("ChatView timeline estimator parity (full app)", () => {
2666 ),
2667 },
2668 resolveRpc: (body) => {
2668 - if (body._tag === WS_METHODS.gitListBranches) {
2669 + if (body._tag === WS_METHODS.vcsListRefs) {
2670 return {
2671 isRepo: true,
2671 - hasOriginRemote: true,
2672 + hasPrimaryRemote: true,
2673 nextCursor: null,
2674 totalCount: 2,
2674 - branches: [
2675 + refs: [
2676 {
2677 name: "main",
2678 current: true,
@@ -2761,13 +2762,13 @@ describe("ChatView timeline estimator parity (full app)", () => {
2762 viewport: DEFAULT_VIEWPORT,
2763 snapshot: snapshotWithTwoThreads,
2764 resolveRpc: (body) => {
2764 - if (body._tag === WS_METHODS.gitListBranches) {
2765 + if (body._tag === WS_METHODS.vcsListRefs) {
2766 return {
2767 isRepo: true,
2767 - hasOriginRemote: true,
2768 + hasPrimaryRemote: true,
2769 nextCursor: null,
2770 totalCount: 2,
2770 - branches: [
2771 + refs: [
2772 {
2773 name: "main",
2774 current: true,
@@ -3021,13 +3022,13 @@ describe("ChatView timeline estimator parity (full app)", () => {
3022 snapshot: createDraftOnlySnapshot(),
3023 initialPath: `/draft/${activeDraftId}`,
3024 resolveRpc: (body) => {
3024 - if (body._tag === WS_METHODS.gitListBranches) {
3025 + if (body._tag === WS_METHODS.vcsListRefs) {
3026 return {
3027 isRepo: true,
3027 - hasOriginRemote: true,
3028 + hasPrimaryRemote: true,
3029 nextCursor: null,
3030 totalCount: 2,
3030 - branches: [
3031 + refs: [
3032 {
3033 name: "main",
3034 current: true,
@@ -3146,13 +3147,13 @@ describe("ChatView timeline estimator parity (full app)", () => {
3147 snapshot: createDraftOnlySnapshot(),
3148 initialPath: `/draft/${draftId}`,
3149 resolveRpc: (body) => {
3149 - if (body._tag === WS_METHODS.gitListBranches) {
3150 + if (body._tag === WS_METHODS.vcsListRefs) {
3151 return {
3152 isRepo: true,
3152 - hasOriginRemote: true,
3153 + hasPrimaryRemote: true,
3154 nextCursor: null,
3155 totalCount: branches.length,
3155 - branches,
3156 + refs: branches,
3157 };
3158 }
3159 return undefined;
@@ -3170,8 +3171,8 @@ describe("ChatView timeline estimator parity (full app)", () => {
3171 branchButton.click();
3172
3173 await waitForElement(
3173 - () => document.querySelector<HTMLInputElement>('input[placeholder="Search branches..."]'),
3174 - "Unable to find branch search input.",
3174 + () => document.querySelector<HTMLInputElement>('input[placeholder="Search refs..."]'),
3175 + "Unable to find ref search input.",
3176 );
3177
3178 const popup = await waitForElement(
apps/web/src/components/GitActionsControl.browser.tsx
+9 -1
@@ -113,7 +113,15 @@ vi.mock("~/lib/gitStatusState", () => ({
113 resetGitStatusStateForTests: () => undefined,
114 useGitStatus: vi.fn(() => ({
115 data: {
116 - branch: BRANCH_NAME,
116 + isRepo: true,
117 + sourceControlProvider: {
118 + kind: "github",
119 + name: "GitHub",
120 + baseUrl: "https://github.com",
121 + },
122 + hasPrimaryRemote: true,
123 + isDefaultRef: false,
124 + refName: BRANCH_NAME,
125 hasWorkingTreeChanges: false,
126 workingTree: { files: [], insertions: 0, deletions: 0 },
127 hasUpstream: true,
apps/web/src/components/GitActionsControl.logic.test.ts
+101 -75
@@ -1,4 +1,4 @@
1 -import type { GitStatusResult } from "@t3tools/contracts";
1 +import type { VcsStatusResult } from "@t3tools/contracts";
2 import { assert, describe, it } from "vitest";
3 import {
4 buildGitActionProgressStages,
@@ -11,12 +11,12 @@ import {
11 resolveThreadBranchUpdate,
12 } from "./GitActionsControl.logic";
13
14 -function status(overrides: Partial<GitStatusResult> = {}): GitStatusResult {
14 +function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
15 return {
16 isRepo: true,
17 - hasOriginRemote: true,
18 - isDefaultBranch: false,
19 - branch: "feature/test",
17 + hasPrimaryRemote: true,
18 + isDefaultRef: false,
19 + refName: "feature/test",
20 hasWorkingTreeChanges: false,
21 workingTree: {
22 files: [],
@@ -31,7 +31,7 @@ function status(overrides: Partial<GitStatusResult> = {}): GitStatusResult {
31 };
32 }
33
34 -describe("when: branch is clean and has an open PR", () => {
34 +describe("when: ref is clean and has an open PR", () => {
35 it("resolveQuickAction opens the existing PR", () => {
36 const quick = resolveQuickAction(
37 status({
@@ -39,8 +39,8 @@ describe("when: branch is clean and has an open PR", () => {
39 number: 10,
40 title: "Open PR",
41 url: "https://example.com/pr/10",
42 - baseBranch: "main",
43 - headBranch: "feature/test",
42 + baseRef: "main",
43 + headRef: "feature/test",
44 state: "open",
45 },
46 }),
@@ -56,8 +56,8 @@ describe("when: branch is clean and has an open PR", () => {
56 number: 11,
57 title: "Existing PR",
58 url: "https://example.com/pr/11",
59 - baseBranch: "main",
60 - headBranch: "feature/test",
59 + baseRef: "main",
60 + headRef: "feature/test",
61 state: "open",
62 },
63 }),
@@ -150,7 +150,7 @@ describe("when: git status is unavailable", () => {
150 });
151 });
152
153 -describe("when: branch is clean, ahead, and has an open PR", () => {
153 +describe("when: ref is clean, ahead, and has an open PR", () => {
154 it("resolveQuickAction prefers push", () => {
155 const quick = resolveQuickAction(
156 status({
@@ -159,8 +159,8 @@ describe("when: branch is clean, ahead, and has an open PR", () => {
159 number: 13,
160 title: "Open PR",
161 url: "https://example.com/pr/13",
162 - baseBranch: "main",
163 - headBranch: "feature/test",
162 + baseRef: "main",
163 + headRef: "feature/test",
164 state: "open",
165 },
166 }),
@@ -177,8 +177,8 @@ describe("when: branch is clean, ahead, and has an open PR", () => {
177 number: 12,
178 title: "Existing PR",
179 url: "https://example.com/pr/12",
180 - baseBranch: "main",
181 - headBranch: "feature/test",
180 + baseRef: "main",
181 + headRef: "feature/test",
182 state: "open",
183 },
184 }),
@@ -212,7 +212,7 @@ describe("when: branch is clean, ahead, and has an open PR", () => {
212 });
213 });
214
215 -describe("when: branch is clean, ahead, and has no open PR", () => {
215 +describe("when: ref is clean, ahead, and has no open PR", () => {
216 it("resolveQuickAction pushes and creates a PR", () => {
217 const quick = resolveQuickAction(status({ aheadCount: 2, pr: null }), false);
218 assert.deepInclude(quick, {
@@ -253,7 +253,33 @@ describe("when: branch is clean, ahead, and has no open PR", () => {
253 });
254 });
255
256 -describe("when: branch is clean, up to date, and has no open PR", () => {
256 +describe("when: source control provider uses merge requests", () => {
257 + it("uses GitLab MR terminology in quick actions and menu items", () => {
258 + const gitlabStatus = status({
259 + aheadCount: 2,
260 + sourceControlProvider: {
261 + kind: "gitlab",
262 + name: "GitLab",
263 + baseUrl: "https://gitlab.com",
264 + },
265 + });
266 +
267 + const quick = resolveQuickAction(gitlabStatus, false);
268 + const items = buildMenuItems(gitlabStatus, false);
269 +
270 + assert.deepInclude(quick, {
271 + kind: "run_action",
272 + action: "create_pr",
273 + label: "Push & create MR",
274 + });
275 + assert.deepInclude(items[2], {
276 + id: "pr",
277 + label: "Create MR",
278 + });
279 + });
280 +});
281 +
282 +describe("when: ref is clean, up to date, and has no open PR", () => {
283 it("resolveQuickAction returns disabled no-action state", () => {
284 const quick = resolveQuickAction(
285 status({ aheadCount: 0, behindCount: 0, hasWorkingTreeChanges: false, pr: null }),
@@ -293,7 +319,7 @@ describe("when: branch is clean, up to date, and has no open PR", () => {
319 });
320 });
321
296 -describe("when: branch is behind upstream", () => {
322 +describe("when: ref is behind upstream", () => {
323 it("resolveQuickAction returns pull", () => {
324 const quick = resolveQuickAction(status({ behindCount: 2 }), false);
325 assert.deepInclude(quick, { kind: "run_pull", label: "Pull", disabled: false });
@@ -330,11 +356,11 @@ describe("when: branch is behind upstream", () => {
356 });
357 });
358
333 -describe("when: branch has diverged from upstream", () => {
359 +describe("when: ref has diverged from upstream", () => {
360 it("resolveQuickAction returns a disabled sync hint", () => {
361 const quick = resolveQuickAction(status({ aheadCount: 2, behindCount: 1 }), false);
362 assert.deepEqual(quick, {
337 - label: "Sync branch",
363 + label: "Sync ref",
364 disabled: true,
365 kind: "show_hint",
366 hint: "Branch has diverged from upstream. Rebase/merge first.",
@@ -375,8 +401,8 @@ describe("when: working tree has local changes", () => {
401 number: 16,
402 title: "Existing PR",
403 url: "https://example.com/pr/16",
378 - baseBranch: "main",
379 - headBranch: "feature/test",
404 + baseRef: "main",
405 + headRef: "feature/test",
406 state: "open",
407 },
408 }),
@@ -420,10 +446,10 @@ describe("when: working tree has local changes", () => {
446 });
447 });
448
423 -describe("when: on default branch without open PR", () => {
449 +describe("when: on default ref without open PR", () => {
450 it("resolveQuickAction returns commit and push when local changes exist", () => {
451 const quick = resolveQuickAction(
426 - status({ branch: "main", hasWorkingTreeChanges: true }),
452 + status({ refName: "main", hasWorkingTreeChanges: true }),
453 false,
454 true,
455 );
@@ -435,9 +461,9 @@ describe("when: on default branch without open PR", () => {
461 });
462 });
463
438 - it("resolveQuickAction returns push when branch is ahead", () => {
464 + it("resolveQuickAction returns push when ref is ahead", () => {
465 const quick = resolveQuickAction(
440 - status({ branch: "main", aheadCount: 2, pr: null }),
466 + status({ refName: "main", aheadCount: 2, pr: null }),
467 false,
468 true,
469 );
@@ -450,7 +476,7 @@ describe("when: on default branch without open PR", () => {
476 });
477 });
478
453 -describe("when: working tree has local changes and branch is behind upstream", () => {
479 +describe("when: working tree has local changes and ref is behind upstream", () => {
480 it("resolveQuickAction still prefers commit, push, and create PR", () => {
481 const quick = resolveQuickAction(
482 status({ hasWorkingTreeChanges: true, behindCount: 1 }),
@@ -497,14 +523,14 @@ describe("when: working tree has local changes and branch is behind upstream", (
523 describe("when: HEAD is detached and there are no local changes", () => {
524 it("resolveQuickAction shows detached head hint", () => {
525 const quick = resolveQuickAction(
500 - status({ branch: null, hasWorkingTreeChanges: false, hasUpstream: false }),
526 + status({ refName: null, hasWorkingTreeChanges: false, hasUpstream: false }),
527 false,
528 );
529 assert.deepInclude(quick, { kind: "show_hint", label: "Commit", disabled: true });
530 });
531
532 it("buildMenuItems keeps commit, push, and PR disabled", () => {
507 - const items = buildMenuItems(status({ branch: null, hasWorkingTreeChanges: false }), false);
533 + const items = buildMenuItems(status({ refName: null, hasWorkingTreeChanges: false }), false);
534 assert.deepEqual(items, [
535 {
536 id: "commit",
@@ -534,7 +560,7 @@ describe("when: HEAD is detached and there are no local changes", () => {
560 });
561 });
562
537 -describe("when: branch has no upstream configured", () => {
563 +describe("when: ref has no upstream configured", () => {
564 it("resolveQuickAction is disabled when clean, no upstream, and no local commits are ahead", () => {
565 const quick = resolveQuickAction(
566 status({ hasUpstream: false, pr: null, aheadCount: 0 }),
@@ -557,8 +583,8 @@ describe("when: branch has no upstream configured", () => {
583 number: 14,
584 title: "Existing PR",
585 url: "https://example.com/pr/14",
560 - baseBranch: "main",
561 - headBranch: "feature/test",
586 + baseRef: "main",
587 + headRef: "feature/test",
588 state: "open",
589 },
590 }),
@@ -580,8 +606,8 @@ describe("when: branch has no upstream configured", () => {
606 number: 15,
607 title: "Existing PR",
608 url: "https://example.com/pr/15",
583 - baseBranch: "main",
584 - headBranch: "feature/test",
609 + baseRef: "main",
610 + headRef: "feature/test",
611 state: "open",
612 },
613 }),
@@ -656,7 +682,7 @@ describe("when: branch has no upstream configured", () => {
682 assert.deepEqual(quick, {
683 kind: "show_hint",
684 label: "Push",
659 - hint: 'Add an "origin" remote before pushing or creating a PR.',
685 + hint: 'Add an "origin" remote before pushing or creating a pull request.',
686 disabled: true,
687 });
688 });
@@ -725,10 +751,10 @@ describe("when: branch has no upstream configured", () => {
751 ]);
752 });
753
728 - it("resolveQuickAction is disabled on default branch when no upstream exists and no commits are ahead", () => {
754 + it("resolveQuickAction is disabled on default ref when no upstream exists and no commits are ahead", () => {
755 const quick = resolveQuickAction(
756 status({
731 - branch: "main",
757 + refName: "main",
758 hasUpstream: false,
759 aheadCount: 0,
760 pr: null,
@@ -744,10 +770,10 @@ describe("when: branch has no upstream configured", () => {
770 });
771 });
772
747 - it("resolveQuickAction uses push-only on default branch when no upstream exists and commits are ahead", () => {
773 + it("resolveQuickAction uses push-only on default ref when no upstream exists and commits are ahead", () => {
774 const quick = resolveQuickAction(
775 status({
750 - branch: "main",
776 + refName: "main",
777 hasUpstream: false,
778 aheadCount: 1,
779 pr: null,
@@ -763,7 +789,7 @@ describe("when: branch has no upstream configured", () => {
789 });
790 });
791
766 - it("buildMenuItems still disables push and create PR when branch is behind", () => {
792 + it("buildMenuItems still disables push and create PR when ref is behind", () => {
793 const items = buildMenuItems(
794 status({
795 hasUpstream: false,
@@ -803,7 +829,7 @@ describe("when: branch has no upstream configured", () => {
829 });
830
831 describe("requiresDefaultBranchConfirmation", () => {
806 - it("requires confirmation for push actions on default branch", () => {
832 + it("requires confirmation for push actions on default ref", () => {
833 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
834 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
835 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
@@ -823,9 +849,9 @@ describe("resolveDefaultBranchActionDialogCopy", () => {
849 });
850
851 assert.deepEqual(copy, {
826 - title: "Push to default branch?",
852 + title: "Push to default ref?",
853 description:
828 - 'This action will push local commits on "main". You can continue on this branch or create a feature branch and run the same action there.',
854 + 'This action will push local commits on "main". You can continue on this ref or create a feature ref and run the same action there.',
855 continueLabel: "Push to main",
856 });
857 });
@@ -838,9 +864,9 @@ describe("resolveDefaultBranchActionDialogCopy", () => {
864 });
865
866 assert.deepEqual(copy, {
841 - title: "Push & create PR from default branch?",
867 + title: "Push & create PR from default ref?",
868 description:
843 - 'This action will push local commits and create a PR on "main". You can continue on this branch or create a feature branch and run the same action there.',
869 + 'This action will push local commits and create a pull request on "main". You can continue on this ref or create a feature ref and run the same action there.',
870 continueLabel: "Push & create PR",
871 });
872 });
@@ -853,9 +879,9 @@ describe("resolveDefaultBranchActionDialogCopy", () => {
879 });
880
881 assert.deepEqual(copy, {
856 - title: "Commit, push & create PR from default branch?",
882 + title: "Commit, push & create PR from default ref?",
883 description:
858 - 'This action will commit, push, and create a PR on "main". You can continue on this branch or create a feature branch and run the same action there.',
884 + 'This action will commit, push, and create a pull request on "main". You can continue on this ref or create a feature ref and run the same action there.',
885 continueLabel: "Commit, push & create PR",
886 });
887 });
@@ -884,7 +910,7 @@ describe("buildGitActionProgressStages", () => {
910 "Pushing to origin/feature/test...",
911 "Preparing PR...",
912 "Generating PR content...",
887 - "Creating GitHub pull request...",
913 + "Creating pull request...",
914 ]);
915 });
916
@@ -898,7 +924,7 @@ describe("buildGitActionProgressStages", () => {
924 assert.deepEqual(stages, [
925 "Preparing PR...",
926 "Generating PR content...",
901 - "Creating GitHub pull request...",
927 + "Creating pull request...",
928 ]);
929 });
930
@@ -928,7 +954,7 @@ describe("buildGitActionProgressStages", () => {
954 "Pushing to origin/feature/test...",
955 "Preparing PR...",
956 "Generating PR content...",
931 - "Creating GitHub pull request...",
957 + "Creating pull request...",
958 ]);
959 });
960 });
@@ -944,7 +970,7 @@ describe("resolveThreadBranchUpdate", () => {
970 commit: {
971 status: "created",
972 commitSha: "89abcdef01234567",
947 - subject: "feat: add branch sync",
973 + subject: "feat: add ref sync",
974 },
975 push: { status: "pushed", branch: "feature/fix-toast-copy" },
976 pr: { status: "skipped_not_requested" },
@@ -968,7 +994,7 @@ describe("resolveThreadBranchUpdate", () => {
994 commit: {
995 status: "created",
996 commitSha: "89abcdef01234567",
971 - subject: "feat: add branch sync",
997 + subject: "feat: add ref sync",
998 },
999 push: { status: "pushed", branch: "feature/fix-toast-copy" },
1000 pr: { status: "skipped_not_requested" },
@@ -985,8 +1011,8 @@ describe("resolveThreadBranchUpdate", () => {
1011 describe("resolveLiveThreadBranchUpdate", () => {
1012 it("returns a branch update when live git status differs from stored thread metadata", () => {
1013 const update = resolveLiveThreadBranchUpdate({
988 - threadBranch: "feature/old-branch",
989 - gitStatus: status({ branch: "effect-atom" }),
1014 + threadBranch: "feature/old-ref",
1015 + gitStatus: status({ refName: "effect-atom" }),
1016 });
1017
1018 assert.deepEqual(update, {
@@ -996,35 +1022,35 @@ describe("resolveLiveThreadBranchUpdate", () => {
1022
1023 it("returns null when live git status is unavailable", () => {
1024 const update = resolveLiveThreadBranchUpdate({
999 - threadBranch: "feature/old-branch",
1025 + threadBranch: "feature/old-ref",
1026 gitStatus: null,
1027 });
1028
1029 assert.equal(update, null);
1030 });
1031
1006 - it("returns null when the stored thread branch already matches git status", () => {
1032 + it("returns null when the stored thread ref already matches git status", () => {
1033 const update = resolveLiveThreadBranchUpdate({
1034 threadBranch: "effect-atom",
1009 - gitStatus: status({ branch: "effect-atom" }),
1035 + gitStatus: status({ refName: "effect-atom" }),
1036 });
1037
1038 assert.equal(update, null);
1039 });
1040
1015 - it("returns null when git status is detached HEAD but the thread already has a branch", () => {
1041 + it("returns null when git status is detached HEAD but the thread already has a ref", () => {
1042 const update = resolveLiveThreadBranchUpdate({
1043 threadBranch: "effect-atom",
1018 - gitStatus: status({ branch: null }),
1044 + gitStatus: status({ refName: null }),
1045 });
1046
1047 assert.equal(update, null);
1048 });
1049
1024 - it("does not regress a semantic thread branch back to a temporary worktree branch", () => {
1050 + it("does not regress a semantic thread ref back to a temporary worktree ref", () => {
1051 const update = resolveLiveThreadBranchUpdate({
1052 threadBranch: "t3code/github-query-rate-limit",
1027 - gitStatus: status({ branch: "t3code/bda76797" }),
1053 + gitStatus: status({ refName: "t3code/bda76797" }),
1054 });
1055
1056 assert.equal(update, null);
@@ -1032,31 +1058,31 @@ describe("resolveLiveThreadBranchUpdate", () => {
1058 });
1059
1060 describe("resolveAutoFeatureBranchName", () => {
1035 - it("uses semantic preferred branch names when available", () => {
1036 - const branch = resolveAutoFeatureBranchName(["main", "feature/other"], "fix toast copy");
1037 - assert.equal(branch, "feature/fix-toast-copy");
1061 + it("uses semantic preferred ref names when available", () => {
1062 + const ref = resolveAutoFeatureBranchName(["main", "feature/other"], "fix toast copy");
1063 + assert.equal(ref, "feature/fix-toast-copy");
1064 });
1065
1040 - it("normalizes preferred names that already include a branch namespace", () => {
1041 - const branch = resolveAutoFeatureBranchName(["main"], "feature/refine-toolbar-actions");
1042 - assert.equal(branch, "feature/refine-toolbar-actions");
1066 + it("normalizes preferred names that already include a ref namespace", () => {
1067 + const ref = resolveAutoFeatureBranchName(["main"], "feature/refine-toolbar-actions");
1068 + assert.equal(ref, "feature/refine-toolbar-actions");
1069 });
1070
1045 - it("increments suffix when the preferred branch name already exists", () => {
1046 - const branch = resolveAutoFeatureBranchName(
1071 + it("increments suffix when the preferred ref name already exists", () => {
1072 + const ref = resolveAutoFeatureBranchName(
1073 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
1074 "fix toast copy",
1075 );
1050 - assert.equal(branch, "feature/fix-toast-copy-3");
1076 + assert.equal(ref, "feature/fix-toast-copy-3");
1077 });
1078
1053 - it("treats existing branch names as case-insensitive for collision checks", () => {
1054 - const branch = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
1055 - assert.equal(branch, "feature/ticket-1-2");
1079 + it("treats existing ref names as case-insensitive for collision checks", () => {
1080 + const ref = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
1081 + assert.equal(ref, "feature/ticket-1-2");
1082 });
1083
1084 it("falls back to feature/update when no preferred name is provided", () => {
1059 - const branch = resolveAutoFeatureBranchName(["main"]);
1060 - assert.equal(branch, "feature/update");
1085 + const ref = resolveAutoFeatureBranchName(["main"]);
1086 + assert.equal(ref, "feature/update");
1087 });
1088 });
apps/web/src/components/GitActionsControl.logic.ts
+67 -48
@@ -1,9 +1,14 @@
1 import type {
2 GitRunStackedActionResult,
3 GitStackedAction,
4 - GitStatusResult,
4 + VcsStatusResult,
5 } from "@t3tools/contracts";
6 import { isTemporaryWorktreeBranch } from "@t3tools/shared/git";
7 +import {
8 + DEFAULT_CHANGE_REQUEST_TERMINOLOGY,
9 + getChangeRequestTerminology,
10 + type ChangeRequestTerminology,
11 +} from "../sourceControlPresentation";
12
13 export type GitActionIconName = "commit" | "push" | "pr";
14
@@ -38,6 +43,14 @@ export type DefaultBranchConfirmableAction =
43 | "commit_push"
44 | "commit_push_pr";
45
46 +function resolveChangeRequestTerminology(
47 + gitStatus: VcsStatusResult | null,
48 +): ChangeRequestTerminology {
49 + return gitStatus?.sourceControlProvider
50 + ? getChangeRequestTerminology(gitStatus.sourceControlProvider)
51 + : DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
52 +}
53 +
54 export function buildGitActionProgressStages(input: {
55 action: GitStackedAction;
56 hasCustomCommitMessage: boolean;
@@ -45,13 +58,15 @@ export function buildGitActionProgressStages(input: {
58 pushTarget?: string;
59 featureBranch?: boolean;
60 shouldPushBeforePr?: boolean;
61 + terminology?: ChangeRequestTerminology;
62 }): string[] {
49 - const branchStages = input.featureBranch ? ["Preparing feature branch..."] : [];
63 + const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
64 + const branchStages = input.featureBranch ? ["Preparing feature ref..."] : [];
65 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
66 const prStages = [
52 - "Preparing PR...",
53 - "Generating PR content...",
54 - "Creating GitHub pull request...",
67 + `Preparing ${terminology.shortLabel}...`,
68 + `Generating ${terminology.shortLabel} content...`,
69 + `Creating ${terminology.singular}...`,
70 ];
71
72 if (input.action === "push") {
@@ -77,17 +92,18 @@ export function buildGitActionProgressStages(input: {
92 }
93
94 export function buildMenuItems(
80 - gitStatus: GitStatusResult | null,
95 + gitStatus: VcsStatusResult | null,
96 isBusy: boolean,
82 - hasOriginRemote = true,
97 + hasPrimaryRemote = true,
98 ): GitActionMenuItem[] {
99 if (!gitStatus) return [];
100 + const terminology = resolveChangeRequestTerminology(gitStatus);
101
86 - const hasBranch = gitStatus.branch !== null;
102 + const hasBranch = gitStatus.refName !== null;
103 const hasChanges = gitStatus.hasWorkingTreeChanges;
104 const hasOpenPr = gitStatus.pr?.state === "open";
105 const isBehind = gitStatus.behindCount > 0;
90 - const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream;
106 + const canPushWithoutUpstream = hasPrimaryRemote && !gitStatus.hasUpstream;
107 const canCommit = !isBusy && hasChanges;
108 const canPush =
109 !isBusy &&
@@ -126,14 +142,14 @@ export function buildMenuItems(
142 hasOpenPr
143 ? {
144 id: "pr",
129 - label: "View PR",
145 + label: `View ${terminology.shortLabel}`,
146 disabled: !canOpenPr,
147 icon: "pr",
148 kind: "open_pr",
149 }
150 : {
151 id: "pr",
136 - label: "Create PR",
152 + label: `Create ${terminology.shortLabel}`,
153 disabled: !canCreatePr,
154 icon: "pr",
155 kind: "open_dialog",
@@ -143,10 +159,10 @@ export function buildMenuItems(
159 }
160
161 export function resolveQuickAction(
146 - gitStatus: GitStatusResult | null,
162 + gitStatus: VcsStatusResult | null,
163 isBusy: boolean,
148 - isDefaultBranch = false,
149 - hasOriginRemote = true,
164 + isDefaultRef = false,
165 + hasPrimaryRemote = true,
166 ): GitQuickAction {
167 if (isBusy) {
168 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
@@ -161,31 +177,32 @@ export function resolveQuickAction(
177 };
178 }
179
164 - const hasBranch = gitStatus.branch !== null;
180 + const hasBranch = gitStatus.refName !== null;
181 const hasChanges = gitStatus.hasWorkingTreeChanges;
182 const hasOpenPr = gitStatus.pr?.state === "open";
183 const isAhead = gitStatus.aheadCount > 0;
184 const isBehind = gitStatus.behindCount > 0;
185 const isDiverged = isAhead && isBehind;
186 + const terminology = resolveChangeRequestTerminology(gitStatus);
187
188 if (!hasBranch) {
189 return {
190 label: "Commit",
191 disabled: true,
192 kind: "show_hint",
176 - hint: "Create and checkout a branch before pushing or opening a PR.",
193 + hint: `Create and checkout a ref before pushing or opening a ${terminology.singular}.`,
194 };
195 }
196
197 if (hasChanges) {
181 - if (!gitStatus.hasUpstream && !hasOriginRemote) {
198 + if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
199 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
200 }
184 - if (hasOpenPr || isDefaultBranch) {
201 + if (hasOpenPr || isDefaultRef) {
202 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
203 }
204 return {
188 - label: "Commit, push & PR",
205 + label: `Commit, push & ${terminology.shortLabel}`,
206 disabled: false,
207 kind: "run_action",
208 action: "commit_push_pr",
@@ -193,20 +210,20 @@ export function resolveQuickAction(
210 }
211
212 if (!gitStatus.hasUpstream) {
196 - if (!hasOriginRemote) {
213 + if (!hasPrimaryRemote) {
214 if (hasOpenPr && !isAhead) {
198 - return { label: "View PR", disabled: false, kind: "open_pr" };
215 + return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
216 }
217 return {
218 label: "Push",
219 disabled: true,
220 kind: "show_hint",
204 - hint: 'Add an "origin" remote before pushing or creating a PR.',
221 + hint: `Add an "origin" remote before pushing or creating a ${terminology.singular}.`,
222 };
223 }
224 if (!isAhead) {
225 if (hasOpenPr) {
209 - return { label: "View PR", disabled: false, kind: "open_pr" };
226 + return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
227 }
228 return {
229 label: "Push",
@@ -215,16 +232,16 @@ export function resolveQuickAction(
232 hint: "No local commits to push.",
233 };
234 }
218 - if (hasOpenPr || isDefaultBranch) {
235 + if (hasOpenPr || isDefaultRef) {
236 return {
237 label: "Push",
238 disabled: false,
239 kind: "run_action",
223 - action: isDefaultBranch ? "commit_push" : "push",
240 + action: isDefaultRef ? "commit_push" : "push",
241 };
242 }
243 return {
227 - label: "Push & create PR",
244 + label: `Push & create ${terminology.shortLabel}`,
245 disabled: false,
246 kind: "run_action",
247 action: "create_pr",
@@ -233,7 +250,7 @@ export function resolveQuickAction(
250
251 if (isDiverged) {
252 return {
236 - label: "Sync branch",
253 + label: "Sync ref",
254 disabled: true,
255 kind: "show_hint",
256 hint: "Branch has diverged from upstream. Rebase/merge first.",
@@ -249,16 +266,16 @@ export function resolveQuickAction(
266 }
267
268 if (isAhead) {
252 - if (hasOpenPr || isDefaultBranch) {
269 + if (hasOpenPr || isDefaultRef) {
270 return {
271 label: "Push",
272 disabled: false,
273 kind: "run_action",
257 - action: isDefaultBranch ? "commit_push" : "push",
274 + action: isDefaultRef ? "commit_push" : "push",
275 };
276 }
277 return {
261 - label: "Push & create PR",
278 + label: `Push & create ${terminology.shortLabel}`,
279 disabled: false,
280 kind: "run_action",
281 action: "create_pr",
@@ -266,7 +283,7 @@ export function resolveQuickAction(
283 }
284
285 if (hasOpenPr && gitStatus.hasUpstream) {
269 - return { label: "View PR", disabled: false, kind: "open_pr" };
286 + return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
287 }
288
289 return {
@@ -279,9 +296,9 @@ export function resolveQuickAction(
296
297 export function requiresDefaultBranchConfirmation(
298 action: GitStackedAction,
282 - isDefaultBranch: boolean,
299 + isDefaultRef: boolean,
300 ): boolean {
284 - if (!isDefaultBranch) return false;
301 + if (!isDefaultRef) return false;
302 return (
303 action === "push" ||
304 action === "create_pr" ||
@@ -294,20 +311,22 @@ export function resolveDefaultBranchActionDialogCopy(input: {
311 action: DefaultBranchConfirmableAction;
312 branchName: string;
313 includesCommit: boolean;
314 + terminology?: ChangeRequestTerminology;
315 }): DefaultBranchActionDialogCopy {
316 const branchLabel = input.branchName;
299 - const suffix = ` on "${branchLabel}". You can continue on this branch or create a feature branch and run the same action there.`;
317 + const suffix = ` on "${branchLabel}". You can continue on this ref or create a feature ref and run the same action there.`;
318 + const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
319
320 if (input.action === "push" || input.action === "commit_push") {
321 if (input.includesCommit) {
322 return {
304 - title: "Commit & push to default branch?",
323 + title: "Commit & push to default ref?",
324 description: `This action will commit and push changes${suffix}`,
325 continueLabel: `Commit & push to ${branchLabel}`,
326 };
327 }
328 return {
310 - title: "Push to default branch?",
329 + title: "Push to default ref?",
330 description: `This action will push local commits${suffix}`,
331 continueLabel: `Push to ${branchLabel}`,
332 };
@@ -315,15 +334,15 @@ export function resolveDefaultBranchActionDialogCopy(input: {
334
335 if (input.includesCommit) {
336 return {
318 - title: "Commit, push & create PR from default branch?",
319 - description: `This action will commit, push, and create a PR${suffix}`,
320 - continueLabel: `Commit, push & create PR`,
337 + title: `Commit, push & create ${terminology.shortLabel} from default ref?`,
338 + description: `This action will commit, push, and create a ${terminology.singular}${suffix}`,
339 + continueLabel: `Commit, push & create ${terminology.shortLabel}`,
340 };
341 }
342 return {
324 - title: "Push & create PR from default branch?",
325 - description: `This action will push local commits and create a PR${suffix}`,
326 - continueLabel: "Push & create PR",
343 + title: `Push & create ${terminology.shortLabel} from default ref?`,
344 + description: `This action will push local commits and create a ${terminology.singular}${suffix}`,
345 + continueLabel: `Push & create ${terminology.shortLabel}`,
346 };
347 }
348
@@ -341,31 +360,31 @@ export function resolveThreadBranchUpdate(
360
361 export function resolveLiveThreadBranchUpdate(input: {
362 threadBranch: string | null;
344 - gitStatus: GitStatusResult | null;
363 + gitStatus: VcsStatusResult | null;
364 }): { branch: string | null } | null {
365 if (!input.gitStatus) {
366 return null;
367 }
368
350 - if (input.gitStatus.branch === null && input.threadBranch !== null) {
369 + if (input.gitStatus.refName === null && input.threadBranch !== null) {
370 return null;
371 }
372
354 - if (input.threadBranch === input.gitStatus.branch) {
373 + if (input.threadBranch === input.gitStatus.refName) {
374 return null;
375 }
376
377 if (
378 input.threadBranch !== null &&
360 - input.gitStatus.branch !== null &&
379 + input.gitStatus.refName !== null &&
380 !isTemporaryWorktreeBranch(input.threadBranch) &&
362 - isTemporaryWorktreeBranch(input.gitStatus.branch)
381 + isTemporaryWorktreeBranch(input.gitStatus.refName)
382 ) {
383 return null;
384 }
385
386 return {
368 - branch: input.gitStatus.branch,
387 + branch: input.gitStatus.refName,
388 };
389 }
390
apps/web/src/components/GitActionsControl.tsx
+79 -49
@@ -3,12 +3,11 @@ import type {
3 GitActionProgressEvent,
4 GitRunStackedActionResult,
5 GitStackedAction,
6 - GitStatusResult,
6 + VcsStatusResult,
7 } from "@t3tools/contracts";
8 import { useIsMutating, useMutation, useQueryClient } from "@tanstack/react-query";
9 import { useCallback, useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
10 import { ChevronDownIcon, CloudUploadIcon, GitCommitIcon, InfoIcon } from "lucide-react";
11 -import { GitHubIcon } from "./Icons";
11 import {
12 buildGitActionProgressStages,
13 buildMenuItems,
@@ -52,6 +51,7 @@ import { resolvePathLinkTarget } from "~/terminal-links";
51 import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
52 import { readEnvironmentApi } from "~/environmentApi";
53 import { readLocalApi } from "~/localApi";
54 +import { getSourceControlPresentation } from "~/sourceControlPresentation";
55 import { useStore } from "~/store";
56 import { createThreadSelectorByRef } from "~/storeSelectors";
57
@@ -89,7 +89,7 @@ interface RunGitActionWithToastInput {
89 commitMessage?: string;
90 onConfirmed?: () => void;
91 skipDefaultBranchPrompt?: boolean;
92 - statusOverride?: GitStatusResult | null;
92 + statusOverride?: VcsStatusResult | null;
93 featureBranch?: boolean;
94 progressToastId?: GitActionToastId;
95 filePaths?: string[];
@@ -121,22 +121,23 @@ function getMenuActionDisabledReason({
121 item,
122 gitStatus,
123 isBusy,
124 - hasOriginRemote,
124 + hasPrimaryRemote,
125 }: {
126 item: GitActionMenuItem;
127 - gitStatus: GitStatusResult | null;
127 + gitStatus: VcsStatusResult | null;
128 isBusy: boolean;
129 - hasOriginRemote: boolean;
129 + hasPrimaryRemote: boolean;
130 }): string | null {
131 if (!item.disabled) return null;
132 if (isBusy) return "Git action in progress.";
133 if (!gitStatus) return "Git status is unavailable.";
134
135 - const hasBranch = gitStatus.branch !== null;
135 + const hasBranch = gitStatus.refName !== null;
136 const hasChanges = gitStatus.hasWorkingTreeChanges;
137 const hasOpenPr = gitStatus.pr?.state === "open";
138 const isAhead = gitStatus.aheadCount > 0;
139 const isBehind = gitStatus.behindCount > 0;
140 + const terminology = getSourceControlPresentation(gitStatus.sourceControlProvider).terminology;
141
142 if (item.id === "commit") {
143 if (!hasChanges) {
@@ -147,7 +148,7 @@ function getMenuActionDisabledReason({
148
149 if (item.id === "push") {
150 if (!hasBranch) {
150 - return "Detached HEAD: checkout a branch before pushing.";
151 + return "Detached HEAD: checkout a refName before pushing.";
152 }
153 if (hasChanges) {
154 return "Commit or stash local changes before pushing.";
@@ -155,7 +156,7 @@ function getMenuActionDisabledReason({
156 if (isBehind) {
157 return "Branch is behind upstream. Pull/rebase before pushing.";
158 }
158 - if (!gitStatus.hasUpstream && !hasOriginRemote) {
159 + if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
160 return 'Add an "origin" remote before pushing.';
161 }
162 if (!isAhead) {
@@ -165,46 +166,58 @@ function getMenuActionDisabledReason({
166 }
167
168 if (hasOpenPr) {
168 - return "View PR is currently unavailable.";
169 + return `View ${terminology.singular} is currently unavailable.`;
170 }
171 if (!hasBranch) {
171 - return "Detached HEAD: checkout a branch before creating a PR.";
172 + return `Detached HEAD: checkout a refName before creating a ${terminology.singular}.`;
173 }
174 if (hasChanges) {
174 - return "Commit local changes before creating a PR.";
175 + return `Commit local changes before creating a ${terminology.singular}.`;
176 }
176 - if (!gitStatus.hasUpstream && !hasOriginRemote) {
177 - return 'Add an "origin" remote before creating a PR.';
177 + if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
178 + return `Add an "origin" remote before creating a ${terminology.singular}.`;
179 }
180 if (!isAhead) {
180 - return "No local commits to include in a PR.";
181 + return `No local commits to include in a ${terminology.singular}.`;
182 }
183 if (isBehind) {
183 - return "Branch is behind upstream. Pull/rebase before creating a PR.";
184 + return `Branch is behind upstream. Pull/rebase before creating a ${terminology.singular}.`;
185 }
185 - return "Create PR is currently unavailable.";
186 + return `Create ${terminology.singular} is currently unavailable.`;
187 }
188
189 const COMMIT_DIALOG_TITLE = "Commit changes";
190 const COMMIT_DIALOG_DESCRIPTION =
191 "Review and confirm your commit. Leave the message blank to auto-generate one.";
192
192 -function GitActionItemIcon({ icon }: { icon: GitActionIconName }) {
193 +function GitActionItemIcon({
194 + icon,
195 + SourceControlIcon,
196 +}: {
197 + icon: GitActionIconName;
198 + SourceControlIcon: ReturnType<typeof getSourceControlPresentation>["Icon"];
199 +}) {
200 if (icon === "commit") return <GitCommitIcon />;
201 if (icon === "push") return <CloudUploadIcon />;
195 - return <GitHubIcon />;
202 + return <SourceControlIcon />;
203 }
204
198 -function GitQuickActionIcon({ quickAction }: { quickAction: GitQuickAction }) {
205 +function GitQuickActionIcon({
206 + quickAction,
207 + SourceControlIcon,
208 +}: {
209 + quickAction: GitQuickAction;
210 + SourceControlIcon: ReturnType<typeof getSourceControlPresentation>["Icon"];
211 +}) {
212 const iconClassName = "size-3.5";
200 - if (quickAction.kind === "open_pr") return <GitHubIcon className={iconClassName} />;
213 + if (quickAction.kind === "open_pr") return <SourceControlIcon className={iconClassName} />;
214 if (quickAction.kind === "run_pull") return <InfoIcon className={iconClassName} />;
215 if (quickAction.kind === "run_action") {
216 if (quickAction.action === "commit") return <GitCommitIcon className={iconClassName} />;
217 if (quickAction.action === "push" || quickAction.action === "commit_push") {
218 return <CloudUploadIcon className={iconClassName} />;
219 }
207 - return <GitHubIcon className={iconClassName} />;
220 + return <SourceControlIcon className={iconClassName} />;
221 }
222 if (quickAction.label === "Commit") return <GitCommitIcon className={iconClassName} />;
223 return <InfoIcon className={iconClassName} />;
@@ -322,9 +335,15 @@ export default function GitActionsControl({
335 environmentId: activeEnvironmentId,
336 cwd: gitCwd,
337 });
338 + const sourceControlPresentation = useMemo(
339 + () => getSourceControlPresentation(gitStatus?.sourceControlProvider),
340 + [gitStatus?.sourceControlProvider],
341 + );
342 + const changeRequestTerminology = sourceControlPresentation.terminology;
343 + const SourceControlIcon = sourceControlPresentation.Icon;
344 // Default to true while loading so we don't flash init controls.
345 const isRepo = gitStatus?.isRepo ?? true;
327 - const hasOriginRemote = gitStatus?.hasOriginRemote ?? false;
346 + const hasPrimaryRemote = gitStatus?.hasPrimaryRemote ?? false;
347 const gitStatusForActions = gitStatus;
348
349 const allFiles = gitStatusForActions?.workingTree.files ?? [];
@@ -382,18 +401,18 @@ export default function GitActionsControl({
401 persistThreadBranchSync,
402 ]);
403
385 - const isDefaultBranch = useMemo(() => {
386 - return gitStatusForActions?.isDefaultBranch ?? false;
387 - }, [gitStatusForActions?.isDefaultBranch]);
404 + const isDefaultRef = useMemo(() => {
405 + return gitStatusForActions?.isDefaultRef ?? false;
406 + }, [gitStatusForActions?.isDefaultRef]);
407
408 const gitActionMenuItems = useMemo(
390 - () => buildMenuItems(gitStatusForActions, isGitActionRunning, hasOriginRemote),
391 - [gitStatusForActions, hasOriginRemote, isGitActionRunning],
409 + () => buildMenuItems(gitStatusForActions, isGitActionRunning, hasPrimaryRemote),
410 + [gitStatusForActions, hasPrimaryRemote, isGitActionRunning],
411 );
412 const quickAction = useMemo(
413 () =>
395 - resolveQuickAction(gitStatusForActions, isGitActionRunning, isDefaultBranch, hasOriginRemote),
396 - [gitStatusForActions, hasOriginRemote, isDefaultBranch, isGitActionRunning],
414 + resolveQuickAction(gitStatusForActions, isGitActionRunning, isDefaultRef, hasPrimaryRemote),
415 + [gitStatusForActions, hasPrimaryRemote, isDefaultRef, isGitActionRunning],
416 );
417 const quickActionDisabledReason = quickAction.disabled
418 ? (quickAction.hint ?? "This action is currently unavailable.")
@@ -403,6 +422,7 @@ export default function GitActionsControl({
422 action: pendingDefaultBranchAction.action,
423 branchName: pendingDefaultBranchAction.branchName,
424 includesCommit: pendingDefaultBranchAction.includesCommit,
425 + terminology: changeRequestTerminology,
426 })
427 : null;
428
@@ -468,7 +488,7 @@ export default function GitActionsControl({
488 if (!prUrl) {
489 toastManager.add({
490 type: "error",
471 - title: "No open PR found.",
491 + title: "No open pull request found.",
492 data: threadToastData,
493 });
494 return;
@@ -477,7 +497,7 @@ export default function GitActionsControl({
497 toastManager.add(
498 stackedThreadToast({
499 type: "error",
480 - title: "Unable to open PR link",
500 + title: "Unable to open pull request link",
501 description: err instanceof Error ? err.message : "An error occurred.",
502 ...(threadToastData !== undefined ? { data: threadToastData } : {}),
503 }),
@@ -497,8 +517,8 @@ export default function GitActionsControl({
517 filePaths,
518 }: RunGitActionWithToastInput) => {
519 const actionStatus = statusOverride ?? gitStatusForActions;
500 - const actionBranch = actionStatus?.branch ?? null;
501 - const actionIsDefaultBranch = featureBranch ? false : isDefaultBranch;
520 + const actionBranch = actionStatus?.refName ?? null;
521 + const actionIsDefaultBranch = featureBranch ? false : isDefaultRef;
522 const actionCanCommit =
523 action === "commit" || action === "commit_push" || action === "commit_push_pr";
524 const includesCommit =
@@ -534,6 +554,7 @@ export default function GitActionsControl({
554 hasCustomCommitMessage: !!commitMessage?.trim(),
555 hasWorkingTreeChanges: !!actionStatus?.hasWorkingTreeChanges,
556 featureBranch,
557 + terminology: changeRequestTerminology,
558 shouldPushBeforePr:
559 action === "create_pr" &&
560 (!actionStatus?.hasUpstream || (actionStatus?.aheadCount ?? 0) > 0),
@@ -775,8 +796,8 @@ export default function GitActionsControl({
796 title: result.status === "pulled" ? "Pulled" : "Already up to date",
797 description:
798 result.status === "pulled"
778 - ? `Updated ${result.branch} from ${result.upstreamBranch ?? "upstream"}`
779 - : `${result.branch} is already synchronized.`,
799 + ? `Updated ${result.refName} from ${result.upstreamRef ?? "upstream"}`
800 + : `${result.refName} is already synchronized.`,
801 data: threadToastData,
802 }),
803 error: (err) => ({
@@ -889,7 +910,10 @@ export default function GitActionsControl({
910 />
911 }
912 >
892 - <GitQuickActionIcon quickAction={quickAction} />
913 + <GitQuickActionIcon
914 + quickAction={quickAction}
915 + SourceControlIcon={SourceControlIcon}
916 + />
917 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
918 {quickAction.label}
919 </span>
@@ -905,7 +929,7 @@ export default function GitActionsControl({
929 disabled={isGitActionRunning || quickAction.disabled}
930 onClick={runQuickAction}
931 >
908 - <GitQuickActionIcon quickAction={quickAction} />
932 + <GitQuickActionIcon quickAction={quickAction} SourceControlIcon={SourceControlIcon} />
933 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
934 {quickAction.label}
935 </span>
@@ -934,7 +958,7 @@ export default function GitActionsControl({
958 item,
959 gitStatus: gitStatusForActions,
960 isBusy: isGitActionRunning,
937 - hasOriginRemote,
961 + hasPrimaryRemote,
962 });
963 if (item.disabled && disabledReason) {
964 return (
@@ -945,7 +969,10 @@ export default function GitActionsControl({
969 render={<span className="block w-max cursor-not-allowed" />}
970 >
971 <MenuItem className="w-full" disabled>
948 - <GitActionItemIcon icon={item.icon} />
972 + <GitActionItemIcon
973 + icon={item.icon}
974 + SourceControlIcon={SourceControlIcon}
975 + />
976 {item.label}
977 </MenuItem>
978 </PopoverTrigger>
@@ -964,18 +991,19 @@ export default function GitActionsControl({
991 openDialogForMenuItem(item);
992 }}
993 >
967 - <GitActionItemIcon icon={item.icon} />
994 + <GitActionItemIcon icon={item.icon} SourceControlIcon={SourceControlIcon} />
995 {item.label}
996 </MenuItem>
997 );
998 })}
972 - {gitStatusForActions?.branch === null && (
999 + {gitStatusForActions?.refName === null && (
1000 <p className="px-2 py-1.5 text-xs text-warning">
974 - Detached HEAD: create and checkout a branch to enable push and PR actions.
1001 + Detached HEAD: create and checkout a refName to enable push and pull request
1002 + actions.
1003 </p>
1004 )}
1005 {gitStatusForActions &&
978 - gitStatusForActions.branch !== null &&
1006 + gitStatusForActions.refName !== null &&
1007 !gitStatusForActions.hasWorkingTreeChanges &&
1008 gitStatusForActions.behindCount > 0 &&
1009 gitStatusForActions.aheadCount === 0 && (
@@ -1013,10 +1041,12 @@ export default function GitActionsControl({
1041 <span className="text-muted-foreground">Branch</span>
1042 <span className="flex items-center justify-between gap-2">
1043 <span className="font-medium">
1016 - {gitStatusForActions?.branch ?? "(detached HEAD)"}
1044 + {gitStatusForActions?.refName ?? "(detached HEAD)"}
1045 </span>
1018 - {isDefaultBranch && (
1019 - <span className="text-right text-warning text-xs">Warning: default branch</span>
1046 + {isDefaultRef && (
1047 + <span className="text-right text-warning text-xs">
1048 + Warning: default refName
1049 + </span>
1050 )}
1051 </span>
1052 </div>
@@ -1149,7 +1179,7 @@ export default function GitActionsControl({
1179 disabled={noneSelected}
1180 onClick={runDialogActionOnNewBranch}
1181 >
1152 - Commit on new branch
1182 + Commit on new refName
1183 </Button>
1184 <Button size="sm" disabled={noneSelected} onClick={runDialogAction}>
1185 Commit
@@ -1169,7 +1199,7 @@ export default function GitActionsControl({
1199 <DialogPopup className="max-w-xl">
1200 <DialogHeader>
1201 <DialogTitle>
1172 - {pendingDefaultBranchActionCopy?.title ?? "Run action on default branch?"}
1202 + {pendingDefaultBranchActionCopy?.title ?? "Run action on default refName?"}
1203 </DialogTitle>
1204 <DialogDescription>{pendingDefaultBranchActionCopy?.description}</DialogDescription>
1205 </DialogHeader>
apps/web/src/components/Icons.tsx
+25
@@ -15,6 +15,31 @@ export const GitHubIcon: Icon = (props) => (
15 </svg>
16 );
17
18 +export const GitLabIcon: Icon = (props) => (
19 + <svg {...props} viewBox="0 0 24 24" fill="currentColor">
20 + <path d="M22.6 9.3 20.9 4c-.2-.7-1.2-.7-1.5-.1l-2 5.4H6.6l-2-5.4c-.2-.6-1.2-.6-1.5.1L1.4 9.3c-.2.6 0 1.2.5 1.6L12 18.7l10.1-7.8c.5-.4.7-1 .5-1.6Z" />
21 + <path d="m12 18.7 4.1-9.4H7.9L12 18.7Z" opacity=".72" />
22 + <path d="M7.9 9.3H1.5L12 18.7 7.9 9.3ZM16.1 9.3 12 18.7l10.5-9.4h-6.4Z" opacity=".88" />
23 + </svg>
24 +);
25 +
26 +export const AzureDevOpsIcon: Icon = (props) => (
27 + <svg {...props} viewBox="0 0 24 24" fill="currentColor">
28 + <path d="M21 5.2v12.7L16 22l-8-2.8V22l-4.9-6.4 4.9.4V4.8L16 2l5 3.2ZM16 6.4l-5 1.4v8.4l5 1.4V6.4Z" />
29 + <path d="m8 8.4-5 .8v5.6l5 .8V8.4Z" opacity=".72" />
30 + </svg>
31 +);
32 +
33 +export const BitbucketIcon: Icon = (props) => (
34 + <svg {...props} viewBox="0 0 24 24" fill="currentColor">
35 + <path d="M2.4 3.5c-.4 0-.7.3-.6.8L4.3 20c.1.4.4.7.8.7h13.7c.4 0 .7-.3.8-.7l2.6-15.7c.1-.5-.2-.8-.7-.8H2.4Zm12.5 11.1H9.4L8.5 8.7h7.4l-1 5.9Z" />
36 + <path
37 + d="M21 8.7h-5.1l-1 5.9H9.4l-4.2 5c.2.6.5 1.1 1.2 1.1h12.4c.4 0 .7-.3.8-.7L21 8.7Z"
38 + opacity=".72"
39 + />
40 + </svg>
41 +);
42 +
43 export const CursorIcon: Icon = ({ className, ...props }) => (
44 <svg
45 {...props}
apps/web/src/components/KeybindingsToast.browser.tsx
+3 -3
@@ -256,13 +256,13 @@ function resolveWsRpc(tag: string): unknown {
256 if (tag === WS_METHODS.serverGetConfig) {
257 return fixture.serverConfig;
258 }
259 - if (tag === WS_METHODS.gitListBranches) {
259 + if (tag === WS_METHODS.vcsListRefs) {
260 return {
261 isRepo: true,
262 - hasOriginRemote: true,
262 + hasPrimaryRemote: true,
263 nextCursor: null,
264 totalCount: 1,
265 - branches: [{ name: "main", current: true, isDefault: true, worktreePath: null }],
265 + refs: [{ name: "main", current: true, isDefault: true, worktreePath: null }],
266 };
267 }
268 if (tag === WS_METHODS.projectsSearchEntries) {
apps/web/src/components/PullRequestThreadDialog.tsx
+24 -10
@@ -7,8 +7,10 @@ import {
7 gitPreparePullRequestThreadMutationOptions,
8 gitResolvePullRequestQueryOptions,
9 } from "~/lib/gitReactQuery";
10 +import { useGitStatus } from "~/lib/gitStatusState";
11 import { cn } from "~/lib/utils";
12 import { parsePullRequestReference } from "~/pullRequestReference";
13 +import { getSourceControlPresentation } from "~/sourceControlPresentation";
14 import { Button } from "./ui/button";
15 import {
16 Dialog,
@@ -51,6 +53,13 @@ export function PullRequestThreadDialog({
53 { wait: 450 },
54 (debouncerState) => ({ isPending: debouncerState.isPending }),
55 );
56 + const { data: gitStatus = null } = useGitStatus({ environmentId, cwd });
57 + const sourceControlPresentation = useMemo(
58 + () => getSourceControlPresentation(gitStatus?.sourceControlProvider),
59 + [gitStatus?.sourceControlProvider],
60 + );
61 + const terminology = sourceControlPresentation.terminology;
62 + const SourceControlIcon = sourceControlPresentation.Icon;
63
64 useEffect(() => {
65 if (!open) return;
@@ -161,20 +170,20 @@ export function PullRequestThreadDialog({
170 const validationMessage = !referenceDirty
171 ? null
172 : reference.trim().length === 0
164 - ? "Paste a GitHub pull request URL, `gh pr checkout 123`, or enter 123 / #123."
173 + ? `Paste a ${terminology.singular} URL, checkout command, or enter 123 / #123.`
174 : parsedReference === null
166 - ? "Use a GitHub pull request URL, `gh pr checkout 123`, 123, or #123."
175 + ? `Use a ${terminology.singular} URL, checkout command, 123, or #123.`
176 : null;
177 const errorMessage =
178 validationMessage ??
179 (resolvedPullRequest === null && resolvePullRequestQuery.isError
180 ? resolvePullRequestQuery.error instanceof Error
181 ? resolvePullRequestQuery.error.message
173 - : "Failed to resolve pull request."
182 + : `Failed to resolve ${terminology.singular}.`
183 : preparePullRequestThreadMutation.error instanceof Error
184 ? preparePullRequestThreadMutation.error.message
185 : preparePullRequestThreadMutation.error
177 - ? "Failed to prepare pull request thread."
186 + ? `Failed to prepare ${terminology.singular} thread.`
187 : null);
188
189 return (
@@ -188,18 +197,23 @@ export function PullRequestThreadDialog({
197 >
198 <DialogPopup className="max-w-xl">
199 <DialogHeader>
191 - <DialogTitle>Checkout Pull Request</DialogTitle>
200 + <DialogTitle className="flex items-center gap-2">
201 + <SourceControlIcon className="size-4" />
202 + Checkout {terminology.singular}
203 + </DialogTitle>
204 <DialogDescription>
193 - Resolve a GitHub pull request, then create the draft thread in the main repo or in a
194 - dedicated worktree.
205 + Resolve a {sourceControlPresentation.providerName} {terminology.singular}, then create
206 + the draft thread in the main repo or in a dedicated worktree.
207 </DialogDescription>
208 </DialogHeader>
209 <DialogPanel className="space-y-4">
210 <label className="grid gap-1.5">
199 - <span className="text-xs font-medium text-foreground">Pull request</span>
211 + <span className="text-xs font-medium text-foreground capitalize">
212 + {terminology.singular}
213 + </span>
214 <Input
215 ref={referenceInputRef}
202 - placeholder="https://github.com/owner/repo/pull/42, gh pr checkout 42, or #42"
216 + placeholder={`${terminology.shortLabel} URL, checkout command, or #42`}
217 value={reference}
218 onChange={(event) => {
219 setReferenceDirty(true);
@@ -237,7 +251,7 @@ export function PullRequestThreadDialog({
251 {isResolving ? (
252 <div className="flex items-center gap-2 text-muted-foreground text-xs">
253 <Spinner className="size-3.5" />
240 - Resolving pull request...
254 + Resolving {terminology.singular}...
255 </div>
256 ) : null}
257
apps/web/src/components/Sidebar.tsx
+4 -4
@@ -3,7 +3,6 @@ import {
3 ArrowUpDownIcon,
4 ChevronRightIcon,
5 CloudIcon,
6 - GitPullRequestIcon,
6 FolderPlusIcon,
7 SearchIcon,
8 SettingsIcon,
@@ -12,6 +11,7 @@ import {
11 TriangleAlertIcon,
12 } from "lucide-react";
13 import {
14 + ChangeRequestStatusIcon,
15 prStatusIndicator,
16 resolveThreadPr,
17 terminalStatusFromRunningIds,
@@ -367,7 +367,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
367 },
368 });
369 const pr = resolveThreadPr(thread.branch, gitStatus.data);
370 - const prStatus = prStatusIndicator(pr);
370 + const prStatus = prStatusIndicator(pr, gitStatus.data?.sourceControlProvider);
371 const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
372 const isConfirmingArchive = confirmingArchiveThreadKey === threadKey && !isThreadRunning;
373 const threadMetaClassName = isConfirmingArchive
@@ -558,7 +558,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
558 className={`inline-flex items-center justify-center ${prStatus.colorClass} cursor-pointer rounded-sm outline-hidden focus-visible:ring-1 focus-visible:ring-ring`}
559 onClick={handlePrClick}
560 >
561 - <GitPullRequestIcon className="size-3" />
561 + <ChangeRequestStatusIcon className="size-3" />
562 </button>
563 }
564 />
@@ -1002,7 +1002,7 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
1002 toastManager.add(
1003 stackedThreadToast({
1004 type: "error",
1005 - title: "Unable to open PR link",
1005 + title: "Unable to open pull request link",
1006 description: error instanceof Error ? error.message : "An error occurred.",
1007 }),
1008 );
apps/web/src/components/ThreadStatusIndicators.tsx
+24 -15
@@ -1,5 +1,5 @@
1 import { scopeProjectRef, scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime";
2 -import type { GitStatusResult } from "@t3tools/contracts";
2 +import type { VcsStatusResult } from "@t3tools/contracts";
3 import { CloudIcon, GitPullRequestIcon, TerminalIcon } from "lucide-react";
4 import { useMemo } from "react";
5 import { usePrimaryEnvironmentId } from "../environments/primary";
@@ -11,12 +11,13 @@ import { useGitStatus } from "../lib/gitStatusState";
11 import { type AppState, selectProjectByRef, useStore } from "../store";
12 import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
13 import { useUiStateStore } from "../uiStateStore";
14 +import { resolveChangeRequestPresentation } from "../sourceControlPresentation";
15 import { resolveThreadStatusPill, type ThreadStatusPill } from "./Sidebar.logic";
16 import type { SidebarThreadSummary } from "../types";
17 import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
18
19 export interface PrStatusIndicator {
19 - label: "PR open" | "PR closed" | "PR merged";
20 + label: string;
21 colorClass: string;
22 tooltip: string;
23 url: string;
@@ -28,43 +29,51 @@ export interface TerminalStatusIndicator {
29 pulse: boolean;
30 }
31
31 -export type ThreadPr = GitStatusResult["pr"];
32 +export type ThreadPr = VcsStatusResult["pr"];
33
33 -export function prStatusIndicator(pr: ThreadPr): PrStatusIndicator | null {
34 +export function prStatusIndicator(
35 + pr: ThreadPr,
36 + provider: VcsStatusResult["sourceControlProvider"] | null | undefined,
37 +): PrStatusIndicator | null {
38 if (!pr) return null;
39 + const presentation = resolveChangeRequestPresentation(provider);
40
41 if (pr.state === "open") {
42 return {
38 - label: "PR open",
43 + label: `${presentation.shortName} open`,
44 colorClass: "text-emerald-600 dark:text-emerald-300/90",
40 - tooltip: `#${pr.number} PR open: ${pr.title}`,
45 + tooltip: `#${pr.number} ${presentation.shortName} open: ${pr.title}`,
46 url: pr.url,
47 };
48 }
49 if (pr.state === "closed") {
50 return {
46 - label: "PR closed",
51 + label: `${presentation.shortName} closed`,
52 colorClass: "text-zinc-500 dark:text-zinc-400/80",
48 - tooltip: `#${pr.number} PR closed: ${pr.title}`,
53 + tooltip: `#${pr.number} ${presentation.shortName} closed: ${pr.title}`,
54 url: pr.url,
55 };
56 }
57 if (pr.state === "merged") {
58 return {
54 - label: "PR merged",
59 + label: `${presentation.shortName} merged`,
60 colorClass: "text-violet-600 dark:text-violet-300/90",
56 - tooltip: `#${pr.number} PR merged: ${pr.title}`,
61 + tooltip: `#${pr.number} ${presentation.shortName} merged: ${pr.title}`,
62 url: pr.url,
63 };
64 }
65 return null;
66 }
67
68 +export function ChangeRequestStatusIcon({ className }: { className?: string }) {
69 + return <GitPullRequestIcon className={className} />;
70 +}
71 +
72 export function resolveThreadPr(
73 threadBranch: string | null,
65 - gitStatus: GitStatusResult | null,
74 + gitStatus: VcsStatusResult | null,
75 ): ThreadPr | null {
67 - if (threadBranch === null || gitStatus === null || gitStatus.branch !== threadBranch) {
76 + if (threadBranch === null || gitStatus === null || gitStatus.refName !== threadBranch) {
77 return null;
78 }
79
@@ -124,7 +133,7 @@ export function ThreadStatusLabel({
133
134 /**
135 * Non-interactive leading status icons for a thread row in compact contexts
127 - * like the command palette. Shows the PR state icon (if present) and the
136 + * like the command palette. Shows the change request state icon (if present) and the
137 * thread status dot, matching the sidebar's leading indicators.
138 */
139 export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummary }) {
@@ -146,7 +155,7 @@ export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummar
155 cwd: thread.branch != null ? gitCwd : null,
156 });
157 const pr = resolveThreadPr(thread.branch, gitStatus.data);
149 - const prStatus = prStatusIndicator(pr);
158 + const prStatus = prStatusIndicator(pr, gitStatus.data?.sourceControlProvider);
159 const threadStatus = resolveThreadStatusPill({
160 thread: {
161 ...thread,
@@ -170,7 +179,7 @@ export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummar
179 />
180 }
181 >
173 - <GitPullRequestIcon className="size-3" />
182 + <ChangeRequestStatusIcon className="size-3" />
183 </TooltipTrigger>
184 <TooltipPopup side="top">{prStatus.tooltip}</TooltipPopup>
185 </Tooltip>
apps/web/src/components/settings/ProviderInstanceCard.tsx
+8 -44
@@ -1,7 +1,7 @@
1 "use client";
2
3 import { ChevronDownIcon, PlusIcon, Trash2Icon, XIcon } from "lucide-react";
4 -import { useEffect, useMemo, useState, type ReactNode } from "react";
4 +import { useEffect, useState, type ReactNode } from "react";
5 import {
6 isProviderDriverKind,
7 type ProviderInstanceConfig,
@@ -24,6 +24,7 @@ import type { DriverOption } from "./providerDriverMeta";
24 import { ProviderSettingsForm } from "./ProviderSettingsForm";
25 import { ProviderModelsSection } from "./ProviderModelsSection";
26 import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
27 +import { RedactedSensitiveText } from "./RedactedSensitiveText";
28 import {
29 PROVIDER_STATUS_STYLES,
30 getProviderSummary,
@@ -42,8 +43,6 @@ const PROVIDER_ACCENT_SWATCHES = [
43
44 const ENVIRONMENT_VARIABLE_NAME_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
45
45 -const REDACTED_EMAIL_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
46 -
46 let environmentVariableDraftId = 0;
47 const nextEnvironmentVariableDraftId = () => `provider-env-${environmentVariableDraftId++}`;
48
@@ -68,25 +67,6 @@ function makeEnvironmentDraftRow(
67 };
68 }
69
71 -function redactedEmailPlaceholder(email: string): string {
72 - let state = 0x811c9dc5;
73 - for (let index = 0; index < email.length; index += 1) {
74 - state ^= email.charCodeAt(index);
75 - state = Math.imul(state, 0x01000193);
76 - }
77 -
78 - const nextChar = () => {
79 - state = Math.imul(state ^ (state >>> 13), 0x85ebca6b);
80 - state = Math.imul(state ^ (state >>> 16), 0xc2b2ae35);
81 - return REDACTED_EMAIL_ALPHABET[Math.abs(state) % REDACTED_EMAIL_ALPHABET.length] ?? "x";
82 - };
83 -
84 - return Array.from(email, (char) => {
85 - if (char === "@" || char === "." || char === "-" || char === "_") return char;
86 - return nextChar();
87 - }).join("");
88 -}
89 -
70 /**
71 * Read a string[] at `key` from the opaque config blob, filtering out
72 * non-string entries. Used for `customModels`, which is always typed as
@@ -146,35 +126,19 @@ function ProviderAuthEmail(props: {
126 readonly prefix?: string;
127 readonly separator?: boolean;
128 }) {
149 - const [revealed, setRevealed] = useState(false);
129 const trimmed = props.email?.trim();
151 - const redacted = useMemo(() => (trimmed ? redactedEmailPlaceholder(trimmed) : ""), [trimmed]);
130 if (!trimmed) return null;
131
132 return (
133 <span className="inline-flex min-w-0 items-center gap-1.5">
134 {props.separator ? <span aria-hidden>·</span> : null}
135 {props.prefix ? <span className="text-muted-foreground/80">{props.prefix}</span> : null}
158 - <Tooltip>
159 - <TooltipTrigger
160 - render={
161 - <button
162 - type="button"
163 - className={cn(
164 - "min-w-0 cursor-pointer rounded-sm font-mono text-[11px] leading-none transition hover:text-foreground",
165 - revealed ? "text-muted-foreground" : "select-none text-muted-foreground blur-[2px]",
166 - )}
167 - onClick={() => setRevealed((value) => !value)}
168 - aria-label={revealed ? "Hide account email" : "Reveal account email"}
169 - >
170 - {revealed ? trimmed : redacted}
171 - </button>
172 - }
173 - />
174 - <TooltipPopup side="top">
175 - {revealed ? "Click to hide email" : "Click to reveal email"}
176 - </TooltipPopup>
177 - </Tooltip>
136 + <RedactedSensitiveText
137 + value={trimmed}
138 + ariaLabel="Toggle account email visibility"
139 + revealTooltip="Click to reveal email"
140 + hideTooltip="Click to hide email"
141 + />
142 </span>
143 );
144 }
apps/web/src/components/settings/RedactedSensitiveText.tsx new
+61
@@ -0,0 +1,61 @@
1 +import { useMemo, useState } from "react";
2 +
3 +import { cn } from "../../lib/utils";
4 +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
5 +
6 +const REDACTED_TEXT_ALPHABET = "abcdefghjkmnpqrstuvwxyz23456789";
7 +
8 +function redactedPlaceholder(value: string): string {
9 + let state = 0x811c9dc5;
10 + for (let index = 0; index < value.length; index += 1) {
11 + state ^= value.charCodeAt(index);
12 + state = Math.imul(state, 0x01000193);
13 + }
14 +
15 + const nextChar = () => {
16 + state = Math.imul(state ^ (state >>> 13), 0x85ebca6b);
17 + state = Math.imul(state ^ (state >>> 16), 0xc2b2ae35);
18 + return REDACTED_TEXT_ALPHABET[Math.abs(state) % REDACTED_TEXT_ALPHABET.length] ?? "x";
19 + };
20 +
21 + return Array.from(value, (char) => {
22 + if (char === "@" || char === "." || char === "-" || char === "_") return char;
23 + return nextChar();
24 + }).join("");
25 +}
26 +
27 +export function RedactedSensitiveText(props: {
28 + readonly value: string | null | undefined;
29 + readonly ariaLabel: string;
30 + readonly revealTooltip: string;
31 + readonly hideTooltip: string;
32 + readonly className?: string;
33 +}) {
34 + const [revealed, setRevealed] = useState(false);
35 + const value = props.value?.trim();
36 + const redacted = useMemo(() => (value ? redactedPlaceholder(value) : ""), [value]);
37 +
38 + if (!value) return null;
39 +
40 + return (
41 + <Tooltip>
42 + <TooltipTrigger
43 + render={
44 + <button
45 + type="button"
46 + className={cn(
47 + "min-w-0 cursor-pointer rounded-sm font-mono text-[11px] leading-none transition hover:text-foreground",
48 + revealed ? "text-muted-foreground" : "select-none text-muted-foreground blur-[2px]",
49 + props.className,
50 + )}
51 + onClick={() => setRevealed((current) => !current)}
52 + aria-label={props.ariaLabel}
53 + >
54 + {revealed ? value : redacted}
55 + </button>
56 + }
57 + />
58 + <TooltipPopup side="top">{revealed ? props.hideTooltip : props.revealTooltip}</TooltipPopup>
59 + </Tooltip>
60 + );
61 +}
apps/web/src/components/settings/SettingsPanels.browser.tsx
+149 -2
@@ -11,17 +11,19 @@ import {
11 type DesktopUpdateState,
12 type LocalApi,
13 type ServerConfig,
14 + type SourceControlDiscoveryResult,
15 } from "@t3tools/contracts";
15 -import { DateTime } from "effect";
16 +import { DateTime, Option } from "effect";
17 import { page } from "vitest/browser";
18 import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
19 import { render } from "vitest-browser-react";
20
21 import { __resetLocalApiForTests } from "../../localApi";
21 -import { AppAtomRegistryProvider } from "../../rpc/atomRegistry";
22 +import { AppAtomRegistryProvider, resetAppAtomRegistryForTests } from "../../rpc/atomRegistry";
23 import { resetServerStateForTests, setServerConfigSnapshot } from "../../rpc/serverState";
24 import { ConnectionsSettings } from "./ConnectionsSettings";
25 import { GeneralSettingsPanel } from "./SettingsPanels";
26 +import { SourceControlSettingsPanel } from "./SourceControlSettings";
27
28 const authAccessHarness = vi.hoisted(() => {
29 type Snapshot = AuthAccessSnapshot;
@@ -723,3 +725,148 @@ describe("GeneralSettingsPanel observability", () => {
725 await expect.element(page.getByPlaceholder("Optional")).toBeInTheDocument();
726 });
727 });
728 +
729 +describe("SourceControlSettingsPanel discovery states", () => {
730 + let mounted:
731 + | (Awaited<ReturnType<typeof render>> & {
732 + cleanup?: () => Promise<void>;
733 + unmount?: () => Promise<void>;
734 + })
735 + | null = null;
736 +
737 + beforeEach(async () => {
738 + resetAppAtomRegistryForTests();
739 + await __resetLocalApiForTests();
740 + document.body.innerHTML = "";
741 + });
742 +
743 + afterEach(async () => {
744 + if (mounted) {
745 + const teardown = mounted.cleanup ?? mounted.unmount;
746 + await teardown?.call(mounted).catch(() => {});
747 + }
748 + mounted = null;
749 + Reflect.deleteProperty(window, "nativeApi");
750 + document.body.innerHTML = "";
751 + await __resetLocalApiForTests();
752 + resetAppAtomRegistryForTests();
753 + });
754 +
755 + function setSourceControlDiscoveryStub(
756 + discoverSourceControl: () => Promise<SourceControlDiscoveryResult>,
757 + ) {
758 + window.nativeApi = {
759 + server: {
760 + discoverSourceControl,
761 + },
762 + } as LocalApi;
763 + }
764 +
765 + it("shows skeleton sections while the first source control scan is pending", async () => {
766 + setSourceControlDiscoveryStub(() => new Promise(() => {}));
767 +
768 + mounted = await render(
769 + <AppAtomRegistryProvider>
770 + <SourceControlSettingsPanel />
771 + </AppAtomRegistryProvider>,
772 + );
773 +
774 + await expect.element(page.getByText("Version Control")).toBeInTheDocument();
775 + await expect.element(page.getByText("Source Control Providers")).toBeInTheDocument();
776 + await expect
777 + .element(page.getByRole("button", { name: "Scan source control tools" }))
778 + .toBeDisabled();
779 + await expect.element(page.getByText("No source control tools found")).not.toBeInTheDocument();
780 + });
781 +
782 + it("uses the shared empty state when discovery completes without tools", async () => {
783 + setSourceControlDiscoveryStub(async () => ({
784 + versionControlSystems: [],
785 + sourceControlProviders: [],
786 + }));
787 +
788 + mounted = await render(
789 + <AppAtomRegistryProvider>
790 + <SourceControlSettingsPanel />
791 + </AppAtomRegistryProvider>,
792 + );
793 +
794 + await expect.element(page.getByText("No source control tools found")).toBeInTheDocument();
795 + await expect
796 + .element(page.getByText("Install a supported Git or pull request CLI, then scan again."))
797 + .toBeInTheDocument();
798 + await expect.element(page.getByRole("button", { name: "Scan" })).toBeInTheDocument();
799 + });
800 +
801 + it("keeps discovered rows instead of showing the empty state", async () => {
802 + setSourceControlDiscoveryStub(async () => ({
803 + versionControlSystems: [
804 + {
805 + kind: "git",
806 + label: "Git",
807 + executable: "git",
808 + implemented: true,
809 + status: "available",
810 + version: Option.some("git version 2.50.0"),
811 + installHint: "Install Git.",
812 + detail: Option.none(),
813 + },
814 + ],
815 + sourceControlProviders: [],
816 + }));
817 +
818 + mounted = await render(
819 + <AppAtomRegistryProvider>
820 + <SourceControlSettingsPanel />
821 + </AppAtomRegistryProvider>,
822 + );
823 +
824 + await expect.element(page.getByRole("heading", { name: "Git" })).toBeInTheDocument();
825 + await expect.element(page.getByText("No source control tools found")).not.toBeInTheDocument();
826 + });
827 +
828 + it("does not rescan on remount while the discovery atom is fresh", async () => {
829 + let calls = 0;
830 + setSourceControlDiscoveryStub(async () => {
831 + calls += 1;
832 + return {
833 + versionControlSystems: [
834 + {
835 + kind: "git",
836 + label: "Git",
837 + executable: "git",
838 + implemented: true,
839 + status: "available",
840 + version: Option.some("git version 2.50.0"),
841 + installHint: "Install Git.",
842 + detail: Option.none(),
843 + },
844 + ],
845 + sourceControlProviders: [],
846 + };
847 + });
848 +
849 + mounted = await render(
850 + <AppAtomRegistryProvider>
851 + <SourceControlSettingsPanel />
852 + </AppAtomRegistryProvider>,
853 + );
854 +
855 + await expect.element(page.getByRole("heading", { name: "Git" })).toBeInTheDocument();
856 + expect(calls).toBe(1);
857 +
858 + const teardown = mounted.cleanup ?? mounted.unmount;
859 + await teardown?.call(mounted).catch(() => {});
860 + mounted = null;
861 + document.body.innerHTML = "";
862 +
863 + mounted = await render(
864 + <AppAtomRegistryProvider>
865 + <SourceControlSettingsPanel />
866 + </AppAtomRegistryProvider>,
867 + );
868 +
869 + await expect.element(page.getByRole("heading", { name: "Git" })).toBeInTheDocument();
870 + expect(calls).toBe(1);
871 + });
872 +});
apps/web/src/components/settings/SettingsSidebarNav.tsx
+3 -1
@@ -1,5 +1,5 @@
1 import { useCallback, type ComponentType } from "react";
2 -import { ArchiveIcon, ArrowLeftIcon, Link2Icon, Settings2Icon } from "lucide-react";
2 +import { ArchiveIcon, ArrowLeftIcon, GitBranchIcon, Link2Icon, Settings2Icon } from "lucide-react";
3 import { useCanGoBack, useNavigate } from "@tanstack/react-router";
4
5 import {
@@ -15,6 +15,7 @@ import {
15
16 export type SettingsSectionPath =
17 | "/settings/general"
18 + | "/settings/source-control"
19 | "/settings/connections"
20 | "/settings/archived";
21
@@ -24,6 +25,7 @@ export const SETTINGS_NAV_ITEMS: ReadonlyArray<{
25 icon: ComponentType<{ className?: string }>;
26 }> = [
27 { label: "General", to: "/settings/general", icon: Settings2Icon },
28 + { label: "Source Control", to: "/settings/source-control", icon: GitBranchIcon },
29 { label: "Connections", to: "/settings/connections", icon: Link2Icon },
30 { label: "Archive", to: "/settings/archived", icon: ArchiveIcon },
31 ];
apps/web/src/components/settings/SourceControlSettings.tsx new
+471
@@ -0,0 +1,471 @@
1 +import { GitPullRequestIcon, RefreshCwIcon } from "lucide-react";
2 +import { Option } from "effect";
3 +import { type ReactNode, useId } from "react";
4 +import type {
5 + SourceControlProviderKind,
6 + SourceControlDiscoveryResult,
7 + SourceControlProviderAuth,
8 + SourceControlProviderDiscoveryItem,
9 + VcsDiscoveryItem,
10 +} from "@t3tools/contracts";
11 +
12 +import { cn } from "../../lib/utils";
13 +import {
14 + refreshSourceControlDiscovery,
15 + useSourceControlDiscovery,
16 +} from "../../lib/sourceControlDiscoveryState";
17 +import { Badge } from "../ui/badge";
18 +import { Button } from "../ui/button";
19 +import {
20 + Empty,
21 + EmptyContent,
22 + EmptyDescription,
23 + EmptyHeader,
24 + EmptyMedia,
25 + EmptyTitle,
26 +} from "../ui/empty";
27 +import { Skeleton } from "../ui/skeleton";
28 +import { Switch } from "../ui/switch";
29 +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
30 +import { GitHubIcon, type Icon } from "../Icons";
31 +import { RedactedSensitiveText } from "./RedactedSensitiveText";
32 +import { SettingsPageContainer, SettingsSection } from "./settingsLayout";
33 +
34 +const EMPTY_DISCOVERY_RESULT: SourceControlDiscoveryResult = {
35 + versionControlSystems: [],
36 + sourceControlProviders: [],
37 +};
38 +
39 +const GitLabIcon: Icon = (props) => (
40 + <svg {...props} viewBox="0 0 32 32" fill="none">
41 + <path
42 + d="m31.46 12.78-.04-.12-4.35-11.35A1.14 1.14 0 0 0 25.94.6c-.24 0-.47.1-.66.24-.19.15-.33.36-.39.6l-2.94 9h-11.9l-2.94-9A1.14 1.14 0 0 0 6.07.58a1.15 1.15 0 0 0-1.14.72L.58 12.68l-.05.11a8.1 8.1 0 0 0 2.68 9.34l.02.01.04.03 6.63 4.97 3.28 2.48 2 1.52a1.35 1.35 0 0 0 1.62 0l2-1.52 3.28-2.48 6.67-5h.02a8.09 8.09 0 0 0 2.7-9.36Z"
43 + fill="#E24329"
44 + />
45 + <path
46 + d="m31.46 12.78-.04-.12a14.75 14.75 0 0 0-5.86 2.64l-9.55 7.24 6.09 4.6 6.67-5h.02a8.09 8.09 0 0 0 2.67-9.36Z"
47 + fill="#FC6D26"
48 + />
49 + <path
50 + d="m9.9 27.14 3.28 2.48 2 1.52a1.35 1.35 0 0 0 1.62 0l2-1.52 3.28-2.48-6.1-4.6-6.07 4.6Z"
51 + fill="#FCA326"
52 + />
53 + <path
54 + d="M6.44 15.3a14.71 14.71 0 0 0-5.86-2.63l-.05.12a8.1 8.1 0 0 0 2.68 9.34l.02.01.04.03 6.63 4.97 6.1-4.6-9.56-7.24Z"
55 + fill="#FC6D26"
56 + />
57 + </svg>
58 +);
59 +
60 +const AzureDevOpsIcon: Icon = (props) => {
61 + const id = useId().replaceAll(":", "");
62 + const gradientA = `${id}-azure-a`;
63 + const gradientB = `${id}-azure-b`;
64 + const gradientC = `${id}-azure-c`;
65 +
66 + return (
67 + <svg {...props} viewBox="0 0 96 96">
68 + <defs>
69 + <linearGradient
70 + id={gradientA}
71 + x1="-1032.17"
72 + x2="-1059.21"
73 + y1="145.31"
74 + y2="65.43"
75 + gradientTransform="matrix(1 0 0 -1 1075 158)"
76 + gradientUnits="userSpaceOnUse"
77 + >
78 + <stop offset="0" stopColor="#114a8b" />
79 + <stop offset="1" stopColor="#0669bc" />
80 + </linearGradient>
81 + <linearGradient
82 + id={gradientB}
83 + x1="-1023.73"
84 + x2="-1029.98"
85 + y1="108.08"
86 + y2="105.97"
87 + gradientTransform="matrix(1 0 0 -1 1075 158)"
88 + gradientUnits="userSpaceOnUse"
89 + >
90 + <stop offset="0" stopOpacity=".3" />
91 + <stop offset=".07" stopOpacity=".2" />
92 + <stop offset=".32" stopOpacity=".1" />
93 + <stop offset=".62" stopOpacity=".05" />
94 + <stop offset="1" stopOpacity="0" />
95 + </linearGradient>
96 + <linearGradient
97 + id={gradientC}
98 + x1="-1027.16"
99 + x2="-997.48"
100 + y1="147.64"
101 + y2="68.56"
102 + gradientTransform="matrix(1 0 0 -1 1075 158)"
103 + gradientUnits="userSpaceOnUse"
104 + >
105 + <stop offset="0" stopColor="#3ccbf4" />
106 + <stop offset="1" stopColor="#2892df" />
107 + </linearGradient>
108 + </defs>
109 + <path
110 + fill={`url(#${gradientA})`}
111 + d="M33.34 6.54h26.04l-27.03 80.1a4.15 4.15 0 0 1-3.94 2.81H8.15a4.14 4.14 0 0 1-3.93-5.47L29.4 9.38a4.15 4.15 0 0 1 3.94-2.83z"
112 + />
113 + <path
114 + fill="#0078d4"
115 + d="M71.17 60.26H29.88a1.91 1.91 0 0 0-1.3 3.31l26.53 24.76a4.17 4.17 0 0 0 2.85 1.13h23.38z"
116 + />
117 + <path
118 + fill={`url(#${gradientB})`}
119 + d="M33.34 6.54a4.12 4.12 0 0 0-3.95 2.88L4.25 83.92a4.14 4.14 0 0 0 3.91 5.54h20.79a4.44 4.44 0 0 0 3.4-2.9l5.02-14.78 17.91 16.7a4.24 4.24 0 0 0 2.67.97h23.29L71.02 60.26H41.24L59.47 6.55z"
120 + />
121 + <path
122 + fill={`url(#${gradientC})`}
123 + d="M66.6 9.36a4.14 4.14 0 0 0-3.93-2.82H33.65a4.15 4.15 0 0 1 3.93 2.82l25.18 74.62a4.15 4.15 0 0 1-3.93 5.48h29.02a4.15 4.15 0 0 0 3.93-5.48z"
124 + />
125 + </svg>
126 + );
127 +};
128 +
129 +const BitbucketIcon: Icon = (props) => {
130 + const id = useId().replaceAll(":", "");
131 + const gradientId = `${id}-bitbucket-a`;
132 +
133 + return (
134 + <svg {...props} viewBox="8.4 14.39 2481.29 2231.21">
135 + <path fill="none" d="M989.97,1493.09h518.05l125.04-730.04H852.22L989.97,1493.09z" />
136 + <path
137 + fill="#2684FF"
138 + d="M88.92,14.4C45.02,13.83,8.97,48.96,8.41,92.86c-0.06,4.61,0.28,9.22,1.02,13.77l337.48,2048.72 c8.68,51.75,53.26,89.8,105.74,90.24h1619.03c39.38,0.5,73.19-27.9,79.49-66.78l337.49-2071.78c7.03-43.34-22.41-84.17-65.75-91.2 c-4.55-0.74-9.15-1.08-13.76-1.02L88.92,14.4z M1509.99,1495.09H993.24l-139.92-731h781.89L1509.99,1495.09z"
139 + />
140 + <linearGradient
141 + id={gradientId}
142 + gradientUnits="userSpaceOnUse"
143 + x1="945.1094"
144 + y1="1524.8389"
145 + x2="944.4923"
146 + y2="1524.1893"
147 + gradientTransform="matrix(1996.6343 0 0 -1480.3047 -1884485.625 2258195)"
148 + >
149 + <stop offset="0.18" stopColor="#0052CC" />
150 + <stop offset="1" stopColor="#2684FF" />
151 + </linearGradient>
152 + <path
153 + fill={`url(#${gradientId})`}
154 + d="M2379.27,763.06h-745.5l-125.12,730.42H992.31l-609.67,723.67c19.32,16.71,43.96,26,69.5,26.21h1618.13 c39.35,0.51,73.14-27.88,79.44-66.72L2379.27,763.06z"
155 + />
156 + </svg>
157 + );
158 +};
159 +
160 +const SOURCE_CONTROL_PROVIDER_ICONS: Partial<Record<SourceControlProviderKind, Icon>> = {
161 + github: GitHubIcon,
162 + gitlab: GitLabIcon,
163 + "azure-devops": AzureDevOpsIcon,
164 + bitbucket: BitbucketIcon,
165 +};
166 +
167 +function optionLabel(value: Option.Option<string>): string | null {
168 + return Option.getOrNull(value);
169 +}
170 +
171 +function isProviderDiscoveryItem(
172 + item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem,
173 +): item is SourceControlProviderDiscoveryItem {
174 + return "auth" in item;
175 +}
176 +
177 +function authPresentation(auth: SourceControlProviderAuth): {
178 + readonly label: string;
179 + readonly badge: "warning" | null;
180 +} {
181 + if (auth.status === "authenticated") {
182 + return { label: "Signed in", badge: null };
183 + }
184 + if (auth.status === "unauthenticated") {
185 + return { label: "Sign in", badge: "warning" };
186 + }
187 + return { label: "Sign in", badge: null };
188 +}
189 +
190 +function RedactedAccount(props: { readonly account: string | null }) {
191 + return (
192 + <RedactedSensitiveText
193 + value={props.account}
194 + ariaLabel="Toggle source control account visibility"
195 + revealTooltip="Click to reveal account"
196 + hideTooltip="Click to hide account"
197 + />
198 + );
199 +}
200 +
201 +function itemStatusDot(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): string {
202 + if (!item.implemented) return "bg-muted-foreground/35";
203 + if (item.status !== "available") return "bg-warning";
204 + if (isProviderDiscoveryItem(item) && item.auth.status !== "authenticated") return "bg-warning";
205 + return "bg-success";
206 +}
207 +
208 +function SourceControlItemMark({
209 + item,
210 +}: {
211 + readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
212 +}) {
213 + const dotClassName = itemStatusDot(item);
214 + const Icon = isProviderDiscoveryItem(item) ? SOURCE_CONTROL_PROVIDER_ICONS[item.kind] : null;
215 +
216 + if (!Icon) {
217 + return <span className={cn("size-2 shrink-0 rounded-full", dotClassName)} aria-hidden />;
218 + }
219 +
220 + return (
221 + <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
222 + <Icon className="size-4.5 text-foreground/80" aria-hidden />
223 + <span
224 + className={cn(
225 + "pointer-events-none absolute -left-0.5 -top-0.5 size-2 rounded-full ring-2 ring-background",
226 + dotClassName,
227 + )}
228 + aria-hidden
229 + />
230 + </span>
231 + );
232 +}
233 +
234 +function itemSummary({
235 + item,
236 + auth,
237 + authAccount,
238 +}: {
239 + readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
240 + readonly auth: SourceControlProviderAuth | null;
241 + readonly authAccount: string | null;
242 +}) {
243 + if (!item.implemented) {
244 + return <span>Support for {item.label} is coming soon.</span>;
245 + }
246 +
247 + if (item.status !== "available") {
248 + return <span>Not found - {item.installHint}</span>;
249 + }
250 +
251 + if (auth) {
252 + if (auth.status === "authenticated") {
253 + return (
254 + <>
255 + <span>Authenticated</span>
256 + {authAccount ? (
257 + <>
258 + <span aria-hidden>as</span>
259 + <RedactedAccount account={authAccount} />
260 + </>
261 + ) : null}
262 + </>
263 + );
264 + }
265 + if (auth.status === "unauthenticated") {
266 + return <span>Sign in with the {item.executable} CLI to enable pull request actions.</span>;
267 + }
268 + return (
269 + <span>
270 + Install and sign in with the {item.executable} CLI to enable pull request actions.
271 + </span>
272 + );
273 + }
274 +
275 + return <span>Available</span>;
276 +}
277 +
278 +function DiscoveryItemRow({
279 + item,
280 +}: {
281 + readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
282 +}) {
283 + const version = optionLabel(item.version);
284 + const enabled = item.implemented && item.status === "available";
285 + const auth = isProviderDiscoveryItem(item) ? item.auth : null;
286 + const authStatus = auth ? authPresentation(auth) : null;
287 + const authAccount = auth ? optionLabel(auth.account) : null;
288 +
289 + return (
290 + <div
291 + className={cn(
292 + "border-t border-border/60 first:border-t-0",
293 + !item.implemented && "opacity-80",
294 + )}
295 + >
296 + <div className="px-4 py-3.5 sm:px-5">
297 + <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
298 + <div className="min-w-0 flex-1 space-y-1">
299 + <div className="flex min-w-0 flex-wrap items-center gap-2">
300 + <SourceControlItemMark item={item} />
301 + <h3 className="truncate text-[13px] font-semibold tracking-[-0.01em] text-foreground">
302 + {item.label}
303 + </h3>
304 + {version ? <code className="text-xs text-muted-foreground">{version}</code> : null}
305 + {!item.implemented ? (
306 + <Badge variant="warning" size="sm">
307 + Coming Soon
308 + </Badge>
309 + ) : null}
310 + {authStatus?.badge ? (
311 + <Badge variant={authStatus.badge} size="sm">
312 + {authStatus.label}
313 + </Badge>
314 + ) : null}
315 + </div>
316 + <p className="flex min-w-0 flex-wrap items-center gap-x-1 text-xs text-muted-foreground">
317 + {itemSummary({ item, auth, authAccount })}
318 + </p>
319 + </div>
320 + <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
321 + {item.implemented ? (
322 + <Switch checked={enabled} disabled aria-label={`${item.label} availability`} />
323 + ) : null}
324 + </div>
325 + </div>
326 + </div>
327 + </div>
328 + );
329 +}
330 +
331 +function SourceControlSectionSkeleton({
332 + title,
333 + headerAction,
334 +}: {
335 + readonly title: string;
336 + readonly headerAction?: ReactNode;
337 +}) {
338 + return (
339 + <SettingsSection title={title} headerAction={headerAction}>
340 + {Array.from({ length: 2 }, (_, index) => (
341 + <div key={index} className="border-t border-border/60 px-4 py-3.5 first:border-t-0 sm:px-5">
342 + <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
343 + <div className="min-w-0 flex-1 space-y-2">
344 + <div className="flex items-center gap-2">
345 + <Skeleton className="size-2 rounded-full" />
346 + <Skeleton className="h-4 w-28 rounded-full" />
347 + <Skeleton className="h-5 w-14 rounded-full" />
348 + </div>
349 + <Skeleton className="h-3 w-full max-w-xs rounded-full" />
350 + </div>
351 + <div className="flex shrink-0 items-center gap-2">
352 + <Skeleton className="size-7 rounded-md" />
353 + <Skeleton className="h-5 w-9 rounded-full" />
354 + </div>
355 + </div>
356 + </div>
357 + ))}
358 + </SettingsSection>
359 + );
360 +}
361 +
362 +function EmptySourceControlDiscovery({
363 + error,
364 + isPending,
365 + onScan,
366 +}: {
367 + readonly error: string | null;
368 + readonly isPending: boolean;
369 + readonly onScan: () => void;
370 +}) {
371 + const hasError = error !== null;
372 +
373 + return (
374 + <SettingsSection title="Detected tools">
375 + <Empty className="min-h-88">
376 + <EmptyMedia variant="icon">
377 + <GitPullRequestIcon />
378 + </EmptyMedia>
379 + <EmptyHeader>
380 + <EmptyTitle>
381 + {hasError ? "Could not scan source control" : "No source control tools found"}
382 + </EmptyTitle>
383 + <EmptyDescription>
384 + {hasError ? error : "Install a supported Git or pull request CLI, then scan again."}
385 + </EmptyDescription>
386 + </EmptyHeader>
387 + <EmptyContent>
388 + <Button
389 + size="sm"
390 + variant="outline"
391 + className="h-8 gap-1.5 px-3 text-xs"
392 + onClick={onScan}
393 + disabled={isPending}
394 + >
395 + <RefreshCwIcon className={cn("size-3.5", isPending && "animate-spin")} />
396 + Scan
397 + </Button>
398 + </EmptyContent>
399 + </Empty>
400 + </SettingsSection>
401 + );
402 +}
403 +
404 +export function SourceControlSettingsPanel() {
405 + const discovery = useSourceControlDiscovery();
406 +
407 + const result = discovery.data ?? EMPTY_DISCOVERY_RESULT;
408 + const hasDiscoveryItems =
409 + result.versionControlSystems.length > 0 || result.sourceControlProviders.length > 0;
410 + const isInitialScanPending = discovery.isPending && discovery.data === null;
411 + const handleScan = () => {
412 + void refreshSourceControlDiscovery();
413 + };
414 + const scanButton = (
415 + <Tooltip>
416 + <TooltipTrigger
417 + render={
418 + <Button
419 + size="icon-xs"
420 + variant="ghost"
421 + className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
422 + onClick={handleScan}
423 + disabled={discovery.isPending}
424 + aria-label="Scan source control tools"
425 + >
426 + <RefreshCwIcon className={cn("size-3", discovery.isPending && "animate-spin")} />
427 + </Button>
428 + }
429 + />
430 + <TooltipPopup side="top">Scan source control tools</TooltipPopup>
431 + </Tooltip>
432 + );
433 +
434 + return (
435 + <SettingsPageContainer>
436 + {isInitialScanPending ? (
437 + <>
438 + <SourceControlSectionSkeleton title="Version Control" headerAction={scanButton} />
439 + <SourceControlSectionSkeleton title="Source Control Providers" />
440 + </>
441 + ) : hasDiscoveryItems ? (
442 + <>
443 + {result.versionControlSystems.length > 0 ? (
444 + <SettingsSection title="Version Control" headerAction={scanButton}>
445 + {result.versionControlSystems.map((item) => (
446 + <DiscoveryItemRow key={`vcs:${item.kind}`} item={item} />
447 + ))}
448 + </SettingsSection>
449 + ) : null}
450 +
451 + {result.sourceControlProviders.length > 0 ? (
452 + <SettingsSection
453 + title="Source Control Providers"
454 + headerAction={result.versionControlSystems.length === 0 ? scanButton : null}
455 + >
456 + {result.sourceControlProviders.map((item) => (
457 + <DiscoveryItemRow key={`provider:${item.kind}`} item={item} />
458 + ))}
459 + </SettingsSection>
460 + ) : null}
461 + </>
462 + ) : (
463 + <EmptySourceControlDiscovery
464 + error={discovery.error}
465 + isPending={discovery.isPending}
466 + onScan={handleScan}
467 + />
468 + )}
469 + </SettingsPageContainer>
470 + );
471 +}
apps/web/src/components/settings/settingsLayout.tsx
+2 -2
@@ -28,13 +28,13 @@ export function SettingsSection({
28 }) {
29 return (
30 <section className="space-y-2.5">
31 - <div className="flex items-center justify-between px-1">
31 + <div className="flex h-5 items-center justify-between px-1">
32 <h2 className="flex items-center gap-2 text-[11px] font-semibold uppercase tracking-[0.08em] text-foreground/50">
33 <span className="inline-block h-px w-3 bg-border" aria-hidden />
34 {icon}
35 {title}
36 </h2>
37 - {headerAction}
37 + <div className="flex h-5 min-w-5 items-center justify-end">{headerAction}</div>
38 </div>
39 <div className="relative overflow-hidden rounded-2xl border bg-card text-card-foreground shadow-sm/4 not-dark:bg-clip-padding before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-2xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] dark:shadow-none dark:before:shadow-[0_-1px_--theme(--color-white/6%)]">
40 {children}
apps/web/src/environmentApi.ts
+11 -9
@@ -23,16 +23,18 @@ export function createEnvironmentApi(rpcClient: WsRpcClient): EnvironmentApi {
23 filesystem: {
24 browse: rpcClient.filesystem.browse,
25 },
26 + vcs: {
27 + pull: rpcClient.vcs.pull,
28 + refreshStatus: rpcClient.vcs.refreshStatus,
29 + onStatus: (input, callback, options) => rpcClient.vcs.onStatus(input, callback, options),
30 + listRefs: rpcClient.vcs.listRefs,
31 + createWorktree: rpcClient.vcs.createWorktree,
32 + removeWorktree: rpcClient.vcs.removeWorktree,
33 + createRef: rpcClient.vcs.createRef,
34 + switchRef: rpcClient.vcs.switchRef,
35 + init: rpcClient.vcs.init,
36 + },
37 git: {
27 - pull: rpcClient.git.pull,
28 - refreshStatus: rpcClient.git.refreshStatus,
29 - onStatus: (input, callback, options) => rpcClient.git.onStatus(input, callback, options),
30 - listBranches: rpcClient.git.listBranches,
31 - createWorktree: rpcClient.git.createWorktree,
32 - removeWorktree: rpcClient.git.removeWorktree,
33 - createBranch: rpcClient.git.createBranch,
34 - checkout: rpcClient.git.checkout,
35 - init: rpcClient.git.init,
38 resolvePullRequest: rpcClient.git.resolvePullRequest,
39 preparePullRequestThread: rpcClient.git.preparePullRequestThread,
40 },
apps/web/src/environments/runtime/connection.test.ts
-9
@@ -85,16 +85,7 @@ function createTestClient() {
85 openInEditor: vi.fn(async () => undefined),
86 },
87 git: {
88 - pull: vi.fn(async () => undefined),
89 - refreshStatus: vi.fn(async () => undefined),
90 - onStatus: vi.fn(() => () => undefined),
88 runStackedAction: vi.fn(async () => ({}) as any),
92 - listBranches: vi.fn(async () => []),
93 - createWorktree: vi.fn(async () => undefined),
94 - removeWorktree: vi.fn(async () => undefined),
95 - createBranch: vi.fn(async () => undefined),
96 - checkout: vi.fn(async () => undefined),
97 - init: vi.fn(async () => undefined),
89 resolvePullRequest: vi.fn(async () => undefined),
90 preparePullRequestThread: vi.fn(async () => undefined),
91 },
apps/web/src/hooks/useThreadActions.ts
+1 -1
@@ -209,7 +209,7 @@ export function useThreadActions() {
209 }
210
211 try {
212 - await ensureEnvironmentApi(threadRef.environmentId).git.removeWorktree({
212 + await ensureEnvironmentApi(threadRef.environmentId).vcs.removeWorktree({
213 cwd: threadProject.cwd,
214 path: orphanedWorktreePath,
215 force: true,
apps/web/src/lib/gitReactQuery.test.ts
+5 -5
@@ -11,7 +11,7 @@ vi.mock("../wsRpcClient", () => ({
11 }));
12
13 import type { InfiniteData } from "@tanstack/react-query";
14 -import { EnvironmentId, type GitListBranchesResult } from "@t3tools/contracts";
14 +import { EnvironmentId, type VcsListRefsResult } from "@t3tools/contracts";
15
16 import {
17 gitBranchSearchInfiniteQueryOptions,
@@ -22,15 +22,15 @@ import {
22 invalidateGitQueries,
23 } from "./gitReactQuery";
24
25 -const BRANCH_QUERY_RESULT: GitListBranchesResult = {
26 - branches: [],
25 +const BRANCH_QUERY_RESULT: VcsListRefsResult = {
26 + refs: [],
27 isRepo: true,
28 - hasOriginRemote: true,
28 + hasPrimaryRemote: true,
29 nextCursor: null,
30 totalCount: 0,
31 };
32
33 -const BRANCH_SEARCH_RESULT: InfiniteData<GitListBranchesResult, number> = {
33 +const BRANCH_SEARCH_RESULT: InfiniteData<VcsListRefsResult, number> = {
34 pages: [BRANCH_QUERY_RESULT],
35 pageParams: [0],
36 };
apps/web/src/lib/gitReactQuery.ts
+38 -20
@@ -19,17 +19,17 @@ const GIT_BRANCHES_PAGE_SIZE = 100;
19
20 export const gitQueryKeys = {
21 all: ["git"] as const,
22 - branches: (environmentId: EnvironmentId | null, cwd: string | null) =>
23 - ["git", "branches", environmentId ?? null, cwd] as const,
22 + refs: (environmentId: EnvironmentId | null, cwd: string | null) =>
23 + ["git", "refs", environmentId ?? null, cwd] as const,
24 branchSearch: (environmentId: EnvironmentId | null, cwd: string | null, query: string) =>
25 - ["git", "branches", environmentId ?? null, cwd, "search", query] as const,
25 + ["git", "refs", environmentId ?? null, cwd, "search", query] as const,
26 };
27
28 export const gitMutationKeys = {
29 init: (environmentId: EnvironmentId | null, cwd: string | null) =>
30 ["git", "mutation", "init", environmentId ?? null, cwd] as const,
31 - checkout: (environmentId: EnvironmentId | null, cwd: string | null) =>
32 - ["git", "mutation", "checkout", environmentId ?? null, cwd] as const,
31 + switchRef: (environmentId: EnvironmentId | null, cwd: string | null) =>
32 + ["git", "mutation", "switchRef", environmentId ?? null, cwd] as const,
33 runStackedAction: (environmentId: EnvironmentId | null, cwd: string | null) =>
34 ["git", "mutation", "run-stacked-action", environmentId ?? null, cwd] as const,
35 pull: (environmentId: EnvironmentId | null, cwd: string | null) =>
@@ -45,7 +45,7 @@ export function invalidateGitQueries(
45 const environmentId = input?.environmentId ?? null;
46 const cwd = input?.cwd ?? null;
47 if (cwd !== null) {
48 - return queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(environmentId, cwd) });
48 + return queryClient.invalidateQueries({ queryKey: gitQueryKeys.refs(environmentId, cwd) });
49 }
50
51 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
@@ -60,9 +60,12 @@ function invalidateGitBranchQueries(
60 return Promise.resolve();
61 }
62
63 - return queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(environmentId, cwd) });
63 + return queryClient.invalidateQueries({ queryKey: gitQueryKeys.refs(environmentId, cwd) });
64 }
65
66 +/**
67 + * @deprecated Use a VCS-named query helper once the UI naming migration lands.
68 + */
69 export function gitBranchSearchInfiniteQueryOptions(input: {
70 environmentId: EnvironmentId | null;
71 cwd: string | null;
@@ -75,10 +78,10 @@ export function gitBranchSearchInfiniteQueryOptions(input: {
78 queryKey: gitQueryKeys.branchSearch(input.environmentId, input.cwd, normalizedQuery),
79 initialPageParam: 0,
80 queryFn: async ({ pageParam }) => {
78 - if (!input.cwd) throw new Error("Git branches are unavailable.");
79 - if (!input.environmentId) throw new Error("Git branches are unavailable.");
81 + if (!input.cwd) throw new Error("Git refs are unavailable.");
82 + if (!input.environmentId) throw new Error("Git refs are unavailable.");
83 const api = ensureEnvironmentApi(input.environmentId);
81 - return api.git.listBranches({
84 + return api.vcs.listRefs({
85 cwd: input.cwd,
86 ...(normalizedQuery.length > 0 ? { query: normalizedQuery } : {}),
87 cursor: pageParam,
@@ -121,6 +124,9 @@ export function gitResolvePullRequestQueryOptions(input: {
124 });
125 }
126
127 +/**
128 + * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
129 + */
130 export function gitInitMutationOptions(input: {
131 environmentId: EnvironmentId | null;
132 cwd: string | null;
@@ -131,7 +137,7 @@ export function gitInitMutationOptions(input: {
137 mutationFn: async () => {
138 if (!input.cwd || !input.environmentId) throw new Error("Git init is unavailable.");
139 const api = ensureEnvironmentApi(input.environmentId);
134 - return api.git.init({ cwd: input.cwd });
140 + return api.vcs.init({ cwd: input.cwd });
141 },
142 onSettled: async () => {
143 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
@@ -139,17 +145,20 @@ export function gitInitMutationOptions(input: {
145 });
146 }
147
148 +/**
149 + * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
150 + */
151 export function gitCheckoutMutationOptions(input: {
152 environmentId: EnvironmentId | null;
153 cwd: string | null;
154 queryClient: QueryClient;
155 }) {
156 return mutationOptions({
148 - mutationKey: gitMutationKeys.checkout(input.environmentId, input.cwd),
149 - mutationFn: async (branch: string) => {
150 - if (!input.cwd || !input.environmentId) throw new Error("Git checkout is unavailable.");
157 + mutationKey: gitMutationKeys.switchRef(input.environmentId, input.cwd),
158 + mutationFn: async (refName: string) => {
159 + if (!input.cwd || !input.environmentId) throw new Error("Git switchRef is unavailable.");
160 const api = ensureEnvironmentApi(input.environmentId);
152 - return api.git.checkout({ cwd: input.cwd, branch });
161 + return api.vcs.switchRef({ cwd: input.cwd, refName });
162 },
163 onSettled: async () => {
164 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
@@ -198,6 +207,9 @@ export function gitRunStackedActionMutationOptions(input: {
207 });
208 }
209
210 +/**
211 + * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
212 + */
213 export function gitPullMutationOptions(input: {
214 environmentId: EnvironmentId | null;
215 cwd: string | null;
@@ -208,7 +220,7 @@ export function gitPullMutationOptions(input: {
220 mutationFn: async () => {
221 if (!input.cwd || !input.environmentId) throw new Error("Git pull is unavailable.");
222 const api = ensureEnvironmentApi(input.environmentId);
211 - return api.git.pull({ cwd: input.cwd });
223 + return api.vcs.pull({ cwd: input.cwd });
224 },
225 onSuccess: async () => {
226 await invalidateGitBranchQueries(input.queryClient, input.environmentId, input.cwd);
@@ -216,6 +228,9 @@ export function gitPullMutationOptions(input: {
228 });
229 }
230
231 +/**
232 + * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
233 + */
234 export function gitCreateWorktreeMutationOptions(input: {
235 environmentId: EnvironmentId | null;
236 queryClient: QueryClient;
@@ -223,12 +238,12 @@ export function gitCreateWorktreeMutationOptions(input: {
238 return mutationOptions({
239 mutationKey: ["git", "mutation", "create-worktree", input.environmentId ?? null] as const,
240 mutationFn: (
226 - args: Parameters<ReturnType<typeof ensureEnvironmentApi>["git"]["createWorktree"]>[0],
241 + args: Parameters<ReturnType<typeof ensureEnvironmentApi>["vcs"]["createWorktree"]>[0],
242 ) => {
243 if (!input.environmentId) {
244 throw new Error("Worktree creation is unavailable.");
245 }
231 - return ensureEnvironmentApi(input.environmentId).git.createWorktree(args);
246 + return ensureEnvironmentApi(input.environmentId).vcs.createWorktree(args);
247 },
248 onSuccess: async () => {
249 await invalidateGitQueries(input.queryClient, { environmentId: input.environmentId });
@@ -236,6 +251,9 @@ export function gitCreateWorktreeMutationOptions(input: {
251 });
252 }
253
254 +/**
255 + * @deprecated Use a VCS-named mutation helper once the UI naming migration lands.
256 + */
257 export function gitRemoveWorktreeMutationOptions(input: {
258 environmentId: EnvironmentId | null;
259 queryClient: QueryClient;
@@ -243,12 +261,12 @@ export function gitRemoveWorktreeMutationOptions(input: {
261 return mutationOptions({
262 mutationKey: ["git", "mutation", "remove-worktree", input.environmentId ?? null] as const,
263 mutationFn: (
246 - args: Parameters<ReturnType<typeof ensureEnvironmentApi>["git"]["removeWorktree"]>[0],
264 + args: Parameters<ReturnType<typeof ensureEnvironmentApi>["vcs"]["removeWorktree"]>[0],
265 ) => {
266 if (!input.environmentId) {
267 throw new Error("Worktree removal is unavailable.");
268 }
251 - return ensureEnvironmentApi(input.environmentId).git.removeWorktree(args);
269 + return ensureEnvironmentApi(input.environmentId).vcs.removeWorktree(args);
270 },
271 onSuccess: async () => {
272 await invalidateGitQueries(input.queryClient, { environmentId: input.environmentId });
apps/web/src/lib/gitStatusState.test.ts
+31 -29
@@ -1,4 +1,4 @@
1 -import { EnvironmentId, type GitStatusResult } from "@t3tools/contracts";
1 +import { EnvironmentId, type VcsStatusResult } from "@t3tools/contracts";
2 import { afterEach, describe, expect, it, vi } from "vitest";
3
4 import type { WsRpcClient } from "../rpc/wsRpcClient";
@@ -33,17 +33,17 @@ function registerListener<T>(listeners: Set<(event: T) => void>, listener: (even
33 };
34 }
35
36 -const gitStatusListeners = new Set<(event: GitStatusResult) => void>();
36 +const gitStatusListeners = new Set<(event: VcsStatusResult) => void>();
37 const ENVIRONMENT_ID = EnvironmentId.make("environment-local");
38 const OTHER_ENVIRONMENT_ID = EnvironmentId.make("environment-remote");
39 const TARGET = { environmentId: ENVIRONMENT_ID, cwd: "/repo" } as const;
40 const FRESH_TARGET = { environmentId: ENVIRONMENT_ID, cwd: "/fresh" } as const;
41
42 -const BASE_STATUS: GitStatusResult = {
42 +const BASE_STATUS: VcsStatusResult = {
43 isRepo: true,
44 - hasOriginRemote: true,
45 - isDefaultBranch: false,
46 - branch: "feature/push-status",
44 + hasPrimaryRemote: true,
45 + isDefaultRef: false,
46 + refName: "feature/push-status",
47 hasWorkingTreeChanges: false,
48 workingTree: { files: [], insertions: 0, deletions: 0 },
49 hasUpstream: true,
@@ -55,21 +55,21 @@ const BASE_STATUS: GitStatusResult = {
55 const gitClient = {
56 refreshStatus: vi.fn(async (input: { cwd: string }) => ({
57 ...BASE_STATUS,
58 - branch: `${input.cwd}-refreshed`,
58 + refName: `${input.cwd}-refreshed`,
59 })),
60 - onStatus: vi.fn((input: { cwd: string }, listener: (event: GitStatusResult) => void) =>
60 + onStatus: vi.fn((input: { cwd: string }, listener: (event: VcsStatusResult) => void) =>
61 registerListener(gitStatusListeners, listener),
62 ),
63 };
64
65 -function emitGitStatus(event: GitStatusResult) {
65 +function emitGitStatus(event: VcsStatusResult) {
66 for (const listener of gitStatusListeners) {
67 listener(event);
68 }
69 }
70
71 function createRegisteredGitStatusClient(environmentId: EnvironmentId) {
72 - const listeners = new Set<(event: GitStatusResult) => void>();
72 + const listeners = new Set<(event: VcsStatusResult) => void>();
73 const client = {
74 dispose: vi.fn(async () => undefined),
75 reconnect: vi.fn(async () => undefined),
@@ -89,22 +89,24 @@ function createRegisteredGitStatusClient(environmentId: EnvironmentId) {
89 shell: {
90 openInEditor: vi.fn(async () => undefined),
91 },
92 - git: {
92 + vcs: {
93 pull: vi.fn(async () => undefined),
94 refreshStatus: vi.fn(async (input: { cwd: string }) => ({
95 ...BASE_STATUS,
96 - branch: `${input.cwd}-refreshed`,
96 + refName: `${input.cwd}-refreshed`,
97 })),
98 - onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
98 + onStatus: vi.fn((_: { cwd: string }, listener: (event: VcsStatusResult) => void) =>
99 registerListener(listeners, listener),
100 ),
101 - runStackedAction: vi.fn(async () => ({}) as any),
102 - listBranches: vi.fn(async () => []),
101 + listRefs: vi.fn(async () => []),
102 createWorktree: vi.fn(async () => undefined),
103 removeWorktree: vi.fn(async () => undefined),
105 - createBranch: vi.fn(async () => undefined),
106 - checkout: vi.fn(async () => undefined),
104 + createRef: vi.fn(async () => undefined),
105 + switchRef: vi.fn(async () => undefined),
106 init: vi.fn(async () => undefined),
107 + },
108 + git: {
109 + runStackedAction: vi.fn(async () => ({}) as any),
110 resolvePullRequest: vi.fn(async () => undefined),
111 preparePullRequestThread: vi.fn(async () => undefined),
112 },
@@ -155,7 +157,7 @@ function createRegisteredGitStatusClient(environmentId: EnvironmentId) {
157
158 return {
159 client,
158 - emit: (event: GitStatusResult) => {
160 + emit: (event: VcsStatusResult) => {
161 for (const listener of listeners) {
162 listener(event);
163 }
@@ -219,7 +221,7 @@ describe("gitStatusState", () => {
221
222 expect(gitClient.onStatus).toHaveBeenCalledOnce();
223 expect(gitClient.refreshStatus).toHaveBeenCalledWith({ cwd: "/repo" });
222 - expect(refreshed).toEqual({ ...BASE_STATUS, branch: "/repo-refreshed" });
224 + expect(refreshed).toEqual({ ...BASE_STATUS, refName: "/repo-refreshed" });
225 expect(getGitStatusSnapshot(TARGET)).toEqual({
226 data: BASE_STATUS,
227 error: null,
@@ -231,17 +233,17 @@ describe("gitStatusState", () => {
233 });
234
235 it("keeps git status subscriptions isolated by environment when cwds match", () => {
234 - const localListeners = new Set<(event: GitStatusResult) => void>();
235 - const remoteListeners = new Set<(event: GitStatusResult) => void>();
236 + const localListeners = new Set<(event: VcsStatusResult) => void>();
237 + const remoteListeners = new Set<(event: VcsStatusResult) => void>();
238 const localClient = {
239 refreshStatus: vi.fn(),
238 - onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
240 + onStatus: vi.fn((_: { cwd: string }, listener: (event: VcsStatusResult) => void) =>
241 registerListener(localListeners, listener),
242 ),
243 };
244 const remoteClient = {
245 refreshStatus: vi.fn(),
244 - onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
246 + onStatus: vi.fn((_: { cwd: string }, listener: (event: VcsStatusResult) => void) =>
247 registerListener(remoteListeners, listener),
248 ),
249 };
@@ -254,11 +256,11 @@ describe("gitStatusState", () => {
256 listener(BASE_STATUS);
257 }
258 for (const listener of remoteListeners) {
257 - listener({ ...BASE_STATUS, branch: "remote-branch" });
259 + listener({ ...BASE_STATUS, refName: "remote-refName" });
260 }
261
260 - expect(getGitStatusSnapshot(TARGET).data?.branch).toBe("feature/push-status");
261 - expect(getGitStatusSnapshot(remoteTarget).data?.branch).toBe("remote-branch");
262 + expect(getGitStatusSnapshot(TARGET).data?.refName).toBe("feature/push-status");
263 + expect(getGitStatusSnapshot(remoteTarget).data?.refName).toBe("remote-refName");
264
265 releaseLocal();
266 releaseRemote();
@@ -292,7 +294,7 @@ describe("gitStatusState", () => {
294 const release = watchGitStatus(TARGET);
295
296 firstClient.emit(BASE_STATUS);
295 - expect(getGitStatusSnapshot(TARGET).data?.branch).toBe("feature/push-status");
297 + expect(getGitStatusSnapshot(TARGET).data?.refName).toBe("feature/push-status");
298
299 serviceHarness.connections.delete(ENVIRONMENT_ID);
300 for (const listener of serviceHarness.listeners) {
@@ -307,10 +309,10 @@ describe("gitStatusState", () => {
309 });
310
311 const secondClient = createRegisteredGitStatusClient(ENVIRONMENT_ID);
310 - secondClient.emit({ ...BASE_STATUS, branch: "reconnected-branch" });
312 + secondClient.emit({ ...BASE_STATUS, refName: "reconnected-refName" });
313
314 expect(getGitStatusSnapshot(TARGET)).toEqual({
313 - data: { ...BASE_STATUS, branch: "reconnected-branch" },
315 + data: { ...BASE_STATUS, refName: "reconnected-refName" },
316 error: null,
317 cause: null,
318 isPending: false,
apps/web/src/lib/gitStatusState.ts
+7 -7
@@ -2,7 +2,7 @@ import { useAtomValue } from "@effect/atom-react";
2 import {
3 type EnvironmentId,
4 type GitManagerServiceError,
5 - type GitStatusResult,
5 + type VcsStatusResult,
6 } from "@t3tools/contracts";
7 import { Cause } from "effect";
8 import { Atom } from "effect/unstable/reactivity";
@@ -16,13 +16,13 @@ import {
16 import type { WsRpcClient } from "~/rpc/wsRpcClient";
17
18 interface GitStatusState {
19 - readonly data: GitStatusResult | null;
19 + readonly data: VcsStatusResult | null;
20 readonly error: GitManagerServiceError | null;
21 readonly cause: Cause.Cause<GitManagerServiceError> | null;
22 readonly isPending: boolean;
23 }
24
25 -type GitStatusClient = Pick<WsRpcClient["git"], "onStatus" | "refreshStatus">;
25 +type GitStatusClient = Pick<WsRpcClient["vcs"], "onStatus" | "refreshStatus">;
26 interface ResolvedGitStatusClient {
27 readonly clientIdentity: string;
28 readonly client: GitStatusClient;
@@ -56,7 +56,7 @@ const EMPTY_GIT_STATUS_ATOM = Atom.make(EMPTY_GIT_STATUS_STATE).pipe(
56 const NOOP: () => void = () => undefined;
57 const watchedGitStatuses = new Map<string, WatchedGitStatus>();
58 const knownGitStatusKeys = new Set<string>();
59 -const gitStatusRefreshInFlight = new Map<string, Promise<GitStatusResult>>();
59 +const gitStatusRefreshInFlight = new Map<string, Promise<VcsStatusResult>>();
60 const gitStatusLastRefreshAtByKey = new Map<string, number>();
61
62 const GIT_STATUS_REFRESH_DEBOUNCE_MS = 1_000;
@@ -83,7 +83,7 @@ function readResolvedGitStatusClient(target: GitStatusTarget): ResolvedGitStatus
83 }
84 const connection = readEnvironmentConnection(target.environmentId);
85 return connection
86 - ? { clientIdentity: connection.environmentId, client: connection.client.git }
86 + ? { clientIdentity: connection.environmentId, client: connection.client.vcs }
87 : null;
88 }
89
@@ -119,7 +119,7 @@ export function watchGitStatus(target: GitStatusTarget, client?: GitStatusClient
119 export function refreshGitStatus(
120 target: GitStatusTarget,
121 client?: GitStatusClient,
122 -): Promise<GitStatusResult | null> {
122 +): Promise<VcsStatusResult | null> {
123 const targetKey = getGitStatusTargetKey(target);
124 if (targetKey === null || target.cwd === null) {
125 return Promise.resolve(null);
@@ -245,7 +245,7 @@ function subscribeToGitStatus(targetKey: string, cwd: string, client: GitStatusC
245 markGitStatusPending(targetKey);
246 return client.onStatus(
247 { cwd },
248 - (status: GitStatusResult) => {
248 + (status: VcsStatusResult) => {
249 appAtomRegistry.set(gitStatusStateAtom(targetKey), {
250 data: status,
251 error: null,
apps/web/src/lib/sourceControlDiscoveryState.ts new
+56
@@ -0,0 +1,56 @@
1 +import { useAtomValue } from "@effect/atom-react";
2 +import {
3 + EMPTY_SOURCE_CONTROL_DISCOVERY_ATOM,
4 + EMPTY_SOURCE_CONTROL_DISCOVERY_STATE,
5 + type SourceControlDiscoveryState,
6 + createSourceControlDiscoveryManager,
7 + getSourceControlDiscoveryTargetKey,
8 + sourceControlDiscoveryStateAtom,
9 +} from "@t3tools/client-runtime";
10 +import type { SourceControlDiscoveryResult } from "@t3tools/contracts";
11 +import { Effect } from "effect";
12 +import { Atom } from "effect/unstable/reactivity";
13 +
14 +import { readLocalApi } from "../localApi";
15 +import { appAtomRegistry } from "../rpc/atomRegistry";
16 +
17 +const SOURCE_CONTROL_DISCOVERY_TARGET = { key: "primary" } as const;
18 +const SOURCE_CONTROL_DISCOVERY_STALE_TIME_MS = 30_000;
19 +const SOURCE_CONTROL_DISCOVERY_IDLE_TTL_MS = 5 * 60_000;
20 +
21 +export const sourceControlDiscoveryManager = createSourceControlDiscoveryManager({
22 + getRegistry: () => appAtomRegistry,
23 + getClient: () => readLocalApi()?.server ?? null,
24 +});
25 +
26 +const sourceControlDiscoveryAutoRefreshAtom = Atom.make(() =>
27 + Effect.promise(() => refreshSourceControlDiscovery()),
28 +).pipe(
29 + Atom.swr({
30 + staleTime: SOURCE_CONTROL_DISCOVERY_STALE_TIME_MS,
31 + revalidateOnMount: true,
32 + }),
33 + Atom.setIdleTTL(SOURCE_CONTROL_DISCOVERY_IDLE_TTL_MS),
34 + Atom.withLabel("source-control-discovery:auto-refresh"),
35 +);
36 +
37 +export function refreshSourceControlDiscovery(): Promise<SourceControlDiscoveryResult | null> {
38 + return sourceControlDiscoveryManager.refresh(SOURCE_CONTROL_DISCOVERY_TARGET);
39 +}
40 +
41 +export function resetSourceControlDiscoveryStateForTests(): void {
42 + sourceControlDiscoveryManager.reset();
43 +}
44 +
45 +export function useSourceControlDiscovery(): SourceControlDiscoveryState {
46 + const targetKey = getSourceControlDiscoveryTargetKey(SOURCE_CONTROL_DISCOVERY_TARGET);
47 +
48 + useAtomValue(sourceControlDiscoveryAutoRefreshAtom);
49 +
50 + const state = useAtomValue(
51 + targetKey !== null
52 + ? sourceControlDiscoveryStateAtom(targetKey)
53 + : EMPTY_SOURCE_CONTROL_DISCOVERY_ATOM,
54 + );
55 + return targetKey === null ? EMPTY_SOURCE_CONTROL_DISCOVERY_STATE : state;
56 +}
apps/web/src/localApi.test.ts
+19 -17
@@ -3,7 +3,7 @@ import {
3 DEFAULT_SERVER_SETTINGS,
4 type DesktopBridge,
5 EnvironmentId,
6 - type GitStatusResult,
6 + type VcsStatusResult,
7 ProjectId,
8 type OrchestrationShellStreamItem,
9 ProviderDriverKind,
@@ -34,7 +34,7 @@ function registerListener<T>(listeners: Set<(event: T) => void>, listener: (even
34
35 const terminalEventListeners = new Set<(event: TerminalEvent) => void>();
36 const shellStreamListeners = new Set<(event: OrchestrationShellStreamItem) => void>();
37 -const gitStatusListeners = new Set<(event: GitStatusResult) => void>();
37 +const gitStatusListeners = new Set<(event: VcsStatusResult) => void>();
38
39 const rpcClientMock = {
40 dispose: vi.fn(),
@@ -59,19 +59,21 @@ const rpcClientMock = {
59 shell: {
60 openInEditor: vi.fn(),
61 },
62 - git: {
62 + vcs: {
63 pull: vi.fn(),
64 refreshStatus: vi.fn(),
65 - onStatus: vi.fn((input: { cwd: string }, listener: (event: GitStatusResult) => void) =>
65 + onStatus: vi.fn((input: { cwd: string }, listener: (event: VcsStatusResult) => void) =>
66 registerListener(gitStatusListeners, listener),
67 ),
68 - runStackedAction: vi.fn(),
69 - listBranches: vi.fn(),
68 + listRefs: vi.fn(),
69 createWorktree: vi.fn(),
70 removeWorktree: vi.fn(),
72 - createBranch: vi.fn(),
73 - checkout: vi.fn(),
71 + createRef: vi.fn(),
72 + switchRef: vi.fn(),
73 init: vi.fn(),
74 + },
75 + git: {
76 + runStackedAction: vi.fn(),
77 resolvePullRequest: vi.fn(),
78 preparePullRequestThread: vi.fn(),
79 },
@@ -259,11 +261,11 @@ const baseServerConfig: ServerConfig = {
261 settings: DEFAULT_SERVER_SETTINGS,
262 };
263
262 -const baseGitStatus: GitStatusResult = {
264 +const baseGitStatus: VcsStatusResult = {
265 isRepo: true,
264 - hasOriginRemote: true,
265 - isDefaultBranch: false,
266 - branch: "feature/streamed",
266 + hasPrimaryRemote: true,
267 + isDefaultRef: false,
268 + refName: "feature/streamed",
269 hasWorkingTreeChanges: false,
270 workingTree: { files: [], insertions: 0, deletions: 0 },
271 hasUpstream: true,
@@ -351,24 +353,24 @@ describe("wsApi", () => {
353 const api = createEnvironmentApi(rpcClientMock as never);
354 const onStatus = vi.fn();
355
354 - api.git.onStatus({ cwd: "/repo" }, onStatus);
356 + api.vcs.onStatus({ cwd: "/repo" }, onStatus);
357
358 const gitStatus = baseGitStatus;
359 emitEvent(gitStatusListeners, gitStatus);
360
359 - expect(rpcClientMock.git.onStatus).toHaveBeenCalledWith({ cwd: "/repo" }, onStatus, undefined);
361 + expect(rpcClientMock.vcs.onStatus).toHaveBeenCalledWith({ cwd: "/repo" }, onStatus, undefined);
362 expect(onStatus).toHaveBeenCalledWith(gitStatus);
363 });
364
365 it("forwards git status refreshes directly to the RPC client", async () => {
364 - rpcClientMock.git.refreshStatus.mockResolvedValue(baseGitStatus);
366 + rpcClientMock.vcs.refreshStatus.mockResolvedValue(baseGitStatus);
367 const { createEnvironmentApi } = await import("./environmentApi");
368
369 const api = createEnvironmentApi(rpcClientMock as never);
370
369 - await api.git.refreshStatus({ cwd: "/repo" });
371 + await api.vcs.refreshStatus({ cwd: "/repo" });
372
371 - expect(rpcClientMock.git.refreshStatus).toHaveBeenCalledWith({ cwd: "/repo" });
373 + expect(rpcClientMock.vcs.refreshStatus).toHaveBeenCalledWith({ cwd: "/repo" });
374 });
375
376 it("forwards shell stream subscription options to the RPC client", async () => {
apps/web/src/localApi.ts
+3
@@ -1,6 +1,7 @@
1 import type { ContextMenuItem, LocalApi } from "@t3tools/contracts";
2
3 import { resetGitStatusStateForTests } from "./lib/gitStatusState";
4 +import { resetSourceControlDiscoveryStateForTests } from "./lib/sourceControlDiscoveryState";
5 import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
6 import { resetServerStateForTests } from "./rpc/serverState";
7 import { resetWsConnectionStateForTests } from "./rpc/wsConnectionState";
@@ -115,6 +116,7 @@ export function createLocalApi(rpcClient: WsRpcClient): LocalApi {
116 upsertKeybinding: rpcClient.server.upsertKeybinding,
117 getSettings: rpcClient.server.getSettings,
118 updateSettings: rpcClient.server.updateSettings,
119 + discoverSourceControl: rpcClient.server.discoverSourceControl,
120 },
121 };
122 }
@@ -146,6 +148,7 @@ export async function __resetLocalApiForTests() {
148 __resetClientSettingsPersistenceForTests();
149 await resetEnvironmentServiceForTests();
150 resetGitStatusStateForTests();
151 + resetSourceControlDiscoveryStateForTests();
152 resetRequestLatencyStateForTests();
153 resetSavedEnvironmentRegistryStoreForTests();
154 resetSavedEnvironmentRuntimeStoreForTests();
apps/web/src/pullRequestReference.test.ts
+32
@@ -9,6 +9,24 @@ describe("parsePullRequestReference", () => {
9 );
10 });
11
12 + it("accepts Azure DevOps pull request URLs", () => {
13 + expect(
14 + parsePullRequestReference("https://dev.azure.com/acme/project/_git/t3code/pullrequest/42"),
15 + ).toBe("https://dev.azure.com/acme/project/_git/t3code/pullrequest/42");
16 + });
17 +
18 + it("accepts GitLab merge request URLs", () => {
19 + expect(parsePullRequestReference("https://gitlab.com/group/project/-/merge_requests/42")).toBe(
20 + "https://gitlab.com/group/project/-/merge_requests/42",
21 + );
22 + });
23 +
24 + it("accepts legacy Azure DevOps pull request URLs", () => {
25 + expect(
26 + parsePullRequestReference("https://acme.visualstudio.com/project/_git/t3code/pullrequest/42"),
27 + ).toBe("https://acme.visualstudio.com/project/_git/t3code/pullrequest/42");
28 + });
29 +
30 it("accepts raw numbers", () => {
31 expect(parsePullRequestReference("42")).toBe("42");
32 });
@@ -31,6 +49,20 @@ describe("parsePullRequestReference", () => {
49 ).toBe("https://github.com/pingdotgg/t3code/pull/42");
50 });
51
52 + it("accepts glab mr checkout commands with raw numbers", () => {
53 + expect(parsePullRequestReference("glab mr checkout 42")).toBe("42");
54 + });
55 +
56 + it("accepts az repos pr checkout commands with raw numbers", () => {
57 + expect(parsePullRequestReference("az repos pr checkout --id 42")).toBe("42");
58 + });
59 +
60 + it("accepts az repos pr checkout commands with extra flags", () => {
61 + expect(parsePullRequestReference("az repos pr checkout --id 42 --remote-name origin")).toBe(
62 + "42",
63 + );
64 + });
65 +
66 it("rejects non-pull-request input", () => {
67 expect(parsePullRequestReference("feature/my-branch")).toBeNull();
68 });
apps/web/src/pullRequestReference.ts
+28 -2
@@ -1,7 +1,22 @@
1 const GITHUB_PULL_REQUEST_URL_PATTERN =
2 /^https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/(\d+)(?:[/?#].*)?$/i;
3 +const GITLAB_MERGE_REQUEST_URL_PATTERN =
4 + /^https:\/\/[^/\s]*gitlab[^/\s]*\/.+\/-\/merge_requests\/(\d+)(?:[/?#].*)?$/i;
5 +const AZURE_DEVOPS_PULL_REQUEST_URL_PATTERN =
6 + /^https:\/\/(?:dev\.azure\.com\/[^/\s]+\/[^/\s]+|[^/\s]+\.visualstudio\.com\/[^/\s]+)\/_git\/[^/\s]+\/pullrequest\/(\d+)(?:[/?#].*)?$/i;
7 const PULL_REQUEST_NUMBER_PATTERN = /^#?(\d+)$/;
8 const GITHUB_CLI_PR_CHECKOUT_PATTERN = /^gh\s+pr\s+checkout\s+(.+)$/i;
9 +const GITLAB_CLI_MR_CHECKOUT_PATTERN = /^glab\s+mr\s+checkout\s+(.+)$/i;
10 +const AZURE_DEVOPS_CLI_PR_CHECKOUT_PATTERN = /^az\s+repos\s+pr\s+checkout\s+(.+)$/i;
11 +
12 +function parseAzureDevOpsCheckoutReference(args: string): string | null {
13 + const parts = args.trim().split(/\s+/).filter(Boolean);
14 + const idFlagIndex = parts.findIndex((part) => part === "--id" || part === "-i");
15 + if (idFlagIndex >= 0) {
16 + return parts[idFlagIndex + 1] ?? null;
17 + }
18 + return parts.find((part) => !part.startsWith("-")) ?? null;
19 +}
20
21 export function parsePullRequestReference(input: string): string | null {
22 const trimmed = input.trim();
@@ -10,12 +25,23 @@ export function parsePullRequestReference(input: string): string | null {
25 }
26
27 const ghCliCheckoutMatch = GITHUB_CLI_PR_CHECKOUT_PATTERN.exec(trimmed);
13 - const normalizedInput = ghCliCheckoutMatch?.[1]?.trim() ?? trimmed;
28 + const glabCliCheckoutMatch = GITLAB_CLI_MR_CHECKOUT_PATTERN.exec(trimmed);
29 + const azureDevOpsCliCheckoutMatch = AZURE_DEVOPS_CLI_PR_CHECKOUT_PATTERN.exec(trimmed);
30 + const normalizedInput =
31 + ghCliCheckoutMatch?.[1]?.trim() ??
32 + glabCliCheckoutMatch?.[1]?.trim() ??
33 + (azureDevOpsCliCheckoutMatch?.[1]
34 + ? parseAzureDevOpsCheckoutReference(azureDevOpsCliCheckoutMatch[1])
35 + : null) ??
36 + trimmed;
37 if (normalizedInput.length === 0) {
38 return null;
39 }
40
18 - const urlMatch = GITHUB_PULL_REQUEST_URL_PATTERN.exec(normalizedInput);
41 + const urlMatch =
42 + GITHUB_PULL_REQUEST_URL_PATTERN.exec(normalizedInput) ??
43 + GITLAB_MERGE_REQUEST_URL_PATTERN.exec(normalizedInput) ??
44 + AZURE_DEVOPS_PULL_REQUEST_URL_PATTERN.exec(normalizedInput);
45 if (urlMatch?.[1]) {
46 return normalizedInput;
47 }
apps/web/src/routeTree.gen.ts
+21
@@ -13,6 +13,7 @@ import { Route as SettingsRouteImport } from './routes/settings'
13 import { Route as PairRouteImport } from './routes/pair'
14 import { Route as ChatRouteImport } from './routes/_chat'
15 import { Route as ChatIndexRouteImport } from './routes/_chat.index'
16 +import { Route as SettingsSourceControlRouteImport } from './routes/settings.source-control'
17 import { Route as SettingsGeneralRouteImport } from './routes/settings.general'
18 import { Route as SettingsConnectionsRouteImport } from './routes/settings.connections'
19 import { Route as SettingsArchivedRouteImport } from './routes/settings.archived'
@@ -38,6 +39,11 @@ const ChatIndexRoute = ChatIndexRouteImport.update({
39 path: '/',
40 getParentRoute: () => ChatRoute,
41 } as any)
42 +const SettingsSourceControlRoute = SettingsSourceControlRouteImport.update({
43 + id: '/source-control',
44 + path: '/source-control',
45 + getParentRoute: () => SettingsRoute,
46 +} as any)
47 const SettingsGeneralRoute = SettingsGeneralRouteImport.update({
48 id: '/general',
49 path: '/general',
@@ -72,6 +78,7 @@ export interface FileRoutesByFullPath {
78 '/settings/archived': typeof SettingsArchivedRoute
79 '/settings/connections': typeof SettingsConnectionsRoute
80 '/settings/general': typeof SettingsGeneralRoute
81 + '/settings/source-control': typeof SettingsSourceControlRoute
82 '/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute
83 '/draft/$draftId': typeof ChatDraftDraftIdRoute
84 }
@@ -81,6 +88,7 @@ export interface FileRoutesByTo {
88 '/settings/archived': typeof SettingsArchivedRoute
89 '/settings/connections': typeof SettingsConnectionsRoute
90 '/settings/general': typeof SettingsGeneralRoute
91 + '/settings/source-control': typeof SettingsSourceControlRoute
92 '/': typeof ChatIndexRoute
93 '/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute
94 '/draft/$draftId': typeof ChatDraftDraftIdRoute
@@ -93,6 +101,7 @@ export interface FileRoutesById {
101 '/settings/archived': typeof SettingsArchivedRoute
102 '/settings/connections': typeof SettingsConnectionsRoute
103 '/settings/general': typeof SettingsGeneralRoute
104 + '/settings/source-control': typeof SettingsSourceControlRoute
105 '/_chat/': typeof ChatIndexRoute
106 '/_chat/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute
107 '/_chat/draft/$draftId': typeof ChatDraftDraftIdRoute
@@ -106,6 +115,7 @@ export interface FileRouteTypes {
115 | '/settings/archived'
116 | '/settings/connections'
117 | '/settings/general'
118 + | '/settings/source-control'
119 | '/$environmentId/$threadId'
120 | '/draft/$draftId'
121 fileRoutesByTo: FileRoutesByTo
@@ -115,6 +125,7 @@ export interface FileRouteTypes {
125 | '/settings/archived'
126 | '/settings/connections'
127 | '/settings/general'
128 + | '/settings/source-control'
129 | '/'
130 | '/$environmentId/$threadId'
131 | '/draft/$draftId'
@@ -126,6 +137,7 @@ export interface FileRouteTypes {
137 | '/settings/archived'
138 | '/settings/connections'
139 | '/settings/general'
140 + | '/settings/source-control'
141 | '/_chat/'
142 | '/_chat/$environmentId/$threadId'
143 | '/_chat/draft/$draftId'
@@ -167,6 +179,13 @@ declare module '@tanstack/react-router' {
179 preLoaderRoute: typeof ChatIndexRouteImport
180 parentRoute: typeof ChatRoute
181 }
182 + '/settings/source-control': {
183 + id: '/settings/source-control'
184 + path: '/source-control'
185 + fullPath: '/settings/source-control'
186 + preLoaderRoute: typeof SettingsSourceControlRouteImport
187 + parentRoute: typeof SettingsRoute
188 + }
189 '/settings/general': {
190 id: '/settings/general'
191 path: '/general'
@@ -223,12 +242,14 @@ interface SettingsRouteChildren {
242 SettingsArchivedRoute: typeof SettingsArchivedRoute
243 SettingsConnectionsRoute: typeof SettingsConnectionsRoute
244 SettingsGeneralRoute: typeof SettingsGeneralRoute
245 + SettingsSourceControlRoute: typeof SettingsSourceControlRoute
246 }
247
248 const SettingsRouteChildren: SettingsRouteChildren = {
249 SettingsArchivedRoute: SettingsArchivedRoute,
250 SettingsConnectionsRoute: SettingsConnectionsRoute,
251 SettingsGeneralRoute: SettingsGeneralRoute,
252 + SettingsSourceControlRoute: SettingsSourceControlRoute,
253 }
254
255 const SettingsRouteWithChildren = SettingsRoute._addFileChildren(
apps/web/src/routes/settings.source-control.tsx new
+7
@@ -0,0 +1,7 @@
1 +import { createFileRoute } from "@tanstack/react-router";
2 +
3 +import { SourceControlSettingsPanel } from "../components/settings/SourceControlSettings";
4 +
5 +export const Route = createFileRoute("/settings/source-control")({
6 + component: SourceControlSettingsPanel,
7 +});
apps/web/src/rpc/wsRpcClient.test.ts
+11 -11
@@ -1,7 +1,7 @@
1 import type {
2 - GitStatusLocalResult,
3 - GitStatusRemoteResult,
4 - GitStatusStreamEvent,
2 + VcsStatusLocalResult,
3 + VcsStatusRemoteResult,
4 + VcsStatusStreamEvent,
5 } from "@t3tools/contracts";
6 import { describe, expect, it, vi } from "vitest";
7
@@ -18,16 +18,16 @@ vi.mock("./wsTransport", () => ({
18 import { createWsRpcClient } from "./wsRpcClient";
19 import { type WsTransport } from "./wsTransport";
20
21 -const baseLocalStatus: GitStatusLocalResult = {
21 +const baseLocalStatus: VcsStatusLocalResult = {
22 isRepo: true,
23 - hasOriginRemote: true,
24 - isDefaultBranch: false,
25 - branch: "feature/demo",
23 + hasPrimaryRemote: true,
24 + isDefaultRef: false,
25 + refName: "feature/demo",
26 hasWorkingTreeChanges: false,
27 workingTree: { files: [], insertions: 0, deletions: 0 },
28 };
29
30 -const baseRemoteStatus: GitStatusRemoteResult = {
30 +const baseRemoteStatus: VcsStatusRemoteResult = {
31 hasUpstream: true,
32 aheadCount: 0,
33 behindCount: 0,
@@ -35,7 +35,7 @@ const baseRemoteStatus: GitStatusRemoteResult = {
35 };
36
37 describe("wsRpcClient", () => {
38 - it("reduces git status stream events into flat status snapshots", () => {
38 + it("reduces vcs status stream events into flat status snapshots", () => {
39 const subscribe = vi.fn(<TValue>(_connect: unknown, listener: (value: TValue) => void) => {
40 for (const event of [
41 {
@@ -54,7 +54,7 @@ describe("wsRpcClient", () => {
54 hasWorkingTreeChanges: true,
55 },
56 },
57 - ] satisfies GitStatusStreamEvent[]) {
57 + ] satisfies VcsStatusStreamEvent[]) {
58 listener(event as TValue);
59 }
60 return () => undefined;
@@ -74,7 +74,7 @@ describe("wsRpcClient", () => {
74 const client = createWsRpcClient(transport as unknown as WsTransport);
75 const listener = vi.fn();
76
77 - client.git.onStatus({ cwd: "/repo" }, listener);
77 + client.vcs.onStatus({ cwd: "/repo" }, listener);
78
79 expect(listener.mock.calls).toEqual([
80 [
apps/web/src/rpc/wsRpcClient.ts
+39 -29
@@ -2,8 +2,8 @@ import {
2 type GitActionProgressEvent,
3 type GitRunStackedActionInput,
4 type GitRunStackedActionResult,
5 - type GitStatusResult,
6 - type GitStatusStreamEvent,
5 + type VcsStatusResult,
6 + type VcsStatusStreamEvent,
7 type LocalApi,
8 ORCHESTRATION_WS_METHODS,
9 type ServerSettingsPatch,
@@ -77,24 +77,29 @@ export interface WsRpcClient {
77 readonly editor: Parameters<LocalApi["shell"]["openInEditor"]>[1];
78 }) => ReturnType<LocalApi["shell"]["openInEditor"]>;
79 };
80 - readonly git: {
81 - readonly pull: RpcUnaryMethod<typeof WS_METHODS.gitPull>;
82 - readonly refreshStatus: RpcUnaryMethod<typeof WS_METHODS.gitRefreshStatus>;
80 + readonly vcs: {
81 + readonly pull: RpcUnaryMethod<typeof WS_METHODS.vcsPull>;
82 + readonly refreshStatus: RpcUnaryMethod<typeof WS_METHODS.vcsRefreshStatus>;
83 readonly onStatus: (
84 - input: RpcInput<typeof WS_METHODS.subscribeGitStatus>,
85 - listener: (status: GitStatusResult) => void,
84 + input: RpcInput<typeof WS_METHODS.subscribeVcsStatus>,
85 + listener: (status: VcsStatusResult) => void,
86 options?: StreamSubscriptionOptions,
87 ) => () => void;
88 + readonly listRefs: RpcUnaryMethod<typeof WS_METHODS.vcsListRefs>;
89 + readonly createWorktree: RpcUnaryMethod<typeof WS_METHODS.vcsCreateWorktree>;
90 + readonly removeWorktree: RpcUnaryMethod<typeof WS_METHODS.vcsRemoveWorktree>;
91 + readonly createRef: RpcUnaryMethod<typeof WS_METHODS.vcsCreateRef>;
92 + readonly switchRef: RpcUnaryMethod<typeof WS_METHODS.vcsSwitchRef>;
93 + readonly init: RpcUnaryMethod<typeof WS_METHODS.vcsInit>;
94 + };
95 + /**
96 + * Git-specific workflows. Local repository mechanics live under `vcs`.
97 + */
98 + readonly git: {
99 readonly runStackedAction: (
100 input: GitRunStackedActionInput,
101 options?: GitRunStackedActionOptions,
102 ) => Promise<GitRunStackedActionResult>;
92 - readonly listBranches: RpcUnaryMethod<typeof WS_METHODS.gitListBranches>;
93 - readonly createWorktree: RpcUnaryMethod<typeof WS_METHODS.gitCreateWorktree>;
94 - readonly removeWorktree: RpcUnaryMethod<typeof WS_METHODS.gitRemoveWorktree>;
95 - readonly createBranch: RpcUnaryMethod<typeof WS_METHODS.gitCreateBranch>;
96 - readonly checkout: RpcUnaryMethod<typeof WS_METHODS.gitCheckout>;
97 - readonly init: RpcUnaryMethod<typeof WS_METHODS.gitInit>;
103 readonly resolvePullRequest: RpcUnaryMethod<typeof WS_METHODS.gitResolvePullRequest>;
104 readonly preparePullRequestThread: RpcUnaryMethod<
105 typeof WS_METHODS.gitPreparePullRequestThread
@@ -114,6 +119,9 @@ export interface WsRpcClient {
119 readonly updateSettings: (
120 patch: ServerSettingsPatch,
121 ) => ReturnType<RpcUnaryMethod<typeof WS_METHODS.serverUpdateSettings>>;
122 + readonly discoverSourceControl: RpcUnaryNoArgMethod<
123 + typeof WS_METHODS.serverDiscoverSourceControl
124 + >;
125 readonly subscribeConfig: RpcStreamMethod<typeof WS_METHODS.subscribeServerConfig>;
126 readonly subscribeLifecycle: RpcStreamMethod<typeof WS_METHODS.subscribeServerLifecycle>;
127 readonly subscribeAuthAccess: RpcStreamMethod<typeof WS_METHODS.subscribeAuthAccess>;
@@ -161,21 +169,31 @@ export function createWsRpcClient(transport: WsTransport): WsRpcClient {
169 openInEditor: (input) =>
170 transport.request((client) => client[WS_METHODS.shellOpenInEditor](input)),
171 },
164 - git: {
165 - pull: (input) => transport.request((client) => client[WS_METHODS.gitPull](input)),
172 + vcs: {
173 + pull: (input) => transport.request((client) => client[WS_METHODS.vcsPull](input)),
174 refreshStatus: (input) =>
167 - transport.request((client) => client[WS_METHODS.gitRefreshStatus](input)),
175 + transport.request((client) => client[WS_METHODS.vcsRefreshStatus](input)),
176 onStatus: (input, listener, options) => {
169 - let current: GitStatusResult | null = null;
177 + let current: VcsStatusResult | null = null;
178 return transport.subscribe(
171 - (client) => client[WS_METHODS.subscribeGitStatus](input),
172 - (event: GitStatusStreamEvent) => {
179 + (client) => client[WS_METHODS.subscribeVcsStatus](input),
180 + (event: VcsStatusStreamEvent) => {
181 current = applyGitStatusStreamEvent(current, event);
182 listener(current);
183 },
184 options,
185 );
186 },
187 + listRefs: (input) => transport.request((client) => client[WS_METHODS.vcsListRefs](input)),
188 + createWorktree: (input) =>
189 + transport.request((client) => client[WS_METHODS.vcsCreateWorktree](input)),
190 + removeWorktree: (input) =>
191 + transport.request((client) => client[WS_METHODS.vcsRemoveWorktree](input)),
192 + createRef: (input) => transport.request((client) => client[WS_METHODS.vcsCreateRef](input)),
193 + switchRef: (input) => transport.request((client) => client[WS_METHODS.vcsSwitchRef](input)),
194 + init: (input) => transport.request((client) => client[WS_METHODS.vcsInit](input)),
195 + },
196 + git: {
197 runStackedAction: async (input, options) => {
198 let result: GitRunStackedActionResult | null = null;
199
@@ -195,16 +213,6 @@ export function createWsRpcClient(transport: WsTransport): WsRpcClient {
213
214 throw new Error("Git action stream completed without a final result.");
215 },
198 - listBranches: (input) =>
199 - transport.request((client) => client[WS_METHODS.gitListBranches](input)),
200 - createWorktree: (input) =>
201 - transport.request((client) => client[WS_METHODS.gitCreateWorktree](input)),
202 - removeWorktree: (input) =>
203 - transport.request((client) => client[WS_METHODS.gitRemoveWorktree](input)),
204 - createBranch: (input) =>
205 - transport.request((client) => client[WS_METHODS.gitCreateBranch](input)),
206 - checkout: (input) => transport.request((client) => client[WS_METHODS.gitCheckout](input)),
207 - init: (input) => transport.request((client) => client[WS_METHODS.gitInit](input)),
216 resolvePullRequest: (input) =>
217 transport.request((client) => client[WS_METHODS.gitResolvePullRequest](input)),
218 preparePullRequestThread: (input) =>
@@ -219,6 +227,8 @@ export function createWsRpcClient(transport: WsTransport): WsRpcClient {
227 getSettings: () => transport.request((client) => client[WS_METHODS.serverGetSettings]({})),
228 updateSettings: (patch) =>
229 transport.request((client) => client[WS_METHODS.serverUpdateSettings]({ patch })),
230 + discoverSourceControl: () =>
231 + transport.request((client) => client[WS_METHODS.serverDiscoverSourceControl]({})),
232 subscribeConfig: (listener, options) =>
233 transport.subscribe(
234 (client) => client[WS_METHODS.subscribeServerConfig]({}),
apps/web/src/sourceControlPresentation.ts new
+62
@@ -0,0 +1,62 @@
1 +import { GitPullRequestIcon } from "lucide-react";
2 +import type { ElementType } from "react";
3 +import type { SourceControlProviderInfo } from "@t3tools/contracts";
4 +export {
5 + DEFAULT_CHANGE_REQUEST_TERMINOLOGY,
6 + formatChangeRequestAction,
7 + formatCreateChangeRequestPhrase,
8 + getChangeRequestTerminology,
9 + resolveChangeRequestPresentation,
10 + type ChangeRequestPresentation,
11 + type ChangeRequestTerminology,
12 +} from "@t3tools/shared/sourceControl";
13 +import {
14 + getChangeRequestTerminology,
15 + resolveChangeRequestPresentation,
16 + type ChangeRequestTerminology,
17 +} from "@t3tools/shared/sourceControl";
18 +import { AzureDevOpsIcon, BitbucketIcon, GitHubIcon, GitLabIcon } from "./components/Icons";
19 +
20 +export interface SourceControlPresentation {
21 + readonly providerName: string;
22 + readonly terminology: ChangeRequestTerminology;
23 + readonly Icon: ElementType<{ className?: string }>;
24 +}
25 +
26 +export function getSourceControlPresentation(
27 + provider: SourceControlProviderInfo | null | undefined,
28 +): SourceControlPresentation {
29 + const presentation = resolveChangeRequestPresentation(provider);
30 + switch (presentation.icon) {
31 + case "github":
32 + return {
33 + providerName: provider?.name || presentation.providerName,
34 + terminology: getChangeRequestTerminology(provider),
35 + Icon: GitHubIcon,
36 + };
37 + case "gitlab":
38 + return {
39 + providerName: provider?.name || presentation.providerName,
40 + terminology: getChangeRequestTerminology(provider),
41 + Icon: GitLabIcon,
42 + };
43 + case "azure-devops":
44 + return {
45 + providerName: provider?.name || presentation.providerName,
46 + terminology: getChangeRequestTerminology(provider),
47 + Icon: AzureDevOpsIcon,
48 + };
49 + case "bitbucket":
50 + return {
51 + providerName: provider?.name || presentation.providerName,
52 + terminology: getChangeRequestTerminology(provider),
53 + Icon: BitbucketIcon,
54 + };
55 + case "change-request":
56 + return {
57 + providerName: provider?.name || presentation.providerName,
58 + terminology: getChangeRequestTerminology(provider),
59 + Icon: GitPullRequestIcon,
60 + };
61 + }
62 +}
apps/web/test/wsRpcHarness.ts
+1 -1
@@ -26,7 +26,7 @@ const STREAM_METHODS = new Set<string>([
26 ORCHESTRATION_WS_METHODS.subscribeShell,
27 ORCHESTRATION_WS_METHODS.subscribeThread,
28 WS_METHODS.gitRunStackedAction,
29 - WS_METHODS.subscribeGitStatus,
29 + WS_METHODS.subscribeVcsStatus,
30 WS_METHODS.subscribeTerminalEvents,
31 WS_METHODS.subscribeServerConfig,
32 WS_METHODS.subscribeServerLifecycle,
bun.lock
+1
@@ -131,6 +131,7 @@
131 "version": "0.0.0-alpha.1",
132 "dependencies": {
133 "@t3tools/contracts": "workspace:*",
134 + "effect": "catalog:",
135 },
136 "devDependencies": {
137 "@effect/language-service": "catalog:",
packages/client-runtime/package.json
+2 -1
@@ -15,7 +15,8 @@
15 "test": "vitest run"
16 },
17 "dependencies": {
18 - "@t3tools/contracts": "workspace:*"
18 + "@t3tools/contracts": "workspace:*",
19 + "effect": "catalog:"
20 },
21 "devDependencies": {
22 "@effect/language-service": "catalog:",
packages/client-runtime/src/index.ts
+1
@@ -1,2 +1,3 @@
1 export * from "./knownEnvironment.ts";
2 export * from "./scoped.ts";
3 +export * from "./sourceControlDiscoveryState.ts";
packages/client-runtime/src/sourceControlDiscoveryState.test.ts new
+107
@@ -0,0 +1,107 @@
1 +import { assert, beforeEach, it } from "vitest";
2 +import type { SourceControlDiscoveryResult } from "@t3tools/contracts";
3 +import { AtomRegistry } from "effect/unstable/reactivity";
4 +
5 +import {
6 + EMPTY_SOURCE_CONTROL_DISCOVERY_STATE,
7 + createSourceControlDiscoveryManager,
8 +} from "./sourceControlDiscoveryState.ts";
9 +
10 +const EMPTY_RESULT: SourceControlDiscoveryResult = {
11 + versionControlSystems: [],
12 + sourceControlProviders: [],
13 +};
14 +
15 +function unresolvedDiscovery() {
16 + throw new Error("Discovery resolver was not initialized.");
17 +}
18 +
19 +let registry = AtomRegistry.make();
20 +
21 +beforeEach(() => {
22 + registry.dispose();
23 + registry = AtomRegistry.make();
24 +});
25 +
26 +it("stores refreshed discovery data in an atom snapshot", async () => {
27 + const manager = createSourceControlDiscoveryManager({
28 + getRegistry: () => registry,
29 + getClient: () => ({
30 + discoverSourceControl: async () => EMPTY_RESULT,
31 + }),
32 + });
33 +
34 + assert.deepStrictEqual(manager.getSnapshot({ key: null }), EMPTY_SOURCE_CONTROL_DISCOVERY_STATE);
35 +
36 + const result = await manager.refresh({ key: "primary" });
37 +
38 + assert.strictEqual(result, EMPTY_RESULT);
39 + assert.deepStrictEqual(manager.getSnapshot({ key: "primary" }), {
40 + data: EMPTY_RESULT,
41 + error: null,
42 + isPending: false,
43 + });
44 +});
45 +
46 +it("deduplicates in-flight discovery refreshes by target key", async () => {
47 + let resolveDiscovery: (result: SourceControlDiscoveryResult) => void = unresolvedDiscovery;
48 + let calls = 0;
49 + const manager = createSourceControlDiscoveryManager({
50 + getRegistry: () => registry,
51 + getClient: () => ({
52 + discoverSourceControl: () => {
53 + calls += 1;
54 + return new Promise<SourceControlDiscoveryResult>((resolve) => {
55 + resolveDiscovery = resolve;
56 + });
57 + },
58 + }),
59 + });
60 +
61 + const first = manager.refresh({ key: "primary" });
62 + const second = manager.refresh({ key: "primary" });
63 +
64 + assert.strictEqual(first, second);
65 + assert.strictEqual(calls, 1);
66 + assert.deepStrictEqual(manager.getSnapshot({ key: "primary" }), {
67 + data: null,
68 + error: null,
69 + isPending: true,
70 + });
71 +
72 + resolveDiscovery(EMPTY_RESULT);
73 + await first;
74 +
75 + assert.deepStrictEqual(manager.getSnapshot({ key: "primary" }), {
76 + data: EMPTY_RESULT,
77 + error: null,
78 + isPending: false,
79 + });
80 +});
81 +
82 +it("keeps the previous snapshot when refresh fails", async () => {
83 + let shouldFail = false;
84 + const manager = createSourceControlDiscoveryManager({
85 + getRegistry: () => registry,
86 + getClient: () => ({
87 + discoverSourceControl: async () => {
88 + if (shouldFail) {
89 + throw new Error("probe failed");
90 + }
91 + return EMPTY_RESULT;
92 + },
93 + }),
94 + });
95 +
96 + await manager.refresh({ key: "primary" });
97 + shouldFail = true;
98 +
99 + const result = await manager.refresh({ key: "primary" });
100 +
101 + assert.strictEqual(result, EMPTY_RESULT);
102 + assert.deepStrictEqual(manager.getSnapshot({ key: "primary" }), {
103 + data: EMPTY_RESULT,
104 + error: "probe failed",
105 + isPending: false,
106 + });
107 +});
packages/client-runtime/src/sourceControlDiscoveryState.ts new
+206
@@ -0,0 +1,206 @@
1 +import type { SourceControlDiscoveryResult } from "@t3tools/contracts";
2 +import { Atom, type AtomRegistry } from "effect/unstable/reactivity";
3 +
4 +/* --- Types ---------------------------------------------------------- */
5 +
6 +export interface SourceControlDiscoveryState {
7 + readonly data: SourceControlDiscoveryResult | null;
8 + readonly error: string | null;
9 + readonly isPending: boolean;
10 +}
11 +
12 +export interface SourceControlDiscoveryTarget {
13 + readonly key: string | null;
14 +}
15 +
16 +export interface SourceControlDiscoveryClient {
17 + readonly discoverSourceControl: () => Promise<SourceControlDiscoveryResult>;
18 +}
19 +
20 +/* --- Constants ------------------------------------------------------ */
21 +
22 +export const EMPTY_SOURCE_CONTROL_DISCOVERY_STATE = Object.freeze<SourceControlDiscoveryState>({
23 + data: null,
24 + error: null,
25 + isPending: false,
26 +});
27 +
28 +const INITIAL_SOURCE_CONTROL_DISCOVERY_STATE = Object.freeze<SourceControlDiscoveryState>({
29 + data: null,
30 + error: null,
31 + isPending: true,
32 +});
33 +
34 +/* --- Atoms ---------------------------------------------------------- */
35 +
36 +const knownSourceControlDiscoveryKeys = new Set<string>();
37 +
38 +export const sourceControlDiscoveryStateAtom = Atom.family((key: string) => {
39 + knownSourceControlDiscoveryKeys.add(key);
40 + return Atom.make(INITIAL_SOURCE_CONTROL_DISCOVERY_STATE).pipe(
41 + Atom.keepAlive,
42 + Atom.withLabel(`source-control-discovery:${key}`),
43 + );
44 +});
45 +
46 +export const EMPTY_SOURCE_CONTROL_DISCOVERY_ATOM = Atom.make(
47 + EMPTY_SOURCE_CONTROL_DISCOVERY_STATE,
48 +).pipe(Atom.keepAlive, Atom.withLabel("source-control-discovery:null"));
49 +
50 +/* --- Helpers -------------------------------------------------------- */
51 +
52 +export function getSourceControlDiscoveryTargetKey(
53 + target: SourceControlDiscoveryTarget,
54 +): string | null {
55 + const key = target.key?.trim();
56 + return key && key.length > 0 ? key : null;
57 +}
58 +
59 +/* --- Refresh manager ------------------------------------------------ */
60 +
61 +export interface SourceControlDiscoveryManagerConfig {
62 + /**
63 + * Get the atom registry used to read/write source-control discovery snapshots.
64 + */
65 + readonly getRegistry: () => AtomRegistry.AtomRegistry;
66 + /**
67 + * Resolve the runtime client for a discovery target key.
68 + *
69 + * Web currently uses a single `"primary"` target, but keeping this keyed
70 + * lets mobile or future multi-environment clients provide separate discovery
71 + * clients without changing the state primitive.
72 + */
73 + readonly getClient: (key: string) => SourceControlDiscoveryClient | null;
74 +}
75 +
76 +export function createSourceControlDiscoveryManager(config: SourceControlDiscoveryManagerConfig) {
77 + const refreshInFlight = new Map<string, Promise<SourceControlDiscoveryResult | null>>();
78 +
79 + /* -- Atom helpers -------------------------------------------------- */
80 +
81 + function setState(targetKey: string, nextState: SourceControlDiscoveryState): void {
82 + config.getRegistry().set(sourceControlDiscoveryStateAtom(targetKey), nextState);
83 + }
84 +
85 + function markPending(targetKey: string): void {
86 + const current = config.getRegistry().get(sourceControlDiscoveryStateAtom(targetKey));
87 + const next: SourceControlDiscoveryState =
88 + current.data === null
89 + ? INITIAL_SOURCE_CONTROL_DISCOVERY_STATE
90 + : {
91 + data: current.data,
92 + error: null,
93 + isPending: true,
94 + };
95 +
96 + if (
97 + current.data === next.data &&
98 + current.error === next.error &&
99 + current.isPending === next.isPending
100 + ) {
101 + return;
102 + }
103 +
104 + setState(targetKey, next);
105 + }
106 +
107 + function setData(targetKey: string, data: SourceControlDiscoveryResult): void {
108 + setState(targetKey, {
109 + data,
110 + error: null,
111 + isPending: false,
112 + });
113 + }
114 +
115 + function setError(targetKey: string, error: unknown): void {
116 + const current = config.getRegistry().get(sourceControlDiscoveryStateAtom(targetKey));
117 + setState(targetKey, {
118 + data: current.data,
119 + error: error instanceof Error ? error.message : "Failed to discover source control tools.",
120 + isPending: false,
121 + });
122 + }
123 +
124 + /* -- Public API ---------------------------------------------------- */
125 +
126 + /**
127 + * Trigger a one-shot source-control discovery RPC for a target.
128 + *
129 + * Calls are deduplicated while a refresh for the same target key is in
130 + * flight. On failure, the previous successful snapshot is kept in `data`
131 + * and the error message is stored separately so UI can keep rendering stale
132 + * discovery results while showing the failure.
133 + *
134 + * @param target The logical runtime target to refresh.
135 + * @param client Optional pre-resolved client, useful in tests.
136 + */
137 + function refresh(
138 + target: SourceControlDiscoveryTarget,
139 + client?: SourceControlDiscoveryClient,
140 + ): Promise<SourceControlDiscoveryResult | null> {
141 + const targetKey = getSourceControlDiscoveryTargetKey(target);
142 + if (targetKey === null) {
143 + return Promise.resolve(null);
144 + }
145 +
146 + const existing = refreshInFlight.get(targetKey);
147 + if (existing) {
148 + return existing;
149 + }
150 +
151 + const resolvedClient = client ?? config.getClient(targetKey);
152 + if (!resolvedClient) {
153 + const error = new Error("Source control discovery client is unavailable.");
154 + setError(targetKey, error);
155 + return Promise.resolve(getSnapshot(target).data);
156 + }
157 +
158 + markPending(targetKey);
159 + const promise = resolvedClient.discoverSourceControl().then(
160 + (result) => {
161 + setData(targetKey, result);
162 + return result;
163 + },
164 + (error: unknown) => {
165 + setError(targetKey, error);
166 + return getSnapshot(target).data;
167 + },
168 + );
169 + const tracked = promise.finally(() => refreshInFlight.delete(targetKey));
170 + refreshInFlight.set(targetKey, tracked);
171 + return tracked;
172 + }
173 +
174 + /**
175 + * Read the current atom snapshot for `target`.
176 + *
177 + * Invalid targets return the inert empty state rather than creating a new
178 + * family atom entry.
179 + */
180 + function getSnapshot(target: SourceControlDiscoveryTarget): SourceControlDiscoveryState {
181 + const targetKey = getSourceControlDiscoveryTargetKey(target);
182 + if (targetKey === null) {
183 + return EMPTY_SOURCE_CONTROL_DISCOVERY_STATE;
184 + }
185 +
186 + return config.getRegistry().get(sourceControlDiscoveryStateAtom(targetKey));
187 + }
188 +
189 + /**
190 + * Clear in-flight refresh tracking and reset every known discovery atom.
191 + * Primarily used by tests and runtime teardown.
192 + */
193 + function reset(): void {
194 + refreshInFlight.clear();
195 + for (const key of knownSourceControlDiscoveryKeys) {
196 + setState(key, INITIAL_SOURCE_CONTROL_DISCOVERY_STATE);
197 + }
198 + knownSourceControlDiscoveryKeys.clear();
199 + }
200 +
201 + return {
202 + refresh,
203 + getSnapshot,
204 + reset,
205 + };
206 +}
packages/contracts/src/git.test.ts
+7 -7
@@ -2,14 +2,14 @@ import { describe, expect, it } from "vitest";
2 import { Schema } from "effect";
3
4 import {
5 - GitCreateWorktreeInput,
5 + VcsCreateWorktreeInput,
6 GitPreparePullRequestThreadInput,
7 GitRunStackedActionResult,
8 GitRunStackedActionInput,
9 GitResolvePullRequestResult,
10 } from "./git.ts";
11
12 -const decodeCreateWorktreeInput = Schema.decodeUnknownSync(GitCreateWorktreeInput);
12 +const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);
13 const decodePreparePullRequestThreadInput = Schema.decodeUnknownSync(
14 GitPreparePullRequestThreadInput,
15 );
@@ -17,16 +17,16 @@ const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedAction
17 const decodeRunStackedActionResult = Schema.decodeUnknownSync(GitRunStackedActionResult);
18 const decodeResolvePullRequestResult = Schema.decodeUnknownSync(GitResolvePullRequestResult);
19
20 -describe("GitCreateWorktreeInput", () => {
21 - it("accepts omitted newBranch for existing-branch worktrees", () => {
20 +describe("VcsCreateWorktreeInput", () => {
21 + it("accepts omitted newRefName for existing-refName worktrees", () => {
22 const parsed = decodeCreateWorktreeInput({
23 cwd: "/repo",
24 - branch: "feature/existing",
24 + refName: "feature/existing",
25 path: "/tmp/worktree",
26 });
27
28 - expect(parsed.newBranch).toBeUndefined();
29 - expect(parsed.branch).toBe("feature/existing");
28 + expect(parsed.newRefName).toBeUndefined();
29 + expect(parsed.refName).toBe("feature/existing");
30 });
31 });
32
packages/contracts/src/git.ts
+72 -76
@@ -1,5 +1,7 @@
1 import { Schema } from "effect";
2 import { NonNegativeInt, PositiveInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
3 +import { SourceControlProviderError, SourceControlProviderInfo } from "./sourceControl.ts";
4 +import { VcsDriverKind } from "./vcs.ts";
5
6 const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
7 const GIT_LIST_BRANCHES_MAX_LIMIT = 200;
@@ -40,18 +42,10 @@ const GitPushStepStatus = Schema.Literals([
42 ]);
43 const GitBranchStepStatus = Schema.Literals(["created", "skipped_not_requested"]);
44 const GitPrStepStatus = Schema.Literals(["created", "opened_existing", "skipped_not_requested"]);
43 -const GitStatusPrState = Schema.Literals(["open", "closed", "merged"]);
45 +const VcsStatusChangeRequestState = Schema.Literals(["open", "closed", "merged"]);
46 const GitPullRequestReference = TrimmedNonEmptyStringSchema;
47 const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]);
48 const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]);
47 -export const GitHostingProviderKind = Schema.Literals(["github", "gitlab", "unknown"]);
48 -export type GitHostingProviderKind = typeof GitHostingProviderKind.Type;
49 -export const GitHostingProvider = Schema.Struct({
50 - kind: GitHostingProviderKind,
51 - name: TrimmedNonEmptyStringSchema,
52 - baseUrl: Schema.String,
53 -});
54 -export type GitHostingProvider = typeof GitHostingProvider.Type;
49 export const GitRunStackedActionToastRunAction = Schema.Struct({
50 kind: GitStackedAction,
51 });
@@ -79,7 +73,7 @@ const GitRunStackedActionToast = Schema.Struct({
73 });
74 export type GitRunStackedActionToast = typeof GitRunStackedActionToast.Type;
75
82 -export const GitBranch = Schema.Struct({
76 +export const VcsRef = Schema.Struct({
77 name: TrimmedNonEmptyStringSchema,
78 isRemote: Schema.optional(Schema.Boolean),
79 remoteName: Schema.optional(TrimmedNonEmptyStringSchema),
@@ -87,11 +81,11 @@ export const GitBranch = Schema.Struct({
81 isDefault: Schema.Boolean,
82 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
83 });
90 -export type GitBranch = typeof GitBranch.Type;
84 +export type VcsRef = typeof VcsRef.Type;
85
92 -const GitWorktree = Schema.Struct({
86 +const VcsWorktree = Schema.Struct({
87 path: TrimmedNonEmptyStringSchema,
94 - branch: TrimmedNonEmptyStringSchema,
88 + refName: TrimmedNonEmptyStringSchema,
89 });
90 const GitResolvedPullRequest = Schema.Struct({
91 number: PositiveInt,
@@ -105,15 +99,15 @@ export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
99
100 // RPC Inputs
101
108 -export const GitStatusInput = Schema.Struct({
102 +export const VcsStatusInput = Schema.Struct({
103 cwd: TrimmedNonEmptyStringSchema,
104 });
111 -export type GitStatusInput = typeof GitStatusInput.Type;
105 +export type VcsStatusInput = typeof VcsStatusInput.Type;
106
113 -export const GitPullInput = Schema.Struct({
107 +export const VcsPullInput = Schema.Struct({
108 cwd: TrimmedNonEmptyStringSchema,
109 });
116 -export type GitPullInput = typeof GitPullInput.Type;
110 +export type VcsPullInput = typeof VcsPullInput.Type;
111
112 export const GitRunStackedActionInput = Schema.Struct({
113 actionId: TrimmedNonEmptyStringSchema,
@@ -127,7 +121,7 @@ export const GitRunStackedActionInput = Schema.Struct({
121 });
122 export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
123
130 -export const GitListBranchesInput = Schema.Struct({
124 +export const VcsListRefsInput = Schema.Struct({
125 cwd: TrimmedNonEmptyStringSchema,
126 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
127 cursor: Schema.optional(NonNegativeInt),
@@ -135,15 +129,15 @@ export const GitListBranchesInput = Schema.Struct({
129 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
130 ),
131 });
138 -export type GitListBranchesInput = typeof GitListBranchesInput.Type;
132 +export type VcsListRefsInput = typeof VcsListRefsInput.Type;
133
140 -export const GitCreateWorktreeInput = Schema.Struct({
134 +export const VcsCreateWorktreeInput = Schema.Struct({
135 cwd: TrimmedNonEmptyStringSchema,
142 - branch: TrimmedNonEmptyStringSchema,
143 - newBranch: Schema.optional(TrimmedNonEmptyStringSchema),
136 + refName: TrimmedNonEmptyStringSchema,
137 + newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
138 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
139 });
146 -export type GitCreateWorktreeInput = typeof GitCreateWorktreeInput.Type;
140 +export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
141
142 export const GitPullRequestRefInput = Schema.Struct({
143 cwd: TrimmedNonEmptyStringSchema,
@@ -159,53 +153,54 @@ export const GitPreparePullRequestThreadInput = Schema.Struct({
153 });
154 export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
155
162 -export const GitRemoveWorktreeInput = Schema.Struct({
156 +export const VcsRemoveWorktreeInput = Schema.Struct({
157 cwd: TrimmedNonEmptyStringSchema,
158 path: TrimmedNonEmptyStringSchema,
159 force: Schema.optional(Schema.Boolean),
160 });
167 -export type GitRemoveWorktreeInput = typeof GitRemoveWorktreeInput.Type;
161 +export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
162
169 -export const GitCreateBranchInput = Schema.Struct({
163 +export const VcsCreateRefInput = Schema.Struct({
164 cwd: TrimmedNonEmptyStringSchema,
171 - branch: TrimmedNonEmptyStringSchema,
172 - checkout: Schema.optional(Schema.Boolean),
165 + refName: TrimmedNonEmptyStringSchema,
166 + switchRef: Schema.optional(Schema.Boolean),
167 });
174 -export type GitCreateBranchInput = typeof GitCreateBranchInput.Type;
168 +export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
169
176 -export const GitCreateBranchResult = Schema.Struct({
177 - branch: TrimmedNonEmptyStringSchema,
170 +export const VcsCreateRefResult = Schema.Struct({
171 + refName: TrimmedNonEmptyStringSchema,
172 });
179 -export type GitCreateBranchResult = typeof GitCreateBranchResult.Type;
173 +export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
174
181 -export const GitCheckoutInput = Schema.Struct({
175 +export const VcsSwitchRefInput = Schema.Struct({
176 cwd: TrimmedNonEmptyStringSchema,
183 - branch: TrimmedNonEmptyStringSchema,
177 + refName: TrimmedNonEmptyStringSchema,
178 });
185 -export type GitCheckoutInput = typeof GitCheckoutInput.Type;
179 +export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
180
187 -export const GitInitInput = Schema.Struct({
181 +export const VcsInitInput = Schema.Struct({
182 cwd: TrimmedNonEmptyStringSchema,
183 + kind: Schema.optional(VcsDriverKind),
184 });
190 -export type GitInitInput = typeof GitInitInput.Type;
185 +export type VcsInitInput = typeof VcsInitInput.Type;
186
187 // RPC Results
188
194 -const GitStatusPr = Schema.Struct({
189 +const VcsStatusChangeRequest = Schema.Struct({
190 number: PositiveInt,
191 title: TrimmedNonEmptyStringSchema,
192 url: Schema.String,
198 - baseBranch: TrimmedNonEmptyStringSchema,
199 - headBranch: TrimmedNonEmptyStringSchema,
200 - state: GitStatusPrState,
193 + baseRef: TrimmedNonEmptyStringSchema,
194 + headRef: TrimmedNonEmptyStringSchema,
195 + state: VcsStatusChangeRequestState,
196 });
197
203 -const GitStatusLocalShape = {
198 +const VcsStatusLocalShape = {
199 isRepo: Schema.Boolean,
205 - hostingProvider: Schema.optional(GitHostingProvider),
206 - hasOriginRemote: Schema.Boolean,
207 - isDefaultBranch: Schema.Boolean,
208 - branch: Schema.NullOr(TrimmedNonEmptyStringSchema),
200 + sourceControlProvider: Schema.optional(SourceControlProviderInfo),
201 + hasPrimaryRemote: Schema.Boolean,
202 + isDefaultRef: Schema.Boolean,
203 + refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
204 hasWorkingTreeChanges: Schema.Boolean,
205 workingTree: Schema.Struct({
206 files: Schema.Array(
@@ -220,52 +215,52 @@ const GitStatusLocalShape = {
215 }),
216 };
217
223 -const GitStatusRemoteShape = {
218 +const VcsStatusRemoteShape = {
219 hasUpstream: Schema.Boolean,
220 aheadCount: NonNegativeInt,
221 behindCount: NonNegativeInt,
227 - pr: Schema.NullOr(GitStatusPr),
222 + pr: Schema.NullOr(VcsStatusChangeRequest),
223 };
224
230 -export const GitStatusLocalResult = Schema.Struct(GitStatusLocalShape);
231 -export type GitStatusLocalResult = typeof GitStatusLocalResult.Type;
225 +export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
226 +export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
227
233 -export const GitStatusRemoteResult = Schema.Struct(GitStatusRemoteShape);
234 -export type GitStatusRemoteResult = typeof GitStatusRemoteResult.Type;
228 +export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
229 +export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
230
236 -export const GitStatusResult = Schema.Struct({
237 - ...GitStatusLocalShape,
238 - ...GitStatusRemoteShape,
231 +export const VcsStatusResult = Schema.Struct({
232 + ...VcsStatusLocalShape,
233 + ...VcsStatusRemoteShape,
234 });
240 -export type GitStatusResult = typeof GitStatusResult.Type;
235 +export type VcsStatusResult = typeof VcsStatusResult.Type;
236
242 -export const GitStatusStreamEvent = Schema.Union([
237 +export const VcsStatusStreamEvent = Schema.Union([
238 Schema.TaggedStruct("snapshot", {
244 - local: GitStatusLocalResult,
245 - remote: Schema.NullOr(GitStatusRemoteResult),
239 + local: VcsStatusLocalResult,
240 + remote: Schema.NullOr(VcsStatusRemoteResult),
241 }),
242 Schema.TaggedStruct("localUpdated", {
248 - local: GitStatusLocalResult,
243 + local: VcsStatusLocalResult,
244 }),
245 Schema.TaggedStruct("remoteUpdated", {
251 - remote: Schema.NullOr(GitStatusRemoteResult),
246 + remote: Schema.NullOr(VcsStatusRemoteResult),
247 }),
248 ]);
254 -export type GitStatusStreamEvent = typeof GitStatusStreamEvent.Type;
249 +export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
250
256 -export const GitListBranchesResult = Schema.Struct({
257 - branches: Schema.Array(GitBranch),
251 +export const VcsListRefsResult = Schema.Struct({
252 + refs: Schema.Array(VcsRef),
253 isRepo: Schema.Boolean,
259 - hasOriginRemote: Schema.Boolean,
254 + hasPrimaryRemote: Schema.Boolean,
255 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
256 totalCount: NonNegativeInt,
257 });
263 -export type GitListBranchesResult = typeof GitListBranchesResult.Type;
258 +export type VcsListRefsResult = typeof VcsListRefsResult.Type;
259
265 -export const GitCreateWorktreeResult = Schema.Struct({
266 - worktree: GitWorktree,
260 +export const VcsCreateWorktreeResult = Schema.Struct({
261 + worktree: VcsWorktree,
262 });
268 -export type GitCreateWorktreeResult = typeof GitCreateWorktreeResult.Type;
263 +export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
264
265 export const GitResolvePullRequestResult = Schema.Struct({
266 pullRequest: GitResolvedPullRequest,
@@ -279,10 +274,10 @@ export const GitPreparePullRequestThreadResult = Schema.Struct({
274 });
275 export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
276
282 -export const GitCheckoutResult = Schema.Struct({
283 - branch: Schema.NullOr(TrimmedNonEmptyStringSchema),
277 +export const VcsSwitchRefResult = Schema.Struct({
278 + refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
279 });
285 -export type GitCheckoutResult = typeof GitCheckoutResult.Type;
280 +export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
281
282 export const GitRunStackedActionResult = Schema.Struct({
283 action: GitStackedAction,
@@ -313,12 +308,12 @@ export const GitRunStackedActionResult = Schema.Struct({
308 });
309 export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
310
316 -export const GitPullResult = Schema.Struct({
311 +export const VcsPullResult = Schema.Struct({
312 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
318 - branch: TrimmedNonEmptyStringSchema,
319 - upstreamBranch: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
313 + refName: TrimmedNonEmptyStringSchema,
314 + upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
315 });
321 -export type GitPullResult = typeof GitPullResult.Type;
316 +export type VcsPullResult = typeof VcsPullResult.Type;
317
318 // RPC / domain errors
319 export class GitCommandError extends Schema.TaggedErrorClass<GitCommandError>()("GitCommandError", {
@@ -370,6 +365,7 @@ export const GitManagerServiceError = Schema.Union([
365 GitManagerError,
366 GitCommandError,
367 GitHubCliError,
368 + SourceControlProviderError,
369 TextGenerationError,
370 ]);
371 export type GitManagerServiceError = typeof GitManagerServiceError.Type;
packages/contracts/src/index.ts
+2
@@ -11,6 +11,8 @@ export * from "./keybindings.ts";
11 export * from "./server.ts";
12 export * from "./settings.ts";
13 export * from "./git.ts";
14 +export * from "./vcs.ts";
15 +export * from "./sourceControl.ts";
16 export * from "./orchestration.ts";
17 export * from "./editor.ts";
18 export * from "./project.ts";
packages/contracts/src/ipc.ts
+34 -30
@@ -1,22 +1,22 @@
1 import type {
2 - GitCheckoutInput,
3 - GitCheckoutResult,
4 - GitCreateBranchInput,
2 + VcsSwitchRefInput,
3 + VcsSwitchRefResult,
4 + VcsCreateRefInput,
5 GitPreparePullRequestThreadInput,
6 GitPreparePullRequestThreadResult,
7 GitPullRequestRefInput,
8 - GitCreateWorktreeInput,
9 - GitCreateWorktreeResult,
10 - GitInitInput,
11 - GitListBranchesInput,
12 - GitListBranchesResult,
13 - GitPullInput,
14 - GitPullResult,
15 - GitRemoveWorktreeInput,
8 + VcsCreateWorktreeInput,
9 + VcsCreateWorktreeResult,
10 + VcsInitInput,
11 + VcsListRefsInput,
12 + VcsListRefsResult,
13 + VcsPullInput,
14 + VcsPullResult,
15 + VcsRemoveWorktreeInput,
16 GitResolvePullRequestResult,
17 - GitStatusInput,
18 - GitStatusResult,
19 - GitCreateBranchResult,
17 + VcsStatusInput,
18 + VcsStatusResult,
19 + VcsCreateRefResult,
20 } from "./git.ts";
21 import type { FilesystemBrowseInput, FilesystemBrowseResult } from "./filesystem.ts";
22 import type {
@@ -55,6 +55,7 @@ import type {
55 import type { EnvironmentId } from "./baseSchemas.ts";
56 import { EditorId } from "./editor.ts";
57 import { ServerSettings, type ClientSettings, type ServerSettingsPatch } from "./settings.ts";
58 +import type { SourceControlDiscoveryResult } from "./sourceControl.ts";
59
60 export interface ContextMenuItem<T extends string = string> {
61 id: T;
@@ -227,6 +228,7 @@ export interface LocalApi {
228 upsertKeybinding: (input: ServerUpsertKeybindingInput) => Promise<ServerUpsertKeybindingResult>;
229 getSettings: () => Promise<ServerSettings>;
230 updateSettings: (patch: ServerSettingsPatch) => Promise<ServerSettings>;
231 + discoverSourceControl: () => Promise<SourceControlDiscoveryResult>;
232 };
233 }
234
@@ -235,7 +237,7 @@ export interface LocalApi {
237 *
238 * These operations must always be routed with explicit environment context.
239 * They represent remote stateful capabilities such as orchestration, terminal,
238 - * project, and git operations. In multi-environment mode, each environment gets
240 + * project, VCS, and provider operations. In multi-environment mode, each environment gets
241 * its own instance of this surface, and callers should resolve it by
242 * `environmentId` rather than reaching through the local desktop bridge.
243 */
@@ -256,27 +258,29 @@ export interface EnvironmentApi {
258 filesystem: {
259 browse: (input: FilesystemBrowseInput) => Promise<FilesystemBrowseResult>;
260 };
259 - git: {
260 - listBranches: (input: GitListBranchesInput) => Promise<GitListBranchesResult>;
261 - createWorktree: (input: GitCreateWorktreeInput) => Promise<GitCreateWorktreeResult>;
262 - removeWorktree: (input: GitRemoveWorktreeInput) => Promise<void>;
263 - createBranch: (input: GitCreateBranchInput) => Promise<GitCreateBranchResult>;
264 - checkout: (input: GitCheckoutInput) => Promise<GitCheckoutResult>;
265 - init: (input: GitInitInput) => Promise<void>;
266 - resolvePullRequest: (input: GitPullRequestRefInput) => Promise<GitResolvePullRequestResult>;
267 - preparePullRequestThread: (
268 - input: GitPreparePullRequestThreadInput,
269 - ) => Promise<GitPreparePullRequestThreadResult>;
270 - pull: (input: GitPullInput) => Promise<GitPullResult>;
271 - refreshStatus: (input: GitStatusInput) => Promise<GitStatusResult>;
261 + vcs: {
262 + listRefs: (input: VcsListRefsInput) => Promise<VcsListRefsResult>;
263 + createWorktree: (input: VcsCreateWorktreeInput) => Promise<VcsCreateWorktreeResult>;
264 + removeWorktree: (input: VcsRemoveWorktreeInput) => Promise<void>;
265 + createRef: (input: VcsCreateRefInput) => Promise<VcsCreateRefResult>;
266 + switchRef: (input: VcsSwitchRefInput) => Promise<VcsSwitchRefResult>;
267 + init: (input: VcsInitInput) => Promise<void>;
268 + pull: (input: VcsPullInput) => Promise<VcsPullResult>;
269 + refreshStatus: (input: VcsStatusInput) => Promise<VcsStatusResult>;
270 onStatus: (
273 - input: GitStatusInput,
274 - callback: (status: GitStatusResult) => void,
271 + input: VcsStatusInput,
272 + callback: (status: VcsStatusResult) => void,
273 options?: {
274 onResubscribe?: () => void;
275 },
276 ) => () => void;
277 };
278 + git: {
279 + resolvePullRequest: (input: GitPullRequestRefInput) => Promise<GitResolvePullRequestResult>;
280 + preparePullRequestThread: (
281 + input: GitPreparePullRequestThreadInput,
282 + ) => Promise<GitPreparePullRequestThreadResult>;
283 + };
284 orchestration: {
285 dispatchCommand: (command: ClientOrchestrationCommand) => Promise<{ sequence: number }>;
286 getTurnDiff: (input: OrchestrationGetTurnDiffInput) => Promise<OrchestrationGetTurnDiffResult>;
packages/contracts/src/rpc.ts
+71 -60
@@ -11,28 +11,28 @@ import {
11 } from "./filesystem.ts";
12 import {
13 GitActionProgressEvent,
14 - GitCheckoutInput,
15 - GitCheckoutResult,
14 + VcsSwitchRefInput,
15 + VcsSwitchRefResult,
16 GitCommandError,
17 - GitCreateBranchInput,
18 - GitCreateBranchResult,
19 - GitCreateWorktreeInput,
20 - GitCreateWorktreeResult,
21 - GitInitInput,
22 - GitListBranchesInput,
23 - GitListBranchesResult,
17 + VcsCreateRefInput,
18 + VcsCreateRefResult,
19 + VcsCreateWorktreeInput,
20 + VcsCreateWorktreeResult,
21 + VcsInitInput,
22 + VcsListRefsInput,
23 + VcsListRefsResult,
24 GitManagerServiceError,
25 GitPreparePullRequestThreadInput,
26 GitPreparePullRequestThreadResult,
27 - GitPullInput,
27 + VcsPullInput,
28 GitPullRequestRefInput,
29 - GitPullResult,
30 - GitRemoveWorktreeInput,
29 + VcsPullResult,
30 + VcsRemoveWorktreeInput,
31 GitResolvePullRequestResult,
32 GitRunStackedActionInput,
33 - GitStatusInput,
34 - GitStatusResult,
35 - GitStatusStreamEvent,
33 + VcsStatusInput,
34 + VcsStatusResult,
35 + VcsStatusStreamEvent,
36 } from "./git.ts";
37 import { KeybindingsConfigError } from "./keybindings.ts";
38 import {
@@ -77,6 +77,8 @@ import {
77 ServerUpsertKeybindingResult,
78 } from "./server.ts";
79 import { ServerSettings, ServerSettingsError, ServerSettingsPatch } from "./settings.ts";
80 +import { SourceControlDiscoveryResult } from "./sourceControl.ts";
81 +import { VcsError } from "./vcs.ts";
82
83 export const WS_METHODS = {
84 // Project registry methods
@@ -92,16 +94,18 @@ export const WS_METHODS = {
94 // Filesystem methods
95 filesystemBrowse: "filesystem.browse",
96
95 - // Git methods
96 - gitPull: "git.pull",
97 - gitRefreshStatus: "git.refreshStatus",
97 + // VCS methods
98 + vcsPull: "vcs.pull",
99 + vcsRefreshStatus: "vcs.refreshStatus",
100 + vcsListRefs: "vcs.listRefs",
101 + vcsCreateWorktree: "vcs.createWorktree",
102 + vcsRemoveWorktree: "vcs.removeWorktree",
103 + vcsCreateRef: "vcs.createRef",
104 + vcsSwitchRef: "vcs.switchRef",
105 + vcsInit: "vcs.init",
106 +
107 + // Git workflow methods
108 gitRunStackedAction: "git.runStackedAction",
99 - gitListBranches: "git.listBranches",
100 - gitCreateWorktree: "git.createWorktree",
101 - gitRemoveWorktree: "git.removeWorktree",
102 - gitCreateBranch: "git.createBranch",
103 - gitCheckout: "git.checkout",
104 - gitInit: "git.init",
109 gitResolvePullRequest: "git.resolvePullRequest",
110 gitPreparePullRequestThread: "git.preparePullRequestThread",
111
@@ -119,9 +123,10 @@ export const WS_METHODS = {
123 serverUpsertKeybinding: "server.upsertKeybinding",
124 serverGetSettings: "server.getSettings",
125 serverUpdateSettings: "server.updateSettings",
126 + serverDiscoverSourceControl: "server.discoverSourceControl",
127
128 // Streaming subscriptions
124 - subscribeGitStatus: "subscribeGitStatus",
129 + subscribeVcsStatus: "subscribeVcsStatus",
130 subscribeTerminalEvents: "subscribeTerminalEvents",
131 subscribeServerConfig: "subscribeServerConfig",
132 subscribeServerLifecycle: "subscribeServerLifecycle",
@@ -165,6 +170,11 @@ export const WsServerUpdateSettingsRpc = Rpc.make(WS_METHODS.serverUpdateSetting
170 error: ServerSettingsError,
171 });
172
173 +export const WsServerDiscoverSourceControlRpc = Rpc.make(WS_METHODS.serverDiscoverSourceControl, {
174 + payload: Schema.Struct({}),
175 + success: SourceControlDiscoveryResult,
176 +});
177 +
178 export const WsProjectsSearchEntriesRpc = Rpc.make(WS_METHODS.projectsSearchEntries, {
179 payload: ProjectSearchEntriesInput,
180 success: ProjectSearchEntriesResult,
@@ -188,22 +198,22 @@ export const WsFilesystemBrowseRpc = Rpc.make(WS_METHODS.filesystemBrowse, {
198 error: FilesystemBrowseError,
199 });
200
191 -export const WsSubscribeGitStatusRpc = Rpc.make(WS_METHODS.subscribeGitStatus, {
192 - payload: GitStatusInput,
193 - success: GitStatusStreamEvent,
201 +export const WsSubscribeVcsStatusRpc = Rpc.make(WS_METHODS.subscribeVcsStatus, {
202 + payload: VcsStatusInput,
203 + success: VcsStatusStreamEvent,
204 error: GitManagerServiceError,
205 stream: true,
206 });
207
198 -export const WsGitPullRpc = Rpc.make(WS_METHODS.gitPull, {
199 - payload: GitPullInput,
200 - success: GitPullResult,
208 +export const WsVcsPullRpc = Rpc.make(WS_METHODS.vcsPull, {
209 + payload: VcsPullInput,
210 + success: VcsPullResult,
211 error: GitCommandError,
212 });
213
204 -export const WsGitRefreshStatusRpc = Rpc.make(WS_METHODS.gitRefreshStatus, {
205 - payload: GitStatusInput,
206 - success: GitStatusResult,
214 +export const WsVcsRefreshStatusRpc = Rpc.make(WS_METHODS.vcsRefreshStatus, {
215 + payload: VcsStatusInput,
216 + success: VcsStatusResult,
217 error: GitManagerServiceError,
218 });
219
@@ -226,38 +236,38 @@ export const WsGitPreparePullRequestThreadRpc = Rpc.make(WS_METHODS.gitPreparePu
236 error: GitManagerServiceError,
237 });
238
229 -export const WsGitListBranchesRpc = Rpc.make(WS_METHODS.gitListBranches, {
230 - payload: GitListBranchesInput,
231 - success: GitListBranchesResult,
239 +export const WsVcsListRefsRpc = Rpc.make(WS_METHODS.vcsListRefs, {
240 + payload: VcsListRefsInput,
241 + success: VcsListRefsResult,
242 error: GitCommandError,
243 });
244
235 -export const WsGitCreateWorktreeRpc = Rpc.make(WS_METHODS.gitCreateWorktree, {
236 - payload: GitCreateWorktreeInput,
237 - success: GitCreateWorktreeResult,
245 +export const WsVcsCreateWorktreeRpc = Rpc.make(WS_METHODS.vcsCreateWorktree, {
246 + payload: VcsCreateWorktreeInput,
247 + success: VcsCreateWorktreeResult,
248 error: GitCommandError,
249 });
250
241 -export const WsGitRemoveWorktreeRpc = Rpc.make(WS_METHODS.gitRemoveWorktree, {
242 - payload: GitRemoveWorktreeInput,
251 +export const WsVcsRemoveWorktreeRpc = Rpc.make(WS_METHODS.vcsRemoveWorktree, {
252 + payload: VcsRemoveWorktreeInput,
253 error: GitCommandError,
254 });
255
246 -export const WsGitCreateBranchRpc = Rpc.make(WS_METHODS.gitCreateBranch, {
247 - payload: GitCreateBranchInput,
248 - success: GitCreateBranchResult,
256 +export const WsVcsCreateRefRpc = Rpc.make(WS_METHODS.vcsCreateRef, {
257 + payload: VcsCreateRefInput,
258 + success: VcsCreateRefResult,
259 error: GitCommandError,
260 });
261
252 -export const WsGitCheckoutRpc = Rpc.make(WS_METHODS.gitCheckout, {
253 - payload: GitCheckoutInput,
254 - success: GitCheckoutResult,
262 +export const WsVcsSwitchRefRpc = Rpc.make(WS_METHODS.vcsSwitchRef, {
263 + payload: VcsSwitchRefInput,
264 + success: VcsSwitchRefResult,
265 error: GitCommandError,
266 });
267
258 -export const WsGitInitRpc = Rpc.make(WS_METHODS.gitInit, {
259 - payload: GitInitInput,
260 - error: GitCommandError,
268 +export const WsVcsInitRpc = Rpc.make(WS_METHODS.vcsInit, {
269 + payload: VcsInitInput,
270 + error: VcsError,
271 });
272
273 export const WsTerminalOpenRpc = Rpc.make(WS_METHODS.terminalOpen, {
@@ -370,22 +380,23 @@ export const WsRpcGroup = RpcGroup.make(
380 WsServerUpsertKeybindingRpc,
381 WsServerGetSettingsRpc,
382 WsServerUpdateSettingsRpc,
383 + WsServerDiscoverSourceControlRpc,
384 WsProjectsSearchEntriesRpc,
385 WsProjectsWriteFileRpc,
386 WsShellOpenInEditorRpc,
387 WsFilesystemBrowseRpc,
377 - WsSubscribeGitStatusRpc,
378 - WsGitPullRpc,
379 - WsGitRefreshStatusRpc,
388 + WsSubscribeVcsStatusRpc,
389 + WsVcsPullRpc,
390 + WsVcsRefreshStatusRpc,
391 WsGitRunStackedActionRpc,
392 WsGitResolvePullRequestRpc,
393 WsGitPreparePullRequestThreadRpc,
383 - WsGitListBranchesRpc,
384 - WsGitCreateWorktreeRpc,
385 - WsGitRemoveWorktreeRpc,
386 - WsGitCreateBranchRpc,
387 - WsGitCheckoutRpc,
388 - WsGitInitRpc,
394 + WsVcsListRefsRpc,
395 + WsVcsCreateWorktreeRpc,
396 + WsVcsRemoveWorktreeRpc,
397 + WsVcsCreateRefRpc,
398 + WsVcsSwitchRefRpc,
399 + WsVcsInitRpc,
400 WsTerminalOpenRpc,
401 WsTerminalWriteRpc,
402 WsTerminalResizeRpc,
packages/contracts/src/sourceControl.ts new
+111
@@ -0,0 +1,111 @@
1 +import { Schema } from "effect";
2 +import { PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3 +import { VcsDriverKind } from "./vcs.ts";
4 +
5 +export const SourceControlProviderKind = Schema.Literals([
6 + "github",
7 + "gitlab",
8 + "azure-devops",
9 + "bitbucket",
10 + "unknown",
11 +]);
12 +export type SourceControlProviderKind = typeof SourceControlProviderKind.Type;
13 +
14 +export const SourceControlProviderInfo = Schema.Struct({
15 + kind: SourceControlProviderKind,
16 + name: TrimmedNonEmptyString,
17 + baseUrl: Schema.String,
18 +});
19 +export type SourceControlProviderInfo = typeof SourceControlProviderInfo.Type;
20 +
21 +export const ChangeRequestState = Schema.Literals(["open", "closed", "merged"]);
22 +export type ChangeRequestState = typeof ChangeRequestState.Type;
23 +
24 +export const ChangeRequest = Schema.Struct({
25 + provider: SourceControlProviderKind,
26 + number: PositiveInt,
27 + title: TrimmedNonEmptyString,
28 + url: Schema.String,
29 + baseRefName: TrimmedNonEmptyString,
30 + headRefName: TrimmedNonEmptyString,
31 + state: ChangeRequestState,
32 + updatedAt: Schema.Option(Schema.DateTimeUtc),
33 + isCrossRepository: Schema.optional(Schema.Boolean),
34 + headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
35 + headRepositoryOwnerLogin: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
36 +});
37 +export type ChangeRequest = typeof ChangeRequest.Type;
38 +
39 +export const SourceControlRepositoryCloneUrls = Schema.Struct({
40 + nameWithOwner: TrimmedNonEmptyString,
41 + url: TrimmedNonEmptyString,
42 + sshUrl: TrimmedNonEmptyString,
43 +});
44 +export type SourceControlRepositoryCloneUrls = typeof SourceControlRepositoryCloneUrls.Type;
45 +
46 +export const SourceControlDiscoveryStatus = Schema.Literals(["available", "missing"]);
47 +export type SourceControlDiscoveryStatus = typeof SourceControlDiscoveryStatus.Type;
48 +
49 +export const SourceControlProviderAuthStatus = Schema.Literals([
50 + "authenticated",
51 + "unauthenticated",
52 + "unknown",
53 +]);
54 +export type SourceControlProviderAuthStatus = typeof SourceControlProviderAuthStatus.Type;
55 +
56 +export const SourceControlProviderAuth = Schema.Struct({
57 + status: SourceControlProviderAuthStatus,
58 + account: Schema.Option(TrimmedNonEmptyString),
59 + host: Schema.Option(TrimmedNonEmptyString),
60 + detail: Schema.Option(TrimmedNonEmptyString),
61 +});
62 +export type SourceControlProviderAuth = typeof SourceControlProviderAuth.Type;
63 +
64 +const SourceControlDiscoveryItemFields = {
65 + label: TrimmedNonEmptyString,
66 + executable: TrimmedNonEmptyString,
67 + implemented: Schema.Boolean,
68 + status: SourceControlDiscoveryStatus,
69 + version: Schema.Option(TrimmedNonEmptyString),
70 + installHint: TrimmedNonEmptyString,
71 + detail: Schema.Option(TrimmedNonEmptyString),
72 +} as const;
73 +
74 +export const SourceControlDiscoveryItem = Schema.Struct({
75 + kind: Schema.String,
76 + ...SourceControlDiscoveryItemFields,
77 +});
78 +export type SourceControlDiscoveryItem = typeof SourceControlDiscoveryItem.Type;
79 +
80 +export const VcsDiscoveryItem = Schema.Struct({
81 + kind: VcsDriverKind,
82 + ...SourceControlDiscoveryItemFields,
83 +});
84 +export type VcsDiscoveryItem = typeof VcsDiscoveryItem.Type;
85 +
86 +export const SourceControlProviderDiscoveryItem = Schema.Struct({
87 + kind: SourceControlProviderKind,
88 + ...SourceControlDiscoveryItemFields,
89 + auth: SourceControlProviderAuth,
90 +});
91 +export type SourceControlProviderDiscoveryItem = typeof SourceControlProviderDiscoveryItem.Type;
92 +
93 +export const SourceControlDiscoveryResult = Schema.Struct({
94 + versionControlSystems: Schema.Array(VcsDiscoveryItem),
95 + sourceControlProviders: Schema.Array(SourceControlProviderDiscoveryItem),
96 +});
97 +export type SourceControlDiscoveryResult = typeof SourceControlDiscoveryResult.Type;
98 +
99 +export class SourceControlProviderError extends Schema.TaggedErrorClass<SourceControlProviderError>()(
100 + "SourceControlProviderError",
101 + {
102 + provider: SourceControlProviderKind,
103 + operation: Schema.String,
104 + detail: Schema.String,
105 + cause: Schema.optional(Schema.Defect),
106 + },
107 +) {
108 + override get message(): string {
109 + return `Source control provider ${this.provider} failed in ${this.operation}: ${this.detail}`;
110 + }
111 +}
packages/contracts/src/vcs.ts new
+154
@@ -0,0 +1,154 @@
1 +import { Schema } from "effect";
2 +import { TrimmedNonEmptyString } from "./baseSchemas.ts";
3 +
4 +export const VcsDriverKind = Schema.Literals(["git", "jj", "unknown"]);
5 +export type VcsDriverKind = typeof VcsDriverKind.Type;
6 +
7 +export const VcsFreshnessSource = Schema.Literals([
8 + "live-local",
9 + "cached-local",
10 + "cached-remote",
11 + "explicit-remote",
12 +]);
13 +export type VcsFreshnessSource = typeof VcsFreshnessSource.Type;
14 +
15 +export const VcsFreshness = Schema.Struct({
16 + source: VcsFreshnessSource,
17 + observedAt: Schema.DateTimeUtc,
18 + expiresAt: Schema.Option(Schema.DateTimeUtc),
19 +});
20 +export type VcsFreshness = typeof VcsFreshness.Type;
21 +
22 +export const VcsDriverCapabilities = Schema.Struct({
23 + kind: VcsDriverKind,
24 + supportsWorktrees: Schema.Boolean,
25 + supportsBookmarks: Schema.Boolean,
26 + supportsAtomicSnapshot: Schema.Boolean,
27 + supportsPushDefaultRemote: Schema.Boolean,
28 + ignoreClassifier: Schema.Literals(["native", "git-compatible-fallback"]),
29 +});
30 +export type VcsDriverCapabilities = typeof VcsDriverCapabilities.Type;
31 +
32 +export const VcsRepositoryIdentity = Schema.Struct({
33 + kind: VcsDriverKind,
34 + rootPath: TrimmedNonEmptyString,
35 + metadataPath: Schema.NullOr(TrimmedNonEmptyString),
36 + freshness: VcsFreshness,
37 +});
38 +export type VcsRepositoryIdentity = typeof VcsRepositoryIdentity.Type;
39 +
40 +export const VcsListWorkspaceFilesResult = Schema.Struct({
41 + paths: Schema.Array(TrimmedNonEmptyString),
42 + truncated: Schema.Boolean,
43 + freshness: VcsFreshness,
44 +});
45 +export type VcsListWorkspaceFilesResult = typeof VcsListWorkspaceFilesResult.Type;
46 +
47 +export const VcsRemote = Schema.Struct({
48 + name: TrimmedNonEmptyString,
49 + url: TrimmedNonEmptyString,
50 + pushUrl: Schema.Option(TrimmedNonEmptyString),
51 + isPrimary: Schema.Boolean,
52 +});
53 +export type VcsRemote = typeof VcsRemote.Type;
54 +
55 +export const VcsListRemotesResult = Schema.Struct({
56 + remotes: Schema.Array(VcsRemote),
57 + freshness: VcsFreshness,
58 +});
59 +export type VcsListRemotesResult = typeof VcsListRemotesResult.Type;
60 +
61 +export class VcsProcessSpawnError extends Schema.TaggedErrorClass<VcsProcessSpawnError>()(
62 + "VcsProcessSpawnError",
63 + {
64 + operation: Schema.String,
65 + command: Schema.String,
66 + cwd: Schema.String,
67 + cause: Schema.Defect,
68 + },
69 +) {
70 + override get message(): string {
71 + return `VCS process failed to spawn in ${this.operation}: ${this.command} (${this.cwd})`;
72 + }
73 +}
74 +
75 +export class VcsProcessExitError extends Schema.TaggedErrorClass<VcsProcessExitError>()(
76 + "VcsProcessExitError",
77 + {
78 + operation: Schema.String,
79 + command: Schema.String,
80 + cwd: Schema.String,
81 + exitCode: Schema.Number,
82 + detail: Schema.String,
83 + },
84 +) {
85 + override get message(): string {
86 + return `VCS process failed in ${this.operation}: ${this.command} (${this.cwd}) exited with ${this.exitCode} - ${this.detail}`;
87 + }
88 +}
89 +
90 +export class VcsProcessTimeoutError extends Schema.TaggedErrorClass<VcsProcessTimeoutError>()(
91 + "VcsProcessTimeoutError",
92 + {
93 + operation: Schema.String,
94 + command: Schema.String,
95 + cwd: Schema.String,
96 + timeoutMs: Schema.Number,
97 + },
98 +) {
99 + override get message(): string {
100 + return `VCS process timed out in ${this.operation}: ${this.command} (${this.cwd}) after ${this.timeoutMs}ms`;
101 + }
102 +}
103 +
104 +export class VcsOutputDecodeError extends Schema.TaggedErrorClass<VcsOutputDecodeError>()(
105 + "VcsOutputDecodeError",
106 + {
107 + operation: Schema.String,
108 + command: Schema.String,
109 + cwd: Schema.String,
110 + detail: Schema.String,
111 + cause: Schema.optional(Schema.Defect),
112 + },
113 +) {
114 + override get message(): string {
115 + return `VCS output decode failed in ${this.operation}: ${this.command} (${this.cwd}) - ${this.detail}`;
116 + }
117 +}
118 +
119 +export class VcsRepositoryDetectionError extends Schema.TaggedErrorClass<VcsRepositoryDetectionError>()(
120 + "VcsRepositoryDetectionError",
121 + {
122 + operation: Schema.String,
123 + cwd: Schema.String,
124 + detail: Schema.String,
125 + cause: Schema.optional(Schema.Defect),
126 + },
127 +) {
128 + override get message(): string {
129 + return `VCS repository detection failed in ${this.operation}: ${this.cwd} - ${this.detail}`;
130 + }
131 +}
132 +
133 +export class VcsUnsupportedOperationError extends Schema.TaggedErrorClass<VcsUnsupportedOperationError>()(
134 + "VcsUnsupportedOperationError",
135 + {
136 + operation: Schema.String,
137 + kind: VcsDriverKind,
138 + detail: Schema.String,
139 + },
140 +) {
141 + override get message(): string {
142 + return `VCS operation is unsupported for ${this.kind} in ${this.operation}: ${this.detail}`;
143 + }
144 +}
145 +
146 +export const VcsError = Schema.Union([
147 + VcsProcessSpawnError,
148 + VcsProcessExitError,
149 + VcsProcessTimeoutError,
150 + VcsOutputDecodeError,
151 + VcsRepositoryDetectionError,
152 + VcsUnsupportedOperationError,
153 +]);
154 +export type VcsError = typeof VcsError.Type;
packages/shared/package.json
+4
@@ -12,6 +12,10 @@
12 "types": "./src/git.ts",
13 "import": "./src/git.ts"
14 },
15 + "./sourceControl": {
16 + "types": "./src/sourceControl.ts",
17 + "import": "./src/sourceControl.ts"
18 + },
19 "./logging": {
20 "types": "./src/logging.ts",
21 "import": "./src/logging.ts"
packages/shared/src/git.test.ts
+14 -14
@@ -1,4 +1,4 @@
1 -import type { GitStatusRemoteResult, GitStatusResult } from "@t3tools/contracts";
1 +import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
2 import { describe, expect, it } from "vitest";
3
4 import {
@@ -54,17 +54,17 @@ describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
54 });
55
56 describe("isTemporaryWorktreeBranch", () => {
57 - it("matches the generated temporary worktree branch format", () => {
57 + it("matches the generated temporary worktree refName format", () => {
58 expect(isTemporaryWorktreeBranch(buildTemporaryWorktreeBranchName())).toBe(true);
59 });
60
61 - it("matches generated temporary worktree branches", () => {
61 + it("matches generated temporary worktree refs", () => {
62 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
63 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
64 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
65 });
66
67 - it("rejects non-temporary branch names", () => {
67 + it("rejects non-temporary refName names", () => {
68 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
69 expect(isTemporaryWorktreeBranch("main")).toBe(false);
70 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
@@ -73,7 +73,7 @@ describe("isTemporaryWorktreeBranch", () => {
73
74 describe("applyGitStatusStreamEvent", () => {
75 it("treats a remote-only update as a repository when local state is missing", () => {
76 - const remote: GitStatusRemoteResult = {
76 + const remote: VcsStatusRemoteResult = {
77 hasUpstream: true,
78 aheadCount: 2,
79 behindCount: 1,
@@ -82,9 +82,9 @@ describe("applyGitStatusStreamEvent", () => {
82
83 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
84 isRepo: true,
85 - hasOriginRemote: false,
86 - isDefaultBranch: false,
87 - branch: null,
85 + hasPrimaryRemote: false,
86 + isDefaultRef: false,
87 + refName: null,
88 hasWorkingTreeChanges: false,
89 workingTree: { files: [], insertions: 0, deletions: 0 },
90 hasUpstream: true,
@@ -95,16 +95,16 @@ describe("applyGitStatusStreamEvent", () => {
95 });
96
97 it("preserves local-only fields when applying a remote update", () => {
98 - const current: GitStatusResult = {
98 + const current: VcsStatusResult = {
99 isRepo: true,
100 - hostingProvider: {
100 + sourceControlProvider: {
101 kind: "github",
102 name: "GitHub",
103 baseUrl: "https://github.com",
104 },
105 - hasOriginRemote: true,
106 - isDefaultBranch: false,
107 - branch: "feature/demo",
105 + hasPrimaryRemote: true,
106 + isDefaultRef: false,
107 + refName: "feature/demo",
108 hasWorkingTreeChanges: true,
109 workingTree: {
110 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
@@ -117,7 +117,7 @@ describe("applyGitStatusStreamEvent", () => {
117 pr: null,
118 };
119
120 - const remote: GitStatusRemoteResult = {
120 + const remote: VcsStatusRemoteResult = {
121 hasUpstream: true,
122 aheadCount: 2,
123 behindCount: 1,
packages/shared/src/git.ts
+44 -100
@@ -1,19 +1,20 @@
1 import type {
2 - GitBranch,
3 - GitHostingProvider,
4 - GitStatusLocalResult,
5 - GitStatusRemoteResult,
6 - GitStatusResult,
7 - GitStatusStreamEvent,
2 + VcsRef,
3 + SourceControlProviderInfo,
4 + VcsStatusLocalResult,
5 + VcsStatusRemoteResult,
6 + VcsStatusResult,
7 + VcsStatusStreamEvent,
8 } from "@t3tools/contracts";
9 import * as Effect from "effect/Effect";
10 import * as Random from "effect/Random";
11 +import { detectSourceControlProviderFromRemoteUrl } from "./sourceControl.ts";
12
13 export const WORKTREE_BRANCH_PREFIX = "t3code";
14 const TEMP_WORKTREE_BRANCH_PATTERN = new RegExp(`^${WORKTREE_BRANCH_PREFIX}\\/[0-9a-f]{8}$`);
15
16 /**
16 - * Sanitize an arbitrary string into a valid, lowercase git branch fragment.
17 + * Sanitize an arbitrary string into a valid, lowercase git refName fragment.
18 * Strips quotes, collapses separators, limits to 64 chars.
19 */
20 export function sanitizeBranchFragment(raw: string): string {
@@ -35,7 +36,7 @@ export function sanitizeBranchFragment(raw: string): string {
36 }
37
38 /**
38 - * Sanitize a string into a `feature/…` branch name.
39 + * Sanitize a string into a `feature/…` refName name.
40 * Preserves an existing `feature/` prefix or slash-separated namespace.
41 */
42 export function sanitizeFeatureBranchName(raw: string): string {
@@ -49,8 +50,8 @@ export function sanitizeFeatureBranchName(raw: string): string {
50 const AUTO_FEATURE_BRANCH_FALLBACK = "feature/update";
51
52 /**
52 - * Resolve a unique `feature/…` branch name that doesn't collide with
53 - * any existing branch. Appends a numeric suffix when needed.
53 + * Resolve a unique `feature/…` refName name that doesn't collide with
54 + * any existing refName. Appends a numeric suffix when needed.
55 */
56 export function resolveAutoFeatureBranchName(
57 existingBranchNames: readonly string[],
@@ -60,7 +61,7 @@ export function resolveAutoFeatureBranchName(
61 const resolvedBase = sanitizeFeatureBranchName(
62 preferred && preferred.length > 0 ? preferred : AUTO_FEATURE_BRANCH_FALLBACK,
63 );
63 - const existingNames = new Set(existingBranchNames.map((branch) => branch.toLowerCase()));
64 + const existingNames = new Set(existingBranchNames.map((refName) => refName.toLowerCase()));
65
66 if (!existingNames.has(resolvedBase)) {
67 return resolvedBase;
@@ -90,8 +91,8 @@ export function buildTemporaryWorktreeBranchName(): string {
91 return `${WORKTREE_BRANCH_PREFIX}/${token}`;
92 }
93
93 -export function isTemporaryWorktreeBranch(branch: string): boolean {
94 - return TEMP_WORKTREE_BRANCH_PATTERN.test(branch.trim().toLowerCase());
94 +export function isTemporaryWorktreeBranch(refName: string): boolean {
95 + return TEMP_WORKTREE_BRANCH_PATTERN.test(refName.trim().toLowerCase());
96 }
97
98 /**
@@ -165,98 +166,39 @@ function deriveLocalBranchNameCandidatesFromRemoteRef(
166 }
167
168 /**
168 - * Hide `origin/*` remote refs when a matching local branch already exists.
169 + * Hide `origin/*` remote refs when a matching local refName already exists.
170 */
171 export function dedupeRemoteBranchesWithLocalMatches(
171 - branches: ReadonlyArray<GitBranch>,
172 -): ReadonlyArray<GitBranch> {
172 + refs: ReadonlyArray<VcsRef>,
173 +): ReadonlyArray<VcsRef> {
174 const localBranchNames = new Set(
174 - branches.filter((branch) => !branch.isRemote).map((branch) => branch.name),
175 + refs.filter((refName) => !refName.isRemote).map((refName) => refName.name),
176 );
177
177 - return branches.filter((branch) => {
178 - if (!branch.isRemote) {
178 + return refs.filter((refName) => {
179 + if (!refName.isRemote) {
180 return true;
181 }
182
182 - if (branch.remoteName !== "origin") {
183 + if (refName.remoteName !== "origin") {
184 return true;
185 }
186
187 const localBranchCandidates = deriveLocalBranchNameCandidatesFromRemoteRef(
187 - branch.name,
188 - branch.remoteName,
188 + refName.name,
189 + refName.remoteName,
190 );
191 return !localBranchCandidates.some((candidate) => localBranchNames.has(candidate));
192 });
193 }
194
194 -function parseGitRemoteHost(remoteUrl: string): string | null {
195 - const trimmed = remoteUrl.trim();
196 - if (trimmed.length === 0) {
197 - return null;
198 - }
199 -
200 - if (trimmed.startsWith("git@")) {
201 - const hostWithPath = trimmed.slice("git@".length);
202 - const separatorIndex = hostWithPath.search(/[:/]/);
203 - if (separatorIndex <= 0) {
204 - return null;
205 - }
206 - return hostWithPath.slice(0, separatorIndex).toLowerCase();
207 - }
208 -
209 - try {
210 - return new URL(trimmed).hostname.toLowerCase();
211 - } catch {
212 - return null;
213 - }
214 -}
215 -
216 -function toBaseUrl(host: string): string {
217 - return `https://${host}`;
218 -}
219 -
220 -function isGitHubHost(host: string): boolean {
221 - return host === "github.com" || host.includes("github");
222 -}
223 -
224 -function isGitLabHost(host: string): boolean {
225 - return host === "gitlab.com" || host.includes("gitlab");
226 -}
227 -
228 -export function detectGitHostingProviderFromRemoteUrl(
195 +export function detectSourceControlProviderFromGitRemoteUrl(
196 remoteUrl: string,
230 -): GitHostingProvider | null {
231 - const host = parseGitRemoteHost(remoteUrl);
232 - if (!host) {
233 - return null;
234 - }
235 -
236 - if (isGitHubHost(host)) {
237 - return {
238 - kind: "github",
239 - name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted",
240 - baseUrl: toBaseUrl(host),
241 - };
242 - }
243 -
244 - if (isGitLabHost(host)) {
245 - return {
246 - kind: "gitlab",
247 - name: host === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
248 - baseUrl: toBaseUrl(host),
249 - };
250 - }
251 -
252 - return {
253 - kind: "unknown",
254 - name: host,
255 - baseUrl: toBaseUrl(host),
256 - };
197 +): SourceControlProviderInfo | null {
198 + return detectSourceControlProviderFromRemoteUrl(remoteUrl);
199 }
200
259 -const EMPTY_GIT_STATUS_REMOTE: GitStatusRemoteResult = {
201 +const EMPTY_GIT_STATUS_REMOTE: VcsStatusRemoteResult = {
202 hasUpstream: false,
203 aheadCount: 0,
204 behindCount: 0,
@@ -264,16 +206,16 @@ const EMPTY_GIT_STATUS_REMOTE: GitStatusRemoteResult = {
206 };
207
208 export function mergeGitStatusParts(
267 - local: GitStatusLocalResult,
268 - remote: GitStatusRemoteResult | null,
269 -): GitStatusResult {
209 + local: VcsStatusLocalResult,
210 + remote: VcsStatusRemoteResult | null,
211 +): VcsStatusResult {
212 return {
213 ...local,
214 ...(remote ?? EMPTY_GIT_STATUS_REMOTE),
215 };
216 }
217
276 -function toRemoteStatusPart(status: GitStatusResult): GitStatusRemoteResult {
218 +function toRemoteStatusPart(status: VcsStatusResult): VcsStatusRemoteResult {
219 return {
220 hasUpstream: status.hasUpstream,
221 aheadCount: status.aheadCount,
@@ -282,22 +224,24 @@ function toRemoteStatusPart(status: GitStatusResult): GitStatusRemoteResult {
224 };
225 }
226
285 -function toLocalStatusPart(status: GitStatusResult): GitStatusLocalResult {
227 +function toLocalStatusPart(status: VcsStatusResult): VcsStatusLocalResult {
228 return {
229 isRepo: status.isRepo,
288 - ...(status.hostingProvider ? { hostingProvider: status.hostingProvider } : {}),
289 - hasOriginRemote: status.hasOriginRemote,
290 - isDefaultBranch: status.isDefaultBranch,
291 - branch: status.branch,
230 + ...(status.sourceControlProvider
231 + ? { sourceControlProvider: status.sourceControlProvider }
232 + : {}),
233 + hasPrimaryRemote: status.hasPrimaryRemote,
234 + isDefaultRef: status.isDefaultRef,
235 + refName: status.refName,
236 hasWorkingTreeChanges: status.hasWorkingTreeChanges,
237 workingTree: status.workingTree,
238 };
239 }
240
241 export function applyGitStatusStreamEvent(
298 - current: GitStatusResult | null,
299 - event: GitStatusStreamEvent,
300 -): GitStatusResult {
242 + current: VcsStatusResult | null,
243 + event: VcsStatusStreamEvent,
244 +): VcsStatusResult {
245 switch (event._tag) {
246 case "snapshot":
247 return mergeGitStatusParts(event.local, event.remote);
@@ -308,9 +252,9 @@ export function applyGitStatusStreamEvent(
252 return mergeGitStatusParts(
253 {
254 isRepo: true,
311 - hasOriginRemote: false,
312 - isDefaultBranch: false,
313 - branch: null,
255 + hasPrimaryRemote: false,
256 + isDefaultRef: false,
257 + refName: null,
258 hasWorkingTreeChanges: false,
259 workingTree: { files: [], insertions: 0, deletions: 0 },
260 },
packages/shared/src/sourceControl.test.ts new
+59
@@ -0,0 +1,59 @@
1 +import { describe, expect, it } from "vitest";
2 +
3 +import {
4 + detectSourceControlProviderFromRemoteUrl,
5 + getChangeRequestTerminologyForKind,
6 + resolveChangeRequestPresentation,
7 +} from "./sourceControl.ts";
8 +
9 +describe("source control presentation", () => {
10 + it("uses merge request terminology for GitLab", () => {
11 + expect(getChangeRequestTerminologyForKind("gitlab")).toEqual({
12 + shortLabel: "MR",
13 + singular: "merge request",
14 + });
15 + });
16 +
17 + it("uses pull request terminology for GitHub-compatible providers", () => {
18 + expect(getChangeRequestTerminologyForKind("github")).toEqual({
19 + shortLabel: "PR",
20 + singular: "pull request",
21 + });
22 + expect(getChangeRequestTerminologyForKind("azure-devops")).toEqual({
23 + shortLabel: "PR",
24 + singular: "pull request",
25 + });
26 + expect(getChangeRequestTerminologyForKind("bitbucket")).toEqual({
27 + shortLabel: "PR",
28 + singular: "pull request",
29 + });
30 + });
31 +
32 + it("falls back to generic change request copy for unknown providers", () => {
33 + expect(
34 + resolveChangeRequestPresentation({ kind: "unknown", name: "forge", baseUrl: "" }),
35 + ).toEqual(
36 + expect.objectContaining({
37 + shortName: "change request",
38 + longName: "change request",
39 + }),
40 + );
41 + });
42 +});
43 +
44 +describe("detectSourceControlProviderFromRemoteUrl", () => {
45 + it("detects common source control hosts", () => {
46 + expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe(
47 + "github",
48 + );
49 + expect(
50 + detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
51 + ).toBe("gitlab");
52 + expect(
53 + detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
54 + ).toBe("azure-devops");
55 + expect(
56 + detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
57 + ).toBe("bitbucket");
58 + });
59 +});
packages/shared/src/sourceControl.ts new
+225
@@ -0,0 +1,225 @@
1 +import type { SourceControlProviderInfo, SourceControlProviderKind } from "@t3tools/contracts";
2 +
3 +export interface ChangeRequestPresentation {
4 + readonly icon: "github" | "gitlab" | "azure-devops" | "bitbucket" | "change-request";
5 + readonly providerName: string;
6 + readonly shortName: string;
7 + readonly longName: string;
8 + readonly pluralLongName: string;
9 + readonly providerLongName: string;
10 + readonly checkoutCommandExample: string;
11 + readonly urlExample: string;
12 +}
13 +
14 +export interface ChangeRequestTerminology {
15 + readonly shortLabel: string;
16 + readonly singular: string;
17 +}
18 +
19 +export const DEFAULT_CHANGE_REQUEST_TERMINOLOGY: ChangeRequestTerminology = {
20 + shortLabel: "PR",
21 + singular: "pull request",
22 +};
23 +
24 +const GITHUB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
25 + icon: "github",
26 + providerName: "GitHub",
27 + shortName: "PR",
28 + longName: "pull request",
29 + pluralLongName: "pull requests",
30 + providerLongName: "GitHub pull request",
31 + checkoutCommandExample: "gh pr checkout 123",
32 + urlExample: "https://github.com/owner/repo/pull/42",
33 +};
34 +
35 +const GITLAB_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
36 + icon: "gitlab",
37 + providerName: "GitLab",
38 + shortName: "MR",
39 + longName: "merge request",
40 + pluralLongName: "merge requests",
41 + providerLongName: "GitLab merge request",
42 + checkoutCommandExample: "glab mr checkout 123",
43 + urlExample: "https://gitlab.com/group/project/-/merge_requests/42",
44 +};
45 +
46 +const AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
47 + icon: "azure-devops",
48 + providerName: "Azure DevOps",
49 + shortName: "PR",
50 + longName: "pull request",
51 + pluralLongName: "pull requests",
52 + providerLongName: "Azure DevOps pull request",
53 + checkoutCommandExample: "az repos pr checkout --id 123",
54 + urlExample: "https://dev.azure.com/org/project/_git/repo/pullrequest/42",
55 +};
56 +
57 +const BITBUCKET_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
58 + icon: "bitbucket",
59 + providerName: "Bitbucket",
60 + shortName: "PR",
61 + longName: "pull request",
62 + pluralLongName: "pull requests",
63 + providerLongName: "Bitbucket pull request",
64 + checkoutCommandExample: "bb pr checkout 123",
65 + urlExample: "https://bitbucket.org/workspace/repo/pull-requests/42",
66 +};
67 +
68 +const GENERIC_CHANGE_REQUEST_PRESENTATION: ChangeRequestPresentation = {
69 + icon: "change-request",
70 + providerName: "source control",
71 + shortName: "change request",
72 + longName: "change request",
73 + pluralLongName: "change requests",
74 + providerLongName: "change request",
75 + checkoutCommandExample: "123",
76 + urlExample: "#42",
77 +};
78 +
79 +export function resolveChangeRequestPresentation(
80 + provider: SourceControlProviderInfo | null | undefined,
81 +): ChangeRequestPresentation {
82 + switch (provider?.kind) {
83 + case "github":
84 + case undefined:
85 + return GITHUB_CHANGE_REQUEST_PRESENTATION;
86 + case "gitlab":
87 + return GITLAB_CHANGE_REQUEST_PRESENTATION;
88 + case "azure-devops":
89 + return AZURE_DEVOPS_CHANGE_REQUEST_PRESENTATION;
90 + case "bitbucket":
91 + return BITBUCKET_CHANGE_REQUEST_PRESENTATION;
92 + case "unknown":
93 + return GENERIC_CHANGE_REQUEST_PRESENTATION;
94 + }
95 +}
96 +
97 +export function resolveChangeRequestPresentationForKind(
98 + kind: SourceControlProviderKind,
99 +): ChangeRequestPresentation {
100 + return resolveChangeRequestPresentation({ kind, name: "", baseUrl: "" });
101 +}
102 +
103 +export function formatChangeRequestAction(
104 + verb: "View" | "Create",
105 + presentation: ChangeRequestPresentation,
106 +): string {
107 + return `${verb} ${presentation.shortName}`;
108 +}
109 +
110 +export function formatCreateChangeRequestPhrase(presentation: ChangeRequestPresentation): string {
111 + return `create ${presentation.shortName}`;
112 +}
113 +
114 +export function getChangeRequestTerminology(
115 + provider: SourceControlProviderInfo | null | undefined,
116 +): ChangeRequestTerminology {
117 + if (!provider) {
118 + return DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
119 + }
120 +
121 + const presentation = resolveChangeRequestPresentation(provider);
122 + return {
123 + shortLabel: presentation.shortName,
124 + singular: presentation.longName,
125 + };
126 +}
127 +
128 +export function getChangeRequestTerminologyForKind(
129 + kind: SourceControlProviderKind,
130 +): ChangeRequestTerminology {
131 + const presentation = resolveChangeRequestPresentationForKind(kind);
132 + return {
133 + shortLabel: presentation.shortName,
134 + singular: presentation.longName,
135 + };
136 +}
137 +
138 +function parseRemoteHost(remoteUrl: string): string | null {
139 + const trimmed = remoteUrl.trim();
140 + if (trimmed.length === 0) {
141 + return null;
142 + }
143 +
144 + if (trimmed.startsWith("git@")) {
145 + const hostWithPath = trimmed.slice("git@".length);
146 + const separatorIndex = hostWithPath.search(/[:/]/);
147 + if (separatorIndex <= 0) {
148 + return null;
149 + }
150 + return hostWithPath.slice(0, separatorIndex).toLowerCase();
151 + }
152 +
153 + try {
154 + return new URL(trimmed).hostname.toLowerCase();
155 + } catch {
156 + return null;
157 + }
158 +}
159 +
160 +function toBaseUrl(host: string): string {
161 + return `https://${host}`;
162 +}
163 +
164 +function isGitHubHost(host: string): boolean {
165 + return host === "github.com" || host.includes("github");
166 +}
167 +
168 +function isGitLabHost(host: string): boolean {
169 + return host === "gitlab.com" || host.includes("gitlab");
170 +}
171 +
172 +function isAzureDevOpsHost(host: string): boolean {
173 + return host === "dev.azure.com" || host.endsWith(".visualstudio.com");
174 +}
175 +
176 +function isBitbucketHost(host: string): boolean {
177 + return host === "bitbucket.org" || host.includes("bitbucket");
178 +}
179 +
180 +export function detectSourceControlProviderFromRemoteUrl(
181 + remoteUrl: string,
182 +): SourceControlProviderInfo | null {
183 + const host = parseRemoteHost(remoteUrl);
184 + if (!host) {
185 + return null;
186 + }
187 +
188 + if (isGitHubHost(host)) {
189 + return {
190 + kind: "github",
191 + name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted",
192 + baseUrl: toBaseUrl(host),
193 + };
194 + }
195 +
196 + if (isGitLabHost(host)) {
197 + return {
198 + kind: "gitlab",
199 + name: host === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
200 + baseUrl: toBaseUrl(host),
201 + };
202 + }
203 +
204 + if (isAzureDevOpsHost(host)) {
205 + return {
206 + kind: "azure-devops",
207 + name: "Azure DevOps",
208 + baseUrl: toBaseUrl(host),
209 + };
210 + }
211 +
212 + if (isBitbucketHost(host)) {
213 + return {
214 + kind: "bitbucket",
215 + name: host === "bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted",
216 + baseUrl: toBaseUrl(host),
217 + };
218 + }
219 +
220 + return {
221 + kind: "unknown",
222 + name: host,
223 + baseUrl: toBaseUrl(host),
224 + };
225 +}