Skip to content
Closed
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
6 changes: 4 additions & 2 deletions src/core/cycle/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ async function collectChildPutPageSlugs(
childIds: number[],
): Promise<string[]> {
if (childIds.length === 0) return [];
// Handle both properly-stored jsonb objects (input->>'slug') and
// double-encoded jsonb strings from pre-fix data ((input #>> '{}')::jsonb->>'slug').
const rows = await engine.executeRaw<{ slug: string }>(
`SELECT DISTINCT input->>'slug' AS slug
`SELECT DISTINCT
COALESCE(input->>'slug', (input #>> '{}')::jsonb->>'slug') AS slug
FROM subagent_tool_executions
WHERE job_id = ANY($1::int[])
AND tool_name = 'brain_put_page'
AND status = 'complete'
AND input ? 'slug'
ORDER BY 1`,
[childIds],
);
Expand Down
6 changes: 4 additions & 2 deletions src/core/cycle/synthesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,15 @@ async function collectChildPutPageSlugs(
childIds: number[],
): Promise<string[]> {
if (childIds.length === 0) return [];
// Handle both properly-stored jsonb objects (input->>'slug') and
// double-encoded jsonb strings from pre-fix data ((input #>> '{}')::jsonb->>'slug').
const rows = await engine.executeRaw<{ slug: string }>(
`SELECT DISTINCT input->>'slug' AS slug
`SELECT DISTINCT
COALESCE(input->>'slug', (input #>> '{}')::jsonb->>'slug') AS slug
FROM subagent_tool_executions
WHERE job_id = ANY($1::int[])
AND tool_name = 'brain_put_page'
AND status = 'complete'
AND input ? 'slug'
ORDER BY 1`,
[childIds],
);
Expand Down
10 changes: 7 additions & 3 deletions src/core/minions/handlers/subagent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,15 @@ async function persistToolExecPending(
toolName: string,
input: unknown,
): Promise<void> {
// Serialize to JSON string for the ::jsonb cast. When `input` is already a
// string (e.g. pre-serialized), avoid double-encoding which produces a jsonb
// scalar string instead of a jsonb object — breaking `input->>'key'` lookups.
const jsonStr = typeof input === 'string' ? input : JSON.stringify(input);
await engine.executeRaw(
`INSERT INTO subagent_tool_executions (job_id, message_idx, tool_use_id, tool_name, input, status)
VALUES ($1, $2, $3, $4, $5::jsonb, 'pending')
ON CONFLICT (job_id, tool_use_id) DO NOTHING`,
[jobId, messageIdx, toolUseId, toolName, JSON.stringify(input)],
[jobId, messageIdx, toolUseId, toolName, jsonStr],
);
}

Expand All @@ -638,7 +642,7 @@ async function persistToolExecComplete(
`UPDATE subagent_tool_executions
SET status = 'complete', output = $3::jsonb, ended_at = now()
WHERE job_id = $1 AND tool_use_id = $2`,
[jobId, toolUseId, JSON.stringify(output)],
[jobId, toolUseId, typeof output === 'string' ? output : JSON.stringify(output)],
);
}

Expand All @@ -658,7 +662,7 @@ async function persistToolExecFailed(
VALUES ($1, $2, $3, $4, $5::jsonb, 'failed', $6, now())
ON CONFLICT (job_id, tool_use_id) DO UPDATE
SET status = 'failed', error = EXCLUDED.error, ended_at = now()`,
[jobId, messageIdx, toolUseId, toolName, JSON.stringify(input), error],
[jobId, messageIdx, toolUseId, toolName, typeof input === 'string' ? input : JSON.stringify(input), error],
);
}

Expand Down