@jjanczur/tyran 0.1.31 → 0.1.32
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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +292 -0
- package/README.md +16 -4
- package/agents/reviewer.md +56 -9
- package/agents/scout.md +8 -2
- package/bin/tyran.mjs +8 -0
- package/hooks/scripts/policy-gate.mjs +22 -1
- package/hooks/scripts/secrets-gate.mjs +147 -11
- package/hooks/scripts/usage-gate.mjs +46 -12
- package/package.json +1 -1
- package/scripts/board-html.mjs +134 -7
- package/scripts/board.mjs +59 -7
- package/scripts/cost.mjs +392 -23
- package/scripts/doctor.mjs +155 -9
- package/scripts/ensure-gitleaks.mjs +254 -0
- package/scripts/journal.mjs +39 -2
- package/scripts/knowledge.mjs +89 -2
- package/scripts/overnight.mjs +2 -2
- package/scripts/pricing.mjs +255 -0
- package/scripts/scan-repo.mjs +120 -1
- package/scripts/schema.mjs +30 -6
- package/scripts/statusline.mjs +4 -1
- package/scripts/tiers.mjs +24 -0
- package/scripts/usage-source.mjs +135 -0
- package/skills/code-review/SKILL.md +1 -1
- package/skills/setup/SKILL.md +96 -10
|
@@ -62,9 +62,9 @@
|
|
|
62
62
|
* It is never interpolated into another command and never expanded.
|
|
63
63
|
*/
|
|
64
64
|
import { spawn } from 'node:child_process';
|
|
65
|
-
import { realpathSync } from 'node:fs';
|
|
65
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
66
66
|
import { mkdtemp, readFile, rm, stat } from 'node:fs/promises';
|
|
67
|
-
import { tmpdir } from 'node:os';
|
|
67
|
+
import { homedir, tmpdir } from 'node:os';
|
|
68
68
|
import { basename, dirname, join, resolve as resolvePath } from 'node:path';
|
|
69
69
|
import { fileURLToPath } from 'node:url';
|
|
70
70
|
|
|
@@ -110,8 +110,83 @@ const MAX_FINDINGS_SHOWN = 10;
|
|
|
110
110
|
export const MAX_PATH_CHARS = 120;
|
|
111
111
|
export const MAX_RULE_CHARS = 60;
|
|
112
112
|
|
|
113
|
-
/**
|
|
114
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Where the scanner lives, in order of authority.
|
|
115
|
+
*
|
|
116
|
+
* 1. TYRAN_GITLEAKS_BIN — the operator pointing at a build they chose.
|
|
117
|
+
* 2. `gitleaks` on PATH — the machine's own, if it has one.
|
|
118
|
+
* 3. ~/.tyran/bin/gitleaks — the pinned build `scripts/ensure-gitleaks.mjs`
|
|
119
|
+
* fetched and checksummed, for a machine that had none.
|
|
120
|
+
*
|
|
121
|
+
* Step 3 exists because the prerequisite was the wall. This gate refuses
|
|
122
|
+
* every commit and push until a scanner exists, and it refuses rather than
|
|
123
|
+
* warning for a reason that has not changed — but the design assumed the
|
|
124
|
+
* operator could install a binary, and for the people now adopting Tyran that
|
|
125
|
+
* is often false: no Homebrew, no admin rights, Windows. Their realistic
|
|
126
|
+
* response to "install gitleaks and re-run" is to switch this hook off, and a
|
|
127
|
+
* gate nobody can satisfy protects nothing.
|
|
128
|
+
*
|
|
129
|
+
* It is the HOME directory and deliberately not `<repo>/.tyran/bin/`, which
|
|
130
|
+
* was the first shape of this and is worse in a way worth recording: a path
|
|
131
|
+
* inside the repository travels with a clone, so a planted binary that
|
|
132
|
+
* reports clean on everything would arrive with the checkout — an attack the
|
|
133
|
+
* tracked-only rule for `.gitleaksignore` already exists to prevent, reopened
|
|
134
|
+
* through a file that is not even in a diff. Under the home directory the
|
|
135
|
+
* scanner is the operator's machine, exactly like PATH, and one install
|
|
136
|
+
* serves every repository on it.
|
|
137
|
+
*
|
|
138
|
+
* PATH is consulted BEFORE it: a gitleaks the operator installed themselves
|
|
139
|
+
* is their decision, and a newer one should not be silently displaced by
|
|
140
|
+
* whatever version Tyran happens to pin.
|
|
141
|
+
*/
|
|
142
|
+
export function gitleaksBin() {
|
|
143
|
+
const explicit = process.env.TYRAN_GITLEAKS_BIN;
|
|
144
|
+
if (typeof explicit === 'string' && explicit !== '') return explicit;
|
|
145
|
+
if (onPath('gitleaks')) return 'gitleaks';
|
|
146
|
+
return managedScanner() ?? 'gitleaks';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Backwards-compatible alias: this was a zero-argument arrow. */
|
|
150
|
+
export const GITLEAKS_BIN = gitleaksBin;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Is a name resolvable through PATH? Checked by looking rather than by
|
|
154
|
+
* spawning: this runs before every scan, and a spawn to answer "does a spawn
|
|
155
|
+
* work" costs more than the lookup it is standing in for.
|
|
156
|
+
*/
|
|
157
|
+
function onPath(name) {
|
|
158
|
+
const entries = String(process.env.PATH ?? '').split(process.platform === 'win32' ? ';' : ':');
|
|
159
|
+
const names = process.platform === 'win32' ? [`${name}.exe`, `${name}.cmd`, name] : [name];
|
|
160
|
+
for (const dir of entries) {
|
|
161
|
+
if (dir === '') continue;
|
|
162
|
+
for (const candidate of names) {
|
|
163
|
+
try {
|
|
164
|
+
if (statSync(join(dir, candidate)).isFile()) return true;
|
|
165
|
+
} catch { /* not here; keep looking */ }
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The pinned build under the operator's home, or null.
|
|
173
|
+
*
|
|
174
|
+
* Every failure returns null rather than throwing: this runs inside a gate
|
|
175
|
+
* whose refusal is the product, and an unreadable home directory must degrade
|
|
176
|
+
* to the ordinary "gitleaks is not installed" refusal — which already carries
|
|
177
|
+
* instructions — never take the session down with an unhandled error.
|
|
178
|
+
*/
|
|
179
|
+
function managedScanner() {
|
|
180
|
+
try {
|
|
181
|
+
const home = homedir();
|
|
182
|
+
if (typeof home !== 'string' || home === '') return null;
|
|
183
|
+
const name = process.platform === 'win32' ? 'gitleaks.exe' : 'gitleaks';
|
|
184
|
+
const path = join(home, '.tyran', 'bin', name);
|
|
185
|
+
return statSync(path).isFile() ? path : null;
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
115
190
|
|
|
116
191
|
/** Suppression files, honoured ONLY when git tracks them. See `suppression`. */
|
|
117
192
|
export const SUPPRESSION_FILES = Object.freeze({
|
|
@@ -125,9 +200,22 @@ export const SUPPRESSION_FILES = Object.freeze({
|
|
|
125
200
|
/**
|
|
126
201
|
* Characters at which one shell command can end and another begin.
|
|
127
202
|
*
|
|
128
|
-
*
|
|
129
|
-
* only
|
|
130
|
-
*
|
|
203
|
+
* QUOTING is deliberately NOT honoured, and that is not laziness — it is the
|
|
204
|
+
* only safe reading. Splitting inside a quoted string can only produce MORE
|
|
205
|
+
* candidate segments, never fewer, and more candidates is the direction this
|
|
206
|
+
* gate wants to be wrong in. A quote-aware lexer is worse than this one:
|
|
207
|
+
* measured, `git commit -m "it's the source of truth" ; git push origin main`
|
|
208
|
+
* has its apostrophe close the double quote, swallows the `;`, and the push
|
|
209
|
+
* becomes INVISIBLE — the gate reads the verb as `commit`, runs no
|
|
210
|
+
* deployment-class check, and scans the commit instead of the push range.
|
|
211
|
+
* This repository's own commit messages say "source of truth" constantly.
|
|
212
|
+
*
|
|
213
|
+
* BACKSLASH ESCAPES are honoured, because unlike quoting they cannot hide a
|
|
214
|
+
* separator. In all three shell contexts — unquoted, single-quoted,
|
|
215
|
+
* double-quoted — `\<sep>` is never an operator, so consuming it can only
|
|
216
|
+
* remove a split the shell would not have made. Verified against `/bin/sh`
|
|
217
|
+
* with a side-effect oracle over 34 join constructions: zero cases where the
|
|
218
|
+
* shell ran a second command and this lexer kept one segment.
|
|
131
219
|
*/
|
|
132
220
|
export const SEPARATORS = ';&|\n\r()`{}<>';
|
|
133
221
|
|
|
@@ -221,8 +309,22 @@ export function splitSegments(command) {
|
|
|
221
309
|
const folded = stripHeredocBodies(command).replace(/\\\r?\n/g, ' ');
|
|
222
310
|
const out = [];
|
|
223
311
|
let current = '';
|
|
312
|
+
let escaped = false;
|
|
224
313
|
for (const ch of folded) {
|
|
225
|
-
|
|
314
|
+
// ORDER IS LOAD-BEARING. This branch must come first so an escaped
|
|
315
|
+
// backslash consumes itself: without it `echo a\\; git push origin main`
|
|
316
|
+
// reads the real `;` as escaped and the push disappears. Measured — it is
|
|
317
|
+
// the most likely way to get this patch subtly wrong.
|
|
318
|
+
if (escaped) {
|
|
319
|
+
escaped = false;
|
|
320
|
+
current += ch;
|
|
321
|
+
} else if (ch === '\\') {
|
|
322
|
+
escaped = true;
|
|
323
|
+
// KEPT, not dropped: `tokensOf` strips backslashes downstream and
|
|
324
|
+
// `isLiteralPath` inspects this text, so removing it here would change
|
|
325
|
+
// what every consumer sees for a reason unrelated to splitting.
|
|
326
|
+
current += ch;
|
|
327
|
+
} else if (SEPARATORS.includes(ch)) {
|
|
226
328
|
out.push(current);
|
|
227
329
|
current = '';
|
|
228
330
|
} else {
|
|
@@ -1028,6 +1130,23 @@ export async function isTracked(repo, path, { runner, timeoutMs }) {
|
|
|
1028
1130
|
* unexamined. Reproduced on the first attempt: the range counted 0 commits
|
|
1029
1131
|
* while origin held none of them.
|
|
1030
1132
|
*/
|
|
1133
|
+
/**
|
|
1134
|
+
* The branch HEAD is on, or null when git cannot say (detached HEAD, a repo
|
|
1135
|
+
* with no commits, git unavailable). Null becomes a placeholder in the remedy
|
|
1136
|
+
* rather than a wrong branch name: a command that looks runnable and is not
|
|
1137
|
+
* is worse than one that visibly needs a word filled in.
|
|
1138
|
+
*/
|
|
1139
|
+
async function currentBranch(repo, { runner, timeoutMs }) {
|
|
1140
|
+
try {
|
|
1141
|
+
const r = await runner('git', ['-C', repo, 'symbolic-ref', '--quiet', '--short', 'HEAD'], { timeoutMs });
|
|
1142
|
+
if (r.spawned !== true || r.timedOut === true || r.code !== 0) return null;
|
|
1143
|
+
const name = String(r.stdout ?? '').trim();
|
|
1144
|
+
return name === '' ? null : name;
|
|
1145
|
+
} catch {
|
|
1146
|
+
return null;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1031
1150
|
export async function pushRange(repo, remote, refs, { runner, timeoutMs }) {
|
|
1032
1151
|
const listed = await git(['-C', repo, 'remote'], { runner, timeoutMs, what: 'listing remotes' });
|
|
1033
1152
|
const remotes = String(listed).trim().split('\n').filter((r) => r !== '');
|
|
@@ -1036,11 +1155,28 @@ export async function pushRange(repo, remote, refs, { runner, timeoutMs }) {
|
|
|
1036
1155
|
if (remotes.length === 1) target = remotes[0];
|
|
1037
1156
|
else if (remotes.length === 0) target = null;
|
|
1038
1157
|
else {
|
|
1158
|
+
// Hand over the command, do not describe its shape.
|
|
1159
|
+
//
|
|
1160
|
+
// This refusal is CORRECT — excluding commits present on any remote let
|
|
1161
|
+
// a key held on a private upstream reach a public origin unscanned — but
|
|
1162
|
+
// it was reported twice from two installs as a false positive, and both
|
|
1163
|
+
// reporters worked around it rather than satisfying it. The reason is
|
|
1164
|
+
// visible in the old text: `git push <remote> <branch>` is a template,
|
|
1165
|
+
// and the operator has to work out both halves while being told no.
|
|
1166
|
+
//
|
|
1167
|
+
// Everything needed to write the real line is already in hand here, so
|
|
1168
|
+
// it is written. A remedy you can paste is a different object from a
|
|
1169
|
+
// remedy you have to translate, and this gate's whole cost is paid in
|
|
1170
|
+
// the moments people spend deciding whether to route around it.
|
|
1171
|
+
const branch = await currentBranch(repo, { runner, timeoutMs });
|
|
1172
|
+
const ref = branch === null ? '<branch>' : branch;
|
|
1173
|
+
const lines = remotes.map((r) => ` git push ${r} HEAD:refs/heads/${ref}`).join('\n');
|
|
1039
1174
|
throw new ScanFailure(
|
|
1040
1175
|
`this push does not name which of ${remotes.length} remotes it targets (${remotes.join(', ')})`,
|
|
1041
|
-
'name the remote
|
|
1042
|
-
|
|
1043
|
-
'
|
|
1176
|
+
'name the remote — one of these is the command you meant:\n' +
|
|
1177
|
+
`${lines}\n` +
|
|
1178
|
+
'Excluding commits that exist on ANY remote would let a commit held on a private remote ' +
|
|
1179
|
+
'be published to a public one without ever being scanned — measured, not hypothetical.',
|
|
1044
1180
|
);
|
|
1045
1181
|
}
|
|
1046
1182
|
}
|
|
@@ -65,6 +65,7 @@ import { forJournal, locateJournal } from './evidence-gate.mjs';
|
|
|
65
65
|
import { resolveRepoRoot } from './session-start.mjs';
|
|
66
66
|
import { isUnsupervised, actorOf, stripMessageArguments, MESSAGE_ARGUMENT_RE } from './policy-gate.mjs';
|
|
67
67
|
import { splitSegments, tokensOf } from './secrets-gate.mjs';
|
|
68
|
+
import { readPlatformUsage, SIDECAR_RELPATH, SIDECAR_FRESH_MS } from '../../scripts/usage-source.mjs';
|
|
68
69
|
import { PASS, field, main, runGate } from './hook-io.mjs';
|
|
69
70
|
|
|
70
71
|
/** Own budget, below the 8 s hooks.json timeout. All I/O here is small local
|
|
@@ -72,10 +73,12 @@ import { PASS, field, main, runGate } from './hook-io.mjs';
|
|
|
72
73
|
export const DEADLINE_MS = 4000;
|
|
73
74
|
|
|
74
75
|
export const MARKER_RELPATH = join('.tyran', 'state', 'paused-until.json');
|
|
75
|
-
export const SIDECAR_RELPATH = join('.tyran', 'state', 'usage.json');
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
// Re-exported rather than re-declared. These had three spellings of the path
|
|
78
|
+
// and two of the window across the writer, the scheduler and this gate —
|
|
79
|
+
// nothing fails while they agree, and when they stop agreeing the symptom is
|
|
80
|
+
// a configured pause that silently never fires. `usage-source.mjs` owns them.
|
|
81
|
+
export { SIDECAR_RELPATH, SIDECAR_FRESH_MS } from '../../scripts/usage-source.mjs';
|
|
79
82
|
|
|
80
83
|
const MAX_SMALL_FILE_BYTES = 64 * 1024;
|
|
81
84
|
const MAX_CONFIG_BYTES = 256 * 1024;
|
|
@@ -123,13 +126,36 @@ export function readLimits(repoRoot) {
|
|
|
123
126
|
}
|
|
124
127
|
}
|
|
125
128
|
|
|
126
|
-
/**
|
|
127
|
-
|
|
129
|
+
/**
|
|
130
|
+
* The sidecar, or null when absent/stale/garbage. `nowMs` injected.
|
|
131
|
+
*
|
|
132
|
+
* FALLS BACK to the platform's own usage cache in `~/.claude.json` when the
|
|
133
|
+
* sidecar is absent or stale, because on a real machine it is ALWAYS absent:
|
|
134
|
+
* only the statusline writes it, the statusline writes only when the platform
|
|
135
|
+
* payload carries `rate_limits`, and that block is not always sent. Measured
|
|
136
|
+
* on an install running `mode: 'pause'` — no sidecar had ever been written,
|
|
137
|
+
* and the pause had never once been able to fire. The gate fails open, so
|
|
138
|
+
* nothing said so.
|
|
139
|
+
*
|
|
140
|
+
* The statusline stays FIRST when it works: it is session-scoped and refreshed
|
|
141
|
+
* every few seconds, where the platform cache is refreshed on its own schedule
|
|
142
|
+
* and was measured 83 minutes old under continuous use. `platform-usage.mjs`
|
|
143
|
+
* explains why an old reading is still safe to pause on and never safe to
|
|
144
|
+
* resume on; `lower_bound` on the returned document is how that reaches here.
|
|
145
|
+
*/
|
|
146
|
+
export function readSidecar(repoRoot, nowMs, freshMs = SIDECAR_FRESH_MS, readPlatform = readPlatformUsage) {
|
|
128
147
|
const doc = readSmallJson(join(repoRoot, SIDECAR_RELPATH));
|
|
129
|
-
if (doc
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
148
|
+
if (doc !== null) {
|
|
149
|
+
const writtenAt = Date.parse(typeof doc.written_at === 'string' ? doc.written_at : '');
|
|
150
|
+
if (Number.isFinite(writtenAt) && nowMs - writtenAt <= freshMs) return doc;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
return readPlatform({ nowMs });
|
|
154
|
+
} catch {
|
|
155
|
+
// Telemetry is consulted, never depended on. A gate that throws here
|
|
156
|
+
// stops the repository's work over a file it was only reading.
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
133
159
|
}
|
|
134
160
|
|
|
135
161
|
function windowOf(doc, key) {
|
|
@@ -146,13 +172,17 @@ function windowOf(doc, key) {
|
|
|
146
172
|
* refill just hits the weekly wall again.
|
|
147
173
|
*/
|
|
148
174
|
export function trippedWindow(sidecar, limits) {
|
|
175
|
+
// Carried onto the verdict so the marker can record which channel produced
|
|
176
|
+
// it. A reading from the platform cache is a LOWER BOUND on current usage:
|
|
177
|
+
// enough to stop on, never enough to start on.
|
|
178
|
+
const bound = sidecar?.lower_bound === true;
|
|
149
179
|
const weekly = windowOf(sidecar, 'seven_day');
|
|
150
180
|
if (weekly !== null && weekly.used_percentage >= limits.weekly_pause_at_percent) {
|
|
151
|
-
return { window: 'seven_day', ...weekly };
|
|
181
|
+
return { window: 'seven_day', lower_bound: bound, ...weekly };
|
|
152
182
|
}
|
|
153
183
|
const fiveHour = windowOf(sidecar, 'five_hour');
|
|
154
184
|
if (fiveHour !== null && fiveHour.used_percentage >= limits.pause_at_percent) {
|
|
155
|
-
return { window: 'five_hour', ...fiveHour };
|
|
185
|
+
return { window: 'five_hour', lower_bound: bound, ...fiveHour };
|
|
156
186
|
}
|
|
157
187
|
return null;
|
|
158
188
|
}
|
|
@@ -182,7 +212,11 @@ export function markerOf(tripped, limits, nowMs, sessionId, init) {
|
|
|
182
212
|
long_wait_policy: limits.long_wait,
|
|
183
213
|
session_id: typeof sessionId === 'string' && SESSION_ID_RE.test(sessionId) ? sessionId : null,
|
|
184
214
|
init: init ?? null,
|
|
185
|
-
|
|
215
|
+
// WHICH channel said so, and whether the figure was a lower bound. The
|
|
216
|
+
// scheduler must not treat a bound as a measurement when deciding to
|
|
217
|
+
// RESUME: "at least 97%" is a reason to stop and never a reason to start.
|
|
218
|
+
source: tripped.lower_bound === true ? 'claude.json' : 'sidecar',
|
|
219
|
+
lower_bound: tripped.lower_bound === true,
|
|
186
220
|
};
|
|
187
221
|
}
|
|
188
222
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jjanczur/tyran",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.32",
|
|
4
4
|
"description": "Command-line scripts for the Tyran task conductor (doctor, journal, schema, and repo tooling) for CI and terminals outside Claude Code. Installing this package does NOT install the Claude Code plugin — add that separately with `/plugin marketplace add jjanczur/tyran` inside Claude Code.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
package/scripts/board-html.mjs
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { FORBIDDEN } from './invisible.mjs';
|
|
3
|
+
// Interpolated into the client script below rather than typed there as a
|
|
4
|
+
// literal. It was a literal `1`, cost.mjs bumped to 2, and the Spend tab
|
|
5
|
+
// started rejecting the very payload its own server was sending — the whole
|
|
6
|
+
// tab replaced by "a format this page does not know". ADR-21 exactly: one
|
|
7
|
+
// answer, one place. No cycle: cost.mjs does not import this file.
|
|
8
|
+
import { COST_SCHEMA } from './cost.mjs';
|
|
3
9
|
/**
|
|
4
10
|
* board-html — the one page an operator stares at overnight.
|
|
5
11
|
*
|
|
@@ -185,6 +191,17 @@ pre.how .c{color:var(--muted)}
|
|
|
185
191
|
.comp span{display:flex;align-items:center;justify-content:center;font-family:var(--mono);font-size:.63rem;color:var(--bg);font-weight:700;white-space:nowrap;overflow:hidden}
|
|
186
192
|
.comp .s-cache_read{background:var(--brass)}
|
|
187
193
|
.comp .s-cache_write{background:var(--steel)}
|
|
194
|
+
/* The 1-hour write is its own segment because it is its own PRICE — 2x base
|
|
195
|
+
input against the 5-minute write's 1.25x. Without a distinct colour the
|
|
196
|
+
most expensive caching decision on the page is invisible. */
|
|
197
|
+
.comp .s-cache_write_1h{background:var(--ink)}
|
|
198
|
+
/* ---- the per-day series ---- */
|
|
199
|
+
.days{display:flex;align-items:flex-end;gap:2px;height:74px;margin:.5rem 0 .3rem;
|
|
200
|
+
overflow-x:auto;padding-bottom:2px}
|
|
201
|
+
.day{flex:1 0 6px;min-width:6px;height:100%;display:flex;align-items:flex-end}
|
|
202
|
+
.day i{display:block;width:100%;background:var(--brass);border-radius:1px 1px 0 0}
|
|
203
|
+
/* Outside the chosen window: context, not absence. */
|
|
204
|
+
.day.out i{background:var(--line)}
|
|
188
205
|
.comp .s-output{background:var(--sage)}
|
|
189
206
|
.comp .s-input{background:var(--muted)}
|
|
190
207
|
.compkey{display:flex;flex-wrap:wrap;gap:.85rem;font-family:var(--mono);font-size:.66rem;color:var(--muted);margin-top:.3rem}
|
|
@@ -283,6 +300,18 @@ if (data.schema !== 1) {
|
|
|
283
300
|
var errors = data.errors || [];
|
|
284
301
|
var cost = null;
|
|
285
302
|
var costError = null;
|
|
303
|
+
var spendWindow = '30d';
|
|
304
|
+
|
|
305
|
+
// How the period reads to a person. The window carries machine dates; a
|
|
306
|
+
// reader wants "last 30 days", and for the billing period the actual start
|
|
307
|
+
// date, because that is the number they can check against their invoice.
|
|
308
|
+
var windowLabel = function (payload) {
|
|
309
|
+
var w = payload && payload.window;
|
|
310
|
+
if (!w) return 'all time';
|
|
311
|
+
if (w.name === 'period') return 'this billing period, since ' + w.from;
|
|
312
|
+
if (w.name === '30d') return 'last 30 days';
|
|
313
|
+
return w.from + ' to ' + w.to;
|
|
314
|
+
};
|
|
286
315
|
|
|
287
316
|
// The queue count, in the browser tab. The whole point of this page is that
|
|
288
317
|
// you can leave it open while agents work overnight, and a background tab
|
|
@@ -854,6 +883,15 @@ if (data.schema !== 1) {
|
|
|
854
883
|
sp.appendChild(spBody);
|
|
855
884
|
spBody.appendChild(el('div', 'hint', 'Spend is read from the transcripts Claude Code already writes, and it is served rather than embedded — open this board with "board.mjs --serve" to see it. Over file:// there is no server, so this tab stays empty.'));
|
|
856
885
|
|
|
886
|
+
// Mirrors compositionLabel in cost.mjs. Restated rather than imported
|
|
887
|
+
// because this file is a browser bundle with no module graph — the two are
|
|
888
|
+
// pinned equal by a test so the drift is caught rather than lived with.
|
|
889
|
+
var compLabel = function (kind) {
|
|
890
|
+
if (kind === 'cache_write') return 'cache write (5 min)';
|
|
891
|
+
if (kind === 'cache_write_1h') return 'cache write (1 hour)';
|
|
892
|
+
return kind.replace(/_/g, ' ');
|
|
893
|
+
};
|
|
894
|
+
|
|
857
895
|
var chart = function (allRows, key, metric, dimKeys) {
|
|
858
896
|
var box = el('div', 'chart');
|
|
859
897
|
// Ranked by the metric on screen, not by the one the server sorted by:
|
|
@@ -934,12 +972,54 @@ if (data.schema !== 1) {
|
|
|
934
972
|
toggle.appendChild(button);
|
|
935
973
|
});
|
|
936
974
|
header.appendChild(toggle);
|
|
975
|
+
|
|
976
|
+
// The period the numbers describe. A plan is billed MONTHLY, so an
|
|
977
|
+
// all-time total has nothing to compare against; these two windows do.
|
|
978
|
+
// "this period" is offered only when the subscription date was readable,
|
|
979
|
+
// because a period guessed from an assumed anniversary is wrong for
|
|
980
|
+
// almost everyone.
|
|
981
|
+
var periods = [['30d', 'last 30 days'], ['all', 'all time']];
|
|
982
|
+
if (cost.subscription && cost.subscription.created_at) {
|
|
983
|
+
periods.splice(1, 0, ['period', 'this billing period']);
|
|
984
|
+
}
|
|
985
|
+
var picker = el('div', 'toggle');
|
|
986
|
+
periods.forEach(function (pair) {
|
|
987
|
+
var button = el('button', null, pair[1]);
|
|
988
|
+
button.setAttribute('type', 'button');
|
|
989
|
+
button.setAttribute('aria-pressed', spendWindow === pair[0] ? 'true' : 'false');
|
|
990
|
+
button.addEventListener('click', function () { loadCost(pair[0], metric); });
|
|
991
|
+
picker.appendChild(button);
|
|
992
|
+
});
|
|
993
|
+
header.appendChild(picker);
|
|
937
994
|
into.appendChild(header);
|
|
938
995
|
|
|
939
996
|
var tiles = el('div', 'tiles');
|
|
940
997
|
tiles.appendChild(tile('tokens', fmtTokens(t.tokens || 0), (t.requests || 0) + ' requests'));
|
|
941
998
|
tiles.appendChild(tile('cost through the API', fmtUsd(t.usd),
|
|
942
999
|
cost.rate_card ? 'rate card ' + cost.rate_card : 'no rate card set', 'lead'));
|
|
1000
|
+
// What the API-equivalent figure is actually WORTH knowing against. On a
|
|
1001
|
+
// subscription the marginal cost of a token is zero, so the dollar total
|
|
1002
|
+
// above is true of the API and false of the operator's bank account —
|
|
1003
|
+
// shown alone it invites exactly the wrong conclusion. Rendered only when
|
|
1004
|
+
// the plan is known; a guess here would be worse than the silence.
|
|
1005
|
+
var w = cost.window;
|
|
1006
|
+
var sub = cost.subscription;
|
|
1007
|
+
if (sub && typeof sub.monthly_usd === 'number') {
|
|
1008
|
+
// The multiple is shown ONLY under a month-shaped window. Against an
|
|
1009
|
+
// all-time total it would be off by however long this machine has been
|
|
1010
|
+
// running Claude Code — measured at 6.9x when the honest figure was
|
|
1011
|
+
// about 11x — so under "all time" the tile states the span and stops.
|
|
1012
|
+
var monthly = w && (w.name === '30d' || w.name === 'period') && typeof t.usd === 'number';
|
|
1013
|
+
var note = sub.label;
|
|
1014
|
+
if (monthly && sub.monthly_usd > 0) {
|
|
1015
|
+
note += ' — the API cost of ' + windowLabel(cost) + ' is ' +
|
|
1016
|
+
(t.usd / sub.monthly_usd).toFixed(1) + 'x that';
|
|
1017
|
+
} else if (cost.covers && cost.covers.days) {
|
|
1018
|
+
note += ' — the total above covers ' + cost.covers.days +
|
|
1019
|
+
' day' + (cost.covers.days === 1 ? '' : 's');
|
|
1020
|
+
}
|
|
1021
|
+
tiles.appendChild(tile('you pay', fmtUsd(sub.monthly_usd) + '/mo', note, 'lead'));
|
|
1022
|
+
}
|
|
943
1023
|
tiles.appendChild(tile('conductor overhead', (t.conductor_token_share || 0) + '%', 'of tokens, no ticket'));
|
|
944
1024
|
tiles.appendChild(tile('attributed', (cov.attributed || 0) + ' / ' + (cov.agent_transcripts || 0),
|
|
945
1025
|
(cov.unattributed || 0) + ' agent(s) without a ticket'));
|
|
@@ -967,13 +1047,47 @@ if (data.schema !== 1) {
|
|
|
967
1047
|
var legend = el('span', null);
|
|
968
1048
|
legend.appendChild(el('i', 's-' + c.kind));
|
|
969
1049
|
legend.appendChild(el('span', null,
|
|
970
|
-
c.kind
|
|
1050
|
+
compLabel(c.kind) + ' ' + (metric === 'usd' ? fmtUsd(c.usd) : fmtTokens(c.tokens))));
|
|
971
1051
|
key.appendChild(legend);
|
|
972
1052
|
});
|
|
973
1053
|
into.appendChild(bar);
|
|
974
1054
|
into.appendChild(key);
|
|
975
1055
|
}
|
|
976
1056
|
|
|
1057
|
+
// The series. Every other view on this tab is a snapshot, so nothing has
|
|
1058
|
+
// ever been able to answer "am I getting cheaper" — the single question a
|
|
1059
|
+
// spend page exists for. Always the FULL history regardless of the window:
|
|
1060
|
+
// a chart that shrank with the window could not show you that last month
|
|
1061
|
+
// was worse, which is the comparison worth having.
|
|
1062
|
+
var series = cost.per_day || [];
|
|
1063
|
+
if (series.length > 1) {
|
|
1064
|
+
into.appendChild(el('h3', null, 'By day'));
|
|
1065
|
+
var peak = 0;
|
|
1066
|
+
series.forEach(function (d) {
|
|
1067
|
+
var v = metric === 'usd' ? (d.usd || 0) : d.tokens;
|
|
1068
|
+
if (v > peak) peak = v;
|
|
1069
|
+
});
|
|
1070
|
+
var days = el('div', 'days');
|
|
1071
|
+
series.forEach(function (d) {
|
|
1072
|
+
var value = metric === 'usd' ? (d.usd || 0) : d.tokens;
|
|
1073
|
+
var col = el('div', 'day');
|
|
1074
|
+
var bar = el('i');
|
|
1075
|
+
bar.style.height = (peak > 0 ? Math.max(2, (value / peak) * 100) : 0) + '%';
|
|
1076
|
+
// Inside the window is the emphasis; outside it is context, not
|
|
1077
|
+
// absence — the days either side are exactly what makes the window
|
|
1078
|
+
// legible as a choice.
|
|
1079
|
+
if (w && (d.day < w.from || d.day > w.to)) col.className = 'day out';
|
|
1080
|
+
col.appendChild(bar);
|
|
1081
|
+
col.setAttribute('title', d.day + ' \u2014 ' + fmtTokens(d.tokens) + ' tokens' +
|
|
1082
|
+
(typeof d.usd === 'number' ? ' \u00b7 ' + fmtUsd(d.usd) : ''));
|
|
1083
|
+
days.appendChild(col);
|
|
1084
|
+
});
|
|
1085
|
+
into.appendChild(days);
|
|
1086
|
+
into.appendChild(el('div', 'hint',
|
|
1087
|
+
series[0].day + ' to ' + series[series.length - 1].day + ' \u00b7 ' +
|
|
1088
|
+
series.length + ' day(s) with recorded activity \u00b7 hover a bar for its total'));
|
|
1089
|
+
}
|
|
1090
|
+
|
|
977
1091
|
[['By model', cost.by_model || [], 'model', []],
|
|
978
1092
|
['By agent type', cost.by_agent_type || [], 'agent_type', ['conductor']],
|
|
979
1093
|
['By ticket', cost.by_ticket || [], 'ticket', ['conductor', 'unattributed']]].forEach(function (spec) {
|
|
@@ -1030,8 +1144,13 @@ if (data.schema !== 1) {
|
|
|
1030
1144
|
clear(spBody);
|
|
1031
1145
|
spBody.appendChild(el('div', 'err', why));
|
|
1032
1146
|
};
|
|
1033
|
-
|
|
1034
|
-
|
|
1147
|
+
// Re-fetches with a chosen window. The server recomputes rather than the
|
|
1148
|
+
// page filtering, because only the server has the per-day buckets and a
|
|
1149
|
+
// page that filtered a total it was handed could only ever subtract wrong.
|
|
1150
|
+
var loadCost = function (window, metric) {
|
|
1151
|
+
if (typeof fetch !== 'function') return;
|
|
1152
|
+
spendWindow = window;
|
|
1153
|
+
fetch('cost.json?window=' + encodeURIComponent(window), { headers: { accept: 'application/json' } })
|
|
1035
1154
|
.then(function (r) {
|
|
1036
1155
|
// 404 is not a failure, it is an ANSWER: no such route, so this page
|
|
1037
1156
|
// is not being served by the board server — a copy on a docs site, a
|
|
@@ -1050,7 +1169,7 @@ if (data.schema !== 1) {
|
|
|
1050
1169
|
showCostFailure('Spend could not be read: ' + ((payload && payload.error) || 'the server refused the request') + '.');
|
|
1051
1170
|
return;
|
|
1052
1171
|
}
|
|
1053
|
-
if (!payload || payload.schema !==
|
|
1172
|
+
if (!payload || payload.schema !== ${COST_SCHEMA}) {
|
|
1054
1173
|
showCostFailure('Spend was served in a format this page does not know (schema ' +
|
|
1055
1174
|
((payload && payload.schema) || 'absent') + ') — regenerate the board with the Tyran that served it.');
|
|
1056
1175
|
return;
|
|
@@ -1071,11 +1190,14 @@ if (data.schema !== 1) {
|
|
|
1071
1190
|
return;
|
|
1072
1191
|
}
|
|
1073
1192
|
cost = payload;
|
|
1074
|
-
renderSpend(spBody, 'tokens');
|
|
1193
|
+
renderSpend(spBody, metric || 'tokens');
|
|
1075
1194
|
var t = cost.totals || {};
|
|
1195
|
+
// CLEARED first: this runs again on every window change, and appending
|
|
1196
|
+
// left one "Spend" heading and one tile stacked up per click.
|
|
1197
|
+
clear(ovSpend);
|
|
1076
1198
|
var strip = el('div', 'tiles');
|
|
1077
1199
|
strip.appendChild(tile('spend so far', fmtUsd(t.usd),
|
|
1078
|
-
fmtTokens(t.tokens || 0) + ' tokens · see the Spend tab'));
|
|
1200
|
+
fmtTokens(t.tokens || 0) + ' tokens · ' + windowLabel(cost) + ' · see the Spend tab'));
|
|
1079
1201
|
ovSpend.appendChild(el('h2', null, 'Spend'));
|
|
1080
1202
|
ovSpend.appendChild(strip);
|
|
1081
1203
|
})
|
|
@@ -1085,7 +1207,12 @@ if (data.schema !== 1) {
|
|
|
1085
1207
|
if (String(location.protocol) === 'file:') return;
|
|
1086
1208
|
showCostFailure('Spend could not be read: the board server did not answer. It is served, never embedded — start it with: npx @jjanczur/tyran board --dir .tyran --serve');
|
|
1087
1209
|
});
|
|
1088
|
-
}
|
|
1210
|
+
};
|
|
1211
|
+
|
|
1212
|
+
// The DEFAULT is 30 days, not all time: a plan is billed monthly, so a
|
|
1213
|
+
// month-shaped window is the only one an operator can compare against the
|
|
1214
|
+
// price without doing arithmetic. All time remains one click away.
|
|
1215
|
+
loadCost('30d', 'tokens');
|
|
1089
1216
|
|
|
1090
1217
|
// ---- settings ---------------------------------------------------------
|
|
1091
1218
|
//
|