refactor: harden backend per spec/backend-hardening #6
No reviewers
Labels
No labels
backend
bug
chore
duplication
effort:complex
effort:medium
effort:trivial
enhancement
frontend
fullstack
priority:high
ready-for-agent
refactor
security
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dries/ocman!6
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "chore-backend"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Risk-reduction pass on the Go backend: focused tests, observability,
and panic safety for the high-risk modules identified in the codebase
analysis. No user-visible behaviour change except the metrics
dashboard's session/project log, which now sorts by cost descending
(was: most-recent activity first) so the most expensive entries
surface first.
Per-package coverage lift:
internal/pricing: 0% → 80.0%internal/server: 48.4% → 57.3%internal/platforms/opencode: 57.6% → 71.3%Notable changes:
pricing.Lookupmatcher: longest-prefix-then-contains (wasbidirectional
strings.Contains, which made"gpt-4"match"gpt-4o"); fetch failures now log WARN.whisper.go: extractedexecutor+fileStatinterfaces sotranscribe is unit-testable without whisper/ffmpeg installed.
dispatchSessionSubpath: 110-line nested switch replaced by adeclarative
sessionSubRoutestable; 404 body includes theoffending path.
runWithRecoverso a panicking iteration logs at ERROR and theloop survives.
failure (was silent
(nil, nil)); DEBUG line summarising skippedmessages in
convertOpenCodeMessages; RWMutex arounddiscoverPortsImpland pending-prompts cache so test seam swapsare race-safe; new httptest-backed
opencodeFakeexercisesfetchSessionFromOpenCodeCtxend-to-end.GetMetricsDashboard: 11 positional params → singleMetricsDashboardOptionsstruct; Sessions/Projects sort by Costdesc, ties break by recency.
FuzzConvertOpenCodeMessageswith seed corpus.test-race,test-fuzz,test-coverage,help.See
spec/backend-hardening/{requirements,architecture}.mdfor thefull design.
Testing
make test,make test-race,make test-coverage— greenmake test-fuzz— 10s no panicsgo vet, platform-branching guard — cleanAdd focused tests, observability, and panic safety to the high-risk backend modules identified in the codebase analysis. No user-visible behaviour changes except the metrics dashboard's session/project log, which now sorts by cost descending (was: most-recent activity first) so the most expensive entries surface first. Test coverage rises in the two worst-covered packages: internal/server 48.4% → 57.3% (target ≥55%) internal/platforms/opencode 57.6% → 71.3% (target ≥70%) internal/pricing 0% → 80.0% (target ≥80%) Notable changes: - internal/pricing: extract New(client, url) constructor; replace bidirectional strings.Contains matcher with longest-prefix-then- contains so "gpt-4" no longer accidentally matches "gpt-4o"; log a WARN on fetch failure (was silent zero-cost). - internal/server/whisper.go: extract executor + fileStat interfaces so transcribe can be unit-tested without whisper / ffmpeg installed. - internal/server/handlers.go: replace the 110-line nested switch in dispatchSessionSubpath with a sessionSubRoutes table + small pattern matcher; 404 body now includes the offending path. - internal/server/{server,projects_index}.go: wrap auto-archive and projects-index loop ticks in runWithRecover so a panicking iteration logs at ERROR and the loop survives. - internal/platforms/opencode: log WARN on AgentCatalog / SlashCommands upstream failure (was silent (nil, nil)); log DEBUG with the per-call skipped count in convertOpenCodeMessages; protect discoverPortsImpl and the pending-prompts cache globals with an RWMutex so test seam swaps are race-safe; add a httptest-backed opencodeFake helper so the live-session pipeline (fetchSessionFromOpenCodeCtx) can be tested end-to-end. - internal/db: refactor GetMetricsDashboard's 11-parameter signature into MetricsDashboardOptions; sort Sessions and Projects by Cost desc with LastRequestTime as tie-breaker. - Add FuzzConvertOpenCodeMessages with a small seed corpus. - Makefile: add test-race, test-fuzz, test-coverage, help targets. See spec/backend-hardening/{requirements,architecture}.md for the full design and acceptance criteria.