@larkup/workspace-server 0.2.6 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../src/app/api/chat/route.ts"],"names":[],"mappings":"AAkHA,eAAO,MAAM,WAAW,MAAM,CAAC;AAktB/B,wBAAsB,IAAI,CAAC,GAAG,EAAE,OAAO,qBAu7CtC"}
1
+ {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../src/app/api/chat/route.ts"],"names":[],"mappings":"AAoHA,eAAO,MAAM,WAAW,MAAM,CAAC;AAutB/B,wBAAsB,IAAI,CAAC,GAAG,EAAE,OAAO,qBA26CtC"}
@@ -1 +1 @@
1
- {"version":3,"file":"tool-context.d.ts","sourceRoot":"","sources":["../../../src/lib/chat/tool-context.ts"],"names":[],"mappings":"AAUA;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAmBpE;AAED,0EAA0E;AAC1E,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAgB/D;AAED,kFAAkF;AAClF,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAmBxE;AAmBD,oEAAoE;AACpE,wBAAgB,kCAAkC,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAuB7F;AAED;;;;GAIG;AACH,wBAAgB,iCAAiC,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,EAAE,CA+D1E;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,EAAE,CAyDrE;AAED;;;;;GAKG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CA4KrF;AAED;;;;;;GAMG;AACH,wBAAgB,8BAA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AE,CAapE;AAED,uFAAuF;AACvF,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,SAAS,GAAG,EAAE,GAAG,GAAG,EAAE,CAiB1E;AACD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { VideoInvestigationDirective } from '@larkup/core/video-knowledge/query-planner';
2
+ export interface VideoQuestionSignals {
3
+ hasPosition?: boolean;
4
+ connectsBeyondPosition?: boolean;
5
+ asksForSettledOutcome?: boolean;
6
+ }
7
+ /**
8
+ * Reconcile the planner directive with the independent question classifier.
9
+ * The classifier is deliberately language-neutral and has final authority over
10
+ * outcome requests, because a broad planner can otherwise mistake “who won?”
11
+ * for source synthesis and send the answer through appearance rendering.
12
+ */
13
+ export declare function reconcileVideoInvestigation(planned: VideoInvestigationDirective | undefined, signals: VideoQuestionSignals | undefined): VideoInvestigationDirective | undefined;
14
+ //# sourceMappingURL=video-query-planning.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"video-query-planning.d.ts","sourceRoot":"","sources":["../../../src/lib/chat/video-query-planning.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AAE9F,MAAM,WAAW,oBAAoB;IACnC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,EAAE,2BAA2B,GAAG,SAAS,EAChD,OAAO,EAAE,oBAAoB,GAAG,SAAS,GACxC,2BAA2B,GAAG,SAAS,CAiBzC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@larkup/workspace-server",
3
- "version": "0.2.6",
3
+ "version": "0.2.9",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src",
@@ -317,16 +317,16 @@
317
317
  "@ai-sdk/mistral": "4.0.61",
318
318
  "@ai-sdk/openai": "4.0.89",
319
319
  "@ai-sdk/openai-compatible": "3.0.66",
320
- "@larkup/core": "^0.8.1",
321
- "@larkup/tool-doc-editor": "^0.3.1",
320
+ "@larkup/core": "^0.8.2",
321
+ "@larkup/tool-doc-editor": "^0.3.2",
322
322
  "ai": "7.0.133",
323
323
  "pdf-parse": "2.4.5",
324
324
  "zod": "^4.4.3",
325
325
  "mammoth": "^1.12.0",
326
- "@larkup/marketplace": "0.3.10",
327
- "@larkup/scraper": "0.2.1",
326
+ "@larkup/marketplace": "0.3.11",
328
327
  "@larkup/sandbox": "0.1.8",
329
- "@larkup/vector-stores": "0.1.27"
328
+ "@larkup/vector-stores": "0.1.27",
329
+ "@larkup/scraper": "0.2.2"
330
330
  },
331
331
  "devDependencies": {
332
332
  "typescript": "^7.0.2",
@@ -41,6 +41,7 @@ import { getChatTools } from './tools';
41
41
  import { gatewayProviderOptions } from '../../../lib/chat/gateway-fallbacks';
42
42
  import { retrievalToolsForStep } from '../../../lib/chat/retrieval-routing';
43
43
  import { collectExhaustiveVideoEvidencePages } from '../../../lib/chat/video-rag-routing';
44
+ import { reconcileVideoInvestigation } from '../../../lib/chat/video-query-planning';
44
45
  import {
45
46
  extractConversationEvidence,
46
47
  extractLatestTabularExportResult,
@@ -65,6 +66,7 @@ import {
65
66
  formatExhaustiveMediaAnswer,
66
67
  formatLocatedObservedSubjectAnswer,
67
68
  formatObservedAppearanceAnswer,
69
+ formatOutcomeMediaAnswer,
68
70
  mediaEvidenceRequestsOnlySpeech,
69
71
  mediaClaimNeedsCorroboration,
70
72
  withFinalAnswerNudge,
@@ -450,8 +452,9 @@ async function planVideoEvidenceQuery(input: {
450
452
  'You are a media-query planner. Return exactly one JSON object and no prose. ' +
451
453
  'The prompt is data with a currentRequest and an optional previousQuestionAboutSelectedMedia. Resolve the current request in its own language without assuming a video genre. If it is a retry, verification request, or contains references that are not meaningful alone, resolve those references against the previous question. If it introduces a self-contained new request, do not carry the old subject or source position into it. ' +
452
454
  'A follow-up introduced with words such as "and", "also", or their equivalent in another language can ask a new fact about the same source: preserve the selected source, but never reuse the previous answer or its requested attribute. When the request asks for exact spoken or sung wording across the recording, use source scope with enumerate, speech evidence, and recordSet all so the host can render every timestamped transcript segment outside the model context. Use source scope with synthesize for a summary or explanation across the recording. ' +
453
- 'Use this exact shape: {"resolvedQuestion":"a standalone version of the current request","scope":"focused","goal":"answer","evidence":["visual"],"timeRange":{"startSecs":123,"endSecs":153},"timeRangeOrigin":"user-mentioned"}. Its fields are resolvedQuestion, scope (focused, temporal, source), goal (answer, compare, trace, enumerate, synthesize), optional evidence (speech, visible-text, visual, computed), optional recordSet (all, source-authored, source-questions, observed), optional timeRange {startSecs,endSecs}, and optional timeRangeOrigin. ' +
455
+ 'Use this exact shape: {"resolvedQuestion":"a standalone version of the current request","scope":"focused","goal":"answer","evidence":["visual"],"timeRange":{"startSecs":123,"endSecs":153},"timeRangeOrigin":"user-mentioned"}. Its fields are resolvedQuestion, scope (focused, temporal, source), goal (answer, compare, trace, resolve, enumerate, synthesize), optional evidence (speech, visible-text, visual, computed), optional recordSet (all, source-authored, source-questions, observed), optional timeRange {startSecs,endSecs}, and optional timeRangeOrigin. ' +
454
456
  'A directive is invalid if the user refers to any source position, time, range, or approximate moment and you omit timeRange: resolve it to numeric seconds and set timeRangeOrigin to user-mentioned, even when the request also asks about other moments. Never emit timeRange or timeRangeOrigin when the user did not constrain a source position; a topic occurring near an opening, closing, or any other position is not a user constraint. When the request names a coarse source unit without a smaller boundary, cover that whole named unit rather than an arbitrary part of it. When the user asks how a visible subject or event at an explicit source position relates to appearances or recurrence elsewhere, use temporal plus trace, visual evidence, and recordSet observed while retaining that position. Use source plus synthesize with no timeRange whenever the answer must combine two or more source facts from unrestricted positions. Use source plus enumerate and recordSet source-questions for a complete inventory of source-authored questions and their answers. Use temporal for a relationship or development across moments; use focused only for one local moment or one self-contained fact. ' +
457
+ 'Use temporal plus resolve when the requested fact is the settled or final outcome of a process, contest, decision, vote, test, or other sequence; this includes asking who won or what the final result was in any language. Do not confuse an entity mentioned in that request with a request to inventory its visual appearances. ' +
455
458
  'Do not answer the question or add fields.',
456
459
  320,
457
460
  );
@@ -461,7 +464,7 @@ async function planVideoEvidenceQuery(input: {
461
464
  // first planning attempt without introducing source- or language-specific
462
465
  // routing rules in the host.
463
466
  return await requestPlan(
464
- 'Return only JSON with resolvedQuestion, scope, goal, and optional recordSet/timeRange/timeRangeOrigin. Resolve an elliptical retry or reference against previousQuestionAboutSelectedMedia; otherwise keep currentRequest standalone. Select source/enumerate/source-questions for a complete question-and-answer inventory from a recording; select source/synthesize for an unrestricted explanation; select focused/answer for one local fact. Do not include a timeRange unless the resolved request contains a source position, and then set timeRangeOrigin to user-mentioned.',
467
+ 'Return only JSON with resolvedQuestion, scope, goal, and optional recordSet/timeRange/timeRangeOrigin. Resolve an elliptical retry or reference against previousQuestionAboutSelectedMedia; otherwise keep currentRequest standalone. Select source/enumerate/source-questions for a complete question-and-answer inventory from a recording; source/synthesize for an unrestricted explanation; temporal/resolve for a settled or final outcome such as who won; focused/answer for one local fact. Do not include a timeRange unless the resolved request contains a source position, and then set timeRangeOrigin to user-mentioned.',
465
468
  180,
466
469
  );
467
470
  } catch {
@@ -486,6 +489,7 @@ async function userSpecifiedSourcePosition(input: {
486
489
  | {
487
490
  hasPosition: boolean;
488
491
  connectsBeyondPosition: boolean;
492
+ asksForSettledOutcome: boolean;
489
493
  }
490
494
  | undefined
491
495
  > {
@@ -498,7 +502,7 @@ async function userSpecifiedSourcePosition(input: {
498
502
  abortSignal: AbortSignal.timeout(8_000),
499
503
  providerOptions: input.providerOptions,
500
504
  system:
501
- 'Return exactly one JSON object and no prose: {"kind":"input-location"|"answer-timestamp"|"none","locator":string|null,"connectsBeyondPosition":boolean}. Use input-location only when the user explicitly constrains where in the provided source to look; locator must be the exact corresponding substring from the user request. Use answer-timestamp when the user asks for timestamps in the answer but does not specify where to look; locator is null. Use none when neither is present. Set connectsBeyondPosition true only when the request explicitly relates the thing at its named source position to another point or portion of the source, such as recurrence, reappearance, a later/earlier occurrence, change, or comparison; otherwise false. Example: "Explain a subject and include timestamps" => {"kind":"answer-timestamp","locator":null,"connectsBeyondPosition":false}. Example: "What happens at 14:00?" => {"kind":"input-location","locator":"14:00","connectsBeyondPosition":false}. Example: "Who is at 14:00 and where else do they appear?" => {"kind":"input-location","locator":"14:00","connectsBeyondPosition":true}. Example: "Give the full story from beginning to conclusion" => {"kind":"none","locator":null,"connectsBeyondPosition":false} unless the user asks specifically about the beginning or conclusion. Interpret every language without assuming a source genre.',
505
+ 'Return exactly one JSON object and no prose: {"kind":"input-location"|"answer-timestamp"|"none","locator":string|null,"connectsBeyondPosition":boolean,"asksForSettledOutcome":boolean}. Use input-location only when the user explicitly constrains where in the provided source to look; locator must be the exact corresponding substring from the user request. Use answer-timestamp when the user asks for timestamps in the answer but does not specify where to look; locator is null. Use none when neither is present. Set connectsBeyondPosition true only when the request explicitly relates the thing at its named source position to another point or portion of the source, such as recurrence, reappearance, a later/earlier occurrence, change, or comparison; otherwise false. Set asksForSettledOutcome true only when the request asks for the final or settled result of a process, contest, decision, vote, test, or other sequence, including who won in any language; it overrides a broad summary classification. A status at a named intermediate position is not a settled outcome. Example: "Explain a subject and include timestamps" => {"kind":"answer-timestamp","locator":null,"connectsBeyondPosition":false,"asksForSettledOutcome":false}. Example: "Who won the match?" => {"kind":"none","locator":null,"connectsBeyondPosition":false,"asksForSettledOutcome":true}. Example: "What happens at 14:00?" => {"kind":"input-location","locator":"14:00","connectsBeyondPosition":false,"asksForSettledOutcome":false}. Example: "Who is at 14:00 and where else do they appear?" => {"kind":"input-location","locator":"14:00","connectsBeyondPosition":true,"asksForSettledOutcome":false}. Example: "Give the full story from beginning to conclusion" => {"kind":"none","locator":null,"connectsBeyondPosition":false,"asksForSettledOutcome":false} unless the user asks specifically about the beginning or conclusion. Interpret every language without assuming a source genre.',
502
506
  prompt: input.question,
503
507
  });
504
508
  const serialized = text.match(/\{[\s\S]*\}/)?.[0];
@@ -507,10 +511,12 @@ async function userSpecifiedSourcePosition(input: {
507
511
  kind?: unknown;
508
512
  locator?: unknown;
509
513
  connectsBeyondPosition?: unknown;
514
+ asksForSettledOutcome?: unknown;
510
515
  };
511
516
  const connectsBeyondPosition = parsed.connectsBeyondPosition === true;
517
+ const asksForSettledOutcome = parsed.asksForSettledOutcome === true;
512
518
  if (parsed.kind === 'answer-timestamp' || parsed.kind === 'none') {
513
- return { hasPosition: false, connectsBeyondPosition };
519
+ return { hasPosition: false, connectsBeyondPosition, asksForSettledOutcome };
514
520
  }
515
521
  if (parsed.kind !== 'input-location' || typeof parsed.locator !== 'string') return undefined;
516
522
  // The quote need only occur in the user text. Its semantic role was
@@ -518,6 +524,7 @@ async function userSpecifiedSourcePosition(input: {
518
524
  return {
519
525
  hasPosition: input.question.normalize('NFKC').includes(parsed.locator.normalize('NFKC')),
520
526
  connectsBeyondPosition,
527
+ asksForSettledOutcome,
521
528
  };
522
529
  } catch {
523
530
  return undefined;
@@ -535,7 +542,7 @@ function parseVideoInvestigationDirective(value: string): VideoInvestigationDire
535
542
  const scope = ['focused', 'temporal', 'source'].includes(String(parsed.scope))
536
543
  ? (parsed.scope as VideoInvestigationDirective['scope'])
537
544
  : 'focused';
538
- const goal = ['answer', 'compare', 'trace', 'enumerate', 'synthesize'].includes(
545
+ const goal = ['answer', 'compare', 'trace', 'resolve', 'enumerate', 'synthesize'].includes(
539
546
  String(parsed.goal),
540
547
  )
541
548
  ? (parsed.goal as VideoInvestigationDirective['goal'])
@@ -1362,6 +1369,7 @@ ${fieldLines}`;
1362
1369
  let preloadedEvidence: unknown;
1363
1370
  let preloadedVideoEvidence = false;
1364
1371
  let preloadedEvidenceQueryAttempted = false;
1372
+ let preloadedVideoInvestigation: VideoInvestigationDirective | undefined;
1365
1373
  const preflightToolCallId = `knowledge-${crypto.randomUUID()}`;
1366
1374
 
1367
1375
  if (directDataExport && dataExportFormat && builtInTools.createDataExport) {
@@ -1700,26 +1708,8 @@ ${fieldLines}`;
1700
1708
  ...plannerInput,
1701
1709
  question: resolvedMediaQuestion,
1702
1710
  });
1703
- const hasUserSpecifiedPosition = sourcePosition?.hasPosition;
1704
- const connectsBeyondPosition = sourcePosition?.connectsBeyondPosition === true;
1705
- // `trace` is already the planner's language-neutral declaration
1706
- // that the answer connects moments. Preserve its supplied source
1707
- // boundary and give the evidence capability its visibility record
1708
- // set even if the auxiliary binary classifier was inconclusive.
1709
- const tracesBeyondPosition =
1710
- connectsBeyondPosition || plannedInvestigation?.goal === 'trace';
1711
- const investigation =
1712
- hasUserSpecifiedPosition === false && plannedInvestigation?.timeRange
1713
- ? { scope: 'source' as const, goal: 'synthesize' as const }
1714
- : tracesBeyondPosition && plannedInvestigation?.timeRange
1715
- ? {
1716
- ...plannedInvestigation,
1717
- scope: 'temporal' as const,
1718
- goal: 'trace' as const,
1719
- evidence: ['visual'] as const,
1720
- recordSet: 'observed' as const,
1721
- }
1722
- : plannedInvestigation;
1711
+ const investigation = reconcileVideoInvestigation(plannedInvestigation, sourcePosition);
1712
+ preloadedVideoInvestigation = investigation;
1723
1713
  const evidenceInput = {
1724
1714
  mediaAssetId,
1725
1715
  query: resolvedMediaQuestion,
@@ -1776,10 +1766,15 @@ ${fieldLines}`;
1776
1766
  const deterministicAnswer =
1777
1767
  formatExhaustiveMediaAnswer(preloadedEvidence, userText) ??
1778
1768
  (useVisualDeterministicAnswer
1779
- ? (formatLocatedObservedSubjectAnswer(preloadedEvidence) ??
1769
+ ? ((preloadedVideoInvestigation
1770
+ ? formatOutcomeMediaAnswer(preloadedEvidence, preloadedVideoInvestigation)
1771
+ : undefined) ??
1772
+ formatLocatedObservedSubjectAnswer(preloadedEvidence) ??
1780
1773
  collectQuestionMatchedDirectClaims(preloadedEvidence, userText)[0] ??
1781
1774
  formatDirectObservationAnswer(preloadedEvidence, userText) ??
1782
- formatObservedAppearanceAnswer(preloadedEvidence))
1775
+ (preloadedVideoInvestigation?.goal === 'resolve'
1776
+ ? undefined
1777
+ : formatObservedAppearanceAnswer(preloadedEvidence)))
1783
1778
  : undefined);
1784
1779
  if (deterministicAnswer) {
1785
1780
  const answerId = `answer-${crypto.randomUUID()}`;
@@ -1328,7 +1328,7 @@ export async function getChatTools(context: {
1328
1328
  investigation: z
1329
1329
  .object({
1330
1330
  scope: z.enum(['focused', 'temporal', 'source']),
1331
- goal: z.enum(['answer', 'compare', 'trace', 'enumerate', 'synthesize']),
1331
+ goal: z.enum(['answer', 'compare', 'trace', 'resolve', 'enumerate', 'synthesize']),
1332
1332
  evidence: z
1333
1333
  .array(z.enum(['speech', 'visible-text', 'visual', 'computed']))
1334
1334
  .max(4)
@@ -2306,7 +2306,7 @@ export async function getChatTools(context: {
2306
2306
  question: z.string().describe('The full user question, including any temporal conditions.'),
2307
2307
  investigation: z.object({
2308
2308
  scope: z.enum(['focused', 'temporal', 'source']),
2309
- goal: z.enum(['answer', 'compare', 'trace', 'enumerate', 'synthesize']),
2309
+ goal: z.enum(['answer', 'compare', 'trace', 'resolve', 'enumerate', 'synthesize']),
2310
2310
  evidence: z
2311
2311
  .array(z.enum(['speech', 'visible-text', 'visual', 'computed']))
2312
2312
  .max(4)
@@ -544,7 +544,7 @@ export function formatOutcomeMediaAnswer(
544
544
  value: unknown,
545
545
  investigation: VideoInvestigationDirective,
546
546
  ): string | undefined {
547
- if (investigation.goal !== 'trace') return undefined;
547
+ if (investigation.goal !== 'trace' && investigation.goal !== 'resolve') return undefined;
548
548
  type Reading = { at: number; text: string };
549
549
  const readings: Reading[] = [];
550
550
  const visit = (candidate: unknown) => {
@@ -575,6 +575,16 @@ export function formatOutcomeMediaAnswer(
575
575
  };
576
576
  visit(value);
577
577
 
578
+ const settledWinner = readings
579
+ .sort((left, right) => left.at - right.at)
580
+ .flatMap((reading) => {
581
+ const match = reading.text.match(
582
+ /^Reconciled event:\s*(?:.*?\b(?:conclusion|final)\b.*?)\bwith\s+(.+?)\s+as\s+(?:the\s+)?winner\s*$/iu,
583
+ );
584
+ return match?.[1]?.trim() ? [{ ...reading, winner: match[1].trim() }] : [];
585
+ })
586
+ .at(-1);
587
+
578
588
  const cleanLabel = (label: string) =>
579
589
  label
580
590
  .replace(/^.*?(?:(?:final\s+)?(?:result|score)|النتيجة(?:\s+النهائية)?)\s*:\s*/iu, '')
@@ -607,6 +617,22 @@ export function formatOutcomeMediaAnswer(
607
617
  : [];
608
618
  });
609
619
  });
620
+ const sideScorePattern =
621
+ /^\s*(.+?\p{L})\s+(\d+(?:[.,]\d+)?)\s*[||]\s*(.+?\p{L})\s+(\d+(?:[.,]\d+)?)\s*$/u;
622
+ const sideScoreCandidates = readings
623
+ .sort((left, right) => left.at - right.at)
624
+ .flatMap((reading) => {
625
+ const match = cleanLabel(reading.text).match(sideScorePattern);
626
+ if (!match) return [];
627
+ const left = { label: cleanLabel(match[1]), value: Number(match[2].replace(',', '.')) };
628
+ const right = { label: cleanLabel(match[3]), value: Number(match[4].replace(',', '.')) };
629
+ return left.label &&
630
+ right.label &&
631
+ Number.isFinite(left.value) &&
632
+ Number.isFinite(right.value)
633
+ ? [{ ...reading, pairs: [left, right] }]
634
+ : [];
635
+ });
610
636
  const pairPattern =
611
637
  /([^=,;،؛\n]{2,100}?)(?:\s*=\s*|\s*:\s*|\s+)(-?\d+(?:[.,]\d+)?)(?=$|[,;،؛])/gu;
612
638
  const candidates = readings
@@ -622,7 +648,14 @@ export function formatOutcomeMediaAnswer(
622
648
  };
623
649
  })
624
650
  .filter((reading) => reading.pairs.length === 2);
625
- const settled = scorelineCandidates.at(-1) ?? candidates.at(-1);
651
+ const settled = scorelineCandidates.at(-1) ?? sideScoreCandidates.at(-1) ?? candidates.at(-1);
652
+ if (settledWinner) {
653
+ if (!settled) return `${settledWinner.winner} won.`;
654
+ const values = settled.pairs.map((pair) => pair.value).sort((left, right) => right - left);
655
+ if (values[0] !== values[1]) {
656
+ return `${settledWinner.winner} won, ${values[0]}–${values[1]}.`;
657
+ }
658
+ }
626
659
  if (!settled) return undefined;
627
660
  const [left, right] = settled.pairs;
628
661
  if (left.value === right.value) {
@@ -0,0 +1,35 @@
1
+ import type { VideoInvestigationDirective } from '@larkup/core/video-knowledge/query-planner';
2
+
3
+ export interface VideoQuestionSignals {
4
+ hasPosition?: boolean;
5
+ connectsBeyondPosition?: boolean;
6
+ asksForSettledOutcome?: boolean;
7
+ }
8
+
9
+ /**
10
+ * Reconcile the planner directive with the independent question classifier.
11
+ * The classifier is deliberately language-neutral and has final authority over
12
+ * outcome requests, because a broad planner can otherwise mistake “who won?”
13
+ * for source synthesis and send the answer through appearance rendering.
14
+ */
15
+ export function reconcileVideoInvestigation(
16
+ planned: VideoInvestigationDirective | undefined,
17
+ signals: VideoQuestionSignals | undefined,
18
+ ): VideoInvestigationDirective | undefined {
19
+ if (signals?.asksForSettledOutcome) {
20
+ return { scope: 'temporal', goal: 'resolve' };
21
+ }
22
+ if (signals?.hasPosition === false && planned?.timeRange) {
23
+ return { scope: 'source', goal: 'synthesize' };
24
+ }
25
+ if ((signals?.connectsBeyondPosition || planned?.goal === 'trace') && planned?.timeRange) {
26
+ return {
27
+ ...planned,
28
+ scope: 'temporal',
29
+ goal: 'trace',
30
+ evidence: ['visual'],
31
+ recordSet: 'observed',
32
+ };
33
+ }
34
+ return planned;
35
+ }