@danypops/jittor 0.16.3 → 0.18.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.
Files changed (97) hide show
  1. package/README.md +38 -13
  2. package/docs/COMPACTION_EFFECTIVENESS.md +34 -0
  3. package/docs/CONTEXT_SNAPSHOTS.md +53 -0
  4. package/docs/MODEL_CATALOG.md +43 -0
  5. package/docs/OTLP_EXPORT.md +63 -0
  6. package/docs/PROVIDER_RESEARCH.md +1 -1
  7. package/docs/TOKEN_MEASUREMENT.md +53 -0
  8. package/docs/USAGE_BACKFILL.md +35 -0
  9. package/package.json +5 -4
  10. package/src/{providers/anthropic-contracts.ts → anthropic/rate-limits.ts} +1 -1
  11. package/src/{adapters/artificial-analysis-direct-source.ts → artificial-analysis/benchmark-source.ts} +3 -3
  12. package/src/cli-commands/backfill.ts +51 -0
  13. package/src/cli-commands/benchmarks.ts +4 -4
  14. package/src/cli-commands/cache.ts +120 -0
  15. package/src/cli-commands/catalog.ts +123 -0
  16. package/src/cli-commands/compaction.ts +1 -1
  17. package/src/cli-commands/context.ts +77 -4
  18. package/src/cli-commands/export.ts +30 -0
  19. package/src/cli-commands/metrics.ts +8 -2
  20. package/src/cli-commands/op.ts +1 -1
  21. package/src/cli-commands/route-args.ts +2 -2
  22. package/src/cli-commands/router.ts +2 -2
  23. package/src/cli-commands/support.ts +2 -2
  24. package/src/cli.ts +59 -21
  25. package/src/{providers/codex-contracts.ts → codex/contracts.ts} +1 -1
  26. package/src/codex/source.ts +53 -0
  27. package/src/{providers/codex.ts → codex/telemetry.ts} +2 -2
  28. package/src/constants.ts +31 -0
  29. package/src/daemon.ts +53 -23
  30. package/src/{providers/google-vertex-budget-contracts.ts → google-vertex/budget-contracts.ts} +3 -3
  31. package/src/{providers/google-vertex-budget.ts → google-vertex/budget-telemetry.ts} +6 -6
  32. package/src/{providers/google-vertex-contracts.ts → google-vertex/failures.ts} +1 -1
  33. package/src/google-vertex/source.ts +35 -0
  34. package/src/index.ts +149 -55
  35. package/src/{adapters/lmarena-hf-source.ts → lmarena/benchmark-source.ts} +3 -3
  36. package/src/observability/budget.ts +14 -0
  37. package/src/observability/cache-economics.ts +623 -0
  38. package/src/observability/context-delta.ts +309 -0
  39. package/src/{domain → observability}/context-hub.ts +19 -2
  40. package/src/observability/context-snapshot-history.ts +281 -0
  41. package/src/{domain → observability}/context-telemetry.ts +232 -4
  42. package/src/observability/model-identity.ts +42 -0
  43. package/src/{domain → observability}/model-observation.ts +26 -11
  44. package/src/{ports/metric-store.ts → observability/store.ts} +2 -2
  45. package/src/{ports → observability}/telemetry-source.ts +2 -2
  46. package/src/observability/token-measurement.ts +194 -0
  47. package/src/observability/usage-import.ts +122 -0
  48. package/src/openai/token-counter.ts +63 -0
  49. package/src/{adapters/openrouter-benchmark-source.ts → openrouter/benchmark-source.ts} +3 -3
  50. package/src/{providers/openrouter-contracts.ts → openrouter/contracts.ts} +1 -1
  51. package/src/{adapters/openrouter-design-arena-source.ts → openrouter/design-arena-source.ts} +4 -4
  52. package/src/openrouter/source.ts +20 -0
  53. package/src/{providers/openrouter.ts → openrouter/telemetry.ts} +2 -2
  54. package/src/{domain → optimization/model-selection}/benchmark.ts +9 -27
  55. package/src/optimization/model-selection/catalog-store.ts +97 -0
  56. package/src/optimization/model-selection/catalog.ts +478 -0
  57. package/src/{ports/benchmark-controller.ts → optimization/model-selection/controller.ts} +1 -1
  58. package/src/{adapters/metric-benchmark-store.ts → optimization/model-selection/observation-store.ts} +5 -5
  59. package/src/{domain/model-ranking-service.ts → optimization/model-selection/ranker.ts} +6 -12
  60. package/src/{domain/model-ranking.ts → optimization/model-selection/ranking.ts} +8 -2
  61. package/src/{ports/benchmark-source.ts → optimization/model-selection/source.ts} +1 -1
  62. package/src/{ports/benchmark-store.ts → optimization/model-selection/store.ts} +1 -1
  63. package/src/{domain/codex-recovery.ts → optimization/recovery/codex.ts} +1 -1
  64. package/src/{ports/router-controller.ts → optimization/routing/controller.ts} +1 -1
  65. package/src/{policy.ts → optimization/routing/policy.ts} +4 -14
  66. package/src/{router.ts → optimization/routing/router.ts} +4 -10
  67. package/src/otlp/exporter.ts +354 -0
  68. package/src/pi/session-usage-source.ts +203 -0
  69. package/src/{session-identity-service.ts → sessions/identity.ts} +2 -2
  70. package/src/{operations/session-scope.ts → sessions/router-authorization.ts} +1 -1
  71. package/src/{db.ts → sqlite/database.ts} +14 -1
  72. package/src/{adapters/sqlite-metric-store.ts → sqlite/metric-store.ts} +4 -4
  73. package/src/{adapters/sqlite-session-identity-store.ts → sqlite/session-store.ts} +1 -1
  74. package/src/sqlite/usage-import-store.ts +133 -0
  75. package/src/telemetry-export/exporter.ts +84 -0
  76. package/src/{operations → vehicle}/benchmark-operations.ts +3 -3
  77. package/src/vehicle/cache-operations.ts +95 -0
  78. package/src/vehicle/catalog-operations.ts +10 -0
  79. package/src/{client.ts → vehicle/client.ts} +1 -1
  80. package/src/{operations → vehicle}/context-operations.ts +13 -5
  81. package/src/vehicle/export-operations.ts +12 -0
  82. package/src/{operations/metrics-operations.ts → vehicle/metric-operations.ts} +4 -4
  83. package/src/{operations → vehicle}/model-ranking-operations.ts +3 -3
  84. package/src/{vehicle-registration.ts → vehicle/registration.ts} +21 -4
  85. package/src/{operations/router-operations.ts → vehicle/routing-operations.ts} +4 -4
  86. package/src/{service.ts → vehicle/service.ts} +114 -26
  87. package/src/{operations/session-identity-operations.ts → vehicle/session-operations.ts} +3 -3
  88. package/src/vehicle/usage-import-operations.ts +10 -0
  89. package/src/providers/telemetry-sources.ts +0 -108
  90. /package/src/{providers/google-adc-auth.ts → google-vertex/auth.ts} +0 -0
  91. /package/src/{domain → observability}/metric.ts +0 -0
  92. /package/src/{domain → observability}/task-cost.ts +0 -0
  93. /package/src/{domain → observability}/task-focus.ts +0 -0
  94. /package/src/{domain → observability}/usage.ts +0 -0
  95. /package/src/{config.ts → optimization/routing/config.ts} +0 -0
  96. /package/src/{ports/session-identity-store.ts → sessions/store.ts} +0 -0
  97. /package/src/{operations/types.ts → vehicle/operation-types.ts} +0 -0
package/src/constants.ts CHANGED
@@ -18,6 +18,19 @@ export const BENCHMARK_MAX_QUERY_LIMIT = 500;
18
18
  export const BENCHMARK_SOURCE_MAX_RESPONSE_BYTES = 2_097_152;
19
19
  export const BENCHMARK_SOURCE_MAX_TOTAL_RESPONSE_BYTES = 6_291_456;
20
20
  export const BENCHMARK_REFRESH_INTERVAL_MS = 24 * 60 * 60 * 1_000;
21
+ export const MODEL_CATALOG_MAX_RESPONSE_BYTES = 8_388_608;
22
+ export const MODEL_CATALOG_MAX_ENTRIES = 8_000;
23
+ export const MODEL_CATALOG_STORE_QUERY_LIMIT = MODEL_CATALOG_MAX_ENTRIES + 1;
24
+ export const MODEL_CATALOG_DEFAULT_QUERY_LIMIT = 100;
25
+ export const MODEL_CATALOG_MAX_QUERY_LIMIT = 500;
26
+ export const MODEL_CATALOG_REQUEST_TIMEOUT_MS = 10_000;
27
+ export const MODEL_CATALOG_FRESHNESS_MS = 24 * 60 * 60 * 1_000;
28
+ export const PI_USAGE_IMPORT_MAX_FILES = 200;
29
+ export const PI_USAGE_IMPORT_MAX_FILE_BYTES = 16_777_216;
30
+ export const PI_USAGE_IMPORT_MAX_TOTAL_BYTES = 67_108_864;
31
+ export const PI_USAGE_IMPORT_MAX_ENTRIES = 20_000;
32
+ export const PI_USAGE_IMPORT_MAX_RECORDS = 5_000;
33
+ export const PI_USAGE_IMPORT_MAX_DURATION_MS = 30_000;
21
34
  export const MODEL_OBSERVATION_IDENTITY_MAX_CHARACTERS = 160;
22
35
  export const MODEL_AGGREGATE_MAX_ROWS = 10_000;
23
36
  export const MODEL_AGGREGATE_MAX_GROUPS = 500;
@@ -31,6 +44,11 @@ export const MODEL_OBSERVATION_FRESH_MS = 7 * 24 * 60 * 60 * 1_000;
31
44
  export const MAINTENANCE_INTERVAL_MS = 15 * 60 * 1_000;
32
45
  export const TELEMETRY_POLL_INTERVAL_MS = 60_000;
33
46
  export const TELEMETRY_STALE_AFTER_MS = 120_000;
47
+ export const OTLP_EXPORT_MAX_QUEUE_SIZE = 1_000;
48
+ export const OTLP_EXPORT_BATCH_SIZE = 100;
49
+ export const OTLP_EXPORT_INTERVAL_MS = 5_000;
50
+ export const OTLP_EXPORT_TIMEOUT_MS = 3_000;
51
+ export const OTLP_EXPORT_MAX_RETRIES = 1;
34
52
  export const FOOTER_CONTEXT_ACCENT_FRACTION = 0.5;
35
53
  export const FOOTER_CONTEXT_WARNING_FRACTION = 0.7;
36
54
  export const FOOTER_CONTEXT_ERROR_FRACTION = 0.9;
@@ -51,6 +69,19 @@ export const PAPYRUS_TASK_FOCUS_SCHEMA = "papyrus.task-focus/v1";
51
69
  export const TASK_FOCUS_EVENT_MAX_AGE_MS = 5 * MILLISECONDS_PER_MINUTE;
52
70
  export const TASK_FOCUS_ID_MAX_LENGTH = 200;
53
71
  export const TASK_COST_QUERY_LIMIT = 10_000;
72
+ export const CACHE_ECONOMICS_QUERY_LIMIT = 10_000;
73
+ export const CACHE_ECONOMICS_MAX_MODEL_GROUPS = 500;
74
+ /** Bounds the per-task rollup the same way CACHE_ECONOMICS_MAX_MODEL_GROUPS bounds the per-model one -- applied post-hoc (sorted by activity, then sliced) rather than at row-intake time, since realistic task cardinality in one window is small. */
75
+ export const CACHE_ECONOMICS_MAX_TASK_GROUPS = 500;
76
+ /** Bounds the stable-prefix-churn time series; the most recent points are kept (oldest dropped first) since a trend view cares most about what's recent. */
77
+ export const CACHE_ECONOMICS_MAX_STABLE_PREFIX_POINTS = 500;
78
+ export const CACHE_ECONOMICS_MAX_MISSED_OPPORTUNITIES = 100;
79
+ /** How soon after a context-prefix reset (session/provider/model change) a same-session cache-write still counts as a candidate cache-loss consequence of that reset, not an unrelated later write. */
80
+ export const CACHE_ECONOMICS_LOSS_CORRELATION_WINDOW_MS = 5 * MILLISECONDS_PER_MINUTE;
81
+ /** models.dev (and Jittor's catalog) quote pricing per one million tokens; catalog-estimate math divides by this to get a per-token rate. */
82
+ export const CATALOG_PRICE_TOKEN_UNIT = 1_000_000;
83
+ /** models.dev's own long-context pricing convention: a request whose context exceeds this many tokens uses ModelCatalogPricing.contextOver200k instead of the flat/tiered rate. */
84
+ export const CATALOG_LONG_CONTEXT_THRESHOLD_TOKENS = 200_000;
54
85
  /** A `metrics prune --before` cutoff newer than this must pass `force: true`. Guards against accidentally wiping recent/live data with a too-recent cutoff (e.g. "now"), while still allowing routine cleanup of genuinely old rows without ceremony. */
55
86
  export const PRUNE_MIN_AGE_MS = 24 * MILLISECONDS_PER_HOUR;
56
87
  export const CONTEXT_OBSERVATION_MAX_CHARACTERS = 10_000_000;
package/src/daemon.ts CHANGED
@@ -1,27 +1,37 @@
1
+ import { join } from "node:path";
1
2
  import { type RunningDaemon, startDaemon as startDaemonKit } from "@danypops/vehicle-server/daemon";
2
- import { ArtificialAnalysisDirectSource } from "./adapters/artificial-analysis-direct-source.ts";
3
- import { LmArenaHfSource } from "./adapters/lmarena-hf-source.ts";
4
- import { MetricBenchmarkStore } from "./adapters/metric-benchmark-store.ts";
5
- import { OpenRouterBenchmarkSource } from "./adapters/openrouter-benchmark-source.ts";
6
- import { OpenRouterDesignArenaSource } from "./adapters/openrouter-design-arena-source.ts";
7
- import { SQLiteMetricStore } from "./adapters/sqlite-metric-store.ts";
8
- import { SQLiteSessionIdentityStore } from "./adapters/sqlite-session-identity-store.ts";
9
- import { DEFAULT_POLICY, UNCONFIGURED_ROUTE } from "./config.ts";
3
+ import { ArtificialAnalysisDirectSource } from "./artificial-analysis/benchmark-source.ts";
4
+ import { CodexTelemetrySource } from "./codex/source.ts";
10
5
  import { MAINTENANCE_INTERVAL_MS, TELEMETRY_POLL_INTERVAL_MS } from "./constants.ts";
11
- import { openJittorDb } from "./db.ts";
12
- import { BenchmarkCatalog } from "./domain/benchmark.ts";
13
- import { EvidenceModelRanker } from "./domain/model-ranking-service.ts";
6
+ import { createGoogleAdcTokenProvider } from "./google-vertex/auth.ts";
7
+ import { GOOGLE_PUBSUB_READONLY_SCOPE } from "./google-vertex/budget-telemetry.ts";
8
+ import type { GoogleVertexMetricSource } from "./google-vertex/failures.ts";
9
+ import { GoogleVertexBudgetTelemetrySource } from "./google-vertex/source.ts";
10
+ import { LmArenaHfSource } from "./lmarena/benchmark-source.ts";
14
11
  import { logEvent, logger } from "./log.ts";
15
- import type { BenchmarkSource } from "./ports/benchmark-source.ts";
16
- import type { TelemetrySource } from "./ports/telemetry-source.ts";
17
- import { createGoogleAdcTokenProvider } from "./providers/google-adc-auth.ts";
18
- import { GOOGLE_PUBSUB_READONLY_SCOPE } from "./providers/google-vertex-budget.ts";
19
- import type { GoogleVertexMetricSource } from "./providers/google-vertex-contracts.ts";
20
- import { CodexTelemetrySource, GoogleVertexBudgetTelemetrySource, OpenRouterTelemetrySource } from "./providers/telemetry-sources.ts";
21
- import { JittorRouter } from "./router.ts";
22
- import { createApp, JittorService } from "./service.ts";
23
- import { SessionIdentity } from "./session-identity-service.ts";
12
+ import type { TelemetrySource } from "./observability/telemetry-source.ts";
13
+ import { HistoricalUsageImporter } from "./observability/usage-import.ts";
14
+ import { OpenRouterBenchmarkSource } from "./openrouter/benchmark-source.ts";
15
+ import { OpenRouterDesignArenaSource } from "./openrouter/design-arena-source.ts";
16
+ import { OpenRouterTelemetrySource } from "./openrouter/source.ts";
17
+ import { BenchmarkCatalog } from "./optimization/model-selection/benchmark.ts";
18
+ import { ModelCatalog, type ModelCatalogSource, ModelsDevCatalogSource } from "./optimization/model-selection/catalog.ts";
19
+ import { MetricModelCatalogStore } from "./optimization/model-selection/catalog-store.ts";
20
+ import { MetricBenchmarkStore } from "./optimization/model-selection/observation-store.ts";
21
+ import { EvidenceModelRanker } from "./optimization/model-selection/ranker.ts";
22
+ import type { BenchmarkSource } from "./optimization/model-selection/source.ts";
23
+ import { DEFAULT_POLICY, UNCONFIGURED_ROUTE } from "./optimization/routing/config.ts";
24
+ import { JittorRouter } from "./optimization/routing/router.ts";
25
+ import { otlpExporterFromEnvironment } from "./otlp/exporter.ts";
26
+ import { PiSessionUsageSource } from "./pi/session-usage-source.ts";
27
+ import { SessionIdentity } from "./sessions/identity.ts";
28
+ import { openJittorDb } from "./sqlite/database.ts";
29
+ import { SQLiteMetricStore } from "./sqlite/metric-store.ts";
30
+ import { SQLiteSessionIdentityStore } from "./sqlite/session-store.ts";
31
+ import { SQLiteUsageImportStore } from "./sqlite/usage-import-store.ts";
24
32
  import { ensureAuthToken, type JittorPaths, resolveJittorPaths } from "./state.ts";
33
+ import { DisabledObservationExporter, ExportingMetricStore } from "./telemetry-export/exporter.ts";
34
+ import { createApp, JittorService } from "./vehicle/service.ts";
25
35
 
26
36
  export type { RunningDaemon } from "@danypops/vehicle-server/daemon";
27
37
 
@@ -47,6 +57,11 @@ function googleVertexMetricSource(value: string | undefined): GoogleVertexMetric
47
57
  throw new Error("JITTOR_GOOGLE_VERTEX_BUDGET_SOURCE must be google-vertex or anthropic-vertex");
48
58
  }
49
59
 
60
+ export function modelCatalogSourceFromEnvironment(env: Record<string, string | undefined> = process.env): ModelCatalogSource | undefined {
61
+ if (env.JITTOR_MODELS_DEV_CATALOG !== "1") return undefined;
62
+ return new ModelsDevCatalogSource({ sourceUrl: env.JITTOR_MODELS_DEV_CATALOG_URL });
63
+ }
64
+
50
65
  export function telemetrySourcesFromEnvironment(env: Record<string, string | undefined> = process.env): TelemetrySource[] {
51
66
  const sources: TelemetrySource[] = [];
52
67
  const codexAuthFile = env.JITTOR_CODEX_AUTH_FILE;
@@ -80,13 +95,19 @@ export async function startDaemon(
80
95
  ): Promise<RunningDaemon> {
81
96
  const token = ensureAuthToken(paths);
82
97
  const db = openJittorDb(paths.database);
83
- const metrics = new SQLiteMetricStore(db);
98
+ const localMetrics = new SQLiteMetricStore(db);
99
+ const exporter = otlpExporterFromEnvironment(env) ?? new DisabledObservationExporter();
100
+ const metrics = new ExportingMetricStore(localMetrics, exporter);
84
101
  const sessionIdentity = new SessionIdentity(new SQLiteSessionIdentityStore(db));
85
102
  const sources = telemetrySourcesFromEnvironment(env);
86
103
  const benchmarkSources = benchmarkSourcesFromEnvironment(env);
87
104
  const benchmarkStore = new MetricBenchmarkStore(metrics);
88
105
  const benchmarks = new BenchmarkCatalog(benchmarkStore, benchmarkSources);
106
+ const catalogSource = modelCatalogSourceFromEnvironment(env);
107
+ const catalog = new ModelCatalog(new MetricModelCatalogStore(localMetrics), catalogSource);
89
108
  const modelRanker = new EvidenceModelRanker(benchmarkStore, metrics);
109
+ const sessionsRoot = env.JITTOR_PI_SESSIONS_DIR ?? join(env.HOME ?? process.env.HOME ?? "", ".pi", "agent", "sessions");
110
+ const usageImporter = new HistoricalUsageImporter(new PiSessionUsageSource(sessionsRoot), new SQLiteUsageImportStore(db));
90
111
  const router = new JittorRouter({
91
112
  metrics,
92
113
  sources,
@@ -94,7 +115,7 @@ export async function startDaemon(
94
115
  routes: [],
95
116
  currentRoute: UNCONFIGURED_ROUTE,
96
117
  });
97
- const service = new JittorService(metrics, router, benchmarks, modelRanker, sessionIdentity);
118
+ const service = new JittorService(metrics, router, benchmarks, modelRanker, sessionIdentity, undefined, catalog, usageImporter, exporter);
98
119
 
99
120
  const daemon = await startDaemonKit({
100
121
  daemonLabel: "Jittor",
@@ -116,6 +137,13 @@ export async function startDaemon(
116
137
  await benchmarks.refresh().catch((error) => reportMaintenanceFailure("benchmark_refresh_failed", error));
117
138
  },
118
139
  },
140
+ {
141
+ name: "catalog-refresh",
142
+ intervalMs: MAINTENANCE_INTERVAL_MS,
143
+ run: async () => {
144
+ await catalog.refresh().catch((error) => reportMaintenanceFailure("catalog_refresh_failed", error));
145
+ },
146
+ },
119
147
  {
120
148
  name: "telemetry-poll",
121
149
  intervalMs: TELEMETRY_POLL_INTERVAL_MS,
@@ -124,13 +152,15 @@ export async function startDaemon(
124
152
  },
125
153
  },
126
154
  ],
127
- onShutdown: () => {
155
+ onShutdown: async () => {
156
+ await exporter.shutdown();
128
157
  service.close();
129
158
  },
130
159
  });
131
160
 
132
161
  if (sources.length > 0) router.poll().catch((error) => reportMaintenanceFailure("telemetry_poll_failed", error));
133
162
  if (benchmarkSources.length > 0) benchmarks.refresh().catch((error) => reportMaintenanceFailure("benchmark_refresh_failed", error));
163
+ if (catalogSource) catalog.refresh().catch((error) => reportMaintenanceFailure("catalog_refresh_failed", error));
134
164
 
135
165
  return daemon;
136
166
  }
@@ -3,9 +3,9 @@ import {
3
3
  GOOGLE_VERTEX_BUDGET_DISPLAY_NAME_MAX_CHARACTERS,
4
4
  MILLISECONDS_PER_SECOND,
5
5
  } from "../constants.ts";
6
- import type { MetricObservation } from "../domain/metric.ts";
7
- import type { BudgetWindow } from "../policy.ts";
8
- import type { GoogleVertexMetricSource } from "./google-vertex-contracts.ts";
6
+ import type { BudgetWindow } from "../observability/budget.ts";
7
+ import type { MetricObservation } from "../observability/metric.ts";
8
+ import type { GoogleVertexMetricSource } from "./failures.ts";
9
9
 
10
10
  /**
11
11
  * Cloud Billing's programmatic budget notification schema (Pub/Sub attributes + base64 JSON data
@@ -1,14 +1,14 @@
1
1
  import { GOOGLE_VERTEX_BUDGET_MAX_MESSAGES_PER_PULL } from "../constants.ts";
2
- import type { MetricObservation } from "../domain/metric.ts";
3
- import type { BudgetWindow } from "../policy.ts";
4
- import type { GoogleAdcTokenProvider } from "./google-adc-auth.ts";
2
+ import type { BudgetWindow } from "../observability/budget.ts";
3
+ import type { MetricObservation } from "../observability/metric.ts";
4
+ import type { GoogleAdcTokenProvider } from "./auth.ts";
5
5
  import {
6
6
  type GoogleVertexBudgetNotification,
7
7
  googleVertexBudgetMetrics,
8
8
  googleVertexBudgetWindow,
9
9
  parseGoogleVertexBudgetNotification,
10
- } from "./google-vertex-budget-contracts.ts";
11
- import type { GoogleVertexMetricSource } from "./google-vertex-contracts.ts";
10
+ } from "./budget-contracts.ts";
11
+ import type { GoogleVertexMetricSource } from "./failures.ts";
12
12
 
13
13
  export {
14
14
  type GoogleVertexBudgetAmountType,
@@ -16,7 +16,7 @@ export {
16
16
  googleVertexBudgetMetrics,
17
17
  googleVertexBudgetWindow,
18
18
  parseGoogleVertexBudgetNotification,
19
- } from "./google-vertex-budget-contracts.ts";
19
+ } from "./budget-contracts.ts";
20
20
 
21
21
  const PUBSUB_BASE_URL = "https://pubsub.googleapis.com/v1";
22
22
  export const GOOGLE_PUBSUB_READONLY_SCOPE = "https://www.googleapis.com/auth/pubsub";
@@ -1,5 +1,5 @@
1
1
  import { MILLISECONDS_PER_MINUTE, MILLISECONDS_PER_SECOND } from "../constants.ts";
2
- import type { MetricObservation } from "../domain/metric.ts";
2
+ import type { MetricObservation } from "../observability/metric.ts";
3
3
 
4
4
  /**
5
5
  * Google Vertex AI has no documented per-response rate-limit or remaining-quota header, and no
@@ -0,0 +1,35 @@
1
+ import type { TelemetryBatch, TelemetrySource } from "../observability/telemetry-source.ts";
2
+ import type { GoogleAdcTokenProvider } from "./auth.ts";
3
+ import { GoogleVertexBudgetTelemetryAdapter, type GoogleVertexBudgetTransport } from "./budget-telemetry.ts";
4
+ import type { GoogleVertexMetricSource } from "./failures.ts";
5
+
6
+ /**
7
+ * Optional because project budget notification setup happens outside Jittor; its absence cannot
8
+ * block unrelated routes.
9
+ */
10
+ export class GoogleVertexBudgetTelemetrySource implements TelemetrySource {
11
+ readonly id: string;
12
+ readonly provider: GoogleVertexMetricSource;
13
+ readonly required = false;
14
+
15
+ private readonly adapter: GoogleVertexBudgetTelemetryAdapter;
16
+
17
+ constructor(
18
+ subscription: string,
19
+ tokenProvider: GoogleAdcTokenProvider,
20
+ private readonly clock: () => number = Date.now,
21
+ transport: GoogleVertexBudgetTransport = fetch,
22
+ source: GoogleVertexMetricSource = "google-vertex",
23
+ ) {
24
+ this.id = `google-vertex-budget:${source}`;
25
+ this.provider = source;
26
+ this.adapter = new GoogleVertexBudgetTelemetryAdapter(subscription, tokenProvider, transport, source);
27
+ }
28
+
29
+ async poll(): Promise<TelemetryBatch> {
30
+ const observedAt = this.clock();
31
+ const snapshot = await this.adapter.pull(observedAt);
32
+ if (!snapshot) return { observedAt, metrics: [], windows: [] };
33
+ return { observedAt, metrics: snapshot.metrics, windows: snapshot.window ? [snapshot.window] : [] };
34
+ }
35
+ }
package/src/index.ts CHANGED
@@ -1,19 +1,52 @@
1
1
  export {
2
- connectJittorClient,
3
- type FetchTransport,
4
- JittorClient,
5
- } from "./client.ts";
2
+ type AnthropicMetricSource,
3
+ type AnthropicRateLimitSnapshot,
4
+ type AnthropicRateLimitWindow,
5
+ hasAnthropicRateLimitHeaders,
6
+ parseAnthropicRateLimitHeaders,
7
+ } from "./anthropic/rate-limits.ts";
8
+ export { parseCodexRateLimitHeaders } from "./codex/telemetry.ts";
6
9
  export * from "./constants.ts";
7
10
  export {
8
- type CodexFailure,
9
- type CodexFailureKind,
10
- type CodexFailureMetadata,
11
- type CodexRecoveryAttempt,
12
- type CodexRecoveryOptions,
13
- type CodexRecoveryPlan,
14
- CodexRecoveryPolicy,
15
- classifyCodexFailure,
16
- } from "./domain/codex-recovery.ts";
11
+ classifyGoogleVertexFailure,
12
+ type GoogleVertexFailure,
13
+ type GoogleVertexFailureKind,
14
+ type GoogleVertexFailureMetadata,
15
+ type GoogleVertexMetricSource,
16
+ googleVertexFailureMetrics,
17
+ } from "./google-vertex/failures.ts";
18
+ export {
19
+ buildCacheEconomicsSummary,
20
+ type CacheCostBasis,
21
+ type CacheEconomicsAggregateTotals,
22
+ type CacheEconomicsMissedOpportunity,
23
+ type CacheEconomicsModelSummary,
24
+ type CacheEconomicsPricing,
25
+ type CacheEconomicsPricingLookup,
26
+ type CacheEconomicsStablePrefixPoint,
27
+ type CacheEconomicsSummary,
28
+ type CacheEconomicsSummaryOptions,
29
+ type CacheEconomicsTaskSummary,
30
+ type CacheEconomicsUnattributedActivity,
31
+ } from "./observability/cache-economics.ts";
32
+ export {
33
+ CONTEXT_SEGMENT_SOURCES,
34
+ CONTEXT_SEGMENT_STATES,
35
+ CONTEXT_SNAPSHOT_MAX_SEGMENTS,
36
+ type ContextDelta,
37
+ type ContextFingerprinter,
38
+ type ContextPrefixResetReason,
39
+ type ContextSegmentChange,
40
+ type ContextSegmentLifecycle,
41
+ type ContextSegmentSource,
42
+ type ContextSegmentState,
43
+ type ContextSnapshot,
44
+ type ContextSnapshotSegment,
45
+ type ContextSourceGrowth,
46
+ compareContextSnapshots,
47
+ HmacContextFingerprinter,
48
+ validateContextSnapshot,
49
+ } from "./observability/context-delta.ts";
17
50
  export {
18
51
  type ContextConfidenceTier,
19
52
  type ContextContribution,
@@ -27,7 +60,11 @@ export {
27
60
  toolLedgerSegment,
28
61
  validateContextContribution,
29
62
  validateContextSegment,
30
- } from "./domain/context-hub.ts";
63
+ } from "./observability/context-hub.ts";
64
+ export {
65
+ type ContextSnapshotHistory,
66
+ MetricContextSnapshotHistory,
67
+ } from "./observability/context-snapshot-history.ts";
31
68
  export {
32
69
  assessContextTelemetry,
33
70
  type CompactionDurationEstimate,
@@ -38,7 +75,7 @@ export {
38
75
  type PapyrusContextInjection,
39
76
  papyrusContextMetric,
40
77
  validatePapyrusContextInjection,
41
- } from "./domain/context-telemetry.ts";
78
+ } from "./observability/context-telemetry.ts";
42
79
  export {
43
80
  METRIC_UNITS,
44
81
  type MetricObservation,
@@ -46,7 +83,7 @@ export {
46
83
  type MetricUnit,
47
84
  type StoredMetricObservation,
48
85
  validateMetricObservation,
49
- } from "./domain/metric.ts";
86
+ } from "./observability/metric.ts";
50
87
  export {
51
88
  aggregateModelMetrics,
52
89
  classifyTaskFromTools,
@@ -61,25 +98,29 @@ export {
61
98
  TASK_DOMAINS,
62
99
  TASK_TYPES,
63
100
  validateModelRunObservation,
64
- } from "./domain/model-observation.ts";
65
- export {
66
- type ModelCandidate,
67
- type ModelRankingInput,
68
- type ModelRankingResult,
69
- type RankedModel,
70
- type RankingProvenance,
71
- rankModelCandidates,
72
- type ScopeAuthority,
73
- type UtilityComponent,
74
- type UtilityComponentName,
75
- type UtilityWeights,
76
- } from "./domain/model-ranking.ts";
101
+ } from "./observability/model-observation.ts";
102
+ export type { DistinctScopesFilter, MetricStore, UsageAggregateFilter } from "./observability/store.ts";
77
103
  export {
78
104
  applyTaskFocusEvent,
79
105
  type TaskFocusEvent,
80
106
  type TaskFocusStatus,
81
107
  validateTaskFocusEvent,
82
- } from "./domain/task-focus.ts";
108
+ } from "./observability/task-focus.ts";
109
+ export {
110
+ countTextWithFallback,
111
+ type RequestTokenReconciliation,
112
+ reconcileRequestTokens,
113
+ StructuralTextTokenCounter,
114
+ type TextTokenCounter,
115
+ type TextTokenCountInput,
116
+ TOKEN_MEASUREMENT_PROVENANCES,
117
+ TOKEN_MEASUREMENT_SCOPES,
118
+ type TokenMeasurement,
119
+ type TokenMeasurementProvenance,
120
+ type TokenMeasurementScope,
121
+ validateRequestTokenReconciliation,
122
+ validateTokenMeasurement,
123
+ } from "./observability/token-measurement.ts";
83
124
  export {
84
125
  buildCostGraph,
85
126
  buildUsageGraph,
@@ -100,7 +141,62 @@ export {
100
141
  usageBucketIndex,
101
142
  usagePeriod,
102
143
  usagePeriodStart,
103
- } from "./domain/usage.ts";
144
+ } from "./observability/usage.ts";
145
+ export {
146
+ HistoricalUsageImporter,
147
+ type HistoricalUsageRecord,
148
+ type HistoricalUsageScan,
149
+ type HistoricalUsageSource,
150
+ type UsageImportController,
151
+ type UsageImportResult,
152
+ type UsageImportStatus,
153
+ type UsageImportStore,
154
+ } from "./observability/usage-import.ts";
155
+ export { loadOpenAiTextTokenCounter, OpenAiTextTokenCounter } from "./openai/token-counter.ts";
156
+ export {
157
+ ModelCatalog,
158
+ type ModelCatalogAuthority,
159
+ type ModelCatalogController,
160
+ type ModelCatalogEntry,
161
+ type ModelCatalogFieldAuthority,
162
+ type ModelCatalogPricing,
163
+ type ModelCatalogProvenance,
164
+ type ModelCatalogQuery,
165
+ type ModelCatalogQueryResult,
166
+ type ModelCatalogSource,
167
+ type ModelCatalogStatus,
168
+ ModelsDevCatalogSource,
169
+ translateModelsDevCatalog,
170
+ } from "./optimization/model-selection/catalog.ts";
171
+ export {
172
+ type ModelCandidate,
173
+ type ModelRankingInput,
174
+ type ModelRankingResult,
175
+ type RankedModel,
176
+ type RankingProvenance,
177
+ rankModelCandidates,
178
+ type ScopeAuthority,
179
+ type UtilityComponent,
180
+ type UtilityComponentName,
181
+ type UtilityWeights,
182
+ } from "./optimization/model-selection/ranking.ts";
183
+ export {
184
+ type CodexFailure,
185
+ type CodexFailureKind,
186
+ type CodexFailureMetadata,
187
+ type CodexRecoveryAttempt,
188
+ type CodexRecoveryOptions,
189
+ type CodexRecoveryPlan,
190
+ CodexRecoveryPolicy,
191
+ classifyCodexFailure,
192
+ } from "./optimization/recovery/codex.ts";
193
+ export type {
194
+ RouteOverride,
195
+ RouterController,
196
+ RouterStatus,
197
+ TelemetryPollResult,
198
+ TelemetrySourceStatus,
199
+ } from "./optimization/routing/controller.ts";
104
200
  export {
105
201
  type BudgetWindow,
106
202
  evaluateRoutingPolicy,
@@ -112,31 +208,29 @@ export {
112
208
  type PreviousDecision,
113
209
  type Route,
114
210
  type TelemetryFreshness,
115
- } from "./policy.ts";
116
- export type { DistinctScopesFilter, MetricStore, UsageAggregateFilter } from "./ports/metric-store.ts";
117
- export type {
118
- RouteOverride,
119
- RouterController,
120
- RouterStatus,
121
- TelemetryPollResult,
122
- TelemetrySourceStatus,
123
- } from "./ports/router-controller.ts";
211
+ } from "./optimization/routing/policy.ts";
124
212
  export {
125
- type AnthropicMetricSource,
126
- type AnthropicRateLimitSnapshot,
127
- type AnthropicRateLimitWindow,
128
- hasAnthropicRateLimitHeaders,
129
- parseAnthropicRateLimitHeaders,
130
- } from "./providers/anthropic-contracts.ts";
131
- export { parseCodexRateLimitHeaders } from "./providers/codex.ts";
213
+ buildOtlpMetricsRequest,
214
+ mapObservationToOtlp,
215
+ OTLP_GENAI_SEMANTIC_CONVENTIONS,
216
+ type OtlpMetricPoint,
217
+ OtlpObservationExporter,
218
+ type OtlpObservationExporterOptions,
219
+ type OtlpTransport,
220
+ otlpExporterFromEnvironment,
221
+ } from "./otlp/exporter.ts";
222
+ export { PiSessionUsageSource, type PiSessionUsageSourceOptions } from "./pi/session-usage-source.ts";
132
223
  export {
133
- classifyGoogleVertexFailure,
134
- type GoogleVertexFailure,
135
- type GoogleVertexFailureKind,
136
- type GoogleVertexFailureMetadata,
137
- type GoogleVertexMetricSource,
138
- googleVertexFailureMetrics,
139
- } from "./providers/google-vertex-contracts.ts";
224
+ DisabledObservationExporter,
225
+ ExportingMetricStore,
226
+ type ObservationExporter,
227
+ type ObservationExportStatus,
228
+ } from "./telemetry-export/exporter.ts";
229
+ export {
230
+ connectJittorClient,
231
+ type FetchTransport,
232
+ JittorClient,
233
+ } from "./vehicle/client.ts";
140
234
  export {
141
235
  createApp,
142
236
  EXPECTED_OPERATION_NAMES,
@@ -147,5 +241,5 @@ export {
147
241
  type OperationName,
148
242
  type OperationOutputs,
149
243
  UnknownOperationError,
150
- } from "./service.ts";
244
+ } from "./vehicle/service.ts";
151
245
  export { VERSION as jittorVersion } from "./version.ts";
@@ -1,12 +1,12 @@
1
1
  import { BENCHMARK_MAX_MODELS_PER_SOURCE, BENCHMARK_REFRESH_INTERVAL_MS, BENCHMARK_SOURCE_MAX_RESPONSE_BYTES } from "../constants.ts";
2
+ import { contractRecord } from "../openrouter/contracts.ts";
2
3
  import {
3
4
  type BenchmarkObservation,
4
5
  type BenchmarkSourceSnapshot,
5
6
  normalizeModelIdentity,
6
7
  validateBenchmarkObservation,
7
- } from "../domain/benchmark.ts";
8
- import type { BenchmarkSource } from "../ports/benchmark-source.ts";
9
- import { contractRecord } from "../providers/openrouter-contracts.ts";
8
+ } from "../optimization/model-selection/benchmark.ts";
9
+ import type { BenchmarkSource } from "../optimization/model-selection/source.ts";
10
10
 
11
11
  const SOURCE_ID = "lmarena-hf";
12
12
  const BASE_URL = "https://datasets-server.huggingface.co/rows";
@@ -0,0 +1,14 @@
1
+ export type TelemetryFreshness = "fresh" | "stale" | "failed";
2
+
3
+ export interface BudgetWindow {
4
+ id: string;
5
+ source: string;
6
+ scope: string;
7
+ usedFraction: number;
8
+ windowSeconds: number;
9
+ resetsAt: number;
10
+ observedAt: number;
11
+ freshness: TelemetryFreshness;
12
+ confidence: number;
13
+ observedBurnPerSecond?: number;
14
+ }