@danypops/jittor 0.5.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/src/service.ts CHANGED
@@ -1,7 +1,11 @@
1
- import { CONTEXT_ASSESSMENT_DEFAULT_WINDOW_MS, CONTEXT_ASSESSMENT_QUERY_LIMIT, SERVICE_MAX_BODY_BYTES } from "./constants.ts";
1
+ import { COMPACTION_DURATION_ESTIMATE_MAX_SAMPLES, CONTEXT_ASSESSMENT_DEFAULT_WINDOW_MS, CONTEXT_ASSESSMENT_QUERY_LIMIT, SERVICE_MAX_BODY_BYTES, SERVICE_MAX_RESPONSE_BYTES } from "./constants.ts";
2
2
  import { VERSION } from "./version.ts";
3
3
  import { validateMetricObservation, type MetricObservation, type MetricQuery, type StoredMetricObservation } from "./domain/metric.ts";
4
- import { assessContextTelemetry, type ContextAssessment } from "./domain/context-telemetry.ts";
4
+ import { assessContextTelemetry, estimateCompactionDuration, type CompactionDurationEstimate, type ContextAssessment } from "./domain/context-telemetry.ts";
5
+ import type { BenchmarkQuery, BenchmarkQueryResult, BenchmarkRefreshResult } from "./domain/benchmark.ts";
6
+ import type { ModelRanker, ModelRecommendationInput } from "./domain/model-ranking-service.ts";
7
+ import type { ModelRankingResult } from "./domain/model-ranking.ts";
8
+ import type { BenchmarkController } from "./ports/benchmark-controller.ts";
5
9
  import type { MetricStore } from "./ports/metric-store.ts";
6
10
  import type { RouteOverride, RouterController, RouterStatus, TelemetryPollResult } from "./ports/router-controller.ts";
7
11
  import type { PolicyDecision, Route } from "./policy.ts";
@@ -10,7 +14,12 @@ export const EXPECTED_OPERATION_NAMES = [
10
14
  "metrics.record",
11
15
  "metrics.query",
12
16
  "metrics.prune",
17
+ "benchmark.refresh",
18
+ "benchmark.status",
19
+ "benchmark.query",
20
+ "models.rank",
13
21
  "context.assess",
22
+ "compaction.estimate",
14
23
  "service.checkpoint",
15
24
  "telemetry.poll",
16
25
  "router.status",
@@ -28,7 +37,12 @@ export interface OperationInputs {
28
37
  "metrics.record": MetricObservation;
29
38
  "metrics.query": MetricQuery;
30
39
  "metrics.prune": { before: number };
40
+ "benchmark.refresh": { force?: boolean };
41
+ "benchmark.status": Record<string, never>;
42
+ "benchmark.query": BenchmarkQuery;
43
+ "models.rank": ModelRecommendationInput;
31
44
  "context.assess": { since?: number; until?: number };
45
+ "compaction.estimate": Record<string, never>;
32
46
  "service.checkpoint": Record<string, never>;
33
47
  "telemetry.poll": Record<string, never>;
34
48
  "router.status": Record<string, never>;
@@ -44,7 +58,12 @@ export interface OperationOutputs {
44
58
  "metrics.record": StoredMetricObservation;
45
59
  "metrics.query": StoredMetricObservation[];
46
60
  "metrics.prune": { deleted: number };
61
+ "benchmark.refresh": BenchmarkRefreshResult;
62
+ "benchmark.status": BenchmarkRefreshResult;
63
+ "benchmark.query": BenchmarkQueryResult;
64
+ "models.rank": ModelRankingResult;
47
65
  "context.assess": ContextAssessment;
66
+ "compaction.estimate": CompactionDurationEstimate;
48
67
  "service.checkpoint": { ok: true };
49
68
  "telemetry.poll": TelemetryPollResult;
50
69
  "router.status": RouterStatus;
@@ -59,6 +78,16 @@ export interface OperationOutputs {
59
78
 
60
79
  export class UnknownOperationError extends Error {}
61
80
 
81
+ class UnavailableModelRanker implements ModelRanker {
82
+ rank(): ModelRankingResult { throw new Error("model ranking is not configured"); }
83
+ }
84
+
85
+ class UnavailableBenchmarkController implements BenchmarkController {
86
+ async refresh(): Promise<BenchmarkRefreshResult> { return this.status(); }
87
+ status(): BenchmarkRefreshResult { return { observedAt: Date.now(), sources: [] }; }
88
+ query(): BenchmarkQueryResult { throw new Error("benchmark evidence is not configured"); }
89
+ }
90
+
62
91
  class UnavailableRouter implements RouterController {
63
92
  private readonly unavailable: RouterStatus = { ready: false, paused: false, sources: [], lastDecision: null, override: null, currentRoute: null, availableRoutes: [] };
64
93
  async poll(): Promise<TelemetryPollResult> { return { sources: [], observedAt: Date.now() }; }
@@ -76,6 +105,8 @@ export class JittorService {
76
105
  constructor(
77
106
  private readonly metrics: MetricStore,
78
107
  private readonly router: RouterController = new UnavailableRouter(),
108
+ private readonly benchmarks: BenchmarkController = new UnavailableBenchmarkController(),
109
+ private readonly modelRanker: ModelRanker = new UnavailableModelRanker(),
79
110
  ) {}
80
111
 
81
112
  operationNames(): OperationName[] {
@@ -93,6 +124,14 @@ export class JittorService {
93
124
  if (typeof before !== "number") throw new Error("before is required");
94
125
  return { deleted: this.metrics.pruneBefore(before) };
95
126
  }
127
+ case "benchmark.refresh": return this.benchmarks.refresh(input["force"] === true);
128
+ case "benchmark.status": return this.benchmarks.status();
129
+ case "benchmark.query": return this.benchmarks.query(input as unknown as BenchmarkQuery);
130
+ case "models.rank": {
131
+ const result = this.modelRanker.rank(input as unknown as ModelRecommendationInput);
132
+ if (result.automaticSelection && this.router.applyModelRanking) this.router.applyModelRanking(result.ranked.map((item) => item.candidate));
133
+ return result;
134
+ }
96
135
  case "context.assess": {
97
136
  const until = input["until"] === undefined ? Date.now() : input["until"];
98
137
  const since = input["since"] === undefined && typeof until === "number" ? Math.max(0, until - CONTEXT_ASSESSMENT_DEFAULT_WINDOW_MS) : input["since"];
@@ -106,6 +145,13 @@ export class JittorService {
106
145
  truncated: injections.length >= CONTEXT_ASSESSMENT_QUERY_LIMIT || compactions.length >= CONTEXT_ASSESSMENT_QUERY_LIMIT,
107
146
  });
108
147
  }
148
+ case "compaction.estimate": {
149
+ const rows = this.metrics.query({
150
+ source: "pi-context", scope: "compaction", metric: "compaction-duration",
151
+ order: "desc", limit: COMPACTION_DURATION_ESTIMATE_MAX_SAMPLES,
152
+ });
153
+ return estimateCompactionDuration(rows);
154
+ }
109
155
  case "service.checkpoint": this.metrics.checkpoint(); return { ok: true };
110
156
  case "telemetry.poll": return this.router.poll();
111
157
  case "router.status": return this.router.status();
@@ -140,7 +186,18 @@ function authorized(request: Request, token: string): boolean {
140
186
  }
141
187
 
142
188
  function json(value: unknown, status = 200): Response {
143
- return Response.json(value, { status });
189
+ const body = JSON.stringify(value);
190
+ if (new TextEncoder().encode(body).byteLength > SERVICE_MAX_RESPONSE_BYTES) {
191
+ const error = JSON.stringify({ error: "response too large" });
192
+ return new Response(error, {
193
+ status: 413,
194
+ headers: { "content-type": "application/json", "content-length": String(new TextEncoder().encode(error).byteLength) },
195
+ });
196
+ }
197
+ return new Response(body, {
198
+ status,
199
+ headers: { "content-type": "application/json", "content-length": String(new TextEncoder().encode(body).byteLength) },
200
+ });
144
201
  }
145
202
 
146
203
  export function createApp(options: JittorAppOptions): { fetch(request: Request): Promise<Response> } {