mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:58:25 +01:00
feat(prompt): pipe the user's prompt queue into renderTaskForAdapter so the agent sees it
You called out that my session.md 'next:' didn't reflect queue items
because I literally never received them — the next_best prompt body
was just the generic scan instruction. Investigation in the previous
turn confirmed: queue lives in browser localStorage (and a busted
/tmp/agent-queue-<tab> mirror that doesn't persist in Vercel cloud),
the agent only ever got 'scan: 1.interrupted 2.tests 3.SSOT 4.mission'.
Plumbed the queue through end-to-end, model-agnostically:
• src/lib/orchestration/contract.ts — OrchestrationTaskRequest
gains an optional queue: string[] field.
• src/lib/orchestration/renderers.ts — new renderQueueBlock() emits
'User's prompt queue for this project (in priority order): ...'
when the request carries queue items. Block appended for every
adapter (claude / openclaw / codex / gemini fallthrough) so
visibility is uniform regardless of model. Capped at 10 items
displayed with overflow indicator.
• src/app/api/orchestration/run/route.ts — RunOrchestrationBody
accepts queue (≤200 items × 4kB, matching /api/beacon/queue
persistence limits).
• src/hooks/use-control-data.ts:runWithBrain — reads the queue
from window.localStorage[queueKey(project.tab)] at dispatch time
and passes it through. Best-effort: SSR / private browsing /
storage-disabled → empty queue → no harm, prompt renders without
the block. Uses the same key the usePromptQueue hook owns.
Net effect: every dispatch (manual or auto-fired) carries the queue
in its prompt body. The agent can weigh queue items against other
scanning candidates and pick what's truly highest-impact — which is
the human-in-the-loop semantics the system was already trying to do
client-side, now extended to my own reasoning.
What this does NOT do (deliberate scope cut):
• Doesn't fix the broken /tmp/agent-queue-<tab> server mirror.
Real fix is DB-backed queue storage — separate work.
• Doesn't change handleAutoInject's queue-head-drain behavior.
queue[0] still fires as the prompt directly when next_best would
have fired; the new block surfaces queue[1..N] context to the
agent so they know what's coming next.
tsc clean, eslint clean (src/ + home/), npm run test:home 85/85.
This commit is contained in:
4 files changed
+50
No files matched your search
@@ -34,6 +34,11 @@ const RunOrchestrationBody = z.object({
|
||||
intent: z.enum(ORCHESTRATION_TASK_INTENT_IDS),
|
||||
model: z.string().trim().max(160).optional(),
|
||||
customInstructions: z.string().trim().max(4000).optional(),
|
||||
// Optional queue snapshot from the client's localStorage. Plumbed
|
||||
// straight into the prompt body via renderTaskForAdapter so the agent
|
||||
// can weigh queue items against other candidates. Max 200 items × 4kB
|
||||
// matches the persistence limit in /api/beacon/queue/[tab].
|
||||
queue: z.array(z.string().max(4000)).max(200).optional(),
|
||||
});
|
||||
|
||||
async function scheduleOpenClawWorker(runId: string, request: OrchestrationTaskRequest) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { ControlData, ProjectState } from "@/lib/control-types";
|
||||
import type { FastProjectState } from "@/lib/control-fast-state";
|
||||
import type { OrchestrationTaskIntentId } from "@/lib/orchestration";
|
||||
import { getJson, postJson, throwApiError } from "@/lib/api/fetch";
|
||||
import { queueKey } from "@/lib/control-storage";
|
||||
import type { Agent } from "@/lib/agent-registry";
|
||||
type AgentEntry = ControlData["agentRegistry"]["agents"][number];
|
||||
type TabResult = { status: string; tab?: string; reason?: string; error?: string };
|
||||
@@ -190,12 +191,26 @@ export function useControlData(): ControlDataHook {
|
||||
|
||||
const runWithBrain = async (project: ProjectState, intent: OrchestrationTaskIntentId) => {
|
||||
setError(null);
|
||||
// Read the per-project queue from localStorage and pass it through so the
|
||||
// agent's prompt body includes "User's prompt queue for this project" —
|
||||
// otherwise the queue is invisible to the model (it only sees the bare
|
||||
// next_best scan instruction). Best-effort: SSR / private-browsing /
|
||||
// localStorage-disabled → empty queue → no harm, just no context.
|
||||
let queue: string[] = [];
|
||||
try {
|
||||
const raw = typeof window !== "undefined" ? window.localStorage.getItem(queueKey(project.tab)) : null;
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (Array.isArray(parsed)) queue = parsed.filter((s) => typeof s === "string");
|
||||
}
|
||||
} catch { /* ignore — queue context is best-effort */ }
|
||||
const res = await postJson("/api/orchestration/run", {
|
||||
projectId: project.projectId,
|
||||
projectKey: project.tab,
|
||||
projectPath: project.dir,
|
||||
adapter: data?.agentConfig.agent ?? "claude",
|
||||
intent,
|
||||
queue,
|
||||
});
|
||||
if (!res.ok) await throwApiError(res, `HTTP ${res.status}`);
|
||||
await refresh(true);
|
||||
|
||||
@@ -73,6 +73,12 @@ export type OrchestrationTaskRequest = {
|
||||
intent: OrchestrationTaskIntentId;
|
||||
model?: string;
|
||||
customInstructions?: string;
|
||||
/** Optional snapshot of the user's prompt queue for this project at
|
||||
* dispatch time. The renderer surfaces these to the agent as "Pending
|
||||
* queue items" so the agent can weigh them against other scanning
|
||||
* candidates. Model-agnostic: the same context lands in every
|
||||
* adapter's prompt body via renderTaskForAdapter. */
|
||||
queue?: string[];
|
||||
};
|
||||
|
||||
// `status` is the agent's self-reported lifecycle state for this handoff:
|
||||
|
||||
@@ -95,14 +95,36 @@ function renderIntentBody(request: OrchestrationTaskRequest): string {
|
||||
}
|
||||
}
|
||||
|
||||
// Surface the user's prompt queue in the dispatched prompt. The agent now
|
||||
// sees what's pending and can weigh queue items against other scanning
|
||||
// candidates (interrupted work, failing tests, etc). Without this block,
|
||||
// the queue is invisible to the agent — the renderer only fires queue[0]
|
||||
// AS the prompt when handleAutoInject drains it, but the rest of the
|
||||
// queue (and any context about "the user explicitly queued X, Y, Z")
|
||||
// never reaches the model. Model-agnostic: every adapter that calls
|
||||
// renderTaskForAdapter gets the same context block.
|
||||
function renderQueueBlock(queue?: string[]): string | null {
|
||||
if (!queue || queue.length === 0) return null;
|
||||
const lines = queue.slice(0, 10).map((item, i) => `${i + 1}. ${item}`);
|
||||
const overflow = queue.length > 10 ? `\n…and ${queue.length - 10} more` : "";
|
||||
return [
|
||||
`User's prompt queue for this project (in priority order):`,
|
||||
...lines,
|
||||
overflow,
|
||||
`Weigh these against the scanning candidates below — pick what's truly highest-impact right now.`,
|
||||
].filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
export function renderTaskForAdapter(request: OrchestrationTaskRequest, adapter: AdapterId = request.adapter): string {
|
||||
const intent = getOrchestrationIntent(request.intent);
|
||||
const queueBlock = renderQueueBlock(request.queue);
|
||||
|
||||
// Claude adapter: CLAUDE.md is always loaded in the session and already contains
|
||||
// execution rules and project context. Emit only the intent body — no redundant
|
||||
// header or rules. buildPromptWithSession appends the session file + update instruction.
|
||||
if (adapter === "claude") {
|
||||
const sections: string[] = [renderIntentBody(request)];
|
||||
if (queueBlock) sections.push(queueBlock);
|
||||
// For "custom" intent the body IS customInstructions — don't append it again
|
||||
if (request.intent !== "custom" && request.customInstructions?.trim()) {
|
||||
sections.push(`Additional instructions:\n${request.customInstructions.trim()}`);
|
||||
@@ -117,6 +139,8 @@ export function renderTaskForAdapter(request: OrchestrationTaskRequest, adapter:
|
||||
renderSharedExecutionRules(),
|
||||
];
|
||||
|
||||
if (queueBlock) sections.push(queueBlock);
|
||||
|
||||
// For "custom" intent the body IS customInstructions — don't append it again
|
||||
if (request.intent !== "custom" && request.customInstructions?.trim()) {
|
||||
sections.push(`Additional instructions:\n${request.customInstructions.trim()}`);
|
||||
|
||||
Reference in new issue
Block a user