@lmzhen/dsh-evolution-core 0.53.0 → 0.55.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.
Files changed (53) hide show
  1. package/lib/{extract-plan-D4zO7dmD.js → extract-plan-ihQznLVT.js} +2 -1
  2. package/lib/extract.js +7 -7
  3. package/lib/index.js +795 -546
  4. package/lib/{limits-TD4Pv92Q.js → limits-DBg3UMkw.js} +11 -6
  5. package/lib/{skill-history-CZp8vhyL.js → mutation-storage-adapter-BM7mIJKp.js} +4 -647
  6. package/lib/mutations-1wLISSrY.js +592 -0
  7. package/lib/policy.js +9 -3
  8. package/lib/{quality-BLc6UQQb.js → quality-DNUVRbUm.js} +1 -1
  9. package/lib/{skill-health-BIpiPvw6.js → review-context-CXKwGGd8.js} +124 -183
  10. package/lib/runtime/plugin.js +92 -1
  11. package/lib/{session-projection-ByNxQi9O.js → session-projection-CbIKvDfJ.js} +3 -2
  12. package/lib/skill-history-B-DyIedq.js +646 -0
  13. package/lib/skills.js +12 -8
  14. package/lib/text-metrics-CyJhxWBs.js +132 -0
  15. package/lib/types/constants.d.ts +10 -5
  16. package/lib/types/constants.js +10 -5
  17. package/lib/types/drift-signals.d.ts +1 -4
  18. package/lib/types/drift-signals.js +14 -23
  19. package/lib/types/extract-request.js +3 -2
  20. package/lib/types/frontmatter.js +12 -8
  21. package/lib/types/index.d.ts +2 -1
  22. package/lib/types/index.js +2 -1
  23. package/lib/types/memory-store.d.ts +32 -6
  24. package/lib/types/memory-store.js +86 -30
  25. package/lib/types/mutation-unit.js +2 -1
  26. package/lib/types/params.js +6 -3
  27. package/lib/types/policy.d.ts +5 -0
  28. package/lib/types/policy.js +8 -2
  29. package/lib/types/prompts.d.ts +1 -1
  30. package/lib/types/prompts.js +1 -1
  31. package/lib/types/runtime/coherence.d.ts +27 -0
  32. package/lib/types/runtime/coherence.js +115 -0
  33. package/lib/types/runtime/plugin.js +2 -0
  34. package/lib/types/signals.js +3 -2
  35. package/lib/types/skill-health.d.ts +8 -5
  36. package/lib/types/skill-health.js +27 -28
  37. package/lib/types/skill-history.d.ts +2 -2
  38. package/lib/types/skill-history.js +7 -7
  39. package/lib/types/skill-lint.d.ts +1 -1
  40. package/lib/types/skill-lint.js +4 -4
  41. package/lib/types/skill-stats.d.ts +3 -1
  42. package/lib/types/skill-stats.js +2 -0
  43. package/lib/types/skill-store.js +2 -1
  44. package/lib/types/text-budgets.d.ts +150 -0
  45. package/lib/types/text-budgets.js +65 -0
  46. package/lib/types/text-metrics.d.ts +88 -0
  47. package/lib/types/text-metrics.js +131 -0
  48. package/lib/{usage-B1qveKi9.js → usage-e34VZPmK.js} +2 -515
  49. package/lib/write.js +3 -2
  50. package/package.json +1 -1
  51. package/lib/mutations-DQXddSJd.js +0 -75
  52. package/lib/types/cost.d.ts +0 -63
  53. package/lib/types/cost.js +0 -67
@@ -0,0 +1,88 @@
1
+ /**
2
+ * One text-metrics source for every character-based budget in the family.
3
+ *
4
+ * A budget is not a language: the same text needs different units for storage,
5
+ * display, context cost and quality heuristics. This module measures ONE string
6
+ * once and exposes the four units; callers choose a unit through a named budget
7
+ * instead of reaching for `String.length`, `[...text].length` or `slice()`.
8
+ *
9
+ * The token figure is deliberately an ESTIMATE. With no model tokenizer it uses
10
+ * a script-neutral conservative upper bound (`utf8Bytes / 3`, floored by
11
+ * `codePoints / 4`); a caller may inject a provider/model estimator later
12
+ * without changing any budget.
13
+ *
14
+ * @module @lmzhen/dsh-evolution-core
15
+ */
16
+ /** Units a text budget may use. */
17
+ export type TextUnit = 'utf8Bytes' | 'codePoints' | 'graphemes' | 'tokens';
18
+ /** One token estimate, with the measurement range kept visible for UI copy. */
19
+ export interface TokenEstimate {
20
+ /** Conservative point estimate a gate should compare against. */
21
+ readonly value: number;
22
+ /** Lower end of the model-agnostic range (never presented as an exact count). */
23
+ readonly low: number;
24
+ /** Upper end of the model-agnostic range. */
25
+ readonly high: number;
26
+ readonly source: 'unicode-heuristic' | 'platform' | 'model';
27
+ }
28
+ /** All units for one text, measured together so callers cannot drift. */
29
+ export interface TextMetrics {
30
+ /** UTF-8 byte length. */
31
+ readonly utf8Bytes: number;
32
+ /** Unicode scalar values (code points). */
33
+ readonly codePoints: number;
34
+ /** User-perceived characters; falls back to code points without Intl.Segmenter. */
35
+ readonly graphemes: number;
36
+ readonly tokens: TokenEstimate;
37
+ /** True when the runtime lacked Intl.Segmenter and graphemes are code points. */
38
+ readonly degraded: boolean;
39
+ }
40
+ /** Replaceable token estimator; a provider/model adapter can supply exact counts. */
41
+ export type TokenEstimator = (text: string) => TokenEstimate;
42
+ /** UTF-8 byte length; TextEncoder is available on every Node/browser target this package runs on. */
43
+ export declare function utf8ByteLength(text: string): number;
44
+ /** Unicode scalar-value count. */
45
+ export declare function codePointCount(text: string): number;
46
+ /**
47
+ * User-perceived character count.
48
+ * @param text - the string to measure.
49
+ * @returns grapheme clusters, or code points when the runtime has no Segmenter.
50
+ */
51
+ export declare function graphemeCount(text: string): number;
52
+ /**
53
+ * Conservative, model-agnostic token estimate.
54
+ *
55
+ * English prose is about 4 characters/token; CJK prose is about 1 token/char.
56
+ * UTF-8 bytes encode that difference naturally (3 bytes for CJK, 1 for ASCII),
57
+ * so the upper bound is `bytes / 3` while `codePoints / 4` keeps the lower end
58
+ * anchored to the common English heuristic. The point estimate is the upper
59
+ * bound: early warnings are recoverable, silent over-budget is not.
60
+ * @param text - the string to estimate.
61
+ * @returns the estimate and its range; never an exact tokenizer count.
62
+ */
63
+ export declare function conservativeTokenEstimator(text: string): TokenEstimate;
64
+ /**
65
+ * Measure every unit for one string in one pass over the text.
66
+ * @param text - the raw string; body callers should pass the on-disk form.
67
+ * @param estimator - optional exact/estimated token source; defaults to the conservative fallback.
68
+ * @returns all units and the token range.
69
+ */
70
+ export declare function measureText(text: string, estimator?: TokenEstimator): TextMetrics;
71
+ /**
72
+ * Read one unit from a fresh measurement.
73
+ * @param text - the string to measure.
74
+ * @param unit - the budget unit to read.
75
+ * @param estimator - optional token source; ignored for non-token units.
76
+ * @returns the numeric value in that unit.
77
+ */
78
+ export declare function measureUnit(text: string, unit: TextUnit, estimator?: TokenEstimator): number;
79
+ /**
80
+ * Truncate to a grapheme budget without splitting emoji or combining sequences.
81
+ * The ellipsis counts inside `max`, matching how UI budgets are described.
82
+ * @param text - the string to shorten.
83
+ * @param max - maximum output graphemes, ellipsis included.
84
+ * @param ellipsis - marker appended when shortened; may itself be multi-grapheme.
85
+ * @returns the original text or a grapheme-safe prefix plus marker.
86
+ */
87
+ export declare function truncateGraphemes(text: string, max: number, ellipsis?: string): string;
88
+ //# sourceMappingURL=text-metrics.d.ts.map
@@ -0,0 +1,131 @@
1
+ /**
2
+ * One text-metrics source for every character-based budget in the family.
3
+ *
4
+ * A budget is not a language: the same text needs different units for storage,
5
+ * display, context cost and quality heuristics. This module measures ONE string
6
+ * once and exposes the four units; callers choose a unit through a named budget
7
+ * instead of reaching for `String.length`, `[...text].length` or `slice()`.
8
+ *
9
+ * The token figure is deliberately an ESTIMATE. With no model tokenizer it uses
10
+ * a script-neutral conservative upper bound (`utf8Bytes / 3`, floored by
11
+ * `codePoints / 4`); a caller may inject a provider/model estimator later
12
+ * without changing any budget.
13
+ *
14
+ * @module @lmzhen/dsh-evolution-core
15
+ */
16
+ const segmenterCtor = Intl.Segmenter;
17
+ const segmenter = segmenterCtor === undefined
18
+ ? undefined
19
+ : new segmenterCtor(undefined, { granularity: 'grapheme' });
20
+ /** UTF-8 byte length; TextEncoder is available on every Node/browser target this package runs on. */
21
+ export function utf8ByteLength(text) {
22
+ return new TextEncoder().encode(text).length;
23
+ }
24
+ /** Unicode scalar-value count. */
25
+ export function codePointCount(text) {
26
+ let count = 0;
27
+ for (const _codePoint of text)
28
+ count += 1;
29
+ return count;
30
+ }
31
+ /**
32
+ * User-perceived character count.
33
+ * @param text - the string to measure.
34
+ * @returns grapheme clusters, or code points when the runtime has no Segmenter.
35
+ */
36
+ export function graphemeCount(text) {
37
+ if (segmenter === undefined)
38
+ return codePointCount(text);
39
+ let count = 0;
40
+ for (const _grapheme of segmenter.segment(text))
41
+ count += 1;
42
+ return count;
43
+ }
44
+ /** Split a string without ever cutting through a grapheme cluster. */
45
+ function graphemeParts(text) {
46
+ if (segmenter === undefined)
47
+ return [...text];
48
+ const parts = [];
49
+ for (const part of segmenter.segment(text)) {
50
+ const value = part.segment;
51
+ if (typeof value === 'string')
52
+ parts.push(value);
53
+ }
54
+ return parts;
55
+ }
56
+ /** The pure estimate from already-counted bytes/code points. */
57
+ function estimateFromCounts(bytes, codePoints) {
58
+ if (bytes === 0)
59
+ return { value: 0, low: 0, high: 0, source: 'unicode-heuristic' };
60
+ const low = Math.ceil(codePoints / 4);
61
+ const high = Math.ceil(bytes / 3);
62
+ return { value: Math.max(low, high), low, high, source: 'unicode-heuristic' };
63
+ }
64
+ /**
65
+ * Conservative, model-agnostic token estimate.
66
+ *
67
+ * English prose is about 4 characters/token; CJK prose is about 1 token/char.
68
+ * UTF-8 bytes encode that difference naturally (3 bytes for CJK, 1 for ASCII),
69
+ * so the upper bound is `bytes / 3` while `codePoints / 4` keeps the lower end
70
+ * anchored to the common English heuristic. The point estimate is the upper
71
+ * bound: early warnings are recoverable, silent over-budget is not.
72
+ * @param text - the string to estimate.
73
+ * @returns the estimate and its range; never an exact tokenizer count.
74
+ */
75
+ export function conservativeTokenEstimator(text) {
76
+ return estimateFromCounts(utf8ByteLength(text), codePointCount(text));
77
+ }
78
+ /**
79
+ * Measure every unit for one string in one pass over the text.
80
+ * @param text - the raw string; body callers should pass the on-disk form.
81
+ * @param estimator - optional exact/estimated token source; defaults to the conservative fallback.
82
+ * @returns all units and the token range.
83
+ */
84
+ export function measureText(text, estimator) {
85
+ const bytes = utf8ByteLength(text);
86
+ const codePoints = codePointCount(text);
87
+ return {
88
+ utf8Bytes: bytes,
89
+ codePoints,
90
+ graphemes: graphemeCount(text),
91
+ tokens: estimator === undefined ? estimateFromCounts(bytes, codePoints) : estimator(text),
92
+ degraded: segmenter === undefined,
93
+ };
94
+ }
95
+ /**
96
+ * Read one unit from a fresh measurement.
97
+ * @param text - the string to measure.
98
+ * @param unit - the budget unit to read.
99
+ * @param estimator - optional token source; ignored for non-token units.
100
+ * @returns the numeric value in that unit.
101
+ */
102
+ export function measureUnit(text, unit, estimator) {
103
+ const metrics = measureText(text, estimator);
104
+ switch (unit) {
105
+ case 'utf8Bytes': return metrics.utf8Bytes;
106
+ case 'codePoints': return metrics.codePoints;
107
+ case 'graphemes': return metrics.graphemes;
108
+ case 'tokens': return metrics.tokens.value;
109
+ }
110
+ }
111
+ /**
112
+ * Truncate to a grapheme budget without splitting emoji or combining sequences.
113
+ * The ellipsis counts inside `max`, matching how UI budgets are described.
114
+ * @param text - the string to shorten.
115
+ * @param max - maximum output graphemes, ellipsis included.
116
+ * @param ellipsis - marker appended when shortened; may itself be multi-grapheme.
117
+ * @returns the original text or a grapheme-safe prefix plus marker.
118
+ */
119
+ export function truncateGraphemes(text, max, ellipsis = '…') {
120
+ if (!Number.isFinite(max) || max <= 0)
121
+ return '';
122
+ const limit = Math.floor(max);
123
+ const parts = graphemeParts(text);
124
+ if (parts.length <= limit)
125
+ return text;
126
+ const marker = graphemeParts(ellipsis);
127
+ if (marker.length >= limit)
128
+ return marker.slice(0, limit).join('');
129
+ return parts.slice(0, limit - marker.length).join('') + ellipsis;
130
+ }
131
+ //# sourceMappingURL=text-metrics.js.map