sigrank 0.19.0 → 1.0.2
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/CHANGELOG.md +158 -0
- package/MCP_SERVER_DESCRIPTION.md +172 -0
- package/README.md +89 -41
- package/adapters/index.mjs +1100 -0
- package/adapters/tokenpull.mjs +568 -0
- package/adapters.mjs +1 -865
- package/analytics/cascade.mjs +338 -0
- package/bin/sigrank.mjs +137 -0
- package/cascade.mjs +1 -274
- package/cli.mjs +1 -1853
- package/connect.mjs +1 -13
- package/identity/connect.mjs +13 -0
- package/identity/keystore.mjs +231 -0
- package/identity/sign.mjs +106 -0
- package/index.mjs +47 -162
- package/keystore.mjs +1 -242
- package/lib/constants.mjs +137 -0
- package/lib/pkg-version.mjs +59 -0
- package/manifest.json +46 -0
- package/narrate.mjs +1 -77
- package/package.json +44 -18
- package/preflight.mjs +1 -191
- package/presentation/cli.mjs +1926 -0
- package/presentation/narrate.mjs +219 -0
- package/presentation/tui-themes.mjs +189 -0
- package/presentation/tui.mjs +3316 -0
- package/prompts/check-my-efficiency.md +1 -0
- package/prompts/compare-with-leader.md +1 -0
- package/prompts/simulate-improvement.md +1 -0
- package/proxy.mjs +492 -0
- package/resources/class-tiers.md +81 -0
- package/resources/data-policy.md +31 -0
- package/resources/install-guide.md +59 -0
- package/resources/privacy-model.md +44 -0
- package/resources/scoring-formula.md +24 -0
- package/review.mjs +263 -0
- package/sign.mjs +1 -106
- package/submit/index.mjs +297 -0
- package/submit/preflight.mjs +191 -0
- package/submit.mjs +1 -303
- package/tokenpull.mjs +1 -576
- package/tokscale_analytics.mjs +864 -0
- package/tools/_framing.mjs +217 -0
- package/tools/_helpers.mjs +253 -0
- package/tools/_peer-matching.mjs +361 -0
- package/tools/_report.mjs +262 -0
- package/tools/_schemas.mjs +492 -0
- package/tools/_verifiers.mjs +148 -0
- package/tools/compare-operators.mjs +80 -0
- package/tools/compare-self.mjs +124 -0
- package/tools/describe-power-user.mjs +78 -0
- package/tools/diagnose-cascade.mjs +192 -0
- package/tools/discover-peers.mjs +198 -0
- package/tools/enroll.mjs +119 -0
- package/tools/get-best-operator.mjs +59 -0
- package/tools/get-leaderboard.mjs +27 -0
- package/tools/get-operator.mjs +33 -0
- package/tools/index.mjs +233 -0
- package/tools/optimize-efficiency.mjs +170 -0
- package/tools/rank-paste.mjs +51 -0
- package/tools/rank-windows.mjs +120 -0
- package/tools/self-improve.mjs +423 -0
- package/tools/simulate-change.mjs +207 -0
- package/tools/standard-record.mjs +108 -0
- package/tools/submit-paste.mjs +121 -0
- package/tools/submit-verified.mjs +153 -0
- package/tools/suggest-improvements.mjs +168 -0
- package/tools/tokenpull-compare.mjs +123 -0
- package/tools/tokenpull-submit.mjs +235 -0
- package/tools/tokenpull.mjs +113 -0
- package/tools/tokscale-breakdown.mjs +119 -0
- package/tools/tokscale-competitive-intel.mjs +45 -0
- package/tools/tokscale-cost-analysis.mjs +31 -0
- package/tools/tokscale-developer-profile.mjs +36 -0
- package/tools/tokscale-device-profile.mjs +35 -0
- package/tools/tokscale-market-share.mjs +38 -0
- package/tools/tokscale-mcp-usage.mjs +33 -0
- package/tools/tokscale-model-trends.mjs +32 -0
- package/tools/watch-tokenpull.mjs +134 -0
- package/tools.mjs +1 -3897
- package/tui.mjs +4 -2650
- package/banner.svg +0 -7
package/submit/index.mjs
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// submit.mjs — build + ed25519-sign a Schema 1.0 snapshot payload and POST it to the
|
|
2
|
+
// VERIFIED ingest endpoint (D7 §5). Requires an enrolled identity (keystore.codename +
|
|
3
|
+
// operator_id + private key). The server re-derives the ranked Υ from the 4 raw pillars,
|
|
4
|
+
// so the core/background metrics here are schema-valid, plausibility-clean DISPLAY inputs.
|
|
5
|
+
//
|
|
6
|
+
// raw_telemetry sessions/turns/active are conservative PROXIES: tokenpull yields the 4
|
|
7
|
+
// pillars + a message count, not session structure. They are chosen to pass the
|
|
8
|
+
// plausibility gate WITHOUT flagging (a flag downgrades verified→flagged → not ranked).
|
|
9
|
+
// TODO(tokenpull): emit real sessions_count / turns_total / active_minutes per window so
|
|
10
|
+
// the display signa_rate reflects true session shape (the ranked Υ is already exact).
|
|
11
|
+
|
|
12
|
+
import { snapshotHash, signPayload } from "../identity/sign.mjs";
|
|
13
|
+
import { preflight } from "./preflight.mjs";
|
|
14
|
+
import { pkgVersion } from "../lib/pkg-version.mjs";
|
|
15
|
+
import {
|
|
16
|
+
TERMS_VERSION,
|
|
17
|
+
PRIVACY_VERSION,
|
|
18
|
+
isRankedAck,
|
|
19
|
+
} from "../lib/constants.mjs";
|
|
20
|
+
|
|
21
|
+
const WINDOW_TYPE = { "7d": "7d", "30d": "30d", "90d": "90d", all: "all_time" };
|
|
22
|
+
const WINDOW_SPAN_DAYS = { "7d": 7, "30d": 30, "90d": 90, all_time: 3650 };
|
|
23
|
+
const PLATFORM_ENUM = new Set([
|
|
24
|
+
"claude",
|
|
25
|
+
"chatgpt",
|
|
26
|
+
"gemini",
|
|
27
|
+
"pi",
|
|
28
|
+
"omp",
|
|
29
|
+
"codex",
|
|
30
|
+
"multi",
|
|
31
|
+
"other",
|
|
32
|
+
]);
|
|
33
|
+
// Provenance tag only — the board ranks by Υ (no RS.xx weights, §0.1).
|
|
34
|
+
const RULESET_VERSION = "sigrank-token-1";
|
|
35
|
+
const DAY_MS = 86_400_000;
|
|
36
|
+
|
|
37
|
+
const clamp = (x, lo, hi) => Math.max(lo, Math.min(hi, x));
|
|
38
|
+
const round = (n, dp) => {
|
|
39
|
+
const f = 10 ** dp;
|
|
40
|
+
return Math.round(n * f) / f;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Map any tokenpull platform to the Schema 1.0 platform.primary enum (others → 'other'). */
|
|
44
|
+
export function toPlatformPrimary(platform) {
|
|
45
|
+
return PLATFORM_ENUM.has(platform) ? platform : "other";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* buildPayload — Schema 1.0 payload from a tokenpull window + enrolled identity.
|
|
50
|
+
* Pure (given opts.now); agent.snapshot_hash is computed last (canonical strips it).
|
|
51
|
+
*/
|
|
52
|
+
export function buildPayload(
|
|
53
|
+
windowKey,
|
|
54
|
+
pillars,
|
|
55
|
+
messages,
|
|
56
|
+
identity,
|
|
57
|
+
platform,
|
|
58
|
+
opts = {},
|
|
59
|
+
) {
|
|
60
|
+
const windowType = WINDOW_TYPE[windowKey] || windowKey;
|
|
61
|
+
const spanDays = WINDOW_SPAN_DAYS[windowType] || 30;
|
|
62
|
+
const nowMs = opts.now ?? Date.now();
|
|
63
|
+
|
|
64
|
+
const input = Math.max(0, Math.round(pillars?.input || 0));
|
|
65
|
+
const output = Math.max(0, Math.round(pillars?.output || 0));
|
|
66
|
+
const cacheCreate = Math.max(0, Math.round(pillars?.cacheCreate || 0));
|
|
67
|
+
const cacheRead = Math.max(0, Math.round(pillars?.cacheRead || 0));
|
|
68
|
+
const tokensTotal = input + output + cacheCreate + cacheRead;
|
|
69
|
+
|
|
70
|
+
// Proxies — plausibility-clean (see file header).
|
|
71
|
+
const msgs = Math.max(0, Math.round(messages || 0));
|
|
72
|
+
const turnsTotal = Math.max(output > 0 ? 1 : 0, msgs);
|
|
73
|
+
const sessionsCount =
|
|
74
|
+
tokensTotal > 0
|
|
75
|
+
? clamp(Math.round(msgs / 8) || 1, 1, Math.max(1, turnsTotal))
|
|
76
|
+
: 0;
|
|
77
|
+
const spanMinutes = spanDays * 1440;
|
|
78
|
+
const minActive = Math.ceil(output / 19000) + 1; // keep output/active under the 20k/min flag
|
|
79
|
+
const activeMinutes = clamp(
|
|
80
|
+
Math.max(msgs, minActive),
|
|
81
|
+
1,
|
|
82
|
+
Math.max(1, spanMinutes - 1),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const compressionRatio =
|
|
86
|
+
input + output > 0 ? clamp(output / (input + output), 0, 1) : 0;
|
|
87
|
+
const sessionDepth = sessionsCount > 0 ? turnsTotal / sessionsCount : 0;
|
|
88
|
+
|
|
89
|
+
const payload = {
|
|
90
|
+
schema_version: "1.0",
|
|
91
|
+
codename: identity.codename,
|
|
92
|
+
device_id: identity.device_id,
|
|
93
|
+
submitted_at: new Date(nowMs).toISOString(),
|
|
94
|
+
consent_acknowledged: true,
|
|
95
|
+
terms_version: TERMS_VERSION,
|
|
96
|
+
privacy_version: PRIVACY_VERSION,
|
|
97
|
+
window: {
|
|
98
|
+
type: windowType,
|
|
99
|
+
start: new Date(nowMs - spanDays * DAY_MS).toISOString(),
|
|
100
|
+
end: new Date(nowMs).toISOString(),
|
|
101
|
+
},
|
|
102
|
+
platform: { primary: toPlatformPrimary(platform), models: [] },
|
|
103
|
+
core_metrics: {
|
|
104
|
+
compression_ratio: round(compressionRatio, 4),
|
|
105
|
+
prompt_complexity: round(clamp(sessionDepth, 0, 100), 2),
|
|
106
|
+
cross_thread_score: clamp(
|
|
107
|
+
Math.round(cacheRead / Math.max(cacheCreate, 1)),
|
|
108
|
+
0,
|
|
109
|
+
100000,
|
|
110
|
+
),
|
|
111
|
+
session_depth_avg: round(Math.max(0, sessionDepth), 2),
|
|
112
|
+
// M.05 token_throughput muted 2026-06-26 — word-era metric the web already
|
|
113
|
+
// mutes (RS01 tt→0) + strips. Send null (RPC p_token_throughput is nullable
|
|
114
|
+
// DEFAULT NULL). raw_telemetry.tokens_total still carries the raw count.
|
|
115
|
+
token_throughput: null,
|
|
116
|
+
},
|
|
117
|
+
background_metrics: {
|
|
118
|
+
message_volume: msgs,
|
|
119
|
+
account_age_days: Math.max(1, Math.round(opts.accountAgeDays ?? 365)),
|
|
120
|
+
total_messages_lifetime: Math.max(
|
|
121
|
+
msgs,
|
|
122
|
+
Math.round(opts.totalMessages ?? msgs),
|
|
123
|
+
),
|
|
124
|
+
},
|
|
125
|
+
raw_telemetry: {
|
|
126
|
+
sessions_count: sessionsCount,
|
|
127
|
+
turns_total: turnsTotal,
|
|
128
|
+
tokens_total: tokensTotal,
|
|
129
|
+
tokens_input_fresh: input,
|
|
130
|
+
tokens_output: output,
|
|
131
|
+
tokens_cache_read: cacheRead,
|
|
132
|
+
tokens_cache_creation: cacheCreate,
|
|
133
|
+
active_minutes_est: activeMinutes,
|
|
134
|
+
},
|
|
135
|
+
tier: "free",
|
|
136
|
+
agent: {
|
|
137
|
+
version: identity.agent_version,
|
|
138
|
+
ruleset_version: RULESET_VERSION,
|
|
139
|
+
snapshot_hash: "",
|
|
140
|
+
public_key: identity.public_key,
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Cascade Report block (Phase 1 — mode detection + badges + health).
|
|
145
|
+
// Computed locally (pure math), submitted alongside the 4 token pillars.
|
|
146
|
+
// The server stores it as-is — does NOT recompute modes.
|
|
147
|
+
// Weekly granularity is the privacy boundary — daily modes never leave the machine.
|
|
148
|
+
if (opts.report && typeof opts.report === "object") {
|
|
149
|
+
payload.report = opts.report;
|
|
150
|
+
}
|
|
151
|
+
payload.agent.snapshot_hash = snapshotHash(payload);
|
|
152
|
+
return payload;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* submitSignedWindow — build + sign + POST one window to /api/v1/snapshots.
|
|
157
|
+
* Returns a structured result; never throws on a network error. opts: { apiBase,
|
|
158
|
+
* fetchImpl, platform, now } (fetchImpl lets the caller inject a timeout-wrapped fetch).
|
|
159
|
+
*/
|
|
160
|
+
export async function submitSignedWindow(
|
|
161
|
+
windowKey,
|
|
162
|
+
pillars,
|
|
163
|
+
messages,
|
|
164
|
+
identity,
|
|
165
|
+
opts = {},
|
|
166
|
+
) {
|
|
167
|
+
if (!identity?.codename || !identity?.operator_id) {
|
|
168
|
+
return {
|
|
169
|
+
status: "not_enrolled",
|
|
170
|
+
reason: "not_enrolled",
|
|
171
|
+
detail: "Run `npx sigrank-mcp enroll` to bind this device first.",
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (!identity?.private_key_pkcs8_b64) {
|
|
175
|
+
return {
|
|
176
|
+
status: "error",
|
|
177
|
+
reason: "no_key",
|
|
178
|
+
detail: "No local signing key — re-run `npx sigrank-mcp enroll`.",
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const apiBase =
|
|
183
|
+
opts.apiBase || process.env.SIGRANK_API_BASE || "https://signalaf.com";
|
|
184
|
+
// FIX M: default a 15s AbortController timeout when the caller doesn't inject a
|
|
185
|
+
// fetchImpl. The TUI path already wraps fetch (callTool → doFetch, 10s), but the
|
|
186
|
+
// legacy CLI `watch --submit` + the CLI default-view [S] call submitSignedWindow
|
|
187
|
+
// with no fetchImpl → bare fetch → a hung POST blocks forever. This default fixes
|
|
188
|
+
// it everywhere at once. (15s is generous for a signed POST to /api/v1/snapshots.)
|
|
189
|
+
const fetchImpl =
|
|
190
|
+
opts.fetchImpl ||
|
|
191
|
+
((url, init = {}) => {
|
|
192
|
+
const ctrl = new AbortController();
|
|
193
|
+
const timer = setTimeout(() => ctrl.abort(), 15_000);
|
|
194
|
+
const headers = {
|
|
195
|
+
"user-agent": `node/${process.version} sigrank-mcp/${pkgVersion()}`,
|
|
196
|
+
...(init.headers || {}),
|
|
197
|
+
};
|
|
198
|
+
return fetch(url, {
|
|
199
|
+
...init,
|
|
200
|
+
headers,
|
|
201
|
+
signal: init.signal || ctrl.signal,
|
|
202
|
+
}).finally(() => clearTimeout(timer));
|
|
203
|
+
});
|
|
204
|
+
const payload = buildPayload(
|
|
205
|
+
windowKey,
|
|
206
|
+
pillars,
|
|
207
|
+
messages,
|
|
208
|
+
identity,
|
|
209
|
+
opts.platform || "claude",
|
|
210
|
+
opts,
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
// Preflight: run the same anti-gaming checks the server will run, locally.
|
|
214
|
+
// If the payload would be rejected or downgraded, warn the operator BEFORE
|
|
215
|
+
// submitting. The server always runs its own checks — this is a preview.
|
|
216
|
+
const pre = preflight(payload);
|
|
217
|
+
if (!pre.pass && !opts.skipPreflight) {
|
|
218
|
+
if (pre.wouldReject) {
|
|
219
|
+
return {
|
|
220
|
+
status: "preflight_rejected",
|
|
221
|
+
window: WINDOW_TYPE[windowKey] || windowKey,
|
|
222
|
+
preflight: pre,
|
|
223
|
+
detail: `Submission would be REJECTED by the server: ${pre.summary}. Fix the issue or re-run with skipPreflight.`,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
// Flags are internal signals on the server — they no longer block ranking.
|
|
227
|
+
// Warn the operator but don't block unless strict mode is explicitly set.
|
|
228
|
+
if (opts.strictPreflight) {
|
|
229
|
+
return {
|
|
230
|
+
status: "preflight_flagged",
|
|
231
|
+
window: WINDOW_TYPE[windowKey] || windowKey,
|
|
232
|
+
preflight: pre,
|
|
233
|
+
detail: `Submission would be DOWNGRADED (not ranked): ${pre.summary}. Fix the issue or re-run with skipPreflight.`,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
// Non-strict: attach the preflight warning to the result but proceed.
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const signature = signPayload(payload, identity.private_key_pkcs8_b64);
|
|
240
|
+
|
|
241
|
+
// Dry run: build + sign exactly as a real publish, then STOP before the POST.
|
|
242
|
+
// Returns the exact payload that would be sent — the privacy proof ("token
|
|
243
|
+
// counts only") is inspectable, and nothing touches the network.
|
|
244
|
+
if (opts.dryRun) {
|
|
245
|
+
return {
|
|
246
|
+
status: "dry_run",
|
|
247
|
+
window: WINDOW_TYPE[windowKey] || windowKey,
|
|
248
|
+
would_post: `${apiBase}/api/v1/snapshots`,
|
|
249
|
+
payload,
|
|
250
|
+
signature,
|
|
251
|
+
preflight: pre,
|
|
252
|
+
detail:
|
|
253
|
+
"By submitting, you agree to the SignalAF Terms of Service and Privacy Policy. Nothing sent. Re-run without dry_run to publish.",
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
let res;
|
|
258
|
+
let ack;
|
|
259
|
+
try {
|
|
260
|
+
res = await fetchImpl(`${apiBase}/api/v1/snapshots`, {
|
|
261
|
+
method: "POST",
|
|
262
|
+
headers: {
|
|
263
|
+
"content-type": "application/json",
|
|
264
|
+
accept: "application/json",
|
|
265
|
+
"x-agent-signature": signature,
|
|
266
|
+
},
|
|
267
|
+
body: JSON.stringify(payload),
|
|
268
|
+
});
|
|
269
|
+
try {
|
|
270
|
+
ack = await res.json();
|
|
271
|
+
} catch {
|
|
272
|
+
ack = {};
|
|
273
|
+
}
|
|
274
|
+
} catch (e) {
|
|
275
|
+
return {
|
|
276
|
+
status: "error",
|
|
277
|
+
reason: "network",
|
|
278
|
+
detail: e.message,
|
|
279
|
+
window: WINDOW_TYPE[windowKey] || windowKey,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
status: res.ok ? ack.status || "received" : "error",
|
|
285
|
+
httpStatus: res.status,
|
|
286
|
+
window: WINDOW_TYPE[windowKey] || windowKey,
|
|
287
|
+
verification_tier: ack.verification_tier ?? null,
|
|
288
|
+
persisted: ack.persisted ?? null,
|
|
289
|
+
// ranked = actually on the board (verified + written), not just "received".
|
|
290
|
+
// An unenrolled/revoked device gets HTTP 202 received but is NEVER ranked.
|
|
291
|
+
ranked: isRankedAck(res, ack),
|
|
292
|
+
snapshot_hash: payload.agent.snapshot_hash,
|
|
293
|
+
reason: res.ok ? null : ack.reason || ack.status || `http_${res.status}`,
|
|
294
|
+
detail: ack.detail ?? null,
|
|
295
|
+
preflight: pre,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* preflight.mjs — local plausibility pre-checks for the MCP submit path.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors ONLY the server's public plausibility gate (gates.ts:86-137) —
|
|
5
|
+
* totals consistency, turns/sessions ratios, output rate, cache ratio,
|
|
6
|
+
* cadence. These are integrity guards labeled "NOT the proprietary RS.xx"
|
|
7
|
+
* and are safe to replicate in an open agent.
|
|
8
|
+
*
|
|
9
|
+
* The proprietary battery (Benford / cadence / contamination) is SERVER-ONLY
|
|
10
|
+
* and must NOT be shipped in the agent. The server always runs its own
|
|
11
|
+
* battery after the POST — this preflight is a client-side preview of the
|
|
12
|
+
* public plausibility checks only, not a replacement for the full gate chain.
|
|
13
|
+
*
|
|
14
|
+
* If preflight flags, the operator sees WHY their submission would be
|
|
15
|
+
* rejected or flagged, and can fix the issue or submit anyway.
|
|
16
|
+
*
|
|
17
|
+
* Source of truth: lib/ingest/gates.ts (plausibilityGate). This port must
|
|
18
|
+
* stay in sync with the plausibility gate ONLY.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// ── plausibility checks (from gates.ts:86-137) ──────────────────────────────
|
|
22
|
+
|
|
23
|
+
const GATE_LIMITS = {
|
|
24
|
+
TOTALS_TOLERANCE_FRAC: 0.01,
|
|
25
|
+
MAX_OUTPUT_TOKENS_PER_MIN: 20_000,
|
|
26
|
+
// Tightened range-plausibility bounds (deviewreview3)
|
|
27
|
+
MAX_CACHE_REUSE_RATIO: 35,
|
|
28
|
+
MIN_CACHE_WRITE_RATIO: 0.5,
|
|
29
|
+
MIN_INPUT_SHARE_FRAC: 0.0003,
|
|
30
|
+
MAX_CADENCE_PER_MIN: 15,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Run plausibility checks against a payload's raw_telemetry.
|
|
35
|
+
* Returns array of issues: { severity, code, detail }
|
|
36
|
+
*/
|
|
37
|
+
export function plausibilityCheck(rt, window) {
|
|
38
|
+
const out = [];
|
|
39
|
+
const pillars =
|
|
40
|
+
rt.tokens_input_fresh +
|
|
41
|
+
rt.tokens_output +
|
|
42
|
+
rt.tokens_cache_read +
|
|
43
|
+
rt.tokens_cache_creation;
|
|
44
|
+
|
|
45
|
+
if (rt.tokens_total > 0) {
|
|
46
|
+
const tol = Math.max(
|
|
47
|
+
1,
|
|
48
|
+
rt.tokens_total * GATE_LIMITS.TOTALS_TOLERANCE_FRAC,
|
|
49
|
+
);
|
|
50
|
+
if (Math.abs(pillars - rt.tokens_total) > tol) {
|
|
51
|
+
out.push({
|
|
52
|
+
severity: "reject",
|
|
53
|
+
code: "totals_inconsistent",
|
|
54
|
+
detail: `Σ4 pillars (${pillars}) ≠ tokens_total (${rt.tokens_total})`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (rt.sessions_count === 0) {
|
|
58
|
+
out.push({
|
|
59
|
+
severity: "reject",
|
|
60
|
+
code: "tokens_without_sessions",
|
|
61
|
+
detail: `tokens_total ${rt.tokens_total} with sessions_count 0`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (rt.turns_total < rt.sessions_count) {
|
|
66
|
+
out.push({
|
|
67
|
+
severity: "reject",
|
|
68
|
+
code: "turns_lt_sessions",
|
|
69
|
+
detail: `turns_total ${rt.turns_total} < sessions_count ${rt.sessions_count}`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (rt.tokens_output > 0 && rt.turns_total === 0) {
|
|
73
|
+
out.push({
|
|
74
|
+
severity: "reject",
|
|
75
|
+
code: "output_without_turns",
|
|
76
|
+
detail: `tokens_output ${rt.tokens_output} with turns_total 0`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (window) {
|
|
81
|
+
const spanMin =
|
|
82
|
+
(Date.parse(window.end) - Date.parse(window.start)) / 60_000;
|
|
83
|
+
if (Number.isFinite(spanMin) && rt.active_minutes_est > spanMin + 1) {
|
|
84
|
+
out.push({
|
|
85
|
+
severity: "flag",
|
|
86
|
+
code: "active_exceeds_window",
|
|
87
|
+
detail: `active_minutes_est ${rt.active_minutes_est} > window span ${Math.round(spanMin)}m`,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const outPerMin = rt.tokens_output / Math.max(rt.active_minutes_est, 1);
|
|
93
|
+
if (outPerMin > GATE_LIMITS.MAX_OUTPUT_TOKENS_PER_MIN) {
|
|
94
|
+
out.push({
|
|
95
|
+
severity: "flag",
|
|
96
|
+
code: "implausible_output_rate",
|
|
97
|
+
detail: `${Math.round(outPerMin)} output tok/min > ${GATE_LIMITS.MAX_OUTPUT_TOKENS_PER_MIN}`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Cross-field ratio checks (defense-in-depth — also in the plausibility gate)
|
|
102
|
+
// Bounds tightened (deviewreview3): original 100:1 + 50/min were too loose.
|
|
103
|
+
if (rt.tokens_cache_read > 1_000 && rt.tokens_cache_creation === 0) {
|
|
104
|
+
out.push({
|
|
105
|
+
severity: "flag",
|
|
106
|
+
code: "cache_without_creation",
|
|
107
|
+
detail: `${rt.tokens_cache_read} cache_read with 0 cache_creation (impossible cascade)`,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (
|
|
111
|
+
rt.tokens_cache_creation > 0 &&
|
|
112
|
+
rt.tokens_cache_read / rt.tokens_cache_creation >
|
|
113
|
+
GATE_LIMITS.MAX_CACHE_REUSE_RATIO
|
|
114
|
+
) {
|
|
115
|
+
out.push({
|
|
116
|
+
severity: "flag",
|
|
117
|
+
code: "extreme_cache_ratio",
|
|
118
|
+
detail: `cache_read/cache_creation = ${(rt.tokens_cache_read / rt.tokens_cache_creation).toFixed(1)}:1 (real max ~30:1)`,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (
|
|
122
|
+
rt.tokens_output > 1_000 &&
|
|
123
|
+
rt.tokens_cache_creation / rt.tokens_output <
|
|
124
|
+
GATE_LIMITS.MIN_CACHE_WRITE_RATIO
|
|
125
|
+
) {
|
|
126
|
+
out.push({
|
|
127
|
+
severity: "flag",
|
|
128
|
+
code: "low_cache_write_ratio",
|
|
129
|
+
detail: `cache_creation/output = ${(rt.tokens_cache_creation / rt.tokens_output).toFixed(2)}:1 (real min ~1.5:1)`,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (
|
|
133
|
+
pillars > 10_000 &&
|
|
134
|
+
rt.tokens_input_fresh / pillars < GATE_LIMITS.MIN_INPUT_SHARE_FRAC
|
|
135
|
+
) {
|
|
136
|
+
out.push({
|
|
137
|
+
severity: "flag",
|
|
138
|
+
code: "implausible_input_share",
|
|
139
|
+
detail: `input is ${((rt.tokens_input_fresh / pillars) * 100).toFixed(3)}% of total (real min ~0.3%)`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (
|
|
143
|
+
rt.active_minutes_est > 0 &&
|
|
144
|
+
rt.turns_total / rt.active_minutes_est > GATE_LIMITS.MAX_CADENCE_PER_MIN
|
|
145
|
+
) {
|
|
146
|
+
out.push({
|
|
147
|
+
severity: "flag",
|
|
148
|
+
code: "implausible_cadence",
|
|
149
|
+
detail: `${(rt.turns_total / rt.active_minutes_est).toFixed(1)} turns/min (real: 0.5-10)`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── full preflight (plausibility only — NO battery) ─────────────────────────
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Run preflight plausibility checks against a payload. Returns:
|
|
160
|
+
* { pass: true, issues: [] } — clean submission
|
|
161
|
+
* { pass: false, issues: [...], wouldDowngrade } — would be flagged/rejected
|
|
162
|
+
*
|
|
163
|
+
* NOTE: this only mirrors the PUBLIC plausibility gate. The server runs
|
|
164
|
+
* additional proprietary checks (the battery) that this preflight does NOT
|
|
165
|
+
* replicate. A "pass" here means "passes the plausibility gate," not
|
|
166
|
+
* "guaranteed to pass all server gates."
|
|
167
|
+
*/
|
|
168
|
+
export function preflight(payload) {
|
|
169
|
+
const issues = plausibilityCheck(payload.raw_telemetry, payload.window);
|
|
170
|
+
|
|
171
|
+
const hasReject = issues.some((i) => i.severity === "reject");
|
|
172
|
+
const hasFlag = issues.some((i) => i.severity === "flag");
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
pass: issues.length === 0,
|
|
176
|
+
wouldReject: hasReject,
|
|
177
|
+
wouldDowngrade: hasFlag,
|
|
178
|
+
issues,
|
|
179
|
+
summary: hasReject
|
|
180
|
+
? `REJECT: ${issues
|
|
181
|
+
.filter((i) => i.severity === "reject")
|
|
182
|
+
.map((i) => i.code)
|
|
183
|
+
.join(", ")}`
|
|
184
|
+
: hasFlag
|
|
185
|
+
? `FLAG (verified → flagged, not ranked): ${issues
|
|
186
|
+
.filter((i) => i.severity === "flag")
|
|
187
|
+
.map((i) => i.code)
|
|
188
|
+
.join(", ")}`
|
|
189
|
+
: "clean — passes plausibility gate (server runs additional proprietary checks)",
|
|
190
|
+
};
|
|
191
|
+
}
|