Skip to content

Commit f928a71

Browse files
committed
fix: production readiness — goal loop, repo map churn, LSP rename, steering, cost
1 parent 1bb15d7 commit f928a71

55 files changed

Lines changed: 5055 additions & 235 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Releases are cut by hand from a release PR; see [docs/RELEASING.md](docs/RELEASI
5252
- **Dead shell-injection surface removed**: `AssumptionTracker.VerifyCommandSucceeds` ran caller-supplied strings through `sh -c`, bypassing the permission stack; it had zero callers and is deleted. `SelfHealer.RunScript` no longer shell-evaluates the script path (double evaluation) and invokes it directly via `/bin/sh`.
5353

5454
### Fixed
55+
- Git snapshots now keep a separate history for each project. Earlier versions shared a history across projects. To inspect those older commits, run `git --git-dir="<state-dir>/snapshots/.git" log --stat`, using your Reflect state directory for `<state-dir>`.
56+
- The shell installer stages each upgrade under the install prefix. A failed launcher replacement preserves the previous launcher and cleans up its temporary files.
5557
- Simple greetings stay conversational without listing files or reading project instructions, including after earlier tool use; real requests such as "Hi fix bug" keep their tools.
5658
- Batch tool execution now runs post-processing, redaction, and completion hooks and emits one final result per call.
5759
- Plan and Review modes enforce read-only execution even after tool discovery. Loading a local skill uses its read-only permission policy.

‎api/openapi.yaml‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,66 @@ paths:
867867
schema:
868868
$ref: "#/components/schemas/Error"
869869

870+
/v1/sessions/{id}/steer:
871+
post:
872+
operationId: steerSession
873+
tags: [agent]
874+
summary: Steer an in-flight generation
875+
description: |
876+
Splices a guidance message into the session's active generation. The
877+
message is injected between tool batches; the run is not interrupted.
878+
Requires an in-flight generation for the session.
879+
parameters:
880+
- name: id
881+
in: path
882+
required: true
883+
schema:
884+
type: string
885+
requestBody:
886+
required: true
887+
content:
888+
application/json:
889+
schema:
890+
type: object
891+
required: [message]
892+
properties:
893+
message:
894+
type: string
895+
responses:
896+
"200":
897+
description: Guidance queued for the active generation
898+
content:
899+
application/json:
900+
schema:
901+
type: object
902+
properties:
903+
steered:
904+
type: boolean
905+
"400":
906+
description: Invalid session id or missing message
907+
content:
908+
application/json:
909+
schema:
910+
$ref: "#/components/schemas/Error"
911+
"401":
912+
description: Unauthorized
913+
content:
914+
application/json:
915+
schema:
916+
$ref: "#/components/schemas/Error"
917+
"404":
918+
description: No active generation for the session
919+
content:
920+
application/json:
921+
schema:
922+
$ref: "#/components/schemas/Error"
923+
"429":
924+
description: Rate limit exceeded
925+
content:
926+
application/json:
927+
schema:
928+
$ref: "#/components/schemas/Error"
929+
870930
/v1/sessions/{id}/graph:
871931
get:
872932
operationId: getSessionGraph

‎cmd/chat_multiturn_e2e_test.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,15 @@ func TestChatModel_MultiTurnQueuedConversationE2E(t *testing.T) {
8282
m.input.SetValue("second question")
8383
result, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
8484
m = requireChatModel(t, result)
85-
if len(m.messageQueue) != 1 || m.messageQueue[0] != "second question" {
86-
t.Fatalf("queued messages = %v, want [second question]", m.messageQueue)
85+
if len(m.messageQueue) != 0 {
86+
t.Fatalf("deferred queue should stay empty while steering is live, got %v", m.messageQueue)
87+
}
88+
sq := m.session.Persistence().Steering()
89+
if sq == nil || !sq.HasPending() {
90+
t.Fatal("second question should be steered into the live run")
8791
}
88-
if got := lastSystemMessage(m.messages); !strings.Contains(got, "Queued: second question") {
89-
t.Fatalf("expected queued message notice, got %q", got)
92+
if got := lastSystemMessage(m.messages); !strings.Contains(got, "Steering active run: second question") {
93+
t.Fatalf("expected steering notice, got %q", got)
9094
}
9195

9296
result, _ = m.Update(streamChunkMsg("I fixed the failing test in auth_test.go"))

‎cmd/chat_steering_test.go‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
8+
tea "charm.land/bubbletea/v2"
9+
10+
"github.com/GrayCodeAI/reflect/internal/storage"
11+
"github.com/GrayCodeAI/reflect/internal/tool"
12+
)
13+
14+
// isolateSteeringTestState isolates config/state dirs (including the
15+
// persisted prompt queue) for steering tests.
16+
func isolateSteeringTestState(t *testing.T) {
17+
t.Helper()
18+
isolateChatCommandSweepEnv(t)
19+
if err := os.MkdirAll(storage.StateDir(), 0o755); err != nil {
20+
t.Fatalf("mkdir state dir: %v", err)
21+
}
22+
tool.ResetPromptQueueForTest()
23+
t.Cleanup(tool.ResetPromptQueueForTest)
24+
}
25+
26+
func TestSubmitWhileWaitingSteersActiveRun(t *testing.T) {
27+
m := newTestChatModel()
28+
m.waiting = true
29+
m.input.SetValue("focus on the failing test")
30+
31+
result, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
32+
m = requireChatModel(t, result)
33+
34+
if len(m.messageQueue) != 0 {
35+
t.Fatalf("deferred queue should stay empty while steering is live, got %v", m.messageQueue)
36+
}
37+
sq := m.session.Persistence().Steering()
38+
if sq == nil || !sq.HasPending() {
39+
t.Fatal("steering text should be pending on the session's steering queue")
40+
}
41+
drained := sq.Drain()
42+
if len(drained) != 1 || drained[0].Content != "focus on the failing test" {
43+
t.Fatalf("steering queue = %+v, want one guidance message", drained)
44+
}
45+
if got := m.input.Value(); got != "" {
46+
t.Fatalf("input should be cleared after steering submit, got %q", got)
47+
}
48+
if got := lastSystemMessage(m.messages); !strings.Contains(got, "Steering active run: focus on the failing test") {
49+
t.Fatalf("expected steering status line, got %q", got)
50+
}
51+
}
52+
53+
func TestSubmitWhileWaitingFallsBackWithoutSteeringQueue(t *testing.T) {
54+
m := newTestChatModel()
55+
m.session.Persistence().SetSteering(nil)
56+
m.waiting = true
57+
m.input.SetValue("deferred note")
58+
59+
result, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
60+
m = requireChatModel(t, result)
61+
62+
if len(m.messageQueue) != 1 || m.messageQueue[0] != "deferred note" {
63+
t.Fatalf("fallback deferred queue = %v, want [deferred note]", m.messageQueue)
64+
}
65+
if got := lastSystemMessage(m.messages); !strings.Contains(got, "Queued: deferred note") {
66+
t.Fatalf("expected queued status line, got %q", got)
67+
}
68+
}
69+
70+
// TestStreamDoneReclaimsUnconsumedSteering covers the conversational-run
71+
// edge: a turn with no tool batches never drains the steering queue, so the
72+
// done handler must hand the guidance back to the deferred queue instead of
73+
// stranding it for an unrelated future run.
74+
func TestStreamDoneReclaimsUnconsumedSteering(t *testing.T) {
75+
isolateSteeringTestState(t)
76+
77+
m := newTestChatModel()
78+
m.waiting = true
79+
if !m.steerActiveRun("never drained") {
80+
t.Fatal("steering enqueue failed")
81+
}
82+
83+
result, _ := m.Update(streamDoneMsg{})
84+
m = requireChatModel(t, result)
85+
86+
sq := m.session.Persistence().Steering()
87+
if sq.HasPending() {
88+
t.Fatal("unconsumed steering should be reclaimed at stream end")
89+
}
90+
// The stream-done auto-submit path turns the reclaimed guidance into
91+
// the next user turn (same behavior the deferred queue always had).
92+
if !m.waiting {
93+
t.Fatal("reclaimed steering should auto-start the next turn")
94+
}
95+
if got := lastMessageByRole(m.messages, "user"); got != "never drained" {
96+
t.Fatalf("last user message = %q, want %q", got, "never drained")
97+
}
98+
}
99+
100+
func TestStreamErrorReclaimsUnconsumedSteering(t *testing.T) {
101+
m := newTestChatModel()
102+
m.waiting = true
103+
if !m.steerActiveRun("aborted run guidance") {
104+
t.Fatal("steering enqueue failed")
105+
}
106+
107+
result, _ := m.Update(streamErrMsg{err: errNoInteractivePromptInput})
108+
m = requireChatModel(t, result)
109+
110+
if sq := m.session.Persistence().Steering(); sq.HasPending() {
111+
t.Fatal("unconsumed steering should be reclaimed on stream error")
112+
}
113+
if len(m.messageQueue) != 1 || m.messageQueue[0] != "aborted run guidance" {
114+
t.Fatalf("reclaimed queue = %v, want [aborted run guidance]", m.messageQueue)
115+
}
116+
}
117+
118+
// TestPromptQueueAddSteersLiveRun: /prompt-queue add during a live run feeds
119+
// the steering queue directly instead of persisting dead text.
120+
func TestPromptQueueAddSteersLiveRun(t *testing.T) {
121+
isolateSteeringTestState(t)
122+
123+
m := newTestChatModel()
124+
m.waiting = true
125+
126+
m.handleCommand("/prompt-queue add focus on the auth module")
127+
128+
sq := m.session.Persistence().Steering()
129+
if sq == nil || !sq.HasPending() {
130+
t.Fatal("/prompt-queue add during a live run should steer the run")
131+
}
132+
drained := sq.Drain()
133+
if len(drained) != 1 || drained[0].Content != "focus on the auth module" {
134+
t.Fatalf("steering queue = %+v, want the queued prompt", drained)
135+
}
136+
// Steered live: nothing is written to the persisted queue.
137+
if items := tool.GetPromptQueue(); len(items) != 0 {
138+
t.Fatalf("persisted queue = %+v, want empty", items)
139+
}
140+
if got := lastSystemMessage(m.messages); !strings.Contains(got, "Steered into the active run") {
141+
t.Fatalf("expected steering status line, got %q", got)
142+
}
143+
}
144+
145+
// TestPromptQueueAddPersistsWhenIdle: with no live run, /prompt-queue add
146+
// persists for the next run's steering splice.
147+
func TestPromptQueueAddPersistsWhenIdle(t *testing.T) {
148+
isolateSteeringTestState(t)
149+
150+
m := newTestChatModel()
151+
152+
m.handleCommand("/prompt-queue add queued while idle")
153+
154+
items := tool.GetPromptQueue()
155+
if len(items) != 1 || items[0].Prompt != "queued while idle" {
156+
t.Fatalf("persisted queue = %+v, want one queued prompt", items)
157+
}
158+
if sq := m.session.Persistence().Steering(); sq.HasPending() {
159+
t.Fatal("idle add must not enqueue steering")
160+
}
161+
if got := lastSystemMessage(m.messages); !strings.Contains(got, "steers the next run") {
162+
t.Fatalf("expected next-run status line, got %q", got)
163+
}
164+
}
165+
166+
// TestPersistedPromptQueueFeedsNextRun: the run-start drain is the persisted
167+
// queue's consumer — FIFO order is preserved and the file is emptied.
168+
func TestPersistedPromptQueueFeedsNextRun(t *testing.T) {
169+
isolateSteeringTestState(t)
170+
171+
if err := tool.EnqueuePrompt("first queued task", ""); err != nil {
172+
t.Fatal(err)
173+
}
174+
if err := tool.EnqueuePrompt("second queued task", ""); err != nil {
175+
t.Fatal(err)
176+
}
177+
178+
m := newTestChatModel()
179+
m.drainPersistedPromptQueue()
180+
181+
drained := m.session.Persistence().Steering().Drain()
182+
if len(drained) != 2 || drained[0].Content != "first queued task" || drained[1].Content != "second queued task" {
183+
t.Fatalf("steering feed = %+v, want FIFO [first queued task, second queued task]", drained)
184+
}
185+
if items := tool.GetPromptQueue(); len(items) != 0 {
186+
t.Fatalf("persisted queue should be drained, got %+v", items)
187+
}
188+
}

‎cmd/chat_stream.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func (m *chatModel) startStream() {
7878
m.testStreamStarter()
7979
return
8080
}
81+
// The persisted /prompt-queue finally has a consumer: items queued while
82+
// idle splice into this run between tool batches via the steering queue.
83+
m.drainPersistedPromptQueue()
8184
m.streamCancelled = false
8285
m.syncSessionSelection()
8386
// Cancel any prior in-flight stream first: overwriting m.cancel would

‎cmd/chat_subcommand_cost.go‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
"strings"
6+
"time"
7+
48
tea "charm.land/bubbletea/v2"
9+
10+
"github.com/GrayCodeAI/reflect/internal/usage"
511
)
612

713
// costSubcommand implements the /cost slash command. It prints the
8-
// session's cost summary (token usage, USD cost).
14+
// session's cost summary (token usage, USD cost) plus a per-helper
15+
// breakdown when attributed helper usage exists in the ledger.
916
type costSubcommand struct{}
1017

1118
func (c *costSubcommand) Name() string { return "cost" }
1219
func (c *costSubcommand) Aliases() []string { return nil }
1320
func (c *costSubcommand) Description() string { return "print session cost and token usage summary" }
1421
func (c *costSubcommand) Usage() string { return "" }
1522
func (c *costSubcommand) Handle(m *chatModel, args []string, text string) (tea.Model, tea.Cmd) {
16-
m.messages = append(m.messages, displayMsg{role: "system", content: m.session.CostValue().Summary()})
23+
content := m.session.CostValue().Summary()
24+
if helpers := helperSpendSummary(); helpers != "" {
25+
content += "\n" + helpers
26+
}
27+
m.messages = append(m.messages, displayMsg{role: "system", content: content})
1728
return m, nil
1829
}
1930

31+
// helperSpendSummary renders a one-line per-helper (subagent / mission
32+
// worker) spend breakdown from the usage ledger over the last day, or ""
33+
// when no attributed usage exists in that window. The ledger is global, so
34+
// the window keeps the line relevant to the session at hand; attribution
35+
// is stamped where helper sessions are constructed.
36+
func helperSpendSummary() string {
37+
records, err := usage.Read()
38+
if err != nil {
39+
return ""
40+
}
41+
since := time.Now().Add(-24 * time.Hour).UnixMilli()
42+
sum := usage.Summarize(records, since)
43+
if len(sum.ByHelper) == 0 {
44+
return ""
45+
}
46+
parts := make([]string, 0, len(sum.ByHelper))
47+
for _, h := range sum.ByHelper {
48+
parts = append(parts, fmt.Sprintf("%s %d gens $%.4f", h.Helper, h.Generations, h.TotalCostUSD))
49+
}
50+
return "Helpers (ledger, last 24h): " + strings.Join(parts, " | ")
51+
}
52+
2053
func init() {
2154
subcommandRegistry.Register(&costSubcommand{})
2255
}

0 commit comments

Comments
 (0)