@gonzih/cc-agent 0.2.5 → 0.3.3
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 +159 -12
- package/dist/agent.d.ts +7 -5
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +97 -104
- package/dist/agent.js.map +1 -1
- package/dist/index.js +29 -14
- package/dist/index.js.map +1 -1
- package/dist/preamble.d.ts +3 -0
- package/dist/preamble.d.ts.map +1 -0
- package/dist/preamble.js +31 -0
- package/dist/preamble.js.map +1 -0
- package/dist/profiles.d.ts +6 -14
- package/dist/profiles.d.ts.map +1 -1
- package/dist/profiles.js +9 -36
- package/dist/profiles.js.map +1 -1
- package/dist/redis.d.ts +4 -0
- package/dist/redis.d.ts.map +1 -0
- package/dist/redis.js +106 -0
- package/dist/redis.js.map +1 -0
- package/dist/store.d.ts +70 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +286 -0
- package/dist/store.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# cc-agent
|
|
2
2
|
|
|
3
|
+
[](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
|
|
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)
|
|
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
|
-
|
|
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
|
-
// → {
|
|
113
|
+
// → { sent: true }
|
|
114
|
+
|
|
115
|
+
cost_summary()
|
|
116
|
+
// → { totalJobs: 1, totalCostUsd: 1.23, byRepo: { "https://github.com/...": 1.23 } }
|
|
64
117
|
```
|
|
65
118
|
|
|
66
|
-
|
|
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
|
-
|
|
143
|
+
### Profiles for repeated tasks
|
|
69
144
|
|
|
70
|
-
|
|
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
|
-
|
|
211
|
+
## Job statuses
|
|
74
212
|
|
|
75
|
-
|
|
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
|
-
| `
|
|
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,10 +3,12 @@ export declare class JobManager {
|
|
|
3
3
|
private jobs;
|
|
4
4
|
private kills;
|
|
5
5
|
private defaultToken?;
|
|
6
|
-
|
|
6
|
+
/** Jobs restored from storage — their output lives in store, not in job.output[]. */
|
|
7
|
+
private restoredJobs;
|
|
7
8
|
constructor(token?: string);
|
|
8
|
-
|
|
9
|
-
|
|
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>;
|
|
@@ -14,11 +16,11 @@ export declare class JobManager {
|
|
|
14
16
|
private tick;
|
|
15
17
|
private promote;
|
|
16
18
|
getJob(id: string): Job | undefined;
|
|
17
|
-
getOutput(id: string, offset?: number): {
|
|
19
|
+
getOutput(id: string, offset?: number): Promise<{
|
|
18
20
|
lines: string[];
|
|
19
21
|
done: boolean;
|
|
20
22
|
toolCalls: string[];
|
|
21
|
-
}
|
|
23
|
+
}>;
|
|
22
24
|
list(): JobSummary[];
|
|
23
25
|
sendMessage(id: string, message: string): {
|
|
24
26
|
ok: boolean;
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAQA,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;YAuClC,GAAG;IA0GjB,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,11 @@ 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 {
|
|
8
|
+
import { injectPreamble } from "./preamble.js";
|
|
9
|
+
import { ensureStateDirs, isPidAlive } from "./state.js";
|
|
10
|
+
import { jobStore } from "./store.js";
|
|
9
11
|
const execFileAsync = promisify(execFile);
|
|
10
|
-
const JOB_TTL_MS = 60 * 60 * 1000; // 1 hour — clean up old done jobs
|
|
12
|
+
const JOB_TTL_MS = 60 * 60 * 1000; // 1 hour — clean up old done jobs from memory
|
|
11
13
|
// Claude Sonnet 4.6 pricing (USD per 1M tokens)
|
|
12
14
|
const PRICE_INPUT = 3.00;
|
|
13
15
|
const PRICE_OUTPUT = 15.00;
|
|
@@ -21,32 +23,83 @@ function calculateCost(job) {
|
|
|
21
23
|
1_000_000;
|
|
22
24
|
return Math.round(cost * 10000) / 10000;
|
|
23
25
|
}
|
|
26
|
+
function toRecord(job) {
|
|
27
|
+
return {
|
|
28
|
+
id: job.id,
|
|
29
|
+
status: job.status,
|
|
30
|
+
repoUrl: job.repoUrl,
|
|
31
|
+
task: job.task,
|
|
32
|
+
branch: job.branch,
|
|
33
|
+
createBranch: job.createBranch,
|
|
34
|
+
dependsOn: job.dependsOn,
|
|
35
|
+
startedAt: job.startedAt.toISOString(),
|
|
36
|
+
finishedAt: job.finishedAt?.toISOString(),
|
|
37
|
+
exitCode: job.exitCode,
|
|
38
|
+
error: job.error,
|
|
39
|
+
pid: job.pid,
|
|
40
|
+
sessionIdAfter: job.sessionIdAfter,
|
|
41
|
+
usage: job.usage,
|
|
42
|
+
totalInputTokens: job.totalInputTokens,
|
|
43
|
+
totalOutputTokens: job.totalOutputTokens,
|
|
44
|
+
totalCacheReadTokens: job.totalCacheReadTokens,
|
|
45
|
+
totalCacheWriteTokens: job.totalCacheWriteTokens,
|
|
46
|
+
costUsd: job.costUsd,
|
|
47
|
+
recentTools: job.toolCalls.slice(-10),
|
|
48
|
+
outputLineCount: job.output.length,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function fromRecord(r) {
|
|
52
|
+
return {
|
|
53
|
+
id: r.id,
|
|
54
|
+
repoUrl: r.repoUrl,
|
|
55
|
+
task: r.task,
|
|
56
|
+
branch: r.branch,
|
|
57
|
+
createBranch: r.createBranch,
|
|
58
|
+
status: r.status,
|
|
59
|
+
output: [],
|
|
60
|
+
toolCalls: r.recentTools ?? [],
|
|
61
|
+
exitCode: r.exitCode,
|
|
62
|
+
error: r.error,
|
|
63
|
+
startedAt: new Date(r.startedAt ?? Date.now()),
|
|
64
|
+
finishedAt: r.finishedAt ? new Date(r.finishedAt) : undefined,
|
|
65
|
+
pid: r.pid,
|
|
66
|
+
sessionIdAfter: r.sessionIdAfter,
|
|
67
|
+
usage: r.usage,
|
|
68
|
+
totalInputTokens: r.totalInputTokens,
|
|
69
|
+
totalOutputTokens: r.totalOutputTokens,
|
|
70
|
+
totalCacheReadTokens: r.totalCacheReadTokens,
|
|
71
|
+
totalCacheWriteTokens: r.totalCacheWriteTokens,
|
|
72
|
+
costUsd: r.costUsd,
|
|
73
|
+
dependsOn: r.dependsOn,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
24
76
|
export class JobManager {
|
|
25
77
|
jobs = new Map();
|
|
26
78
|
kills = new Map();
|
|
27
79
|
defaultToken;
|
|
28
|
-
|
|
80
|
+
/** Jobs restored from storage — their output lives in store, not in job.output[]. */
|
|
81
|
+
restoredJobs = new Set();
|
|
29
82
|
constructor(token) {
|
|
30
83
|
this.defaultToken = token;
|
|
31
84
|
ensureStateDirs();
|
|
32
|
-
this.loadFromDisk();
|
|
33
85
|
// Periodic cleanup of old finished jobs
|
|
34
86
|
setInterval(() => this.cleanup(), 5 * 60 * 1000).unref();
|
|
35
|
-
// Periodic check for
|
|
36
|
-
setInterval(() => this.
|
|
87
|
+
// Periodic check for restored running jobs whose PID may have died
|
|
88
|
+
setInterval(() => this.checkRestoredRunning(), 30 * 1000).unref();
|
|
37
89
|
// Dependency scheduler — promote pending jobs when deps are done
|
|
38
90
|
setInterval(() => this.tick(), 3000).unref();
|
|
39
91
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let
|
|
46
|
-
let
|
|
92
|
+
/** Must be called after initRedis() at startup. */
|
|
93
|
+
async init() {
|
|
94
|
+
const records = await jobStore.loadAll();
|
|
95
|
+
const updates = [];
|
|
96
|
+
for (const r of records) {
|
|
97
|
+
let status = r.status;
|
|
98
|
+
let error = r.error;
|
|
99
|
+
let finishedAt = r.finishedAt;
|
|
47
100
|
if (status === "running" || status === "cloning") {
|
|
48
|
-
if (
|
|
49
|
-
//
|
|
101
|
+
if (r.pid && isPidAlive(r.pid)) {
|
|
102
|
+
// Process still alive — keep as running
|
|
50
103
|
}
|
|
51
104
|
else {
|
|
52
105
|
status = "failed";
|
|
@@ -54,39 +107,20 @@ export class JobManager {
|
|
|
54
107
|
finishedAt = finishedAt ?? new Date().toISOString();
|
|
55
108
|
}
|
|
56
109
|
}
|
|
57
|
-
const job = {
|
|
58
|
-
id: p.id,
|
|
59
|
-
repoUrl: p.repoUrl,
|
|
60
|
-
task: p.task,
|
|
61
|
-
branch: p.branch,
|
|
62
|
-
createBranch: p.createBranch,
|
|
63
|
-
status,
|
|
64
|
-
output: [],
|
|
65
|
-
toolCalls: [],
|
|
66
|
-
exitCode: p.exitCode,
|
|
67
|
-
error,
|
|
68
|
-
startedAt: new Date(p.startedAt),
|
|
69
|
-
finishedAt: finishedAt ? new Date(finishedAt) : undefined,
|
|
70
|
-
pid: p.pid,
|
|
71
|
-
sessionIdAfter: p.sessionIdAfter,
|
|
72
|
-
usage: p.usage,
|
|
73
|
-
totalInputTokens: p.totalInputTokens,
|
|
74
|
-
totalOutputTokens: p.totalOutputTokens,
|
|
75
|
-
totalCacheReadTokens: p.totalCacheReadTokens,
|
|
76
|
-
totalCacheWriteTokens: p.totalCacheWriteTokens,
|
|
77
|
-
costUsd: p.costUsd,
|
|
78
|
-
dependsOn: p.dependsOn,
|
|
79
|
-
};
|
|
110
|
+
const job = fromRecord({ ...r, status, error, finishedAt });
|
|
80
111
|
this.jobs.set(job.id, job);
|
|
81
|
-
this.
|
|
82
|
-
|
|
112
|
+
this.restoredJobs.add(job.id);
|
|
113
|
+
if (status !== r.status) {
|
|
114
|
+
updates.push({ ...r, status, error, finishedAt });
|
|
115
|
+
}
|
|
83
116
|
}
|
|
84
|
-
|
|
85
|
-
|
|
117
|
+
// Persist any status corrections back to store
|
|
118
|
+
for (const updated of updates) {
|
|
119
|
+
jobStore.saveJob(updated).catch(() => { });
|
|
86
120
|
}
|
|
87
121
|
}
|
|
88
|
-
|
|
89
|
-
for (const id of this.
|
|
122
|
+
checkRestoredRunning() {
|
|
123
|
+
for (const id of this.restoredJobs) {
|
|
90
124
|
const job = this.jobs.get(id);
|
|
91
125
|
if (!job)
|
|
92
126
|
continue;
|
|
@@ -96,46 +130,17 @@ export class JobManager {
|
|
|
96
130
|
job.finishedAt = new Date();
|
|
97
131
|
job.error = (job.error ? job.error + "; " : "") + "Process exited after MCP restart";
|
|
98
132
|
this.persistJob(job);
|
|
99
|
-
|
|
133
|
+
this.addOutput(job, "[cc-agent] Process no longer alive after MCP restart");
|
|
100
134
|
}
|
|
101
135
|
}
|
|
102
136
|
}
|
|
103
137
|
}
|
|
104
138
|
persistJob(job) {
|
|
105
|
-
|
|
106
|
-
const entry = {
|
|
107
|
-
id: job.id,
|
|
108
|
-
status: job.status,
|
|
109
|
-
repoUrl: job.repoUrl,
|
|
110
|
-
task: job.task,
|
|
111
|
-
branch: job.branch,
|
|
112
|
-
createBranch: job.createBranch,
|
|
113
|
-
startedAt: job.startedAt.toISOString(),
|
|
114
|
-
finishedAt: job.finishedAt?.toISOString(),
|
|
115
|
-
exitCode: job.exitCode,
|
|
116
|
-
error: job.error,
|
|
117
|
-
pid: job.pid,
|
|
118
|
-
sessionIdAfter: job.sessionIdAfter,
|
|
119
|
-
usage: job.usage,
|
|
120
|
-
totalInputTokens: job.totalInputTokens,
|
|
121
|
-
totalOutputTokens: job.totalOutputTokens,
|
|
122
|
-
totalCacheReadTokens: job.totalCacheReadTokens,
|
|
123
|
-
totalCacheWriteTokens: job.totalCacheWriteTokens,
|
|
124
|
-
costUsd: job.costUsd,
|
|
125
|
-
dependsOn: job.dependsOn,
|
|
126
|
-
};
|
|
127
|
-
const idx = persisted.findIndex((p) => p.id === job.id);
|
|
128
|
-
if (idx >= 0) {
|
|
129
|
-
persisted[idx] = entry;
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
persisted.push(entry);
|
|
133
|
-
}
|
|
134
|
-
savePersistedJobs(persisted);
|
|
139
|
+
jobStore.saveJob(toRecord(job)).catch(() => { });
|
|
135
140
|
}
|
|
136
141
|
addOutput(job, line) {
|
|
137
142
|
job.output.push(line);
|
|
138
|
-
|
|
143
|
+
jobStore.appendOutput(job.id, line).catch(() => { });
|
|
139
144
|
}
|
|
140
145
|
async spawn(opts) {
|
|
141
146
|
const id = uuidv4();
|
|
@@ -155,6 +160,7 @@ export class JobManager {
|
|
|
155
160
|
sessionId: opts.sessionId,
|
|
156
161
|
claudeToken: opts.claudeToken,
|
|
157
162
|
dependsOn: opts.dependsOn,
|
|
163
|
+
preamble: opts.preamble,
|
|
158
164
|
status: isPending ? "pending" : "cloning",
|
|
159
165
|
output: [],
|
|
160
166
|
toolCalls: [],
|
|
@@ -163,7 +169,6 @@ export class JobManager {
|
|
|
163
169
|
this.jobs.set(id, job);
|
|
164
170
|
this.persistJob(job);
|
|
165
171
|
if (!isPending) {
|
|
166
|
-
// Run async — don't await
|
|
167
172
|
this.run(job, opts.claudeToken ?? this.defaultToken).catch((err) => {
|
|
168
173
|
job.status = "failed";
|
|
169
174
|
job.error = String(err);
|
|
@@ -181,7 +186,9 @@ export class JobManager {
|
|
|
181
186
|
job.workDir = workDir;
|
|
182
187
|
this.addOutput(job, `[cc-agent] Cloning ${job.repoUrl}...`);
|
|
183
188
|
const cloneArgs = ["clone", "--depth", "1"];
|
|
184
|
-
if
|
|
189
|
+
// Only checkout an existing branch during clone; if we're creating a new
|
|
190
|
+
// branch it doesn't exist on remote yet, so clone the default branch first.
|
|
191
|
+
if (job.branch && !job.createBranch)
|
|
185
192
|
cloneArgs.push("--branch", job.branch);
|
|
186
193
|
cloneArgs.push(job.repoUrl, workDir);
|
|
187
194
|
await execFileAsync("git", cloneArgs);
|
|
@@ -195,7 +202,6 @@ export class JobManager {
|
|
|
195
202
|
this.addOutput(job, `[cc-agent] Created branch: ${branchName}`);
|
|
196
203
|
}
|
|
197
204
|
else if (job.createBranch === "true") {
|
|
198
|
-
// createBranch=true but no name — generate one from job id
|
|
199
205
|
const auto = `agent/${job.id.slice(0, 8)}`;
|
|
200
206
|
await execFileAsync("git", ["checkout", "-b", auto], { cwd: workDir });
|
|
201
207
|
this.addOutput(job, `[cc-agent] Created branch: ${auto}`);
|
|
@@ -205,12 +211,11 @@ export class JobManager {
|
|
|
205
211
|
this.persistJob(job);
|
|
206
212
|
this.addOutput(job, `[cc-agent] Starting Claude with task...`);
|
|
207
213
|
await new Promise((resolve, reject) => {
|
|
208
|
-
const proc = runClaude(job.task, workDir, token, {
|
|
214
|
+
const proc = runClaude(injectPreamble(job.task, job.preamble), workDir, token, {
|
|
209
215
|
continueSession: job.continueSession,
|
|
210
216
|
maxBudgetUsd: job.maxBudgetUsd,
|
|
211
217
|
sessionId: job.sessionId,
|
|
212
218
|
});
|
|
213
|
-
// Save PID for cross-restart tracking
|
|
214
219
|
if (proc.pid != null) {
|
|
215
220
|
job.pid = proc.pid;
|
|
216
221
|
this.persistJob(job);
|
|
@@ -228,7 +233,6 @@ export class JobManager {
|
|
|
228
233
|
job.totalOutputTokens = (job.totalOutputTokens ?? 0) + u.outputTokens;
|
|
229
234
|
job.totalCacheReadTokens = (job.totalCacheReadTokens ?? 0) + (u.cacheReadTokens ?? 0);
|
|
230
235
|
job.totalCacheWriteTokens = (job.totalCacheWriteTokens ?? 0) + (u.cacheWriteTokens ?? 0);
|
|
231
|
-
// Prefer authoritative cost_usd from CLI result; fall back to calculated
|
|
232
236
|
job.costUsd = u.costUsd != null ? u.costUsd : calculateCost(job);
|
|
233
237
|
this.persistJob(job);
|
|
234
238
|
});
|
|
@@ -238,23 +242,18 @@ export class JobManager {
|
|
|
238
242
|
});
|
|
239
243
|
proc.on("tool", (name) => {
|
|
240
244
|
job.toolCalls.push(name);
|
|
241
|
-
// Keep last 50 tool calls to avoid unbounded growth
|
|
242
245
|
if (job.toolCalls.length > 50)
|
|
243
246
|
job.toolCalls = job.toolCalls.slice(-50);
|
|
244
247
|
});
|
|
245
|
-
proc.on("error", (err) => {
|
|
246
|
-
reject(err);
|
|
247
|
-
});
|
|
248
|
+
proc.on("error", (err) => { reject(err); });
|
|
248
249
|
proc.on("exit", (code) => {
|
|
249
250
|
job.exitCode = code ?? undefined;
|
|
250
251
|
job.stdinStream = null;
|
|
251
252
|
this.kills.delete(job.id);
|
|
252
|
-
if (code === 0 || code === null)
|
|
253
|
+
if (code === 0 || code === null)
|
|
253
254
|
resolve();
|
|
254
|
-
|
|
255
|
-
else {
|
|
255
|
+
else
|
|
256
256
|
reject(new Error(`Claude exited with code ${code}`));
|
|
257
|
-
}
|
|
258
257
|
});
|
|
259
258
|
});
|
|
260
259
|
job.status = "done";
|
|
@@ -270,7 +269,6 @@ export class JobManager {
|
|
|
270
269
|
finally {
|
|
271
270
|
job.finishedAt = new Date();
|
|
272
271
|
this.persistJob(job);
|
|
273
|
-
// Clean up work dir after 10 minutes to allow output inspection
|
|
274
272
|
if (workDir) {
|
|
275
273
|
setTimeout(() => rm(workDir, { recursive: true, force: true }).catch(() => { }), 10 * 60 * 1000).unref();
|
|
276
274
|
}
|
|
@@ -284,13 +282,10 @@ export class JobManager {
|
|
|
284
282
|
this.promote(job);
|
|
285
283
|
continue;
|
|
286
284
|
}
|
|
287
|
-
const allDone = job.dependsOn.every((depId) =>
|
|
288
|
-
const dep = this.jobs.get(depId);
|
|
289
|
-
return dep?.status === "done";
|
|
290
|
-
});
|
|
285
|
+
const allDone = job.dependsOn.every((depId) => this.jobs.get(depId)?.status === "done");
|
|
291
286
|
const anyFailed = job.dependsOn.some((depId) => {
|
|
292
|
-
const
|
|
293
|
-
return
|
|
287
|
+
const s = this.jobs.get(depId)?.status;
|
|
288
|
+
return s === "failed" || s === "cancelled";
|
|
294
289
|
});
|
|
295
290
|
if (anyFailed) {
|
|
296
291
|
job.status = "failed";
|
|
@@ -314,17 +309,15 @@ export class JobManager {
|
|
|
314
309
|
getJob(id) {
|
|
315
310
|
return this.jobs.get(id);
|
|
316
311
|
}
|
|
317
|
-
getOutput(id, offset = 0) {
|
|
312
|
+
async getOutput(id, offset = 0) {
|
|
318
313
|
const job = this.jobs.get(id);
|
|
319
314
|
if (!job) {
|
|
320
|
-
|
|
321
|
-
const lines = readLogSync(id, offset);
|
|
315
|
+
const lines = await jobStore.getOutput(id, offset);
|
|
322
316
|
return { lines, done: true, toolCalls: [] };
|
|
323
317
|
}
|
|
324
318
|
const done = job.status === "done" || job.status === "failed" || job.status === "cancelled";
|
|
325
|
-
if (this.
|
|
326
|
-
|
|
327
|
-
return { lines: readLogSync(id, offset), done, toolCalls: job.toolCalls };
|
|
319
|
+
if (this.restoredJobs.has(id)) {
|
|
320
|
+
return { lines: await jobStore.getOutput(id, offset), done, toolCalls: job.toolCalls };
|
|
328
321
|
}
|
|
329
322
|
return { lines: job.output.slice(offset), done, toolCalls: job.toolCalls };
|
|
330
323
|
}
|
|
@@ -386,7 +379,7 @@ export class JobManager {
|
|
|
386
379
|
job.finishedAt &&
|
|
387
380
|
now - job.finishedAt.getTime() > JOB_TTL_MS) {
|
|
388
381
|
this.jobs.delete(id);
|
|
389
|
-
this.
|
|
382
|
+
this.restoredJobs.delete(id);
|
|
390
383
|
}
|
|
391
384
|
}
|
|
392
385
|
}
|