@danypops/jittor 0.10.0 → 0.12.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/README.md +28 -77
- package/package.json +11 -14
- package/src/adapters/sqlite-metric-store.ts +11 -2
- package/src/adapters/sqlite-session-identity-store.ts +45 -0
- package/src/cli-commands/benchmarks.ts +140 -0
- package/src/cli-commands/compaction.ts +17 -0
- package/src/cli-commands/context.ts +49 -0
- package/src/cli-commands/metrics.ts +296 -0
- package/src/cli-commands/op.ts +40 -0
- package/src/cli-commands/route-args.ts +15 -0
- package/src/cli-commands/router.ts +207 -0
- package/src/cli-commands/service-daemon.ts +72 -0
- package/src/cli-commands/session.ts +42 -0
- package/src/cli-commands/support.ts +33 -0
- package/src/cli.ts +42 -769
- package/src/constants.ts +7 -0
- package/src/daemon.ts +13 -3
- package/src/db.ts +15 -1
- package/src/index.ts +137 -0
- package/src/operations/benchmark-operations.ts +12 -0
- package/src/operations/context-operations.ts +30 -0
- package/src/operations/metrics-operations.ts +77 -0
- package/src/operations/model-ranking-operations.ts +16 -0
- package/src/operations/router-operations.ts +19 -0
- package/src/operations/session-identity-operations.ts +15 -0
- package/src/operations/session-scope.ts +31 -0
- package/src/operations/types.ts +3 -0
- package/src/ports/metric-store.ts +2 -0
- package/src/ports/router-controller.ts +9 -9
- package/src/ports/session-identity-store.ts +5 -0
- package/src/providers/telemetry-sources.ts +2 -1
- package/src/router.ts +124 -67
- package/src/service.ts +60 -118
- package/src/session-identity-service.ts +55 -0
- package/docs/USAGE_PRIOR_ART.md +0 -64
- package/extension/src/benchmark-tui.ts +0 -105
- package/extension/src/footer.ts +0 -366
- package/extension/src/index.ts +0 -828
- package/extension/src/service-client.ts +0 -26
- package/extension/src/settings-tui.ts +0 -153
- package/extension/src/settings.ts +0 -103
- package/extension/src/tui.ts +0 -270
- package/extension/src/usage.ts +0 -320
package/src/constants.ts
CHANGED
|
@@ -91,6 +91,13 @@ export const MAX_USAGE_BUCKETS = 120;
|
|
|
91
91
|
/** Defense-in-depth cap on the SQL-side usage aggregation result: (scopes x metrics x buckets) is already small by construction, but this bounds it explicitly rather than trusting that alone. */
|
|
92
92
|
export const USAGE_AGGREGATE_MAX_ROWS = 25_000;
|
|
93
93
|
export const MAX_DYNAMIC_ROUTES = 100;
|
|
94
|
+
/** Bounds concurrent in-memory Pi router scopes; the least recently used non-global scope is evicted. */
|
|
95
|
+
export const ROUTER_MAX_SESSION_SCOPES = 500;
|
|
96
|
+
export const ROUTER_SESSION_ID_MAX_CHARACTERS = 128;
|
|
97
|
+
/** Hard cap on registered session_identities rows; oldest-seen identity is evicted beyond this. */
|
|
98
|
+
export const SESSION_IDENTITY_MAX_ROWS = 2_000;
|
|
99
|
+
/** Bounds one metrics.record_batch call; a real per-turn event batch (usage, headers, local-run metrics) is a handful of rows, never thousands. */
|
|
100
|
+
export const METRIC_BATCH_MAX_OBSERVATIONS = 100;
|
|
94
101
|
export const CODEX_ERROR_MESSAGE_LIMIT = 160;
|
|
95
102
|
export const CODEX_RETRY_AFTER_MAX_MS = 5 * MILLISECONDS_PER_MINUTE;
|
|
96
103
|
export const CODEX_RECOVERY_BASE_DELAY_MS = 2 * MILLISECONDS_PER_SECOND;
|
package/src/daemon.ts
CHANGED
|
@@ -12,6 +12,8 @@ import { BenchmarkCatalog } from "./domain/benchmark.ts";
|
|
|
12
12
|
import { EvidenceModelRanker } from "./domain/model-ranking-service.ts";
|
|
13
13
|
import { createApp, JittorService } from "./service.ts";
|
|
14
14
|
import { JittorRouter } from "./router.ts";
|
|
15
|
+
import { SQLiteSessionIdentityStore } from "./adapters/sqlite-session-identity-store.ts";
|
|
16
|
+
import { SessionIdentity } from "./session-identity-service.ts";
|
|
15
17
|
import type { BenchmarkSource } from "./ports/benchmark-source.ts";
|
|
16
18
|
import type { TelemetrySource } from "./ports/telemetry-source.ts";
|
|
17
19
|
import { CodexTelemetrySource, GoogleVertexBudgetTelemetrySource, OpenRouterTelemetrySource } from "./providers/telemetry-sources.ts";
|
|
@@ -39,6 +41,12 @@ export function benchmarkSourcesFromEnvironment(env: Record<string, string | und
|
|
|
39
41
|
return sources;
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
function googleVertexMetricSource(value: string | undefined): GoogleVertexMetricSource {
|
|
45
|
+
if (value === undefined || value === "google-vertex") return "google-vertex";
|
|
46
|
+
if (value === "anthropic-vertex") return "anthropic-vertex";
|
|
47
|
+
throw new Error("JITTOR_GOOGLE_VERTEX_BUDGET_SOURCE must be google-vertex or anthropic-vertex");
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
export function telemetrySourcesFromEnvironment(env: Record<string, string | undefined> = process.env): TelemetrySource[] {
|
|
43
51
|
const sources: TelemetrySource[] = [];
|
|
44
52
|
const codexAuthFile = env["JITTOR_CODEX_AUTH_FILE"];
|
|
@@ -49,7 +57,7 @@ export function telemetrySourcesFromEnvironment(env: Record<string, string | und
|
|
|
49
57
|
// docs/PROVIDER_RESEARCH.md), so its absence must never attempt ADC discovery or a network call.
|
|
50
58
|
const vertexBudgetSubscription = env["JITTOR_GOOGLE_VERTEX_BUDGET_SUBSCRIPTION"];
|
|
51
59
|
if (vertexBudgetSubscription) {
|
|
52
|
-
const source = (env["JITTOR_GOOGLE_VERTEX_BUDGET_SOURCE"]
|
|
60
|
+
const source = googleVertexMetricSource(env["JITTOR_GOOGLE_VERTEX_BUDGET_SOURCE"]);
|
|
53
61
|
const tokenProvider = createGoogleAdcTokenProvider([GOOGLE_PUBSUB_READONLY_SCOPE]);
|
|
54
62
|
sources.push(new GoogleVertexBudgetTelemetrySource(vertexBudgetSubscription, tokenProvider, Date.now, fetch, source));
|
|
55
63
|
}
|
|
@@ -71,7 +79,9 @@ export function startDaemon(
|
|
|
71
79
|
env: Record<string, string | undefined> = process.env,
|
|
72
80
|
): RunningDaemon {
|
|
73
81
|
const token = ensureAuthToken(paths);
|
|
74
|
-
const
|
|
82
|
+
const db = openJittorDb(paths.database);
|
|
83
|
+
const metrics = new SQLiteMetricStore(db);
|
|
84
|
+
const sessionIdentity = new SessionIdentity(new SQLiteSessionIdentityStore(db));
|
|
75
85
|
const sources = telemetrySourcesFromEnvironment(env);
|
|
76
86
|
const benchmarkSources = benchmarkSourcesFromEnvironment(env);
|
|
77
87
|
const benchmarkStore = new MetricBenchmarkStore(metrics);
|
|
@@ -84,7 +94,7 @@ export function startDaemon(
|
|
|
84
94
|
routes: [],
|
|
85
95
|
currentRoute: UNCONFIGURED_ROUTE,
|
|
86
96
|
});
|
|
87
|
-
const service = new JittorService(metrics, router, benchmarks, modelRanker);
|
|
97
|
+
const service = new JittorService(metrics, router, benchmarks, modelRanker, sessionIdentity);
|
|
88
98
|
|
|
89
99
|
const daemon = startDaemonKit({
|
|
90
100
|
daemonLabel: "Jittor",
|
package/src/db.ts
CHANGED
|
@@ -19,6 +19,17 @@ CREATE INDEX metric_observations_time_idx
|
|
|
19
19
|
ON metric_observations(observed_at);
|
|
20
20
|
`;
|
|
21
21
|
|
|
22
|
+
const SESSION_IDENTITY_SCHEMA = `
|
|
23
|
+
CREATE TABLE session_identities (
|
|
24
|
+
session_id TEXT PRIMARY KEY,
|
|
25
|
+
secret_hash TEXT NOT NULL,
|
|
26
|
+
registered_at TEXT NOT NULL,
|
|
27
|
+
last_seen_at TEXT NOT NULL
|
|
28
|
+
);
|
|
29
|
+
CREATE INDEX session_identities_last_seen_idx
|
|
30
|
+
ON session_identities(last_seen_at);
|
|
31
|
+
`;
|
|
32
|
+
|
|
22
33
|
/**
|
|
23
34
|
* Delegates bootstrap (pragmas, migration engine) to `@danypops/daemon-kit/storage`, which
|
|
24
35
|
* generalizes the byte-identical pragma/PRAGMA-user_version skeleton jittor's own db.ts used to
|
|
@@ -28,6 +39,9 @@ export function openJittorDb(path: string): Database {
|
|
|
28
39
|
return openSqliteWithPragmas(path, {
|
|
29
40
|
databaseOptions: { create: true, strict: true },
|
|
30
41
|
busyTimeoutMs: SQLITE_BUSY_TIMEOUT_MS,
|
|
31
|
-
migrations: [
|
|
42
|
+
migrations: [
|
|
43
|
+
{ version: 1, up: (db) => db.exec(INITIAL_SCHEMA) },
|
|
44
|
+
{ version: 2, up: (db) => db.exec(SESSION_IDENTITY_SCHEMA) },
|
|
45
|
+
],
|
|
32
46
|
});
|
|
33
47
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
export * from "./constants.ts";
|
|
2
|
+
export {
|
|
3
|
+
type FetchTransport,
|
|
4
|
+
JittorClient,
|
|
5
|
+
connectJittorClient,
|
|
6
|
+
} from "./client.ts";
|
|
7
|
+
export {
|
|
8
|
+
EXPECTED_OPERATION_NAMES,
|
|
9
|
+
InvalidSessionSecretError,
|
|
10
|
+
JittorService,
|
|
11
|
+
UnknownOperationError,
|
|
12
|
+
createApp,
|
|
13
|
+
type JittorAppOptions,
|
|
14
|
+
type OperationInputs,
|
|
15
|
+
type OperationName,
|
|
16
|
+
type OperationOutputs,
|
|
17
|
+
} from "./service.ts";
|
|
18
|
+
export {
|
|
19
|
+
type CompactionDurationEstimate,
|
|
20
|
+
type CompactionStart,
|
|
21
|
+
CompactionTelemetry,
|
|
22
|
+
type ContextAssessment,
|
|
23
|
+
type PapyrusContextInjection,
|
|
24
|
+
assessContextTelemetry,
|
|
25
|
+
estimateCompactionDuration,
|
|
26
|
+
papyrusContextMetric,
|
|
27
|
+
validatePapyrusContextInjection,
|
|
28
|
+
} from "./domain/context-telemetry.ts";
|
|
29
|
+
export {
|
|
30
|
+
type TaskFocusEvent,
|
|
31
|
+
type TaskFocusStatus,
|
|
32
|
+
applyTaskFocusEvent,
|
|
33
|
+
validateTaskFocusEvent,
|
|
34
|
+
} from "./domain/task-focus.ts";
|
|
35
|
+
export {
|
|
36
|
+
METRIC_UNITS,
|
|
37
|
+
type MetricObservation,
|
|
38
|
+
type MetricQuery,
|
|
39
|
+
type MetricUnit,
|
|
40
|
+
type StoredMetricObservation,
|
|
41
|
+
validateMetricObservation,
|
|
42
|
+
} from "./domain/metric.ts";
|
|
43
|
+
export {
|
|
44
|
+
TASK_DOMAINS,
|
|
45
|
+
TASK_TYPES,
|
|
46
|
+
type ExplicitOutcome,
|
|
47
|
+
type ModelAggregateOptions,
|
|
48
|
+
type ModelMetricAggregate,
|
|
49
|
+
type ModelRunObservation,
|
|
50
|
+
type ModelTaskClassification,
|
|
51
|
+
type ModelTaskDomain,
|
|
52
|
+
type ModelTaskType,
|
|
53
|
+
aggregateModelMetrics,
|
|
54
|
+
classifyTaskFromTools,
|
|
55
|
+
modelRunMetrics,
|
|
56
|
+
validateModelRunObservation,
|
|
57
|
+
} from "./domain/model-observation.ts";
|
|
58
|
+
export {
|
|
59
|
+
type ModelCandidate,
|
|
60
|
+
type ModelRankingInput,
|
|
61
|
+
type ModelRankingResult,
|
|
62
|
+
type RankedModel,
|
|
63
|
+
type RankingProvenance,
|
|
64
|
+
type ScopeAuthority,
|
|
65
|
+
type UtilityComponent,
|
|
66
|
+
type UtilityComponentName,
|
|
67
|
+
type UtilityWeights,
|
|
68
|
+
rankModelCandidates,
|
|
69
|
+
} from "./domain/model-ranking.ts";
|
|
70
|
+
export {
|
|
71
|
+
USAGE_PERIODS,
|
|
72
|
+
type CostBucket,
|
|
73
|
+
type CostGraph,
|
|
74
|
+
type CostSeries,
|
|
75
|
+
type UsageAggregateRow,
|
|
76
|
+
type UsageBreakdown,
|
|
77
|
+
type UsageBucket,
|
|
78
|
+
type UsageBucketWindow,
|
|
79
|
+
type UsageGraph,
|
|
80
|
+
type UsageGraphOptions,
|
|
81
|
+
type UsagePeriod,
|
|
82
|
+
type UsageSeries,
|
|
83
|
+
buildCostGraph,
|
|
84
|
+
buildUsageGraph,
|
|
85
|
+
identity,
|
|
86
|
+
resolveUsageWindow,
|
|
87
|
+
usageBucketIndex,
|
|
88
|
+
usagePeriod,
|
|
89
|
+
usagePeriodStart,
|
|
90
|
+
} from "./domain/usage.ts";
|
|
91
|
+
export {
|
|
92
|
+
CodexRecoveryPolicy,
|
|
93
|
+
classifyCodexFailure,
|
|
94
|
+
type CodexFailure,
|
|
95
|
+
type CodexFailureKind,
|
|
96
|
+
type CodexFailureMetadata,
|
|
97
|
+
type CodexRecoveryAttempt,
|
|
98
|
+
type CodexRecoveryOptions,
|
|
99
|
+
type CodexRecoveryPlan,
|
|
100
|
+
} from "./domain/codex-recovery.ts";
|
|
101
|
+
export {
|
|
102
|
+
hasAnthropicRateLimitHeaders,
|
|
103
|
+
parseAnthropicRateLimitHeaders,
|
|
104
|
+
type AnthropicMetricSource,
|
|
105
|
+
type AnthropicRateLimitSnapshot,
|
|
106
|
+
type AnthropicRateLimitWindow,
|
|
107
|
+
} from "./providers/anthropic-contracts.ts";
|
|
108
|
+
export { parseCodexRateLimitHeaders } from "./providers/codex.ts";
|
|
109
|
+
export {
|
|
110
|
+
classifyGoogleVertexFailure,
|
|
111
|
+
googleVertexFailureMetrics,
|
|
112
|
+
type GoogleVertexFailure,
|
|
113
|
+
type GoogleVertexFailureKind,
|
|
114
|
+
type GoogleVertexFailureMetadata,
|
|
115
|
+
type GoogleVertexMetricSource,
|
|
116
|
+
} from "./providers/google-vertex-contracts.ts";
|
|
117
|
+
export {
|
|
118
|
+
evaluateRoutingPolicy,
|
|
119
|
+
type BudgetWindow,
|
|
120
|
+
type PolicyAction,
|
|
121
|
+
type PolicyConfig,
|
|
122
|
+
type PolicyDecision,
|
|
123
|
+
type PolicyInput,
|
|
124
|
+
type PolicyThresholds,
|
|
125
|
+
type PreviousDecision,
|
|
126
|
+
type Route,
|
|
127
|
+
type TelemetryFreshness,
|
|
128
|
+
} from "./policy.ts";
|
|
129
|
+
export type {
|
|
130
|
+
RouteOverride,
|
|
131
|
+
RouterController,
|
|
132
|
+
RouterStatus,
|
|
133
|
+
TelemetryPollResult,
|
|
134
|
+
TelemetrySourceStatus,
|
|
135
|
+
} from "./ports/router-controller.ts";
|
|
136
|
+
export type { DistinctScopesFilter, MetricStore, UsageAggregateFilter } from "./ports/metric-store.ts";
|
|
137
|
+
export { VERSION as jittorVersion } from "./version.ts";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BenchmarkQuery } from "../domain/benchmark.ts";
|
|
2
|
+
import type { BenchmarkController } from "../ports/benchmark-controller.ts";
|
|
3
|
+
import type { OperationHandlerMap } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
/** benchmark.* -- every operation whose only collaborator is the benchmark-controller port. */
|
|
6
|
+
export function benchmarkOperations(benchmarks: BenchmarkController): OperationHandlerMap {
|
|
7
|
+
return {
|
|
8
|
+
"benchmark.refresh": (input) => benchmarks.refresh(input["force"] === true),
|
|
9
|
+
"benchmark.status": () => benchmarks.status(),
|
|
10
|
+
"benchmark.query": (input) => benchmarks.query(input as unknown as BenchmarkQuery),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { COMPACTION_DURATION_ESTIMATE_MAX_SAMPLES, CONTEXT_ASSESSMENT_DEFAULT_WINDOW_MS, CONTEXT_ASSESSMENT_QUERY_LIMIT } from "../constants.ts";
|
|
2
|
+
import { assessContextTelemetry, estimateCompactionDuration } from "../domain/context-telemetry.ts";
|
|
3
|
+
import type { MetricStore } from "../ports/metric-store.ts";
|
|
4
|
+
import type { OperationHandlerMap } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
/** context.assess and compaction.estimate -- both read-only projections of recorded metrics, no router/session involvement. */
|
|
7
|
+
export function contextOperations(metrics: MetricStore): OperationHandlerMap {
|
|
8
|
+
return {
|
|
9
|
+
"context.assess": (input) => {
|
|
10
|
+
const until = input["until"] === undefined ? Date.now() : input["until"];
|
|
11
|
+
const since = input["since"] === undefined && typeof until === "number" ? Math.max(0, until - CONTEXT_ASSESSMENT_DEFAULT_WINDOW_MS) : input["since"];
|
|
12
|
+
if (!Number.isSafeInteger(since) || !Number.isSafeInteger(until) || (since as number) < 0 || (until as number) < (since as number)) throw new Error("context assessment requires non-negative ordered integer bounds");
|
|
13
|
+
const query = { since: since as number, until: until as number, order: "asc" as const, limit: CONTEXT_ASSESSMENT_QUERY_LIMIT };
|
|
14
|
+
const injections = metrics.query({ ...query, source: "papyrus-context", metric: "injected-characters" });
|
|
15
|
+
const compactions = metrics.query({ ...query, source: "pi-context" });
|
|
16
|
+
return assessContextTelemetry(injections, compactions, {
|
|
17
|
+
since: since as number,
|
|
18
|
+
until: until as number,
|
|
19
|
+
truncated: injections.length >= CONTEXT_ASSESSMENT_QUERY_LIMIT || compactions.length >= CONTEXT_ASSESSMENT_QUERY_LIMIT,
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
"compaction.estimate": () => {
|
|
23
|
+
const rows = metrics.query({
|
|
24
|
+
source: "pi-context", scope: "compaction", metric: "compaction-duration",
|
|
25
|
+
order: "desc", limit: COMPACTION_DURATION_ESTIMATE_MAX_SAMPLES,
|
|
26
|
+
});
|
|
27
|
+
return estimateCompactionDuration(rows);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { MAX_USAGE_BUCKETS, METRIC_BATCH_MAX_OBSERVATIONS, PRUNE_MIN_AGE_MS, TASK_COST_QUERY_LIMIT, USAGE_MAX_DISTINCT_SCOPES } from "../constants.ts";
|
|
2
|
+
import { validateMetricObservation, type MetricQuery } from "../domain/metric.ts";
|
|
3
|
+
import { buildTaskCostSummary } from "../domain/task-cost.ts";
|
|
4
|
+
import type { MetricStore } from "../ports/metric-store.ts";
|
|
5
|
+
import type { OperationHandlerMap } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
/** metrics.* and service.checkpoint -- every operation whose only collaborator is the metric-store port. */
|
|
8
|
+
export function metricsOperations(metrics: MetricStore): OperationHandlerMap {
|
|
9
|
+
return {
|
|
10
|
+
"metrics.record": (input) => metrics.record(validateMetricObservation(input)),
|
|
11
|
+
"metrics.record_batch": (input) => {
|
|
12
|
+
const observations = input["observations"];
|
|
13
|
+
if (!Array.isArray(observations) || observations.length === 0) throw new Error("observations must be a non-empty array");
|
|
14
|
+
if (observations.length > METRIC_BATCH_MAX_OBSERVATIONS) throw new Error(`observations must contain at most ${METRIC_BATCH_MAX_OBSERVATIONS} entries`);
|
|
15
|
+
return metrics.recordBatch(observations.map((observation) => validateMetricObservation(observation)));
|
|
16
|
+
},
|
|
17
|
+
"metrics.query": (input) => metrics.query(input as MetricQuery),
|
|
18
|
+
"metrics.distinct_scopes": (input) => {
|
|
19
|
+
const source = input["source"];
|
|
20
|
+
const since = input["since"];
|
|
21
|
+
const until = input["until"];
|
|
22
|
+
if (typeof source !== "string" || source.length === 0) throw new Error("source is required");
|
|
23
|
+
if (!Number.isSafeInteger(since) || !Number.isSafeInteger(until) || (since as number) < 0 || (until as number) < (since as number)) {
|
|
24
|
+
throw new Error("distinct scopes requires non-negative ordered integer bounds");
|
|
25
|
+
}
|
|
26
|
+
const requestedLimit = input["limit"];
|
|
27
|
+
const limit = Number.isFinite(requestedLimit) ? Math.max(1, Math.min(USAGE_MAX_DISTINCT_SCOPES, Math.floor(requestedLimit as number))) : USAGE_MAX_DISTINCT_SCOPES;
|
|
28
|
+
return metrics.distinctScopes({ source, since: since as number, until: until as number, limit });
|
|
29
|
+
},
|
|
30
|
+
"metrics.usage_series": (input) => {
|
|
31
|
+
const source = input["source"];
|
|
32
|
+
const since = input["since"];
|
|
33
|
+
const until = input["until"];
|
|
34
|
+
const bucketSizeMs = input["bucketSizeMs"];
|
|
35
|
+
const bucketCount = input["bucketCount"];
|
|
36
|
+
if (typeof source !== "string" || source.length === 0) throw new Error("source is required");
|
|
37
|
+
if (!Number.isSafeInteger(since) || !Number.isSafeInteger(until) || (since as number) < 0 || (until as number) < (since as number)) {
|
|
38
|
+
throw new Error("usage series requires non-negative ordered integer bounds");
|
|
39
|
+
}
|
|
40
|
+
if (typeof bucketSizeMs !== "number" || !Number.isFinite(bucketSizeMs) || bucketSizeMs <= 0) throw new Error("bucketSizeMs must be a positive number");
|
|
41
|
+
if (!Number.isInteger(bucketCount) || (bucketCount as number) <= 0 || (bucketCount as number) > MAX_USAGE_BUCKETS) {
|
|
42
|
+
throw new Error(`bucketCount must be a positive integer up to ${MAX_USAGE_BUCKETS}`);
|
|
43
|
+
}
|
|
44
|
+
const requestedScopeLimit = input["scopeLimit"];
|
|
45
|
+
const scopeLimit = Number.isFinite(requestedScopeLimit) ? Math.max(1, Math.min(USAGE_MAX_DISTINCT_SCOPES, Math.floor(requestedScopeLimit as number))) : USAGE_MAX_DISTINCT_SCOPES;
|
|
46
|
+
const scopes = metrics.distinctScopes({ source, since: since as number, until: until as number, limit: scopeLimit });
|
|
47
|
+
// More distinct scopes may exist beyond this bounded list -- that is the only remaining
|
|
48
|
+
// truncation risk once aggregation replaces a per-scope raw-row fetch (see aggregateUsage's
|
|
49
|
+
// own doc comment for the incident this was built to stop repeating).
|
|
50
|
+
const truncated = scopes.length >= scopeLimit;
|
|
51
|
+
const rows = scopes.length === 0 ? [] : metrics.aggregateUsage({
|
|
52
|
+
source, scopes, since: since as number, until: until as number, bucketSizeMs, bucketCount: bucketCount as number,
|
|
53
|
+
});
|
|
54
|
+
return { rows, truncated };
|
|
55
|
+
},
|
|
56
|
+
"metrics.cost_by_task": (input) => {
|
|
57
|
+
const since = input["since"];
|
|
58
|
+
const until = input["until"];
|
|
59
|
+
if (!Number.isSafeInteger(since) || !Number.isSafeInteger(until) || (since as number) < 0 || (until as number) < (since as number)) {
|
|
60
|
+
throw new Error("cost by task requires non-negative ordered integer bounds");
|
|
61
|
+
}
|
|
62
|
+
const rows = metrics.query({ source: "pi", since: since as number, until: until as number, order: "desc", limit: TASK_COST_QUERY_LIMIT });
|
|
63
|
+
return buildTaskCostSummary(rows, { since: since as number, until: until as number, truncated: rows.length >= TASK_COST_QUERY_LIMIT });
|
|
64
|
+
},
|
|
65
|
+
"metrics.prune": (input) => {
|
|
66
|
+
const before = input["before"];
|
|
67
|
+
if (typeof before !== "number") throw new Error("before is required");
|
|
68
|
+
const force = input["force"] === true;
|
|
69
|
+
const minCutoff = Date.now() - PRUNE_MIN_AGE_MS;
|
|
70
|
+
if (!force && before > minCutoff) {
|
|
71
|
+
throw new Error(`refusing to prune metrics newer than ${new Date(minCutoff).toISOString()} without force: true (this looked like it could delete recent or live data)`);
|
|
72
|
+
}
|
|
73
|
+
return { deleted: metrics.pruneBefore(before) };
|
|
74
|
+
},
|
|
75
|
+
"service.checkpoint": () => { metrics.checkpoint(); return { ok: true }; },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ModelRanker, ModelRecommendationInput } from "../domain/model-ranking-service.ts";
|
|
2
|
+
import type { RouterController } from "../ports/router-controller.ts";
|
|
3
|
+
import type { OperationHandlerMap } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
/** models.rank -- scores candidates via the model ranker, then, only for an automatic selection, applies it as a router mutation (so it shares the same session-identity authorization as every other router mutation). */
|
|
6
|
+
export function modelRankingOperations(modelRanker: ModelRanker, router: RouterController, authorize: (input: Record<string, unknown>) => string | undefined): OperationHandlerMap {
|
|
7
|
+
return {
|
|
8
|
+
"models.rank": (input) => {
|
|
9
|
+
const result = modelRanker.rank(input as unknown as ModelRecommendationInput);
|
|
10
|
+
if (result.automaticSelection && router.applyModelRanking) {
|
|
11
|
+
router.applyModelRanking(result.ranked.map((item) => item.candidate), authorize(input));
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RouteOverride, RouterController } from "../ports/router-controller.ts";
|
|
2
|
+
import type { Route } from "../policy.ts";
|
|
3
|
+
import type { OperationHandlerMap } from "./types.ts";
|
|
4
|
+
import { routerSessionId } from "./session-scope.ts";
|
|
5
|
+
|
|
6
|
+
/** telemetry.poll and every router.* operation -- reads pass a bare session_id through; mutations run authorize first, matching the opt-in session-identity armor. */
|
|
7
|
+
export function routerOperations(router: RouterController, authorize: (input: Record<string, unknown>) => string | undefined): OperationHandlerMap {
|
|
8
|
+
return {
|
|
9
|
+
"telemetry.poll": () => router.poll(),
|
|
10
|
+
"router.status": (input) => router.status(routerSessionId(input)),
|
|
11
|
+
"router.decide": (input) => router.decide(routerSessionId(input)),
|
|
12
|
+
"router.pause": (input) => router.pause(authorize(input)),
|
|
13
|
+
"router.resume": (input) => router.resume(authorize(input)),
|
|
14
|
+
"router.override": (input) => router.setOverride(input as unknown as RouteOverride, authorize(input)),
|
|
15
|
+
"router.clear_override": (input) => router.clearOverride(authorize(input)),
|
|
16
|
+
"router.current_route": (input) => router.setCurrentRoute(input as unknown as Route, authorize(input)),
|
|
17
|
+
"router.available_routes": (input) => router.setAvailableRoutes(Array.isArray(input["routes"]) ? input["routes"] as Route[] : [], authorize(input)),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SessionIdentity } from "../session-identity-service.ts";
|
|
2
|
+
import type { OperationHandlerMap } from "./types.ts";
|
|
3
|
+
import { requiredString, routerSessionSecret } from "./session-scope.ts";
|
|
4
|
+
|
|
5
|
+
/** session.register and session.release -- the only two operations that mutate SessionIdentity itself, distinct from the router mutations it later authorizes. */
|
|
6
|
+
export function sessionIdentityOperations(sessionIdentity: SessionIdentity | undefined): OperationHandlerMap {
|
|
7
|
+
const require = (): SessionIdentity => {
|
|
8
|
+
if (!sessionIdentity) throw new Error("session identity is not configured");
|
|
9
|
+
return sessionIdentity;
|
|
10
|
+
};
|
|
11
|
+
return {
|
|
12
|
+
"session.register": (input) => require().register(requiredString(input, "session_id")),
|
|
13
|
+
"session.release": (input) => require().release(requiredString(input, "session_id"), routerSessionSecret(input)),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SessionIdentity } from "../session-identity-service.ts";
|
|
2
|
+
|
|
3
|
+
/** Shared input parsing for every operation that accepts an optional session_id/session_secret pair. */
|
|
4
|
+
export function routerSessionId(input: Record<string, unknown>): string | undefined {
|
|
5
|
+
const value = input["session_id"];
|
|
6
|
+
if (value === undefined) return undefined;
|
|
7
|
+
if (typeof value !== "string") throw new Error("session_id must be a string");
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function routerSessionSecret(input: Record<string, unknown>): string | undefined {
|
|
12
|
+
const value = input["session_secret"];
|
|
13
|
+
if (value === undefined) return undefined;
|
|
14
|
+
if (typeof value !== "string") throw new Error("session_secret must be a string");
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function requiredString(input: Record<string, unknown>, key: string): string {
|
|
19
|
+
const value = input[key];
|
|
20
|
+
if (typeof value !== "string" || value.length === 0) throw new Error(`${key} is required`);
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Opt-in armor: a session_id never registered via session.register mutates exactly as before. Bound once per JittorService instance and shared by every router-mutating operation module. */
|
|
25
|
+
export function routerMutationAuthorizer(sessionIdentity: SessionIdentity | undefined): (input: Record<string, unknown>) => string | undefined {
|
|
26
|
+
return (input) => {
|
|
27
|
+
const sessionId = routerSessionId(input);
|
|
28
|
+
sessionIdentity?.assertAuthorized(sessionId, routerSessionSecret(input));
|
|
29
|
+
return sessionId;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** One capability module's contribution to the operation dispatch table: a bounded set of operation names, each backed by a handler that only needs the collaborators its own factory was given. */
|
|
2
|
+
export type OperationHandler = (input: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
3
|
+
export type OperationHandlerMap = Partial<Record<string, OperationHandler>>;
|
|
@@ -20,6 +20,8 @@ export interface UsageAggregateFilter {
|
|
|
20
20
|
|
|
21
21
|
export interface MetricStore {
|
|
22
22
|
record(observation: MetricObservation): StoredMetricObservation;
|
|
23
|
+
/** Validates and writes every observation in one atomic transaction: either all rows land, or none do -- a single-event RPC loop can otherwise leave a partially-persisted event when a later observation in the same event fails validation or the connection drops mid-loop. */
|
|
24
|
+
recordBatch(observations: MetricObservation[]): StoredMetricObservation[];
|
|
23
25
|
query(filter?: MetricQuery): StoredMetricObservation[];
|
|
24
26
|
/** Bounded distinct scope values for a source within a time window, so callers can fetch a fair share per scope instead of one flat query a single heavy scope could monopolize. */
|
|
25
27
|
distinctScopes(filter: DistinctScopesFilter): string[];
|
|
@@ -31,13 +31,13 @@ export interface RouterStatus {
|
|
|
31
31
|
|
|
32
32
|
export interface RouterController {
|
|
33
33
|
poll(): Promise<TelemetryPollResult>;
|
|
34
|
-
status(): RouterStatus;
|
|
35
|
-
decide(): PolicyDecision;
|
|
36
|
-
pause(): RouterStatus;
|
|
37
|
-
resume(): RouterStatus;
|
|
38
|
-
setOverride(override?:
|
|
39
|
-
clearOverride(): RouterStatus;
|
|
40
|
-
setCurrentRoute(route: Route): RouterStatus;
|
|
41
|
-
setAvailableRoutes(routes: Route[]): RouterStatus;
|
|
42
|
-
applyModelRanking?(candidates: Route[]): RouterStatus;
|
|
34
|
+
status(sessionId?: string): RouterStatus;
|
|
35
|
+
decide(sessionId?: string): PolicyDecision;
|
|
36
|
+
pause(sessionId?: string): RouterStatus;
|
|
37
|
+
resume(sessionId?: string): RouterStatus;
|
|
38
|
+
setOverride(override: RouteOverride | undefined, sessionId?: string): RouterStatus;
|
|
39
|
+
clearOverride(sessionId?: string): RouterStatus;
|
|
40
|
+
setCurrentRoute(route: Route, sessionId?: string): RouterStatus;
|
|
41
|
+
setAvailableRoutes(routes: Route[], sessionId?: string): RouterStatus;
|
|
42
|
+
applyModelRanking?(candidates: Route[], sessionId?: string): RouterStatus;
|
|
43
43
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SessionIdentityRecord, SessionIdentityStore as DaemonKitSessionIdentityStore } from "@danypops/daemon-kit/session-identity";
|
|
2
|
+
|
|
3
|
+
/** Jittor's persistence port for daemon-kit's storage-agnostic session-identity primitive. */
|
|
4
|
+
export type SessionIdentityStore = DaemonKitSessionIdentityStore;
|
|
5
|
+
export type { SessionIdentityRecord };
|
|
@@ -79,7 +79,7 @@ export class OpenRouterTelemetrySource implements TelemetrySource {
|
|
|
79
79
|
*/
|
|
80
80
|
export class GoogleVertexBudgetTelemetrySource implements TelemetrySource {
|
|
81
81
|
readonly id: string;
|
|
82
|
-
readonly provider
|
|
82
|
+
readonly provider: GoogleVertexMetricSource;
|
|
83
83
|
readonly required = false;
|
|
84
84
|
|
|
85
85
|
private readonly adapter: GoogleVertexBudgetTelemetryAdapter;
|
|
@@ -92,6 +92,7 @@ export class GoogleVertexBudgetTelemetrySource implements TelemetrySource {
|
|
|
92
92
|
source: GoogleVertexMetricSource = "google-vertex",
|
|
93
93
|
) {
|
|
94
94
|
this.id = `google-vertex-budget:${source}`;
|
|
95
|
+
this.provider = source;
|
|
95
96
|
this.adapter = new GoogleVertexBudgetTelemetryAdapter(subscription, tokenProvider, transport, source);
|
|
96
97
|
}
|
|
97
98
|
|