@gonzih/cc-agent 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # cc-agent
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@gonzih/cc-agent?label=@gonzih/cc-agent)](https://www.npmjs.com/package/@gonzih/cc-agent)
4
+
3
5
  MCP server for spawning Claude Code agents in GitHub repos. Give Claude Code the ability to **branch itself** — clone a repo and kick off a sub-agent to work on it autonomously, with persistent state across MCP restarts.
4
6
 
5
7
  Built by [@Gonzih](https://github.com/Gonzih).
@@ -16,33 +18,81 @@ CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-... # OAuth token (recommended)
16
18
  ANTHROPIC_API_KEY=sk-ant-api03-... # API key
17
19
  ```
18
20
 
19
- Restart Claude Code. You now have 7 new MCP tools.
21
+ Restart Claude Code. You now have 13 new MCP tools.
20
22
 
21
23
  ## MCP Tools
22
24
 
23
25
  | Tool | Description |
24
26
  |------|-------------|
25
27
  | `spawn_agent` | Clone a repo, optionally create a branch, run Claude Code on a task |
26
- | `list_jobs` | List all jobs with status, recent tool calls, and exit info |
27
28
  | `get_job_status` | Check status of a specific job |
28
29
  | `get_job_output` | Stream output lines from a job (supports offset for tailing) |
30
+ | `list_jobs` | List all jobs with status, recent tool calls, and exit info |
29
31
  | `cancel_job` | Kill a running job |
30
32
  | `send_message` | Write a message to a running agent's stdin mid-task |
33
+ | `cost_summary` | Total USD cost across all jobs, broken down by repo |
31
34
  | `get_version` | Return the running cc-agent version |
35
+ | `create_plan` | Spawn a dependency graph of agent jobs in one call |
36
+ | `create_profile` | Save a named spawn config for repeated use with `{{variable}}` templates |
37
+ | `list_profiles` | List all saved profiles |
38
+ | `delete_profile` | Delete a named profile |
39
+ | `spawn_from_profile` | Spawn a job from a saved profile with variable interpolation |
32
40
 
33
41
  ## spawn_agent parameters
34
42
 
35
43
  | Parameter | Type | Required | Description |
36
44
  |-----------|------|----------|-------------|
37
- | `repo_url` | string | yes | Git repo to clone (HTTPS) |
45
+ | `repo_url` | string | yes | Git repo to clone (HTTPS or SSH) |
38
46
  | `task` | string | yes | Task prompt for Claude Code |
39
47
  | `branch` | string | no | Existing branch to check out after clone |
40
48
  | `create_branch` | string | no | New branch to create (e.g. `feat/my-feature`) |
41
49
  | `claude_token` | string | no | Per-job token override |
42
50
  | `continue_session` | boolean | no | Pass `--continue` to resume last Claude session in workdir |
43
- | `max_budget_usd` | number | no | Spend cap in USD (default: 20). Prevents runaway costs |
51
+ | `max_budget_usd` | number | no | Spend cap in USD (default: 20) |
52
+ | `session_id` | string | no | Session ID from a prior job's `sessionIdAfter` — resumes that session |
53
+ | `depends_on` | string[] | no | Job IDs that must be `done` before this job starts (queued as `pending`) |
54
+
55
+ ## create_plan parameters
56
+
57
+ | Parameter | Type | Required | Description |
58
+ |-----------|------|----------|-------------|
59
+ | `goal` | string | yes | High-level description of what this plan achieves |
60
+ | `steps` | array | yes | Ordered list of steps to execute |
44
61
 
45
- ## Usage example
62
+ Each step in `steps`:
63
+
64
+ | Field | Type | Required | Description |
65
+ |-------|------|----------|-------------|
66
+ | `id` | string | yes | Logical step ID (used for `depends_on` references within the plan) |
67
+ | `repo_url` | string | yes | Git repo to clone |
68
+ | `task` | string | yes | Task prompt for Claude Code |
69
+ | `create_branch` | string | no | New branch to create before running |
70
+ | `depends_on` | string[] | no | Step IDs from this plan that must complete first |
71
+
72
+ ## create_profile parameters
73
+
74
+ | Parameter | Type | Required | Description |
75
+ |-----------|------|----------|-------------|
76
+ | `name` | string | yes | Profile name (alphanumeric, dashes, underscores) |
77
+ | `repo_url` | string | yes | Git repo to clone |
78
+ | `task_template` | string | yes | Task template — use `{{varName}}` for substitution |
79
+ | `default_budget_usd` | number | no | Default USD budget for jobs from this profile |
80
+ | `branch` | string | no | Branch to check out after cloning |
81
+ | `description` | string | no | Human-readable profile description |
82
+
83
+ ## spawn_from_profile parameters
84
+
85
+ | Parameter | Type | Required | Description |
86
+ |-----------|------|----------|-------------|
87
+ | `profile_name` | string | yes | Name of the saved profile to use |
88
+ | `vars` | object | no | Variables to interpolate into the task template |
89
+ | `task_override` | string | no | Use this task instead of the profile template |
90
+ | `branch_override` | string | no | Override the profile's branch |
91
+ | `budget_override` | number | no | Override the profile's default budget |
92
+
93
+ ## Usage examples
94
+
95
+ ### Basic agent
46
96
 
47
97
  ```
48
98
  spawn_agent({
@@ -60,19 +110,114 @@ get_job_output({ job_id: "abc-123", offset: 0 })
60
110
  // → { lines: ["[cc-agent] Cloning...", "Reading src/api.ts...", ...], done: false }
61
111
 
62
112
  send_message({ job_id: "abc-123", message: "Also update the tests." })
63
- // → { ok: true }
113
+ // → { sent: true }
114
+
115
+ cost_summary()
116
+ // → { totalJobs: 1, totalCostUsd: 1.23, byRepo: { "https://github.com/...": 1.23 } }
64
117
  ```
65
118
 
66
- ## Persistent job storage
119
+ ### Multi-step plan with dependencies
120
+
121
+ ```
122
+ create_plan({
123
+ goal: "Refactor auth and update docs",
124
+ steps: [
125
+ {
126
+ id: "refactor",
127
+ repo_url: "https://github.com/yourorg/app",
128
+ task: "Refactor auth middleware to use JWT. Open a PR.",
129
+ create_branch: "feat/jwt-auth"
130
+ },
131
+ {
132
+ id: "docs",
133
+ repo_url: "https://github.com/yourorg/app",
134
+ task: "Update README to document the new JWT auth flow.",
135
+ create_branch: "docs/jwt-auth",
136
+ depends_on: ["refactor"]
137
+ }
138
+ ]
139
+ })
140
+ // → { goal: "...", totalSteps: 2, steps: [{ stepId: "refactor", jobId: "abc-1", status: "cloning" }, { stepId: "docs", jobId: "abc-2", status: "pending" }] }
141
+ ```
67
142
 
68
- Job state is persisted to `<cwd>/.cc-agent/` across MCP server restarts:
143
+ ### Profiles for repeated tasks
69
144
 
70
- - `.cc-agent/jobs.json` — full job metadata (status, exit code, PID, params)
145
+ ```
146
+ // Save once:
147
+ create_profile({
148
+ name: "fix-issue",
149
+ repo_url: "https://github.com/yourorg/app",
150
+ task_template: "Fix issue #{{issue}}: {{title}}. Open a PR when done.",
151
+ default_budget_usd: 5
152
+ })
153
+
154
+ // Use many times:
155
+ spawn_from_profile({
156
+ profile_name: "fix-issue",
157
+ vars: { issue: "42", title: "Login broken on mobile" }
158
+ })
159
+ ```
160
+
161
+ ### Resume a prior session
162
+
163
+ ```
164
+ // Get session ID from a completed job:
165
+ get_job_status({ job_id: "abc-123" })
166
+ // → { ..., session_id_after: "ses_xyz" }
167
+
168
+ // Resume it:
169
+ spawn_agent({
170
+ repo_url: "https://github.com/yourorg/app",
171
+ task: "Continue where you left off — finish the tests.",
172
+ session_id: "ses_xyz"
173
+ })
174
+ ```
175
+
176
+ ## Persistence
177
+
178
+ cc-agent v0.3.0+ stores all job state in **Redis**, which is auto-provisioned on startup — zero configuration needed.
179
+
180
+ ### Auto-provisioning
181
+
182
+ On startup, cc-agent tries to connect to Redis at `localhost:6379`. If unavailable:
183
+
184
+ 1. **Docker** — runs `docker run -d --name cc-agent-redis -p 6379:6379 --restart=unless-stopped redis:alpine`
185
+ 2. **redis-server** — if `redis-server` is on PATH, spawns it as a daemon
186
+ 3. **In-memory fallback** — logs a warning and continues; jobs are not persisted across restarts
187
+
188
+ Once Redis is available, all job state, output, profiles, and plans survive MCP server restarts and are **shared across all Claude Code sessions** pointing at the same Redis instance.
189
+
190
+ ### Key schema
191
+
192
+ | Key | Type | TTL | Contents |
193
+ |-----|------|-----|----------|
194
+ | `cca:job:<id>` | String (JSON) | 7 days | Full job record |
195
+ | `cca:jobs:index` | List | — | Job IDs, newest first (capped at 500) |
196
+ | `cca:job:<id>:output` | List | 7 days | Output lines (one entry per line) |
197
+ | `cca:plan:<id>` | String (JSON) | 30 days | Plan record with step→job mapping |
198
+ | `cca:profile:<name>` | String (JSON) | permanent | Profile config |
199
+ | `cca:profiles:index` | Set | permanent | All profile names |
200
+
201
+ ### Disk fallback
202
+
203
+ When Redis is unavailable, cc-agent falls back to the original disk-based storage:
204
+
205
+ - `.cc-agent/jobs.json` — job metadata
71
206
  - `.cc-agent/jobs/<id>.log` — per-job output log
207
+ - `~/.cc-agent/profiles.json` — profiles
208
+
209
+ Existing disk profiles are automatically migrated to Redis on first startup with Redis available.
72
210
 
73
- On restart, jobs whose processes are still alive are recovered as `running`. Dead PIDs are marked `failed` automatically. This means **you don't lose job history if the MCP server restarts**.
211
+ ## Job statuses
74
212
 
75
- `.cc-agent/` is gitignored automatically.
213
+ | Status | Meaning |
214
+ |--------|---------|
215
+ | `pending` | Waiting for `depends_on` jobs to complete |
216
+ | `cloning` | Cloning the repo |
217
+ | `running` | Claude Code is running |
218
+ | `done` | Completed successfully |
219
+ | `failed` | Exited with an error (check `error` field) |
220
+ | `cancelled` | Cancelled by `cancel_job` |
76
221
 
77
222
  ## Tool call visibility
78
223
 
@@ -127,12 +272,14 @@ npm version patch && npm publish # if it's a library
127
272
  6. Tool calls are captured from the stream-JSON and stored in `tool_calls[]`
128
273
  7. On exit: job marked done/failed, workdir cleaned up after 10 minutes
129
274
  8. Jobs expire from memory after 1 hour (log file remains on disk)
275
+ 9. Pending jobs are promoted automatically every 3 seconds when their dependencies complete
130
276
 
131
277
  ## Environment variables
132
278
 
133
279
  | Variable | Description |
134
280
  |----------|-------------|
135
- | `CLAUDE_CODE_OAUTH_TOKEN` | Claude OAuth token (recommended) |
281
+ | `CLAUDE_CODE_TOKEN` | Claude OAuth token or Anthropic API key |
282
+ | `CLAUDE_CODE_OAUTH_TOKEN` | Claude OAuth token (alternative) |
136
283
  | `ANTHROPIC_API_KEY` | Anthropic API key (alternative) |
137
284
 
138
285
  ## Requirements
package/dist/agent.d.ts CHANGED
@@ -3,20 +3,24 @@ export declare class JobManager {
3
3
  private jobs;
4
4
  private kills;
5
5
  private defaultToken?;
6
- private diskLoadedJobs;
6
+ /** Jobs restored from storage — their output lives in store, not in job.output[]. */
7
+ private restoredJobs;
7
8
  constructor(token?: string);
8
- private loadFromDisk;
9
- private checkDiskLoadedRunning;
9
+ /** Must be called after initRedis() at startup. */
10
+ init(): Promise<void>;
11
+ private checkRestoredRunning;
10
12
  private persistJob;
11
13
  private addOutput;
12
14
  spawn(opts: SpawnOptions): Promise<string>;
13
15
  private run;
16
+ private tick;
17
+ private promote;
14
18
  getJob(id: string): Job | undefined;
15
- getOutput(id: string, offset?: number): {
19
+ getOutput(id: string, offset?: number): Promise<{
16
20
  lines: string[];
17
21
  done: boolean;
18
22
  toolCalls: string[];
19
- };
23
+ }>;
20
24
  list(): JobSummary[];
21
25
  sendMessage(id: string, message: string): {
22
26
  ok: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA+BhE,qBAAa,UAAU;IACrB,OAAO,CAAC,IAAI,CAA0B;IACtC,OAAO,CAAC,KAAK,CAAiC;IAC9C,OAAO,CAAC,YAAY,CAAC,CAAS;IAC9B,OAAO,CAAC,cAAc,CAAqB;gBAE/B,KAAK,CAAC,EAAE,MAAM;IAU1B,OAAO,CAAC,YAAY;IAoDpB,OAAO,CAAC,sBAAsB;IAgB9B,OAAO,CAAC,UAAU;IA+BlB,OAAO,CAAC,SAAS;IAKX,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;YA8BlC,GAAG;IA+GjB,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS;IAInC,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG;QAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,OAAO,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE;IAe1F,IAAI,IAAI,UAAU,EAAE;IAuBpB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;IAWzE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAkB3B,OAAO,CAAC,OAAO;CAahB"}
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA4EhE,qBAAa,UAAU;IACrB,OAAO,CAAC,IAAI,CAA0B;IACtC,OAAO,CAAC,KAAK,CAAiC;IAC9C,OAAO,CAAC,YAAY,CAAC,CAAS;IAC9B,qFAAqF;IACrF,OAAO,CAAC,YAAY,CAAqB;gBAE7B,KAAK,CAAC,EAAE,MAAM;IAW1B,mDAAmD;IAC7C,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAkC3B,OAAO,CAAC,oBAAoB;IAgB5B,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,SAAS;IAKX,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;YAsClC,GAAG;IAwGjB,OAAO,CAAC,IAAI;IAoBZ,OAAO,CAAC,OAAO;IASf,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS;IAI7B,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,OAAO,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAazG,IAAI,IAAI,UAAU,EAAE;IAuBpB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;IAWzE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAkB3B,OAAO,CAAC,OAAO;CAahB"}
package/dist/agent.js CHANGED
@@ -5,9 +5,10 @@ import { join } from "path";
5
5
  import { promisify } from "util";
6
6
  import { v4 as uuidv4 } from "uuid";
7
7
  import { runClaude } from "./claude.js";
8
- import { ensureStateDirs, loadPersistedJobs, savePersistedJobs, appendLog, readLogSync, isPidAlive, } from "./state.js";
8
+ import { ensureStateDirs, isPidAlive } from "./state.js";
9
+ import { jobStore } from "./store.js";
9
10
  const execFileAsync = promisify(execFile);
10
- const JOB_TTL_MS = 60 * 60 * 1000; // 1 hour — clean up old done jobs
11
+ const JOB_TTL_MS = 60 * 60 * 1000; // 1 hour — clean up old done jobs from memory
11
12
  // Claude Sonnet 4.6 pricing (USD per 1M tokens)
12
13
  const PRICE_INPUT = 3.00;
13
14
  const PRICE_OUTPUT = 15.00;
@@ -21,30 +22,83 @@ function calculateCost(job) {
21
22
  1_000_000;
22
23
  return Math.round(cost * 10000) / 10000;
23
24
  }
25
+ function toRecord(job) {
26
+ return {
27
+ id: job.id,
28
+ status: job.status,
29
+ repoUrl: job.repoUrl,
30
+ task: job.task,
31
+ branch: job.branch,
32
+ createBranch: job.createBranch,
33
+ dependsOn: job.dependsOn,
34
+ startedAt: job.startedAt.toISOString(),
35
+ finishedAt: job.finishedAt?.toISOString(),
36
+ exitCode: job.exitCode,
37
+ error: job.error,
38
+ pid: job.pid,
39
+ sessionIdAfter: job.sessionIdAfter,
40
+ usage: job.usage,
41
+ totalInputTokens: job.totalInputTokens,
42
+ totalOutputTokens: job.totalOutputTokens,
43
+ totalCacheReadTokens: job.totalCacheReadTokens,
44
+ totalCacheWriteTokens: job.totalCacheWriteTokens,
45
+ costUsd: job.costUsd,
46
+ recentTools: job.toolCalls.slice(-10),
47
+ outputLineCount: job.output.length,
48
+ };
49
+ }
50
+ function fromRecord(r) {
51
+ return {
52
+ id: r.id,
53
+ repoUrl: r.repoUrl,
54
+ task: r.task,
55
+ branch: r.branch,
56
+ createBranch: r.createBranch,
57
+ status: r.status,
58
+ output: [],
59
+ toolCalls: r.recentTools ?? [],
60
+ exitCode: r.exitCode,
61
+ error: r.error,
62
+ startedAt: new Date(r.startedAt ?? Date.now()),
63
+ finishedAt: r.finishedAt ? new Date(r.finishedAt) : undefined,
64
+ pid: r.pid,
65
+ sessionIdAfter: r.sessionIdAfter,
66
+ usage: r.usage,
67
+ totalInputTokens: r.totalInputTokens,
68
+ totalOutputTokens: r.totalOutputTokens,
69
+ totalCacheReadTokens: r.totalCacheReadTokens,
70
+ totalCacheWriteTokens: r.totalCacheWriteTokens,
71
+ costUsd: r.costUsd,
72
+ dependsOn: r.dependsOn,
73
+ };
74
+ }
24
75
  export class JobManager {
25
76
  jobs = new Map();
26
77
  kills = new Map();
27
78
  defaultToken;
28
- diskLoadedJobs = new Set();
79
+ /** Jobs restored from storage — their output lives in store, not in job.output[]. */
80
+ restoredJobs = new Set();
29
81
  constructor(token) {
30
82
  this.defaultToken = token;
31
83
  ensureStateDirs();
32
- this.loadFromDisk();
33
84
  // Periodic cleanup of old finished jobs
34
85
  setInterval(() => this.cleanup(), 5 * 60 * 1000).unref();
35
- // Periodic check for disk-loaded running jobs whose PID may have died
36
- setInterval(() => this.checkDiskLoadedRunning(), 30 * 1000).unref();
86
+ // Periodic check for restored running jobs whose PID may have died
87
+ setInterval(() => this.checkRestoredRunning(), 30 * 1000).unref();
88
+ // Dependency scheduler — promote pending jobs when deps are done
89
+ setInterval(() => this.tick(), 3000).unref();
37
90
  }
38
- loadFromDisk() {
39
- const persisted = loadPersistedJobs();
40
- const updated = [];
41
- for (const p of persisted) {
42
- let status = p.status;
43
- let error = p.error;
44
- let finishedAt = p.finishedAt;
91
+ /** Must be called after initRedis() at startup. */
92
+ async init() {
93
+ const records = await jobStore.loadAll();
94
+ const updates = [];
95
+ for (const r of records) {
96
+ let status = r.status;
97
+ let error = r.error;
98
+ let finishedAt = r.finishedAt;
45
99
  if (status === "running" || status === "cloning") {
46
- if (p.pid && isPidAlive(p.pid)) {
47
- // Still alive — keep as running, output reads from disk log
100
+ if (r.pid && isPidAlive(r.pid)) {
101
+ // Process still alive — keep as running
48
102
  }
49
103
  else {
50
104
  status = "failed";
@@ -52,38 +106,20 @@ export class JobManager {
52
106
  finishedAt = finishedAt ?? new Date().toISOString();
53
107
  }
54
108
  }
55
- const job = {
56
- id: p.id,
57
- repoUrl: p.repoUrl,
58
- task: p.task,
59
- branch: p.branch,
60
- createBranch: p.createBranch,
61
- status,
62
- output: [],
63
- toolCalls: [],
64
- exitCode: p.exitCode,
65
- error,
66
- startedAt: new Date(p.startedAt),
67
- finishedAt: finishedAt ? new Date(finishedAt) : undefined,
68
- pid: p.pid,
69
- sessionIdAfter: p.sessionIdAfter,
70
- usage: p.usage,
71
- totalInputTokens: p.totalInputTokens,
72
- totalOutputTokens: p.totalOutputTokens,
73
- totalCacheReadTokens: p.totalCacheReadTokens,
74
- totalCacheWriteTokens: p.totalCacheWriteTokens,
75
- costUsd: p.costUsd,
76
- };
109
+ const job = fromRecord({ ...r, status, error, finishedAt });
77
110
  this.jobs.set(job.id, job);
78
- this.diskLoadedJobs.add(job.id);
79
- updated.push({ ...p, status, error, finishedAt });
111
+ this.restoredJobs.add(job.id);
112
+ if (status !== r.status) {
113
+ updates.push({ ...r, status, error, finishedAt });
114
+ }
80
115
  }
81
- if (updated.length > 0) {
82
- savePersistedJobs(updated);
116
+ // Persist any status corrections back to store
117
+ for (const updated of updates) {
118
+ jobStore.saveJob(updated).catch(() => { });
83
119
  }
84
120
  }
85
- checkDiskLoadedRunning() {
86
- for (const id of this.diskLoadedJobs) {
121
+ checkRestoredRunning() {
122
+ for (const id of this.restoredJobs) {
87
123
  const job = this.jobs.get(id);
88
124
  if (!job)
89
125
  continue;
@@ -93,48 +129,25 @@ export class JobManager {
93
129
  job.finishedAt = new Date();
94
130
  job.error = (job.error ? job.error + "; " : "") + "Process exited after MCP restart";
95
131
  this.persistJob(job);
96
- appendLog(job.id, "[cc-agent] Process no longer alive after MCP restart");
132
+ this.addOutput(job, "[cc-agent] Process no longer alive after MCP restart");
97
133
  }
98
134
  }
99
135
  }
100
136
  }
101
137
  persistJob(job) {
102
- const persisted = loadPersistedJobs();
103
- const entry = {
104
- id: job.id,
105
- status: job.status,
106
- repoUrl: job.repoUrl,
107
- task: job.task,
108
- branch: job.branch,
109
- createBranch: job.createBranch,
110
- startedAt: job.startedAt.toISOString(),
111
- finishedAt: job.finishedAt?.toISOString(),
112
- exitCode: job.exitCode,
113
- error: job.error,
114
- pid: job.pid,
115
- sessionIdAfter: job.sessionIdAfter,
116
- usage: job.usage,
117
- totalInputTokens: job.totalInputTokens,
118
- totalOutputTokens: job.totalOutputTokens,
119
- totalCacheReadTokens: job.totalCacheReadTokens,
120
- totalCacheWriteTokens: job.totalCacheWriteTokens,
121
- costUsd: job.costUsd,
122
- };
123
- const idx = persisted.findIndex((p) => p.id === job.id);
124
- if (idx >= 0) {
125
- persisted[idx] = entry;
126
- }
127
- else {
128
- persisted.push(entry);
129
- }
130
- savePersistedJobs(persisted);
138
+ jobStore.saveJob(toRecord(job)).catch(() => { });
131
139
  }
132
140
  addOutput(job, line) {
133
141
  job.output.push(line);
134
- appendLog(job.id, line);
142
+ jobStore.appendOutput(job.id, line).catch(() => { });
135
143
  }
136
144
  async spawn(opts) {
137
145
  const id = uuidv4();
146
+ const pendingDeps = opts.dependsOn?.filter((depId) => {
147
+ const dep = this.jobs.get(depId);
148
+ return dep?.status !== "done";
149
+ });
150
+ const isPending = pendingDeps && pendingDeps.length > 0;
138
151
  const job = {
139
152
  id,
140
153
  repoUrl: opts.repoUrl,
@@ -144,20 +157,23 @@ export class JobManager {
144
157
  continueSession: opts.continueSession,
145
158
  maxBudgetUsd: opts.maxBudgetUsd ?? 20,
146
159
  sessionId: opts.sessionId,
147
- status: "cloning",
160
+ claudeToken: opts.claudeToken,
161
+ dependsOn: opts.dependsOn,
162
+ status: isPending ? "pending" : "cloning",
148
163
  output: [],
149
164
  toolCalls: [],
150
165
  startedAt: new Date(),
151
166
  };
152
167
  this.jobs.set(id, job);
153
168
  this.persistJob(job);
154
- // Run async — don't await
155
- this.run(job, opts.claudeToken ?? this.defaultToken).catch((err) => {
156
- job.status = "failed";
157
- job.error = String(err);
158
- job.finishedAt = new Date();
159
- this.persistJob(job);
160
- });
169
+ if (!isPending) {
170
+ this.run(job, opts.claudeToken ?? this.defaultToken).catch((err) => {
171
+ job.status = "failed";
172
+ job.error = String(err);
173
+ job.finishedAt = new Date();
174
+ this.persistJob(job);
175
+ });
176
+ }
161
177
  return id;
162
178
  }
163
179
  async run(job, token) {
@@ -182,7 +198,6 @@ export class JobManager {
182
198
  this.addOutput(job, `[cc-agent] Created branch: ${branchName}`);
183
199
  }
184
200
  else if (job.createBranch === "true") {
185
- // createBranch=true but no name — generate one from job id
186
201
  const auto = `agent/${job.id.slice(0, 8)}`;
187
202
  await execFileAsync("git", ["checkout", "-b", auto], { cwd: workDir });
188
203
  this.addOutput(job, `[cc-agent] Created branch: ${auto}`);
@@ -197,7 +212,6 @@ export class JobManager {
197
212
  maxBudgetUsd: job.maxBudgetUsd,
198
213
  sessionId: job.sessionId,
199
214
  });
200
- // Save PID for cross-restart tracking
201
215
  if (proc.pid != null) {
202
216
  job.pid = proc.pid;
203
217
  this.persistJob(job);
@@ -215,7 +229,6 @@ export class JobManager {
215
229
  job.totalOutputTokens = (job.totalOutputTokens ?? 0) + u.outputTokens;
216
230
  job.totalCacheReadTokens = (job.totalCacheReadTokens ?? 0) + (u.cacheReadTokens ?? 0);
217
231
  job.totalCacheWriteTokens = (job.totalCacheWriteTokens ?? 0) + (u.cacheWriteTokens ?? 0);
218
- // Prefer authoritative cost_usd from CLI result; fall back to calculated
219
232
  job.costUsd = u.costUsd != null ? u.costUsd : calculateCost(job);
220
233
  this.persistJob(job);
221
234
  });
@@ -225,23 +238,18 @@ export class JobManager {
225
238
  });
226
239
  proc.on("tool", (name) => {
227
240
  job.toolCalls.push(name);
228
- // Keep last 50 tool calls to avoid unbounded growth
229
241
  if (job.toolCalls.length > 50)
230
242
  job.toolCalls = job.toolCalls.slice(-50);
231
243
  });
232
- proc.on("error", (err) => {
233
- reject(err);
234
- });
244
+ proc.on("error", (err) => { reject(err); });
235
245
  proc.on("exit", (code) => {
236
246
  job.exitCode = code ?? undefined;
237
247
  job.stdinStream = null;
238
248
  this.kills.delete(job.id);
239
- if (code === 0 || code === null) {
249
+ if (code === 0 || code === null)
240
250
  resolve();
241
- }
242
- else {
251
+ else
243
252
  reject(new Error(`Claude exited with code ${code}`));
244
- }
245
253
  });
246
254
  });
247
255
  job.status = "done";
@@ -257,26 +265,55 @@ export class JobManager {
257
265
  finally {
258
266
  job.finishedAt = new Date();
259
267
  this.persistJob(job);
260
- // Clean up work dir after 10 minutes to allow output inspection
261
268
  if (workDir) {
262
269
  setTimeout(() => rm(workDir, { recursive: true, force: true }).catch(() => { }), 10 * 60 * 1000).unref();
263
270
  }
264
271
  }
265
272
  }
273
+ tick() {
274
+ for (const [, job] of this.jobs) {
275
+ if (job.status !== "pending")
276
+ continue;
277
+ if (!job.dependsOn?.length) {
278
+ this.promote(job);
279
+ continue;
280
+ }
281
+ const allDone = job.dependsOn.every((depId) => this.jobs.get(depId)?.status === "done");
282
+ const anyFailed = job.dependsOn.some((depId) => {
283
+ const s = this.jobs.get(depId)?.status;
284
+ return s === "failed" || s === "cancelled";
285
+ });
286
+ if (anyFailed) {
287
+ job.status = "failed";
288
+ job.error = "Dependency failed";
289
+ job.finishedAt = new Date();
290
+ this.persistJob(job);
291
+ }
292
+ else if (allDone) {
293
+ this.promote(job);
294
+ }
295
+ }
296
+ }
297
+ promote(job) {
298
+ this.run(job, job.claudeToken ?? this.defaultToken).catch((err) => {
299
+ job.status = "failed";
300
+ job.error = String(err);
301
+ job.finishedAt = new Date();
302
+ this.persistJob(job);
303
+ });
304
+ }
266
305
  getJob(id) {
267
306
  return this.jobs.get(id);
268
307
  }
269
- getOutput(id, offset = 0) {
308
+ async getOutput(id, offset = 0) {
270
309
  const job = this.jobs.get(id);
271
310
  if (!job) {
272
- // Job not in memory (expired or unknown) — try disk log
273
- const lines = readLogSync(id, offset);
311
+ const lines = await jobStore.getOutput(id, offset);
274
312
  return { lines, done: true, toolCalls: [] };
275
313
  }
276
314
  const done = job.status === "done" || job.status === "failed" || job.status === "cancelled";
277
- if (this.diskLoadedJobs.has(id)) {
278
- // Output lives on disk for jobs recovered after restart
279
- return { lines: readLogSync(id, offset), done, toolCalls: job.toolCalls };
315
+ if (this.restoredJobs.has(id)) {
316
+ return { lines: await jobStore.getOutput(id, offset), done, toolCalls: job.toolCalls };
280
317
  }
281
318
  return { lines: job.output.slice(offset), done, toolCalls: job.toolCalls };
282
319
  }
@@ -318,7 +355,7 @@ export class JobManager {
318
355
  const job = this.jobs.get(id);
319
356
  if (!job)
320
357
  return false;
321
- if (job.status !== "cloning" && job.status !== "running")
358
+ if (job.status !== "pending" && job.status !== "cloning" && job.status !== "running")
322
359
  return false;
323
360
  const kill = this.kills.get(id);
324
361
  if (kill) {
@@ -338,7 +375,7 @@ export class JobManager {
338
375
  job.finishedAt &&
339
376
  now - job.finishedAt.getTime() > JOB_TTL_MS) {
340
377
  this.jobs.delete(id);
341
- this.diskLoadedJobs.delete(id);
378
+ this.restoredJobs.delete(id);
342
379
  }
343
380
  }
344
381
  }