@dsh-cc/compaction-cost-gate 0.8.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.i18n.yaml +6 -0
- package/README.md +46 -0
- package/README.zh.md +46 -0
- package/lib/fold-counters.d.ts +33 -0
- package/lib/fold-counters.d.ts.map +1 -0
- package/lib/fold-counters.js +62 -0
- package/lib/fold-counters.js.map +1 -0
- package/lib/gate.d.ts +15 -0
- package/lib/gate.d.ts.map +1 -0
- package/lib/gate.js +60 -0
- package/lib/gate.js.map +1 -0
- package/lib/index.d.ts +124 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +368 -0
- package/lib/index.js.map +1 -0
- package/lib/invariant.d.ts +16 -0
- package/lib/invariant.d.ts.map +1 -0
- package/lib/invariant.js +24 -0
- package/lib/invariant.js.map +1 -0
- package/lib/ledger.d.ts +60 -0
- package/lib/ledger.d.ts.map +1 -0
- package/lib/ledger.js +55 -0
- package/lib/ledger.js.map +1 -0
- package/lib/settings.d.ts +26 -0
- package/lib/settings.d.ts.map +1 -0
- package/lib/settings.js +57 -0
- package/lib/settings.js.map +1 -0
- package/lib/todo-diff.d.ts +29 -0
- package/lib/todo-diff.d.ts.map +1 -0
- package/lib/todo-diff.js +40 -0
- package/lib/todo-diff.js.map +1 -0
- package/lib/types.d.ts +103 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +13 -0
- package/lib/types.js.map +1 -0
- package/package.json +63 -0
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.
|
package/README.i18n.yaml
ADDED
|
@@ -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: d6e3615c67773da7c55fb9d3165721b11dbb879b
|
|
6
|
+
README.zh.md: fe55550499e05d58cd3799eea80aa9f2731f87bc
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @dsh-cc/compaction-cost-gate
|
|
2
|
+
|
|
3
|
+
English | [中文](README.zh.md)
|
|
4
|
+
|
|
5
|
+
Cost-gated plan-step compaction. Completing a plan step (a `todo_write` transition to completed) arms a boundary; when the agent next goes idle, the service evaluates whether a full-prefix compaction pays for itself — projected input-token savings over the remaining plan versus the prompt-cache rewrite cost plus any unamortized rewrite debt — and only then calls `ctx.compaction.compactNow` on the root session. Passes are invisible except in the ledger.
|
|
6
|
+
|
|
7
|
+
## How it decides
|
|
8
|
+
|
|
9
|
+
- **Detection (mid-turn):** a `tools/post-execute` listener filters `todo_write`, diffs the todos snapshot against the previous one, and arms the boundary on any transition to completed. It never mutates the downstream decision.
|
|
10
|
+
- **Action (idle):** an `agent/status` listener (`status === 'idle'`, the compaction family's own precedent) runs for the latched root session only. Subagent todo completions and requests never arm or trigger.
|
|
11
|
+
- **Gate arithmetic:** `projectedSavedInput = contextTokens × shrink × requestsPerStep × pendingSteps` must strictly exceed `margin × (rewriteCost + debtTokens)`. `contextTokens` is summed over the shadow-aware surface (`session.surface.nodes` + `eventAt`) so compacted spans are not double-counted. A past compaction's rewrite cost contributes as debt until 5 subsequent provider requests. `pendingSteps === 0` never fires — plan end has no future requests to amortize over. With a resolvable price row, savings price at the cache-read rate and rewrite at the cache-write rate; without one the comparison stays in tokens (conservative).
|
|
12
|
+
- **Window-pressure override:** when `contextTokens ≥ window-pressure-tokens` the gate is bypassed (cooldown and the failure fuse still apply).
|
|
13
|
+
|
|
14
|
+
## Circuit breakers
|
|
15
|
+
|
|
16
|
+
Only real defect classes (`changed | summary | commit | persistence`, unexpected throws) count. The expected `busy`/`cancelled` classes are ledgered as skips. At 3 consecutive real failures the feature pauses for the session with one model-visible notice pointing at manual `/compact`. After any actual compaction the gate cools down for `cooldown-ms` (default 600 000).
|
|
17
|
+
|
|
18
|
+
## Config (settings namespace `cc-compaction-cost-gate`)
|
|
19
|
+
|
|
20
|
+
| Key | Default | Meaning |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| `enabled` | `false` | Ships dark. |
|
|
23
|
+
| `mode` | `'dry-run'` | `dry-run` evaluates and ledgers both sides of the inequality, never calls `compactNow`. |
|
|
24
|
+
| `margin` | `1.0` | Required savings multiple over rewrite + debt. |
|
|
25
|
+
| `cooldown-ms` | `600000` | Cooldown after an actual compaction. |
|
|
26
|
+
| `window-pressure-tokens` | unset | Gate-bypass threshold; unset = no override. |
|
|
27
|
+
| `model-table` | unset | Optional price table (`ModelPrice[]`, same shape as `/cost`). |
|
|
28
|
+
|
|
29
|
+
Ledger: `<dshHome>/compaction-cost-gate/<projectKey>.jsonl`, append-only, fire-and-forget.
|
|
30
|
+
|
|
31
|
+
## foldCounters convention
|
|
32
|
+
|
|
33
|
+
This package exports `foldCounters(materials)` for `@dsh-cc/token-efficiency`; any change to the marker/ledger shape MUST update the fold and its tests in the same PR.
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
39
|
+
import CompactionCostGate from '@dsh-cc/compaction-cost-gate'
|
|
40
|
+
|
|
41
|
+
export function apply(ctx: Context): void {
|
|
42
|
+
ctx.plugin(CompactionCostGate)
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The service is mounted by the cc preset (`cc-services` group, id `compaction-cost-gate`). The compaction engine is deliberately not injected: cordis strict-read means a hard `inject` would kill the service where compaction is absent. It is read through a guarded accessor; when absent the package inactivates with one log line and one `compaction-unavailable` ledger row.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @dsh-cc/compaction-cost-gate
|
|
2
|
+
|
|
3
|
+
[English](README.md) | 中文
|
|
4
|
+
|
|
5
|
+
成本门控的计划步骤压缩。完成一个计划步骤(`todo_write` 中出现转为 completed 的迁移)会武装一个边界;当代理下一次进入空闲时,服务会评估一次全前缀压缩是否划算——将剩余计划上的输入 token 节省预期与提示缓存重写成本加未摊销的债务进行比较——只有通过时才对根会话调用 `ctx.compaction.compactNow`。通过除了账本外完全不可见。
|
|
6
|
+
|
|
7
|
+
## 决策方式
|
|
8
|
+
|
|
9
|
+
- **检测(回合中):** `tools/post-execute` 监听器过滤 `todo_write`,将 todos 快照与前一个快照做差分,并在任何转为 completed 的迁移上武装边界。它从不修改下游决策。
|
|
10
|
+
- **动作(空闲):** `agent/status` 监听器(`status === 'idle'`,压缩家族自身的先例)仅针对已锁定的根会话运行。子代理的 todo 完成和请求既不会武装也不会触发。
|
|
11
|
+
- **门控算术:** `projectedSavedInput = contextTokens × shrink × requestsPerStep × pendingSteps` 必须严格大于 `margin × (rewriteCost + debtTokens)`。`contextTokens` 在影子感知表面上求和(`session.surface.nodes` + `eventAt`),因此被压缩的跨度不会被重复计算。过去一次压缩的重写成本作为债务保留,直到后续 5 个提供者请求。`pendingSteps === 0` 永不触发——计划结束没有未来的请求可以摊销。当价格行可解析时,节省按缓存读取速率计价,重写按缓存写入速率计价;否则比较保持以 token 为单位(保守)。
|
|
12
|
+
- **窗口压力覆盖:** 当 `contextTokens ≥ window-pressure-tokens` 时绕过门控(冷却和失败熔断仍然生效)。
|
|
13
|
+
|
|
14
|
+
## 断路器
|
|
15
|
+
|
|
16
|
+
只有真实缺陷类别(`changed | summary | commit | persistence`、意外抛出)会被计数。预期的 `busy`/`cancelled` 类别被记为跳过。连续 3 次真实失败后,功能对该会话暂停,并给出一条指向手动 `/compact` 的模型可见通知。任何实际压缩之后,门控会冷却 `cooldown-ms`(默认 600 000)。
|
|
17
|
+
|
|
18
|
+
## 配置(settings 命名空间 `cc-compaction-cost-gate`)
|
|
19
|
+
|
|
20
|
+
| 键 | 默认 | 含义 |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| `enabled` | `false` | 默认熄火发布。 |
|
|
23
|
+
| `mode` | `'dry-run'` | `dry-run` 会评估并记录不等式两侧,从不调用 `compactNow`。 |
|
|
24
|
+
| `margin` | `1.0` | 相对重写加债务所需的节省倍数。 |
|
|
25
|
+
| `cooldown-ms` | `600000` | 实际压缩后的冷却时间。 |
|
|
26
|
+
| `window-pressure-tokens` | 未设置 | 门控绕过阈值;未设置 = 无覆盖。 |
|
|
27
|
+
| `model-table` | 未设置 | 可选价格表(`ModelPrice[]`,与 `/cost` 相同形状)。 |
|
|
28
|
+
|
|
29
|
+
账本:`<dshHome>/compaction-cost-gate/<projectKey>.jsonl`,仅追加、即发即忘。
|
|
30
|
+
|
|
31
|
+
## foldCounters 约定
|
|
32
|
+
|
|
33
|
+
本包为 `@dsh-cc/token-efficiency` 导出 `foldCounters(materials)`;标记/台账形态的任何变更必须在同一个 PR 中同步更新该 fold 及其测试。
|
|
34
|
+
|
|
35
|
+
## 用法
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
39
|
+
import CompactionCostGate from '@dsh-cc/compaction-cost-gate'
|
|
40
|
+
|
|
41
|
+
export function apply(ctx: Context): void {
|
|
42
|
+
ctx.plugin(CompactionCostGate)
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
该服务由 cc 预设挂载(`cc-services` 组,id `compaction-cost-gate`)。压缩引擎被刻意不注入:cordis 严格读取意味着硬 `inject` 会在压缩缺失的地方杀死服务。它通过受保护的访问器读取;缺失时该包以一行日志和一条 `compaction-unavailable` 账本行失效。
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* foldCounters seam for @dsh-cc/token-efficiency (design
|
|
3
|
+
* docs/plans/2026-09-20-token-efficiency-eval-harness.md §3.4).
|
|
4
|
+
*
|
|
5
|
+
* The ledger shape knowledge lives HERE (the feature owns it): any change to
|
|
6
|
+
* the ledger row kinds or path derivation must update this fold and its tests
|
|
7
|
+
* in the same PR — never a silently-wrong foreign regex.
|
|
8
|
+
*
|
|
9
|
+
* @module @dsh-cc/compaction-cost-gate/fold-counters
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Structural materials contract shared by every efficiency-feature fold.
|
|
13
|
+
* Declared locally (no cross-package import); the loose event shape
|
|
14
|
+
* intentionally mirrors SessionLogEvent without depending on dsh-session.
|
|
15
|
+
* The events field is unused by this fold (ledger-only evidence).
|
|
16
|
+
*/
|
|
17
|
+
export interface FoldCounterMaterials {
|
|
18
|
+
readonly events: readonly {
|
|
19
|
+
readonly type: string;
|
|
20
|
+
readonly time?: number;
|
|
21
|
+
readonly data?: Record<string, unknown> | undefined;
|
|
22
|
+
}[];
|
|
23
|
+
readonly dshHome: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Fold the package's own ledger (`<dshHome>/compaction-cost-gate/<projectKey>.jsonl`,
|
|
27
|
+
* same projectKey derivation as the writer) into counts by kind.
|
|
28
|
+
* Missing file → all-zero counters. A trailing partial line (live file
|
|
29
|
+
* truncation) is skipped silently; any OTHER malformed line throws with its
|
|
30
|
+
* 1-based line number.
|
|
31
|
+
*/
|
|
32
|
+
export declare function foldCounters(materials: FoldCounterMaterials): Record<string, number>;
|
|
33
|
+
//# sourceMappingURL=fold-counters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold-counters.d.ts","sourceRoot":"","sources":["../src/fold-counters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,MAAM,EAAE,SAAS;QACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;QACrB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;QACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;KACpD,EAAE,CAAA;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CACzB;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAgCpF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* foldCounters seam for @dsh-cc/token-efficiency (design
|
|
3
|
+
* docs/plans/2026-09-20-token-efficiency-eval-harness.md §3.4).
|
|
4
|
+
*
|
|
5
|
+
* The ledger shape knowledge lives HERE (the feature owns it): any change to
|
|
6
|
+
* the ledger row kinds or path derivation must update this fold and its tests
|
|
7
|
+
* in the same PR — never a silently-wrong foreign regex.
|
|
8
|
+
*
|
|
9
|
+
* @module @dsh-cc/compaction-cost-gate/fold-counters
|
|
10
|
+
*/
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { projectKeyOf } from "./ledger.js";
|
|
14
|
+
/**
|
|
15
|
+
* Fold the package's own ledger (`<dshHome>/compaction-cost-gate/<projectKey>.jsonl`,
|
|
16
|
+
* same projectKey derivation as the writer) into counts by kind.
|
|
17
|
+
* Missing file → all-zero counters. A trailing partial line (live file
|
|
18
|
+
* truncation) is skipped silently; any OTHER malformed line throws with its
|
|
19
|
+
* 1-based line number.
|
|
20
|
+
*/
|
|
21
|
+
export function foldCounters(materials) {
|
|
22
|
+
const out = {
|
|
23
|
+
'costgate.gate': 0,
|
|
24
|
+
'costgate.compacted': 0,
|
|
25
|
+
'costgate.skipped': 0,
|
|
26
|
+
'costgate.unavailable': 0,
|
|
27
|
+
};
|
|
28
|
+
const file = join(materials.dshHome, 'compaction-cost-gate', `${projectKeyOf(process.cwd())}.jsonl`);
|
|
29
|
+
let raw;
|
|
30
|
+
try {
|
|
31
|
+
raw = readFileSync(file, 'utf8');
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return out; // missing ledger → zeros, not an error
|
|
35
|
+
}
|
|
36
|
+
const lines = raw.split('\n');
|
|
37
|
+
// A trailing partial line is the live-writer truncation case: drop it silently.
|
|
38
|
+
if (lines.length > 0 && lines[lines.length - 1] !== '')
|
|
39
|
+
lines.pop();
|
|
40
|
+
for (let i = 0; i < lines.length; i++) {
|
|
41
|
+
const line = lines[i] ?? '';
|
|
42
|
+
if (line === '')
|
|
43
|
+
continue;
|
|
44
|
+
let kind;
|
|
45
|
+
try {
|
|
46
|
+
kind = String(JSON.parse(line).kind);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
throw new Error(`compaction-cost-gate fold: malformed ledger line ${i + 1} in ${file}`);
|
|
50
|
+
}
|
|
51
|
+
if (kind === 'gate')
|
|
52
|
+
out['costgate.gate'] += 1;
|
|
53
|
+
else if (kind === 'compacted')
|
|
54
|
+
out['costgate.compacted'] += 1;
|
|
55
|
+
else if (kind.startsWith('skipped:'))
|
|
56
|
+
out['costgate.skipped'] += 1;
|
|
57
|
+
else if (kind === 'compaction-unavailable')
|
|
58
|
+
out['costgate.unavailable'] += 1;
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=fold-counters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold-counters.js","sourceRoot":"","sources":["../src/fold-counters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,O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,CAAC,CAAA;QACzF,CAAC;QACD,IAAI,IAAI,KAAK,MAAM;YAAE,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAA;aACzC,IAAI,IAAI,KAAK,WAAW;YAAE,GAAG,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAA;aACxD,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;YAAE,GAAG,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAA;aAC7D,IAAI,IAAI,KAAK,wBAAwB;YAAE,GAAG,CAAC,sBAAsB,CAAC,IAAI,CAAC,CAAA;IAC9E,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/lib/gate.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure cost-gate arithmetic (design §3.3): compare projected input-token
|
|
3
|
+
* savings over the remaining plan against the prompt-cache rewrite price plus
|
|
4
|
+
* unamortized rewrite debt. No I/O, no clock, no services — a table-testable
|
|
5
|
+
* fold over measured inputs.
|
|
6
|
+
* @module @dsh-cc/compaction-cost-gate/gate
|
|
7
|
+
*/
|
|
8
|
+
import { type GateInput, type GateOutcome } from './types.ts';
|
|
9
|
+
/**
|
|
10
|
+
* Evaluate the gate for one idle boundary.
|
|
11
|
+
* @param input - measured gate inputs (§3.3.1–3.3.7).
|
|
12
|
+
* @returns the outcome with every intermediate term for the ledger.
|
|
13
|
+
*/
|
|
14
|
+
export declare function evaluateGate(input: GateInput): GateOutcome;
|
|
15
|
+
//# sourceMappingURL=gate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../src/gate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAGL,KAAK,SAAS,EACd,KAAK,WAAW,EACjB,MAAM,YAAY,CAAA;AAEnB;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,WAAW,CA6C1D"}
|
package/lib/gate.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure cost-gate arithmetic (design §3.3): compare projected input-token
|
|
3
|
+
* savings over the remaining plan against the prompt-cache rewrite price plus
|
|
4
|
+
* unamortized rewrite debt. No I/O, no clock, no services — a table-testable
|
|
5
|
+
* fold over measured inputs.
|
|
6
|
+
* @module @dsh-cc/compaction-cost-gate/gate
|
|
7
|
+
*/
|
|
8
|
+
import { COLD_SHRINK_PRIOR, DEBT_AMORTIZE_REQUESTS, } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Evaluate the gate for one idle boundary.
|
|
11
|
+
* @param input - measured gate inputs (§3.3.1–3.3.7).
|
|
12
|
+
* @returns the outcome with every intermediate term for the ledger.
|
|
13
|
+
*/
|
|
14
|
+
export function evaluateGate(input) {
|
|
15
|
+
const requestsPerStep = Math.max(input.streamRequestCount / Math.max(input.completedSteps, 1), 1);
|
|
16
|
+
const shrink = input.lastShrink ?? COLD_SHRINK_PRIOR;
|
|
17
|
+
let debtTokens = 0;
|
|
18
|
+
for (const entry of input.rewriteDebt) {
|
|
19
|
+
if (entry.requestsSince < DEBT_AMORTIZE_REQUESTS)
|
|
20
|
+
debtTokens += entry.tokens;
|
|
21
|
+
}
|
|
22
|
+
const rewriteCost = input.contextTokens + debtTokens;
|
|
23
|
+
const pendingSteps = input.pendingSteps;
|
|
24
|
+
// Plan end is a natural session tail: no future requests to amortize over.
|
|
25
|
+
const projectedSavedInput = pendingSteps === 0
|
|
26
|
+
? 0
|
|
27
|
+
: input.contextTokens * shrink * requestsPerStep * pendingSteps;
|
|
28
|
+
const windowPressureOverride = input.windowPressureTokens !== undefined
|
|
29
|
+
&& input.contextTokens >= input.windowPressureTokens
|
|
30
|
+
&& pendingSteps > 0;
|
|
31
|
+
let pass;
|
|
32
|
+
if (pendingSteps === 0) {
|
|
33
|
+
pass = false;
|
|
34
|
+
}
|
|
35
|
+
else if (windowPressureOverride) {
|
|
36
|
+
pass = true;
|
|
37
|
+
}
|
|
38
|
+
else if (input.price !== undefined) {
|
|
39
|
+
// Savings price at the cache-read rate, rewrite at the cache-write rate.
|
|
40
|
+
// Without a price row the comparison stays in tokens (conservative:
|
|
41
|
+
// cache writes price above reads).
|
|
42
|
+
const savedUsd = projectedSavedInput * input.price.cacheReadPerMTok;
|
|
43
|
+
const costUsd = rewriteCost * input.price.cacheWritePerMTok;
|
|
44
|
+
pass = savedUsd > input.margin * costUsd;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
pass = projectedSavedInput > input.margin * rewriteCost;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
pass,
|
|
51
|
+
pendingSteps,
|
|
52
|
+
requestsPerStep,
|
|
53
|
+
shrink,
|
|
54
|
+
projectedSavedInput,
|
|
55
|
+
rewriteCost,
|
|
56
|
+
debtTokens,
|
|
57
|
+
windowPressureOverride,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=gate.js.map
|
package/lib/gate.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gate.js","sourceRoot":"","sources":["../src/gate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,iBAAiB,EACjB,sBAAsB,GAGvB,MAAM,YAAY,CAAA;AAEnB;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAgB;IAC3C,MAAM,eAAe,GAAG,IAAI,CAAC,GAAG,CAC9B,KAAK,CAAC,kBAAkB,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,CAAC,EAC5D,CAAC,CACF,CAAA;IACD,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,IAAI,iBAAiB,CAAA;IACpD,IAAI,UAAU,GAAG,CAAC,CAAA;IAClB,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;QACtC,IAAI,KAAK,C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ib/index.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost-gated plan-step compaction service (design
|
|
3
|
+
* docs/plans/2026-09-20-cost-gated-plan-step-compaction.md §3.4–§3.6).
|
|
4
|
+
*
|
|
5
|
+
* Two seams: a mid-turn `tools/post-execute` listener ARMS a boundary when a
|
|
6
|
+
* `todo_write` transitions a todo to completed (observe-only, decision
|
|
7
|
+
* untouched); an `agent/status` idle listener runs the §3.3 cost gate for the
|
|
8
|
+
* latched root session only and, on a pass in `mode: 'on'`, calls
|
|
9
|
+
* `ctx.compaction.compactNow` with a preservation hint parked immediately
|
|
10
|
+
* before the call and a fresh abort signal (the turn-scoped signal is dead by
|
|
11
|
+
* idle). Subagent agents never arm, count, or trigger.
|
|
12
|
+
*
|
|
13
|
+
* The compaction engine is deliberately NOT injected (cordis strict-read
|
|
14
|
+
* would kill the service where compaction is absent); it is read through the
|
|
15
|
+
* guarded {@link optionalCompaction} accessor, and absence inactivates the
|
|
16
|
+
* gate with one log line and one ledger row.
|
|
17
|
+
*
|
|
18
|
+
* @module @dsh-cc/compaction-cost-gate
|
|
19
|
+
*/
|
|
20
|
+
import { Service, type Context } from '@deepseek-ai/cordis';
|
|
21
|
+
import type { GenerateOptions } from '@deepseek-ai/dsh-llm';
|
|
22
|
+
import { type Session } from '@deepseek-ai/dsh-session';
|
|
23
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
24
|
+
import { CostGateLedger } from './ledger.ts';
|
|
25
|
+
export { CostGateLedger, projectKeyOf, type LedgerRow } from './ledger.ts';
|
|
26
|
+
export { evaluateGate } from './gate.ts';
|
|
27
|
+
export { foldCounters } from './fold-counters.ts';
|
|
28
|
+
export type { FoldCounterMaterials } from './fold-counters.ts';
|
|
29
|
+
export { diffTodos } from './todo-diff.ts';
|
|
30
|
+
export { SETTINGS_NAMESPACE, registerCostGateSettings } from './settings.ts';
|
|
31
|
+
export * from './types.ts';
|
|
32
|
+
declare module '@deepseek-ai/cordis' {
|
|
33
|
+
interface Context {
|
|
34
|
+
/** Harness-home path resolver, provided by @deepseek-ai/dsh-app-boot at boot. Optional in tests. */
|
|
35
|
+
dshHomePath?: (...segments: string[]) => string;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
declare module '@deepseek-ai/cordis' {
|
|
39
|
+
interface Context {
|
|
40
|
+
/** The mounted cost gate (present whenever the plugin mounts). */
|
|
41
|
+
compactionCostGate?: CompactionCostGate;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** The minimal agent surface the gate needs (structural; satisfied by the harness Agent). */
|
|
45
|
+
interface GateAgent {
|
|
46
|
+
readonly session: Session;
|
|
47
|
+
readonly options: {
|
|
48
|
+
provider?: string;
|
|
49
|
+
model?: string;
|
|
50
|
+
};
|
|
51
|
+
inject?(message: ReturnType<typeof createUserMessage>): void;
|
|
52
|
+
}
|
|
53
|
+
/** Testable seams over the settings reader and clock. */
|
|
54
|
+
export interface CostGateDeps {
|
|
55
|
+
/** Settings reader override (defaults to the live namespace reader). */
|
|
56
|
+
readonly readSettings?: () => import('./types.ts').CostGateSettings;
|
|
57
|
+
/** Ledger override (defaults to `<dshHome>/compaction-cost-gate`). */
|
|
58
|
+
readonly ledger?: CostGateLedger;
|
|
59
|
+
/** Clock override for cooldown tests. */
|
|
60
|
+
readonly now?: () => number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Cost-gated plan-step compaction service. One instance per mounted plugin;
|
|
64
|
+
* state is keyed by the latched root session id and lives in memory only.
|
|
65
|
+
*/
|
|
66
|
+
export declare class CompactionCostGate extends Service {
|
|
67
|
+
/** The token meter prices surface nodes for the gate arithmetic. */
|
|
68
|
+
static inject: string[];
|
|
69
|
+
private readonly readSettings;
|
|
70
|
+
private readonly ledger;
|
|
71
|
+
private readonly now;
|
|
72
|
+
/** Content of the todo whose completion armed the current boundary. */
|
|
73
|
+
private rootLatch;
|
|
74
|
+
private readonly stats;
|
|
75
|
+
constructor(ctx: Context, deps?: CostGateDeps);
|
|
76
|
+
/** Register the observe-only listeners (two-seam split, §3.4). */
|
|
77
|
+
private registerListeners;
|
|
78
|
+
/** Fresh per-root-session state (§3.2). */
|
|
79
|
+
private freshStats;
|
|
80
|
+
/**
|
|
81
|
+
* Latch the root session on the first main-loop observation and count
|
|
82
|
+
* main-loop requests for it (auxiliary purposes never latch or count);
|
|
83
|
+
* amortize rewrite debt by the requests it has since observed.
|
|
84
|
+
*/
|
|
85
|
+
observeStream(options: GenerateOptions): void;
|
|
86
|
+
/**
|
|
87
|
+
* Detection seam: filter `todo_write`, diff the snapshot, and arm the
|
|
88
|
+
* boundary on any transition to completed — for the latched root session
|
|
89
|
+
* only. Never mutates the tool decision.
|
|
90
|
+
*/
|
|
91
|
+
observePostExecute(exec: {
|
|
92
|
+
name: string;
|
|
93
|
+
arguments?: unknown;
|
|
94
|
+
agent?: {
|
|
95
|
+
session: Session;
|
|
96
|
+
};
|
|
97
|
+
}): void;
|
|
98
|
+
/** Action seam: evaluate the gate at idle for the latched root session only. */
|
|
99
|
+
evaluateIdle(agent: GateAgent): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Hint-then-call: the hint must survive only the instant between set and
|
|
102
|
+
* consume; it is cleared on every exit path. A fresh AbortController (the
|
|
103
|
+
* turn-scoped signal is dead by idle and must never be reused).
|
|
104
|
+
*/
|
|
105
|
+
private compactNow;
|
|
106
|
+
/** One durable model-visible pause notice pointing at manual `/compact`. */
|
|
107
|
+
private notifyPaused;
|
|
108
|
+
/** Resolve the optional pricing layer from the last observed provider/model. */
|
|
109
|
+
private resolvePriceFor;
|
|
110
|
+
/**
|
|
111
|
+
* Σ estimateMessage over message-carrying surface nodes — the shadow-aware
|
|
112
|
+
* accessor (`session.surface.nodes` + `eventAt`), never raw events, so
|
|
113
|
+
* spans shadowed by prior compactions are not double-counted.
|
|
114
|
+
*/
|
|
115
|
+
private contextTokens;
|
|
116
|
+
/** Non-completed todos in the latest snapshot. */
|
|
117
|
+
private pendingSteps;
|
|
118
|
+
/** Parse the todos array out of the `todo_write` tool arguments. */
|
|
119
|
+
private parseTodos;
|
|
120
|
+
/** Fire-and-forget ledger append; errors are swallowed by the ledger. */
|
|
121
|
+
private write;
|
|
122
|
+
}
|
|
123
|
+
export default CompactionCostGate;
|
|
124
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAe,MAAM,sBAAsB,CAAA;AAGxE,OAAO,EAAsB,KAAK,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAK3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAExD,OAAO,EAAE,cAAc,EAAgB,MAAM,aAAa,CAAA;AAU1D,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAA;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,YAAY,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AAC5E,cAAc,YAAY,CAAA;AAK1B,OAAO,QAAQ,qBAAqB,CAAC;IACnC,UAAU,OAAO;QACf,oGAAoG;QACpG,WAAW,CAAC,EAAE,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,KAAK,MAAM,CAAA;KAChD;CACF;AAED,OAAO,QAAQ,qBAAqB,CAAC;IACnC,UAAU,O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