Split main.ts into worker.ts (queue) + agent-runner.ts (execution) #36
Labels
No labels
area:agents
area:dashboard
area:database
area:design
area:design-review
area:flows
area:infra
area:meta
area:security
area:sessions
area:webhook
area:workdir
security
type:bug
type:chore
type:meta
type:user-story
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
charles/claude-hooks#36
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
User story
As a maintainer, I want
src/main.ts(1212 lines) split so that the per-agent task queue and the Claude Agent SDK execution live in separate modules, so thatWorker.runAgent(222 lines of container setup + session resume + SDK spawn) stops being the biggest unreadable block in the codebase and each concern can be tested in isolation.Context
src/main.tscurrently owns:Workerclass (queue, enqueue, getResult, processNext)Worker.runAgent— 222 lines combining: pre-flight container checks, credential+token wiring, SDK spawn, session resume, session captureThe
Workerlifecycle (FIFO queue, idempotent dispatch, backpressure) is orthogonal to what happens inside a single task (container setup, SDK invocation, session bookkeeping). Splitting the two lets future changes to either side land without touching the other.Acceptance criteria
New file layout
src/worker.ts—Workerclass only (FIFO queue,enqueue,getResult,processNext, result store TTL). Knows nothing about the SDK; takes arunTaskcallback injected at construction.src/agent-runner.ts— the logic currently inWorker.runAgent: container pre-flight, env construction, SDK spawn, session resume, session capture, retry-on-invalid-session. One exported function:runAgentTask(agent, task, session, env)→ result.src/main.ts— HTTP server + API routes + orchestration. Wires eachWorkerwithrunAgentTaskfromagent-runner.ts. Target ≤ 500 lines.Session resume stays correct
resumeto SDK → on failure, drop session, retry fresh — is identical.Container mode unchanged
agent-runner.tsowns thecwd = hostCwd | workdirdecision (the fix from commit0e3ea72). Regression test: run a task in container mode and verify the SDK is spawned with a host-existingcwd.ensureContainerReady) still fails fast with a clear error if the agent's container is down.Behavior-preserving
main.test.tstests pass after the split. Tests can move intoworker.test.tsandagent-runner.test.tsas appropriate.charles/dummy-appissue → PR → merge still completes.just qagreen.Public API stable
main.tsstill exportsgetWorker(used bywebhook.ts) with the same signature.TaskRequesttype stays exported from wherever callers currently import it (can stay inmain.tsre-exported from the new module, or move to a sharedtypes.ts— justify whichever choice in the PR).Out of scope
runAgentbeyond the three session-resume cases listed above (see #D).References
src/sessions.tsand its existing tests.Dependencies
main