@agent-wiki/mcp 0.20.0 → 0.21.1
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/evidence-report.d.ts +82 -0
- package/dist/evidence-report.d.ts.map +1 -0
- package/dist/evidence-report.js +368 -0
- package/dist/evidence-report.js.map +1 -0
- package/dist/evidence-report.test.d.ts +2 -0
- package/dist/evidence-report.test.d.ts.map +1 -0
- package/dist/evidence-report.test.js +334 -0
- package/dist/evidence-report.test.js.map +1 -0
- package/dist/evidence-search-log.d.ts +33 -0
- package/dist/evidence-search-log.d.ts.map +1 -0
- package/dist/evidence-search-log.js +34 -0
- package/dist/evidence-search-log.js.map +1 -0
- package/dist/evidence-search-log.test.d.ts +2 -0
- package/dist/evidence-search-log.test.d.ts.map +1 -0
- package/dist/evidence-search-log.test.js +144 -0
- package/dist/evidence-search-log.test.js.map +1 -0
- package/dist/evidence-search.d.ts +2 -1
- package/dist/evidence-search.d.ts.map +1 -1
- package/dist/evidence-search.js +29 -1
- package/dist/evidence-search.js.map +1 -1
- package/dist/evidence-write-log.d.ts +73 -6
- package/dist/evidence-write-log.d.ts.map +1 -1
- package/dist/evidence-write-log.js +84 -83
- package/dist/evidence-write-log.js.map +1 -1
- package/dist/evidence-write-log.test.js +138 -7
- package/dist/evidence-write-log.test.js.map +1 -1
- package/dist/evidence.d.ts +17 -3
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +33 -4
- package/dist/evidence.js.map +1 -1
- package/dist/jsonl-log.d.ts +30 -0
- package/dist/jsonl-log.d.ts.map +1 -0
- package/dist/jsonl-log.js +97 -0
- package/dist/jsonl-log.js.map +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +86 -27
- package/dist/server.js.map +1 -1
- package/dist/server.test.js +201 -0
- package/dist/server.test.js.map +1 -1
- package/dist/wiki.d.ts +4 -0
- package/dist/wiki.d.ts.map +1 -1
- package/dist/wiki.js +25 -11
- package/dist/wiki.js.map +1 -1
- package/dist/wiki.test.js +69 -0
- package/dist/wiki.test.js.map +1 -1
- package/docs/evidence-envelope.md +22 -3
- package/package.json +1 -1
package/dist/evidence-search.js
CHANGED
|
@@ -25,8 +25,22 @@ export const ABSOLUTE_FLOOR = 2.0;
|
|
|
25
25
|
const TOP1_OVER_TOP2_STRONG = 2.0;
|
|
26
26
|
/** Provenance fan-out — top N pages cited in the envelope on a non-abstain match. */
|
|
27
27
|
const PROVENANCE_LIMIT = 3;
|
|
28
|
-
export function buildSearchEnvelope(results, query) {
|
|
28
|
+
export function buildSearchEnvelope(results, query, onEvent) {
|
|
29
|
+
// Telemetry is optional. Server call sites pass a closure that appends to
|
|
30
|
+
// the search log; tests and non-server callers omit it. Keeps this module
|
|
31
|
+
// free of filesystem knowledge.
|
|
32
|
+
const log = (event) => {
|
|
33
|
+
if (!onEvent)
|
|
34
|
+
return;
|
|
35
|
+
onEvent({ ...event, timestamp: new Date().toISOString() });
|
|
36
|
+
};
|
|
29
37
|
if (results.length === 0) {
|
|
38
|
+
log({
|
|
39
|
+
abstainReason: "no-results",
|
|
40
|
+
top1Score: null,
|
|
41
|
+
top1Top2Ratio: null,
|
|
42
|
+
confidence: "absent",
|
|
43
|
+
});
|
|
30
44
|
return {
|
|
31
45
|
provenance: [],
|
|
32
46
|
confidence: "absent",
|
|
@@ -41,6 +55,12 @@ export function buildSearchEnvelope(results, query) {
|
|
|
41
55
|
// the weak hits as supporting evidence. Mention the top hits in the
|
|
42
56
|
// rationale so a debugger can still see what the engine returned.
|
|
43
57
|
const sample = results.slice(0, PROVENANCE_LIMIT).map((r) => r.path).join(", ");
|
|
58
|
+
log({
|
|
59
|
+
abstainReason: "below-floor",
|
|
60
|
+
top1Score: top1,
|
|
61
|
+
top1Top2Ratio: null,
|
|
62
|
+
confidence: "absent",
|
|
63
|
+
});
|
|
44
64
|
return {
|
|
45
65
|
provenance: [],
|
|
46
66
|
confidence: "absent",
|
|
@@ -57,6 +77,14 @@ export function buildSearchEnvelope(results, query) {
|
|
|
57
77
|
? `Top match clearly dominates (${top1.toFixed(2)} vs ${top2.toFixed(2)}).`
|
|
58
78
|
: `Top matches are close in score (${top1.toFixed(2)} vs ${top2.toFixed(2)}); ` +
|
|
59
79
|
`multiple plausible answers — read several before relying on the top result.`;
|
|
80
|
+
log({
|
|
81
|
+
abstainReason: null,
|
|
82
|
+
top1Score: top1,
|
|
83
|
+
// Infinity is not JSON-serializable; record null when there's only one
|
|
84
|
+
// result (top2 == 0). The dashboard treats null as "single-result match".
|
|
85
|
+
top1Top2Ratio: top2 > 0 ? ratio : null,
|
|
86
|
+
confidence,
|
|
87
|
+
});
|
|
60
88
|
return {
|
|
61
89
|
provenance: results.slice(0, PROVENANCE_LIMIT).map((r) => ({ wikiPage: `wiki/${r.path}` })),
|
|
62
90
|
confidence,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evidence-search.js","sourceRoot":"","sources":["../src/evidence-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;
|
|
1
|
+
{"version":3,"file":"evidence-search.js","sourceRoot":"","sources":["../src/evidence-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAMH,kFAAkF;AAClF,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC;AAElC,wDAAwD;AACxD,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAElC,qFAAqF;AACrF,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAE3B,MAAM,UAAU,mBAAmB,CACjC,OAAuB,EACvB,KAAa,EACb,OAAsC;IAEtC,0EAA0E;IAC1E,0EAA0E;IAC1E,gCAAgC;IAChC,MAAM,GAAG,GAAG,CAAC,KAAqC,EAAE,EAAE;QACpD,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,OAAO,CAAC,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,C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CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO;QAC1D,SAAS,EACP,OAAO,CAAC,MAAM,GAAG,CAAC;YAChB,CAAC,CAAC,GAAG,QAAQ,CAAC,SAAS,gBAAgB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,SAAS,EAAE;YACxE,CAAC,CAAC,GAAG,QAAQ,CAAC,SAAS,GAAG,SAAS,EAAE;KAC1C,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAC,CAAqB;IACrC,IAAI,CAAC,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC;IAClC,IAAI,CAAC,KAAK,MAAM;QAAE,OAAO,QAAQ,CAAC;IAClC,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -54,9 +54,33 @@ export interface UnsupportedWriteEvent {
|
|
|
54
54
|
}
|
|
55
55
|
export declare function evidenceDir(workspace: string): string;
|
|
56
56
|
export declare function writeLogPath(workspace: string): string;
|
|
57
|
+
export declare function counterLogPath(workspace: string): string;
|
|
57
58
|
export declare function migrationMarkerPath(workspace: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Classification kinds for the per-write counter. Mirrors the branches in
|
|
61
|
+
* `wiki.ts` evidence classification — every classified write emits exactly
|
|
62
|
+
* one event, regardless of whether it transitions in/out of unsupported.
|
|
63
|
+
*
|
|
64
|
+
* - `grounded` — page has non-empty `sources: [...]`
|
|
65
|
+
* - `synthesis` — page has `synthesis: true` or `type: synthesis`
|
|
66
|
+
* - `unsupported`— page has neither, soft-warn mode (Phase 2a)
|
|
67
|
+
* - `rejected` — page has neither, hard-reject mode (Phase 2b)
|
|
68
|
+
* - `legacy` — page is tagged `legacyUnsupported`, soft-warn (skipped in unsupported log)
|
|
69
|
+
*/
|
|
70
|
+
export type WriteEventKind = "grounded" | "synthesis" | "unsupported" | "rejected" | "legacy";
|
|
71
|
+
export interface WriteEvent {
|
|
72
|
+
timestamp: string;
|
|
73
|
+
kind: WriteEventKind;
|
|
74
|
+
}
|
|
58
75
|
/** Append a single unsupported-write event. Best-effort — never throws. */
|
|
59
76
|
export declare function appendUnsupportedWriteEvent(workspace: string, event: UnsupportedWriteEvent): void;
|
|
77
|
+
/**
|
|
78
|
+
* Append a per-write counter event — fired on every evidence-classified
|
|
79
|
+
* write so the dashboard can compute (unsupported / total) ratios. Unlike
|
|
80
|
+
* `appendUnsupportedWriteEvent` (which dedupes on the unsupported transition),
|
|
81
|
+
* this fires on every classified write attempt. Best-effort — never throws.
|
|
82
|
+
*/
|
|
83
|
+
export declare function appendWriteEvent(workspace: string, kind: WriteEventKind, timestamp: string): void;
|
|
60
84
|
/**
|
|
61
85
|
* Read the log and return entries within the rotation window. Entries
|
|
62
86
|
* outside the time window are silently dropped from the returned list
|
|
@@ -65,21 +89,64 @@ export declare function appendUnsupportedWriteEvent(workspace: string, event: Un
|
|
|
65
89
|
*/
|
|
66
90
|
export declare function readWriteLog(workspace: string, now?: Date): UnsupportedWriteEvent[];
|
|
67
91
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
|
|
92
|
+
* Read the per-write counter log within the 30-day rotation window. Same
|
|
93
|
+
* filtering rules as `readWriteLog`.
|
|
94
|
+
*/
|
|
95
|
+
export declare function readWriteCounter(workspace: string, now?: Date): WriteEvent[];
|
|
96
|
+
/**
|
|
97
|
+
* Truncate both evidence log files (the unsupported-only log and the
|
|
98
|
+
* per-write counter) to rotation policy: drop entries older than 30 days,
|
|
99
|
+
* then if the result still exceeds 10 MB drop oldest entries until it
|
|
100
|
+
* fits. Called from `wiki_admin lint`.
|
|
101
|
+
*
|
|
102
|
+
* Returns combined counts across both files for the legacy single-file
|
|
103
|
+
* shape; per-file detail is available via `counter` / `unsupported`.
|
|
71
104
|
*/
|
|
72
105
|
export declare function rotateEventLog(workspace: string, now?: Date): {
|
|
73
106
|
entriesBefore: number;
|
|
74
107
|
entriesAfter: number;
|
|
75
108
|
bytesAfter: number;
|
|
109
|
+
unsupported: {
|
|
110
|
+
entriesBefore: number;
|
|
111
|
+
entriesAfter: number;
|
|
112
|
+
bytesAfter: number;
|
|
113
|
+
};
|
|
114
|
+
counter: {
|
|
115
|
+
entriesBefore: number;
|
|
116
|
+
entriesAfter: number;
|
|
117
|
+
bytesAfter: number;
|
|
118
|
+
};
|
|
76
119
|
};
|
|
77
120
|
/**
|
|
78
121
|
* Count writes in the last 7 days, broken down for a one-line summary
|
|
79
|
-
* the rebuild step appends to wiki/log.md.
|
|
80
|
-
*
|
|
122
|
+
* the rebuild step appends to wiki/log.md. Coexists with the Phase 4
|
|
123
|
+
* `wiki_admin --action evidence-report` dashboard: this stays as the
|
|
124
|
+
* cheap inline glance during rebuild; the dashboard is the full report
|
|
125
|
+
* when the operator asks for one.
|
|
126
|
+
*
|
|
127
|
+
* Returned numerators answer different questions:
|
|
128
|
+
*
|
|
129
|
+
* - `unsupportedTransitions` — distinct pages that *became* unsupported
|
|
130
|
+
* (from `evidence-write-log.jsonl`, deduped on transition). Answers:
|
|
131
|
+
* "how many new unsupported pages did agents introduce?"
|
|
132
|
+
*
|
|
133
|
+
* - `unsupportedWrites` — every write that ended up unsupported in
|
|
134
|
+
* warn mode, including re-edits (counter `kind: "unsupported"`).
|
|
135
|
+
*
|
|
136
|
+
* - `rejectedWrites` — every write blocked under Phase 2b reject mode
|
|
137
|
+
* (counter `kind: "rejected"`).
|
|
138
|
+
*
|
|
139
|
+
* `wouldRejectRatio` = `(unsupportedWrites + rejectedWrites) / totalWrites`.
|
|
140
|
+
* Mode-invariant: in warn mode, `rejectedWrites = 0` and the ratio
|
|
141
|
+
* reflects what 2b would block; in reject mode, `unsupportedWrites = 0`
|
|
142
|
+
* and the ratio reflects what 2b is currently blocking. `null` when
|
|
143
|
+
* `totalWrites === 0` to avoid a misleading 0% on early deployments.
|
|
81
144
|
*/
|
|
82
145
|
export declare function summarizeLastWeek(workspace: string, now?: Date): {
|
|
83
|
-
|
|
146
|
+
unsupportedTransitions: number;
|
|
147
|
+
unsupportedWrites: number;
|
|
148
|
+
rejectedWrites: number;
|
|
149
|
+
totalWrites: number;
|
|
150
|
+
wouldRejectRatio: number | null;
|
|
84
151
|
};
|
|
85
152
|
//# sourceMappingURL=evidence-write-log.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evidence-write-log.d.ts","sourceRoot":"","sources":["../src/evidence-write-log.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;
|
|
1
|
+
{"version":3,"file":"evidence-write-log.d.ts","sourceRoot":"","sources":["../src/evidence-write-log.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AASH,MAAM,WAAW,qBAAqB;IACpC,wEAAwE;IACxE,IAAI,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,SAAS,EAAE,MAAM,CAAC;IAClB,+EAA+E;IAC/E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,gBAAgB,EAAE,OAAO,CAAC;IAC1B,0EAA0E;IAC1E,eAAe,EAAE,MAAM,CAAC;IACxB;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;CACnC;AAQD,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAErD;AACD,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEtD;AACD,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAExD;AACD,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,cAAc,GACtB,UAAU,GACV,WAAW,GACX,aAAa,GACb,UAAU,GACV,QAAQ,CAAC;AAEb,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,cAAc,C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@@
|
|
|
15
15
|
*
|
|
16
16
|
* See docs/evidence-envelope.md ("Unsupported-write telemetry").
|
|
17
17
|
*/
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
const ROTATION_MAX_BYTES = 10 * 1024 * 1024; // 10 MB
|
|
21
|
-
const ROTATION_MAX_AGE_DAYS = 30;
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import { appendJsonlEvent, readJsonlInWindow, rotateJsonlFile, } from "./jsonl-log.js";
|
|
22
20
|
/** Path conventions, relative to a workspace root. */
|
|
23
21
|
const EVIDENCE_DIR = ".agent-wiki";
|
|
24
22
|
const WRITE_LOG_FILE = "evidence-write-log.jsonl";
|
|
23
|
+
const COUNTER_LOG_FILE = "evidence-write-counter.jsonl";
|
|
25
24
|
const MIGRATION_MARKER_FILE = "evidence-migrated";
|
|
26
25
|
export function evidenceDir(workspace) {
|
|
27
26
|
return join(workspace, EVIDENCE_DIR);
|
|
@@ -29,19 +28,24 @@ export function evidenceDir(workspace) {
|
|
|
29
28
|
export function writeLogPath(workspace) {
|
|
30
29
|
return join(workspace, EVIDENCE_DIR, WRITE_LOG_FILE);
|
|
31
30
|
}
|
|
31
|
+
export function counterLogPath(workspace) {
|
|
32
|
+
return join(workspace, EVIDENCE_DIR, COUNTER_LOG_FILE);
|
|
33
|
+
}
|
|
32
34
|
export function migrationMarkerPath(workspace) {
|
|
33
35
|
return join(workspace, EVIDENCE_DIR, MIGRATION_MARKER_FILE);
|
|
34
36
|
}
|
|
35
37
|
/** Append a single unsupported-write event. Best-effort — never throws. */
|
|
36
38
|
export function appendUnsupportedWriteEvent(workspace, event) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
appendJsonlEvent(writeLogPath(workspace), event);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Append a per-write counter event — fired on every evidence-classified
|
|
43
|
+
* write so the dashboard can compute (unsupported / total) ratios. Unlike
|
|
44
|
+
* `appendUnsupportedWriteEvent` (which dedupes on the unsupported transition),
|
|
45
|
+
* this fires on every classified write attempt. Best-effort — never throws.
|
|
46
|
+
*/
|
|
47
|
+
export function appendWriteEvent(workspace, kind, timestamp) {
|
|
48
|
+
appendJsonlEvent(counterLogPath(workspace), { timestamp, kind });
|
|
45
49
|
}
|
|
46
50
|
/**
|
|
47
51
|
* Read the log and return entries within the rotation window. Entries
|
|
@@ -50,90 +54,87 @@ export function appendUnsupportedWriteEvent(workspace, event) {
|
|
|
50
54
|
* malformed lines are skipped.
|
|
51
55
|
*/
|
|
52
56
|
export function readWriteLog(workspace, now = new Date()) {
|
|
53
|
-
|
|
54
|
-
if (!existsSync(path))
|
|
55
|
-
return [];
|
|
56
|
-
try {
|
|
57
|
-
const cutoffMs = now.getTime() - ROTATION_MAX_AGE_DAYS * 24 * 60 * 60 * 1000;
|
|
58
|
-
const lines = readFileSync(path, "utf-8").split("\n");
|
|
59
|
-
const events = [];
|
|
60
|
-
for (const line of lines) {
|
|
61
|
-
const trimmed = line.trim();
|
|
62
|
-
if (!trimmed)
|
|
63
|
-
continue;
|
|
64
|
-
try {
|
|
65
|
-
const entry = JSON.parse(trimmed);
|
|
66
|
-
if (!entry.timestamp)
|
|
67
|
-
continue;
|
|
68
|
-
const t = new Date(entry.timestamp).getTime();
|
|
69
|
-
if (Number.isFinite(t) && t >= cutoffMs)
|
|
70
|
-
events.push(entry);
|
|
71
|
-
}
|
|
72
|
-
catch {
|
|
73
|
-
// skip malformed line
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return events;
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
return [];
|
|
80
|
-
}
|
|
57
|
+
return readJsonlInWindow(writeLogPath(workspace), now);
|
|
81
58
|
}
|
|
82
59
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
|
|
60
|
+
* Read the per-write counter log within the 30-day rotation window. Same
|
|
61
|
+
* filtering rules as `readWriteLog`.
|
|
62
|
+
*/
|
|
63
|
+
export function readWriteCounter(workspace, now = new Date()) {
|
|
64
|
+
return readJsonlInWindow(counterLogPath(workspace), now);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Truncate both evidence log files (the unsupported-only log and the
|
|
68
|
+
* per-write counter) to rotation policy: drop entries older than 30 days,
|
|
69
|
+
* then if the result still exceeds 10 MB drop oldest entries until it
|
|
70
|
+
* fits. Called from `wiki_admin lint`.
|
|
71
|
+
*
|
|
72
|
+
* Returns combined counts across both files for the legacy single-file
|
|
73
|
+
* shape; per-file detail is available via `counter` / `unsupported`.
|
|
86
74
|
*/
|
|
87
75
|
export function rotateEventLog(workspace, now = new Date()) {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
return { entriesBefore: 0, entriesAfter: 0, bytesAfter: 0 };
|
|
91
|
-
}
|
|
92
|
-
const cutoffMs = now.getTime() - ROTATION_MAX_AGE_DAYS * 24 * 60 * 60 * 1000;
|
|
93
|
-
const lines = readFileSync(path, "utf-8").split("\n").filter((l) => l.trim());
|
|
94
|
-
const entriesBefore = lines.length;
|
|
95
|
-
// First pass: drop entries older than the time window.
|
|
96
|
-
const kept = [];
|
|
97
|
-
for (const line of lines) {
|
|
98
|
-
try {
|
|
99
|
-
const entry = JSON.parse(line);
|
|
100
|
-
const t = entry.timestamp ? new Date(entry.timestamp).getTime() : NaN;
|
|
101
|
-
if (Number.isFinite(t) && t >= cutoffMs) {
|
|
102
|
-
kept.push({ line, ts: t });
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
// malformed — drop
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
// Second pass: if still over size cap, drop oldest entries.
|
|
110
|
-
kept.sort((a, b) => a.ts - b.ts);
|
|
111
|
-
let totalBytes = kept.reduce((acc, { line }) => acc + Buffer.byteLength(line) + 1, 0);
|
|
112
|
-
while (totalBytes > ROTATION_MAX_BYTES && kept.length > 0) {
|
|
113
|
-
const dropped = kept.shift();
|
|
114
|
-
totalBytes -= Buffer.byteLength(dropped.line) + 1;
|
|
115
|
-
}
|
|
116
|
-
const newContent = kept.length === 0 ? "" : kept.map((k) => k.line).join("\n") + "\n";
|
|
117
|
-
writeFileSync(path, newContent);
|
|
76
|
+
const unsupported = rotateJsonlFile(writeLogPath(workspace), now);
|
|
77
|
+
const counter = rotateJsonlFile(counterLogPath(workspace), now);
|
|
118
78
|
return {
|
|
119
|
-
entriesBefore,
|
|
120
|
-
entriesAfter:
|
|
121
|
-
bytesAfter:
|
|
79
|
+
entriesBefore: unsupported.entriesBefore + counter.entriesBefore,
|
|
80
|
+
entriesAfter: unsupported.entriesAfter + counter.entriesAfter,
|
|
81
|
+
bytesAfter: unsupported.bytesAfter + counter.bytesAfter,
|
|
82
|
+
unsupported,
|
|
83
|
+
counter,
|
|
122
84
|
};
|
|
123
85
|
}
|
|
124
86
|
/**
|
|
125
87
|
* Count writes in the last 7 days, broken down for a one-line summary
|
|
126
|
-
* the rebuild step appends to wiki/log.md.
|
|
127
|
-
*
|
|
88
|
+
* the rebuild step appends to wiki/log.md. Coexists with the Phase 4
|
|
89
|
+
* `wiki_admin --action evidence-report` dashboard: this stays as the
|
|
90
|
+
* cheap inline glance during rebuild; the dashboard is the full report
|
|
91
|
+
* when the operator asks for one.
|
|
92
|
+
*
|
|
93
|
+
* Returned numerators answer different questions:
|
|
94
|
+
*
|
|
95
|
+
* - `unsupportedTransitions` — distinct pages that *became* unsupported
|
|
96
|
+
* (from `evidence-write-log.jsonl`, deduped on transition). Answers:
|
|
97
|
+
* "how many new unsupported pages did agents introduce?"
|
|
98
|
+
*
|
|
99
|
+
* - `unsupportedWrites` — every write that ended up unsupported in
|
|
100
|
+
* warn mode, including re-edits (counter `kind: "unsupported"`).
|
|
101
|
+
*
|
|
102
|
+
* - `rejectedWrites` — every write blocked under Phase 2b reject mode
|
|
103
|
+
* (counter `kind: "rejected"`).
|
|
104
|
+
*
|
|
105
|
+
* `wouldRejectRatio` = `(unsupportedWrites + rejectedWrites) / totalWrites`.
|
|
106
|
+
* Mode-invariant: in warn mode, `rejectedWrites = 0` and the ratio
|
|
107
|
+
* reflects what 2b would block; in reject mode, `unsupportedWrites = 0`
|
|
108
|
+
* and the ratio reflects what 2b is currently blocking. `null` when
|
|
109
|
+
* `totalWrites === 0` to avoid a misleading 0% on early deployments.
|
|
128
110
|
*/
|
|
129
111
|
export function summarizeLastWeek(workspace, now = new Date()) {
|
|
130
112
|
const sevenDaysMs = 7 * 24 * 60 * 60 * 1000;
|
|
131
113
|
const cutoff = now.getTime() - sevenDaysMs;
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
const t = new Date(e.timestamp).getTime();
|
|
114
|
+
const within = (ts) => {
|
|
115
|
+
const t = new Date(ts).getTime();
|
|
135
116
|
return Number.isFinite(t) && t >= cutoff;
|
|
136
|
-
}
|
|
137
|
-
|
|
117
|
+
};
|
|
118
|
+
// Exclude `rejected: true` entries: the unsupported log carries BOTH
|
|
119
|
+
// warn-mode transitions (deduped) AND reject-mode attempts (one per
|
|
120
|
+
// blocked retry). Only the former are real "new unsupported pages".
|
|
121
|
+
const unsupportedTransitions = readWriteLog(workspace, now)
|
|
122
|
+
.filter((e) => within(e.timestamp) && !e.rejected)
|
|
123
|
+
.length;
|
|
124
|
+
const recentCounter = readWriteCounter(workspace, now)
|
|
125
|
+
.filter((e) => within(e.timestamp));
|
|
126
|
+
const totalWrites = recentCounter.length;
|
|
127
|
+
const unsupportedWrites = recentCounter.filter((e) => e.kind === "unsupported").length;
|
|
128
|
+
const rejectedWrites = recentCounter.filter((e) => e.kind === "rejected").length;
|
|
129
|
+
const wouldRejectRatio = totalWrites === 0
|
|
130
|
+
? null
|
|
131
|
+
: (unsupportedWrites + rejectedWrites) / totalWrites;
|
|
132
|
+
return {
|
|
133
|
+
unsupportedTransitions,
|
|
134
|
+
unsupportedWrites,
|
|
135
|
+
rejectedWrites,
|
|
136
|
+
totalWrites,
|
|
137
|
+
wouldRejectRatio,
|
|
138
|
+
};
|
|
138
139
|
}
|
|
139
140
|
//# sourceMappingURL=evidence-write-log.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evidence-write-log.js","sourceRoot":"","sources":["../src/evidence-write-log.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"evidence-write-log.js","sourceRoot":"","sources":["../src/evidence-write-log.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAwCxB,sDAAsD;AACtD,MAAM,YAAY,GAAG,aAAa,CAAC;AACnC,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAClD,MAAM,gBAAgB,GAAG,8BAA8B,CAAC;AACxD,MAAM,qBAAqB,GAAG,mBAAmB,CAAC;AAElD,MAAM,UAAU,WAAW,CAAC,SAAiB;IAC3C,OAAO,IAAI,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;AACvC,CAAC;AACD,MAAM,UAAU,YAAY,CAAC,SAAiB;IAC5C,OAAO,IAAI,CAAC,SAAS,EAAE,YAAY,EAAE,cAAc,CAAC,CAAC;AACvD,CAAC;AACD,MAAM,UAAU,cAAc,CAAC,SAAiB;IAC9C,OAAO,IAAI,CAAC,SAAS,EAAE,YAAY,EAAE,gBAAgB,CAAC,CAAC;AACzD,CAAC;AACD,MAAM,UAAU,mBAAmB,CAAC,SAAiB;IACnD,OAAO,IAAI,CAAC,SAAS,EAAE,YAAY,EAAE,qBAAqB,CAAC,CAAC;AAC9D,CAAC;AAyBD,2EAA2E;AAC3E,MAAM,UAAU,2BAA2B,CACzC,SAAiB,EACjB,KAA4B;IAE5B,gBAAgB,CAAC,YAAY,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B,cAAc;QACd,WAAW;QACX,gBAAgB;KACjB,CAAC;AACJ,CAAC"}
|
|
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
|
2
2
|
import { mkdtempSync, rmSync, readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import { appendUnsupportedWriteEvent, readWriteLog, rotateEventLog, summarizeLastWeek, writeLogPath, evidenceDir, } from "./evidence-write-log.js";
|
|
5
|
+
import { appendUnsupportedWriteEvent, appendWriteEvent, counterLogPath, readWriteCounter, readWriteLog, rotateEventLog, summarizeLastWeek, writeLogPath, evidenceDir, } from "./evidence-write-log.js";
|
|
6
6
|
let workspace;
|
|
7
7
|
beforeEach(() => {
|
|
8
8
|
workspace = mkdtempSync(join(tmpdir(), "evidence-log-"));
|
|
@@ -88,9 +88,13 @@ describe("rotateEventLog", () => {
|
|
|
88
88
|
expect(remaining).toContain("new.md");
|
|
89
89
|
expect(remaining).not.toContain("old.md");
|
|
90
90
|
});
|
|
91
|
-
it("returns zero counts when no log
|
|
91
|
+
it("returns zero counts when no log files exist", () => {
|
|
92
92
|
const result = rotateEventLog(workspace);
|
|
93
|
-
expect(result).
|
|
93
|
+
expect(result.entriesBefore).toBe(0);
|
|
94
|
+
expect(result.entriesAfter).toBe(0);
|
|
95
|
+
expect(result.bytesAfter).toBe(0);
|
|
96
|
+
expect(result.unsupported).toEqual({ entriesBefore: 0, entriesAfter: 0, bytesAfter: 0 });
|
|
97
|
+
expect(result.counter).toEqual({ entriesBefore: 0, entriesAfter: 0, bytesAfter: 0 });
|
|
94
98
|
});
|
|
95
99
|
it("drops oldest entries when total exceeds size cap", () => {
|
|
96
100
|
// Synthetic: write a 12 MB log — well over the 10 MB cap.
|
|
@@ -121,7 +125,7 @@ describe("rotateEventLog", () => {
|
|
|
121
125
|
});
|
|
122
126
|
});
|
|
123
127
|
describe("summarizeLastWeek", () => {
|
|
124
|
-
it("counts only
|
|
128
|
+
it("counts unsupported transitions only within the past 7 days", () => {
|
|
125
129
|
const now = new Date("2026-05-06T00:00:00.000Z");
|
|
126
130
|
appendUnsupportedWriteEvent(workspace, {
|
|
127
131
|
page: "in.md", timestamp: "2026-05-04T00:00:00.000Z",
|
|
@@ -131,10 +135,137 @@ describe("summarizeLastWeek", () => {
|
|
|
131
135
|
page: "out.md", timestamp: "2026-04-20T00:00:00.000Z",
|
|
132
136
|
hadSynthesisFlag: false, rawSourcesCount: 0,
|
|
133
137
|
});
|
|
134
|
-
expect(summarizeLastWeek(workspace, now).
|
|
138
|
+
expect(summarizeLastWeek(workspace, now).unsupportedTransitions).toBe(1);
|
|
135
139
|
});
|
|
136
|
-
it("returns zero when no log
|
|
137
|
-
expect(summarizeLastWeek(workspace)
|
|
140
|
+
it("returns zero counts and null ratio when no log files exist", () => {
|
|
141
|
+
expect(summarizeLastWeek(workspace)).toEqual({
|
|
142
|
+
unsupportedTransitions: 0,
|
|
143
|
+
unsupportedWrites: 0,
|
|
144
|
+
rejectedWrites: 0,
|
|
145
|
+
totalWrites: 0,
|
|
146
|
+
wouldRejectRatio: null,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
it("computes ratio as (unsupported + rejected) / total (mode-invariant)", () => {
|
|
150
|
+
const now = new Date("2026-05-06T00:00:00.000Z");
|
|
151
|
+
// Counter: 5 total — 2 grounded, 1 synthesis, 1 unsupported, 1 rejected.
|
|
152
|
+
// Numerator (would-reject) = unsupported + rejected = 2.
|
|
153
|
+
appendWriteEvent(workspace, "grounded", "2026-05-04T00:00:00.000Z");
|
|
154
|
+
appendWriteEvent(workspace, "grounded", "2026-05-04T00:01:00.000Z");
|
|
155
|
+
appendWriteEvent(workspace, "synthesis", "2026-05-05T00:00:00.000Z");
|
|
156
|
+
appendWriteEvent(workspace, "unsupported", "2026-05-04T00:00:00.000Z");
|
|
157
|
+
appendWriteEvent(workspace, "rejected", "2026-05-05T01:00:00.000Z");
|
|
158
|
+
const result = summarizeLastWeek(workspace, now);
|
|
159
|
+
expect(result.unsupportedWrites).toBe(1);
|
|
160
|
+
expect(result.rejectedWrites).toBe(1);
|
|
161
|
+
expect(result.totalWrites).toBe(5);
|
|
162
|
+
expect(result.wouldRejectRatio).toBeCloseTo(0.4);
|
|
163
|
+
});
|
|
164
|
+
it("ratio stays meaningful in reject mode (rejectedWrites carries the signal)", () => {
|
|
165
|
+
const now = new Date("2026-05-06T00:00:00.000Z");
|
|
166
|
+
// Phase 2b enabled — wiki.write never produces kind="unsupported";
|
|
167
|
+
// the equivalent writes show up as kind="rejected" instead.
|
|
168
|
+
appendWriteEvent(workspace, "grounded", "2026-05-04T00:00:00.000Z");
|
|
169
|
+
appendWriteEvent(workspace, "rejected", "2026-05-04T01:00:00.000Z");
|
|
170
|
+
appendWriteEvent(workspace, "rejected", "2026-05-04T02:00:00.000Z");
|
|
171
|
+
const result = summarizeLastWeek(workspace, now);
|
|
172
|
+
expect(result.unsupportedWrites).toBe(0);
|
|
173
|
+
expect(result.rejectedWrites).toBe(2);
|
|
174
|
+
expect(result.totalWrites).toBe(3);
|
|
175
|
+
// 2/3 ≈ 0.667 — the ratio still reflects "fraction of writes 2b blocks",
|
|
176
|
+
// even though all unsupported attempts went through the rejected path.
|
|
177
|
+
expect(result.wouldRejectRatio).toBeCloseTo(2 / 3);
|
|
178
|
+
});
|
|
179
|
+
it("reports transitions and unsupportedWrites independently when re-edits inflate the latter", () => {
|
|
180
|
+
const now = new Date("2026-05-06T00:00:00.000Z");
|
|
181
|
+
// 1 transition (page first becomes unsupported), 3 unsupported writes
|
|
182
|
+
// (the transition + 2 re-edits while still unsupported), plus 1 grounded
|
|
183
|
+
// — 4 total writes, 3 of which would be rejected by 2b.
|
|
184
|
+
appendUnsupportedWriteEvent(workspace, {
|
|
185
|
+
page: "p.md", timestamp: "2026-05-04T00:00:00.000Z",
|
|
186
|
+
hadSynthesisFlag: false, rawSourcesCount: 0,
|
|
187
|
+
});
|
|
188
|
+
appendWriteEvent(workspace, "unsupported", "2026-05-04T00:00:00.000Z");
|
|
189
|
+
appendWriteEvent(workspace, "unsupported", "2026-05-04T01:00:00.000Z");
|
|
190
|
+
appendWriteEvent(workspace, "unsupported", "2026-05-04T02:00:00.000Z");
|
|
191
|
+
appendWriteEvent(workspace, "grounded", "2026-05-05T00:00:00.000Z");
|
|
192
|
+
const result = summarizeLastWeek(workspace, now);
|
|
193
|
+
expect(result.unsupportedTransitions).toBe(1);
|
|
194
|
+
expect(result.unsupportedWrites).toBe(3);
|
|
195
|
+
expect(result.totalWrites).toBe(4);
|
|
196
|
+
expect(result.wouldRejectRatio).toBeCloseTo(0.75);
|
|
197
|
+
});
|
|
198
|
+
it("excludes counter events outside the 7-day window from the denominator", () => {
|
|
199
|
+
const now = new Date("2026-05-06T00:00:00.000Z");
|
|
200
|
+
appendWriteEvent(workspace, "grounded", "2026-05-04T00:00:00.000Z"); // in
|
|
201
|
+
appendWriteEvent(workspace, "grounded", "2026-04-20T00:00:00.000Z"); // out of 7d (still in 30d)
|
|
202
|
+
const result = summarizeLastWeek(workspace, now);
|
|
203
|
+
expect(result.totalWrites).toBe(1);
|
|
204
|
+
expect(result.wouldRejectRatio).toBe(0);
|
|
205
|
+
});
|
|
206
|
+
it("excludes rejected entries from unsupportedTransitions (Phase 2b log noise)", () => {
|
|
207
|
+
const now = new Date("2026-05-06T00:00:00.000Z");
|
|
208
|
+
// The unsupported log carries TWO event shapes: warn-mode transitions
|
|
209
|
+
// (no `rejected` flag) and reject-mode attempts (`rejected: true`,
|
|
210
|
+
// emitted once per retry). Only the former count as transitions —
|
|
211
|
+
// otherwise a retry loop in reject mode would balloon the count.
|
|
212
|
+
appendUnsupportedWriteEvent(workspace, {
|
|
213
|
+
page: "real-transition.md", timestamp: "2026-05-04T00:00:00.000Z",
|
|
214
|
+
hadSynthesisFlag: false, rawSourcesCount: 0,
|
|
215
|
+
});
|
|
216
|
+
appendUnsupportedWriteEvent(workspace, {
|
|
217
|
+
page: "blocked.md", timestamp: "2026-05-04T01:00:00.000Z",
|
|
218
|
+
hadSynthesisFlag: false, rawSourcesCount: 0,
|
|
219
|
+
rejected: true, rejectReason: "fresh",
|
|
220
|
+
});
|
|
221
|
+
appendUnsupportedWriteEvent(workspace, {
|
|
222
|
+
page: "blocked.md", timestamp: "2026-05-04T01:01:00.000Z",
|
|
223
|
+
hadSynthesisFlag: false, rawSourcesCount: 0,
|
|
224
|
+
rejected: true, rejectReason: "fresh",
|
|
225
|
+
});
|
|
226
|
+
const result = summarizeLastWeek(workspace, now);
|
|
227
|
+
expect(result.unsupportedTransitions).toBe(1);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
describe("appendWriteEvent / readWriteCounter", () => {
|
|
231
|
+
it("appends classified writes to the counter file", () => {
|
|
232
|
+
appendWriteEvent(workspace, "grounded", "2026-05-06T10:00:00.000Z");
|
|
233
|
+
appendWriteEvent(workspace, "synthesis", "2026-05-06T10:01:00.000Z");
|
|
234
|
+
appendWriteEvent(workspace, "unsupported", "2026-05-06T10:02:00.000Z");
|
|
235
|
+
appendWriteEvent(workspace, "rejected", "2026-05-06T10:03:00.000Z");
|
|
236
|
+
appendWriteEvent(workspace, "legacy", "2026-05-06T10:04:00.000Z");
|
|
237
|
+
const events = readWriteCounter(workspace, new Date("2026-05-07T00:00:00.000Z"));
|
|
238
|
+
expect(events.map((e) => e.kind)).toEqual([
|
|
239
|
+
"grounded", "synthesis", "unsupported", "rejected", "legacy",
|
|
240
|
+
]);
|
|
241
|
+
});
|
|
242
|
+
it("filters counter entries older than 30 days on read", () => {
|
|
243
|
+
appendWriteEvent(workspace, "grounded", "2026-01-01T00:00:00.000Z");
|
|
244
|
+
appendWriteEvent(workspace, "grounded", "2026-05-05T00:00:00.000Z");
|
|
245
|
+
const events = readWriteCounter(workspace, new Date("2026-05-06T00:00:00.000Z"));
|
|
246
|
+
expect(events).toHaveLength(1);
|
|
247
|
+
});
|
|
248
|
+
it("does not throw when the counter directory is unwritable", () => {
|
|
249
|
+
const nonexistent = join(workspace, "blocked", "path");
|
|
250
|
+
writeFileSync(join(workspace, "blocked"), "not a dir");
|
|
251
|
+
expect(() => appendWriteEvent(nonexistent, "grounded", "2026-05-06T10:00:00.000Z")).not.toThrow();
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
describe("rotateEventLog (counter file)", () => {
|
|
255
|
+
it("rotates the counter file alongside the unsupported log", () => {
|
|
256
|
+
appendWriteEvent(workspace, "grounded", "2025-01-01T00:00:00.000Z"); // old
|
|
257
|
+
appendWriteEvent(workspace, "grounded", "2026-05-05T00:00:00.000Z"); // new
|
|
258
|
+
appendUnsupportedWriteEvent(workspace, {
|
|
259
|
+
page: "u.md", timestamp: "2026-05-05T00:00:00.000Z",
|
|
260
|
+
hadSynthesisFlag: false, rawSourcesCount: 0,
|
|
261
|
+
});
|
|
262
|
+
const result = rotateEventLog(workspace, new Date("2026-05-06T00:00:00.000Z"));
|
|
263
|
+
expect(result.counter.entriesBefore).toBe(2);
|
|
264
|
+
expect(result.counter.entriesAfter).toBe(1);
|
|
265
|
+
expect(result.unsupported.entriesAfter).toBe(1);
|
|
266
|
+
const remaining = readFileSync(counterLogPath(workspace), "utf-8");
|
|
267
|
+
expect(remaining).not.toContain("2025-01-01");
|
|
268
|
+
expect(remaining).toContain("2026-05-05");
|
|
138
269
|
});
|
|
139
270
|
});
|
|
140
271
|
//# sourceMappingURL=evidence-write-log.test.js.map
|