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
35 changes: 33 additions & 2 deletions runners/extension/agentContext.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { callLlm } from "./llm.js";
import { collectFrames } from "./frameDiscovery.js";
import { state } from "./state.js";
import { dbg } from "./debugLog.js";

/**
* Merge homepage agent context with optional Advanced "business use case" text.
Expand All @@ -12,15 +13,40 @@ export function mergeBusinessContext(agentContext, extraBusinessUseCase) {
return a || b || "";
}

const CONTEXT_TIMEOUT_MS = 45_000;

/**
* Use the reader LLM to infer what the on-page chat agent is from DOM snapshots.
*/
export async function summarizeAgentFromTab(tabId, readerCfg, siteUrl = "") {
dbg("context", "summarizeAgentFromTab called", { tabId, siteUrl: String(siteUrl).slice(0, 120) });

return Promise.race([
_summarizeAgentFromTabInner(tabId, readerCfg, siteUrl),
new Promise((_, reject) =>
setTimeout(
() =>
reject(
new Error(`Agent context detection timed out after ${CONTEXT_TIMEOUT_MS / 1000}s`)
),
CONTEXT_TIMEOUT_MS
)
),
]);
Comment on lines +16 to +35

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Abort the LLM request when the context timeout expires.

Promise.race returns the timeout error, but _summarizeAgentFromTabInner continues its callLlm request. The request can consume provider capacity and cost after the run has moved to manual input or failed.

Create a timeout-owned AbortController, pass its signal to callLlm, abort it when the timeout expires, and clear the timer. Preserve user-stop cancellation.

🧰 Tools
🪛 ast-grep (0.45.0)

[warning] 26-32: Avoid using the initial state variable in setState
Context: setTimeout(
() =>
reject(
new Error(Agent context detection timed out after ${CONTEXT_TIMEOUT_MS / 1000}s)
),
CONTEXT_TIMEOUT_MS
)
Note: [CWE-710] Improper Adherence to Coding Standards. Security best practice.

(setstate-same-var)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@runners/extension/agentContext.js` around lines 16 - 35, Update
summarizeAgentFromTab and _summarizeAgentFromTabInner so the timeout-owned
AbortController signal is passed through to callLlm, aborting the request when
CONTEXT_TIMEOUT_MS expires. Replace Promise.race with explicit timer cleanup on
success, timeout, or error, while preserving any existing user-stop cancellation
behavior.

}

async function _summarizeAgentFromTabInner(tabId, readerCfg, siteUrl) {
let frames = [];
try {
frames = await collectFrames(tabId);
} catch {
// proceed with empty frames; prompt will note missing snapshot
dbg("context", "Collected frames for agent summary", {
count: frames.length,
chatFrames: frames.filter((f) => (f.chatScore || 0) > 0).length,
});
} catch (e) {
dbg("context", "Frame collection failed — proceeding with empty frames", {
error: e instanceof Error ? e.message : String(e),
});
}

const top = frames.find((f) => f.frameId === 0);
Expand Down Expand Up @@ -78,8 +104,13 @@ export async function summarizeAgentFromTab(tabId, readerCfg, siteUrl = "") {

const summary = typeof out?.summary === "string" ? out.summary.trim() : "";
if (!summary) {
dbg("context", "Agent summary LLM returned empty summary");
throw new Error("Agent summary LLM returned empty summary");
}
dbg("context", "Agent summary generated", {
summaryLen: summary.length,
preview: summary.slice(0, 300),
});
return summary;
}

Expand Down
Loading
Loading