pi-charset 0.1.7 → 0.1.8
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 +10 -0
- package/CHANGELOG.zh-CN.md +10 -0
- package/dist/diagnostics/incidents.js +436 -0
- package/dist/edit/diagnostics.js +27 -0
- package/dist/faults.js +67 -0
- package/dist/mcp/server.js +138 -45
- package/dist/operations.js +169 -35
- package/dist/setup/adapters.js +524 -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/diagnostics/incidents.ts +474 -0
- package/src/edit/diagnostics.ts +31 -0
- package/src/faults.ts +165 -0
- package/src/mcp/server.ts +191 -45
- package/src/operations.ts +178 -37
- package/src/setup/adapters.ts +613 -0
- package/src/version.ts +1 -1
- package/tools/pi-charset-cli.mjs +232 -22
- package/tools/pi-charset-mcp.mjs +3 -1
package/src/operations.ts
CHANGED
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
import iconv from "iconv-lite";
|
|
46
46
|
import { planEdits, type EditReplacement, type PlannedText } from "./edit/plan";
|
|
47
47
|
import { EditError } from "./edit/diagnostics";
|
|
48
|
+
import { CharsetFault } from "./faults";
|
|
48
49
|
import { pushEncodingNote } from "./notify";
|
|
49
50
|
import { autoCommit, recordWrite } from "./journal";
|
|
50
51
|
import {
|
|
@@ -193,19 +194,24 @@ function isFreshCacheHit(entry: DecodedReadEntry | undefined, curStamp: FileStam
|
|
|
193
194
|
);
|
|
194
195
|
}
|
|
195
196
|
|
|
196
|
-
function mixedLineEndingError(absPath: string, locale?: LocaleMode, suggested?: "CRLF" | "LF"):
|
|
197
|
+
function mixedLineEndingError(absPath: string, locale?: LocaleMode, suggested?: "CRLF" | "LF"): CharsetFault {
|
|
197
198
|
const rec = (suggested ?? "lf").toLowerCase();
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
`[MIXED_EOL; not_applied] Refused to write ${absPath}: file contains mixed CRLF and LF line endings. ` +
|
|
201
|
-
`Run /charset fix ${absPath} (or /charset-normalize-eol ${absPath} --to ${rec}) before editing
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
199
|
+
const msg =
|
|
200
|
+
resolveLocale(locale) === "en"
|
|
201
|
+
? `[MIXED_EOL; not_applied] Refused to write ${absPath}: file contains mixed CRLF and LF line endings. ` +
|
|
202
|
+
`Run /charset fix ${absPath} (or /charset-normalize-eol ${absPath} --to ${rec}) before editing.`
|
|
203
|
+
: `[MIXED_EOL; not_applied] 拒写 ${absPath}:文件同时包含 CRLF 与 LF(混合换行)。` +
|
|
204
|
+
`直接写入会将全文行尾统一,已拒绝修改;文件仍可正常读取。` +
|
|
205
|
+
`请先执行 /charset fix(或 /charset-normalize-eol ${absPath} --to ${rec})一键统一为多数行尾后再修改。`;
|
|
206
|
+
return new CharsetFault(msg, {
|
|
207
|
+
code: "MIXED_EOL",
|
|
208
|
+
category: "policy",
|
|
209
|
+
phase: "validate",
|
|
210
|
+
outcome: "not_applied",
|
|
211
|
+
retryable: false,
|
|
212
|
+
actionHint: `Normalize line endings first via charset_fix(action='normalize_eol', to='${(suggested ?? "LF").toUpperCase()}').`,
|
|
213
|
+
eol: "MIXED",
|
|
214
|
+
});
|
|
209
215
|
}
|
|
210
216
|
|
|
211
217
|
async function readAsUtf8Buffer(
|
|
@@ -286,21 +292,35 @@ function isNotFound(e: unknown): boolean {
|
|
|
286
292
|
}
|
|
287
293
|
|
|
288
294
|
/** Replacing a hard-linked path with a fresh inode would silently leave the other names on stale content. */
|
|
289
|
-
function hardLinkError(absPath: string, nlink: number, locale?: LocaleMode):
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
`[HARDLINK_REFUSED; not_applied] Refused to write ${absPath}: file has ${nlink} hard links; atomic replacement would detach sibling links
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
295
|
+
function hardLinkError(absPath: string, nlink: number, locale?: LocaleMode): CharsetFault {
|
|
296
|
+
const msg =
|
|
297
|
+
resolveLocale(locale) === "en"
|
|
298
|
+
? `[HARDLINK_REFUSED; not_applied] Refused to write ${absPath}: file has ${nlink} hard links; atomic replacement would detach sibling links.`
|
|
299
|
+
: `[HARDLINK_REFUSED; not_applied] 拒写 ${absPath}:该文件有 ${nlink} 个硬链接。替换会断开与其他路径名的关联,已拒绝写入。请直接修改原文件或先解除硬链接。`;
|
|
300
|
+
return new CharsetFault(msg, {
|
|
301
|
+
code: "UNSUPPORTED_FILE",
|
|
302
|
+
category: "policy",
|
|
303
|
+
phase: "validate",
|
|
304
|
+
outcome: "not_applied",
|
|
305
|
+
retryable: false,
|
|
306
|
+
actionHint: "Unlink extra hard links or modify a single-link file directly.",
|
|
307
|
+
});
|
|
298
308
|
}
|
|
299
309
|
|
|
300
|
-
const symlinkError = (absPath: string, locale?: LocaleMode) =>
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
310
|
+
const symlinkError = (absPath: string, locale?: LocaleMode): CharsetFault =>
|
|
311
|
+
new CharsetFault(
|
|
312
|
+
resolveLocale(locale) === "en"
|
|
313
|
+
? `[SYMLINK_REFUSED; not_applied] Refused to write ${absPath}: path is a symbolic link. Edit the link target directly.`
|
|
314
|
+
: `[SYMLINK_REFUSED; not_applied] 拒写 ${absPath}:该路径为符号链接(symlink)。为防止替换符号链接本身或引发安全竞态,已拒绝写入。请直接对目标文件进行修改。`,
|
|
315
|
+
{
|
|
316
|
+
code: "UNSUPPORTED_FILE",
|
|
317
|
+
category: "policy",
|
|
318
|
+
phase: "validate",
|
|
319
|
+
outcome: "not_applied",
|
|
320
|
+
retryable: false,
|
|
321
|
+
actionHint: "Resolve and target the real file behind the symbolic link.",
|
|
322
|
+
},
|
|
323
|
+
);
|
|
304
324
|
|
|
305
325
|
/** Read the target file once prior to writing (returns null if the file does not exist yet). Rejects symlinks. */
|
|
306
326
|
async function readExistingOrNull(absPath: string): Promise<Buffer | null> {
|
|
@@ -388,9 +408,25 @@ function applyGate1(
|
|
|
388
408
|
`本次已升为 GB18030 写出`,
|
|
389
409
|
};
|
|
390
410
|
}
|
|
391
|
-
throw new
|
|
411
|
+
throw new CharsetFault(gate1Message(absPath, text, unmappableChars(text, "GB18030"), "GB18030", strategy, locale), {
|
|
412
|
+
code: "UNMAPPABLE",
|
|
413
|
+
category: "policy",
|
|
414
|
+
phase: "encode",
|
|
415
|
+
outcome: "not_applied",
|
|
416
|
+
retryable: false,
|
|
417
|
+
actionHint: "Replace unsupported characters or change target encoding explicitly.",
|
|
418
|
+
encoding: "GB18030",
|
|
419
|
+
});
|
|
392
420
|
}
|
|
393
|
-
throw new
|
|
421
|
+
throw new CharsetFault(gate1Message(absPath, text, bad, enc, strategy, locale), {
|
|
422
|
+
code: "UNMAPPABLE",
|
|
423
|
+
category: "policy",
|
|
424
|
+
phase: "encode",
|
|
425
|
+
outcome: "not_applied",
|
|
426
|
+
retryable: false,
|
|
427
|
+
actionHint: `Replace unsupported characters for ${enc} or change target encoding explicitly.`,
|
|
428
|
+
encoding: enc,
|
|
429
|
+
});
|
|
394
430
|
}
|
|
395
431
|
|
|
396
432
|
function encodeForWrite(
|
|
@@ -419,12 +455,21 @@ function encodeForWrite(
|
|
|
419
455
|
|
|
420
456
|
const upper = encoding.toUpperCase();
|
|
421
457
|
if (!isWellFormed(restored)) {
|
|
422
|
-
throw new
|
|
458
|
+
throw new CharsetFault(
|
|
423
459
|
resolveLocale(plan.rule?.locale) === "en"
|
|
424
460
|
? `[INVALID_UNICODE; not_applied] Refused to write ${absPath}: content contains lone UTF-16 surrogates.`
|
|
425
461
|
: upper === "UTF-8"
|
|
426
462
|
? `[INVALID_UNICODE; not_applied] 拒写 ${absPath}:内容含孤立 UTF-16 代理项,UTF-8 编码会产生 U+FFFD,已拒绝落盘。`
|
|
427
463
|
: `[INVALID_UNICODE; not_applied] 拒写 ${absPath}:内容含孤立 UTF-16 代理项,已拒绝落盘。`,
|
|
464
|
+
{
|
|
465
|
+
code: "UNMAPPABLE",
|
|
466
|
+
category: "input",
|
|
467
|
+
phase: "encode",
|
|
468
|
+
outcome: "not_applied",
|
|
469
|
+
retryable: false,
|
|
470
|
+
actionHint: "Remove lone UTF-16 surrogate code units from the text before writing.",
|
|
471
|
+
encoding,
|
|
472
|
+
},
|
|
428
473
|
);
|
|
429
474
|
}
|
|
430
475
|
if (upper === "UTF-8") {
|
|
@@ -432,10 +477,19 @@ function encodeForWrite(
|
|
|
432
477
|
const bodyBytes = Buffer.from(restored, "utf-8");
|
|
433
478
|
const out = bom.length ? Buffer.concat([bom, bodyBytes]) : bodyBytes;
|
|
434
479
|
if (bom.length === 0 && containsNul(out)) {
|
|
435
|
-
throw new
|
|
480
|
+
throw new CharsetFault(
|
|
436
481
|
resolveLocale(plan.rule?.locale) === "en"
|
|
437
482
|
? `[UNENCODABLE; not_applied] Refused to write ${absPath}: content contains NUL bytes and would classify as binary.`
|
|
438
483
|
: `[UNENCODABLE; not_applied] 拒写 ${absPath}:内容包含 NUL 字节,落盘后将被判为二进制文件,已拒绝写入。`,
|
|
484
|
+
{
|
|
485
|
+
code: "UNMAPPABLE",
|
|
486
|
+
category: "policy",
|
|
487
|
+
phase: "encode",
|
|
488
|
+
outcome: "not_applied",
|
|
489
|
+
retryable: false,
|
|
490
|
+
actionHint: "Remove NUL (0x00) characters before writing text files.",
|
|
491
|
+
encoding: "UTF-8",
|
|
492
|
+
},
|
|
439
493
|
);
|
|
440
494
|
}
|
|
441
495
|
return { bytes: out, text: restored, bomLen: bom.length, provenLossless: true };
|
|
@@ -446,18 +500,36 @@ function encodeForWrite(
|
|
|
446
500
|
plan.addBom === "utf16le" ? BOM_BYTES.utf16le : plan.addBom === "utf16be" ? BOM_BYTES.utf16be : Buffer.alloc(0);
|
|
447
501
|
const out = bom.length ? Buffer.concat([bom, bytes]) : bytes;
|
|
448
502
|
if ((bom.length === 0 && containsNul(out)) || (upper === "UTF-16LE" && bom.length > 0 && restored.startsWith("\u0000"))) {
|
|
449
|
-
throw new
|
|
503
|
+
throw new CharsetFault(
|
|
450
504
|
resolveLocale(plan.rule?.locale) === "en"
|
|
451
505
|
? `[UNENCODABLE; not_applied] Refused to write ${absPath}: encoded output contains NUL bytes that would classify as binary.`
|
|
452
506
|
: `[UNENCODABLE; not_applied] 拒写 ${absPath}:编码输出包含 NUL 字节,落盘后将被判为二进制文件,已拒绝写入。`,
|
|
507
|
+
{
|
|
508
|
+
code: "UNMAPPABLE",
|
|
509
|
+
category: "policy",
|
|
510
|
+
phase: "encode",
|
|
511
|
+
outcome: "not_applied",
|
|
512
|
+
retryable: false,
|
|
513
|
+
actionHint: "Remove NUL (0x00) characters before writing text files.",
|
|
514
|
+
encoding,
|
|
515
|
+
},
|
|
453
516
|
);
|
|
454
517
|
}
|
|
455
518
|
// When fastCanEncodeAll already proved every scalar value round-trips cleanly without combining sequences,
|
|
456
519
|
// decoding the full output buffer again is redundant.
|
|
457
520
|
const fastOk = fastCanEncodeAll(restored, encoding) === true;
|
|
458
521
|
if (!fastOk && decodeToUtf8(out.subarray(bom.length), encoding) !== restored) {
|
|
459
|
-
throw new
|
|
522
|
+
throw new CharsetFault(
|
|
460
523
|
`拒写 ${absPath}:内容经 ${encoding} 编码后解码不一致,已拒绝落盘。可将 writeEncoding 改为 UTF-8 或 GB18030。`,
|
|
524
|
+
{
|
|
525
|
+
code: "UNMAPPABLE",
|
|
526
|
+
category: "policy",
|
|
527
|
+
phase: "encode",
|
|
528
|
+
outcome: "not_applied",
|
|
529
|
+
retryable: false,
|
|
530
|
+
actionHint: `Replace characters that do not round-trip in ${encoding}, or change target encoding.`,
|
|
531
|
+
encoding,
|
|
532
|
+
},
|
|
461
533
|
);
|
|
462
534
|
}
|
|
463
535
|
return { bytes: out, text: restored, bomLen: bom.length, provenLossless: true };
|
|
@@ -569,16 +641,42 @@ async function writeEncodedLocked(
|
|
|
569
641
|
if (expectedVersion !== undefined) {
|
|
570
642
|
const actualVersion = prevRaw === null ? "absent" : sha1(prevRaw);
|
|
571
643
|
if (actualVersion !== expectedVersion) {
|
|
572
|
-
throw new
|
|
644
|
+
throw new CharsetFault(
|
|
573
645
|
resolveLocale() === "en"
|
|
574
646
|
? `[STALE_VERSION; not_applied] Refused to write ${absPath}: expectedVersion=${expectedVersion} does not match on-disk version (${actualVersion}). Re-read before writing.`
|
|
575
647
|
: `[STALE_VERSION; not_applied] 拒写 ${absPath}:请求版本 ${expectedVersion} 与磁盘当前版本(${actualVersion})不一致。请重新读取后再写入。`,
|
|
648
|
+
{
|
|
649
|
+
code: "STALE_VERSION",
|
|
650
|
+
category: "conflict",
|
|
651
|
+
phase: "validate",
|
|
652
|
+
outcome: "not_applied",
|
|
653
|
+
retryable: true,
|
|
654
|
+
actionHint: "Re-read using charset_read, then retry with the latest version.",
|
|
655
|
+
fileBytes: prevRaw?.length,
|
|
656
|
+
},
|
|
576
657
|
);
|
|
577
658
|
}
|
|
578
659
|
}
|
|
579
660
|
const plan = await resolveWritePlan(absPath, utf8content, prevRaw);
|
|
580
661
|
const locale = plan.rule?.locale;
|
|
581
|
-
if (plan.reject)
|
|
662
|
+
if (plan.reject) {
|
|
663
|
+
const rejectCode =
|
|
664
|
+
plan.kind === "binary"
|
|
665
|
+
? "BINARY_FILE"
|
|
666
|
+
: plan.verdict?.crossFamilyAmbiguous
|
|
667
|
+
? "AMBIGUOUS_ENCODING"
|
|
668
|
+
: "UNKNOWN_ENCODING";
|
|
669
|
+
throw new CharsetFault(`[GATE3_REJECTED; not_applied] ${plan.reject}`, {
|
|
670
|
+
code: rejectCode,
|
|
671
|
+
category: "policy",
|
|
672
|
+
phase: "detect",
|
|
673
|
+
outcome: "not_applied",
|
|
674
|
+
retryable: false,
|
|
675
|
+
actionHint: "Inspect the file with charset_check and resolve or declare its encoding before writing.",
|
|
676
|
+
encoding: plan.verdict?.encoding,
|
|
677
|
+
fileBytes: prevRaw?.length,
|
|
678
|
+
});
|
|
679
|
+
}
|
|
582
680
|
for (const w of plan.warnings) pushEncodingNote(absPath, w);
|
|
583
681
|
|
|
584
682
|
const g1 = applyGate1(plan, utf8content, absPath);
|
|
@@ -589,17 +687,34 @@ async function writeEncodedLocked(
|
|
|
589
687
|
const remembered = scope.baselines.get(key);
|
|
590
688
|
if (remembered !== undefined) {
|
|
591
689
|
if (prevRaw === null) {
|
|
592
|
-
throw new
|
|
690
|
+
throw new CharsetFault(
|
|
593
691
|
resolveLocale(locale) === "en"
|
|
594
692
|
? `[STALE_READ; not_applied] Refused to write ${absPath}: file was deleted externally after the last read.`
|
|
595
693
|
: `[STALE_READ; not_applied] 拒写 ${absPath}:文件自上次读取后已被外部删除。请重新确认后再操作。`,
|
|
694
|
+
{
|
|
695
|
+
code: "STALE_VERSION",
|
|
696
|
+
category: "conflict",
|
|
697
|
+
phase: "validate",
|
|
698
|
+
outcome: "not_applied",
|
|
699
|
+
retryable: true,
|
|
700
|
+
actionHint: "Verify file existence and re-read before writing.",
|
|
701
|
+
},
|
|
596
702
|
);
|
|
597
703
|
}
|
|
598
704
|
if (sha1(prevRaw) !== remembered) {
|
|
599
|
-
throw new
|
|
705
|
+
throw new CharsetFault(
|
|
600
706
|
resolveLocale(locale) === "en"
|
|
601
707
|
? `[STALE_READ; not_applied] Refused to write ${absPath}: file was modified externally since the last read. Re-read before writing.`
|
|
602
708
|
: `[STALE_READ; not_applied] 拒写 ${absPath}:文件自上次读取后已被外部修改(磁盘内容与读取基线不一致)。请重新读取该文件后再修改。`,
|
|
709
|
+
{
|
|
710
|
+
code: "STALE_VERSION",
|
|
711
|
+
category: "conflict",
|
|
712
|
+
phase: "validate",
|
|
713
|
+
outcome: "not_applied",
|
|
714
|
+
retryable: true,
|
|
715
|
+
actionHint: "Re-read using charset_read, then rebuild the write.",
|
|
716
|
+
fileBytes: prevRaw.length,
|
|
717
|
+
},
|
|
603
718
|
);
|
|
604
719
|
}
|
|
605
720
|
}
|
|
@@ -610,11 +725,23 @@ async function writeEncodedLocked(
|
|
|
610
725
|
try {
|
|
611
726
|
await seam.writeBytes(absPath, bytes, prevRaw);
|
|
612
727
|
} catch (e) {
|
|
613
|
-
throw
|
|
728
|
+
if (e instanceof CharsetFault || e instanceof EditError) throw e;
|
|
729
|
+
throw new CharsetFault(
|
|
614
730
|
resolveLocale(locale) === "en"
|
|
615
731
|
? `[WRITE_FAILED; unknown] Failed writing ${absPath}: ${(e as Error).message}. Disk state is unverified; re-read before retrying.`
|
|
616
732
|
: `[WRITE_FAILED; unknown] 写入 ${absPath} 失败:${(e as Error).message}。磁盘当前状态未验证,请重新读取确认后再重试。` +
|
|
617
733
|
`${g1.note ? `(${g1.note})` : ""}`,
|
|
734
|
+
{
|
|
735
|
+
code: "WRITE_FAILED",
|
|
736
|
+
category: "io",
|
|
737
|
+
phase: "commit",
|
|
738
|
+
outcome: "uncertain",
|
|
739
|
+
retryable: false,
|
|
740
|
+
actionHint: "Re-read the file to confirm on-disk state before retrying.",
|
|
741
|
+
encoding: g1.encoding,
|
|
742
|
+
eol: detectLineEndingKind(bytes, g1.encoding),
|
|
743
|
+
fileBytes: prevRaw?.length ?? bytes.length,
|
|
744
|
+
},
|
|
618
745
|
);
|
|
619
746
|
}
|
|
620
747
|
const entry = await recordWrite(absPath, prevRaw, bytes, "write");
|
|
@@ -623,7 +750,7 @@ async function writeEncodedLocked(
|
|
|
623
750
|
const problem = await verifyWritten(absPath, bytes, text, g1.encoding, plan.rule, bomLen, provenLossless);
|
|
624
751
|
if (problem) {
|
|
625
752
|
invalidatePathCaches(absPath);
|
|
626
|
-
throw new
|
|
753
|
+
throw new CharsetFault(
|
|
627
754
|
resolveLocale(locale) === "en"
|
|
628
755
|
? `[GATE2_VERIFY_FAILED; unknown] Post-write verification failed for ${absPath}: ${problem}. On-disk state was preserved without rollback and nothing was auto-committed; re-read before retrying.`
|
|
629
756
|
: `[GATE2_VERIFY_FAILED; unknown] 闸门 2(写后自校验)失败:${problem}。` +
|
|
@@ -631,6 +758,20 @@ async function writeEncodedLocked(
|
|
|
631
758
|
? `磁盘内容与本次写入不一致(可能被其它进程改写,外部修改冲突),已保留磁盘现状(未回滚、未自动提交);请重新读取后决定。`
|
|
632
759
|
: `文件写前不存在,已保留写出的内容供检查(未删除、未自动提交);请读取核对后决定。`) +
|
|
633
760
|
`${g1.note ? `(${g1.note})` : ""}`,
|
|
761
|
+
{
|
|
762
|
+
code: "WRITE_VERIFY_FAILED",
|
|
763
|
+
category: "engine",
|
|
764
|
+
phase: "verify",
|
|
765
|
+
outcome: "uncertain",
|
|
766
|
+
retryable: false,
|
|
767
|
+
applied: false,
|
|
768
|
+
verified: false,
|
|
769
|
+
committed: false,
|
|
770
|
+
actionHint: "Re-read the file to inspect on-disk bytes; nothing was auto-committed.",
|
|
771
|
+
encoding: g1.encoding,
|
|
772
|
+
eol: detectLineEndingKind(bytes, g1.encoding),
|
|
773
|
+
fileBytes: prevRaw?.length ?? bytes.length,
|
|
774
|
+
},
|
|
634
775
|
);
|
|
635
776
|
}
|
|
636
777
|
}
|