@hassangameryt/freeflow 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +2 -1
- package/.codex-plugin/plugin.json +2 -1
- package/README.md +150 -155
- package/capabilities/cognitive-routing/SKILL.md +229 -0
- package/capabilities/context-virtualization/SKILL.md +103 -0
- package/capabilities/conversation-history/SKILL.md +53 -0
- package/command-surface.json +17 -13
- package/hooks/freeflow-runtime-context.mjs +393 -159
- package/hooks/hooks.json +26 -1
- package/hooks/tests/check-runtime-context-hook.sh +223 -54
- package/package.json +25 -16
- package/pi-extension/dist/cognitive-routing/commands.js +96 -0
- package/pi-extension/dist/cognitive-routing/config.js +236 -0
- package/pi-extension/dist/cognitive-routing/controller.js +862 -0
- package/pi-extension/dist/cognitive-routing/history.js +439 -0
- package/pi-extension/dist/cognitive-routing/host.js +126 -0
- package/pi-extension/dist/cognitive-routing/runtime.js +43 -0
- package/pi-extension/dist/cognitive-routing/tool.js +211 -0
- package/pi-extension/dist/cognitive-routing/types.js +6 -0
- package/pi-extension/dist/context-virtualization/commands.js +89 -0
- package/pi-extension/dist/context-virtualization/projector.js +42 -0
- package/pi-extension/dist/context-virtualization/runtime.js +356 -0
- package/pi-extension/dist/context-virtualization/state.js +57 -0
- package/pi-extension/dist/context-virtualization/types.js +10 -0
- package/pi-extension/dist/conversation-history/passages.js +66 -0
- package/pi-extension/dist/conversation-history/ranking.js +105 -0
- package/pi-extension/dist/conversation-history/runtime.js +226 -0
- package/pi-extension/dist/conversation-history/search.js +56 -0
- package/pi-extension/dist/conversation-history/types.js +1 -0
- package/pi-extension/dist/freeflow-context/resolver.js +120 -0
- package/pi-extension/dist/freeflow-context/runtime.js +80 -0
- package/pi-extension/dist/freeflow-context/source-projector.js +95 -0
- package/pi-extension/dist/freeflow-context/tool.js +308 -0
- package/pi-extension/dist/freeflow-context/types.js +12 -0
- package/pi-extension/dist/freeflow-context/visibility.js +28 -0
- package/pi-extension/dist/index.js +516 -98
- package/pi-extension/dist/runtime/runtime-context.js +318 -271
- package/pi-extension/dist/runtime/runtime-identity.js +5 -0
- package/pi-extension/dist/settings/settings-tui.js +78 -0
- package/pi-extension/dist/settings/settings-ui.js +593 -563
- package/pi-extension/dist/ui/text-component.js +57 -0
- package/pi-extension/src/cognitive-routing/commands.ts +119 -0
- package/pi-extension/src/cognitive-routing/config.ts +307 -0
- package/pi-extension/src/cognitive-routing/controller.ts +1118 -0
- package/pi-extension/src/cognitive-routing/history.ts +638 -0
- package/pi-extension/src/cognitive-routing/host.ts +172 -0
- package/pi-extension/src/cognitive-routing/runtime.ts +57 -0
- package/pi-extension/src/cognitive-routing/tool.ts +272 -0
- package/pi-extension/src/cognitive-routing/types.ts +89 -0
- package/pi-extension/src/context-virtualization/commands.ts +111 -0
- package/pi-extension/src/context-virtualization/projector.ts +62 -0
- package/pi-extension/src/context-virtualization/runtime.ts +406 -0
- package/pi-extension/src/context-virtualization/state.ts +79 -0
- package/pi-extension/src/context-virtualization/types.ts +92 -0
- package/pi-extension/src/conversation-history/passages.ts +81 -0
- package/pi-extension/src/conversation-history/ranking.ts +155 -0
- package/pi-extension/src/conversation-history/runtime.ts +250 -0
- package/pi-extension/src/conversation-history/search.ts +79 -0
- package/pi-extension/src/conversation-history/types.ts +31 -0
- package/pi-extension/src/freeflow-context/resolver.ts +139 -0
- package/pi-extension/src/freeflow-context/runtime.ts +100 -0
- package/pi-extension/src/freeflow-context/source-projector.ts +106 -0
- package/pi-extension/src/freeflow-context/tool.ts +360 -0
- package/pi-extension/src/freeflow-context/types.ts +70 -0
- package/pi-extension/src/freeflow-context/visibility.ts +53 -0
- package/pi-extension/src/index.ts +553 -101
- package/pi-extension/src/runtime/runtime-context.ts +339 -281
- package/pi-extension/src/runtime/runtime-identity.ts +8 -0
- package/pi-extension/src/settings/settings-tui.ts +93 -0
- package/pi-extension/src/settings/settings-ui.ts +700 -613
- package/pi-extension/src/ui/text-component.ts +66 -0
- package/pi-extension/tests/cognitive-routing/commands.test.js +134 -0
- package/pi-extension/tests/cognitive-routing/controller.test.js +1099 -0
- package/pi-extension/tests/cognitive-routing/extension-lifecycle.test.js +1071 -0
- package/pi-extension/tests/cognitive-routing/history.test.js +274 -0
- package/pi-extension/tests/cognitive-routing/host-fixture.js +4 -0
- package/pi-extension/tests/cognitive-routing/host-identity.test.js +63 -0
- package/pi-extension/tests/cognitive-routing/profile-resolution.test.js +236 -0
- package/pi-extension/tests/cognitive-routing/tool.test.js +230 -0
- package/pi-extension/tests/context-virtualization/extension.test.js +749 -0
- package/pi-extension/tests/context-virtualization/projector.test.js +110 -0
- package/pi-extension/tests/context-virtualization/runtime.test.js +216 -0
- package/pi-extension/tests/conversation-history/runtime.test.js +98 -0
- package/pi-extension/tests/conversation-history/search.test.js +97 -0
- package/pi-extension/tests/integration/pi-extension.test.js +928 -2047
- package/pi-extension/tests/integration/prompt-architecture.test.js +305 -0
- package/runtime/prompts/cognitive-routing.md +9 -0
- package/runtime/prompts/context-virtualization.md +3 -0
- package/runtime/prompts/conversation-history.md +3 -0
- package/runtime/prompts/core.md +13 -0
- package/runtime/prompts/interaction-contract.md +7 -0
- package/runtime/prompts/skills.md +65 -0
- package/skills/action-selection/SKILL.md +91 -0
- package/skills/bypass/SKILL.md +3 -2
- package/skills/commit-work/SKILL.md +1 -1
- package/skills/commit-work/references/staging-decisions.md +1 -1
- package/skills/decision-gate/SKILL.md +1 -1
- package/skills/design-for-depth/SKILL.md +14 -33
- package/skills/design-for-depth/references/design-pressure-signals.md +1 -1
- package/skills/design-for-depth/references/interface-design-loop.md +1 -1
- package/skills/design-for-depth/references/software-design-philosophy.md +1 -1
- package/skills/diagnose-failure/SKILL.md +20 -14
- package/skills/diagnose-failure/references/diagnostic-loop-catalog.md +1 -3
- package/skills/diagnose-failure/references/{flaky-and-performance.md → flaky-and-performance-diagnosis.md} +1 -1
- package/skills/discuss/SKILL.md +34 -22
- package/skills/discuss/references/discussion-continuity.md +71 -0
- package/skills/evaluate-skill/SKILL.md +5 -5
- package/skills/evaluate-skill/references/{definition-schema.md → evaluation-definition-schema.md} +42 -2
- package/skills/evaluate-skill/references/execution-and-evidence.md +8 -3
- package/skills/evaluate-skill/scripts/lib/definitions.mjs +114 -3
- package/skills/evaluate-skill/scripts/lib/grade-interaction.mjs +156 -1
- package/skills/evaluate-skill/scripts/lib/grade.mjs +9 -1
- package/skills/evaluate-skill/scripts/lib/pi.mjs +147 -32
- package/skills/evaluate-skill/scripts/lib/rpc.mjs +4 -4
- package/skills/evaluate-skill/scripts/lib/runner.mjs +36 -14
- package/skills/evaluate-skill/scripts/lib/sandbox.mjs +92 -0
- package/skills/evaluate-skill/scripts/pi-guard.mjs +1 -1
- package/skills/evaluate-skill/scripts/pi-observer.mjs +66 -0
- package/skills/evaluate-skill/scripts/skill-eval.mjs +6 -5
- package/skills/execute-work/SKILL.md +18 -15
- package/skills/execute-work/references/code-practices.md +1 -1
- package/skills/execute-work/references/execute-work-edges.md +42 -0
- package/skills/finish-branch/SKILL.md +2 -2
- package/skills/finish-branch/references/integration-options.md +1 -1
- package/skills/handoff/SKILL.md +3 -3
- package/skills/launch-work/SKILL.md +1 -1
- package/skills/launch-work/references/launch-readiness.md +2 -2
- package/skills/migration-work/SKILL.md +2 -2
- package/skills/migration-work/references/migration-lifecycle.md +1 -1
- package/skills/mode-contract/SKILL.md +32 -10
- package/skills/release-work/SKILL.md +1 -1
- package/skills/release-work/references/release-evidence.md +1 -1
- package/skills/review-artifact/SKILL.md +28 -26
- package/skills/review-work/SKILL.md +28 -26
- package/skills/review-work/references/{reviewer-prompt.md → independent-work-reviewer-contract.md} +1 -1
- package/skills/review-work/references/security-risk-lens.md +1 -1
- package/skills/setup-freeflow/SKILL.md +6 -10
- package/skills/setup-freeflow/references/activation-contract.md +5 -10
- package/skills/setup-freeflow/references/host-setup.md +3 -7
- package/skills/simplify-code/SKILL.md +1 -1
- package/skills/simplify-code/references/simplification-patterns.md +1 -1
- package/skills/tdd/SKILL.md +18 -38
- package/skills/tdd/references/test-design.md +2 -2
- package/skills/track-work/SKILL.md +362 -58
- package/skills/track-work/scripts/lib/codec.mjs +570 -0
- package/skills/track-work/scripts/lib/commands.mjs +568 -0
- package/skills/track-work/scripts/lib/contract.mjs +303 -0
- package/skills/track-work/scripts/lib/model.mjs +506 -0
- package/skills/track-work/scripts/lib/operations.mjs +965 -0
- package/skills/track-work/scripts/lib/record-store.mjs +264 -0
- package/skills/track-work/scripts/lib/result.mjs +37 -0
- package/skills/track-work/scripts/lib/rewrites.mjs +740 -0
- package/skills/track-work/scripts/lib/schema.mjs +277 -0
- package/skills/track-work/scripts/lib/source-inventory.mjs +162 -0
- package/skills/track-work/scripts/lib/views.mjs +443 -0
- package/skills/track-work/scripts/lib/workspace.mjs +134 -0
- package/skills/track-work/scripts/working-record.mjs +122 -0
- package/skills/verify-work/SKILL.md +14 -2
- package/skills/verify-work/references/browser-runtime-evidence.md +1 -1
- package/skills/verify-work/references/integration-evidence.md +1 -1
- package/skills/verify-work/references/performance-evidence.md +1 -1
- package/skills/workflow/SKILL.md +105 -53
- package/skills/workflow/references/domain-skill-composition.md +22 -42
- package/skills/write-plan/SKILL.md +1 -1
- package/skills/write-skill/SKILL.md +3 -2
- package/skills/write-skill/references/progressive-disclosure.md +17 -3
- package/pi-extension/dist/output-router/host-producer-identification.js +0 -232
- package/pi-extension/dist/output-router/native-safety-net.js +0 -216
- package/pi-extension/dist/output-router/observed-tool-routing.js +0 -122
- package/pi-extension/dist/output-router/renderers.js +0 -1023
- package/pi-extension/dist/output-router/router-tools.js +0 -554
- package/pi-extension/dist/output-router/schemas.js +0 -383
- package/pi-extension/dist/output-router/status.js +0 -744
- package/pi-extension/dist/output-router/utils.js +0 -502
- package/pi-extension/src/output-router/host-producer-identification.ts +0 -256
- package/pi-extension/src/output-router/native-safety-net.ts +0 -248
- package/pi-extension/src/output-router/observed-tool-routing.ts +0 -134
- package/pi-extension/src/output-router/renderers.ts +0 -1108
- package/pi-extension/src/output-router/router-tools.ts +0 -579
- package/pi-extension/src/output-router/schemas.ts +0 -387
- package/pi-extension/src/output-router/status.ts +0 -791
- package/pi-extension/src/output-router/utils.ts +0 -554
- package/pi-extension/tests/output-router/observed-producers.test.js +0 -132
- package/pi-extension/tests/output-router/search.test.js +0 -722
- package/router/dist/benchmarks/benchmark-harness.d.ts +0 -35
- package/router/dist/benchmarks/benchmark-harness.js +0 -92
- package/router/dist/benchmarks/benchmarks.d.ts +0 -109
- package/router/dist/benchmarks/benchmarks.js +0 -979
- package/router/dist/benchmarks/codex-qa-benchmarks.d.ts +0 -95
- package/router/dist/benchmarks/codex-qa-benchmarks.js +0 -500
- package/router/dist/benchmarks/command-benchmarks.d.ts +0 -143
- package/router/dist/benchmarks/command-benchmarks.js +0 -698
- package/router/dist/benchmarks/context-mode-normalized-benchmarks.d.ts +0 -82
- package/router/dist/benchmarks/context-mode-normalized-benchmarks.js +0 -773
- package/router/dist/benchmarks/context-mode-real-deep-benchmark.d.ts +0 -132
- package/router/dist/benchmarks/context-mode-real-deep-benchmark.js +0 -1380
- package/router/dist/benchmarks/context-saving-baseline.d.ts +0 -60
- package/router/dist/benchmarks/context-saving-baseline.js +0 -470
- package/router/dist/benchmarks/index-benchmarks.d.ts +0 -103
- package/router/dist/benchmarks/index-benchmarks.js +0 -862
- package/router/dist/benchmarks/storage-policy-benchmarks.d.ts +0 -101
- package/router/dist/benchmarks/storage-policy-benchmarks.js +0 -618
- package/router/dist/benchmarks/vault-index-benchmarks.d.ts +0 -56
- package/router/dist/benchmarks/vault-index-benchmarks.js +0 -322
- package/router/dist/config/config.d.ts +0 -103
- package/router/dist/config/config.js +0 -632
- package/router/dist/config/router-contract.d.ts +0 -10
- package/router/dist/config/router-contract.js +0 -54
- package/router/dist/config/schema.d.ts +0 -27
- package/router/dist/config/schema.js +0 -648
- package/router/dist/config/types.d.ts +0 -573
- package/router/dist/config/types.js +0 -41
- package/router/dist/evidence/bounded-evidence.d.ts +0 -40
- package/router/dist/evidence/bounded-evidence.js +0 -323
- package/router/dist/evidence/evidence-range-selector.d.ts +0 -24
- package/router/dist/evidence/evidence-range-selector.js +0 -232
- package/router/dist/evidence/evidence-search.d.ts +0 -27
- package/router/dist/evidence/evidence-search.js +0 -490
- package/router/dist/evidence/evidence.d.ts +0 -38
- package/router/dist/evidence/evidence.js +0 -200
- package/router/dist/evidence/failure-contracts.d.ts +0 -37
- package/router/dist/evidence/failure-contracts.js +0 -166
- package/router/dist/evidence/line-ranges.d.ts +0 -24
- package/router/dist/evidence/line-ranges.js +0 -22
- package/router/dist/experiments/local-index.d.ts +0 -64
- package/router/dist/experiments/local-index.js +0 -581
- package/router/dist/index.d.ts +0 -30
- package/router/dist/index.js +0 -32
- package/router/dist/local/local-traversal.d.ts +0 -18
- package/router/dist/local/local-traversal.js +0 -334
- package/router/dist/processing/engine.d.ts +0 -132
- package/router/dist/processing/engine.js +0 -533
- package/router/dist/processing/reducers.d.ts +0 -280
- package/router/dist/processing/reducers.js +0 -1673
- package/router/dist/processing/renderers.d.ts +0 -31
- package/router/dist/processing/renderers.js +0 -195
- package/router/dist/processing/scripts.d.ts +0 -91
- package/router/dist/processing/scripts.js +0 -386
- package/router/dist/repo/repo-traversal.d.ts +0 -18
- package/router/dist/repo/repo-traversal.js +0 -245
- package/router/dist/routing/observed-reducers.d.ts +0 -41
- package/router/dist/routing/observed-reducers.js +0 -661
- package/router/dist/routing/observed-routing.d.ts +0 -26
- package/router/dist/routing/observed-routing.js +0 -378
- package/router/dist/routing/parsers.d.ts +0 -16
- package/router/dist/routing/parsers.js +0 -521
- package/router/dist/routing/run-filters.d.ts +0 -60
- package/router/dist/routing/run-filters.js +0 -238
- package/router/dist/routing/run-reducers.d.ts +0 -49
- package/router/dist/routing/run-reducers.js +0 -122
- package/router/dist/sandbox/adapter-roots.d.ts +0 -46
- package/router/dist/sandbox/adapter-roots.js +0 -66
- package/router/dist/sandbox/eryx-python-adapter.d.ts +0 -13
- package/router/dist/sandbox/eryx-python-adapter.js +0 -868
- package/router/dist/sandbox/jq-wasm-adapter.d.ts +0 -9
- package/router/dist/sandbox/jq-wasm-adapter.js +0 -429
- package/router/dist/sandbox/quickjs-wasi-adapter.d.ts +0 -13
- package/router/dist/sandbox/quickjs-wasi-adapter.js +0 -384
- package/router/dist/sandbox/script-sandbox.d.ts +0 -134
- package/router/dist/sandbox/script-sandbox.js +0 -337
- package/router/dist/setup/script-transform-adapters.d.ts +0 -32
- package/router/dist/setup/script-transform-adapters.js +0 -278
- package/router/dist/tools/batch.d.ts +0 -33
- package/router/dist/tools/batch.js +0 -586
- package/router/dist/tools/run.d.ts +0 -62
- package/router/dist/tools/run.js +0 -1304
- package/router/dist/tools/search.d.ts +0 -66
- package/router/dist/tools/search.js +0 -1364
- package/router/dist/transform/engine.d.ts +0 -168
- package/router/dist/transform/engine.js +0 -2072
- package/router/dist/vault/vault-index.d.ts +0 -125
- package/router/dist/vault/vault-index.js +0 -416
- package/router/dist/vault/vault.d.ts +0 -138
- package/router/dist/vault/vault.js +0 -630
- package/runtime/interaction-contract.md +0 -16
- package/skills/discuss/references/checkpoints.md +0 -66
- package/skills/execute-work/references/execution-loop.md +0 -77
- package/skills/output-router/SKILL.md +0 -206
- package/skills/output-router/references/safety-policy.md +0 -40
- package/skills/setup-freeflow/references/output-router-setup.md +0 -110
- package/skills/track-work/references/working-record-schema.md +0 -378
- package/skills/workflow/references/workflow-loop.md +0 -92
- /package/skills/handoff/references/{templates.md → handoff-templates.md} +0 -0
- /package/skills/review-artifact/references/{reviewer-prompt.md → independent-artifact-reviewer-contract.md} +0 -0
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
import type { CognitiveRoutingProfileName } from "./types.js";
|
|
2
|
+
|
|
3
|
+
const COGNITIVE_ROUTING_INTENT_ENTRY = "freeflow-cognitive-routing-intent";
|
|
4
|
+
const COGNITIVE_ROUTING_CONTROL_ENTRY = "freeflow-cognitive-routing-control";
|
|
5
|
+
const UNKNOWN = "unknown";
|
|
6
|
+
|
|
7
|
+
export type CognitiveRoutingHistoryScope = "session" | "active-branch";
|
|
8
|
+
export type CognitiveRoutingHistoryClassification =
|
|
9
|
+
| "semantic-switch"
|
|
10
|
+
| "lifecycle-replay"
|
|
11
|
+
| "initialization"
|
|
12
|
+
| "return-target-restore"
|
|
13
|
+
| "control-only"
|
|
14
|
+
| "external-host-change";
|
|
15
|
+
export type CognitiveRoutingHistoryDecisionSource = "user" | "agent" | "none" | "unknown";
|
|
16
|
+
export type CognitiveRoutingHistoryOutcome = "completed" | "unresolved" | "abandoned" | "external";
|
|
17
|
+
export type CognitiveRoutingHistoryIntegrity = "valid" | "unknown" | "anomaly";
|
|
18
|
+
|
|
19
|
+
export type CognitiveRoutingHistoryPair = {
|
|
20
|
+
provider: string;
|
|
21
|
+
modelId: string;
|
|
22
|
+
thinkingLevel: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type CognitiveRoutingHistoryCurrent = {
|
|
26
|
+
control: "automatic" | "manual" | "unavailable";
|
|
27
|
+
profile: CognitiveRoutingProfileName | "unavailable";
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type CognitiveRoutingHistoryEvent = {
|
|
31
|
+
id: string;
|
|
32
|
+
timestamp?: string;
|
|
33
|
+
jsonlPosition: number;
|
|
34
|
+
entryId: string;
|
|
35
|
+
parentId?: string;
|
|
36
|
+
branchAnchor?: string;
|
|
37
|
+
classification: CognitiveRoutingHistoryClassification;
|
|
38
|
+
decisionSource: CognitiveRoutingHistoryDecisionSource;
|
|
39
|
+
mechanism?: string;
|
|
40
|
+
outcome: CognitiveRoutingHistoryOutcome;
|
|
41
|
+
changed: boolean | "unknown";
|
|
42
|
+
integrity: CognitiveRoutingHistoryIntegrity;
|
|
43
|
+
from?: CognitiveRoutingProfileName;
|
|
44
|
+
to?: CognitiveRoutingProfileName;
|
|
45
|
+
control?: "automatic" | "manual";
|
|
46
|
+
reason?: string;
|
|
47
|
+
epoch?: string;
|
|
48
|
+
correlationId?: string;
|
|
49
|
+
decisionCorrelationId?: string;
|
|
50
|
+
hostOrigin?: unknown;
|
|
51
|
+
anomalyReason?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type CognitiveRoutingHistoryResult = {
|
|
55
|
+
current: CognitiveRoutingHistoryCurrent;
|
|
56
|
+
summary: {
|
|
57
|
+
latestSemanticEventId?: string;
|
|
58
|
+
latestCompletedEventId?: string;
|
|
59
|
+
unresolvedCount: number;
|
|
60
|
+
anomalyCount: number;
|
|
61
|
+
};
|
|
62
|
+
events: CognitiveRoutingHistoryEvent[];
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type CognitiveRoutingHistoryOptions = {
|
|
66
|
+
branchEntries?: readonly unknown[];
|
|
67
|
+
scope?: CognitiveRoutingHistoryScope;
|
|
68
|
+
anomaliesOnly?: boolean;
|
|
69
|
+
limit?: number;
|
|
70
|
+
current?: CognitiveRoutingHistoryCurrent;
|
|
71
|
+
profilePairs?: Partial<Record<CognitiveRoutingProfileName, CognitiveRoutingHistoryPair>>;
|
|
72
|
+
profileForPair?: (pair: CognitiveRoutingHistoryPair) => CognitiveRoutingProfileName | undefined;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type CognitiveRoutingHistoryContext = {
|
|
76
|
+
sessionManager?: {
|
|
77
|
+
getEntries?: () => readonly unknown[];
|
|
78
|
+
getBranch?: () => readonly unknown[];
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
type EntryRecord = Record<string, unknown>;
|
|
83
|
+
type HistoryVersion = 1 | 2;
|
|
84
|
+
type IntentSource = "system" | "user" | "agent" | "unknown";
|
|
85
|
+
|
|
86
|
+
type ParsedIntent = {
|
|
87
|
+
version: HistoryVersion;
|
|
88
|
+
kind: "activation" | "profile" | "closing";
|
|
89
|
+
phase: "prepared" | "abandoned";
|
|
90
|
+
control: "automatic" | "manual";
|
|
91
|
+
source: IntentSource;
|
|
92
|
+
reason?: string;
|
|
93
|
+
branchId?: string | null;
|
|
94
|
+
epoch: string;
|
|
95
|
+
correlationId: string;
|
|
96
|
+
profile?: CognitiveRoutingProfileName;
|
|
97
|
+
resumeProfile?: CognitiveRoutingProfileName;
|
|
98
|
+
resumeControl?: "automatic" | "manual";
|
|
99
|
+
resumeReason?: string;
|
|
100
|
+
target: CognitiveRoutingHistoryPair;
|
|
101
|
+
returnTarget: CognitiveRoutingHistoryPair;
|
|
102
|
+
mechanism?: string;
|
|
103
|
+
decisionCorrelationId?: string;
|
|
104
|
+
fromPair?: CognitiveRoutingHistoryPair;
|
|
105
|
+
fromProfile?: CognitiveRoutingProfileName;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
type ParsedControl = {
|
|
109
|
+
version: HistoryVersion;
|
|
110
|
+
control: "automatic";
|
|
111
|
+
source: "user" | "unknown";
|
|
112
|
+
reason?: string;
|
|
113
|
+
branchId?: string | null;
|
|
114
|
+
epoch?: string;
|
|
115
|
+
mechanism?: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type EntryMetadata = {
|
|
119
|
+
raw: EntryRecord;
|
|
120
|
+
position: number;
|
|
121
|
+
id: string;
|
|
122
|
+
parentId?: string;
|
|
123
|
+
timestamp?: string;
|
|
124
|
+
branchAnchor?: string;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
type HostRecord = {
|
|
128
|
+
metadata: EntryMetadata;
|
|
129
|
+
pair?: CognitiveRoutingHistoryPair;
|
|
130
|
+
correlationId?: string;
|
|
131
|
+
hostOrigin?: unknown;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
type IntentRecord = {
|
|
135
|
+
prepared: { metadata: EntryMetadata; intent: ParsedIntent };
|
|
136
|
+
abandonment?: { metadata: EntryMetadata; intent: ParsedIntent };
|
|
137
|
+
conflict: boolean;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
type InternalEvent = CognitiveRoutingHistoryEvent & {
|
|
141
|
+
representativePosition: number;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
function isRecord(value: unknown): value is EntryRecord {
|
|
145
|
+
return Boolean(value && typeof value === "object");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function stringValue(value: unknown): string | undefined {
|
|
149
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function nullableString(value: unknown): string | null | undefined {
|
|
153
|
+
if (value === null) return null;
|
|
154
|
+
return typeof value === "string" ? value : undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function profileName(value: unknown): CognitiveRoutingProfileName | undefined {
|
|
158
|
+
return value === "standard" || value === "reasoning" ? value : undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function pairFrom(value: unknown): CognitiveRoutingHistoryPair | undefined {
|
|
162
|
+
if (!isRecord(value)) return undefined;
|
|
163
|
+
const provider = stringValue(value.provider);
|
|
164
|
+
const modelId = stringValue(value.modelId ?? value.id);
|
|
165
|
+
const thinkingLevel = stringValue(value.thinkingLevel);
|
|
166
|
+
if (!provider || !modelId || !thinkingLevel) return undefined;
|
|
167
|
+
return { provider, modelId, thinkingLevel };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function pairFromEntry(entry: EntryRecord): CognitiveRoutingHistoryPair | undefined {
|
|
171
|
+
return pairFrom({
|
|
172
|
+
provider: entry.provider,
|
|
173
|
+
modelId: entry.modelId,
|
|
174
|
+
thinkingLevel: entry.thinkingLevel,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function pairsEqual(
|
|
179
|
+
left: CognitiveRoutingHistoryPair | undefined,
|
|
180
|
+
right: CognitiveRoutingHistoryPair | undefined,
|
|
181
|
+
): boolean {
|
|
182
|
+
return (
|
|
183
|
+
left?.provider === right?.provider &&
|
|
184
|
+
left?.modelId === right?.modelId &&
|
|
185
|
+
left?.thinkingLevel === right?.thinkingLevel
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function validTimestamp(value: unknown): string | undefined {
|
|
190
|
+
if (typeof value === "string" && !Number.isNaN(Date.parse(value))) return value;
|
|
191
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
192
|
+
const date = new Date(value);
|
|
193
|
+
return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
|
|
194
|
+
}
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function metadataFor(entry: unknown, position: number): EntryMetadata {
|
|
199
|
+
const raw = isRecord(entry) ? entry : {};
|
|
200
|
+
const knownId = stringValue(raw.id);
|
|
201
|
+
const parentId = stringValue(raw.parentId);
|
|
202
|
+
const timestamp = validTimestamp(raw.timestamp);
|
|
203
|
+
const id = knownId ?? `${UNKNOWN}:${position}`;
|
|
204
|
+
return {
|
|
205
|
+
raw,
|
|
206
|
+
position,
|
|
207
|
+
id,
|
|
208
|
+
...(parentId ? { parentId } : {}),
|
|
209
|
+
...(timestamp ? { timestamp } : {}),
|
|
210
|
+
...(knownId ? { branchAnchor: knownId } : {}),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function sourceFrom(value: unknown): IntentSource {
|
|
215
|
+
return value === "user" || value === "agent" || value === "system" ? value : "unknown";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function versionFrom(value: unknown): HistoryVersion | undefined {
|
|
219
|
+
return value === 1 || value === 2 ? value : undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function parseIntent(value: unknown): ParsedIntent | undefined {
|
|
223
|
+
if (!isRecord(value)) return undefined;
|
|
224
|
+
const version = versionFrom(value.version);
|
|
225
|
+
const kind =
|
|
226
|
+
value.kind === "activation" || value.kind === "profile" || value.kind === "closing" ? value.kind : undefined;
|
|
227
|
+
const phase = value.phase === "prepared" || value.phase === "abandoned" ? value.phase : undefined;
|
|
228
|
+
const epoch = stringValue(value.epoch);
|
|
229
|
+
const correlationId = stringValue(value.correlationId);
|
|
230
|
+
const target = pairFrom(value.target);
|
|
231
|
+
const returnTarget = pairFrom(value.returnTarget);
|
|
232
|
+
if (!version || !kind || !phase || !target || !returnTarget || !epoch || !correlationId) return undefined;
|
|
233
|
+
const reason = stringValue(value.reason);
|
|
234
|
+
const resumeReason = stringValue(value.resumeReason);
|
|
235
|
+
const branchId = nullableString(value.branchId);
|
|
236
|
+
const mechanism = stringValue(value.mechanism);
|
|
237
|
+
const decisionCorrelationId = stringValue(value.decisionCorrelationId);
|
|
238
|
+
const fromPair = pairFrom(value.fromPair);
|
|
239
|
+
const profile = profileName(value.profile);
|
|
240
|
+
const resumeProfile = profileName(value.resumeProfile);
|
|
241
|
+
const fromProfile = profileName(value.fromProfile);
|
|
242
|
+
return {
|
|
243
|
+
version,
|
|
244
|
+
kind,
|
|
245
|
+
phase,
|
|
246
|
+
control: value.control === "manual" ? "manual" : "automatic",
|
|
247
|
+
source: sourceFrom(value.source),
|
|
248
|
+
...(reason ? { reason } : {}),
|
|
249
|
+
...(branchId === undefined ? {} : { branchId }),
|
|
250
|
+
epoch,
|
|
251
|
+
correlationId,
|
|
252
|
+
...(profile ? { profile } : {}),
|
|
253
|
+
...(resumeProfile ? { resumeProfile } : {}),
|
|
254
|
+
...(value.resumeControl === "automatic" || value.resumeControl === "manual"
|
|
255
|
+
? { resumeControl: value.resumeControl }
|
|
256
|
+
: {}),
|
|
257
|
+
...(resumeReason ? { resumeReason } : {}),
|
|
258
|
+
target,
|
|
259
|
+
returnTarget,
|
|
260
|
+
...(mechanism ? { mechanism } : {}),
|
|
261
|
+
...(decisionCorrelationId ? { decisionCorrelationId } : {}),
|
|
262
|
+
...(fromPair ? { fromPair } : {}),
|
|
263
|
+
...(fromProfile ? { fromProfile } : {}),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function parseControl(value: unknown): ParsedControl | undefined {
|
|
268
|
+
if (!isRecord(value)) return undefined;
|
|
269
|
+
const version = versionFrom(value.version);
|
|
270
|
+
if (!version || value.control !== "automatic") return undefined;
|
|
271
|
+
const reason = stringValue(value.reason);
|
|
272
|
+
const branchId = nullableString(value.branchId);
|
|
273
|
+
const epoch = stringValue(value.epoch);
|
|
274
|
+
const mechanism = stringValue(value.mechanism);
|
|
275
|
+
return {
|
|
276
|
+
version,
|
|
277
|
+
control: "automatic",
|
|
278
|
+
source: value.source === "user" ? "user" : "unknown",
|
|
279
|
+
...(reason ? { reason } : {}),
|
|
280
|
+
...(branchId === undefined ? {} : { branchId }),
|
|
281
|
+
...(epoch ? { epoch } : {}),
|
|
282
|
+
...(mechanism ? { mechanism } : {}),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function eventBase(
|
|
287
|
+
id: string,
|
|
288
|
+
metadata: EntryMetadata,
|
|
289
|
+
): Omit<CognitiveRoutingHistoryEvent, "classification" | "decisionSource" | "outcome" | "changed" | "integrity"> {
|
|
290
|
+
return {
|
|
291
|
+
id,
|
|
292
|
+
...(metadata.timestamp ? { timestamp: metadata.timestamp } : {}),
|
|
293
|
+
jsonlPosition: metadata.position,
|
|
294
|
+
entryId: metadata.id,
|
|
295
|
+
...(metadata.parentId ? { parentId: metadata.parentId } : {}),
|
|
296
|
+
...(metadata.branchAnchor ? { branchAnchor: metadata.branchAnchor } : {}),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function profileForPair(pair: CognitiveRoutingHistoryPair | undefined, options: CognitiveRoutingHistoryOptions) {
|
|
301
|
+
if (!pair) return undefined;
|
|
302
|
+
if (options.profileForPair) return options.profileForPair(pair);
|
|
303
|
+
for (const profile of ["standard", "reasoning"] as const) {
|
|
304
|
+
if (pairsEqual(pair, options.profilePairs?.[profile])) return profile;
|
|
305
|
+
}
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function intentClassification(intent: ParsedIntent): CognitiveRoutingHistoryClassification {
|
|
310
|
+
if (intent.kind === "profile")
|
|
311
|
+
return intent.source === "user" || intent.source === "agent" ? "semantic-switch" : "lifecycle-replay";
|
|
312
|
+
if (intent.kind === "closing") return "return-target-restore";
|
|
313
|
+
return ["reload-restore", "recovery", "session-tree", "session-compact"].includes(intent.mechanism ?? "")
|
|
314
|
+
? "lifecycle-replay"
|
|
315
|
+
: "initialization";
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function lifecycleDecisionSource(
|
|
319
|
+
intent: ParsedIntent,
|
|
320
|
+
semanticSources: ReadonlyMap<string, "user" | "agent">,
|
|
321
|
+
): CognitiveRoutingHistoryDecisionSource {
|
|
322
|
+
if (intent.kind === "profile" && (intent.source === "user" || intent.source === "agent")) return intent.source;
|
|
323
|
+
if (intent.decisionCorrelationId && semanticSources.get(intent.decisionCorrelationId)) {
|
|
324
|
+
return semanticSources.get(intent.decisionCorrelationId) as "user" | "agent";
|
|
325
|
+
}
|
|
326
|
+
if (intent.kind === "profile" || intent.source === "unknown") return "unknown";
|
|
327
|
+
return "none";
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function metadataIntegrity(
|
|
331
|
+
intent: ParsedIntent,
|
|
332
|
+
semanticSources: ReadonlyMap<string, "user" | "agent">,
|
|
333
|
+
): { integrity: CognitiveRoutingHistoryIntegrity; anomalyReason?: string } {
|
|
334
|
+
if (intent.version === 1) return { integrity: "unknown" };
|
|
335
|
+
if (!intent.mechanism) return { integrity: "anomaly", anomalyReason: "mechanism_missing" };
|
|
336
|
+
if (!intent.fromPair) return { integrity: "anomaly", anomalyReason: "from_pair_missing" };
|
|
337
|
+
if (intent.kind === "profile") {
|
|
338
|
+
if (!intent.decisionCorrelationId) return { integrity: "anomaly", anomalyReason: "decision_correlation_missing" };
|
|
339
|
+
if (intent.source === "user" || intent.source === "agent") {
|
|
340
|
+
if (intent.decisionCorrelationId !== intent.correlationId) {
|
|
341
|
+
return { integrity: "anomaly", anomalyReason: "semantic_decision_correlation_mismatch" };
|
|
342
|
+
}
|
|
343
|
+
} else if (!semanticSources.has(intent.decisionCorrelationId)) {
|
|
344
|
+
return { integrity: "anomaly", anomalyReason: "lifecycle_decision_unresolved" };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return { integrity: "valid" };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function intentEvent(
|
|
351
|
+
record: IntentRecord,
|
|
352
|
+
representative: EntryMetadata,
|
|
353
|
+
host: HostRecord | undefined,
|
|
354
|
+
options: CognitiveRoutingHistoryOptions,
|
|
355
|
+
semanticSources: ReadonlyMap<string, "user" | "agent">,
|
|
356
|
+
outcome: CognitiveRoutingHistoryOutcome,
|
|
357
|
+
integrity: CognitiveRoutingHistoryIntegrity,
|
|
358
|
+
changed: boolean | "unknown",
|
|
359
|
+
anomalyReason?: string,
|
|
360
|
+
): InternalEvent {
|
|
361
|
+
const intent = record.prepared.intent;
|
|
362
|
+
const metadata = metadataIntegrity(intent, semanticSources);
|
|
363
|
+
const effectiveIntegrity = metadata.integrity === "anomaly" ? "anomaly" : integrity;
|
|
364
|
+
const classification = intentClassification(intent);
|
|
365
|
+
const from = intent.fromProfile ?? profileForPair(intent.fromPair, options);
|
|
366
|
+
const to = intent.profile ?? profileForPair(intent.target, options);
|
|
367
|
+
return {
|
|
368
|
+
...eventBase(`intent:${record.prepared.metadata.id}`, representative),
|
|
369
|
+
classification,
|
|
370
|
+
decisionSource: lifecycleDecisionSource(intent, semanticSources),
|
|
371
|
+
...(intent.mechanism ? { mechanism: intent.mechanism } : {}),
|
|
372
|
+
outcome,
|
|
373
|
+
changed,
|
|
374
|
+
integrity: effectiveIntegrity,
|
|
375
|
+
...(from ? { from } : {}),
|
|
376
|
+
...(to ? { to } : {}),
|
|
377
|
+
...(intent.control ? { control: intent.control } : {}),
|
|
378
|
+
...(intent.reason ? { reason: intent.reason } : {}),
|
|
379
|
+
epoch: intent.epoch,
|
|
380
|
+
correlationId: intent.correlationId,
|
|
381
|
+
...(intent.decisionCorrelationId ? { decisionCorrelationId: intent.decisionCorrelationId } : {}),
|
|
382
|
+
...(host?.hostOrigin === undefined ? {} : { hostOrigin: host.hostOrigin }),
|
|
383
|
+
...(anomalyReason || metadata.anomalyReason ? { anomalyReason: anomalyReason ?? metadata.anomalyReason } : {}),
|
|
384
|
+
representativePosition: representative.position,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function controlEvent(metadata: EntryMetadata, control: ParsedControl): InternalEvent {
|
|
389
|
+
const integrity = control.version === 1 ? "unknown" : control.mechanism ? "valid" : "anomaly";
|
|
390
|
+
const anomalyReason = control.version === 2 && !control.mechanism ? "mechanism_missing" : undefined;
|
|
391
|
+
return {
|
|
392
|
+
...eventBase(`control:${metadata.id}`, metadata),
|
|
393
|
+
classification: "control-only",
|
|
394
|
+
decisionSource: control.source,
|
|
395
|
+
...(control.mechanism ? { mechanism: control.mechanism } : {}),
|
|
396
|
+
outcome: "completed",
|
|
397
|
+
changed: false,
|
|
398
|
+
integrity,
|
|
399
|
+
control: "automatic",
|
|
400
|
+
...(control.reason ? { reason: control.reason } : {}),
|
|
401
|
+
...(control.epoch ? { epoch: control.epoch } : {}),
|
|
402
|
+
...(anomalyReason ? { anomalyReason } : {}),
|
|
403
|
+
representativePosition: metadata.position,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function hostEvent(
|
|
408
|
+
host: HostRecord,
|
|
409
|
+
integrity: CognitiveRoutingHistoryIntegrity,
|
|
410
|
+
anomalyReason: string | undefined,
|
|
411
|
+
): InternalEvent {
|
|
412
|
+
return {
|
|
413
|
+
...eventBase(`host:${host.metadata.id}`, host.metadata),
|
|
414
|
+
classification: "external-host-change",
|
|
415
|
+
decisionSource: "none",
|
|
416
|
+
outcome: "external",
|
|
417
|
+
changed: "unknown",
|
|
418
|
+
integrity,
|
|
419
|
+
...(host.correlationId ? { correlationId: host.correlationId } : {}),
|
|
420
|
+
...(host.hostOrigin === undefined ? {} : { hostOrigin: host.hostOrigin }),
|
|
421
|
+
...(anomalyReason ? { anomalyReason } : {}),
|
|
422
|
+
representativePosition: host.metadata.position,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function activeEntryIds(entries: readonly unknown[] | undefined): Set<string> {
|
|
427
|
+
return new Set(
|
|
428
|
+
(Array.isArray(entries) ? entries : [])
|
|
429
|
+
.map((entry, index) => metadataFor(entry, index).id)
|
|
430
|
+
.filter((id) => !id.startsWith(`${UNKNOWN}:`)),
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function normalizeLimit(limit: number | undefined): number {
|
|
435
|
+
if (!Number.isInteger(limit)) return 20;
|
|
436
|
+
return Math.max(1, Math.min(100, limit as number));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function currentState(current: CognitiveRoutingHistoryCurrent | undefined): CognitiveRoutingHistoryCurrent {
|
|
440
|
+
return {
|
|
441
|
+
control: current?.control === "automatic" || current?.control === "manual" ? current.control : "unavailable",
|
|
442
|
+
profile: current?.profile === "standard" || current?.profile === "reasoning" ? current.profile : "unavailable",
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
export function projectCognitiveRoutingHistory(
|
|
447
|
+
entries: readonly unknown[],
|
|
448
|
+
options: CognitiveRoutingHistoryOptions = {},
|
|
449
|
+
): CognitiveRoutingHistoryResult {
|
|
450
|
+
const intentRecords = new Map<string, IntentRecord>();
|
|
451
|
+
const controls: Array<{ metadata: EntryMetadata; control: ParsedControl }> = [];
|
|
452
|
+
const hosts: HostRecord[] = [];
|
|
453
|
+
const semanticSources = new Map<string, "user" | "agent">();
|
|
454
|
+
|
|
455
|
+
entries.forEach((entry, position) => {
|
|
456
|
+
const metadata = metadataFor(entry, position);
|
|
457
|
+
if (!isRecord(entry)) return;
|
|
458
|
+
if (entry.type === "custom" && entry.customType === COGNITIVE_ROUTING_INTENT_ENTRY) {
|
|
459
|
+
const parsed = parseIntent(entry.data);
|
|
460
|
+
if (!parsed) return;
|
|
461
|
+
if (parsed.phase === "abandoned") {
|
|
462
|
+
const existing = intentRecords.get(parsed.correlationId);
|
|
463
|
+
if (existing) existing.abandonment = { metadata, intent: parsed };
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const existing = intentRecords.get(parsed.correlationId);
|
|
467
|
+
if (existing) {
|
|
468
|
+
existing.conflict = true;
|
|
469
|
+
} else {
|
|
470
|
+
intentRecords.set(parsed.correlationId, { prepared: { metadata, intent: parsed }, conflict: false });
|
|
471
|
+
if (parsed.kind === "profile" && (parsed.source === "user" || parsed.source === "agent")) {
|
|
472
|
+
semanticSources.set(parsed.correlationId, parsed.source);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (entry.type === "custom" && entry.customType === COGNITIVE_ROUTING_CONTROL_ENTRY) {
|
|
478
|
+
const parsed = parseControl(entry.data);
|
|
479
|
+
if (parsed) controls.push({ metadata, control: parsed });
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (entry.type === "model_state_change") {
|
|
483
|
+
const correlationId = stringValue(entry.correlationId);
|
|
484
|
+
hosts.push({
|
|
485
|
+
metadata,
|
|
486
|
+
pair: pairFromEntry(entry),
|
|
487
|
+
...(correlationId ? { correlationId } : {}),
|
|
488
|
+
...(entry.origin === undefined
|
|
489
|
+
? entry.hostOrigin === undefined
|
|
490
|
+
? {}
|
|
491
|
+
: { hostOrigin: entry.hostOrigin }
|
|
492
|
+
: { hostOrigin: entry.origin }),
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
const usedHostPositions = new Set<number>();
|
|
498
|
+
const events: InternalEvent[] = [];
|
|
499
|
+
const activeEpochs = new Set<string>();
|
|
500
|
+
let completedClosing = false;
|
|
501
|
+
|
|
502
|
+
for (const record of intentRecords.values()) {
|
|
503
|
+
const intent = record.prepared.intent;
|
|
504
|
+
const correlatedHosts = hosts.filter((host) => host.correlationId === intent.correlationId);
|
|
505
|
+
const matchingHosts = correlatedHosts.filter((host) => pairsEqual(host.pair, intent.target));
|
|
506
|
+
correlatedHosts.forEach((host) => usedHostPositions.add(host.metadata.position));
|
|
507
|
+
const metadata = metadataIntegrity(intent, semanticSources);
|
|
508
|
+
|
|
509
|
+
if (intent.kind === "closing" && matchingHosts.length > 0) completedClosing = true;
|
|
510
|
+
|
|
511
|
+
if (record.abandonment) {
|
|
512
|
+
events.push(
|
|
513
|
+
intentEvent(
|
|
514
|
+
record,
|
|
515
|
+
record.abandonment.metadata,
|
|
516
|
+
undefined,
|
|
517
|
+
options,
|
|
518
|
+
semanticSources,
|
|
519
|
+
"abandoned",
|
|
520
|
+
record.conflict ? "anomaly" : metadata.integrity,
|
|
521
|
+
"unknown",
|
|
522
|
+
record.conflict ? "duplicate_intent" : metadata.anomalyReason,
|
|
523
|
+
),
|
|
524
|
+
);
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (intent.kind !== "closing") activeEpochs.add(intent.epoch);
|
|
529
|
+
|
|
530
|
+
if (matchingHosts.length > 0) {
|
|
531
|
+
const representative = matchingHosts.at(-1) as HostRecord;
|
|
532
|
+
const changed = intent.fromPair ? !pairsEqual(intent.fromPair, representative.pair) : "unknown";
|
|
533
|
+
const duplicateReason = record.conflict
|
|
534
|
+
? "duplicate_intent"
|
|
535
|
+
: correlatedHosts.length > 1
|
|
536
|
+
? "conflicting_host_results"
|
|
537
|
+
: undefined;
|
|
538
|
+
events.push(
|
|
539
|
+
intentEvent(
|
|
540
|
+
record,
|
|
541
|
+
representative.metadata,
|
|
542
|
+
representative,
|
|
543
|
+
options,
|
|
544
|
+
semanticSources,
|
|
545
|
+
"completed",
|
|
546
|
+
duplicateReason ? "anomaly" : changed === "unknown" ? "unknown" : metadata.integrity,
|
|
547
|
+
changed,
|
|
548
|
+
duplicateReason,
|
|
549
|
+
),
|
|
550
|
+
);
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
events.push(
|
|
555
|
+
intentEvent(
|
|
556
|
+
record,
|
|
557
|
+
record.prepared.metadata,
|
|
558
|
+
undefined,
|
|
559
|
+
options,
|
|
560
|
+
semanticSources,
|
|
561
|
+
"unresolved",
|
|
562
|
+
record.conflict || correlatedHosts.length > 0 || metadata.integrity === "anomaly" ? "anomaly" : "unknown",
|
|
563
|
+
"unknown",
|
|
564
|
+
record.conflict
|
|
565
|
+
? "duplicate_intent"
|
|
566
|
+
: correlatedHosts.length > 0
|
|
567
|
+
? "host_target_mismatch"
|
|
568
|
+
: metadata.anomalyReason,
|
|
569
|
+
),
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
for (const item of controls) events.push(controlEvent(item.metadata, item.control));
|
|
574
|
+
|
|
575
|
+
const routingOwnsHost = activeEpochs.size > 0 && !completedClosing;
|
|
576
|
+
for (const host of hosts) {
|
|
577
|
+
if (usedHostPositions.has(host.metadata.position)) continue;
|
|
578
|
+
events.push(
|
|
579
|
+
hostEvent(
|
|
580
|
+
host,
|
|
581
|
+
host.correlationId || routingOwnsHost ? "anomaly" : "valid",
|
|
582
|
+
host.correlationId ? "correlation_unresolved" : routingOwnsHost ? "host_change_during_routing" : undefined,
|
|
583
|
+
),
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const scope = options.scope ?? "session";
|
|
588
|
+
const activeIds = activeEntryIds(options.branchEntries);
|
|
589
|
+
const scoped = events.filter((event) => scope !== "active-branch" || activeIds.has(event.entryId));
|
|
590
|
+
const summary = {
|
|
591
|
+
...(scoped
|
|
592
|
+
.filter((event) => event.classification === "semantic-switch")
|
|
593
|
+
.sort((left, right) => left.representativePosition - right.representativePosition)
|
|
594
|
+
.at(-1)?.id
|
|
595
|
+
? {
|
|
596
|
+
latestSemanticEventId: scoped
|
|
597
|
+
.filter((event) => event.classification === "semantic-switch")
|
|
598
|
+
.sort((left, right) => left.representativePosition - right.representativePosition)
|
|
599
|
+
.at(-1)?.id,
|
|
600
|
+
}
|
|
601
|
+
: {}),
|
|
602
|
+
...(scoped
|
|
603
|
+
.filter((event) => event.outcome === "completed" && event.classification !== "control-only")
|
|
604
|
+
.sort((left, right) => left.representativePosition - right.representativePosition)
|
|
605
|
+
.at(-1)?.id
|
|
606
|
+
? {
|
|
607
|
+
latestCompletedEventId: scoped
|
|
608
|
+
.filter((event) => event.outcome === "completed" && event.classification !== "control-only")
|
|
609
|
+
.sort((left, right) => left.representativePosition - right.representativePosition)
|
|
610
|
+
.at(-1)?.id,
|
|
611
|
+
}
|
|
612
|
+
: {}),
|
|
613
|
+
unresolvedCount: scoped.filter((event) => event.outcome === "unresolved").length,
|
|
614
|
+
anomalyCount: scoped.filter((event) => event.integrity === "anomaly").length,
|
|
615
|
+
};
|
|
616
|
+
const visible = (options.anomaliesOnly ? scoped.filter((event) => event.integrity === "anomaly") : scoped)
|
|
617
|
+
.sort((left, right) => right.representativePosition - left.representativePosition)
|
|
618
|
+
.slice(0, normalizeLimit(options.limit))
|
|
619
|
+
.map(({ representativePosition: _representativePosition, ...event }) => event);
|
|
620
|
+
|
|
621
|
+
return { current: currentState(options.current), summary, events: visible };
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
export function readCognitiveRoutingHistory(
|
|
625
|
+
context: CognitiveRoutingHistoryContext | undefined,
|
|
626
|
+
options: CognitiveRoutingHistoryOptions = {},
|
|
627
|
+
): CognitiveRoutingHistoryResult {
|
|
628
|
+
let entries: readonly unknown[] = [];
|
|
629
|
+
let branchEntries: readonly unknown[] = [];
|
|
630
|
+
try {
|
|
631
|
+
entries = context?.sessionManager?.getEntries?.() ?? [];
|
|
632
|
+
branchEntries = context?.sessionManager?.getBranch?.() ?? entries;
|
|
633
|
+
} catch {
|
|
634
|
+
entries = [];
|
|
635
|
+
branchEntries = [];
|
|
636
|
+
}
|
|
637
|
+
return projectCognitiveRoutingHistory(entries, { ...options, branchEntries });
|
|
638
|
+
}
|