@kyun.si/codex-polish 0.0.1-beta.3 → 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 +15 -0
  2. package/CONTRIBUTING.en.md +6 -6
  3. package/CONTRIBUTING.md +6 -6
  4. package/README.en.md +45 -17
  5. package/README.md +45 -17
  6. package/SECURITY.en.md +5 -3
  7. package/SECURITY.md +4 -2
  8. package/docs/technical.en.md +57 -41
  9. package/docs/technical.md +54 -34
  10. package/evaluation/README.en.md +1 -1
  11. package/evaluation/README.md +1 -1
  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 +137 -114
  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 -33
  21. package/plugins/codex-polish/skills/codex-polish/WORKFLOW.md +47 -0
  22. package/plugins/codex-polish/skills/codex-polish/references/acp.md +30 -17
  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 -166
  33. package/plugins/codex-polish/skills/codex-polish-setup/SKILL.md +37 -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
@@ -2,18 +2,16 @@
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import { isDeepStrictEqual, parseArgs } from 'node:util';
5
- import { CREDENTIAL, DISPLAY_CONTROL, PREFIX } from '../skills/codex-polish/scripts/text_guard.mjs';
5
+ import { originalReplyText } from '../skills/codex-polish/scripts/format_reply.mjs';
6
+ import { CREDENTIAL, DISPLAY_CONTROL } from '../skills/codex-polish/scripts/text_guard.mjs';
6
7
  import { DEFAULT_HISTORY_TURNS, MAX_HISTORY_TURNS } from './preferences.mjs';
7
8
 
8
- export const MAX_HISTORY_BYTES = 16 * 1024;
9
- export const MAX_SCAN_BYTES = 4 * 1024 * 1024;
10
- const MAX_HEADER_BYTES = 1024 * 1024;
11
9
  const HOST_TEXT = /^\s*(?:<\/?(?:environment_context|system-reminder|subagent_notification|turn_aborted|user_shell_command|task-notification|user_instructions|skills_instructions|permissions|developer_instructions|instructions|goal_context|ide_context|local-command-[\w-]+|agent_message|skill|t3_code_[\w-]+)(?:[\s>/])|# AGENTS\.md instructions\b|# (?:Context from my IDE setup|Files mentioned by the user):|Subagent completed:)/im;
12
10
  const ATTACHMENTS = ['images', 'file_ids', 'local_images', 'audio', 'audio_references', 'local_audio'];
13
11
 
14
12
  function safeText(text) {
15
- return typeof text === 'string' && text.trim() !== '' && text.length <= MAX_HISTORY_BYTES && text.isWellFormed() &&
16
- !CREDENTIAL.test(text) && !DISPLAY_CONTROL.test(text) && !HOST_TEXT.test(text) && !text.includes(PREFIX);
13
+ return typeof text === 'string' && text.trim() !== '' && text.isWellFormed() &&
14
+ !CREDENTIAL.test(text) && !DISPLAY_CONTROL.test(text) && !HOST_TEXT.test(text);
17
15
  }
18
16
 
19
17
  function textMessage(parts, kind) {
@@ -28,15 +26,22 @@ function present(value) {
28
26
  }
29
27
 
30
28
  class Turn {
31
- constructor(start) {
29
+ constructor(start, interrupted) {
32
30
  this.id = start.turn_id;
33
31
  this.events = [];
34
32
  this.messages = [];
35
33
  this.answer = null;
34
+ this.previousInput = [];
35
+ this.turnIds = new Set([this.id]);
36
36
  const source = start.turn_attribution ?? {};
37
37
  this.ordinary = typeof source === 'object' && !Array.isArray(source) &&
38
- [undefined, null, 'user', 'exec'].includes(source.turn_trigger) &&
39
- !source.initiating_agent_path && !source.parent_turn_id && (start.root_turn_id ?? this.id) === this.id;
38
+ [undefined, null, 'user', 'exec', 'composer', 'resume_interrupted_task'].includes(source.turn_trigger) &&
39
+ !source.initiating_agent_path && !source.parent_turn_id && (start.root_turn_id ?? this.id) === this.id &&
40
+ (source.root_turn_id ?? this.id) === this.id && (source.turn_id ?? this.id) === this.id;
41
+ if (this.ordinary && source.turn_trigger === 'resume_interrupted_task' && interrupted?.ordinary) {
42
+ this.previousInput = interrupted.userInput();
43
+ this.turnIds = new Set([...interrupted.turnIds, this.id]);
44
+ }
40
45
  }
41
46
 
42
47
  read(kind, item) {
@@ -64,92 +69,128 @@ class Turn {
64
69
  }
65
70
  }
66
71
 
72
+ userInput() {
73
+ return this.previousInput.concat(this.events.length ? this.events : this.messages);
74
+ }
75
+
67
76
  finish(completed) {
68
- const messages = this.events.length ? this.events : this.messages;
69
- if (completed.last_agent_message !== this.answer || !safeText(this.answer) || !messages.every(safeText)) return null;
70
- const user = messages.join('\n\n');
71
- if (Buffer.byteLength(user) + Buffer.byteLength(this.answer) > MAX_HISTORY_BYTES) return null;
72
- return { user, assistant: this.answer };
77
+ const messages = this.userInput();
78
+ if (completed.last_agent_message !== this.answer || !safeText(this.answer) || !messages.length || !messages.every(safeText)) return null;
79
+ return { user: messages.join('\n\n'), assistant: this.answer };
73
80
  }
74
81
  }
75
82
 
76
- function completedExchanges(data, sessionId, turnId, turns) {
83
+ // Snapshot the size at open and stream complete JSONL records. Only the current
84
+ // turn and the configured number of completed exchanges stay in memory.
85
+ function* records(fd, size) {
86
+ const chunkSize = 64 * 1024;
87
+ const decoder = new TextDecoder('utf-8', { fatal: true });
88
+ let pending = [];
89
+ for (let pos = 0; pos < size;) {
90
+ const chunk = Buffer.allocUnsafe(Math.min(chunkSize, size - pos));
91
+ const count = fs.readSync(fd, chunk, 0, chunk.length, pos);
92
+ if (!count) throw new Error('truncated transcript');
93
+ pos += count;
94
+ const raw = chunk.subarray(0, count);
95
+ let start = 0;
96
+ for (let newline = raw.indexOf(10); newline !== -1; newline = raw.indexOf(10, start)) {
97
+ const part = raw.subarray(start, newline);
98
+ const line = pending.length ? Buffer.concat([...pending, part]) : part;
99
+ pending = [];
100
+ yield JSON.parse(decoder.decode(line));
101
+ start = newline + 1;
102
+ }
103
+ if (start < raw.length) pending.push(raw.subarray(start));
104
+ }
105
+ if (pending.length) throw new Error('incomplete transcript');
106
+ }
107
+
108
+ function completedExchanges(rows, sessionId, turnId, limit, { previous = false, through = Infinity } = {}) {
77
109
  const last = [];
110
+ let count = 0;
111
+ let header = false;
78
112
  let active = null;
79
- for (const line of data.split('\n')) {
80
- const { type: kind, payload: item } = JSON.parse(line);
81
- if (typeof kind !== 'string') return [];
113
+ let interrupted = null;
114
+ for (const { type: kind, payload: item } of rows) {
115
+ if (typeof kind !== 'string' || (!header && kind !== 'session_meta')) return null;
82
116
  if (!['session_meta', 'event_msg', 'response_item'].includes(kind)) continue;
83
- if (!item || typeof item !== 'object' || Array.isArray(item)) return [];
117
+ if (!item || typeof item !== 'object' || Array.isArray(item)) return null;
84
118
  if (kind === 'session_meta') {
85
- if (item.id !== sessionId) return [];
119
+ if (header || item.id !== sessionId || item.parent_thread_id || item.forked_from_id ||
120
+ item.source?.subagent || (item.thread_source != null && item.thread_source !== 'user')) return null;
121
+ header = true;
86
122
  } else if (kind === 'event_msg' && item.type === 'task_started') {
87
- active = typeof item.turn_id === 'string' && item.turn_id ? new Turn(item) : null;
88
- } else if (kind === 'event_msg' && ['turn_aborted', 'thread_rolled_back'].includes(item.type)) {
123
+ active = typeof item.turn_id === 'string' && item.turn_id ? new Turn(item, interrupted) : null;
124
+ interrupted = null;
125
+ } else if (kind === 'event_msg' && item.type === 'turn_aborted') {
126
+ interrupted = item.reason === 'interrupted' && active?.id === item.turn_id ? active : null;
89
127
  active = null;
90
- if (item.type === 'thread_rolled_back') last.length = 0;
128
+ } else if (kind === 'event_msg' && item.type === 'thread_rolled_back') {
129
+ interrupted = active = null;
130
+ count = 0;
131
+ last.length = 0;
91
132
  } else if (kind === 'event_msg' && item.type === 'task_complete') {
92
- if (active?.ordinary && active.id !== turnId && item.turn_id === active.id && !present(item.error) &&
93
- (active.events.length || active.messages.length) && item.last_agent_message) {
94
- last.push(active.finish(item));
95
- if (last.length > Math.min(turns, MAX_HISTORY_TURNS)) last.shift();
133
+ if (active?.ordinary && !active.turnIds.has(turnId) && item.turn_id === active.id &&
134
+ (item.last_agent_message || (previous && active.answer != null)) &&
135
+ (previous || (!present(item.error) && active.userInput().length))) {
136
+ count++;
137
+ if (limit && count <= through) {
138
+ last.push(present(item.error) ? null : active.finish(item));
139
+ if (last.length > limit) last.shift();
140
+ }
96
141
  }
97
- active = null;
142
+ active = interrupted = null;
98
143
  } else {
99
144
  active?.read(kind, item);
100
145
  }
101
146
  }
102
- if (!active?.ordinary || (turnId !== null && active.id !== turnId)) return [];
103
- let budget = MAX_HISTORY_BYTES;
104
- const result = [];
105
- for (const exchange of last.reverse()) {
106
- if (!exchange) continue;
107
- budget -= Buffer.byteLength(exchange.user) + Buffer.byteLength(exchange.assistant);
108
- if (budget < 0) break;
109
- result.push(exchange);
110
- }
111
- return result.reverse();
147
+ if (!active?.ordinary || (turnId !== null && !active.turnIds.has(turnId))) return null;
148
+ return { last, count };
112
149
  }
113
150
 
114
- function readRange(fd, start, length) {
115
- const raw = Buffer.alloc(length);
116
- let count = 0;
117
- while (count < length) {
118
- const n = fs.readSync(fd, raw, count, length - count, start + count);
119
- if (!n) break;
120
- count += n;
121
- }
122
- return raw.subarray(0, count);
123
- }
124
-
125
- export function readHistory(file, sessionId, turnId = null, turns = DEFAULT_HISTORY_TURNS) {
126
- if (!Number.isInteger(turns) || turns <= 0 || !sessionId || typeof file !== 'string' || !path.isAbsolute(file)) return [];
151
+ function withTranscript(file, read) {
152
+ if (typeof file !== 'string' || !path.isAbsolute(file)) return null;
127
153
  try {
128
154
  const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK | fs.constants.O_NOFOLLOW);
129
155
  try {
130
156
  const info = fs.fstatSync(fd);
131
- if (!info.isFile()) return [];
132
- const head = readRange(fd, 0, Math.min(info.size, MAX_HEADER_BYTES));
133
- const headerEnd = head.indexOf(10) + 1;
134
- if (!headerEnd || info.size <= headerEnd) return [];
135
- const decoder = new TextDecoder('utf-8', { fatal: true });
136
- const { type, payload } = JSON.parse(decoder.decode(head.subarray(0, headerEnd)));
137
- if (type !== 'session_meta' || payload?.id !== sessionId || payload.parent_thread_id || payload.forked_from_id) return [];
138
- const start = Math.max(headerEnd, info.size - MAX_SCAN_BYTES);
139
- let raw = readRange(fd, start, info.size - start);
140
- if (raw.length !== info.size - start || raw.at(-1) !== 10) return [];
141
- // Drop the truncated first line before decoding, since it may start mid-codepoint.
142
- if (start > headerEnd) raw = raw.subarray(raw.indexOf(10) + 1);
143
- if (!raw.length) return [];
144
- return completedExchanges(decoder.decode(raw.subarray(0, -1)), sessionId, turnId, turns);
157
+ if (!info.isFile()) return null;
158
+ return read(() => records(fd, info.size));
145
159
  } finally {
146
160
  fs.closeSync(fd);
147
161
  }
148
162
  } catch {
149
- return [];
163
+ return null;
150
164
  }
151
165
  }
152
166
 
167
+ export function readHistory(file, sessionId, turnId = null, turns = DEFAULT_HISTORY_TURNS) {
168
+ if (!Number.isInteger(turns) || turns <= 0 || !sessionId) return [];
169
+ const exchanges = withTranscript(file, rows =>
170
+ completedExchanges(rows(), sessionId, turnId, Math.min(turns, MAX_HISTORY_TURNS)));
171
+ return exchanges?.last.filter(Boolean) ?? [];
172
+ }
173
+
174
+ export function readPreviousReply(file, sessionId, turnId, turns = DEFAULT_HISTORY_TURNS, turnsBack = 1) {
175
+ if (!turnId || !sessionId || !Number.isSafeInteger(turnsBack) || turnsBack < 1 ||
176
+ !Number.isInteger(turns) || turns < 0 || turns > MAX_HISTORY_TURNS) return null;
177
+ return withTranscript(file, rows => {
178
+ let through = Infinity;
179
+ if (turnsBack > 1) {
180
+ // Find the selected ordinal without retaining every intervening answer.
181
+ const scan = completedExchanges(rows(), sessionId, turnId, 0, { previous: true });
182
+ if (!scan || scan.count < turnsBack) return null;
183
+ through = scan.count - turnsBack + 1;
184
+ }
185
+ const scan = completedExchanges(rows(), sessionId, turnId, turns + 1, { previous: true, through });
186
+ const target = scan?.last.pop();
187
+ if (!target) return null;
188
+ const draft = originalReplyText(target.assistant);
189
+ if (!safeText(draft)) return null;
190
+ return { question: target.user, draft, history: scan.last.filter(Boolean) };
191
+ });
192
+ }
193
+
153
194
  if (process.argv[1] && fs.existsSync(process.argv[1]) && fs.realpathSync(process.argv[1]) === import.meta.filename) {
154
195
  try {
155
196
  const { values } = parseArgs({ options: {