dsh-compaction-weave 0.0.0-stage → 0.3.3
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/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.en.md +136 -0
- package/README.md +139 -2
- package/cordis.patch.yml +19 -0
- package/docs/architecture.md +84 -0
- package/docs/benchmarks/published-0.3.2-rc.1-tests.json +576 -0
- package/docs/benchmarks/rc2-documentation.json +41 -0
- package/docs/benchmarks/rc2-linux-tests.json +590 -0
- package/docs/benchmarks/rc2-live-and-cost.json +746 -0
- package/docs/benchmarks/rc3-host-compatibility.json +2314 -0
- package/docs/benchmarks/rc4-tests.json +617 -0
- package/docs/benchmarks/review35-compression.json +117 -0
- package/docs/benchmarks/v0.3.2-live-four-way.json +3495 -0
- package/docs/benchmarks/v0.3.2-tests.json +590 -0
- package/docs/configuration.md +131 -0
- package/docs/development.md +88 -0
- package/docs/en/architecture.md +75 -0
- package/docs/en/configuration.md +131 -0
- package/docs/en/development.md +86 -0
- package/docs/en/feedback.md +47 -0
- package/docs/en/principles.md +235 -0
- package/docs/en/testing.md +368 -0
- package/docs/en/troubleshooting.md +59 -0
- package/docs/feedback.md +47 -0
- package/docs/images/principles-context.en.svg +77 -0
- package/docs/images/principles-context.zh.svg +77 -0
- package/docs/images/principles-pipeline.en.svg +93 -0
- package/docs/images/principles-pipeline.zh.svg +93 -0
- package/docs/images/principles-recovery.en.svg +46 -0
- package/docs/images/principles-recovery.zh.svg +46 -0
- package/docs/principles.md +235 -0
- package/docs/test-results.json +634 -0
- package/docs/testing.md +358 -0
- package/docs/troubleshooting.md +59 -0
- package/lib/abort.js +13 -0
- package/lib/action-state.js +60 -0
- package/lib/atomic-replace.js +18 -0
- package/lib/attachments.js +76 -0
- package/lib/audit-reject.js +161 -0
- package/lib/audit.js +939 -0
- package/lib/cache-policy.js +71 -0
- package/lib/client.js +696 -0
- package/lib/command-compact.js +38 -0
- package/lib/command-heartbeat.js +61 -0
- package/lib/config.js +596 -0
- package/lib/constraint-scope.js +37 -0
- package/lib/continuation-budget.js +69 -0
- package/lib/continuation-pending.js +57 -0
- package/lib/continuation-progress.js +46 -0
- package/lib/continuation.js +287 -0
- package/lib/engine-entry.js +103 -0
- package/lib/engine.js +2485 -0
- package/lib/failure-diagnostics.js +220 -0
- package/lib/history-source.js +74 -0
- package/lib/history.js +712 -0
- package/lib/idle-compaction.js +64 -0
- package/lib/index.js +611 -0
- package/lib/instruction-source.js +163 -0
- package/lib/journal.js +164 -0
- package/lib/manual-error.js +179 -0
- package/lib/memory-policy.js +383 -0
- package/lib/meter.js +387 -0
- package/lib/observation-mask.js +390 -0
- package/lib/proactive.js +108 -0
- package/lib/progress-facts.js +347 -0
- package/lib/progress-state.js +30 -0
- package/lib/prompt.js +194 -0
- package/lib/reconstruct.js +72 -0
- package/lib/reference-input.js +107 -0
- package/lib/region.js +972 -0
- package/lib/source-provenance.js +58 -0
- package/lib/source-recovery.js +22 -0
- package/lib/source-refs.js +137 -0
- package/lib/state.js +46 -0
- package/lib/storage-path.js +42 -0
- package/lib/summary-budget.js +187 -0
- package/lib/summary-config.js +308 -0
- package/lib/summary-progress.js +56 -0
- package/lib/summary-retry.js +27 -0
- package/lib/summary-timeout.js +68 -0
- package/lib/summary.js +476 -0
- package/lib/tools.js +341 -0
- package/lib/transaction-host.js +252 -0
- package/lib/transaction.js +285 -0
- package/lib/validate.js +503 -0
- package/lib/version.js +3 -0
- package/lib/window.js +120 -0
- package/lib/work-report.js +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +71 -4
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// Summary-model configuration for dsh-compaction-weave.
|
|
2
|
+
//
|
|
3
|
+
// The value is deliberately kept OUT of cordis.patch.yml / the profile manifest:
|
|
4
|
+
// the plugin owns a small JSON file under its storage root, the Settings page
|
|
5
|
+
// writes it through the host RPC below, and the gate merges it into the engine
|
|
6
|
+
// config at each operation boundary. This survives bundle updates and never rewrites shared
|
|
7
|
+
// profile configuration.
|
|
8
|
+
//
|
|
9
|
+
// File: <DSH_HOME>/storages/dsh-compaction-weave/summary-config.json
|
|
10
|
+
// Shape: { version: 1, summarizationProvider, summarizationModel, maxTokens, thresholdRatio, idleCompactionMinutes?, updatedAt }
|
|
11
|
+
//
|
|
12
|
+
// thresholdRatio is the auto-compaction trigger line as a fraction of the model
|
|
13
|
+
// window (0.5–0.95). It is stored here as well so the Settings page can tune it
|
|
14
|
+
// without touching the engine config; null/absent means "use the engine default
|
|
15
|
+
// (0.8)".
|
|
16
|
+
// idleCompactionMinutes is also independent of the model: absence inherits,
|
|
17
|
+
// while zero explicitly disables it. Model-only saves/clears retain its value.
|
|
18
|
+
|
|
19
|
+
import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
20
|
+
import { dirname, join } from 'node:path';
|
|
21
|
+
import { homedir } from 'node:os';
|
|
22
|
+
import { normalizeIdleCompactionMinutes } from './config.js';
|
|
23
|
+
|
|
24
|
+
export const CONFIG_VERSION = 1;
|
|
25
|
+
export const MAX_BODY_BYTES = 64 * 1024;
|
|
26
|
+
const MAX_ID_LENGTH = 200;
|
|
27
|
+
const MAX_TOKENS_LIMIT = 1_000_000;
|
|
28
|
+
export const MIN_THRESHOLD_RATIO = 0.5;
|
|
29
|
+
export const MAX_THRESHOLD_RATIO = 0.95;
|
|
30
|
+
|
|
31
|
+
/** Merge over the original engine row, never over a previous settings override.
|
|
32
|
+
* Clearing an override must restore that row's inherited value immediately. */
|
|
33
|
+
export function mergeSummaryConfig(rawEngine, savedSummary) {
|
|
34
|
+
const config = { ...(rawEngine && typeof rawEngine === 'object' ? rawEngine : {}) };
|
|
35
|
+
if (!savedSummary || typeof savedSummary !== 'object') return config;
|
|
36
|
+
if (savedSummary.provider && savedSummary.model) {
|
|
37
|
+
config.summarizationProvider = savedSummary.provider;
|
|
38
|
+
config.summarizationModel = savedSummary.model;
|
|
39
|
+
if (savedSummary.maxTokens !== null && savedSummary.maxTokens !== undefined) config.maxTokens = savedSummary.maxTokens;
|
|
40
|
+
}
|
|
41
|
+
for (const key of ['summaryRetryCount', 'summaryRetryDelayMs', 'summaryTimeoutMs']) {
|
|
42
|
+
if (Number.isInteger(savedSummary[key])) config[key] = savedSummary[key];
|
|
43
|
+
}
|
|
44
|
+
if (savedSummary.idleCompactionMinutes !== undefined) config.idleCompactionMinutes = normalizeIdleCompactionMinutes(savedSummary.idleCompactionMinutes);
|
|
45
|
+
for (const key of ['minimumReductionRatio', 'thresholdRatio']) {
|
|
46
|
+
if (typeof savedSummary[key] === 'number' && Number.isFinite(savedSummary[key])) config[key] = savedSummary[key];
|
|
47
|
+
}
|
|
48
|
+
return config;
|
|
49
|
+
}
|
|
50
|
+
// Provider/model ids are opaque routing keys, not paths. Keep them printable
|
|
51
|
+
// and slash/colon/@ friendly; reject whitespace and control characters.
|
|
52
|
+
const ID_RE = /^[A-Za-z0-9@][A-Za-z0-9._/@:+-]*$/;
|
|
53
|
+
|
|
54
|
+
export class SummaryConfigError extends Error {
|
|
55
|
+
constructor(code, message) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = 'SummaryConfigError';
|
|
58
|
+
this.code = code;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Default location: the plugin's own storage root, next to state/journal. */
|
|
63
|
+
export function defaultSummaryConfigPath() {
|
|
64
|
+
const home = process.env.DSH_HOME ?? join(homedir(), '.dsh');
|
|
65
|
+
return join(home, 'storages', 'dsh-compaction-weave', 'summary-config.json');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function textOf(value) {
|
|
69
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function maxTokensOf(value) {
|
|
73
|
+
if (value === undefined || value === null) return null;
|
|
74
|
+
if (typeof value === 'string' && value.trim() === '') return null;
|
|
75
|
+
const number = typeof value === 'number' ? value : Number(value);
|
|
76
|
+
if (!Number.isInteger(number) || number < 1 || number > MAX_TOKENS_LIMIT) return null;
|
|
77
|
+
return number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Parse a trigger ratio. Returns null when the value is absent/blank, and a
|
|
82
|
+
* rounded fraction when it is a finite number inside the allowed range.
|
|
83
|
+
* Callers that need "invalid vs absent" distinction use validateSummaryConfig.
|
|
84
|
+
*/
|
|
85
|
+
function thresholdRatioOf(value) {
|
|
86
|
+
if (value === undefined || value === null) return null;
|
|
87
|
+
if (typeof value === 'string' && value.trim() === '') return null;
|
|
88
|
+
const number = typeof value === 'number' ? value : Number(value);
|
|
89
|
+
if (!Number.isFinite(number) || number < MIN_THRESHOLD_RATIO || number > MAX_THRESHOLD_RATIO) return null;
|
|
90
|
+
// The slider speaks whole percents; round away float tails like 0.8000000000000001.
|
|
91
|
+
return Math.round(number * 100) / 100;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function minimumReductionRatioOf(value) {
|
|
95
|
+
if (value === undefined || value === null || (typeof value === 'string' && value.trim() === '')) return null;
|
|
96
|
+
const number = typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : NaN;
|
|
97
|
+
return Number.isFinite(number) && number >= 0.05 && number <= 0.9 ? Math.round(number * 100) / 100 : null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function invalidThresholdRatioInput(input) {
|
|
101
|
+
const value = input?.thresholdRatio;
|
|
102
|
+
if (value === undefined || value === null) return false;
|
|
103
|
+
if (typeof value === 'string' && value.trim() === '') return false;
|
|
104
|
+
return thresholdRatioOf(value) === null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const SUMMARY_RETRY_SETTINGS = Object.freeze({
|
|
108
|
+
summaryRetryCount: {min:0,max:4},
|
|
109
|
+
summaryRetryDelayMs: {min:0,max:30000},
|
|
110
|
+
summaryTimeoutMs: {min:5000,max:3600000},
|
|
111
|
+
});
|
|
112
|
+
function retrySettingsOf(input) {
|
|
113
|
+
const value={};
|
|
114
|
+
for(const [key,range] of Object.entries(SUMMARY_RETRY_SETTINGS)) {
|
|
115
|
+
const raw=input?.[key];
|
|
116
|
+
if(raw===undefined||raw===null||typeof raw==='string'&&raw.trim()==='')continue;
|
|
117
|
+
const n=typeof raw==='number'?raw:typeof raw==='string'?Number(raw):NaN;
|
|
118
|
+
if(!Number.isInteger(n)||n<range.min||n>range.max)return {ok:false,code:'INVALID_'+key.replace(/[A-Z]/g,m=>'_'+m).toUpperCase(),message:`${key} must be an integer between ${range.min} and ${range.max}`};
|
|
119
|
+
value[key]=n;
|
|
120
|
+
}
|
|
121
|
+
return {ok:true,value};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function idleSettingsOf(input) {
|
|
125
|
+
const raw = input?.idleCompactionMinutes;
|
|
126
|
+
// Absence inherits the engine config; an explicit zero disables the feature.
|
|
127
|
+
if (raw === undefined) return { ok: true, value: {} };
|
|
128
|
+
const number = typeof raw === 'string' && /^\d+$/.test(raw.trim()) ? Number(raw.trim()) : raw;
|
|
129
|
+
try {
|
|
130
|
+
return { ok: true, value: { idleCompactionMinutes: normalizeIdleCompactionMinutes(number) } };
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return { ok: false, code: 'INVALID_IDLE_COMPACTION_MINUTES', message: error.message };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Validate a settings submission. Both ids empty = clear the model override
|
|
138
|
+
* (follow the session model); one empty = reject rather than guess.
|
|
139
|
+
* thresholdRatio and idleCompactionMinutes are independent of the model override.
|
|
140
|
+
*/
|
|
141
|
+
export function validateSummaryConfig(input) {
|
|
142
|
+
const retry=retrySettingsOf(input);if(!retry.ok)return retry;
|
|
143
|
+
const idle = idleSettingsOf(input);
|
|
144
|
+
if (!idle.ok) return idle;
|
|
145
|
+
const provider = textOf(input?.provider ?? input?.summarizationProvider);
|
|
146
|
+
const model = textOf(input?.model ?? input?.summarizationModel);
|
|
147
|
+
const maxTokens = maxTokensOf(input?.maxTokens);
|
|
148
|
+
const thresholdRatio = thresholdRatioOf(input?.thresholdRatio);
|
|
149
|
+
const minimumReductionRatio = minimumReductionRatioOf(input?.minimumReductionRatio);
|
|
150
|
+
if (input?.minimumReductionRatio !== undefined && input?.minimumReductionRatio !== null && !(typeof input.minimumReductionRatio === 'string' && input.minimumReductionRatio.trim() === '') && minimumReductionRatio === null) return {ok:false,code:'INVALID_MINIMUM_REDUCTION_RATIO',message:'minimumReductionRatio must be a number between 0.05 and 0.9'};
|
|
151
|
+
if (maxTokens === null && input?.maxTokens !== undefined && input?.maxTokens !== null && String(input.maxTokens).trim() !== '') {
|
|
152
|
+
return { ok: false, code: 'INVALID_MAX_TOKENS', message: `maxTokens must be an integer between 1 and ${MAX_TOKENS_LIMIT}` };
|
|
153
|
+
}
|
|
154
|
+
if (invalidThresholdRatioInput(input)) {
|
|
155
|
+
return {
|
|
156
|
+
ok: false,
|
|
157
|
+
code: 'INVALID_THRESHOLD_RATIO',
|
|
158
|
+
message: `thresholdRatio must be a number between ${MIN_THRESHOLD_RATIO} and ${MAX_THRESHOLD_RATIO}`,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (!provider && !model) {
|
|
162
|
+
return { ok: true, value: { provider: '', model: '', maxTokens, thresholdRatio, ...retry.value, ...idle.value, ...(minimumReductionRatio === null ? {} : {minimumReductionRatio}) } };
|
|
163
|
+
}
|
|
164
|
+
if (!provider || !model) {
|
|
165
|
+
return { ok: false, code: 'SUMMARY_PAIR_REQUIRED', message: 'provider and model must be set together or left empty together' };
|
|
166
|
+
}
|
|
167
|
+
if (provider.length > MAX_ID_LENGTH || !ID_RE.test(provider)) {
|
|
168
|
+
return { ok: false, code: 'INVALID_PROVIDER', message: 'provider contains unsupported characters or is too long' };
|
|
169
|
+
}
|
|
170
|
+
if (model.length > MAX_ID_LENGTH || !ID_RE.test(model)) {
|
|
171
|
+
return { ok: false, code: 'INVALID_MODEL', message: 'model contains unsupported characters or is too long' };
|
|
172
|
+
}
|
|
173
|
+
return { ok: true, value: { provider, model, maxTokens, thresholdRatio, ...retry.value, ...idle.value, ...(minimumReductionRatio === null ? {} : {minimumReductionRatio}) } };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Strict engine reads distinguish an absent override from a broken file.
|
|
177
|
+
* The settings UI can still open a damaged file and replace it with a save. */
|
|
178
|
+
export function readSummaryConfig(file = defaultSummaryConfigPath(), {strict = false} = {}) {
|
|
179
|
+
try {
|
|
180
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
181
|
+
if (strict && (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) ||
|
|
182
|
+
(parsed.version !== undefined && parsed.version !== CONFIG_VERSION))) throw new Error('Invalid settings document.');
|
|
183
|
+
if (!parsed || typeof parsed !== 'object') return null;
|
|
184
|
+
if (strict) {
|
|
185
|
+
for (const key of ['summarizationProvider', 'summarizationModel']) {
|
|
186
|
+
if (parsed[key] !== undefined && typeof parsed[key] !== 'string') throw new Error(`Invalid ${key}.`);
|
|
187
|
+
}
|
|
188
|
+
const verdict = validateSummaryConfig({...parsed, provider:parsed.summarizationProvider, model:parsed.summarizationModel});
|
|
189
|
+
if (!verdict.ok) throw new Error(verdict.message);
|
|
190
|
+
}
|
|
191
|
+
const retry=retrySettingsOf(parsed);if(!retry.ok)return null;
|
|
192
|
+
const idle = idleSettingsOf(parsed);
|
|
193
|
+
if (!idle.ok) return null;
|
|
194
|
+
const provider = textOf(parsed.summarizationProvider);
|
|
195
|
+
const model = textOf(parsed.summarizationModel);
|
|
196
|
+
const thresholdRatio = thresholdRatioOf(parsed.thresholdRatio);
|
|
197
|
+
const minimumReductionRatio = minimumReductionRatioOf(parsed.minimumReductionRatio);
|
|
198
|
+
const maxTokens = maxTokensOf(parsed.maxTokens);
|
|
199
|
+
// Trigger/retry/idle settings are valid without a model override, including
|
|
200
|
+
// an idle interval of zero that must still override inherited engine config.
|
|
201
|
+
if (!provider && !model && thresholdRatio === null && minimumReductionRatio === null && maxTokens === null && Object.keys(retry.value).length===0 && idle.value.idleCompactionMinutes === undefined) return null;
|
|
202
|
+
if ((provider === '') !== (model === '')) return null;
|
|
203
|
+
if (provider && (provider.length > MAX_ID_LENGTH || !ID_RE.test(provider))) return null;
|
|
204
|
+
if (model && (model.length > MAX_ID_LENGTH || !ID_RE.test(model))) return null;
|
|
205
|
+
return { provider, model, maxTokens, thresholdRatio, ...retry.value, ...idle.value, ...(minimumReductionRatio === null ? {} : {minimumReductionRatio}) };
|
|
206
|
+
} catch (cause) {
|
|
207
|
+
if (strict && cause?.code !== 'ENOENT') {
|
|
208
|
+
const error = new SummaryConfigError('INVALID_SAVED_SETTINGS', 'Compaction settings could not be read safely. Open Summary model settings and save them again; the pending input is retained.');
|
|
209
|
+
error.cause = cause;
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Write the override file. Invalid input must never reach this function. */
|
|
217
|
+
export function writeSummaryConfig(file, value) {
|
|
218
|
+
const retry=retrySettingsOf(value);if(!retry.ok)throw new SummaryConfigError(retry.code,retry.message);
|
|
219
|
+
const idle = idleSettingsOf(value);
|
|
220
|
+
if (!idle.ok) throw new SummaryConfigError(idle.code, idle.message);
|
|
221
|
+
const provider = textOf(value?.provider);
|
|
222
|
+
const model = textOf(value?.model);
|
|
223
|
+
const maxTokens = maxTokensOf(value?.maxTokens);
|
|
224
|
+
const thresholdRatio = thresholdRatioOf(value?.thresholdRatio);
|
|
225
|
+
const minimumReductionRatio = minimumReductionRatioOf(value?.minimumReductionRatio);
|
|
226
|
+
const payload = {
|
|
227
|
+
version: CONFIG_VERSION,
|
|
228
|
+
...retry.value,
|
|
229
|
+
...idle.value,
|
|
230
|
+
updatedAt: new Date().toISOString(),
|
|
231
|
+
...(provider && model ? { summarizationProvider: provider, summarizationModel: model } : {}),
|
|
232
|
+
...(maxTokens === null ? {} : { maxTokens }),
|
|
233
|
+
...(thresholdRatio === null ? {} : { thresholdRatio }),
|
|
234
|
+
...(minimumReductionRatio === null ? {} : { minimumReductionRatio }),
|
|
235
|
+
};
|
|
236
|
+
const body = JSON.stringify(payload, null, 2) + '\n';
|
|
237
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
238
|
+
const temporary = `${file}.tmp-${process.pid}-${Date.now()}`;
|
|
239
|
+
writeFileSync(temporary, body, 'utf8');
|
|
240
|
+
try {
|
|
241
|
+
renameSync(temporary, file);
|
|
242
|
+
} catch {
|
|
243
|
+
// Windows can refuse rename-over-existing; a direct rewrite is still safe
|
|
244
|
+
// for this small, single-writer file, and readSummaryConfig ignores junk.
|
|
245
|
+
writeFileSync(file, body, 'utf8');
|
|
246
|
+
try { unlinkSync(temporary); } catch { /* best effort */ }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Pure RPC dispatch, kept separate from HTTP so it can be tested offline.
|
|
252
|
+
* Throws SummaryConfigError for invalid methods/values.
|
|
253
|
+
*/
|
|
254
|
+
export async function handleSummaryConfigRpc(payload, file = defaultSummaryConfigPath(), llm = null) {
|
|
255
|
+
const method = typeof payload?.method === 'string' ? payload.method : '';
|
|
256
|
+
if (method === 'get') {
|
|
257
|
+
return { config: readSummaryConfig(file), cleared: false, restartRequired: false };
|
|
258
|
+
}
|
|
259
|
+
if (method === 'models') {
|
|
260
|
+
// The Settings page renders provider/model selects from the host's live
|
|
261
|
+
// LLM catalog, so the dropdowns match what this DSH can actually route.
|
|
262
|
+
if (!llm || typeof llm.listProviders !== 'function') {
|
|
263
|
+
throw new SummaryConfigError('MODELS_UNAVAILABLE', 'the host LLM catalog is unavailable');
|
|
264
|
+
}
|
|
265
|
+
const providers = [];
|
|
266
|
+
for (const info of llm.listProviders() ?? []) {
|
|
267
|
+
const provider = typeof info?.id === 'string' ? info.id : typeof info?.provider === 'string' ? info.provider : '';
|
|
268
|
+
if (!provider) continue;
|
|
269
|
+
let models = [];
|
|
270
|
+
try {
|
|
271
|
+
models = typeof llm.listModels === 'function' ? await llm.listModels(provider) : [];
|
|
272
|
+
} catch {
|
|
273
|
+
models = [];
|
|
274
|
+
}
|
|
275
|
+
providers.push({
|
|
276
|
+
provider,
|
|
277
|
+
name: typeof info?.name === 'string' && info.name ? info.name : provider,
|
|
278
|
+
models: (Array.isArray(models) ? models : [])
|
|
279
|
+
.filter(model => model && typeof model.id === 'string' && model.id.length > 0)
|
|
280
|
+
.map(model => ({ id: model.id, name: typeof model.name === 'string' && model.name ? model.name : model.id })),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return { providers };
|
|
284
|
+
}
|
|
285
|
+
if (method === 'set') {
|
|
286
|
+
const verdict = validateSummaryConfig(payload?.params ?? {});
|
|
287
|
+
if (!verdict.ok) throw new SummaryConfigError(verdict.code, verdict.message);
|
|
288
|
+
const value = verdict.value;
|
|
289
|
+
// Older clients and model-only clears omit this independent setting.
|
|
290
|
+
// Keep it until the user explicitly replaces it, including with zero.
|
|
291
|
+
if (value.idleCompactionMinutes === undefined) {
|
|
292
|
+
const previous = readSummaryConfig(file);
|
|
293
|
+
if (previous?.idleCompactionMinutes !== undefined) value.idleCompactionMinutes = previous.idleCompactionMinutes;
|
|
294
|
+
}
|
|
295
|
+
if (!value.provider && !value.model) {
|
|
296
|
+
writeSummaryConfig(file, value);
|
|
297
|
+
const cleared = value.maxTokens === null && value.thresholdRatio === null && value.minimumReductionRatio === undefined && value.idleCompactionMinutes === undefined && Object.keys(SUMMARY_RETRY_SETTINGS).every(key=>value[key]===undefined);
|
|
298
|
+
return {
|
|
299
|
+
config: cleared ? null : value,
|
|
300
|
+
cleared,
|
|
301
|
+
restartRequired: false,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
writeSummaryConfig(file, value);
|
|
305
|
+
return { config: value, cleared: false, restartRequired: false };
|
|
306
|
+
}
|
|
307
|
+
throw new SummaryConfigError('UNKNOWN_METHOD', `unknown settings method: ${method || '(empty)'}`);
|
|
308
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Timing only: no draft text, reasoning content, credentials or token estimates.
|
|
2
|
+
// Uses a monotonic clock. Observations come from the assembler's accepted changes,
|
|
3
|
+
// so empty deltas, metadata and ignored post-finalization deltas are not progress.
|
|
4
|
+
export class SummaryProgress {
|
|
5
|
+
constructor(clock = () => performance.now()) {
|
|
6
|
+
this.clock = clock;
|
|
7
|
+
this.startedAt = clock();
|
|
8
|
+
this.firstChunkMs = null;
|
|
9
|
+
this.lastChunkMs = null;
|
|
10
|
+
this.firstTextMs = null;
|
|
11
|
+
this.lastTextMs = null;
|
|
12
|
+
this.firstProgressMs = null;
|
|
13
|
+
this.lastProgressMs = null;
|
|
14
|
+
this.maxProgressGapMs = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
elapsed() {
|
|
18
|
+
return Math.max(0, Math.floor(this.clock() - this.startedAt));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
observe(change) {
|
|
22
|
+
if (!change) return;
|
|
23
|
+
const at = this.elapsed();
|
|
24
|
+
this.firstChunkMs ??= at;
|
|
25
|
+
this.lastChunkMs = at;
|
|
26
|
+
if (change.textChanged) {
|
|
27
|
+
this.firstTextMs ??= at;
|
|
28
|
+
this.lastTextMs = at;
|
|
29
|
+
}
|
|
30
|
+
if (!change.textChanged && !change.reasoningChanged) return;
|
|
31
|
+
this.firstProgressMs ??= at;
|
|
32
|
+
if (this.lastProgressMs !== null) {
|
|
33
|
+
this.maxProgressGapMs = Math.max(this.maxProgressGapMs, at - this.lastProgressMs);
|
|
34
|
+
}
|
|
35
|
+
this.lastProgressMs = at;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
snapshot() {
|
|
39
|
+
const elapsedMs = this.elapsed();
|
|
40
|
+
const idleMs = elapsedMs - (this.lastProgressMs ?? 0);
|
|
41
|
+
return {
|
|
42
|
+
elapsedMs,
|
|
43
|
+
firstChunkMs: this.firstChunkMs,
|
|
44
|
+
lastChunkMs: this.lastChunkMs,
|
|
45
|
+
firstTextMs: this.firstTextMs,
|
|
46
|
+
lastTextMs: this.lastTextMs,
|
|
47
|
+
firstProgressMs: this.firstProgressMs,
|
|
48
|
+
lastProgressMs: this.lastProgressMs,
|
|
49
|
+
idleMs,
|
|
50
|
+
textIdleMs: elapsedMs - (this.lastTextMs ?? 0),
|
|
51
|
+
// First-response latency has its own field; this is the largest gap
|
|
52
|
+
// after content began, including any unfinished gap at snapshot time.
|
|
53
|
+
maxProgressGapMs: this.lastProgressMs === null ? null : Math.max(this.maxProgressGapMs, idleMs),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {setTimeout as delay} from 'node:timers/promises';
|
|
2
|
+
export const DEFAULT_SUMMARY_RETRY_COUNT=2;
|
|
3
|
+
export const DEFAULT_SUMMARY_RETRY_DELAY_MS=1000;
|
|
4
|
+
export function summaryRetryPolicy(options={}) {
|
|
5
|
+
return {count:Number.isInteger(options.retryCount)&&options.retryCount>=0&&options.retryCount<=4?options.retryCount:DEFAULT_SUMMARY_RETRY_COUNT,
|
|
6
|
+
delayMs:Number.isInteger(options.retryDelayMs)&&options.retryDelayMs>=0&&options.retryDelayMs<=30000?options.retryDelayMs:DEFAULT_SUMMARY_RETRY_DELAY_MS};
|
|
7
|
+
}
|
|
8
|
+
export const CONTENT_RETRY_CODES=new Set(['PROGRESS_FACT_CONFLICT','SUMMARY_STRUCTURE_INVALID','SUMMARY_INVALID','SUMMARY_NOT_SMALLER','RETIRED_MEMORY_REINTRODUCED','EMPTY_SUMMARY','CHECKPOINT_COVERAGE_INCOMPLETE','CONSTRAINT_LOST','CONSTRAINT_POLARITY_LOST','CONTINUATION_STRUCTURE','CONTINUATION_TOOL_OUTPUT','CONTINUATION_TOO_LARGE','MAX_TOKENS']);
|
|
9
|
+
export function retryableSummaryError(error){
|
|
10
|
+
const chain=[],seen=new Set();
|
|
11
|
+
for(let e=error,n=0;e&&n<5&&!seen.has(e);e=e.cause,n++){seen.add(e);chain.push(e);}
|
|
12
|
+
if(chain.some(e=>[400,401,403,404,405,413,422].includes(e.status??e.statusCode??e.response?.status)))return false;
|
|
13
|
+
if(CONTENT_RETRY_CODES.has(error?.code)||error?.code==='SUMMARY_TIMEOUT')return true;
|
|
14
|
+
for(const e of chain){
|
|
15
|
+
const status=e.status??e.statusCode??e.response?.status;
|
|
16
|
+
if([408,425,429,500,502,503,504].includes(status))return true;
|
|
17
|
+
if(['TIMEOUT','TRANSPORT','ECONNRESET','ETIMEDOUT','ECONNABORTED','EPIPE','EAI_AGAIN','ECONNREFUSED','UND_ERR_CONNECT_TIMEOUT','UND_ERR_HEADERS_TIMEOUT','UND_ERR_BODY_TIMEOUT','UND_ERR_SOCKET','NETWORK_ERROR','NETWORK','STREAM_INTERRUPTED','STREAM_ERROR','ABORTED'].includes(e.code))return true;
|
|
18
|
+
if(e.name==='AbortError'||e.name==='APIConnectionTimeoutError')return true;
|
|
19
|
+
if(/fetch failed|\b(?:request|connection|connect|read|headers|body) (?:timed out|timeout)\b|network (?:error|failure)|socket hang up|connection (?:error|reset|closed)|premature (?:close|end)|stream (?:interrupted|disconnected)|\b(?:HTTP[ /:]*)?(?:429|502|503|504)\b/i.test(e.message??''))return true;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
export async function waitForSummaryRetry(base,index,signal){
|
|
24
|
+
signal?.throwIfAborted?.();const ms=Math.min(30000,base*2**Math.max(0,index-1));
|
|
25
|
+
if(ms>0)try{await delay(ms,undefined,{signal});}catch(error){if(signal?.aborted)throw signal.reason??error;throw error;}
|
|
26
|
+
signal?.throwIfAborted?.();return ms;
|
|
27
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Timeout-only presentation. Never expose source text or arbitrary provider errors.
|
|
2
|
+
const TIMING_FIELDS = ['elapsedMs','firstChunkMs','lastChunkMs','firstTextMs','lastTextMs','firstProgressMs','lastProgressMs','idleMs','textIdleMs','maxProgressGapMs'];
|
|
3
|
+
const COUNT_FIELDS = ['chunks','textChars','reasoningChars'];
|
|
4
|
+
function safeProgress(error) {
|
|
5
|
+
const out = {};
|
|
6
|
+
for (const key of [...TIMING_FIELDS,...COUNT_FIELDS]) {
|
|
7
|
+
const parsed = new RegExp(`(?:^|[ (,])${key}=(none|[0-9]+)(?=[ ,)]|$)`).exec(error.message ?? '');
|
|
8
|
+
const value = error.diagnostics && Object.hasOwn(error.diagnostics,key) ? error.diagnostics[key] : (parsed ? parsed[1] === 'none' ? null : Number(parsed[1]) : undefined);
|
|
9
|
+
if (value === null) out[key] = null;
|
|
10
|
+
else if (Number.isSafeInteger(value) && value >= 0 && value <= (TIMING_FIELDS.includes(key) ? 86400000 : 1000000000)) out[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return Number.isSafeInteger(out.textChars) && Object.hasOwn(out,'firstProgressMs') ? out : undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function summaryTimeoutInfo(error) {
|
|
16
|
+
const seen = new Set();
|
|
17
|
+
for (let current = error, depth = 0; current && depth < 8 && !seen.has(current); current = current.cause, depth++) {
|
|
18
|
+
seen.add(current);
|
|
19
|
+
if (current.code === 'SUMMARY_TIMEOUT') {
|
|
20
|
+
const parsed = /timed out after (\d+)ms/.exec(current.message ?? '');
|
|
21
|
+
const ms = Number.isFinite(current.timeoutMs) ? current.timeoutMs : parsed ? Number(parsed[1]) : null;
|
|
22
|
+
const diagnostics = safeProgress(current);
|
|
23
|
+
return {code:'SUMMARY_TIMEOUT', timeoutMs:ms > 0 && ms <= 86400000 ? ms : null, ...(diagnostics ? {diagnostics} : {})};
|
|
24
|
+
}
|
|
25
|
+
if (current.code === 'SUMMARY_ALL_FAILED' && Array.isArray(current.failures) && current.failures.length && current.failures.every(f => f.code === 'SUMMARY_TIMEOUT')) {
|
|
26
|
+
return summaryTimeoutInfo(current.failures[0]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// compaction/end serializes errors as strings. Only recognize the native code
|
|
33
|
+
// prefix, including the legacy single-target aggregate seen before review.20.
|
|
34
|
+
export function timeoutInfoFromEnd(errorText) {
|
|
35
|
+
if (typeof errorText !== 'string') return null;
|
|
36
|
+
if (!/^SUMMARY_TIMEOUT:/.test(errorText) && !/^SUMMARY_ALL_FAILED: all summarization targets failed: [^\n]+ → SUMMARY_TIMEOUT:/.test(errorText)) return null;
|
|
37
|
+
const matches = [...errorText.matchAll(/→ ([A-Z_]+):/g)];
|
|
38
|
+
if (matches.some(m => m[1] !== 'SUMMARY_TIMEOUT')) return null;
|
|
39
|
+
return summaryTimeoutInfo({code:'SUMMARY_TIMEOUT',message:errorText});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function summaryTimeoutText(errorOrInfo) {
|
|
43
|
+
const info = summaryTimeoutInfo(errorOrInfo);
|
|
44
|
+
if (!info) return null;
|
|
45
|
+
const duration = info.timeoutMs === null ? '' : `(${Math.ceil(info.timeoutMs / 1000)} 秒)`;
|
|
46
|
+
const d = info.diagnostics;
|
|
47
|
+
let progress = '';
|
|
48
|
+
if (d?.textChars > 0 && Number.isSafeInteger(d.textIdleMs)) {
|
|
49
|
+
progress = `已收到 ${d.textChars} 字符正文,最后正文进展距今 ${Math.ceil(d.textIdleMs / 1000)} 秒。`;
|
|
50
|
+
} else if (d?.firstChunkMs === null) {
|
|
51
|
+
progress = '尚未收到流分片。';
|
|
52
|
+
} else if (d && d.firstProgressMs === null) {
|
|
53
|
+
progress = '已收到流分片,但尚无可观测的内容进展。';
|
|
54
|
+
} else if (d && Number.isSafeInteger(d.idleMs)) {
|
|
55
|
+
progress = `尚未收到正文,最后可观测内容进展距今 ${Math.ceil(d.idleMs / 1000)} 秒。`;
|
|
56
|
+
}
|
|
57
|
+
return `摘要请求超时${duration}。${progress}本次压缩已停止,未完成区间的原上下文仍保留。请稍后手动执行 /compact 重试。`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function timeoutForCommand(session, commandId) {
|
|
61
|
+
if (!commandId || typeof session?.eventAt !== 'function' || !Number.isSafeInteger(session.seq)) return null;
|
|
62
|
+
for (let seq = session.seq - 1; seq >= 0; seq--) {
|
|
63
|
+
const event = session.eventAt(seq);
|
|
64
|
+
if (event?.type === 'compaction/end' && event.data?.sourceCommandId === commandId) return timeoutInfoFromEnd(event.data.error);
|
|
65
|
+
if (event?.type === 'command/run' && event.data?.commandId === commandId) break;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|