agentfootprint 19.0.0 → 19.1.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/CAPABILITIES.md +2 -0
- package/dist/answer-account/facts/calls.js +26 -7
- package/dist/answer-account/facts/calls.js.map +1 -1
- package/dist/core/agent/assessment/gather/stage.js +1 -1
- package/dist/core/agent/assessment/gather/stage.js.map +1 -1
- package/dist/core/agent/assessment/periodRows.js +7 -11
- package/dist/core/agent/assessment/periodRows.js.map +1 -1
- package/dist/core/agent/assessment/reasonClasses.js +28 -1
- package/dist/core/agent/assessment/reasonClasses.js.map +1 -1
- package/dist/core/agent/assessment/resultEvidence.js +8 -5
- package/dist/core/agent/assessment/resultEvidence.js.map +1 -1
- package/dist/core/agent/assessment/results/coverage/declarations.js +28 -0
- package/dist/core/agent/assessment/results/coverage/declarations.js.map +1 -0
- package/dist/core/agent/assessment/results/stage.js +1 -1
- package/dist/core/agent/assessment/results/stage.js.map +1 -1
- package/dist/core/agent/assessment/settledResults.js +4 -2
- package/dist/core/agent/assessment/settledResults.js.map +1 -1
- package/dist/core/agent/assessment/standingAdmission.js +14 -2
- package/dist/core/agent/assessment/standingAdmission.js.map +1 -1
- package/dist/core/agent/emptiness.js +6 -1
- package/dist/core/agent/emptiness.js.map +1 -1
- package/dist/core/agent/recordedCalls/resultDispositions.js +9 -1
- package/dist/core/agent/recordedCalls/resultDispositions.js.map +1 -1
- package/dist/core/agent/recordedCalls/resultRewriteIndex.js +40 -0
- package/dist/core/agent/recordedCalls/resultRewriteIndex.js.map +1 -0
- package/dist/core/agent/recordedCalls.js +3 -1
- package/dist/core/agent/recordedCalls.js.map +1 -1
- package/dist/esm/answer-account/facts/calls.js +27 -8
- package/dist/esm/answer-account/facts/calls.js.map +1 -1
- package/dist/esm/answer-account/types.d.ts +6 -0
- package/dist/esm/core/agent/assessment/gather/stage.js +1 -1
- package/dist/esm/core/agent/assessment/gather/stage.js.map +1 -1
- package/dist/esm/core/agent/assessment/periodRows.d.ts +1 -3
- package/dist/esm/core/agent/assessment/periodRows.js +4 -9
- package/dist/esm/core/agent/assessment/periodRows.js.map +1 -1
- package/dist/esm/core/agent/assessment/reasonClasses.d.ts +9 -0
- package/dist/esm/core/agent/assessment/reasonClasses.js +25 -0
- package/dist/esm/core/agent/assessment/reasonClasses.js.map +1 -1
- package/dist/esm/core/agent/assessment/resultEvidence.d.ts +1 -0
- package/dist/esm/core/agent/assessment/resultEvidence.js +9 -6
- package/dist/esm/core/agent/assessment/resultEvidence.js.map +1 -1
- package/dist/esm/core/agent/assessment/results/coverage/declarations.d.ts +16 -0
- package/dist/esm/core/agent/assessment/results/coverage/declarations.js +22 -0
- package/dist/esm/core/agent/assessment/results/coverage/declarations.js.map +1 -0
- package/dist/esm/core/agent/assessment/results/stage.js +1 -1
- package/dist/esm/core/agent/assessment/results/stage.js.map +1 -1
- package/dist/esm/core/agent/assessment/settledResults.js +4 -2
- package/dist/esm/core/agent/assessment/settledResults.js.map +1 -1
- package/dist/esm/core/agent/assessment/standingAdmission.d.ts +1 -0
- package/dist/esm/core/agent/assessment/standingAdmission.js +15 -3
- package/dist/esm/core/agent/assessment/standingAdmission.js.map +1 -1
- package/dist/esm/core/agent/emptiness.d.ts +2 -0
- package/dist/esm/core/agent/emptiness.js +4 -0
- package/dist/esm/core/agent/emptiness.js.map +1 -1
- package/dist/esm/core/agent/recordedCalls/resultDispositions.d.ts +2 -0
- package/dist/esm/core/agent/recordedCalls/resultDispositions.js +7 -0
- package/dist/esm/core/agent/recordedCalls/resultDispositions.js.map +1 -1
- package/dist/esm/core/agent/recordedCalls/resultRewriteIndex.d.ts +11 -0
- package/dist/esm/core/agent/recordedCalls/resultRewriteIndex.js +36 -0
- package/dist/esm/core/agent/recordedCalls/resultRewriteIndex.js.map +1 -0
- package/dist/esm/core/agent/recordedCalls.d.ts +1 -0
- package/dist/esm/core/agent/recordedCalls.js +1 -0
- package/dist/esm/core/agent/recordedCalls.js.map +1 -1
- package/dist/esm/llm-ledger/build.d.ts +77 -0
- package/dist/esm/llm-ledger/build.js +344 -0
- package/dist/esm/llm-ledger/build.js.map +1 -0
- package/dist/esm/llm-ledger/digest.d.ts +24 -0
- package/dist/esm/llm-ledger/digest.js +22 -0
- package/dist/esm/llm-ledger/digest.js.map +1 -0
- package/dist/esm/llm-ledger/entries.d.ts +37 -0
- package/dist/esm/llm-ledger/entries.js +291 -0
- package/dist/esm/llm-ledger/entries.js.map +1 -0
- package/dist/esm/llm-ledger/fileStore.d.ts +12 -0
- package/dist/esm/llm-ledger/fileStore.js +571 -0
- package/dist/esm/llm-ledger/fileStore.js.map +1 -0
- package/dist/esm/llm-ledger/limits.d.ts +65 -0
- package/dist/esm/llm-ledger/limits.js +64 -0
- package/dist/esm/llm-ledger/limits.js.map +1 -0
- package/dist/esm/llm-ledger/memoryStore.d.ts +3 -0
- package/dist/esm/llm-ledger/memoryStore.js +44 -0
- package/dist/esm/llm-ledger/memoryStore.js.map +1 -0
- package/dist/esm/llm-ledger/session.d.ts +6 -0
- package/dist/esm/llm-ledger/session.js +43 -0
- package/dist/esm/llm-ledger/session.js.map +1 -0
- package/dist/esm/llm-ledger/store.d.ts +97 -0
- package/dist/esm/llm-ledger/store.js +208 -0
- package/dist/esm/llm-ledger/store.js.map +1 -0
- package/dist/esm/llm-ledger/tokens.d.ts +30 -0
- package/dist/esm/llm-ledger/tokens.js +319 -0
- package/dist/esm/llm-ledger/tokens.js.map +1 -0
- package/dist/esm/llm-ledger/types.d.ts +173 -0
- package/dist/esm/llm-ledger/types.js +2 -0
- package/dist/esm/llm-ledger/types.js.map +1 -0
- package/dist/esm/llm-ledger/window.d.ts +40 -0
- package/dist/esm/llm-ledger/window.js +66 -0
- package/dist/esm/llm-ledger/window.js.map +1 -0
- package/dist/esm/trace-toolpack/traceToolpack/resultRewrite.d.ts +3 -0
- package/dist/esm/trace-toolpack/traceToolpack/resultRewrite.js +14 -0
- package/dist/esm/trace-toolpack/traceToolpack/resultRewrite.js.map +1 -0
- package/dist/esm/trace-toolpack/traceToolpack/toolCallJournal.d.ts +6 -5
- package/dist/esm/trace-toolpack/traceToolpack/toolCallJournal.js +12 -17
- package/dist/esm/trace-toolpack/traceToolpack/toolCallJournal.js.map +1 -1
- package/dist/esm/trace-toolpack/traceToolpack/toolCallOccurrence.d.ts +7 -0
- package/dist/esm/trace-toolpack/traceToolpack/toolCallOccurrence.js +19 -0
- package/dist/esm/trace-toolpack/traceToolpack/toolCallOccurrence.js.map +1 -1
- package/dist/llm-ledger/build.js +354 -0
- package/dist/llm-ledger/build.js.map +1 -0
- package/dist/llm-ledger/digest.js +28 -0
- package/dist/llm-ledger/digest.js.map +1 -0
- package/dist/llm-ledger/entries.js +308 -0
- package/dist/llm-ledger/entries.js.map +1 -0
- package/dist/llm-ledger/fileStore.js +575 -0
- package/dist/llm-ledger/fileStore.js.map +1 -0
- package/dist/llm-ledger/limits.js +67 -0
- package/dist/llm-ledger/limits.js.map +1 -0
- package/dist/llm-ledger/memoryStore.js +48 -0
- package/dist/llm-ledger/memoryStore.js.map +1 -0
- package/dist/llm-ledger/session.js +49 -0
- package/dist/llm-ledger/session.js.map +1 -0
- package/dist/llm-ledger/store.js +220 -0
- package/dist/llm-ledger/store.js.map +1 -0
- package/dist/llm-ledger/tokens.js +327 -0
- package/dist/llm-ledger/tokens.js.map +1 -0
- package/dist/llm-ledger/types.js +3 -0
- package/dist/llm-ledger/types.js.map +1 -0
- package/dist/llm-ledger/window.js +72 -0
- package/dist/llm-ledger/window.js.map +1 -0
- package/dist/trace-toolpack/traceToolpack/resultRewrite.js +18 -0
- package/dist/trace-toolpack/traceToolpack/resultRewrite.js.map +1 -0
- package/dist/trace-toolpack/traceToolpack/toolCallJournal.js +14 -19
- package/dist/trace-toolpack/traceToolpack/toolCallJournal.js.map +1 -1
- package/dist/trace-toolpack/traceToolpack/toolCallOccurrence.js +22 -1
- package/dist/trace-toolpack/traceToolpack/toolCallOccurrence.js.map +1 -1
- package/dist/types/answer-account/facts/calls.d.ts.map +1 -1
- package/dist/types/answer-account/types.d.ts +6 -0
- package/dist/types/answer-account/types.d.ts.map +1 -1
- package/dist/types/core/agent/assessment/periodRows.d.ts +1 -3
- package/dist/types/core/agent/assessment/periodRows.d.ts.map +1 -1
- package/dist/types/core/agent/assessment/reasonClasses.d.ts +9 -0
- package/dist/types/core/agent/assessment/reasonClasses.d.ts.map +1 -1
- package/dist/types/core/agent/assessment/resultEvidence.d.ts +1 -0
- package/dist/types/core/agent/assessment/resultEvidence.d.ts.map +1 -1
- package/dist/types/core/agent/assessment/results/coverage/declarations.d.ts +17 -0
- package/dist/types/core/agent/assessment/results/coverage/declarations.d.ts.map +1 -0
- package/dist/types/core/agent/assessment/settledResults.d.ts.map +1 -1
- package/dist/types/core/agent/assessment/standingAdmission.d.ts +1 -0
- package/dist/types/core/agent/assessment/standingAdmission.d.ts.map +1 -1
- package/dist/types/core/agent/emptiness.d.ts +2 -0
- package/dist/types/core/agent/emptiness.d.ts.map +1 -1
- package/dist/types/core/agent/recordedCalls/resultDispositions.d.ts +2 -0
- package/dist/types/core/agent/recordedCalls/resultDispositions.d.ts.map +1 -1
- package/dist/types/core/agent/recordedCalls/resultRewriteIndex.d.ts +12 -0
- package/dist/types/core/agent/recordedCalls/resultRewriteIndex.d.ts.map +1 -0
- package/dist/types/core/agent/recordedCalls.d.ts +1 -0
- package/dist/types/core/agent/recordedCalls.d.ts.map +1 -1
- package/dist/types/llm-ledger/build.d.ts +78 -0
- package/dist/types/llm-ledger/build.d.ts.map +1 -0
- package/dist/types/llm-ledger/digest.d.ts +25 -0
- package/dist/types/llm-ledger/digest.d.ts.map +1 -0
- package/dist/types/llm-ledger/entries.d.ts +38 -0
- package/dist/types/llm-ledger/entries.d.ts.map +1 -0
- package/dist/types/llm-ledger/fileStore.d.ts +13 -0
- package/dist/types/llm-ledger/fileStore.d.ts.map +1 -0
- package/dist/types/llm-ledger/limits.d.ts +66 -0
- package/dist/types/llm-ledger/limits.d.ts.map +1 -0
- package/dist/types/llm-ledger/memoryStore.d.ts +4 -0
- package/dist/types/llm-ledger/memoryStore.d.ts.map +1 -0
- package/dist/types/llm-ledger/session.d.ts +7 -0
- package/dist/types/llm-ledger/session.d.ts.map +1 -0
- package/dist/types/llm-ledger/store.d.ts +98 -0
- package/dist/types/llm-ledger/store.d.ts.map +1 -0
- package/dist/types/llm-ledger/tokens.d.ts +31 -0
- package/dist/types/llm-ledger/tokens.d.ts.map +1 -0
- package/dist/types/llm-ledger/types.d.ts +174 -0
- package/dist/types/llm-ledger/types.d.ts.map +1 -0
- package/dist/types/llm-ledger/window.d.ts +41 -0
- package/dist/types/llm-ledger/window.d.ts.map +1 -0
- package/dist/types/trace-toolpack/traceToolpack/resultRewrite.d.ts +4 -0
- package/dist/types/trace-toolpack/traceToolpack/resultRewrite.d.ts.map +1 -0
- package/dist/types/trace-toolpack/traceToolpack/toolCallJournal.d.ts +6 -5
- package/dist/types/trace-toolpack/traceToolpack/toolCallJournal.d.ts.map +1 -1
- package/dist/types/trace-toolpack/traceToolpack/toolCallOccurrence.d.ts +7 -0
- package/dist/types/trace-toolpack/traceToolpack/toolCallOccurrence.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* llm-ledger/tokens — which tokens of a text are VALUES, read the same way on every side.
|
|
3
|
+
*
|
|
4
|
+
* Pattern: one pure reader, used for what a read returned (a receipt), what
|
|
5
|
+
* was supplied, and — in the rules — what a note or a draft states.
|
|
6
|
+
* One spelling on every side, so two sides can never disagree about
|
|
7
|
+
* what "the same value" means.
|
|
8
|
+
* Role: the closed leaf's own copy of rules 1–3 in the header of
|
|
9
|
+
* `core/agent/evidence/extract.ts` (this folder cannot import it). A
|
|
10
|
+
* differential test against that extractor is added when the plugin
|
|
11
|
+
* adapter starts.
|
|
12
|
+
* Emits: N/A.
|
|
13
|
+
*
|
|
14
|
+
* ## A token is a piece of the text
|
|
15
|
+
*
|
|
16
|
+
* A text is split into PIECES: runs of the characters `A–Z a–z 0–9 : _ - / . % + @ # $`. Every
|
|
17
|
+
* other character separates — a backslash too — and so does every comma. A leading `$ # @ + :`
|
|
18
|
+
* and a trailing `. : %` are decoration, so `{"total":1200}` gives `1200`. Nothing is folded,
|
|
19
|
+
* joined, decoded or respelled but for lower case: every token is a whole piece of the lower-cased
|
|
20
|
+
* text, whatever the text is — prose, CSV, JSON, JSON Lines, a path, or any part of one — and no
|
|
21
|
+
* text is recognised as one kind first.
|
|
22
|
+
*
|
|
23
|
+
* ## A piece that reads two ways is counted, never read
|
|
24
|
+
*
|
|
25
|
+
* After a backslash, a piece that opens with `b f n r t u` or `/` may be a JSON escape's letter and
|
|
26
|
+
* what follows it, or a path's next name: `D:\logs\node1234.log` holds `node1234.log`, or
|
|
27
|
+
* `ode1234.log` after a newline. Neither reading is kept, and the piece is counted in
|
|
28
|
+
* `escapedPieces` when it holds a digit (a piece without one is no value either way). So is the
|
|
29
|
+
* piece right before `\/`, which JSON joins to the next one with `/`. Backslashes pair from the
|
|
30
|
+
* left: `\\` and `\"` are an escaped backslash and an escaped quote, plain separators with nothing
|
|
31
|
+
* ambiguous after them, and a piece after any other character (`\logs`) is read as written. A
|
|
32
|
+
* receipt that counted one is incomplete (`entries.ts` · `isIncomplete`).
|
|
33
|
+
*
|
|
34
|
+
* ## Which pieces are values
|
|
35
|
+
*
|
|
36
|
+
* A piece is a token only if:
|
|
37
|
+
*
|
|
38
|
+
* 1. it contains a digit (prose is alphabetic — so an all-letter name is
|
|
39
|
+
* invisible here, stated plainly);
|
|
40
|
+
* 2. it is distinctive: an IDENTIFIER of at least 4 characters (digits
|
|
41
|
+
* mixed with letters or with `:` `_` `-` `/` `.`), or a NUMBER of at
|
|
42
|
+
* least 4 digits;
|
|
43
|
+
* 3. it is not prose wearing a number: a number with a short unit glued on
|
|
44
|
+
* (`32G`, `48-port`, `41200iops`) is judged by its number alone, and a
|
|
45
|
+
* bare one-or-two-digit ratio (`24/7`) is never a token.
|
|
46
|
+
*
|
|
47
|
+
* Tokens are compared exactly: `98304.0` is not `98304`, and `1.2 s` is not `1200ms`.
|
|
48
|
+
*
|
|
49
|
+
* ## Numbers written with thousands separators are not read
|
|
50
|
+
*
|
|
51
|
+
* A run of digits and commas that matches `\d{1,3}(,\d{3})+` and is not glued to a letter before
|
|
52
|
+
* it (`41,200`, `$1,234.50`, `1,234ms`, `[100,200,300` — not `host1,234`) may be one number or a
|
|
53
|
+
* list of values, and no reading of its pieces can tell which. It is COUNTED (`groupedNumbers`),
|
|
54
|
+
* and its groups — the pieces after its first comma — are never read, so `1,234.5` gives no
|
|
55
|
+
* `234.5`. A receipt that counted one is incomplete (`entries.ts` · `isIncomplete`); in a claim,
|
|
56
|
+
* such a number is no visible token. An escape's own characters (`n` of `\n`, `u00e9` of `\u00e9`)
|
|
57
|
+
* neither start a run nor glue one. This differs on purpose from the agent's own extractor, which
|
|
58
|
+
* folds the commas out (`5,100` is `5100`) and reads one JSON value through its literals.
|
|
59
|
+
*
|
|
60
|
+
* ## The caps (`limits.ts`)
|
|
61
|
+
*
|
|
62
|
+
* `readTokens` reads at most `maxTextBytes` UTF-8 bytes. Past the cap each character is counted
|
|
63
|
+
* once (`bytes`, `cut.bytesUnread`) and never read: nothing past it is copied, split or looked at
|
|
64
|
+
* to decide anything. The part read ends at a separator: the last piece before the cap is kept
|
|
65
|
+
* only when a separator follows it within the bytes read, and a backslash right at the cap, whose
|
|
66
|
+
* meaning is the character past it, is left unread with the piece before it. It keeps the first
|
|
67
|
+
* `maxTokens` distinct tokens in first-seen order;
|
|
68
|
+
* every other distinct token — past the cap, or longer than `maxTokenChars` — is counted in
|
|
69
|
+
* `cut.tokensDropped`. A reading that kept everything has no `cut`. Nothing read is dropped
|
|
70
|
+
* uncounted.
|
|
71
|
+
*/
|
|
72
|
+
import { LEDGER_LIMITS } from './limits.js';
|
|
73
|
+
/** A run of piece characters and commas; the commas then split it. */
|
|
74
|
+
const ROUGH = /[A-Za-z0-9:_\-/.,%+@#$]+/g;
|
|
75
|
+
const GROUPED = /^\d{1,3}(?:,\d{3})+$/;
|
|
76
|
+
const LEADING = /^[$#@+:]+/;
|
|
77
|
+
const TRAILING = '.:%';
|
|
78
|
+
const RATIO = /^\d{1,2}\/\d{1,2}$/;
|
|
79
|
+
const WITH_TAIL = /^([-+]?\d+(?:\.\d+)?)[-/]?[a-z]{1,5}(?:\/[a-z]{1,5})?$/;
|
|
80
|
+
const NUMBER = /^[-+]?\d+(?:\.\d+)?$/;
|
|
81
|
+
const STRUCTURAL = /[:_\-/.]/;
|
|
82
|
+
const LETTER = /[a-z]/;
|
|
83
|
+
const DIGIT = /\d/;
|
|
84
|
+
const COMMA = 0x2c;
|
|
85
|
+
const BACKSLASH = 0x5c;
|
|
86
|
+
const SLASH = 0x2f;
|
|
87
|
+
/** What a JSON escape opens with after its backslash, where a path's next name could too. */
|
|
88
|
+
const ESCAPE_LETTERS = 'bfnrtu/';
|
|
89
|
+
const HEX4 = /^u[0-9A-Fa-f]{4}/;
|
|
90
|
+
const isDigitAt = (s, i) => {
|
|
91
|
+
const c = s.charCodeAt(i);
|
|
92
|
+
return c >= 0x30 && c <= 0x39;
|
|
93
|
+
};
|
|
94
|
+
const isLetterAt = (s, i) => {
|
|
95
|
+
const c = s.charCodeAt(i);
|
|
96
|
+
return (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a);
|
|
97
|
+
};
|
|
98
|
+
/** A comma at `i` with a digit on both sides: inside a run of digits and commas. */
|
|
99
|
+
const runCommaAt = (s, i) => s.charCodeAt(i) === COMMA && isDigitAt(s, i - 1) && isDigitAt(s, i + 1);
|
|
100
|
+
/** Whether the code unit `c` may live inside a piece (a comma never does). */
|
|
101
|
+
function inPiece(c) {
|
|
102
|
+
return ((c >= 0x30 && c <= 0x39) || // 0-9
|
|
103
|
+
(c >= 0x41 && c <= 0x5a) || // A-Z
|
|
104
|
+
(c >= 0x61 && c <= 0x7a) || // a-z
|
|
105
|
+
c === 0x3a || // :
|
|
106
|
+
c === 0x5f || // _
|
|
107
|
+
c === 0x2d || // -
|
|
108
|
+
c === 0x2f || // /
|
|
109
|
+
c === 0x2e || // .
|
|
110
|
+
c === 0x25 || // %
|
|
111
|
+
c === 0x2b || // +
|
|
112
|
+
c === 0x40 || // @
|
|
113
|
+
c === 0x23 || // #
|
|
114
|
+
c === 0x24 // $
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
/** Whether the backslash at `i` opens an escape: it is not the second of an escaped one (`\\`). */
|
|
118
|
+
function opensEscape(text, i) {
|
|
119
|
+
let run = 0;
|
|
120
|
+
while (i - run >= 0 && text.charCodeAt(i - run) === BACKSLASH)
|
|
121
|
+
run++;
|
|
122
|
+
return run % 2 === 1;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* How many of the first characters of `rough` (found at `at` in `text`) are an escape's own — `n`
|
|
126
|
+
* of `\n`, `u00e9` of `\u00e9` — when a backslash that opens an escape stands right before it and
|
|
127
|
+
* it opens with an escape's letter; else 0.
|
|
128
|
+
*/
|
|
129
|
+
function escapeLength(text, at, rough) {
|
|
130
|
+
if (at === 0 || text.charCodeAt(at - 1) !== BACKSLASH || !opensEscape(text, at - 1))
|
|
131
|
+
return 0;
|
|
132
|
+
if (!ESCAPE_LETTERS.includes(rough[0]))
|
|
133
|
+
return 0;
|
|
134
|
+
return HEX4.test(rough) ? 5 : 1;
|
|
135
|
+
}
|
|
136
|
+
/** Rules 1–3 on one piece, in the form every side compares on: the token, or `undefined`. */
|
|
137
|
+
function tokenOf(raw) {
|
|
138
|
+
const v = raw.toLowerCase().replace(LEADING, '');
|
|
139
|
+
let end = v.length;
|
|
140
|
+
while (end > 0 && TRAILING.includes(v[end - 1]))
|
|
141
|
+
end--;
|
|
142
|
+
const piece = v.slice(0, end);
|
|
143
|
+
if (!DIGIT.test(piece) || RATIO.test(piece))
|
|
144
|
+
return undefined;
|
|
145
|
+
const tail = WITH_TAIL.exec(piece);
|
|
146
|
+
const numeric = tail !== null ? tail[1] : NUMBER.test(piece) ? piece : undefined;
|
|
147
|
+
if (numeric !== undefined) {
|
|
148
|
+
let digits = 0;
|
|
149
|
+
for (let i = 0; i < numeric.length; i++)
|
|
150
|
+
if (DIGIT.test(numeric[i]))
|
|
151
|
+
digits++;
|
|
152
|
+
return digits >= 4 ? numeric : undefined;
|
|
153
|
+
}
|
|
154
|
+
if (piece.length < 4 || (!LETTER.test(piece) && !STRUCTURAL.test(piece)))
|
|
155
|
+
return undefined;
|
|
156
|
+
return piece;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* One rough run's pieces, split at every comma, each read by rules 1–3 — but for the groups of a
|
|
160
|
+
* number written with thousands separators (the pieces after its first comma), and for a piece
|
|
161
|
+
* that reads two ways: the first, after `escape` characters of an escape's own, and the last when
|
|
162
|
+
* `\/` follows it (`slashAfter`). Neither is read; both are counted in `unread`.
|
|
163
|
+
*/
|
|
164
|
+
function readRough(rough, escape, slashAfter, into, unread) {
|
|
165
|
+
// Each grouped number as [its first comma, the end of its last group's piece]. An escape's own
|
|
166
|
+
// characters neither start a run nor glue one.
|
|
167
|
+
const groups = [];
|
|
168
|
+
for (let i = escape; i < rough.length;) {
|
|
169
|
+
if (!isDigitAt(rough, i)) {
|
|
170
|
+
i++;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const start = i;
|
|
174
|
+
while (i < rough.length && (isDigitAt(rough, i) || runCommaAt(rough, i)))
|
|
175
|
+
i++;
|
|
176
|
+
if (!(start > escape && isLetterAt(rough, start - 1)) && GROUPED.test(rough.slice(start, i))) {
|
|
177
|
+
const end = rough.indexOf(',', i);
|
|
178
|
+
groups.push(rough.indexOf(',', start), end === -1 ? rough.length : end);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
unread.grouped += groups.length / 2;
|
|
182
|
+
const pieces = rough.split(',');
|
|
183
|
+
let k = 0;
|
|
184
|
+
let at = 0;
|
|
185
|
+
for (let n = 0; n < pieces.length; n++) {
|
|
186
|
+
const piece = pieces[n];
|
|
187
|
+
while (k < groups.length && groups[k + 1] <= at)
|
|
188
|
+
k += 2;
|
|
189
|
+
if ((n === 0 && escape > 0) || (n === pieces.length - 1 && slashAfter)) {
|
|
190
|
+
if (DIGIT.test(piece))
|
|
191
|
+
unread.escaped++;
|
|
192
|
+
}
|
|
193
|
+
else if (!(k < groups.length && groups[k] < at)) {
|
|
194
|
+
const token = tokenOf(piece);
|
|
195
|
+
if (token !== undefined)
|
|
196
|
+
into(token);
|
|
197
|
+
}
|
|
198
|
+
at += piece.length + 1;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Every token of `text`, duplicates kept; what it could not read is counted in `unread`. */
|
|
202
|
+
function readPieces(text, into, unread) {
|
|
203
|
+
for (const m of text.matchAll(ROUGH)) {
|
|
204
|
+
const end = m.index + m[0].length;
|
|
205
|
+
const slashAfter = text.charCodeAt(end) === BACKSLASH && text.charCodeAt(end + 1) === SLASH;
|
|
206
|
+
readRough(m[0], escapeLength(text, m.index, m[0]), slashAfter, into, unread);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** Every distinct token of `text`, in first-seen order, with no cap. */
|
|
210
|
+
export function tokensOf(text) {
|
|
211
|
+
const seen = new Set();
|
|
212
|
+
readPieces(text, (token) => void seen.add(token), { grouped: 0, escaped: 0 });
|
|
213
|
+
return [...seen];
|
|
214
|
+
}
|
|
215
|
+
/** Whether `value` is exactly one token, spelled as `tokensOf` writes it. */
|
|
216
|
+
export function isToken(value) {
|
|
217
|
+
if (typeof value !== 'string')
|
|
218
|
+
return false;
|
|
219
|
+
if (value.length < LEDGER_LIMITS.minTokenChars || value.length > LEDGER_LIMITS.maxTokenChars)
|
|
220
|
+
return false;
|
|
221
|
+
const read = tokensOf(value);
|
|
222
|
+
return read.length === 1 && read[0] === value;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* UTF-8 bytes of the character starting at code unit `i`, as an encoder
|
|
226
|
+
* writes it: a surrogate pair is 4 bytes over 2 units, a lone surrogate 3
|
|
227
|
+
* (U+FFFD). Never more than one character is read.
|
|
228
|
+
*/
|
|
229
|
+
function bytesAt(text, i) {
|
|
230
|
+
const c = text.charCodeAt(i);
|
|
231
|
+
if (c < 0x80)
|
|
232
|
+
return 1;
|
|
233
|
+
if (c < 0x800)
|
|
234
|
+
return 2;
|
|
235
|
+
if (c >= 0xd800 && c <= 0xdbff) {
|
|
236
|
+
const next = text.charCodeAt(i + 1);
|
|
237
|
+
if (next >= 0xdc00 && next <= 0xdfff)
|
|
238
|
+
return 4;
|
|
239
|
+
}
|
|
240
|
+
return 3;
|
|
241
|
+
}
|
|
242
|
+
/** UTF-8 bytes of `text`, counted without encoding it. */
|
|
243
|
+
export function utf8Bytes(text) {
|
|
244
|
+
let bytes = 0;
|
|
245
|
+
for (let i = 0; i < text.length;) {
|
|
246
|
+
const step = bytesAt(text, i);
|
|
247
|
+
bytes += step;
|
|
248
|
+
i += step === 4 ? 2 : 1;
|
|
249
|
+
}
|
|
250
|
+
return bytes;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* The tokens a receipt keeps of the host's `text`, and what it did not keep.
|
|
254
|
+
* The text itself is never kept: only its byte count and its capped tokens.
|
|
255
|
+
*/
|
|
256
|
+
export function readTokens(text) {
|
|
257
|
+
return readTokensUpTo(text, LEDGER_LIMITS.maxTextBytes);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* `readTokens` under another byte cap, `maxBytes` (a whole number). The
|
|
261
|
+
* receipt's cap is `maxTextBytes`; a smaller one reads less, by the same law.
|
|
262
|
+
*/
|
|
263
|
+
export function readTokensUpTo(text, maxBytes) {
|
|
264
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0)
|
|
265
|
+
throw new RangeError('readTokensUpTo: maxBytes must be a whole number');
|
|
266
|
+
// One pass counts every character's bytes and finds where the cap falls; nothing past it is read.
|
|
267
|
+
let readUnits = 0;
|
|
268
|
+
let readBytes = 0;
|
|
269
|
+
let bytes = 0;
|
|
270
|
+
for (let i = 0; i < text.length;) {
|
|
271
|
+
const step = bytesAt(text, i);
|
|
272
|
+
const units = step === 4 ? 2 : 1;
|
|
273
|
+
bytes += step;
|
|
274
|
+
if (bytes <= maxBytes) {
|
|
275
|
+
readUnits = i + units;
|
|
276
|
+
readBytes = bytes;
|
|
277
|
+
}
|
|
278
|
+
i += units;
|
|
279
|
+
}
|
|
280
|
+
let read = text.slice(0, readUnits);
|
|
281
|
+
// A cut text's part read ends at a separator: a piece the cap cut is not read, nor a backslash
|
|
282
|
+
// that opens an escape at the end (its meaning is the character after it) with the piece before.
|
|
283
|
+
if (readUnits < text.length) {
|
|
284
|
+
let end = read.length;
|
|
285
|
+
for (;;) {
|
|
286
|
+
while (end > 0 && inPiece(read.charCodeAt(end - 1)))
|
|
287
|
+
end--;
|
|
288
|
+
if (!(end > 0 && read.charCodeAt(end - 1) === BACKSLASH && opensEscape(read, end - 1)))
|
|
289
|
+
break;
|
|
290
|
+
end--;
|
|
291
|
+
}
|
|
292
|
+
readBytes -= read.length - end; // piece characters and backslashes are ASCII: one byte
|
|
293
|
+
read = read.slice(0, end);
|
|
294
|
+
}
|
|
295
|
+
const seen = new Set();
|
|
296
|
+
const unread = { grouped: 0, escaped: 0 };
|
|
297
|
+
readPieces(read, (token) => void seen.add(token), unread);
|
|
298
|
+
const all = [...seen];
|
|
299
|
+
const tokens = [];
|
|
300
|
+
for (const token of all) {
|
|
301
|
+
if (tokens.length === LEDGER_LIMITS.maxTokens)
|
|
302
|
+
break;
|
|
303
|
+
if (token.length <= LEDGER_LIMITS.maxTokenChars)
|
|
304
|
+
tokens.push(token);
|
|
305
|
+
}
|
|
306
|
+
const tokensDropped = all.length - tokens.length;
|
|
307
|
+
const bytesUnread = bytes - readBytes;
|
|
308
|
+
Object.freeze(tokens);
|
|
309
|
+
return Object.freeze({
|
|
310
|
+
tokens,
|
|
311
|
+
bytes,
|
|
312
|
+
...(tokensDropped > 0 || bytesUnread > 0
|
|
313
|
+
? { cut: Object.freeze({ tokensDropped, bytesUnread }) }
|
|
314
|
+
: {}),
|
|
315
|
+
...(unread.grouped > 0 ? { groupedNumbers: unread.grouped } : {}),
|
|
316
|
+
...(unread.escaped > 0 ? { escapedPieces: unread.escaped } : {}),
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../../src/llm-ledger/tokens.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAc5C,sEAAsE;AACtE,MAAM,KAAK,GAAG,2BAA2B,CAAC;AAC1C,MAAM,OAAO,GAAG,sBAAsB,CAAC;AACvC,MAAM,OAAO,GAAG,WAAW,CAAC;AAC5B,MAAM,QAAQ,GAAG,KAAK,CAAC;AACvB,MAAM,KAAK,GAAG,oBAAoB,CAAC;AACnC,MAAM,SAAS,GAAG,wDAAwD,CAAC;AAC3E,MAAM,MAAM,GAAG,sBAAsB,CAAC;AACtC,MAAM,UAAU,GAAG,UAAU,CAAC;AAC9B,MAAM,MAAM,GAAG,OAAO,CAAC;AACvB,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,6FAA6F;AAC7F,MAAM,cAAc,GAAG,SAAS,CAAC;AACjC,MAAM,IAAI,GAAG,kBAAkB,CAAC;AAQhC,MAAM,SAAS,GAAG,CAAC,CAAS,EAAE,CAAS,EAAW,EAAE;IAClD,MAAM,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CA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AAC;IACtE,CAAC;IACD,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACjD,MAAM,WAAW,GAAG,KAAK,GAAG,SAAS,CAAC;IACtC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,MAAM;QACN,KAAK;QACL,GAAG,CAAC,aAAa,GAAG,CAAC,IAAI,WAAW,GAAG,CAAC;YACtC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,aAAa,EAAE,WAAW,EAAE,CAAC,EAAE;YACxD,CAAC,CAAC,EAAE,CAAC;QACP,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjE,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACjE,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* llm-ledger/types — the entry shapes and the ledger's closed words.
|
|
3
|
+
*
|
|
4
|
+
* Pattern: declarations only. Every entry kind is one interface over one
|
|
5
|
+
* shared base; `entries.ts` is the one check that a value has one of
|
|
6
|
+
* these shapes, and `build.ts` holds the builders that make them.
|
|
7
|
+
* Role: the closed leaf's vocabulary. Every exported name carries the
|
|
8
|
+
* `Ledger` prefix, because `receipt` already names what the model was
|
|
9
|
+
* handed elsewhere in this package.
|
|
10
|
+
* Emits: N/A.
|
|
11
|
+
*
|
|
12
|
+
* All seq numbers are the entry numbers of ONE session, 1…n, shared by every
|
|
13
|
+
* kind. A reference to another entry (a cite, a claim, `submit`, `note`,
|
|
14
|
+
* `restsOn`) is always to an EARLIER entry.
|
|
15
|
+
*/
|
|
16
|
+
import type { JsonValue } from 'foottrace/canonical-json';
|
|
17
|
+
/** Who wrote an entry. */
|
|
18
|
+
export type LedgerBy = 'host' | 'agent' | 'ledger' | 'person';
|
|
19
|
+
/** Whether the ledger saw it happen (`observed`) or was told (`reported`). */
|
|
20
|
+
export type LedgerHow = 'observed' | 'reported';
|
|
21
|
+
/** The nine entry kinds. */
|
|
22
|
+
export type LedgerKind = 'opened' | 'receipt' | 'supplied' | 'note' | 'withdrawal' | 'submit' | 'check' | 'released' | 'delivered';
|
|
23
|
+
/** What a note says it is. */
|
|
24
|
+
export type LedgerNoteKind = 'claim' | 'ruled-out' | 'question';
|
|
25
|
+
/** Who supplied a value the agent did not read. */
|
|
26
|
+
export type LedgerSupplier = 'person' | 'app' | 'system';
|
|
27
|
+
/** A check's disposition: the same four words as `integrity/disposition/types.ts` · `Disposition`. */
|
|
28
|
+
export type LedgerDisposition = 'checked-pass' | 'checked-fail' | 'not-applicable' | 'unreachable';
|
|
29
|
+
/** A claim's verdict. There is no "contradicted" in this release. */
|
|
30
|
+
export type LedgerVerdict = 'verified' | 'consistent-with-reported' | 'unsupported' | 'open';
|
|
31
|
+
/** Why a rule gave no answer of its own. No message is ever stored beside it. */
|
|
32
|
+
export type LedgerCheckWhy = 'rule-threw' | 'rule-timeout' | 'bad-result';
|
|
33
|
+
/** A person's answer to a withheld report. */
|
|
34
|
+
export type LedgerAnswer = 'release' | 'withhold';
|
|
35
|
+
/**
|
|
36
|
+
* The closed refusal words a call answers with. A refusal never carries a
|
|
37
|
+
* source value. `store-refused` travels with the store's code.
|
|
38
|
+
*/
|
|
39
|
+
export type LedgerRefusal = 'no-session' | 'bad-session' | 'session-exists' | 'bad-input' | 'too-large' | 'unknown-receipt' | 'not-a-receipt' | 'unknown-note' | 'not-a-note' | 'not-a-claim' | 'already-withdrawn' | 'withdrawn-note' | 'bad-window' | 'unknown-rule' | 'rule-not-loaded' | 'ledger-full' | 'busy' | 'store-refused';
|
|
40
|
+
/** `sha256:` and 64 lower-case hex digits. */
|
|
41
|
+
export type LedgerDigest = string;
|
|
42
|
+
/** A half-open time window `[from, to)`, both ends RFC 3339 instants with an offset. */
|
|
43
|
+
export interface LedgerWindow {
|
|
44
|
+
readonly from: string;
|
|
45
|
+
readonly to: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A stored decision record. Its shape is the engine's (`decisionRecord` /
|
|
49
|
+
* `fixedRecord`, not yet released); until then it is admitted as bounded,
|
|
50
|
+
* canonical JSON.
|
|
51
|
+
*/
|
|
52
|
+
export type LedgerDecisionRecord = {
|
|
53
|
+
readonly [key: string]: JsonValue;
|
|
54
|
+
};
|
|
55
|
+
/** What every entry carries. `at` is the host's clock, RFC 3339 UTC. */
|
|
56
|
+
export interface LedgerEntryBase<K extends LedgerKind> {
|
|
57
|
+
readonly v: 1;
|
|
58
|
+
readonly seq: number;
|
|
59
|
+
readonly kind: K;
|
|
60
|
+
readonly by: LedgerBy;
|
|
61
|
+
readonly how: LedgerHow;
|
|
62
|
+
readonly at: string;
|
|
63
|
+
}
|
|
64
|
+
/** The first entry of every session: what it runs. */
|
|
65
|
+
export interface LedgerOpened extends LedgerEntryBase<'opened'> {
|
|
66
|
+
/** The `id@version` list this session runs. */
|
|
67
|
+
readonly rules: readonly string[];
|
|
68
|
+
readonly confirm: boolean;
|
|
69
|
+
readonly askRunsRules: boolean;
|
|
70
|
+
}
|
|
71
|
+
/** What one read was told to have returned — tokens, never the text. */
|
|
72
|
+
export interface LedgerReceipt extends LedgerEntryBase<'receipt'> {
|
|
73
|
+
readonly source: string;
|
|
74
|
+
readonly query?: string;
|
|
75
|
+
readonly window?: LedgerWindow;
|
|
76
|
+
readonly limit?: {
|
|
77
|
+
readonly rows: number;
|
|
78
|
+
readonly hit: boolean;
|
|
79
|
+
};
|
|
80
|
+
/** UTF-8 bytes of the text the host handed in. */
|
|
81
|
+
readonly bytes: number;
|
|
82
|
+
readonly nothingFound: boolean;
|
|
83
|
+
/** Distinct tokens, first-seen order. */
|
|
84
|
+
readonly tokens: readonly string[];
|
|
85
|
+
/** Present only when something was not kept: tokens past the cap, text past the byte cap. */
|
|
86
|
+
readonly cut?: {
|
|
87
|
+
readonly tokensDropped: number;
|
|
88
|
+
readonly bytesUnread: number;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Present only when the part read holds numbers written with thousands separators
|
|
92
|
+
* (`1,234`): how many. Their values are not tokens, so the receipt is incomplete.
|
|
93
|
+
*/
|
|
94
|
+
readonly groupedNumbers?: number;
|
|
95
|
+
/**
|
|
96
|
+
* Present only when the part read holds pieces next to a backslash that read two ways
|
|
97
|
+
* (`\node1234`: a path's name, or a newline and `ode1234`): how many. Neither is a token.
|
|
98
|
+
*/
|
|
99
|
+
readonly escapedPieces?: number;
|
|
100
|
+
/** How many values the host's redactor masked before recording. */
|
|
101
|
+
readonly hidden?: number;
|
|
102
|
+
readonly ref?: {
|
|
103
|
+
readonly uri: string;
|
|
104
|
+
readonly digest: LedgerDigest;
|
|
105
|
+
};
|
|
106
|
+
readonly externalId?: string;
|
|
107
|
+
}
|
|
108
|
+
/** A value given to the agent by the person, the app or the system: exempt, never support. */
|
|
109
|
+
export interface LedgerSupplied extends LedgerEntryBase<'supplied'> {
|
|
110
|
+
readonly from: LedgerSupplier;
|
|
111
|
+
readonly tokens: readonly string[];
|
|
112
|
+
readonly bytes: number;
|
|
113
|
+
readonly digest: LedgerDigest;
|
|
114
|
+
}
|
|
115
|
+
/** What the agent noted, and which receipts it cites. */
|
|
116
|
+
export interface LedgerNote extends LedgerEntryBase<'note'> {
|
|
117
|
+
readonly noteKind: LedgerNoteKind;
|
|
118
|
+
readonly text: string;
|
|
119
|
+
readonly values?: readonly string[];
|
|
120
|
+
readonly window?: LedgerWindow;
|
|
121
|
+
/** Receipt seqs, sorted, unique. */
|
|
122
|
+
readonly cites: readonly number[];
|
|
123
|
+
}
|
|
124
|
+
/** The agent takes a note back. */
|
|
125
|
+
export interface LedgerWithdrawal extends LedgerEntryBase<'withdrawal'> {
|
|
126
|
+
readonly note: number;
|
|
127
|
+
readonly why: string;
|
|
128
|
+
}
|
|
129
|
+
/** The agent hands in a draft and names the notes it rests on. */
|
|
130
|
+
export interface LedgerSubmit extends LedgerEntryBase<'submit'> {
|
|
131
|
+
readonly claims: readonly number[];
|
|
132
|
+
readonly draftDigest: LedgerDigest;
|
|
133
|
+
readonly draftBytes: number;
|
|
134
|
+
}
|
|
135
|
+
/** One rule's finding on one subject of one submit. */
|
|
136
|
+
export interface LedgerCheck extends LedgerEntryBase<'check'> {
|
|
137
|
+
readonly submit: number;
|
|
138
|
+
readonly subject: {
|
|
139
|
+
readonly note: number;
|
|
140
|
+
} | {
|
|
141
|
+
readonly draft: true;
|
|
142
|
+
};
|
|
143
|
+
/** `id@version`. */
|
|
144
|
+
readonly rule: string;
|
|
145
|
+
readonly disposition: LedgerDisposition;
|
|
146
|
+
readonly verdict?: LedgerVerdict;
|
|
147
|
+
/** Receipt seqs, sorted, unique. */
|
|
148
|
+
readonly restsOn: readonly number[];
|
|
149
|
+
readonly decision?: LedgerDecisionRecord;
|
|
150
|
+
readonly counts: {
|
|
151
|
+
readonly visible: number;
|
|
152
|
+
readonly missing: number;
|
|
153
|
+
readonly supplied: number;
|
|
154
|
+
readonly incomplete: number;
|
|
155
|
+
};
|
|
156
|
+
readonly missing?: readonly string[];
|
|
157
|
+
readonly why?: LedgerCheckWhy;
|
|
158
|
+
}
|
|
159
|
+
/** A person's answer to a submit held for them, through the host. */
|
|
160
|
+
export interface LedgerReleased extends LedgerEntryBase<'released'> {
|
|
161
|
+
readonly submit: number;
|
|
162
|
+
readonly answer: LedgerAnswer;
|
|
163
|
+
readonly why?: 'no-person-channel';
|
|
164
|
+
}
|
|
165
|
+
/** What the host delivered, compared with the last submit's draft. */
|
|
166
|
+
export interface LedgerDelivered extends LedgerEntryBase<'delivered'> {
|
|
167
|
+
readonly digest: LedgerDigest;
|
|
168
|
+
readonly bytes: number;
|
|
169
|
+
readonly submit: number | null;
|
|
170
|
+
readonly matches: boolean;
|
|
171
|
+
}
|
|
172
|
+
/** Any entry. */
|
|
173
|
+
export type LedgerEntry = LedgerOpened | LedgerReceipt | LedgerSupplied | LedgerNote | LedgerWithdrawal | LedgerSubmit | LedgerCheck | LedgerReleased | LedgerDelivered;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/llm-ledger/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* llm-ledger/window — the ledger's one instant validator, and the window built on it.
|
|
3
|
+
*
|
|
4
|
+
* Pattern: a validator, not a second grammar. It accepts a strict SUBSET of
|
|
5
|
+
* what `time/instant.ts` · `instantOf` accepts in its `strict`
|
|
6
|
+
* profile, and gives the same epoch milliseconds for every string it
|
|
7
|
+
* accepts — pinned by `test/llm-ledger/window-differential.test.ts`.
|
|
8
|
+
* It is the one named exception to "no other file parses an
|
|
9
|
+
* instant", because this folder is a closed leaf and cannot import
|
|
10
|
+
* `src/time/`.
|
|
11
|
+
* Role: the closed leaf's time check: a receipt's and a note's `window`,
|
|
12
|
+
* and every entry's `at`.
|
|
13
|
+
* Emits: N/A.
|
|
14
|
+
*
|
|
15
|
+
* ## The grammar
|
|
16
|
+
*
|
|
17
|
+
* `YYYY-MM-DDTHH:MM:SS[.f]` then `Z` or `±HH:MM`:
|
|
18
|
+
*
|
|
19
|
+
* - upper-case `T` and `Z` only;
|
|
20
|
+
* - seconds required, 0–59 (no leap second);
|
|
21
|
+
* - a fraction of 1–3 digits (millisecond precision), or none;
|
|
22
|
+
* - a day that exists in its month; hours 0–23; an offset of at most ±23:59.
|
|
23
|
+
*
|
|
24
|
+
* Anything else is refused. A window is `{ from, to }`, half-open, with
|
|
25
|
+
* `from` strictly before `to`; the strings are kept as written.
|
|
26
|
+
*/
|
|
27
|
+
import type { LedgerWindow } from './types.js';
|
|
28
|
+
/**
|
|
29
|
+
* The epoch milliseconds an instant names, or `undefined` when `value` is not
|
|
30
|
+
* one under the grammar above.
|
|
31
|
+
*/
|
|
32
|
+
export declare function instantMs(value: unknown): number | undefined;
|
|
33
|
+
/** Whether `value` is an instant in UTC (`Z`): what every entry's `at` must be. */
|
|
34
|
+
export declare function isUtcInstant(value: unknown): value is string;
|
|
35
|
+
/**
|
|
36
|
+
* The window `value` names, as a fresh frozen `{ from, to }`, or `undefined`
|
|
37
|
+
* (the refusal `bad-window`) when it is not exactly those two keys, both
|
|
38
|
+
* instants, with `from` before `to`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function readWindow(value: unknown): LedgerWindow | undefined;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const INSTANT = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?(?:(Z)|([+-])(\d{2}):(\d{2}))$/;
|
|
2
|
+
/** Days in `month` (1–12) of the proleptic Gregorian `year`. */
|
|
3
|
+
function daysIn(year, month) {
|
|
4
|
+
if (month === 2)
|
|
5
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
6
|
+
return month === 4 || month === 6 || month === 9 || month === 11 ? 30 : 31;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The epoch milliseconds an instant names, or `undefined` when `value` is not
|
|
10
|
+
* one under the grammar above.
|
|
11
|
+
*/
|
|
12
|
+
export function instantMs(value) {
|
|
13
|
+
if (typeof value !== 'string')
|
|
14
|
+
return undefined;
|
|
15
|
+
const m = INSTANT.exec(value);
|
|
16
|
+
if (m === null)
|
|
17
|
+
return undefined;
|
|
18
|
+
const [year, month, day, hour, minute, second] = [m[1], m[2], m[3], m[4], m[5], m[6]].map(Number);
|
|
19
|
+
if (month < 1 || month > 12 || day < 1 || day > daysIn(year, month))
|
|
20
|
+
return undefined;
|
|
21
|
+
if (hour > 23 || minute > 59 || second > 59)
|
|
22
|
+
return undefined;
|
|
23
|
+
let offsetMinutes = 0;
|
|
24
|
+
if (m[8] === undefined) {
|
|
25
|
+
const offsetHours = Number(m[10]);
|
|
26
|
+
const offsetMins = Number(m[11]);
|
|
27
|
+
if (offsetHours > 23 || offsetMins > 59)
|
|
28
|
+
return undefined;
|
|
29
|
+
offsetMinutes = (m[9] === '-' ? -1 : 1) * (offsetHours * 60 + offsetMins);
|
|
30
|
+
}
|
|
31
|
+
const millis = m[7] === undefined ? 0 : Number(m[7].padEnd(3, '0'));
|
|
32
|
+
// `setUTCFullYear`, never `Date.UTC`: the latter reads years 0–99 as 1900–1999.
|
|
33
|
+
const date = new Date(0);
|
|
34
|
+
date.setUTCFullYear(year, month - 1, day);
|
|
35
|
+
date.setUTCHours(hour, minute, second, millis);
|
|
36
|
+
return date.getTime() - offsetMinutes * 60_000;
|
|
37
|
+
}
|
|
38
|
+
/** Whether `value` is an instant in UTC (`Z`): what every entry's `at` must be. */
|
|
39
|
+
export function isUtcInstant(value) {
|
|
40
|
+
return typeof value === 'string' && value.endsWith('Z') && instantMs(value) !== undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The window `value` names, as a fresh frozen `{ from, to }`, or `undefined`
|
|
44
|
+
* (the refusal `bad-window`) when it is not exactly those two keys, both
|
|
45
|
+
* instants, with `from` before `to`.
|
|
46
|
+
*/
|
|
47
|
+
export function readWindow(value) {
|
|
48
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
49
|
+
return undefined;
|
|
50
|
+
const proto = Object.getPrototypeOf(value);
|
|
51
|
+
if (proto !== Object.prototype && proto !== null)
|
|
52
|
+
return undefined;
|
|
53
|
+
const keys = Reflect.ownKeys(value);
|
|
54
|
+
if (keys.length !== 2 || !keys.includes('from') || !keys.includes('to'))
|
|
55
|
+
return undefined;
|
|
56
|
+
// Read through descriptors, so a getter is never called: an accessor has no `value`, and
|
|
57
|
+
// `undefined` is not an instant, so an accessor end is refused like any other non-instant.
|
|
58
|
+
const fromField = Object.getOwnPropertyDescriptor(value, 'from');
|
|
59
|
+
const toField = Object.getOwnPropertyDescriptor(value, 'to');
|
|
60
|
+
const from = instantMs(fromField?.value);
|
|
61
|
+
const to = instantMs(toField?.value);
|
|
62
|
+
if (from === undefined || to === undefined || from >= to)
|
|
63
|
+
return undefined;
|
|
64
|
+
return Object.freeze({ from: fromField?.value, to: toField?.value });
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=window.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"window.js","sourceRoot":"","sources":["../../../src/llm-ledger/window.ts"],"names":[],"mappings":"AA4BA,MAAM,OAAO,GACX,gGAAgG,CAAC;AAEnG,gEAAgE;AAChE,SAAS,MAAM,CAAC,IAAY,EAAE,KAAa;IACzC,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,GAAG,KAAK,CAAC,IAAI,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC3F,OAAO,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AAC7E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAChD,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IACjC,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC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,GAAG,SAAS,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACzC,MAAM,EAAE,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACrC,IAAI,IAAI,KAAK,SAAS,IAAI,EAAE,KAAK,SAAS,IAAI,IAAI,IAAI,EAAE;QAAE,OAAO,SAAS,CAAC;IAC3E,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAe,EAAE,EAAE,EAAE,OAAO,EAAE,KAAe,EAAE,CAAC,CAAC;AAC3F,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ResultRewriteIndex } from '../../core/agent/recordedCalls.js';
|
|
2
|
+
/** Names-only metadata from a positively bound occurrence; never opens result bytes. */
|
|
3
|
+
export declare function resultRewriteLine(occurrenceKey: string | undefined, rewrites: ResultRewriteIndex, snapshot: unknown): string | undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isPlaceholder, servedUnderPolicy } from '../../redaction/marker.js';
|
|
2
|
+
import { boundedPreview, displayText, renderPreview } from '../bounded.js';
|
|
3
|
+
import { TOOL_RESULT_PREVIEW_CHARS } from './structuralLimits.js';
|
|
4
|
+
/** Names-only metadata from a positively bound occurrence; never opens result bytes. */
|
|
5
|
+
export function resultRewriteLine(occurrenceKey, rewrites, snapshot) {
|
|
6
|
+
if (occurrenceKey === undefined)
|
|
7
|
+
return undefined;
|
|
8
|
+
const owner = rewrites.get(occurrenceKey);
|
|
9
|
+
const name = owner?.kind === 'rewritten' ? owner.name : undefined;
|
|
10
|
+
if (name === undefined || (servedUnderPolicy(snapshot) && isPlaceholder(name)))
|
|
11
|
+
return undefined;
|
|
12
|
+
return `rewritten by: ${displayText(renderPreview(boundedPreview(name, TOOL_RESULT_PREVIEW_CHARS)))}`;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=resultRewrite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resultRewrite.js","sourceRoot":"","sources":["../../../../src/trace-toolpack/traceToolpack/resultRewrite.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAElE,wFAAwF;AACxF,MAAM,UAAU,iBAAiB,CAC/B,aAAiC,EACjC,QAA4B,EAC5B,QAAiB;IAEjB,IAAI,aAAa,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAClD,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IAC1C,MAAM,IAAI,GAAG,KAAK,EAAE,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,IAAI,IAAI,KAAK,SAAS,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,SAAS,CAAC;IACjG,OAAO,iBAAiB,WAAW,CACjC,aAAa,CAAC,cAAc,CAAC,IAAI,EAAE,yBAAyB,CAAC,CAAC,CAC/D,EAAE,CAAC;AACN,CAAC"}
|