dsh-context-compression-improved 0.2.2 → 0.2.4

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 (52) hide show
  1. package/.github/workflows/ci.yml +39 -39
  2. package/CHANGELOG.md +137 -135
  3. package/README.md +112 -103
  4. package/package.json +142 -138
  5. package/packages/selector/README.md +31 -26
  6. package/packages/selector/README.zh.md +31 -26
  7. package/packages/selector/dsh.plugin.json +16 -16
  8. package/packages/selector/lib/client.d.ts +228 -228
  9. package/packages/selector/lib/client.js +1462 -1462
  10. package/packages/selector/lib/config.js +856 -842
  11. package/packages/selector/lib/index.d.ts +37 -37
  12. package/packages/selector/lib/index.js +693 -645
  13. package/packages/selector/lib/invariant.js +70 -70
  14. package/packages/selector/lib/pruner.d.ts +728 -683
  15. package/packages/selector/lib/pruner.js +4765 -3807
  16. package/packages/selector/lib/style.css +46 -46
  17. package/packages/selector/lib/tail-trim.js +136 -136
  18. package/packages/selector/package.json +15 -7
  19. package/packages/selector/src/client/index.ts +111 -111
  20. package/packages/selector/src/preset-overlay.ts +60 -1
  21. package/packages/selector/src/pruner/content.ts +18 -5
  22. package/packages/selector/src/pruner/types.ts +23 -5
  23. package/packages/selector/src/pruner.ts +164 -154
  24. package/packages/selector/src/runtime/adaptive-cost.ts +23 -12
  25. package/packages/selector/src/runtime/audit.ts +18 -2
  26. package/packages/selector/src/runtime/config.ts +30 -1
  27. package/packages/selector/src/runtime/measurement.ts +31 -2
  28. package/packages/selector/src/runtime/reducers.ts +1115 -97
  29. package/packages/selector/src/runtime/tokenpilot/dedup.ts +1 -1
  30. package/packages/selector/src/runtime/tokenpilot/locator.ts +1 -1
  31. package/packages/selector/src/runtime/tokenpilot/read-state.ts +23 -2
  32. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +275 -0
  33. package/packages/selector/src/runtime/toolclass.ts +103 -0
  34. package/packages/selector/src/runtime/types.ts +16 -0
  35. package/packages/selector/tests/preset-overlay.host.spec.ts +256 -243
  36. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +7 -7
  37. package/packages/selector/tests/runtime/audit.spec.ts +44 -1
  38. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
  39. package/packages/selector/tests/runtime/code-skeleton.spec.ts +14 -3
  40. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
  41. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
  42. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
  43. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
  44. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +198 -27
  45. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
  46. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
  47. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
  48. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
  49. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +24 -0
  50. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
  51. package/packages/selector/tests/standing-generation.host.spec.ts +642 -631
  52. package/scripts/toolclass-corpus-replay.mjs +273 -0
@@ -1,4 +1,4 @@
1
- .WcpPCW_settingsSection {
1
+ .xdHrtq_settingsSection {
2
2
  max-width: 720px;
3
3
  color: var(--dsw-alias-label-primary);
4
4
  flex-direction: column;
@@ -6,7 +6,7 @@
6
6
  display: flex;
7
7
  }
8
8
 
9
- .WcpPCW_settingsTitle {
9
+ .xdHrtq_settingsTitle {
10
10
  color: var(--dsw-alias-label-primary);
11
11
  margin: 0;
12
12
  font-size: 16px;
@@ -14,24 +14,24 @@
14
14
  line-height: 24px;
15
15
  }
16
16
 
17
- .WcpPCW_settingsDescription {
17
+ .xdHrtq_settingsDescription {
18
18
  color: var(--dsw-alias-label-tertiary);
19
19
  margin: 0;
20
20
  font-size: 14px;
21
21
  line-height: 22px;
22
22
  }
23
23
 
24
- .WcpPCW_root {
24
+ .xdHrtq_root {
25
25
  width: 100%;
26
26
  }
27
27
 
28
- .WcpPCW_profileGrid {
28
+ .xdHrtq_profileGrid {
29
29
  grid-template-columns: repeat(2, minmax(0, 1fr));
30
30
  gap: 10px;
31
31
  display: grid;
32
32
  }
33
33
 
34
- .WcpPCW_profileCard {
34
+ .xdHrtq_profileCard {
35
35
  border: 1px solid var(--dsw-alias-border-l2);
36
36
  min-height: 112px;
37
37
  color: var(--dsw-alias-label-primary);
@@ -45,41 +45,41 @@
45
45
  display: flex;
46
46
  }
47
47
 
48
- .WcpPCW_profileCard:hover:not(:disabled) {
48
+ .xdHrtq_profileCard:hover:not(:disabled) {
49
49
  background: var(--dsw-alias-interactive-bg-hover);
50
50
  }
51
51
 
52
- .WcpPCW_profileCard[aria-pressed="true"] {
52
+ .xdHrtq_profileCard[aria-pressed="true"] {
53
53
  border-color: var(--dsw-alias-label-primary);
54
54
  }
55
55
 
56
- .WcpPCW_profileCard:active:not(:disabled) {
56
+ .xdHrtq_profileCard:active:not(:disabled) {
57
57
  transform: scale(.99);
58
58
  }
59
59
 
60
- .WcpPCW_profileCard:disabled {
60
+ .xdHrtq_profileCard:disabled {
61
61
  cursor: default;
62
62
  opacity: .6;
63
63
  }
64
64
 
65
- .WcpPCW_profileCard:focus-visible {
65
+ .xdHrtq_profileCard:focus-visible {
66
66
  outline-offset: 2px;
67
67
  outline: 2px solid;
68
68
  }
69
69
 
70
- .WcpPCW_profileCardTop {
70
+ .xdHrtq_profileCardTop {
71
71
  align-items: center;
72
72
  gap: 8px;
73
73
  display: flex;
74
74
  }
75
75
 
76
- .WcpPCW_profileCardTitle {
76
+ .xdHrtq_profileCardTitle {
77
77
  font-size: 14px;
78
78
  font-weight: 500;
79
79
  line-height: 20px;
80
80
  }
81
81
 
82
- .WcpPCW_profileCurrent {
82
+ .xdHrtq_profileCurrent {
83
83
  background: var(--dsw-alias-label-primary);
84
84
  color: var(--dsw-alias-bg-base);
85
85
  border-radius: 999px;
@@ -88,13 +88,13 @@
88
88
  line-height: 14px;
89
89
  }
90
90
 
91
- .WcpPCW_profileCardDetail {
91
+ .xdHrtq_profileCardDetail {
92
92
  color: var(--dsw-alias-label-secondary);
93
93
  font-size: 12px;
94
94
  line-height: 18px;
95
95
  }
96
96
 
97
- .WcpPCW_button {
97
+ .xdHrtq_button {
98
98
  border: 1px solid var(--dsw-alias-border-l2);
99
99
  width: 100%;
100
100
  min-height: 34px;
@@ -109,27 +109,27 @@
109
109
  display: flex;
110
110
  }
111
111
 
112
- .WcpPCW_button:hover {
112
+ .xdHrtq_button:hover {
113
113
  background: var(--dsw-alias-interactive-bg-hover);
114
114
  }
115
115
 
116
- .WcpPCW_button:disabled {
116
+ .xdHrtq_button:disabled {
117
117
  cursor: default;
118
118
  opacity: .6;
119
119
  }
120
120
 
121
- .WcpPCW_copy {
121
+ .xdHrtq_copy {
122
122
  flex: 1;
123
123
  min-width: 0;
124
124
  }
125
125
 
126
- .WcpPCW_label {
126
+ .xdHrtq_label {
127
127
  color: var(--dsw-alias-label-tertiary);
128
128
  font-size: 11px;
129
129
  line-height: 14px;
130
130
  }
131
131
 
132
- .WcpPCW_value {
132
+ .xdHrtq_value {
133
133
  text-overflow: ellipsis;
134
134
  white-space: nowrap;
135
135
  font-size: 13px;
@@ -137,24 +137,24 @@
137
137
  overflow: hidden;
138
138
  }
139
139
 
140
- .WcpPCW_chevron {
140
+ .xdHrtq_chevron {
141
141
  color: var(--dsw-alias-label-secondary);
142
142
  flex: none;
143
143
  }
144
144
 
145
- .WcpPCW_menuCopy {
145
+ .xdHrtq_menuCopy {
146
146
  flex-direction: column;
147
147
  gap: 2px;
148
148
  min-width: 220px;
149
149
  display: flex;
150
150
  }
151
151
 
152
- .WcpPCW_menuTitle {
152
+ .xdHrtq_menuTitle {
153
153
  font-size: 13px;
154
154
  line-height: 17px;
155
155
  }
156
156
 
157
- .WcpPCW_menuDetail {
157
+ .xdHrtq_menuDetail {
158
158
  white-space: normal;
159
159
  max-width: 290px;
160
160
  color: var(--dsw-alias-label-tertiary);
@@ -162,61 +162,61 @@
162
162
  line-height: 15px;
163
163
  }
164
164
 
165
- .WcpPCW_error {
165
+ .xdHrtq_error {
166
166
  color: var(--dsw-alias-state-error-primary);
167
167
  padding: 4px 8px 0;
168
168
  font-size: 11px;
169
169
  line-height: 15px;
170
170
  }
171
171
 
172
- .WcpPCW_unavailable {
172
+ .xdHrtq_unavailable {
173
173
  color: var(--dsw-alias-label-tertiary);
174
174
  padding: 4px 8px 0;
175
175
  font-size: 11px;
176
176
  line-height: 15px;
177
177
  }
178
178
 
179
- .WcpPCW_settingsHint {
179
+ .xdHrtq_settingsHint {
180
180
  color: var(--dsw-alias-label-secondary);
181
181
  padding: 4px 8px 0;
182
182
  font-size: 11px;
183
183
  line-height: 15px;
184
184
  }
185
185
 
186
- .WcpPCW_pricing {
186
+ .xdHrtq_pricing {
187
187
  color: var(--dsw-alias-label-tertiary);
188
188
  padding: 4px 8px 0;
189
189
  font-size: 11px;
190
190
  line-height: 15px;
191
191
  }
192
192
 
193
- .WcpPCW_custom {
193
+ .xdHrtq_custom {
194
194
  border-top: 1px solid var(--dsw-alias-border-l2);
195
195
  margin-top: 16px;
196
196
  padding: 16px 0 0;
197
197
  }
198
198
 
199
- .WcpPCW_customTitle {
199
+ .xdHrtq_customTitle {
200
200
  margin: 0 0 6px;
201
201
  font-size: 13px;
202
202
  line-height: 17px;
203
203
  }
204
204
 
205
- .WcpPCW_customNote {
205
+ .xdHrtq_customNote {
206
206
  color: var(--dsw-alias-label-tertiary);
207
207
  margin: 4px 0;
208
208
  font-size: 11px;
209
209
  line-height: 15px;
210
210
  }
211
211
 
212
- .WcpPCW_stage {
212
+ .xdHrtq_stage {
213
213
  border: 0;
214
214
  border-top: 1px solid var(--dsw-alias-border-l2);
215
215
  margin: 12px 0 0;
216
216
  padding: 12px 0 0;
217
217
  }
218
218
 
219
- .WcpPCW_stageToggle {
219
+ .xdHrtq_stageToggle {
220
220
  align-items: center;
221
221
  gap: 6px;
222
222
  font-size: 12px;
@@ -224,13 +224,13 @@
224
224
  display: inline-flex;
225
225
  }
226
226
 
227
- .WcpPCW_fieldGrid {
227
+ .xdHrtq_fieldGrid {
228
228
  grid-template-columns: 1fr;
229
229
  gap: 8px;
230
230
  display: grid;
231
231
  }
232
232
 
233
- .WcpPCW_field {
233
+ .xdHrtq_field {
234
234
  min-width: 0;
235
235
  color: var(--dsw-alias-label-secondary);
236
236
  flex-direction: column;
@@ -241,7 +241,7 @@
241
241
  display: flex;
242
242
  }
243
243
 
244
- .WcpPCW_field input, .WcpPCW_field select {
244
+ .xdHrtq_field input, .xdHrtq_field select {
245
245
  box-sizing: border-box;
246
246
  border: 1px solid var(--dsw-alias-border-l2);
247
247
  background: var(--dsw-alias-bg-base);
@@ -253,19 +253,19 @@
253
253
  padding: 4px 6px;
254
254
  }
255
255
 
256
- .WcpPCW_field input:focus-visible, .WcpPCW_field select:focus-visible, .WcpPCW_actions button:focus-visible {
256
+ .xdHrtq_field input:focus-visible, .xdHrtq_field select:focus-visible, .xdHrtq_actions button:focus-visible {
257
257
  outline-offset: 2px;
258
258
  outline: 2px solid;
259
259
  }
260
260
 
261
- .WcpPCW_actions {
261
+ .xdHrtq_actions {
262
262
  flex-wrap: wrap;
263
263
  gap: 8px;
264
264
  margin-top: 10px;
265
265
  display: flex;
266
266
  }
267
267
 
268
- .WcpPCW_actions button {
268
+ .xdHrtq_actions button {
269
269
  border: 1px solid var(--dsw-alias-border-l2);
270
270
  min-height: 30px;
271
271
  color: var(--dsw-alias-label-primary);
@@ -274,34 +274,34 @@
274
274
  padding: 4px 8px;
275
275
  }
276
276
 
277
- .WcpPCW_actions button:active:not(:disabled) {
277
+ .xdHrtq_actions button:active:not(:disabled) {
278
278
  transform: scale(.98);
279
279
  }
280
280
 
281
- .WcpPCW_actions button:disabled {
281
+ .xdHrtq_actions button:disabled {
282
282
  opacity: .6;
283
283
  }
284
284
 
285
285
  @media (width <= 560px) {
286
- .WcpPCW_profileGrid {
286
+ .xdHrtq_profileGrid {
287
287
  grid-template-columns: 1fr;
288
288
  }
289
289
  }
290
290
 
291
- .WcpPCW_autoCompact {
291
+ .xdHrtq_autoCompact {
292
292
  border: 1px solid #80808059;
293
293
  border-radius: 8px;
294
294
  margin-top: 24px;
295
295
  padding: 16px;
296
296
  }
297
297
 
298
- .WcpPCW_autoCompactTitle {
298
+ .xdHrtq_autoCompactTitle {
299
299
  margin: 0 0 8px;
300
300
  font-size: 15px;
301
301
  font-weight: 600;
302
302
  }
303
303
 
304
- .WcpPCW_autoCompactRisk {
304
+ .xdHrtq_autoCompactRisk {
305
305
  opacity: .9;
306
306
  margin: 8px 0 0;
307
307
  font-size: 12px;
@@ -1,136 +1,136 @@
1
- import { createUserMessage } from "@deepseek-ai/dsh-llm";
2
- //#region src/runtime/session-events.ts
3
- /**
4
- * Read one immutable Session event snapshot.
5
- *
6
- * Harness rc.2 exposed `events`; Alpha 5 replaced it with the public
7
- * `snapshotEvents()` method. Prefer the new API when present and retain the
8
- * old accessor as the compatibility fallback for already-supported hosts.
9
- */
10
- function sessionEvents(session) {
11
- const snapshot = session.snapshotEvents;
12
- return typeof snapshot === "function" ? snapshot.call(session) : session.events;
13
- }
14
- //#endregion
15
- //#region src/runtime/tail-trim.ts
16
- /** Plugin-owned TailTrim publication protocol over official Session events. */
17
- const TAIL_TRIM_REF_PATTERN = /^session:\/\/([^/]+)\/tailtrim\/(\d+)$/;
18
- const MAX_ROOTS = 64;
19
- const MAX_STUB_CODE_POINTS = 1024;
20
- /** Build a same-session reference keyed by the standard prune event. */
21
- function tailTrimRef(sessionId, manifestSeq) {
22
- return `session://${sessionId}/tailtrim/${String(manifestSeq)}`;
23
- }
24
- /** Parse one exact TailTrim reference. */
25
- function parseTailTrimRef(ref) {
26
- const match = TAIL_TRIM_REF_PATTERN.exec(ref);
27
- if (match === null) return null;
28
- const manifestSeq = Number(match[2]);
29
- if (!Number.isSafeInteger(manifestSeq) || manifestSeq < 0) return null;
30
- return {
31
- sessionId: match[1] ?? "",
32
- manifestSeq
33
- };
34
- }
35
- /** Build the fixed bounded model-visible TailTrim stub. */
36
- function tailTrimStub(ref, toolNames, sourceEventSeqs) {
37
- const stub = [
38
- "[TailTrim: completed tool-call group]",
39
- `ref: ${ref}`,
40
- `tools: ${toolNames.join(", ")}`,
41
- `source_event_seqs: ${sourceEventSeqs.join(", ")}`,
42
- "use context_compression_retrieve with this TailTrim ref if needed"
43
- ].join("\n");
44
- return Array.from(stub).length <= MAX_STUB_CODE_POINTS ? stub : null;
45
- }
46
- /** Wrap a TailTrim stub in the one user message used for range replacement. */
47
- function tailTrimMessage(stub) {
48
- return createUserMessage({
49
- content: [{
50
- type: "text",
51
- text: stub
52
- }],
53
- source: {
54
- kind: "plugin",
55
- plugin: "dsh-context-compression-improved-runtime"
56
- }
57
- });
58
- }
59
- /** Validate the standard prune, adjacent replacement, append roots and stub. */
60
- function validatePublishedTailTrim(session, manifestSeq) {
61
- const events = sessionEvents(session);
62
- const manifest = events[manifestSeq];
63
- if (manifest?.type !== "compaction/prune" || manifest.data.shadowedSeqs.length < 2 || manifest.data.shadowedSeqs.length > MAX_ROOTS || manifest.data.shadowedSeqs[0] !== manifest.data.shadowedRange.start || manifest.data.shadowedSeqs.at(-1) !== manifest.data.shadowedRange.end || new Set(manifest.data.shadowedSeqs).size !== manifest.data.shadowedSeqs.length) return null;
64
- const replacement = events[manifestSeq + 1];
65
- if (replacement?.type !== "user/message" || replacement.seq !== manifest.seq + 1 || replacement.data.source.kind !== "plugin" || replacement.data.source.plugin !== "dsh-context-compression-improved-runtime" || replacement.surfaceOp === void 0 || replacement.surfaceOp === "append" || replacement.surfaceOp.start !== manifest.data.shadowedRange.start || replacement.surfaceOp.end !== manifest.data.shadowedRange.end || !sameNumbers(replacement.sourceEventSeqs, [manifest.seq, ...manifest.data.shadowedSeqs]) || replacement.data.content.length !== 1 || replacement.data.content[0]?.type !== "text") return null;
66
- const tracedRoots = manifest.data.shadowedSeqs.map((seq) => uniqueAppendRoot(session, seq, manifestSeq));
67
- if (tracedRoots.some((root) => root === null)) return null;
68
- const sourceEventSeqs = tracedRoots;
69
- if (new Set(sourceEventSeqs).size !== sourceEventSeqs.length) return null;
70
- const roots = sourceEventSeqs.map((seq) => events[seq]);
71
- if (roots.some((event) => event === void 0)) return null;
72
- const typedRoots = roots;
73
- if (!validRootGroup(typedRoots, manifestSeq)) return null;
74
- const assistant = typedRoots[0];
75
- if (assistant?.type !== "assistant/message") return null;
76
- const toolNames = assistant.data.message.content.map((block) => {
77
- if (block.type !== "tool-call") throw new Error("unreachable");
78
- return block.name;
79
- });
80
- const ref = tailTrimRef(String(session.id), manifestSeq);
81
- const stub = tailTrimStub(ref, toolNames, sourceEventSeqs);
82
- if (stub === null || replacement.data.content[0].text !== stub) return null;
83
- return {
84
- manifest,
85
- replacement,
86
- roots: typedRoots,
87
- toolNames,
88
- ref,
89
- stub
90
- };
91
- }
92
- function uniqueAppendRoot(session, seq, beforeSeq) {
93
- const events = sessionEvents(session);
94
- const pending = [{
95
- seq,
96
- depth: 0
97
- }];
98
- const visited = /* @__PURE__ */ new Set();
99
- const roots = /* @__PURE__ */ new Set();
100
- while (pending.length > 0) {
101
- const next = pending.pop();
102
- if (next === void 0 || next.depth > MAX_ROOTS || visited.has(next.seq)) continue;
103
- if (!Number.isSafeInteger(next.seq) || next.seq < 0 || next.seq >= beforeSeq) return null;
104
- visited.add(next.seq);
105
- if (visited.size > MAX_ROOTS) return null;
106
- const event = events[next.seq];
107
- if (event === void 0 || event.type !== "assistant/message" && event.type !== "tool/result") return null;
108
- if (event.surfaceOp === "append") roots.add(event.seq);
109
- else if (typeof event.surfaceOp === "object") {
110
- const sources = event.sourceEventSeqs;
111
- if (sources === void 0 || sources.length === 0) return null;
112
- for (const source of sources) pending.push({
113
- seq: source,
114
- depth: next.depth + 1
115
- });
116
- } else return null;
117
- if (roots.size > 1) return null;
118
- }
119
- return roots.size === 1 ? [...roots][0] ?? null : null;
120
- }
121
- function validRootGroup(roots, manifestSeq) {
122
- const assistant = roots[0];
123
- if (assistant?.type !== "assistant/message" || assistant.seq >= manifestSeq || assistant.surfaceOp !== "append" || assistant.data.interrupted === true || assistant.data.message.content.length === 0 || assistant.data.message.content.some((block) => block.type !== "tool-call")) return false;
124
- const callIds = assistant.data.message.content.map((call) => call.id);
125
- if (new Set(callIds).size !== callIds.length || roots.length !== callIds.length + 1) return false;
126
- for (const [index, root] of roots.slice(1).entries()) {
127
- if (root.type !== "tool/result" || root.seq >= manifestSeq || root.surfaceOp !== "append") return false;
128
- if (root.data.message.content[0].isError === true || root.data.error !== void 0 || root.data.turn !== assistant.data.turn || root.data.step !== assistant.data.step || String(root.data.message.source.callId) !== String(callIds[index])) return false;
129
- }
130
- return true;
131
- }
132
- function sameNumbers(left, right) {
133
- return left !== void 0 && left.length === right.length && left.every((value, index) => value === right[index]);
134
- }
135
- //#endregion
136
- export { validatePublishedTailTrim as a, tailTrimStub as i, tailTrimMessage as n, sessionEvents as o, tailTrimRef as r, parseTailTrimRef as t };
1
+ import { createUserMessage } from "@deepseek-ai/dsh-llm";
2
+ //#region src/runtime/session-events.ts
3
+ /**
4
+ * Read one immutable Session event snapshot.
5
+ *
6
+ * Harness rc.2 exposed `events`; Alpha 5 replaced it with the public
7
+ * `snapshotEvents()` method. Prefer the new API when present and retain the
8
+ * old accessor as the compatibility fallback for already-supported hosts.
9
+ */
10
+ function sessionEvents(session) {
11
+ const snapshot = session.snapshotEvents;
12
+ return typeof snapshot === "function" ? snapshot.call(session) : session.events;
13
+ }
14
+ //#endregion
15
+ //#region src/runtime/tail-trim.ts
16
+ /** Plugin-owned TailTrim publication protocol over official Session events. */
17
+ const TAIL_TRIM_REF_PATTERN = /^session:\/\/([^/]+)\/tailtrim\/(\d+)$/;
18
+ const MAX_ROOTS = 64;
19
+ const MAX_STUB_CODE_POINTS = 1024;
20
+ /** Build a same-session reference keyed by the standard prune event. */
21
+ function tailTrimRef(sessionId, manifestSeq) {
22
+ return `session://${sessionId}/tailtrim/${String(manifestSeq)}`;
23
+ }
24
+ /** Parse one exact TailTrim reference. */
25
+ function parseTailTrimRef(ref) {
26
+ const match = TAIL_TRIM_REF_PATTERN.exec(ref);
27
+ if (match === null) return null;
28
+ const manifestSeq = Number(match[2]);
29
+ if (!Number.isSafeInteger(manifestSeq) || manifestSeq < 0) return null;
30
+ return {
31
+ sessionId: match[1] ?? "",
32
+ manifestSeq
33
+ };
34
+ }
35
+ /** Build the fixed bounded model-visible TailTrim stub. */
36
+ function tailTrimStub(ref, toolNames, sourceEventSeqs) {
37
+ const stub = [
38
+ "[TailTrim: completed tool-call group]",
39
+ `ref: ${ref}`,
40
+ `tools: ${toolNames.join(", ")}`,
41
+ `source_event_seqs: ${sourceEventSeqs.join(", ")}`,
42
+ "use context_compression_retrieve with this TailTrim ref if needed"
43
+ ].join("\n");
44
+ return Array.from(stub).length <= MAX_STUB_CODE_POINTS ? stub : null;
45
+ }
46
+ /** Wrap a TailTrim stub in the one user message used for range replacement. */
47
+ function tailTrimMessage(stub) {
48
+ return createUserMessage({
49
+ content: [{
50
+ type: "text",
51
+ text: stub
52
+ }],
53
+ source: {
54
+ kind: "plugin",
55
+ plugin: "dsh-context-compression-improved-runtime"
56
+ }
57
+ });
58
+ }
59
+ /** Validate the standard prune, adjacent replacement, append roots and stub. */
60
+ function validatePublishedTailTrim(session, manifestSeq) {
61
+ const events = sessionEvents(session);
62
+ const manifest = events[manifestSeq];
63
+ if (manifest?.type !== "compaction/prune" || manifest.data.shadowedSeqs.length < 2 || manifest.data.shadowedSeqs.length > MAX_ROOTS || manifest.data.shadowedSeqs[0] !== manifest.data.shadowedRange.start || manifest.data.shadowedSeqs.at(-1) !== manifest.data.shadowedRange.end || new Set(manifest.data.shadowedSeqs).size !== manifest.data.shadowedSeqs.length) return null;
64
+ const replacement = events[manifestSeq + 1];
65
+ if (replacement?.type !== "user/message" || replacement.seq !== manifest.seq + 1 || replacement.data.source.kind !== "plugin" || replacement.data.source.plugin !== "dsh-context-compression-improved-runtime" || replacement.surfaceOp === void 0 || replacement.surfaceOp === "append" || replacement.surfaceOp.start !== manifest.data.shadowedRange.start || replacement.surfaceOp.end !== manifest.data.shadowedRange.end || !sameNumbers(replacement.sourceEventSeqs, [manifest.seq, ...manifest.data.shadowedSeqs]) || replacement.data.content.length !== 1 || replacement.data.content[0]?.type !== "text") return null;
66
+ const tracedRoots = manifest.data.shadowedSeqs.map((seq) => uniqueAppendRoot(session, seq, manifestSeq));
67
+ if (tracedRoots.some((root) => root === null)) return null;
68
+ const sourceEventSeqs = tracedRoots;
69
+ if (new Set(sourceEventSeqs).size !== sourceEventSeqs.length) return null;
70
+ const roots = sourceEventSeqs.map((seq) => events[seq]);
71
+ if (roots.some((event) => event === void 0)) return null;
72
+ const typedRoots = roots;
73
+ if (!validRootGroup(typedRoots, manifestSeq)) return null;
74
+ const assistant = typedRoots[0];
75
+ if (assistant?.type !== "assistant/message") return null;
76
+ const toolNames = assistant.data.message.content.map((block) => {
77
+ if (block.type !== "tool-call") throw new Error("unreachable");
78
+ return block.name;
79
+ });
80
+ const ref = tailTrimRef(String(session.id), manifestSeq);
81
+ const stub = tailTrimStub(ref, toolNames, sourceEventSeqs);
82
+ if (stub === null || replacement.data.content[0].text !== stub) return null;
83
+ return {
84
+ manifest,
85
+ replacement,
86
+ roots: typedRoots,
87
+ toolNames,
88
+ ref,
89
+ stub
90
+ };
91
+ }
92
+ function uniqueAppendRoot(session, seq, beforeSeq) {
93
+ const events = sessionEvents(session);
94
+ const pending = [{
95
+ seq,
96
+ depth: 0
97
+ }];
98
+ const visited = /* @__PURE__ */ new Set();
99
+ const roots = /* @__PURE__ */ new Set();
100
+ while (pending.length > 0) {
101
+ const next = pending.pop();
102
+ if (next === void 0 || next.depth > MAX_ROOTS || visited.has(next.seq)) continue;
103
+ if (!Number.isSafeInteger(next.seq) || next.seq < 0 || next.seq >= beforeSeq) return null;
104
+ visited.add(next.seq);
105
+ if (visited.size > MAX_ROOTS) return null;
106
+ const event = events[next.seq];
107
+ if (event === void 0 || event.type !== "assistant/message" && event.type !== "tool/result") return null;
108
+ if (event.surfaceOp === "append") roots.add(event.seq);
109
+ else if (typeof event.surfaceOp === "object") {
110
+ const sources = event.sourceEventSeqs;
111
+ if (sources === void 0 || sources.length === 0) return null;
112
+ for (const source of sources) pending.push({
113
+ seq: source,
114
+ depth: next.depth + 1
115
+ });
116
+ } else return null;
117
+ if (roots.size > 1) return null;
118
+ }
119
+ return roots.size === 1 ? [...roots][0] ?? null : null;
120
+ }
121
+ function validRootGroup(roots, manifestSeq) {
122
+ const assistant = roots[0];
123
+ if (assistant?.type !== "assistant/message" || assistant.seq >= manifestSeq || assistant.surfaceOp !== "append" || assistant.data.interrupted === true || assistant.data.message.content.length === 0 || assistant.data.message.content.some((block) => block.type !== "tool-call")) return false;
124
+ const callIds = assistant.data.message.content.map((call) => call.id);
125
+ if (new Set(callIds).size !== callIds.length || roots.length !== callIds.length + 1) return false;
126
+ for (const [index, root] of roots.slice(1).entries()) {
127
+ if (root.type !== "tool/result" || root.seq >= manifestSeq || root.surfaceOp !== "append") return false;
128
+ if (root.data.message.content[0].isError === true || root.data.error !== void 0 || root.data.turn !== assistant.data.turn || root.data.step !== assistant.data.step || String(root.data.message.source.callId) !== String(callIds[index])) return false;
129
+ }
130
+ return true;
131
+ }
132
+ function sameNumbers(left, right) {
133
+ return left !== void 0 && left.length === right.length && left.every((value, index) => value === right[index]);
134
+ }
135
+ //#endregion
136
+ export { validatePublishedTailTrim as a, tailTrimStub as i, tailTrimMessage as n, sessionEvents as o, tailTrimRef as r, parseTailTrimRef as t };
@@ -69,7 +69,7 @@
69
69
  "author": "WilliamShi666",
70
70
  "engines": {
71
71
  "node": "^22.19.0 || >=24.0.0",
72
- "dsh": ">=0.1.2-rc.1 <0.2.0-0"
72
+ "dsh": ">=0.1.2-rc.1 <0.1.3-0"
73
73
  },
74
74
  "keywords": [
75
75
  "deepseek",
@@ -96,12 +96,12 @@
96
96
  "@deepseek-ai/dsh-command-compact": ">=0.1.1-rc.2 <0.2.0",
97
97
  "@deepseek-ai/dsh-compaction": ">=0.1.1-rc.2 <0.2.0",
98
98
  "@deepseek-ai/dsh-compaction-basic": ">=0.1.1-rc.2 <0.2.0",
99
- "@deepseek-ai/dsh-client-locale": ">=0.1.2-rc.1 <0.2.0-0",
100
- "@deepseek-ai/dsh-client-runtime": ">=0.1.2-rc.1 <0.2.0-0",
101
- "@deepseek-ai/dsh-client-ui-primitives": ">=0.1.2-rc.1 <0.2.0-0",
102
- "@deepseek-ai/dsh-client-ui-settings": ">=0.1.2-rc.1 <0.2.0-0",
103
- "@deepseek-ai/dsh-client-ui-slots": ">=0.1.2-rc.1 <0.2.0-0",
104
- "@deepseek-ai/dsh-client-ui-workspace": ">=0.1.2-rc.1 <0.2.0-0",
99
+ "@deepseek-ai/dsh-client-locale": ">=0.1.2-rc.1 <0.1.3-0",
100
+ "@deepseek-ai/dsh-client-runtime": ">=0.1.1-rc.2 <0.2.0",
101
+ "@deepseek-ai/dsh-client-ui-primitives": ">=0.1.2-rc.1 <0.1.3-0",
102
+ "@deepseek-ai/dsh-client-ui-settings": ">=0.1.2-rc.1 <0.1.3-0",
103
+ "@deepseek-ai/dsh-client-ui-slots": ">=0.1.2-rc.1 <0.1.3-0",
104
+ "@deepseek-ai/dsh-client-ui-workspace": ">=0.1.2-rc.1 <0.1.3-0",
105
105
  "@deepseek-ai/dsh-invariants": ">=0.1.1-rc.2 <0.2.0",
106
106
  "@deepseek-ai/dsh-llm": ">=0.1.1-rc.2 <0.2.0",
107
107
  "@deepseek-ai/dsh-session": ">=0.1.1-rc.2 <0.2.0",
@@ -111,5 +111,13 @@
111
111
  "@deepseek-ai/dsh-tools": ">=0.1.1-rc.2 <0.2.0",
112
112
  "@deepseek-ai/schemastery": ">=3.18.1 <4",
113
113
  "react": ">=18 <19"
114
+ },
115
+ "devDependencies": {
116
+ "@deepseek-ai/dsh-client-locale": "0.1.2-rc.1",
117
+ "@deepseek-ai/dsh-client-ui-primitives": "0.1.2-rc.1",
118
+ "@deepseek-ai/dsh-client-ui-settings": "0.1.2-rc.1",
119
+ "@deepseek-ai/dsh-client-ui-slots": "0.1.2-rc.1",
120
+ "@deepseek-ai/dsh-client-ui-workspace": "0.1.2-rc.1",
121
+ "@deepseek-ai/dsh-client-runtime": "0.1.1-rc.2"
114
122
  }
115
123
  }