karajan-code 1.3.0 → 1.4.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
@@ -36,7 +36,9 @@ Instead of running one AI agent and manually reviewing its output, `kj` chains a
36
36
  - **Review profiles** — standard, strict, relaxed, paranoid
37
37
  - **Budget tracking** — per-session token and cost monitoring with `--trace`
38
38
  - **Git automation** — auto-commit, auto-push, auto-PR after approval
39
- - **Session management** — pause/resume with fail-fast detection
39
+ - **Session management** — pause/resume with fail-fast detection and automatic cleanup of expired sessions
40
+ - **Plugin system** — extend with custom agents via `.karajan/plugins/`
41
+ - **Retry with backoff** — automatic recovery from transient API errors (429, 5xx) with exponential backoff and jitter
40
42
  - **Planning Game integration** — optionally pair with [Planning Game](https://github.com/AgenteIA-Geniova/planning-game) for agile project management (tasks, sprints, estimation) — like Jira, but open-source and XP-native
41
43
 
42
44
  > **Best with MCP** — Karajan Code is designed to be used as an MCP server inside your AI agent (Claude, Codex, etc.). The agent sends tasks to `kj_run`, gets real-time progress notifications, and receives structured results — no copy-pasting needed.
@@ -428,7 +430,7 @@ Use `kj roles show <role>` to inspect any template. Create a project override to
428
430
  git clone https://github.com/manufosela/karajan-code.git
429
431
  cd karajan-code
430
432
  npm install
431
- npm test # Run 761+ tests with Vitest
433
+ npm test # Run 899+ tests with Vitest
432
434
  npm run test:watch # Watch mode
433
435
  npm run validate # Lint + test
434
436
  ```
@@ -439,6 +441,7 @@ npm run validate # Lint + test
439
441
 
440
442
  ## Links
441
443
 
444
+ - [Website](https://karajancode.com) (also [kj-code.com](https://kj-code.com))
442
445
  - [Changelog](CHANGELOG.md)
443
446
  - [Security Policy](SECURITY.md)
444
447
  - [License (AGPL-3.0)](LICENSE)
package/docs/README.es.md CHANGED
@@ -36,7 +36,9 @@ En lugar de ejecutar un agente de IA y revisar manualmente su output, `kj` encad
36
36
  - **Perfiles de revision** — standard, strict, relaxed, paranoid
37
37
  - **Tracking de presupuesto** — monitorizacion de tokens y costes por sesion con `--trace`
38
38
  - **Automatizacion Git** — auto-commit, auto-push, auto-PR tras aprobacion
39
- - **Gestion de sesiones** — pausa/reanudacion con deteccion fail-fast
39
+ - **Gestion de sesiones** — pausa/reanudacion con deteccion fail-fast y limpieza automatica de sesiones expiradas
40
+ - **Sistema de plugins** — extiende con agentes custom via `.karajan/plugins/`
41
+ - **Retry con backoff** — recuperacion automatica ante errores transitorios de API (429, 5xx) con backoff exponencial y jitter
40
42
  - **Integracion con Planning Game** — combina opcionalmente con [Planning Game](https://github.com/AgenteIA-Geniova/planning-game) para gestion agil de proyectos (tareas, sprints, estimacion) — como Jira, pero open-source y nativo XP
41
43
 
42
44
  > **Mejor con MCP** — Karajan Code esta disenado para usarse como servidor MCP dentro de tu agente de IA (Claude, Codex, etc.). El agente envia tareas a `kj_run`, recibe notificaciones de progreso en tiempo real, y obtiene resultados estructurados — sin copiar y pegar.
@@ -227,7 +229,7 @@ Usa `kj roles show <rol>` para inspeccionar cualquier template. Crea un override
227
229
  git clone https://github.com/manufosela/karajan-code.git
228
230
  cd karajan-code
229
231
  npm install
230
- npm test # Ejecutar 761+ tests con Vitest
232
+ npm test # Ejecutar 899+ tests con Vitest
231
233
  npm run test:watch # Modo watch
232
234
  npm run validate # Lint + test
233
235
  ```
@@ -238,6 +240,7 @@ npm run validate # Lint + test
238
240
 
239
241
  ## Enlaces
240
242
 
243
+ - [Web](https://karajancode.com) (tambien [kj-code.com](https://kj-code.com))
241
244
  - [Changelog](../CHANGELOG.md)
242
245
  - [Politica de seguridad](../SECURITY.md)
243
246
  - [Licencia (AGPL-3.0)](../LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "karajan-code",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Local multi-agent coding orchestrator with TDD, SonarQube, and code review pipeline",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0",
package/src/cli.js CHANGED
@@ -71,6 +71,7 @@ program
71
71
  .option("--max-total-minutes <n>")
72
72
  .option("--base-branch <name>")
73
73
  .option("--base-ref <ref>")
74
+ .option("--coder-fallback <name>")
74
75
  .option("--reviewer-fallback <name>")
75
76
  .option("--reviewer-retries <n>")
76
77
  .option("--auto-commit")
package/src/config.js CHANGED
@@ -33,7 +33,7 @@ const DEFAULTS = {
33
33
  review_rules: "./review-rules.md",
34
34
  coder_rules: "./coder-rules.md",
35
35
  base_branch: "main",
36
- coder_options: { model: null, auto_approve: true },
36
+ coder_options: { model: null, auto_approve: true, fallback_coder: null },
37
37
  reviewer_options: {
38
38
  output_format: "json",
39
39
  require_schema: true,
@@ -240,6 +240,7 @@ export function applyRunOverrides(config, flags) {
240
240
  if (flags.maxIterationMinutes) out.session.max_iteration_minutes = Number(flags.maxIterationMinutes);
241
241
  if (flags.maxTotalMinutes) out.session.max_total_minutes = Number(flags.maxTotalMinutes);
242
242
  if (flags.baseBranch) out.base_branch = flags.baseBranch;
243
+ if (flags.coderFallback) out.coder_options.fallback_coder = flags.coderFallback;
243
244
  if (flags.reviewerFallback) out.reviewer_options.fallback_reviewer = flags.reviewerFallback;
244
245
  if (flags.reviewerRetries !== undefined) out.reviewer_options.retries = Number(flags.reviewerRetries);
245
246
  if (flags.autoCommit !== undefined) out.git.auto_commit = Boolean(flags.autoCommit);
@@ -0,0 +1,83 @@
1
+ import { createAgent } from "../agents/index.js";
2
+ import { addCheckpoint } from "../session-store.js";
3
+ import { detectRateLimit } from "../utils/rate-limit-detector.js";
4
+
5
+ /**
6
+ * Run a coder-like role with fallback on rate limit.
7
+ * Tries the primary agent first. If it fails with a rate limit,
8
+ * switches to the fallback agent (if configured).
9
+ * Non-rate-limit failures stop immediately (no fallback).
10
+ *
11
+ * Returns { execResult, attempts, allRateLimited }
12
+ */
13
+ export async function runCoderWithFallback({
14
+ coderName,
15
+ fallbackCoder,
16
+ config,
17
+ logger,
18
+ emitter,
19
+ RoleClass,
20
+ roleInput,
21
+ session,
22
+ iteration,
23
+ onAttemptResult
24
+ }) {
25
+ const candidates = [coderName];
26
+ if (fallbackCoder && fallbackCoder !== coderName) {
27
+ candidates.push(fallbackCoder);
28
+ }
29
+
30
+ const attempts = [];
31
+ let allRateLimited = true;
32
+
33
+ for (const name of candidates) {
34
+ const agentConfig = {
35
+ ...config,
36
+ roles: { ...config.roles, coder: { ...config.roles?.coder, provider: name } }
37
+ };
38
+
39
+ const role = new RoleClass({ config: agentConfig, logger, emitter, createAgentFn: createAgent });
40
+ await role.init();
41
+
42
+ const execResult = await role.execute(roleInput);
43
+
44
+ if (onAttemptResult) {
45
+ await onAttemptResult({ coder: name, result: execResult.result });
46
+ }
47
+
48
+ const rateLimited = !execResult.ok && detectRateLimit({
49
+ stderr: execResult.result?.error || "",
50
+ stdout: execResult.result?.output || ""
51
+ }).isRateLimit;
52
+
53
+ attempts.push({
54
+ coder: name,
55
+ ok: execResult.ok,
56
+ rateLimited,
57
+ result: execResult.result,
58
+ execResult
59
+ });
60
+
61
+ await addCheckpoint(session, {
62
+ stage: "coder-attempt",
63
+ iteration,
64
+ coder: name,
65
+ ok: execResult.ok,
66
+ rateLimited
67
+ });
68
+
69
+ if (execResult.ok) {
70
+ return { execResult, attempts, allRateLimited: false };
71
+ }
72
+
73
+ // Only fallback on rate limit errors
74
+ if (!rateLimited) {
75
+ allRateLimited = false;
76
+ return { execResult: null, attempts, allRateLimited: false };
77
+ }
78
+
79
+ logger.warn(`Agent ${name} hit rate limit, trying fallback...`);
80
+ }
81
+
82
+ return { execResult: null, attempts, allRateLimited };
83
+ }
@@ -1,4 +1,5 @@
1
1
  import { createAgent } from "../agents/index.js";
2
+ import { CoderRole } from "../roles/coder-role.js";
2
3
  import { RefactorerRole } from "../roles/refactorer-role.js";
3
4
  import { SonarRole } from "../roles/sonar-role.js";
4
5
  import { addCheckpoint, markSessionStatus, saveSession, pauseSession } from "../session-store.js";
@@ -7,7 +8,9 @@ import { evaluateTddPolicy } from "../review/tdd-policy.js";
7
8
  import { validateReviewResult } from "../review/schema.js";
8
9
  import { emitProgress, makeEvent } from "../utils/events.js";
9
10
  import { runReviewerWithFallback } from "./reviewer-fallback.js";
11
+ import { runCoderWithFallback } from "./agent-fallback.js";
10
12
  import { invokeSolomon } from "./solomon-escalation.js";
13
+ import { detectRateLimit } from "../utils/rate-limit-detector.js";
11
14
 
12
15
  export async function runCoderStage({ coderRoleInstance, coderRole, config, logger, emitter, eventBase, session, plannedTask, trackBudget, iteration }) {
13
16
  logger.setContext({ iteration, stage: "coder" });
@@ -35,8 +38,70 @@ export async function runCoderStage({ coderRoleInstance, coderRole, config, logg
35
38
  trackBudget({ role: "coder", provider: coderRole.provider, model: coderRole.model, result: coderExecResult.result, duration_ms: Date.now() - coderStart });
36
39
 
37
40
  if (!coderExecResult.ok) {
38
- await markSessionStatus(session, "failed");
39
41
  const details = coderExecResult.result?.error || coderExecResult.summary || "unknown error";
42
+ const rateLimitCheck = detectRateLimit({
43
+ stderr: coderExecResult.result?.error || "",
44
+ stdout: coderExecResult.result?.output || ""
45
+ });
46
+
47
+ if (rateLimitCheck.isRateLimit) {
48
+ // Try fallback agent if configured
49
+ const fallbackCoder = config.coder_options?.fallback_coder;
50
+ if (fallbackCoder && fallbackCoder !== coderRole.provider) {
51
+ logger.warn(`Coder ${coderRole.provider} hit rate limit, falling back to ${fallbackCoder}`);
52
+ emitProgress(
53
+ emitter,
54
+ makeEvent("coder:fallback", { ...eventBase, stage: "coder" }, {
55
+ message: `Coder ${coderRole.provider} rate-limited, switching to ${fallbackCoder}`,
56
+ detail: { primary: coderRole.provider, fallback: fallbackCoder }
57
+ })
58
+ );
59
+
60
+ const fallbackResult = await runCoderWithFallback({
61
+ coderName: fallbackCoder,
62
+ fallbackCoder: null,
63
+ config,
64
+ logger,
65
+ emitter,
66
+ RoleClass: CoderRole,
67
+ roleInput: { task: plannedTask, reviewerFeedback: session.last_reviewer_feedback, sonarSummary: session.last_sonar_summary, onOutput: coderOnOutput },
68
+ session,
69
+ iteration,
70
+ onAttemptResult: ({ coder, result }) => {
71
+ trackBudget({ role: "coder", provider: coder, model: coderRole.model, result, duration_ms: Date.now() - coderStart });
72
+ }
73
+ });
74
+
75
+ if (fallbackResult.execResult?.ok) {
76
+ await addCheckpoint(session, { stage: "coder", iteration, note: `Coder completed via fallback (${fallbackCoder})` });
77
+ emitProgress(
78
+ emitter,
79
+ makeEvent("coder:end", { ...eventBase, stage: "coder" }, {
80
+ message: `Coder completed (fallback: ${fallbackCoder})`
81
+ })
82
+ );
83
+ return;
84
+ }
85
+ }
86
+
87
+ // No fallback or fallback also failed — pause
88
+ const question = `Agent ${coderRole.provider} hit a rate limit: ${rateLimitCheck.message}. Session paused until the token window resets.`;
89
+ await pauseSession(session, {
90
+ question,
91
+ context: { iteration, stage: "coder", reason: "rate_limit", agent: coderRole.provider, detail: rateLimitCheck.message }
92
+ });
93
+ emitProgress(
94
+ emitter,
95
+ makeEvent("coder:rate_limit", { ...eventBase, stage: "coder" }, {
96
+ status: "paused",
97
+ message: question,
98
+ detail: { agent: coderRole.provider, rateLimitMessage: rateLimitCheck.message, sessionId: session.id }
99
+ })
100
+ );
101
+ return { action: "pause", result: { paused: true, sessionId: session.id, question, context: "rate_limit" } };
102
+ }
103
+
104
+ await markSessionStatus(session, "failed");
40
105
  emitProgress(
41
106
  emitter,
42
107
  makeEvent("coder:end", { ...eventBase, stage: "coder" }, {
@@ -71,8 +136,30 @@ export async function runRefactorerStage({ refactorerRole, config, logger, emitt
71
136
  const refResult = await refRole.execute(plannedTask);
72
137
  trackBudget({ role: "refactorer", provider: refactorerRole.provider, model: refactorerRole.model, result: refResult.result, duration_ms: Date.now() - refactorerStart });
73
138
  if (!refResult.ok) {
74
- await markSessionStatus(session, "failed");
75
139
  const details = refResult.result?.error || refResult.summary || "unknown error";
140
+ const rateLimitCheck = detectRateLimit({
141
+ stderr: refResult.result?.error || "",
142
+ stdout: refResult.result?.output || ""
143
+ });
144
+
145
+ if (rateLimitCheck.isRateLimit) {
146
+ const question = `Agent ${refactorerRole.provider} hit a rate limit: ${rateLimitCheck.message}. Session paused until the token window resets.`;
147
+ await pauseSession(session, {
148
+ question,
149
+ context: { iteration, stage: "refactorer", reason: "rate_limit", agent: refactorerRole.provider, detail: rateLimitCheck.message }
150
+ });
151
+ emitProgress(
152
+ emitter,
153
+ makeEvent("refactorer:rate_limit", { ...eventBase, stage: "refactorer" }, {
154
+ status: "paused",
155
+ message: question,
156
+ detail: { agent: refactorerRole.provider, rateLimitMessage: rateLimitCheck.message, sessionId: session.id }
157
+ })
158
+ );
159
+ return { action: "pause", result: { paused: true, sessionId: session.id, question, context: "rate_limit" } };
160
+ }
161
+
162
+ await markSessionStatus(session, "failed");
76
163
  emitProgress(
77
164
  emitter,
78
165
  makeEvent("refactorer:end", { ...eventBase, stage: "refactorer" }, {
@@ -318,12 +405,35 @@ export async function runReviewerStage({ reviewerRole, config, logger, emitter,
318
405
  });
319
406
 
320
407
  if (!reviewerExec.execResult || !reviewerExec.execResult.ok) {
321
- await markSessionStatus(session, "failed");
322
408
  const lastAttempt = reviewerExec.attempts.at(-1);
323
409
  const details =
324
410
  lastAttempt?.result?.error ||
325
411
  lastAttempt?.execResult?.summary ||
326
412
  `reviewer=${lastAttempt?.reviewer || "unknown"}`;
413
+
414
+ const rateLimitCheck = detectRateLimit({
415
+ stderr: lastAttempt?.result?.error || "",
416
+ stdout: lastAttempt?.result?.output || ""
417
+ });
418
+
419
+ if (rateLimitCheck.isRateLimit) {
420
+ const question = `Reviewer ${reviewerRole.provider} hit a rate limit: ${rateLimitCheck.message}. Session paused until the token window resets.`;
421
+ await pauseSession(session, {
422
+ question,
423
+ context: { iteration, stage: "reviewer", reason: "rate_limit", agent: reviewerRole.provider, detail: rateLimitCheck.message }
424
+ });
425
+ emitProgress(
426
+ emitter,
427
+ makeEvent("reviewer:rate_limit", { ...eventBase, stage: "reviewer" }, {
428
+ status: "paused",
429
+ message: question,
430
+ detail: { agent: reviewerRole.provider, rateLimitMessage: rateLimitCheck.message, sessionId: session.id }
431
+ })
432
+ );
433
+ return { action: "pause", result: { paused: true, sessionId: session.id, question, context: "rate_limit" } };
434
+ }
435
+
436
+ await markSessionStatus(session, "failed");
327
437
  emitProgress(
328
438
  emitter,
329
439
  makeEvent("reviewer:end", { ...eventBase, stage: "reviewer" }, {
@@ -255,11 +255,17 @@ export async function runFlow({ task, config, logger, flags = {}, emitter = null
255
255
  logger.info(`Iteration ${i}/${config.max_iterations}`);
256
256
 
257
257
  // --- Coder ---
258
- await runCoderStage({ coderRoleInstance, coderRole, config, logger, emitter, eventBase, session, plannedTask, trackBudget, iteration: i });
258
+ const coderResult = await runCoderStage({ coderRoleInstance, coderRole, config, logger, emitter, eventBase, session, plannedTask, trackBudget, iteration: i });
259
+ if (coderResult?.action === "pause") {
260
+ return coderResult.result;
261
+ }
259
262
 
260
263
  // --- Refactorer ---
261
264
  if (refactorerEnabled) {
262
- await runRefactorerStage({ refactorerRole, config, logger, emitter, eventBase, session, plannedTask, trackBudget, iteration: i });
265
+ const refResult = await runRefactorerStage({ refactorerRole, config, logger, emitter, eventBase, session, plannedTask, trackBudget, iteration: i });
266
+ if (refResult?.action === "pause") {
267
+ return refResult.result;
268
+ }
263
269
  }
264
270
 
265
271
  // --- TDD Policy ---
@@ -302,6 +308,9 @@ export async function runFlow({ task, config, logger, flags = {}, emitter = null
302
308
  reviewerRole, config, logger, emitter, eventBase, session, trackBudget,
303
309
  iteration: i, reviewRules, task, repeatDetector, budgetSummary
304
310
  });
311
+ if (reviewerResult.action === "pause") {
312
+ return reviewerResult.result;
313
+ }
305
314
  review = reviewerResult.review;
306
315
  if (reviewerResult.stalled) {
307
316
  return reviewerResult.stalledResult;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Detects rate limit / usage cap messages from CLI agent output.
3
+ * Returns { isRateLimit, agent, message } where agent is the best guess
4
+ * of which CLI triggered it (or "unknown").
5
+ */
6
+
7
+ const RATE_LIMIT_PATTERNS = [
8
+ // Claude CLI
9
+ { pattern: /usage limit/i, agent: "claude" },
10
+ { pattern: /plan's usage limit/i, agent: "claude" },
11
+ { pattern: /Claude Pro usage limit/i, agent: "claude" },
12
+
13
+ // OpenAI / Codex CLI
14
+ { pattern: /exceeded your current quota/i, agent: "codex" },
15
+
16
+ // Gemini CLI
17
+ { pattern: /resource exhausted/i, agent: "gemini" },
18
+ { pattern: /quota exceeded/i, agent: "gemini" },
19
+
20
+ // Generic (match any agent)
21
+ { pattern: /rate limit/i, agent: "unknown" },
22
+ { pattern: /token limit reached/i, agent: "unknown" },
23
+ { pattern: /\b429\b/, agent: "unknown" },
24
+ { pattern: /too many requests/i, agent: "unknown" },
25
+ { pattern: /throttl/i, agent: "unknown" },
26
+ ];
27
+
28
+ export function detectRateLimit({ stderr = "", stdout = "" }) {
29
+ const combined = `${stderr}\n${stdout}`;
30
+
31
+ for (const { pattern, agent } of RATE_LIMIT_PATTERNS) {
32
+ if (pattern.test(combined)) {
33
+ const matchedLine = combined.split("\n").find((l) => pattern.test(l)) || combined.trim();
34
+ return {
35
+ isRateLimit: true,
36
+ agent,
37
+ message: matchedLine.trim()
38
+ };
39
+ }
40
+ }
41
+
42
+ return { isRateLimit: false, agent: "", message: "" };
43
+ }