Skip to content

Commit bbecb79

Browse files
committed
wip: opt environment in to tracking a branch
Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
1 parent 962825b commit bbecb79

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

environment/state.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ type State struct {
1010
CreatedAt time.Time `json:"created_at,omitempty"`
1111
UpdatedAt time.Time `json:"updated_at,omitempty"`
1212

13-
Config *EnvironmentConfig `json:"config,omitempty"`
14-
Container string `json:"container,omitempty"`
15-
Title string `json:"title,omitempty"`
13+
Config *EnvironmentConfig `json:"config,omitempty"`
14+
Container string `json:"container,omitempty"`
15+
Title string `json:"title,omitempty"`
16+
TrackingBranch string `json:"tracking_branch,omitempty"`
1617
}
1718

1819
func (s *State) Marshal() ([]byte, error) {

mcpserver/tools.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func init() {
131131
EnvironmentOpenTool,
132132
EnvironmentCreateTool,
133133
EnvironmentUpdateMetadataTool,
134+
EnvironmentEnableTrackingTool,
134135
EnvironmentConfigTool,
135136

136137
EnvironmentRunCmdTool,
@@ -312,6 +313,42 @@ var EnvironmentUpdateMetadataTool = &Tool{
312313
},
313314
}
314315

316+
var EnvironmentEnableTrackingTool = &Tool{
317+
Definition: newEnvironmentTool(
318+
"environment_enable_tracking",
319+
"Enable branch tracking for an environment. When enabled, environment changes will be automatically synced to the user's working tree when on the tracked branch. CRITICAL: This is an opt-in feature that can only be enabled by explicit user request.",
320+
),
321+
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
322+
repo, env, err := openEnvironment(ctx, request)
323+
if err != nil {
324+
return nil, err
325+
}
326+
327+
// Get the current branch from the user's repository
328+
currentBranch, err := repository.RunGitCommand(ctx, repo.SourcePath(), "branch", "--show-current")
329+
if err != nil {
330+
return nil, fmt.Errorf("unable to determine current branch: %w", err)
331+
}
332+
currentBranch = strings.TrimSpace(currentBranch)
333+
if currentBranch == "" {
334+
return nil, fmt.Errorf("not on a branch (detached HEAD state) - cannot enable tracking")
335+
}
336+
337+
// Set the tracking branch to the current branch
338+
env.State.TrackingBranch = currentBranch
339+
340+
if err := repo.Update(ctx, env, request.GetString("explanation", "")); err != nil {
341+
return nil, fmt.Errorf("unable to update the environment: %w", err)
342+
}
343+
344+
out, err := marshalEnvironment(env)
345+
if err != nil {
346+
return nil, fmt.Errorf("failed to marshal environment: %w", err)
347+
}
348+
return mcp.NewToolResultText(fmt.Sprintf("Branch tracking enabled for branch '%s'. Environment changes will now be synced to the working tree when on this branch.\n%s", currentBranch, out)), nil
349+
},
350+
}
351+
315352
var EnvironmentConfigTool = &Tool{
316353
Definition: newEnvironmentTool(
317354
"environment_config",

repository/repository.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,25 @@ func (r *Repository) Update(ctx context.Context, env *environment.Environment, e
283283
if err := r.propagateToWorktree(ctx, env, explanation); err != nil {
284284
return err
285285
}
286+
287+
// Check if branch tracking is enabled and we're on the tracked branch
288+
if env.State.TrackingBranch != "" {
289+
currentBranch, err := RunGitCommand(ctx, r.userRepoPath, "branch", "--show-current")
290+
if err != nil {
291+
// Log the error but don't fail the update
292+
slog.Warn("Failed to check current branch for tracking", "error", err)
293+
} else {
294+
currentBranch = strings.TrimSpace(currentBranch)
295+
if currentBranch == env.State.TrackingBranch {
296+
// Apply environment changes to the user's working tree
297+
if err := r.Apply(ctx, env.ID, io.Discard); err != nil {
298+
// Log the error but don't fail the update to avoid breaking the environment
299+
slog.Warn("Failed to apply tracking changes to working tree", "error", err, "branch", currentBranch)
300+
}
301+
}
302+
}
303+
}
304+
286305
if note := env.Notes.Pop(); note != "" {
287306
return r.addGitNote(ctx, env, note)
288307
}

0 commit comments

Comments
 (0)