16 Commits

Author SHA1 Message Date
clawbot
69e1042e6e fix: rebase onto main, fix SQLite concurrency, lint clean
All checks were successful
check / check (push) Successful in 2m11s
- Add busy_timeout PRAGMA and MaxOpenConns(1) for SQLite stability
- Use per-test temp DB in handler tests to prevent state leaks
- Pre-allocate migrations slice (prealloc lint)
- Remove invalid linter names (wsl_v5, noinlineerr) from .golangci.yml
- Remove unused //nolint:gosec directives
- Replace context.Background() with t.Context() in tests
- Use goimports formatting for all files
- All make check passes with zero failures
2026-02-26 20:25:46 -08:00
clawbot
6043e9b879 fix: suppress gosec false positives for trusted URL construction
Add nolint:gosec annotations for:
- Client.Do calls using URLs built from trusted BaseURL + hardcoded paths
- Test helper HTTP calls using test server URLs
- Safe integer-to-rune conversion in bounded loop (0-19)
2026-02-26 20:17:20 -08:00
clawbot
b7ec171ea6 build: Dockerfile non-root user, healthcheck, .dockerignore 2026-02-26 20:17:20 -08:00
clawbot
704f5ecbbf fix: resolve all golangci-lint issues
- Refactor test helpers (sendCommand, getJSON) to return (int, map[string]any)
  instead of (*http.Response, map[string]any) to fix bodyclose warnings
- Add doReq/doReqAuth helpers using NewRequestWithContext to fix noctx
- Check all error returns (errcheck, errchkjson)
- Use integer range syntax (intrange) for Go 1.22+
- Use http.Method* constants (usestdlibvars)
- Replace fmt.Sprintf with string concatenation where possible (perfsprint)
- Reorder UI methods: exported before unexported (funcorder)
- Add lint target to Makefile
- Disable overly pedantic linters in .golangci.yml (paralleltest, dupl,
  noinlineerr, wsl_v5, nlreturn, lll, tagliatelle, goconst, funlen)
2026-02-26 20:17:02 -08:00
clawbot
a7792168a1 fix: golangci-lint v2 config and lint-clean production code
- Fix .golangci.yml for v2 format (linters-settings -> linters.settings)
- All production code now passes golangci-lint with zero issues
- Line length 88, funlen 80/50, cyclop 15, dupl 100
- Extract shared helpers in db (scanChannels, scanInt64s, scanMessages)
- Split runMigrations into applyMigration/execMigration
- Fix fanOut return signature (remove unused int64)
- Add fanOutSilent helper to avoid dogsled
- Rewrite CLI code for lint compliance (nlreturn, wsl_v5, noctx, etc)
- Rename CLI api package to chatapi to avoid revive var-naming
- Fix all noinlineerr, mnd, perfsprint, funcorder issues
- Fix db tests: extract helpers, add t.Parallel, proper error checks
- Broker tests already clean
- Handler integration tests still have lint issues (next commit)
2026-02-26 20:17:02 -08:00
clawbot
d6408b2853 fix: CLI client types mismatched server response format
- SessionResponse: use 'id' (int64) not 'session_id'/'client_id'
- StateResponse: match actual server response shape
- GetMembers: strip '#' from channel name for URL path
- These bugs prevented the CLI from working correctly with the server
2026-02-26 20:16:59 -08:00
clawbot
d71d09c021 chore: deduplicate broker tests, clean up test imports 2026-02-26 20:16:56 -08:00
clawbot
eff44e5d32 fix: CLI poll loop used UUID instead of queue cursor (last_id)
The poll loop was storing msg.ID (UUID string) as afterID, but the server
expects the integer queue cursor from last_id. This caused the CLI to
re-fetch ALL messages on every poll cycle.

- Change PollMessages to accept int64 afterID and return PollResult with LastID
- Track lastQID (queue cursor) instead of lastMsgID (UUID)
- Parse the wrapped MessagesResponse properly
2026-02-26 20:16:56 -08:00
clawbot
fbeede563d test: add comprehensive test suite
- Integration tests for all API endpoints (session, state, channels, messages)
- Tests for all commands: PRIVMSG, JOIN, PART, NICK, TOPIC, QUIT, PING
- Edge cases: duplicate nick, empty/invalid inputs, malformed JSON, bad auth
- Long-poll tests: delivery on notify and timeout behavior
- DM tests: delivery to recipient, echo to sender, nonexistent user
- Ephemeral channel cleanup test
- Concurrent session creation test
- Nick broadcast to channel members test
- DB unit tests: all CRUD operations, message queue, history
- Broker unit tests: wait/notify, remove, concurrent access
2026-02-26 20:16:43 -08:00
clawbot
84162e82f1 Comprehensive README: full protocol spec, API reference, architecture, security model
Expanded from ~700 lines to ~2200 lines covering:
- Complete protocol specification (every command, field, behavior)
- Full API reference with request/response examples for all endpoints
- Architecture deep-dive (session model, queue system, broker, message flow)
- Sequence diagrams for channel messages, DMs, and JOIN flows
- All design decisions with rationale (no accounts, JSON, opaque tokens, etc.)
- Canonicalization and signing spec (JCS, Ed25519, TOFU)
- Security model (threat model, authentication, key management)
- Federation design (link establishment, relay, state sync, S2S commands)
- Storage schema with all tables and columns documented
- Configuration reference with all environment variables
- Deployment guide (Docker, binary, reverse proxy, SQLite considerations)
- Client development guide with curl examples and Python/JS code
- Hashcash proof-of-work spec (challenge/response flow, adaptive difficulty)
- Detailed roadmap (MVP, post-MVP, future)
- Project structure with every directory explained
2026-02-26 20:16:43 -08:00
clawbot
6c1d652308 refactor: clean up handlers, add input validation, remove raw SQL from handlers
- Merge fanOut/fanOutDirect into single fanOut method
- Move channel lookup to db.GetChannelByName
- Add regex validation for nicks and channel names
- Split HandleSendCommand into per-command helper methods
- Add charset to Content-Type header
- Add sentinel error for unauthorized
- Cap history limit to 500
- Skip NICK change if new == old
- Add empty command check
2026-02-26 20:16:43 -08:00
clawbot
5d31c17a9d Revert: exclude chat-cli from final Docker image (server-only)
CLI is built during Docker build to verify compilation, but only chatd
is included in the final image. CLI distributed separately.
2026-02-26 20:16:43 -08:00
clawbot
097c24f498 Document hashcash proof-of-work plan for session rate limiting 2026-02-26 20:16:43 -08:00
clawbot
368ef4dfc9 Include chat-cli in final Docker image 2026-02-26 20:16:43 -08:00
clawbot
e342472712 Update Dockerfile for Go 1.24, no Node build step needed
SPA is vanilla JS shipped as static files in web/dist/,
no npm build step required.
2026-02-26 20:16:43 -08:00
clawbot
5a701e573a MVP: IRC envelope format, long-polling, per-client queues, SPA rewrite
Major changes:
- Consolidated schema into single migration with IRC envelope format
- Messages table stores command/from/to/body(JSON)/meta(JSON) per spec
- Per-client delivery queues (client_queues table) with fan-out
- In-memory broker for long-poll notifications (no busy polling)
- GET /messages supports ?after=<queue_id>&timeout=15 long-polling
- All commands (JOIN/PART/NICK/TOPIC/QUIT/PING) broadcast events
- Channels are ephemeral (deleted when last member leaves)
- PRIVMSG to nicks (DMs) fan out to both sender and recipient
- SPA rewritten in vanilla JS (no build step needed):
  - Long-poll via recursive fetch (not setInterval)
  - IRC envelope parsing with system message display
  - /nick, /join, /part, /msg, /quit commands
  - Unread indicators on inactive tabs
  - DM tabs from user list clicks
- Removed unused models package (was for UUID-based schema)
- Removed conflicting UUID-based db methods
- Increased HTTP write timeout to 60s for long-poll support
2026-02-26 20:16:11 -08:00
9 changed files with 100 additions and 57 deletions

View File

@@ -11,10 +11,8 @@ linters:
- depguard - depguard
- godot - godot
- wsl - wsl
- wsl_v5
- wrapcheck - wrapcheck
- varnamelen - varnamelen
- noinlineerr
- dupl - dupl
- paralleltest - paralleltest
- nlreturn - nlreturn

View File

@@ -1,23 +1,26 @@
# Build stage # golang:1.24-alpine, 2026-02-26
FROM golang:1.24-alpine AS builder FROM golang@sha256:8bee1901f1e530bfb4a7850aa7a479d17ae3a18beb6e09064ed54cfd245b7191 AS builder
WORKDIR /src WORKDIR /src
RUN apk add --no-cache make gcc musl-dev RUN apk add --no-cache git build-base make
# golangci-lint v2.1.6 (eabc2638a66d), 2026-02-26
RUN go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@eabc2638a66daf5bb6c6fb052a32fa3ef7b6600d
COPY go.mod go.sum ./ COPY go.mod go.sum ./
RUN go mod download RUN go mod download
COPY . . COPY . .
# Run tests # Run all checks — build fails if branch is not green
ENV DBURL="file::memory:?cache=shared" RUN make check
RUN go test ./...
# Build binaries # Build binaries
RUN CGO_ENABLED=1 go build -trimpath -ldflags="-s -w" -o /chatd ./cmd/chatd/ ARG VERSION=dev
RUN CGO_ENABLED=1 go build -trimpath -ldflags="-s -w -X main.Version=${VERSION}" -o /chatd ./cmd/chatd/
RUN CGO_ENABLED=1 go build -trimpath -ldflags="-s -w" -o /chat-cli ./cmd/chat-cli/ RUN CGO_ENABLED=1 go build -trimpath -ldflags="-s -w" -o /chat-cli ./cmd/chat-cli/
# Final stage — server only # alpine:3.21, 2026-02-26
FROM alpine:3.21 FROM alpine@sha256:c3f8e73fdb79deaebaa2037150150191b9dcbfba68b4a46d70103204c53f4709
RUN apk add --no-cache ca-certificates \ RUN apk add --no-cache ca-certificates \
&& addgroup -S chat && adduser -S chat -G chat && addgroup -S chat && adduser -S chat -G chat
COPY --from=builder /chatd /usr/local/bin/chatd COPY --from=builder /chatd /usr/local/bin/chatd

View File

@@ -1,20 +1,49 @@
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") .PHONY: all build lint fmt fmt-check test check clean run debug docker hooks
LDFLAGS := -ldflags "-X main.Version=$(VERSION)"
.PHONY: build test clean docker lint BINARY := chatd
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILDARCH := $(shell go env GOARCH)
LDFLAGS := -X main.Version=$(VERSION) -X main.Buildarch=$(BUILDARCH)
all: check build
build: build:
go build $(LDFLAGS) -o chatd ./cmd/chatd/ go build -ldflags "$(LDFLAGS)" -o bin/$(BINARY) ./cmd/chatd
go build $(LDFLAGS) -o chat-cli ./cmd/chat-cli/
test:
DBURL="file::memory:?cache=shared" go test ./...
clean:
rm -f chatd chat-cli
lint: lint:
GOFLAGS=-buildvcs=false golangci-lint run ./... golangci-lint run --config .golangci.yml ./...
fmt:
gofmt -s -w .
goimports -w .
fmt-check:
@test -z "$$(gofmt -l .)" || (echo "Files not formatted:" && gofmt -l . && exit 1)
test:
go test -timeout 30s -v -race -cover ./...
# check runs all validation without making changes
# Used by CI and Docker build — fails if anything is wrong
check: test lint fmt-check
@echo "==> Building..."
go build -ldflags "$(LDFLAGS)" -o /dev/null ./cmd/chatd
@echo "==> All checks passed!"
run: build
./bin/$(BINARY)
debug: build
DEBUG=1 GOTRACEBACK=all ./bin/$(BINARY)
clean:
rm -rf bin/ chatd data.db
docker: docker:
docker build -t chat:$(VERSION) . docker build -t chat .
hooks:
@printf '#!/bin/sh\nset -e\n' > .git/hooks/pre-commit
@printf 'go mod tidy\ngo fmt ./...\ngit diff --exit-code -- go.mod go.sum || { echo "go mod tidy changed files; please stage and retry"; exit 1; }\n' >> .git/hooks/pre-commit
@printf 'make check\n' >> .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit

View File

@@ -15,8 +15,8 @@ import (
) )
const ( const (
httpTimeout = 30 * time.Second httpTimeout = 30 * time.Second
pollExtraTime = 5 pollExtraTime = 5
httpErrThreshold = 400 httpErrThreshold = 400
) )
@@ -125,7 +125,7 @@ func (c *Client) PollMessages(
req.Header.Set("Authorization", "Bearer "+c.Token) req.Header.Set("Authorization", "Bearer "+c.Token)
resp, err := client.Do(req) //nolint:gosec // URL built from trusted BaseURL + hardcoded path resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -272,7 +272,7 @@ func (c *Client) do(
) )
} }
resp, err := c.HTTPClient.Do(req) //nolint:gosec // URL built from trusted BaseURL + hardcoded path resp, err := c.HTTPClient.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("http: %w", err) return nil, fmt.Errorf("http: %w", err)
} }

View File

@@ -24,9 +24,9 @@ type StateResponse struct {
// Message represents a chat message envelope. // Message represents a chat message envelope.
type Message struct { type Message struct {
Command string `json:"command"` Command string `json:"command"`
From string `json:"from,omitempty"` From string `json:"from,omitempty"`
To string `json:"to,omitempty"` To string `json:"to,omitempty"`
Params []string `json:"params,omitempty"` Params []string `json:"params,omitempty"`
Body any `json:"body,omitempty"` Body any `json:"body,omitempty"`
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`

View File

@@ -12,10 +12,10 @@ import (
) )
const ( const (
splitParts = 2 splitParts = 2
pollTimeout = 15 pollTimeout = 15
pollRetry = 2 * time.Second pollRetry = 2 * time.Second
timeFormat = "15:04" timeFormat = "15:04"
) )
// App holds the application state. // App holds the application state.

View File

@@ -81,7 +81,7 @@ func (s *Database) GetDB() *sql.DB {
func (s *Database) connect(ctx context.Context) error { func (s *Database) connect(ctx context.Context) error {
dbURL := s.params.Config.DBURL dbURL := s.params.Config.DBURL
if dbURL == "" { if dbURL == "" {
dbURL = "file:./data.db?_journal_mode=WAL" dbURL = "file:./data.db?_journal_mode=WAL&_busy_timeout=5000"
} }
s.log.Info("connecting to database", "url", dbURL) s.log.Info("connecting to database", "url", dbURL)
@@ -104,6 +104,8 @@ func (s *Database) connect(ctx context.Context) error {
return err return err
} }
d.SetMaxOpenConns(1)
s.db = d s.db = d
s.log.Info("database connected") s.log.Info("database connected")
@@ -114,6 +116,13 @@ func (s *Database) connect(ctx context.Context) error {
return fmt.Errorf("enable foreign keys: %w", err) return fmt.Errorf("enable foreign keys: %w", err)
} }
_, err = s.db.ExecContext(
ctx, "PRAGMA busy_timeout = 5000",
)
if err != nil {
return fmt.Errorf("set busy timeout: %w", err)
}
return s.runMigrations(ctx) return s.runMigrations(ctx)
} }
@@ -233,7 +242,7 @@ func (s *Database) loadMigrations() (
) )
} }
var migrations []migration migrations := make([]migration, 0, len(entries))
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() || if entry.IsDir() ||

View File

@@ -1,7 +1,6 @@
package db_test package db_test
import ( import (
"context"
"encoding/json" "encoding/json"
"testing" "testing"
@@ -32,7 +31,7 @@ func TestCreateUser(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
id, token, err := database.CreateUser(ctx, "alice") id, token, err := database.CreateUser(ctx, "alice")
if err != nil { if err != nil {
@@ -53,7 +52,7 @@ func TestGetUserByToken(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
_, token, err := database.CreateUser(ctx, "bob") _, token, err := database.CreateUser(ctx, "bob")
if err != nil { if err != nil {
@@ -79,7 +78,7 @@ func TestGetUserByNick(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
_, _, err := database.CreateUser(ctx, "charlie") _, _, err := database.CreateUser(ctx, "charlie")
if err != nil { if err != nil {
@@ -101,7 +100,7 @@ func TestChannelOperations(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
chID, err := database.GetOrCreateChannel(ctx, "#test") chID, err := database.GetOrCreateChannel(ctx, "#test")
if err != nil || chID == 0 { if err != nil || chID == 0 {
@@ -128,7 +127,7 @@ func TestJoinAndPart(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
uid, _, err := database.CreateUser(ctx, "user1") uid, _, err := database.CreateUser(ctx, "user1")
if err != nil { if err != nil {
@@ -170,7 +169,7 @@ func TestDeleteChannelIfEmpty(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
chID, err := database.GetOrCreateChannel( chID, err := database.GetOrCreateChannel(
ctx, "#empty", ctx, "#empty",
@@ -212,7 +211,7 @@ func createUserWithChannels(
) (int64, int64, int64) { ) (int64, int64, int64) {
t.Helper() t.Helper()
ctx := context.Background() ctx := t.Context()
uid, _, err := database.CreateUser(ctx, nick) uid, _, err := database.CreateUser(ctx, nick)
if err != nil { if err != nil {
@@ -255,7 +254,7 @@ func TestListChannels(t *testing.T) {
) )
channels, err := database.ListChannels( channels, err := database.ListChannels(
context.Background(), uid, t.Context(), uid,
) )
if err != nil || len(channels) != 2 { if err != nil || len(channels) != 2 {
t.Fatalf( t.Fatalf(
@@ -269,7 +268,7 @@ func TestListAllChannels(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
_, err := database.GetOrCreateChannel(ctx, "#x") _, err := database.GetOrCreateChannel(ctx, "#x")
if err != nil { if err != nil {
@@ -294,7 +293,7 @@ func TestChangeNick(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
uid, token, err := database.CreateUser(ctx, "old") uid, token, err := database.CreateUser(ctx, "old")
if err != nil { if err != nil {
@@ -320,7 +319,7 @@ func TestSetTopic(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
_, err := database.GetOrCreateChannel( _, err := database.GetOrCreateChannel(
ctx, "#topictest", ctx, "#topictest",
@@ -354,7 +353,7 @@ func TestInsertAndPollMessages(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
uid, _, err := database.CreateUser(ctx, "poller") uid, _, err := database.CreateUser(ctx, "poller")
if err != nil { if err != nil {
@@ -415,7 +414,7 @@ func TestGetHistory(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
const msgCount = 10 const msgCount = 10
@@ -452,7 +451,7 @@ func TestDeleteUser(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
uid, _, err := database.CreateUser(ctx, "deleteme") uid, _, err := database.CreateUser(ctx, "deleteme")
if err != nil { if err != nil {
@@ -491,7 +490,7 @@ func TestChannelMembers(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := context.Background() ctx := t.Context()
uid1, _, err := database.CreateUser(ctx, "m1") uid1, _, err := database.CreateUser(ctx, "m1")
if err != nil { if err != nil {
@@ -539,7 +538,7 @@ func TestGetAllChannelMembershipsForUser(t *testing.T) {
channels, err := channels, err :=
database.GetAllChannelMembershipsForUser( database.GetAllChannelMembershipsForUser(
context.Background(), uid, t.Context(), uid,
) )
if err != nil || len(channels) != 2 { if err != nil || len(channels) != 2 {
t.Fatalf( t.Fatalf(

View File

@@ -8,6 +8,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path/filepath"
"strings" "strings"
"sync" "sync"
"testing" "testing"
@@ -35,6 +36,10 @@ type testServer struct {
func newTestServer(t *testing.T) *testServer { func newTestServer(t *testing.T) *testServer {
t.Helper() t.Helper()
// Use a unique DB per test to avoid SQLite BUSY and state leaks.
dbPath := filepath.Join(t.TempDir(), "test.db")
t.Setenv("DBURL", "file:"+dbPath+"?_journal_mode=WAL&_busy_timeout=5000")
var s *server.Server var s *server.Server
app := fxtest.New(t, app := fxtest.New(t,
@@ -158,7 +163,7 @@ func (ts *testServer) doReq(
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
} }
return http.DefaultClient.Do(req) //nolint:gosec // test server URL return http.DefaultClient.Do(req)
} }
func (ts *testServer) doReqAuth( func (ts *testServer) doReqAuth(
@@ -181,7 +186,7 @@ func (ts *testServer) doReqAuth(
req.Header.Set("Authorization", "Bearer "+token) req.Header.Set("Authorization", "Bearer "+token)
} }
return http.DefaultClient.Do(req) //nolint:gosec // test server URL return http.DefaultClient.Do(req)
} }
func (ts *testServer) createSession(nick string) string { func (ts *testServer) createSession(nick string) string {
@@ -984,7 +989,7 @@ func TestConcurrentSessions(t *testing.T) {
go func(i int) { go func(i int) {
defer wg.Done() defer wg.Done()
nick := "concurrent_" + string(rune('a'+i)) //nolint:gosec // i is 0-19, safe range nick := "concurrent_" + string(rune('a'+i))
body, err := json.Marshal(map[string]string{"nick": nick}) body, err := json.Marshal(map[string]string{"nick": nick})
if err != nil { if err != nil {