Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(remediation): free job steps run when the score target is unreachable; combined_command aims at the reachable ceiling (E3)

garrytan5341f8f76958
diff --git a/src/commands/doctor/remediate.ts b/src/commands/doctor/remediate.ts
index fdda05f0cc..821c4451f3 100644
--- a/src/commands/doctor/remediate.ts
+++ b/src/commands/doctor/remediate.ts
@@ -100,7 +100,7 @@ export async function runRemediationPlan(engine: BrainEngine, args: string[]): P
const plan = await computeRemediationPlan(engine, { targetScore, repairs: { noEmbed: args.includes('--no-embed') } });
if (args.includes('--json')) {
console.log(JSON.stringify({ ...plan, plan: plan.plan.map(step => ({ ...step, command: jobStepCommand(step) })),
- combined_command: combinedRemediateCommand(plan, targetScore) }, null, 2));
+ combined_command: combinedRemediateCommand(plan, plan.target_unreachable ? plan.max_reachable_score : targetScore) }, null, 2));
return;
}
for (const line of renderRemediationPlanLines(plan, targetScore)) console.log(line);
@@ -164,7 +164,7 @@ export function renderRemediationPlanLines(plan: RemediationPlanShape, targetSco
for (const notice of plan.explicit_repairs) lines.push(` ${notice.kind}: ${notice.preview_command}`);
}
if (plan.plan.length > 0 || repairs.length > 0) {
- lines.push(`\nApply everything${repairs.length ? ' after the user agrees' : ''}: ${combinedRemediateCommand(plan, targetScore)}`);
+ lines.push(`\nApply everything${repairs.length ? ' after the user agrees' : ''}: ${combinedRemediateCommand(plan, plan.target_unreachable ? plan.max_reachable_score : targetScore)}`);
if (repairs.length) lines.push('Ask the user before applying any repair step.');
}
if (plan.blocked.length > 0) {
diff --git a/src/core/remediation/run.ts b/src/core/remediation/run.ts
index 5624178b96..3098d2b735 100644
--- a/src/core/remediation/run.ts
+++ b/src/core/remediation/run.ts
@@ -113,7 +113,13 @@ export async function runRemediation(
: [];
// Embeddings a budget stop left behind after re-sealing; the re-sealed pages no longer show up in a repair plan.
let pendingEmbedSources = includeRepairs && manifest ? [...(cp?.pending_embed_sources ?? [])] : [];
- if (initialPlan.target_unreachable && !(includeRepairs && (repairSteps.length || pendingEmbedSources.length))) {
+ // E3: an unreachable score target skips the PAID job steps only; free job
+ // steps (no provider spend) still run, so the brain improves as far as it can.
+ const initialHealth = await engine.getHealth();
+ const plannedRecs = computeRecommendations(initialHealth, ctx, extraRemediations)
+ .filter((r) => r.status === 'remediable' && (!manifest || manifest.job_ids.includes(r.id)));
+ const freeRecs = plannedRecs.filter((r) => (r.est_usd_cost ?? 0) === 0);
+ if (initialPlan.target_unreachable && freeRecs.length === 0 && !(includeRepairs && (repairSteps.length || pendingEmbedSources.length))) {
hooks.onTargetUnreachable?.(targetScore, initialPlan.max_reachable_score);
return synthetic(initialPlan.brain_score_current, {
target_unreachable: { target: targetScore, ceiling: initialPlan.max_reachable_score },
@@ -121,12 +127,11 @@ export async function runRemediation(
});
}
const jobStepsSkipped = initialPlan.target_unreachable
- ? { reason: 'target_unreachable' as const, target: targetScore, ceiling: initialPlan.max_reachable_score } : undefined;
+ ? { reason: 'target_unreachable' as const, target: targetScore, ceiling: initialPlan.max_reachable_score,
+ skipped: plannedRecs.filter((r) => !freeRecs.includes(r)).map((r) => r.id) } : undefined;
if (jobStepsSkipped) hooks.onTargetUnreachable?.(targetScore, initialPlan.max_reachable_score);
- const initialHealth = await engine.getHealth();
- let recs: RemediationStep[] = jobStepsSkipped ? [] : computeRecommendations(initialHealth, ctx, extraRemediations)
- .filter((r) => r.status === 'remediable' && (!manifest || manifest.job_ids.includes(r.id)));
+ let recs: RemediationStep[] = jobStepsSkipped ? freeRecs : plannedRecs;
const skippedRepairs = includeRepairs ? [] : repairSteps;
if (!includeRepairs) repairSteps = [];
if (recs.length === 0 && repairSteps.length === 0 && pendingEmbedSources.length === 0) {
@@ -404,7 +409,8 @@ export async function runRemediation(
// would resubmit completed extras every iteration, forever.
const pendingExtras = extraRemediations.filter((r) => !attemptedIds.has(r.id));
recs = computeRecommendations(freshHealth, ctx, pendingExtras)
- .filter((r) => r.status === 'remediable' && !attemptedIds.has(r.id) && (!manifest || manifest.job_ids.includes(r.id)));
+ .filter((r) => r.status === 'remediable' && !attemptedIds.has(r.id) && (!manifest || manifest.job_ids.includes(r.id))
+ && (!jobStepsSkipped || (r.est_usd_cost ?? 0) === 0));
}
};
diff --git a/src/core/remediation/types.ts b/src/core/remediation/types.ts
index 3d591bece4..36cd8e06c5 100644
--- a/src/core/remediation/types.ts
+++ b/src/core/remediation/types.ts
@@ -137,8 +137,11 @@ export interface RemediationResult {
target: number;
ceiling: number;
};
- /** Set when job steps were skipped for an unreachable target while repair steps still ran. */
- job_steps_skipped?: { reason: 'target_unreachable'; target: number; ceiling: number };
+ /**
+ * Set when the score target is unreachable: the paid job steps (`skipped`
+ * ids) were not run; free job steps and included repair steps still ran.
+ */
+ job_steps_skipped?: { reason: 'target_unreachable'; target: number; ceiling: number; skipped?: string[] };
/** Set when `--resume` refused (a checkpoint for another brain). */
resume_refused?: { reason: string; checkpoint_brain_id: string; brain_id: string; plan_hash: string };
/** Repair steps the run applied or refused (only when `repairs` was passed). */
diff --git a/test/doctor-remediation-plan-render.test.ts b/test/doctor-remediation-plan-render.test.ts
index b73d314ffa..47e1f8784b 100644
--- a/test/doctor-remediation-plan-render.test.ts
+++ b/test/doctor-remediation-plan-render.test.ts
@@ -100,4 +100,16 @@ describe('renderRemediationPlanLines', () => {
expect(text).toContain('Blocked checks');
expect(text).not.toContain('Brain is at target');
});
+
+ test('E3: an unreachable target renders a combined command aimed at the reachable ceiling (it would otherwise refuse)', () => {
+ const plan = planFixture({
+ brain_score_current: 45,
+ target_unreachable: true,
+ max_reachable_score: 70,
+ plan: [{ step: 1, id: 'extract.stale', job: 'extract', params: { stale: true }, severity: 'medium', est_seconds: 30, est_usd_cost: 0,
+ rationale: '3 stale pages', status: 'remediable', depends_on: [], idempotency_key: 'k' } as Plan['plan'][number]],
+ });
+ const text = renderRemediationPlanLines(plan, 90).join('\n');
+ expect(text).toContain('gbrain doctor --remediate --yes --max-usd 0 --target-score 70');
+ });
});
diff --git a/test/remediation-run-extras.serial.test.ts b/test/remediation-run-extras.serial.test.ts
index d73fdd5c0e..62c36d52be 100644
--- a/test/remediation-run-extras.serial.test.ts
+++ b/test/remediation-run-extras.serial.test.ts
@@ -108,4 +108,23 @@ describe('runRemediation extraRemediations threading', () => {
'extract-timeline-from-meetings',
]);
});
+
+ test('E3: an unreachable score target still runs the free job steps and skips only the paid ones', async () => {
+ const { runRemediation } = await import('../src/core/remediation/run.ts');
+ submittedJobs.length = 0;
+ const paid = makeRemediationStep({
+ id: 'onboard.paid_step', job: 'extract-takes-from-pages', params: {}, severity: 'medium', est_seconds: 5,
+ est_usd_cost: 2, rationale: 'synthetic paid extra', status: 'remediable',
+ });
+ let unreachable = false;
+ const result = await runRemediation(
+ engine,
+ { targetScore: 101, extraRemediations: [extra('onboard.free_step', 'extract-ner'), paid], maxJobs: 5, maxUsd: 10 },
+ { onTargetUnreachable: () => { unreachable = true; } },
+ );
+ expect(unreachable).toBe(true);
+ expect(result.submitted.map((s) => s.id)).toEqual(['onboard.free_step']);
+ expect(result.job_steps_skipped).toMatchObject({ reason: 'target_unreachable', target: 101, skipped: ['onboard.paid_step'] });
+ expect(submittedJobs.map((j) => j.name)).toEqual(['extract-ner']);
+ });
});