literally-aeo 0.2.2 → 0.3.0
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.
- package/dist/cli.mjs +6031 -5158
- package/dist/lib/agents/author-schema.js +5 -5
- package/dist/lib/agents/author-validation.js +7 -7
- package/dist/lib/agents/copy-validation.js +9 -9
- package/dist/lib/agents/copywriter-input.js +8 -8
- package/dist/lib/agents/facts.js +7 -7
- package/dist/lib/agents/narrative-facts.js +6 -6
- package/dist/lib/agents/verifier.js +13 -12
- package/dist/lib/analysis/domain-classifier.js +12 -10
- package/dist/lib/analysis/domain-labels.js +4 -4
- package/dist/lib/analysis/outreach.js +2 -2
- package/dist/lib/analysis/progress.js +8 -8
- package/dist/lib/analysis/retrieval.js +5 -5
- package/dist/lib/analysis/scoring.js +5 -5
- package/dist/lib/analysis/search-analyzer.js +6 -6
- package/dist/lib/analysis/sentiment.js +12 -10
- package/dist/lib/analysis/source-mix.js +2 -2
- package/dist/lib/author/examples.js +6 -6
- package/dist/lib/bundle/contracts.js +6 -6
- package/dist/lib/bundle/export.js +18 -18
- package/dist/lib/bundle/journal.js +7 -7
- package/dist/lib/bundle/project-journal.js +10 -10
- package/dist/lib/bundle/project.js +16 -16
- package/dist/lib/bundle/read.js +19 -19
- package/dist/lib/bundle/recorder.js +8 -8
- package/dist/lib/bundle/redact.js +7 -7
- package/dist/lib/bundle/trace-coverage.js +7 -7
- package/dist/lib/bundle/verify-integrity.js +9 -9
- package/dist/lib/bundle/verify-measurement.js +22 -13
- package/dist/lib/bundle/verify-report.js +23 -15
- package/dist/lib/bundle/verify.js +31 -23
- package/dist/lib/capture-manifest.js +6 -6
- package/dist/lib/checks/crawler-access.js +2 -2
- package/dist/lib/checks/renderability.js +2 -2
- package/dist/lib/checks/run-checks.js +12 -12
- package/dist/lib/chunks/{chunk-3CMPR6ZZ.js → chunk-2C723CW7.js} +1 -1
- package/dist/lib/chunks/chunk-2FDSOZ6Q.js +287 -0
- package/dist/lib/chunks/{chunk-PWOYEIPF.js → chunk-2U5QSA6Q.js} +7 -7
- package/dist/lib/chunks/{chunk-6EI4CVRW.js → chunk-3ESH5NDR.js} +5 -1
- package/dist/lib/chunks/{chunk-VHKHYAC5.js → chunk-3S2FAVRE.js} +2 -2
- package/dist/lib/chunks/{chunk-LR27H2HZ.js → chunk-44NSZBCP.js} +6 -6
- package/dist/lib/chunks/{chunk-JBOOD6MU.js → chunk-4AWFF53U.js} +12 -4
- package/dist/lib/chunks/{chunk-MENTBQGB.js → chunk-4HPMLJKC.js} +4 -4
- package/dist/lib/chunks/{chunk-S6QOXJAL.js → chunk-4Q2WFZ5J.js} +11 -4
- package/dist/lib/chunks/{chunk-7DNLRXEY.js → chunk-5JU2Y2I6.js} +2 -2
- package/dist/lib/chunks/{chunk-KFAT65P7.js → chunk-64JFN2Q5.js} +1 -1
- package/dist/lib/chunks/chunk-66UE4TT2.js +245 -0
- package/dist/lib/chunks/chunk-6N3C6E3D.js +105 -0
- package/dist/lib/chunks/{chunk-F2NSJ6PG.js → chunk-6RED3G6K.js} +7 -3
- package/dist/lib/chunks/{chunk-HCUJ2J7P.js → chunk-7KLTOVIY.js} +9 -9
- package/dist/lib/chunks/{chunk-EOO5RBUW.js → chunk-7ZR3WDF2.js} +38 -5
- package/dist/lib/chunks/chunk-A5PF7UE2.js +86 -0
- package/dist/lib/chunks/{chunk-PKNODC6Y.js → chunk-AB7DUTX5.js} +1 -1
- package/dist/lib/chunks/{chunk-WDT2Y7D7.js → chunk-AQCWKHTE.js} +11 -7
- package/dist/lib/chunks/{chunk-UNCUG2YS.js → chunk-ASHFEFCU.js} +3 -3
- package/dist/lib/chunks/{chunk-BYHXNFWN.js → chunk-ASIFUJG2.js} +1 -1
- package/dist/lib/chunks/{chunk-ZSEY7AHQ.js → chunk-BBK54RVK.js} +14 -11
- package/dist/lib/chunks/chunk-BQMUZLHO.js +17 -0
- package/dist/lib/chunks/{chunk-QD3JXA3Y.js → chunk-CQNIPZH3.js} +18 -18
- package/dist/lib/chunks/{chunk-7HM6IZ3L.js → chunk-CVEXMYPW.js} +2 -2
- package/dist/lib/chunks/chunk-CXZIHHLJ.js +95 -0
- package/dist/lib/chunks/{chunk-75DMLOQ7.js → chunk-CYJNBVQB.js} +1 -1
- package/dist/lib/chunks/{chunk-FAUFE7MI.js → chunk-D7N5OLYL.js} +1 -1
- package/dist/lib/chunks/{chunk-E2YTTB6Q.js → chunk-DSK3PM4Z.js} +1 -1
- package/dist/lib/chunks/{chunk-TNL2GWYR.js → chunk-EUXNCXCR.js} +22 -0
- package/dist/lib/chunks/{chunk-FJL2V6P5.js → chunk-F4EQULFR.js} +6 -6
- package/dist/lib/chunks/{chunk-QGZ4QSUE.js → chunk-FHV4PBYG.js} +1 -1
- package/dist/lib/chunks/{chunk-K2FZFESR.js → chunk-FONFW67K.js} +4 -4
- package/dist/lib/chunks/{chunk-PIFTG6YW.js → chunk-FPPHYGIO.js} +2 -2
- package/dist/lib/chunks/{chunk-VXYQWE45.js → chunk-FYE4R5TY.js} +2 -2
- package/dist/lib/chunks/{chunk-JYCVBFH5.js → chunk-H2DOBJZC.js} +2 -2
- package/dist/lib/chunks/{chunk-XA27KAUV.js → chunk-HEWHNSDP.js} +68 -36
- package/dist/lib/chunks/{chunk-RXSHKWZJ.js → chunk-HLC6OHGO.js} +2 -2
- package/dist/lib/chunks/{chunk-6HMHXSCS.js → chunk-HXXFRZTJ.js} +2 -2
- package/dist/lib/chunks/{chunk-R5UPOEWE.js → chunk-HYMWNGOK.js} +10 -3
- package/dist/lib/chunks/chunk-IH67TEGY.js +73 -0
- package/dist/lib/chunks/{chunk-JKPBXPCW.js → chunk-IMADI2YP.js} +1 -1
- package/dist/lib/chunks/{chunk-AT3XMGES.js → chunk-JBGQFPTY.js} +4 -4
- package/dist/lib/chunks/{chunk-JQDQVG3D.js → chunk-JDU3ZCKO.js} +1 -1
- package/dist/lib/chunks/{chunk-2KH7LNK7.js → chunk-JGIX427K.js} +7 -7
- package/dist/lib/chunks/{chunk-7CW6U5WJ.js → chunk-JIE2ANVD.js} +1 -1
- package/dist/lib/chunks/{chunk-YEUCAPKP.js → chunk-JJFOILZK.js} +2 -2
- package/dist/lib/chunks/{chunk-F7DBTXKG.js → chunk-JMIK2QN3.js} +4 -3
- package/dist/lib/chunks/chunk-JW7DTYSI.js +116 -0
- package/dist/lib/chunks/{chunk-BFWK3TBN.js → chunk-JXLCKACO.js} +2 -2
- package/dist/lib/chunks/{chunk-7WO3FFPS.js → chunk-K5NQDJLE.js} +1 -1
- package/dist/lib/chunks/{chunk-BXF4OC74.js → chunk-KI2FB4Y3.js} +30 -16
- package/dist/lib/chunks/{chunk-Y2CZ6FZY.js → chunk-KRWDELRS.js} +30 -7
- package/dist/lib/chunks/{chunk-SIJOQRRK.js → chunk-KRXOJJR7.js} +1 -1
- package/dist/lib/chunks/chunk-KSUI4DAY.js +15 -0
- package/dist/lib/chunks/chunk-KXFZ3CH3.js +98 -0
- package/dist/lib/chunks/{chunk-XSER7WEB.js → chunk-LJCWZ5SZ.js} +14 -14
- package/dist/lib/chunks/{chunk-HGGRPOSA.js → chunk-LNYQMWJO.js} +3 -3
- package/dist/lib/chunks/{chunk-DL6YQDTE.js → chunk-LOIAGEKN.js} +2 -2
- package/dist/lib/chunks/{chunk-I3UE52ZV.js → chunk-LPOIMPJK.js} +2 -2
- package/dist/lib/chunks/{chunk-66T7JC57.js → chunk-LUKP64NX.js} +2 -2
- package/dist/lib/chunks/chunk-MNG5TZRW.js +208 -0
- package/dist/lib/chunks/{chunk-HGGOGCPX.js → chunk-MPQXB7HN.js} +7 -7
- package/dist/lib/chunks/{chunk-YGLWQFKL.js → chunk-MZ2KN3IH.js} +3 -3
- package/dist/lib/chunks/chunk-N62G4EWT.js +91 -0
- package/dist/lib/chunks/{chunk-7YDXD4MD.js → chunk-ODIXZHHX.js} +1 -1
- package/dist/lib/chunks/{chunk-GLUGYZQO.js → chunk-OTVHGRCO.js} +8 -8
- package/dist/lib/chunks/{chunk-RIJERFGU.js → chunk-OVF27NAO.js} +69 -17
- package/dist/lib/chunks/chunk-P6LHGBYU.js +90 -0
- package/dist/lib/chunks/{chunk-U5USH2HB.js → chunk-PAL5SXIP.js} +1 -1
- package/dist/lib/chunks/{chunk-W5XYW6ZM.js → chunk-PT6PK2MJ.js} +26 -19
- package/dist/lib/chunks/{chunk-OD36HUXA.js → chunk-Q22FCF4A.js} +6 -7
- package/dist/lib/chunks/chunk-QKTKQSPS.js +292 -0
- package/dist/lib/chunks/{chunk-K4Y5NT4M.js → chunk-QO5TPMWS.js} +2 -2
- package/dist/lib/chunks/{chunk-4GLW3ZRO.js → chunk-QUZYFAGP.js} +4 -4
- package/dist/lib/chunks/{chunk-T6XZQOXS.js → chunk-QVMPTT7R.js} +5 -5
- package/dist/lib/chunks/{chunk-OOLWU667.js → chunk-RGZY6YUQ.js} +5 -5
- package/dist/lib/chunks/{chunk-U6BW6LGX.js → chunk-RM2VVR7Y.js} +1 -1
- package/dist/lib/chunks/{chunk-AJO7XVXK.js → chunk-RMSH7RUG.js} +6 -6
- package/dist/lib/chunks/{chunk-V5B6UZ3E.js → chunk-RQGEPR6V.js} +1 -0
- package/dist/lib/chunks/{chunk-V5JJEEBT.js → chunk-RYB77E7O.js} +6 -5
- package/dist/lib/chunks/chunk-S24JGY7B.js +97 -0
- package/dist/lib/chunks/{chunk-6GKKALCI.js → chunk-SF4UJ2AY.js} +1 -1
- package/dist/lib/chunks/chunk-SJCUR4QX.js +94 -0
- package/dist/lib/chunks/{chunk-XAN4CRFP.js → chunk-SQ55KGHO.js} +1 -1
- package/dist/lib/chunks/{chunk-ZOGIJGU3.js → chunk-TKOQMB6A.js} +7 -7
- package/dist/lib/chunks/{chunk-UYDQXXDQ.js → chunk-TWK6IA4P.js} +1 -1
- package/dist/lib/chunks/{chunk-DVFFYY5G.js → chunk-U42I3SBR.js} +13 -9
- package/dist/lib/chunks/{chunk-PIF3GL62.js → chunk-UECQONVZ.js} +5 -5
- package/dist/lib/chunks/{chunk-FDM2YNQL.js → chunk-VTY53IFN.js} +1 -1
- package/dist/lib/chunks/chunk-VYUC772A.js +338 -0
- package/dist/lib/chunks/{chunk-WCLKIPJV.js → chunk-WPCBE2NZ.js} +1 -1
- package/dist/lib/chunks/{chunk-3OE4UQIB.js → chunk-WRHGLRUK.js} +5 -5
- package/dist/lib/chunks/{chunk-J7QKT5E7.js → chunk-WUIY7BXR.js} +34 -17
- package/dist/lib/chunks/{chunk-7YYDUHJG.js → chunk-WZA4H4TG.js} +39 -13
- package/dist/lib/chunks/{chunk-NH4QGBJV.js → chunk-XBSA7MNJ.js} +7 -2
- package/dist/lib/chunks/chunk-XTBCXEH7.js +243 -0
- package/dist/lib/chunks/{chunk-ZQ3ER7JD.js → chunk-YPVJAXPP.js} +1 -1
- package/dist/lib/chunks/{chunk-SASE7U6E.js → chunk-YQD5ITKI.js} +1 -1
- package/dist/lib/chunks/{chunk-LTCDLURT.js → chunk-YY4BH5XM.js} +6 -6
- package/dist/lib/chunks/{chunk-J5NPHPXY.js → chunk-Z3BZRBQF.js} +36 -2
- package/dist/lib/chunks/{chunk-XFUYEFQP.js → chunk-ZBQU2VU3.js} +1 -1
- package/dist/lib/chunks/{chunk-7YZPEX3T.js → chunk-ZHGABG7L.js} +2 -2
- package/dist/lib/chunks/chunk-ZZCRYG36.js +31 -0
- package/dist/lib/cli/bundle-commands.js +89 -73
- package/dist/lib/cli/run-commands.js +136 -92
- package/dist/lib/cli/status-command.js +75 -59
- package/dist/lib/codex-workflow/aggregate.js +20 -12
- package/dist/lib/codex-workflow/author-tools.js +7 -7
- package/dist/lib/codex-workflow/author.js +23 -22
- package/dist/lib/codex-workflow/capture.js +38 -17
- package/dist/lib/codex-workflow/checkpoint.js +7 -7
- package/dist/lib/codex-workflow/checks.js +9 -9
- package/dist/lib/codex-workflow/copy-editor.js +19 -18
- package/dist/lib/codex-workflow/copy-recovery.js +4 -4
- package/dist/lib/codex-workflow/copy.js +16 -15
- package/dist/lib/codex-workflow/finish.js +28 -24
- package/dist/lib/codex-workflow/host-models.js +2 -2
- package/dist/lib/codex-workflow/loop.js +51 -46
- package/dist/lib/codex-workflow/quality.js +4 -4
- package/dist/lib/codex-workflow/run-inputs.js +6 -6
- package/dist/lib/codex-workflow/run-source.js +16 -16
- package/dist/lib/codex-workflow/schemas.js +6 -6
- package/dist/lib/codex-workflow/session.js +8 -8
- package/dist/lib/codex-workflow/stage.js +30 -28
- package/dist/lib/codex-workflow/workdir.js +18 -11
- package/dist/lib/engine/audit-file.js +7 -7
- package/dist/lib/engine/author.js +38 -36
- package/dist/lib/engine/cost-estimate.js +37 -9
- package/dist/lib/engine/local-run.js +74 -58
- package/dist/lib/engine/preflight.js +37 -9
- package/dist/lib/engine/rejudge.js +80 -64
- package/dist/lib/engine/run-bindings.js +62 -0
- package/dist/lib/engine/run-status.js +75 -59
- package/dist/lib/engine/templates.js +8 -8
- package/dist/lib/grounding/controller.js +8 -7
- package/dist/lib/grounding/extract.js +8 -6
- package/dist/lib/grounding/grounding-runtime.js +22 -21
- package/dist/lib/grounding/model-usage.js +2 -2
- package/dist/lib/grounding/model.js +7 -5
- package/dist/lib/grounding/recorded-tools.js +9 -9
- package/dist/lib/harness/report/artifacts.js +5 -5
- package/dist/lib/lib/checkpoint.js +5 -5
- package/dist/lib/lib/log.js +1 -1
- package/dist/lib/models/accounting.js +1 -1
- package/dist/lib/models/batching.js +6 -4
- package/dist/lib/models/budget-protection.js +1 -1
- package/dist/lib/models/cost-coefficients.js +2 -1
- package/dist/lib/models/gateway-cost.js +7 -0
- package/dist/lib/models/structured.js +5 -3
- package/dist/lib/models/supported-models.js +1 -1
- package/dist/lib/pipeline.js +36 -17
- package/dist/lib/probes/adapter.js +6 -0
- package/dist/lib/probes/adapters/cli-agents.js +44 -0
- package/dist/lib/probes/adapters/cli-claude-code.js +21 -0
- package/dist/lib/probes/adapters/cli-codex.js +23 -0
- package/dist/lib/probes/adapters/cli-cursor.js +21 -0
- package/dist/lib/probes/adapters/cli-process.js +34 -0
- package/dist/lib/probes/adapters/dataforseo-ai-overview.js +20 -0
- package/dist/lib/probes/adapters/gateway-chat.js +30 -0
- package/dist/lib/probes/adapters/gateway-perplexity.js +19 -0
- package/dist/lib/probes/agent-cli-parsers.js +2 -2
- package/dist/lib/probes/agent-clis.js +26 -11
- package/dist/lib/probes/answer-text.js +21 -0
- package/dist/lib/probes/attempt.js +28 -0
- package/dist/lib/probes/bindings.js +69 -0
- package/dist/lib/probes/charged-trace.js +47 -3
- package/dist/lib/probes/dataforseo-locale.js +1 -1
- package/dist/lib/probes/dataforseo.js +8 -5
- package/dist/lib/probes/grounding-urls.js +6 -4
- package/dist/lib/probes/legacy-bindings.js +7 -0
- package/dist/lib/probes/limits.js +1 -1
- package/dist/lib/probes/probes.js +33 -12
- package/dist/lib/runner.js +17 -14
- package/dist/lib/schema.js +4 -4
- package/dist/lib/section-result.js +6 -6
- package/dist/lib/stores/artifacts.js +7 -7
- package/dist/lib/stores/files.js +19 -17
- package/dist/lib/stores/local-versions.js +5 -5
- package/dist/lib/stores/memory.js +19 -14
- package/dist/lib/stores/publication.js +10 -10
- package/dist/lib/stores/unit-record.js +28 -0
- package/dist/lib/viewer/server.js +32 -24
- package/dist/lib/workflow/schema.js +3 -3
- package/dist/types/bundle/contracts.d.ts +1 -1
- package/dist/types/capture-manifest.d.ts +1 -0
- package/dist/types/codex-workflow/capture.d.ts +13 -0
- package/dist/types/codex-workflow/workdir.d.ts +0 -1
- package/dist/types/engine/cost-estimate.d.ts +5 -1
- package/dist/types/engine/preflight.d.ts +6 -1
- package/dist/types/engine/run-bindings.d.ts +25 -0
- package/dist/types/models/accounting.d.ts +7 -0
- package/dist/types/models/cost-coefficients.d.ts +5 -5
- package/dist/types/models/gateway-cost.d.ts +3 -0
- package/dist/types/pipeline.d.ts +2 -0
- package/dist/types/probes/adapter.d.ts +112 -0
- package/dist/types/probes/adapters/cli-agents.d.ts +19 -0
- package/dist/types/probes/adapters/cli-claude-code.d.ts +4 -0
- package/dist/types/probes/adapters/cli-codex.d.ts +10 -0
- package/dist/types/probes/adapters/cli-cursor.d.ts +4 -0
- package/dist/types/probes/adapters/cli-process.d.ts +86 -0
- package/dist/types/probes/adapters/dataforseo-ai-overview.d.ts +12 -0
- package/dist/types/probes/adapters/gateway-chat.d.ts +32 -0
- package/dist/types/probes/adapters/gateway-perplexity.d.ts +10 -0
- package/dist/types/probes/agent-clis.d.ts +7 -49
- package/dist/types/probes/answer-text.d.ts +20 -0
- package/dist/types/probes/attempt.d.ts +113 -0
- package/dist/types/probes/bindings.d.ts +79 -0
- package/dist/types/probes/charged-trace.d.ts +2 -1
- package/dist/types/probes/dataforseo.d.ts +24 -1
- package/dist/types/probes/grounding-urls.d.ts +18 -5
- package/dist/types/probes/legacy-bindings.d.ts +36 -0
- package/dist/types/probes/limits.d.ts +12 -6
- package/dist/types/probes/probe-trace.d.ts +10 -0
- package/dist/types/probes/probes.d.ts +5 -3
- package/dist/types/stores/ports.d.ts +1 -0
- package/dist/types/stores/unit-record.d.ts +11 -0
- package/package.json +6 -6
- package/src/analysis/progress.ts +10 -1
- package/src/bundle/contracts.ts +1 -1
- package/src/bundle/recorder.ts +47 -1
- package/src/capture-manifest.ts +6 -0
- package/src/cli/run-commands.ts +37 -1
- package/src/codex-workflow/capture.ts +60 -0
- package/src/codex-workflow/workdir.ts +3 -4
- package/src/engine/cost-estimate.ts +10 -4
- package/src/engine/local-run.ts +21 -2
- package/src/engine/preflight.ts +113 -17
- package/src/engine/rejudge.ts +5 -0
- package/src/engine/run-bindings.ts +142 -0
- package/src/grounding/extract.ts +1 -0
- package/src/grounding/model-usage.ts +4 -0
- package/src/lib/log.ts +17 -15
- package/src/models/accounting.ts +34 -0
- package/src/models/budget-protection.ts +1 -0
- package/src/models/cost-coefficients.ts +17 -13
- package/src/models/gateway-cost.ts +32 -0
- package/src/models/structured.ts +2 -10
- package/src/models/supported-models.ts +2 -2
- package/src/pipeline.ts +36 -17
- package/src/probes/adapter.ts +122 -0
- package/src/probes/adapters/cli-agents.ts +123 -0
- package/src/probes/adapters/cli-claude-code.ts +119 -0
- package/src/probes/adapters/cli-codex.ts +145 -0
- package/src/probes/adapters/cli-cursor.ts +95 -0
- package/src/probes/adapters/cli-process.ts +522 -0
- package/src/probes/adapters/dataforseo-ai-overview.ts +106 -0
- package/src/probes/adapters/gateway-chat.ts +339 -0
- package/src/probes/adapters/gateway-perplexity.ts +104 -0
- package/src/probes/agent-clis.ts +151 -1088
- package/src/probes/answer-text.ts +153 -0
- package/src/probes/attempt.ts +462 -0
- package/src/probes/bindings.ts +344 -0
- package/src/probes/charged-trace.ts +54 -13
- package/src/probes/dataforseo-locale.ts +13 -2
- package/src/probes/dataforseo.ts +265 -188
- package/src/probes/grounding-urls.ts +68 -19
- package/src/probes/legacy-bindings.ts +40 -0
- package/src/probes/limits.ts +31 -18
- package/src/probes/probe-trace.ts +10 -0
- package/src/probes/probes.ts +64 -736
- package/src/runner.ts +13 -0
- package/src/stores/files.ts +78 -54
- package/src/stores/memory.ts +4 -1
- package/src/stores/ports.ts +3 -0
- package/src/stores/unit-record.ts +151 -0
- package/dist/lib/chunks/chunk-BGAHZIL3.js +0 -563
- package/dist/lib/chunks/chunk-IB6RROHE.js +0 -204
- package/dist/lib/chunks/chunk-WZGI7UVM.js +0 -937
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { RetrievalLog } from '../schema';
|
|
2
|
+
import type { ProbeAnswer } from './adapter';
|
|
3
|
+
import type { AIOverviewReference } from './dataforseo';
|
|
4
|
+
import { retrievalResultUrls, rewriteRetrievalUrls } from './chat-retrieval';
|
|
5
|
+
import {
|
|
6
|
+
extractGroundingRedirectUrls,
|
|
7
|
+
isGroundingRedirectUrl,
|
|
8
|
+
rewriteTextUrls,
|
|
9
|
+
traceGroundingRedirects,
|
|
10
|
+
type RedirectResolution,
|
|
11
|
+
} from './grounding-urls';
|
|
12
|
+
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// The banked answer's bytes (specs/aeo-probe-adapters-spec.md §4.4). The
|
|
15
|
+
// order is part of the contract, because each step changes the bytes:
|
|
16
|
+
// 1. render the source block after the body;
|
|
17
|
+
// 2. strip U+0000, so JSON snapshot files stay portable;
|
|
18
|
+
// 3. resolve Google's grounding redirects in the text and the retrieval
|
|
19
|
+
// result sets, for the adapters that do (never the coding agents), so
|
|
20
|
+
// attribution sees real targets. A probe banks the moment its own
|
|
21
|
+
// redirects resolve, never after the whole batch.
|
|
22
|
+
// The markers below are the one copy the work directory reads.
|
|
23
|
+
// ============================================================================
|
|
24
|
+
|
|
25
|
+
export const searchSourcesMarker = 'Sources used by search:';
|
|
26
|
+
export const aiOverviewSourcesMarker = 'Sources cited in AI Overview:';
|
|
27
|
+
|
|
28
|
+
const searchSourceRows = (sources: readonly unknown[]): string[] => {
|
|
29
|
+
const rows: string[] = [];
|
|
30
|
+
|
|
31
|
+
for (const source of sources) {
|
|
32
|
+
if (
|
|
33
|
+
!source ||
|
|
34
|
+
typeof source !== 'object' ||
|
|
35
|
+
!('sourceType' in source) ||
|
|
36
|
+
source.sourceType !== 'url' ||
|
|
37
|
+
!('url' in source) ||
|
|
38
|
+
typeof source.url !== 'string'
|
|
39
|
+
) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const title =
|
|
44
|
+
'title' in source && typeof source.title === 'string'
|
|
45
|
+
? source.title
|
|
46
|
+
: source.url;
|
|
47
|
+
|
|
48
|
+
rows.push(`- ${title}: ${source.url}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return rows;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The body with its search sources appended; trimmed only when a source
|
|
55
|
+
* is listed. */
|
|
56
|
+
const withSearchSources = (
|
|
57
|
+
text: string,
|
|
58
|
+
sources: readonly unknown[],
|
|
59
|
+
): string => {
|
|
60
|
+
const rows = searchSourceRows(sources);
|
|
61
|
+
|
|
62
|
+
if (rows.length === 0) {
|
|
63
|
+
return text;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return `${text.trim()}\n\n${searchSourcesMarker}\n${rows.join('\n')}`;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const withAiOverviewReferences = (
|
|
70
|
+
markdown: string,
|
|
71
|
+
references: readonly AIOverviewReference[],
|
|
72
|
+
): string => {
|
|
73
|
+
if (references.length === 0) {
|
|
74
|
+
return markdown;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const rows = references.map((reference) => {
|
|
78
|
+
// A wrapped reference (google.com/goto) reports domain "google.com" for
|
|
79
|
+
// every source; its URL is resolved to the real target after the call,
|
|
80
|
+
// so the misleading label is dropped rather than printed next to it.
|
|
81
|
+
const domainLabel = isGroundingRedirectUrl(reference.url)
|
|
82
|
+
? ''
|
|
83
|
+
: ` (${reference.domain})`;
|
|
84
|
+
|
|
85
|
+
return `- ${reference.source}${domainLabel}: "${reference.title}" — ${reference.url}`;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return `${markdown}\n\n${aiOverviewSourcesMarker}\n${rows.join('\n')}`;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** renderAnswer is the answer's text with its source block. */
|
|
92
|
+
export const renderAnswer = (answer: ProbeAnswer): string => {
|
|
93
|
+
const { sources } = answer;
|
|
94
|
+
|
|
95
|
+
switch (sources.format) {
|
|
96
|
+
case 'search':
|
|
97
|
+
return withSearchSources(answer.answerText, sources.sources);
|
|
98
|
+
|
|
99
|
+
case 'ai_overview':
|
|
100
|
+
return withAiOverviewReferences(answer.answerText, sources.references);
|
|
101
|
+
|
|
102
|
+
case 'none':
|
|
103
|
+
return answer.answerText;
|
|
104
|
+
|
|
105
|
+
default: {
|
|
106
|
+
const unhandled: never = sources;
|
|
107
|
+
|
|
108
|
+
throw new Error(`Unhandled source format: ${String(unhandled)}.`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type FinalAnswer = {
|
|
114
|
+
text: string;
|
|
115
|
+
retrieval: RetrievalLog | null;
|
|
116
|
+
/** Every redirect resolution attempted, for the journal; empty when the
|
|
117
|
+
* answer named no wrapper or the adapter keeps them. */
|
|
118
|
+
redirects: RedirectResolution[];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** finalizeAnswer renders, strips U+0000 and resolves redirects, in that
|
|
122
|
+
* order. */
|
|
123
|
+
export const finalizeAnswer = async (
|
|
124
|
+
answer: ProbeAnswer,
|
|
125
|
+
{
|
|
126
|
+
resolvesRedirects,
|
|
127
|
+
logContext,
|
|
128
|
+
}: { resolvesRedirects: boolean; logContext?: Record<string, unknown> },
|
|
129
|
+
): Promise<FinalAnswer> => {
|
|
130
|
+
const text = renderAnswer(answer).replaceAll('\u0000', '');
|
|
131
|
+
|
|
132
|
+
if (!resolvesRedirects) {
|
|
133
|
+
return { text, retrieval: answer.retrieval, redirects: [] };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const { resolved, resolutions } = await traceGroundingRedirects(
|
|
137
|
+
[
|
|
138
|
+
...extractGroundingRedirectUrls(text),
|
|
139
|
+
...(answer.retrieval ? retrievalResultUrls(answer.retrieval) : []),
|
|
140
|
+
],
|
|
141
|
+
logContext,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const finalAnswer: FinalAnswer = {
|
|
145
|
+
text: rewriteTextUrls(text, resolved),
|
|
146
|
+
retrieval: answer.retrieval
|
|
147
|
+
? rewriteRetrievalUrls(answer.retrieval, resolved)
|
|
148
|
+
: null,
|
|
149
|
+
redirects: resolutions,
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
return finalAnswer;
|
|
153
|
+
};
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import type { Log } from '../lib/log';
|
|
2
|
+
import { servedModelOf } from '../models/served-model';
|
|
3
|
+
import type { RetrievalLog } from '../schema';
|
|
4
|
+
import type { ProbeAdapter, ProbeAnswer } from './adapter';
|
|
5
|
+
import { finalizeAnswer } from './answer-text';
|
|
6
|
+
import {
|
|
7
|
+
agentProbeRetryPolicy,
|
|
8
|
+
errorMessage,
|
|
9
|
+
probeRetryPolicy,
|
|
10
|
+
retryDelayMs,
|
|
11
|
+
type ProbeFailureKind,
|
|
12
|
+
type RetryPolicy,
|
|
13
|
+
} from './failure';
|
|
14
|
+
import { sleep, type ProbeLimiter } from './limits';
|
|
15
|
+
import type { ProbeOutcome, ProbeUnitRef } from './outcomes';
|
|
16
|
+
import {
|
|
17
|
+
endAttempt,
|
|
18
|
+
ProbeTraceError,
|
|
19
|
+
startAttempt,
|
|
20
|
+
type ProbeAttemptHandle,
|
|
21
|
+
type ProbeTrace,
|
|
22
|
+
} from './probe-trace';
|
|
23
|
+
|
|
24
|
+
// ============================================================================
|
|
25
|
+
// The host's attempt loop (specs/aeo-probe-adapters-spec.md §4.3), one for
|
|
26
|
+
// chat surfaces, coding agents and canaries. The host owns what makes the
|
|
27
|
+
// evidence trustworthy: the pool, the journal (an attempt's operation opens
|
|
28
|
+
// before its dispatch and ends with its real outcome), retries and the
|
|
29
|
+
// failure taxonomy, answer finalisation and banking before anything else
|
|
30
|
+
// can fail. An adapter only asks and classifies.
|
|
31
|
+
//
|
|
32
|
+
// Chat units and coding agents run under the same rules (attemptRules) and
|
|
33
|
+
// differ only in their retry ladder: each takes its pool slot aware of the
|
|
34
|
+
// abort signal, jitters its retries, and on capacity pauses its own adapter
|
|
35
|
+
// rather than the whole pool, which serves other surfaces. A canary takes
|
|
36
|
+
// no pool and keeps the default ladder. These are run policy, not method.
|
|
37
|
+
// ============================================================================
|
|
38
|
+
|
|
39
|
+
type AttemptRules = {
|
|
40
|
+
/** The family's name in log lines. */
|
|
41
|
+
label: string;
|
|
42
|
+
policy: Readonly<Record<ProbeFailureKind, RetryPolicy>>;
|
|
43
|
+
/** ±20% on every retry delay, so units that failed together do not all
|
|
44
|
+
* retry in the same second. */
|
|
45
|
+
jitter: boolean;
|
|
46
|
+
/** Whether the attempt takes a slot on its adapter's pool. */
|
|
47
|
+
pooled: boolean;
|
|
48
|
+
completedLevel: 'debug' | 'info';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const attemptRules = {
|
|
52
|
+
chat: {
|
|
53
|
+
label: 'Probe',
|
|
54
|
+
policy: probeRetryPolicy,
|
|
55
|
+
jitter: true,
|
|
56
|
+
pooled: true,
|
|
57
|
+
completedLevel: 'debug',
|
|
58
|
+
},
|
|
59
|
+
agent: {
|
|
60
|
+
label: 'Agent probe',
|
|
61
|
+
policy: agentProbeRetryPolicy,
|
|
62
|
+
jitter: true,
|
|
63
|
+
pooled: true,
|
|
64
|
+
completedLevel: 'info',
|
|
65
|
+
},
|
|
66
|
+
canary: {
|
|
67
|
+
label: 'Canary probe',
|
|
68
|
+
policy: probeRetryPolicy,
|
|
69
|
+
jitter: false,
|
|
70
|
+
pooled: false,
|
|
71
|
+
completedLevel: 'info',
|
|
72
|
+
},
|
|
73
|
+
} as const satisfies Record<string, AttemptRules>;
|
|
74
|
+
|
|
75
|
+
const retryJitterRatio = 0.2;
|
|
76
|
+
|
|
77
|
+
const addJitter = (delayMs: number): number => {
|
|
78
|
+
const factor = 1 - retryJitterRatio + Math.random() * 2 * retryJitterRatio;
|
|
79
|
+
|
|
80
|
+
return Math.round(delayMs * factor);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** What the journal names an attempt by. */
|
|
84
|
+
export type AttemptUnit = {
|
|
85
|
+
promptId: string;
|
|
86
|
+
promptText: string;
|
|
87
|
+
runIndex: number;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type AttemptHost = {
|
|
91
|
+
rules: AttemptRules;
|
|
92
|
+
trace?: ProbeTrace;
|
|
93
|
+
/** Required unless the rules take no pool. */
|
|
94
|
+
limiter?: ProbeLimiter;
|
|
95
|
+
signal?: AbortSignal;
|
|
96
|
+
log: Log;
|
|
97
|
+
logContext: Record<string, unknown>;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type AttemptResult =
|
|
101
|
+
| {
|
|
102
|
+
status: 'answered';
|
|
103
|
+
answer: ProbeAnswer;
|
|
104
|
+
attempts: number;
|
|
105
|
+
durationMs: number;
|
|
106
|
+
/** The answered attempt's open operation; the caller ends it. */
|
|
107
|
+
handle: ProbeAttemptHandle | undefined;
|
|
108
|
+
}
|
|
109
|
+
| {
|
|
110
|
+
status: 'failed';
|
|
111
|
+
kind: ProbeFailureKind;
|
|
112
|
+
lastError: string;
|
|
113
|
+
attempts: number;
|
|
114
|
+
/** The last error thrown, when one was: a provider error, or the
|
|
115
|
+
* ProbeTraceError of an operation that could not be opened. */
|
|
116
|
+
cause?: unknown;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* requestedModelOf is the model a probe asked for: the model its binding
|
|
121
|
+
* pins for a model API; otherwise the one the answer names (Codex learns
|
|
122
|
+
* it only when it answers); otherwise none.
|
|
123
|
+
*/
|
|
124
|
+
export const requestedModelOf = (
|
|
125
|
+
bound: { model?: string },
|
|
126
|
+
answeredModel?: string | null,
|
|
127
|
+
): string | null => {
|
|
128
|
+
return bound.model ?? answeredModel ?? null;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** The key one adapter's capacity pause is held under: the adapter serving
|
|
132
|
+
* this surface, never the pool it shares with others. */
|
|
133
|
+
const pauseKeyOf = (adapter: ProbeAdapter): string => {
|
|
134
|
+
return `${adapter.id}|${adapter.surface}`;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** A unit whose run was aborted while it waited for its pool slot: it is
|
|
138
|
+
* not dispatched once the slot frees. */
|
|
139
|
+
class AbortedBeforeDispatchError extends Error {
|
|
140
|
+
constructor() {
|
|
141
|
+
super('run aborted before the unit executed');
|
|
142
|
+
this.name = 'AbortedBeforeDispatchError';
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const inPool = async <T>(
|
|
147
|
+
adapter: ProbeAdapter,
|
|
148
|
+
{ rules, limiter, signal }: AttemptHost,
|
|
149
|
+
work: () => Promise<T>,
|
|
150
|
+
): Promise<T> => {
|
|
151
|
+
if (!rules.pooled || !limiter) {
|
|
152
|
+
return work();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return limiter.run(
|
|
156
|
+
adapter.pool,
|
|
157
|
+
async () => {
|
|
158
|
+
if (signal?.aborted) {
|
|
159
|
+
throw new AbortedBeforeDispatchError();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return work();
|
|
163
|
+
},
|
|
164
|
+
{ signal, pauseKey: pauseKeyOf(adapter) },
|
|
165
|
+
);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* runProbeAttempts asks `adapter` until it answers or its retry policy is
|
|
170
|
+
* spent. Each attempt opens its operation inside the pool slot, so the
|
|
171
|
+
* operation starts when the request does, and before it: no dispatch goes
|
|
172
|
+
* unrecorded. An answered attempt's operation is left open for the caller,
|
|
173
|
+
* who finalises the answer before ending it.
|
|
174
|
+
*/
|
|
175
|
+
export const runProbeAttempts = async (
|
|
176
|
+
adapter: ProbeAdapter,
|
|
177
|
+
unit: AttemptUnit,
|
|
178
|
+
host: AttemptHost,
|
|
179
|
+
): Promise<AttemptResult> => {
|
|
180
|
+
const { rules, log, signal } = host;
|
|
181
|
+
|
|
182
|
+
// A wiring mistake, refused before the first attempt: inside one it would
|
|
183
|
+
// be classified and retried as a provider failure.
|
|
184
|
+
if (rules.pooled && !host.limiter) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`Please give the ${rules.label} attempts of ${adapter.surface} a pool limiter.`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let attempt = 0;
|
|
191
|
+
let handle: ProbeAttemptHandle | undefined;
|
|
192
|
+
|
|
193
|
+
for (;;) {
|
|
194
|
+
if (signal?.aborted) {
|
|
195
|
+
return {
|
|
196
|
+
status: 'failed',
|
|
197
|
+
kind: 'timeout',
|
|
198
|
+
attempts: attempt,
|
|
199
|
+
lastError: 'run aborted before the unit executed',
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
attempt += 1;
|
|
204
|
+
const startedAt = Date.now();
|
|
205
|
+
const retryOf = handle?.id;
|
|
206
|
+
|
|
207
|
+
handle = undefined;
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
const answer = await inPool(adapter, host, async () => {
|
|
211
|
+
handle = await startAttempt(host.trace, {
|
|
212
|
+
surface: adapter.surface,
|
|
213
|
+
promptId: unit.promptId,
|
|
214
|
+
promptText: unit.promptText,
|
|
215
|
+
runIndex: unit.runIndex,
|
|
216
|
+
attempt,
|
|
217
|
+
...(retryOf === undefined ? {} : { retryOf }),
|
|
218
|
+
requestedModel: requestedModelOf(adapter),
|
|
219
|
+
adapter: adapter.id,
|
|
220
|
+
request: adapter.request(unit.promptText),
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
return adapter.probe(unit.promptText, { signal });
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
status: 'answered',
|
|
228
|
+
answer,
|
|
229
|
+
attempts: attempt,
|
|
230
|
+
durationMs: Date.now() - startedAt,
|
|
231
|
+
handle,
|
|
232
|
+
};
|
|
233
|
+
} catch (error) {
|
|
234
|
+
// Never dispatched: the unit stays as unattempted as it was.
|
|
235
|
+
if (error instanceof AbortedBeforeDispatchError) {
|
|
236
|
+
return {
|
|
237
|
+
status: 'failed',
|
|
238
|
+
kind: 'timeout',
|
|
239
|
+
attempts: attempt - 1,
|
|
240
|
+
lastError: error.message,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (error instanceof ProbeTraceError) {
|
|
245
|
+
return {
|
|
246
|
+
status: 'failed',
|
|
247
|
+
kind: 'persistence',
|
|
248
|
+
attempts: attempt,
|
|
249
|
+
lastError: error.message,
|
|
250
|
+
cause: error,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const failure = adapter.classifyError(error);
|
|
255
|
+
const unrecorded = await endAttempt(handle, {
|
|
256
|
+
outcome: signal?.aborted ? 'cancelled' : 'failed',
|
|
257
|
+
kind: failure.kind,
|
|
258
|
+
error: failure.message,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
if (unrecorded !== null) {
|
|
262
|
+
return {
|
|
263
|
+
status: 'failed',
|
|
264
|
+
kind: 'persistence',
|
|
265
|
+
attempts: attempt,
|
|
266
|
+
lastError: unrecorded,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const ladderMs = retryDelayMs(failure.kind, attempt, rules.policy);
|
|
271
|
+
const delayMs =
|
|
272
|
+
ladderMs !== null && rules.jitter ? addJitter(ladderMs) : ladderMs;
|
|
273
|
+
|
|
274
|
+
log.warn(
|
|
275
|
+
delayMs === null
|
|
276
|
+
? `${rules.label} failed`
|
|
277
|
+
: `${rules.label} failed; retrying`,
|
|
278
|
+
{
|
|
279
|
+
...host.logContext,
|
|
280
|
+
attempt,
|
|
281
|
+
kind: failure.kind,
|
|
282
|
+
durationMs: Date.now() - startedAt,
|
|
283
|
+
...(delayMs === null ? {} : { retryInMs: delayMs }),
|
|
284
|
+
error: failure.message,
|
|
285
|
+
},
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
if (delayMs === null) {
|
|
289
|
+
return {
|
|
290
|
+
status: 'failed',
|
|
291
|
+
kind: failure.kind,
|
|
292
|
+
attempts: attempt,
|
|
293
|
+
lastError: failure.message,
|
|
294
|
+
cause: error,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Every unit of this adapter waits the window out, not only this one;
|
|
299
|
+
// other surfaces on the same pool keep going.
|
|
300
|
+
if (failure.kind === 'capacity' && rules.pooled) {
|
|
301
|
+
host.limiter?.pause(pauseKeyOf(adapter), delayMs);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
await sleep(delayMs, signal);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/** reportedModelOf applies the adapter's served-model rule. */
|
|
310
|
+
export const reportedModelOf = (
|
|
311
|
+
adapter: ProbeAdapter,
|
|
312
|
+
answer: ProbeAnswer,
|
|
313
|
+
): string | undefined => {
|
|
314
|
+
const raw = answer.servedModel === '' ? undefined : answer.servedModel;
|
|
315
|
+
|
|
316
|
+
switch (adapter.observes.servedModel) {
|
|
317
|
+
case 'compare_to_requested': {
|
|
318
|
+
const requested = requestedModelOf(adapter);
|
|
319
|
+
|
|
320
|
+
// Without a requested model there is nothing to compare against.
|
|
321
|
+
return requested === null ? raw : servedModelOf(requested, raw);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
case 'raw':
|
|
325
|
+
return raw;
|
|
326
|
+
|
|
327
|
+
case 'none':
|
|
328
|
+
return undefined;
|
|
329
|
+
|
|
330
|
+
default: {
|
|
331
|
+
const unhandled: never = adapter.observes.servedModel;
|
|
332
|
+
|
|
333
|
+
throw new Error(`Unhandled served-model rule: ${String(unhandled)}.`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
/** A unit's answer as it is banked and journaled. */
|
|
339
|
+
export type BankedAnswer = {
|
|
340
|
+
text: string;
|
|
341
|
+
retrieval: RetrievalLog | null;
|
|
342
|
+
requestedModel: string | null;
|
|
343
|
+
reportedModel?: string;
|
|
344
|
+
noOverview?: true;
|
|
345
|
+
usedWebSearch?: boolean | null;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
export type UnitHost<P> = AttemptHost & {
|
|
349
|
+
/** The banked result of an answer. */
|
|
350
|
+
toResult: (answer: BankedAnswer) => P;
|
|
351
|
+
/** Banks a captured result. A throw fails the unit as `persistence`; the
|
|
352
|
+
* probe is never dispatched again (hardening spec §4.2). */
|
|
353
|
+
bank?: (result: P) => Promise<void>;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* runProbeUnit runs one planned unit to exactly one outcome: its answer
|
|
358
|
+
* finalised, its operation ended with what is banked, and the result
|
|
359
|
+
* banked; or the typed failure that stopped it.
|
|
360
|
+
*/
|
|
361
|
+
export const runProbeUnit = async <P>(
|
|
362
|
+
adapter: ProbeAdapter,
|
|
363
|
+
unit: AttemptUnit,
|
|
364
|
+
host: UnitHost<P>,
|
|
365
|
+
): Promise<ProbeOutcome<P>> => {
|
|
366
|
+
const { log, rules } = host;
|
|
367
|
+
const ref: ProbeUnitRef = {
|
|
368
|
+
promptId: unit.promptId,
|
|
369
|
+
surface: adapter.surface,
|
|
370
|
+
runIndex: unit.runIndex,
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const attempted = await runProbeAttempts(adapter, unit, host);
|
|
374
|
+
|
|
375
|
+
if (attempted.status === 'failed') {
|
|
376
|
+
return {
|
|
377
|
+
status: 'failed',
|
|
378
|
+
unit: ref,
|
|
379
|
+
kind: attempted.kind,
|
|
380
|
+
attempts: attempted.attempts,
|
|
381
|
+
lastError: attempted.lastError,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const { answer, attempts } = attempted;
|
|
386
|
+
const final = await finalizeAnswer(answer, {
|
|
387
|
+
resolvesRedirects: adapter.resolvesRedirects,
|
|
388
|
+
logContext: host.logContext,
|
|
389
|
+
});
|
|
390
|
+
const reportedModel = reportedModelOf(adapter, answer);
|
|
391
|
+
const banked: BankedAnswer = {
|
|
392
|
+
text: final.text,
|
|
393
|
+
retrieval: final.retrieval,
|
|
394
|
+
requestedModel: requestedModelOf(adapter, answer.requestedModel),
|
|
395
|
+
...(reportedModel ? { reportedModel } : {}),
|
|
396
|
+
...(answer.noOverview ? { noOverview: true as const } : {}),
|
|
397
|
+
...(answer.usedWebSearch === undefined
|
|
398
|
+
? {}
|
|
399
|
+
: { usedWebSearch: answer.usedWebSearch }),
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
log[rules.completedLevel](`${rules.label} completed`, {
|
|
403
|
+
...host.logContext,
|
|
404
|
+
attempt: attempts,
|
|
405
|
+
durationMs: attempted.durationMs,
|
|
406
|
+
chars: final.text.length,
|
|
407
|
+
...(banked.usedWebSearch === undefined
|
|
408
|
+
? {}
|
|
409
|
+
: {
|
|
410
|
+
usedWebSearch: banked.usedWebSearch,
|
|
411
|
+
retrievalCalls: final.retrieval?.calls.length ?? null,
|
|
412
|
+
}),
|
|
413
|
+
...(banked.noOverview ? { noOverview: true } : {}),
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
const unrecorded = await endAttempt(attempted.handle, {
|
|
417
|
+
outcome: 'succeeded',
|
|
418
|
+
text: banked.text,
|
|
419
|
+
retrieval: banked.retrieval,
|
|
420
|
+
requestedModel: banked.requestedModel,
|
|
421
|
+
...(banked.reportedModel ? { reportedModel: banked.reportedModel } : {}),
|
|
422
|
+
...(banked.noOverview ? { noOverview: true as const } : {}),
|
|
423
|
+
...(answer.reportedCostUsd === undefined
|
|
424
|
+
? {}
|
|
425
|
+
: { costUsd: answer.reportedCostUsd }),
|
|
426
|
+
...(final.redirects.length > 0 ? { redirects: final.redirects } : {}),
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
if (unrecorded !== null) {
|
|
430
|
+
return {
|
|
431
|
+
status: 'failed',
|
|
432
|
+
unit: ref,
|
|
433
|
+
kind: 'persistence',
|
|
434
|
+
attempts,
|
|
435
|
+
lastError: unrecorded,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const result = host.toResult(banked);
|
|
440
|
+
|
|
441
|
+
// Banking sits outside the attempt loop: a writer that throws must never
|
|
442
|
+
// re-run a paid probe (§4.2).
|
|
443
|
+
try {
|
|
444
|
+
await host.bank?.(result);
|
|
445
|
+
} catch (error) {
|
|
446
|
+
log.error(
|
|
447
|
+
`${rules.label} captured but could not be persisted`,
|
|
448
|
+
error,
|
|
449
|
+
host.logContext,
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
return {
|
|
453
|
+
status: 'failed',
|
|
454
|
+
unit: ref,
|
|
455
|
+
kind: 'persistence',
|
|
456
|
+
attempts,
|
|
457
|
+
lastError: errorMessage(error),
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return { status: 'captured', unit: ref, probe: result, attempts };
|
|
462
|
+
};
|