@kyun.si/codex-polish 0.0.1-beta.2 → 0.0.1-beta.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 (37) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/CONTRIBUTING.en.md +6 -6
  3. package/CONTRIBUTING.md +6 -6
  4. package/README.en.md +53 -18
  5. package/README.md +53 -18
  6. package/SECURITY.en.md +5 -3
  7. package/SECURITY.md +4 -2
  8. package/docs/technical.en.md +61 -39
  9. package/docs/technical.md +58 -32
  10. package/evaluation/README.en.md +4 -4
  11. package/evaluation/README.md +4 -4
  12. package/package.json +1 -1
  13. package/plugins/codex-polish/.codex-plugin/plugin.json +1 -1
  14. package/plugins/codex-polish/hooks/editor_prompt.mjs +51 -0
  15. package/plugins/codex-polish/hooks/history.mjs +107 -66
  16. package/plugins/codex-polish/hooks/preferences.mjs +140 -117
  17. package/plugins/codex-polish/hooks/project_setting.mjs +40 -30
  18. package/plugins/codex-polish/hooks/recovery.mjs +2 -69
  19. package/plugins/codex-polish/hooks/rewrite_mode.mjs +19 -0
  20. package/plugins/codex-polish/skills/codex-polish/SKILL.md +3 -31
  21. package/plugins/codex-polish/skills/codex-polish/WORKFLOW.md +47 -0
  22. package/plugins/codex-polish/skills/codex-polish/references/acp.md +30 -18
  23. package/plugins/codex-polish/skills/codex-polish/references/codex-polish-editor.md +4 -2
  24. package/plugins/codex-polish/skills/codex-polish/scripts/acp_models.mjs +31 -6
  25. package/plugins/codex-polish/skills/codex-polish/scripts/acp_payload.mjs +55 -30
  26. package/plugins/codex-polish/skills/codex-polish/scripts/cli_io.mjs +2 -2
  27. package/plugins/codex-polish/skills/codex-polish/scripts/decisions.mjs +49 -58
  28. package/plugins/codex-polish/skills/codex-polish/scripts/editor_role.mjs +48 -0
  29. package/plugins/codex-polish/skills/codex-polish/scripts/format_reply.mjs +171 -0
  30. package/plugins/codex-polish/skills/codex-polish/scripts/previous_reply.mjs +31 -0
  31. package/plugins/codex-polish/skills/codex-polish/scripts/rewrite_material.mjs +28 -0
  32. package/plugins/codex-polish/skills/codex-polish/scripts/text_guard.mjs +65 -154
  33. package/plugins/codex-polish/skills/codex-polish-setup/SKILL.md +39 -11
  34. package/plugins/codex-polish/skills/codex-polish-setup/references/credentials.en.md +8 -6
  35. package/plugins/codex-polish/skills/codex-polish-setup/references/credentials.md +3 -3
  36. package/scripts/install.mjs +389 -123
  37. package/scripts/native_cache.mjs +5 -5
@@ -0,0 +1,48 @@
1
+ import { closeSync, constants, fstatSync, openSync, readSync } from 'node:fs';
2
+ import { isAbsolute } from 'node:path';
3
+ import { parseArgs } from 'node:util';
4
+ import { readBundledEditorRole, readEditorPrompt } from '../../../hooks/editor_prompt.mjs';
5
+ import { effectiveRewriteMode, REWRITE_TRIGGERS } from '../../../hooks/rewrite_mode.mjs';
6
+ import { isMain } from './cli_io.mjs';
7
+
8
+ function renderEditorRole({ source, text }) {
9
+ const { header } = readBundledEditorRole();
10
+ return header + (source === 'custom' ? text : text + '\n');
11
+ }
12
+
13
+ export function editorRole(cwd, trigger) {
14
+ if (trigger !== undefined && !REWRITE_TRIGGERS.includes(trigger)) throw new Error('invalid_trigger');
15
+ return renderEditorRole(readEditorPrompt(cwd, effectiveRewriteMode(cwd, trigger)));
16
+ }
17
+
18
+ export function checkStagedEditorRole(file, prompt) {
19
+ const expected = Buffer.from(renderEditorRole(prompt));
20
+ const fd = openSync(file, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
21
+ try {
22
+ const info = fstatSync(fd);
23
+ if (!info.isFile() || info.size !== expected.length) throw new Error('editor_role_changed');
24
+ // Bound the read even if the staged file grows after the size check.
25
+ const actual = Buffer.alloc(expected.length + 1);
26
+ let bytes = 0;
27
+ while (bytes < actual.length) {
28
+ const count = readSync(fd, actual, bytes, actual.length - bytes, bytes);
29
+ if (!count) break;
30
+ bytes += count;
31
+ }
32
+ if (!actual.subarray(0, bytes).equals(expected)) throw new Error('editor_role_changed');
33
+ } finally {
34
+ closeSync(fd);
35
+ }
36
+ }
37
+
38
+ if (isMain(import.meta.url)) {
39
+ try {
40
+ const { values } = parseArgs({ options: { cwd: { type: 'string' }, trigger: { type: 'string' }, help: { type: 'boolean', short: 'h' } } });
41
+ if (values.help) console.log('Usage: editor_role.mjs --cwd PROJECT [--trigger auto|explicit|turbo]');
42
+ else if (!values.cwd || !isAbsolute(values.cwd)) process.exitCode = 2;
43
+ else process.stdout.write(editorRole(values.cwd, values.trigger));
44
+ } catch (error) {
45
+ process.stderr.write(error.message === 'rewrite_mode_invalid' ? 'rewrite_mode_invalid\n' : 'editor_prompt_invalid\n');
46
+ process.exitCode = 1;
47
+ }
48
+ }
@@ -0,0 +1,171 @@
1
+ import { isMain, readLimited, utf8 } from './cli_io.mjs';
2
+
3
+ const FENCE = /^ {0,3}(`{3,}|~{3,})/;
4
+ const VISUAL_START = '\uE200visualize\uE202';
5
+ const VISUAL_END = '\uE201';
6
+ const VISUAL_LINE = /^( {0,3})(\uE200?visualize\uE202?(\{[^\r\n]*\})\uE201?)[ \t]*$/u;
7
+ const VISUAL_TOKEN = /\uE200visualize\uE202([^\uE201\r\n]*)\uE201/gu;
8
+ const DISPLAY_LABELS = {
9
+ zh: { polished: 'Gemini 润色版', original: '查看 GPT 原稿', unchanged: 'Gemini 返回了原稿',
10
+ quoted: '查看原文', unchangedQuote: 'Gemini 返回了原文' },
11
+ en: { polished: 'Polished by Gemini', original: 'View original GPT draft', unchanged: 'Gemini returned the original draft',
12
+ quoted: 'View original text', unchangedQuote: 'Gemini returned the original text' },
13
+ };
14
+
15
+ function originalDisclosure(label, draft) {
16
+ // The desktop client converts HTML details to a Markdown container. If the
17
+ // draft documents either delimiter, quote its source inside that container.
18
+ if (/<\/?details\b|<!--|^[\t ]*:::/im.test(draft)) {
19
+ let fenceLength = 3;
20
+ for (const [ticks] of draft.matchAll(/`+/g)) fenceLength = Math.max(fenceLength, ticks.length + 1);
21
+ const fence = '`'.repeat(fenceLength);
22
+ const quoted = draft.replace(VISUAL_TOKEN, (_, data) => '\\uE200visualize\\uE202' + data + '\\uE201')
23
+ .replace(/\r\n|[\r\n]/g, '$&> ');
24
+ return `:::github-details{summary=${JSON.stringify(label)}}\n\n> ${fence}text\n> ${quoted}\n> ${fence}\n\n:::`;
25
+ }
26
+ // Desktop expands live references before it renders the Markdown disclosure.
27
+ // Entities display the original characters as text without instantiating a second chart.
28
+ let original = draft;
29
+ if (draft.includes(VISUAL_START)) {
30
+ const edits = visualizationLines(draft).filter(ref => ref.live).map(ref => ({
31
+ ...ref, replacement: literalVisual(ref.data)
32
+ }));
33
+ original = replaceReferences(draft, edits);
34
+ }
35
+ return `<details><summary>${label}</summary>\n\n${original}\n\n</details>`;
36
+ }
37
+
38
+ // Recover the displayed original from our own comparison format. Encoded
39
+ // chart references may also be literal examples, so never unescape those into
40
+ // a live widget. A malformed comparison has no trustworthy target.
41
+ export function originalReplyText(reply) {
42
+ const skipped = /^\*\*(?:未润色|Polishing skipped)\*\* · [^\r\n]+\n\n/.exec(reply);
43
+ if (skipped) return reply.slice(skipped[0].length);
44
+ for (const labels of Object.values(DISPLAY_LABELS)) {
45
+ for (const heading of [labels.unchanged, labels.unchangedQuote]) {
46
+ const unchanged = `**${heading}**\n\n`;
47
+ if (reply.startsWith(unchanged)) return reply.slice(unchanged.length);
48
+ }
49
+ if (!reply.startsWith(`**${labels.polished}**\n\n`)) continue;
50
+
51
+ const html = reply.endsWith('\n\n</details>');
52
+ const index = reply.lastIndexOf(html ? '\n\n<details><summary>' : '\n\n:::github-details{summary=');
53
+ if (index === -1) return null;
54
+ const disclosure = reply.slice(index + 2);
55
+ const prefix = label => html ? `<details><summary>${label}</summary>\n\n` : `:::github-details{summary=${JSON.stringify(label)}}\n\n`;
56
+ const label = [labels.original, labels.quoted].find(label => disclosure.startsWith(prefix(label)));
57
+ if (!label) return null;
58
+ const body = disclosure.slice(prefix(label).length);
59
+ let draft;
60
+ if (html) {
61
+ draft = body.slice(0, -'\n\n</details>'.length);
62
+ if (draft.includes('&#xE200;visualize&#xE202;')) return null;
63
+ } else {
64
+ const match = /^> (`{3,})text\n> /.exec(body);
65
+ if (!match) return null;
66
+ const suffix = `\n> ${match[1]}\n\n:::`;
67
+ if (!body.endsWith(suffix)) return null;
68
+ draft = body.slice(match[0].length, -suffix.length).replace(/(\r\n|[\r\n])> /g, '$1');
69
+ if (draft.includes('\\uE200visualize\\uE202')) return null;
70
+ }
71
+ return originalDisclosure(label, draft) === disclosure ? draft : null;
72
+ }
73
+ return reply;
74
+ }
75
+
76
+ // A Codex visual is a standalone reference. A provider can return its exact
77
+ // JSON while dropping the private-use characters that make it render in chat.
78
+ function visualizationLines(text) {
79
+ const refs = [];
80
+ let fence;
81
+ for (const line of text.matchAll(/^[^\r\n]+/gm)) {
82
+ const delimiter = FENCE.exec(line[0])?.[1];
83
+ if (fence) {
84
+ if (delimiter?.[0] === fence[0] && delimiter.length >= fence.length && line[0].trim() === delimiter) fence = null;
85
+ } else if (delimiter) {
86
+ fence = delimiter;
87
+ } else {
88
+ const match = VISUAL_LINE.exec(line[0]);
89
+ if (!match) continue;
90
+ const [, indent, token, data] = match;
91
+ let attrs;
92
+ try { attrs = JSON.parse(data); } catch { continue; }
93
+ if (typeof attrs?.path !== 'string' || !attrs.path.startsWith('/')) continue;
94
+ refs.push({ data, token, at: line.index + indent.length, live: token === VISUAL_START + data + VISUAL_END });
95
+ }
96
+ }
97
+ return refs;
98
+ }
99
+
100
+ // Restore unambiguous widget delimiters for the original chart. Other returned
101
+ // text is still displayed, even when no original chart can be matched.
102
+ function formatVisualizations(draft, candidate) {
103
+ if (!draft.includes(VISUAL_START) && !candidate.includes(VISUAL_START)) return candidate;
104
+ const groups = new Map();
105
+ for (const ref of visualizationLines(draft)) {
106
+ const group = groups.get(ref.data) ?? { live: 0, literal: false, slots: [] };
107
+ if (ref.live) group.live++; else group.literal = true;
108
+ groups.set(ref.data, group);
109
+ }
110
+ const edits = [];
111
+ for (const ref of visualizationLines(candidate)) {
112
+ const group = groups.get(ref.data);
113
+ if (!ref.live) {
114
+ if (group) group.slots.push(ref);
115
+ } else if (group?.live) {
116
+ group.live--;
117
+ } else {
118
+ // A model can write text for a new reference, but only the authoring
119
+ // agent's original references have a corresponding trusted local widget.
120
+ edits.push({ ...ref, replacement: literalVisual(ref.data) });
121
+ }
122
+ }
123
+ for (const [data, group] of groups) {
124
+ if (group.literal || group.slots.length !== group.live) continue;
125
+ for (const ref of group.slots) edits.push({ ...ref, replacement: VISUAL_START + data + VISUAL_END });
126
+ }
127
+ return replaceReferences(candidate, edits.sort((a, b) => a.at - b.at));
128
+ }
129
+
130
+ function literalVisual(data) {
131
+ return '&#xE200;visualize&#xE202;' + data + '&#xE201;';
132
+ }
133
+
134
+ function replaceReferences(text, edits) {
135
+ if (!edits.length) return text;
136
+ let result = '', start = 0;
137
+ for (const edit of edits) {
138
+ result += text.slice(start, edit.at) + edit.replacement;
139
+ start = edit.at + edit.token.length;
140
+ }
141
+ return result + text.slice(start);
142
+ }
143
+
144
+ function comparableProse(text) {
145
+ return text.replace(/\r\n?/g, '\n').trim();
146
+ }
147
+
148
+ export function formatReply({ draft, candidate, display_language, source_label }) {
149
+ if (source_label !== undefined && source_label !== 'quoted') throw new Error('invalid_source_label');
150
+ const text = formatVisualizations(draft, candidate);
151
+ if (display_language === undefined) return { text };
152
+ const labels = DISPLAY_LABELS[display_language];
153
+ const same = comparableProse(text) === comparableProse(draft);
154
+ const quoted = source_label === 'quoted';
155
+ const display_text = same
156
+ ? `**${quoted ? labels.unchangedQuote : labels.unchanged}**\n\n${text}`
157
+ : `**${labels.polished}**\n\n${text}\n\n${originalDisclosure(quoted ? labels.quoted : labels.original, draft)}`;
158
+ return { text, display_text };
159
+ }
160
+
161
+ if (isMain(import.meta.url)) {
162
+ try {
163
+ if (process.argv.length !== 2) throw new Error('unexpected arguments');
164
+ const payload = JSON.parse(utf8(await readLimited(process.stdin)));
165
+ if (typeof payload?.draft !== 'string' || typeof payload?.candidate !== 'string' ||
166
+ (payload.display_language !== undefined && !Object.hasOwn(DISPLAY_LABELS, payload.display_language))) {
167
+ throw new Error('invalid input');
168
+ }
169
+ console.log(JSON.stringify(formatReply(payload)));
170
+ } catch { process.exitCode = 2; }
171
+ }
@@ -0,0 +1,31 @@
1
+ import { isAbsolute } from 'node:path';
2
+ import { parseArgs } from 'node:util';
3
+ import { readPreviousReply } from '../../../hooks/history.mjs';
4
+ import { historySource, projectHistoryTurns } from '../../../hooks/preferences.mjs';
5
+ import { isMain } from './cli_io.mjs';
6
+
7
+ export function previousReply(cwd, { noHistory = false, turnsBack = 1 } = {}) {
8
+ const source = historySource(cwd);
9
+ if (!source) return null;
10
+ return readPreviousReply(source.transcript_path, source.session_id, source.turn_id,
11
+ noHistory ? 0 : projectHistoryTurns(cwd), turnsBack);
12
+ }
13
+
14
+ if (isMain(import.meta.url)) {
15
+ try {
16
+ const { values } = parseArgs({ options: {
17
+ cwd: { type: 'string' }, 'turns-back': { type: 'string', default: '1' }, help: { type: 'boolean', short: 'h' },
18
+ } });
19
+ if (values.help) console.log('Usage: previous_reply.mjs --cwd PROJECT [--turns-back N]');
20
+ else {
21
+ if (!values.cwd || !isAbsolute(values.cwd) || !/^[1-9]\d*$/.test(values['turns-back']) ||
22
+ !Number.isSafeInteger(Number(values['turns-back']))) throw new Error('invalid_arguments');
23
+ const target = previousReply(values.cwd, { noHistory: true, turnsBack: Number(values['turns-back']) });
24
+ if (!target) throw new Error('previous_reply_unavailable');
25
+ console.log(JSON.stringify({ question: target.question, draft: target.draft }));
26
+ }
27
+ } catch (error) {
28
+ console.error(error.message === 'previous_reply_unavailable' ? error.message : 'invalid_arguments');
29
+ process.exitCode = 1;
30
+ }
31
+ }
@@ -0,0 +1,28 @@
1
+ import { readEditorPrompt } from '../../../hooks/editor_prompt.mjs';
2
+ import { record } from './cli_io.mjs';
3
+ import { CREDENTIAL, DISPLAY_CONTROL } from './text_guard.mjs';
4
+
5
+ export class MaterialError extends Error {}
6
+
7
+ export function validateSourceText(value, maxBytes) {
8
+ if (typeof value !== 'string' || !value.trim() || !value.isWellFormed()) throw new MaterialError('invalid_input');
9
+ if (maxBytes !== undefined && Buffer.byteLength(value) > maxBytes) throw new MaterialError('input_too_large');
10
+ if (CREDENTIAL.test(value)) throw new MaterialError('possible_credential');
11
+ if (DISPLAY_CONTROL.test(value)) throw new MaterialError('display_control');
12
+ }
13
+
14
+ export function primaryMaterial(data) {
15
+ if (!record(data) || Object.keys(data).length !== 2 || !Object.hasOwn(data, 'question') || !Object.hasOwn(data, 'draft'))
16
+ throw new MaterialError('invalid_input');
17
+ validateSourceText(data.question);
18
+ validateSourceText(data.draft);
19
+ return { question: data.question, draft: data.draft };
20
+ }
21
+
22
+ export function projectMaterial(primary, cwd, mode) {
23
+ let prompt;
24
+ try { prompt = readEditorPrompt(cwd ?? process.cwd(), mode); }
25
+ catch { throw new MaterialError('editor_prompt_invalid'); }
26
+ const evidence = prompt.source === 'custom' ? { ...primary, editor_prompt: prompt.text } : primary;
27
+ return { evidence, prompt };
28
+ }
@@ -1,203 +1,114 @@
1
- // Protect literals before editing a reply, then verify and restore them locally.
2
- import { createHash } from 'node:crypto';
3
- import { isDeepStrictEqual } from 'node:util';
4
1
  import { isMain, readLimited, record, utf8 } from './cli_io.mjs';
5
2
 
6
- export const MAX_TEXT = 128 * 1024;
7
- export const MAX_REQUEST = 8 * MAX_TEXT;
8
- export const PREFIX = '⟦CP:';
9
- export const MARKER = /⟦CP:[0-9a-f]{16}:[QD]:[1-9][0-9]*⟧/g;
10
3
  // Allow ordinary tabs, line endings and RTL letters. Reject terminal and bidi controls.
11
4
  export const DISPLAY_CONTROL = /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/;
12
- export const CREDENTIAL = new RegExp([
5
+ const CREDENTIAL_SIGNATURE = new RegExp([
13
6
  /-----BEGIN [A-Z ]*PRIVATE KEY-----/.source,
14
7
  /sk-(?:proj-|svcacct-)?[\p{L}\p{N}_-]{16,}|gh[pousr]_[A-Za-z0-9]{16,}/u.source,
15
8
  /github_pat_[A-Za-z0-9_]{16,}|AKIA[A-Z0-9]{16}|ASIA[A-Z0-9]{16,}/.source,
16
9
  /AIza[A-Za-z0-9_-]{28,}|xox[baprs]-[\p{L}\p{N}_-]{12,}/u.source,
17
10
  /eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/.source,
18
- /\bbearer\s+[^\s,;'"]{8,}/.source,
19
- /\b(?:proxy-)?authorization["']?[ \t]*:[ \t]*["']?[^\s,;"']+/.source,
20
11
  /\b(?:set-cookie|cookie)["']?[ \t]*:[ \t]*["']?[^\s=;]+=[^\s;]+/.source,
21
12
  /:\/\/[^/\s:@]+:[^/\s@]+@/.source,
22
- /(?:api[_-]?key|access[_-]?token|auth[_-]?token|token|password|passwd|secret|密钥|密码|令牌)["']?\s*[:=:]\s*["']?[^\s,;“”"']+/.source,
23
13
  ].join('|'), 'iu');
24
14
 
25
- const EXACT = new RegExp([
15
+ const CREDENTIAL_FIELDS = [
16
+ /\bbearer\s+["'“‘`]*([^\s,;“”‘’'"`]{8,})/giu,
17
+ /\b(?:proxy-)?authorization["']?[ \t]*:[ \t]*["'“‘`]*(?:(?:bearer|basic|token)[ \t]+)?["'“‘`]*([^\s,;“”‘’"'`]+)/giu,
18
+ /(?:api[_-]?key|access[_-]?token|auth[_-]?token|token|password|passwd|secret|密钥|密码|令牌)["']?\s*[:=:]\s*["'“‘`]*([^\s,;“”‘’"'`]+)/giu,
19
+ ];
20
+ const PLACEHOLDER = /^(?:\$[A-Za-z_][\w]*|\$\{[A-Za-z_][\w]*\}|<(?:your[-_ ]?)?(?:api[-_ ]?key|key|token|password|secret|(?:你的)?(?:密钥|密码|令牌))>|\[REDACTED\]|YOUR[_-](?:API[_-]KEY|TOKEN|PASSWORD|SECRET)(?:[_-]HERE)?)$/iu;
21
+
22
+ export const CREDENTIAL = {
23
+ test(text) {
24
+ if (CREDENTIAL_SIGNATURE.test(text)) return true;
25
+ for (const pattern of CREDENTIAL_FIELDS) {
26
+ for (const [, value] of text.matchAll(pattern)) {
27
+ if (!PLACEHOLDER.test(value.replace(/[.。,;!?、]+$/u, ''))) return true;
28
+ }
29
+ }
30
+ return false;
31
+ },
32
+ };
33
+
34
+ const EXACT_ZH = new RegExp([
26
35
  /(?:只(?:需|要)?(?:给(?:我)?|输出|返回|提供|回答|写)(?:出)?|仅(?:输出|返回|提供)|直接输出|务必输出)\s*(?:一(?:条|行|个)\s*)?(?:可执行的?)?\s*(?:命令|代码|json|yaml|toml|xml|sql|补丁|diff|原文)/.source,
27
36
  /(?:只要|仅需)\s*(?:一(?:条|行|个)\s*)?(?:命令|代码|json|补丁)/.source,
28
37
  /(?:原样|逐字|一字不改地?)\s*(?:输出|返回|复述|引用|复制)/.source,
29
38
  /(?:固定|严格按(?:照)?|完全按照|严格遵守)(?:指定|以下|下面|这个|此)?格式/.source,
30
- /\b(?:return|output|reply with|give me|print)\s+(?:only|exactly)\b/.source,
31
- /\b(?:json|yaml|code|commands?|patch|diff)\s+only\b|\bverbatim\b/.source,
32
39
  ].join('|'), 'iu');
33
40
 
34
- const STRUCTURED = /^(?:diff --git |@@ |\*\*\* (?:Begin Patch|End Patch)|--- [ab]\/|[ \t]*:?-{3,}:?[ \t]*\|[ \t]*:?-{3,}:?|#!|<\?xml|<!doctype\s|\{\s*"|(?:async )?def [\p{L}\p{N}_]+\(|class [\p{L}\p{N}_]+[(:]|#include\s*<|(?:import|from) [\p{L}\p{N}_.]+(?:\s|$)|(?:const|let|function) [\p{L}\p{N}_$]+[ (=:])/imu;
41
+ const ENGLISH_DIRECTIVE = /(?:^|[\n,;.!?])\s*(?:(?:please|then|and|can you|could you|you must|you should)\s+)*/.source;
42
+ const EXACT_EN = new RegExp(ENGLISH_DIRECTIVE + '(?:' + [
43
+ /\b(?:return|output|reply with|give me|print)\s+exactly\b/.source,
44
+ /\b(?:return|output|reply with|give me|print)\s+only\s+(?:(?:the|a|an|one|single|valid|raw)\s+)*(?:json|yaml|toml|xml|sql|code|commands?|patch|diff)\b/.source,
45
+ /\breturn\s+only\s+(?:the\s+)?(?:polished\s+)?(?:text|reply|answer)\s+as\s+(?:json|yaml|xml|code)\b/.source,
46
+ /\b(?:json|yaml|code|commands?|patch|diff)\s+only\b/.source,
47
+ /\b(?:reproduce|quote|repeat|copy|keep|return|print|output)\s+(?:(?:this|that|it|the|my|following|original|text|quote|passage|answer|response)\s+)*verbatim\b/.source,
48
+ ].join('|') + ')', 'iu');
49
+
50
+ const STRUCTURED = /^(?:diff --git |@@ |\*\*\* (?:Begin Patch|End Patch)|--- [ab]\/|[ \t]*:?-{3,}:?[ \t]*\|[ \t]*:?-{3,}:?|#!|<\?xml|<!doctype\s|\{\s*"|(?:async )?def [\p{L}\p{N}_]+\(|class [\p{L}\p{N}_]+[(:]|#include\s*<|from [\p{L}\p{N}_.]+ import |import [\p{L}\p{N}_.]+(?:$|[,;]| as )|(?:const|let|function) [\p{L}\p{N}_$]+[ (=:])/mu;
35
51
  const COMMANDS = /(?:git|curl|wget|sudo|npm|npx|pnpm|yarn|bun|pip[0-9.]*|python[0-9.]*|pwd|whoami|node|bash|zsh|sh|docker|kubectl|make|cargo|go|brew|env|ls|cd|cat|echo|printf|rm|mv|cp|mkdir|chmod|rg|grep|sed|awk|ssh|scp|codex|agy)/.source;
36
- // Expand case only within ASCII units. Node 22 does not support scoped regexp modifiers.
37
- const asciiUnits = 'GiB|MiB|KiB|GB|MB|KB|milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|hours?|hrs?|Hz|kg|km|px|tokens?'
38
- .replace(/[a-z]/gi, letter => '[' + letter.toLowerCase() + letter.toUpperCase() + ']');
39
- const UNITS = '(?:' + asciiUnits + ')(?![A-Za-z])|%|‰|%|毫秒|秒钟|秒|分钟|小时|天|周|个月|月|年|元|美元|个|条|项|处|次|份|台|行|列|页|组|步|种|位|人|字节|字|倍|成|万|亿|公里|米';
40
- const CHINESE_DIGITS = '[零〇一二两三四五六七八九十百千万亿]+(?:点[零〇一二三四五六七八九]+)?';
41
- const WORD_START = /(?<![A-Za-z0-9_./-])/.source;
42
- const FILE = [
43
- /(?:[A-Za-z]:[\\/]|~?\/|\.{1,2}\/|\\\\)[^\s`<>\[\](),。;、:!?]+/.source,
44
- /[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)+(?::\p{Nd}+)?/u.source,
45
- /(?:[A-Za-z0-9_.-]+\.(?:py|ts|tsx|js|jsx|mjs|rs|go|java|c|cpp|h|sh|json|toml|ya?ml|md|txt|csv|log|sql|html|css|png|jpg|svg|pdf)|\.(?:env|gitignore|npmrc))(?::\p{Nd}+)?/u.source,
46
- ].join('|');
47
- const INLINE = new RegExp([
48
- /(?<bracket>!?\[)|(?<ticks>`+)/.source,
49
- /[^\r\n]*|【[^【】\r\n]+】/.source,
50
- /“[^“”\r\n]+”|「[^「」\r\n]+」|『[^『』\r\n]+』|"[^"\r\n]+"|(?<![A-Za-z0-9])'[^'\r\n]+'(?![A-Za-z0-9])/.source,
51
- WORD_START + /(?:[A-Za-z][A-Za-z0-9+.-]*:\/\/|mailto:|www\.)[^\s<>`\[\]"',。;、!?]+/.source,
52
- WORD_START + /(?:[A-Za-z0-9-]+\.)+(?:com|org|net|cn|io|dev|app|edu|gov)(?![A-Za-z0-9-])(?:\/[^\s<>`\[\],。;、!?]*)?/.source,
53
- /<\/?[A-Za-z][^<>\r\n]*>/.source,
54
- WORD_START + COMMANDS + /(?=[ \t]+[-~./A-Za-z0-9'"$])[ \t]+[ -~]+/.source,
55
- '(?:' + /(?<![\p{L}\p{N}_./-])[A-Za-z][\p{L}\p{N}_.-]*/u.source + '|' + WORD_START + '[A-Za-z][A-Za-z0-9_.-]*)[ \\t]+--?[A-Za-z][ -~]*',
56
- WORD_START + '(?:' + FILE + ')',
57
- WORD_START + /--[A-Za-z][A-Za-z0-9_-]*/.source,
58
- WORD_START + /(?=[A-Za-z._-]*\p{Nd})[A-Za-z][A-Za-z0-9._-]*\p{Nd}[A-Za-z0-9._-]*/u.source,
59
- /[+−-]?\p{Nd}+(?:[,.:/–-]\p{Nd}+)*/u.source + '(?:[ \\t]*(?:' + UNITS + '))?',
60
- '第' + CHINESE_DIGITS + '(?:' + UNITS + ')?',
61
- '(?<![零〇一二两三四五六七八九十百千万亿])' + CHINESE_DIGITS + '(?:' + UNITS + ')',
62
- '(?<=[剩共为从到约有])' + CHINESE_DIGITS,
63
- ].join('|'), 'gu');
64
52
  const FENCE = /^ {0,3}(`{3,}|~{3,})/;
65
- const FROZEN_LINE = /^(?:[ \t]*>| {4,}\S|\t\S|[ \t]*\[[^\]]+\]:|[ \t]*\|.*\||[ \t]*(?:-{3,}|\*{3,}|_{3,})[ \t]*$|[ \t]*(?:\$ |PS> )\S)/;
53
+ const NON_PROSE_LINE = /^(?:[ \t]*>| {4,}\S|\t\S|[ \t]*\[[^\]]+\]:|[ \t]*\|.*\||[ \t]*(?:-{3,}|\*{3,}|_{3,})[ \t]*$|[ \t]*(?:\$ |PS> )\S)/;
66
54
  const COMMAND_LINE = new RegExp('^[ \\t]*(?:' + COMMANDS + /(?:[ \t]+[ -~]*)?|(?:\.\/|~\/|\/)[A-Za-z0-9_./-]+[ \t]+[ -~]+/.source + ')$');
67
- const STRUCTURE = /^([ \t]*)(#{1,6}(?=\s)|[-*+](?=\s)|[0-9]+[.)](?=\s)|>|\||`{3,}|~{3,}|\[[^\]]+\]:)/;
68
55
 
69
56
  class Skip extends Error {}
70
57
 
71
- function balancedEnd(line, start, opener, closer) {
72
- let depth = 0;
73
- for (let i = start; i < line.length; i++) {
74
- if (line[i] === '\\') i++;
75
- else if (line[i] === opener) depth++;
76
- else if (line[i] === closer && --depth === 0) return i + 1;
77
- }
78
- throw new Skip('ambiguous_markup');
79
- }
80
-
81
- function maskInline(line, protect) {
82
- let pos = 0;
83
- const pieces = [];
84
- for (;;) {
85
- INLINE.lastIndex = pos;
86
- const match = INLINE.exec(line);
87
- if (!match) return pieces.join('') + line.slice(pos);
88
- let end = INLINE.lastIndex;
89
- if (match.groups.bracket) {
90
- end = balancedEnd(line, end - 1, '[', ']');
91
- if (line[end] === '(' || line[end] === '[')
92
- end = balancedEnd(line, end, line[end], line[end] === '(' ? ')' : ']');
93
- } else if (match.groups.ticks) {
94
- const ticks = /`+/g;
95
- ticks.lastIndex = end;
96
- let closer;
97
- do { closer = ticks.exec(line); } while (closer && closer[0].length !== match[0].length);
98
- if (!closer) throw new Skip('ambiguous_markup');
99
- end = ticks.lastIndex;
100
- }
101
- const literal = line.slice(match.index, end).replace(/[ \t]+$/, '');
102
- pieces.push(line.slice(pos, match.index), protect(literal));
103
- pos = match.index + literal.length;
104
- }
105
- }
106
-
107
- function maskText(text, nonce, role) {
108
- const literals = new Map();
109
- const protect = value => {
110
- const marker = PREFIX + nonce + ':' + role + ':' + (literals.size + 1) + '⟧';
111
- literals.set(marker, value);
112
- return marker;
113
- };
114
- const output = [];
115
- const lines = text.match(/[^\r\n\u2028\u2029]*(?:\r\n|[\r\n\u2028\u2029])|[^\r\n\u2028\u2029]+/g) ?? [];
116
- for (let index = 0; index < lines.length; index++) {
117
- const line = lines[index];
118
- const body = line.replace(/[\r\n]+$/, '');
119
- const opening = FENCE.exec(body)?.[1];
120
- if (opening) {
121
- const closing = new RegExp('^ {0,3}' + opening[0] + '{' + opening.length + ',}[ \\t]*$');
122
- const start = index;
123
- while (++index < lines.length && !closing.test(lines[index].replace(/[\r\n]+$/, ''))) {}
124
- if (index === lines.length) throw new Skip('ambiguous_markup');
125
- const last = lines[index].replace(/[\r\n]+$/, '');
126
- output.push(protect(lines.slice(start, index).join('') + last), lines[index].slice(last.length));
127
- } else {
128
- const masked = FROZEN_LINE.test(body) || COMMAND_LINE.test(body) ? protect(body) : maskInline(body, protect);
129
- output.push(masked, line.slice(body.length));
130
- }
131
- }
132
- return [output.join(''), literals];
133
- }
134
-
135
- function structure(text) {
136
- return text.split(/\r\n|[\r\n\u2028\u2029]/).map(line => STRUCTURE.exec(line)?.slice(1)).filter(Boolean);
137
- }
138
-
139
58
  function validateText(value, emptyReason) {
140
59
  if (typeof value !== 'string' || !value.trim()) throw new Skip(emptyReason);
141
60
  if (!value.isWellFormed()) throw new Skip('invalid_input');
142
- if (Buffer.byteLength(value) > MAX_TEXT) throw new Skip('size_limit');
143
61
  if (DISPLAY_CONTROL.test(value)) throw new Skip('display_control');
144
62
  if (CREDENTIAL.test(value)) throw new Skip('possible_credential');
145
63
  }
146
64
 
147
- function prepare(payload) {
148
- if (!record(payload)) throw new Skip('invalid_input');
149
- const { question, draft } = payload;
150
- for (const value of [question, draft]) {
151
- validateText(value, 'invalid_input');
152
- if (value.includes(PREFIX)) throw new Skip('reserved_marker');
153
- }
154
- if (EXACT.test(question) || STRUCTURED.test(draft) || ['true','false','null'].includes(draft.trim()))
155
- throw new Skip('fixed_format');
156
- if (/^\s*[\[{]/.test(draft)) {
157
- try { JSON.parse(draft); throw new Skip('fixed_format'); }
65
+ function isFixedFormat(draft) {
66
+ const trimmed = draft.trim();
67
+ if (/^(?:diff --git |\*\*\* Begin Patch(?:\r?\n|$)|--- [^\r\n]+\r?\n\+\+\+ )/.test(trimmed)) return true;
68
+ if (/^(`{1,2})[^`\r\n]+\1$/.test(trimmed)) return true;
69
+ if (/^[\[{"]/.test(trimmed) || ['true', 'false', 'null'].includes(trimmed)) {
70
+ try { JSON.parse(trimmed); return true; }
158
71
  catch (error) { if (!(error instanceof SyntaxError)) throw error; }
159
72
  }
160
- const encoded = JSON.stringify([question, draft]);
161
- const nonce = createHash('sha256').update(encoded).digest('hex').slice(0, 16);
162
- const [protectedQuestion] = maskText(question, nonce, 'Q');
163
- const [protectedDraft, literals] = maskText(draft, nonce, 'D');
164
- if ([protectedQuestion, protectedDraft].some(value => Buffer.byteLength(value) > MAX_TEXT))
165
- throw new Skip('size_limit');
166
- if (!/[\p{L}\p{Nl}\p{No}]/u.test(protectedDraft.replace(MARKER, ''))) throw new Skip('fixed_format');
167
- return { protectedQuestion, protectedDraft, literals, nonce };
73
+
74
+ let fence = null;
75
+ let hasProse = false;
76
+ for (const line of draft.split(/\r\n|[\r\n\u2028\u2029]/)) {
77
+ const delimiter = FENCE.exec(line)?.[1];
78
+ if (fence) {
79
+ if (delimiter?.[0] === fence[0] && delimiter.length >= fence.length && line.trim() === delimiter) fence = null;
80
+ } else if (delimiter) {
81
+ fence = delimiter;
82
+ } else if (!STRUCTURED.test(line) && !COMMAND_LINE.test(line) && !NON_PROSE_LINE.test(line) && /[\p{L}\p{Nl}\p{No}]/u.test(line)) {
83
+ hasProse = true;
84
+ }
85
+ }
86
+ if (fence) throw new Skip('ambiguous_markup');
87
+ return !hasProse;
168
88
  }
169
89
 
170
- export function run(action, payload) {
90
+ export function prepare(payload) {
171
91
  try {
172
- const { protectedQuestion, protectedDraft, literals, nonce } = prepare(payload);
173
- if (action === 'prepare')
174
- return { bypass: false, protected_question: protectedQuestion, protected_draft: protectedDraft };
175
- const { candidate } = payload;
176
- validateText(candidate, 'empty_candidate');
177
- if (!isDeepStrictEqual(candidate.match(MARKER) ?? [], [...literals.keys()]) || candidate.replace(MARKER, '').includes(PREFIX))
178
- throw new Skip('marker_mismatch');
179
- const text = candidate.replace(MARKER, marker => literals.get(marker));
180
- if (Buffer.byteLength(text) > MAX_TEXT) throw new Skip('size_limit');
181
- if (CREDENTIAL.test(text)) throw new Skip('possible_credential');
182
- const [, actual] = maskText(text, nonce, 'D');
183
- if (!isDeepStrictEqual([...actual.values()], [...literals.values()])) throw new Skip('literal_changed');
184
- if (!isDeepStrictEqual(structure(text), structure(payload.draft)) || STRUCTURED.test(text))
185
- throw new Skip('structure_changed');
186
- return { ok: true, text };
92
+ if (!record(payload)) throw new Skip('invalid_input');
93
+ const { question, draft } = payload;
94
+ validateText(question, 'invalid_input');
95
+ validateText(draft, 'invalid_input');
96
+ if (EXACT_ZH.test(question) || EXACT_EN.test(question) || isFixedFormat(draft)) throw new Skip('fixed_format');
97
+ return { bypass: false };
187
98
  } catch (error) {
188
99
  if (!(error instanceof Skip)) throw error;
189
- return action === 'prepare' ? { bypass: true, reason: error.message } : { ok: false, reason: error.message };
100
+ return { bypass: true, reason: error.message };
190
101
  }
191
102
  }
192
103
 
193
104
  if (isMain(import.meta.url)) {
194
105
  const [action, ...extra] = process.argv.slice(2);
195
- if (action === '--help' || action === '-h') console.log('Usage: text_guard.mjs prepare|restore');
196
- else if (!['prepare', 'restore'].includes(action) || extra.length) process.exitCode = 2;
106
+ if (action === '--help' || action === '-h') console.log('Usage: text_guard.mjs prepare');
107
+ else if (action !== 'prepare' || extra.length) process.exitCode = 2;
197
108
  else {
198
109
  let payload;
199
- try { payload = JSON.parse(utf8(await readLimited(process.stdin, MAX_REQUEST))); }
110
+ try { payload = JSON.parse(utf8(await readLimited(process.stdin))); }
200
111
  catch { payload = null; }
201
- console.log(JSON.stringify(run(action, payload)));
112
+ console.log(JSON.stringify(prepare(payload)));
202
113
  }
203
114
  }