@tangle-network/agent-eval 2.0.7 → 2.0.10
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/CHANGELOG.md +80 -16
- package/dist/adapters/http.d.ts +2 -2
- package/dist/{agent-profile-CRdalpZv.d.ts → agent-profile-_JzxgLDM.d.ts} +11 -5
- package/dist/{agent-profile-CRdalpZv.d.ts.map → agent-profile-_JzxgLDM.d.ts.map} +1 -1
- package/dist/analyst/index.d.ts +14 -13
- package/dist/analyst/index.d.ts.map +1 -1
- package/dist/analyst/index.js +34 -13
- package/dist/analyst/index.js.map +1 -1
- package/dist/analyze-runs-Izz_ZeWg.d.ts +65 -0
- package/dist/analyze-runs-Izz_ZeWg.d.ts.map +1 -0
- package/dist/{analyze-runs-C_9cXPZn.js → analyze-runs-xhH2Qxoc.js} +6 -5
- package/dist/{analyze-runs-C_9cXPZn.js.map → analyze-runs-xhH2Qxoc.js.map} +1 -1
- package/dist/app-grade/index.d.ts +4 -4
- package/dist/app-grade/index.js +1 -1
- package/dist/{benchmark-command-DkBMUDwy.js → benchmark-command-CGQXNFj7.js} +12 -8
- package/dist/benchmark-command-CGQXNFj7.js.map +1 -0
- package/dist/benchmarks/index.d.ts +4 -4
- package/dist/benchmarks/index.js +4 -4
- package/dist/builder-eval/index.js +2 -2
- package/dist/calibration-CrORXMWx.d.ts +128 -0
- package/dist/calibration-CrORXMWx.d.ts.map +1 -0
- package/dist/{calibration-Cfoup0xz.js → calibration-Dz8mQb5P.js} +2 -2
- package/dist/{calibration-Cfoup0xz.js.map → calibration-Dz8mQb5P.js.map} +1 -1
- package/dist/campaign/index.d.ts +8 -8
- package/dist/campaign/index.js +13 -12
- package/dist/{campaign-DfSWSreo.js → campaign-eus7YRs2.js} +1590 -15
- package/dist/campaign-eus7YRs2.js.map +1 -0
- package/dist/{campaign-evidence-BojANgU7.d.ts → campaign-evidence-BOcX4w0r.d.ts} +5 -5
- package/dist/{campaign-evidence-BojANgU7.d.ts.map → campaign-evidence-BOcX4w0r.d.ts.map} +1 -1
- package/dist/{campaign-evidence-D_oBFfLc.js → campaign-evidence-KM5GTsHG.js} +5 -5
- package/dist/{campaign-evidence-D_oBFfLc.js.map → campaign-evidence-KM5GTsHG.js.map} +1 -1
- package/dist/{cluster-Bj90LK-1.d.ts → chat-client-BlTooBDA.d.ts} +9 -241
- package/dist/chat-client-BlTooBDA.d.ts.map +1 -0
- package/dist/{chat-client-CvyDOQBv.js → chat-client-Oyot5tyo.js} +2 -2
- package/dist/{chat-client-CvyDOQBv.js.map → chat-client-Oyot5tyo.js.map} +1 -1
- package/dist/{chat-json-call-DMrwmB-k.js → chat-json-call-DTmv2uNY.js} +2 -2
- package/dist/{chat-json-call-DMrwmB-k.js.map → chat-json-call-DTmv2uNY.js.map} +1 -1
- package/dist/cli.js +7 -7
- package/dist/cluster-BJ0nW3P7.d.ts +241 -0
- package/dist/cluster-BJ0nW3P7.d.ts.map +1 -0
- package/dist/{cluster-bootstrap-DPzVy3_K.js → cluster-bootstrap-CTC8Dd30.js} +3 -2
- package/dist/{cluster-bootstrap-DPzVy3_K.js.map → cluster-bootstrap-CTC8Dd30.js.map} +1 -1
- package/dist/{completion-verifier-DsotH2l3.js → completion-verifier-CCPG2Yc0.js} +3 -3
- package/dist/{completion-verifier-DsotH2l3.js.map → completion-verifier-CCPG2Yc0.js.map} +1 -1
- package/dist/{completion-verifier-Cy38EpGE.d.ts → completion-verifier-CQOC3_bm.d.ts} +2 -2
- package/dist/{completion-verifier-Cy38EpGE.d.ts.map → completion-verifier-CQOC3_bm.d.ts.map} +1 -1
- package/dist/contract/index.d.ts +14 -12
- package/dist/contract/index.js +14 -12
- package/dist/contract/index.js.map +1 -1
- package/dist/{decision-point-C46p-p9v.js → decision-point-CfqtnGop.js} +3 -3
- package/dist/{decision-point-C46p-p9v.js.map → decision-point-CfqtnGop.js.map} +1 -1
- package/dist/{default-registry-BWFPtmuI.js → default-registry-Rs5uzlfJ.js} +4 -4
- package/dist/{default-registry-BWFPtmuI.js.map → default-registry-Rs5uzlfJ.js.map} +1 -1
- package/dist/{default-registry-xoX0PMCb.d.ts → default-registry-X8McMoUV.d.ts} +6 -6
- package/dist/{default-registry-xoX0PMCb.d.ts.map → default-registry-X8McMoUV.d.ts.map} +1 -1
- package/dist/define-agent-eval-D8ntOXqV.js +96 -0
- package/dist/define-agent-eval-D8ntOXqV.js.map +1 -0
- package/dist/define-agent-eval-DH5-xi-F.d.ts +52 -0
- package/dist/define-agent-eval-DH5-xi-F.d.ts.map +1 -0
- package/dist/diagnosis/index.d.ts +3 -3
- package/dist/diagnosis/index.js +1 -1
- package/dist/{dspy-rlm-engine-r7YpKLhS.js → dspy-rlm-engine-ChE1PUM5.js} +3 -3
- package/dist/{dspy-rlm-engine-r7YpKLhS.js.map → dspy-rlm-engine-ChE1PUM5.js.map} +1 -1
- package/dist/{emitter-BjW3NnX-.js → emitter-xN9wGTVw.js} +9 -2
- package/dist/{emitter-BjW3NnX-.js.map → emitter-xN9wGTVw.js.map} +1 -1
- package/dist/{eval-campaign-Cyk-tkLh.js → eval-campaign-BciGnrGy.js} +3 -3
- package/dist/{eval-campaign-Cyk-tkLh.js.map → eval-campaign-BciGnrGy.js.map} +1 -1
- package/dist/{evaluation-X5E22NQK.d.ts → evaluation-CjZH6gW0.d.ts} +4 -4
- package/dist/{evaluation-X5E22NQK.d.ts.map → evaluation-CjZH6gW0.d.ts.map} +1 -1
- package/dist/evaluation.d.ts +1 -1
- package/dist/{exact-types-DjuabhUV.d.ts → exact-types-BIHqYBYf.d.ts} +2 -2
- package/dist/{exact-types-DjuabhUV.d.ts.map → exact-types-BIHqYBYf.d.ts.map} +1 -1
- package/dist/{execution-insight-CE20Bxq0.d.ts → execution-insight-YihQrSqd.d.ts} +2 -2
- package/dist/{execution-insight-CE20Bxq0.d.ts.map → execution-insight-YihQrSqd.d.ts.map} +1 -1
- package/dist/experiment/index.d.ts +5 -5
- package/dist/experiment/index.js +10 -184
- package/dist/experiment/index.js.map +1 -1
- package/dist/{external-optimizer-contracts-ZDBfbZ_s.js → external-optimizer-contracts-BFV8lnhl.js} +7 -2
- package/dist/{external-optimizer-contracts-ZDBfbZ_s.js.map → external-optimizer-contracts-BFV8lnhl.js.map} +1 -1
- package/dist/{external-optimizer-process-4QdcF7Kd.js → external-optimizer-process-B6emPodQ.js} +3 -3
- package/dist/{external-optimizer-process-4QdcF7Kd.js.map → external-optimizer-process-B6emPodQ.js.map} +1 -1
- package/dist/{external-optimizer-subprocess-BW0ZWUUr.js → external-optimizer-subprocess-_O06eDVA.js} +35 -376
- package/dist/external-optimizer-subprocess-_O06eDVA.js.map +1 -0
- package/dist/{feedback-trajectory-BMkJezPc.d.ts → feedback-trajectory-CoQPmSJV.d.ts} +3 -3
- package/dist/{feedback-trajectory-BMkJezPc.d.ts.map → feedback-trajectory-CoQPmSJV.d.ts.map} +1 -1
- package/dist/finding-scope-B_As3F_v.js +359 -0
- package/dist/finding-scope-B_As3F_v.js.map +1 -0
- package/dist/{findings-store-DV8WA5nD.js → findings-store-CvYUzQBR.js} +4 -4
- package/dist/{findings-store-DV8WA5nD.js.map → findings-store-CvYUzQBR.js.map} +1 -1
- package/dist/fuzz.js +1 -1
- package/dist/{index-C6TG1LwA.d.ts → index-BSb3fJyZ.d.ts} +318 -131
- package/dist/index-BSb3fJyZ.d.ts.map +1 -0
- package/dist/{index-CKjqN9_U.d.ts → index-Bk1XEkP7.d.ts} +99 -3
- package/dist/index-Bk1XEkP7.d.ts.map +1 -0
- package/dist/{index-DAFZRMlF.d.ts → index-BmWw8zN6.d.ts} +10 -8
- package/dist/index-BmWw8zN6.d.ts.map +1 -0
- package/dist/{index-DKpMyD_Z.d.ts → index-CCOaNmh2.d.ts} +7 -7
- package/dist/{index-DKpMyD_Z.d.ts.map → index-CCOaNmh2.d.ts.map} +1 -1
- package/dist/{index-xEM_SoCl.d.ts → index-ZffK_5sD.d.ts} +22 -20
- package/dist/index-ZffK_5sD.d.ts.map +1 -0
- package/dist/{index-BqAPIToS.d.ts → index-aRalB_Ma.d.ts} +3 -3
- package/dist/{index-BqAPIToS.d.ts.map → index-aRalB_Ma.d.ts.map} +1 -1
- package/dist/index.d.ts +33 -29
- package/dist/index.js +28 -25
- package/dist/index.js.map +1 -1
- package/dist/{analyze-runs-xjKBBqIB.d.ts → insight-report-DYofCMLx.d.ts} +4 -64
- package/dist/insight-report-DYofCMLx.d.ts.map +1 -0
- package/dist/{integrity-BF0tJK21.js → integrity-DacT3oJ1.js} +2 -2
- package/dist/{integrity-BF0tJK21.js.map → integrity-DacT3oJ1.js.map} +1 -1
- package/dist/{integrity-B9320cw1.d.ts → integrity-DotDLWtj.d.ts} +2 -2
- package/dist/{integrity-B9320cw1.d.ts.map → integrity-DotDLWtj.d.ts.map} +1 -1
- package/dist/{judge-gate-DqlzOBOm.js → judge-gate-CpD3wmcf.js} +3 -3
- package/dist/{judge-gate-DqlzOBOm.js.map → judge-gate-CpD3wmcf.js.map} +1 -1
- package/dist/{llm-client-Dw4x6EdE.js → llm-client-Dk8lBnMh.js} +15 -2
- package/dist/{llm-client-Dw4x6EdE.js.map → llm-client-Dk8lBnMh.js.map} +1 -1
- package/dist/llm-judge-DYmEdo3M.js +476 -0
- package/dist/llm-judge-DYmEdo3M.js.map +1 -0
- package/dist/{matrix-CqZSpwgN.d.ts → matrix-BDl7hoBr.d.ts} +6 -2
- package/dist/matrix-BDl7hoBr.d.ts.map +1 -0
- package/dist/measure-validity-plan-BReXzcVh.d.ts +201 -0
- package/dist/measure-validity-plan-BReXzcVh.d.ts.map +1 -0
- package/dist/measure-validity-plan-CJkBNzX_.js +154 -0
- package/dist/measure-validity-plan-CJkBNzX_.js.map +1 -0
- package/dist/meta-eval/index.d.ts +5 -3
- package/dist/meta-eval/index.js +808 -15
- package/dist/meta-eval/index.js.map +1 -1
- package/dist/multishot/golden/index.d.ts +1 -1
- package/dist/multishot/index.d.ts +497 -4
- package/dist/multishot/index.d.ts.map +1 -1
- package/dist/multishot/index.js +1000 -48
- package/dist/multishot/index.js.map +1 -1
- package/dist/node-transport-DJno8V46.js +62 -0
- package/dist/node-transport-DJno8V46.js.map +1 -0
- package/dist/openapi.json +1 -1
- package/dist/{llm-judge-DQJTQx01.js → optimization-method-4JlbMxd2.js} +38 -513
- package/dist/optimization-method-4JlbMxd2.js.map +1 -0
- package/dist/{outcome-observations-DL3CC8w1.js → outcome-observations-DEh8_V_P.js} +31 -6
- package/dist/outcome-observations-DEh8_V_P.js.map +1 -0
- package/dist/outcome-store-CHtuvstd.d.ts +56 -0
- package/dist/outcome-store-CHtuvstd.d.ts.map +1 -0
- package/dist/{paired-arms-DZhH3FGu.js → paired-arms-CJH5Q0wx.js} +3 -2
- package/dist/{paired-arms-DZhH3FGu.js.map → paired-arms-CJH5Q0wx.js.map} +1 -1
- package/dist/{paired-promotion-decision-Dn3-jaq4.js → paired-promotion-decision-BtfVwEAk.js} +3 -2
- package/dist/{paired-promotion-decision-Dn3-jaq4.js.map → paired-promotion-decision-BtfVwEAk.js.map} +1 -1
- package/dist/{paired-promotion-decision-ChHBuuHV.d.ts → paired-promotion-decision-DJlI4z_k.d.ts} +2 -2
- package/dist/{paired-promotion-decision-ChHBuuHV.d.ts.map → paired-promotion-decision-DJlI4z_k.d.ts.map} +1 -1
- package/dist/{paired-promotion-power-BlzXdgMD.js → paired-promotion-power-DG1avBKf.js} +3 -3
- package/dist/{paired-promotion-power-BlzXdgMD.js.map → paired-promotion-power-DG1avBKf.js.map} +1 -1
- package/dist/paired-tests-CXcD_B80.js +688 -0
- package/dist/paired-tests-CXcD_B80.js.map +1 -0
- package/dist/pareto-DIVE5D0T.js +81 -0
- package/dist/pareto-DIVE5D0T.js.map +1 -0
- package/dist/pipelines/index.d.ts +2 -1
- package/dist/pipelines/index.d.ts.map +1 -1
- package/dist/pipelines/index.js +1 -1
- package/dist/{sequential-BphITa5b.js → power-COie_o6E.js} +181 -4
- package/dist/power-COie_o6E.js.map +1 -0
- package/dist/{prime-protocol-Cv6_47rh.d.ts → prime-protocol-BRMk6dGI.d.ts} +2 -2
- package/dist/{prime-protocol-Cv6_47rh.d.ts.map → prime-protocol-BRMk6dGI.d.ts.map} +1 -1
- package/dist/{promotion-gate-CGkxh36r.d.ts → promotion-gate-B0VTBoXg.d.ts} +2 -2
- package/dist/{promotion-gate-CGkxh36r.d.ts.map → promotion-gate-B0VTBoXg.d.ts.map} +1 -1
- package/dist/{promotion-policy-CxDIh-9p.js → promotion-policy-BgI7PsQn.js} +4 -4
- package/dist/{promotion-policy-CxDIh-9p.js.map → promotion-policy-BgI7PsQn.js.map} +1 -1
- package/dist/{proposal-findings-DedMih-u.js → proposal-findings-CfRFCFXS.js} +2 -2
- package/dist/{proposal-findings-DedMih-u.js.map → proposal-findings-CfRFCFXS.js.map} +1 -1
- package/dist/{registry-CIjC6SdV.d.ts → registry-BeInzotS.d.ts} +4 -4
- package/dist/{registry-CIjC6SdV.d.ts.map → registry-BeInzotS.d.ts.map} +1 -1
- package/dist/{release-confidence-BLHOMK5Q.js → release-confidence-Cndq1hwi.js} +2 -2
- package/dist/{release-confidence-BLHOMK5Q.js.map → release-confidence-Cndq1hwi.js.map} +1 -1
- package/dist/reporting.d.ts +5 -5
- package/dist/reporting.js +6 -5
- package/dist/reporting.js.map +1 -1
- package/dist/{research-report-D6XIK0eK.d.ts → research-report-CL5_UqCX.d.ts} +2 -2
- package/dist/{research-report-D6XIK0eK.d.ts.map → research-report-CL5_UqCX.d.ts.map} +1 -1
- package/dist/{researcher---DhHG4J.d.ts → researcher-BOpZP_Ja.d.ts} +4 -4
- package/dist/{researcher---DhHG4J.d.ts.map → researcher-BOpZP_Ja.d.ts.map} +1 -1
- package/dist/{reward-hacking-DKAflk2c.js → reward-hacking-DMTm1OBd.js} +2 -2
- package/dist/{reward-hacking-DKAflk2c.js.map → reward-hacking-DMTm1OBd.js.map} +1 -1
- package/dist/rl.d.ts +9 -8
- package/dist/rl.d.ts.map +1 -1
- package/dist/rl.js +11 -10
- package/dist/rl.js.map +1 -1
- package/dist/rollout/index.d.ts +2 -2
- package/dist/rollout/index.js +1 -1
- package/dist/{rollout-B_lrpd9a.js → rollout--rSr43Pd.js} +2 -2
- package/dist/{rollout-B_lrpd9a.js.map → rollout--rSr43Pd.js.map} +1 -1
- package/dist/{rollout-nodes-Vas6OSxX.js → rollout-nodes-DFxKpIr2.js} +39 -10
- package/dist/rollout-nodes-DFxKpIr2.js.map +1 -0
- package/dist/{rubric-predictive-validity--6z0q8s6.js → rubric-predictive-validity-BXJ92Jp_.js} +3 -9
- package/dist/rubric-predictive-validity-BXJ92Jp_.js.map +1 -0
- package/dist/{rubric-predictive-validity-C9wc7f_D.d.ts → rubric-predictive-validity-DH94_0Q1.d.ts} +3 -56
- package/dist/rubric-predictive-validity-DH94_0Q1.d.ts.map +1 -0
- package/dist/{run-improvement-loop-BfwOUudy.d.ts → run-improvement-loop-BeHu5D9Z.d.ts} +396 -11
- package/dist/run-improvement-loop-BeHu5D9Z.d.ts.map +1 -0
- package/dist/{run-record-Bqg0NZ_e.js → run-record-CuO8ErrI.js} +3 -2
- package/dist/run-record-CuO8ErrI.js.map +1 -0
- package/dist/{runtime-rollouts-drQynu3t.js → runtime-rollouts-CE0MbQPq.js} +55 -8
- package/dist/runtime-rollouts-CE0MbQPq.js.map +1 -0
- package/dist/search/index.d.ts +2 -2
- package/dist/search/index.js +2 -2
- package/dist/{search-kernel-DJq9bUDU.d.ts → search-kernel-CJZ-ZSV1.d.ts} +3 -3
- package/dist/{search-kernel-DJq9bUDU.d.ts.map → search-kernel-CJZ-ZSV1.d.ts.map} +1 -1
- package/dist/{pareto-Bg75Gajo.js → search-kernel-mZc79S6N.js} +6 -88
- package/dist/search-kernel-mZc79S6N.js.map +1 -0
- package/dist/{search-summary-B1x1Vmzh.js → search-summary-CCgP2qXz.js} +4 -4
- package/dist/{search-summary-B1x1Vmzh.js.map → search-summary-CCgP2qXz.js.map} +1 -1
- package/dist/{define-agent-eval-Cjc0wWnO.d.ts → self-improve-B1fsNejr.d.ts} +8 -54
- package/dist/self-improve-B1fsNejr.d.ts.map +1 -0
- package/dist/{define-agent-eval-B6uE3gNu.js → self-improve-C_Bcdmv8.js} +9 -97
- package/dist/self-improve-C_Bcdmv8.js.map +1 -0
- package/dist/{sequential-BhbTSxpt.d.ts → sequential-CHplorpC.d.ts} +2 -2
- package/dist/{sequential-BhbTSxpt.d.ts.map → sequential-CHplorpC.d.ts.map} +1 -1
- package/dist/{sequential-ux_XrjBf.js → sequential-DR-pnYQ7.js} +3 -3
- package/dist/{sequential-ux_XrjBf.js.map → sequential-DR-pnYQ7.js.map} +1 -1
- package/dist/{server-1fzBCxMh.js → server-aZFmRucn.js} +3 -3
- package/dist/{server-1fzBCxMh.js.map → server-aZFmRucn.js.map} +1 -1
- package/dist/{skillopt-optimization-method-D_swD0Cy.js → skillopt-optimization-method-DHk1K757.js} +95 -40
- package/dist/skillopt-optimization-method-DHk1K757.js.map +1 -0
- package/dist/{statistical-heldout-CaS4T-43.js → statistical-heldout-CL_7mI5C.js} +3 -3
- package/dist/{statistical-heldout-CaS4T-43.js.map → statistical-heldout-CL_7mI5C.js.map} +1 -1
- package/dist/{statistics-DSidA5I2.js → statistics-AKnVFnK-.js} +233 -700
- package/dist/statistics-AKnVFnK-.js.map +1 -0
- package/dist/statistics.d.ts +3 -3
- package/dist/statistics.js +3 -2
- package/dist/{store-tool-spans-DniaFwjM.d.ts → store-tool-spans-M3WxeRrG.d.ts} +4 -4
- package/dist/{store-tool-spans-DniaFwjM.d.ts.map → store-tool-spans-M3WxeRrG.d.ts.map} +1 -1
- package/dist/{summary-report-CI4J6gxU.js → summary-report-DyBkOC6V.js} +15 -7
- package/dist/summary-report-DyBkOC6V.js.map +1 -0
- package/dist/summary-report-figures-CJHygLgw.d.ts.map +1 -1
- package/dist/supervisor-run/index.d.ts +12 -2
- package/dist/supervisor-run/index.d.ts.map +1 -1
- package/dist/supervisor-run/index.js +3 -3
- package/dist/{systemone-DzY-2Zri.d.ts → systemone-4PadCE4C.d.ts} +8 -7
- package/dist/systemone-4PadCE4C.d.ts.map +1 -0
- package/dist/{systemone-review-CXecR_m-.d.ts → systemone-review-DpAXWDkL.d.ts} +2 -2
- package/dist/{systemone-review-CXecR_m-.d.ts.map → systemone-review-DpAXWDkL.d.ts.map} +1 -1
- package/dist/systemone.d.ts +3 -3
- package/dist/systemone.js +1 -1
- package/dist/{tool-groups-DF6OqhoG.d.ts → tool-groups-SlnoQNyT.d.ts} +2 -2
- package/dist/tool-groups-SlnoQNyT.d.ts.map +1 -0
- package/dist/tool-waste-B01AMpID.d.ts +121 -0
- package/dist/tool-waste-B01AMpID.d.ts.map +1 -0
- package/dist/{tool-waste-u7JJ_BlE.js → tool-waste-BZnYSa6J.js} +3 -2
- package/dist/{tool-waste-u7JJ_BlE.js.map → tool-waste-BZnYSa6J.js.map} +1 -1
- package/dist/{trace-questions-DO8y3J-E.d.ts → trace-questions-ybyF0B6N.d.ts} +2 -2
- package/dist/{trace-questions-DO8y3J-E.d.ts.map → trace-questions-ybyF0B6N.d.ts.map} +1 -1
- package/dist/trace-repair/index.d.ts +1 -1
- package/dist/trace-repair/index.js +1 -1
- package/dist/traces.d.ts +4 -4
- package/dist/traces.js +1 -1
- package/dist/trajectory-B3o6Ald_.d.ts +100 -0
- package/dist/trajectory-B3o6Ald_.d.ts.map +1 -0
- package/dist/trajectory-replay/index.js +1 -1
- package/dist/{tree-DQMlcNFI.js → tree-CiIe-f3f.js} +6 -4
- package/dist/{tree-DQMlcNFI.js.map → tree-CiIe-f3f.js.map} +1 -1
- package/dist/{types-fuOwNq1C.d.ts → types-B1W3v8dT.d.ts} +3 -2
- package/dist/types-B1W3v8dT.d.ts.map +1 -0
- package/dist/{types-BMSDkS8l.d.ts → types-D5XtEH9Q.d.ts} +11 -4
- package/dist/{types-BMSDkS8l.d.ts.map → types-D5XtEH9Q.d.ts.map} +1 -1
- package/dist/types-DK4vNv5q.js.map +1 -1
- package/dist/{types-D_PfKC59.d.ts → types-an-CUndX.d.ts} +2 -2
- package/dist/{types-D_PfKC59.d.ts.map → types-an-CUndX.d.ts.map} +1 -1
- package/dist/{types-BLv2Ngla.d.ts → types-xVtEOBhO.d.ts} +31 -8
- package/dist/types-xVtEOBhO.d.ts.map +1 -0
- package/dist/wire/index.d.ts +2 -2
- package/dist/wire/index.js +1 -1
- package/dist/world/index.d.ts +529 -0
- package/dist/world/index.d.ts.map +1 -0
- package/dist/world/index.js +1356 -0
- package/dist/world/index.js.map +1 -0
- package/docs/campaign-proposers.md +7 -0
- package/docs/concepts.md +2 -0
- package/docs/design/statistics-decisions.md +48 -0
- package/docs/eval-surface-map.md +5 -2
- package/docs/feature-guide.md +3 -1
- package/docs/multi-turn-optimization.md +84 -0
- package/docs/outcome-validity.md +108 -0
- package/docs/public-api.md +89 -34
- package/docs/{multi-shot-optimization.md → trajectory-optimization.md} +1 -1
- package/docs/world.md +104 -0
- package/package.json +6 -1
- package/dist/analyze-runs-xjKBBqIB.d.ts.map +0 -1
- package/dist/benchmark-command-DkBMUDwy.js.map +0 -1
- package/dist/campaign-DfSWSreo.js.map +0 -1
- package/dist/cluster-Bj90LK-1.d.ts.map +0 -1
- package/dist/define-agent-eval-B6uE3gNu.js.map +0 -1
- package/dist/define-agent-eval-Cjc0wWnO.d.ts.map +0 -1
- package/dist/external-optimizer-subprocess-BW0ZWUUr.js.map +0 -1
- package/dist/index-C6TG1LwA.d.ts.map +0 -1
- package/dist/index-CKjqN9_U.d.ts.map +0 -1
- package/dist/index-DAFZRMlF.d.ts.map +0 -1
- package/dist/index-xEM_SoCl.d.ts.map +0 -1
- package/dist/interval-CWyxqDpE.d.ts +0 -43
- package/dist/interval-CWyxqDpE.d.ts.map +0 -1
- package/dist/llm-judge-DQJTQx01.js.map +0 -1
- package/dist/matrix-CqZSpwgN.d.ts.map +0 -1
- package/dist/outcome-observations-DL3CC8w1.js.map +0 -1
- package/dist/pareto-Bg75Gajo.js.map +0 -1
- package/dist/rollout-nodes-Vas6OSxX.js.map +0 -1
- package/dist/rubric-predictive-validity--6z0q8s6.js.map +0 -1
- package/dist/rubric-predictive-validity-C9wc7f_D.d.ts.map +0 -1
- package/dist/run-improvement-loop-BfwOUudy.d.ts.map +0 -1
- package/dist/run-record-Bqg0NZ_e.js.map +0 -1
- package/dist/runtime-rollouts-drQynu3t.js.map +0 -1
- package/dist/sequential-BphITa5b.js.map +0 -1
- package/dist/skillopt-optimization-method-D_swD0Cy.js.map +0 -1
- package/dist/statistics-DSidA5I2.js.map +0 -1
- package/dist/summary-report-CI4J6gxU.js.map +0 -1
- package/dist/systemone-DzY-2Zri.d.ts.map +0 -1
- package/dist/tool-groups-DF6OqhoG.d.ts.map +0 -1
- package/dist/tool-waste-CIhtU1ad.d.ts +0 -217
- package/dist/tool-waste-CIhtU1ad.d.ts.map +0 -1
- package/dist/types-BLv2Ngla.d.ts.map +0 -1
- package/dist/types-fuOwNq1C.d.ts.map +0 -1
|
@@ -0,0 +1,1356 @@
|
|
|
1
|
+
import { s as ValidationError } from "../errors-DeEEV9Vx.js";
|
|
2
|
+
import { a as hashCanonical } from "../canonical-DKgL_Wby.js";
|
|
3
|
+
import { E as zQuantile, S as makeRng, a as pairedSignTest, c as meanConfidenceInterval, w as seedFromData } from "../paired-tests-CXcD_B80.js";
|
|
4
|
+
// src/world/chat-agent.ts
|
|
5
|
+
/** The first user message of a session: all the agent is given. */
|
|
6
|
+
function worldSessionPrompt(session) {
|
|
7
|
+
const notes = Object.entries(session.notes);
|
|
8
|
+
return [
|
|
9
|
+
session.brief,
|
|
10
|
+
"",
|
|
11
|
+
`Today is ${session.date}: day ${session.day}, review ${session.index + 1}. ${session.daysLeft} days are left.`,
|
|
12
|
+
notes.length === 0 ? "You have saved no notes." : `Notes you saved earlier:\n${notes.map(([key, text]) => `[${key}]\n${text}`).join("\n\n")}`,
|
|
13
|
+
"",
|
|
14
|
+
"Read the state with the tools, act if the evidence supports it, and save what you will need next time. When you are done for this review, reply in plain text with what you did and why."
|
|
15
|
+
].join("\n");
|
|
16
|
+
}
|
|
17
|
+
/** An agent that plays each session as a tool loop against a chat model. */
|
|
18
|
+
function chatWorldAgent(opts) {
|
|
19
|
+
const maxTurns = opts.maxTurnsPerSession ?? 16;
|
|
20
|
+
const emptyRetries = opts.emptyRetries ?? 2;
|
|
21
|
+
if (!Number.isSafeInteger(maxTurns) || maxTurns < 1) throw new ValidationError("chatWorldAgent: maxTurnsPerSession must be a positive integer");
|
|
22
|
+
if (!Number.isSafeInteger(emptyRetries) || emptyRetries < 0) throw new ValidationError("chatWorldAgent: emptyRetries must be a non-negative integer");
|
|
23
|
+
return async (session) => {
|
|
24
|
+
const tools = session.tools.map((tool) => ({
|
|
25
|
+
type: "function",
|
|
26
|
+
function: {
|
|
27
|
+
name: tool.name,
|
|
28
|
+
description: tool.description,
|
|
29
|
+
parameters: tool.inputSchema
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
const messages = [{
|
|
33
|
+
role: "system",
|
|
34
|
+
content: opts.systemPrompt
|
|
35
|
+
}, {
|
|
36
|
+
role: "user",
|
|
37
|
+
content: (opts.sessionPrompt ?? worldSessionPrompt)(session)
|
|
38
|
+
}];
|
|
39
|
+
const transcript = {
|
|
40
|
+
session: session.index,
|
|
41
|
+
day: session.day,
|
|
42
|
+
messages,
|
|
43
|
+
responses: [],
|
|
44
|
+
ended: "turn_limit"
|
|
45
|
+
};
|
|
46
|
+
let emptyInARow = 0;
|
|
47
|
+
try {
|
|
48
|
+
while (transcript.responses.length < maxTurns) {
|
|
49
|
+
const response = await opts.client.chat({
|
|
50
|
+
...opts.model ? { model: opts.model } : {},
|
|
51
|
+
messages,
|
|
52
|
+
tools,
|
|
53
|
+
toolChoice: "auto",
|
|
54
|
+
...opts.maxTokens !== void 0 ? { maxTokens: opts.maxTokens } : {},
|
|
55
|
+
...opts.temperature !== void 0 ? { temperature: opts.temperature } : {}
|
|
56
|
+
});
|
|
57
|
+
const calls = response.toolCalls ?? [];
|
|
58
|
+
transcript.responses.push({
|
|
59
|
+
servedModel: response.servedModel ?? null,
|
|
60
|
+
finishReason: response.finishReason ?? null,
|
|
61
|
+
toolCalls: calls.length,
|
|
62
|
+
contentChars: response.content.length,
|
|
63
|
+
completionTokens: response.usage.completionTokens,
|
|
64
|
+
reasoningTokens: response.usage.reasoningTokens ?? null
|
|
65
|
+
});
|
|
66
|
+
if (calls.length === 0 && response.content.trim() === "") {
|
|
67
|
+
emptyInARow += 1;
|
|
68
|
+
if (emptyInARow > emptyRetries) {
|
|
69
|
+
transcript.ended = "empty";
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
emptyInARow = 0;
|
|
75
|
+
messages.push({
|
|
76
|
+
role: "assistant",
|
|
77
|
+
content: response.content,
|
|
78
|
+
...calls.length > 0 ? { toolCalls: calls } : {}
|
|
79
|
+
});
|
|
80
|
+
if (calls.length === 0) {
|
|
81
|
+
transcript.ended = "answered";
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
for (const call of calls) {
|
|
85
|
+
let input;
|
|
86
|
+
try {
|
|
87
|
+
input = call.argumentsJson.trim() === "" ? {} : JSON.parse(call.argumentsJson);
|
|
88
|
+
} catch {
|
|
89
|
+
input = call.argumentsJson;
|
|
90
|
+
}
|
|
91
|
+
const result = session.call(call.name, input);
|
|
92
|
+
messages.push({
|
|
93
|
+
role: "tool",
|
|
94
|
+
toolCallId: call.id,
|
|
95
|
+
content: JSON.stringify(result)
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
transcript.ended = "failed";
|
|
101
|
+
throw error;
|
|
102
|
+
} finally {
|
|
103
|
+
await opts.onSession?.(transcript);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// src/world/world.ts
|
|
108
|
+
const DAY_MS = 864e5;
|
|
109
|
+
const WEEK_DAYS = 7;
|
|
110
|
+
/** Stream slots at or above this belong to weekly shocks, below it to days. */
|
|
111
|
+
const WEEK_STREAM_OFFSET = 1e6;
|
|
112
|
+
const MAX_NOTES = 32;
|
|
113
|
+
const MAX_NOTE_CHARS = 8e3;
|
|
114
|
+
const MAX_REASON_CHARS = 500;
|
|
115
|
+
const NOTE_KEY = /^[A-Za-z0-9][A-Za-z0-9 _.-]{0,79}$/;
|
|
116
|
+
/** Digest of the whole scenario, key included: two episodes compare only when
|
|
117
|
+
* this matches. */
|
|
118
|
+
function worldScenarioDigest(scenario) {
|
|
119
|
+
return hashCanonical(scenario);
|
|
120
|
+
}
|
|
121
|
+
function finite(name, value, min, max = Number.POSITIVE_INFINITY) {
|
|
122
|
+
if (!Number.isFinite(value) || value < min || value > max) throw new ValidationError(`world scenario: ${name} must be within [${min}, ${max}]`);
|
|
123
|
+
}
|
|
124
|
+
/** Refuse a scenario the simulation cannot run as written. */
|
|
125
|
+
function validateWorldScenario(scenario) {
|
|
126
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(scenario.startDate) || Number.isNaN(dateMs(scenario.startDate))) throw new ValidationError("world scenario: startDate must be a YYYY-MM-DD date");
|
|
127
|
+
for (const name of ["horizonDays", "sessionEveryDays"]) if (!Number.isSafeInteger(scenario[name]) || scenario[name] < 1) throw new ValidationError(`world scenario: ${name} must be a positive integer`);
|
|
128
|
+
if (!Number.isSafeInteger(scenario.approvalDelayDays) || scenario.approvalDelayDays < 0) throw new ValidationError("world scenario: approvalDelayDays must be a non-negative integer");
|
|
129
|
+
finite("budget", scenario.budget, 0);
|
|
130
|
+
finite("outcome.value", scenario.outcome.value, 0);
|
|
131
|
+
if (scenario.levers.length === 0) throw new ValidationError("world scenario: at least one lever is required");
|
|
132
|
+
const ids = /* @__PURE__ */ new Set();
|
|
133
|
+
for (const lever of scenario.levers) {
|
|
134
|
+
if (!/^[a-z][a-z0-9_]*$/.test(lever.id) || ids.has(lever.id)) throw new ValidationError(`world scenario: lever id '${lever.id}' is invalid or repeated`);
|
|
135
|
+
ids.add(lever.id);
|
|
136
|
+
finite(`${lever.id}.maxRate`, lever.maxRate, 0);
|
|
137
|
+
finite(`${lever.id}.guidelineRate`, lever.guidelineRate, 0, lever.maxRate);
|
|
138
|
+
finite(`${lever.id}.startRate`, lever.startRate, 0, lever.maxRate);
|
|
139
|
+
finite(`${lever.id}.resultLagDays.from`, lever.resultLagDays.from, 0);
|
|
140
|
+
finite(`${lever.id}.resultLagDays.to`, lever.resultLagDays.to, lever.resultLagDays.from);
|
|
141
|
+
const key = scenario.key.levers[lever.id];
|
|
142
|
+
if (!key) throw new ValidationError(`world scenario: the key has no lever '${lever.id}'`);
|
|
143
|
+
finite(`${lever.id}.yield`, key.yield, 0);
|
|
144
|
+
finite(`${lever.id}.weeklyNoise`, key.weeklyNoise, 0, 3);
|
|
145
|
+
finite(`${lever.id}.rampDays`, key.rampDays, 1);
|
|
146
|
+
finite(`${lever.id}.saturationRate`, key.saturationRate, 0);
|
|
147
|
+
finite(`${lever.id}.marginalYield`, key.marginalYield, 0, 1);
|
|
148
|
+
finite(`${lever.id}.delayDays.min`, key.delayDays.min, 1);
|
|
149
|
+
finite(`${lever.id}.delayDays.median`, key.delayDays.median, key.delayDays.min);
|
|
150
|
+
finite(`${lever.id}.delayDays.max`, key.delayDays.max, key.delayDays.median);
|
|
151
|
+
if (!Number.isSafeInteger(key.delayDays.min) || !Number.isSafeInteger(key.delayDays.max)) throw new ValidationError(`world scenario: ${lever.id}.delayDays min and max must be whole days`);
|
|
152
|
+
finite(`${lever.id} daily outcomes at maxRate`, lever.maxRate * key.yield, 0, 1e4);
|
|
153
|
+
if (key.wear) {
|
|
154
|
+
finite(`${lever.id}.wear.weeklyLoad`, key.wear.weeklyLoad, Number.MIN_VALUE);
|
|
155
|
+
finite(`${lever.id}.wear.damagePerDay`, key.wear.damagePerDay, 0, 1);
|
|
156
|
+
finite(`${lever.id}.wear.recoveryPerDay`, key.wear.recoveryPerDay, 0, 1);
|
|
157
|
+
finite(`${lever.id}.wear.floor`, key.wear.floor, 0, 1);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const id of Object.keys(scenario.key.levers)) if (!ids.has(id)) throw new ValidationError(`world scenario: the key names no lever '${id}'`);
|
|
161
|
+
finite("key.evidence.minOutcomes", scenario.key.evidence.minOutcomes, 0);
|
|
162
|
+
finite("key.evidence.scaleFactor", scenario.key.evidence.scaleFactor, 1);
|
|
163
|
+
}
|
|
164
|
+
function dateMs(date) {
|
|
165
|
+
return Date.parse(`${date}T00:00:00.000Z`);
|
|
166
|
+
}
|
|
167
|
+
function standardNormal(rng) {
|
|
168
|
+
return Math.sqrt(-2 * Math.log(1 - rng())) * Math.cos(2 * Math.PI * rng());
|
|
169
|
+
}
|
|
170
|
+
function poisson(mean, rng) {
|
|
171
|
+
if (mean <= 0) return 0;
|
|
172
|
+
if (mean >= 30) return Math.max(0, Math.round(mean + Math.sqrt(mean) * standardNormal(rng)));
|
|
173
|
+
const limit = Math.exp(-mean);
|
|
174
|
+
let count = 0;
|
|
175
|
+
let product = rng();
|
|
176
|
+
while (product > limit) {
|
|
177
|
+
count += 1;
|
|
178
|
+
product *= rng();
|
|
179
|
+
}
|
|
180
|
+
return count;
|
|
181
|
+
}
|
|
182
|
+
function round2(value) {
|
|
183
|
+
return Math.round(value * 100) / 100;
|
|
184
|
+
}
|
|
185
|
+
var World = class World {
|
|
186
|
+
scenario;
|
|
187
|
+
seed;
|
|
188
|
+
scenarioDigest;
|
|
189
|
+
state;
|
|
190
|
+
constructor(scenario, seed) {
|
|
191
|
+
validateWorldScenario(scenario);
|
|
192
|
+
if (!Number.isSafeInteger(seed)) throw new ValidationError(`world: seed must be a safe integer, got ${seed}`);
|
|
193
|
+
this.scenario = scenario;
|
|
194
|
+
this.seed = seed;
|
|
195
|
+
this.scenarioDigest = worldScenarioDigest(scenario);
|
|
196
|
+
const levers = {};
|
|
197
|
+
for (const lever of scenario.levers) levers[lever.id] = {
|
|
198
|
+
rate: 0,
|
|
199
|
+
rateAtDayStart: 0,
|
|
200
|
+
age: 0,
|
|
201
|
+
health: 1,
|
|
202
|
+
spent: 0,
|
|
203
|
+
produced: 0,
|
|
204
|
+
landed: 0,
|
|
205
|
+
approvedMaxRate: 0,
|
|
206
|
+
overdriveDays: 0,
|
|
207
|
+
spendByDay: [],
|
|
208
|
+
landedByDay: [],
|
|
209
|
+
landing: [],
|
|
210
|
+
onceByDay: {}
|
|
211
|
+
};
|
|
212
|
+
this.state = {
|
|
213
|
+
day: 0,
|
|
214
|
+
budget: scenario.budget,
|
|
215
|
+
levers,
|
|
216
|
+
approvals: [],
|
|
217
|
+
notes: {}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/** Rebuild a world from `snapshot()`. The scenario and seed must be the ones it was taken from. */
|
|
221
|
+
static restore(scenario, snapshot) {
|
|
222
|
+
const world = new World(scenario, snapshot.seed);
|
|
223
|
+
if (snapshot.scenarioDigest !== world.scenarioDigest) throw new ValidationError("world: the snapshot was taken from a different scenario");
|
|
224
|
+
world.state = structuredClone(snapshot.state);
|
|
225
|
+
return world;
|
|
226
|
+
}
|
|
227
|
+
snapshot() {
|
|
228
|
+
return {
|
|
229
|
+
scenarioDigest: this.scenarioDigest,
|
|
230
|
+
seed: this.seed,
|
|
231
|
+
state: structuredClone(this.state)
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
get day() {
|
|
235
|
+
return this.state.day;
|
|
236
|
+
}
|
|
237
|
+
get daysLeft() {
|
|
238
|
+
return this.scenario.horizonDays - this.state.day;
|
|
239
|
+
}
|
|
240
|
+
get over() {
|
|
241
|
+
return this.state.day >= this.scenario.horizonDays;
|
|
242
|
+
}
|
|
243
|
+
/** Calendar date of a day, by default today. */
|
|
244
|
+
date(day = this.state.day) {
|
|
245
|
+
return new Date(dateMs(this.scenario.startDate) + day * DAY_MS).toISOString().slice(0, 10);
|
|
246
|
+
}
|
|
247
|
+
/** The simulated instant: midnight UTC at the start of today. */
|
|
248
|
+
now() {
|
|
249
|
+
return new Date(dateMs(this.scenario.startDate) + this.state.day * DAY_MS);
|
|
250
|
+
}
|
|
251
|
+
/** Run the world forward. Time moves only here, and stops at the horizon. */
|
|
252
|
+
advance(days) {
|
|
253
|
+
if (!Number.isSafeInteger(days) || days < 0) throw new ValidationError(`world: advance takes a non-negative integer, got ${days}`);
|
|
254
|
+
for (let i = 0; i < days && !this.over; i++) {
|
|
255
|
+
this.simulateDay(this.state.day);
|
|
256
|
+
this.state.day += 1;
|
|
257
|
+
this.decideApprovals();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
get notes() {
|
|
261
|
+
return { ...this.state.notes };
|
|
262
|
+
}
|
|
263
|
+
status() {
|
|
264
|
+
const { scenario, state } = this;
|
|
265
|
+
let spent = 0;
|
|
266
|
+
let outcomes = 0;
|
|
267
|
+
const levers = scenario.levers.map((lever) => {
|
|
268
|
+
const s = state.levers[lever.id];
|
|
269
|
+
spent += s.spent;
|
|
270
|
+
outcomes += s.landed;
|
|
271
|
+
return {
|
|
272
|
+
...lever,
|
|
273
|
+
resultLagDays: { ...lever.resultLagDays },
|
|
274
|
+
dailyRate: s.rate,
|
|
275
|
+
...lever.requiresApproval ? { approvedMaxRate: s.approvedMaxRate } : {},
|
|
276
|
+
spent: round2(s.spent),
|
|
277
|
+
outcomes: s.landed
|
|
278
|
+
};
|
|
279
|
+
});
|
|
280
|
+
return {
|
|
281
|
+
day: state.day,
|
|
282
|
+
date: this.date(),
|
|
283
|
+
daysLeft: this.daysLeft,
|
|
284
|
+
horizonDays: scenario.horizonDays,
|
|
285
|
+
sessionEveryDays: scenario.sessionEveryDays,
|
|
286
|
+
budgetLeft: round2(state.budget),
|
|
287
|
+
spent: round2(spent),
|
|
288
|
+
outcomes,
|
|
289
|
+
outcomeUnit: scenario.outcome.unit,
|
|
290
|
+
outcomeValue: scenario.outcome.value,
|
|
291
|
+
currency: scenario.currency,
|
|
292
|
+
approvalDelayDays: scenario.approvalDelayDays,
|
|
293
|
+
levers,
|
|
294
|
+
approvals: state.approvals.map((approval) => ({ ...approval }))
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** Spend and landed outcomes for every finished day, by week or by day. */
|
|
298
|
+
results(opts = {}) {
|
|
299
|
+
const span = opts.by === "day" ? 1 : WEEK_DAYS;
|
|
300
|
+
const rows = [];
|
|
301
|
+
for (const lever of this.scenario.levers) {
|
|
302
|
+
if (opts.lever !== void 0 && opts.lever !== lever.id) continue;
|
|
303
|
+
const s = this.state.levers[lever.id];
|
|
304
|
+
for (let from = 0; from < this.state.day; from += span) {
|
|
305
|
+
const to = Math.min(from + span, this.state.day);
|
|
306
|
+
let spend = 0;
|
|
307
|
+
let outcomes = 0;
|
|
308
|
+
for (let day = from; day < to; day++) {
|
|
309
|
+
spend += s.spendByDay[day] ?? 0;
|
|
310
|
+
outcomes += s.landedByDay[day] ?? 0;
|
|
311
|
+
}
|
|
312
|
+
rows.push({
|
|
313
|
+
lever: lever.id,
|
|
314
|
+
fromDay: from,
|
|
315
|
+
toDay: to - 1,
|
|
316
|
+
fromDate: this.date(from),
|
|
317
|
+
spend: round2(spend),
|
|
318
|
+
outcomes
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return rows;
|
|
323
|
+
}
|
|
324
|
+
/** Set a lever's standing daily spend, from today on. */
|
|
325
|
+
setRate(leverId, dailyRate) {
|
|
326
|
+
const planned = dailyRate + this.onceToday(leverId);
|
|
327
|
+
const raises = dailyRate > (this.state.levers[leverId]?.rate ?? 0);
|
|
328
|
+
const checked = this.spendCheck(leverId, dailyRate, planned, raises);
|
|
329
|
+
if (!checked.ok) return checked;
|
|
330
|
+
const s = checked.state;
|
|
331
|
+
const evidence = this.evidence(leverId);
|
|
332
|
+
s.rate = dailyRate;
|
|
333
|
+
return {
|
|
334
|
+
ok: true,
|
|
335
|
+
message: `${leverId} runs at ${dailyRate} ${this.scenario.currency} a day from ${this.date()}.`,
|
|
336
|
+
action: {
|
|
337
|
+
kind: "set_rate",
|
|
338
|
+
day: this.state.day,
|
|
339
|
+
lever: leverId,
|
|
340
|
+
dailyRate
|
|
341
|
+
},
|
|
342
|
+
evidence
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/** Add a one-off spend to today, on top of the lever's standing rate. */
|
|
346
|
+
spendOnce(leverId, amount) {
|
|
347
|
+
const planned = (this.state.levers[leverId]?.rate ?? 0) + this.onceToday(leverId) + amount;
|
|
348
|
+
const checked = this.spendCheck(leverId, amount, planned, true);
|
|
349
|
+
if (!checked.ok) return checked;
|
|
350
|
+
if (!(amount > 0)) return {
|
|
351
|
+
ok: false,
|
|
352
|
+
refused: "invalid_input",
|
|
353
|
+
message: "amount must be above zero."
|
|
354
|
+
};
|
|
355
|
+
let placed = 0;
|
|
356
|
+
for (const lever of this.scenario.levers) placed += this.onceToday(lever.id);
|
|
357
|
+
if (placed + amount > this.state.budget) return {
|
|
358
|
+
ok: false,
|
|
359
|
+
refused: "insufficient_budget",
|
|
360
|
+
message: `The budget has ${round2(this.state.budget - placed)} ${this.scenario.currency} not already placed today.`
|
|
361
|
+
};
|
|
362
|
+
const today = String(this.state.day);
|
|
363
|
+
checked.state.onceByDay[today] = this.onceToday(leverId) + amount;
|
|
364
|
+
return {
|
|
365
|
+
ok: true,
|
|
366
|
+
message: `${leverId} spends an extra ${amount} ${this.scenario.currency} on ${this.date()}.`,
|
|
367
|
+
action: {
|
|
368
|
+
kind: "spend_once",
|
|
369
|
+
day: this.state.day,
|
|
370
|
+
lever: leverId,
|
|
371
|
+
amount
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
/** Ask for approval to run a gated lever up to a daily rate. The decision
|
|
376
|
+
* lands `approvalDelayDays` later. */
|
|
377
|
+
requestApproval(leverId, maxDailyRate, reason) {
|
|
378
|
+
if (this.over) return {
|
|
379
|
+
ok: false,
|
|
380
|
+
refused: "episode_over",
|
|
381
|
+
message: "The episode is over."
|
|
382
|
+
};
|
|
383
|
+
const lever = this.scenario.levers.find((candidate) => candidate.id === leverId);
|
|
384
|
+
if (!lever) return {
|
|
385
|
+
ok: false,
|
|
386
|
+
refused: "unknown_lever",
|
|
387
|
+
message: `No lever '${leverId}'.`
|
|
388
|
+
};
|
|
389
|
+
if (!Number.isFinite(maxDailyRate) || maxDailyRate <= 0) return {
|
|
390
|
+
ok: false,
|
|
391
|
+
refused: "invalid_input",
|
|
392
|
+
message: "maxDailyRate must be above zero."
|
|
393
|
+
};
|
|
394
|
+
if (reason.length > MAX_REASON_CHARS) return {
|
|
395
|
+
ok: false,
|
|
396
|
+
refused: "invalid_input",
|
|
397
|
+
message: `reason holds at most ${MAX_REASON_CHARS} characters.`
|
|
398
|
+
};
|
|
399
|
+
if (maxDailyRate > lever.maxRate) return {
|
|
400
|
+
ok: false,
|
|
401
|
+
refused: "over_max_rate",
|
|
402
|
+
message: `${leverId} cannot run above ${lever.maxRate} ${this.scenario.currency} a day.`
|
|
403
|
+
};
|
|
404
|
+
const approval = {
|
|
405
|
+
id: `approval-${this.state.approvals.length + 1}`,
|
|
406
|
+
lever: leverId,
|
|
407
|
+
maxDailyRate,
|
|
408
|
+
reason,
|
|
409
|
+
requestedDay: this.state.day,
|
|
410
|
+
decisionDay: this.state.day + this.scenario.approvalDelayDays,
|
|
411
|
+
status: "pending"
|
|
412
|
+
};
|
|
413
|
+
this.state.approvals.push(approval);
|
|
414
|
+
this.decideApprovals();
|
|
415
|
+
return {
|
|
416
|
+
ok: true,
|
|
417
|
+
message: approval.status === "granted" ? `${approval.id} is granted.` : `${approval.id} is pending; the decision lands on ${this.date(approval.decisionDay)}.`,
|
|
418
|
+
action: {
|
|
419
|
+
kind: "request_approval",
|
|
420
|
+
day: this.state.day,
|
|
421
|
+
lever: leverId,
|
|
422
|
+
maxDailyRate,
|
|
423
|
+
reason
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/** Keep a note for later sessions. Notes are the only memory the world carries. */
|
|
428
|
+
saveNote(key, text) {
|
|
429
|
+
if (!NOTE_KEY.test(key)) return {
|
|
430
|
+
ok: false,
|
|
431
|
+
message: "key must be 1 to 80 letters, digits, spaces, dots, dashes or underscores, starting with a letter or digit."
|
|
432
|
+
};
|
|
433
|
+
if (text.length > MAX_NOTE_CHARS) return {
|
|
434
|
+
ok: false,
|
|
435
|
+
message: `A note holds at most ${MAX_NOTE_CHARS} characters.`
|
|
436
|
+
};
|
|
437
|
+
if (!Object.hasOwn(this.state.notes, key) && Object.keys(this.state.notes).length >= MAX_NOTES) return {
|
|
438
|
+
ok: false,
|
|
439
|
+
message: `At most ${MAX_NOTES} notes are kept.`
|
|
440
|
+
};
|
|
441
|
+
if (text === "") delete this.state.notes[key];
|
|
442
|
+
else this.state.notes[key] = text;
|
|
443
|
+
return { ok: true };
|
|
444
|
+
}
|
|
445
|
+
/** Apply a recorded action. Used to replay an episode. */
|
|
446
|
+
apply(action) {
|
|
447
|
+
if (action.kind === "set_rate") return this.setRate(action.lever, action.dailyRate);
|
|
448
|
+
if (action.kind === "spend_once") return this.spendOnce(action.lever, action.amount);
|
|
449
|
+
return this.requestApproval(action.lever, action.maxDailyRate, action.reason);
|
|
450
|
+
}
|
|
451
|
+
/** How much a lever's results can say so far. Hidden from the agent. */
|
|
452
|
+
evidence(leverId) {
|
|
453
|
+
const s = this.state.levers[leverId];
|
|
454
|
+
const key = this.scenario.key.levers[leverId];
|
|
455
|
+
if (!s || !key) throw new ValidationError(`world: no lever '${leverId}'`);
|
|
456
|
+
let maturedSpend = 0;
|
|
457
|
+
for (let day = 0; day + key.delayDays.max < this.state.day; day++) maturedSpend += s.spendByDay[day] ?? 0;
|
|
458
|
+
return {
|
|
459
|
+
lever: leverId,
|
|
460
|
+
rateAtDayStart: s.rateAtDayStart,
|
|
461
|
+
landed: s.landed,
|
|
462
|
+
maturedSpend
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
end() {
|
|
466
|
+
const levers = {};
|
|
467
|
+
let spent = 0;
|
|
468
|
+
let outcomes = 0;
|
|
469
|
+
for (const lever of this.scenario.levers) {
|
|
470
|
+
const s = this.state.levers[lever.id];
|
|
471
|
+
spent += s.spent;
|
|
472
|
+
outcomes += s.landed;
|
|
473
|
+
levers[lever.id] = {
|
|
474
|
+
rate: s.rate,
|
|
475
|
+
health: s.health,
|
|
476
|
+
spent: s.spent,
|
|
477
|
+
landed: s.landed,
|
|
478
|
+
inFlight: s.produced - s.landed,
|
|
479
|
+
overdriveDays: s.overdriveDays
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
return {
|
|
483
|
+
day: this.state.day,
|
|
484
|
+
budgetLeft: this.state.budget,
|
|
485
|
+
spent,
|
|
486
|
+
outcomes,
|
|
487
|
+
value: this.state.budget + outcomes * this.scenario.outcome.value,
|
|
488
|
+
levers,
|
|
489
|
+
approvals: this.state.approvals.map((approval) => ({ ...approval }))
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
onceToday(leverId) {
|
|
493
|
+
return this.state.levers[leverId]?.onceByDay[String(this.state.day)] ?? 0;
|
|
494
|
+
}
|
|
495
|
+
/** `plannedDailySpend` is everything the lever would spend today: its
|
|
496
|
+
* standing rate and its one-offs. A change that only lowers spend passes
|
|
497
|
+
* the cap and the approval check. */
|
|
498
|
+
spendCheck(leverId, amount, plannedDailySpend, raises) {
|
|
499
|
+
if (this.over) return {
|
|
500
|
+
ok: false,
|
|
501
|
+
refused: "episode_over",
|
|
502
|
+
message: "The episode is over."
|
|
503
|
+
};
|
|
504
|
+
const lever = this.scenario.levers.find((candidate) => candidate.id === leverId);
|
|
505
|
+
const state = this.state.levers[leverId];
|
|
506
|
+
if (!lever || !state) return {
|
|
507
|
+
ok: false,
|
|
508
|
+
refused: "unknown_lever",
|
|
509
|
+
message: `No lever '${leverId}'.`
|
|
510
|
+
};
|
|
511
|
+
if (!Number.isFinite(amount) || amount < 0) return {
|
|
512
|
+
ok: false,
|
|
513
|
+
refused: "invalid_input",
|
|
514
|
+
message: "Spend must be zero or more."
|
|
515
|
+
};
|
|
516
|
+
if (!raises) return {
|
|
517
|
+
ok: true,
|
|
518
|
+
state
|
|
519
|
+
};
|
|
520
|
+
if (plannedDailySpend > lever.maxRate) return {
|
|
521
|
+
ok: false,
|
|
522
|
+
refused: "over_max_rate",
|
|
523
|
+
message: `${leverId} cannot spend more than ${lever.maxRate} ${this.scenario.currency} in a day.`
|
|
524
|
+
};
|
|
525
|
+
if (lever.requiresApproval && plannedDailySpend > state.approvedMaxRate) return {
|
|
526
|
+
ok: false,
|
|
527
|
+
refused: "approval_missing",
|
|
528
|
+
message: state.approvedMaxRate > 0 ? `${leverId} is approved up to ${state.approvedMaxRate} ${this.scenario.currency} a day. Request approval for more first.` : `${leverId} needs a granted approval before it can spend. Request one first.`
|
|
529
|
+
};
|
|
530
|
+
return {
|
|
531
|
+
ok: true,
|
|
532
|
+
state
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
decideApprovals() {
|
|
536
|
+
for (const approval of this.state.approvals) {
|
|
537
|
+
if (approval.status !== "pending" || approval.decisionDay > this.state.day) continue;
|
|
538
|
+
approval.status = "granted";
|
|
539
|
+
const s = this.state.levers[approval.lever];
|
|
540
|
+
s.approvedMaxRate = Math.max(s.approvedMaxRate, approval.maxDailyRate);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
stream(leverIndex, slot) {
|
|
544
|
+
return makeRng(seedFromData([[
|
|
545
|
+
this.seed,
|
|
546
|
+
leverIndex,
|
|
547
|
+
slot
|
|
548
|
+
]]));
|
|
549
|
+
}
|
|
550
|
+
weekShock(leverIndex, key, day) {
|
|
551
|
+
if (key.weeklyNoise === 0) return 1;
|
|
552
|
+
const week = Math.floor(day / WEEK_DAYS);
|
|
553
|
+
const z = standardNormal(this.stream(leverIndex, WEEK_STREAM_OFFSET + week));
|
|
554
|
+
return Math.exp(key.weeklyNoise * z - key.weeklyNoise ** 2 / 2);
|
|
555
|
+
}
|
|
556
|
+
simulateDay(day) {
|
|
557
|
+
const { scenario, state } = this;
|
|
558
|
+
scenario.levers.forEach((lever, leverIndex) => {
|
|
559
|
+
const s = state.levers[lever.id];
|
|
560
|
+
const key = scenario.key.levers[lever.id];
|
|
561
|
+
const landedToday = s.landing[day] ?? 0;
|
|
562
|
+
s.landedByDay[day] = landedToday;
|
|
563
|
+
s.landed += landedToday;
|
|
564
|
+
const planned = s.rate + (s.onceByDay[String(day)] ?? 0);
|
|
565
|
+
const spend = Math.min(planned, state.budget);
|
|
566
|
+
state.budget -= spend;
|
|
567
|
+
s.spent += spend;
|
|
568
|
+
s.spendByDay[day] = spend;
|
|
569
|
+
s.age = spend > 0 ? s.age + 1 : 0;
|
|
570
|
+
if (spend > 0) {
|
|
571
|
+
const effective = spend <= key.saturationRate ? spend : key.saturationRate + (spend - key.saturationRate) * key.marginalYield;
|
|
572
|
+
const ramp = Math.min(1, s.age / key.rampDays);
|
|
573
|
+
const mean = effective * key.yield * ramp * s.health * this.weekShock(leverIndex, key, day);
|
|
574
|
+
const rng = this.stream(leverIndex, day);
|
|
575
|
+
const count = poisson(mean, rng);
|
|
576
|
+
const { min, median, max } = key.delayDays;
|
|
577
|
+
const spread = max > median ? Math.log(max / median) / 2 : 0;
|
|
578
|
+
for (let i = 0; i < count; i++) {
|
|
579
|
+
const delay = Math.round(median * Math.exp(spread * standardNormal(rng)));
|
|
580
|
+
const lands = day + Math.min(max, Math.max(min, delay));
|
|
581
|
+
s.landing[lands] = (s.landing[lands] ?? 0) + 1;
|
|
582
|
+
}
|
|
583
|
+
s.produced += count;
|
|
584
|
+
}
|
|
585
|
+
s.rateAtDayStart = s.rate;
|
|
586
|
+
if (key.wear) {
|
|
587
|
+
let load = 0;
|
|
588
|
+
for (let back = 0; back < WEEK_DAYS; back++) load += s.spendByDay[day - back] ?? 0;
|
|
589
|
+
if (load > key.wear.weeklyLoad) {
|
|
590
|
+
s.overdriveDays += 1;
|
|
591
|
+
const excess = load / key.wear.weeklyLoad - 1;
|
|
592
|
+
s.health = Math.max(key.wear.floor, s.health - key.wear.damagePerDay * excess);
|
|
593
|
+
} else s.health = Math.min(1, s.health + key.wear.recoveryPerDay);
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
// src/world/score.ts
|
|
599
|
+
/** The accepted actions of a log, in the order they were taken. */
|
|
600
|
+
function worldActions(log) {
|
|
601
|
+
return log.flatMap((entry) => entry.action ? [entry.action] : []);
|
|
602
|
+
}
|
|
603
|
+
/** Re-run a scenario and seed under a list of actions. An action the world
|
|
604
|
+
* refuses on replay is skipped, as it would have been live. */
|
|
605
|
+
function replayWorld(scenario, seed, actions) {
|
|
606
|
+
const world = new World(scenario, seed);
|
|
607
|
+
for (const action of actions) {
|
|
608
|
+
if (action.day > world.day) world.advance(action.day - world.day);
|
|
609
|
+
world.apply(action);
|
|
610
|
+
}
|
|
611
|
+
world.advance(world.daysLeft);
|
|
612
|
+
return world.end();
|
|
613
|
+
}
|
|
614
|
+
function prematureDecisions(scenario, log) {
|
|
615
|
+
const rule = scenario.key.evidence;
|
|
616
|
+
const net = /* @__PURE__ */ new Map();
|
|
617
|
+
for (const entry of log) if (entry.action?.kind === "set_rate" && entry.evidence) net.set(`${entry.action.day}:${entry.action.lever}`, entry);
|
|
618
|
+
const found = [];
|
|
619
|
+
for (const entry of net.values()) {
|
|
620
|
+
if (entry.action?.kind !== "set_rate" || !entry.evidence) continue;
|
|
621
|
+
const { rateAtDayStart, landed, maturedSpend } = entry.evidence;
|
|
622
|
+
if (rateAtDayStart <= 0) continue;
|
|
623
|
+
const next = entry.action.dailyRate;
|
|
624
|
+
const kind = next <= rateAtDayStart / rule.scaleFactor ? "cut" : next >= rateAtDayStart * rule.scaleFactor ? "scale" : null;
|
|
625
|
+
if (!kind) continue;
|
|
626
|
+
const exposure = maturedSpend / scenario.outcome.value;
|
|
627
|
+
if (landed >= rule.minOutcomes || exposure >= rule.minOutcomes) continue;
|
|
628
|
+
found.push({
|
|
629
|
+
seq: entry.seq,
|
|
630
|
+
day: entry.day,
|
|
631
|
+
lever: entry.action.lever,
|
|
632
|
+
kind,
|
|
633
|
+
landed,
|
|
634
|
+
maturedSpend
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
return found.sort((a, b) => a.seq - b.seq);
|
|
638
|
+
}
|
|
639
|
+
/** Price every accepted action of an episode by replaying the episode without
|
|
640
|
+
* it, costliest in expectation first. The other actions stay as recorded, so
|
|
641
|
+
* a removed rate change leaves the lever at its previous rate until the next
|
|
642
|
+
* recorded change. */
|
|
643
|
+
function worldDecisionCosts(scenario, episode, opts = {}) {
|
|
644
|
+
const noiseSeeds = opts.noiseSeeds ?? 20;
|
|
645
|
+
if (!Number.isSafeInteger(noiseSeeds) || noiseSeeds < 1) throw new ValidationError("worldDecisionCosts: noiseSeeds must be a positive integer");
|
|
646
|
+
const taken = episode.log.filter((entry) => entry.action !== void 0);
|
|
647
|
+
const all = taken.map((entry) => entry.action);
|
|
648
|
+
const fresh = Array.from({ length: noiseSeeds }, (_, index) => seedFromData([[episode.seed, index + 1]]));
|
|
649
|
+
const realizedBase = replayWorld(scenario, episode.seed, all).value;
|
|
650
|
+
const freshBase = fresh.map((seed) => replayWorld(scenario, seed, all).value);
|
|
651
|
+
return taken.map((entry, index) => {
|
|
652
|
+
const without = all.filter((_, at) => at !== index);
|
|
653
|
+
const expected = fresh.map((seed, at) => replayWorld(scenario, seed, without).value - freshBase[at]);
|
|
654
|
+
return {
|
|
655
|
+
seq: entry.seq,
|
|
656
|
+
day: entry.day,
|
|
657
|
+
action: entry.action,
|
|
658
|
+
realizedCost: replayWorld(scenario, episode.seed, without).value - realizedBase,
|
|
659
|
+
expectedCost: expected.reduce((sum, cost) => sum + cost, 0) / expected.length
|
|
660
|
+
};
|
|
661
|
+
}).sort((x, y) => y.expectedCost - x.expectedCost || x.seq - y.seq);
|
|
662
|
+
}
|
|
663
|
+
/** Score an episode from its end state and its decision log. */
|
|
664
|
+
function scoreWorldEpisode(scenario, sessions, log, end) {
|
|
665
|
+
const refused = {};
|
|
666
|
+
for (const entry of log) {
|
|
667
|
+
const reason = entry.result.refused;
|
|
668
|
+
if (reason) refused[reason] = (refused[reason] ?? 0) + 1;
|
|
669
|
+
}
|
|
670
|
+
let overdriveDays = 0;
|
|
671
|
+
let inFlight = 0;
|
|
672
|
+
for (const lever of Object.values(end.levers)) {
|
|
673
|
+
overdriveDays += lever.overdriveDays;
|
|
674
|
+
inFlight += lever.inFlight;
|
|
675
|
+
}
|
|
676
|
+
return {
|
|
677
|
+
value: end.value,
|
|
678
|
+
gain: end.value - scenario.budget,
|
|
679
|
+
refused,
|
|
680
|
+
prematureDecisions: prematureDecisions(scenario, log),
|
|
681
|
+
overdriveDays,
|
|
682
|
+
lateValue: inFlight * scenario.outcome.value,
|
|
683
|
+
failedSessions: sessions.filter((session) => session.error !== void 0).length
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
// src/world/tools.ts
|
|
687
|
+
/** Tool definitions for a scenario. Built from its public half only. */
|
|
688
|
+
function worldTools(scenario) {
|
|
689
|
+
const lever = {
|
|
690
|
+
type: "string",
|
|
691
|
+
enum: scenario.levers.map((lever) => lever.id),
|
|
692
|
+
description: "Lever id."
|
|
693
|
+
};
|
|
694
|
+
return [
|
|
695
|
+
{
|
|
696
|
+
name: "status",
|
|
697
|
+
description: "Today's date, days left, budget left, every lever with its current daily rate, guideline and approval state, and every approval request.",
|
|
698
|
+
inputSchema: {
|
|
699
|
+
type: "object",
|
|
700
|
+
properties: {},
|
|
701
|
+
additionalProperties: false
|
|
702
|
+
}
|
|
703
|
+
},
|
|
704
|
+
{
|
|
705
|
+
name: "results",
|
|
706
|
+
description: `Spend and landed ${scenario.outcome.unit} for each finished period. Outcomes are counted in the period they landed in, which is later than the spend that caused them.`,
|
|
707
|
+
inputSchema: {
|
|
708
|
+
type: "object",
|
|
709
|
+
properties: {
|
|
710
|
+
lever: {
|
|
711
|
+
...lever,
|
|
712
|
+
description: "Limit to one lever. Default: all."
|
|
713
|
+
},
|
|
714
|
+
by: {
|
|
715
|
+
type: "string",
|
|
716
|
+
enum: ["week", "day"],
|
|
717
|
+
description: "Period length. Default: week."
|
|
718
|
+
}
|
|
719
|
+
},
|
|
720
|
+
additionalProperties: false
|
|
721
|
+
}
|
|
722
|
+
},
|
|
723
|
+
{
|
|
724
|
+
name: "set_rate",
|
|
725
|
+
description: `Set a lever's standing daily spend in ${scenario.currency}, from today on. Zero stops it.`,
|
|
726
|
+
inputSchema: {
|
|
727
|
+
type: "object",
|
|
728
|
+
properties: {
|
|
729
|
+
lever,
|
|
730
|
+
dailyRate: {
|
|
731
|
+
type: "number",
|
|
732
|
+
minimum: 0
|
|
733
|
+
}
|
|
734
|
+
},
|
|
735
|
+
required: ["lever", "dailyRate"],
|
|
736
|
+
additionalProperties: false
|
|
737
|
+
}
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
name: "spend_once",
|
|
741
|
+
description: `Spend an extra amount in ${scenario.currency} on a lever today only, on top of its standing rate.`,
|
|
742
|
+
inputSchema: {
|
|
743
|
+
type: "object",
|
|
744
|
+
properties: {
|
|
745
|
+
lever,
|
|
746
|
+
amount: {
|
|
747
|
+
type: "number",
|
|
748
|
+
exclusiveMinimum: 0
|
|
749
|
+
}
|
|
750
|
+
},
|
|
751
|
+
required: ["lever", "amount"],
|
|
752
|
+
additionalProperties: false
|
|
753
|
+
}
|
|
754
|
+
},
|
|
755
|
+
{
|
|
756
|
+
name: "request_approval",
|
|
757
|
+
description: `Ask for approval to run a lever up to a daily rate. Levers marked requiresApproval refuse spend that no granted approval covers. A decision takes ${scenario.approvalDelayDays} day(s).`,
|
|
758
|
+
inputSchema: {
|
|
759
|
+
type: "object",
|
|
760
|
+
properties: {
|
|
761
|
+
lever,
|
|
762
|
+
maxDailyRate: {
|
|
763
|
+
type: "number",
|
|
764
|
+
exclusiveMinimum: 0
|
|
765
|
+
},
|
|
766
|
+
reason: { type: "string" }
|
|
767
|
+
},
|
|
768
|
+
required: [
|
|
769
|
+
"lever",
|
|
770
|
+
"maxDailyRate",
|
|
771
|
+
"reason"
|
|
772
|
+
],
|
|
773
|
+
additionalProperties: false
|
|
774
|
+
}
|
|
775
|
+
},
|
|
776
|
+
{
|
|
777
|
+
name: "save_note",
|
|
778
|
+
description: "Save a note under a key. Notes are the only thing carried into later sessions; saving an empty text deletes the key.",
|
|
779
|
+
inputSchema: {
|
|
780
|
+
type: "object",
|
|
781
|
+
properties: {
|
|
782
|
+
key: { type: "string" },
|
|
783
|
+
text: { type: "string" }
|
|
784
|
+
},
|
|
785
|
+
required: ["key", "text"],
|
|
786
|
+
additionalProperties: false
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
];
|
|
790
|
+
}
|
|
791
|
+
function refusedInput(message) {
|
|
792
|
+
return { result: {
|
|
793
|
+
ok: false,
|
|
794
|
+
refused: "invalid_input",
|
|
795
|
+
message
|
|
796
|
+
} };
|
|
797
|
+
}
|
|
798
|
+
function fromOutcome(outcome) {
|
|
799
|
+
if (!outcome.ok) return { result: {
|
|
800
|
+
ok: false,
|
|
801
|
+
refused: outcome.refused,
|
|
802
|
+
message: outcome.message
|
|
803
|
+
} };
|
|
804
|
+
return {
|
|
805
|
+
result: {
|
|
806
|
+
ok: true,
|
|
807
|
+
message: outcome.message
|
|
808
|
+
},
|
|
809
|
+
action: outcome.action,
|
|
810
|
+
...outcome.evidence ? { evidence: outcome.evidence } : {}
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
/** Run one tool call against the world. Never throws on agent input. */
|
|
814
|
+
function callWorldTool(world, name, input) {
|
|
815
|
+
const args = input ?? {};
|
|
816
|
+
if (typeof args !== "object" || Array.isArray(args)) return refusedInput("Tool input must be an object.");
|
|
817
|
+
switch (name) {
|
|
818
|
+
case "status": return { result: {
|
|
819
|
+
ok: true,
|
|
820
|
+
message: "Current state.",
|
|
821
|
+
data: world.status()
|
|
822
|
+
} };
|
|
823
|
+
case "results": {
|
|
824
|
+
if (args.lever !== void 0 && typeof args.lever !== "string") return refusedInput("lever must be a lever id.");
|
|
825
|
+
if (args.by !== void 0 && args.by !== "week" && args.by !== "day") return refusedInput("by must be 'week' or 'day'.");
|
|
826
|
+
const rows = world.results({
|
|
827
|
+
lever: args.lever,
|
|
828
|
+
by: args.by
|
|
829
|
+
});
|
|
830
|
+
return { result: {
|
|
831
|
+
ok: true,
|
|
832
|
+
message: `${rows.length} period(s).`,
|
|
833
|
+
data: rows
|
|
834
|
+
} };
|
|
835
|
+
}
|
|
836
|
+
case "set_rate":
|
|
837
|
+
if (typeof args.lever !== "string" || typeof args.dailyRate !== "number") return refusedInput("set_rate takes { lever, dailyRate }.");
|
|
838
|
+
return fromOutcome(world.setRate(args.lever, args.dailyRate));
|
|
839
|
+
case "spend_once":
|
|
840
|
+
if (typeof args.lever !== "string" || typeof args.amount !== "number") return refusedInput("spend_once takes { lever, amount }.");
|
|
841
|
+
return fromOutcome(world.spendOnce(args.lever, args.amount));
|
|
842
|
+
case "request_approval":
|
|
843
|
+
if (typeof args.lever !== "string" || typeof args.maxDailyRate !== "number") return refusedInput("request_approval takes { lever, maxDailyRate, reason }.");
|
|
844
|
+
return fromOutcome(world.requestApproval(args.lever, args.maxDailyRate, typeof args.reason === "string" ? args.reason : ""));
|
|
845
|
+
case "save_note": {
|
|
846
|
+
if (typeof args.key !== "string" || typeof args.text !== "string") return refusedInput("save_note takes { key, text }.");
|
|
847
|
+
const saved = world.saveNote(args.key, args.text);
|
|
848
|
+
return saved.ok ? { result: {
|
|
849
|
+
ok: true,
|
|
850
|
+
message: `Saved '${args.key}'.`
|
|
851
|
+
} } : refusedInput(saved.message);
|
|
852
|
+
}
|
|
853
|
+
default: return { result: {
|
|
854
|
+
ok: false,
|
|
855
|
+
refused: "unknown_tool",
|
|
856
|
+
message: `No tool '${name}'.`
|
|
857
|
+
} };
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
// src/world/episode.ts
|
|
861
|
+
function loggable(input) {
|
|
862
|
+
try {
|
|
863
|
+
return JSON.parse(JSON.stringify(input ?? {}));
|
|
864
|
+
} catch {
|
|
865
|
+
return "[unserializable input]";
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
/** Run one agent through one episode of a scenario. */
|
|
869
|
+
async function runWorldEpisode(opts) {
|
|
870
|
+
const { scenario, seed } = opts;
|
|
871
|
+
const maxCalls = opts.maxCallsPerSession ?? 64;
|
|
872
|
+
if (!Number.isSafeInteger(maxCalls) || maxCalls < 1) throw new ValidationError("runWorldEpisode: maxCallsPerSession must be a positive integer");
|
|
873
|
+
const world = new World(scenario, seed);
|
|
874
|
+
const tools = worldTools(scenario);
|
|
875
|
+
const log = [];
|
|
876
|
+
const sessions = [];
|
|
877
|
+
for (let index = 0; !world.over; index++) {
|
|
878
|
+
const record = {
|
|
879
|
+
index,
|
|
880
|
+
day: world.day,
|
|
881
|
+
calls: 0
|
|
882
|
+
};
|
|
883
|
+
let open = true;
|
|
884
|
+
const session = {
|
|
885
|
+
index,
|
|
886
|
+
day: world.day,
|
|
887
|
+
date: world.date(),
|
|
888
|
+
daysLeft: world.daysLeft,
|
|
889
|
+
brief: scenario.brief,
|
|
890
|
+
notes: world.notes,
|
|
891
|
+
tools,
|
|
892
|
+
call(name, input) {
|
|
893
|
+
let call;
|
|
894
|
+
if (!open) call = { result: {
|
|
895
|
+
ok: false,
|
|
896
|
+
refused: "session_closed",
|
|
897
|
+
message: "This session has ended."
|
|
898
|
+
} };
|
|
899
|
+
else if (record.calls >= maxCalls) call = { result: {
|
|
900
|
+
ok: false,
|
|
901
|
+
refused: "call_limit",
|
|
902
|
+
message: `A session allows ${maxCalls} tool calls.`
|
|
903
|
+
} };
|
|
904
|
+
else call = callWorldTool(world, name, input);
|
|
905
|
+
if (open) record.calls += 1;
|
|
906
|
+
log.push({
|
|
907
|
+
seq: log.length,
|
|
908
|
+
session: index,
|
|
909
|
+
day: world.day,
|
|
910
|
+
tool: name,
|
|
911
|
+
input: loggable(input),
|
|
912
|
+
result: call.result,
|
|
913
|
+
...call.action ? { action: call.action } : {},
|
|
914
|
+
...call.evidence ? { evidence: call.evidence } : {}
|
|
915
|
+
});
|
|
916
|
+
return structuredClone(call.result);
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
try {
|
|
920
|
+
await opts.agent(session);
|
|
921
|
+
} catch (error) {
|
|
922
|
+
record.error = error instanceof Error ? error.message : String(error);
|
|
923
|
+
}
|
|
924
|
+
open = false;
|
|
925
|
+
sessions.push(record);
|
|
926
|
+
await opts.onSession?.(record, world);
|
|
927
|
+
world.advance(scenario.sessionEveryDays);
|
|
928
|
+
}
|
|
929
|
+
const end = world.end();
|
|
930
|
+
return {
|
|
931
|
+
scenarioId: scenario.id,
|
|
932
|
+
scenarioDigest: world.scenarioDigest,
|
|
933
|
+
seed,
|
|
934
|
+
sessions,
|
|
935
|
+
log,
|
|
936
|
+
notes: { ...world.notes },
|
|
937
|
+
end,
|
|
938
|
+
score: scoreWorldEpisode(scenario, sessions, log, end)
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
// src/world/compare.ts
|
|
942
|
+
/** Run every policy on every seed and report each row and each contrast. */
|
|
943
|
+
async function compareWorldPolicies(opts) {
|
|
944
|
+
const seeds = [...opts.seeds];
|
|
945
|
+
if (seeds.length < 2 || new Set(seeds).size !== seeds.length) throw new ValidationError("compareWorldPolicies: at least two distinct seeds are required");
|
|
946
|
+
const names = Object.keys(opts.policies);
|
|
947
|
+
if (names.length === 0) throw new ValidationError("compareWorldPolicies: at least one policy is required");
|
|
948
|
+
for (const name of (opts.contrasts ?? []).flat()) if (!names.includes(name)) throw new ValidationError(`compareWorldPolicies: no policy '${name}'`);
|
|
949
|
+
const episodes = {};
|
|
950
|
+
for (const [policy, make] of Object.entries(opts.policies)) {
|
|
951
|
+
const runs = [];
|
|
952
|
+
for (const seed of seeds) runs.push(await runWorldEpisode({
|
|
953
|
+
scenario: opts.scenario,
|
|
954
|
+
seed,
|
|
955
|
+
agent: make(seed)
|
|
956
|
+
}));
|
|
957
|
+
episodes[policy] = runs;
|
|
958
|
+
}
|
|
959
|
+
const rows = Object.entries(episodes).map(([policy, runs]) => {
|
|
960
|
+
const values = runs.map((run) => run.end.value);
|
|
961
|
+
const summary = meanConfidenceInterval(values);
|
|
962
|
+
return {
|
|
963
|
+
policy,
|
|
964
|
+
values,
|
|
965
|
+
mean: summary.mean,
|
|
966
|
+
stddev: summary.stdev,
|
|
967
|
+
min: Math.min(...values),
|
|
968
|
+
max: Math.max(...values),
|
|
969
|
+
meanGain: summary.mean - opts.scenario.budget,
|
|
970
|
+
unapprovedSpendAttempts: runs.reduce((sum, run) => sum + (run.score.refused.approval_missing ?? 0), 0),
|
|
971
|
+
prematureDecisions: runs.reduce((sum, run) => sum + run.score.prematureDecisions.length, 0),
|
|
972
|
+
failedSessions: runs.reduce((sum, run) => sum + run.score.failedSessions, 0)
|
|
973
|
+
};
|
|
974
|
+
});
|
|
975
|
+
const contrasts = (opts.contrasts ?? []).map(([better, worse]) => {
|
|
976
|
+
const high = rows.find((row) => row.policy === better);
|
|
977
|
+
const low = rows.find((row) => row.policy === worse);
|
|
978
|
+
const differences = high.values.map((value, index) => value - low.values[index]);
|
|
979
|
+
const meanDifference = differences.reduce((sum, d) => sum + d, 0) / differences.length;
|
|
980
|
+
const minDifference = Math.min(...differences);
|
|
981
|
+
const seedNoise = Math.max(high.stddev, low.stddev);
|
|
982
|
+
return {
|
|
983
|
+
better,
|
|
984
|
+
worse,
|
|
985
|
+
differences,
|
|
986
|
+
meanDifference,
|
|
987
|
+
minDifference,
|
|
988
|
+
wins: differences.filter((d) => d > 0).length,
|
|
989
|
+
seedNoise,
|
|
990
|
+
signTestP: pairedSignTest(differences, "greater").pValue,
|
|
991
|
+
beyondNoise: minDifference > 0 && meanDifference > 2 * seedNoise && high.failedSessions + low.failedSessions === 0
|
|
992
|
+
};
|
|
993
|
+
});
|
|
994
|
+
return {
|
|
995
|
+
scenarioId: opts.scenario.id,
|
|
996
|
+
scenarioDigest: episodes[names[0]][0].scenarioDigest,
|
|
997
|
+
seeds,
|
|
998
|
+
rows,
|
|
999
|
+
contrasts,
|
|
1000
|
+
episodes
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
// src/world/launch-quarter.ts
|
|
1004
|
+
const launchQuarterScenario = {
|
|
1005
|
+
id: "launch-quarter-v1",
|
|
1006
|
+
title: "Launch quarter",
|
|
1007
|
+
brief: [
|
|
1008
|
+
"You run go-to-market for a product launch. The quarter is 91 days and you review it once a week.",
|
|
1009
|
+
"You have a budget and five channels. Set a daily spend on each channel; it keeps running at that rate until you change it.",
|
|
1010
|
+
"Your result is the budget you have left at the end of the quarter plus the value of every qualified signup that has landed by then. Signups that land after day 91 count for nothing, and unspent budget keeps its full value.",
|
|
1011
|
+
"Results land days or weeks after the spend that caused them, and a single week of numbers is noisy. Each channel states when its results usually land.",
|
|
1012
|
+
"Email and content go out under your name: they need an approval on record before they can spend, and an approval takes two days.",
|
|
1013
|
+
"Between reviews you remember only what you saved with save_note."
|
|
1014
|
+
].join("\n"),
|
|
1015
|
+
startDate: "2027-01-04",
|
|
1016
|
+
horizonDays: 91,
|
|
1017
|
+
sessionEveryDays: 7,
|
|
1018
|
+
budget: 6e4,
|
|
1019
|
+
currency: "USD",
|
|
1020
|
+
outcome: {
|
|
1021
|
+
unit: "qualified signups",
|
|
1022
|
+
value: 100
|
|
1023
|
+
},
|
|
1024
|
+
approvalDelayDays: 2,
|
|
1025
|
+
levers: [
|
|
1026
|
+
{
|
|
1027
|
+
id: "email",
|
|
1028
|
+
label: "Email to the house list",
|
|
1029
|
+
description: "Campaigns to the existing subscriber list. Results land within days. The deliverability team asks for at most about two full sends a week, which is the guideline rate; mailing harder than that risks spam complaints, and a damaged sender reputation takes months to repair.",
|
|
1030
|
+
startRate: 40,
|
|
1031
|
+
guidelineRate: 60,
|
|
1032
|
+
maxRate: 400,
|
|
1033
|
+
requiresApproval: true,
|
|
1034
|
+
resultLagDays: {
|
|
1035
|
+
from: 1,
|
|
1036
|
+
to: 4
|
|
1037
|
+
}
|
|
1038
|
+
},
|
|
1039
|
+
{
|
|
1040
|
+
id: "paid_search",
|
|
1041
|
+
label: "Paid search",
|
|
1042
|
+
description: "Search ads on buying-intent keywords. Results land within a week. The ad platform needs about a week of steady spend to finish learning, and pausing a campaign starts that over. Keyword volume is limited, so spend above the guideline buys weaker clicks.",
|
|
1043
|
+
startRate: 150,
|
|
1044
|
+
guidelineRate: 300,
|
|
1045
|
+
maxRate: 1500,
|
|
1046
|
+
requiresApproval: false,
|
|
1047
|
+
resultLagDays: {
|
|
1048
|
+
from: 1,
|
|
1049
|
+
to: 6
|
|
1050
|
+
}
|
|
1051
|
+
},
|
|
1052
|
+
{
|
|
1053
|
+
id: "paid_social",
|
|
1054
|
+
label: "Paid social",
|
|
1055
|
+
description: "Promoted posts to a cold audience. Results land within days and swing widely from week to week: one post can carry a week.",
|
|
1056
|
+
startRate: 150,
|
|
1057
|
+
guidelineRate: 300,
|
|
1058
|
+
maxRate: 1500,
|
|
1059
|
+
requiresApproval: false,
|
|
1060
|
+
resultLagDays: {
|
|
1061
|
+
from: 1,
|
|
1062
|
+
to: 4
|
|
1063
|
+
}
|
|
1064
|
+
},
|
|
1065
|
+
{
|
|
1066
|
+
id: "content",
|
|
1067
|
+
label: "Content and search",
|
|
1068
|
+
description: "Articles and guides published under the company name. A piece starts to rank and bring signups two to five weeks after it is paid for, and a steady publishing cadence compounds; stopping resets it.",
|
|
1069
|
+
startRate: 150,
|
|
1070
|
+
guidelineRate: 250,
|
|
1071
|
+
maxRate: 800,
|
|
1072
|
+
requiresApproval: true,
|
|
1073
|
+
resultLagDays: {
|
|
1074
|
+
from: 14,
|
|
1075
|
+
to: 35
|
|
1076
|
+
}
|
|
1077
|
+
},
|
|
1078
|
+
{
|
|
1079
|
+
id: "community",
|
|
1080
|
+
label: "Community sponsorships",
|
|
1081
|
+
description: "Sponsored newsletters, meetups and podcasts. Signups arrive one to three weeks after a placement runs, in clumps.",
|
|
1082
|
+
startRate: 100,
|
|
1083
|
+
guidelineRate: 200,
|
|
1084
|
+
maxRate: 800,
|
|
1085
|
+
requiresApproval: false,
|
|
1086
|
+
resultLagDays: {
|
|
1087
|
+
from: 7,
|
|
1088
|
+
to: 21
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
],
|
|
1092
|
+
key: {
|
|
1093
|
+
levers: {
|
|
1094
|
+
email: {
|
|
1095
|
+
yield: .06,
|
|
1096
|
+
delayDays: {
|
|
1097
|
+
min: 1,
|
|
1098
|
+
median: 2,
|
|
1099
|
+
max: 4
|
|
1100
|
+
},
|
|
1101
|
+
weeklyNoise: .25,
|
|
1102
|
+
rampDays: 1,
|
|
1103
|
+
saturationRate: 120,
|
|
1104
|
+
marginalYield: .5,
|
|
1105
|
+
wear: {
|
|
1106
|
+
weeklyLoad: 450,
|
|
1107
|
+
damagePerDay: .06,
|
|
1108
|
+
recoveryPerDay: .004,
|
|
1109
|
+
floor: .15
|
|
1110
|
+
}
|
|
1111
|
+
},
|
|
1112
|
+
paid_search: {
|
|
1113
|
+
yield: .016,
|
|
1114
|
+
delayDays: {
|
|
1115
|
+
min: 1,
|
|
1116
|
+
median: 3,
|
|
1117
|
+
max: 6
|
|
1118
|
+
},
|
|
1119
|
+
weeklyNoise: .2,
|
|
1120
|
+
rampDays: 7,
|
|
1121
|
+
saturationRate: 300,
|
|
1122
|
+
marginalYield: .25
|
|
1123
|
+
},
|
|
1124
|
+
paid_social: {
|
|
1125
|
+
yield: .007,
|
|
1126
|
+
delayDays: {
|
|
1127
|
+
min: 1,
|
|
1128
|
+
median: 2,
|
|
1129
|
+
max: 4
|
|
1130
|
+
},
|
|
1131
|
+
weeklyNoise: .9,
|
|
1132
|
+
rampDays: 1,
|
|
1133
|
+
saturationRate: 300,
|
|
1134
|
+
marginalYield: .5
|
|
1135
|
+
},
|
|
1136
|
+
content: {
|
|
1137
|
+
yield: .035,
|
|
1138
|
+
delayDays: {
|
|
1139
|
+
min: 14,
|
|
1140
|
+
median: 21,
|
|
1141
|
+
max: 35
|
|
1142
|
+
},
|
|
1143
|
+
weeklyNoise: .3,
|
|
1144
|
+
rampDays: 28,
|
|
1145
|
+
saturationRate: 250,
|
|
1146
|
+
marginalYield: .3
|
|
1147
|
+
},
|
|
1148
|
+
community: {
|
|
1149
|
+
yield: .022,
|
|
1150
|
+
delayDays: {
|
|
1151
|
+
min: 7,
|
|
1152
|
+
median: 12,
|
|
1153
|
+
max: 21
|
|
1154
|
+
},
|
|
1155
|
+
weeklyNoise: .6,
|
|
1156
|
+
rampDays: 1,
|
|
1157
|
+
saturationRate: 200,
|
|
1158
|
+
marginalYield: .3
|
|
1159
|
+
}
|
|
1160
|
+
},
|
|
1161
|
+
evidence: {
|
|
1162
|
+
minOutcomes: 20,
|
|
1163
|
+
scaleFactor: 1.5
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
};
|
|
1167
|
+
// src/world/policies.ts
|
|
1168
|
+
const MEMORY_NOTE = "policy";
|
|
1169
|
+
function statusOf(session) {
|
|
1170
|
+
return session.call("status").data;
|
|
1171
|
+
}
|
|
1172
|
+
function approvalCovers(lever, rate) {
|
|
1173
|
+
return !lever.requiresApproval || (lever.approvedMaxRate ?? 0) >= rate;
|
|
1174
|
+
}
|
|
1175
|
+
function requestApprovalOnce(session, status, lever, rate) {
|
|
1176
|
+
if (approvalCovers(lever, rate)) return;
|
|
1177
|
+
if (!status.approvals.some((approval) => approval.lever === lever.id && approval.maxDailyRate >= rate)) session.call("request_approval", {
|
|
1178
|
+
lever: lever.id,
|
|
1179
|
+
maxDailyRate: rate,
|
|
1180
|
+
reason: `Run ${lever.label} at up to ${rate} a day.`
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
/** Never acts. Its end value is the starting budget: the floor every policy is read against. */
|
|
1184
|
+
const doNothingPolicy = () => {};
|
|
1185
|
+
/** Sets random rates across the whole allowed range each session. */
|
|
1186
|
+
function randomPolicy(seed) {
|
|
1187
|
+
return (session) => {
|
|
1188
|
+
const rng = makeRng(seedFromData([[seed, session.index]]));
|
|
1189
|
+
const status = statusOf(session);
|
|
1190
|
+
for (const lever of status.levers) {
|
|
1191
|
+
if (rng() < .5) continue;
|
|
1192
|
+
const rate = Math.round(rng() * lever.maxRate);
|
|
1193
|
+
if (lever.requiresApproval && rng() < .5) session.call("request_approval", {
|
|
1194
|
+
lever: lever.id,
|
|
1195
|
+
maxDailyRate: rate,
|
|
1196
|
+
reason: "random"
|
|
1197
|
+
});
|
|
1198
|
+
session.call("set_rate", {
|
|
1199
|
+
lever: lever.id,
|
|
1200
|
+
dailyRate: rate
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
/** Starts every lever at its suggested rate, then kills and scales on the
|
|
1206
|
+
* evidence rule its options state. */
|
|
1207
|
+
function evidencePolicy(opts) {
|
|
1208
|
+
if (!(opts.scaleStep >= 1) || !(opts.confidence >= 0) || !(opts.minEvidenceDays >= 0)) throw new ValidationError("evidencePolicy: scaleStep must be 1 or more; confidence and minEvidenceDays zero or more");
|
|
1209
|
+
return (session) => {
|
|
1210
|
+
const status = statusOf(session);
|
|
1211
|
+
const rows = session.call("results", { by: "day" }).data;
|
|
1212
|
+
const memory = session.notes[MEMORY_NOTE] ? JSON.parse(session.notes[MEMORY_NOTE]) : { killed: [] };
|
|
1213
|
+
const rates = new Map(status.levers.map((lever) => [lever.id, lever.dailyRate]));
|
|
1214
|
+
const returns = /* @__PURE__ */ new Map();
|
|
1215
|
+
const setRate = (lever, dailyRate) => {
|
|
1216
|
+
if (session.call("set_rate", {
|
|
1217
|
+
lever,
|
|
1218
|
+
dailyRate
|
|
1219
|
+
}).ok) rates.set(lever, dailyRate);
|
|
1220
|
+
};
|
|
1221
|
+
for (const lever of status.levers) {
|
|
1222
|
+
const ceiling = opts.ceiling === "guideline" ? lever.guidelineRate : lever.maxRate;
|
|
1223
|
+
requestApprovalOnce(session, status, lever, ceiling);
|
|
1224
|
+
if (memory.killed.includes(lever.id)) continue;
|
|
1225
|
+
const lag = (lever.resultLagDays.from + lever.resultLagDays.to) / 2;
|
|
1226
|
+
const tooLate = opts.planAhead && status.daysLeft <= lag;
|
|
1227
|
+
if (lever.dailyRate === 0) {
|
|
1228
|
+
if (!tooLate && lever.spent === 0 && approvalCovers(lever, lever.startRate)) setRate(lever.id, lever.startRate);
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
if (tooLate) {
|
|
1232
|
+
setRate(lever.id, 0);
|
|
1233
|
+
continue;
|
|
1234
|
+
}
|
|
1235
|
+
const mine = rows.filter((row) => row.lever === lever.id);
|
|
1236
|
+
const window = opts.evidence === "latest" ? status.sessionEveryDays : Number.POSITIVE_INFINITY;
|
|
1237
|
+
const shift = opts.evidence === "matured" ? lag : 0;
|
|
1238
|
+
let spend = 0;
|
|
1239
|
+
let days = 0;
|
|
1240
|
+
let outcomes = 0;
|
|
1241
|
+
for (const row of mine) {
|
|
1242
|
+
if (row.fromDay < status.day - window) continue;
|
|
1243
|
+
outcomes += row.outcomes;
|
|
1244
|
+
if (row.fromDay < status.day - shift && row.spend > 0) {
|
|
1245
|
+
spend += row.spend;
|
|
1246
|
+
days += 1;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
if (spend === 0 || days < opts.minEvidenceDays) continue;
|
|
1250
|
+
const error = opts.confidence * Math.sqrt(Math.max(outcomes, 1));
|
|
1251
|
+
const low = (outcomes - error) * status.outcomeValue / spend;
|
|
1252
|
+
const high = (outcomes + error) * status.outcomeValue / spend;
|
|
1253
|
+
returns.set(lever.id, outcomes * status.outcomeValue / spend);
|
|
1254
|
+
if (high < 1) {
|
|
1255
|
+
setRate(lever.id, 0);
|
|
1256
|
+
memory.killed.push(lever.id);
|
|
1257
|
+
} else if (low > 1 && lever.dailyRate < ceiling) {
|
|
1258
|
+
const next = Math.min(ceiling, Math.round(lever.dailyRate * opts.scaleStep));
|
|
1259
|
+
if (approvalCovers(lever, next)) setRate(lever.id, next);
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
if (opts.planAhead) {
|
|
1263
|
+
const weakestFirst = [...rates.keys()].sort((a, b) => (returns.get(a) ?? 1) - (returns.get(b) ?? 1));
|
|
1264
|
+
for (const lever of weakestFirst) {
|
|
1265
|
+
const over = [...rates.values()].reduce((sum, rate) => sum + rate, 0) * status.daysLeft - status.budgetLeft;
|
|
1266
|
+
if (over <= 0) break;
|
|
1267
|
+
const rate = rates.get(lever) ?? 0;
|
|
1268
|
+
if (rate > 0) setRate(lever, Math.max(0, Math.floor(rate - over / status.daysLeft)));
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
session.call("save_note", {
|
|
1272
|
+
key: MEMORY_NOTE,
|
|
1273
|
+
text: JSON.stringify(memory)
|
|
1274
|
+
});
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
/** Waits for a lever's matured results to clear break-even by two standard
|
|
1278
|
+
* errors, moves in small steps, stays inside the guidelines and plans to the
|
|
1279
|
+
* horizon. */
|
|
1280
|
+
const patientPolicy = evidencePolicy({
|
|
1281
|
+
evidence: "matured",
|
|
1282
|
+
minEvidenceDays: 14,
|
|
1283
|
+
confidence: 2,
|
|
1284
|
+
scaleStep: 1.25,
|
|
1285
|
+
ceiling: "guideline",
|
|
1286
|
+
planAhead: true
|
|
1287
|
+
});
|
|
1288
|
+
/** Acts on last period's number at the first review: kills what is under
|
|
1289
|
+
* break-even, doubles what is over it, up to the hard cap. */
|
|
1290
|
+
const impulsivePolicy = evidencePolicy({
|
|
1291
|
+
evidence: "latest",
|
|
1292
|
+
minEvidenceDays: 1,
|
|
1293
|
+
confidence: 0,
|
|
1294
|
+
scaleStep: 2,
|
|
1295
|
+
ceiling: "max",
|
|
1296
|
+
planAhead: false
|
|
1297
|
+
});
|
|
1298
|
+
/** Starts every lever at its suggested rate and never changes it. Patience
|
|
1299
|
+
* has to beat this too, or the scenario only rewards sitting still. */
|
|
1300
|
+
const setAndHoldPolicy = evidencePolicy({
|
|
1301
|
+
evidence: "matured",
|
|
1302
|
+
minEvidenceDays: Number.POSITIVE_INFINITY,
|
|
1303
|
+
confidence: 0,
|
|
1304
|
+
scaleStep: 1,
|
|
1305
|
+
ceiling: "guideline",
|
|
1306
|
+
planAhead: false
|
|
1307
|
+
});
|
|
1308
|
+
/** A reference that reads the key. Each lever is two tiers of spend, up to
|
|
1309
|
+
* its saturation or wear limit and above it; tiers are funded best return
|
|
1310
|
+
* first until the budget is planned, and a tier stops once its results would
|
|
1311
|
+
* land too late to pay. It is a strong informed plan, not a proven optimum. */
|
|
1312
|
+
function informedPolicy(scenario) {
|
|
1313
|
+
const value = scenario.outcome.value;
|
|
1314
|
+
const tiers = scenario.levers.flatMap((lever) => {
|
|
1315
|
+
const key = scenario.key.levers[lever.id];
|
|
1316
|
+
const cap = Math.min(lever.maxRate, key.wear ? key.wear.weeklyLoad / 7 : lever.maxRate);
|
|
1317
|
+
const base = Math.floor(Math.min(cap, key.saturationRate));
|
|
1318
|
+
const spread = key.delayDays.max > key.delayDays.median ? Math.log(key.delayDays.max / key.delayDays.median) / 2 : 0;
|
|
1319
|
+
return [{
|
|
1320
|
+
rate: base,
|
|
1321
|
+
perUnit: key.yield * value
|
|
1322
|
+
}, {
|
|
1323
|
+
rate: Math.floor(cap) - base,
|
|
1324
|
+
perUnit: key.yield * value * key.marginalYield
|
|
1325
|
+
}].map((tier) => ({
|
|
1326
|
+
...tier,
|
|
1327
|
+
lever: lever.id,
|
|
1328
|
+
stopDaysLeft: tier.perUnit > 1 ? Math.ceil(key.delayDays.median * Math.exp(spread * zQuantile(1 / tier.perUnit))) : 0
|
|
1329
|
+
}));
|
|
1330
|
+
}).filter((tier) => tier.rate > 0 && tier.perUnit > 1).sort((a, b) => b.perUnit - a.perUnit);
|
|
1331
|
+
return (session) => {
|
|
1332
|
+
const status = statusOf(session);
|
|
1333
|
+
const target = /* @__PURE__ */ new Map();
|
|
1334
|
+
const wanted = /* @__PURE__ */ new Map();
|
|
1335
|
+
let unplanned = status.budgetLeft;
|
|
1336
|
+
for (const tier of tiers) {
|
|
1337
|
+
wanted.set(tier.lever, (wanted.get(tier.lever) ?? 0) + tier.rate);
|
|
1338
|
+
const days = Math.max(0, status.daysLeft - tier.stopDaysLeft);
|
|
1339
|
+
const affordable = days > 0 ? Math.min(tier.rate, Math.floor(unplanned / days)) : 0;
|
|
1340
|
+
unplanned -= affordable * days;
|
|
1341
|
+
target.set(tier.lever, (target.get(tier.lever) ?? 0) + affordable);
|
|
1342
|
+
}
|
|
1343
|
+
for (const lever of status.levers) {
|
|
1344
|
+
const rate = target.get(lever.id);
|
|
1345
|
+
if (rate === void 0) continue;
|
|
1346
|
+
requestApprovalOnce(session, status, lever, wanted.get(lever.id) ?? rate);
|
|
1347
|
+
if (rate !== lever.dailyRate && approvalCovers(lever, rate)) session.call("set_rate", {
|
|
1348
|
+
lever: lever.id,
|
|
1349
|
+
dailyRate: rate
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
export { World, chatWorldAgent, compareWorldPolicies, doNothingPolicy, evidencePolicy, impulsivePolicy, informedPolicy, launchQuarterScenario, patientPolicy, randomPolicy, replayWorld, runWorldEpisode, setAndHoldPolicy, worldActions, worldDecisionCosts, worldSessionPrompt, worldTools };
|
|
1355
|
+
|
|
1356
|
+
//# sourceMappingURL=index.js.map
|