ctxtop 0.0.0-stage → 4.1.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/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +166 -0
- package/LICENSE +27 -0
- package/README.md +350 -2
- package/SECURITY.md +22 -0
- package/agents/ah-flash-scout.md +25 -0
- package/assets/advanced_view_loaded_420px.png +0 -0
- package/assets/hero-demo.svg +188 -0
- package/assets/logo.svg +19 -0
- package/assets/simple_view_lean_420px.png +0 -0
- package/assets/simple_view_loaded_420px.png +0 -0
- package/assets/social-preview.svg +72 -0
- package/assets/zone_c_step3_shield_full.png +0 -0
- package/bin/ah-statusline.mjs +175 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1799 -0
- package/commands/cost-radar.md +11 -0
- package/commands/evolve.md +10 -0
- package/commands/handoff.md +10 -0
- package/commands/horizon.md +10 -0
- package/commands/inspect.md +10 -0
- package/commands/precheck.md +10 -0
- package/config/default_flightcheck.json +133 -0
- package/config/modelCatalog.json +785 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +577 -0
- package/hooks/handlers/config-change.mjs +402 -0
- package/hooks/handlers/hookIo.mjs +311 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-model-switch.mjs +111 -0
- package/hooks/handlers/post-tool-use.mjs +366 -0
- package/hooks/handlers/pre-model-switch.mjs +1048 -0
- package/hooks/handlers/pre-tool-use.mjs +632 -0
- package/hooks/handlers/stop-gate.mjs +526 -0
- package/hooks/handlers/user-prompt-submit.mjs +367 -0
- package/hooks/hooks.json +131 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +25 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +726 -0
- package/hooks/session_start.mjs +24 -0
- package/hooks/stop.mjs +23 -0
- package/hooks.json +56 -0
- package/package.json +110 -4
- package/packages/antigravity/package.json +14 -0
- package/packages/antigravity/src/index.mjs +1 -0
- package/packages/claude-code/package.json +14 -0
- package/packages/claude-code/src/index.mjs +2 -0
- package/packages/core/package.json +14 -0
- package/packages/core/src/index.mjs +1 -0
- package/packages/engine-node/package.json +14 -0
- package/packages/engine-node/src/index.mjs +1 -0
- package/packages/mcp/package.json +17 -0
- package/packages/mcp/src/server.mjs +396 -0
- package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
- package/packages/pro/README-SIGNING.md +30 -0
- package/packages/pro/package.json +16 -0
- package/packages/pro/src/index.mjs +3 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -0
- package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
- package/plugin.json +36 -0
- package/rules/AGENTS.md +10 -0
- package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
- package/sidecars/horizon-daemon/server.mjs +2038 -0
- package/sidecars/horizon-daemon/sidecar.json +19 -0
- package/skills/cost-radar/SKILL.md +21 -0
- package/skills/evolve/SKILL.md +21 -0
- package/skills/handoff/SKILL.md +50 -0
- package/skills/horizon/SKILL.md +17 -0
- package/skills/inspect/SKILL.md +18 -0
- package/skills/precheck/SKILL.md +44 -0
- package/src/adapters/antigravity/index.mjs +109 -0
- package/src/adapters/claude-code/index.mjs +194 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +888 -0
- package/src/cli/init.mjs +425 -0
- package/src/cli/postmortem.mjs +515 -0
- package/src/coach/cacheGravityRouter.mjs +12 -0
- package/src/config/modelCatalog.json +704 -0
- package/src/config/platformQuirks.json +19 -0
- package/src/core/action_queue.mjs +264 -0
- package/src/core/ag_bridge.mjs +346 -0
- package/src/core/atomic_io.mjs +124 -0
- package/src/core/baseline.mjs +607 -0
- package/src/core/blindspot_sensors.mjs +399 -0
- package/src/core/bucket_b_runtime.mjs +1200 -0
- package/src/core/classifier.mjs +1757 -0
- package/src/core/coach/circuit_breakers.mjs +650 -0
- package/src/core/coach/contradiction_auditor.mjs +239 -0
- package/src/core/coach/effort_advisor.mjs +143 -0
- package/src/core/coach/handoff_hydrator.mjs +483 -0
- package/src/core/coach/progressive_checkpointer.mjs +551 -0
- package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
- package/src/core/coach/reflector_queue.mjs +505 -0
- package/src/core/coach/saved_quota_ledger.mjs +615 -0
- package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
- package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
- package/src/core/coach/task_quota_router.mjs +955 -0
- package/src/core/coach/turn_zero_gate.mjs +574 -0
- package/src/core/compaction_coach.mjs +1126 -0
- package/src/core/config.mjs +214 -0
- package/src/core/constants.mjs +1987 -0
- package/src/core/detectors_det01_to_16.mjs +1523 -0
- package/src/core/doc_indexer.mjs +338 -0
- package/src/core/episodic_memory.mjs +135 -0
- package/src/core/fts5_output_compressor.mjs +359 -0
- package/src/core/git_ledger.mjs +189 -0
- package/src/core/handoff_audit.mjs +558 -0
- package/src/core/history.mjs +221 -0
- package/src/core/hook_diagnostics.mjs +158 -0
- package/src/core/horizon_classifier.mjs +326 -0
- package/src/core/horizon_config.mjs +802 -0
- package/src/core/host_watchdog.mjs +200 -0
- package/src/core/layer5_hygiene.mjs +1084 -0
- package/src/core/log_resolver.mjs +231 -0
- package/src/core/mcp_auditor.mjs +138 -0
- package/src/core/micro_skills_store.mjs +91 -0
- package/src/core/offenders.mjs +161 -0
- package/src/core/parser.mjs +1997 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +311 -0
- package/src/core/prefix_cache_shield.mjs +386 -0
- package/src/core/pro_license.mjs +385 -0
- package/src/core/prompt_rubric.mjs +518 -0
- package/src/core/pruner.mjs +433 -0
- package/src/core/quota.mjs +1860 -0
- package/src/core/rsi_instinct_engine.mjs +1197 -0
- package/src/core/rsl_clash_auditor.mjs +423 -0
- package/src/core/rsl_distiller.mjs +372 -0
- package/src/core/rsl_ledger.mjs +969 -0
- package/src/core/rsl_triage.mjs +909 -0
- package/src/core/sanitizer.mjs +121 -0
- package/src/core/session_auditor.mjs +658 -0
- package/src/core/session_forensics.mjs +303 -0
- package/src/core/session_timeline.mjs +358 -0
- package/src/core/settings.mjs +99 -0
- package/src/core/shared/detectors.mjs +1342 -0
- package/src/core/shared/fnv1a64.mjs +85 -0
- package/src/core/shared/handoff_schema.mjs +123 -0
- package/src/core/shared/index.mjs +5 -0
- package/src/core/shared/ipi_sanitizer.mjs +165 -0
- package/src/core/shared/micro_skills.mjs +1 -0
- package/src/core/shared/plugin_validator.mjs +202 -0
- package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
- package/src/core/shield_manager.mjs +1417 -0
- package/src/core/sidecar_store.mjs +302 -0
- package/src/core/sprint_metrics.mjs +366 -0
- package/src/core/sqlite_client.mjs +283 -0
- package/src/core/storage_doctor.mjs +358 -0
- package/src/core/subagents.mjs +609 -0
- package/src/core/swarm_topology.mjs +360 -0
- package/src/core/task_quota_router.mjs +1656 -0
- package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
- package/src/core/telemetry/scope_lock_registry.mjs +578 -0
- package/src/core/telemetry/shadow_file_detector.mjs +291 -0
- package/src/core/telemetry/trajectory_watcher.mjs +259 -0
- package/src/core/timeline_svg.mjs +236 -0
- package/src/core/tool_surgeon.mjs +305 -0
- package/src/core/trajectory_stitcher.mjs +645 -0
- package/src/core/u_shaped_optimizer.mjs +789 -0
- package/src/core/wal_hygiene.mjs +233 -0
- package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/src/core-isomorphic/catalog/index.mjs +1 -0
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/src/core-isomorphic/coach/microSkills.mjs +1 -0
- package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +606 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +1268 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/src/core-isomorphic/control/callTree.mjs +338 -0
- package/src/core-isomorphic/control/coachLatches.mjs +126 -0
- package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/src/core-isomorphic/control/index.mjs +49 -0
- package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/src/core-isomorphic/diagnostics/index.mjs +9 -0
- package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +369 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/src/core-isomorphic/economics/index.mjs +47 -0
- package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/src/core-isomorphic/index.js +27 -0
- package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1156 -0
- package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1237 -0
- package/src/core-isomorphic/ui/modViews.mjs +1504 -0
- package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/src/core-isomorphic/ustf/index.mjs +7 -0
- package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/src/core-isomorphic/ustf/schema.mjs +186 -0
- package/src/core-isomorphic/utils/runtime.mjs +962 -0
- package/src/demo/synthetic_states.mjs +661 -0
- package/src/diagnostics/bugReportGenerator.mjs +253 -0
- package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
- package/src/diagnostics/doctorEngine.mjs +295 -0
- package/src/engine-node/db/index.mjs +58 -0
- package/src/engine-node/db/jsonl_fallback.mjs +184 -0
- package/src/engine-node/db/probe.mjs +342 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +12 -0
- package/src/engine-node/ipc/client.mjs +447 -0
- package/src/engine-node/ipc/index.mjs +22 -0
- package/src/engine-node/ipc/protocol.mjs +177 -0
- package/src/engine-node/ipc/server.mjs +445 -0
- package/src/engine-node/offloader/index.mjs +6 -0
- package/src/engine-node/offloader/tool_offloader.mjs +241 -0
- package/src/engine-node/sensors/blindspots.mjs +376 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/antigravity_rsl_hook.mjs +250 -0
- package/src/hooks/claude_rsl_hook.mjs +288 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +611 -0
- package/src/hooks/pre_invocation.mjs +1725 -0
- package/src/hooks/session_start.mjs +199 -0
- package/src/shims/agy_cli_compressor.mjs +705 -0
- package/src/shims/agy_git_guard.mjs +749 -0
- package/src/shims/ah_log_query.mjs +39 -0
- package/src/ui/costRadarSvg.mjs +6 -0
- package/src/ui/horizonGrid.mjs +1 -0
- package/src/ui/horizonPanelHtml.mjs +1 -0
- package/src/ui/horizon_panel.css +167 -0
- package/src/ui/horizon_panel.js +212 -0
- package/src/ui/modViews.mjs +1 -0
- package/src/utils/modelSwitchTelemetry.mjs +308 -0
- package/src/utils/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/utils/sessionCalibration.mjs +657 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +12397 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/ustf/normalizer.mjs
|
|
3
|
+
* Universal Session Trajectory Format (USTF v1) Dual-Harness Normalizer.
|
|
4
|
+
*
|
|
5
|
+
* Normalizes raw session events, steps, DAGs, and transcripts from Google Antigravity
|
|
6
|
+
* and Claude Code into canonical USTF v1 trajectory objects.
|
|
7
|
+
*
|
|
8
|
+
* Pure JS / ESM with strictly 0 node:* imports adhering to Claude Mod Laws 1–5.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
USTF_SCHEMA_VERSION,
|
|
13
|
+
USTF_SCHEMA_URI,
|
|
14
|
+
validateUstfTrajectory
|
|
15
|
+
} from './schema.mjs';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates an empty canonical USTF v1 session object.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} sessionId
|
|
21
|
+
* @param {'antigravity' | 'claude-code' | 'cursor'} [harness='claude-code']
|
|
22
|
+
* @param {object} [options={}]
|
|
23
|
+
* @returns {object}
|
|
24
|
+
*/
|
|
25
|
+
export function createEmptyUstfSession(sessionId = 'default-session', harness = 'claude-code', options = {}) {
|
|
26
|
+
const now = options.nowMs || Date.now();
|
|
27
|
+
return {
|
|
28
|
+
$schema: USTF_SCHEMA_URI,
|
|
29
|
+
version: USTF_SCHEMA_VERSION,
|
|
30
|
+
sessionId: String(sessionId),
|
|
31
|
+
harness,
|
|
32
|
+
streamKey: `${sessionId}::${options.agentId || 'main'}`,
|
|
33
|
+
modelId: options.modelId || 'unknown',
|
|
34
|
+
startTimeMs: options.startTimeMs || now,
|
|
35
|
+
endTimeMs: options.endTimeMs || null,
|
|
36
|
+
events: [],
|
|
37
|
+
spans: [],
|
|
38
|
+
usage: {
|
|
39
|
+
uncached: 0,
|
|
40
|
+
cacheWrite5m: 0,
|
|
41
|
+
cacheWrite1h: 0,
|
|
42
|
+
cacheRead: 0,
|
|
43
|
+
output: 0,
|
|
44
|
+
thinking: 0,
|
|
45
|
+
costUSD: 0
|
|
46
|
+
},
|
|
47
|
+
worktree: {
|
|
48
|
+
path: options.worktreePath || '',
|
|
49
|
+
branch: options.branch || '',
|
|
50
|
+
isDetached: Boolean(options.isDetached)
|
|
51
|
+
},
|
|
52
|
+
rewindBranches: [],
|
|
53
|
+
metadata: { ...options.metadata }
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Normalizes a raw usage object from various host shapes into canonical USTF usage.
|
|
59
|
+
*
|
|
60
|
+
* @param {object} [raw={}]
|
|
61
|
+
* @param {object} [rates={}]
|
|
62
|
+
* @returns {object}
|
|
63
|
+
*/
|
|
64
|
+
export function normalizeUsage(raw = {}, rates = {}) {
|
|
65
|
+
if (!raw || typeof raw !== 'object') {
|
|
66
|
+
return {
|
|
67
|
+
uncached: 0,
|
|
68
|
+
cacheWrite5m: 0,
|
|
69
|
+
cacheWrite1h: 0,
|
|
70
|
+
cacheRead: 0,
|
|
71
|
+
output: 0,
|
|
72
|
+
thinking: 0,
|
|
73
|
+
costUSD: 0
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const cacheCreation = raw.cache_creation || raw.cacheCreation || null;
|
|
78
|
+
const cacheWrite5m = Number(
|
|
79
|
+
raw.cacheWrite5m ??
|
|
80
|
+
cacheCreation?.ephemeral_5m_input_tokens ??
|
|
81
|
+
raw.cache_creation_input_tokens ??
|
|
82
|
+
raw.cacheCreationInputTokens ??
|
|
83
|
+
0
|
|
84
|
+
);
|
|
85
|
+
const cacheWrite1h = Number(
|
|
86
|
+
raw.cacheWrite1h ??
|
|
87
|
+
cacheCreation?.ephemeral_1h_input_tokens ??
|
|
88
|
+
0
|
|
89
|
+
);
|
|
90
|
+
const cacheRead = Number(
|
|
91
|
+
raw.cacheRead ??
|
|
92
|
+
raw.cache_read_input_tokens ??
|
|
93
|
+
raw.cacheReadInputTokens ??
|
|
94
|
+
raw.cached_tokens ??
|
|
95
|
+
raw.cachedTokens ??
|
|
96
|
+
0
|
|
97
|
+
);
|
|
98
|
+
const uncached = Number(
|
|
99
|
+
raw.uncached ??
|
|
100
|
+
raw.input_tokens ??
|
|
101
|
+
raw.inputTokens ??
|
|
102
|
+
raw.prompt_tokens ??
|
|
103
|
+
raw.promptTokens ??
|
|
104
|
+
0
|
|
105
|
+
);
|
|
106
|
+
const output = Number(
|
|
107
|
+
raw.output ??
|
|
108
|
+
raw.output_tokens ??
|
|
109
|
+
raw.outputTokens ??
|
|
110
|
+
0
|
|
111
|
+
);
|
|
112
|
+
const thinking = Number(
|
|
113
|
+
raw.thinking ??
|
|
114
|
+
raw.thinking_tokens ??
|
|
115
|
+
raw.thinkingTokens ??
|
|
116
|
+
0
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
let costUSD = Number(raw.costUSD ?? raw.cost_usd ?? raw.costUsd ?? 0);
|
|
120
|
+
if (costUSD === 0 && rates && typeof rates === 'object') {
|
|
121
|
+
const inputRate = Number(rates.inputRate ?? rates.input ?? 3.0) / 1_000_000;
|
|
122
|
+
const cacheWriteRate = Number(rates.cacheWriteRate ?? rates.cacheWrite ?? 3.75) / 1_000_000;
|
|
123
|
+
const cacheReadRate = Number(rates.cacheReadRate ?? rates.cacheRead ?? 0.30) / 1_000_000;
|
|
124
|
+
const outputRate = Number(rates.outputRate ?? rates.output ?? 15.0) / 1_000_000;
|
|
125
|
+
costUSD =
|
|
126
|
+
uncached * inputRate +
|
|
127
|
+
(cacheWrite5m + cacheWrite1h) * cacheWriteRate +
|
|
128
|
+
cacheRead * cacheReadRate +
|
|
129
|
+
output * outputRate;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
uncached: Math.max(0, uncached),
|
|
134
|
+
cacheWrite5m: Math.max(0, cacheWrite5m),
|
|
135
|
+
cacheWrite1h: Math.max(0, cacheWrite1h),
|
|
136
|
+
cacheRead: Math.max(0, cacheRead),
|
|
137
|
+
output: Math.max(0, output),
|
|
138
|
+
thinking: Math.max(0, thinking),
|
|
139
|
+
costUSD: Math.max(0, Number(costUSD.toFixed(6)))
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Normalizes Antigravity session steps and transcripts into a USTF v1 trajectory.
|
|
145
|
+
*
|
|
146
|
+
* @param {object} raw - Antigravity session data (steps array, transcript lines, metadata)
|
|
147
|
+
* @param {object} [options={}]
|
|
148
|
+
* @returns {object}
|
|
149
|
+
*/
|
|
150
|
+
export function normalizeAntigravitySession(raw = {}, options = {}) {
|
|
151
|
+
const sessionId = raw.sessionId || raw.session_id || options.sessionId || 'antigravity-session';
|
|
152
|
+
const session = createEmptyUstfSession(sessionId, 'antigravity', {
|
|
153
|
+
...options,
|
|
154
|
+
modelId: raw.modelId || options.modelId || 'gemini-2-5-pro'
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const rawSteps = Array.isArray(raw.steps)
|
|
158
|
+
? raw.steps
|
|
159
|
+
: Array.isArray(raw)
|
|
160
|
+
? raw
|
|
161
|
+
: [];
|
|
162
|
+
|
|
163
|
+
let currentTurnId = null;
|
|
164
|
+
let turnIndex = 0;
|
|
165
|
+
let currentTurnStartMs = session.startTimeMs;
|
|
166
|
+
|
|
167
|
+
for (let i = 0; i < rawSteps.length; i++) {
|
|
168
|
+
const step = rawSteps[i];
|
|
169
|
+
if (!step || typeof step !== 'object') continue;
|
|
170
|
+
|
|
171
|
+
const stepType = String(step.type || step.stepType || step.step_type || 'STEP').toUpperCase();
|
|
172
|
+
const timestampMs = Number(step.createdAt || step.timestampMs || step.timestamp || (session.startTimeMs + i * 1000));
|
|
173
|
+
const content = String(step.content || step.extractedText || '');
|
|
174
|
+
|
|
175
|
+
if (stepType === 'USER_INPUT' || stepType === 'USER') {
|
|
176
|
+
if (currentTurnId) {
|
|
177
|
+
session.events.push({
|
|
178
|
+
eventId: `turn-comp-${turnIndex}`,
|
|
179
|
+
type: 'turn.complete',
|
|
180
|
+
turnId: currentTurnId,
|
|
181
|
+
timestampMs: timestampMs - 1
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
turnIndex++;
|
|
185
|
+
currentTurnId = `turn-${turnIndex}`;
|
|
186
|
+
currentTurnStartMs = timestampMs;
|
|
187
|
+
|
|
188
|
+
session.events.push({
|
|
189
|
+
eventId: `turn-start-${turnIndex}`,
|
|
190
|
+
type: 'turn.start',
|
|
191
|
+
turnId: currentTurnId,
|
|
192
|
+
timestampMs,
|
|
193
|
+
payload: { userPrompt: content }
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
session.spans.push({
|
|
197
|
+
spanId: currentTurnId,
|
|
198
|
+
parentSpanId: null,
|
|
199
|
+
kind: 'turn',
|
|
200
|
+
name: `Turn ${turnIndex}`,
|
|
201
|
+
startTimeMs: currentTurnStartMs,
|
|
202
|
+
durationMs: 0,
|
|
203
|
+
status: 'ok',
|
|
204
|
+
attributes: { userPrompt: content }
|
|
205
|
+
});
|
|
206
|
+
} else if (stepType === 'TOOL_CALL' || stepType === 'CALL') {
|
|
207
|
+
const toolName = step.toolName || step.tool_name || 'Tool';
|
|
208
|
+
const toolInput = step.toolInput || step.tool_input || {};
|
|
209
|
+
const spanId = `span-tool-${i}`;
|
|
210
|
+
|
|
211
|
+
session.events.push({
|
|
212
|
+
eventId: `ev-tool-call-${i}`,
|
|
213
|
+
type: 'tool.call',
|
|
214
|
+
turnId: currentTurnId || `turn-${turnIndex}`,
|
|
215
|
+
timestampMs,
|
|
216
|
+
payload: { toolName, toolInput, spanId }
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
session.spans.push({
|
|
220
|
+
spanId,
|
|
221
|
+
parentSpanId: currentTurnId,
|
|
222
|
+
kind: 'tool',
|
|
223
|
+
name: toolName,
|
|
224
|
+
startTimeMs: timestampMs,
|
|
225
|
+
durationMs: Number(step.durationMs || 50),
|
|
226
|
+
status: step.status === 'ERROR' ? 'error' : 'ok',
|
|
227
|
+
attributes: { toolName, input: toolInput }
|
|
228
|
+
});
|
|
229
|
+
} else if (stepType === 'TOOL_RESULT' || stepType === 'RESULT') {
|
|
230
|
+
session.events.push({
|
|
231
|
+
eventId: `ev-tool-result-${i}`,
|
|
232
|
+
type: 'tool.result',
|
|
233
|
+
turnId: currentTurnId || `turn-${turnIndex}`,
|
|
234
|
+
timestampMs,
|
|
235
|
+
payload: {
|
|
236
|
+
toolName: step.toolName || step.tool_name,
|
|
237
|
+
result: content.slice(0, 1000)
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
} else {
|
|
241
|
+
session.events.push({
|
|
242
|
+
eventId: `ev-turn-step-${i}`,
|
|
243
|
+
type: 'turn.step',
|
|
244
|
+
turnId: currentTurnId || `turn-${turnIndex}`,
|
|
245
|
+
timestampMs,
|
|
246
|
+
payload: { stepType, content: content.slice(0, 1000) }
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (step.usage) {
|
|
251
|
+
const u = normalizeUsage(step.usage);
|
|
252
|
+
session.usage.uncached += u.uncached;
|
|
253
|
+
session.usage.cacheWrite5m += u.cacheWrite5m;
|
|
254
|
+
session.usage.cacheWrite1h += u.cacheWrite1h;
|
|
255
|
+
session.usage.cacheRead += u.cacheRead;
|
|
256
|
+
session.usage.output += u.output;
|
|
257
|
+
session.usage.thinking += u.thinking;
|
|
258
|
+
session.usage.costUSD += u.costUSD;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (currentTurnId) {
|
|
263
|
+
session.events.push({
|
|
264
|
+
eventId: `turn-comp-${turnIndex}`,
|
|
265
|
+
type: 'turn.complete',
|
|
266
|
+
turnId: currentTurnId,
|
|
267
|
+
timestampMs: session.startTimeMs + rawSteps.length * 1000
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return session;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Normalizes Claude Code DAG entries / JSONL transcript into a USTF v1 trajectory.
|
|
276
|
+
*
|
|
277
|
+
* @param {string | object[] | object} raw - JSONL content, array of JSONL entries, or session object
|
|
278
|
+
* @param {object} [options={}]
|
|
279
|
+
* @returns {object}
|
|
280
|
+
*/
|
|
281
|
+
export function normalizeClaudeCodeSession(raw = {}, options = {}) {
|
|
282
|
+
let entries = [];
|
|
283
|
+
let sessionId = options.sessionId || 'claude-code-session';
|
|
284
|
+
|
|
285
|
+
if (typeof raw === 'string') {
|
|
286
|
+
entries = raw
|
|
287
|
+
.split(/\r?\n/)
|
|
288
|
+
.filter(Boolean)
|
|
289
|
+
.map((line) => {
|
|
290
|
+
try {
|
|
291
|
+
return JSON.parse(line);
|
|
292
|
+
} catch {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
.filter(Boolean);
|
|
297
|
+
} else if (Array.isArray(raw)) {
|
|
298
|
+
entries = raw;
|
|
299
|
+
} else if (raw && typeof raw === 'object') {
|
|
300
|
+
sessionId = raw.sessionId || raw.session_id || sessionId;
|
|
301
|
+
if (Array.isArray(raw.entries)) entries = raw.entries;
|
|
302
|
+
else if (Array.isArray(raw.events)) entries = raw.events;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const session = createEmptyUstfSession(sessionId, 'claude-code', {
|
|
306
|
+
...options,
|
|
307
|
+
modelId: options.modelId || 'claude-sonnet-5-5'
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
const nodeMap = new Map();
|
|
311
|
+
const childrenMap = new Map();
|
|
312
|
+
let explicitLeafUuid = null;
|
|
313
|
+
let lastEntryUuid = null;
|
|
314
|
+
|
|
315
|
+
for (let i = 0; i < entries.length; i++) {
|
|
316
|
+
const e = entries[i];
|
|
317
|
+
if (!e || typeof e !== 'object') continue;
|
|
318
|
+
|
|
319
|
+
if (e.leafUuid) explicitLeafUuid = e.leafUuid;
|
|
320
|
+
const uuid = e.uuid || e.id || `node-${i}`;
|
|
321
|
+
const parentUuid = e.parentUuid !== undefined ? e.parentUuid : lastEntryUuid;
|
|
322
|
+
lastEntryUuid = uuid;
|
|
323
|
+
|
|
324
|
+
nodeMap.set(uuid, { ...e, uuid, parentUuid, rawIndex: i });
|
|
325
|
+
const pKey = parentUuid || '__ROOT__';
|
|
326
|
+
if (!childrenMap.has(pKey)) childrenMap.set(pKey, []);
|
|
327
|
+
childrenMap.get(pKey).push(uuid);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const activeTipUuid = explicitLeafUuid && nodeMap.has(explicitLeafUuid) ? explicitLeafUuid : lastEntryUuid;
|
|
331
|
+
const activeChain = [];
|
|
332
|
+
const activeUuidSet = new Set();
|
|
333
|
+
let curr = activeTipUuid;
|
|
334
|
+
|
|
335
|
+
while (curr && nodeMap.has(curr) && !activeUuidSet.has(curr)) {
|
|
336
|
+
activeUuidSet.add(curr);
|
|
337
|
+
activeChain.push(nodeMap.get(curr));
|
|
338
|
+
curr = nodeMap.get(curr).parentUuid;
|
|
339
|
+
}
|
|
340
|
+
activeChain.reverse();
|
|
341
|
+
|
|
342
|
+
// Find rewind branches
|
|
343
|
+
for (const node of activeChain) {
|
|
344
|
+
const siblings = childrenMap.get(node.uuid) || [];
|
|
345
|
+
for (const childUuid of siblings) {
|
|
346
|
+
if (!activeUuidSet.has(childUuid)) {
|
|
347
|
+
session.rewindBranches.push({
|
|
348
|
+
forkParentUuid: node.uuid,
|
|
349
|
+
abandonedTipUuid: childUuid
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
let turnIndex = 0;
|
|
356
|
+
let currentTurnId = null;
|
|
357
|
+
|
|
358
|
+
for (let i = 0; i < activeChain.length; i++) {
|
|
359
|
+
const node = activeChain[i];
|
|
360
|
+
const timestampMs = Number(node.timestamp || node.createdAt || (session.startTimeMs + i * 1000));
|
|
361
|
+
const role = node.role || node.type || 'message';
|
|
362
|
+
|
|
363
|
+
if (node.type === 'file-history-snapshot' || node.snapshot) {
|
|
364
|
+
session.events.push({
|
|
365
|
+
eventId: `ev-compact-${i}`,
|
|
366
|
+
type: 'compact.lifecycle',
|
|
367
|
+
timestampMs,
|
|
368
|
+
payload: { snapshotFiles: Object.keys(node.snapshot?.trackedFileBackups || node.files || {}) }
|
|
369
|
+
});
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (role === 'user' || node.type === 'user_input') {
|
|
374
|
+
if (currentTurnId) {
|
|
375
|
+
session.events.push({
|
|
376
|
+
eventId: `ev-turn-comp-${turnIndex}`,
|
|
377
|
+
type: 'turn.complete',
|
|
378
|
+
turnId: currentTurnId,
|
|
379
|
+
timestampMs: timestampMs - 1
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
turnIndex++;
|
|
383
|
+
currentTurnId = `turn-${turnIndex}`;
|
|
384
|
+
|
|
385
|
+
const text = typeof node.content === 'string'
|
|
386
|
+
? node.content
|
|
387
|
+
: Array.isArray(node.content)
|
|
388
|
+
? node.content.map((c) => c.text || '').join('')
|
|
389
|
+
: '';
|
|
390
|
+
|
|
391
|
+
session.events.push({
|
|
392
|
+
eventId: `ev-turn-start-${turnIndex}`,
|
|
393
|
+
type: 'turn.start',
|
|
394
|
+
turnId: currentTurnId,
|
|
395
|
+
timestampMs,
|
|
396
|
+
payload: { prompt: text }
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
session.spans.push({
|
|
400
|
+
spanId: currentTurnId,
|
|
401
|
+
parentSpanId: null,
|
|
402
|
+
kind: 'turn',
|
|
403
|
+
name: `Turn ${turnIndex}`,
|
|
404
|
+
startTimeMs: timestampMs,
|
|
405
|
+
durationMs: 0,
|
|
406
|
+
status: 'ok',
|
|
407
|
+
attributes: { prompt: text }
|
|
408
|
+
});
|
|
409
|
+
} else {
|
|
410
|
+
// Assistant / Tool turns
|
|
411
|
+
const contentBlocks = Array.isArray(node.content) ? node.content : [];
|
|
412
|
+
for (const block of contentBlocks) {
|
|
413
|
+
if (block.type === 'tool_use') {
|
|
414
|
+
const spanId = `span-tool-${block.id || i}`;
|
|
415
|
+
session.events.push({
|
|
416
|
+
eventId: `ev-tool-call-${block.id || i}`,
|
|
417
|
+
type: 'tool.call',
|
|
418
|
+
turnId: currentTurnId || `turn-${turnIndex}`,
|
|
419
|
+
timestampMs,
|
|
420
|
+
payload: { toolName: block.name, toolInput: block.input, spanId }
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
session.spans.push({
|
|
424
|
+
spanId,
|
|
425
|
+
parentSpanId: currentTurnId,
|
|
426
|
+
kind: 'tool',
|
|
427
|
+
name: block.name,
|
|
428
|
+
startTimeMs: timestampMs,
|
|
429
|
+
durationMs: 50,
|
|
430
|
+
status: 'ok',
|
|
431
|
+
attributes: { toolName: block.name, input: block.input }
|
|
432
|
+
});
|
|
433
|
+
} else if (block.type === 'tool_result') {
|
|
434
|
+
session.events.push({
|
|
435
|
+
eventId: `ev-tool-result-${block.tool_use_id || i}`,
|
|
436
|
+
type: 'tool.result',
|
|
437
|
+
turnId: currentTurnId || `turn-${turnIndex}`,
|
|
438
|
+
timestampMs,
|
|
439
|
+
payload: { result: String(block.content || '').slice(0, 1000) }
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
session.events.push({
|
|
445
|
+
eventId: `ev-step-${i}`,
|
|
446
|
+
type: 'turn.step',
|
|
447
|
+
turnId: currentTurnId || `turn-${turnIndex}`,
|
|
448
|
+
timestampMs,
|
|
449
|
+
payload: { role: 'assistant', messageId: node.message?.id || node.id }
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const rawUsage = node.message?.usage || node.usage;
|
|
454
|
+
if (rawUsage) {
|
|
455
|
+
const u = normalizeUsage(rawUsage);
|
|
456
|
+
session.usage.uncached += u.uncached;
|
|
457
|
+
session.usage.cacheWrite5m += u.cacheWrite5m;
|
|
458
|
+
session.usage.cacheWrite1h += u.cacheWrite1h;
|
|
459
|
+
session.usage.cacheRead += u.cacheRead;
|
|
460
|
+
session.usage.output += u.output;
|
|
461
|
+
session.usage.thinking += u.thinking;
|
|
462
|
+
session.usage.costUSD += u.costUSD;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (currentTurnId) {
|
|
467
|
+
session.events.push({
|
|
468
|
+
eventId: `ev-turn-comp-${turnIndex}`,
|
|
469
|
+
type: 'turn.complete',
|
|
470
|
+
turnId: currentTurnId,
|
|
471
|
+
timestampMs: session.startTimeMs + activeChain.length * 1000
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
return session;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Auto-detects the host format and normalizes into a USTF v1 trajectory.
|
|
480
|
+
*
|
|
481
|
+
* @param {any} rawInput
|
|
482
|
+
* @param {object} [options={}]
|
|
483
|
+
* @returns {object}
|
|
484
|
+
*/
|
|
485
|
+
export function normalizeSessionTrajectory(rawInput, options = {}) {
|
|
486
|
+
if (!rawInput) {
|
|
487
|
+
return createEmptyUstfSession('empty-session', 'claude-code', options);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (typeof rawInput === 'string') {
|
|
491
|
+
return normalizeClaudeCodeSession(rawInput, options);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (rawInput.harness === 'antigravity' || Array.isArray(rawInput.steps)) {
|
|
495
|
+
return normalizeAntigravitySession(rawInput, options);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (rawInput.harness === 'claude-code' || rawInput.leafUuid || Array.isArray(rawInput.entries)) {
|
|
499
|
+
return normalizeClaudeCodeSession(rawInput, options);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Heuristic: check first item in array
|
|
503
|
+
if (Array.isArray(rawInput)) {
|
|
504
|
+
const first = rawInput[0];
|
|
505
|
+
if (first && (first.step_type || first.stepType)) {
|
|
506
|
+
return normalizeAntigravitySession(rawInput, options);
|
|
507
|
+
}
|
|
508
|
+
return normalizeClaudeCodeSession(rawInput, options);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return createEmptyUstfSession('unknown-session', 'claude-code', options);
|
|
512
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/ustf/schema.mjs
|
|
3
|
+
* Universal Session Trajectory Format (USTF v1) Schema & Validators.
|
|
4
|
+
*
|
|
5
|
+
* Cross-platform JSON schema representing agent sessions, turns, tool calls,
|
|
6
|
+
* costs, and execution spans across Google Antigravity and Claude Code.
|
|
7
|
+
*
|
|
8
|
+
* Pure JS / ESM with strictly 0 node:* imports adhering to Claude Mod Laws 1–5.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export const USTF_SCHEMA_VERSION = '1.0.0';
|
|
12
|
+
export const USTF_SCHEMA_URI = 'https://ctxtop.dev/schemas/ustf-v1.json';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 9 Canonical USTF v1 event types.
|
|
16
|
+
*/
|
|
17
|
+
export const USTF_EVENT_TYPES = Object.freeze([
|
|
18
|
+
'turn.start',
|
|
19
|
+
'turn.step',
|
|
20
|
+
'turn.complete',
|
|
21
|
+
'tool.call',
|
|
22
|
+
'tool.result',
|
|
23
|
+
'model.switch',
|
|
24
|
+
'compact.lifecycle',
|
|
25
|
+
'subagent.lifecycle',
|
|
26
|
+
'control.action'
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Canonical USTF v1 hierarchical call-tree span kinds.
|
|
31
|
+
*/
|
|
32
|
+
export const USTF_SPAN_KINDS = Object.freeze([
|
|
33
|
+
'turn',
|
|
34
|
+
'agent',
|
|
35
|
+
'tool',
|
|
36
|
+
'hook',
|
|
37
|
+
'llm'
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validates a USTF v1 usage accounting object.
|
|
42
|
+
*
|
|
43
|
+
* @param {object} usage
|
|
44
|
+
* @returns {{ valid: boolean, errors: string[] }}
|
|
45
|
+
*/
|
|
46
|
+
export function validateUstfUsage(usage) {
|
|
47
|
+
const errors = [];
|
|
48
|
+
if (!usage || typeof usage !== 'object' || Array.isArray(usage)) {
|
|
49
|
+
return { valid: false, errors: ['usage must be an object'] };
|
|
50
|
+
}
|
|
51
|
+
const numericFields = [
|
|
52
|
+
'uncached',
|
|
53
|
+
'cacheWrite5m',
|
|
54
|
+
'cacheWrite1h',
|
|
55
|
+
'cacheRead',
|
|
56
|
+
'output',
|
|
57
|
+
'thinking',
|
|
58
|
+
'costUSD'
|
|
59
|
+
];
|
|
60
|
+
for (const f of numericFields) {
|
|
61
|
+
if (usage[f] !== undefined && typeof usage[f] !== 'number') {
|
|
62
|
+
errors.push(`usage.${f} must be a number`);
|
|
63
|
+
} else if (usage[f] < 0) {
|
|
64
|
+
errors.push(`usage.${f} must be non-negative`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return { valid: errors.length === 0, errors };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Validates a USTF v1 execution span.
|
|
72
|
+
*
|
|
73
|
+
* @param {object} span
|
|
74
|
+
* @returns {{ valid: boolean, errors: string[] }}
|
|
75
|
+
*/
|
|
76
|
+
export function validateUstfSpan(span) {
|
|
77
|
+
const errors = [];
|
|
78
|
+
if (!span || typeof span !== 'object' || Array.isArray(span)) {
|
|
79
|
+
return { valid: false, errors: ['span must be an object'] };
|
|
80
|
+
}
|
|
81
|
+
if (!span.spanId || typeof span.spanId !== 'string') {
|
|
82
|
+
errors.push('spanId must be a non-empty string');
|
|
83
|
+
}
|
|
84
|
+
if (span.parentSpanId !== null && span.parentSpanId !== undefined && typeof span.parentSpanId !== 'string') {
|
|
85
|
+
errors.push('parentSpanId must be string or null');
|
|
86
|
+
}
|
|
87
|
+
if (!span.kind || !USTF_SPAN_KINDS.includes(span.kind)) {
|
|
88
|
+
errors.push(`kind must be one of: ${USTF_SPAN_KINDS.join(', ')}`);
|
|
89
|
+
}
|
|
90
|
+
if (!span.name || typeof span.name !== 'string') {
|
|
91
|
+
errors.push('name must be a non-empty string');
|
|
92
|
+
}
|
|
93
|
+
if (typeof span.startTimeMs !== 'number' || span.startTimeMs < 0) {
|
|
94
|
+
errors.push('startTimeMs must be a non-negative number');
|
|
95
|
+
}
|
|
96
|
+
if (span.durationMs !== undefined && span.durationMs !== null) {
|
|
97
|
+
if (typeof span.durationMs !== 'number' || span.durationMs < 0) {
|
|
98
|
+
errors.push('durationMs must be a non-negative number');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (span.status && !['ok', 'error', 'aborted'].includes(span.status)) {
|
|
102
|
+
errors.push('status must be one of: ok, error, aborted');
|
|
103
|
+
}
|
|
104
|
+
return { valid: errors.length === 0, errors };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Validates a USTF v1 event.
|
|
109
|
+
*
|
|
110
|
+
* @param {object} event
|
|
111
|
+
* @returns {{ valid: boolean, errors: string[] }}
|
|
112
|
+
*/
|
|
113
|
+
export function validateUstfEvent(event) {
|
|
114
|
+
const errors = [];
|
|
115
|
+
if (!event || typeof event !== 'object' || Array.isArray(event)) {
|
|
116
|
+
return { valid: false, errors: ['event must be an object'] };
|
|
117
|
+
}
|
|
118
|
+
if (!event.eventId || typeof event.eventId !== 'string') {
|
|
119
|
+
errors.push('eventId must be a non-empty string');
|
|
120
|
+
}
|
|
121
|
+
if (!event.type || !USTF_EVENT_TYPES.includes(event.type)) {
|
|
122
|
+
errors.push(`type must be one of: ${USTF_EVENT_TYPES.join(', ')}`);
|
|
123
|
+
}
|
|
124
|
+
if (typeof event.timestampMs !== 'number' || event.timestampMs < 0) {
|
|
125
|
+
errors.push('timestampMs must be a non-negative number');
|
|
126
|
+
}
|
|
127
|
+
return { valid: errors.length === 0, errors };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Validates a complete USTF v1 session trajectory.
|
|
132
|
+
*
|
|
133
|
+
* @param {object} trajectory
|
|
134
|
+
* @returns {{ valid: boolean, errors: string[] }}
|
|
135
|
+
*/
|
|
136
|
+
export function validateUstfTrajectory(trajectory) {
|
|
137
|
+
const errors = [];
|
|
138
|
+
if (!trajectory || typeof trajectory !== 'object' || Array.isArray(trajectory)) {
|
|
139
|
+
return { valid: false, errors: ['trajectory must be an object'] };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (trajectory.$schema && trajectory.$schema !== USTF_SCHEMA_URI) {
|
|
143
|
+
errors.push(`$schema must be "${USTF_SCHEMA_URI}"`);
|
|
144
|
+
}
|
|
145
|
+
if (trajectory.version && trajectory.version !== USTF_SCHEMA_VERSION) {
|
|
146
|
+
errors.push(`version must be "${USTF_SCHEMA_VERSION}"`);
|
|
147
|
+
}
|
|
148
|
+
if (!trajectory.sessionId || typeof trajectory.sessionId !== 'string') {
|
|
149
|
+
errors.push('sessionId must be a non-empty string');
|
|
150
|
+
}
|
|
151
|
+
if (!trajectory.harness || !['antigravity', 'claude-code', 'cursor'].includes(trajectory.harness)) {
|
|
152
|
+
errors.push('harness must be "antigravity", "claude-code", or "cursor"');
|
|
153
|
+
}
|
|
154
|
+
if (!Array.isArray(trajectory.events)) {
|
|
155
|
+
errors.push('events must be an array');
|
|
156
|
+
} else {
|
|
157
|
+
for (let i = 0; i < trajectory.events.length; i++) {
|
|
158
|
+
const evCheck = validateUstfEvent(trajectory.events[i]);
|
|
159
|
+
if (!evCheck.valid) {
|
|
160
|
+
errors.push(`events[${i}]: ${evCheck.errors.join('; ')}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (trajectory.spans !== undefined) {
|
|
166
|
+
if (!Array.isArray(trajectory.spans)) {
|
|
167
|
+
errors.push('spans must be an array');
|
|
168
|
+
} else {
|
|
169
|
+
for (let i = 0; i < trajectory.spans.length; i++) {
|
|
170
|
+
const spanCheck = validateUstfSpan(trajectory.spans[i]);
|
|
171
|
+
if (!spanCheck.valid) {
|
|
172
|
+
errors.push(`spans[${i}]: ${spanCheck.errors.join('; ')}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (trajectory.usage) {
|
|
179
|
+
const usageCheck = validateUstfUsage(trajectory.usage);
|
|
180
|
+
if (!usageCheck.valid) {
|
|
181
|
+
errors.push(`usage: ${usageCheck.errors.join('; ')}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { valid: errors.length === 0, errors };
|
|
186
|
+
}
|