pi-charset 0.1.7 → 0.1.9
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 +27 -0
- package/CHANGELOG.zh-CN.md +27 -0
- package/dist/config.js +14 -0
- package/dist/diagnostics/incidents.js +436 -0
- package/dist/edit/diagnostics.js +27 -0
- package/dist/encoding/classify.js +26 -5
- package/dist/encoding/fast-codec.js +95 -44
- package/dist/faults.js +67 -0
- package/dist/mcp/server.js +303 -65
- package/dist/operations.js +172 -35
- package/dist/setup/adapters.js +625 -0
- package/dist/version.js +1 -1
- package/docs/MCP-AND-MULTI-AGENT.md +17 -2
- package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +17 -2
- package/package.json +1 -1
- package/src/config.ts +13 -0
- package/src/diagnostics/incidents.ts +474 -0
- package/src/edit/diagnostics.ts +31 -0
- package/src/encoding/classify.ts +25 -5
- package/src/encoding/fast-codec.ts +81 -39
- package/src/faults.ts +165 -0
- package/src/mcp/server.ts +356 -65
- package/src/operations.ts +180 -37
- package/src/setup/adapters.ts +701 -0
- package/src/version.ts +1 -1
- package/tools/pi-charset-cli.mjs +233 -22
- package/tools/pi-charset-mcp.mjs +3 -1
|
@@ -53,8 +53,10 @@ interface CompiledDbcs {
|
|
|
53
53
|
dfa: Int16Array[];
|
|
54
54
|
asciiFast: boolean;
|
|
55
55
|
gb18030: Gb18030Ranges | null;
|
|
56
|
+
decodeTables: number[][];
|
|
56
57
|
encodeTable: (number[] | undefined)[];
|
|
57
|
-
|
|
58
|
+
/** Lazily populated 8 KiB BMP round-trip encodability bitset (only needed on write/edit, never on read/classify). */
|
|
59
|
+
bmpEncodable: Uint8Array | null;
|
|
58
60
|
hasEncodeSeq: boolean;
|
|
59
61
|
asciiEncodable: boolean;
|
|
60
62
|
/** Lazily populated flat BMP -> 1/2-byte DBCS table (0 means NUL or fallback to iconv). */
|
|
@@ -205,36 +207,28 @@ function compileDbcs(encoding: string): CompiledDbcs | null {
|
|
|
205
207
|
}
|
|
206
208
|
}
|
|
207
209
|
|
|
208
|
-
const bmpEncodable = new Uint8Array(8192);
|
|
209
210
|
let asciiIdentity = true;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const mb =
|
|
211
|
+
let asciiEncodable = true;
|
|
212
|
+
const asciiBucket = encodeTable[0];
|
|
213
|
+
for (let cp = 0; cp < 128; cp++) {
|
|
214
|
+
const mb = asciiBucket !== undefined ? asciiBucket[cp] : UNASSIGNED;
|
|
215
|
+
if (mb !== cp) asciiIdentity = false;
|
|
214
216
|
let ok = false;
|
|
215
217
|
if (mb >= 0 && (gb18030 || mb < 0x1000000)) {
|
|
216
218
|
ok = decodeMbCode(decodeTables, mb) === cp;
|
|
217
219
|
} else if (mb === UNASSIGNED && gb18030) {
|
|
218
220
|
ok = gb18030CodePointEncodable(cp, gb18030, encodeTable);
|
|
219
221
|
}
|
|
220
|
-
if (ok)
|
|
221
|
-
if (cp < 128 && mb !== cp) asciiIdentity = false;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
let asciiEncodable = true;
|
|
225
|
-
for (let cp = 0; cp < 128; cp++) {
|
|
226
|
-
if ((bmpEncodable[cp >> 3] & (1 << (cp & 7))) === 0) {
|
|
227
|
-
asciiEncodable = false;
|
|
228
|
-
break;
|
|
229
|
-
}
|
|
222
|
+
if (!ok) asciiEncodable = false;
|
|
230
223
|
}
|
|
231
224
|
|
|
232
225
|
const compiled: CompiledDbcs = {
|
|
233
226
|
dfa,
|
|
234
227
|
asciiFast,
|
|
235
228
|
gb18030,
|
|
229
|
+
decodeTables,
|
|
236
230
|
encodeTable,
|
|
237
|
-
bmpEncodable,
|
|
231
|
+
bmpEncodable: null,
|
|
238
232
|
hasEncodeSeq,
|
|
239
233
|
asciiEncodable,
|
|
240
234
|
bmpToMb: null,
|
|
@@ -245,6 +239,26 @@ function compileDbcs(encoding: string): CompiledDbcs | null {
|
|
|
245
239
|
return compiled;
|
|
246
240
|
}
|
|
247
241
|
|
|
242
|
+
function getBmpEncodable(c: CompiledDbcs): Uint8Array {
|
|
243
|
+
if (c.bmpEncodable) return c.bmpEncodable;
|
|
244
|
+
const bits = new Uint8Array(8192);
|
|
245
|
+
const { encodeTable, decodeTables, gb18030 } = c;
|
|
246
|
+
for (let cp = 0; cp < 0x10000; cp++) {
|
|
247
|
+
if (cp >= 0xd800 && cp <= 0xdfff) continue;
|
|
248
|
+
const bucket = encodeTable[cp >> 8];
|
|
249
|
+
const mb = bucket !== undefined ? bucket[cp & 0xff] : UNASSIGNED;
|
|
250
|
+
let ok = false;
|
|
251
|
+
if (mb >= 0 && (gb18030 || mb < 0x1000000)) {
|
|
252
|
+
ok = decodeMbCode(decodeTables, mb) === cp;
|
|
253
|
+
} else if (mb === UNASSIGNED && gb18030) {
|
|
254
|
+
ok = gb18030CodePointEncodable(cp, gb18030, encodeTable);
|
|
255
|
+
}
|
|
256
|
+
if (ok) bits[cp >> 3] |= 1 << (cp & 7);
|
|
257
|
+
}
|
|
258
|
+
c.bmpEncodable = bits;
|
|
259
|
+
return bits;
|
|
260
|
+
}
|
|
261
|
+
|
|
248
262
|
function getBmpEncodeTable(c: CompiledDbcs): Uint16Array {
|
|
249
263
|
if (c.bmpToMb) return c.bmpToMb;
|
|
250
264
|
const table = new Uint16Array(65536);
|
|
@@ -271,28 +285,56 @@ export function fastExactRoundTrip(buf: Buffer, encoding: string): boolean | nul
|
|
|
271
285
|
const root = dfa[0];
|
|
272
286
|
let node = root;
|
|
273
287
|
const len = buf.length;
|
|
274
|
-
const asciiFast = c.asciiFast;
|
|
275
288
|
let icuSafe = c.icuDecoder !== null;
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
289
|
+
if (c.asciiFast) {
|
|
290
|
+
let i = 0;
|
|
291
|
+
while (i < len) {
|
|
292
|
+
if (node === root) {
|
|
293
|
+
while (i < len && buf[i] < 0x80) i++;
|
|
294
|
+
if (i >= len) break;
|
|
295
|
+
}
|
|
296
|
+
const b = buf[i];
|
|
297
|
+
const v = node[b];
|
|
298
|
+
if (v === DFA_VALID) {
|
|
299
|
+
node = root;
|
|
300
|
+
} else if (v < 0) {
|
|
301
|
+
node = dfa[-v];
|
|
302
|
+
} else if (v === DFA_VALID_ICU_DIFF) {
|
|
303
|
+
icuSafe = false;
|
|
304
|
+
node = root;
|
|
305
|
+
} else if (v === DFA_GB18030 && c.gb18030 && i >= 3) {
|
|
306
|
+
icuSafe = false;
|
|
307
|
+
const ptr = (buf[i - 3] - 0x81) * 12600 + (buf[i - 2] - 0x30) * 1260 + (buf[i - 1] - 0x81) * 10 + (b - 0x30);
|
|
308
|
+
if (!gb18030PtrValid(ptr, c.gb18030, c.encodeTable)) return false;
|
|
309
|
+
node = root;
|
|
310
|
+
} else if (v === DFA_FALLBACK) {
|
|
311
|
+
return null;
|
|
312
|
+
} else {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
i++;
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
for (let i = 0; i < len; i++) {
|
|
319
|
+
const b = buf[i];
|
|
320
|
+
const v = node[b];
|
|
321
|
+
if (v === DFA_VALID) {
|
|
322
|
+
node = root;
|
|
323
|
+
} else if (v < 0) {
|
|
324
|
+
node = dfa[-v];
|
|
325
|
+
} else if (v === DFA_VALID_ICU_DIFF) {
|
|
326
|
+
icuSafe = false;
|
|
327
|
+
node = root;
|
|
328
|
+
} else if (v === DFA_GB18030 && c.gb18030 && i >= 3) {
|
|
329
|
+
icuSafe = false;
|
|
330
|
+
const ptr = (buf[i - 3] - 0x81) * 12600 + (buf[i - 2] - 0x30) * 1260 + (buf[i - 1] - 0x81) * 10 + (b - 0x30);
|
|
331
|
+
if (!gb18030PtrValid(ptr, c.gb18030, c.encodeTable)) return false;
|
|
332
|
+
node = root;
|
|
333
|
+
} else if (v === DFA_FALLBACK) {
|
|
334
|
+
return null;
|
|
335
|
+
} else {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
296
338
|
}
|
|
297
339
|
}
|
|
298
340
|
if (node !== root) return false;
|
|
@@ -428,7 +470,7 @@ export function fastCanEncodeAll(text: string, encoding: string): boolean | null
|
|
|
428
470
|
const c = compileDbcs(encoding);
|
|
429
471
|
if (!c || c.hasEncodeSeq) return null;
|
|
430
472
|
if (c.asciiEncodable && !NON_ASCII_STR.test(text)) return true;
|
|
431
|
-
const bits = c
|
|
473
|
+
const bits = getBmpEncodable(c);
|
|
432
474
|
const asciiOk = c.asciiEncodable;
|
|
433
475
|
const len = text.length;
|
|
434
476
|
for (let i = 0; i < len; i++) {
|
package/src/faults.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Canonical typed fault contract shared across core I/O, edit/patch engines, grep, CLI, and MCP.
|
|
2
|
+
// Error classification always originates from typed codes at the source — never parsed from human-readable text.
|
|
3
|
+
|
|
4
|
+
export type FaultCode =
|
|
5
|
+
// Expected operational / policy rejections (never recorded as internal bugs)
|
|
6
|
+
| "INVALID_INPUT"
|
|
7
|
+
| "UNMAPPABLE"
|
|
8
|
+
| "UNKNOWN_ENCODING"
|
|
9
|
+
| "AMBIGUOUS_ENCODING"
|
|
10
|
+
| "MIXED_EOL"
|
|
11
|
+
| "STALE_VERSION"
|
|
12
|
+
| "STALE_READ"
|
|
13
|
+
| "CONFLICT"
|
|
14
|
+
| "CANCELLED"
|
|
15
|
+
| "PERMISSION_DENIED"
|
|
16
|
+
| "NOT_FOUND"
|
|
17
|
+
| "BINARY_FILE"
|
|
18
|
+
| "UNSUPPORTED_FILE"
|
|
19
|
+
| "LOCK_BUSY"
|
|
20
|
+
| "WRITE_FAILED"
|
|
21
|
+
// Reportable internal / invariant / compatibility faults
|
|
22
|
+
| "WRITE_VERIFY_FAILED"
|
|
23
|
+
| "ROLLBACK_FAILED"
|
|
24
|
+
| "BYTE_INVARIANT_FAILED"
|
|
25
|
+
| "MCP_PROTOCOL_ERROR"
|
|
26
|
+
| "CLIENT_STARTUP_FAILED"
|
|
27
|
+
| "TOOL_BYPASS_DETECTED"
|
|
28
|
+
| "LOCK_INVARIANT_FAILED"
|
|
29
|
+
| "GIT_INDEX_INVARIANT_FAILED"
|
|
30
|
+
| "SEARCH_ENGINE_FAILED"
|
|
31
|
+
| "INTERNAL";
|
|
32
|
+
|
|
33
|
+
export type FaultCategory =
|
|
34
|
+
| "input"
|
|
35
|
+
| "policy"
|
|
36
|
+
| "conflict"
|
|
37
|
+
| "io"
|
|
38
|
+
| "engine"
|
|
39
|
+
| "compatibility"
|
|
40
|
+
| "critical";
|
|
41
|
+
|
|
42
|
+
export type FaultPhase =
|
|
43
|
+
| "startup"
|
|
44
|
+
| "detect"
|
|
45
|
+
| "decode"
|
|
46
|
+
| "validate"
|
|
47
|
+
| "encode"
|
|
48
|
+
| "commit"
|
|
49
|
+
| "verify"
|
|
50
|
+
| "rollback"
|
|
51
|
+
| "transport"
|
|
52
|
+
| "search"
|
|
53
|
+
| "policy"
|
|
54
|
+
| "unknown";
|
|
55
|
+
|
|
56
|
+
export type FaultOutcome =
|
|
57
|
+
| "not_applied"
|
|
58
|
+
| "rolled_back"
|
|
59
|
+
| "applied_verified"
|
|
60
|
+
| "uncertain"
|
|
61
|
+
| "unknown";
|
|
62
|
+
|
|
63
|
+
export interface FaultMetadata {
|
|
64
|
+
readonly code: FaultCode;
|
|
65
|
+
readonly category: FaultCategory;
|
|
66
|
+
readonly phase: FaultPhase;
|
|
67
|
+
readonly outcome: FaultOutcome;
|
|
68
|
+
readonly retryable: boolean;
|
|
69
|
+
readonly actionHint: string;
|
|
70
|
+
readonly applied?: boolean;
|
|
71
|
+
readonly verified?: boolean;
|
|
72
|
+
readonly committed?: boolean;
|
|
73
|
+
readonly encoding?: string;
|
|
74
|
+
readonly eol?: string;
|
|
75
|
+
readonly fileBytes?: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const REPORTABLE_FAULT_CODES: ReadonlySet<FaultCode> = new Set([
|
|
79
|
+
"WRITE_VERIFY_FAILED",
|
|
80
|
+
"ROLLBACK_FAILED",
|
|
81
|
+
"BYTE_INVARIANT_FAILED",
|
|
82
|
+
"MCP_PROTOCOL_ERROR",
|
|
83
|
+
"CLIENT_STARTUP_FAILED",
|
|
84
|
+
"TOOL_BYPASS_DETECTED",
|
|
85
|
+
"LOCK_INVARIANT_FAILED",
|
|
86
|
+
"GIT_INDEX_INVARIANT_FAILED",
|
|
87
|
+
"SEARCH_ENGINE_FAILED",
|
|
88
|
+
"INTERNAL",
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
export interface McpErrorEnvelope {
|
|
92
|
+
readonly status: "failed";
|
|
93
|
+
readonly error: {
|
|
94
|
+
readonly code: FaultCode;
|
|
95
|
+
readonly category: FaultCategory;
|
|
96
|
+
readonly phase: FaultPhase;
|
|
97
|
+
readonly retryable: boolean;
|
|
98
|
+
};
|
|
99
|
+
readonly applied: boolean;
|
|
100
|
+
readonly verified: boolean;
|
|
101
|
+
readonly committed: boolean;
|
|
102
|
+
readonly outcome: FaultOutcome;
|
|
103
|
+
readonly actionHint: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class CharsetFault extends Error {
|
|
107
|
+
readonly fault: FaultMetadata;
|
|
108
|
+
|
|
109
|
+
constructor(message: string, fault: FaultMetadata) {
|
|
110
|
+
super(message);
|
|
111
|
+
this.name = "CharsetFault";
|
|
112
|
+
this.fault = Object.freeze({ ...fault });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get code(): FaultCode {
|
|
116
|
+
return this.fault.code;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
get outcome(): FaultOutcome {
|
|
120
|
+
return this.fault.outcome;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
get reportable(): boolean {
|
|
124
|
+
return REPORTABLE_FAULT_CODES.has(this.fault.code);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
toMcpEnvelope(): McpErrorEnvelope {
|
|
128
|
+
const f = this.fault;
|
|
129
|
+
return {
|
|
130
|
+
status: "failed",
|
|
131
|
+
error: {
|
|
132
|
+
code: f.code,
|
|
133
|
+
category: f.category,
|
|
134
|
+
phase: f.phase,
|
|
135
|
+
retryable: f.retryable,
|
|
136
|
+
},
|
|
137
|
+
applied: f.applied ?? false,
|
|
138
|
+
verified: f.verified ?? false,
|
|
139
|
+
committed: f.committed ?? false,
|
|
140
|
+
outcome: f.outcome,
|
|
141
|
+
actionHint: f.actionHint,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function isCharsetFault(err: unknown): err is CharsetFault {
|
|
147
|
+
return err instanceof CharsetFault;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function toMcpErrorEnvelope(fault: FaultMetadata): McpErrorEnvelope {
|
|
151
|
+
return {
|
|
152
|
+
status: "failed",
|
|
153
|
+
error: {
|
|
154
|
+
code: fault.code,
|
|
155
|
+
category: fault.category,
|
|
156
|
+
phase: fault.phase,
|
|
157
|
+
retryable: fault.retryable,
|
|
158
|
+
},
|
|
159
|
+
applied: fault.applied ?? false,
|
|
160
|
+
verified: fault.verified ?? false,
|
|
161
|
+
committed: fault.committed ?? false,
|
|
162
|
+
outcome: fault.outcome,
|
|
163
|
+
actionHint: fault.actionHint,
|
|
164
|
+
};
|
|
165
|
+
}
|