@sonettogu14/dsh-te-core 0.0.0-stage → 0.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
- # Temporary Holding Version
1
+ # @sonettogu14/dsh-te-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Pure functions shared by the `dsh-te-*` components of
4
+ [dsh-tokeneconomizer](https://github.com/SonettoGu14/dsh-tokeneconomizer): the cache-weighted pricing table, token
5
+ buckets, fact lifecycle, shell-call parsing, repository keys and atomic
6
+ storage.
7
+
8
+ This is a library, not a DSH plugin — installing it into a profile does
9
+ nothing. Install one of the components instead:
10
+
11
+ ```sh
12
+ dsh plugin --profile <profile> add @sonettogu14/dsh-te-cost
13
+ ```
14
+
15
+ It has no Cordis dependency and no I/O beyond `node:fs`, so it can be unit
16
+ tested directly.
17
+
18
+ Full documentation: [README.md](https://github.com/SonettoGu14/dsh-tokeneconomizer#readme).
19
+ Licensed Apache-2.0; see `NOTICE` in the repository for upstream attribution.
package/lib/cost.d.ts ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Bucket arithmetic for the cache-weighted equivalent-token metric.
3
+ *
4
+ * Pure functions only: the plugin owns timers, sessions and files, this module
5
+ * owns the numbers so they can be tested against a hand-computed fixture.
6
+ * @module @sonettogu14/dsh-te-core/cost
7
+ */
8
+ import type { TokenWeights } from './pricing.ts';
9
+ /** One assistant call's provider-reported usage. */
10
+ export interface UsageSample {
11
+ /** Uncached input (billed input minus the two cache classes). */
12
+ readonly inputTokens: number;
13
+ readonly outputTokens: number;
14
+ readonly cacheReadTokens?: number;
15
+ readonly cacheWriteTokens?: number;
16
+ }
17
+ /** The same four disjoint buckets, accumulated. */
18
+ export interface TokenBuckets {
19
+ readonly uncachedInput: number;
20
+ readonly cacheRead: number;
21
+ readonly cacheWrite: number;
22
+ readonly output: number;
23
+ }
24
+ /** The additive identity. */
25
+ export declare const ZERO_BUCKETS: TokenBuckets;
26
+ /**
27
+ * Normalize one usage sample into buckets.
28
+ * @param sample - provider usage for one assistant call.
29
+ * @returns the four buckets, absent cache classes counted as zero.
30
+ */
31
+ export declare function sampleBuckets(sample: UsageSample): TokenBuckets;
32
+ /**
33
+ * Add two bucket sets.
34
+ * @param a - base buckets.
35
+ * @param b - buckets to add.
36
+ * @returns a new bucket set.
37
+ */
38
+ export declare function addBuckets(a: TokenBuckets, b: TokenBuckets): TokenBuckets;
39
+ /**
40
+ * Raw token count across all four buckets (what a bill-less meter would report).
41
+ * @param buckets - buckets to sum.
42
+ * @returns the sum.
43
+ */
44
+ export declare function bucketsTotal(buckets: TokenBuckets): number;
45
+ /**
46
+ * Price one bucket set into equivalent fresh-input tokens.
47
+ * @param buckets - accumulated buckets.
48
+ * @param weights - the route's price ratios.
49
+ * @returns equivalent tokens, in the metric's unit.
50
+ */
51
+ export declare function equivalentTokens(buckets: TokenBuckets, weights: TokenWeights): number;
52
+ /**
53
+ * Price one usage sample directly.
54
+ * @param sample - provider usage for one call.
55
+ * @param weights - the route's price ratios.
56
+ * @returns equivalent tokens for that call.
57
+ */
58
+ export declare function sampleEquivalent(sample: UsageSample, weights: TokenWeights): number;
59
+ /** A running per-session total: the buckets, their price, and the call count. */
60
+ export interface CostAccumulator {
61
+ readonly buckets: TokenBuckets;
62
+ readonly equivalent: number;
63
+ readonly messages: number;
64
+ }
65
+ /** The zero accumulator. */
66
+ export declare const EMPTY_ACCUMULATOR: CostAccumulator;
67
+ /**
68
+ * Fold one usage sample into an accumulator.
69
+ *
70
+ * Weights are supplied per sample because a session may switch route mid-flight;
71
+ * each call is priced with the weights that were in force for it.
72
+ *
73
+ * @param accumulator - totals so far.
74
+ * @param sample - provider usage for one call.
75
+ * @param weights - the route's price ratios for that call.
76
+ * @returns the new totals.
77
+ */
78
+ export declare function foldUsage(accumulator: CostAccumulator, sample: UsageSample, weights: TokenWeights): CostAccumulator;
79
+ //# sourceMappingURL=cost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAEhD,oDAAoD;AACpD,MAAM,WAAW,WAAW;IAC1B,iEAAiE;IACjE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;IACjC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CACnC;AAED,mDAAmD;AACnD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB;AAED,6BAA6B;AAC7B,eAAO,MAAM,YAAY,EAAE,YAKzB,CAAA;AAEF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,YAAY,CAO/D;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,YAAY,CAOzE;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,MAAM,CAE1D;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAOrF;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAEnF;AAED,iFAAiF;AACjF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAA;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAC1B;AAED,4BAA4B;AAC5B,eAAO,MAAM,iBAAiB,EAAE,eAI9B,CAAA;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,WAAW,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,GAAG,eAAe,CAOnH"}
package/lib/cost.js ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Bucket arithmetic for the cache-weighted equivalent-token metric.
3
+ *
4
+ * Pure functions only: the plugin owns timers, sessions and files, this module
5
+ * owns the numbers so they can be tested against a hand-computed fixture.
6
+ * @module @sonettogu14/dsh-te-core/cost
7
+ */
8
+ /** The additive identity. */
9
+ export const ZERO_BUCKETS = Object.freeze({
10
+ uncachedInput: 0,
11
+ cacheRead: 0,
12
+ cacheWrite: 0,
13
+ output: 0,
14
+ });
15
+ /**
16
+ * Normalize one usage sample into buckets.
17
+ * @param sample - provider usage for one assistant call.
18
+ * @returns the four buckets, absent cache classes counted as zero.
19
+ */
20
+ export function sampleBuckets(sample) {
21
+ return {
22
+ uncachedInput: sample.inputTokens,
23
+ cacheRead: sample.cacheReadTokens ?? 0,
24
+ cacheWrite: sample.cacheWriteTokens ?? 0,
25
+ output: sample.outputTokens,
26
+ };
27
+ }
28
+ /**
29
+ * Add two bucket sets.
30
+ * @param a - base buckets.
31
+ * @param b - buckets to add.
32
+ * @returns a new bucket set.
33
+ */
34
+ export function addBuckets(a, b) {
35
+ return {
36
+ uncachedInput: a.uncachedInput + b.uncachedInput,
37
+ cacheRead: a.cacheRead + b.cacheRead,
38
+ cacheWrite: a.cacheWrite + b.cacheWrite,
39
+ output: a.output + b.output,
40
+ };
41
+ }
42
+ /**
43
+ * Raw token count across all four buckets (what a bill-less meter would report).
44
+ * @param buckets - buckets to sum.
45
+ * @returns the sum.
46
+ */
47
+ export function bucketsTotal(buckets) {
48
+ return buckets.uncachedInput + buckets.cacheRead + buckets.cacheWrite + buckets.output;
49
+ }
50
+ /**
51
+ * Price one bucket set into equivalent fresh-input tokens.
52
+ * @param buckets - accumulated buckets.
53
+ * @param weights - the route's price ratios.
54
+ * @returns equivalent tokens, in the metric's unit.
55
+ */
56
+ export function equivalentTokens(buckets, weights) {
57
+ return (buckets.uncachedInput * weights.input +
58
+ buckets.cacheRead * weights.cacheRead +
59
+ buckets.cacheWrite * weights.cacheWrite +
60
+ buckets.output * weights.output);
61
+ }
62
+ /**
63
+ * Price one usage sample directly.
64
+ * @param sample - provider usage for one call.
65
+ * @param weights - the route's price ratios.
66
+ * @returns equivalent tokens for that call.
67
+ */
68
+ export function sampleEquivalent(sample, weights) {
69
+ return equivalentTokens(sampleBuckets(sample), weights);
70
+ }
71
+ /** The zero accumulator. */
72
+ export const EMPTY_ACCUMULATOR = Object.freeze({
73
+ buckets: ZERO_BUCKETS,
74
+ equivalent: 0,
75
+ messages: 0,
76
+ });
77
+ /**
78
+ * Fold one usage sample into an accumulator.
79
+ *
80
+ * Weights are supplied per sample because a session may switch route mid-flight;
81
+ * each call is priced with the weights that were in force for it.
82
+ *
83
+ * @param accumulator - totals so far.
84
+ * @param sample - provider usage for one call.
85
+ * @param weights - the route's price ratios for that call.
86
+ * @returns the new totals.
87
+ */
88
+ export function foldUsage(accumulator, sample, weights) {
89
+ const buckets = sampleBuckets(sample);
90
+ return {
91
+ buckets: addBuckets(accumulator.buckets, buckets),
92
+ equivalent: accumulator.equivalent + equivalentTokens(buckets, weights),
93
+ messages: accumulator.messages + 1,
94
+ };
95
+ }
96
+ //# sourceMappingURL=cost.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.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C,MAAM,GAAG,OAAO,CAAC,MAAM,CAChC,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAmB,EAAE,OAAqB;IACzE,OAAO,gBAAgB,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAA;AACzD,CAAC;AASD,4BAA4B;AAC5B,MAAM,CAAC,MAAM,iBAAiB,GAAoB,MAAM,CAAC,MAAM,CAAC;IAC9D,OAAO,EAAE,YAAY;IACrB,UAAU,EAAE,CAAC;IACb,QAAQ,EAAE,CAAC;CACZ,CAAC,CAAA;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,SAAS,CAAC,WAA4B,EAAE,MAAmB,EAAE,OAAqB;IAChG,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA;IACrC,OAAO;QACL,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC;QACjD,UAAU,EAAE,WAAW,CAAC,UAAU,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC;QACvE,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,CAAC;KACnC,CAAA;AACH,CAAC"}
package/lib/facts.d.ts ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * The fact lifecycle: how one observation becomes a claim worth injecting, and
3
+ * how it dies.
4
+ *
5
+ * This is the piece that makes injection defensible. A fact is not shown to the
6
+ * model because it was observed; it is shown once it has been *confirmed across
7
+ * separate tasks* (so it is a property of the repository, not of one attempt)
8
+ * and only while the waste it prevents is still happening at a rate that pays
9
+ * for its own tokens.
10
+ *
11
+ * Ported from TokenEconomizer's `microskills/lifecycle.py`, with the same
12
+ * constants and the same asymmetry: evidence decays with distance, and a
13
+ * contradiction costs five times what a confirmation earns.
14
+ *
15
+ * @module @sonettogu14/dsh-te-core/facts
16
+ */
17
+ /** Confirmations in distinct tasks before a fact may be injected. */
18
+ export declare const K_PROMOTE = 3;
19
+ /** Recency decay: a confirmation `d` tasks ago is worth `GAMMA ** d`. */
20
+ export declare const GAMMA = 0.8;
21
+ /** A contradiction costs this many confirmations. */
22
+ export declare const LAMBDA = 5;
23
+ /** Below this score an active fact is retired. */
24
+ export declare const SCORE_FLOOR = 0;
25
+ /**
26
+ * Contradictions in this many distinct tasks before a fact dies.
27
+ *
28
+ * One noisy success is not evidence: a suite that happened to need none of the
29
+ * missing packages, or a wrapper that happened to work, should not erase a fact
30
+ * that three earlier tasks confirmed. The reference implementation uses the same
31
+ * two-strike rule for form repairs.
32
+ */
33
+ export declare const CONTRADICT_STREAK = 2;
34
+ /**
35
+ * Evidence older than this many tasks is dropped.
36
+ *
37
+ * A confirmation from a year ago says little about the repository today, and an
38
+ * unbounded array is a memory and serialization problem in a long-lived repo.
39
+ */
40
+ export declare const EVIDENCE_WINDOW = 20;
41
+ /** Injected facts must still be paying for themselves this often. */
42
+ export declare const FIRE_GATE = 0.4;
43
+ /** ... and only after this many exercised tasks, so one bad session cannot kill it. */
44
+ export declare const FIRE_MIN_N = 4;
45
+ /** Where a fact sits in its life. */
46
+ export type FactState = 'candidate' | 'active' | 'retired';
47
+ /** The bookkeeping every fact carries. */
48
+ export interface FactLifecycle {
49
+ /** Task ordinals in which the fact was confirmed. */
50
+ readonly confirms: readonly number[];
51
+ /** Task ordinals in which it was contradicted. */
52
+ readonly contradicts: readonly number[];
53
+ /** Tasks in which the fact was injected and its situation actually arose. */
54
+ readonly fired: number;
55
+ /** Of those, tasks where the waste it prevents still happened. */
56
+ readonly firedWaste: number;
57
+ readonly state: FactState;
58
+ }
59
+ /** A fact that has never been observed. */
60
+ export declare const NEW_LIFECYCLE: FactLifecycle;
61
+ /**
62
+ * Recency-weighted evidence score.
63
+ * @param life - the lifecycle to score.
64
+ * @param now - the current task ordinal.
65
+ * @returns confirmations minus `LAMBDA`× contradictions, both decayed.
66
+ */
67
+ export declare function score(life: FactLifecycle, now: number): number;
68
+ /**
69
+ * The share of injected tasks in which the waste still happened.
70
+ * @returns a rate in [0, 1]; 0 when never injected.
71
+ */
72
+ export declare function firingRate(life: FactLifecycle): number;
73
+ /**
74
+ * Whether this fact earns its tokens right now.
75
+ *
76
+ * Two gates: it must be active with enough cross-task evidence, and the waste
77
+ * it prevents must still be happening in at least `FIRE_GATE` of the tasks
78
+ * where it was exercised — otherwise the model has learned it and the fact is
79
+ * pure overhead.
80
+ */
81
+ export declare function injectable(life: FactLifecycle, now: number): boolean;
82
+ /**
83
+ * Fold one confirmation.
84
+ *
85
+ * A task confirms a fact once, no matter how many times it is seen inside that
86
+ * task: three repeats inside one session are one piece of evidence, not three.
87
+ *
88
+ * @param life - lifecycle so far.
89
+ * @param now - the task ordinal the confirmation belongs to.
90
+ * @returns the updated lifecycle, promoted to `active` once enough distinct
91
+ * tasks agree.
92
+ */
93
+ export declare function confirm(life: FactLifecycle, now: number): FactLifecycle;
94
+ /**
95
+ * Fold one contradiction.
96
+ *
97
+ * A contradiction costs `LAMBDA` confirmations, and a fact is retired once its
98
+ * score is spent — but only after contradictions in `CONTRADICT_STREAK` distinct
99
+ * tasks. Retired is terminal for this build: a fact that comes back should be
100
+ * re-learned, not resurrected with a stale history.
101
+ *
102
+ * @param life - lifecycle so far.
103
+ * @param now - the task ordinal the contradiction belongs to.
104
+ * @returns the updated lifecycle.
105
+ */
106
+ export declare function contradict(life: FactLifecycle, now: number): FactLifecycle;
107
+ /**
108
+ * Record one task in which the fact's situation arose.
109
+ *
110
+ * "Exercised" is independent of injection: the situation counts whenever the
111
+ * task touched the fact's intent, whether or not the fact was shown. That is
112
+ * what lets a fact be measured *before* it is ever injected, and what stops an
113
+ * unexercised fact from looking like a free win.
114
+ *
115
+ * @param life - lifecycle so far.
116
+ * @param exercised - whether the task touched the fact's situation at all.
117
+ * @param waste - whether the waste the fact prevents happened in this task.
118
+ * @returns the updated lifecycle.
119
+ */
120
+ export declare function recordExercise(life: FactLifecycle, exercised: boolean, waste: boolean): FactLifecycle;
121
+ //# sourceMappingURL=fac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