fix: reject extra tool submissions and avoid renewal refetch
3 files changed,
+61
−7
internal/runner/final_tool_test.go+45 −0internal/runner/task13_fixes_test.go+6 −3internal/runner/tool_socket.go+10 −4
diff --git a/internal/runner/final_tool_test.go b/internal/runner/final_tool_test.go| new file mode 100644 |
| index 00000000..9069de76 |
| --- /dev/null |
| +++ b/internal/runner/final_tool_test.go |
| @@ -0,0 +1,45 @@ |
| +package runner |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestExtraTurnToolReturnsError(t *testing.T) { |
| + for _, name := range []string{"ask_person", "submit_candidate"} { |
| + t.Run(name, func(t *testing.T) { |
| + s, b := toolFixture(t) |
| + cap, err := readToolCapability(s.CapabilityPath) |
| + mustJournal(t, err) |
| + args := json.RawMessage(`{"question":"Which?"}`) |
| + if name == "submit_candidate" { |
| + args = json.RawMessage(`{"commit":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}`) |
| + } |
| + q := toolRequest{Capability: cap.Secret, InstanceID: b.InstanceID, TaskID: b.TaskID, Attempt: b.Attempt, Fence: b.Fence, RequestID: "one", Name: name, Arguments: args} |
| + mustJournal(t, callToolSocket(context.Background(), s.SocketPath, q)) |
| + q.RequestID = "two" |
| + if callToolSocket(context.Background(), s.SocketPath, q) == nil { |
| + t.Fatal("second turn submission accepted") |
| + } |
| + if s.invalid { |
| + t.Fatal("extra submission invalidated attempt") |
| + } |
| + pending, err := b.Journal.ToolSubmissions(context.Background(), 10) |
| + mustJournal(t, err) |
| + if len(pending) != 1 { |
| + t.Fatal("extra submission reached workflow", pending) |
| + } |
| + q.RequestID = "one" |
| + mustJournal(t, callToolSocket(context.Background(), s.SocketPath, q)) |
| + }) |
| + } |
| +} |
| +func TestToolRenewalDoesNotRefetchAuthority(t *testing.T) { |
| + s, _ := toolFixture(t) |
| + s.mu.Lock() |
| + s.binding.Current = func(context.Context) error { t.Error("renewal repeated authority network call"); return nil } |
| + s.mu.Unlock() |
| + mustJournal(t, s.RenewLease(context.Background(), time.Now().Add(time.Minute))) |
| +} |
diff --git a/internal/runner/task13_fixes_test.go b/internal/runner/task13_fixes_test.go| index 615d7fbc..e7bdc842 100644 |
| --- a/internal/runner/task13_fixes_test.go |
| +++ b/internal/runner/task13_fixes_test.go |
| @@ -103,12 +103,15 @@ func TestAgentToolJournalCapacity(t *testing.T) { |
| func TestAgentToolAttemptCapacity(t *testing.T) { |
| s, b := toolFixture(t) |
| q := toolRequestFixture(s, b) |
| + appendRaw := func() error { |
| + return b.Journal.appendTool(context.Background(), ToolSubmission{InstanceID: b.InstanceID, TaskID: q.TaskID, Attempt: b.Attempt, Fence: b.Fence, RequestID: q.RequestID, Name: q.Name, Arguments: q.Arguments, Agent: b.Agent, Isolation: b.Isolation}, runnerproto.EventHash(q.Arguments)) |
| + } |
| for i := 0; i < 128; i++ { |
| q.RequestID = fmt.Sprint(i) |
| - mustJournal(t, s.submit(context.Background(), q)) |
| + mustJournal(t, appendRaw()) |
| } |
| q.RequestID = "overflow" |
| - if s.submit(context.Background(), q) == nil { |
| + if appendRaw() == nil { |
| t.Fatal("attempt exhausted shared journal without a cap") |
| } |
| mustJournal(t, b.Journal.AdmissionAllowed(context.Background())) |
| @@ -120,7 +123,7 @@ func TestAgentToolAttemptCapacity(t *testing.T) { |
| rows, err := b.Journal.ToolSubmissions(context.Background(), 1024) |
| mustJournal(t, err) |
| mustJournal(t, b.Journal.AckTool(context.Background(), rows[0])) |
| - mustJournal(t, s.submit(context.Background(), q)) |
| + mustJournal(t, appendRaw()) |
| } |
| func TestAgentToolReproductionFields(t *testing.T) { |
diff --git a/internal/runner/tool_socket.go b/internal/runner/tool_socket.go| index 8a9de2d9..b8b80b30 100644 |
| --- a/internal/runner/tool_socket.go |
| +++ b/internal/runner/tool_socket.go |
| @@ -236,10 +236,6 @@ func (s *AttemptSocket) RenewLease(ctx context.Context, deadline time.Time) erro |
| s.invalidateLocked() |
| return runnerproto.ErrInvalid |
| } |
| - if err := s.binding.Current(ctx); err != nil { |
| - s.invalidateLocked() |
| - return err |
| - } |
| if deadline.IsZero() || !time.Now().Before(deadline) { |
| return runnerproto.ErrInvalid |
| } |
| @@ -346,6 +342,16 @@ func (s *AttemptSocket) submit(ctx context.Context, q toolRequest) error { |
| s.invalidateLocked() |
| return err |
| } |
| + var pending int |
| + if err := b.Journal.db.QueryRowContext(ctx, `SELECT count(*) FROM journal_tools WHERE acked=0 |
| + AND json_extract(body,'$.instance_id')=? AND json_extract(body,'$.task_id')=? |
| + AND json_extract(body,'$.attempt')=? AND json_extract(body,'$.fence')=? |
| + AND json_extract(body,'$.request_id')<>?`, b.InstanceID, b.TaskID, b.Attempt, b.Fence, q.RequestID).Scan(&pending); err != nil { |
| + return b.Journal.storageFailure(ctx, err) |
| + } |
| + if pending > 0 { |
| + return runnerproto.ErrInvalid |
| + } |
| value := ToolSubmission{InstanceID: b.InstanceID, TaskID: b.TaskID, Attempt: b.Attempt, Fence: b.Fence, RequestID: q.RequestID, Name: q.Name, Arguments: RedactJSON(q.Arguments, []string{s.capability.Secret}), Agent: b.Agent, Isolation: b.Isolation} |
| if err := b.Journal.appendTool(ctx, value, runnerproto.EventHash(json.RawMessage(q.Arguments))); err != nil { |
| return err |