@dzhechkov/harness-core 0.3.99 → 0.3.101

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/dist/usage.d.ts CHANGED
@@ -1,70 +1,123 @@
1
1
  /**
2
2
  * `dz usage` data source — a READONLY, never-throw, best-effort estimate of Claude SESSION
3
- * (active 5h-block) and WEEKLY (rolling 7d) token usage, aggregated from the local Claude Code
4
- * transcript files under `~/.claude/projects/<munged>/<session>.jsonl`.
3
+ * and WEEKLY token usage, aggregated from the local Claude Code transcript files under
4
+ * `~/.claude/projects/<munged>/<session>.jsonl`.
5
5
  *
6
- * ## Honest-uncertainty contract (LOAD-BEARING — NFR-4)
6
+ * ## Honest-uncertainty contract (LOAD-BEARING)
7
7
  *
8
- * These percentages are **ESTIMATES** derived by aggregating local transcript token counts against
9
- * a USER-CONFIGURED limit. There is **no official usage API** being consulted this is a fuel
10
- * gauge built from the flow meter, not a tank sensor. Therefore:
8
+ * These percentages are ESTIMATES derived by aggregating local transcript token counts against
9
+ * USER-CONFIGURED limits. There is no official Anthropic usage API being consulted; claude.ai is
10
+ * authoritative. Therefore:
11
11
  *
12
- * - Every payload carries `estimated: true` so a consumer can NEVER mistake it for ground truth.
13
- * - When a limit is **unconfigured** (`.dz/config.json` has no `memory.usage.sessionTokenLimit` /
14
- * `weeklyTokenLimit`), the corresponding `pct` is `null` **unknown, never 0, never a guess**.
15
- * - **Calibration protocol (observed-exhaustion):** when a real limit-hit occurs at an estimated
16
- * `X%`, scale the configured limit by `X/100` so the next estimate lines up with reality. The
17
- * 4 token fields are summed with equal weight; the relative weighting (cache-read tokens
18
- * dominate real transcripts) is absorbed into the calibrated limit value.
12
+ * - Every JSON payload emitted by the CLI carries `estimated: true`.
13
+ * - When a limit is unconfigured, the corresponding `pct` is `null` — unknown, never 0.
14
+ * - Weekly usage counts from a fixed configured reset anchor such as `Wed 08:59`, not from a
15
+ * rolling seven-day window.
16
+ * - Session usage counts from the active fixed-length transcript block, not from a rolling
17
+ * last-N-hours window.
19
18
  *
20
- * ## Statusline discipline (NFR-1)
19
+ * ## Statusline discipline
21
20
  *
22
21
  * Modeled on {@link ./statusline.ts} and {@link ./vector-tier.ts}'s `readVectorEngineMode`:
23
- * - **never-throw**any error (missing `~/.claude/projects`, corrupt jsonl line, missing/corrupt
24
- * config, garbled usage object) collapses to a value with `null` pcts, never an exception.
25
- * - **readonly** — zero writes/appends/mkdir/unlink anywhere (the optional aggregate cache in
26
- * arch §2.2(6) is DEFERRED v1 ships with zero writes so READONLY is trivially true).
27
- * - **<100ms steady-state** via an `mtime` prefilter: a file whose `mtime` is older than the weekly
28
- * window cannot contribute and is skipped WITHOUT opening it; only files touched inside the
29
- * session window are line-parsed for the active block.
30
- * - **injectable clock** — `computeUsage(root, now?)` takes an optional `now` (ms epoch) so all
31
- * window math is deterministic under test.
22
+ * - never-throw — missing/corrupt config, transcript directories, and jsonl lines collapse to
23
+ * best-effort zero/null values, never exceptions;
24
+ * - readonly — `computeUsage` performs zero writes;
25
+ * - bounded scanning via an mtime prefilter;
26
+ * - injectable clock `computeUsage(root, now?)` takes an optional epoch-ms clock.
32
27
  *
33
28
  * @packageDocumentation
34
29
  */
30
+ export declare const CLAUDE_USAGE_MODELS: readonly ["fable", "opus", "sonnet", "haiku"];
31
+ export type ClaudeUsageModel = (typeof CLAUDE_USAGE_MODELS)[number];
32
+ export interface UsageWindow {
33
+ readonly startedAtMs: number;
34
+ readonly resetsAtMs: number;
35
+ }
36
+ export interface WeeklyResetAnchor {
37
+ readonly weekday: number;
38
+ readonly hour: number;
39
+ readonly minute: number;
40
+ }
35
41
  /** Optional, plan-dependent calibration limits from `.dz/config.json`. Absent ⇒ pct is `null`. */
36
42
  export interface UsageLimits {
37
43
  readonly sessionTokenLimit?: number;
38
44
  readonly weeklyTokenLimit?: number;
45
+ readonly weeklyTokenLimitByModel?: Partial<Record<ClaudeUsageModel, number>>;
46
+ readonly weeklyResetAnchor?: string;
47
+ readonly sessionBlockHours?: number;
48
+ readonly calibratedAt?: string;
49
+ readonly source?: string;
50
+ }
51
+ export interface UsageModelEstimate {
52
+ readonly tokens: number;
53
+ readonly pct: number | null;
39
54
  }
40
55
  /** A never-throw usage estimate. `estimated` is ALWAYS `true` (honest-uncertainty marker). */
41
56
  export interface UsageEstimate {
42
- /** Active 5h-block token total (all projects). `0` when there is no active block. */
57
+ /** Active fixed-length session block token total (all projects). */
43
58
  readonly sessionTokens: number;
44
- /** Rolling 7d token total (all projects). */
59
+ /** Fixed weekly-reset-window token total (all projects). */
45
60
  readonly weeklyTokens: number;
46
- /** `null` ⇔ `sessionTokenLimit` unconfigured (unknown — never 0). */
61
+ /** `null` ⇔ `sessionTokenLimit` unconfigured (unknown — never a guess). */
47
62
  readonly sessionPct: number | null;
48
- /** `null` `weeklyTokenLimit` unconfigured (unknown never 0). */
63
+ /** Binding weekly pct: per-model max when configured, otherwise all-model aggregate. */
49
64
  readonly weeklyPct: number | null;
50
- /** ISO — active block start + 5h; `null` when there is no active block. */
65
+ /** ISO — active block start + configured session hours; `null` when there is no active block. */
51
66
  readonly sessionResetsAt: string | null;
52
- /** ISO — oldest in-window sample ts + 7d (estimate); `null` when the weekly window is empty. */
67
+ /** ISO — next configured fixed weekly reset. */
53
68
  readonly weeklyResetsAt: string | null;
54
69
  /** ALWAYS `true` — these are estimates from local aggregation, not an official API. */
55
70
  readonly estimated: true;
71
+ /** Present only when per-model weekly limits are configured. */
72
+ readonly weeklyByModel?: Partial<Record<ClaudeUsageModel, UsageModelEstimate>>;
73
+ /** Raw weekly model totals for calibration; CLI omits this from the compatibility JSON. */
74
+ readonly weeklyTokensByModel: Partial<Record<ClaudeUsageModel, number>>;
75
+ /** Model family that supplied the binding top-level weeklyPct, if any. */
76
+ readonly weeklyBindingModel?: ClaudeUsageModel;
77
+ /** Traceability for tests and calibration diagnostics. */
78
+ readonly sessionStartedAt: string | null;
79
+ readonly weeklyStartedAt: string | null;
80
+ }
81
+ export interface UsageCalibrationInput {
82
+ readonly sessionPct?: unknown;
83
+ readonly weeklyPct?: unknown;
84
+ readonly modelPct?: Readonly<Record<string, unknown>>;
85
+ readonly calibratedAt: string;
86
+ readonly source: 'claude.ai/settings/usage';
56
87
  }
88
+ export interface UsageCalibrationChange {
89
+ readonly key: string;
90
+ readonly before: number | null;
91
+ readonly after: number;
92
+ readonly tokens: number;
93
+ readonly pct: number;
94
+ }
95
+ export interface UsageCalibrationPlan {
96
+ readonly before: UsageLimits;
97
+ readonly after: UsageLimits;
98
+ readonly changes: readonly UsageCalibrationChange[];
99
+ readonly skipped: readonly string[];
100
+ }
101
+ export declare function parseWeeklyResetAnchor(anchor: string): WeeklyResetAnchor | null;
102
+ export declare function weeklyWindowFor(nowMs: number, anchor: string): UsageWindow | null;
103
+ /**
104
+ * A simple fixed-duration grid helper kept exported for pure date-math tests. `computeUsage` uses
105
+ * transcript-established session blocks per the feature requirements because no account-specific
106
+ * session anchor is stored today.
107
+ */
108
+ export declare function fixedBlockWindowFor(nowMs: number, blockHours: number): UsageWindow | null;
109
+ export declare function normalizeClaudeUsageModel(raw: unknown): ClaudeUsageModel | null;
110
+ export declare function normalizeClaudeUsageModelKey(raw: unknown): ClaudeUsageModel | null;
57
111
  /**
58
- * Read `memory.usage.{sessionTokenLimit,weeklyTokenLimit}` from `<projectRoot>/.dz/config.json`.
59
- * NEVER throws — absent/corrupt/partial config ⇒ `{}` (⇒ pct `null`). Mirrors the
60
- * `readVectorEngineMode` never-throw shape exactly. `projectRoot` is the ONLY thing that scopes to
61
- * a project; the MEASUREMENT below is account-wide (FR-1.6).
112
+ * Read `memory.usage.*` from `<projectRoot>/.dz/config.json`. NEVER throws —
113
+ * absent/corrupt/partial config ⇒ `{}` or only valid fields.
62
114
  */
63
115
  export declare function readUsageLimits(projectRoot: string): UsageLimits;
64
116
  /**
65
117
  * Estimate SESSION + WEEKLY token usage from the local Claude transcript store. NEVER throws;
66
- * READONLY; `<100ms` steady-state via the `mtime` prefilter. `projectRoot` scopes ONLY the config
67
- * (limits) read — measurement is account-wide (all projects). `now` is injectable for tests.
118
+ * READONLY; `projectRoot` scopes ONLY the config (limits) read measurement is account-wide
119
+ * (all projects). `now` is injectable for tests.
68
120
  */
69
121
  export declare function computeUsage(projectRoot: string, now?: number): UsageEstimate;
122
+ export declare function deriveUsageCalibration(current: UsageEstimate, before: UsageLimits, input: UsageCalibrationInput): UsageCalibrationPlan;
70
123
  //# sourceMappingURL=usage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../src/usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAaH,kGAAkG;AAClG,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CACpC;AAED,8FAA8F;AAC9F,MAAM,WAAW,aAAa;IAC5B,qFAAqF;IACrF,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,6CAA6C;IAC7C,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,qEAAqE;IACrE,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,oEAAoE;IACpE,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,2EAA2E;IAC3E,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IACxC,gGAAgG;IAChG,QAAQ,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IACvC,uFAAuF;IACvF,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;CAC1B;AAaD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,WAAW,CAgBhE;AAsID;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,CAAC,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