@remnic/core 1.1.11 → 1.1.12
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/README.md +3 -3
- package/dist/access-cli.js +67 -59
- package/dist/access-cli.js.map +1 -1
- package/dist/access-http.d.ts +7 -4
- package/dist/access-http.js +30 -26
- package/dist/access-mcp.d.ts +9 -4
- package/dist/access-mcp.js +29 -25
- package/dist/access-schema.d.ts +188 -8
- package/dist/access-schema.js +12 -3
- package/dist/{access-service-BkXt3di1.d.ts → access-service-DDjzFALq.d.ts} +60 -11
- package/dist/access-service.d.ts +7 -4
- package/dist/access-service.js +26 -23
- package/dist/action-confidence.d.ts +83 -0
- package/dist/action-confidence.js +22 -0
- package/dist/active-memory-bridge.d.ts +1 -1
- package/dist/active-recall.d.ts +1 -1
- package/dist/behavior-learner.d.ts +1 -1
- package/dist/behavior-signals.d.ts +1 -1
- package/dist/bootstrap.d.ts +4 -2
- package/dist/briefing.d.ts +1 -1
- package/dist/briefing.js +5 -5
- package/dist/buffer-surprise-report.d.ts +1 -1
- package/dist/buffer.d.ts +1 -1
- package/dist/calibration.d.ts +1 -1
- package/dist/calibration.js +4 -4
- package/dist/{capsule-export-LLEVB2RG.js → capsule-export-7QNCBZOQ.js} +3 -3
- package/dist/{capsule-import-UW45R2MZ.js → capsule-import-EPBHD2EN.js} +3 -3
- package/dist/causal-behavior.d.ts +1 -1
- package/dist/causal-consolidation.d.ts +1 -1
- package/dist/causal-consolidation.js +11 -11
- package/dist/{chunk-VQXK37XA.js → chunk-23ZZK64Y.js} +1 -1
- package/dist/chunk-23ZZK64Y.js.map +1 -0
- package/dist/{chunk-HJYHRE4S.js → chunk-242S3I2A.js} +2 -2
- package/dist/{chunk-MCC6KDQF.js → chunk-3B6KIRBH.js} +131 -13
- package/dist/chunk-3B6KIRBH.js.map +1 -0
- package/dist/chunk-4RA3C3EV.js +60 -0
- package/dist/chunk-4RA3C3EV.js.map +1 -0
- package/dist/{chunk-EYNQTST2.js → chunk-4YM32CRU.js} +4 -4
- package/dist/{chunk-6AUUAZEX.js → chunk-5NXIJZFX.js} +38 -8
- package/dist/chunk-5NXIJZFX.js.map +1 -0
- package/dist/chunk-6NKAQ74D.js +2237 -0
- package/dist/chunk-6NKAQ74D.js.map +1 -0
- package/dist/{chunk-PHNGXFQ6.js → chunk-7V22HTMD.js} +3 -3
- package/dist/{chunk-363MWCD3.js → chunk-7ZM3BFKK.js} +84 -62
- package/dist/chunk-7ZM3BFKK.js.map +1 -0
- package/dist/chunk-AC5LO7IU.js +308 -0
- package/dist/chunk-AC5LO7IU.js.map +1 -0
- package/dist/chunk-AH2JUU6X.js +336 -0
- package/dist/chunk-AH2JUU6X.js.map +1 -0
- package/dist/{chunk-VX2IUQFE.js → chunk-AQJNPMOA.js} +41 -11
- package/dist/chunk-AQJNPMOA.js.map +1 -0
- package/dist/{chunk-P73JTV34.js → chunk-BBE34QBJ.js} +4 -4
- package/dist/{chunk-KUHRUM6B.js → chunk-BZSQEPRW.js} +452 -139
- package/dist/chunk-BZSQEPRW.js.map +1 -0
- package/dist/chunk-C5BCH4ZS.js +317 -0
- package/dist/chunk-C5BCH4ZS.js.map +1 -0
- package/dist/{chunk-C5HUWVH2.js → chunk-CPKTBRS2.js} +6 -6
- package/dist/{chunk-IBX3VFOM.js → chunk-D4GAOFF6.js} +118 -2
- package/dist/chunk-D4GAOFF6.js.map +1 -0
- package/dist/chunk-DB5A3NHS.js +906 -0
- package/dist/chunk-DB5A3NHS.js.map +1 -0
- package/dist/{chunk-I6BQZSML.js → chunk-DZZPC36E.js} +10 -10
- package/dist/{chunk-O4XJUPSF.js → chunk-E2UCDP5S.js} +39 -2
- package/dist/chunk-E2UCDP5S.js.map +1 -0
- package/dist/{chunk-SRBJUAMP.js → chunk-FMEBPEAO.js} +11 -67
- package/dist/chunk-FMEBPEAO.js.map +1 -0
- package/dist/{chunk-4DXC6HQQ.js → chunk-FQDPCE3I.js} +5 -5
- package/dist/{chunk-NN3LPQ5D.js → chunk-HELQZFZO.js} +155 -16
- package/dist/chunk-HELQZFZO.js.map +1 -0
- package/dist/{chunk-57QNCUEZ.js → chunk-HL5LRPNA.js} +2 -2
- package/dist/{chunk-VTU2B4VF.js → chunk-HQZVVSVB.js} +2 -1
- package/dist/chunk-HQZVVSVB.js.map +1 -0
- package/dist/{chunk-6Z6UH6TK.js → chunk-HY3L4WKC.js} +69 -3
- package/dist/chunk-HY3L4WKC.js.map +1 -0
- package/dist/{chunk-QIGOEM65.js → chunk-IB3BFHGN.js} +5 -5
- package/dist/{chunk-RXTFCYQF.js → chunk-JESOB2HO.js} +6 -6
- package/dist/{chunk-2YMTO4ZJ.js → chunk-JKDVIE52.js} +9 -2
- package/dist/chunk-JKDVIE52.js.map +1 -0
- package/dist/{chunk-WGK4VHGP.js → chunk-MNU6ZBWT.js} +302 -140
- package/dist/chunk-MNU6ZBWT.js.map +1 -0
- package/dist/chunk-OAZ5MFUB.js +4124 -0
- package/dist/chunk-OAZ5MFUB.js.map +1 -0
- package/dist/{chunk-ZTSE2ZJ6.js → chunk-OIGNEXKZ.js} +50 -3
- package/dist/chunk-OIGNEXKZ.js.map +1 -0
- package/dist/chunk-OZKZ2TRP.js +3729 -0
- package/dist/chunk-OZKZ2TRP.js.map +1 -0
- package/dist/{chunk-GGD5W7TB.js → chunk-PD6O7AXF.js} +7 -2
- package/dist/chunk-PD6O7AXF.js.map +1 -0
- package/dist/{chunk-S3IP6R6K.js → chunk-PH4C2U43.js} +23 -3
- package/dist/chunk-PH4C2U43.js.map +1 -0
- package/dist/chunk-PYPOFEMK.js +294 -0
- package/dist/chunk-PYPOFEMK.js.map +1 -0
- package/dist/{chunk-EQINRHYR.js → chunk-QDZ2RLEC.js} +243 -7
- package/dist/chunk-QDZ2RLEC.js.map +1 -0
- package/dist/{chunk-KWBPHZUU.js → chunk-RK6F44Y6.js} +3 -2
- package/dist/chunk-RK6F44Y6.js.map +1 -0
- package/dist/{chunk-36CTNQY7.js → chunk-RVPLBATS.js} +42 -10
- package/dist/chunk-RVPLBATS.js.map +1 -0
- package/dist/chunk-SOAU2OE2.js +125 -0
- package/dist/chunk-SOAU2OE2.js.map +1 -0
- package/dist/{chunk-A4ACKWIW.js → chunk-U5JMRGKX.js} +55 -4
- package/dist/chunk-U5JMRGKX.js.map +1 -0
- package/dist/{chunk-LIO5X3CM.js → chunk-UVMUAWVT.js} +2 -2
- package/dist/chunk-VWT3F4IV.js +2161 -0
- package/dist/chunk-VWT3F4IV.js.map +1 -0
- package/dist/{chunk-PB5KW5PL.js → chunk-WEJG4TB5.js} +6 -6
- package/dist/{chunk-KBYWQWSB.js → chunk-X7HPGUVG.js} +2 -2
- package/dist/{chunk-Y5KDIOKF.js → chunk-XAMBKFQS.js} +383 -9
- package/dist/chunk-XAMBKFQS.js.map +1 -0
- package/dist/{chunk-ZL4S7ARC.js → chunk-Y3VMVTYX.js} +3 -3
- package/dist/{chunk-Z5S5HNGY.js → chunk-ZG7PTKBK.js} +21 -5
- package/dist/chunk-ZG7PTKBK.js.map +1 -0
- package/dist/{chunk-6XA7UN4Z.js → chunk-ZNQN6ZTA.js} +2 -2
- package/dist/{chunk-WTFWLUSX.js → chunk-ZVTKDVVM.js} +2 -2
- package/dist/{cli-Cvy2SNhF.d.ts → cli-BR8KpIU0.d.ts} +2 -2
- package/dist/cli.d.ts +7 -4
- package/dist/cli.js +44 -40
- package/dist/codex-cli-fallback.d.ts +1 -0
- package/dist/codex-cli-fallback.js +1 -1
- package/dist/compression-optimizer.d.ts +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.js +1 -1
- package/dist/consolidation-provenance-check.d.ts +1 -1
- package/dist/consolidation-undo.d.ts +1 -1
- package/dist/{contradiction-scan-3Z6YW7YA.js → contradiction-scan-QTXAMBUA.js} +3 -2
- package/dist/contradiction-scan-QTXAMBUA.js.map +1 -0
- package/dist/day-summary.d.ts +1 -1
- package/dist/delinearize.d.ts +1 -1
- package/dist/direct-answer-wiring.d.ts +1 -1
- package/dist/direct-answer-wiring.js +1 -1
- package/dist/direct-answer.d.ts +1 -1
- package/dist/embedding-fallback.d.ts +1 -1
- package/dist/{engine-FOC3IJLA.js → engine-35M5BKQ7.js} +7 -7
- package/dist/entity-retrieval.d.ts +1 -1
- package/dist/entity-retrieval.js +5 -5
- package/dist/entity-schema.d.ts +1 -1
- package/dist/event-order-recall.d.ts +17 -0
- package/dist/event-order-recall.js +11 -0
- package/dist/event-order-recall.js.map +1 -0
- package/dist/evidence-pack.d.ts +3 -1
- package/dist/evidence-pack.js +5 -3
- package/dist/explicit-capture.d.ts +4 -2
- package/dist/explicit-cue-recall.d.ts +4 -1
- package/dist/explicit-cue-recall.js +4 -2
- package/dist/extraction-judge-telemetry.d.ts +1 -1
- package/dist/extraction-judge-training.d.ts +1 -1
- package/dist/extraction-judge.d.ts +1 -1
- package/dist/extraction.d.ts +1 -1
- package/dist/extraction.js +8 -7
- package/dist/fallback-llm.d.ts +2 -1
- package/dist/fallback-llm.js +4 -4
- package/dist/focused-list-recall.d.ts +17 -0
- package/dist/focused-list-recall.js +11 -0
- package/dist/focused-list-recall.js.map +1 -0
- package/dist/identity-continuity.d.ts +1 -1
- package/dist/importance.d.ts +1 -1
- package/dist/{index-1qIcnbG1.d.ts → index-DJ9QWMw-.d.ts} +3 -2
- package/dist/index.d.ts +49 -12
- package/dist/index.js +284 -114
- package/dist/index.js.map +1 -1
- package/dist/intent.d.ts +1 -1
- package/dist/intent.js +1 -1
- package/dist/lifecycle.d.ts +1 -1
- package/dist/live-connectors-runner.d.ts +1 -1
- package/dist/local-llm.d.ts +8 -4
- package/dist/local-llm.js +1 -1
- package/dist/mcp-memory-inspector-app.d.ts +106 -0
- package/dist/mcp-memory-inspector-app.js +20 -0
- package/dist/mcp-memory-inspector-app.js.map +1 -0
- package/dist/memory-action-policy.d.ts +1 -1
- package/dist/memory-cache.d.ts +1 -1
- package/dist/{memory-governance-F3QOJGEY.js → memory-governance-IMPQZXFC.js} +7 -7
- package/dist/memory-governance-IMPQZXFC.js.map +1 -0
- package/dist/memory-lifecycle-ledger-utils.d.ts +1 -1
- package/dist/memory-projection-store.d.ts +1 -1
- package/dist/memory-provenance.d.ts +57 -0
- package/dist/memory-provenance.js +13 -0
- package/dist/memory-provenance.js.map +1 -0
- package/dist/memory-worth-outcomes.d.ts +1 -1
- package/dist/models-json.d.ts +1 -1
- package/dist/native-knowledge.d.ts +1 -1
- package/dist/objective-state-writers.d.ts +1 -1
- package/dist/objective-state-writers.js +2 -2
- package/dist/operator-toolkit.d.ts +1 -1
- package/dist/operator-toolkit.js +11 -11
- package/dist/{orchestrator-AOQMo7QI.d.ts → orchestrator-DDMPqU6R.d.ts} +9 -1
- package/dist/orchestrator.d.ts +4 -2
- package/dist/orchestrator.js +53 -46
- package/dist/patterns-cli.d.ts +1 -1
- package/dist/policy-runtime.d.ts +1 -1
- package/dist/qmd-recall-cache.d.ts +1 -1
- package/dist/qmd.d.ts +1 -1
- package/dist/recall-disclosure-escalation.d.ts +1 -1
- package/dist/recall-explain-renderer.d.ts +3 -1
- package/dist/recall-explain-renderer.js +5 -3
- package/dist/recall-state.d.ts +1 -1
- package/dist/recall-tag-filter.d.ts +3 -1
- package/dist/recall-xray-cli.d.ts +3 -1
- package/dist/recall-xray-cli.js +6 -4
- package/dist/recall-xray-renderer.d.ts +3 -1
- package/dist/recall-xray-renderer.js +5 -3
- package/dist/recall-xray.d.ts +8 -1
- package/dist/recall-xray.js +4 -2
- package/dist/resolve-auth-token.d.ts +1 -1
- package/dist/resolve-provider-secret.js +1 -1
- package/dist/response-guidance-recall.d.ts +18 -0
- package/dist/response-guidance-recall.js +11 -0
- package/dist/response-guidance-recall.js.map +1 -0
- package/dist/resume-bundles.js +3 -3
- package/dist/retrieval-agents.d.ts +1 -1
- package/dist/retrieval-tiers.d.ts +1 -1
- package/dist/schemas.d.ts +22 -22
- package/dist/semantic-consolidation.d.ts +1 -1
- package/dist/semantic-consolidation.js +6 -6
- package/dist/semantic-rule-promotion.js +5 -5
- package/dist/semantic-rule-verifier.d.ts +1 -1
- package/dist/semantic-rule-verifier.js +6 -6
- package/dist/session-observer-bands.d.ts +1 -1
- package/dist/session-observer-state.d.ts +1 -1
- package/dist/signal.d.ts +1 -1
- package/dist/storage.d.ts +3 -1
- package/dist/storage.js +4 -4
- package/dist/summarizer.d.ts +1 -1
- package/dist/summarizer.js +6 -6
- package/dist/summary-snapshot.d.ts +1 -1
- package/dist/targeted-fact-recall.d.ts +17 -0
- package/dist/targeted-fact-recall.js +11 -0
- package/dist/targeted-fact-recall.js.map +1 -0
- package/dist/telemetry-transcript.d.ts +7 -0
- package/dist/telemetry-transcript.js +16 -0
- package/dist/telemetry-transcript.js.map +1 -0
- package/dist/temporal-supersession.d.ts +1 -1
- package/dist/temporal-supersession.js +2 -1
- package/dist/temporal-validity.d.ts +1 -1
- package/dist/threading.d.ts +1 -1
- package/dist/tier-migration.d.ts +1 -1
- package/dist/tier-routing.d.ts +1 -1
- package/dist/tokens.js +1 -1
- package/dist/topics.d.ts +1 -1
- package/dist/transcript.d.ts +1 -1
- package/dist/trust-zones.d.ts +3 -2
- package/dist/trust-zones.js +1 -1
- package/dist/types.d.ts +60 -2
- package/dist/types.js +1 -1
- package/dist/user-model.d.ts +37 -0
- package/dist/user-model.js +28 -0
- package/dist/user-model.js.map +1 -0
- package/dist/utility-runtime.d.ts +1 -1
- package/dist/verified-recall.js +6 -6
- package/package.json +1 -1
- package/dist/chunk-2YMTO4ZJ.js.map +0 -1
- package/dist/chunk-363MWCD3.js.map +0 -1
- package/dist/chunk-36CTNQY7.js.map +0 -1
- package/dist/chunk-6AUUAZEX.js.map +0 -1
- package/dist/chunk-6Z6UH6TK.js.map +0 -1
- package/dist/chunk-74WWN7ZW.js +0 -82
- package/dist/chunk-74WWN7ZW.js.map +0 -1
- package/dist/chunk-A4ACKWIW.js.map +0 -1
- package/dist/chunk-EQINRHYR.js.map +0 -1
- package/dist/chunk-GGD5W7TB.js.map +0 -1
- package/dist/chunk-IBX3VFOM.js.map +0 -1
- package/dist/chunk-KUHRUM6B.js.map +0 -1
- package/dist/chunk-KWBPHZUU.js.map +0 -1
- package/dist/chunk-MCC6KDQF.js.map +0 -1
- package/dist/chunk-NN3LPQ5D.js.map +0 -1
- package/dist/chunk-O4XJUPSF.js.map +0 -1
- package/dist/chunk-S2JJBLJG.js +0 -2101
- package/dist/chunk-S2JJBLJG.js.map +0 -1
- package/dist/chunk-S3IP6R6K.js.map +0 -1
- package/dist/chunk-SRBJUAMP.js.map +0 -1
- package/dist/chunk-VQXK37XA.js.map +0 -1
- package/dist/chunk-VTU2B4VF.js.map +0 -1
- package/dist/chunk-VX2IUQFE.js.map +0 -1
- package/dist/chunk-WGK4VHGP.js.map +0 -1
- package/dist/chunk-Y5KDIOKF.js.map +0 -1
- package/dist/chunk-Z5S5HNGY.js.map +0 -1
- package/dist/chunk-ZTSE2ZJ6.js.map +0 -1
- package/dist/contradiction-scan-3Z6YW7YA.js.map +0 -1
- /package/dist/{capsule-export-LLEVB2RG.js.map → action-confidence.js.map} +0 -0
- /package/dist/{capsule-import-UW45R2MZ.js.map → capsule-export-7QNCBZOQ.js.map} +0 -0
- /package/dist/{engine-FOC3IJLA.js.map → capsule-import-EPBHD2EN.js.map} +0 -0
- /package/dist/{chunk-HJYHRE4S.js.map → chunk-242S3I2A.js.map} +0 -0
- /package/dist/{chunk-EYNQTST2.js.map → chunk-4YM32CRU.js.map} +0 -0
- /package/dist/{chunk-PHNGXFQ6.js.map → chunk-7V22HTMD.js.map} +0 -0
- /package/dist/{chunk-P73JTV34.js.map → chunk-BBE34QBJ.js.map} +0 -0
- /package/dist/{chunk-C5HUWVH2.js.map → chunk-CPKTBRS2.js.map} +0 -0
- /package/dist/{chunk-I6BQZSML.js.map → chunk-DZZPC36E.js.map} +0 -0
- /package/dist/{chunk-4DXC6HQQ.js.map → chunk-FQDPCE3I.js.map} +0 -0
- /package/dist/{chunk-57QNCUEZ.js.map → chunk-HL5LRPNA.js.map} +0 -0
- /package/dist/{chunk-QIGOEM65.js.map → chunk-IB3BFHGN.js.map} +0 -0
- /package/dist/{chunk-RXTFCYQF.js.map → chunk-JESOB2HO.js.map} +0 -0
- /package/dist/{chunk-LIO5X3CM.js.map → chunk-UVMUAWVT.js.map} +0 -0
- /package/dist/{chunk-PB5KW5PL.js.map → chunk-WEJG4TB5.js.map} +0 -0
- /package/dist/{chunk-KBYWQWSB.js.map → chunk-X7HPGUVG.js.map} +0 -0
- /package/dist/{chunk-ZL4S7ARC.js.map → chunk-Y3VMVTYX.js.map} +0 -0
- /package/dist/{chunk-6XA7UN4Z.js.map → chunk-ZNQN6ZTA.js.map} +0 -0
- /package/dist/{chunk-WTFWLUSX.js.map → chunk-ZVTKDVVM.js.map} +0 -0
- /package/dist/{memory-governance-F3QOJGEY.js.map → engine-35M5BKQ7.js.map} +0 -0
|
@@ -0,0 +1,4124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildEvidencePack
|
|
3
|
+
} from "./chunk-PYPOFEMK.js";
|
|
4
|
+
|
|
5
|
+
// src/explicit-cue-recall.ts
|
|
6
|
+
var DEFAULT_MAX_CHARS = 2400;
|
|
7
|
+
var DEFAULT_MAX_ITEM_CHARS = 1200;
|
|
8
|
+
var DEFAULT_MAX_REFERENCES = 24;
|
|
9
|
+
var REFERENCE_SCAN_TOKEN_FACTOR = 3;
|
|
10
|
+
var TURN_REFERENCE_WINDOW_RADIUS = 0;
|
|
11
|
+
var LEXICAL_CUE_WINDOW_RADIUS = 1;
|
|
12
|
+
var LEXICAL_CUE_SEARCH_LIMIT = 3;
|
|
13
|
+
var LEXICAL_CUE_MAX_TOKENS = 400;
|
|
14
|
+
var FOCUSED_TRANSCRIPT_SCAN_MAX_TOKENS = 25e4;
|
|
15
|
+
var CONTENT_LABEL_SEARCH_LIMIT = 64;
|
|
16
|
+
var CONTENT_LABEL_MAX_TOKENS = 2e3;
|
|
17
|
+
var CONTENT_LABEL_MAX_PAIRED_WINDOWS_PER_REFERENCE = 1;
|
|
18
|
+
var TRAJECTORY_ANALYSIS_MAX_TOKENS = 25e4;
|
|
19
|
+
var TRAJECTORY_ANALYSIS_MAX_RANGE_STEPS = 80;
|
|
20
|
+
var TRAJECTORY_ANALYSIS_MAX_LINES = 160;
|
|
21
|
+
var LATEST_STATE_CUES = /* @__PURE__ */ new Set([
|
|
22
|
+
"as of",
|
|
23
|
+
"currently",
|
|
24
|
+
"latest",
|
|
25
|
+
"most recent",
|
|
26
|
+
"newest",
|
|
27
|
+
"now",
|
|
28
|
+
"updated",
|
|
29
|
+
"changed",
|
|
30
|
+
"change"
|
|
31
|
+
]);
|
|
32
|
+
var STRUCTURED_PLAN_FIELD_CUES = /* @__PURE__ */ new Set([
|
|
33
|
+
"accommodation",
|
|
34
|
+
"attraction",
|
|
35
|
+
"breakfast",
|
|
36
|
+
"current city",
|
|
37
|
+
"dinner",
|
|
38
|
+
"flight",
|
|
39
|
+
"flights",
|
|
40
|
+
"hotel",
|
|
41
|
+
"lunch",
|
|
42
|
+
"restaurant",
|
|
43
|
+
"restaurants",
|
|
44
|
+
"transportation",
|
|
45
|
+
"traveler",
|
|
46
|
+
"travelers"
|
|
47
|
+
]);
|
|
48
|
+
var STRUCTURED_PLAN_DEPENDENCY_CUES = /* @__PURE__ */ new Set([
|
|
49
|
+
"comparison",
|
|
50
|
+
"constraint",
|
|
51
|
+
"constraints",
|
|
52
|
+
"dependency",
|
|
53
|
+
"dependencies",
|
|
54
|
+
"join",
|
|
55
|
+
"same",
|
|
56
|
+
"shared"
|
|
57
|
+
]);
|
|
58
|
+
var BENCHMARK_ABILITY_CUES = /* @__PURE__ */ new Map([
|
|
59
|
+
["information extraction", "ability=information_extraction"],
|
|
60
|
+
["knowledge update", "ability=knowledge_update"],
|
|
61
|
+
["multi session reasoning", "ability=multi_session_reasoning"],
|
|
62
|
+
["multi-session reasoning", "ability=multi_session_reasoning"],
|
|
63
|
+
["instruction following", "ability=instruction_following"]
|
|
64
|
+
]);
|
|
65
|
+
var BENCHMARK_ANCHOR_VALUE_STOPWORDS = /* @__PURE__ */ new Set([
|
|
66
|
+
"a",
|
|
67
|
+
"about",
|
|
68
|
+
"an",
|
|
69
|
+
"for",
|
|
70
|
+
"from",
|
|
71
|
+
"in",
|
|
72
|
+
"on",
|
|
73
|
+
"the",
|
|
74
|
+
"to",
|
|
75
|
+
"use",
|
|
76
|
+
"using",
|
|
77
|
+
"with"
|
|
78
|
+
]);
|
|
79
|
+
var RELATIVE_TEMPORAL_CUES = [
|
|
80
|
+
"as of",
|
|
81
|
+
"most recent",
|
|
82
|
+
"last time",
|
|
83
|
+
"last week",
|
|
84
|
+
"last month",
|
|
85
|
+
"last year",
|
|
86
|
+
"last session",
|
|
87
|
+
"last conversation",
|
|
88
|
+
"next time",
|
|
89
|
+
"next week",
|
|
90
|
+
"next month",
|
|
91
|
+
"next year",
|
|
92
|
+
"next session",
|
|
93
|
+
"next conversation",
|
|
94
|
+
"previous time",
|
|
95
|
+
"previous week",
|
|
96
|
+
"previous month",
|
|
97
|
+
"previous year",
|
|
98
|
+
"previous session",
|
|
99
|
+
"previous conversation",
|
|
100
|
+
"prior time",
|
|
101
|
+
"prior week",
|
|
102
|
+
"prior month",
|
|
103
|
+
"prior year",
|
|
104
|
+
"prior session",
|
|
105
|
+
"prior conversation",
|
|
106
|
+
"today",
|
|
107
|
+
"yesterday",
|
|
108
|
+
"tomorrow",
|
|
109
|
+
"tonight",
|
|
110
|
+
"earlier",
|
|
111
|
+
"later",
|
|
112
|
+
"recently",
|
|
113
|
+
"previously",
|
|
114
|
+
"currently",
|
|
115
|
+
"now",
|
|
116
|
+
"latest",
|
|
117
|
+
"newest",
|
|
118
|
+
"oldest",
|
|
119
|
+
"earliest",
|
|
120
|
+
"before",
|
|
121
|
+
"after",
|
|
122
|
+
"since",
|
|
123
|
+
"updated",
|
|
124
|
+
"changed",
|
|
125
|
+
"change"
|
|
126
|
+
];
|
|
127
|
+
var SPEAKER_NAME_STOPWORDS = /* @__PURE__ */ new Set([
|
|
128
|
+
"A",
|
|
129
|
+
"According",
|
|
130
|
+
"An",
|
|
131
|
+
"And",
|
|
132
|
+
"Are",
|
|
133
|
+
"As",
|
|
134
|
+
"At",
|
|
135
|
+
"Before",
|
|
136
|
+
"Can",
|
|
137
|
+
"Compare",
|
|
138
|
+
"Could",
|
|
139
|
+
"Did",
|
|
140
|
+
"Do",
|
|
141
|
+
"Does",
|
|
142
|
+
"For",
|
|
143
|
+
"From",
|
|
144
|
+
"Had",
|
|
145
|
+
"Has",
|
|
146
|
+
"Have",
|
|
147
|
+
"How",
|
|
148
|
+
"In",
|
|
149
|
+
"Is",
|
|
150
|
+
"It",
|
|
151
|
+
"Join",
|
|
152
|
+
"Of",
|
|
153
|
+
"On",
|
|
154
|
+
"Or",
|
|
155
|
+
"Please",
|
|
156
|
+
"Review",
|
|
157
|
+
"Step",
|
|
158
|
+
"Tell",
|
|
159
|
+
"The",
|
|
160
|
+
"To",
|
|
161
|
+
"Turn",
|
|
162
|
+
"Use",
|
|
163
|
+
"Was",
|
|
164
|
+
"Were",
|
|
165
|
+
"What",
|
|
166
|
+
"When",
|
|
167
|
+
"Where",
|
|
168
|
+
"Which",
|
|
169
|
+
"Who",
|
|
170
|
+
"Why",
|
|
171
|
+
"Will",
|
|
172
|
+
"Would"
|
|
173
|
+
]);
|
|
174
|
+
var QUESTION_SLOT_STOPWORDS = /* @__PURE__ */ new Set([
|
|
175
|
+
"answer",
|
|
176
|
+
"choice",
|
|
177
|
+
"did",
|
|
178
|
+
"does",
|
|
179
|
+
"do",
|
|
180
|
+
"is",
|
|
181
|
+
"should",
|
|
182
|
+
"single",
|
|
183
|
+
"the",
|
|
184
|
+
"user",
|
|
185
|
+
"was",
|
|
186
|
+
"were"
|
|
187
|
+
]);
|
|
188
|
+
var CONTENT_LEXICAL_CUE_STOPWORDS = /* @__PURE__ */ new Set([
|
|
189
|
+
"about",
|
|
190
|
+
"across",
|
|
191
|
+
"after",
|
|
192
|
+
"again",
|
|
193
|
+
"also",
|
|
194
|
+
"and",
|
|
195
|
+
"answer",
|
|
196
|
+
"answers",
|
|
197
|
+
"are",
|
|
198
|
+
"before",
|
|
199
|
+
"between",
|
|
200
|
+
"can",
|
|
201
|
+
"could",
|
|
202
|
+
"different",
|
|
203
|
+
"did",
|
|
204
|
+
"does",
|
|
205
|
+
"during",
|
|
206
|
+
"feature",
|
|
207
|
+
"features",
|
|
208
|
+
"from",
|
|
209
|
+
"have",
|
|
210
|
+
"help",
|
|
211
|
+
"how",
|
|
212
|
+
"implement",
|
|
213
|
+
"into",
|
|
214
|
+
"made",
|
|
215
|
+
"many",
|
|
216
|
+
"me",
|
|
217
|
+
"mention",
|
|
218
|
+
"my",
|
|
219
|
+
"need",
|
|
220
|
+
"only",
|
|
221
|
+
"order",
|
|
222
|
+
"over",
|
|
223
|
+
"project",
|
|
224
|
+
"question",
|
|
225
|
+
"questions",
|
|
226
|
+
"request",
|
|
227
|
+
"requests",
|
|
228
|
+
"show",
|
|
229
|
+
"tell",
|
|
230
|
+
"that",
|
|
231
|
+
"the",
|
|
232
|
+
"there",
|
|
233
|
+
"these",
|
|
234
|
+
"through",
|
|
235
|
+
"throughout",
|
|
236
|
+
"trying",
|
|
237
|
+
"want",
|
|
238
|
+
"were",
|
|
239
|
+
"what",
|
|
240
|
+
"when",
|
|
241
|
+
"which",
|
|
242
|
+
"with",
|
|
243
|
+
"would",
|
|
244
|
+
"you"
|
|
245
|
+
]);
|
|
246
|
+
var CONTENT_LEXICAL_PHRASE_STOPWORDS = new Set(
|
|
247
|
+
[...CONTENT_LEXICAL_CUE_STOPWORDS].filter(
|
|
248
|
+
(word) => word !== "feature" && word !== "features" && word !== "implement"
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
async function buildExplicitCueRecallSection(options) {
|
|
252
|
+
const engine = options.engine;
|
|
253
|
+
const query = options.query.trim();
|
|
254
|
+
const maxChars = normalizePositiveInteger(options.maxChars, DEFAULT_MAX_CHARS);
|
|
255
|
+
if (!engine || query.length === 0 || maxChars <= 0) {
|
|
256
|
+
return "";
|
|
257
|
+
}
|
|
258
|
+
const maxReferences = normalizePositiveInteger(
|
|
259
|
+
options.maxReferences,
|
|
260
|
+
DEFAULT_MAX_REFERENCES
|
|
261
|
+
);
|
|
262
|
+
if (maxReferences <= 0) {
|
|
263
|
+
return "";
|
|
264
|
+
}
|
|
265
|
+
const evidenceItems = [];
|
|
266
|
+
const seenTurns = /* @__PURE__ */ new Set();
|
|
267
|
+
await collectTurnReferenceEvidence({
|
|
268
|
+
engine,
|
|
269
|
+
sessionId: options.sessionId,
|
|
270
|
+
query,
|
|
271
|
+
maxReferences,
|
|
272
|
+
evidenceItems,
|
|
273
|
+
seenTurns
|
|
274
|
+
});
|
|
275
|
+
if (options.includeContentLexicalCues) {
|
|
276
|
+
await collectNamedMeetingFactEvidence({
|
|
277
|
+
engine,
|
|
278
|
+
sessionId: options.sessionId,
|
|
279
|
+
query,
|
|
280
|
+
maxReferences,
|
|
281
|
+
evidenceItems,
|
|
282
|
+
seenTurns
|
|
283
|
+
});
|
|
284
|
+
await collectFocusedTranscriptCueEvidence({
|
|
285
|
+
engine,
|
|
286
|
+
sessionId: options.sessionId,
|
|
287
|
+
query,
|
|
288
|
+
evidenceItems,
|
|
289
|
+
seenTurns
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
await collectLexicalCueEvidence({
|
|
293
|
+
engine,
|
|
294
|
+
sessionId: options.sessionId,
|
|
295
|
+
query,
|
|
296
|
+
maxReferences,
|
|
297
|
+
includeBenchmarkAnchorCues: options.includeBenchmarkAnchorCues,
|
|
298
|
+
includeContentLexicalCues: options.includeContentLexicalCues,
|
|
299
|
+
includeStructuredPlanCues: options.includeStructuredPlanCues,
|
|
300
|
+
evidenceItems,
|
|
301
|
+
seenTurns
|
|
302
|
+
});
|
|
303
|
+
const evidenceFocusQuery = buildEvidenceFocusQuery(query, {
|
|
304
|
+
includeBenchmarkAnchorCues: options.includeBenchmarkAnchorCues,
|
|
305
|
+
includeContentLexicalCues: options.includeContentLexicalCues,
|
|
306
|
+
includeStructuredPlanCues: options.includeStructuredPlanCues
|
|
307
|
+
});
|
|
308
|
+
return buildEvidencePack(evidenceItems, {
|
|
309
|
+
title: "Explicit Cue Evidence",
|
|
310
|
+
query: evidenceFocusQuery,
|
|
311
|
+
maxChars,
|
|
312
|
+
maxItemChars: normalizePositiveInteger(
|
|
313
|
+
options.maxItemChars,
|
|
314
|
+
DEFAULT_MAX_ITEM_CHARS
|
|
315
|
+
)
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
function buildEvidenceFocusQuery(query, options) {
|
|
319
|
+
const cues = collectLexicalCues(query, options);
|
|
320
|
+
if (cues.length === 0) {
|
|
321
|
+
return query;
|
|
322
|
+
}
|
|
323
|
+
return `${query}
|
|
324
|
+
${cues.join("\n")}`;
|
|
325
|
+
}
|
|
326
|
+
async function buildTrajectoryAnalysisRecallSection(options) {
|
|
327
|
+
const engine = options.engine;
|
|
328
|
+
const sessionId = options.sessionId;
|
|
329
|
+
const query = options.query.trim();
|
|
330
|
+
const maxChars = normalizePositiveInteger(options.maxChars, DEFAULT_MAX_CHARS);
|
|
331
|
+
if (!engine || !sessionId || !engine.getStats || query.length === 0 || maxChars <= 0) {
|
|
332
|
+
return "";
|
|
333
|
+
}
|
|
334
|
+
if (!hasTrajectoryAnalysisIntent(query)) {
|
|
335
|
+
return "";
|
|
336
|
+
}
|
|
337
|
+
const stats = await engine.getStats(sessionId);
|
|
338
|
+
const totalMessages = Math.max(0, Math.floor(stats.totalMessages));
|
|
339
|
+
if (totalMessages <= 0) {
|
|
340
|
+
return "";
|
|
341
|
+
}
|
|
342
|
+
const expansionEnd = normalizeTurnExpansionEnd(stats);
|
|
343
|
+
const messages = await engine.expandContext(
|
|
344
|
+
sessionId,
|
|
345
|
+
0,
|
|
346
|
+
expansionEnd,
|
|
347
|
+
TRAJECTORY_ANALYSIS_MAX_TOKENS
|
|
348
|
+
);
|
|
349
|
+
const trajectory = parseLabeledTrajectory(messages);
|
|
350
|
+
if (trajectory.length === 0) {
|
|
351
|
+
return "";
|
|
352
|
+
}
|
|
353
|
+
const bounds = inferTrajectoryBounds(query, trajectory);
|
|
354
|
+
const lines = buildTrajectoryAnalysisLines(query, trajectory, bounds);
|
|
355
|
+
if (lines.length === 0) {
|
|
356
|
+
return "";
|
|
357
|
+
}
|
|
358
|
+
const header = "## Trajectory analysis";
|
|
359
|
+
const bodyBudget = maxChars - header.length - 1;
|
|
360
|
+
if (bodyBudget <= 0) {
|
|
361
|
+
return "";
|
|
362
|
+
}
|
|
363
|
+
const clipped = truncateTrajectoryAnalysisLines(lines, bodyBudget);
|
|
364
|
+
return clipped.length === 0 ? "" : `${header}
|
|
365
|
+
${clipped.join("\n")}`;
|
|
366
|
+
}
|
|
367
|
+
async function collectTurnReferenceEvidence(options) {
|
|
368
|
+
if (!options.sessionId) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const references = collectExplicitTurnReferences(options.query).slice(
|
|
372
|
+
0,
|
|
373
|
+
options.maxReferences
|
|
374
|
+
);
|
|
375
|
+
if (references.length === 0) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
await collectContentLabelReferenceEvidence({
|
|
379
|
+
engine: options.engine,
|
|
380
|
+
sessionId: options.sessionId,
|
|
381
|
+
query: options.query,
|
|
382
|
+
references,
|
|
383
|
+
evidenceItems: options.evidenceItems,
|
|
384
|
+
seenTurns: options.seenTurns
|
|
385
|
+
});
|
|
386
|
+
const windows = /* @__PURE__ */ new Map();
|
|
387
|
+
for (const reference of references) {
|
|
388
|
+
for (const center of candidateTurnIndexesForReference(reference)) {
|
|
389
|
+
if (center < 0) {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const fromTurn = Math.max(0, center - TURN_REFERENCE_WINDOW_RADIUS);
|
|
393
|
+
const toTurn = center + TURN_REFERENCE_WINDOW_RADIUS;
|
|
394
|
+
windows.set(`${fromTurn}:${toTurn}`, { fromTurn, toTurn });
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
for (const window of [...windows.values()].sort(
|
|
398
|
+
(left, right) => left.fromTurn - right.fromTurn || left.toTurn - right.toTurn
|
|
399
|
+
)) {
|
|
400
|
+
const expanded = await options.engine.expandContext(
|
|
401
|
+
options.sessionId,
|
|
402
|
+
window.fromTurn,
|
|
403
|
+
window.toTurn,
|
|
404
|
+
2e3
|
|
405
|
+
);
|
|
406
|
+
appendExpandedEvidence(
|
|
407
|
+
options.evidenceItems,
|
|
408
|
+
options.seenTurns,
|
|
409
|
+
options.sessionId,
|
|
410
|
+
expanded
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
async function collectFocusedTranscriptCueEvidence(options) {
|
|
415
|
+
if (!options.sessionId || !options.engine.getStats) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const matchers = focusedTranscriptCueMatchers(options.query);
|
|
419
|
+
if (matchers.length === 0) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const stats = await options.engine.getStats(options.sessionId);
|
|
423
|
+
const expansionEnd = normalizeTurnExpansionEnd(stats);
|
|
424
|
+
if (expansionEnd < 0) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const messages = await options.engine.expandContext(
|
|
428
|
+
options.sessionId,
|
|
429
|
+
0,
|
|
430
|
+
expansionEnd,
|
|
431
|
+
FOCUSED_TRANSCRIPT_SCAN_MAX_TOKENS
|
|
432
|
+
);
|
|
433
|
+
for (const matcher of matchers) {
|
|
434
|
+
for (const message of messages) {
|
|
435
|
+
if (!matcher(message.content)) {
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
appendEvidenceItem(options.evidenceItems, options.seenTurns, {
|
|
439
|
+
id: `${options.sessionId}:${message.turn_index}`,
|
|
440
|
+
sessionId: options.sessionId,
|
|
441
|
+
turnIndex: message.turn_index,
|
|
442
|
+
role: message.role,
|
|
443
|
+
content: message.content
|
|
444
|
+
});
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function focusedTranscriptCueMatchers(query) {
|
|
450
|
+
const normalized = query.toLowerCase().replace(/[^a-z0-9+#.-]+/g, " ").trim();
|
|
451
|
+
const matchers = [];
|
|
452
|
+
if (hasSecurityFeatureEnumerationIntent(normalized)) {
|
|
453
|
+
matchers.push((content) => /\bpassword hashing\b/i.test(content));
|
|
454
|
+
matchers.push((content) => /\brole-based access control\b|\bRBAC\b/i.test(content));
|
|
455
|
+
matchers.push(
|
|
456
|
+
(content) => /\baccount lockout\b/i.test(content) && /\bfailed login attempts?\b/i.test(content)
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
if (hasTemporalScheduleIntent(normalized)) {
|
|
460
|
+
matchers.push(
|
|
461
|
+
(content) => /\btransaction management\b/i.test(content) && /\bdeployment\b/i.test(content) && /\bmilestones?\b/i.test(content)
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
if (hasFlaskRouteContradictionIntent(normalized)) {
|
|
465
|
+
matchers.push(
|
|
466
|
+
(content) => /\bnever written\b.{0,80}\bFlask routes?\b/i.test(content) && /\bhandled HTTP requests?\b/i.test(content)
|
|
467
|
+
);
|
|
468
|
+
matchers.push(
|
|
469
|
+
(content) => /\bbasic homepage route\b/i.test(content) || /\b@app\.route\(['"]\/['"]\)/i.test(content) && /\bhomepage\b/i.test(content)
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
if (hasFlaskLoginContradictionIntent(normalized)) {
|
|
473
|
+
matchers.push(
|
|
474
|
+
(content) => /\bnever\b.{0,80}\bintegrated\b.{0,80}\bFlask-Login\b/i.test(content) || /\bFlask-Login\b/i.test(content) && /\bstarting from scratch\b/i.test(content)
|
|
475
|
+
);
|
|
476
|
+
matchers.push(
|
|
477
|
+
(content) => /\bFlask-Login\s+v?0\.6\.2\b/i.test(content) && /\bsession management\b/i.test(content)
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
if (hasSecurityDatabaseChallengeSummaryIntent(normalized)) {
|
|
481
|
+
matchers.push(
|
|
482
|
+
(content) => /\bWerkzeug\.security\b/i.test(content) || /\bpbkdf2:sha256\b/i.test(content)
|
|
483
|
+
);
|
|
484
|
+
matchers.push(
|
|
485
|
+
(content) => /\bUNIQUE constraint\b/i.test(content) && /\bUUID\b/i.test(content)
|
|
486
|
+
);
|
|
487
|
+
matchers.push((content) => /\bOperationalError\b/i.test(content));
|
|
488
|
+
matchers.push(
|
|
489
|
+
(content) => /\bCSRF\b/i.test(content) && /\b(?:hidden_tag|WTF_CSRF_ENABLED|cookies?)\b/i.test(content)
|
|
490
|
+
);
|
|
491
|
+
matchers.push(
|
|
492
|
+
(content) => /\baccount lockout\b/i.test(content) && /\bRedis\b/i.test(content) && /\b(?:atomic|expiry|expire|reset(?:ting)? counters?|rate limiting)\b/i.test(content)
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
if (hasProjectProgressSummaryIntent(normalized)) {
|
|
496
|
+
matchers.push(
|
|
497
|
+
(content) => /\b(?:data visualization|visualization)\b/i.test(content) && /\b(?:expenses?|income|analytics|budget tracker)\b/i.test(content)
|
|
498
|
+
);
|
|
499
|
+
matchers.push(
|
|
500
|
+
(content) => /\bApril 15\b/i.test(content) && /\bMVP\b/i.test(content)
|
|
501
|
+
);
|
|
502
|
+
matchers.push(
|
|
503
|
+
(content) => /\bauthentication\b/i.test(content) && /\btransaction management\b/i.test(content) && /\banalytics\b/i.test(content) && /\bdeployment\b/i.test(content)
|
|
504
|
+
);
|
|
505
|
+
matchers.push(
|
|
506
|
+
(content) => /\bpassword hashing\b/i.test(content)
|
|
507
|
+
);
|
|
508
|
+
matchers.push(
|
|
509
|
+
(content) => /\btoken-based authentication\b|\bJWT\b|\baccess_token\b/i.test(content)
|
|
510
|
+
);
|
|
511
|
+
matchers.push(
|
|
512
|
+
(content) => /\brole-based access control\b|\bRBAC\b/i.test(content)
|
|
513
|
+
);
|
|
514
|
+
matchers.push(
|
|
515
|
+
(content) => /\binput validation\b|\bvalidate and sanitize\b|\bSQL injection\b/i.test(content)
|
|
516
|
+
);
|
|
517
|
+
matchers.push(
|
|
518
|
+
(content) => /\b(?:stronger password hashing|password hashing|token-based authentication|role-based access control|input validation)\b/i.test(content)
|
|
519
|
+
);
|
|
520
|
+
matchers.push(
|
|
521
|
+
(content) => /\bConfluence\b/i.test(content) && /\b(?:tables?|diagrams?)\b/i.test(content)
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
if (hasSoftwareProjectOrderingIntent(normalized)) {
|
|
525
|
+
matchers.push(
|
|
526
|
+
(content) => /\bcore functionality\b/i.test(content) && /\b(?:budget tracker|authentication|expense tracking|data visualization)\b/i.test(content)
|
|
527
|
+
);
|
|
528
|
+
matchers.push(
|
|
529
|
+
(content) => /\btransaction CRUD\b/i.test(content) || /\btransactions?\b/i.test(content) && /\bCRUD\b/i.test(content)
|
|
530
|
+
);
|
|
531
|
+
matchers.push(
|
|
532
|
+
(content) => /\bdeployment configuration\b/i.test(content) || /\bRender\.com\b/i.test(content) && /\b(?:deployment|configuration|PostgreSQL)\b/i.test(content)
|
|
533
|
+
);
|
|
534
|
+
matchers.push(
|
|
535
|
+
(content) => /\bintegration test coverage\b/i.test(content) || /\bintegration tests?\b/i.test(content)
|
|
536
|
+
);
|
|
537
|
+
matchers.push(
|
|
538
|
+
(content) => /\bdeployment and test improvements\b/i.test(content) || /\bdeployment\b/i.test(content) && /\b(?:final testing|bug fixes|test improvements)\b/i.test(content)
|
|
539
|
+
);
|
|
540
|
+
matchers.push(
|
|
541
|
+
(content) => /\btransaction error handling\b/i.test(content) || /\btransactions?\b/i.test(content) && /\b(?:OperationalError|error handling)\b/i.test(content)
|
|
542
|
+
);
|
|
543
|
+
matchers.push(
|
|
544
|
+
(content) => /\bsecurity hardening\b/i.test(content) || /\bsecurity\b/i.test(content) && /\bdeployment\b/i.test(content)
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
if (hasSprintTaskOrganizationIntent(normalized)) {
|
|
548
|
+
matchers.push((content) => /\bdevelopment environment\b|\binitial project structure\b/i.test(content));
|
|
549
|
+
matchers.push((content) => /\bdatabase schema\b/i.test(content));
|
|
550
|
+
matchers.push((content) => /\bregistration and login\b|\buser registration\b.{0,80}\blogin\b/i.test(content));
|
|
551
|
+
matchers.push((content) => /\bvalidation\b/i.test(content));
|
|
552
|
+
matchers.push((content) => /\bunit tests?\b/i.test(content));
|
|
553
|
+
matchers.push(
|
|
554
|
+
(content) => /\bfrontend\b/i.test(content) && /\b(?:forms?|integrat(?:e|ing|ion))\b/i.test(content)
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
return matchers;
|
|
558
|
+
}
|
|
559
|
+
async function collectContentLabelReferenceEvidence(options) {
|
|
560
|
+
const resolved = /* @__PURE__ */ new Set();
|
|
561
|
+
for (const reference of options.references) {
|
|
562
|
+
if (reference.includeDirectTurn) {
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
const hits = await searchReferenceContentLabels(
|
|
566
|
+
options.engine,
|
|
567
|
+
reference.number,
|
|
568
|
+
options.sessionId
|
|
569
|
+
);
|
|
570
|
+
if (hits.length === 0) {
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
resolved.add(reference.number);
|
|
574
|
+
let appendedWindows = 0;
|
|
575
|
+
for (const hit of hits) {
|
|
576
|
+
if (appendedWindows >= CONTENT_LABEL_MAX_PAIRED_WINDOWS_PER_REFERENCE) {
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
const { fromTurn, toTurn } = contentLabelEvidenceWindow(hit, {
|
|
580
|
+
includeSuccessor: hasSuccessorTrajectoryIntent(options.query)
|
|
581
|
+
});
|
|
582
|
+
const expanded = await options.engine.expandContext(
|
|
583
|
+
hit.session_id,
|
|
584
|
+
fromTurn,
|
|
585
|
+
toTurn,
|
|
586
|
+
CONTENT_LABEL_MAX_TOKENS
|
|
587
|
+
);
|
|
588
|
+
if (!expandedHasPairedTrajectoryLabels(expanded, reference.number)) {
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
if (expanded.length === 0) {
|
|
592
|
+
appendEvidenceItem(options.evidenceItems, options.seenTurns, {
|
|
593
|
+
id: `${hit.session_id}:${hit.turn_index}`,
|
|
594
|
+
sessionId: hit.session_id,
|
|
595
|
+
turnIndex: hit.turn_index,
|
|
596
|
+
role: hit.role,
|
|
597
|
+
content: hit.content
|
|
598
|
+
});
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
appendExpandedEvidence(
|
|
602
|
+
options.evidenceItems,
|
|
603
|
+
options.seenTurns,
|
|
604
|
+
hit.session_id,
|
|
605
|
+
expanded
|
|
606
|
+
);
|
|
607
|
+
appendedWindows += 1;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return resolved;
|
|
611
|
+
}
|
|
612
|
+
async function searchReferenceContentLabels(engine, referenceNumber, sessionId) {
|
|
613
|
+
const hits = /* @__PURE__ */ new Map();
|
|
614
|
+
for (const labelKind of ["action", "observation"]) {
|
|
615
|
+
const label = labelKind === "action" ? "Action" : "Observation";
|
|
616
|
+
for (const query of [`[${label} ${referenceNumber}]`, `${label} ${referenceNumber}`]) {
|
|
617
|
+
const results = await engine.searchContextFull(
|
|
618
|
+
query,
|
|
619
|
+
CONTENT_LABEL_SEARCH_LIMIT,
|
|
620
|
+
sessionId
|
|
621
|
+
);
|
|
622
|
+
for (const result of results) {
|
|
623
|
+
if (!isReferenceLabelRole(result.role, labelKind) || !contentHasReferenceLabel(result.content, labelKind, referenceNumber)) {
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
hits.set(`${result.session_id}:${result.turn_index}:${labelKind}`, {
|
|
627
|
+
turn_index: result.turn_index,
|
|
628
|
+
role: result.role,
|
|
629
|
+
content: result.content,
|
|
630
|
+
session_id: result.session_id,
|
|
631
|
+
labelKind
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const numericCandidates = candidateTurnIndexesForReference({
|
|
637
|
+
number: referenceNumber,
|
|
638
|
+
includeDirectTurn: false
|
|
639
|
+
});
|
|
640
|
+
return [...hits.values()].sort((left, right) => {
|
|
641
|
+
const sessionOrder = left.session_id.localeCompare(right.session_id);
|
|
642
|
+
if (sessionOrder !== 0) {
|
|
643
|
+
return sessionOrder;
|
|
644
|
+
}
|
|
645
|
+
const leftDistance = nearestTurnDistance(left.turn_index, numericCandidates);
|
|
646
|
+
const rightDistance = nearestTurnDistance(right.turn_index, numericCandidates);
|
|
647
|
+
if (leftDistance !== rightDistance) {
|
|
648
|
+
return leftDistance - rightDistance;
|
|
649
|
+
}
|
|
650
|
+
return left.turn_index - right.turn_index || left.labelKind.localeCompare(right.labelKind);
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
function nearestTurnDistance(turnIndex, candidates) {
|
|
654
|
+
let nearest = Number.POSITIVE_INFINITY;
|
|
655
|
+
for (const candidate of candidates) {
|
|
656
|
+
nearest = Math.min(nearest, Math.abs(turnIndex - candidate));
|
|
657
|
+
}
|
|
658
|
+
return nearest;
|
|
659
|
+
}
|
|
660
|
+
function contentLabelEvidenceWindow(hit, options = {}) {
|
|
661
|
+
const successorTurns = options.includeSuccessor === true ? 2 : 0;
|
|
662
|
+
if (hit.labelKind === "action") {
|
|
663
|
+
return {
|
|
664
|
+
fromTurn: Math.max(0, hit.turn_index - 1),
|
|
665
|
+
toTurn: hit.turn_index + 1 + successorTurns
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
return {
|
|
669
|
+
fromTurn: Math.max(0, hit.turn_index - 1),
|
|
670
|
+
toTurn: hit.turn_index + successorTurns
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
function hasSuccessorTrajectoryIntent(query) {
|
|
674
|
+
const raw = query.toLowerCase();
|
|
675
|
+
const normalized = raw.replace(/[^a-z0-9]+/g, " ").trim();
|
|
676
|
+
if ([
|
|
677
|
+
/\bafter\s+(?:step|action|observation|turn)\s+\d+\b/,
|
|
678
|
+
/\b(?:next|following|subsequent|successor)\s+(?:step|action|observation|turn)\b/,
|
|
679
|
+
/\b(?:step|action|observation|turn)\s+\d+\s+(?:then|and then)\b/,
|
|
680
|
+
/\bwhat\s+(?:happened|came|occurred)\s+next\b/
|
|
681
|
+
].some((pattern) => pattern.test(normalized))) {
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
if (!hasLoopExitIntent(normalized)) {
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
687
|
+
if (!hasBoundedTrajectoryReference(raw)) {
|
|
688
|
+
return true;
|
|
689
|
+
}
|
|
690
|
+
return !asksForActionInsideBoundedRange(normalized) && hasNamedTrajectoryActionCue(normalized);
|
|
691
|
+
}
|
|
692
|
+
function hasBoundedTrajectoryReference(query) {
|
|
693
|
+
return hasBoundedTrajectoryRange(query) || hasSingleTrajectoryReference(query);
|
|
694
|
+
}
|
|
695
|
+
function hasLoopExitIntent(normalizedQuery) {
|
|
696
|
+
const exitVerbs = "(?:breaks?|breaking|broke|ends?|ending|ended|stops?|stopping|stopped)";
|
|
697
|
+
const loopNouns = "(?:loop|cycle|pattern|sequence)";
|
|
698
|
+
return [
|
|
699
|
+
new RegExp(
|
|
700
|
+
`\\b${exitVerbs}\\s+(?:out\\s+of\\s+)?(?:this|that|the|a|an)?\\s*${loopNouns}\\b`
|
|
701
|
+
),
|
|
702
|
+
new RegExp(
|
|
703
|
+
`\\b${loopNouns}\\s+(?:${exitVerbs}|is\\s+${exitVerbs}|was\\s+${exitVerbs})\\b`
|
|
704
|
+
)
|
|
705
|
+
].some((pattern) => pattern.test(normalizedQuery));
|
|
706
|
+
}
|
|
707
|
+
function hasBoundedTrajectoryRange(query) {
|
|
708
|
+
return [
|
|
709
|
+
/\b(?:between|from|in|during|within)\s+(?:steps?|actions?|observations?|turns?)\s+#?\d+\s*(?:-|\u2013|\u2014|\bto\b|\bthrough\b|\bthru\b|\band\b)\s*(?:(?:steps?|actions?|observations?|turns?)\s+)?#?\d+\b/,
|
|
710
|
+
/\b(?:steps?|actions?|observations?|turns?)\s+#?\d+\s*(?:-|\u2013|\u2014|\bto\b|\bthrough\b|\bthru\b)\s*(?:(?:steps?|actions?|observations?|turns?)\s+)?#?\d+\b/
|
|
711
|
+
].some((pattern) => pattern.test(query));
|
|
712
|
+
}
|
|
713
|
+
function hasSingleTrajectoryReference(query) {
|
|
714
|
+
return /\b(?:in|during|within|at|on)?\s*(?:steps?|actions?|observations?|turns?)\s+#?\d+\b/.test(
|
|
715
|
+
query
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
function asksForActionInsideBoundedRange(normalizedQuery) {
|
|
719
|
+
return /\b(?:which|what)\s+(?:single\s+)?(?:action|move|step|maneuver)\s+(?:broke|breaks|breaking|ended|ends|stopped|stops|mattered|accomplished|advanced)\b/.test(
|
|
720
|
+
normalizedQuery
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
function hasNamedTrajectoryActionCue(normalizedQuery) {
|
|
724
|
+
const actions = "(?:up|down|left|right|wait|stay|push|pull|open|close|use|enter|exit)";
|
|
725
|
+
return new RegExp(
|
|
726
|
+
`\\b(?:${actions}\\s+(?:action|move|step|maneuver)|(?:action|move|step|maneuver)\\s+${actions})\\b`
|
|
727
|
+
).test(normalizedQuery);
|
|
728
|
+
}
|
|
729
|
+
function contentHasReferenceLabel(content, labelKind, referenceNumber) {
|
|
730
|
+
const label = labelKind === "action" ? "Action" : "Observation";
|
|
731
|
+
const escapedNumber = String(referenceNumber).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
732
|
+
return new RegExp(
|
|
733
|
+
`^\\s*\\[\\s*${label}\\s+${escapedNumber}\\s*\\]\\s*(?::\\s*)?`,
|
|
734
|
+
"i"
|
|
735
|
+
).test(content);
|
|
736
|
+
}
|
|
737
|
+
function isReferenceLabelRole(role, labelKind) {
|
|
738
|
+
if (labelKind === "action") {
|
|
739
|
+
return role === "user";
|
|
740
|
+
}
|
|
741
|
+
return role === "assistant";
|
|
742
|
+
}
|
|
743
|
+
function expandedHasPairedTrajectoryLabels(expanded, referenceNumber) {
|
|
744
|
+
let hasAction = false;
|
|
745
|
+
let hasObservation = false;
|
|
746
|
+
for (const message of expanded) {
|
|
747
|
+
if (isReferenceLabelRole(message.role, "action") && contentHasReferenceLabel(message.content, "action", referenceNumber)) {
|
|
748
|
+
hasAction = true;
|
|
749
|
+
}
|
|
750
|
+
if (isReferenceLabelRole(message.role, "observation") && contentHasReferenceLabel(message.content, "observation", referenceNumber)) {
|
|
751
|
+
hasObservation = true;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return hasAction && hasObservation;
|
|
755
|
+
}
|
|
756
|
+
async function collectLexicalCueEvidence(options) {
|
|
757
|
+
const cues = prioritizeLexicalCueSearchCues(
|
|
758
|
+
options.query,
|
|
759
|
+
collectLexicalCues(options.query, {
|
|
760
|
+
includeBenchmarkAnchorCues: options.includeBenchmarkAnchorCues,
|
|
761
|
+
includeContentLexicalCues: options.includeContentLexicalCues,
|
|
762
|
+
includeStructuredPlanCues: options.includeStructuredPlanCues
|
|
763
|
+
})
|
|
764
|
+
).slice(0, options.maxReferences);
|
|
765
|
+
const preferLatest = hasLatestStateIntent(options.query);
|
|
766
|
+
for (const cue of cues) {
|
|
767
|
+
const exactHitFirst = isHighPriorityImplementationCue(options.query, cue);
|
|
768
|
+
const results = sortLexicalCueResults(
|
|
769
|
+
await options.engine.searchContextFull(
|
|
770
|
+
cue,
|
|
771
|
+
lexicalCueSearchLimit(options.query, cue),
|
|
772
|
+
options.sessionId
|
|
773
|
+
),
|
|
774
|
+
preferLatest,
|
|
775
|
+
options.query
|
|
776
|
+
);
|
|
777
|
+
for (const result of results) {
|
|
778
|
+
const windowRadius = preferLatest ? 0 : LEXICAL_CUE_WINDOW_RADIUS;
|
|
779
|
+
const fromTurn = Math.max(0, result.turn_index - windowRadius);
|
|
780
|
+
const toTurn = result.turn_index + windowRadius;
|
|
781
|
+
const expanded = await options.engine.expandContext(
|
|
782
|
+
result.session_id,
|
|
783
|
+
fromTurn,
|
|
784
|
+
toTurn,
|
|
785
|
+
LEXICAL_CUE_MAX_TOKENS
|
|
786
|
+
);
|
|
787
|
+
if (expanded.length === 0) {
|
|
788
|
+
appendEvidenceItem(options.evidenceItems, options.seenTurns, {
|
|
789
|
+
id: `${result.session_id}:${result.turn_index}`,
|
|
790
|
+
sessionId: result.session_id,
|
|
791
|
+
turnIndex: result.turn_index,
|
|
792
|
+
role: result.role,
|
|
793
|
+
content: result.content,
|
|
794
|
+
...typeof result.score === "number" ? { score: result.score } : {}
|
|
795
|
+
});
|
|
796
|
+
continue;
|
|
797
|
+
}
|
|
798
|
+
if (exactHitFirst) {
|
|
799
|
+
appendEvidenceItem(options.evidenceItems, options.seenTurns, {
|
|
800
|
+
id: `${result.session_id}:${result.turn_index}`,
|
|
801
|
+
sessionId: result.session_id,
|
|
802
|
+
turnIndex: result.turn_index,
|
|
803
|
+
role: result.role,
|
|
804
|
+
content: result.content,
|
|
805
|
+
...typeof result.score === "number" ? { score: result.score } : {}
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
appendExpandedEvidence(
|
|
809
|
+
options.evidenceItems,
|
|
810
|
+
options.seenTurns,
|
|
811
|
+
result.session_id,
|
|
812
|
+
expanded
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function hasTrajectoryAnalysisIntent(query) {
|
|
818
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
819
|
+
if (collectExplicitTurnReferences(query).length > 0) {
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
return [
|
|
823
|
+
/\bbefore\s+(?:step|action|observation)\s+\d+\b/,
|
|
824
|
+
/\b(?:until|through|thru)\s+(?:step|action|observation)\s+\d+\b/,
|
|
825
|
+
/\bup\s+to\s+(?:and\s+including\s+)?(?:step|action|observation)\s+\d+\b/,
|
|
826
|
+
/\bwhat\s+sequence\s+of\s+actions\s+would\s+transform\b/,
|
|
827
|
+
/\bactions?\s+were\s+performed\b/,
|
|
828
|
+
/\bstate\s+of\s+[a-z0-9 _-]+\s+\d+\s+at\s+step\s+\d+\b/,
|
|
829
|
+
/\bwhole\s+changes?\s+history\b/,
|
|
830
|
+
/\bthroughout\s+the\s+trajectory\b/,
|
|
831
|
+
/\binventory\b/,
|
|
832
|
+
/\bcontainers?\b.*\binteracted\b/,
|
|
833
|
+
/\btypes?\s+of\s+actions?\b.*\bfrequen/,
|
|
834
|
+
/\bhow\s+frequently\b/
|
|
835
|
+
].some((pattern) => pattern.test(normalized));
|
|
836
|
+
}
|
|
837
|
+
function parseLabeledTrajectory(messages) {
|
|
838
|
+
const byStep = /* @__PURE__ */ new Map();
|
|
839
|
+
for (const message of messages) {
|
|
840
|
+
const action = parseTrajectoryLabel(message.content, "Action");
|
|
841
|
+
if (action && isReferenceLabelRole(message.role, "action")) {
|
|
842
|
+
const step = getOrCreateTrajectoryStep(byStep, action.step);
|
|
843
|
+
step.action = action.value;
|
|
844
|
+
step.actionTurnIndex = message.turn_index;
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
const observation = parseTrajectoryLabel(message.content, "Observation");
|
|
848
|
+
if (observation && isReferenceLabelRole(message.role, "observation")) {
|
|
849
|
+
const step = getOrCreateTrajectoryStep(byStep, observation.step);
|
|
850
|
+
step.observation = observation.value;
|
|
851
|
+
step.observationTurnIndex = message.turn_index;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return [...byStep.values()].sort((left, right) => left.step - right.step);
|
|
855
|
+
}
|
|
856
|
+
function parseTrajectoryLabel(content, label) {
|
|
857
|
+
const match = new RegExp(
|
|
858
|
+
`^\\s*\\[\\s*${label}\\s+(\\d+)\\s*\\]\\s*(?::\\s*)?([\\s\\S]*)$`,
|
|
859
|
+
"i"
|
|
860
|
+
).exec(content);
|
|
861
|
+
if (!match) {
|
|
862
|
+
return void 0;
|
|
863
|
+
}
|
|
864
|
+
const step = parseNonNegativeIntegerToken(match[1] ?? "");
|
|
865
|
+
if (step === void 0) {
|
|
866
|
+
return void 0;
|
|
867
|
+
}
|
|
868
|
+
const value = (match[2] ?? "").trim();
|
|
869
|
+
return { step, value };
|
|
870
|
+
}
|
|
871
|
+
function getOrCreateTrajectoryStep(byStep, stepNumber) {
|
|
872
|
+
const existing = byStep.get(stepNumber);
|
|
873
|
+
if (existing) {
|
|
874
|
+
return existing;
|
|
875
|
+
}
|
|
876
|
+
const created = { step: stepNumber };
|
|
877
|
+
byStep.set(stepNumber, created);
|
|
878
|
+
return created;
|
|
879
|
+
}
|
|
880
|
+
function inferTrajectoryBounds(query, trajectory) {
|
|
881
|
+
const minStep = trajectory[0]?.step ?? 0;
|
|
882
|
+
const maxStep = trajectory[trajectory.length - 1]?.step ?? minStep;
|
|
883
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
884
|
+
const before = firstMatchInteger(
|
|
885
|
+
normalized,
|
|
886
|
+
/\bbefore\s+(?:step|action|observation)\s+(\d+)\b/
|
|
887
|
+
);
|
|
888
|
+
if (before !== void 0) {
|
|
889
|
+
return clampTrajectoryBounds(minStep, before - 1, minStep, maxStep, "before");
|
|
890
|
+
}
|
|
891
|
+
const until = firstMatchInteger(
|
|
892
|
+
normalized,
|
|
893
|
+
/\b(?:until|through|thru)\s+(?:step|action|observation)\s+(\d+)\b/
|
|
894
|
+
) ?? firstMatchInteger(
|
|
895
|
+
normalized,
|
|
896
|
+
/\bup\s+to\s+(?:and\s+including\s+)?(?:step|action|observation)\s+(\d+)\b/
|
|
897
|
+
);
|
|
898
|
+
if (until !== void 0) {
|
|
899
|
+
return clampTrajectoryBounds(minStep, until, minStep, maxStep, "through");
|
|
900
|
+
}
|
|
901
|
+
const explicitRange = extractExplicitTrajectoryRange(normalized);
|
|
902
|
+
if (explicitRange) {
|
|
903
|
+
return clampTrajectoryBounds(
|
|
904
|
+
explicitRange.start,
|
|
905
|
+
explicitRange.end,
|
|
906
|
+
minStep,
|
|
907
|
+
maxStep,
|
|
908
|
+
"range"
|
|
909
|
+
);
|
|
910
|
+
}
|
|
911
|
+
const references = collectExplicitTurnReferences(query).map((reference) => reference.number);
|
|
912
|
+
if (references.length > 1) {
|
|
913
|
+
return clampTrajectoryBounds(
|
|
914
|
+
Math.min(...references),
|
|
915
|
+
Math.max(...references),
|
|
916
|
+
minStep,
|
|
917
|
+
maxStep,
|
|
918
|
+
"references"
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
const atStep = firstMatchInteger(
|
|
922
|
+
normalized,
|
|
923
|
+
/\b(?:at|in|on)\s+(?:step|action|observation)\s+(\d+)\b/
|
|
924
|
+
);
|
|
925
|
+
if (atStep !== void 0) {
|
|
926
|
+
return clampTrajectoryBounds(minStep, atStep, minStep, maxStep, "at");
|
|
927
|
+
}
|
|
928
|
+
return { start: minStep, end: maxStep, reason: "full" };
|
|
929
|
+
}
|
|
930
|
+
function buildTrajectoryAnalysisLines(query, trajectory, bounds) {
|
|
931
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
932
|
+
const explicitReferences = collectExplicitTurnReferences(query);
|
|
933
|
+
const lines = [
|
|
934
|
+
`Analyzed labeled action/observation transcript window: steps ${bounds.start}-${bounds.end} (${bounds.reason}).`
|
|
935
|
+
];
|
|
936
|
+
const hasQuotedObservationPair = extractQuotedObservations(query).length >= 2;
|
|
937
|
+
const transition = findObservationTransition(query, trajectory);
|
|
938
|
+
const entities = extractNumberedEntities(query);
|
|
939
|
+
const asksActionRange = asksForTrajectoryActionRange(normalized) || transition !== void 0;
|
|
940
|
+
const asksFrequency = asksForActionFrequency(normalized);
|
|
941
|
+
const asksInventory = /\binventory\b/.test(normalized);
|
|
942
|
+
const asksLocation = /\blocations?\b/.test(normalized) || /\bwhere\b/.test(normalized);
|
|
943
|
+
const asksContainerHistory = /\bcontainers?\b/.test(normalized) || /\binteracted\b/.test(normalized);
|
|
944
|
+
const asksContainerObjectTransfer = /\b(?:objects?|items?)\b/.test(normalized) && /\b(?:placed|put|inserted|moved|removed)\b/.test(normalized);
|
|
945
|
+
const asksEntityState = /\bstate\b/.test(normalized) || /\bchanges?\s+history\b/.test(normalized) || /\bwhole\s+changes?\s+history\b/.test(normalized);
|
|
946
|
+
const asksResultingObservation = /\bresulting\s+state\b/.test(normalized) || /\bprovide\s+the\s+full\s+observation\b/.test(normalized) || /\bwhat\s+will\s+be\s+the\s+resulting\b/.test(normalized);
|
|
947
|
+
if (transition) {
|
|
948
|
+
lines.push(
|
|
949
|
+
`Matched quoted observations: Observation ${transition.fromStep} -> Observation ${transition.toStep}.`
|
|
950
|
+
);
|
|
951
|
+
appendActionSequenceSummary(
|
|
952
|
+
lines,
|
|
953
|
+
trajectory,
|
|
954
|
+
transition.fromStep + 1,
|
|
955
|
+
transition.toStep,
|
|
956
|
+
"Action sequence that transforms the quoted observations:"
|
|
957
|
+
);
|
|
958
|
+
appendActionRangeLines(lines, trajectory, transition.fromStep + 1, transition.toStep, {
|
|
959
|
+
includeObservations: true,
|
|
960
|
+
heading: "Detailed transition evidence:"
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
if (asksResultingObservation && !transition && explicitReferences.length > 0) {
|
|
964
|
+
appendActionRangeLines(lines, trajectory, bounds.start, bounds.end, {
|
|
965
|
+
includeObservations: true,
|
|
966
|
+
heading: "Referenced action sequence and observations:"
|
|
967
|
+
});
|
|
968
|
+
appendResultingObservationLine(lines, trajectory, bounds.end);
|
|
969
|
+
}
|
|
970
|
+
if (asksActionRange && !transition && entities.length === 0 && !hasQuotedObservationPair) {
|
|
971
|
+
if (bounds.reason === "references") {
|
|
972
|
+
appendReferencedTrajectoryLines(lines, trajectory, explicitReferences, {
|
|
973
|
+
includeObservations: false,
|
|
974
|
+
heading: "Actions at referenced steps:"
|
|
975
|
+
});
|
|
976
|
+
} else {
|
|
977
|
+
appendActionRangeLines(lines, trajectory, bounds.start, bounds.end, {
|
|
978
|
+
includeObservations: false,
|
|
979
|
+
heading: "Actions in requested step window:"
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
if (asksFrequency) {
|
|
984
|
+
appendActionFrequencyLines(lines, trajectory, bounds);
|
|
985
|
+
}
|
|
986
|
+
appendSpatialTrajectoryInferenceLines(lines, query, trajectory, bounds);
|
|
987
|
+
if (asksInventory) {
|
|
988
|
+
appendInventoryChangeLines(lines, trajectory, bounds);
|
|
989
|
+
}
|
|
990
|
+
if (asksContainerHistory) {
|
|
991
|
+
appendContainerStateChangeLines(lines, trajectory, bounds);
|
|
992
|
+
}
|
|
993
|
+
if (entities.length > 0 && (asksEntityState || asksLocation || /actions?\s+were\s+performed/.test(normalized))) {
|
|
994
|
+
appendEntityTimelineLines(lines, trajectory, bounds, entities, {
|
|
995
|
+
includeIndirectMentions: asksLocation,
|
|
996
|
+
includeContainerObjectTransfers: asksContainerObjectTransfer,
|
|
997
|
+
includeMovableStateActions: asksEntityState
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
if (lines.length === 1 && explicitReferences.length > 0) {
|
|
1001
|
+
if (bounds.reason === "references") {
|
|
1002
|
+
appendReferencedTrajectoryLines(lines, trajectory, explicitReferences, {
|
|
1003
|
+
includeObservations: true,
|
|
1004
|
+
heading: "Referenced trajectory evidence:"
|
|
1005
|
+
});
|
|
1006
|
+
} else {
|
|
1007
|
+
appendActionRangeLines(lines, trajectory, bounds.start, bounds.end, {
|
|
1008
|
+
includeObservations: true,
|
|
1009
|
+
heading: "Referenced trajectory evidence:"
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
return lines.length === 1 ? [] : lines;
|
|
1014
|
+
}
|
|
1015
|
+
function appendReferencedTrajectoryLines(lines, trajectory, references, options) {
|
|
1016
|
+
const byStep = new Map(trajectory.map((step) => [step.step, step]));
|
|
1017
|
+
const window = [...new Set(references.map((reference) => reference.number))].sort((left, right) => left - right).map((step) => byStep.get(step)).filter((step) => step !== void 0);
|
|
1018
|
+
if (window.length === 0) {
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
1021
|
+
lines.push(options.heading);
|
|
1022
|
+
for (const step of window) {
|
|
1023
|
+
if (step.action) {
|
|
1024
|
+
lines.push(`[Action ${step.step}]: ${step.action}`);
|
|
1025
|
+
}
|
|
1026
|
+
if (options.includeObservations && step.observation) {
|
|
1027
|
+
lines.push(`[Observation ${step.step}]: ${oneLine(step.observation)}`);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
function appendActionRangeLines(lines, trajectory, start, end, options) {
|
|
1032
|
+
const low = Math.min(start, end);
|
|
1033
|
+
const high = Math.max(start, end);
|
|
1034
|
+
if (high - low + 1 > TRAJECTORY_ANALYSIS_MAX_RANGE_STEPS) {
|
|
1035
|
+
lines.push(`${options.heading} requested range ${low}-${high} is too large for inline expansion.`);
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
const window = trajectory.filter((step) => step.step >= low && step.step <= high);
|
|
1039
|
+
if (window.length === 0) {
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
lines.push(options.heading);
|
|
1043
|
+
for (const step of window) {
|
|
1044
|
+
if (step.action) {
|
|
1045
|
+
lines.push(`[Action ${step.step}]: ${step.action}`);
|
|
1046
|
+
}
|
|
1047
|
+
if (options.includeObservations && step.observation) {
|
|
1048
|
+
lines.push(`[Observation ${step.step}]: ${oneLine(step.observation)}`);
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
function appendActionSequenceSummary(lines, trajectory, start, end, heading) {
|
|
1053
|
+
const low = Math.min(start, end);
|
|
1054
|
+
const high = Math.max(start, end);
|
|
1055
|
+
const actions = trajectory.filter((step) => step.step >= low && step.step <= high && step.action).map((step) => `step ${step.step}: ${step.action}`);
|
|
1056
|
+
if (actions.length === 0) {
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
lines.push(`${heading} ${actions.join("; ")}.`);
|
|
1060
|
+
}
|
|
1061
|
+
function appendResultingObservationLine(lines, trajectory, stepNumber) {
|
|
1062
|
+
const step = trajectory.find((candidate) => candidate.step === stepNumber);
|
|
1063
|
+
if (!step?.observation) {
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
lines.push(`Resulting observation after Action ${step.step}: ${oneLine(step.observation)}`);
|
|
1067
|
+
}
|
|
1068
|
+
function appendSpatialTrajectoryInferenceLines(lines, query, trajectory, bounds) {
|
|
1069
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
1070
|
+
const wantsRelativePosition = /\brelative\s+position\b/.test(normalized) || /\bsteps?\s+(?:to\s+the\s+)?(?:left|right|up|down)\b/.test(normalized);
|
|
1071
|
+
const wantsRules = /\brules?\b/.test(normalized) || /\bwin\s+condition\b/.test(normalized) || /\bpush(?:able)?\s+word\b/.test(normalized);
|
|
1072
|
+
const wantsStrategicOrCounterfactual = /\b(?:blocked|counterproductive|failed|failure|infer|inferred|objective|instead|alternative|progress|goal|necessary|critical|strategic|successful|relevant|ineffective|only\s+one|caused?\s+a\s+change)\b/.test(
|
|
1073
|
+
normalized
|
|
1074
|
+
);
|
|
1075
|
+
const wantsStateTransformation = /\b(?:appear|appears|appeared|disappear|disappears|disappeared|temporary|transformation|hidden\s+state|reversed)\b/.test(
|
|
1076
|
+
normalized
|
|
1077
|
+
);
|
|
1078
|
+
if (!wantsRelativePosition && !wantsRules && !wantsStrategicOrCounterfactual && !wantsStateTransformation) {
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
const window = spatialTrajectoryWindowForQuery(query, trajectory, bounds);
|
|
1082
|
+
const counterfactualContactLines = collectCounterfactualContactLines(query, trajectory, normalized);
|
|
1083
|
+
if (counterfactualContactLines.length > 0) {
|
|
1084
|
+
lines.push("Counterfactual contact cues:");
|
|
1085
|
+
lines.push(...counterfactualContactLines);
|
|
1086
|
+
}
|
|
1087
|
+
const selfReversingLines = collectSelfReversingProgressLines(query, window, normalized);
|
|
1088
|
+
if (selfReversingLines.length > 0) {
|
|
1089
|
+
lines.push("Self-reversing sequence cues:");
|
|
1090
|
+
lines.push(...selfReversingLines);
|
|
1091
|
+
}
|
|
1092
|
+
const onlyEffectiveActionLines = collectOnlyEffectiveActionLines(window, normalized);
|
|
1093
|
+
if (onlyEffectiveActionLines.length > 0) {
|
|
1094
|
+
lines.push("Only-effective action cues:");
|
|
1095
|
+
lines.push(...onlyEffectiveActionLines);
|
|
1096
|
+
}
|
|
1097
|
+
const pushedPhraseGroupLines = collectPushedPhraseGroupShiftLines(query, window, normalized);
|
|
1098
|
+
if (pushedPhraseGroupLines.length > 0) {
|
|
1099
|
+
lines.push("Pushed phrase-group shift cues:");
|
|
1100
|
+
lines.push(...pushedPhraseGroupLines);
|
|
1101
|
+
}
|
|
1102
|
+
const suppressMovementProgressEvidence = onlyEffectiveActionLines.length > 0 || selfReversingLines.some(
|
|
1103
|
+
(line) => line.includes("named movement sequence") && /\bwhich\b/.test(normalized) && /\brelevant\b/.test(normalized)
|
|
1104
|
+
) || pushedPhraseGroupLines.length > 0;
|
|
1105
|
+
const actionMovementLines = suppressMovementProgressEvidence ? [] : collectActionMovementSummaryLines(query, window, bounds);
|
|
1106
|
+
if (actionMovementLines.length > 0) {
|
|
1107
|
+
lines.push("Action movement summary cues:");
|
|
1108
|
+
lines.push(...actionMovementLines);
|
|
1109
|
+
}
|
|
1110
|
+
const movementLines = suppressMovementProgressEvidence ? [] : collectMovementDeltaLines(query, window);
|
|
1111
|
+
if (movementLines.length > 0) {
|
|
1112
|
+
lines.push("Relative-position movement cues:");
|
|
1113
|
+
lines.push(...movementLines);
|
|
1114
|
+
}
|
|
1115
|
+
const objectAlignmentWindow = objectAlignmentWindowForQuery(query, trajectory, bounds);
|
|
1116
|
+
const objectAlignmentLines = collectObjectAlignmentLines(
|
|
1117
|
+
query,
|
|
1118
|
+
objectAlignmentWindow,
|
|
1119
|
+
normalized
|
|
1120
|
+
);
|
|
1121
|
+
if (objectAlignmentLines.length > 0) {
|
|
1122
|
+
lines.push("Object alignment cues:");
|
|
1123
|
+
lines.push(...objectAlignmentLines);
|
|
1124
|
+
}
|
|
1125
|
+
const alternativeActionLines = collectAlternativeActionLines(query, trajectory);
|
|
1126
|
+
if (alternativeActionLines.length > 0) {
|
|
1127
|
+
lines.push("Counterfactual action cues:");
|
|
1128
|
+
lines.push(...alternativeActionLines);
|
|
1129
|
+
}
|
|
1130
|
+
const adjacentRuleSetupLines = collectAdjacentRuleSetupLines(window, normalized);
|
|
1131
|
+
if (adjacentRuleSetupLines.length > 0) {
|
|
1132
|
+
lines.push("Adjacent rule-block setup cues:");
|
|
1133
|
+
lines.push(...adjacentRuleSetupLines);
|
|
1134
|
+
}
|
|
1135
|
+
const blockedMoveLines = collectBlockedMoveLines(query, window, normalized);
|
|
1136
|
+
if (blockedMoveLines.length > 0) {
|
|
1137
|
+
lines.push("Blocked-move cues:");
|
|
1138
|
+
lines.push(...blockedMoveLines);
|
|
1139
|
+
}
|
|
1140
|
+
const failedEscapeLines = collectFailedEscapeLines(query, trajectory, normalized);
|
|
1141
|
+
if (failedEscapeLines.length > 0) {
|
|
1142
|
+
lines.push("Failed-move escape cues:");
|
|
1143
|
+
lines.push(...failedEscapeLines);
|
|
1144
|
+
}
|
|
1145
|
+
const sameRelativeTextPushLines = collectSameRelativeTextPushLines(window, normalized);
|
|
1146
|
+
if (sameRelativeTextPushLines.length > 0) {
|
|
1147
|
+
lines.push("Same-relative text-push cues:");
|
|
1148
|
+
lines.push(...sameRelativeTextPushLines);
|
|
1149
|
+
}
|
|
1150
|
+
const failedContactLines = sameRelativeTextPushLines.length > 0 ? [] : collectFailedContactBoundaryLines(window, normalized);
|
|
1151
|
+
if (failedContactLines.length > 0) {
|
|
1152
|
+
lines.push("Failed-push boundary cues:");
|
|
1153
|
+
lines.push(...failedContactLines);
|
|
1154
|
+
}
|
|
1155
|
+
const transformationLines = collectTemporaryRuleTransformationLines(window, normalized);
|
|
1156
|
+
if (transformationLines.length > 0) {
|
|
1157
|
+
lines.push("Temporary transformation cues:");
|
|
1158
|
+
lines.push(...transformationLines);
|
|
1159
|
+
}
|
|
1160
|
+
const wholeConfigurationShiftLines = collectWholeConfigurationShiftLines(window, normalized);
|
|
1161
|
+
if (wholeConfigurationShiftLines.length > 0) {
|
|
1162
|
+
lines.push("Whole-configuration shift cues:");
|
|
1163
|
+
lines.push(...wholeConfigurationShiftLines);
|
|
1164
|
+
}
|
|
1165
|
+
const ruleInterventionLines = collectRuleInterventionStrategyLines(window, normalized);
|
|
1166
|
+
if (ruleInterventionLines.length > 0) {
|
|
1167
|
+
lines.push("Rule-intervention strategy cues:");
|
|
1168
|
+
lines.push(...ruleInterventionLines);
|
|
1169
|
+
}
|
|
1170
|
+
const missingPushTargetLines = collectMissingPushTargetLines(window, normalized);
|
|
1171
|
+
if (missingPushTargetLines.length > 0) {
|
|
1172
|
+
lines.push("Missing-interaction cues:");
|
|
1173
|
+
lines.push(...missingPushTargetLines);
|
|
1174
|
+
}
|
|
1175
|
+
const rulePhraseAlignmentLines = collectRulePhraseAlignmentLines(window, normalized);
|
|
1176
|
+
if (rulePhraseAlignmentLines.length > 0) {
|
|
1177
|
+
lines.push("Rule-phrase alignment cues:");
|
|
1178
|
+
lines.push(...rulePhraseAlignmentLines);
|
|
1179
|
+
}
|
|
1180
|
+
const controlRuleInteractionLines = collectControlRuleInteractionLines(window, normalized);
|
|
1181
|
+
if (controlRuleInteractionLines.length > 0) {
|
|
1182
|
+
lines.push("Control-rule interaction cues:");
|
|
1183
|
+
lines.push(...controlRuleInteractionLines);
|
|
1184
|
+
}
|
|
1185
|
+
const ruleTextPositionLines = suppressMovementProgressEvidence ? [] : collectRuleTextPositionLines(window, normalized);
|
|
1186
|
+
if (ruleTextPositionLines.length > 0) {
|
|
1187
|
+
lines.push("Rule-text positioning cues:");
|
|
1188
|
+
lines.push(...ruleTextPositionLines);
|
|
1189
|
+
}
|
|
1190
|
+
const ruleLines = collectRuleStateLines(window, normalized);
|
|
1191
|
+
if (ruleLines.length > 0) {
|
|
1192
|
+
lines.push("Rule-state cues:");
|
|
1193
|
+
lines.push(...ruleLines);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
function spatialTrajectoryWindowForQuery(query, trajectory, bounds) {
|
|
1197
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
1198
|
+
const minStep = trajectory[0]?.step ?? bounds.start;
|
|
1199
|
+
const maxStep = trajectory[trajectory.length - 1]?.step ?? bounds.end;
|
|
1200
|
+
let start = bounds.start;
|
|
1201
|
+
let end = bounds.end;
|
|
1202
|
+
const references = collectExplicitTurnReferences(query).map((reference) => reference.number);
|
|
1203
|
+
if (references.length > 0 && hasManeuverInterpretationCue(normalized)) {
|
|
1204
|
+
start = Math.min(start, ...references);
|
|
1205
|
+
end = Math.max(end, ...references);
|
|
1206
|
+
}
|
|
1207
|
+
if (references.length > 0 && /\b(?:then|at\s+the\s+start\s+of|start\s+of|optimal|successful|failed|corrective)\b/.test(
|
|
1208
|
+
normalized
|
|
1209
|
+
)) {
|
|
1210
|
+
end = Math.max(end, ...references);
|
|
1211
|
+
}
|
|
1212
|
+
if (/\b(?:failed|successful|before\s+the\s+successful)\b/.test(normalized)) {
|
|
1213
|
+
for (const match of normalized.matchAll(/\bsteps?\s+(\d+)\s*(?:-|to|through|thru)\s*(\d+)\b/g)) {
|
|
1214
|
+
const rangeStart = parseNonNegativeIntegerToken(match[1] ?? "");
|
|
1215
|
+
const rangeEnd = parseNonNegativeIntegerToken(match[2] ?? "");
|
|
1216
|
+
if (rangeStart !== void 0 && rangeEnd !== void 0) {
|
|
1217
|
+
start = Math.min(start, rangeStart, rangeEnd);
|
|
1218
|
+
end = Math.max(end, rangeStart, rangeEnd);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
const successfulStep = firstMatchInteger(normalized, /\bsuccessful\s+move\s+at\s+step\s+(\d+)\b/);
|
|
1222
|
+
if (successfulStep !== void 0) {
|
|
1223
|
+
end = Math.max(end, successfulStep);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
if (references.length > 0 && /\b(?:appear|appears|appeared|disappear|disappears|disappeared|temporary|transformation|hidden\s+state|reversed)\b/.test(
|
|
1227
|
+
normalized
|
|
1228
|
+
)) {
|
|
1229
|
+
end = Math.max(end, ...references.map((reference) => reference + 1));
|
|
1230
|
+
}
|
|
1231
|
+
if (hasManeuverInterpretationCue(normalized) || /\brelative\s+position\b/.test(normalized)) {
|
|
1232
|
+
start -= 1;
|
|
1233
|
+
}
|
|
1234
|
+
if (/\b(?:breaks?|breaking|broke)\s+(?:this\s+|the\s+)?(?:loop|cycle|sequence)\b/.test(normalized)) {
|
|
1235
|
+
end += 1;
|
|
1236
|
+
}
|
|
1237
|
+
return boundedTrajectory(trajectory, {
|
|
1238
|
+
start: Math.max(minStep, start),
|
|
1239
|
+
end: Math.min(maxStep, end),
|
|
1240
|
+
reason: bounds.reason
|
|
1241
|
+
});
|
|
1242
|
+
}
|
|
1243
|
+
function collectActionMovementSummaryLines(query, window, bounds) {
|
|
1244
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
1245
|
+
if (!hasManeuverInterpretationCue(normalized) && !/\b(?:loop|cycle|sequence)\b/.test(normalized)) {
|
|
1246
|
+
return [];
|
|
1247
|
+
}
|
|
1248
|
+
const referencedEnd = Math.max(
|
|
1249
|
+
bounds.end,
|
|
1250
|
+
...collectExplicitTurnReferences(query).map((reference) => reference.number),
|
|
1251
|
+
hasLoopExitIntent(normalized) ? window[window.length - 1]?.step ?? bounds.end : bounds.end
|
|
1252
|
+
);
|
|
1253
|
+
const actions = window.filter((step) => step.step >= bounds.start && step.step <= referencedEnd && step.action).map((step) => ({
|
|
1254
|
+
step: step.step,
|
|
1255
|
+
action: step.action,
|
|
1256
|
+
move: agentMoveDeltaFromAction(step.action)
|
|
1257
|
+
})).filter((step) => step.move !== void 0);
|
|
1258
|
+
if (actions.length < 2) {
|
|
1259
|
+
return [];
|
|
1260
|
+
}
|
|
1261
|
+
const lines = [];
|
|
1262
|
+
const repeated = actions.every((step) => step.move.direction === actions[0].move.direction);
|
|
1263
|
+
const blockedPremise = /\b(?:blocked|fail|failed|failing|unchanged|stop|impassable|successfully\s+moved)\b/.test(
|
|
1264
|
+
normalized
|
|
1265
|
+
);
|
|
1266
|
+
const noChangePremise = /\b(?:game\s+state|state|position|observations?)\s+(?:did\s+not|does\s+not|do\s+not|didn't|doesn't|remain(?:ed)?|remains?)\s+(?:change|changed|unchanged)\b/.test(
|
|
1267
|
+
normalized
|
|
1268
|
+
) || /\b(?:no\s+change|unchanged|did\s+not\s+change\s+at\s+all|does\s+not\s+change\s+at\s+all)\b/.test(
|
|
1269
|
+
normalized
|
|
1270
|
+
);
|
|
1271
|
+
if (repeated && (blockedPremise || noChangePremise) && (observationsRemainStable(window, actions[0].step, actions[actions.length - 1].step) || findStopBlockerForRepeatedMove(window, actions[0].move) !== void 0)) {
|
|
1272
|
+
const blocker = findStopBlockerForRepeatedMove(window, actions[0].move);
|
|
1273
|
+
lines.push(
|
|
1274
|
+
blocker ? `Actions ${actions[0].step}-${actions[actions.length - 1].step} (${actions.map((step) => step.move.direction).join(", ")}) are repeated blocked/no-progress attempts: ${blocker.entity} is ${blocker.position.raw} at Observation ${blocker.step} and active rule "${blocker.entity} is stop" makes that target cell impassable. Do not describe these attempts as hidden-position displacement.` : `Actions ${actions[0].step}-${actions[actions.length - 1].step} (${actions.map((step) => step.move.direction).join(", ")}) are repeated attempts with stable relative observations; treat them as blocked/no-progress attempts, not as confirmed hidden-position displacement.`
|
|
1275
|
+
);
|
|
1276
|
+
return lines;
|
|
1277
|
+
}
|
|
1278
|
+
const noKeyWinCue = collectNoKeyWinRepositioningCue(query, window);
|
|
1279
|
+
if (noKeyWinCue) {
|
|
1280
|
+
lines.push(noKeyWinCue);
|
|
1281
|
+
}
|
|
1282
|
+
const net = summarizeMoveNet(actions.map((step) => step.move));
|
|
1283
|
+
if (net.dx === 0 && net.dy === 0) {
|
|
1284
|
+
lines.push(
|
|
1285
|
+
`Actions ${actions[0].step}-${actions[actions.length - 1].step} (${actions.map((step) => step.move.direction).join(", ")}) have net displacement 0; treat this as a self-canceling movement sequence unless another state change is shown.`
|
|
1286
|
+
);
|
|
1287
|
+
} else {
|
|
1288
|
+
lines.push(
|
|
1289
|
+
`Actions ${actions[0].step}-${actions[actions.length - 1].step} (${actions.map((step) => step.move.direction).join(", ")}) change the agent's hidden absolute position by ${formatMoveNet(net)}.`
|
|
1290
|
+
);
|
|
1291
|
+
}
|
|
1292
|
+
if (actions.length >= 3) {
|
|
1293
|
+
const prior = actions.slice(0, -1);
|
|
1294
|
+
const priorNet = summarizeMoveNet(prior.map((step) => step.move));
|
|
1295
|
+
const last = actions[actions.length - 1];
|
|
1296
|
+
if (priorNet.dx === 0 && priorNet.dy === 0 && (last.move.dx !== 0 || last.move.dy !== 0)) {
|
|
1297
|
+
lines.push(
|
|
1298
|
+
`The final ${last.move.direction} action at step ${last.step} is the first non-canceling movement after the prior loop; use it as vertical/horizontal progress toward nearby rule text rather than treating it as more oscillation.`
|
|
1299
|
+
);
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
if (repeated && actions.length >= 3) {
|
|
1303
|
+
lines.push(
|
|
1304
|
+
`The repeated ${actions[0].move.direction} actions are changing the agent's hidden absolute position over multiple rows/columns even if no immediate reward appears.`
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
return lines.slice(0, 4);
|
|
1308
|
+
}
|
|
1309
|
+
function summarizeMoveNet(moves) {
|
|
1310
|
+
return moves.reduce(
|
|
1311
|
+
(sum, move) => ({ dx: sum.dx + move.dx, dy: sum.dy + move.dy }),
|
|
1312
|
+
{ dx: 0, dy: 0 }
|
|
1313
|
+
);
|
|
1314
|
+
}
|
|
1315
|
+
function formatMoveNet(net) {
|
|
1316
|
+
const parts = [];
|
|
1317
|
+
if (net.dx !== 0) {
|
|
1318
|
+
parts.push(`${Math.abs(net.dx)} ${net.dx > 0 ? "right" : "left"}`);
|
|
1319
|
+
}
|
|
1320
|
+
if (net.dy !== 0) {
|
|
1321
|
+
parts.push(`${Math.abs(net.dy)} ${net.dy > 0 ? "down" : "up"}`);
|
|
1322
|
+
}
|
|
1323
|
+
return parts.length === 0 ? "0" : parts.join(" and ");
|
|
1324
|
+
}
|
|
1325
|
+
function collectMovementDeltaLines(query, window) {
|
|
1326
|
+
const entities = extractRelativePositionEntities(query);
|
|
1327
|
+
const lines = [];
|
|
1328
|
+
const focusSteps = new Set(collectExplicitTurnReferences(query).map((reference) => reference.number));
|
|
1329
|
+
for (let index = 1; index < window.length; index += 1) {
|
|
1330
|
+
const previous = window[index - 1];
|
|
1331
|
+
const current = window[index];
|
|
1332
|
+
if (!previous.observation || !current.observation) {
|
|
1333
|
+
continue;
|
|
1334
|
+
}
|
|
1335
|
+
if (focusSteps.size > 0 && !focusSteps.has(previous.step) && !focusSteps.has(current.step)) {
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
const previousPositions = parseRelativePositions(previous.observation);
|
|
1339
|
+
const currentPositions = parseRelativePositions(current.observation);
|
|
1340
|
+
for (const [entity, previousPosition] of previousPositions.entries()) {
|
|
1341
|
+
if (entities.size > 0 && ![...entities].some((candidate) => entity.includes(candidate))) {
|
|
1342
|
+
continue;
|
|
1343
|
+
}
|
|
1344
|
+
const currentPosition = currentPositions.get(entity);
|
|
1345
|
+
if (!currentPosition) {
|
|
1346
|
+
continue;
|
|
1347
|
+
}
|
|
1348
|
+
const dx = currentPosition.x - previousPosition.x;
|
|
1349
|
+
const dy = currentPosition.y - previousPosition.y;
|
|
1350
|
+
if (dx === 0 && dy === 0) {
|
|
1351
|
+
continue;
|
|
1352
|
+
}
|
|
1353
|
+
const inferredAgentMove = inferAgentMoveFromRelativeDelta(dx, dy);
|
|
1354
|
+
if (!inferredAgentMove) {
|
|
1355
|
+
continue;
|
|
1356
|
+
}
|
|
1357
|
+
lines.push(
|
|
1358
|
+
`Observation ${previous.step}->${current.step}: ${entity} changed from ${previousPosition.raw} to ${currentPosition.raw}; this implies the agent moved ${inferredAgentMove} relative to a static object.`
|
|
1359
|
+
);
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return lines.slice(0, 8);
|
|
1363
|
+
}
|
|
1364
|
+
function collectSelfReversingProgressLines(query, window, normalizedQuery) {
|
|
1365
|
+
if (!/\b(?:relevant|progress|goal|touching|win\s+object|zero\s+net|self[- ]?reversing|opposing)\b/.test(
|
|
1366
|
+
normalizedQuery
|
|
1367
|
+
)) {
|
|
1368
|
+
return [];
|
|
1369
|
+
}
|
|
1370
|
+
const mentionedMoves = extractMentionedMoveSequence(query);
|
|
1371
|
+
const moves = mentionedMoves.length >= 2 ? mentionedMoves : window.map((step) => step.action ? agentMoveDeltaFromAction(step.action)?.direction : void 0).filter((direction) => direction !== void 0);
|
|
1372
|
+
if (moves.length < 2) {
|
|
1373
|
+
return [];
|
|
1374
|
+
}
|
|
1375
|
+
const hasReversePair = containsAdjacentReversePair(moves, "right", "left") || containsAdjacentReversePair(moves, "left", "right") || containsAdjacentReversePair(moves, "up", "down") || containsAdjacentReversePair(moves, "down", "up");
|
|
1376
|
+
if (!hasReversePair) {
|
|
1377
|
+
return [];
|
|
1378
|
+
}
|
|
1379
|
+
const net = summarizeMoveNet(
|
|
1380
|
+
moves.map((direction) => agentMoveDeltaFromAction(direction))
|
|
1381
|
+
);
|
|
1382
|
+
const lines = [];
|
|
1383
|
+
if (net.dx === 0 && net.dy === 0) {
|
|
1384
|
+
lines.push(
|
|
1385
|
+
`The named movement sequence (${moves.join(", ")}) has net displacement 0; treat the actions as self-reversing exploratory noise unless a lasting rule, reward, inventory, or object-contact change is shown.`
|
|
1386
|
+
);
|
|
1387
|
+
if (/\bwhich\b/.test(normalizedQuery) && /\brelevant\b/.test(normalizedQuery)) {
|
|
1388
|
+
lines.push(
|
|
1389
|
+
`For a question asking which named actions were relevant, answer that none of the named actions made lasting progress when the sequence cancels out and no durable state change is shown.`
|
|
1390
|
+
);
|
|
1391
|
+
}
|
|
1392
|
+
} else if (mentionedMoves.length >= 4 && containsAdjacentReversePair(mentionedMoves, "right", "left") && containsAdjacentReversePair(mentionedMoves, "down", "up")) {
|
|
1393
|
+
lines.push(
|
|
1394
|
+
`The named right/left and down/up pairs are self-reversing; temporary closeness to a win object inside a reversed pair is not lasting progress toward touching that object.`
|
|
1395
|
+
);
|
|
1396
|
+
}
|
|
1397
|
+
return lines;
|
|
1398
|
+
}
|
|
1399
|
+
function extractMentionedMoveSequence(query) {
|
|
1400
|
+
const moves = [];
|
|
1401
|
+
for (const value of extractBacktickValues(query)) {
|
|
1402
|
+
const move = agentMoveDeltaFromAction(value);
|
|
1403
|
+
if (move) {
|
|
1404
|
+
moves.push(move.direction);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
if (moves.length > 0) {
|
|
1408
|
+
return moves;
|
|
1409
|
+
}
|
|
1410
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
1411
|
+
const tokens = normalized.split(" ").filter(Boolean);
|
|
1412
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
1413
|
+
const startIndex = moveSequenceStartIndex(tokens, index);
|
|
1414
|
+
if (startIndex === void 0) {
|
|
1415
|
+
continue;
|
|
1416
|
+
}
|
|
1417
|
+
const sequence = collectMoveSequenceTokens(tokens, startIndex);
|
|
1418
|
+
if (sequence.length > 0) {
|
|
1419
|
+
return sequence;
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
return [];
|
|
1423
|
+
}
|
|
1424
|
+
function moveSequenceStartIndex(tokens, index) {
|
|
1425
|
+
if (tokens[index] === "sequence" && tokens[index + 1] === "of") {
|
|
1426
|
+
let cursor2 = index + 2;
|
|
1427
|
+
if (tokens[cursor2] === "four" || parseNonNegativeIntegerToken(tokens[cursor2] ?? "") !== void 0) {
|
|
1428
|
+
cursor2 += 1;
|
|
1429
|
+
}
|
|
1430
|
+
const label = tokens[cursor2];
|
|
1431
|
+
if (label === "movement" || label === "movements") {
|
|
1432
|
+
return cursor2 + 1;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
const token = tokens[index];
|
|
1436
|
+
if (token !== "action" && token !== "actions") {
|
|
1437
|
+
return void 0;
|
|
1438
|
+
}
|
|
1439
|
+
if (tokens[index + 1] !== "from" || tokens[index + 2] !== "step") {
|
|
1440
|
+
return void 0;
|
|
1441
|
+
}
|
|
1442
|
+
let cursor = index + 3;
|
|
1443
|
+
if (parseNonNegativeIntegerToken(tokens[cursor] ?? "") !== void 0) {
|
|
1444
|
+
cursor += 1;
|
|
1445
|
+
}
|
|
1446
|
+
if (tokens[cursor] !== "to") {
|
|
1447
|
+
return void 0;
|
|
1448
|
+
}
|
|
1449
|
+
cursor += 1;
|
|
1450
|
+
if (tokens[cursor] === "step") {
|
|
1451
|
+
cursor += 1;
|
|
1452
|
+
}
|
|
1453
|
+
if (parseNonNegativeIntegerToken(tokens[cursor] ?? "") !== void 0) {
|
|
1454
|
+
cursor += 1;
|
|
1455
|
+
}
|
|
1456
|
+
return tokens[cursor] === "consist" && tokens[cursor + 1] === "of" ? cursor + 2 : void 0;
|
|
1457
|
+
}
|
|
1458
|
+
function collectMoveSequenceTokens(tokens, startIndex) {
|
|
1459
|
+
const moves = [];
|
|
1460
|
+
for (let index = startIndex; index < tokens.length && moves.length < 16; index += 1) {
|
|
1461
|
+
const token = tokens[index];
|
|
1462
|
+
const move = agentMoveDeltaFromAction(token);
|
|
1463
|
+
if (move) {
|
|
1464
|
+
moves.push(move.direction);
|
|
1465
|
+
continue;
|
|
1466
|
+
}
|
|
1467
|
+
if (moves.length > 0 && (token === "and" || token === "then")) {
|
|
1468
|
+
continue;
|
|
1469
|
+
}
|
|
1470
|
+
if (moves.length === 0 && MOVE_SEQUENCE_PREFIX_TOKENS.has(token)) {
|
|
1471
|
+
continue;
|
|
1472
|
+
}
|
|
1473
|
+
break;
|
|
1474
|
+
}
|
|
1475
|
+
return moves;
|
|
1476
|
+
}
|
|
1477
|
+
var MOVE_SEQUENCE_PREFIX_TOKENS = /* @__PURE__ */ new Set([
|
|
1478
|
+
"the",
|
|
1479
|
+
"following",
|
|
1480
|
+
"movement",
|
|
1481
|
+
"movements",
|
|
1482
|
+
"action",
|
|
1483
|
+
"actions",
|
|
1484
|
+
"are",
|
|
1485
|
+
"is",
|
|
1486
|
+
"were",
|
|
1487
|
+
"namely",
|
|
1488
|
+
"as",
|
|
1489
|
+
"listed"
|
|
1490
|
+
]);
|
|
1491
|
+
function containsAdjacentReversePair(moves, first, second) {
|
|
1492
|
+
for (let index = 1; index < moves.length; index += 1) {
|
|
1493
|
+
if (moves[index - 1] === first && moves[index] === second) {
|
|
1494
|
+
return true;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return false;
|
|
1498
|
+
}
|
|
1499
|
+
function collectOnlyEffectiveActionLines(window, normalizedQuery) {
|
|
1500
|
+
if (!(/\bonly\s+one\b/.test(normalizedQuery) && /\b(?:caused?\s+a\s+change|made\s+progress|relevant|ineffective)\b/.test(normalizedQuery)) && !/\bother\s+(?:\d+|one|two|three|four|five|six|seven|eight|nine)\b.*\bineffective\b/.test(normalizedQuery)) {
|
|
1501
|
+
return [];
|
|
1502
|
+
}
|
|
1503
|
+
const steps = window.filter((step) => step.action && step.observation);
|
|
1504
|
+
if (steps.length < 3) {
|
|
1505
|
+
return [];
|
|
1506
|
+
}
|
|
1507
|
+
const baseline = observationStateSignature(steps[0].observation);
|
|
1508
|
+
if (!baseline) {
|
|
1509
|
+
return [];
|
|
1510
|
+
}
|
|
1511
|
+
const unchanged = [];
|
|
1512
|
+
let changed;
|
|
1513
|
+
for (const step of steps) {
|
|
1514
|
+
const signature = observationStateSignature(step.observation);
|
|
1515
|
+
if (signature === baseline && changed === void 0) {
|
|
1516
|
+
unchanged.push(step);
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1519
|
+
changed = step;
|
|
1520
|
+
break;
|
|
1521
|
+
}
|
|
1522
|
+
if (!changed || unchanged.length < 2) {
|
|
1523
|
+
return [];
|
|
1524
|
+
}
|
|
1525
|
+
const previous = unchanged[unchanged.length - 1];
|
|
1526
|
+
const changedMove = agentMoveDeltaFromAction(changed.action);
|
|
1527
|
+
const unchangedActions = unchanged.map((step) => `Action ${step.step} ${normalizeActionVerb(step.action) || step.action}`).join(", ");
|
|
1528
|
+
const example = positionShiftExample(previous.observation, changed.observation);
|
|
1529
|
+
return [
|
|
1530
|
+
`Within the named span, Observations ${unchanged[0].step}-${previous.step} have the same object-relative signature after ${unchangedActions}; treat those actions as ineffective/no-progress attempts even if an earlier pre-span observation differs.`,
|
|
1531
|
+
`Observation ${previous.step}->${changed.step} is the first state change inside the span after Action ${changed.step} ${changedMove?.direction ?? changed.action}${example ? ` (${example})` : ""}; answer that Action ${changed.step} ${changedMove?.direction ?? changed.action} is the only progress-making action.`
|
|
1532
|
+
];
|
|
1533
|
+
}
|
|
1534
|
+
function collectObjectAlignmentLines(query, window, normalizedQuery) {
|
|
1535
|
+
const entities = extractRelativePositionEntities(query);
|
|
1536
|
+
const focusSteps = new Set(collectExplicitTurnReferences(query).map((reference) => reference.number));
|
|
1537
|
+
const lines = [];
|
|
1538
|
+
const strategicCueEntities = /* @__PURE__ */ new Set();
|
|
1539
|
+
for (let index = 1; index < window.length; index += 1) {
|
|
1540
|
+
const previous = window[index - 1];
|
|
1541
|
+
const current = window[index];
|
|
1542
|
+
const next = window[index + 1];
|
|
1543
|
+
if (!previous.observation || !current.observation || !current.action) {
|
|
1544
|
+
continue;
|
|
1545
|
+
}
|
|
1546
|
+
if (focusSteps.size > 0 && !focusSteps.has(previous.step) && !focusSteps.has(current.step) && (!next || !focusSteps.has(next.step))) {
|
|
1547
|
+
continue;
|
|
1548
|
+
}
|
|
1549
|
+
const move = agentMoveDeltaFromAction(current.action);
|
|
1550
|
+
if (!move) {
|
|
1551
|
+
continue;
|
|
1552
|
+
}
|
|
1553
|
+
const previousPositions = parseRelativePositions(previous.observation);
|
|
1554
|
+
const currentPositions = parseRelativePositions(current.observation);
|
|
1555
|
+
for (const [entity, previousPosition] of previousPositions.entries()) {
|
|
1556
|
+
if (entity.startsWith("rule ")) {
|
|
1557
|
+
continue;
|
|
1558
|
+
}
|
|
1559
|
+
if (entities.size > 0 && ![...entities].some((candidate) => entity.includes(candidate))) {
|
|
1560
|
+
continue;
|
|
1561
|
+
}
|
|
1562
|
+
if (currentPositions.has(entity)) {
|
|
1563
|
+
continue;
|
|
1564
|
+
}
|
|
1565
|
+
if (!relativePositionMatchesMove(previousPosition, move)) {
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
lines.push(
|
|
1569
|
+
`Observation ${previous.step}->${current.step}: ${entity} was ${previousPosition.raw}, Action ${current.step} was ${move.direction}, and ${entity} is absent from Observation ${current.step}; infer a zero-offset same-tile alignment at the end of step ${current.step}, not that ${entity} was removed.`
|
|
1570
|
+
);
|
|
1571
|
+
if (next?.observation && next.action) {
|
|
1572
|
+
const nextPositions = parseRelativePositions(next.observation);
|
|
1573
|
+
const nextPosition = nextPositions.get(entity);
|
|
1574
|
+
const nextMove = agentMoveDeltaFromAction(next.action);
|
|
1575
|
+
if (nextPosition && nextMove) {
|
|
1576
|
+
lines.push(
|
|
1577
|
+
`Observation ${current.step}->${next.step}: after Action ${next.step} ${nextMove.direction}, ${entity} reappears ${nextPosition.raw}; this confirms the step-${current.step} same-tile alignment and leaves the agent on the opposite side of ${entity} for a future ${oppositeDirection(nextMove.direction)} interaction or alignment.`
|
|
1578
|
+
);
|
|
1579
|
+
if (normalizedQuery.includes("win") || hasManeuverInterpretationCue(normalizedQuery) && hasRelativeEntityInObservations("rule win", [
|
|
1580
|
+
previous.observation,
|
|
1581
|
+
current.observation,
|
|
1582
|
+
next.observation
|
|
1583
|
+
])) {
|
|
1584
|
+
lines.push(
|
|
1585
|
+
`Because the question asks about a win-condition objective, treat that future ${oppositeDirection(nextMove.direction)} setup as supporting possible ${entity} manipulation or rule alignment toward a new win condition.`
|
|
1586
|
+
);
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
if (hasManeuverInterpretationCue(normalizedQuery) && !hasExactRelativeStateIntent(normalizedQuery) && !strategicCueEntities.has(entity)) {
|
|
1591
|
+
lines.push(
|
|
1592
|
+
`For ${entity} strategic maneuver questions, treat same-tile or vanishing-object cues as path-positioning evidence; the strategic goal should be framed as repositioning around a blocking object for later rule/object manipulation, not as pushing, collecting, or removing ${entity}.`
|
|
1593
|
+
);
|
|
1594
|
+
strategicCueEntities.add(entity);
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
return lines.slice(0, 8);
|
|
1599
|
+
}
|
|
1600
|
+
function objectAlignmentWindowForQuery(query, trajectory, bounds) {
|
|
1601
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
1602
|
+
let end = bounds.end;
|
|
1603
|
+
if (/(?:after|vanish|vanished|disappear|disappeared|reappear|reappeared)/.test(normalized)) {
|
|
1604
|
+
for (const reference of collectExplicitTurnReferences(query)) {
|
|
1605
|
+
if (reference.number > end) {
|
|
1606
|
+
end = reference.number;
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
const maxStep = trajectory[trajectory.length - 1]?.step ?? bounds.end;
|
|
1611
|
+
return boundedTrajectory(trajectory, {
|
|
1612
|
+
start: bounds.start,
|
|
1613
|
+
end: Math.min(end, maxStep),
|
|
1614
|
+
reason: bounds.reason
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
function collectAlternativeActionLines(query, trajectory) {
|
|
1618
|
+
const normalized = normalizeTrajectoryQuery(query);
|
|
1619
|
+
if (!/\b(?:alternative|instead|would\s+have|should\s+have)\b/.test(normalized) || !/\b(?:loop|zero\s+progress|reversing|reverse|win\s+condition|objective|goal|rule|strategic|counterproductive)\b/.test(normalized)) {
|
|
1620
|
+
return [];
|
|
1621
|
+
}
|
|
1622
|
+
const targetStep = firstMatchInteger(normalized, /\bstart\s+of\s+step\s+(\d+)\b/) ?? firstMatchInteger(normalized, /\bfrom\s+step\s+(\d+)\s+to\s+\d+\b/) ?? firstMatchInteger(normalized, /\binstead\s+of\s+(?:moving|going)\s+[a-z]+\s+(?:in|at|on)\s+step\s+(\d+)\b/) ?? firstMatchInteger(normalized, /\b(?:if\s+)?(?:at|in|on)\s+step\s+(\d+)\b/);
|
|
1623
|
+
if (targetStep === void 0) {
|
|
1624
|
+
return [];
|
|
1625
|
+
}
|
|
1626
|
+
const byStep = new Map(trajectory.map((step) => [step.step, step]));
|
|
1627
|
+
const priorStep = byStep.get(targetStep - 1);
|
|
1628
|
+
const target = byStep.get(targetStep);
|
|
1629
|
+
if (!priorStep?.observation) {
|
|
1630
|
+
return [];
|
|
1631
|
+
}
|
|
1632
|
+
const unavailableMove = extractDisallowedMove(normalized) ?? (target?.action ? agentMoveDeltaFromAction(target.action)?.direction : void 0);
|
|
1633
|
+
const alternativeMove = extractAlternativeMove(normalized);
|
|
1634
|
+
const positions = parseRelativePositions(priorStep.observation);
|
|
1635
|
+
const lines = [];
|
|
1636
|
+
if (alternativeMove) {
|
|
1637
|
+
const entities = extractRelativePositionEntities(query);
|
|
1638
|
+
const queryAsksForTextBlock = /\b(?:text|word)\s+blocks?\b/.test(normalized) || /\bblocks?\s+(?:of|for)\s+(?:text|word)\b/.test(normalized);
|
|
1639
|
+
let focusEntities = entities.size > 0 ? [...entities].filter((entity) => positions.has(entity)) : [];
|
|
1640
|
+
if (queryAsksForTextBlock && focusEntities.some((entity) => entity.startsWith("rule "))) {
|
|
1641
|
+
focusEntities = focusEntities.filter((entity) => entity.startsWith("rule "));
|
|
1642
|
+
}
|
|
1643
|
+
if (queryAsksForTextBlock && focusEntities.length > 0) {
|
|
1644
|
+
const first = focusEntities[0];
|
|
1645
|
+
lines.push(
|
|
1646
|
+
`Question target cue: because the query asks for a text/word block, use ${first} rather than the ordinary object with the same name when both appear in the observation.`
|
|
1647
|
+
);
|
|
1648
|
+
}
|
|
1649
|
+
for (const entity of focusEntities.slice(0, 3)) {
|
|
1650
|
+
const position = positions.get(entity);
|
|
1651
|
+
const nextPosition = {
|
|
1652
|
+
x: position.x - alternativeMove.dx,
|
|
1653
|
+
y: position.y - alternativeMove.dy
|
|
1654
|
+
};
|
|
1655
|
+
lines.push(
|
|
1656
|
+
`At the start of step ${targetStep}, use Observation ${priorStep.step}: if Action ${targetStep} were ${alternativeMove.direction}, static ${entity} would shift from ${position.raw} to ${formatRelativePosition(nextPosition)} relative to the agent.`
|
|
1657
|
+
);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
const focus = selectAlternativeActionTargets(positions, normalized);
|
|
1661
|
+
if (focus.length === 0) {
|
|
1662
|
+
return lines;
|
|
1663
|
+
}
|
|
1664
|
+
const scored = ["left", "right", "up", "down"].map((direction) => agentMoveDeltaFromAction(direction)).filter((move) => move.direction !== unavailableMove).map((move) => ({
|
|
1665
|
+
move,
|
|
1666
|
+
score: scoreMoveTowardTargets(move, focus)
|
|
1667
|
+
})).sort((left, right) => {
|
|
1668
|
+
if (right.score !== left.score) {
|
|
1669
|
+
return right.score - left.score;
|
|
1670
|
+
}
|
|
1671
|
+
return left.move.direction.localeCompare(right.move.direction);
|
|
1672
|
+
});
|
|
1673
|
+
const best = scored[0];
|
|
1674
|
+
if (!best || best.score <= 0) {
|
|
1675
|
+
return lines;
|
|
1676
|
+
}
|
|
1677
|
+
const targetDescriptions = focus.map(
|
|
1678
|
+
({ entity, position }) => `${entity} at ${position.raw}`
|
|
1679
|
+
);
|
|
1680
|
+
lines.push(
|
|
1681
|
+
`At the start of step ${targetStep}, use Observation ${priorStep.step} as the decision state: ${targetDescriptions.join("; ")}.`,
|
|
1682
|
+
`Excluding the actual/reversing move${unavailableMove ? ` ${unavailableMove}` : ""}, ${best.move.direction} is the alternative that most directly reduces distance to the win-condition rule text target(s).`
|
|
1683
|
+
);
|
|
1684
|
+
if (normalized.includes("win condition")) {
|
|
1685
|
+
lines.push(
|
|
1686
|
+
`${best.move.direction} advances the objective of getting beside or behind IS/WIN rule text for later rule construction; moves toward unrelated object words should not be preferred when the question asks about creating a win condition.`
|
|
1687
|
+
);
|
|
1688
|
+
}
|
|
1689
|
+
return lines;
|
|
1690
|
+
}
|
|
1691
|
+
function collectCounterfactualContactLines(query, trajectory, normalizedQuery) {
|
|
1692
|
+
if (!/\b(?:instead|would\s+have|if\s+the\s+agent|if\s+at\s+step|had\s+instead)\b/.test(normalizedQuery)) {
|
|
1693
|
+
return [];
|
|
1694
|
+
}
|
|
1695
|
+
const targetStep = firstMatchInteger(normalizedQuery, /\bstart\s+of\s+step\s+(\d+)\b/) ?? firstMatchInteger(normalizedQuery, /\bfrom\s+step\s+(\d+)\s+to\s+\d+\b/) ?? firstMatchInteger(normalizedQuery, /\binstead\s+of\s+(?:moving|going)\s+[a-z]+\s+(?:in|at|on)\s+step\s+(\d+)\b/) ?? firstMatchInteger(normalizedQuery, /\b(?:if\s+)?(?:at|in|on)\s+step\s+(\d+)\b/) ?? firstMatchInteger(normalizedQuery, /\baction\s+at\s+step\s+(\d+)\b/);
|
|
1696
|
+
const move = extractAlternativeMove(normalizedQuery);
|
|
1697
|
+
if (targetStep === void 0 || !move) {
|
|
1698
|
+
return [];
|
|
1699
|
+
}
|
|
1700
|
+
const byStep = new Map(trajectory.map((step) => [step.step, step]));
|
|
1701
|
+
const useSameStepLayout = normalizedQuery.includes(`layout in step ${targetStep}`) || normalizedQuery.includes(`shown in step ${targetStep}`) || normalizedQuery.includes(`configuration shown in step ${targetStep}`) || normalizedQuery.includes(`based on the object layout in step ${targetStep}`);
|
|
1702
|
+
const observationStep = useSameStepLayout ? byStep.get(targetStep) : byStep.get(targetStep - 1) ?? byStep.get(targetStep);
|
|
1703
|
+
if (!observationStep?.observation) {
|
|
1704
|
+
return [];
|
|
1705
|
+
}
|
|
1706
|
+
const contacts = parseRelativePositionEntries(observationStep.observation).filter((position) => position.x === move.dx && position.y === move.dy).sort((left, right) => {
|
|
1707
|
+
const leftRule = normalizeEntity(left.entity).startsWith("rule ") ? 0 : 1;
|
|
1708
|
+
const rightRule = normalizeEntity(right.entity).startsWith("rule ") ? 0 : 1;
|
|
1709
|
+
return leftRule - rightRule || normalizeEntity(left.entity).localeCompare(normalizeEntity(right.entity));
|
|
1710
|
+
});
|
|
1711
|
+
if (contacts.length === 0) {
|
|
1712
|
+
return [];
|
|
1713
|
+
}
|
|
1714
|
+
const contact = contacts[0];
|
|
1715
|
+
const lines = [
|
|
1716
|
+
`At Observation ${observationStep.step}, ${contact.entity} is ${contact.raw}; a counterfactual Action ${targetStep} ${move.direction} would contact that block. In the benchmark's push mechanics, this is the expected push interaction: push it one cell ${move.direction} and move the agent into the block's original cell unless an explicit STOP/boundary cue says otherwise. Do not describe this as merely stepping onto or overlapping the block.`
|
|
1717
|
+
];
|
|
1718
|
+
if (/\bwall\s+is\s+stop\b/.test(normalizedQuery)) {
|
|
1719
|
+
const sameRowIs = findRuleIsOnSameRowAfterMove(observationStep.observation, move);
|
|
1720
|
+
if (sameRowIs) {
|
|
1721
|
+
lines.push(
|
|
1722
|
+
`After that ${move.direction} move, ${sameRowIs.entity} from ${sameRowIs.raw} would be on the agent's same horizontal row at ${formatRelativePosition({ x: sameRowIs.x - move.dx, y: sameRowIs.y - move.dy })}, setting up a later lateral push of IS toward WALL and STOP.`
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
lines.push(
|
|
1726
|
+
`For a WALL IS STOP formation question, this contact is a positioning setup for getting onto the same row/line as rule IS so a later move can push IS into alignment with WALL and STOP.`
|
|
1727
|
+
);
|
|
1728
|
+
}
|
|
1729
|
+
if (/\brule\s+words?\b|\brule\s+blocks?\b|\bis\s+block\b|\btext\s+block\b/.test(normalizedQuery)) {
|
|
1730
|
+
lines.push(
|
|
1731
|
+
`Prefer this concrete contact/push interpretation over a vague movement-only explanation when the question asks about manipulating rule words.`
|
|
1732
|
+
);
|
|
1733
|
+
}
|
|
1734
|
+
return lines.slice(0, 3);
|
|
1735
|
+
}
|
|
1736
|
+
function findRuleIsOnSameRowAfterMove(observation, move) {
|
|
1737
|
+
return parseRelativePositionEntries(observation).filter((position) => normalizeEntity(position.entity) === "rule is").map((position) => ({
|
|
1738
|
+
position,
|
|
1739
|
+
afterX: position.x - move.dx,
|
|
1740
|
+
afterY: position.y - move.dy
|
|
1741
|
+
})).filter((entry) => entry.afterY === 0).sort((left, right) => Math.abs(left.afterX) - Math.abs(right.afterX))[0]?.position;
|
|
1742
|
+
}
|
|
1743
|
+
function collectAdjacentRuleSetupLines(window, normalizedQuery) {
|
|
1744
|
+
if (!hasAdjacentRuleSetupIntent(normalizedQuery)) {
|
|
1745
|
+
return [];
|
|
1746
|
+
}
|
|
1747
|
+
const latest = [...window].reverse().find((step) => step.observation);
|
|
1748
|
+
if (!latest?.observation) {
|
|
1749
|
+
return [];
|
|
1750
|
+
}
|
|
1751
|
+
const adjacent = parseRelativePositionEntries(latest.observation).filter((position) => normalizeEntity(position.entity).startsWith("rule ")).filter((position) => manhattanDistance(position) === 1).sort((left, right) => {
|
|
1752
|
+
const leftMentioned = normalizedQuery.includes(normalizeEntity(left.entity).replace(/^rule\s+/, "")) ? 0 : 1;
|
|
1753
|
+
const rightMentioned = normalizedQuery.includes(normalizeEntity(right.entity).replace(/^rule\s+/, "")) ? 0 : 1;
|
|
1754
|
+
return leftMentioned - rightMentioned || normalizeEntity(left.entity).localeCompare(normalizeEntity(right.entity));
|
|
1755
|
+
});
|
|
1756
|
+
const lines = [];
|
|
1757
|
+
for (const position of adjacent.slice(0, 3)) {
|
|
1758
|
+
const direction = pushDirectionForAdjacentPosition(position);
|
|
1759
|
+
if (!direction) {
|
|
1760
|
+
continue;
|
|
1761
|
+
}
|
|
1762
|
+
const base = normalizeEntity(position.entity).replace(/^rule\s+/, "");
|
|
1763
|
+
lines.push(
|
|
1764
|
+
`At Observation ${latest.step}, ${position.entity} is ${position.raw}; the agent is directly ${agentRelationToAdjacentBlock(position)} the ${base.toUpperCase()} text block, so a future ${direction} action can push ${base.toUpperCase()} ${direction} to manipulate rule syntax.`
|
|
1765
|
+
);
|
|
1766
|
+
}
|
|
1767
|
+
return lines;
|
|
1768
|
+
}
|
|
1769
|
+
function hasAdjacentRuleSetupIntent(normalizedQuery) {
|
|
1770
|
+
for (const phrase of [
|
|
1771
|
+
"final position",
|
|
1772
|
+
"relative to rule",
|
|
1773
|
+
"relative to the rule",
|
|
1774
|
+
"rule block",
|
|
1775
|
+
"rule blocks",
|
|
1776
|
+
"rule word",
|
|
1777
|
+
"rule words",
|
|
1778
|
+
"strategic advantage",
|
|
1779
|
+
"manipulate rule",
|
|
1780
|
+
"manipulate the rule",
|
|
1781
|
+
"manipulating rule",
|
|
1782
|
+
"manipulating the rule"
|
|
1783
|
+
]) {
|
|
1784
|
+
if (containsBoundedPhrase(normalizedQuery, phrase)) {
|
|
1785
|
+
return true;
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
return hasPushBlockIntent(normalizedQuery);
|
|
1789
|
+
}
|
|
1790
|
+
function hasPushBlockIntent(normalizedQuery) {
|
|
1791
|
+
return /\b(?:push|pushed|pushing)\s+(?:the\s+|a\s+|an\s+)?(?:(?:and|baba|ball|door|flag|grass|is|key|keke|lava|me|melt|move|not|object|open|pull|push|rock|rule|shut|sink|skull|stop|target|text|wall|water|win|word|you)\s+)?(?:text\s+)?blocks?\b/.test(
|
|
1792
|
+
normalizedQuery
|
|
1793
|
+
);
|
|
1794
|
+
}
|
|
1795
|
+
function pushDirectionForAdjacentPosition(position) {
|
|
1796
|
+
if (position.x === 0 && position.y === -1) return "up";
|
|
1797
|
+
if (position.x === 0 && position.y === 1) return "down";
|
|
1798
|
+
if (position.x === -1 && position.y === 0) return "left";
|
|
1799
|
+
if (position.x === 1 && position.y === 0) return "right";
|
|
1800
|
+
return void 0;
|
|
1801
|
+
}
|
|
1802
|
+
function agentRelationToAdjacentBlock(position) {
|
|
1803
|
+
if (position.x === 0 && position.y === -1) return "underneath";
|
|
1804
|
+
if (position.x === 0 && position.y === 1) return "above";
|
|
1805
|
+
if (position.x === -1 && position.y === 0) return "to the right of";
|
|
1806
|
+
if (position.x === 1 && position.y === 0) return "to the left of";
|
|
1807
|
+
return "next to";
|
|
1808
|
+
}
|
|
1809
|
+
function collectBlockedMoveLines(query, window, normalizedQuery) {
|
|
1810
|
+
if (!/\b(?:blocked|fail|failed|unchanged|stop|impassable|successfully\s+moved|instead)\b/.test(
|
|
1811
|
+
normalizedQuery
|
|
1812
|
+
)) {
|
|
1813
|
+
return [];
|
|
1814
|
+
}
|
|
1815
|
+
const lines = [];
|
|
1816
|
+
const alternativeMove = extractAlternativeMove(normalizedQuery);
|
|
1817
|
+
const focusStep = firstMatchInteger(normalizedQuery, /\b(?:at|in|on)\s+step\s+(\d+)\b/);
|
|
1818
|
+
const asksForOtherActions = /\b(?:what\s+)?other\s+actions?\b/.test(normalizedQuery) || /\bmore\s+strategic\s+actions?\b/.test(normalizedQuery);
|
|
1819
|
+
for (const step of window) {
|
|
1820
|
+
if (!step.observation) {
|
|
1821
|
+
continue;
|
|
1822
|
+
}
|
|
1823
|
+
const stopObjects = parseStopRuleObjects(step.observation);
|
|
1824
|
+
if (stopObjects.length === 0) {
|
|
1825
|
+
continue;
|
|
1826
|
+
}
|
|
1827
|
+
const actionMove = step.action ? agentMoveDeltaFromAction(step.action) : void 0;
|
|
1828
|
+
if (actionMove) {
|
|
1829
|
+
const blocker = findBlockingStopObject(step.observation, stopObjects, actionMove);
|
|
1830
|
+
if (blocker) {
|
|
1831
|
+
lines.push(
|
|
1832
|
+
`Action ${step.step} ${actionMove.direction} is blocked: ${blocker.entity} is ${blocker.position.raw} and active rule "${blocker.entity} is stop" makes that object impassable.`
|
|
1833
|
+
);
|
|
1834
|
+
if (asksForOtherActions) {
|
|
1835
|
+
const openMoves = openNonStopMoves(step.observation, stopObjects, actionMove.direction);
|
|
1836
|
+
if (openMoves.length > 0) {
|
|
1837
|
+
lines.push(
|
|
1838
|
+
`Immediate strategic alternatives at Observation ${step.step}: ${formatMoveList(openMoves)} have no adjacent STOP object in the target cell, so those moves can change position while ${actionMove.direction} cannot.`
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
if (alternativeMove && (focusStep === void 0 || step.step === focusStep - 1 || step.step === focusStep)) {
|
|
1845
|
+
const alternativeBlocker = findBlockingStopObject(
|
|
1846
|
+
step.observation,
|
|
1847
|
+
stopObjects,
|
|
1848
|
+
alternativeMove
|
|
1849
|
+
);
|
|
1850
|
+
if (!alternativeBlocker) {
|
|
1851
|
+
lines.push(
|
|
1852
|
+
`At Observation ${step.step}, ${alternativeMove.direction} has no adjacent STOP object in the target cell, so that alternative is not blocked by the active stop rule(s).`
|
|
1853
|
+
);
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
return uniqueLines(lines).slice(0, 6);
|
|
1858
|
+
}
|
|
1859
|
+
function collectFailedContactBoundaryLines(window, normalizedQuery) {
|
|
1860
|
+
if (!/\b(?:failed|fail|blocked|no\s+change|unchanged|inability)\b/.test(normalizedQuery)) {
|
|
1861
|
+
return [];
|
|
1862
|
+
}
|
|
1863
|
+
if (/\bfailed\s+moves?\b/.test(normalizedQuery) && /\bsuccessful\s+move\s+at\s+step\s+\d+\b/.test(normalizedQuery)) {
|
|
1864
|
+
return [];
|
|
1865
|
+
}
|
|
1866
|
+
const directions = /* @__PURE__ */ new Set();
|
|
1867
|
+
let hasExplicitDirection = false;
|
|
1868
|
+
for (const direction of ["up", "down", "left", "right"]) {
|
|
1869
|
+
if (normalizedQuery.includes(`failed ${direction}`) || normalizedQuery.includes(`${direction} action repeatedly fails`) || normalizedQuery.includes(`${direction} attempt`) || normalizedQuery.includes(`moves ${direction}`)) {
|
|
1870
|
+
directions.add(direction);
|
|
1871
|
+
hasExplicitDirection = true;
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
if (!hasExplicitDirection) {
|
|
1875
|
+
for (const step of window) {
|
|
1876
|
+
const move = step.action ? agentMoveDeltaFromAction(step.action) : void 0;
|
|
1877
|
+
if (move) {
|
|
1878
|
+
directions.add(move.direction);
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
if (directions.size === 0) {
|
|
1883
|
+
return [];
|
|
1884
|
+
}
|
|
1885
|
+
const lines = [];
|
|
1886
|
+
for (const step of window) {
|
|
1887
|
+
if (!step.observation) {
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
const positions = parseRelativePositionEntries(step.observation);
|
|
1891
|
+
for (const direction of directions) {
|
|
1892
|
+
const move = agentMoveDeltaFromAction(direction);
|
|
1893
|
+
const contact = positions.find(
|
|
1894
|
+
(position) => position.x === move.dx && position.y === move.dy
|
|
1895
|
+
);
|
|
1896
|
+
if (!contact) {
|
|
1897
|
+
continue;
|
|
1898
|
+
}
|
|
1899
|
+
const normalizedEntity = normalizeEntity(contact.entity);
|
|
1900
|
+
if (normalizedEntity.startsWith("rule ")) {
|
|
1901
|
+
const stopRules = parseActiveRules(step.observation).filter((rule) => rule.endsWith(" is stop"));
|
|
1902
|
+
const stopContext = stopRules.length === 0 ? "No active STOP rule is involved; " : "";
|
|
1903
|
+
lines.push(
|
|
1904
|
+
`At Observation ${step.step}, ${contact.entity} is ${contact.raw}; failed ${direction} moves identify this text block as the blocker. ${stopContext}since rule/text blocks normally move when pushed, the failure implies the block cannot move further ${direction} because it is pressed against the ${boundaryNameForDirection(direction)}.`
|
|
1905
|
+
);
|
|
1906
|
+
} else {
|
|
1907
|
+
lines.push(
|
|
1908
|
+
`At Observation ${step.step}, ${contact.entity} is ${contact.raw}; failed ${direction} moves identify that adjacent object as the immediate blocker.`
|
|
1909
|
+
);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
return uniqueLines(lines).slice(0, 5);
|
|
1914
|
+
}
|
|
1915
|
+
function collectSameRelativeTextPushLines(window, normalizedQuery) {
|
|
1916
|
+
if (!/\bhidden\b/.test(normalizedQuery) && !(/\brelative\b/.test(normalizedQuery) && /\b(?:same|unchanged|static)\b/.test(normalizedQuery)) && !/\bobservation\s+(?:fail|fails|failed)\s+to\s+show\b/.test(normalizedQuery)) {
|
|
1917
|
+
return [];
|
|
1918
|
+
}
|
|
1919
|
+
const lines = [];
|
|
1920
|
+
for (let index = 1; index < window.length; index += 1) {
|
|
1921
|
+
const previous = window[index - 1];
|
|
1922
|
+
const current = window[index];
|
|
1923
|
+
const previousMove = previous.action ? agentMoveDeltaFromAction(previous.action) : void 0;
|
|
1924
|
+
const currentMove = current.action ? agentMoveDeltaFromAction(current.action) : void 0;
|
|
1925
|
+
if (!previous.observation || !current.observation || !previousMove || !currentMove || previousMove.direction !== currentMove.direction) {
|
|
1926
|
+
continue;
|
|
1927
|
+
}
|
|
1928
|
+
const previousPositions = parseRelativePositionEntries(previous.observation);
|
|
1929
|
+
const currentPositions = parseRelativePositions(current.observation);
|
|
1930
|
+
for (const previousPosition of previousPositions) {
|
|
1931
|
+
const normalizedEntity = normalizeEntity(previousPosition.entity);
|
|
1932
|
+
if (!normalizedEntity.startsWith("rule ") || previousPosition.x !== currentMove.dx || previousPosition.y !== currentMove.dy) {
|
|
1933
|
+
continue;
|
|
1934
|
+
}
|
|
1935
|
+
const currentPosition = currentPositions.get(normalizedEntity);
|
|
1936
|
+
if (!currentPosition || currentPosition.x !== previousPosition.x || currentPosition.y !== previousPosition.y) {
|
|
1937
|
+
continue;
|
|
1938
|
+
}
|
|
1939
|
+
lines.push(
|
|
1940
|
+
`Actions ${previous.step}-${current.step} are repeated ${currentMove.direction} moves with ${previousPosition.entity} remaining adjacent at ${previousPosition.raw} in both resulting observations; for a hidden-state question, answer the cumulative displacement across the named repeated actions: ${previousPosition.entity} moved one cell ${currentMove.direction} per ${currentMove.direction} action while the agent moved with it, so the relative offset stayed constant.`
|
|
1941
|
+
);
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
for (let index = 2; index < window.length; index += 1) {
|
|
1945
|
+
const beforeApproach = window[index - 2];
|
|
1946
|
+
const contactStep = window[index - 1];
|
|
1947
|
+
const pushStep = window[index];
|
|
1948
|
+
const contactMove = contactStep.action ? agentMoveDeltaFromAction(contactStep.action) : void 0;
|
|
1949
|
+
const pushMove = pushStep.action ? agentMoveDeltaFromAction(pushStep.action) : void 0;
|
|
1950
|
+
if (!beforeApproach.observation || !contactStep.observation || !pushStep.observation || !contactMove || !pushMove || contactMove.direction !== pushMove.direction) {
|
|
1951
|
+
continue;
|
|
1952
|
+
}
|
|
1953
|
+
const beforePositions = parseRelativePositions(beforeApproach.observation);
|
|
1954
|
+
const contactPositions = parseRelativePositionEntries(contactStep.observation);
|
|
1955
|
+
const afterPositions = parseRelativePositions(pushStep.observation);
|
|
1956
|
+
for (const contactPosition of contactPositions) {
|
|
1957
|
+
const normalizedEntity = normalizeEntity(contactPosition.entity);
|
|
1958
|
+
if (!normalizedEntity.startsWith("rule ")) {
|
|
1959
|
+
continue;
|
|
1960
|
+
}
|
|
1961
|
+
if (contactPosition.x !== pushMove.dx || contactPosition.y !== pushMove.dy) {
|
|
1962
|
+
continue;
|
|
1963
|
+
}
|
|
1964
|
+
const afterPosition = afterPositions.get(normalizedEntity);
|
|
1965
|
+
if (!afterPosition || afterPosition.x !== contactPosition.x || afterPosition.y !== contactPosition.y) {
|
|
1966
|
+
continue;
|
|
1967
|
+
}
|
|
1968
|
+
const beforePosition = beforePositions.get(normalizedEntity);
|
|
1969
|
+
if (beforePosition && beforePosition.x === contactPosition.x && beforePosition.y === contactPosition.y) {
|
|
1970
|
+
continue;
|
|
1971
|
+
}
|
|
1972
|
+
lines.push(
|
|
1973
|
+
`Observation ${contactStep.step}->${pushStep.step}: after Action ${contactStep.step} ${contactMove.direction} made ${contactPosition.entity} adjacent at ${contactPosition.raw}, Action ${pushStep.step} ${pushMove.direction} kept it at the same relative offset; infer the agent pushed ${contactPosition.entity} ${pushMove.direction} and moved with it in hidden absolute coordinates. The unchanged relative offset is not by itself a failed-push/boundary cue.`
|
|
1974
|
+
);
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
return uniqueLines(lines).slice(0, 4);
|
|
1978
|
+
}
|
|
1979
|
+
function boundaryNameForDirection(direction) {
|
|
1980
|
+
if (direction === "up") return "top/northern edge of the playable area";
|
|
1981
|
+
if (direction === "down") return "bottom/southern edge of the playable area";
|
|
1982
|
+
if (direction === "left") return "left/western edge of the playable area";
|
|
1983
|
+
return "right/eastern edge of the playable area";
|
|
1984
|
+
}
|
|
1985
|
+
function collectFailedEscapeLines(query, trajectory, normalizedQuery) {
|
|
1986
|
+
if (!/\bfailed\s+moves?\b/.test(normalizedQuery) || !/\bsuccessful\s+move\s+at\s+step\s+\d+\b/.test(normalizedQuery)) {
|
|
1987
|
+
return [];
|
|
1988
|
+
}
|
|
1989
|
+
const failedGroups = [...normalizedQuery.matchAll(/\b(up|down|left|right)\s+in\s+steps?\s+(\d+)\s*(?:-|to|through|thru)\s*(\d+)\b/g)].map((match) => match[1]);
|
|
1990
|
+
if (failedGroups.length === 0) {
|
|
1991
|
+
return [];
|
|
1992
|
+
}
|
|
1993
|
+
const successStep = firstMatchInteger(normalizedQuery, /\bsuccessful\s+move\s+at\s+step\s+(\d+)\b/);
|
|
1994
|
+
const successAction = successStep === void 0 ? void 0 : trajectory.find((step) => step.step === successStep)?.action;
|
|
1995
|
+
const blocked = failedGroups.map(blockedSideName);
|
|
1996
|
+
const escapes = uniqueLines(failedGroups.map(oppositeMoveDirection));
|
|
1997
|
+
const success = successStep === void 0 ? "the later successful move" : `the successful ${normalizeActionVerb(successAction ?? "") || "escape"} move at step ${successStep}`;
|
|
1998
|
+
return [
|
|
1999
|
+
`Failed ${failedGroups.join("/")} move groups imply the agent was blocked ${formatNaturalList(blocked)} and likely cornered; the useful escape direction(s) are ${formatNaturalList(escapes)}. Treat ${success} as the escape from that blocked corner, not as missing evidence.`
|
|
2000
|
+
];
|
|
2001
|
+
}
|
|
2002
|
+
function blockedSideName(direction) {
|
|
2003
|
+
if (direction === "up") return "above";
|
|
2004
|
+
if (direction === "down") return "below";
|
|
2005
|
+
return `to the ${direction}`;
|
|
2006
|
+
}
|
|
2007
|
+
function oppositeMoveDirection(direction) {
|
|
2008
|
+
if (direction === "up") return "down";
|
|
2009
|
+
if (direction === "down") return "up";
|
|
2010
|
+
if (direction === "left") return "right";
|
|
2011
|
+
return "left";
|
|
2012
|
+
}
|
|
2013
|
+
function formatNaturalList(values) {
|
|
2014
|
+
const unique = uniqueLines(values);
|
|
2015
|
+
if (unique.length <= 1) {
|
|
2016
|
+
return unique[0] ?? "";
|
|
2017
|
+
}
|
|
2018
|
+
if (unique.length === 2) {
|
|
2019
|
+
return `${unique[0]} and ${unique[1]}`;
|
|
2020
|
+
}
|
|
2021
|
+
return `${unique.slice(0, -1).join(", ")}, and ${unique[unique.length - 1]}`;
|
|
2022
|
+
}
|
|
2023
|
+
function collectPushedPhraseGroupShiftLines(query, window, normalizedQuery) {
|
|
2024
|
+
if (!/\bpush(?:ed|es|ing)?\b/.test(normalizedQuery) || !/\b(?:hidden\s+movement\s+mechanic|standard\s+push|pushed\s+objects?|both\s+shift|horizontal\s+and\s+vertical|diagonal)\b/.test(
|
|
2025
|
+
normalizedQuery
|
|
2026
|
+
)) {
|
|
2027
|
+
return [];
|
|
2028
|
+
}
|
|
2029
|
+
const mentionsIsKey = /\bis\b/.test(normalizedQuery) && /\bkey\b/.test(normalizedQuery);
|
|
2030
|
+
if (!mentionsIsKey) {
|
|
2031
|
+
return [];
|
|
2032
|
+
}
|
|
2033
|
+
const queryMove = extractNamedActionMove(normalizedQuery);
|
|
2034
|
+
for (let index = 1; index < window.length; index += 1) {
|
|
2035
|
+
const previous = window[index - 1];
|
|
2036
|
+
const current = window[index];
|
|
2037
|
+
const move = queryMove ?? (current.action ? agentMoveDeltaFromAction(current.action) : void 0);
|
|
2038
|
+
if (!previous.observation || !current.observation || !move) {
|
|
2039
|
+
continue;
|
|
2040
|
+
}
|
|
2041
|
+
const previousPositions = parseRelativePositionEntries(previous.observation);
|
|
2042
|
+
const currentPositions = parseRelativePositionEntries(current.observation);
|
|
2043
|
+
const contactedIs = previousPositions.find(
|
|
2044
|
+
(position) => normalizeEntity(position.entity) === "rule is" && position.x === move.dx && position.y === move.dy
|
|
2045
|
+
);
|
|
2046
|
+
if (!contactedIs) {
|
|
2047
|
+
continue;
|
|
2048
|
+
}
|
|
2049
|
+
const adjacentKey = previousPositions.find(
|
|
2050
|
+
(position) => isKeyPhraseBlock(position.entity) && Math.abs(position.x - contactedIs.x) <= 2 && Math.abs(position.y - contactedIs.y) <= 1
|
|
2051
|
+
);
|
|
2052
|
+
if (!adjacentKey) {
|
|
2053
|
+
continue;
|
|
2054
|
+
}
|
|
2055
|
+
const stillHasIs = currentPositions.some(
|
|
2056
|
+
(position) => normalizeEntity(position.entity) === "rule is"
|
|
2057
|
+
);
|
|
2058
|
+
const stillHasKey = currentPositions.some(
|
|
2059
|
+
(position) => isKeyPhraseBlock(position.entity)
|
|
2060
|
+
);
|
|
2061
|
+
if (!stillHasIs || !stillHasKey) {
|
|
2062
|
+
continue;
|
|
2063
|
+
}
|
|
2064
|
+
const keyLabel = normalizeEntity(adjacentKey.entity).startsWith("rule ") ? "rule KEY" : "KEY";
|
|
2065
|
+
return [
|
|
2066
|
+
`Observation ${previous.step}->${current.step}: Action ${current.step} ${move.direction} contacts rule IS at ${contactedIs.raw}, with adjacent ${keyLabel} at ${adjacentKey.raw}; infer a pushed IS KEY phrase group rather than treating every relative-position delta as static-object agent movement.`,
|
|
2067
|
+
`The hidden pushed-object mechanic is diagonal phrase-group motion: the contacted IS block and adjacent KEY word move together one cell ${move.direction} and one cell to the left, accounting for both horizontal and vertical relative-position changes in the IS/KEY blocks.`
|
|
2068
|
+
];
|
|
2069
|
+
}
|
|
2070
|
+
return [];
|
|
2071
|
+
}
|
|
2072
|
+
function isKeyPhraseBlock(entity) {
|
|
2073
|
+
const normalized = normalizeEntity(entity);
|
|
2074
|
+
return normalized === "key" || normalized === "rule key";
|
|
2075
|
+
}
|
|
2076
|
+
function extractNamedActionMove(normalizedQuery) {
|
|
2077
|
+
const match = /\baction\s+['"`]?(up|down|left|right)['"`]?\b/.exec(normalizedQuery) ?? /\bexecutes?\s+(?:the\s+)?['"`]?(up|down|left|right)['"`]?\b/.exec(normalizedQuery);
|
|
2078
|
+
return match?.[1] ? agentMoveDeltaFromAction(match[1]) : void 0;
|
|
2079
|
+
}
|
|
2080
|
+
function collectTemporaryRuleTransformationLines(window, normalizedQuery) {
|
|
2081
|
+
if (!/\b(?:appear|appears|appeared|disappear|disappears|disappeared|temporary|transformation|hidden\s+state|reversed)\b/.test(
|
|
2082
|
+
normalizedQuery
|
|
2083
|
+
)) {
|
|
2084
|
+
return [];
|
|
2085
|
+
}
|
|
2086
|
+
const lines = [];
|
|
2087
|
+
for (let index = 1; index < window.length - 1; index += 1) {
|
|
2088
|
+
const previous = window[index - 1];
|
|
2089
|
+
const current = window[index];
|
|
2090
|
+
const next = window[index + 1];
|
|
2091
|
+
if (!previous.observation || !current.observation || !next.observation) {
|
|
2092
|
+
continue;
|
|
2093
|
+
}
|
|
2094
|
+
const previousPositions = parseRelativePositions(previous.observation);
|
|
2095
|
+
const currentPositions = parseRelativePositions(current.observation);
|
|
2096
|
+
const nextPositions = parseRelativePositions(next.observation);
|
|
2097
|
+
for (const [entity] of currentPositions.entries()) {
|
|
2098
|
+
if (entity.startsWith("rule ") || previousPositions.has(entity) || nextPositions.has(entity)) {
|
|
2099
|
+
continue;
|
|
2100
|
+
}
|
|
2101
|
+
const activeRules = /* @__PURE__ */ new Set([
|
|
2102
|
+
...parseActiveRules(previous.observation),
|
|
2103
|
+
...parseActiveRules(current.observation),
|
|
2104
|
+
...parseActiveRules(next.observation)
|
|
2105
|
+
]);
|
|
2106
|
+
if (activeRules.has(`${entity} is win`) || activeRules.has(`${entity} is you`)) {
|
|
2107
|
+
continue;
|
|
2108
|
+
}
|
|
2109
|
+
lines.push(
|
|
2110
|
+
`${entity} appears in Observation ${current.step} but is absent before and after; with surrounding actions reversing each other, explain this as a temporary hidden rule/text alignment that transformed another object into ${entity} and then broke again, not as a permanent object pickup or overlap.`
|
|
2111
|
+
);
|
|
2112
|
+
if (entity === "key") {
|
|
2113
|
+
lines.push(
|
|
2114
|
+
`For a temporary key appearance, a plausible hidden rule is BALL IS KEY or another object IS KEY formed by the right move and broken by the left move.`
|
|
2115
|
+
);
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
return uniqueLines(lines).slice(0, 4);
|
|
2120
|
+
}
|
|
2121
|
+
function collectWholeConfigurationShiftLines(window, normalizedQuery) {
|
|
2122
|
+
if (!/\b(?:entire|whole|all)\b/.test(normalizedQuery) || !/\b(?:configuration|level|objects?|rules?)\b/.test(normalizedQuery) || !/\bshift(?:ed|ing)?\b/.test(normalizedQuery)) {
|
|
2123
|
+
return [];
|
|
2124
|
+
}
|
|
2125
|
+
const lines = [];
|
|
2126
|
+
for (let index = 1; index < window.length; index += 1) {
|
|
2127
|
+
const previous = window[index - 1];
|
|
2128
|
+
const current = window[index];
|
|
2129
|
+
const move = current.action ? agentMoveDeltaFromAction(current.action) : void 0;
|
|
2130
|
+
if (!previous.observation || !current.observation || !move) {
|
|
2131
|
+
continue;
|
|
2132
|
+
}
|
|
2133
|
+
const previousPositions = parseRelativePositions(previous.observation);
|
|
2134
|
+
const currentPositions = parseRelativePositions(current.observation);
|
|
2135
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2136
|
+
for (const [entity, previousPosition] of previousPositions.entries()) {
|
|
2137
|
+
const currentPosition = currentPositions.get(entity);
|
|
2138
|
+
if (!currentPosition) {
|
|
2139
|
+
continue;
|
|
2140
|
+
}
|
|
2141
|
+
const dx = currentPosition.x - previousPosition.x;
|
|
2142
|
+
const dy = currentPosition.y - previousPosition.y;
|
|
2143
|
+
if (dx === 0 && dy === 0) {
|
|
2144
|
+
continue;
|
|
2145
|
+
}
|
|
2146
|
+
const key = `${dx}:${dy}`;
|
|
2147
|
+
const entry = counts.get(key) ?? { dx, dy, count: 0 };
|
|
2148
|
+
entry.count += 1;
|
|
2149
|
+
counts.set(key, entry);
|
|
2150
|
+
}
|
|
2151
|
+
const best = [...counts.values()].sort((left, right) => right.count - left.count)[0];
|
|
2152
|
+
if (!best || best.count < 5) {
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
lines.push(
|
|
2156
|
+
`Observation ${previous.step}->${current.step} shows a coordinated whole-level push/shift after Action ${current.step} ${move.direction}: ${best.count} tracked objects or rule words all moved ${formatRelativePosition({ x: best.dx, y: best.dy })} relative to the agent. Treat the ${move.direction} action as the progress-enabling whole-configuration contact created by the prior setup, not as ordinary empty-space movement.`
|
|
2157
|
+
);
|
|
2158
|
+
if (/\bleft\b/.test(normalizedQuery) || /\bdown\b/.test(normalizedQuery)) {
|
|
2159
|
+
lines.push(
|
|
2160
|
+
`By contrast, moving left or down would move back into empty space or undo the setup and would not trigger that coordinated configuration shift.`
|
|
2161
|
+
);
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
return uniqueLines(lines).slice(0, 3);
|
|
2165
|
+
}
|
|
2166
|
+
function collectControlRuleInteractionLines(window, normalizedQuery) {
|
|
2167
|
+
if (!/\bbaba\s+is\s+you\b/.test(normalizedQuery) || !/\b(?:interact(?:ing)?|prerequisite|position(?:ing)?|control\s+rule|own\s+rule|necessary)\b/.test(normalizedQuery)) {
|
|
2168
|
+
return [];
|
|
2169
|
+
}
|
|
2170
|
+
const latest = [...window].reverse().find((step) => step.observation);
|
|
2171
|
+
if (!latest?.observation || !parseActiveRules(latest.observation).includes("baba is you")) {
|
|
2172
|
+
return [];
|
|
2173
|
+
}
|
|
2174
|
+
const phrase = findAlignedRulePhrasePositions(
|
|
2175
|
+
parseRelativePositionEntries(latest.observation),
|
|
2176
|
+
"baba",
|
|
2177
|
+
"you"
|
|
2178
|
+
);
|
|
2179
|
+
if (!phrase) {
|
|
2180
|
+
return [];
|
|
2181
|
+
}
|
|
2182
|
+
return [
|
|
2183
|
+
`At Observation ${latest.step}, the specific target text block is rule baba at ${phrase.subject.raw}, the leftmost word of the active BABA IS YOU phrase.`,
|
|
2184
|
+
`Because BABA IS YOU makes Baba the controlled agent, the setup should not be described as directly pushing the BABA word itself from inside its own control rule. The purpose of becoming adjacent is to later push a different object or text block into the rule's syntax line to modify or break that control rule.`
|
|
2185
|
+
];
|
|
2186
|
+
}
|
|
2187
|
+
function collectNoKeyWinRepositioningCue(query, window) {
|
|
2188
|
+
const normalizedQuery = normalizeTrajectoryQuery(query);
|
|
2189
|
+
if (!/\b(?:no\s+key|key\s+objects?|hidden\s+state|reposition|solve|win\s+condition|essential)\b/.test(
|
|
2190
|
+
normalizedQuery
|
|
2191
|
+
)) {
|
|
2192
|
+
return void 0;
|
|
2193
|
+
}
|
|
2194
|
+
const observations = window.map((step) => step.observation).filter((observation) => observation !== void 0);
|
|
2195
|
+
if (observations.length === 0) {
|
|
2196
|
+
return void 0;
|
|
2197
|
+
}
|
|
2198
|
+
const rules = new Set(observations.flatMap((observation) => parseActiveRules(observation)));
|
|
2199
|
+
if (!rules.has("key is win")) {
|
|
2200
|
+
return void 0;
|
|
2201
|
+
}
|
|
2202
|
+
const positions = observations.flatMap((observation) => parseRelativePositionEntries(observation));
|
|
2203
|
+
const hasOrdinaryKey = positions.some((position) => normalizeEntity(position.entity) === "key");
|
|
2204
|
+
const explicitNoKeyPremise = hasExplicitNoKeyObjectPremise(normalizedQuery);
|
|
2205
|
+
if (hasOrdinaryKey && !explicitNoKeyPremise) {
|
|
2206
|
+
return void 0;
|
|
2207
|
+
}
|
|
2208
|
+
return explicitNoKeyPremise ? "Given the question premise that no ordinary key object should be used, the down-move sequence is changing the agent's hidden absolute row/board position so it can get below or beside rule text for later horizontal movement and rule-block pushes, rather than trying to collect a key." : "Because key is win is active but no ordinary key object appears, the down-move sequence is changing the agent's hidden absolute row/board position so it can get below or beside rule text for later horizontal movement and rule-block pushes, rather than trying to collect a key.";
|
|
2209
|
+
}
|
|
2210
|
+
function collectRuleInterventionStrategyLines(window, normalizedQuery) {
|
|
2211
|
+
const observations = window.map((step) => step.observation).filter((observation) => observation !== void 0);
|
|
2212
|
+
if (observations.length === 0) {
|
|
2213
|
+
return [];
|
|
2214
|
+
}
|
|
2215
|
+
const rules = new Set(observations.flatMap((observation) => parseActiveRules(observation)));
|
|
2216
|
+
const latest = observations[observations.length - 1];
|
|
2217
|
+
const latestPositions = parseRelativePositionEntries(latest);
|
|
2218
|
+
const allPositions = observations.flatMap((observation) => parseRelativePositionEntries(observation));
|
|
2219
|
+
const lines = [];
|
|
2220
|
+
const hasWallStop = rules.has("wall is stop");
|
|
2221
|
+
if (hasWallStop && /\b(?:door|right|blocked|impassable|optimal|progress|solve|wall)\b/.test(normalizedQuery)) {
|
|
2222
|
+
const rightBlocked = observations.some(
|
|
2223
|
+
(observation) => findBlockingStopObject(observation, ["wall"], agentMoveDeltaFromAction("right")) !== void 0
|
|
2224
|
+
);
|
|
2225
|
+
if (rightBlocked) {
|
|
2226
|
+
lines.push(
|
|
2227
|
+
`With wall is stop active, wall objects immediately to the right block the right-side/door path; continuing right is not the strategic mechanism for progress.`
|
|
2228
|
+
);
|
|
2229
|
+
}
|
|
2230
|
+
lines.push(
|
|
2231
|
+
"Strategic mechanism: progress requires reaching and manipulating the WALL IS STOP rule text so IS can be pushed out or the phrase can be broken, removing STOP from wall objects and opening blocked paths."
|
|
2232
|
+
);
|
|
2233
|
+
}
|
|
2234
|
+
const hasKeyWin = rules.has("key is win");
|
|
2235
|
+
const hasOrdinaryKey = allPositions.some((position) => normalizeEntity(position.entity) === "key");
|
|
2236
|
+
const explicitNoKeyPremise = hasExplicitNoKeyObjectPremise(normalizedQuery);
|
|
2237
|
+
const hasRuleDoor = latestPositions.some((position) => normalizeEntity(position.entity) === "rule door");
|
|
2238
|
+
if (hasKeyWin && (!hasOrdinaryKey || explicitNoKeyPremise) && /\b(?:no\s+key|key\s+objects?|hidden\s+state|reposition|solve|win\s+condition|essential)\b/.test(
|
|
2239
|
+
normalizedQuery
|
|
2240
|
+
)) {
|
|
2241
|
+
lines.push(
|
|
2242
|
+
explicitNoKeyPremise ? "Given the question premise that no ordinary key object should be used, key is win cannot be satisfied directly; solving depends on changing hidden board position to get beside/below rule text rather than collecting a key." : "Because key is win is active but no ordinary key object appears, solving depends on changing hidden board position to get beside/below rule text rather than collecting a key."
|
|
2243
|
+
);
|
|
2244
|
+
if (hasRuleDoor) {
|
|
2245
|
+
lines.push(
|
|
2246
|
+
"That repositioning sets up later rule-block pushes that can make an existing object such as door participate in the win condition, for example by forming or enabling a door/key/win rule relation."
|
|
2247
|
+
);
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
return uniqueLines(lines).slice(0, 5);
|
|
2251
|
+
}
|
|
2252
|
+
function collectMissingPushTargetLines(window, normalizedQuery) {
|
|
2253
|
+
const asksMissingProgressAction = /\b(?:absent|missing|conspicuously|lacks?|without)\b/.test(normalizedQuery) && /\b(?:progress|enabling|interaction|action|mechanic|sequence)\b/.test(normalizedQuery);
|
|
2254
|
+
const namesPush = /\bpush\b/.test(normalizedQuery);
|
|
2255
|
+
const hasPushAction = window.some((step) => normalizeActionVerb(step.action ?? "") === "push");
|
|
2256
|
+
if (!asksMissingProgressAction && !namesPush || hasPushAction) {
|
|
2257
|
+
return [];
|
|
2258
|
+
}
|
|
2259
|
+
const observationStep = window.find((step) => step.observation);
|
|
2260
|
+
if (!observationStep?.observation) {
|
|
2261
|
+
return [];
|
|
2262
|
+
}
|
|
2263
|
+
const positions = parseRelativePositionEntries(observationStep.observation);
|
|
2264
|
+
const ruleIs = positions.filter((position) => normalizeEntity(position.entity) === "rule is").sort((left, right) => manhattanDistance(left) - manhattanDistance(right))[0];
|
|
2265
|
+
if (!ruleIs) {
|
|
2266
|
+
return [];
|
|
2267
|
+
}
|
|
2268
|
+
return [
|
|
2269
|
+
`The absent progress-enabling action is push; the most logical target is the nearby rule IS text block at ${ruleIs.raw}, because pushing rule words changes rules while ordinary objects do not help unless active rules make them useful.`
|
|
2270
|
+
];
|
|
2271
|
+
}
|
|
2272
|
+
function collectRulePhraseAlignmentLines(window, normalizedQuery) {
|
|
2273
|
+
if (!hasManeuverInterpretationCue(normalizedQuery) && !normalizedQuery.includes("stop")) {
|
|
2274
|
+
return [];
|
|
2275
|
+
}
|
|
2276
|
+
const latest = [...window].reverse().find((step) => step.observation);
|
|
2277
|
+
if (!latest?.observation) {
|
|
2278
|
+
return [];
|
|
2279
|
+
}
|
|
2280
|
+
const rules = parseActiveRules(latest.observation).map(parseThreeWordRule).filter((rule) => rule !== void 0);
|
|
2281
|
+
if (rules.length === 0) {
|
|
2282
|
+
return [];
|
|
2283
|
+
}
|
|
2284
|
+
const positions = parseRelativePositionEntries(latest.observation);
|
|
2285
|
+
const lines = [];
|
|
2286
|
+
for (const rule of rules) {
|
|
2287
|
+
const phrase = findAlignedRulePhrasePositions(positions, rule.subject, rule.property);
|
|
2288
|
+
if (!phrase) {
|
|
2289
|
+
continue;
|
|
2290
|
+
}
|
|
2291
|
+
lines.push(
|
|
2292
|
+
`At Observation ${latest.step}, active rule ${rule.subject} is ${rule.property} is positioned as rule ${rule.subject} at ${phrase.subject.raw}, rule is at ${phrase.is.raw}, and rule ${rule.property} at ${phrase.property.raw}.`
|
|
2293
|
+
);
|
|
2294
|
+
if (phrase.is.x === 0 && Math.abs(phrase.is.y) === 1) {
|
|
2295
|
+
const pushDirection = phrase.is.y > 0 ? "down" : "up";
|
|
2296
|
+
lines.push(
|
|
2297
|
+
`The agent is directly ${phrase.is.y > 0 ? "above" : "below"} the rule is block, so a future ${pushDirection} action can push IS out of the phrase and break ${rule.subject} is ${rule.property}.`
|
|
2298
|
+
);
|
|
2299
|
+
if (rule.property === "stop") {
|
|
2300
|
+
lines.push(
|
|
2301
|
+
`Breaking ${rule.subject} is stop removes the STOP property from ${rule.subject} objects and can open paths blocked by those objects.`
|
|
2302
|
+
);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
return lines.slice(0, 6);
|
|
2307
|
+
}
|
|
2308
|
+
function collectRuleTextPositionLines(window, normalizedQuery) {
|
|
2309
|
+
if (!hasManeuverInterpretationCue(normalizedQuery)) {
|
|
2310
|
+
return [];
|
|
2311
|
+
}
|
|
2312
|
+
const firstByEntity = /* @__PURE__ */ new Map();
|
|
2313
|
+
const lastByEntity = /* @__PURE__ */ new Map();
|
|
2314
|
+
for (const step of window) {
|
|
2315
|
+
if (!step.observation) {
|
|
2316
|
+
continue;
|
|
2317
|
+
}
|
|
2318
|
+
for (const [entity, position] of parseRelativePositions(step.observation).entries()) {
|
|
2319
|
+
if (!entity.startsWith("rule ")) {
|
|
2320
|
+
continue;
|
|
2321
|
+
}
|
|
2322
|
+
if (!firstByEntity.has(entity)) {
|
|
2323
|
+
firstByEntity.set(entity, position);
|
|
2324
|
+
}
|
|
2325
|
+
lastByEntity.set(entity, position);
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
const movedLeft = [...firstByEntity.entries()].map(([entity, first]) => ({
|
|
2329
|
+
entity,
|
|
2330
|
+
first,
|
|
2331
|
+
last: lastByEntity.get(entity)
|
|
2332
|
+
})).filter((item) => item.last !== void 0 && item.last.x < item.first.x).sort((left, right) => left.entity.localeCompare(right.entity)).slice(0, 4);
|
|
2333
|
+
if (movedLeft.length === 0) {
|
|
2334
|
+
return [];
|
|
2335
|
+
}
|
|
2336
|
+
const descriptions = movedLeft.map(
|
|
2337
|
+
({ entity, first, last }) => `${entity} moved from ${first.raw} to ${last.raw}`
|
|
2338
|
+
);
|
|
2339
|
+
return [
|
|
2340
|
+
`${descriptions.join("; ")}; this supports describing the maneuver as repositioning the agent to the right of those rule text blocks for later rule manipulation.`
|
|
2341
|
+
];
|
|
2342
|
+
}
|
|
2343
|
+
function collectRuleStateLines(window, normalizedQuery) {
|
|
2344
|
+
const lines = [];
|
|
2345
|
+
const seenRules = /* @__PURE__ */ new Set();
|
|
2346
|
+
const seenObjects = /* @__PURE__ */ new Set();
|
|
2347
|
+
for (const step of window) {
|
|
2348
|
+
if (!step.observation) {
|
|
2349
|
+
continue;
|
|
2350
|
+
}
|
|
2351
|
+
for (const rule of parseActiveRules(step.observation)) {
|
|
2352
|
+
seenRules.add(rule);
|
|
2353
|
+
}
|
|
2354
|
+
for (const position of parseRelativePositions(step.observation).values()) {
|
|
2355
|
+
const normalizedEntity = normalizeEntity(position.entity);
|
|
2356
|
+
if (!normalizedEntity.startsWith("rule ")) {
|
|
2357
|
+
seenObjects.add(normalizedEntity);
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
const activeRules = [...seenRules].sort((left, right) => left.localeCompare(right));
|
|
2362
|
+
const asksExactRelativeState = hasExactRelativeStateIntent(normalizedQuery);
|
|
2363
|
+
if (activeRules.length > 0) {
|
|
2364
|
+
lines.push(`Active rules in this window: ${activeRules.join("; ")}.`);
|
|
2365
|
+
}
|
|
2366
|
+
for (const object of [...seenObjects].sort((left, right) => left.localeCompare(right))) {
|
|
2367
|
+
if (!activeRules.some((rule) => rule.startsWith(`${object} is `))) {
|
|
2368
|
+
if (!shouldExplainMissingObjectPushRule(object, normalizedQuery, asksExactRelativeState)) {
|
|
2369
|
+
continue;
|
|
2370
|
+
}
|
|
2371
|
+
lines.push(
|
|
2372
|
+
`No active rule for ${object} appears in this window; specifically, no "${object} is push" rule is active, so treat ${object} as not currently pushable and do not claim that a current move pushed ${object} unless the observations state that rule.`
|
|
2373
|
+
);
|
|
2374
|
+
lines.push(
|
|
2375
|
+
`For ${object} maneuver questions, make bypassing or repositioning around a not-pushable obstacle the strategic goal unless an active rule says "${object} is push"; do not make overlap, collection, removal, or pushing the goal unless the question asks that exact relative state.`
|
|
2376
|
+
);
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
return lines.slice(0, 8);
|
|
2380
|
+
}
|
|
2381
|
+
function shouldExplainMissingObjectPushRule(object, normalizedQuery, asksExactRelativeState) {
|
|
2382
|
+
if (asksExactRelativeState || !normalizedQuery.includes(object) || !hasManeuverInterpretationCue(normalizedQuery)) {
|
|
2383
|
+
return false;
|
|
2384
|
+
}
|
|
2385
|
+
return /\b(?:implicit\s+property|not\s+pushable|unpushable|not-pushable|bypass|obstacle|around\s+(?:it|the)|ordinary\s+object)\b/.test(
|
|
2386
|
+
normalizedQuery
|
|
2387
|
+
);
|
|
2388
|
+
}
|
|
2389
|
+
function parseStopRuleObjects(observation) {
|
|
2390
|
+
return parseActiveRules(observation).map((rule) => /^(.+?)\s+is\s+stop$/i.exec(rule)?.[1]?.trim()).filter((object) => !!object);
|
|
2391
|
+
}
|
|
2392
|
+
function findBlockingStopObject(observation, stopObjects, move) {
|
|
2393
|
+
const positions = parseRelativePositionEntries(observation);
|
|
2394
|
+
for (const object of stopObjects) {
|
|
2395
|
+
for (const position of positions) {
|
|
2396
|
+
if (normalizeEntity(position.entity) !== object) {
|
|
2397
|
+
continue;
|
|
2398
|
+
}
|
|
2399
|
+
if (position.x === move.dx && position.y === move.dy) {
|
|
2400
|
+
return { entity: object, position };
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
2404
|
+
return void 0;
|
|
2405
|
+
}
|
|
2406
|
+
function openNonStopMoves(observation, stopObjects, blockedDirection) {
|
|
2407
|
+
const open = [];
|
|
2408
|
+
for (const direction of ["up", "down", "left", "right"]) {
|
|
2409
|
+
if (direction === blockedDirection) {
|
|
2410
|
+
continue;
|
|
2411
|
+
}
|
|
2412
|
+
const move = agentMoveDeltaFromAction(direction);
|
|
2413
|
+
if (!move || findBlockingStopObject(observation, stopObjects, move)) {
|
|
2414
|
+
continue;
|
|
2415
|
+
}
|
|
2416
|
+
open.push(direction);
|
|
2417
|
+
}
|
|
2418
|
+
return open;
|
|
2419
|
+
}
|
|
2420
|
+
function formatMoveList(moves) {
|
|
2421
|
+
if (moves.length === 0) {
|
|
2422
|
+
return "";
|
|
2423
|
+
}
|
|
2424
|
+
if (moves.length === 1) {
|
|
2425
|
+
return moves[0];
|
|
2426
|
+
}
|
|
2427
|
+
if (moves.length === 2) {
|
|
2428
|
+
return `${moves[0]} and ${moves[1]}`;
|
|
2429
|
+
}
|
|
2430
|
+
return `${moves.slice(0, -1).join(", ")}, and ${moves[moves.length - 1]}`;
|
|
2431
|
+
}
|
|
2432
|
+
function parseThreeWordRule(rule) {
|
|
2433
|
+
const match = /^(.+?)\s+is\s+(.+)$/i.exec(rule.trim());
|
|
2434
|
+
if (!match?.[1] || !match[2]) {
|
|
2435
|
+
return void 0;
|
|
2436
|
+
}
|
|
2437
|
+
return {
|
|
2438
|
+
subject: normalizeEntity(match[1]),
|
|
2439
|
+
property: normalizeEntity(match[2])
|
|
2440
|
+
};
|
|
2441
|
+
}
|
|
2442
|
+
function findAlignedRulePhrasePositions(positions, subject, property) {
|
|
2443
|
+
const subjects = positions.filter((position) => normalizeEntity(position.entity) === `rule ${subject}`);
|
|
2444
|
+
const isWords = positions.filter((position) => normalizeEntity(position.entity) === "rule is");
|
|
2445
|
+
const properties = positions.filter((position) => normalizeEntity(position.entity) === `rule ${property}`);
|
|
2446
|
+
let best;
|
|
2447
|
+
for (const subjectPosition of subjects) {
|
|
2448
|
+
for (const isPosition of isWords) {
|
|
2449
|
+
for (const propertyPosition of properties) {
|
|
2450
|
+
const sameRowPenalty = Math.abs(subjectPosition.y - isPosition.y) + Math.abs(propertyPosition.y - isPosition.y);
|
|
2451
|
+
const orderPenalty = subjectPosition.x < isPosition.x && isPosition.x < propertyPosition.x ? 0 : 10;
|
|
2452
|
+
const spacingPenalty = Math.abs(isPosition.x - subjectPosition.x - 1) + Math.abs(propertyPosition.x - isPosition.x - 1);
|
|
2453
|
+
const score = sameRowPenalty * 5 + orderPenalty + spacingPenalty;
|
|
2454
|
+
if (!best || score < best.score) {
|
|
2455
|
+
best = {
|
|
2456
|
+
subject: subjectPosition,
|
|
2457
|
+
is: isPosition,
|
|
2458
|
+
property: propertyPosition,
|
|
2459
|
+
score
|
|
2460
|
+
};
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
if (!best || best.score > 4) {
|
|
2466
|
+
return void 0;
|
|
2467
|
+
}
|
|
2468
|
+
return {
|
|
2469
|
+
subject: best.subject,
|
|
2470
|
+
is: best.is,
|
|
2471
|
+
property: best.property
|
|
2472
|
+
};
|
|
2473
|
+
}
|
|
2474
|
+
function parseRelativePositionEntries(observation) {
|
|
2475
|
+
const objectsBlock = /objects on the map:\s*([\s\S]*)$/i.exec(observation)?.[1];
|
|
2476
|
+
if (!objectsBlock) {
|
|
2477
|
+
return [];
|
|
2478
|
+
}
|
|
2479
|
+
const positions = [];
|
|
2480
|
+
for (const rawLine of objectsBlock.split(/\n+/)) {
|
|
2481
|
+
const parsed = parseRelativePositionLine(rawLine.trim());
|
|
2482
|
+
if (parsed) {
|
|
2483
|
+
positions.push(parsed);
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
return positions;
|
|
2487
|
+
}
|
|
2488
|
+
function uniqueLines(lines) {
|
|
2489
|
+
return [...new Set(lines)];
|
|
2490
|
+
}
|
|
2491
|
+
function appendActionFrequencyLines(lines, trajectory, bounds) {
|
|
2492
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2493
|
+
for (const step of boundedTrajectory(trajectory, bounds)) {
|
|
2494
|
+
if (!step.action) continue;
|
|
2495
|
+
const verb = normalizeActionVerb(step.action);
|
|
2496
|
+
if (!verb) continue;
|
|
2497
|
+
counts.set(verb, (counts.get(verb) ?? 0) + 1);
|
|
2498
|
+
}
|
|
2499
|
+
if (counts.size === 0) {
|
|
2500
|
+
return;
|
|
2501
|
+
}
|
|
2502
|
+
const frequencies = [...counts.entries()].sort(
|
|
2503
|
+
(left, right) => right[1] - left[1] || left[0].localeCompare(right[0])
|
|
2504
|
+
);
|
|
2505
|
+
lines.push(
|
|
2506
|
+
`Action frequency through requested window: ${frequencies.map(([verb, count]) => `${verb}=${count}`).join(", ")}.`
|
|
2507
|
+
);
|
|
2508
|
+
}
|
|
2509
|
+
function appendInventoryChangeLines(lines, trajectory, bounds) {
|
|
2510
|
+
const held = /* @__PURE__ */ new Set();
|
|
2511
|
+
const changes = [];
|
|
2512
|
+
for (const step of boundedTrajectory(trajectory, bounds)) {
|
|
2513
|
+
if (!step.action) continue;
|
|
2514
|
+
const change = parseInventoryChange(step.action, held);
|
|
2515
|
+
if (change) {
|
|
2516
|
+
changes.push({ step: step.step, action: step.action, change });
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
if (changes.length === 0) {
|
|
2520
|
+
return;
|
|
2521
|
+
}
|
|
2522
|
+
lines.push("Inventory changes from action transcript:");
|
|
2523
|
+
if (changes.length > 5) {
|
|
2524
|
+
lines.push(
|
|
2525
|
+
`First five inventory changes: ${formatInventoryChangeSummary(changes.slice(0, 5))}.`
|
|
2526
|
+
);
|
|
2527
|
+
lines.push(
|
|
2528
|
+
`Complete inventory changes: ${formatInventoryChangeSummary(changes)}.`
|
|
2529
|
+
);
|
|
2530
|
+
}
|
|
2531
|
+
for (const change of changes) {
|
|
2532
|
+
lines.push(`[Action ${change.step}]: ${change.action} => ${change.change}`);
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
function formatInventoryChangeSummary(changes) {
|
|
2536
|
+
return changes.map((change) => `step ${change.step}: ${summarizeInventoryChange(change.change)}`).join("; ");
|
|
2537
|
+
}
|
|
2538
|
+
function summarizeInventoryChange(change) {
|
|
2539
|
+
const added = /^inventory added (.+?);/i.exec(change);
|
|
2540
|
+
if (added) {
|
|
2541
|
+
return `${added[1].trim()} added`;
|
|
2542
|
+
}
|
|
2543
|
+
const removed = /^inventory removed (.+?);/i.exec(change) ?? /^inventory removed (.+)$/i.exec(change);
|
|
2544
|
+
if (removed) {
|
|
2545
|
+
return `${removed[1].trim()} removed`;
|
|
2546
|
+
}
|
|
2547
|
+
return change;
|
|
2548
|
+
}
|
|
2549
|
+
function appendContainerStateChangeLines(lines, trajectory, bounds) {
|
|
2550
|
+
const changes = collectContainerStateChanges(trajectory, bounds);
|
|
2551
|
+
if (changes.length === 0) {
|
|
2552
|
+
return;
|
|
2553
|
+
}
|
|
2554
|
+
lines.push("Container open/close state changes:");
|
|
2555
|
+
for (const change of changes) {
|
|
2556
|
+
lines.push(`[Action ${change.step}]: ${change.action} => ${change.entity} ${change.state}`);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
function appendEntityTimelineLines(lines, trajectory, bounds, entities, options) {
|
|
2560
|
+
for (const entity of entities) {
|
|
2561
|
+
const includeIndirectMentions = options.includeIndirectMentions || options.includeMovableStateActions && !isLikelyContainerEntity(entity);
|
|
2562
|
+
const directActions = boundedTrajectory(trajectory, bounds).filter(
|
|
2563
|
+
(step) => step.action && (isDirectEntityAction(step.action, entity) || includeIndirectMentions && actionMentionsEntity(step.action, entity))
|
|
2564
|
+
);
|
|
2565
|
+
const stateChanges = collectContainerStateChanges(trajectory, bounds).filter(
|
|
2566
|
+
(change) => normalizeEntity(change.entity) === normalizeEntity(entity)
|
|
2567
|
+
);
|
|
2568
|
+
const objectTransfers = options.includeContainerObjectTransfers ? collectContainerObjectTransfers(trajectory, bounds).filter(
|
|
2569
|
+
(transfer) => normalizeEntity(transfer.container) === normalizeEntity(entity)
|
|
2570
|
+
) : [];
|
|
2571
|
+
if (directActions.length === 0 && stateChanges.length === 0 && objectTransfers.length === 0) {
|
|
2572
|
+
continue;
|
|
2573
|
+
}
|
|
2574
|
+
lines.push(`Timeline for ${entity}:`);
|
|
2575
|
+
for (const step of directActions) {
|
|
2576
|
+
lines.push(`[Action ${step.step}]: ${step.action}`);
|
|
2577
|
+
}
|
|
2578
|
+
if (objectTransfers.length > 0) {
|
|
2579
|
+
lines.push(`Object transfers involving ${entity}:`);
|
|
2580
|
+
for (const transfer of objectTransfers) {
|
|
2581
|
+
lines.push(`[Action ${transfer.step}]: ${transfer.action} => ${formatContainerTransfer(transfer)}`);
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
const inferredLocation = inferEntityLocation(trajectory, bounds, entity);
|
|
2585
|
+
if (inferredLocation) {
|
|
2586
|
+
lines.push(
|
|
2587
|
+
`Inferred ${entity} location at step ${bounds.end}: ${inferredLocation}.`
|
|
2588
|
+
);
|
|
2589
|
+
}
|
|
2590
|
+
if (stateChanges.length > 0) {
|
|
2591
|
+
const latest = stateChanges[stateChanges.length - 1];
|
|
2592
|
+
lines.push(
|
|
2593
|
+
`Latest ${entity} state at step ${bounds.end}: ${latest.state}; state changes: ${stateChanges.map((change) => `${change.step}:${change.state}`).join(", ")}.`
|
|
2594
|
+
);
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
function boundedTrajectory(trajectory, bounds) {
|
|
2599
|
+
return trajectory.filter((step) => step.step >= bounds.start && step.step <= bounds.end);
|
|
2600
|
+
}
|
|
2601
|
+
function collectContainerStateChanges(trajectory, bounds) {
|
|
2602
|
+
const changes = [];
|
|
2603
|
+
for (const step of boundedTrajectory(trajectory, bounds)) {
|
|
2604
|
+
if (!step.action) continue;
|
|
2605
|
+
const match = /^(open|close)\s+(.+)$/i.exec(step.action.trim());
|
|
2606
|
+
if (!match) continue;
|
|
2607
|
+
const verb = match[1].toLowerCase();
|
|
2608
|
+
changes.push({
|
|
2609
|
+
step: step.step,
|
|
2610
|
+
action: step.action,
|
|
2611
|
+
entity: match[2].trim(),
|
|
2612
|
+
state: verb === "open" ? "open" : "closed"
|
|
2613
|
+
});
|
|
2614
|
+
}
|
|
2615
|
+
return changes;
|
|
2616
|
+
}
|
|
2617
|
+
function collectContainerObjectTransfers(trajectory, bounds) {
|
|
2618
|
+
const transfers = [];
|
|
2619
|
+
for (const step of boundedTrajectory(trajectory, bounds)) {
|
|
2620
|
+
if (!step.action) continue;
|
|
2621
|
+
const action = step.action.trim();
|
|
2622
|
+
const take = /^take\s+(.+?)\s+from\s+(.+)$/i.exec(action);
|
|
2623
|
+
if (take) {
|
|
2624
|
+
transfers.push({
|
|
2625
|
+
step: step.step,
|
|
2626
|
+
action: step.action,
|
|
2627
|
+
object: take[1].trim(),
|
|
2628
|
+
container: take[2].trim(),
|
|
2629
|
+
direction: "removed"
|
|
2630
|
+
});
|
|
2631
|
+
continue;
|
|
2632
|
+
}
|
|
2633
|
+
const place = /^(?:move|put|place|insert)\s+(.+?)\s+(?:to|in|into|on)\s+(.+)$/i.exec(
|
|
2634
|
+
action
|
|
2635
|
+
);
|
|
2636
|
+
if (!place) continue;
|
|
2637
|
+
transfers.push({
|
|
2638
|
+
step: step.step,
|
|
2639
|
+
action: step.action,
|
|
2640
|
+
object: place[1].trim(),
|
|
2641
|
+
container: place[2].trim(),
|
|
2642
|
+
direction: "placed"
|
|
2643
|
+
});
|
|
2644
|
+
}
|
|
2645
|
+
return transfers;
|
|
2646
|
+
}
|
|
2647
|
+
function formatContainerTransfer(transfer) {
|
|
2648
|
+
return transfer.direction === "placed" ? `${transfer.object} placed in ${transfer.container}` : `${transfer.object} removed from ${transfer.container}`;
|
|
2649
|
+
}
|
|
2650
|
+
function parseInventoryChange(action, held) {
|
|
2651
|
+
const normalized = action.trim();
|
|
2652
|
+
const take = /^take\s+(.+?)\s+from\s+(.+)$/i.exec(normalized);
|
|
2653
|
+
if (take) {
|
|
2654
|
+
const object = take[1].trim();
|
|
2655
|
+
const source = take[2].trim();
|
|
2656
|
+
held.add(normalizeEntity(object));
|
|
2657
|
+
return `inventory added ${object}; ${object} removed from ${source}`;
|
|
2658
|
+
}
|
|
2659
|
+
const place = /^(?:move|put|place|insert)\s+(.+?)\s+(?:to|in|into|on)\s+(.+)$/i.exec(
|
|
2660
|
+
normalized
|
|
2661
|
+
);
|
|
2662
|
+
if (place) {
|
|
2663
|
+
const object = place[1].trim();
|
|
2664
|
+
const destination = place[2].trim();
|
|
2665
|
+
const key = normalizeEntity(object);
|
|
2666
|
+
const wasHeld = held.delete(key);
|
|
2667
|
+
return wasHeld ? `inventory removed ${object}; ${object} moved to ${destination}` : `${object} moved to ${destination}`;
|
|
2668
|
+
}
|
|
2669
|
+
const drop = /^drop\s+(.+)$/i.exec(normalized);
|
|
2670
|
+
if (drop) {
|
|
2671
|
+
const object = drop[1].trim();
|
|
2672
|
+
held.delete(normalizeEntity(object));
|
|
2673
|
+
return `inventory removed ${object}`;
|
|
2674
|
+
}
|
|
2675
|
+
return void 0;
|
|
2676
|
+
}
|
|
2677
|
+
function asksForTrajectoryActionRange(normalizedQuery) {
|
|
2678
|
+
return [
|
|
2679
|
+
/\bwhat\s+actions?\s+were\s+performed\b/,
|
|
2680
|
+
/\bwhich\s+actions?\s+were\s+performed\b/,
|
|
2681
|
+
/\bsequence\s+of\s+actions?\b/,
|
|
2682
|
+
/\bactions?\s+(?:between|from|during|within)\b/
|
|
2683
|
+
].some((pattern) => pattern.test(normalizedQuery));
|
|
2684
|
+
}
|
|
2685
|
+
function asksForActionFrequency(normalizedQuery) {
|
|
2686
|
+
return /\btypes?\s+of\s+actions?\b/.test(normalizedQuery) || /\bfrequen/.test(normalizedQuery) || /\bhow\s+often\b/.test(normalizedQuery);
|
|
2687
|
+
}
|
|
2688
|
+
function extractExplicitTrajectoryRange(normalizedQuery) {
|
|
2689
|
+
const patterns = [
|
|
2690
|
+
/\b(?:between|from|during|within)\s+(?:steps?|actions?|observations?)\s+(\d+)\s*(?:-|to|through|thru|and)\s*(?:(?:steps?|actions?|observations?)\s+)?(\d+)\b/,
|
|
2691
|
+
/\b(?:steps?|actions?|observations?)\s+(\d+)\s*(?:-|to|through|thru)\s*(?:(?:steps?|actions?|observations?)\s+)?(\d+)\b/
|
|
2692
|
+
];
|
|
2693
|
+
for (const pattern of patterns) {
|
|
2694
|
+
const match = pattern.exec(normalizedQuery);
|
|
2695
|
+
if (!match) continue;
|
|
2696
|
+
const start = parseNonNegativeIntegerToken(match[1] ?? "");
|
|
2697
|
+
const end = parseNonNegativeIntegerToken(match[2] ?? "");
|
|
2698
|
+
if (start !== void 0 && end !== void 0) {
|
|
2699
|
+
return { start, end };
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
return void 0;
|
|
2703
|
+
}
|
|
2704
|
+
function findObservationTransition(query, trajectory) {
|
|
2705
|
+
const quoted = extractQuotedObservations(query);
|
|
2706
|
+
if (quoted.length < 2) {
|
|
2707
|
+
return void 0;
|
|
2708
|
+
}
|
|
2709
|
+
const fromCandidates = findObservationStepCandidates(trajectory, quoted[0]);
|
|
2710
|
+
const toCandidates = findObservationStepCandidates(trajectory, quoted[1]);
|
|
2711
|
+
if (fromCandidates.length === 0 || toCandidates.length === 0) {
|
|
2712
|
+
return void 0;
|
|
2713
|
+
}
|
|
2714
|
+
let best;
|
|
2715
|
+
const pairs = [];
|
|
2716
|
+
for (const fromStep of fromCandidates) {
|
|
2717
|
+
for (const toStep of toCandidates) {
|
|
2718
|
+
if (toStep <= fromStep) {
|
|
2719
|
+
continue;
|
|
2720
|
+
}
|
|
2721
|
+
pairs.push({ fromStep, toStep });
|
|
2722
|
+
if (!best || toStep - fromStep < best.toStep - best.fromStep || toStep - fromStep === best.toStep - best.fromStep && fromStep < best.fromStep) {
|
|
2723
|
+
best = { fromStep, toStep };
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
if (best) {
|
|
2728
|
+
return selectObservationTransition(trajectory, best, pairs);
|
|
2729
|
+
}
|
|
2730
|
+
return void 0;
|
|
2731
|
+
}
|
|
2732
|
+
function selectObservationTransition(trajectory, shortest, pairs) {
|
|
2733
|
+
const gap = shortest.toStep - shortest.fromStep;
|
|
2734
|
+
const targetStep = trajectory.find((step) => step.step === shortest.toStep);
|
|
2735
|
+
if (gap > 2 || normalizeActionVerb(targetStep?.action ?? "") !== "look") {
|
|
2736
|
+
return shortest;
|
|
2737
|
+
}
|
|
2738
|
+
const nextRepeatedTarget = pairs.filter(
|
|
2739
|
+
(pair) => pair.fromStep === shortest.fromStep && pair.toStep > shortest.toStep && pair.toStep - shortest.fromStep <= 8
|
|
2740
|
+
).sort((left, right) => left.toStep - right.toStep)[0];
|
|
2741
|
+
if (!nextRepeatedTarget || !observationsHaveSameFullText(trajectory, shortest.toStep, nextRepeatedTarget.toStep)) {
|
|
2742
|
+
return shortest;
|
|
2743
|
+
}
|
|
2744
|
+
return nextRepeatedTarget;
|
|
2745
|
+
}
|
|
2746
|
+
function observationsHaveSameFullText(trajectory, leftStep, rightStep) {
|
|
2747
|
+
const left = trajectory.find((step) => step.step === leftStep)?.observation;
|
|
2748
|
+
const right = trajectory.find((step) => step.step === rightStep)?.observation;
|
|
2749
|
+
return !!left && !!right && normalizeObservationText(left) === normalizeObservationText(right);
|
|
2750
|
+
}
|
|
2751
|
+
function extractQuotedObservations(query) {
|
|
2752
|
+
const observations = [];
|
|
2753
|
+
for (const match of query.matchAll(/\bobservation:\s*"([\s\S]*?)"/gi)) {
|
|
2754
|
+
const value = match[1]?.trim();
|
|
2755
|
+
if (value) {
|
|
2756
|
+
observations.push(value);
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
return observations;
|
|
2760
|
+
}
|
|
2761
|
+
function findObservationStepCandidates(trajectory, quotedObservation) {
|
|
2762
|
+
const normalizedQuoted = normalizeObservationText(quotedObservation);
|
|
2763
|
+
const normalizedQuotedCore = normalizeObservationCore(quotedObservation);
|
|
2764
|
+
const fullCandidates = /* @__PURE__ */ new Set();
|
|
2765
|
+
const coreCandidates = /* @__PURE__ */ new Set();
|
|
2766
|
+
for (const step of trajectory) {
|
|
2767
|
+
if (!step.observation) continue;
|
|
2768
|
+
const normalizedObservation = normalizeObservationText(step.observation);
|
|
2769
|
+
if (normalizedObservation === normalizedQuoted || normalizedObservation.includes(normalizedQuoted) || normalizedQuoted.includes(normalizedObservation)) {
|
|
2770
|
+
fullCandidates.add(step.step);
|
|
2771
|
+
continue;
|
|
2772
|
+
}
|
|
2773
|
+
const normalizedObservationCore = normalizeObservationCore(step.observation);
|
|
2774
|
+
if (normalizedObservationCore.length > 0 && normalizedQuotedCore.length > 0 && (normalizedObservationCore === normalizedQuotedCore || normalizedObservationCore.includes(normalizedQuotedCore) || normalizedQuotedCore.includes(normalizedObservationCore))) {
|
|
2775
|
+
coreCandidates.add(step.step);
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
const candidates = /* @__PURE__ */ new Set([...fullCandidates, ...coreCandidates]);
|
|
2779
|
+
return [...candidates].sort((left, right) => left - right);
|
|
2780
|
+
}
|
|
2781
|
+
function extractNumberedEntities(query) {
|
|
2782
|
+
const entities = /* @__PURE__ */ new Set();
|
|
2783
|
+
for (const match of query.matchAll(/\b([a-z][a-z0-9_-]*(?:\s+[a-z][a-z0-9_-]*)?)\s+(\d+)\b/gi)) {
|
|
2784
|
+
const rawPrefix = match[1]?.trim().toLowerCase();
|
|
2785
|
+
const number = match[2]?.trim();
|
|
2786
|
+
if (!rawPrefix || !number || isTrajectoryReferenceEntity(rawPrefix)) {
|
|
2787
|
+
continue;
|
|
2788
|
+
}
|
|
2789
|
+
const words = rawPrefix.split(/\s+/);
|
|
2790
|
+
const entityHead = words[words.length - 1];
|
|
2791
|
+
if (isTrajectoryReferenceEntity(entityHead)) {
|
|
2792
|
+
continue;
|
|
2793
|
+
}
|
|
2794
|
+
entities.add(`${entityHead} ${number}`);
|
|
2795
|
+
}
|
|
2796
|
+
return [...entities].sort((left, right) => left.localeCompare(right));
|
|
2797
|
+
}
|
|
2798
|
+
function isTrajectoryReferenceEntity(value) {
|
|
2799
|
+
return [
|
|
2800
|
+
"action",
|
|
2801
|
+
"actions",
|
|
2802
|
+
"observation",
|
|
2803
|
+
"observations",
|
|
2804
|
+
"step",
|
|
2805
|
+
"steps",
|
|
2806
|
+
"turn",
|
|
2807
|
+
"turns"
|
|
2808
|
+
].includes(value);
|
|
2809
|
+
}
|
|
2810
|
+
function actionMentionsEntity(action, entity) {
|
|
2811
|
+
const normalizedAction = normalizeTrajectoryQuery(action);
|
|
2812
|
+
const normalizedEntity = normalizeEntity(entity);
|
|
2813
|
+
return normalizedAction === normalizedEntity || normalizedAction.startsWith(`${normalizedEntity} `) || normalizedAction.endsWith(` ${normalizedEntity}`) || normalizedAction.includes(` ${normalizedEntity} `);
|
|
2814
|
+
}
|
|
2815
|
+
function isDirectEntityAction(action, entity) {
|
|
2816
|
+
const normalizedAction = normalizeTrajectoryQuery(action);
|
|
2817
|
+
const normalizedEntity = normalizeEntity(entity);
|
|
2818
|
+
return [
|
|
2819
|
+
`open ${normalizedEntity}`,
|
|
2820
|
+
`close ${normalizedEntity}`,
|
|
2821
|
+
`examine ${normalizedEntity}`
|
|
2822
|
+
].includes(normalizedAction);
|
|
2823
|
+
}
|
|
2824
|
+
function isLikelyContainerEntity(entity) {
|
|
2825
|
+
const head = normalizeEntity(entity).split(/\s+/)[0];
|
|
2826
|
+
return [
|
|
2827
|
+
"bed",
|
|
2828
|
+
"cabinet",
|
|
2829
|
+
"counter",
|
|
2830
|
+
"countertop",
|
|
2831
|
+
"coffeemachine",
|
|
2832
|
+
"desk",
|
|
2833
|
+
"drawer",
|
|
2834
|
+
"fridge",
|
|
2835
|
+
"garbagecan",
|
|
2836
|
+
"handtowelholder",
|
|
2837
|
+
"laundryhamper",
|
|
2838
|
+
"microwave",
|
|
2839
|
+
"safe",
|
|
2840
|
+
"shelf",
|
|
2841
|
+
"sinkbasin",
|
|
2842
|
+
"stoveburner",
|
|
2843
|
+
"toaster",
|
|
2844
|
+
"toilet",
|
|
2845
|
+
"toiletpaperhanger",
|
|
2846
|
+
"towelholder"
|
|
2847
|
+
].includes(head ?? "");
|
|
2848
|
+
}
|
|
2849
|
+
function inferEntityLocation(trajectory, bounds, entity) {
|
|
2850
|
+
const normalizedEntity = normalizeEntity(entity);
|
|
2851
|
+
let location;
|
|
2852
|
+
for (const step of boundedTrajectory(trajectory, bounds)) {
|
|
2853
|
+
if (!step.action) continue;
|
|
2854
|
+
const action = step.action.trim();
|
|
2855
|
+
const take = /^take\s+(.+?)\s+from\s+(.+)$/i.exec(action);
|
|
2856
|
+
if (take && normalizeEntity(take[1]) === normalizedEntity) {
|
|
2857
|
+
location = "inventory";
|
|
2858
|
+
continue;
|
|
2859
|
+
}
|
|
2860
|
+
const move = /^(?:move|put|place|insert)\s+(.+?)\s+(?:to|in|into|on)\s+(.+)$/i.exec(
|
|
2861
|
+
action
|
|
2862
|
+
);
|
|
2863
|
+
if (move && normalizeEntity(move[1]) === normalizedEntity) {
|
|
2864
|
+
location = move[2].trim();
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
2867
|
+
return location;
|
|
2868
|
+
}
|
|
2869
|
+
function normalizeActionVerb(action) {
|
|
2870
|
+
const match = /^([a-z][a-z0-9_-]*)\b/i.exec(action.trim());
|
|
2871
|
+
return match?.[1]?.toLowerCase();
|
|
2872
|
+
}
|
|
2873
|
+
function clampTrajectoryBounds(start, end, minStep, maxStep, reason) {
|
|
2874
|
+
const low = Math.max(minStep, Math.min(start, end));
|
|
2875
|
+
const high = Math.min(maxStep, Math.max(start, end));
|
|
2876
|
+
return {
|
|
2877
|
+
start: Math.min(low, high),
|
|
2878
|
+
end: Math.max(low, high),
|
|
2879
|
+
reason
|
|
2880
|
+
};
|
|
2881
|
+
}
|
|
2882
|
+
function firstMatchInteger(query, pattern) {
|
|
2883
|
+
const match = pattern.exec(query);
|
|
2884
|
+
return match ? parseNonNegativeIntegerToken(match[1] ?? "") : void 0;
|
|
2885
|
+
}
|
|
2886
|
+
function normalizeTurnExpansionEnd(stats) {
|
|
2887
|
+
const messageCountEnd = Math.max(0, Math.floor(stats.totalMessages) - 1);
|
|
2888
|
+
if (typeof stats.maxTurnIndex !== "number" || !Number.isFinite(stats.maxTurnIndex)) {
|
|
2889
|
+
return messageCountEnd;
|
|
2890
|
+
}
|
|
2891
|
+
return Math.max(messageCountEnd, Math.floor(stats.maxTurnIndex));
|
|
2892
|
+
}
|
|
2893
|
+
function truncateTrajectoryAnalysisLines(lines, maxChars) {
|
|
2894
|
+
const result = [];
|
|
2895
|
+
let used = 0;
|
|
2896
|
+
for (const line of lines.slice(0, TRAJECTORY_ANALYSIS_MAX_LINES)) {
|
|
2897
|
+
const separator = result.length === 0 ? 0 : 1;
|
|
2898
|
+
const remaining = maxChars - used - separator;
|
|
2899
|
+
if (remaining <= 0) {
|
|
2900
|
+
break;
|
|
2901
|
+
}
|
|
2902
|
+
const clipped = line.length > remaining ? remaining <= 3 ? line.slice(0, remaining) : `${line.slice(0, remaining - 3).trimEnd()}...` : line;
|
|
2903
|
+
if (clipped.length === 0) {
|
|
2904
|
+
break;
|
|
2905
|
+
}
|
|
2906
|
+
result.push(clipped);
|
|
2907
|
+
used += separator + clipped.length;
|
|
2908
|
+
}
|
|
2909
|
+
return result;
|
|
2910
|
+
}
|
|
2911
|
+
function oneLine(value) {
|
|
2912
|
+
return value.replace(/\s+/g, " ").trim();
|
|
2913
|
+
}
|
|
2914
|
+
function normalizeEntity(value) {
|
|
2915
|
+
return normalizeTrajectoryQuery(value);
|
|
2916
|
+
}
|
|
2917
|
+
function normalizeObservationText(value) {
|
|
2918
|
+
return value.toLowerCase().replace(/\s+/g, " ").trim();
|
|
2919
|
+
}
|
|
2920
|
+
function normalizeObservationCore(value) {
|
|
2921
|
+
return normalizeObservationText(
|
|
2922
|
+
value.split(/\bthe current available actions are:/i)[0] ?? value
|
|
2923
|
+
);
|
|
2924
|
+
}
|
|
2925
|
+
function parseActiveRules(observation) {
|
|
2926
|
+
const rulesBlock = /active rules:\s*([\s\S]*?)(?:\n\s*\n|objects on the map:|$)/i.exec(
|
|
2927
|
+
observation
|
|
2928
|
+
)?.[1];
|
|
2929
|
+
if (!rulesBlock) {
|
|
2930
|
+
return [];
|
|
2931
|
+
}
|
|
2932
|
+
return rulesBlock.split(/\n+/).map((line) => normalizeTrajectoryQuery(line)).filter((line) => line.length > 0);
|
|
2933
|
+
}
|
|
2934
|
+
function parseRelativePositions(observation) {
|
|
2935
|
+
const positions = /* @__PURE__ */ new Map();
|
|
2936
|
+
const objectsBlock = /objects on the map:\s*([\s\S]*)$/i.exec(observation)?.[1];
|
|
2937
|
+
if (!objectsBlock) {
|
|
2938
|
+
return positions;
|
|
2939
|
+
}
|
|
2940
|
+
for (const rawLine of objectsBlock.split(/\n+/)) {
|
|
2941
|
+
const line = rawLine.trim();
|
|
2942
|
+
if (!line) {
|
|
2943
|
+
continue;
|
|
2944
|
+
}
|
|
2945
|
+
const parsed = parseRelativePositionLine(line);
|
|
2946
|
+
if (parsed) {
|
|
2947
|
+
positions.set(normalizeEntity(parsed.entity), parsed);
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
return positions;
|
|
2951
|
+
}
|
|
2952
|
+
function parseRelativePositionLine(line) {
|
|
2953
|
+
const normalizedLine = line.replace(/`/g, "").trim();
|
|
2954
|
+
const positionStart = findRelativePositionStart(normalizedLine);
|
|
2955
|
+
if (positionStart <= 0) {
|
|
2956
|
+
return void 0;
|
|
2957
|
+
}
|
|
2958
|
+
const entity = normalizedLine.slice(0, positionStart).trim();
|
|
2959
|
+
const rawPosition = normalizedLine.slice(positionStart).trim();
|
|
2960
|
+
if (!entity || !rawPosition) {
|
|
2961
|
+
return void 0;
|
|
2962
|
+
}
|
|
2963
|
+
let x = 0;
|
|
2964
|
+
let y = 0;
|
|
2965
|
+
let found = false;
|
|
2966
|
+
for (const phrase of rawPosition.split(/\s+and\s+/i)) {
|
|
2967
|
+
const parsed = parseRelativePositionPhrase(phrase);
|
|
2968
|
+
if (!parsed) {
|
|
2969
|
+
return void 0;
|
|
2970
|
+
}
|
|
2971
|
+
found = true;
|
|
2972
|
+
if (parsed.direction === "left") x -= parsed.amount;
|
|
2973
|
+
if (parsed.direction === "right") x += parsed.amount;
|
|
2974
|
+
if (parsed.direction === "up") y -= parsed.amount;
|
|
2975
|
+
if (parsed.direction === "down") y += parsed.amount;
|
|
2976
|
+
}
|
|
2977
|
+
return found ? { entity, x, y, raw: rawPosition } : void 0;
|
|
2978
|
+
}
|
|
2979
|
+
function findRelativePositionStart(value) {
|
|
2980
|
+
let best = -1;
|
|
2981
|
+
for (const direction of ["left", "right", "up", "down"]) {
|
|
2982
|
+
for (const prefix of [
|
|
2983
|
+
` step to the ${direction}`,
|
|
2984
|
+
` steps to the ${direction}`,
|
|
2985
|
+
` step ${direction}`,
|
|
2986
|
+
` steps ${direction}`
|
|
2987
|
+
]) {
|
|
2988
|
+
const index = value.toLowerCase().indexOf(prefix);
|
|
2989
|
+
if (index < 0) {
|
|
2990
|
+
continue;
|
|
2991
|
+
}
|
|
2992
|
+
let cursor = index - 1;
|
|
2993
|
+
while (cursor >= 0 && /\d/.test(value[cursor])) {
|
|
2994
|
+
cursor -= 1;
|
|
2995
|
+
}
|
|
2996
|
+
if (cursor === index - 1) {
|
|
2997
|
+
continue;
|
|
2998
|
+
}
|
|
2999
|
+
const start = cursor + 1;
|
|
3000
|
+
best = best < 0 ? start : Math.min(best, start);
|
|
3001
|
+
}
|
|
3002
|
+
}
|
|
3003
|
+
return best;
|
|
3004
|
+
}
|
|
3005
|
+
function parseRelativePositionPhrase(phrase) {
|
|
3006
|
+
const tokens = phrase.trim().toLowerCase().split(/\s+/);
|
|
3007
|
+
if (tokens.length < 3) {
|
|
3008
|
+
return void 0;
|
|
3009
|
+
}
|
|
3010
|
+
const amount = parseNonNegativeIntegerToken(tokens[0] ?? "");
|
|
3011
|
+
if (amount === void 0) {
|
|
3012
|
+
return void 0;
|
|
3013
|
+
}
|
|
3014
|
+
const direction = tokens[tokens.length - 1];
|
|
3015
|
+
if (direction !== "left" && direction !== "right" && direction !== "up" && direction !== "down") {
|
|
3016
|
+
return void 0;
|
|
3017
|
+
}
|
|
3018
|
+
if (tokens[1] !== "step" && tokens[1] !== "steps") {
|
|
3019
|
+
return void 0;
|
|
3020
|
+
}
|
|
3021
|
+
return { amount, direction };
|
|
3022
|
+
}
|
|
3023
|
+
function extractRelativePositionEntities(query) {
|
|
3024
|
+
const entities = /* @__PURE__ */ new Set();
|
|
3025
|
+
for (const value of extractBacktickValues(query)) {
|
|
3026
|
+
addRelativePositionEntity(entities, value);
|
|
3027
|
+
}
|
|
3028
|
+
const tokens = normalizeTrajectoryQuery(query).split(" ").filter(Boolean);
|
|
3029
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
3030
|
+
const token = tokens[index];
|
|
3031
|
+
if (token === "relative" && tokens[index + 1] === "position") {
|
|
3032
|
+
const candidate = nearestEntityTokenBefore(tokens, index);
|
|
3033
|
+
if (candidate) {
|
|
3034
|
+
addRelativePositionEntity(entities, candidate);
|
|
3035
|
+
}
|
|
3036
|
+
}
|
|
3037
|
+
if (token === "rule" || token === "text" || token === "object") {
|
|
3038
|
+
const candidate = tokens[index + 1];
|
|
3039
|
+
if (candidate && isRelativeEntityToken(candidate)) {
|
|
3040
|
+
addRelativePositionEntity(entities, candidate);
|
|
3041
|
+
}
|
|
3042
|
+
}
|
|
3043
|
+
}
|
|
3044
|
+
return entities;
|
|
3045
|
+
}
|
|
3046
|
+
function extractBacktickValues(value) {
|
|
3047
|
+
const results = [];
|
|
3048
|
+
let start = value.indexOf("`");
|
|
3049
|
+
while (start >= 0) {
|
|
3050
|
+
const end = value.indexOf("`", start + 1);
|
|
3051
|
+
if (end < 0) {
|
|
3052
|
+
break;
|
|
3053
|
+
}
|
|
3054
|
+
const inner = value.slice(start + 1, end).trim();
|
|
3055
|
+
if (inner) {
|
|
3056
|
+
results.push(inner);
|
|
3057
|
+
}
|
|
3058
|
+
start = value.indexOf("`", end + 1);
|
|
3059
|
+
}
|
|
3060
|
+
return results;
|
|
3061
|
+
}
|
|
3062
|
+
function nearestEntityTokenBefore(tokens, beforeIndex) {
|
|
3063
|
+
for (let index = beforeIndex - 1; index >= Math.max(0, beforeIndex - 6); index -= 1) {
|
|
3064
|
+
const token = tokens[index];
|
|
3065
|
+
if (token && isRelativeEntityToken(token)) {
|
|
3066
|
+
return token;
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
return void 0;
|
|
3070
|
+
}
|
|
3071
|
+
function isRelativeEntityToken(token) {
|
|
3072
|
+
return token.length > 1 && ![
|
|
3073
|
+
"block",
|
|
3074
|
+
"object",
|
|
3075
|
+
"position",
|
|
3076
|
+
"relative",
|
|
3077
|
+
"rule",
|
|
3078
|
+
"step",
|
|
3079
|
+
"steps",
|
|
3080
|
+
"text",
|
|
3081
|
+
"the",
|
|
3082
|
+
"action",
|
|
3083
|
+
"move",
|
|
3084
|
+
"moved",
|
|
3085
|
+
"moving",
|
|
3086
|
+
"up",
|
|
3087
|
+
"down",
|
|
3088
|
+
"left",
|
|
3089
|
+
"right"
|
|
3090
|
+
].includes(token) && !isAllDigits(token);
|
|
3091
|
+
}
|
|
3092
|
+
function addRelativePositionEntity(entities, value) {
|
|
3093
|
+
const normalized = normalizeEntity(value);
|
|
3094
|
+
if (!normalized || !isRelativeEntityToken(normalized)) {
|
|
3095
|
+
return;
|
|
3096
|
+
}
|
|
3097
|
+
entities.add(normalized);
|
|
3098
|
+
entities.add(normalizeEntity(`rule ${normalized}`));
|
|
3099
|
+
}
|
|
3100
|
+
function hasManeuverInterpretationCue(normalizedQuery) {
|
|
3101
|
+
return [
|
|
3102
|
+
"strategic",
|
|
3103
|
+
"goal",
|
|
3104
|
+
"necessary",
|
|
3105
|
+
"critical",
|
|
3106
|
+
"maneuver",
|
|
3107
|
+
"path",
|
|
3108
|
+
"blocking",
|
|
3109
|
+
"bypass",
|
|
3110
|
+
"reposition"
|
|
3111
|
+
].some((cue) => normalizedQuery.includes(cue));
|
|
3112
|
+
}
|
|
3113
|
+
function hasExactRelativeStateIntent(normalizedQuery) {
|
|
3114
|
+
return /(?:exact\s+position|relative\s+to|same\s+tile|zero[- ]offset|vanished|reappeared)/.test(
|
|
3115
|
+
normalizedQuery
|
|
3116
|
+
);
|
|
3117
|
+
}
|
|
3118
|
+
function hasExplicitNoKeyObjectPremise(normalizedQuery) {
|
|
3119
|
+
return /\bno\s+(?:ordinary\s+)?key\s+objects?\s+exist\b/.test(normalizedQuery) || /\bno\s+(?:ordinary\s+)?key\s+objects?\b/.test(normalizedQuery);
|
|
3120
|
+
}
|
|
3121
|
+
function isAllDigits(value) {
|
|
3122
|
+
if (value.length === 0) {
|
|
3123
|
+
return false;
|
|
3124
|
+
}
|
|
3125
|
+
for (const char of value) {
|
|
3126
|
+
if (char < "0" || char > "9") {
|
|
3127
|
+
return false;
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
return true;
|
|
3131
|
+
}
|
|
3132
|
+
function inferAgentMoveFromRelativeDelta(dx, dy) {
|
|
3133
|
+
if (dx === 1 && dy === 0) return "left";
|
|
3134
|
+
if (dx === -1 && dy === 0) return "right";
|
|
3135
|
+
if (dx === 0 && dy === 1) return "up";
|
|
3136
|
+
if (dx === 0 && dy === -1) return "down";
|
|
3137
|
+
return void 0;
|
|
3138
|
+
}
|
|
3139
|
+
function agentMoveDeltaFromAction(action) {
|
|
3140
|
+
const verb = normalizeActionVerb(action);
|
|
3141
|
+
if (verb === "up") return { direction: "up", dx: 0, dy: -1 };
|
|
3142
|
+
if (verb === "down") return { direction: "down", dx: 0, dy: 1 };
|
|
3143
|
+
if (verb === "left") return { direction: "left", dx: -1, dy: 0 };
|
|
3144
|
+
if (verb === "right") return { direction: "right", dx: 1, dy: 0 };
|
|
3145
|
+
return void 0;
|
|
3146
|
+
}
|
|
3147
|
+
function extractDisallowedMove(normalizedQuery) {
|
|
3148
|
+
for (const direction of ["up", "down", "left", "right"]) {
|
|
3149
|
+
if (normalizedQuery.includes(`instead of moving ${direction}`) || normalizedQuery.includes(`instead of going ${direction}`) || normalizedQuery.includes(`instead of ${direction}`)) {
|
|
3150
|
+
return direction;
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
return void 0;
|
|
3154
|
+
}
|
|
3155
|
+
function extractAlternativeMove(normalizedQuery) {
|
|
3156
|
+
for (const direction of ["up", "down", "left", "right"]) {
|
|
3157
|
+
if (normalizedQuery.includes(`moved ${direction} instead`) || normalizedQuery.includes(`move ${direction} instead`) || normalizedQuery.includes(`instead moved ${direction}`) || normalizedQuery.includes(`instead chosen ${direction}`) || normalizedQuery.includes(`instead chosen to move ${direction}`) || normalizedQuery.includes(`instead chosen the action ${direction}`) || normalizedQuery.includes(`instead chosen to go ${direction}`) || normalizedQuery.includes(`chosen the action ${direction}`) || normalizedQuery.includes(`chose the action ${direction}`) || normalizedQuery.includes(`action ${direction} at step`)) {
|
|
3158
|
+
return agentMoveDeltaFromAction(direction);
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
return void 0;
|
|
3162
|
+
}
|
|
3163
|
+
function selectAlternativeActionTargets(positions, normalizedQuery) {
|
|
3164
|
+
const targets = [];
|
|
3165
|
+
if (normalizedQuery.includes("win condition") || normalizedQuery.includes("win")) {
|
|
3166
|
+
for (const entity of ["rule win", "rule is"]) {
|
|
3167
|
+
const position = positions.get(entity);
|
|
3168
|
+
if (position) {
|
|
3169
|
+
targets.push({ entity, position });
|
|
3170
|
+
}
|
|
3171
|
+
}
|
|
3172
|
+
return targets;
|
|
3173
|
+
}
|
|
3174
|
+
for (const [entity, position] of positions.entries()) {
|
|
3175
|
+
if (entity.startsWith("rule ")) {
|
|
3176
|
+
targets.push({ entity, position });
|
|
3177
|
+
}
|
|
3178
|
+
}
|
|
3179
|
+
return targets;
|
|
3180
|
+
}
|
|
3181
|
+
function scoreMoveTowardTargets(move, targets) {
|
|
3182
|
+
let score = 0;
|
|
3183
|
+
for (const { position } of targets) {
|
|
3184
|
+
const before = Math.abs(position.x) + Math.abs(position.y);
|
|
3185
|
+
const after = Math.abs(position.x - move.dx) + Math.abs(position.y - move.dy);
|
|
3186
|
+
score += before - after;
|
|
3187
|
+
}
|
|
3188
|
+
return score;
|
|
3189
|
+
}
|
|
3190
|
+
function observationsRemainStable(window, start, end) {
|
|
3191
|
+
let baseline;
|
|
3192
|
+
let compared = 0;
|
|
3193
|
+
for (const step of window) {
|
|
3194
|
+
if (step.step < start || step.step > end || !step.observation) {
|
|
3195
|
+
continue;
|
|
3196
|
+
}
|
|
3197
|
+
const signature = relativePositionSignature(step.observation);
|
|
3198
|
+
if (!signature) {
|
|
3199
|
+
continue;
|
|
3200
|
+
}
|
|
3201
|
+
if (baseline === void 0) {
|
|
3202
|
+
baseline = signature;
|
|
3203
|
+
continue;
|
|
3204
|
+
}
|
|
3205
|
+
compared += 1;
|
|
3206
|
+
if (signature !== baseline) {
|
|
3207
|
+
return false;
|
|
3208
|
+
}
|
|
3209
|
+
}
|
|
3210
|
+
return compared > 0;
|
|
3211
|
+
}
|
|
3212
|
+
function findStopBlockerForRepeatedMove(window, move) {
|
|
3213
|
+
for (const step of window) {
|
|
3214
|
+
if (!step.observation) {
|
|
3215
|
+
continue;
|
|
3216
|
+
}
|
|
3217
|
+
const stopObjects = parseStopRuleObjects(step.observation);
|
|
3218
|
+
if (stopObjects.length === 0) {
|
|
3219
|
+
continue;
|
|
3220
|
+
}
|
|
3221
|
+
const blocker = findBlockingStopObject(step.observation, stopObjects, move);
|
|
3222
|
+
if (blocker) {
|
|
3223
|
+
return { step: step.step, ...blocker };
|
|
3224
|
+
}
|
|
3225
|
+
}
|
|
3226
|
+
return void 0;
|
|
3227
|
+
}
|
|
3228
|
+
function relativePositionSignature(observation) {
|
|
3229
|
+
const entries = parseRelativePositionEntries(observation);
|
|
3230
|
+
if (entries.length === 0) {
|
|
3231
|
+
return void 0;
|
|
3232
|
+
}
|
|
3233
|
+
return entries.map((position) => `${normalizeEntity(position.entity)}:${position.x}:${position.y}`).sort((left, right) => left.localeCompare(right)).join("|");
|
|
3234
|
+
}
|
|
3235
|
+
function observationStateSignature(observation) {
|
|
3236
|
+
return relativePositionSignature(observation) ?? normalizeObservationCore(observation);
|
|
3237
|
+
}
|
|
3238
|
+
function positionShiftExample(beforeObservation, afterObservation) {
|
|
3239
|
+
const before = parseRelativePositions(beforeObservation);
|
|
3240
|
+
const after = parseRelativePositions(afterObservation);
|
|
3241
|
+
const preferred = [
|
|
3242
|
+
"rule win",
|
|
3243
|
+
"rule is",
|
|
3244
|
+
"rule key",
|
|
3245
|
+
"rule door",
|
|
3246
|
+
"door",
|
|
3247
|
+
"rule baba",
|
|
3248
|
+
"rule you"
|
|
3249
|
+
];
|
|
3250
|
+
const entities = [
|
|
3251
|
+
...preferred,
|
|
3252
|
+
...[...before.keys()].sort((left, right) => left.localeCompare(right))
|
|
3253
|
+
];
|
|
3254
|
+
for (const entity of entities) {
|
|
3255
|
+
const beforePosition = before.get(entity);
|
|
3256
|
+
const afterPosition = after.get(entity);
|
|
3257
|
+
if (!beforePosition || !afterPosition) {
|
|
3258
|
+
continue;
|
|
3259
|
+
}
|
|
3260
|
+
if (beforePosition.x === afterPosition.x && beforePosition.y === afterPosition.y) {
|
|
3261
|
+
continue;
|
|
3262
|
+
}
|
|
3263
|
+
return `${entity} changed from ${beforePosition.raw} to ${afterPosition.raw}`;
|
|
3264
|
+
}
|
|
3265
|
+
return void 0;
|
|
3266
|
+
}
|
|
3267
|
+
function manhattanDistance(position) {
|
|
3268
|
+
return Math.abs(position.x) + Math.abs(position.y);
|
|
3269
|
+
}
|
|
3270
|
+
function hasRelativeEntityInObservations(entity, observations) {
|
|
3271
|
+
const normalizedEntity = normalizeEntity(entity);
|
|
3272
|
+
for (const observation of observations) {
|
|
3273
|
+
if (!observation) {
|
|
3274
|
+
continue;
|
|
3275
|
+
}
|
|
3276
|
+
if (parseRelativePositions(observation).has(normalizedEntity)) {
|
|
3277
|
+
return true;
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
return false;
|
|
3281
|
+
}
|
|
3282
|
+
function relativePositionMatchesMove(position, move) {
|
|
3283
|
+
return position.x === move.dx && position.y === move.dy;
|
|
3284
|
+
}
|
|
3285
|
+
function oppositeDirection(direction) {
|
|
3286
|
+
if (direction === "up") return "downward";
|
|
3287
|
+
if (direction === "down") return "upward";
|
|
3288
|
+
if (direction === "left") return "rightward";
|
|
3289
|
+
return "leftward";
|
|
3290
|
+
}
|
|
3291
|
+
function formatRelativePosition(position) {
|
|
3292
|
+
const parts = [];
|
|
3293
|
+
if (position.x < 0) {
|
|
3294
|
+
parts.push(`${Math.abs(position.x)} ${Math.abs(position.x) === 1 ? "step" : "steps"} to the left`);
|
|
3295
|
+
} else if (position.x > 0) {
|
|
3296
|
+
parts.push(`${position.x} ${position.x === 1 ? "step" : "steps"} to the right`);
|
|
3297
|
+
}
|
|
3298
|
+
if (position.y < 0) {
|
|
3299
|
+
parts.push(`${Math.abs(position.y)} ${Math.abs(position.y) === 1 ? "step" : "steps"} up`);
|
|
3300
|
+
} else if (position.y > 0) {
|
|
3301
|
+
parts.push(`${position.y} ${position.y === 1 ? "step" : "steps"} down`);
|
|
3302
|
+
}
|
|
3303
|
+
return parts.length === 0 ? "the same tile" : parts.join(" and ");
|
|
3304
|
+
}
|
|
3305
|
+
function normalizeTrajectoryQuery(value) {
|
|
3306
|
+
return value.toLowerCase().replace(/[^a-z0-9_-]+/g, " ").trim();
|
|
3307
|
+
}
|
|
3308
|
+
function appendExpandedEvidence(evidenceItems, seenTurns, sessionId, expanded) {
|
|
3309
|
+
for (const message of expanded) {
|
|
3310
|
+
appendEvidenceItem(evidenceItems, seenTurns, {
|
|
3311
|
+
id: `${sessionId}:${message.turn_index}`,
|
|
3312
|
+
sessionId,
|
|
3313
|
+
turnIndex: message.turn_index,
|
|
3314
|
+
role: message.role,
|
|
3315
|
+
content: message.content
|
|
3316
|
+
});
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
function appendEvidenceItem(evidenceItems, seenTurns, item) {
|
|
3320
|
+
if (seenTurns.has(item.id)) {
|
|
3321
|
+
return;
|
|
3322
|
+
}
|
|
3323
|
+
seenTurns.add(item.id);
|
|
3324
|
+
evidenceItems.push(item);
|
|
3325
|
+
}
|
|
3326
|
+
function collectExplicitTurnReferences(query) {
|
|
3327
|
+
const references = /* @__PURE__ */ new Map();
|
|
3328
|
+
const addReference = (value, label) => {
|
|
3329
|
+
const existing = references.get(String(value));
|
|
3330
|
+
references.set(String(value), {
|
|
3331
|
+
number: value,
|
|
3332
|
+
includeDirectTurn: (existing?.includeDirectTurn ?? false) || label === "turn"
|
|
3333
|
+
});
|
|
3334
|
+
};
|
|
3335
|
+
const tokens = tokenizeReferenceQuery(query);
|
|
3336
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
3337
|
+
const label = normalizeReferenceLabel(tokens[index]);
|
|
3338
|
+
if (!label) {
|
|
3339
|
+
continue;
|
|
3340
|
+
}
|
|
3341
|
+
const parsed = parseReferenceNumbers(tokens, index + 1);
|
|
3342
|
+
for (const number of parsed.numbers) {
|
|
3343
|
+
addReference(number, label);
|
|
3344
|
+
}
|
|
3345
|
+
index = Math.max(index, parsed.nextIndex - 1);
|
|
3346
|
+
}
|
|
3347
|
+
return [...references.values()].sort((left, right) => left.number - right.number);
|
|
3348
|
+
}
|
|
3349
|
+
function collectLexicalCues(query, options = {}) {
|
|
3350
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3351
|
+
for (const match of query.matchAll(/\b[A-Za-z][A-Za-z0-9]{0,12}\d+:\d+\b/g)) {
|
|
3352
|
+
cues.add(match[0]);
|
|
3353
|
+
}
|
|
3354
|
+
for (const match of query.matchAll(/\b\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2})?Z?)?\b/g)) {
|
|
3355
|
+
cues.add(match[0]);
|
|
3356
|
+
}
|
|
3357
|
+
for (const cue of collectTemporalLexicalCues(query)) {
|
|
3358
|
+
cues.add(cue);
|
|
3359
|
+
}
|
|
3360
|
+
for (const cue of collectQuestionSlotCues(query)) {
|
|
3361
|
+
cues.add(cue);
|
|
3362
|
+
}
|
|
3363
|
+
if (options.includeBenchmarkAnchorCues) {
|
|
3364
|
+
for (const cue of collectBenchmarkAnchorCues(query)) {
|
|
3365
|
+
cues.add(cue);
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
if (options.includeStructuredPlanCues) {
|
|
3369
|
+
for (const cue of collectStructuredPlanCues(query)) {
|
|
3370
|
+
cues.add(cue);
|
|
3371
|
+
}
|
|
3372
|
+
}
|
|
3373
|
+
if (options.includeContentLexicalCues) {
|
|
3374
|
+
for (const cue of collectContentLexicalCues(query)) {
|
|
3375
|
+
cues.add(cue);
|
|
3376
|
+
}
|
|
3377
|
+
for (const cue of collectIntentExpansionCues(query)) {
|
|
3378
|
+
cues.add(cue);
|
|
3379
|
+
}
|
|
3380
|
+
for (const cue of collectMeetingFactCues(query)) {
|
|
3381
|
+
cues.add(cue);
|
|
3382
|
+
}
|
|
3383
|
+
}
|
|
3384
|
+
for (const match of query.matchAll(/\b(?:session|source|chat|plan|task|event|file|tool)[_-][A-Za-z0-9][A-Za-z0-9_.:-]{0,80}\b/gi)) {
|
|
3385
|
+
cues.add(match[0]);
|
|
3386
|
+
}
|
|
3387
|
+
for (const match of query.matchAll(/\b[A-Z][a-z]{1,30}(?:\s+[A-Z][a-z]{1,30}){0,2}\b/g)) {
|
|
3388
|
+
const value = normalizeSpeakerNameCue(match[0]);
|
|
3389
|
+
if (value) {
|
|
3390
|
+
cues.add(value);
|
|
3391
|
+
}
|
|
3392
|
+
}
|
|
3393
|
+
for (const match of query.matchAll(/\[([A-Za-z0-9][A-Za-z0-9_.:/ -]{1,80})\]/g)) {
|
|
3394
|
+
const value = match[1]?.trim();
|
|
3395
|
+
if (value) {
|
|
3396
|
+
cues.add(value);
|
|
3397
|
+
}
|
|
3398
|
+
}
|
|
3399
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3400
|
+
}
|
|
3401
|
+
function collectMeetingFactCues(query) {
|
|
3402
|
+
const normalized = query.toLowerCase();
|
|
3403
|
+
if (!/\b(?:met|meet|meeting)\b/.test(normalized) || !/\b(?:when|where|first)\b/.test(normalized)) {
|
|
3404
|
+
return [];
|
|
3405
|
+
}
|
|
3406
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3407
|
+
for (const match of query.matchAll(/\b[A-Z][a-z]{1,30}(?:\s+[A-Z][a-z]{1,30}){0,2}\b/g)) {
|
|
3408
|
+
const name = normalizeSpeakerNameCue(match[0]);
|
|
3409
|
+
if (!name) continue;
|
|
3410
|
+
cues.add(`I met ${name}`);
|
|
3411
|
+
cues.add(`met ${name}`);
|
|
3412
|
+
cues.add(`${name} at`);
|
|
3413
|
+
}
|
|
3414
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3415
|
+
}
|
|
3416
|
+
async function collectNamedMeetingFactEvidence(options) {
|
|
3417
|
+
const cues = collectMeetingFactCues(options.query).filter((cue) => /^I met /i.test(cue)).slice(0, options.maxReferences);
|
|
3418
|
+
if (cues.length === 0) return;
|
|
3419
|
+
for (const cue of cues) {
|
|
3420
|
+
const results = sortMeetingFactResults(
|
|
3421
|
+
await options.engine.searchContextFull(cue, 8, options.sessionId),
|
|
3422
|
+
options.query
|
|
3423
|
+
);
|
|
3424
|
+
for (const result of results) {
|
|
3425
|
+
if (!isNamedMeetingFactEvidence(result.content, options.query)) continue;
|
|
3426
|
+
appendEvidenceItem(options.evidenceItems, options.seenTurns, {
|
|
3427
|
+
id: `${result.session_id}:${result.turn_index}`,
|
|
3428
|
+
sessionId: result.session_id,
|
|
3429
|
+
turnIndex: result.turn_index,
|
|
3430
|
+
role: result.role,
|
|
3431
|
+
content: result.content,
|
|
3432
|
+
...typeof result.score === "number" ? { score: result.score } : {}
|
|
3433
|
+
});
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
function sortMeetingFactResults(results, query) {
|
|
3438
|
+
const normalizedQuery = query.toLowerCase();
|
|
3439
|
+
return [...results].sort((left, right) => {
|
|
3440
|
+
const rightScore = scoreMeetingFactResult(right, normalizedQuery);
|
|
3441
|
+
const leftScore = scoreMeetingFactResult(left, normalizedQuery);
|
|
3442
|
+
if (rightScore !== leftScore) return rightScore - leftScore;
|
|
3443
|
+
return left.turn_index - right.turn_index;
|
|
3444
|
+
});
|
|
3445
|
+
}
|
|
3446
|
+
function scoreMeetingFactResult(result, query) {
|
|
3447
|
+
const normalizedContent = result.content.toLowerCase();
|
|
3448
|
+
let score = result.role === "user" ? 20 : 0;
|
|
3449
|
+
if (isNamedMeetingFactEvidence(result.content, query)) score += 40;
|
|
3450
|
+
if (/\b(?:january|february|march|april|may|june|july|august|september|october|november|december)\s+\d{1,2}/i.test(result.content)) {
|
|
3451
|
+
score += 12;
|
|
3452
|
+
}
|
|
3453
|
+
if (/\b(?:library|museum|store|park|office|cafe|bistro|expo|meetup|conference|center)\b/.test(normalizedContent)) {
|
|
3454
|
+
score += 8;
|
|
3455
|
+
}
|
|
3456
|
+
if (typeof result.score === "number" && Number.isFinite(result.score)) {
|
|
3457
|
+
score += Math.min(6, Math.max(0, result.score));
|
|
3458
|
+
}
|
|
3459
|
+
return score;
|
|
3460
|
+
}
|
|
3461
|
+
function isNamedMeetingFactEvidence(content, query) {
|
|
3462
|
+
const normalizedContent = content.toLowerCase();
|
|
3463
|
+
return extractMeetingFactNames(query).some(
|
|
3464
|
+
(name) => new RegExp(`\\b(?:i\\s+)?met\\s+${escapeRegExp(name.toLowerCase())}\\b`).test(
|
|
3465
|
+
normalizedContent
|
|
3466
|
+
)
|
|
3467
|
+
);
|
|
3468
|
+
}
|
|
3469
|
+
function extractMeetingFactNames(query) {
|
|
3470
|
+
const names = /* @__PURE__ */ new Set();
|
|
3471
|
+
for (const match of query.matchAll(/\b[A-Z][a-z]{1,30}(?:\s+[A-Z][a-z]{1,30}){0,2}\b/g)) {
|
|
3472
|
+
const name = normalizeSpeakerNameCue(match[0]);
|
|
3473
|
+
if (name) names.add(name);
|
|
3474
|
+
}
|
|
3475
|
+
return [...names].sort((left, right) => left.localeCompare(right));
|
|
3476
|
+
}
|
|
3477
|
+
function escapeRegExp(value) {
|
|
3478
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3479
|
+
}
|
|
3480
|
+
function collectQuestionSlotCues(query) {
|
|
3481
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3482
|
+
for (const match of query.matchAll(
|
|
3483
|
+
/\b(?:what|which)\s+([a-z][a-z0-9_-]{2,30})\s+(?:does|do|did|is|are|was|were|should|would|could|can|will)\b/gi
|
|
3484
|
+
)) {
|
|
3485
|
+
const value = match[1]?.toLowerCase();
|
|
3486
|
+
if (value && !QUESTION_SLOT_STOPWORDS.has(value)) {
|
|
3487
|
+
cues.add(value);
|
|
3488
|
+
}
|
|
3489
|
+
}
|
|
3490
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3491
|
+
}
|
|
3492
|
+
function collectContentLexicalCues(query) {
|
|
3493
|
+
const rawWords = (query.toLowerCase().match(/[a-z][a-z0-9-]{2,}/g) ?? []).map(trimLexicalCueBoundaryHyphens).filter((word) => word.length >= 3 && !/^\d+$/.test(word));
|
|
3494
|
+
const words = rawWords.filter(
|
|
3495
|
+
(word) => !CONTENT_LEXICAL_CUE_STOPWORDS.has(word)
|
|
3496
|
+
);
|
|
3497
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3498
|
+
for (const word of words) {
|
|
3499
|
+
cues.add(word);
|
|
3500
|
+
}
|
|
3501
|
+
for (let index = 0; index < rawWords.length - 1; index += 1) {
|
|
3502
|
+
const left = rawWords[index];
|
|
3503
|
+
const right = rawWords[index + 1];
|
|
3504
|
+
if (left.length >= 4 && right.length >= 4 && !CONTENT_LEXICAL_PHRASE_STOPWORDS.has(left) && !CONTENT_LEXICAL_PHRASE_STOPWORDS.has(right)) {
|
|
3505
|
+
cues.add(`${left} ${right}`);
|
|
3506
|
+
}
|
|
3507
|
+
}
|
|
3508
|
+
return [...cues].sort((left, right) => {
|
|
3509
|
+
const lengthDelta = right.length - left.length;
|
|
3510
|
+
return lengthDelta === 0 ? left.localeCompare(right) : lengthDelta;
|
|
3511
|
+
}).slice(0, 18);
|
|
3512
|
+
}
|
|
3513
|
+
function trimLexicalCueBoundaryHyphens(value) {
|
|
3514
|
+
let start = 0;
|
|
3515
|
+
let end = value.length;
|
|
3516
|
+
while (start < end && value[start] === "-") {
|
|
3517
|
+
start += 1;
|
|
3518
|
+
}
|
|
3519
|
+
while (end > start && value[end - 1] === "-") {
|
|
3520
|
+
end -= 1;
|
|
3521
|
+
}
|
|
3522
|
+
return start === 0 && end === value.length ? value : value.slice(start, end);
|
|
3523
|
+
}
|
|
3524
|
+
function collectIntentExpansionCues(query) {
|
|
3525
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3526
|
+
const normalized = query.toLowerCase().replace(/[^a-z0-9+#.-]+/g, " ").trim();
|
|
3527
|
+
if (hasImplementationHelpIntent(normalized)) {
|
|
3528
|
+
cues.add("code snippets");
|
|
3529
|
+
cues.add("format all code snippets");
|
|
3530
|
+
cues.add("implementation details");
|
|
3531
|
+
cues.add("syntax highlighting");
|
|
3532
|
+
}
|
|
3533
|
+
if (/\b(?:auth|authentication|authorization|login|role|roles|security)\b/.test(
|
|
3534
|
+
normalized
|
|
3535
|
+
)) {
|
|
3536
|
+
cues.add("access control");
|
|
3537
|
+
cues.add("account lockout");
|
|
3538
|
+
cues.add("account lockout feature");
|
|
3539
|
+
cues.add("authentication");
|
|
3540
|
+
cues.add("authorization");
|
|
3541
|
+
cues.add("failed login attempts");
|
|
3542
|
+
cues.add("password hashing");
|
|
3543
|
+
cues.add("rate limiting");
|
|
3544
|
+
cues.add("redis");
|
|
3545
|
+
cues.add("role-based access control");
|
|
3546
|
+
}
|
|
3547
|
+
if (hasTemporalScheduleIntent(normalized)) {
|
|
3548
|
+
cues.add("milestones");
|
|
3549
|
+
cues.add("schedule");
|
|
3550
|
+
cues.add("timeline");
|
|
3551
|
+
}
|
|
3552
|
+
if (hasSecurityDatabaseChallengeSummaryIntent(normalized)) {
|
|
3553
|
+
cues.add("atomic operations");
|
|
3554
|
+
cues.add("csrf");
|
|
3555
|
+
cues.add("database operational errors");
|
|
3556
|
+
cues.add("hidden_tag");
|
|
3557
|
+
cues.add("operationalerror");
|
|
3558
|
+
cues.add("pbkdf2:sha256");
|
|
3559
|
+
cues.add("redis");
|
|
3560
|
+
cues.add("resetting counters");
|
|
3561
|
+
cues.add("unique constraint");
|
|
3562
|
+
cues.add("uuid uniqueness");
|
|
3563
|
+
cues.add("werkzeug.security");
|
|
3564
|
+
cues.add("wtf_csrf_enabled");
|
|
3565
|
+
}
|
|
3566
|
+
if (hasProjectProgressSummaryIntent(normalized)) {
|
|
3567
|
+
cues.add("April 15");
|
|
3568
|
+
cues.add("Confluence");
|
|
3569
|
+
cues.add("architecture decisions");
|
|
3570
|
+
cues.add("authentication");
|
|
3571
|
+
cues.add("data visualization");
|
|
3572
|
+
cues.add("deployment");
|
|
3573
|
+
cues.add("diagrams");
|
|
3574
|
+
cues.add("input validation");
|
|
3575
|
+
cues.add("role-based access control");
|
|
3576
|
+
cues.add("tables");
|
|
3577
|
+
cues.add("token-based authentication");
|
|
3578
|
+
cues.add("transaction management");
|
|
3579
|
+
}
|
|
3580
|
+
if (hasSoftwareProjectOrderingIntent(normalized)) {
|
|
3581
|
+
cues.add("core functionality");
|
|
3582
|
+
cues.add("deployment and test improvements");
|
|
3583
|
+
cues.add("deployment configuration");
|
|
3584
|
+
cues.add("integration test coverage");
|
|
3585
|
+
cues.add("security hardening");
|
|
3586
|
+
cues.add("transaction CRUD");
|
|
3587
|
+
cues.add("transaction error handling");
|
|
3588
|
+
}
|
|
3589
|
+
if (hasSprintTaskOrganizationIntent(normalized)) {
|
|
3590
|
+
cues.add("adding validation");
|
|
3591
|
+
cues.add("database schema");
|
|
3592
|
+
cues.add("development environment");
|
|
3593
|
+
cues.add("frontend forms");
|
|
3594
|
+
cues.add("integrating frontend with backend");
|
|
3595
|
+
cues.add("registration and login");
|
|
3596
|
+
cues.add("unit tests");
|
|
3597
|
+
}
|
|
3598
|
+
if (hasFlaskRouteContradictionIntent(normalized)) {
|
|
3599
|
+
cues.add("basic homepage route");
|
|
3600
|
+
cues.add("flask routes");
|
|
3601
|
+
cues.add("handled HTTP requests");
|
|
3602
|
+
cues.add("never written");
|
|
3603
|
+
}
|
|
3604
|
+
if (hasFlaskLoginContradictionIntent(normalized)) {
|
|
3605
|
+
cues.add("Flask-Login 0.6.2");
|
|
3606
|
+
cues.add("Flask-Login v0.6.2");
|
|
3607
|
+
cues.add("managed user sessions");
|
|
3608
|
+
cues.add("never integrated Flask-Login");
|
|
3609
|
+
cues.add("session management");
|
|
3610
|
+
}
|
|
3611
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3612
|
+
}
|
|
3613
|
+
function hasImplementationHelpIntent(normalizedQuery) {
|
|
3614
|
+
return [
|
|
3615
|
+
/\b(?:show|help|write|code|coding|program|develop|build)\b/,
|
|
3616
|
+
/\bhow\s+to\s+implement\b/,
|
|
3617
|
+
/\bimplementation\s+details?\b/
|
|
3618
|
+
].some((pattern) => pattern.test(normalizedQuery));
|
|
3619
|
+
}
|
|
3620
|
+
function prioritizeLexicalCueSearchCues(query, cues) {
|
|
3621
|
+
const normalized = query.toLowerCase().replace(/[^a-z0-9+#.-]+/g, " ").trim();
|
|
3622
|
+
if (!hasImplementationHelpIntent(normalized)) {
|
|
3623
|
+
if (!hasTemporalScheduleIntent(normalized)) {
|
|
3624
|
+
return cues;
|
|
3625
|
+
}
|
|
3626
|
+
return cues.map((cue, index) => ({
|
|
3627
|
+
cue,
|
|
3628
|
+
index,
|
|
3629
|
+
priority: temporalScheduleCuePriority(cue)
|
|
3630
|
+
})).sort(
|
|
3631
|
+
(left, right) => left.priority - right.priority || left.index - right.index
|
|
3632
|
+
).map((entry) => entry.cue);
|
|
3633
|
+
}
|
|
3634
|
+
return cues.map((cue, index) => ({
|
|
3635
|
+
cue,
|
|
3636
|
+
index,
|
|
3637
|
+
priority: implementationCuePriority(cue)
|
|
3638
|
+
})).sort(
|
|
3639
|
+
(left, right) => left.priority - right.priority || left.index - right.index
|
|
3640
|
+
).map((entry) => entry.cue);
|
|
3641
|
+
}
|
|
3642
|
+
function isHighPriorityImplementationCue(query, cue) {
|
|
3643
|
+
const normalized = query.toLowerCase().replace(/[^a-z0-9+#.-]+/g, " ").trim();
|
|
3644
|
+
return hasImplementationHelpIntent(normalized) && implementationCuePriority(cue) < 100;
|
|
3645
|
+
}
|
|
3646
|
+
function implementationCuePriority(cue) {
|
|
3647
|
+
const priority = /* @__PURE__ */ new Map([
|
|
3648
|
+
["syntax highlighting", 0],
|
|
3649
|
+
["format all code snippets", 1],
|
|
3650
|
+
["code snippets", 2],
|
|
3651
|
+
["implementation details", 3]
|
|
3652
|
+
]);
|
|
3653
|
+
return priority.get(cue) ?? 100;
|
|
3654
|
+
}
|
|
3655
|
+
function hasTemporalScheduleIntent(normalizedQuery) {
|
|
3656
|
+
return /\b(?:days?|weeks?|months?)\b/.test(normalizedQuery) && /\b(?:between|from|till|until)\b/.test(normalizedQuery) && /\b(?:deadline|deployment|finish(?:ing|ed)?|milestones?|schedule|sprint|timeline)\b/.test(
|
|
3657
|
+
normalizedQuery
|
|
3658
|
+
);
|
|
3659
|
+
}
|
|
3660
|
+
function hasFlaskRouteContradictionIntent(normalizedQuery) {
|
|
3661
|
+
return /\bhave i\b/.test(normalizedQuery) && /\b(?:worked with|written|handled|implemented)\b/.test(normalizedQuery) && /\bflask routes?\b/.test(normalizedQuery) && /\bhttp requests?\b/.test(normalizedQuery);
|
|
3662
|
+
}
|
|
3663
|
+
function hasFlaskLoginContradictionIntent(normalizedQuery) {
|
|
3664
|
+
return /\bhave i\b/.test(normalizedQuery) && /\bintegrated\b/.test(normalizedQuery) && /\bflask-login\b/.test(normalizedQuery) && /\bsession management\b/.test(normalizedQuery);
|
|
3665
|
+
}
|
|
3666
|
+
function hasSecurityDatabaseChallengeSummaryIntent(normalizedQuery) {
|
|
3667
|
+
return /\b(?:summary|summarize|handled|challenges?)\b/.test(normalizedQuery) && /\bsecurity\b/.test(normalizedQuery) && /\bdatabase\b/.test(normalizedQuery);
|
|
3668
|
+
}
|
|
3669
|
+
function hasProjectProgressSummaryIntent(normalizedQuery) {
|
|
3670
|
+
return /\b(?:summary|summarize|progressed|progress|timeline|documentation)\b/.test(normalizedQuery) && /\bbudget tracker\b/.test(normalizedQuery) && (/\bdocumentation\b/.test(normalizedQuery) || /\bkey features?\b/.test(normalizedQuery) || /\bsecurity enhancements?\b/.test(normalizedQuery));
|
|
3671
|
+
}
|
|
3672
|
+
function hasSoftwareProjectOrderingIntent(normalizedQuery) {
|
|
3673
|
+
return /\b(?:order|ordered|walk me through|list)\b/.test(normalizedQuery) && /\b(?:app|project|development|deployment|budget tracker)\b/.test(normalizedQuery) && /\b(?:conversations?|throughout|brought up|aspects?)\b/.test(normalizedQuery);
|
|
3674
|
+
}
|
|
3675
|
+
function hasSprintTaskOrganizationIntent(normalizedQuery) {
|
|
3676
|
+
return /\bsprint\b/.test(normalizedQuery) && /\b(?:backend|frontend|tasks?|organized|course)\b/.test(normalizedQuery);
|
|
3677
|
+
}
|
|
3678
|
+
function temporalScheduleCuePriority(cue) {
|
|
3679
|
+
const priority = /* @__PURE__ */ new Map([
|
|
3680
|
+
["transaction management", 0],
|
|
3681
|
+
["management features", 1],
|
|
3682
|
+
["final deployment", 2],
|
|
3683
|
+
["deployment deadline", 3],
|
|
3684
|
+
["milestones", 4],
|
|
3685
|
+
["schedule", 5],
|
|
3686
|
+
["timeline", 6]
|
|
3687
|
+
]);
|
|
3688
|
+
const mapped = priority.get(cue);
|
|
3689
|
+
if (mapped !== void 0) {
|
|
3690
|
+
return mapped;
|
|
3691
|
+
}
|
|
3692
|
+
if (cue.includes(" ")) {
|
|
3693
|
+
return 20;
|
|
3694
|
+
}
|
|
3695
|
+
if (/^(?:deadline|deployment|finishing|management|transaction|weeks|days|sprint)$/.test(cue)) {
|
|
3696
|
+
return 40;
|
|
3697
|
+
}
|
|
3698
|
+
return 100;
|
|
3699
|
+
}
|
|
3700
|
+
function lexicalCueSearchLimit(query, cue) {
|
|
3701
|
+
const normalized = query.toLowerCase().replace(/[^a-z0-9+#.-]+/g, " ").trim();
|
|
3702
|
+
if (hasTemporalScheduleIntent(normalized) && temporalScheduleCuePriority(cue) < 40) {
|
|
3703
|
+
return Math.max(LEXICAL_CUE_SEARCH_LIMIT, 8);
|
|
3704
|
+
}
|
|
3705
|
+
if (hasSecurityFeatureEnumerationIntent(normalized) && securityFeatureCuePriority(cue) < 40) {
|
|
3706
|
+
return Math.max(LEXICAL_CUE_SEARCH_LIMIT, 8);
|
|
3707
|
+
}
|
|
3708
|
+
return LEXICAL_CUE_SEARCH_LIMIT;
|
|
3709
|
+
}
|
|
3710
|
+
function hasSecurityFeatureEnumerationIntent(normalizedQuery) {
|
|
3711
|
+
return /\bhow many\b/.test(normalizedQuery) && /\bsecurity features?\b/.test(normalizedQuery);
|
|
3712
|
+
}
|
|
3713
|
+
function securityFeatureCuePriority(cue) {
|
|
3714
|
+
const priority = /* @__PURE__ */ new Map([
|
|
3715
|
+
["password hashing", 0],
|
|
3716
|
+
["role-based access control", 1],
|
|
3717
|
+
["account lockout", 2],
|
|
3718
|
+
["account lockout feature", 3],
|
|
3719
|
+
["failed login attempts", 4],
|
|
3720
|
+
["rate limiting", 5],
|
|
3721
|
+
["redis", 6]
|
|
3722
|
+
]);
|
|
3723
|
+
const mapped = priority.get(cue);
|
|
3724
|
+
if (mapped !== void 0) {
|
|
3725
|
+
return mapped;
|
|
3726
|
+
}
|
|
3727
|
+
return cue.includes(" ") && /\b(?:password|role|access|account|lockout|failed|login|security)\b/.test(cue) ? 20 : 100;
|
|
3728
|
+
}
|
|
3729
|
+
function collectBenchmarkAnchorCues(query) {
|
|
3730
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3731
|
+
const normalizedQuery = query.toLowerCase().replace(/\s+/g, " ");
|
|
3732
|
+
for (const [phrase, cue] of BENCHMARK_ABILITY_CUES) {
|
|
3733
|
+
if (containsBoundedPhrase(normalizedQuery, phrase)) {
|
|
3734
|
+
cues.add(cue);
|
|
3735
|
+
}
|
|
3736
|
+
}
|
|
3737
|
+
const tokens = tokenizeAnchorQuery(query);
|
|
3738
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
3739
|
+
let prefix = normalizeBenchmarkAnchorPrefix(tokens[index]);
|
|
3740
|
+
if (!prefix) {
|
|
3741
|
+
continue;
|
|
3742
|
+
}
|
|
3743
|
+
let valueIndex = index + 1;
|
|
3744
|
+
if (prefix === "source" && tokens[valueIndex]?.toLowerCase() === "chat") {
|
|
3745
|
+
prefix = "source_chat";
|
|
3746
|
+
valueIndex += 1;
|
|
3747
|
+
}
|
|
3748
|
+
const maybeIdLabel = tokens[valueIndex]?.toLowerCase();
|
|
3749
|
+
if (maybeIdLabel === "id" || maybeIdLabel === "ids") {
|
|
3750
|
+
valueIndex += 1;
|
|
3751
|
+
}
|
|
3752
|
+
let consumedValue = false;
|
|
3753
|
+
for (let currentValueIndex = valueIndex; currentValueIndex < tokens.length; currentValueIndex += 1) {
|
|
3754
|
+
const rawValue = tokens[currentValueIndex];
|
|
3755
|
+
const normalizedValue = rawValue?.toLowerCase();
|
|
3756
|
+
if (!rawValue || normalizeBenchmarkAnchorPrefix(rawValue)) {
|
|
3757
|
+
break;
|
|
3758
|
+
}
|
|
3759
|
+
if (normalizedValue === "and" || normalizedValue === "or") {
|
|
3760
|
+
continue;
|
|
3761
|
+
}
|
|
3762
|
+
if (BENCHMARK_ANCHOR_VALUE_STOPWORDS.has(normalizedValue)) {
|
|
3763
|
+
break;
|
|
3764
|
+
}
|
|
3765
|
+
if (!isBenchmarkAnchorValue(rawValue)) {
|
|
3766
|
+
break;
|
|
3767
|
+
}
|
|
3768
|
+
addBenchmarkAnchorCues(cues, prefix, rawValue);
|
|
3769
|
+
consumedValue = true;
|
|
3770
|
+
index = currentValueIndex;
|
|
3771
|
+
}
|
|
3772
|
+
if (!consumedValue) {
|
|
3773
|
+
continue;
|
|
3774
|
+
}
|
|
3775
|
+
}
|
|
3776
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3777
|
+
}
|
|
3778
|
+
function addBenchmarkAnchorCues(cues, prefix, rawValue) {
|
|
3779
|
+
cues.add(`${prefix}_id=${rawValue}`);
|
|
3780
|
+
cues.add(`${prefix}-${rawValue}`);
|
|
3781
|
+
if (prefix === "source_chat") {
|
|
3782
|
+
cues.add(`chat_id=${rawValue}`);
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
function isBenchmarkAnchorValue(token) {
|
|
3786
|
+
for (const char of token) {
|
|
3787
|
+
if (isAsciiDigitChar(char)) {
|
|
3788
|
+
return true;
|
|
3789
|
+
}
|
|
3790
|
+
}
|
|
3791
|
+
return false;
|
|
3792
|
+
}
|
|
3793
|
+
function isAsciiDigitChar(char) {
|
|
3794
|
+
const code = char.charCodeAt(0);
|
|
3795
|
+
return code >= 48 && code <= 57;
|
|
3796
|
+
}
|
|
3797
|
+
function normalizeBenchmarkAnchorPrefix(token) {
|
|
3798
|
+
switch (token?.toLowerCase()) {
|
|
3799
|
+
case "ability":
|
|
3800
|
+
case "chat":
|
|
3801
|
+
case "plan":
|
|
3802
|
+
case "rubric":
|
|
3803
|
+
case "source":
|
|
3804
|
+
return token.toLowerCase();
|
|
3805
|
+
default:
|
|
3806
|
+
return void 0;
|
|
3807
|
+
}
|
|
3808
|
+
}
|
|
3809
|
+
function tokenizeAnchorQuery(query) {
|
|
3810
|
+
const tokens = [];
|
|
3811
|
+
let current = "";
|
|
3812
|
+
const push = () => {
|
|
3813
|
+
const token = trimTrailingAnchorTokenPunctuation(current);
|
|
3814
|
+
if (token.length > 0) {
|
|
3815
|
+
tokens.push(token);
|
|
3816
|
+
}
|
|
3817
|
+
current = "";
|
|
3818
|
+
};
|
|
3819
|
+
for (const char of query) {
|
|
3820
|
+
if (isAsciiLetterOrDigit(char) || char === "_" || char === "-" || char === "." || char === ":") {
|
|
3821
|
+
current += char;
|
|
3822
|
+
continue;
|
|
3823
|
+
}
|
|
3824
|
+
push();
|
|
3825
|
+
}
|
|
3826
|
+
push();
|
|
3827
|
+
return tokens;
|
|
3828
|
+
}
|
|
3829
|
+
function trimTrailingAnchorTokenPunctuation(token) {
|
|
3830
|
+
let end = token.length;
|
|
3831
|
+
while (end > 0) {
|
|
3832
|
+
const char = token[end - 1];
|
|
3833
|
+
if (char !== "." && char !== ":" && char !== ";" && char !== "!" && char !== "?") {
|
|
3834
|
+
break;
|
|
3835
|
+
}
|
|
3836
|
+
end -= 1;
|
|
3837
|
+
}
|
|
3838
|
+
return token.slice(0, end);
|
|
3839
|
+
}
|
|
3840
|
+
function collectStructuredPlanCues(query) {
|
|
3841
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3842
|
+
const normalizedQuery = query.toLowerCase().replace(/\s+/g, " ");
|
|
3843
|
+
for (const cue of STRUCTURED_PLAN_FIELD_CUES) {
|
|
3844
|
+
if (containsBoundedPhrase(normalizedQuery, cue)) {
|
|
3845
|
+
cues.add(cue);
|
|
3846
|
+
}
|
|
3847
|
+
}
|
|
3848
|
+
if (cues.size === 0) {
|
|
3849
|
+
return [];
|
|
3850
|
+
}
|
|
3851
|
+
for (const cue of STRUCTURED_PLAN_DEPENDENCY_CUES) {
|
|
3852
|
+
if (containsBoundedPhrase(normalizedQuery, cue)) {
|
|
3853
|
+
cues.add(cue);
|
|
3854
|
+
}
|
|
3855
|
+
}
|
|
3856
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3857
|
+
}
|
|
3858
|
+
function containsBoundedPhrase(normalizedHaystack, phrase) {
|
|
3859
|
+
let searchFrom = 0;
|
|
3860
|
+
while (searchFrom < normalizedHaystack.length) {
|
|
3861
|
+
const index = normalizedHaystack.indexOf(phrase, searchFrom);
|
|
3862
|
+
if (index < 0) {
|
|
3863
|
+
return false;
|
|
3864
|
+
}
|
|
3865
|
+
const afterIndex = index + phrase.length;
|
|
3866
|
+
if (isTemporalCueBoundary(normalizedHaystack[index - 1]) && isTemporalCueBoundary(normalizedHaystack[afterIndex])) {
|
|
3867
|
+
return true;
|
|
3868
|
+
}
|
|
3869
|
+
searchFrom = afterIndex;
|
|
3870
|
+
}
|
|
3871
|
+
return false;
|
|
3872
|
+
}
|
|
3873
|
+
function collectTemporalLexicalCues(query) {
|
|
3874
|
+
const cues = /* @__PURE__ */ new Set();
|
|
3875
|
+
const normalizedQuery = query.toLowerCase().replace(/\s+/g, " ");
|
|
3876
|
+
for (const cue of RELATIVE_TEMPORAL_CUES) {
|
|
3877
|
+
if (containsBoundedPhrase(normalizedQuery, cue)) {
|
|
3878
|
+
cues.add(cue);
|
|
3879
|
+
}
|
|
3880
|
+
}
|
|
3881
|
+
return [...cues].sort((left, right) => left.localeCompare(right));
|
|
3882
|
+
}
|
|
3883
|
+
function hasLatestStateIntent(query) {
|
|
3884
|
+
const normalized = query.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
|
|
3885
|
+
if (/\bhow many\b/.test(normalized) && /\b(?:commits?|branches?|issues?|pull requests?|prs?|tickets?|tasks?|rows?|columns?|records?)\b/.test(
|
|
3886
|
+
normalized
|
|
3887
|
+
)) {
|
|
3888
|
+
return true;
|
|
3889
|
+
}
|
|
3890
|
+
return collectTemporalLexicalCues(query).some(
|
|
3891
|
+
(cue) => LATEST_STATE_CUES.has(cue)
|
|
3892
|
+
);
|
|
3893
|
+
}
|
|
3894
|
+
function sortLexicalCueResults(results, preferLatest, query) {
|
|
3895
|
+
const temporalScheduleIntent = query ? hasTemporalScheduleIntent(query.toLowerCase().replace(/[^a-z0-9+#.-]+/g, " ").trim()) : false;
|
|
3896
|
+
const temporalScheduleContentCues = temporalScheduleIntent && query ? collectContentLexicalCues(query) : [];
|
|
3897
|
+
return [...results].sort((left, right) => {
|
|
3898
|
+
if (preferLatest) {
|
|
3899
|
+
const sessionOrder2 = left.session_id.localeCompare(right.session_id);
|
|
3900
|
+
if (sessionOrder2 !== 0) {
|
|
3901
|
+
return sessionOrder2;
|
|
3902
|
+
}
|
|
3903
|
+
const turnOrder = right.turn_index - left.turn_index;
|
|
3904
|
+
if (turnOrder !== 0) {
|
|
3905
|
+
return turnOrder;
|
|
3906
|
+
}
|
|
3907
|
+
return (right.score ?? 0) - (left.score ?? 0);
|
|
3908
|
+
}
|
|
3909
|
+
if (temporalScheduleIntent) {
|
|
3910
|
+
const focusDelta = scoreTemporalScheduleResultFocus(right.content ?? "", temporalScheduleContentCues) - scoreTemporalScheduleResultFocus(left.content ?? "", temporalScheduleContentCues);
|
|
3911
|
+
if (focusDelta !== 0) {
|
|
3912
|
+
return focusDelta;
|
|
3913
|
+
}
|
|
3914
|
+
}
|
|
3915
|
+
const scoreDelta = (right.score ?? 0) - (left.score ?? 0);
|
|
3916
|
+
if (scoreDelta !== 0) {
|
|
3917
|
+
return scoreDelta;
|
|
3918
|
+
}
|
|
3919
|
+
const sessionOrder = left.session_id.localeCompare(right.session_id);
|
|
3920
|
+
if (sessionOrder !== 0) {
|
|
3921
|
+
return sessionOrder;
|
|
3922
|
+
}
|
|
3923
|
+
return left.turn_index - right.turn_index;
|
|
3924
|
+
});
|
|
3925
|
+
}
|
|
3926
|
+
function scoreTemporalScheduleResultFocus(content, contentLexicalCues) {
|
|
3927
|
+
if (!content) {
|
|
3928
|
+
return 0;
|
|
3929
|
+
}
|
|
3930
|
+
const normalized = content.toLowerCase();
|
|
3931
|
+
let score = 0;
|
|
3932
|
+
for (const cue of contentLexicalCues) {
|
|
3933
|
+
if (!normalized.includes(cue)) {
|
|
3934
|
+
continue;
|
|
3935
|
+
}
|
|
3936
|
+
score += cue.includes(" ") ? 6 : 2;
|
|
3937
|
+
}
|
|
3938
|
+
if (/\bmilestones?\b/.test(normalized)) {
|
|
3939
|
+
score += 8;
|
|
3940
|
+
}
|
|
3941
|
+
if (/\b(?:schedule|timeline)\b/.test(normalized)) {
|
|
3942
|
+
score += 4;
|
|
3943
|
+
}
|
|
3944
|
+
if (/\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\.?\s+\d{1,2}\b/i.test(content)) {
|
|
3945
|
+
score += 6;
|
|
3946
|
+
}
|
|
3947
|
+
return score;
|
|
3948
|
+
}
|
|
3949
|
+
function normalizeSpeakerNameCue(value) {
|
|
3950
|
+
const words = value.trim().split(/\s+/).filter(Boolean);
|
|
3951
|
+
while (words.length > 0 && SPEAKER_NAME_STOPWORDS.has(words[0])) {
|
|
3952
|
+
words.shift();
|
|
3953
|
+
}
|
|
3954
|
+
while (words.length > 0 && SPEAKER_NAME_STOPWORDS.has(words[words.length - 1])) {
|
|
3955
|
+
words.pop();
|
|
3956
|
+
}
|
|
3957
|
+
return words.length > 0 ? words.join(" ") : void 0;
|
|
3958
|
+
}
|
|
3959
|
+
function isTemporalCueBoundary(char) {
|
|
3960
|
+
if (!char) {
|
|
3961
|
+
return true;
|
|
3962
|
+
}
|
|
3963
|
+
return !isAsciiLetterOrDigit(char);
|
|
3964
|
+
}
|
|
3965
|
+
function tokenizeReferenceQuery(query) {
|
|
3966
|
+
const tokens = [];
|
|
3967
|
+
let current = "";
|
|
3968
|
+
const flushCurrent = () => {
|
|
3969
|
+
if (current) {
|
|
3970
|
+
tokens.push(current);
|
|
3971
|
+
current = "";
|
|
3972
|
+
}
|
|
3973
|
+
};
|
|
3974
|
+
for (const char of query) {
|
|
3975
|
+
if (isAsciiLetterOrDigit(char)) {
|
|
3976
|
+
current += char;
|
|
3977
|
+
continue;
|
|
3978
|
+
}
|
|
3979
|
+
flushCurrent();
|
|
3980
|
+
if (char === "#" || char === ",") {
|
|
3981
|
+
tokens.push(char);
|
|
3982
|
+
} else if (isReferenceDash(char)) {
|
|
3983
|
+
tokens.push("-");
|
|
3984
|
+
}
|
|
3985
|
+
}
|
|
3986
|
+
flushCurrent();
|
|
3987
|
+
return tokens;
|
|
3988
|
+
}
|
|
3989
|
+
function parseReferenceNumbers(tokens, startIndex) {
|
|
3990
|
+
const numbers = [];
|
|
3991
|
+
let lastNumber;
|
|
3992
|
+
let pendingRangeStart;
|
|
3993
|
+
let index = startIndex;
|
|
3994
|
+
const scanEnd = Math.min(
|
|
3995
|
+
tokens.length,
|
|
3996
|
+
startIndex + DEFAULT_MAX_REFERENCES * REFERENCE_SCAN_TOKEN_FACTOR
|
|
3997
|
+
);
|
|
3998
|
+
for (; index < scanEnd; index += 1) {
|
|
3999
|
+
const token = tokens[index];
|
|
4000
|
+
const normalized = token.toLowerCase();
|
|
4001
|
+
const value = parseNonNegativeIntegerToken(token);
|
|
4002
|
+
if (value !== void 0) {
|
|
4003
|
+
if (pendingRangeStart !== void 0) {
|
|
4004
|
+
numbers.push(...expandReferenceRange(pendingRangeStart, value));
|
|
4005
|
+
pendingRangeStart = void 0;
|
|
4006
|
+
} else {
|
|
4007
|
+
numbers.push(value);
|
|
4008
|
+
}
|
|
4009
|
+
lastNumber = value;
|
|
4010
|
+
continue;
|
|
4011
|
+
}
|
|
4012
|
+
if (normalized === "#" || normalized === "number" || normalized === ",") {
|
|
4013
|
+
continue;
|
|
4014
|
+
}
|
|
4015
|
+
if (normalized === "-" || normalized === "to" || normalized === "through" || normalized === "thru") {
|
|
4016
|
+
if (lastNumber !== void 0) {
|
|
4017
|
+
if (numbers[numbers.length - 1] === lastNumber) {
|
|
4018
|
+
numbers.pop();
|
|
4019
|
+
}
|
|
4020
|
+
pendingRangeStart = lastNumber;
|
|
4021
|
+
}
|
|
4022
|
+
continue;
|
|
4023
|
+
}
|
|
4024
|
+
if (normalized === "and" && numbers.length > 0) {
|
|
4025
|
+
continue;
|
|
4026
|
+
}
|
|
4027
|
+
if (normalizeReferenceLabel(token)) {
|
|
4028
|
+
break;
|
|
4029
|
+
}
|
|
4030
|
+
break;
|
|
4031
|
+
}
|
|
4032
|
+
if (pendingRangeStart !== void 0) {
|
|
4033
|
+
numbers.push(pendingRangeStart);
|
|
4034
|
+
}
|
|
4035
|
+
return {
|
|
4036
|
+
numbers: [...new Set(numbers)],
|
|
4037
|
+
nextIndex: index
|
|
4038
|
+
};
|
|
4039
|
+
}
|
|
4040
|
+
function expandReferenceRange(start, end) {
|
|
4041
|
+
const low = Math.min(start, end);
|
|
4042
|
+
const high = Math.max(start, end);
|
|
4043
|
+
if (high - low + 1 > DEFAULT_MAX_REFERENCES) {
|
|
4044
|
+
return [start, end];
|
|
4045
|
+
}
|
|
4046
|
+
const values = [];
|
|
4047
|
+
for (let value = low; value <= high; value += 1) {
|
|
4048
|
+
values.push(value);
|
|
4049
|
+
}
|
|
4050
|
+
return values;
|
|
4051
|
+
}
|
|
4052
|
+
function normalizeReferenceLabel(token) {
|
|
4053
|
+
const normalized = token?.toLowerCase();
|
|
4054
|
+
switch (normalized) {
|
|
4055
|
+
case "step":
|
|
4056
|
+
case "steps":
|
|
4057
|
+
return "step";
|
|
4058
|
+
case "turn":
|
|
4059
|
+
case "turns":
|
|
4060
|
+
return "turn";
|
|
4061
|
+
case "action":
|
|
4062
|
+
case "actions":
|
|
4063
|
+
return "action";
|
|
4064
|
+
case "observation":
|
|
4065
|
+
case "observations":
|
|
4066
|
+
return "observation";
|
|
4067
|
+
default:
|
|
4068
|
+
return void 0;
|
|
4069
|
+
}
|
|
4070
|
+
}
|
|
4071
|
+
function candidateTurnIndexesForReference(reference) {
|
|
4072
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
4073
|
+
if (reference.includeDirectTurn) {
|
|
4074
|
+
for (let offset = -1; offset <= 1; offset += 1) {
|
|
4075
|
+
candidates.add(reference.number + offset);
|
|
4076
|
+
}
|
|
4077
|
+
}
|
|
4078
|
+
const pairedBase = reference.number * 2;
|
|
4079
|
+
for (let offset = -1; offset <= 1; offset += 1) {
|
|
4080
|
+
candidates.add(pairedBase + offset);
|
|
4081
|
+
}
|
|
4082
|
+
return [...candidates].sort((left, right) => left - right);
|
|
4083
|
+
}
|
|
4084
|
+
function parseNonNegativeIntegerToken(token) {
|
|
4085
|
+
if (token.length === 0) {
|
|
4086
|
+
return void 0;
|
|
4087
|
+
}
|
|
4088
|
+
let value = 0;
|
|
4089
|
+
for (const char of token) {
|
|
4090
|
+
const code = char.charCodeAt(0);
|
|
4091
|
+
if (code < 48 || code > 57) {
|
|
4092
|
+
return void 0;
|
|
4093
|
+
}
|
|
4094
|
+
value = value * 10 + (code - 48);
|
|
4095
|
+
}
|
|
4096
|
+
return value;
|
|
4097
|
+
}
|
|
4098
|
+
function normalizePositiveInteger(value, fallback) {
|
|
4099
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
4100
|
+
return fallback;
|
|
4101
|
+
}
|
|
4102
|
+
return Math.max(0, Math.floor(value));
|
|
4103
|
+
}
|
|
4104
|
+
function isAsciiLetterOrDigit(char) {
|
|
4105
|
+
const code = char.charCodeAt(0);
|
|
4106
|
+
return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
4107
|
+
}
|
|
4108
|
+
function isReferenceDash(char) {
|
|
4109
|
+
return char === "-" || char === "\u2010" || char === "\u2011" || char === "\u2012" || char === "\u2013" || char === "\u2014" || char === "\u2015";
|
|
4110
|
+
}
|
|
4111
|
+
|
|
4112
|
+
export {
|
|
4113
|
+
buildExplicitCueRecallSection,
|
|
4114
|
+
buildTrajectoryAnalysisRecallSection,
|
|
4115
|
+
normalizeTurnExpansionEnd,
|
|
4116
|
+
collectExplicitTurnReferences,
|
|
4117
|
+
collectLexicalCues,
|
|
4118
|
+
collectQuestionSlotCues,
|
|
4119
|
+
collectContentLexicalCues,
|
|
4120
|
+
collectBenchmarkAnchorCues,
|
|
4121
|
+
collectStructuredPlanCues,
|
|
4122
|
+
collectTemporalLexicalCues
|
|
4123
|
+
};
|
|
4124
|
+
//# sourceMappingURL=chunk-OAZ5MFUB.js.map
|