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
22 changes: 16 additions & 6 deletions open-sse/executors/devin-agentic/toolParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash } from "node:crypto";
import { asRecord, DevinAgenticBridgeError, type AnthropicTool, type JsonRecord } from "./types.ts";
import { findTagBlocks } from "../../utils/tagBlocks.ts";

function stableJson(value: unknown): string {
if (Array.isArray(value)) return `[${value.map((item) => stableJson(item)).join(",")}]`;
Expand Down Expand Up @@ -66,7 +67,8 @@ function validateSchema(value: unknown, schema: JsonRecord, path: string): strin
return errors;
}

const BARE_SUMMARY_ENVELOPE_RE = /^<summary>\s*([\s\S]*?)\s*<\/summary>$/i;
const SUMMARY_OPEN = "<summary>";
const SUMMARY_CLOSE = "</summary>";

/**
* Detects a whole-response Devin "summarizer" progress report: an internal
Expand All @@ -77,12 +79,20 @@ const BARE_SUMMARY_ENVELOPE_RE = /^<summary>\s*([\s\S]*?)\s*<\/summary>$/i;
*/
export function extractBareSummaryEnvelope(text: string): string | null {
const trimmed = text.trim();
const match = trimmed.match(BARE_SUMMARY_ENVELOPE_RE);
return match ? match[1].trim() : null;
// Anchored prefix/suffix checks instead of one `^<summary>\s*([\s\S]*?)\s*<\/summary>$`
// pattern, whose overlapping whitespace classes are cubic on a long run of spaces.
if (trimmed.length < SUMMARY_OPEN.length + SUMMARY_CLOSE.length) return null;
if (!/^<summary>/i.test(trimmed) || !/<\/summary>$/i.test(trimmed)) return null;
return trimmed.slice(SUMMARY_OPEN.length, trimmed.length - SUMMARY_CLOSE.length).trim();
}

export function parseDevinToolRequest(text: string, tools: AnthropicTool[], idSeed = "") {
const matches = [...text.matchAll(/<tool>\s*([\s\S]*?)\s*<\/tool>/g)];
// `text` is model output; scan forward once instead of matching `<tool>\s*([\s\S]*?)\s*<\/tool>`,
// which is cubic on a long run of spaces after an unclosed tag.
const matches = findTagBlocks(text, /<tool>/g, /<\/tool>/g).map((block) => ({
whole: text.slice(block.start, block.end),
inner: block.inner.trim(),
}));
if (matches.length === 0) return null;
if (matches.length > 1) {
throw new DevinAgenticBridgeError(
Expand All @@ -91,7 +101,7 @@ export function parseDevinToolRequest(text: string, tools: AnthropicTool[], idSe
);
}

if (text.trim() !== matches[0][0].trim()) {
if (text.trim() !== matches[0].whole.trim()) {
throw new DevinAgenticBridgeError(
"Devin tool request must be a standalone tool envelope without narrative text",
"mixed_tool_narrative"
Expand All @@ -100,7 +110,7 @@ export function parseDevinToolRequest(text: string, tools: AnthropicTool[], idSe

let payload: JsonRecord;
try {
payload = asRecord(JSON.parse(matches[0][1] || "{}"));
payload = asRecord(JSON.parse(matches[0].inner || "{}"));
} catch {
throw new DevinAgenticBridgeError("Devin tool request was not valid JSON", "invalid_tool_json");
}
Expand Down
12 changes: 5 additions & 7 deletions open-sse/executors/promptql/eventTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
* embedded as `<final_response>…</final_response>` XML inside `response_text`).
*/

import { findTagBlocks } from "../../utils/tagBlocks.ts";

export function walkStrings(
node: unknown,
out: Array<{ path: string; text: string }> = [],
path = ""
): Array<{ path: string; text: string }> {
if (node == null) return out;
if (typeof node === "string") {
if (
node.length >= 1 &&
!/^[0-9a-f-]{36}$/i.test(node) &&
!/^\d{4}-\d{2}-\d{2}T/.test(node)
) {
if (node.length >= 1 && !/^[0-9a-f-]{36}$/i.test(node) && !/^\d{4}-\d{2}-\d{2}T/.test(node)) {
out.push({ path, text: node });
}
return out;
Expand All @@ -40,8 +38,8 @@ export function extractFinalResponseMessage(eventData: unknown): string | null {
// response_text XML fallback
const raw = walkStrings(eventData).find((t) => /response_text$/i.test(t.path));
if (raw) {
const m = raw.text.match(/<final_response>\s*([\s\S]*?)\s*<\/final_response>/i);
if (m) return m[1]!.trim();
const [block] = findTagBlocks(raw.text, /<final_response>/gi, /<\/final_response>/gi);
if (block) return block.inner.trim();
}
return null;
}
Expand Down
122 changes: 122 additions & 0 deletions tests/unit/devin-promptql-tag-scan-linear.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import test from "node:test";
import assert from "node:assert/strict";

import {
extractBareSummaryEnvelope,
parseDevinToolRequest,
} from "../../open-sse/executors/devin-agentic/toolParser.ts";
import { extractFinalResponseMessage } from "../../open-sse/executors/promptql/eventTree.ts";

const OLD_SUMMARY = /^<summary>\s*([\s\S]*?)\s*<\/summary>$/i;
const OLD_FINAL = /<final_response>\s*([\s\S]*?)\s*<\/final_response>/i;

// Small enough to finish with the old cubic regexes, large enough that they would take
// seconds if these paths still used them (2,000 spaces cost the old patterns about 3 s each).
const RUN = 20_000;
const BUDGET_MS = 500;

function timed<T>(fn: () => T): { value: T; ms: number } {
const started = performance.now();
const value = fn();
return { value, ms: performance.now() - started };
}

test("bare summary envelope: same result as the old regex on ordinary input", () => {
const samples = [
"<summary>hello</summary>",
" <SUMMARY>\n spaced out \n</SUMMARY> ",
"<summary></summary>",
"<summary> </summary>",
"<summary>a</summary> tail",
"head <summary>a</summary>",
"<summary>a</summary><summary>b</summary>",
"<summary>only open",
"</summary>",
"<summary></summary",
"no envelope",
];
for (const sample of samples) {
const old = sample.trim().match(OLD_SUMMARY);
assert.equal(extractBareSummaryEnvelope(sample), old ? old[1].trim() : null, sample);
}
});

test("bare summary envelope: a long run of spaces is scanned in linear time", () => {
const open = timed(() => extractBareSummaryEnvelope(`<summary>${" ".repeat(RUN)}x`));
assert.equal(open.value, null);
assert.ok(open.ms < BUDGET_MS, `unclosed took ${open.ms}ms`);
const closed = timed(() => extractBareSummaryEnvelope(`<summary>${" ".repeat(RUN)}x</summary>`));
assert.equal(closed.value, "x");
assert.ok(closed.ms < BUDGET_MS, `closed took ${closed.ms}ms`);
});

test("devin tool request: well-formed envelope still parses", () => {
const tools = [
{
name: "read_file",
description: "",
input_schema: {
type: "object",
properties: { path: { type: "string" } },
required: ["path"],
},
},
];
const call = parseDevinToolRequest(
`\n<tool>\n {"name":"read_file","arguments":{"path":"a.txt"}}\n</tool>\n`,
tools as never
) as { name?: string } | null;
assert.ok(call);
assert.equal(parseDevinToolRequest("no tool here", tools as never), null);
assert.throws(
() =>
parseDevinToolRequest(
`hi <tool>{"name":"read_file","arguments":{"path":"a"}}</tool>`,
tools as never
),
/standalone tool envelope/
);
assert.throws(
() => parseDevinToolRequest("<tool>{}</tool><tool>{}</tool>", tools as never),
/more than one tool request/
);
});

test("devin tool request: unclosed tags and long whitespace are scanned in linear time", () => {
for (const text of [
`<tool>${" ".repeat(RUN)}x`,
"<tool>".repeat(RUN),
`<tool>${"\n ".repeat(RUN)}`,
]) {
const { value, ms } = timed(() => parseDevinToolRequest(text, []));
assert.equal(value, null);
assert.ok(ms < BUDGET_MS, `took ${ms}ms`);
}
});

test("promptql final response: same result as the old regex on ordinary input", () => {
const samples = [
"<final_response>hello</final_response>",
"x <FINAL_RESPONSE>\n spaced \n</FINAL_RESPONSE> y",
"<final_response></final_response>",
"<final_response>a</final_response><final_response>b</final_response>",
"<final_response>unclosed",
"no tag",
];
for (const sample of samples) {
const old = sample.match(OLD_FINAL);
const got = extractFinalResponseMessage({ response_text: sample });
assert.equal(got, old ? old[1].trim() : null, sample);
}
});

test("promptql final response: unclosed tag and long whitespace are scanned in linear time", () => {
for (const text of [
`<final_response>${" ".repeat(RUN)}x`,
"<final_response>".repeat(RUN),
`<final_response>${" ".repeat(RUN)}x</final_response>`,
]) {
const { ms } = timed(() => extractFinalResponseMessage({ response_text: text }));
assert.ok(ms < BUDGET_MS, `took ${ms}ms`);
}
});
Loading