@dzhechkov/harness-core 0.7.4 → 0.7.6
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/.dz-manifest.json +141 -61
- package/README.md +38 -1
- package/dist/agentdb-index.d.ts.map +1 -1
- package/dist/agentdb-index.js +26 -2
- package/dist/agentdb-index.js.map +1 -1
- package/dist/amendment-trace.d.ts.map +1 -1
- package/dist/amendment-trace.js +6 -1
- package/dist/amendment-trace.js.map +1 -1
- package/dist/cadence.d.ts +66 -0
- package/dist/cadence.d.ts.map +1 -0
- package/dist/cadence.js +222 -0
- package/dist/cadence.js.map +1 -0
- package/dist/feature-adr-routing.d.ts.map +1 -1
- package/dist/feature-adr-routing.js +7 -2
- package/dist/feature-adr-routing.js.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/loop-blobs.generated.js +2 -2
- package/dist/loop-blobs.generated.js.map +1 -1
- package/dist/operations.d.ts.map +1 -1
- package/dist/operations.js +53 -18
- package/dist/operations.js.map +1 -1
- package/dist/provenance.d.ts +6 -0
- package/dist/provenance.d.ts.map +1 -1
- package/dist/provenance.js +22 -1
- package/dist/provenance.js.map +1 -1
- package/dist/publish.d.ts +31 -0
- package/dist/publish.d.ts.map +1 -1
- package/dist/publish.js +78 -0
- package/dist/publish.js.map +1 -1
- package/dist/recall-hook-policy.d.ts +3 -1
- package/dist/recall-hook-policy.d.ts.map +1 -1
- package/dist/recall-hook-policy.js +15 -2
- package/dist/recall-hook-policy.js.map +1 -1
- package/dist/recall-usage.d.ts +15 -0
- package/dist/recall-usage.d.ts.map +1 -1
- package/dist/recall-usage.js +51 -1
- package/dist/recall-usage.js.map +1 -1
- package/dist/score.d.ts.map +1 -1
- package/dist/score.js +38 -4
- package/dist/score.js.map +1 -1
- package/dist/skill-drift.d.ts +8 -2
- package/dist/skill-drift.d.ts.map +1 -1
- package/dist/skill-drift.js +60 -11
- package/dist/skill-drift.js.map +1 -1
- package/dist/skill-install-roots.d.ts +100 -0
- package/dist/skill-install-roots.d.ts.map +1 -0
- package/dist/skill-install-roots.js +116 -0
- package/dist/skill-install-roots.js.map +1 -0
- package/dist/tg-post.d.ts +76 -0
- package/dist/tg-post.d.ts.map +1 -0
- package/dist/tg-post.js +158 -0
- package/dist/tg-post.js.map +1 -0
- package/dist/usage.d.ts +31 -0
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +108 -21
- package/dist/usage.js.map +1 -1
- package/dist/writer-quiescence.d.ts +42 -0
- package/dist/writer-quiescence.d.ts.map +1 -0
- package/dist/writer-quiescence.js +82 -0
- package/dist/writer-quiescence.js.map +1 -0
- package/package.json +13 -13
- package/sbom.json +260 -60
- package/src/agentdb-index.ts +26 -2
- package/src/amendment-trace.ts +6 -1
- package/src/cadence.ts +227 -0
- package/src/feature-adr-routing.ts +7 -2
- package/src/index.ts +8 -1
- package/src/loop-blobs.generated.ts +2 -2
- package/src/operations.ts +52 -19
- package/src/provenance.ts +23 -1
- package/src/publish.ts +98 -0
- package/src/recall-hook-policy.ts +15 -2
- package/src/recall-usage.ts +44 -1
- package/src/score.ts +30 -4
- package/src/skill-drift.ts +70 -13
- package/src/skill-install-roots.ts +119 -0
- package/src/tg-post.ts +192 -0
- package/src/usage.ts +132 -17
- package/src/writer-quiescence.ts +95 -0
package/src/tg-post.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* `dz tg-post` — the pure half: validate an approved draft against what Telegram will accept and
|
|
4
|
+
* what the channel's own accepted design demands.
|
|
5
|
+
*
|
|
6
|
+
* The design is NOT this module's to invent. features/genai-tweets-channel/ carries four ACCEPTED
|
|
7
|
+
* ADRs (2026-08-04): HTML mode, never MarkdownV2 (18 escapes against 3, one miss is a 400); the
|
|
8
|
+
* cadence rule "no posts 00:00-06:00 MSK"; link previews off by default because x.com previews in
|
|
9
|
+
* Telegram have been broken since 2022; and ADR-004's standing order — publishing stays MANUAL, and
|
|
10
|
+
* autonomous publishing means REVISING the ADR, not flipping a quiet flag. This module only makes
|
|
11
|
+
* those decisions checkable.
|
|
12
|
+
*
|
|
13
|
+
* PURE: no filesystem, no network, no clock — the timestamp arrives as a parameter, or the MSK
|
|
14
|
+
* night-window rule could not be tested at all.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Tags Bot API accepts in HTML mode (verified against the live docs, Bot API 10.2). */
|
|
18
|
+
export const TG_ALLOWED_TAGS: readonly string[] = [
|
|
19
|
+
'b', 'strong', 'i', 'em', 'u', 'ins', 's', 'strike', 'del', 'tg-spoiler',
|
|
20
|
+
'a', 'tg-emoji', 'code', 'pre', 'blockquote',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/** Hard ceiling for `sendMessage.text`, characters after entity parsing. */
|
|
24
|
+
export const TG_TEXT_LIMIT = 4096;
|
|
25
|
+
|
|
26
|
+
export interface TgHtmlIssue {
|
|
27
|
+
readonly kind: 'unknown-tag' | 'unclosed-tag' | 'stray-close' | 'bare-ampersand' | 'bare-angle' | 'over-limit' | 'empty';
|
|
28
|
+
readonly detail: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The character count Telegram limits: text WITHOUT the markup. An approximation is stated as one —
|
|
33
|
+
* entities like tg-emoji count differently — but a draft within this bound by a margin is safe, and
|
|
34
|
+
* the render prints the number so the author sees the headroom, not a verdict.
|
|
35
|
+
*/
|
|
36
|
+
export function tgVisibleLength(html: string): number {
|
|
37
|
+
return html.replace(/<[^>]*>/g, '').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').length;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Everything wrong with a draft, or an empty list. One pass, every finding named — a validator that
|
|
42
|
+
* stops at the first fault sends the author around the loop once per mistake.
|
|
43
|
+
*/
|
|
44
|
+
export function tgPostHtmlIssues(html: string): TgHtmlIssue[] {
|
|
45
|
+
const issues: TgHtmlIssue[] = [];
|
|
46
|
+
const text = html.trim();
|
|
47
|
+
if (text === '') return [{ kind: 'empty', detail: 'the draft is empty — nothing to send' }];
|
|
48
|
+
|
|
49
|
+
// Tag balance over the allowed set. Telegram closes nothing for you: an unclosed <b> is a 400.
|
|
50
|
+
const stack: string[] = [];
|
|
51
|
+
const tagRe = /<(\/?)([a-zA-Z-]+)((?:\s+[a-zA-Z-]+(?:="[^"]*")?)*)\s*(\/?)>/g;
|
|
52
|
+
let covered = 0;
|
|
53
|
+
for (let m = tagRe.exec(text); m !== null; m = tagRe.exec(text)) {
|
|
54
|
+
covered += 1;
|
|
55
|
+
const closing = m[1] === '/';
|
|
56
|
+
const name = (m[2] as string).toLowerCase();
|
|
57
|
+
const expandable = name === 'blockquote'; // `<blockquote expandable>` is the ADR-003 body form
|
|
58
|
+
if (!TG_ALLOWED_TAGS.includes(name) && !expandable) {
|
|
59
|
+
issues.push({ kind: 'unknown-tag', detail: `<${name}> is not a Bot API HTML tag — Telegram answers 400 to tags it does not know` });
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (closing) {
|
|
63
|
+
if (stack.length === 0 || stack[stack.length - 1] !== name) {
|
|
64
|
+
issues.push({ kind: 'stray-close', detail: `</${name}> closes nothing that is open — tags must nest, not interleave` });
|
|
65
|
+
} else {
|
|
66
|
+
stack.pop();
|
|
67
|
+
}
|
|
68
|
+
} else if (m[4] !== '/') {
|
|
69
|
+
stack.push(name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
for (const open of stack) {
|
|
73
|
+
issues.push({ kind: 'unclosed-tag', detail: `<${open}> is never closed — Telegram closes nothing for you, this is a 400` });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Bare & and < outside tags: HTML mode requires entity-escaping exactly these.
|
|
77
|
+
const outside = text.replace(/<[^>]*>/g, '');
|
|
78
|
+
if (/&(?!(lt|gt|amp|quot|#\d+|#x[0-9a-fA-F]+);)/.test(outside)) {
|
|
79
|
+
issues.push({ kind: 'bare-ampersand', detail: 'a bare & outside an entity — HTML mode needs &' });
|
|
80
|
+
}
|
|
81
|
+
if (/</.test(outside.replace(/</g, ''))) {
|
|
82
|
+
issues.push({ kind: 'bare-angle', detail: 'a bare < that is not a known tag — Telegram reads it as markup and answers 400' });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const visible = tgVisibleLength(text);
|
|
86
|
+
if (visible > TG_TEXT_LIMIT) {
|
|
87
|
+
issues.push({ kind: 'over-limit', detail: `${visible} visible characters against the ${TG_TEXT_LIMIT} hard limit — cut ${visible - TG_TEXT_LIMIT}` });
|
|
88
|
+
}
|
|
89
|
+
void covered;
|
|
90
|
+
return issues;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface TgSendDecision {
|
|
94
|
+
readonly action: 'send' | 'refuse';
|
|
95
|
+
readonly reason: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* May this draft go out NOW?
|
|
100
|
+
*
|
|
101
|
+
* The night window is ADR-003's cadence rule, encoded as a refusal with an explicit override rather
|
|
102
|
+
* than as advice: 00:00-06:00 MSK is when the channel's audience is asleep and its author is too —
|
|
103
|
+
* a send landing then is far more often a timezone mistake than an intention. `--night` states the
|
|
104
|
+
* intention; without it the refusal names the local MSK time it computed, so the operator can check
|
|
105
|
+
* the arithmetic instead of trusting it.
|
|
106
|
+
*/
|
|
107
|
+
/**
|
|
108
|
+
* The dedup key for a post: sha256 of its VISIBLE text (markup stripped). Two drafts that render
|
|
109
|
+
* identically in Telegram are the same post even if their HTML differs by a whitespace — the key
|
|
110
|
+
* must be about what the reader sees, not the bytes (G5, ADR-005).
|
|
111
|
+
*/
|
|
112
|
+
export function tgVisibleSha256(html: string): string {
|
|
113
|
+
const visible = html.replace(/<[^>]*>/g, '').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
|
|
114
|
+
return createHash('sha256').update(visible.trim(), 'utf-8').digest('hex');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** One recorded send. sha256 is the visible-text key; ts is ISO. */
|
|
118
|
+
/** One journal line. status: 'pending' written BEFORE the send (crash-window guard), 'sent' after
|
|
119
|
+
* Telegram accepts. A legacy line with no status is treated as 'sent'. */
|
|
120
|
+
export interface TgSentRecord { readonly sha256: string; readonly ts: string; readonly status?: 'pending' | 'sent' }
|
|
121
|
+
|
|
122
|
+
export function decideTgSend(input: {
|
|
123
|
+
readonly issues: readonly TgHtmlIssue[];
|
|
124
|
+
readonly provenanceOutcome: 'allowed' | 'blocked' | 'not-established' | 'skipped';
|
|
125
|
+
readonly confirmed: boolean;
|
|
126
|
+
readonly nowUtcIso: string;
|
|
127
|
+
readonly nightOverride: boolean;
|
|
128
|
+
/** G6 stop-cord: a `.dz/tg-post/HALT` file exists. Checked FIRST, before anything else. */
|
|
129
|
+
readonly halted?: boolean;
|
|
130
|
+
/** sha256 of THIS post's visible text (tgVisibleSha256) — the G5 dedup key. */
|
|
131
|
+
readonly sha256?: string;
|
|
132
|
+
/** The send journal as FACTS (the pure half never reads a file). Undefined ⇒ log unreadable. */
|
|
133
|
+
readonly sentLog?: readonly TgSentRecord[] | undefined;
|
|
134
|
+
/** G4 ceiling; default 10 (ADR-003). Sends in the trailing 24h are counted against it. */
|
|
135
|
+
readonly maxPostsPerDay?: number;
|
|
136
|
+
}): TgSendDecision {
|
|
137
|
+
// G6 (ADR-001 fail-closed-guard-order): the stop-cord is ABSOLUTE and FIRST — it halts even a
|
|
138
|
+
// perfect post, before any cheaper check, so no bug in a later gate can route around it.
|
|
139
|
+
if (input.halted === true) {
|
|
140
|
+
return { action: 'refuse', reason: 'STOP-CORD engaged (.dz/tg-post/HALT exists) — nothing is published while it is present; remove the file to resume' };
|
|
141
|
+
}
|
|
142
|
+
if (input.issues.length > 0) {
|
|
143
|
+
return { action: 'refuse', reason: `${input.issues.length} formatting issue(s) — Telegram would refuse or mangle this draft` };
|
|
144
|
+
}
|
|
145
|
+
// The provenance gate is not optional and "skipped" is not a pass: ADR-002 of the provenance
|
|
146
|
+
// feature — nothing leaves this machine citing a source that may not.
|
|
147
|
+
if (input.provenanceOutcome !== 'allowed') {
|
|
148
|
+
return {
|
|
149
|
+
action: 'refuse',
|
|
150
|
+
reason: input.provenanceOutcome === 'skipped'
|
|
151
|
+
? 'no provenance manifest was checked — an unchecked draft is not an approved draft'
|
|
152
|
+
: `the provenance gate said ${input.provenanceOutcome} — nothing goes out citing a source that may not leave this machine`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (!input.confirmed) {
|
|
156
|
+
return { action: 'refuse', reason: 'publishing is MANUAL by the channel\'s own ADR-004 — pass --send --yes to state the decision out loud' };
|
|
157
|
+
}
|
|
158
|
+
const utc = Date.parse(input.nowUtcIso);
|
|
159
|
+
if (Number.isFinite(utc)) {
|
|
160
|
+
const mskHour = new Date(utc + 3 * 3600_000).getUTCHours();
|
|
161
|
+
if (mskHour < 6 && !input.nightOverride) {
|
|
162
|
+
return { action: 'refuse', reason: `it is ${String(mskHour).padStart(2, '0')}:xx MSK — the channel posts nothing between 00:00 and 06:00 MSK (ADR-003). Pass --night if this is deliberate` };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// G4/G5 need the journal. An UNREADABLE journal (sentLog undefined) is fail-closed: an unreadable
|
|
166
|
+
// limit counter does not prove the limit is unreached (Step-0 recall — never invert fail-closed on
|
|
167
|
+
// an absent/degraded input).
|
|
168
|
+
if (input.sentLog === undefined) {
|
|
169
|
+
return { action: 'refuse', reason: 'the send journal could not be read — an unreadable limit/dedup counter cannot show the ceiling is unreached; refusing (fail-closed)' };
|
|
170
|
+
}
|
|
171
|
+
// G5 dedup: this exact visible-text post already went out.
|
|
172
|
+
// G5 dedup covers BOTH a completed send AND an in-flight PENDING record of the same visible text
|
|
173
|
+
// (Codex A- two-phase): a pending row means a prior attempt reached the send path — do not
|
|
174
|
+
// double-publish. A stuck pending is surfaced (the CLI warns) and cleared by removing its line.
|
|
175
|
+
if (input.sha256 !== undefined && input.sentLog.some((r) => r.sha256 === input.sha256)) {
|
|
176
|
+
const prior = input.sentLog.find((r) => r.sha256 === input.sha256);
|
|
177
|
+
return { action: 'refuse', reason: (prior && prior.status === 'pending')
|
|
178
|
+
? 'a PENDING send of this exact post exists (a prior attempt reached the send path) — refusing to double-publish; if that attempt truly failed, remove its line from .dz/tg-post/sent-log.jsonl and retry'
|
|
179
|
+
: 'this exact post (by visible text) has already been sent — refusing a duplicate' };
|
|
180
|
+
}
|
|
181
|
+
// G4 daily limit: sends in the trailing 24h against the ceiling.
|
|
182
|
+
const nowMs = Date.parse(input.nowUtcIso);
|
|
183
|
+
const cutoff = Number.isFinite(nowMs) ? nowMs - 24 * 3600_000 : -Infinity;
|
|
184
|
+
// Only ACCEPTED sends count against the daily limit — a pending row may never have landed, so it
|
|
185
|
+
// must not eat the ceiling (mirror of 'record after'): status 'pending' is excluded, 'sent'/legacy counted.
|
|
186
|
+
const inWindow = input.sentLog.filter((r) => { const t = Date.parse(r.ts); return Number.isFinite(t) && t >= cutoff && r.status !== 'pending'; }).length;
|
|
187
|
+
const ceiling = typeof input.maxPostsPerDay === 'number' && input.maxPostsPerDay > 0 ? Math.floor(input.maxPostsPerDay) : 10;
|
|
188
|
+
if (inWindow >= ceiling) {
|
|
189
|
+
return { action: 'refuse', reason: `daily limit reached: ${inWindow} post(s) sent in the last 24h, ceiling ${ceiling} (ADR-003) — the channel does not exceed its own tempo` };
|
|
190
|
+
}
|
|
191
|
+
return { action: 'send', reason: 'stop-cord clear, formatted, provenance-cleared, confirmed, inside hours, not a duplicate, under the daily limit' };
|
|
192
|
+
}
|
package/src/usage.ts
CHANGED
|
@@ -146,6 +146,11 @@ export interface UsageLimits {
|
|
|
146
146
|
readonly sessionBlockHours?: number;
|
|
147
147
|
readonly calibratedAt?: string;
|
|
148
148
|
readonly source?: string;
|
|
149
|
+
/** Routing must not read the estimated pcts (ADR-001 usage-honesty, FR-3). Legacy
|
|
150
|
+
* `_disabledReason` free-text also reads as true — the note WAS the switch, now it is data. */
|
|
151
|
+
readonly routingDisabled?: boolean;
|
|
152
|
+
/** Account identity captured at calibration time (FR-4); a login change stales the calibration. */
|
|
153
|
+
readonly calibrationAccount?: string | null;
|
|
149
154
|
}
|
|
150
155
|
|
|
151
156
|
export interface UsageModelEstimate {
|
|
@@ -178,6 +183,21 @@ export interface UsageEstimate {
|
|
|
178
183
|
/** Traceability for tests and calibration diagnostics. */
|
|
179
184
|
readonly sessionStartedAt: string | null;
|
|
180
185
|
readonly weeklyStartedAt: string | null;
|
|
186
|
+
/**
|
|
187
|
+
* Why the routed pcts are null (ADR-001 usage-honesty): empty ⇔ the numbers are established.
|
|
188
|
+
* `scan-empty` — recent transcripts exist yet the scan extracted nothing (instrument failure);
|
|
189
|
+
* `window-miss:*` — samples exist but the configured window filtered them all (misaligned
|
|
190
|
+
* anchor); `routing-disabled` — config says the estimates must not steer; `calibration-stale` —
|
|
191
|
+
* the account changed since calibration. A zero pct is legitimate ONLY on a machine with no
|
|
192
|
+
* recent transcripts at all.
|
|
193
|
+
*/
|
|
194
|
+
readonly notEstablished: readonly UsageNotEstablishedReason[];
|
|
195
|
+
/**
|
|
196
|
+
* The raw estimates when the routed pcts are nulled by `routing-disabled`/`calibration-stale` —
|
|
197
|
+
* for HUMAN eyes (recalled lesson: an estimated pct must never drive routing until its window is
|
|
198
|
+
* verified against the provider). Absent when the top-level pcts already carry the numbers.
|
|
199
|
+
*/
|
|
200
|
+
readonly estimatesNotForRouting?: { readonly sessionPct: number | null; readonly weeklyPct: number | null };
|
|
181
201
|
}
|
|
182
202
|
|
|
183
203
|
export interface UsageCalibrationInput {
|
|
@@ -211,6 +231,8 @@ interface MutableUsageLimits {
|
|
|
211
231
|
sessionBlockHours?: number;
|
|
212
232
|
calibratedAt?: string;
|
|
213
233
|
source?: string;
|
|
234
|
+
routingDisabled?: boolean;
|
|
235
|
+
calibrationAccount?: string | null;
|
|
214
236
|
}
|
|
215
237
|
|
|
216
238
|
const WEEKDAY_TO_DAY: Record<string, number> = {
|
|
@@ -337,10 +359,59 @@ export function normalizeClaudeUsageModelKey(raw: unknown): ClaudeUsageModel | n
|
|
|
337
359
|
return (CLAUDE_USAGE_MODELS as readonly string[]).includes(key) ? (key as ClaudeUsageModel) : null;
|
|
338
360
|
}
|
|
339
361
|
|
|
362
|
+
/** Recursive .jsonl collector under a subagents tree — bounded depth, lstat-guarded. */
|
|
363
|
+
function walkTranscriptTree(dir: string, depthLeft: number, out: Array<{ path: string; mtimeMs: number }>): void {
|
|
364
|
+
if (depthLeft <= 0) return;
|
|
365
|
+
let entries: string[];
|
|
366
|
+
try {
|
|
367
|
+
if (!lstatSync(dir).isDirectory()) return; // symlinked dir ⇒ not walked
|
|
368
|
+
entries = readdirSync(dir);
|
|
369
|
+
} catch {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
for (const e of entries) {
|
|
373
|
+
const p = join(dir, e);
|
|
374
|
+
if (e.endsWith('.jsonl')) {
|
|
375
|
+
const m = regularFileMtime(p);
|
|
376
|
+
if (m !== null) out.push({ path: p, mtimeMs: m });
|
|
377
|
+
} else {
|
|
378
|
+
walkTranscriptTree(p, depthLeft - 1, out);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
340
383
|
/**
|
|
341
384
|
* The `~/.claude/projects` root (the account-wide transcript store). Overridable via
|
|
342
385
|
* `DZ_CLAUDE_PROJECTS_ROOT` — used by tests to point at a temp tree. Never throws.
|
|
343
386
|
*/
|
|
387
|
+
/**
|
|
388
|
+
* The logged-in account identity, from `~/.claude.json` (oauthAccount email or uuid). Honest null
|
|
389
|
+
* when unreadable/absent — and null==null is NOT an account change (machines that never expose it
|
|
390
|
+
* keep the pre-FR-4 behavior). Never throws.
|
|
391
|
+
*/
|
|
392
|
+
export function readClaudeAccountId(): string | null {
|
|
393
|
+
try {
|
|
394
|
+
const raw = JSON.parse(readFileSync(join(homedir(), '.claude.json'), 'utf-8')) as {
|
|
395
|
+
oauthAccount?: { emailAddress?: unknown; accountUuid?: unknown };
|
|
396
|
+
};
|
|
397
|
+
const email = raw.oauthAccount?.emailAddress;
|
|
398
|
+
if (typeof email === 'string' && email !== '') return email;
|
|
399
|
+
const uuid = raw.oauthAccount?.accountUuid;
|
|
400
|
+
if (typeof uuid === 'string' && uuid !== '') return uuid;
|
|
401
|
+
return null;
|
|
402
|
+
} catch {
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Closed set of not-established reasons (ADR-001). */
|
|
408
|
+
export type UsageNotEstablishedReason =
|
|
409
|
+
| 'scan-empty'
|
|
410
|
+
| 'window-miss:weekly'
|
|
411
|
+
| 'routing-disabled'
|
|
412
|
+
| 'calibration-stale:account-changed'
|
|
413
|
+
| 'calibration-stale:account-unverifiable';
|
|
414
|
+
|
|
344
415
|
export function claudeProjectsRoot(): string {
|
|
345
416
|
const override = process.env['DZ_CLAUDE_PROJECTS_ROOT'];
|
|
346
417
|
if (typeof override === 'string' && override.length > 0) return override;
|
|
@@ -387,6 +458,12 @@ export function readUsageLimits(projectRoot: string): UsageLimits {
|
|
|
387
458
|
|
|
388
459
|
if (typeof u['calibratedAt'] === 'string') out.calibratedAt = u['calibratedAt'];
|
|
389
460
|
if (typeof u['source'] === 'string') out.source = u['source'];
|
|
461
|
+
// routingDisabled: the boolean is authoritative; the legacy free-text note counts as true so
|
|
462
|
+
// the fleet's existing config disables TODAY, without an edit.
|
|
463
|
+
if (u['routingDisabled'] === true || typeof u['_disabledReason'] === 'string') out.routingDisabled = true;
|
|
464
|
+
if (typeof u['calibrationAccount'] === 'string' || u['calibrationAccount'] === null) {
|
|
465
|
+
out.calibrationAccount = u['calibrationAccount'] as string | null;
|
|
466
|
+
}
|
|
390
467
|
return out;
|
|
391
468
|
} catch {
|
|
392
469
|
return {};
|
|
@@ -440,22 +517,15 @@ function listTranscriptFiles(root: string): Array<{ path: string; mtimeMs: numbe
|
|
|
440
517
|
continue;
|
|
441
518
|
}
|
|
442
519
|
for (const f of files) {
|
|
443
|
-
// A session's SUBAGENT transcripts live
|
|
444
|
-
//
|
|
445
|
-
//
|
|
520
|
+
// A session's SUBAGENT transcripts live under `<session>/subagents/` and carry real,
|
|
521
|
+
// non-duplicated usage that was silently excluded (MEASURED: 27 such files in the first
|
|
522
|
+
// round). The walk is RECURSIVE with a depth cap: workflow agents write to
|
|
523
|
+
// `subagents/workflows/wf_*/agent-*.jsonl` — one level deeper than the first fix reached —
|
|
524
|
+
// and that blind spot alone hid 283.62M weighted tokens across 551 files (MEASURED
|
|
525
|
+
// 2026-08-24, 7-day window, this machine). Depth 4 covers today's deepest layout plus one
|
|
526
|
+
// future level; lstat at EVERY step keeps symlinked directories unwalked.
|
|
446
527
|
if (!f.endsWith('.jsonl')) {
|
|
447
|
-
|
|
448
|
-
try {
|
|
449
|
-
if (!lstatSync(nested).isDirectory()) continue; // no symlinked session/subagent dirs
|
|
450
|
-
for (const sf of readdirSync(nested)) {
|
|
451
|
-
if (!sf.endsWith('.jsonl')) continue;
|
|
452
|
-
const sp = join(nested, sf);
|
|
453
|
-
const m = regularFileMtime(sp);
|
|
454
|
-
if (m !== null) out.push({ path: sp, mtimeMs: m });
|
|
455
|
-
}
|
|
456
|
-
} catch {
|
|
457
|
-
/* not a session dir — skip */
|
|
458
|
-
}
|
|
528
|
+
walkTranscriptTree(join(projDir, f, 'subagents'), 4, out);
|
|
459
529
|
continue;
|
|
460
530
|
}
|
|
461
531
|
const p = join(projDir, f);
|
|
@@ -618,11 +688,13 @@ export function computeUsage(projectRoot: string, now?: number): UsageEstimate {
|
|
|
618
688
|
|
|
619
689
|
const samples: Sample[] = [];
|
|
620
690
|
const seen = new Set<string>();
|
|
691
|
+
let scanFileCount = 0;
|
|
621
692
|
try {
|
|
622
693
|
const files = listTranscriptFiles(claudeProjectsRoot());
|
|
623
694
|
for (const f of files) {
|
|
624
695
|
// mtime prefilter: a file last written before every relevant cutoff cannot contribute.
|
|
625
696
|
if (f.mtimeMs < scanCutoff) continue;
|
|
697
|
+
scanFileCount += 1;
|
|
626
698
|
extractSamples(f.path, scanCutoff, samples, seen);
|
|
627
699
|
}
|
|
628
700
|
} catch {
|
|
@@ -660,11 +732,46 @@ export function computeUsage(projectRoot: string, now?: number): UsageEstimate {
|
|
|
660
732
|
}
|
|
661
733
|
}
|
|
662
734
|
|
|
735
|
+
// ── Establishment gate (ADR-001): a number may only flow to the routed pct fields when the
|
|
736
|
+
// scan actually established it. Fail-closed in exactly four named ways; the raw estimates stay
|
|
737
|
+
// visible to humans under estimatesNotForRouting when policy (not measurement) nulls them.
|
|
738
|
+
const reasons: UsageNotEstablishedReason[] = [];
|
|
739
|
+
const recentFiles = scanFileCount > 0;
|
|
740
|
+
if (recentFiles && samples.length === 0) reasons.push('scan-empty');
|
|
741
|
+
// An IDLE session inside a busy week (block 0, weekly > 0) is a MEASURED zero, not a miss — the
|
|
742
|
+
// first cut of this gate flagged it and four standing tests rightly reddened. Second narrowing
|
|
743
|
+
// (cross-family review): a week that JUST reset over an idle machine still scans pre-reset
|
|
744
|
+
// samples (the session cutoff reaches 10h back), and weekly 0 is then a healthy fresh week. The
|
|
745
|
+
// true miss signature needs a sample AT or PAST the window start that the window still refuses —
|
|
746
|
+
// future-stamped (clock skew) or beyond-reset (stale anchor) — exactly d3639bf0's shape.
|
|
747
|
+
if (
|
|
748
|
+
weeklyWindow !== null &&
|
|
749
|
+
weeklyTokens <= 0 &&
|
|
750
|
+
samples.some((smp) => smp.ts >= weeklyWindow.startedAtMs)
|
|
751
|
+
) {
|
|
752
|
+
reasons.push('window-miss:weekly');
|
|
753
|
+
}
|
|
754
|
+
if (limits.routingDisabled === true) reasons.push('routing-disabled');
|
|
755
|
+
const account = readClaudeAccountId();
|
|
756
|
+
if (limits.calibrationAccount !== undefined && limits.calibrationAccount !== null) {
|
|
757
|
+
// A stored identity DEMANDS verification (cross-family review: null-current was fail-open —
|
|
758
|
+
// an unreadable ~/.claude.json silently reused another account's calibration). A stored null
|
|
759
|
+
// stays exempt: those machines never claimed an identity to verify.
|
|
760
|
+
if (account === null) reasons.push('calibration-stale:account-unverifiable');
|
|
761
|
+
else if (account !== limits.calibrationAccount) reasons.push('calibration-stale:account-changed');
|
|
762
|
+
}
|
|
763
|
+
const rawSessionPct = pct(block.tokens, limits.sessionTokenLimit);
|
|
764
|
+
const rawWeeklyPct = weeklyPct;
|
|
765
|
+
const measurementBroken = reasons.some((r) => r === 'scan-empty' || r.startsWith('window-miss'));
|
|
766
|
+
const policyNulled = reasons.some((r) => r === 'routing-disabled' || r.startsWith('calibration-stale'));
|
|
767
|
+
const gatedSessionPct = measurementBroken || policyNulled ? null : rawSessionPct;
|
|
768
|
+
const gatedWeeklyPct = measurementBroken || policyNulled ? null : rawWeeklyPct;
|
|
769
|
+
|
|
663
770
|
return {
|
|
664
771
|
sessionTokens: block.tokens,
|
|
665
772
|
weeklyTokens,
|
|
666
|
-
sessionPct:
|
|
667
|
-
weeklyPct,
|
|
773
|
+
sessionPct: gatedSessionPct,
|
|
774
|
+
weeklyPct: gatedWeeklyPct,
|
|
668
775
|
sessionResetsAt: block.resetsAtMs === null ? null : new Date(block.resetsAtMs).toISOString(),
|
|
669
776
|
weeklyResetsAt: weeklyWindow === null ? null : new Date(weeklyWindow.resetsAtMs).toISOString(),
|
|
670
777
|
estimated: true,
|
|
@@ -673,6 +780,10 @@ export function computeUsage(projectRoot: string, now?: number): UsageEstimate {
|
|
|
673
780
|
...(weeklyBindingModel !== undefined ? { weeklyBindingModel } : {}),
|
|
674
781
|
sessionStartedAt: block.startedAtMs === null ? null : new Date(block.startedAtMs).toISOString(),
|
|
675
782
|
weeklyStartedAt: weeklyWindow === null ? null : new Date(weeklyWindow.startedAtMs).toISOString(),
|
|
783
|
+
notEstablished: reasons,
|
|
784
|
+
...(policyNulled && !measurementBroken
|
|
785
|
+
? { estimatesNotForRouting: { sessionPct: rawSessionPct, weeklyPct: rawWeeklyPct } }
|
|
786
|
+
: {}),
|
|
676
787
|
};
|
|
677
788
|
}
|
|
678
789
|
|
|
@@ -782,6 +893,10 @@ export function deriveUsageCalibration(
|
|
|
782
893
|
if (changes.length > 0) {
|
|
783
894
|
after.calibratedAt = input.calibratedAt;
|
|
784
895
|
after.source = input.source;
|
|
896
|
+
// FR-4: a calibration is a claim about ONE account's limits. Stamp whose — a later login under
|
|
897
|
+
// a different identity then stales it by itself (the 2026-08-24 re-login is the reproducer:
|
|
898
|
+
// the old anchor kept printing 55% on the new account).
|
|
899
|
+
after.calibrationAccount = readClaudeAccountId();
|
|
785
900
|
}
|
|
786
901
|
|
|
787
902
|
return { before, after, changes, skipped };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writer-quiescence probe for Step 8 — feature qe-writer-quiescence (backlog 700b46a4).
|
|
3
|
+
*
|
|
4
|
+
* MEASURED (crossrt-1, 2026-08-18): Step-8 graded a MOVING tree — a background worker wrote at
|
|
5
|
+
* 19:25, 19:32 and 19:46, AFTER the verdict, its last write clobbering a file the same round had
|
|
6
|
+
* just written. The reviewer hand-waited six consecutive 30-second zero-write windows and
|
|
7
|
+
* re-measured everything. This module is that wait, as a machine: the same idea the publish gate
|
|
8
|
+
* already enforces («не публикуй, пока рой жив»), applied to grading.
|
|
9
|
+
*
|
|
10
|
+
* A BELT, not the root: mutual exclusion of writers (worktree isolation) is item 9520e506. The
|
|
11
|
+
* probe therefore NEVER blocks a run — a moving tree downgrades the verdict's standing loudly
|
|
12
|
+
* instead of stopping the pipeline.
|
|
13
|
+
*
|
|
14
|
+
* PURE: the shell script is GENERATED here and executed by a workflow agent (the workflow sandbox
|
|
15
|
+
* has no child_process — the agent is the shell, same as the landing barrier); the answer is
|
|
16
|
+
* PARSED here, parse-never-synthesize: an empty or malformed probe is 'inconclusive', never
|
|
17
|
+
* 'quiet'.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface WriterQuiescenceDecision {
|
|
21
|
+
readonly verdict: 'quiet' | 'moving' | 'inconclusive';
|
|
22
|
+
/** Per-window changed counts actually parsed, in order. */
|
|
23
|
+
readonly windows: readonly number[];
|
|
24
|
+
readonly note: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const WQ_WINDOW_SECONDS = 20;
|
|
28
|
+
export const WQ_MAX_WINDOWS = 9;
|
|
29
|
+
export const WQ_REQUIRED_QUIET = 3;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The probe script a workflow agent runs verbatim. Polls the declared targets PLUS the feature
|
|
33
|
+
* dir (FR-4: the feature's own artifacts are exactly the surface crossrt-1 saw clobbered) in
|
|
34
|
+
* fixed windows; prints one `WQ-WINDOW <n> changed=<count>` line per window and exits early after
|
|
35
|
+
* `requiredQuiet` consecutive zeros. `find -newermt '-<w+5> seconds'` widens the lookback slightly
|
|
36
|
+
* past the sleep so a write on the window boundary cannot fall between two polls.
|
|
37
|
+
*/
|
|
38
|
+
export function quiescenceProbeScript(
|
|
39
|
+
paths: readonly string[],
|
|
40
|
+
opts?: { windowSeconds?: number; maxWindows?: number; requiredQuiet?: number },
|
|
41
|
+
): string {
|
|
42
|
+
const w = opts?.windowSeconds ?? WQ_WINDOW_SECONDS;
|
|
43
|
+
const max = opts?.maxWindows ?? WQ_MAX_WINDOWS;
|
|
44
|
+
const need = opts?.requiredQuiet ?? WQ_REQUIRED_QUIET;
|
|
45
|
+
const targets = paths
|
|
46
|
+
.map((p) => String(p).trim())
|
|
47
|
+
.filter((p) => p !== '' && !p.startsWith('-') && !p.includes("'"))
|
|
48
|
+
.map((p) => `'${p.replace(/'/g, '')}'`)
|
|
49
|
+
.join(' ');
|
|
50
|
+
const lookback = w + 5;
|
|
51
|
+
// Cross-family review (round 1, D): find errors — a missing/typo'd target, a permission failure —
|
|
52
|
+
// used to feed ZERO to `wc -l`, so a probe that could not look reported quiet. Errors now print
|
|
53
|
+
// `changed=ERR` and the parser refuses to let an ERR window feed a quiet streak.
|
|
54
|
+
return (
|
|
55
|
+
`quiet=0; n=0; while [ $n -lt ${max} ]; do n=$((n+1)); sleep ${w}; ` +
|
|
56
|
+
`out=$(find ${targets} -type f -newermt '-${lookback} seconds' 2>&1 >/tmp/wq-list.$$); st=$?; ` +
|
|
57
|
+
`if [ $st -ne 0 ] || [ -n "$out" ]; then c=ERR; else c=$(wc -l < /tmp/wq-list.$$); fi; rm -f /tmp/wq-list.$$; ` +
|
|
58
|
+
`echo "WQ-WINDOW $n changed=$c"; ` +
|
|
59
|
+
`if [ "$c" = "0" ]; then quiet=$((quiet+1)); if [ $quiet -ge ${need} ]; then echo "WQ-DONE quiet"; exit 0; fi; else quiet=0; fi; ` +
|
|
60
|
+
`done; echo "WQ-DONE budget"`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Parse the probe transcript into a verdict. Empty/malformed ⇒ inconclusive, never quiet. */
|
|
65
|
+
export function decideWriterQuiescence(probeText: unknown, requiredQuiet = WQ_REQUIRED_QUIET): WriterQuiescenceDecision {
|
|
66
|
+
const text = probeText === null || probeText === undefined ? '' : String(probeText);
|
|
67
|
+
// -1 encodes an ERR window (find could not look): it can never join a quiet streak, and its
|
|
68
|
+
// presence degrades a no-streak outcome to 'inconclusive' — an instrument that failed to observe
|
|
69
|
+
// must not testify to movement either.
|
|
70
|
+
const windows: number[] = [];
|
|
71
|
+
for (const line of text.split(/\r?\n/)) {
|
|
72
|
+
const m = /WQ-WINDOW\s+\d+\s+changed=(\d+|ERR)/.exec(line);
|
|
73
|
+
if (m) windows.push(m[1] === 'ERR' ? -1 : Number(m[1]));
|
|
74
|
+
}
|
|
75
|
+
if (windows.length === 0) {
|
|
76
|
+
return { verdict: 'inconclusive', windows, note: 'quiescence probe returned no windows — grading standing NOT established (probe failure is never quiet)' };
|
|
77
|
+
}
|
|
78
|
+
let streak = 0;
|
|
79
|
+
for (const c of windows) {
|
|
80
|
+
streak = c === 0 ? streak + 1 : 0;
|
|
81
|
+
if (streak >= requiredQuiet) {
|
|
82
|
+
// The claim is exactly what was measured (cross-family review): mtime evidence of no recent
|
|
83
|
+
// writes — never a writer-lifecycle guarantee. The root guarantee is worktree isolation.
|
|
84
|
+
return { verdict: 'quiet', windows, note: `no observed writes in ${requiredQuiet} consecutive windows (mtime evidence only — not a writer-lifecycle guarantee)` };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (windows.some((c) => c < 0)) {
|
|
88
|
+
return { verdict: 'inconclusive', windows, note: 'quiescence probe could not observe every window (find errored) — grading standing NOT established' };
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
verdict: 'moving',
|
|
92
|
+
windows,
|
|
93
|
+
note: `tree is MOVING: no ${requiredQuiet} consecutive quiet windows within budget (per-window changed counts: ${windows.join(',')}) — the verdict below was graded on a moving tree and must say so`,
|
|
94
|
+
};
|
|
95
|
+
}
|