Skip to content

Commit 61007bd

Browse files
committed
fix: migrate daemon and multiagent to Session sub-service APIs
Replace direct writes to the deprecated legacy Session fields (sess.Autonomy, sess.MaxTurns) with the canonical PermSvc / Session setters: - sess.Autonomy = X -> sess.PermSvc().SetAutonomy(X) - sess.MaxTurns = N -> sess.SetMaxTurns(N) The god-object decomposition (Phases 2-3, see docs/session-decomposition.md) extracted Autonomy into PermissionService and MaxTurns into Session.SetMaxTurns. Reading sess.Autonomy directly now bypasses the service and silently desynchronises the two views. The daemons's handleChat previously wrote sess.MaxTurns without checking the error path; SetMaxTurns returns an error for negative values, so the HTTP handler now surfaces 400 Bad Request on invalid input. The two mission workers (EngineWorker and ReadOnlyValidationWorker) already validate the surrounding worktree setup, so they wrap the error and return it the same way as their other failure modes. Touches: - internal/daemon/daemon.go (Autonomy setter, Autonomy read-back, MaxTurns error path) - internal/daemon/daemon_test.go (MaxTurns setter, error check) - internal/multiagent/worker.go (both EngineWorker and ReadOnlyValidationWorker) Verification: - go vet ./... -> clean - staticcheck ./... -> 0 issues (was 12 SA1019) - gofumpt -d -> clean - go build ./... -> clean - go test -short ./... -> all packages pass
1 parent 3bf56fc commit 61007bd

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

internal/daemon/daemon.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,20 +397,24 @@ func (s *Server) handleChat(w http.ResponseWriter, r *http.Request) {
397397

398398
// Set autonomy
399399
if req.Autonomy != "" {
400-
sess.Autonomy = engine.ParseAutonomyLevel(req.Autonomy)
400+
sess.PermSvc().SetAutonomy(engine.ParseAutonomyLevel(req.Autonomy))
401401
}
402402

403403
// Auto-approve permissions based on autonomy (non-interactive)
404404
sess.PermissionFn = func(pr engine.PermissionRequest) {
405-
cfg := engine.PresetConfig(sess.Autonomy)
405+
cfg := engine.PresetConfig(sess.PermSvc().Autonomy())
406406
allowed := !cfg.NeedsPermission(pr.ToolName, false)
407407
if pr.Response != nil {
408408
pr.Response <- allowed
409409
}
410410
}
411411

412412
if req.MaxTurns > 0 {
413-
sess.MaxTurns = req.MaxTurns
413+
if err := sess.SetMaxTurns(req.MaxTurns); err != nil {
414+
slog.Error("invalid max turns", "err", err, "max_turns", req.MaxTurns)
415+
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid max_turns"})
416+
return
417+
}
414418
}
415419

416420
sess.AddUser(req.Prompt)

internal/daemon/daemon_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ func TestDaemon_RejectsUnknownFields(t *testing.T) {
234234
func TestDaemon_Chat_WithEngine(t *testing.T) {
235235
factory := func(req ChatRequest) (*engine.Session, error) {
236236
sess := engine.NewSession("", "test-model", "you are helpful", nil)
237-
sess.MaxTurns = 1
237+
if err := sess.SetMaxTurns(1); err != nil {
238+
t.Fatalf("set max turns: %v", err)
239+
}
238240
return sess, nil
239241
}
240242
srv := New(Config{Port: 0, Host: testutil.LoopbackHost}, factory)

internal/multiagent/worker.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ func EngineWorker(provider, model, systemPrompt string) WorkerFunc {
4141
sess := engine.NewHawkSession(ctx, hawkconfig.DeploymentRoutingEnabled(settings), provider, model, systemPrompt, registry)
4242

4343
// Configure for autonomous operation
44-
sess.Autonomy = engine.AutonomyLevel(cfg.AutonomyLevel)
45-
if sess.Autonomy < engine.AutonomyFull {
46-
sess.Autonomy = engine.AutonomyFull
44+
level := engine.AutonomyLevel(cfg.AutonomyLevel)
45+
if level < engine.AutonomyFull {
46+
level = engine.AutonomyFull
47+
}
48+
sess.PermSvc().SetAutonomy(level)
49+
if err := sess.SetMaxTurns(30); err != nil {
50+
return nil, fmt.Errorf("set max turns: %w", err)
4751
}
48-
sess.MaxTurns = 30
4952

5053
// Auto-approve everything in mission workers
5154
sess.PermissionFn = func(req engine.PermissionRequest) {
@@ -146,11 +149,14 @@ func ReadOnlyValidationWorker(provider, model, systemPrompt string) WorkerFunc {
146149
settings := hawkconfig.LoadSettings()
147150
sess := engine.NewHawkSession(ctx, hawkconfig.DeploymentRoutingEnabled(settings), provider, model, systemPrompt, registry)
148151

149-
sess.Autonomy = engine.AutonomyLevel(cfg.AutonomyLevel)
150-
if sess.Autonomy < engine.AutonomyFull {
151-
sess.Autonomy = engine.AutonomyFull
152+
level := engine.AutonomyLevel(cfg.AutonomyLevel)
153+
if level < engine.AutonomyFull {
154+
level = engine.AutonomyFull
155+
}
156+
sess.PermSvc().SetAutonomy(level)
157+
if err := sess.SetMaxTurns(30); err != nil {
158+
return nil, fmt.Errorf("set max turns: %w", err)
152159
}
153-
sess.MaxTurns = 30
154160
sess.PermissionFn = func(req engine.PermissionRequest) {
155161
if req.Response != nil {
156162
req.Response <- true

0 commit comments

Comments
 (0)