Skip to content

Mega refactor: package layout, typed events, tests, docs, tooling#2

Merged
s2x merged 3 commits into
mainfrom
mega-refactor
May 21, 2026
Merged

Mega refactor: package layout, typed events, tests, docs, tooling#2
s2x merged 3 commits into
mainfrom
mega-refactor

Conversation

@s2x
Copy link
Copy Markdown
Contributor

@s2x s2x commented May 21, 2026

Summary

Senior-pass refactor of the whole project. The 283-line main() is gone; the program is now split into 4 small packages with full test coverage and matching tooling.

Package layout

Package Responsibility
internal/event Typed Envelope / AssistantMessageEvent / ToolCall / Result structs replacing every map[string]any assertion.
internal/render Renderer state machine with named ANSI constants and a tiny printf helper.
internal/pi Subprocess lifecycle (Start / Events / Close / Kill) wired to context.Context. BuildArgs extracted for testing.
internal/cli Flag parsing + event-loop orchestration; structured exit codes (0/1/2/130); --version.

main.go is now a ~10-line entrypoint: installs signal.NotifyContext(SIGINT, SIGTERM) and delegates to cli.Run.

Tooling aligned with decodo-coding-tools

  • .golangci.yml v2 — govet, staticcheck, unused, ineffassign, errcheck, dupl + gofmt
  • Makefilefmt, vet, staticcheck, lint, lint-fix, tidy, check, build, install, clean, test
  • CI split into independent jobs: lint, fmt-check, govet, staticcheck, tidy, test (ubuntu + macOS matrix), build (gated on the above, embeds short SHA via -ldflags)

Other senior touches

  • LICENSE (MIT)
  • README with usage, flags table, exit codes, dev commands
  • Graceful shutdown via signal.NotifyContext — no orphaned pi child on Ctrl+C
  • Removed dead State.String, dead start-event handlers, magic numbers pulled into named constants
  • All interface{} replaced with any

Tests

34 tests across 4 packages, all passing under -race. Coverage:

  • event: envelope parsing for response/text_delta/toolcall_end/tool_execution_end, Result.SummaryText nil/empty cases
  • render: all section transitions, ToolCall with/without args, ToolExecStart bash vs non-bash, ToolExecEnd success/error/truncation, JSON without HTML escaping, newline injection
  • pi: BuildArgs defaults, with session, full options, omits empty fields
  • cli: agent_end stops with 0, error envelope stops with 1, response failure stops with 1, text_delta renders, tool_execution_end renders, tool_call_end with/without ToolCall

Test plan

  • go test -race -count=1 ./... — 34 passing
  • gofmt -l . — clean
  • go vet ./... — clean
  • staticcheck ./... — clean
  • golangci-lint run — clean
  • go build -ldflags '...' -o pi-stream . — produces working binary
  • Manual: ./pi-stream --version prints version
  • Manual: ./pi-stream --model "GLM 5.1" -t bash "..." renders correctly

Piotr Hałas added 3 commits May 21, 2026 08:23
Split the 283-line main() into four small packages:
  internal/event   typed Envelope/AssistantMessageEvent/ToolCall/Result
                   structs replacing map[string]any assertions
  internal/render  Renderer state machine with named ANSI constants,
                   small printf helper, and 15 unit tests covering all
                   section transitions and edge cases
  internal/pi      subprocess lifecycle (Start/Events/Close/Kill) wired
                   to a context.Context for cancellation; BuildArgs is
                   exposed and tested separately
  internal/cli     flag parsing + event-loop orchestration with
                   structured exit codes (0/1/2/130) and --version

main.go is now a thin entrypoint that installs a SIGINT/SIGTERM
NotifyContext and delegates to cli.Run.

Tooling lifted from decodo-coding-tools:
  - .golangci.yml v2 config (govet, staticcheck, unused, ineffassign,
    errcheck, dupl + gofmt formatter)
  - Makefile targets: fmt, vet, staticcheck, lint, lint-fix, tidy, check
  - CI split into separate lint/fmt-check/govet/staticcheck/tidy/test
    jobs; test job covers ubuntu + macOS; build is gated on all of them
    and embeds the commit SHA via -ldflags

Other senior touches:
  - LICENSE (MIT)
  - README with usage, flags table, exit codes, layout, dev commands
  - graceful shutdown via signal.NotifyContext (no orphaned pi child)
  - removed dead State.String, dead start-event handlers, magic numbers
    pulled into named constants, all interface{} replaced with any

Tests: 34 passing, race detector clean. golangci-lint, staticcheck,
go vet, gofmt -l, go test -race all clean locally.
@s2x s2x merged commit e5f319b into main May 21, 2026
8 checks passed
@s2x s2x deleted the mega-refactor branch May 21, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant