mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:37:19 +01:00
feat(dispatch): wrap COMPOSED bodies with LOOP v2 invariants
Roadmap T1: "new src/lib/orchestration/wrap-loop-v2.ts + change to
src/app/api/control/dispatch/route.ts to prepend loop invariants to
every auto-injected body. Templates can't opt out. After this, LOOP v2
quality applies to strategist runs, not just manual prompts."
Before this commit: ACTION:QUEUE inherited LOOP v2 from the canned
template body the queue item is. ACTION:NEXTBEST inherited it from
the next_best template at ~/.config/agent-prompts.json. But
ACTION:COMPOSED — the strategist's free-form Groq composition — went
out raw. No accountability line. No T0/T1/T2 triage. No scope cap.
No handoff requirement. The autonomous agent fleet's most contextual
dispatch path had the least discipline.
wrap-loop-v2.ts ships the canonical compact preamble:
- "open your response with Picked <X> (T_); displaced <Y>"
- T0/T1/T2 classification rule
- "one user-visible outcome per session; >3 commits → split"
- Required handoff fields (status / done / next / tests / todos / health)
The wrapper is prepended (not appended) so the operating frame is the
agent's first read. Composed body comes after, separated by a divider.
Compact-on-purpose: we drop the full next_best PICK algorithm because
the strategist already picked — re-running the pick in the agent's
context would either waste tokens or invite an override of Groq's
pick. Keep what's universal; cut what the composed body already
supplies.
Idempotent guard: if the body already starts with the preamble (won't
happen on the dispatch path; defensive against future double-wrap
through some other entry point) the wrapper is a no-op. No double
token charge.
Pairs with 73b8f0a (Evidence-for-pick guardrail). Together: the
strategist must cite a real commit to choose COMPOSED, AND the
COMPOSED body it writes is wrapped in the same discipline manual
prompts get. Strategist mode is now a notch safer to default-on for
projects that opt in.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
73b8f0ae38
commit
680c7ed058
2 files changed
+73
-1
No files matched your search
@@ -21,6 +21,7 @@ import { getBeaconSettings } from "@/db/queries/beacon-settings";
|
||||
import { getProjectAutopilotOverride } from "@/db/queries/projects";
|
||||
import { DEFAULT_AUTO_INJECT_MODE } from "@/lib/constants/control";
|
||||
import { evaluateDispatchGates } from "@/lib/orchestration/dispatch-gates";
|
||||
import { wrapLoopV2 } from "@/lib/orchestration/wrap-loop-v2";
|
||||
import type { AutoInjectMode } from "@/config/beacon";
|
||||
import { getOrangeCatContext, renderOrangeCatContext } from "@/lib/integrations/orangecat-context";
|
||||
import { promptFingerprint, recordControlAuditEvent } from "@/db/queries/control-audit-events";
|
||||
@@ -337,7 +338,15 @@ export async function POST(req: NextRequest) {
|
||||
const prompt = buildPrompt(handoff, queue, projectName, gitBranch, recentCommits, recentOutcomes, mission, orangeCatContext);
|
||||
|
||||
try {
|
||||
const { action, reason, prompt: composedPrompt, citedCommit } = await callGroq(prompt);
|
||||
const groqResult = await callGroq(prompt);
|
||||
const { action, reason, citedCommit } = groqResult;
|
||||
// Templates can't opt out: wrap every composed body in LOOP v2
|
||||
// invariants (accountability line + classification + scope cap +
|
||||
// handoff requirement). Other actions (queue / nextbest) don't go
|
||||
// through this code path — they either return null prompt (caller
|
||||
// looks it up) or carry their own LOOP v2 preamble via the
|
||||
// canonical template body.
|
||||
const composedPrompt = groqResult.prompt ? wrapLoopV2(groqResult.prompt) : undefined;
|
||||
|
||||
// Evidence-for-pick guardrail (Strategist guardrail #2 from the roadmap):
|
||||
// COMPOSED must cite a short SHA from the supplied recentCommits list.
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* LOOP v2 wrapper for strategist-composed dispatch bodies.
|
||||
*
|
||||
* Background. /api/control/dispatch can return three actions: QUEUE (fire
|
||||
* the queue's head), NEXTBEST (fire the canned next_best template — which
|
||||
* IS the full LOOP v2 preamble), and COMPOSED (Groq writes a fresh prompt
|
||||
* body tailored to the current context). The first two paths inherit LOOP
|
||||
* v2 discipline because the templates carry it. COMPOSED bodies didn't —
|
||||
* they were free-form prose written by Groq, with no accountability line,
|
||||
* no T0/T1/T2 triage, no scope cap, no handoff requirement.
|
||||
*
|
||||
* This wrapper closes that gap. Every COMPOSED body emitted by the
|
||||
* strategist gets the invariants prepended before it reaches the agent.
|
||||
* Templates "can't opt out" — the dispatch route applies this
|
||||
* unconditionally to action === "composed" results.
|
||||
*
|
||||
* Why prepend (not append): the agent's first read sets the operating
|
||||
* frame. Putting "open with Picked <X> (T_); displaced <Y>" at the top
|
||||
* makes it the response shape the agent commits to before it engages
|
||||
* with the substantive task below.
|
||||
*
|
||||
* Why a compact subset of the next_best preamble (not the whole thing):
|
||||
* the next_best body's job is to TRIAGE — read state, pick a task,
|
||||
* justify the pick. A COMPOSED body already names the task ("fix the
|
||||
* failing test at X"). Re-running the pick algorithm would either
|
||||
* waste tokens or, worse, give Groq's composed pick the opportunity to
|
||||
* be overridden by the agent's own re-pick. Keep what's universal
|
||||
* (accountability, classification, scope, handoff) and drop what the
|
||||
* composed body already supplies (the actual decision).
|
||||
*/
|
||||
|
||||
export const LOOP_V2_PREAMBLE = `Before acting: open your response with "Picked <X> (T_); displaced <Y>." where T is the severity tier:
|
||||
T0 = broken in production OR data integrity at risk
|
||||
T1 = a real user is blocked today or by ship
|
||||
T2 = nothing broken, no user blocked, code/DX improvement
|
||||
|
||||
Scope: one user-visible outcome per session. If the work needs more than 3 commits, split into separate sessions and stop. Pivot only at commit boundaries — never mid-commit.
|
||||
|
||||
When done, update the session handoff file (~/.claude/sessions/<project>.md) with exactly these lines:
|
||||
status: ready | working | blocked
|
||||
done: <one sentence what you completed>
|
||||
next: <precise next step or empty when nothing mid-flight>
|
||||
tests: <N pass · N fail, or 'no suite'>
|
||||
todos: <count of open TODO/FIXME/HACK items>
|
||||
health: <good | needs attention | critical>
|
||||
|
||||
──────────────────────────────────────────────────────────────────────
|
||||
Substantive task (from the strategist):
|
||||
──────────────────────────────────────────────────────────────────────
|
||||
`;
|
||||
|
||||
/**
|
||||
* Prepend LOOP v2 invariants to a strategist-composed body. Idempotent: if
|
||||
* the body already contains the preamble (defensive — should not happen in
|
||||
* the dispatch path, but cheap to guard against), returns the body as-is
|
||||
* so a future double-wrap doesn't double-charge tokens.
|
||||
*/
|
||||
export function wrapLoopV2(composedBody: string): string {
|
||||
const trimmed = composedBody.trim();
|
||||
if (!trimmed) return trimmed;
|
||||
if (trimmed.startsWith(LOOP_V2_PREAMBLE.trim().slice(0, 64))) return trimmed;
|
||||
return `${LOOP_V2_PREAMBLE}${trimmed}\n`;
|
||||
}
|
||||
Reference in new issue
Block a user