@dsh-cc/context-crusher 0.6.2

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,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,6 @@
1
+ # Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
2
+ # side as of the last confirmed-consistent state. Both languages carry equal authority;
3
+ # after editing either side, bring the other along and re-record with:
4
+ # pnpm check:readme --write
5
+ README.md: 1a5f04928fd09d495217a4792b36ec6cf672f20d
6
+ README.zh.md: 7b77e3b5e7d77cc841805df0842a7b86ed97a8e8
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # @dsh-cc/context-crusher
2
+
3
+ English | [中文](README.zh.md)
4
+
5
+ CCR (Compress-Cache-Retrieve) reversible tool-output compression. A `tools/post-execute` listener compresses large grep/log-shaped tool results, caches the original in a content-addressed store, and appends a `ccr://<hash>` marker; the `context_retrieve({ hash })` tool restores the original verbatim. Compression is risk-free because the original is always one call away.
6
+
7
+ ## Gated flow
8
+
9
+ 1. `tools/post-execute` fires; the crusher is the OUTERMOST listener (`{ prepend: true }`, mounted before the hooks bridge) and composes post-`next()`.
10
+ 2. A non-`accept` downstream decision passes through untouched.
11
+ 3. Gates, in order: disabled → protected tool (replace-semantics list) → short error (< 2×min) → under min size → non-text blocks → router `null` → savings ratio below threshold. Each gate degrades to passthrough.
12
+ 4. `mode: 'dry-run'` (default) measures and appends a ledger row with `applied: false`; `mode: 'on'` stores the original, appends the marker, and replaces the result with one fresh text block. Downstream `additionalContexts` survive the replace.
13
+ 5. Every I/O error degrades to passthrough; a missing `dshHomePath` force-disables the crusher. A throw here would turn the user's tool result into an error — data loss — so it never throws into the waterfall.
14
+
15
+ ## Marker contract (pinned)
16
+
17
+ `[dsh-cc compressed BEFORE→AFTER tokens. Original: ccr://<hash>]` — appended to every replaced result. The `context_retrieve` tool description references the same `ccr://<hash>` spelling; a test pins the pairing. `hash` is `sha256(original utf8)` hex, first 16 chars.
18
+
19
+ ## Store
20
+
21
+ `$DSH_HOME/ccr/<projectKey>/<hash16>`, projectKey = `sha256(session cwd)` first 16 hex (the worktree-path divergence from the TUI project-root convention is accepted: the store is self-consistent). Atomic temp+rename writes, plain UTF-8 envelope files, LRU 200 entries, TTL 3600 s, sweep-on-write (fire-and-forget). Corrupt/expired entries fail closed (`corrupt` / `expired` / `unknown_hash`).
22
+
23
+ ## Config
24
+
25
+ Namespace `cc-context-compression` (settings overlay re-read on every use; config defaults under it). `protected-tools` REPLACES the default list when explicitly set — not a union.
26
+
27
+ | Key | Default | Meaning |
28
+ |---|---|---|
29
+ | `enabled` | `false` | Master flag; the feature is opt-in. |
30
+ | `mode` | `dry-run` | `dry-run` measures only; `on` replaces. |
31
+ | `min-bytes` | `8192` | Minimum size before eligibility (token-meter tokens; the key keeps its legacy name). |
32
+ | `min-savings-ratio` | `0.4` | Minimum token saving fraction. |
33
+ | `protected-tools` | over-inclusive default list | Tools never crushed. |
34
+
35
+ ## Caveats
36
+
37
+ - The transcript keeps the COMPRESSED form forever: TUI replay and `command-export` show the compressed blob (the original lives only in the store + ledger).
38
+ - `final-result` dispatches and tool-definition `finalizeContent` bypass or follow the waterfall and may rewrite crushed content — residual bypass classes, best-effort coverage.
39
+ - The marker is inert text; there is no auto-restore and no model-summarization.
package/README.zh.md ADDED
@@ -0,0 +1,39 @@
1
+ # @dsh-cc/context-crusher
2
+
3
+ [English](README.md) | 中文
4
+
5
+ CCR(Compress-Cache-Retrieve)可逆的工具输出压缩。一个 `tools/post-execute` 监听器压缩大体量的 grep/日志形态的工具结果,把原文缓存到内容寻址存储,并附加 `ccr://<hash>` 标记;`context_retrieve({ hash })` 工具可原样取回原文。原文始终一次调用可得,因此压缩是无风险的。
6
+
7
+ ## 门控流程
8
+
9
+ 1. `tools/post-execute` 触发;crusher 是最外层监听器(`{ prepend: true }`,挂载在 hooks bridge 之前),在 `next()` 之后组合。
10
+ 2. 下游非 `accept` 的决策原样通过。
11
+ 3. 门控依序:未启用 → 受保护工具(replace 语义列表)→ 短错误(< 2×min)→ 低于最小尺寸 → 存在非 text 块 → 路由返回 `null` → 节省比例不足。每个门控都退化为透传。
12
+ 4. `mode: 'dry-run'`(默认)只度量并追加 `applied: false` 的台账行;`mode: 'on'` 存原文、附加标记,并用一个全新 text 块替换结果。下游 `additionalContexts` 在替换后保留。
13
+ 5. 所有 I/O 错误都退化为透传;缺少 `dshHomePath` 时强制禁用。此处抛错会把用户的工具结果变成错误——数据丢失——因此绝不向瀑布抛错。
14
+
15
+ ## 标记契约(钉死)
16
+
17
+ `[dsh-cc compressed BEFORE→AFTER tokens. Original: ccr://<hash>]` —— 附加在每个被替换的结果上。`context_retrieve` 的工具描述引用同样的 `ccr://<hash>` 拼写;有测试钉死这一配对。`hash` 为 `sha256(原文 utf8)` 十六进制前 16 位。
18
+
19
+ ## 存储
20
+
21
+ `$DSH_HOME/ccr/<projectKey>/<hash16>`,projectKey = `sha256(会话 cwd)` 前 16 位(与 TUI 项目根约定的工作树路径分歧在此被接受:存储自身一致)。原子 temp+rename 写入、纯 UTF-8 信封文件、LRU 200 条、TTL 3600 秒、写入后清扫(fire-and-forget)。损坏/过期条目封闭失败(`corrupt` / `expired` / `unknown_hash`)。
22
+
23
+ ## 配置
24
+
25
+ 命名空间 `cc-context-compression`(设置覆盖每次使用时重读;配置默认值在其下)。`protected-tools` 显式设置时替换默认列表——不是并集。
26
+
27
+ | 键 | 默认 | 含义 |
28
+ |---|---|---|
29
+ | `enabled` | `false` | 总开关;功能需显式开启。 |
30
+ | `mode` | `dry-run` | `dry-run` 只度量;`on` 替换。 |
31
+ | `min-bytes` | `8192` | 参与压缩的最小尺寸(按 token 度量;键名保留旧称)。 |
32
+ | `min-savings-ratio` | `0.4` | 最小 token 节省比例。 |
33
+ | `protected-tools` | 过宽的默认列表 | 永不压缩的工具。 |
34
+
35
+ ## 注意事项
36
+
37
+ - 转录中永久保留的是压缩后的形态:TUI 回放与 `command-export` 展示压缩块(原文只在存储与台账中)。
38
+ - `final-result` 派发与工具定义的 `finalizeContent` 绕过或晚于瀑布,可能重写已压缩内容——残余绕过类别,尽力覆盖。
39
+ - 标记是惰性文本;没有自动恢复,也没有模型摘要。
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Configuration resolution and schema for the CCR tool-output crusher.
3
+ * @module @dsh-cc/context-crusher/config
4
+ */
5
+ import z from '@deepseek-ai/schemastery';
6
+ import type { CrusherConfig, ResolvedConfig } from './types.ts';
7
+ /**
8
+ * Tools whose output is never crushed. Intentionally over-inclusive
9
+ * (ponytail: structured-output and mutation-adjacent tools stay verbatim
10
+ * until ledger data proves a narrower list is safe).
11
+ */
12
+ export declare const DEFAULT_PROTECTED_TOOLS: readonly string[];
13
+ export declare const DEFAULTS: ResolvedConfig;
14
+ /** Row-level deployment-default schema (cordis `static Config`). */
15
+ export declare const Config: z<CrusherConfig>;
16
+ /** Settings namespace schema (same shape as the config layer). */
17
+ export declare const SettingsSchema: z<CrusherConfig>;
18
+ /**
19
+ * Resolve and validate the plugin configuration over the defaults.
20
+ * @param config - raw row config.
21
+ * @returns a detached deeply immutable configuration.
22
+ */
23
+ export declare function resolveConfig(config?: CrusherConfig): ResolvedConfig;
24
+ /**
25
+ * Overlay the live settings scope onto the config defaults. `protected-tools`
26
+ * uses REPLACE semantics: an explicitly set settings list replaces the
27
+ * defaults entirely (never a union).
28
+ * @param base - resolved config-layer defaults.
29
+ * @param scope - live settings scope value, when a provider is mounted.
30
+ * @returns the effective configuration for one use.
31
+ */
32
+ export declare function overlaySettings(base: ResolvedConfig, scope: CrusherConfig | undefined): ResolvedConfig;
33
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAExC,OAAO,KAAK,EAAE,aAAa,EAAe,cAAc,EAAE,MAAM,YAAY,CAAA;AAE5E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,SAAS,MAAM,EAIpD,CAAA;AAED,eAAO,MAAM,QAAQ,EAAE,cAMrB,CAAA;AAIF,oEAAoE;AACpE,eAAO,MAAM,MAAM,EAAE,CAAC,CAAC,aAAa,CAMlC,CAAA;AAEF,kEAAkE;AAClE,eAAO,MAAM,cAAc,EAAE,CAAC,CAAC,aAAa,CAAU,CAAA;AAEtD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,GAAE,aAAkB,GAAG,cAAc,CAkBxE;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,aAAa,GAAG,SAAS,GAAG,cAAc,CAStG"}
package/lib/config.js ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Configuration resolution and schema for the CCR tool-output crusher.
3
+ * @module @dsh-cc/context-crusher/config
4
+ */
5
+ import z from '@deepseek-ai/schemastery';
6
+ import { deepFreeze } from '@deepseek-ai/dsh-util-values';
7
+ /**
8
+ * Tools whose output is never crushed. Intentionally over-inclusive
9
+ * (ponytail: structured-output and mutation-adjacent tools stay verbatim
10
+ * until ledger data proves a narrower list is safe).
11
+ */
12
+ export const DEFAULT_PROTECTED_TOOLS = [
13
+ 'edit', 'write', 'notebook_edit', 'memory_save', 'ask_user_question',
14
+ 'enter_worktree', 'exit_worktree', 'subagent_fork', 'send_message',
15
+ 'task', 'interrupt_agent',
16
+ ];
17
+ export const DEFAULTS = deepFreeze({
18
+ enabled: false,
19
+ mode: 'dry-run',
20
+ minBytes: 8192,
21
+ minSavingsRatio: 0.4,
22
+ protectedTools: DEFAULT_PROTECTED_TOOLS,
23
+ });
24
+ const MODES = ['dry-run', 'on'];
25
+ /** Row-level deployment-default schema (cordis `static Config`). */
26
+ export const Config = z.object({
27
+ enabled: z.boolean(),
28
+ mode: z.union(MODES),
29
+ 'min-bytes': z.number().step(1).min(1),
30
+ 'min-savings-ratio': z.number().min(0).max(1),
31
+ 'protected-tools': z.array(z.string()),
32
+ });
33
+ /** Settings namespace schema (same shape as the config layer). */
34
+ export const SettingsSchema = Config;
35
+ /**
36
+ * Resolve and validate the plugin configuration over the defaults.
37
+ * @param config - raw row config.
38
+ * @returns a detached deeply immutable configuration.
39
+ */
40
+ export function resolveConfig(config = {}) {
41
+ if (config.mode !== undefined && !MODES.includes(config.mode)) {
42
+ throw new Error(`context-crusher: mode must be one of ${MODES.join(', ')}`);
43
+ }
44
+ if (config['min-bytes'] !== undefined && (!Number.isFinite(config['min-bytes']) || config['min-bytes'] <= 0)) {
45
+ throw new Error('context-crusher: min-bytes must be a positive number');
46
+ }
47
+ if (config['min-savings-ratio'] !== undefined
48
+ && (!Number.isFinite(config['min-savings-ratio']) || config['min-savings-ratio'] < 0 || config['min-savings-ratio'] > 1)) {
49
+ throw new Error('context-crusher: min-savings-ratio must be within [0, 1]');
50
+ }
51
+ return deepFreeze({
52
+ enabled: config.enabled ?? DEFAULTS.enabled,
53
+ mode: config.mode ?? DEFAULTS.mode,
54
+ minBytes: config['min-bytes'] ?? DEFAULTS.minBytes,
55
+ minSavingsRatio: config['min-savings-ratio'] ?? DEFAULTS.minSavingsRatio,
56
+ protectedTools: config['protected-tools'] ?? DEFAULTS.protectedTools,
57
+ });
58
+ }
59
+ /**
60
+ * Overlay the live settings scope onto the config defaults. `protected-tools`
61
+ * uses REPLACE semantics: an explicitly set settings list replaces the
62
+ * defaults entirely (never a union).
63
+ * @param base - resolved config-layer defaults.
64
+ * @param scope - live settings scope value, when a provider is mounted.
65
+ * @returns the effective configuration for one use.
66
+ */
67
+ export function overlaySettings(base, scope) {
68
+ if (scope === undefined || typeof scope !== 'object')
69
+ return base;
70
+ return {
71
+ enabled: scope.enabled ?? base.enabled,
72
+ mode: scope.mode ?? base.mode,
73
+ minBytes: scope['min-bytes'] ?? base.minBytes,
74
+ minSavingsRatio: scope['min-savings-ratio'] ?? base.minSavingsRatio,
75
+ protectedTools: scope['protected-tools'] ?? base.protectedTools,
76
+ };
77
+ }
78
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,CAAC,MAAM,0BAA0B,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAGzD;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAsB;IACxD,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,mBAAmB;IACpE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,cAAc;IAClE,MAAM,EAAE,iBAAiB;CAC1B,CAAA;AAED,MAAM,CAAC,MAAM,QAAQ,GAAmB,UAAU,CAAC;IACjD,OAAO,EAAE,KAAK;IACd,IAAI,EAAE,SAAS;IACf,QAAQ,EAAE,IAAI;IACd,eAAe,EAAE,GAAG;IACpB,cAAc,EAAE,uBAAuB;CACxC,CAAC,CAAA;AAEF,MAAM,KAAK,GAA2B,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;AAEvD,oEAAoE;AACpE,MAAM,CAAC,MAAM,MAAM,GAAqB,CAAC,CAAC,MAAM,CAAC;IAC/C,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;IACpB,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,KAAmD,CAAC;IAClE,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACtC,mBAAmB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC7C,iBAAiB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;CACvC,CAAC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AAQ,CAAC,cAAc;KACrE,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,IAAoB,EAAE,KAAgC;IACpF,IAAI,KAAK,KAAK,SAAS,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IACjE,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO;QACtC,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI;QAC7B,QAAQ,EAAE,KAAK,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,QAAQ;QAC7C,eAAe,EAAE,KAAK,CAAC,mBAAmB,CAAC,IAAI,IAAI,CAAC,eAAe;QACnE,cAAc,EAAE,KAAK,CAAC,iBAAiB,CAAC,IAAI,IAAI,CAAC,cAAc;KAChE,CAAA;AACH,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * CCR (Compress-Cache-Retrieve) reversible tool-output compression.
3
+ *
4
+ * A `tools/post-execute` listener (registered `{ prepend: true }`, composed
5
+ * post-`next()`) compresses large grep/log-shaped tool results, persists the
6
+ * original in a content-addressed store, and appends a `ccr://<hash>` marker;
7
+ * the `context_retrieve({ hash })` agent tool restores the original. Defaults
8
+ * are `enabled: false, mode: 'dry-run'` — nothing replaces until opted in.
9
+ *
10
+ * Fail-closed everywhere: a missing `dshHomePath` force-disables the crusher,
11
+ * and every store/ledger/sweep I/O error degrades to passthrough — a throw in
12
+ * this waterfall would turn the user's tool result into an error (data loss).
13
+ *
14
+ * @module @dsh-cc/context-crusher
15
+ */
16
+ import { Service } from '@deepseek-ai/cordis';
17
+ import type { Context } from '@deepseek-ai/cordis';
18
+ import type { ToolRunContext } from '@dsh-cc/tools';
19
+ import type { CrusherConfig, RetrieveError, ResolvedConfig } from './types.ts';
20
+ export { resolveConfig, overlaySettings, DEFAULTS, DEFAULT_PROTECTED_TOOLS, Config } from './config.ts';
21
+ export { SETTINGS_NAMESPACE } from './settings.ts';
22
+ export { route } from './router.ts';
23
+ export { CrusherStore, shortHash, STORE_MAX_ENTRIES, STORE_TTL_MS } from './store.ts';
24
+ export { SavingsLedger } from './ledger.ts';
25
+ export { buildMarker, parseMarker } from './marker.ts';
26
+ export type { CrusherConfig, CrusherMode, ResolvedConfig, LedgerRow, RetrieveError } from './types.ts';
27
+ export declare const RETRIEVE_TOOL_NAME = "context_retrieve";
28
+ /** The pinned marker contract, mirrored verbatim in the retrieve tool's description. */
29
+ export declare const RETRIEVE_TOOL_DESCRIPTION: string;
30
+ declare module '@deepseek-ai/cordis' {
31
+ interface Context {
32
+ /** Harness-home path resolver, provided by @deepseek-ai/dsh-app-boot at boot. Optional in tests. */
33
+ dshHomePath?: (...segments: string[]) => string;
34
+ }
35
+ }
36
+ /**
37
+ * The context-crusher service: registers the post-execute tripwire listener
38
+ * and the `context_retrieve` agent tool.
39
+ */
40
+ export declare class ContextCrusher extends Service {
41
+ static inject: string[];
42
+ static Config: import("@deepseek-ai/schemastery").default<CrusherConfig>;
43
+ /** Resolved config-layer defaults; the settings overlay re-reads per use. */
44
+ private readonly base;
45
+ private readonly readSettings;
46
+ private readonly store;
47
+ private readonly ledger;
48
+ constructor(ctx: Context, config?: CrusherConfig);
49
+ /** Effective configuration for one use: config defaults overlaid by live settings. */
50
+ effectiveConfig(): ResolvedConfig;
51
+ private registerListener;
52
+ private crush;
53
+ /** Token-based sizing (D3): the same estimator gates, markers, and ledger rows. */
54
+ private estimate;
55
+ /** `sha256(sessionCwd)` first 16 hex — the store's project bucket. */
56
+ private projectKey;
57
+ /** Typed retrieval through the store; fails closed (used by the tool body). */
58
+ retrieve(exec: ToolRunContext, hash: string): Promise<{
59
+ ok: true;
60
+ text: string;
61
+ } | {
62
+ ok: false;
63
+ error: RetrieveError | 'unavailable' | 'invalid_hash';
64
+ }>;
65
+ private registerRetrieveTool;
66
+ }
67
+ export default ContextCrusher;
68
+ //# sour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