Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GITHUB_TOKEN=your_token
OPENROUTER_API_KEY=your_key
CFORGE_MAX_BUDGET=5
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ npm install
npm run build
export GITHUB_TOKEN=your_token
export OPENROUTER_API_KEY=your_key
export CFORGE_MAX_BUDGET=5 # max USD per Claude Code session (default: 5)
cforge-dev chat
```

Expand Down
5 changes: 3 additions & 2 deletions src/infrastructure/claude/ClaudeCodeRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync, spawn } from "child_process";
import { CodeRunner, RunOptions, RunResult } from "../../domain/interfaces/CodeRunner";

const DEFAULT_MODEL = "claude-sonnet-4-6";
const DEFAULT_BUDGET = 1.0;
const DEFAULT_BUDGET = 5;
const DEFAULT_TOOLS = ["Bash", "Edit", "Read", "Write", "Glob", "Grep"];

interface ClaudeJsonResponse {
Expand All @@ -18,7 +18,8 @@ interface ClaudeJsonResponse {
export class ClaudeCodeRunner implements CodeRunner {
async run(prompt: string, workingDir: string, options: RunOptions): Promise<RunResult> {
const model = options.model ?? DEFAULT_MODEL;
const budget = options.maxBudgetUsd ?? DEFAULT_BUDGET;
const envBudget = process.env.CFORGE_MAX_BUDGET ? Number(process.env.CFORGE_MAX_BUDGET) : DEFAULT_BUDGET;
const budget = options.maxBudgetUsd ?? envBudget;
const tools = options.allowedTools ?? DEFAULT_TOOLS;

// Check if claude CLI exists
Expand Down
60 changes: 58 additions & 2 deletions tests/infrastructure/ClaudeCodeRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe("ClaudeCodeRunner", () => {
);
});

it("should pass correct default flags", async () => {
it("should pass correct default flags with env-based budget", async () => {
mockSpawnSuccess(successJson);
const runner = new ClaudeCodeRunner();
await runner.run("prompt", "/tmp/work", {});
Expand All @@ -241,12 +241,68 @@ describe("ClaudeCodeRunner", () => {
"--model", "claude-sonnet-4-6",
"--dangerously-skip-permissions",
"--allowed-tools", "Bash,Edit,Read,Write,Glob,Grep",
"--max-budget-usd", "1",
"--max-budget-usd", "5",
"--output-format", "json",
]);
expect(opts.cwd).toBe("/tmp/work");
});

it("should read CFORGE_MAX_BUDGET from env with default of 5", async () => {
mockSpawnSuccess(successJson);
const original = process.env.CFORGE_MAX_BUDGET;
try {
delete process.env.CFORGE_MAX_BUDGET;
const runner = new ClaudeCodeRunner();
await runner.run("prompt", "/tmp/test", {});

const args: string[] = (spawn as jest.Mock).mock.calls[0][1];
const idx = args.indexOf("--max-budget-usd");
expect(args[idx + 1]).toBe("5");
} finally {
if (original !== undefined) process.env.CFORGE_MAX_BUDGET = original;
}
});

it("should use CFORGE_MAX_BUDGET env var when set", async () => {
mockSpawnSuccess(successJson);
const original = process.env.CFORGE_MAX_BUDGET;
try {
process.env.CFORGE_MAX_BUDGET = "10";
const runner = new ClaudeCodeRunner();
await runner.run("prompt", "/tmp/test", {});

const args: string[] = (spawn as jest.Mock).mock.calls[0][1];
const idx = args.indexOf("--max-budget-usd");
expect(args[idx + 1]).toBe("10");
} finally {
if (original !== undefined) {
process.env.CFORGE_MAX_BUDGET = original;
} else {
delete process.env.CFORGE_MAX_BUDGET;
}
}
});

it("should let RunOptions.maxBudgetUsd override env var", async () => {
mockSpawnSuccess(successJson);
const original = process.env.CFORGE_MAX_BUDGET;
try {
process.env.CFORGE_MAX_BUDGET = "10";
const runner = new ClaudeCodeRunner();
await runner.run("prompt", "/tmp/test", { maxBudgetUsd: 20 });

const args: string[] = (spawn as jest.Mock).mock.calls[0][1];
const idx = args.indexOf("--max-budget-usd");
expect(args[idx + 1]).toBe("20");
} finally {
if (original !== undefined) {
process.env.CFORGE_MAX_BUDGET = original;
} else {
delete process.env.CFORGE_MAX_BUDGET;
}
}
});

it("should respect custom model and budget options", async () => {
mockSpawnSuccess(successJson);
const runner = new ClaudeCodeRunner();
Expand Down
Loading