Skip to content

Commit b18b235

Browse files
Merge pull request #344 from CrewForm/fix/webhook-dispatcher-db-fallback
Fix/webhook dispatcher db fallback
2 parents 1b6bb6b + fcd44a4 commit b18b235

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

task-runner/src/collaborationExecutor.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ export async function processCollaborationRun(run: TeamRun): Promise<void> {
2828
let totalTokens = 0;
2929
let totalCost = 0;
3030

31+
let teamData: { name: string; config: CollaborationConfig; mode: string; output_route_ids: string[] | null } | null = null;
32+
3133
try {
3234
// 1. Fetch team config
3335
const teamResponse = await supabase
3436
.from('teams')
35-
.select('config, mode')
37+
.select('name, config, mode, output_route_ids')
3638
.eq('id', run.team_id)
3739
.single();
3840

3941
if (teamResponse.error || !teamResponse.data) {
4042
throw new Error(`Failed to load team: ${teamResponse.error?.message ?? 'not found'}`);
4143
}
4244

43-
const teamData = teamResponse.data as { config: CollaborationConfig; mode: string };
45+
teamData = teamResponse.data as { name: string; config: CollaborationConfig; mode: string; output_route_ids: string[] | null };
4446
const config = teamData.config;
4547

4648
if (!config.agent_ids?.length || config.agent_ids.length < 2) {
@@ -193,8 +195,9 @@ export async function processCollaborationRun(run: TeamRun): Promise<void> {
193195
// Fire webhook (fire-and-forget)
194196
void dispatchTeamRunWebhooks(
195197
{ id: run.id, team_id: run.team_id, workspace_id: run.workspace_id, status: 'completed', input_task: run.input_task, output: finalOutput },
196-
`Collaboration Team ${run.team_id}`,
198+
teamData.name,
197199
'team_run.completed',
200+
teamData.output_route_ids,
198201
);
199202

200203
} catch (error: unknown) {
@@ -214,8 +217,9 @@ export async function processCollaborationRun(run: TeamRun): Promise<void> {
214217

215218
void dispatchTeamRunWebhooks(
216219
{ id: run.id, team_id: run.team_id, workspace_id: run.workspace_id, status: 'failed', input_task: run.input_task, error_message: errMsg },
217-
`Collaboration Team ${run.team_id}`,
220+
teamData?.name ?? `Collaboration Team ${run.team_id}`,
218221
'team_run.failed',
222+
teamData?.output_route_ids ?? null,
219223
);
220224
}
221225
}

task-runner/src/orchestratorExecutor.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,44 @@ async function executeToolCall(
542542
}
543543

544544
case 'final_answer': {
545-
const output = args.output as string;
546-
console.log(`[Orchestrator] final_answer received, output length: ${output?.length ?? 0} chars`);
545+
let output = (args.output as string) ?? '';
546+
console.log(`[Orchestrator] final_answer received, brain output length: ${output.length} chars`);
547+
548+
// ── Augment with worker outputs if the brain was lazy ────────
549+
// LLMs often call final_answer with a brief summary like
550+
// "Task completed successfully" instead of including the full
551+
// synthesized content. If the final answer is much shorter than
552+
// the accumulated worker outputs, append them.
553+
const completedDelegations = Array.from(delegations.values())
554+
.filter((d) => d.status === 'completed' && d.worker_output);
555+
556+
if (completedDelegations.length > 0) {
557+
const workerOutputsTotal = completedDelegations
558+
.reduce((sum, d) => sum + (d.worker_output?.length ?? 0), 0);
559+
560+
// If brain's final answer is less than 20% of workers' combined output,
561+
// the brain likely just wrote a summary — augment with full worker outputs
562+
if (output.length < workerOutputsTotal * 0.2) {
563+
console.log(`[Orchestrator] Brain output (${output.length} chars) << worker outputs (${workerOutputsTotal} chars) — augmenting`);
564+
565+
const workerSections = completedDelegations.map((d) => {
566+
const worker = workers.find((w) => w.id === d.worker_agent_id);
567+
const workerName = worker?.name ?? 'Worker';
568+
return `## ${workerName}\n\n${d.worker_output}`;
569+
});
570+
571+
output = [
572+
output,
573+
'',
574+
'---',
575+
'',
576+
...workerSections,
577+
].join('\n');
578+
579+
console.log(`[Orchestrator] Augmented final output: ${output.length} chars`);
580+
}
581+
}
582+
547583
await finalizeRun(run.id, output, 0, 0); // tokens/cost already tracked
548584
return { result: output, tokensUsed: 0, costUsed: 0, isDone: true };
549585
}

0 commit comments

Comments
 (0)