@trazum/core 1.40.0 → 1.42.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 -0
- package/dist/config-schema.d.ts +14 -1
- package/dist/config-schema.d.ts.map +1 -1
- package/dist/config-schema.js +27 -0
- package/dist/config-schema.js.map +1 -1
- package/dist/connector.d.ts +227 -0
- package/dist/connector.d.ts.map +1 -0
- package/dist/connector.js +431 -0
- package/dist/connector.js.map +1 -0
- package/dist/history.d.ts +7 -2
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/store.d.ts +134 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +177 -0
- package/dist/store.js.map +1 -0
- package/package.json +1 -1
- package/src/config-schema.ts +40 -0
- package/src/connector.ts +616 -0
- package/src/history.ts +7 -2
- package/src/index.ts +28 -0
- package/src/store.ts +281 -0
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A year of measured spend on disk, and not one prompt inside it.
|
|
3
|
+
*
|
|
4
|
+
* A connector that re-downloads a month every time it runs is a connector
|
|
5
|
+
* nobody leaves on, and `history` needs stored reports — which until now meant
|
|
6
|
+
* a human curating a directory of `--json` files. The store is where pulled
|
|
7
|
+
* usage lands so neither is true any more.
|
|
8
|
+
*
|
|
9
|
+
* **Pure, and in the core.** This module decides what a record *is*, when two
|
|
10
|
+
* records are the same record, and what a set of them adds up to. The
|
|
11
|
+
* filesystem lives in the CLI, the same split every other browser-safe module
|
|
12
|
+
* here keeps.
|
|
13
|
+
*
|
|
14
|
+
* **Convergence, not accumulation.** Re-pulling an overlapping window must not
|
|
15
|
+
* double the bill. Two records covering the same window, from the same
|
|
16
|
+
* provider, for the same model and grouping, are the *same fact restated* —
|
|
17
|
+
* the later pull wins, because a window pulled again is at worst as complete
|
|
18
|
+
* as it was. That makes overlapping pulls idempotent, which is what lets a
|
|
19
|
+
* scheduled job run every hour over a rolling day without inventing money.
|
|
20
|
+
*
|
|
21
|
+
* **Deduplication that cannot lie.** Two records the store cannot tell apart —
|
|
22
|
+
* a source that served no window, or no model — are kept as *two* and reported
|
|
23
|
+
* as possibly-double. Merging them on a guess makes a bill quietly smaller,
|
|
24
|
+
* and quietly smaller is the flattering direction this repository refuses
|
|
25
|
+
* everywhere it can occur.
|
|
26
|
+
*/
|
|
27
|
+
import type { UsageBucket } from './connector.js';
|
|
28
|
+
/** Bump when the meaning of a field changes. Readers keep what they cannot read. */
|
|
29
|
+
export declare const STORE_SCHEMA_VERSION = 1;
|
|
30
|
+
/**
|
|
31
|
+
* One stored measurement.
|
|
32
|
+
*
|
|
33
|
+
* Short keys because a year of these is a file somebody has to keep, and the
|
|
34
|
+
* shape is documented rather than inferred from its own verbosity.
|
|
35
|
+
*/
|
|
36
|
+
export interface StoreRecord {
|
|
37
|
+
/** Schema version of this line, so an old reader keeps what it cannot parse. */
|
|
38
|
+
v: number;
|
|
39
|
+
provider: string;
|
|
40
|
+
fromMs: number;
|
|
41
|
+
toMs: number;
|
|
42
|
+
model: string;
|
|
43
|
+
/** null when the source serves no request count. Never zero for absent. */
|
|
44
|
+
calls: number | null;
|
|
45
|
+
input: number;
|
|
46
|
+
cacheRead: number;
|
|
47
|
+
write5m: number;
|
|
48
|
+
write1h: number;
|
|
49
|
+
/** False when the source reported writes without saying which TTL. */
|
|
50
|
+
ttlKnown: boolean;
|
|
51
|
+
output: number;
|
|
52
|
+
/**
|
|
53
|
+
* The account's own opaque identifiers — workspace, key, tier — as the
|
|
54
|
+
* provider served them.
|
|
55
|
+
*
|
|
56
|
+
* These are identifiers, not secrets: they name a workspace, not a way into
|
|
57
|
+
* it, and per-owner attribution needs them. Prompt text, completion text and
|
|
58
|
+
* credentials are never here, and the documentation says exactly what a
|
|
59
|
+
* store holds rather than leaving somebody to guess about their own file.
|
|
60
|
+
*/
|
|
61
|
+
group: Record<string, string>;
|
|
62
|
+
/** When this record was pulled, which is how the later restatement wins. */
|
|
63
|
+
pulledAtMs: number;
|
|
64
|
+
}
|
|
65
|
+
/** What makes two records the same record. See the module note. */
|
|
66
|
+
export declare function identityOf(record: StoreRecord): string;
|
|
67
|
+
export interface ResolvedStore {
|
|
68
|
+
/** One record per identity, newest pull winning. */
|
|
69
|
+
records: StoreRecord[];
|
|
70
|
+
/**
|
|
71
|
+
* Records the store could not tell apart, kept in full rather than merged.
|
|
72
|
+
*
|
|
73
|
+
* Reported so a total built on them can be read for what it is: possibly
|
|
74
|
+
* counting the same spend twice, and saying so beats a smaller number
|
|
75
|
+
* nobody can check.
|
|
76
|
+
*/
|
|
77
|
+
possiblyDouble: StoreRecord[];
|
|
78
|
+
/** Lines from a schema version this binary does not know, kept and counted. */
|
|
79
|
+
unknownVersion: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Collapses an append-only log into the current truth.
|
|
83
|
+
*
|
|
84
|
+
* Append-only on disk and last-wins at read time, rather than rewriting a file
|
|
85
|
+
* in place: a crash during a rewrite can lose a year, and a crash during an
|
|
86
|
+
* append loses the tail of one line.
|
|
87
|
+
*/
|
|
88
|
+
export declare function resolveStore(records: readonly StoreRecord[]): ResolvedStore;
|
|
89
|
+
/** Turns a connector's buckets into records ready to append. */
|
|
90
|
+
export declare function recordsFromBuckets(provider: string, buckets: readonly UsageBucket[], pulledAtMs: number): StoreRecord[];
|
|
91
|
+
/** The reverse, so a stored month prices exactly as a fresh pull does. */
|
|
92
|
+
export declare function bucketsFromRecords(records: readonly StoreRecord[]): UsageBucket[];
|
|
93
|
+
export interface StoreInventory {
|
|
94
|
+
schemaVersion: 1;
|
|
95
|
+
/** Per provider, oldest first by the span it covers. */
|
|
96
|
+
providers: {
|
|
97
|
+
provider: string;
|
|
98
|
+
records: number;
|
|
99
|
+
span: {
|
|
100
|
+
fromMs: number;
|
|
101
|
+
toMs: number;
|
|
102
|
+
} | null;
|
|
103
|
+
/** null when no provider in the set serves request counts. */
|
|
104
|
+
calls: number | null;
|
|
105
|
+
models: string[];
|
|
106
|
+
}[];
|
|
107
|
+
totalRecords: number;
|
|
108
|
+
span: {
|
|
109
|
+
fromMs: number;
|
|
110
|
+
toMs: number;
|
|
111
|
+
} | null;
|
|
112
|
+
possiblyDouble: number;
|
|
113
|
+
unknownVersion: number;
|
|
114
|
+
}
|
|
115
|
+
export declare function storeInventory(resolved: ResolvedStore): StoreInventory;
|
|
116
|
+
export interface PruneResult {
|
|
117
|
+
kept: StoreRecord[];
|
|
118
|
+
dropped: StoreRecord[];
|
|
119
|
+
/** The span the dropped records covered — what a reader loses by pruning. */
|
|
120
|
+
droppedSpan: {
|
|
121
|
+
fromMs: number;
|
|
122
|
+
toMs: number;
|
|
123
|
+
} | null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Drops records whose window ended before the cutoff.
|
|
127
|
+
*
|
|
128
|
+
* Judged on `toMs`: a bucket that *ends* inside the retained period is
|
|
129
|
+
* retained whole, because half a bucket is a measurement of nothing. What goes
|
|
130
|
+
* is returned rather than counted, so the caller can say what went — silence
|
|
131
|
+
* about deleted measurements is the one thing a store must not do.
|
|
132
|
+
*/
|
|
133
|
+
export declare function pruneRecords(records: readonly StoreRecord[], cutoffMs: number): PruneResult;
|
|
134
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,oFAAoF;AACpF,eAAO,MAAM,oBAAoB,IAAI,CAAC;AAEtC;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC1B,gFAAgF;IAChF,CAAC,EAAE,MAAM,CAAC;IACV,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AM,CAAC;QAChB,IAAI,EAAE;YAAE,MAAM,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,GAAG,IAAI,CAAC;QAC9C,8DAA8D;QAC9D,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;QACrB,MAAM,EAAE,MAAM,EAAE,CAAC;KAClB,EAAE,CAAC;IACJ,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAC9C,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,aAAa,GAAG,cAAc,CAuCtE;AAMD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,WAAW,EAAE,CAAC;IACpB,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,6EAA6E;IAC7E,WAAW,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;CACtD;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,WAAW,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,WAAW,CAiB3F"}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A year of measured spend on disk, and not one prompt inside it.
|
|
3
|
+
*
|
|
4
|
+
* A connector that re-downloads a month every time it runs is a connector
|
|
5
|
+
* nobody leaves on, and `history` needs stored reports — which until now meant
|
|
6
|
+
* a human curating a directory of `--json` files. The store is where pulled
|
|
7
|
+
* usage lands so neither is true any more.
|
|
8
|
+
*
|
|
9
|
+
* **Pure, and in the core.** This module decides what a record *is*, when two
|
|
10
|
+
* records are the same record, and what a set of them adds up to. The
|
|
11
|
+
* filesystem lives in the CLI, the same split every other browser-safe module
|
|
12
|
+
* here keeps.
|
|
13
|
+
*
|
|
14
|
+
* **Convergence, not accumulation.** Re-pulling an overlapping window must not
|
|
15
|
+
* double the bill. Two records covering the same window, from the same
|
|
16
|
+
* provider, for the same model and grouping, are the *same fact restated* —
|
|
17
|
+
* the later pull wins, because a window pulled again is at worst as complete
|
|
18
|
+
* as it was. That makes overlapping pulls idempotent, which is what lets a
|
|
19
|
+
* scheduled job run every hour over a rolling day without inventing money.
|
|
20
|
+
*
|
|
21
|
+
* **Deduplication that cannot lie.** Two records the store cannot tell apart —
|
|
22
|
+
* a source that served no window, or no model — are kept as *two* and reported
|
|
23
|
+
* as possibly-double. Merging them on a guess makes a bill quietly smaller,
|
|
24
|
+
* and quietly smaller is the flattering direction this repository refuses
|
|
25
|
+
* everywhere it can occur.
|
|
26
|
+
*/
|
|
27
|
+
/** Bump when the meaning of a field changes. Readers keep what they cannot read. */
|
|
28
|
+
export const STORE_SCHEMA_VERSION = 1;
|
|
29
|
+
/** What makes two records the same record. See the module note. */
|
|
30
|
+
export function identityOf(record) {
|
|
31
|
+
return [
|
|
32
|
+
record.provider,
|
|
33
|
+
record.fromMs,
|
|
34
|
+
record.toMs,
|
|
35
|
+
record.model,
|
|
36
|
+
JSON.stringify(record.group),
|
|
37
|
+
].join('\n');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A record whose identity is not trustworthy enough to converge on.
|
|
41
|
+
*
|
|
42
|
+
* A window of zero length, or a record with no model, cannot be told apart
|
|
43
|
+
* from another like it. Those are kept whole and counted separately.
|
|
44
|
+
*/
|
|
45
|
+
function identifiable(record) {
|
|
46
|
+
return record.model !== '' && record.toMs > record.fromMs;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Collapses an append-only log into the current truth.
|
|
50
|
+
*
|
|
51
|
+
* Append-only on disk and last-wins at read time, rather than rewriting a file
|
|
52
|
+
* in place: a crash during a rewrite can lose a year, and a crash during an
|
|
53
|
+
* append loses the tail of one line.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveStore(records) {
|
|
56
|
+
const byIdentity = new Map();
|
|
57
|
+
const possiblyDouble = [];
|
|
58
|
+
let unknownVersion = 0;
|
|
59
|
+
for (const record of records) {
|
|
60
|
+
if (record.v > STORE_SCHEMA_VERSION) {
|
|
61
|
+
unknownVersion += 1;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!identifiable(record)) {
|
|
65
|
+
possiblyDouble.push(record);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const key = identityOf(record);
|
|
69
|
+
const seen = byIdentity.get(key);
|
|
70
|
+
if (seen === undefined || record.pulledAtMs >= seen.pulledAtMs) {
|
|
71
|
+
byIdentity.set(key, record);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
records: [...byIdentity.values()].sort((a, b) => a.fromMs - b.fromMs || a.model.localeCompare(b.model)),
|
|
76
|
+
possiblyDouble,
|
|
77
|
+
unknownVersion,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** Turns a connector's buckets into records ready to append. */
|
|
81
|
+
export function recordsFromBuckets(provider, buckets, pulledAtMs) {
|
|
82
|
+
return buckets.map((bucket) => ({
|
|
83
|
+
v: STORE_SCHEMA_VERSION,
|
|
84
|
+
provider,
|
|
85
|
+
fromMs: bucket.fromMs,
|
|
86
|
+
toMs: bucket.toMs,
|
|
87
|
+
model: bucket.model,
|
|
88
|
+
calls: bucket.calls,
|
|
89
|
+
input: bucket.inputTokens,
|
|
90
|
+
cacheRead: bucket.cacheReadTokens,
|
|
91
|
+
write5m: bucket.cacheWrite5mTokens,
|
|
92
|
+
write1h: bucket.cacheWrite1hTokens,
|
|
93
|
+
ttlKnown: bucket.writeTtlKnown,
|
|
94
|
+
output: bucket.outputTokens,
|
|
95
|
+
group: bucket.group,
|
|
96
|
+
pulledAtMs,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
/** The reverse, so a stored month prices exactly as a fresh pull does. */
|
|
100
|
+
export function bucketsFromRecords(records) {
|
|
101
|
+
return records.map((record) => ({
|
|
102
|
+
fromMs: record.fromMs,
|
|
103
|
+
toMs: record.toMs,
|
|
104
|
+
model: record.model,
|
|
105
|
+
calls: record.calls,
|
|
106
|
+
inputTokens: record.input,
|
|
107
|
+
cacheReadTokens: record.cacheRead,
|
|
108
|
+
cacheWrite5mTokens: record.write5m,
|
|
109
|
+
cacheWrite1hTokens: record.write1h,
|
|
110
|
+
writeTtlKnown: record.ttlKnown,
|
|
111
|
+
outputTokens: record.output,
|
|
112
|
+
group: record.group,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
export function storeInventory(resolved) {
|
|
116
|
+
const byProvider = new Map();
|
|
117
|
+
for (const record of resolved.records) {
|
|
118
|
+
const list = byProvider.get(record.provider) ?? [];
|
|
119
|
+
list.push(record);
|
|
120
|
+
byProvider.set(record.provider, list);
|
|
121
|
+
}
|
|
122
|
+
const providers = [...byProvider.entries()]
|
|
123
|
+
.map(([provider, records]) => {
|
|
124
|
+
const anyUnknown = records.some((r) => r.calls === null);
|
|
125
|
+
return {
|
|
126
|
+
provider,
|
|
127
|
+
records: records.length,
|
|
128
|
+
span: {
|
|
129
|
+
fromMs: Math.min(...records.map((r) => r.fromMs)),
|
|
130
|
+
toMs: Math.max(...records.map((r) => r.toMs)),
|
|
131
|
+
},
|
|
132
|
+
calls: anyUnknown ? null : records.reduce((sum, r) => sum + (r.calls ?? 0), 0),
|
|
133
|
+
models: [...new Set(records.map((r) => r.model))].sort(),
|
|
134
|
+
};
|
|
135
|
+
})
|
|
136
|
+
.sort((a, b) => a.provider.localeCompare(b.provider));
|
|
137
|
+
const all = resolved.records;
|
|
138
|
+
return {
|
|
139
|
+
schemaVersion: 1,
|
|
140
|
+
providers,
|
|
141
|
+
totalRecords: all.length,
|
|
142
|
+
span: all.length === 0
|
|
143
|
+
? null
|
|
144
|
+
: {
|
|
145
|
+
fromMs: Math.min(...all.map((r) => r.fromMs)),
|
|
146
|
+
toMs: Math.max(...all.map((r) => r.toMs)),
|
|
147
|
+
},
|
|
148
|
+
possiblyDouble: resolved.possiblyDouble.length,
|
|
149
|
+
unknownVersion: resolved.unknownVersion,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Drops records whose window ended before the cutoff.
|
|
154
|
+
*
|
|
155
|
+
* Judged on `toMs`: a bucket that *ends* inside the retained period is
|
|
156
|
+
* retained whole, because half a bucket is a measurement of nothing. What goes
|
|
157
|
+
* is returned rather than counted, so the caller can say what went — silence
|
|
158
|
+
* about deleted measurements is the one thing a store must not do.
|
|
159
|
+
*/
|
|
160
|
+
export function pruneRecords(records, cutoffMs) {
|
|
161
|
+
const kept = [];
|
|
162
|
+
const dropped = [];
|
|
163
|
+
for (const record of records) {
|
|
164
|
+
(record.toMs < cutoffMs ? dropped : kept).push(record);
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
kept,
|
|
168
|
+
dropped,
|
|
169
|
+
droppedSpan: dropped.length === 0
|
|
170
|
+
? null
|
|
171
|
+
: {
|
|
172
|
+
fromMs: Math.min(...dropped.map((r) => r.fromMs)),
|
|
173
|
+
toMs: Math.max(...dropped.map((r) => r.toMs)),
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,oFAAoF;AACpF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAsCtC,mEAAmE;AACnE,MAAM,UAAU,UAAU,CAAC,MAAmB;IAC5C,OAAO;QACL,MAAM,CAAC,QAAQ;QACf,MAAM,CAAC,MAAM;QACb,MAAM,CAAC,IAAI;QACX,MAAM,CAAC,KAAK;QACZ,IAAI,CAAC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AAM,CAAC,CAAC;IACzD,CAAC;IACD,OAAO;QACL,IAAI;QACJ,OAAO;QACP,WAAW,EACT,OAAO,CAAC,MAAM,KAAK,CAAC;YAClB,CAAC,CAAC,IAAI;YACN,CAAC,CAAC;gBACE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;gBACjD,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;aAC9C;KACR,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trazum/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.42.0",
|
|
4
4
|
"description": "Trazum core: priced advisories for LLM prompts (caching, model tier, batching, schemas), plus deterministic trimming, token counting and pricing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "David Mu\u00f1oz Rey",
|
package/src/config-schema.ts
CHANGED
|
@@ -147,6 +147,16 @@ export interface TrazumConfig {
|
|
|
147
147
|
* one is a silence nobody can audit. Waived failures render as waived,
|
|
148
148
|
* never hidden — the bill still counts them; only the exit code is quiet.
|
|
149
149
|
*/
|
|
150
|
+
/**
|
|
151
|
+
* How long the local usage store keeps a measurement.
|
|
152
|
+
*
|
|
153
|
+
* Retention is a policy, so it lives in the repository beside the budgets
|
|
154
|
+
* rather than in whichever invocation happened to run the prune. There is
|
|
155
|
+
* deliberately **no default**: deleting measurements on a policy nobody
|
|
156
|
+
* wrote down is not something anybody should get by accident, so `--prune`
|
|
157
|
+
* with neither this nor `--keep` refuses and says so.
|
|
158
|
+
*/
|
|
159
|
+
store?: { keepDays?: number };
|
|
150
160
|
waive?: WaiveEntry[];
|
|
151
161
|
/** Default for `trazum diff --max-growth`, in tokens. */
|
|
152
162
|
maxGrowth?: number;
|
|
@@ -186,6 +196,7 @@ export const CONFIG_KEYS = [
|
|
|
186
196
|
'labels',
|
|
187
197
|
'spend',
|
|
188
198
|
'sources',
|
|
199
|
+
'store',
|
|
189
200
|
'waive',
|
|
190
201
|
'maxGrowth',
|
|
191
202
|
'baseline',
|
|
@@ -199,6 +210,8 @@ export const CONFIG_SPEND_KEYS = ['maxUsd', 'maxDayUsd', 'maxSessionUsd', 'byLab
|
|
|
199
210
|
|
|
200
211
|
export const CONFIG_WAIVE_KEYS = ['gate', 'reason', 'until'] as const;
|
|
201
212
|
|
|
213
|
+
export const CONFIG_STORE_KEYS = ['keepDays'] as const;
|
|
214
|
+
|
|
202
215
|
/**
|
|
203
216
|
* The gates a waiver can silence. The list is closed on purpose: a waiver
|
|
204
217
|
* naming a gate that does not exist is a decision about nothing, and the
|
|
@@ -466,6 +479,32 @@ function parseSources(raw: unknown, source: string): Record<string, string[]> {
|
|
|
466
479
|
* nothing. The expiry is *not* checked against today here — a config file is
|
|
467
480
|
* timeless, and whether a waiver is expired is judged where the gate runs.
|
|
468
481
|
*/
|
|
482
|
+
/**
|
|
483
|
+
* `store.keepDays` — retention, in whole days.
|
|
484
|
+
*
|
|
485
|
+
* A fraction of a day is refused rather than rounded: retention decides what
|
|
486
|
+
* gets deleted, and a policy the tool rounded on the operator's behalf is a
|
|
487
|
+
* policy nobody agreed to.
|
|
488
|
+
*/
|
|
489
|
+
function parseStore(raw: unknown, source: string): { keepDays?: number } {
|
|
490
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
491
|
+
throw new Error(`"store" in ${source} must be an object, for example {"keepDays": 90}.`);
|
|
492
|
+
}
|
|
493
|
+
const entry = raw as Record<string, unknown>;
|
|
494
|
+
rejectUnknownKeys(entry, CONFIG_STORE_KEYS, source, 'store.');
|
|
495
|
+
const out: { keepDays?: number } = {};
|
|
496
|
+
if (entry.keepDays !== undefined) {
|
|
497
|
+
const days = entry.keepDays;
|
|
498
|
+
if (typeof days !== 'number' || !Number.isInteger(days) || days <= 0) {
|
|
499
|
+
throw new Error(
|
|
500
|
+
`"store.keepDays" in ${source} must be a whole number of days above zero, and it is ${JSON.stringify(days)}.`,
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
out.keepDays = days;
|
|
504
|
+
}
|
|
505
|
+
return out;
|
|
506
|
+
}
|
|
507
|
+
|
|
469
508
|
function parseWaive(raw: unknown, source: string): WaiveEntry[] {
|
|
470
509
|
if (!Array.isArray(raw)) throw new ConfigError('"waive" must be an array', source);
|
|
471
510
|
return raw.map((entry, index) => {
|
|
@@ -654,6 +693,7 @@ export function parseConfig(raw: string, source = CONFIG_FILENAME): TrazumConfig
|
|
|
654
693
|
if (document.labels !== undefined) config.labels = parseLabels(document.labels, source);
|
|
655
694
|
if (document.spend !== undefined) config.spend = parseSpend(document.spend, source);
|
|
656
695
|
if (document.sources !== undefined) config.sources = parseSources(document.sources, source);
|
|
696
|
+
if (document.store !== undefined) config.store = parseStore(document.store, source);
|
|
657
697
|
if (document.waive !== undefined) config.waive = parseWaive(document.waive, source);
|
|
658
698
|
if (document.baseline !== undefined) {
|
|
659
699
|
config.baseline = parseBaselineConfig(document.baseline, source);
|