Skip to content

Commit 3a90478

Browse files
authored
Merge pull request #2157 from dgageot/board/we-don-t-need-filewritetracker-anymore-i-07f1f5ff
Remove unused fileWriteTracker from creator package
2 parents b837f3d + 5143bc6 commit 3a90478

2 files changed

Lines changed: 0 additions & 152 deletions

File tree

pkg/creator/agent.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ package creator
55
import (
66
"context"
77
_ "embed"
8-
"encoding/json"
98
"fmt"
109
"strings"
1110

1211
"github.com/goccy/go-yaml"
1312

1413
"github.com/docker/docker-agent/pkg/config"
15-
"github.com/docker/docker-agent/pkg/config/latest"
1614
"github.com/docker/docker-agent/pkg/team"
1715
"github.com/docker/docker-agent/pkg/teamloader"
18-
"github.com/docker/docker-agent/pkg/tools"
19-
"github.com/docker/docker-agent/pkg/tools/builtin"
2016
)
2117

2218
//go:embed instructions.txt
@@ -46,14 +42,11 @@ func Agent(ctx context.Context, runConfig *config.RuntimeConfig, modelNameOverri
4642
return nil, fmt.Errorf("building creator config: %w", err)
4743
}
4844

49-
registry := createToolsetRegistry(runConfig.WorkingDir)
50-
5145
return teamloader.Load(
5246
ctx,
5347
config.NewBytesSource("creator", configYAML),
5448
runConfig,
5549
teamloader.WithModelOverrides([]string{modelNameOverride}),
56-
teamloader.WithToolsetRegistry(registry),
5750
)
5851
}
5952

@@ -111,64 +104,3 @@ func buildCreatorConfigYAML(instructions string) ([]byte, error) {
111104

112105
return yaml.Marshal(fullConfig)
113106
}
114-
115-
// createToolsetRegistry creates a custom toolset registry that wraps the filesystem
116-
// toolset to track file paths written by the agent.
117-
func createToolsetRegistry(workingDir string) *teamloader.ToolsetRegistry {
118-
tracker := &fileWriteTracker{
119-
ToolSet: builtin.NewFilesystemTool(workingDir),
120-
}
121-
122-
registry := teamloader.NewDefaultToolsetRegistry()
123-
registry.Register("filesystem", func(context.Context, latest.Toolset, string, *config.RuntimeConfig, string) (tools.ToolSet, error) {
124-
return tracker, nil
125-
})
126-
127-
return registry
128-
}
129-
130-
// fileWriteTracker wraps a filesystem toolset to track files written by the agent.
131-
// This allows the creator to know what files were created during the session.
132-
type fileWriteTracker struct {
133-
tools.ToolSet
134-
originalWriteFileHandler tools.ToolHandler
135-
path string
136-
}
137-
138-
// Tools returns the available tools, wrapping the write_file tool to track paths.
139-
func (t *fileWriteTracker) Tools(ctx context.Context) ([]tools.Tool, error) {
140-
innerTools, err := t.ToolSet.Tools(ctx)
141-
if err != nil {
142-
return nil, err
143-
}
144-
145-
for i, tool := range innerTools {
146-
if tool.Name == builtin.ToolNameWriteFile {
147-
t.originalWriteFileHandler = tool.Handler
148-
innerTools[i].Handler = t.trackWriteFile
149-
}
150-
}
151-
152-
return innerTools, nil
153-
}
154-
155-
// trackWriteFile intercepts write_file calls to track the path being written.
156-
func (t *fileWriteTracker) trackWriteFile(ctx context.Context, toolCall tools.ToolCall) (*tools.ToolCallResult, error) {
157-
var args struct {
158-
Path string `json:"path"`
159-
Content string `json:"content"`
160-
}
161-
if err := json.Unmarshal([]byte(toolCall.Function.Arguments), &args); err != nil {
162-
return nil, fmt.Errorf("failed to parse write_file arguments: %w", err)
163-
}
164-
165-
t.path = args.Path
166-
167-
return t.originalWriteFileHandler(ctx, toolCall)
168-
}
169-
170-
// LastWrittenPath returns the path of the last file written by the agent.
171-
// Returns an empty string if no file has been written yet.
172-
func (t *fileWriteTracker) LastWrittenPath() string {
173-
return t.path
174-
}

pkg/creator/agent_test.go

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,13 @@ package creator
33
import (
44
"testing"
55

6-
"github.com/goccy/go-yaml"
76
"github.com/stretchr/testify/assert"
87
"github.com/stretchr/testify/require"
98

109
"github.com/docker/docker-agent/pkg/config"
11-
"github.com/docker/docker-agent/pkg/config/latest"
1210
"github.com/docker/docker-agent/pkg/environment"
1311
)
1412

15-
func TestAgentConfigYAML(t *testing.T) {
16-
t.Parallel()
17-
18-
// Build the same structure as the buildCreatorConfigYAML function
19-
agentToolsets := []map[string]any{
20-
{"type": "shell"},
21-
{"type": "filesystem"},
22-
}
23-
24-
rootAgent := yaml.MapSlice{
25-
{Key: "model", Value: "auto"},
26-
{Key: "welcome_message", Value: "Hello! I'm here to create agents for you.\n\nCan you explain to me what the agent will be used for?"},
27-
{Key: "instruction", Value: "Some test instructions"},
28-
{Key: "toolsets", Value: agentToolsets},
29-
}
30-
31-
agentsMapSlice := yaml.MapSlice{
32-
{Key: "root", Value: rootAgent},
33-
}
34-
35-
newAgentConfig := yaml.MapSlice{
36-
{Key: "agents", Value: agentsMapSlice},
37-
}
38-
39-
data, err := yaml.Marshal(newAgentConfig)
40-
require.NoError(t, err)
41-
42-
t.Logf("YAML output:\n%s", string(data))
43-
44-
// Verify it can be loaded by the config loader
45-
cfg, err := config.Load(t.Context(), config.NewBytesSource("test", data))
46-
require.NoError(t, err)
47-
48-
// Verify the config has the expected structure
49-
require.Len(t, cfg.Agents, 1)
50-
assert.Equal(t, "root", cfg.Agents[0].Name)
51-
assert.Equal(t, "auto", cfg.Agents[0].Model)
52-
assert.Contains(t, cfg.Agents[0].WelcomeMessage, "Hello!")
53-
require.Len(t, cfg.Agents[0].Toolsets, 2)
54-
assert.Equal(t, "shell", cfg.Agents[0].Toolsets[0].Type)
55-
assert.Equal(t, "filesystem", cfg.Agents[0].Toolsets[1].Type)
56-
}
57-
5813
func TestBuildCreatorConfigYAML(t *testing.T) {
5914
t.Parallel()
6015

@@ -144,45 +99,6 @@ func TestAgent(t *testing.T) {
14499
assert.Contains(t, toolNames, "write_file")
145100
}
146101

147-
func TestFileWriteTracker(t *testing.T) {
148-
t.Parallel()
149-
150-
ctx := t.Context()
151-
runConfig := &config.RuntimeConfig{
152-
Config: config.Config{
153-
WorkingDir: t.TempDir(),
154-
},
155-
}
156-
157-
registry := createToolsetRegistry(runConfig.WorkingDir)
158-
require.NotNil(t, registry)
159-
160-
// Create the toolset through the registry
161-
toolset, err := registry.CreateTool(ctx, latest.Toolset{Type: "filesystem"}, runConfig.WorkingDir, runConfig, "test-agent")
162-
require.NoError(t, err)
163-
require.NotNil(t, toolset)
164-
165-
// Verify the toolset is a file write tracker
166-
tracker, ok := toolset.(*fileWriteTracker)
167-
require.True(t, ok, "expected fileWriteTracker, got %T", toolset)
168-
169-
// Initially, no path should be tracked
170-
assert.Empty(t, tracker.LastWrittenPath())
171-
172-
// Get the tools and verify write_file is present
173-
tools, err := tracker.Tools(ctx)
174-
require.NoError(t, err)
175-
176-
var hasWriteFile bool
177-
for _, tool := range tools {
178-
if tool.Name == "write_file" {
179-
hasWriteFile = true
180-
break
181-
}
182-
}
183-
assert.True(t, hasWriteFile, "write_file tool should be present")
184-
}
185-
186102
func TestBuildCreatorConfigYAML_MultilineStrings(t *testing.T) {
187103
t.Parallel()
188104

0 commit comments

Comments
 (0)