@etilic/rutter_ai 0.0.0-stage → 0.2.1

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 (66) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +80 -2
  3. package/THIRD_PARTY_LICENSES.txt +111 -0
  4. package/bin/cli.cjs +94 -0
  5. package/bin/npm-runtime.cjs +125 -0
  6. package/client/darwin-arm64/bootstrap.cjs +159 -0
  7. package/client/darwin-arm64/compat.cjs +61 -0
  8. package/client/darwin-arm64/hook.cjs +597 -0
  9. package/client/darwin-arm64/install.cjs +555 -0
  10. package/client/darwin-arm64/manifest.json +1 -0
  11. package/client/darwin-arm64/mcp.cjs +5666 -0
  12. package/client/darwin-arm64/migration.cjs +53 -0
  13. package/client/darwin-arm64/node_sqlite3.node +0 -0
  14. package/client/darwin-arm64/ordered-upload.cjs +236 -0
  15. package/client/darwin-arm64/outbox.cjs +218 -0
  16. package/client/darwin-arm64/recovery.cjs +61 -0
  17. package/client/darwin-arm64/sqlite3.cjs +217 -0
  18. package/client/darwin-arm64/transcript.cjs +138 -0
  19. package/client/darwin-arm64/upload.cjs +446 -0
  20. package/client/darwin-arm64/worker.cjs +76 -0
  21. package/client/darwin-x64/bootstrap.cjs +159 -0
  22. package/client/darwin-x64/compat.cjs +61 -0
  23. package/client/darwin-x64/hook.cjs +597 -0
  24. package/client/darwin-x64/install.cjs +555 -0
  25. package/client/darwin-x64/manifest.json +1 -0
  26. package/client/darwin-x64/mcp.cjs +5666 -0
  27. package/client/darwin-x64/migration.cjs +53 -0
  28. package/client/darwin-x64/node_sqlite3.node +0 -0
  29. package/client/darwin-x64/ordered-upload.cjs +236 -0
  30. package/client/darwin-x64/outbox.cjs +218 -0
  31. package/client/darwin-x64/recovery.cjs +61 -0
  32. package/client/darwin-x64/sqlite3.cjs +217 -0
  33. package/client/darwin-x64/transcript.cjs +138 -0
  34. package/client/darwin-x64/upload.cjs +446 -0
  35. package/client/darwin-x64/worker.cjs +76 -0
  36. package/client/linux-x64-glibc/bootstrap.cjs +159 -0
  37. package/client/linux-x64-glibc/compat.cjs +61 -0
  38. package/client/linux-x64-glibc/hook.cjs +597 -0
  39. package/client/linux-x64-glibc/install.cjs +555 -0
  40. package/client/linux-x64-glibc/manifest.json +1 -0
  41. package/client/linux-x64-glibc/mcp.cjs +5666 -0
  42. package/client/linux-x64-glibc/migration.cjs +53 -0
  43. package/client/linux-x64-glibc/node_sqlite3.node +0 -0
  44. package/client/linux-x64-glibc/ordered-upload.cjs +236 -0
  45. package/client/linux-x64-glibc/outbox.cjs +218 -0
  46. package/client/linux-x64-glibc/recovery.cjs +61 -0
  47. package/client/linux-x64-glibc/sqlite3.cjs +217 -0
  48. package/client/linux-x64-glibc/transcript.cjs +138 -0
  49. package/client/linux-x64-glibc/upload.cjs +446 -0
  50. package/client/linux-x64-glibc/worker.cjs +76 -0
  51. package/client/win32-x64/bootstrap.cjs +159 -0
  52. package/client/win32-x64/compat.cjs +61 -0
  53. package/client/win32-x64/hook.cjs +597 -0
  54. package/client/win32-x64/install.cjs +555 -0
  55. package/client/win32-x64/manifest.json +1 -0
  56. package/client/win32-x64/mcp.cjs +5666 -0
  57. package/client/win32-x64/migration.cjs +53 -0
  58. package/client/win32-x64/node_sqlite3.node +0 -0
  59. package/client/win32-x64/ordered-upload.cjs +236 -0
  60. package/client/win32-x64/outbox.cjs +218 -0
  61. package/client/win32-x64/recovery.cjs +61 -0
  62. package/client/win32-x64/sqlite3.cjs +217 -0
  63. package/client/win32-x64/transcript.cjs +138 -0
  64. package/client/win32-x64/upload.cjs +446 -0
  65. package/client/win32-x64/worker.cjs +76 -0
  66. package/package.json +34 -4
@@ -0,0 +1,446 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const { request: fetch, performance } = require('./compat.cjs');
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const crypto = require('node:crypto');
9
+ const readline = require('./compat.cjs');
10
+ const { parseTranscript } = require('./transcript.cjs');
11
+ const { MemoryAccessError, readConfig, configPath, readSession, requestMemory, normalizeCatalog, validateSelection, publicError, diagnostic, diagnosticFile, configFingerprint } = require('./hook.cjs');
12
+
13
+ const MAX_QUEUE = 500;
14
+ const MAX_SEEN = 100000;
15
+ const MAX_TRANSCRIPT_BYTES = 32 * 1024 * 1024;
16
+ const MAX_MESSAGE = 8192;
17
+
18
+ function directory(env = process.env) { return path.join(env.HOME || require('node:os').homedir(), '.config', 'rutter-direct-memory'); }
19
+ function statePath(env) { return path.join(directory(env), 'upload-state.json'); }
20
+ function emptyState() { return { enabled: false, since: 0, queue: [], seen: {}, manually_disabled: false }; }
21
+ function readState(env, file = statePath(env)) {
22
+ try {
23
+ const state = JSON.parse(fs.readFileSync(file, 'utf8'));
24
+ if (typeof state.enabled !== 'boolean' || !Array.isArray(state.queue) || !state.seen || typeof state.seen !== 'object') throw new Error();
25
+ return state;
26
+ } catch (error) {
27
+ if (error.code === 'ENOENT') return emptyState();
28
+ throw new Error('本地上传状态无法读取,已停止采集。');
29
+ }
30
+ }
31
+ function writeState(env, state, file = statePath(env)) {
32
+ const dir = directory(env);
33
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
34
+ const temp = `${file}.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`;
35
+ fs.writeFileSync(temp, JSON.stringify(state), { mode: 0o600, flag: 'wx' });
36
+ fs.renameSync(temp, file);
37
+ fs.chmodSync(file, 0o600);
38
+ }
39
+
40
+ async function enableForSession(env = process.env) {
41
+ return withLock(env, async () => {
42
+ const config = readConfig(configPath(env));
43
+ const state = readState(env);
44
+ if (state.manually_disabled) throw new Error('Memory 上传已全局关闭,请先运行 upload.cjs --enable。');
45
+ if (!state.enabled) {
46
+ state.enabled = true;
47
+ state.since ||= Date.now();
48
+ // Legacy snapshots retain their original identity until migration proves it.
49
+ state.fingerprint ||= configFingerprint(config);
50
+ writeState(env, state);
51
+ }
52
+ return Date.now();
53
+ });
54
+ }
55
+
56
+ async function enableAfterInstall(env = process.env) {
57
+ return withLock(env, async () => {
58
+ const fingerprint = configFingerprint(readConfig(configPath(env)));
59
+ const previous = readState(env);
60
+ // Reinstall/Key rotation must not invalidate other windows' consent or
61
+ // erase legacy snapshots. Ordered recovery verifies stable user identity.
62
+ if (previous.enabled) return;
63
+ writeState(env, { ...previous, enabled: true, manually_disabled: false,
64
+ since: previous.since || Date.now(), fingerprint: previous.fingerprint || fingerprint });
65
+ });
66
+ }
67
+
68
+ async function withLock(env, fn) {
69
+ const started = performance.now();
70
+ const dir = directory(env);
71
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
72
+ const lock = path.join(dir, 'upload.lock');
73
+ let acquired = false;
74
+ for (let i = 0; i < 300; i++) {
75
+ try {
76
+ fs.mkdirSync(lock, { mode: 0o700 });
77
+ fs.writeFileSync(path.join(lock, 'owner'), String(process.pid), { mode: 0o600 });
78
+ acquired = true;
79
+ break;
80
+ }
81
+ catch (error) {
82
+ if (error.code !== 'EEXIST') throw error;
83
+ try {
84
+ if (Date.now() - fs.statSync(lock).mtimeMs > 120000) {
85
+ let owner;
86
+ try { owner = Number(fs.readFileSync(path.join(lock, 'owner'), 'utf8')); }
87
+ catch (ownerError) { if (ownerError.code === 'ENOENT') fs.rmdirSync(lock); continue; }
88
+ if (Number.isInteger(owner) && owner > 0) {
89
+ try { process.kill(owner, 0); }
90
+ catch (probeError) {
91
+ if (probeError.code === 'ESRCH') { fs.unlinkSync(path.join(lock, 'owner')); fs.rmdirSync(lock); }
92
+ }
93
+ }
94
+ }
95
+ } catch {}
96
+ await new Promise(resolve => setTimeout(resolve, 100));
97
+ }
98
+ }
99
+ diagnostic(env, 'lock', acquired ? 'ok' : 'failed', { duration_ms: performance.now() - started });
100
+ if (!acquired) throw new Error('上传队列正在使用,请稍后重试。');
101
+ try { return await fn(); } finally { fs.unlinkSync(path.join(lock, 'owner')); fs.rmdirSync(lock); }
102
+ }
103
+
104
+ function splitMessage(message) {
105
+ const chunks = [];
106
+ for (let index = 0; index < message.content.length;) {
107
+ let end = Math.min(index + MAX_MESSAGE, message.content.length);
108
+ if (end < message.content.length && /[\uD800-\uDBFF]/.test(message.content[end - 1])) end--;
109
+ const content = message.content.slice(index, end);
110
+ chunks.push({ ...message, content, direct_event_id: crypto.createHash('sha256').update(`${message.direct_event_id}:${index}`).digest('hex') });
111
+ index = end;
112
+ }
113
+ return chunks;
114
+ }
115
+
116
+ const UPGRADE_REQUIRED = 'Memory 服务或会话尚未支持 ordered-v1 有序上传,请升级服务器并重新加载会话;原待传数据已保留,未退回旧上传通道。';
117
+ async function flush(state, _config, env) {
118
+ if (!state.queue.length) return '';
119
+ diagnostic(env, 'upload', 'capture_blocked', { reason: 'capability', pending: state.queue.length });
120
+ return UPGRADE_REQUIRED;
121
+ }
122
+
123
+ async function capture({ client, event, env = process.env }) {
124
+ if (event?.hook_event_name !== 'Stop' || !['codex', 'claude'].includes(client)) return '';
125
+ if (typeof event.cwd !== 'string' || typeof event.session_id !== 'string') return '';
126
+ const session = readSession(client, event.session_id, path.resolve(event.cwd), env);
127
+ if (!session || session.mode === 'off' || !session.upload_enabled_at || !readState(env).enabled) return '';
128
+ if (session.conversation_upload?.protocol === 'ordered-v1') return require('./ordered-upload.cjs').capture({ client, event, env });
129
+ diagnostic(env, 'upload', 'capture_blocked', { reason: 'capability', id: `${client}:${event.session_id}` });
130
+ return UPGRADE_REQUIRED;
131
+ }
132
+
133
+ async function setConsent(enabled, env = process.env) {
134
+ return withLock(env, async () => {
135
+ const previous = readState(env);
136
+ if (enabled && previous.enabled) throw new Error('自动上传已开启;原待传队列未更改。');
137
+ const state = enabled ? { ...previous } : emptyState();
138
+ state.enabled = enabled;
139
+ state.manually_disabled = !enabled;
140
+ state.since = enabled ? previous.since || Date.now() : 0;
141
+ if (enabled) state.fingerprint ||= configFingerprint(readConfig(configPath(env)));
142
+ writeState(env, state);
143
+ if (!enabled) {
144
+ // Explicit opt-out also removes our migration snapshots and manual
145
+ // journals; the model client's original transcript is never modified.
146
+ fs.rmSync(path.join(directory(env), 'migration-backups'), { recursive: true, force: true });
147
+ fs.rmSync(path.join(directory(env), 'manual-uploads'), { recursive: true, force: true });
148
+ for (const file of fs.readdirSync(directory(env))) if (/^upload-state\.json\.[a-f0-9]{64}\.json$/.test(file)) {
149
+ // Key switching archives are also pending conversation bodies.
150
+ writeState(env, { ...emptyState(), manually_disabled: true }, path.join(directory(env), file));
151
+ }
152
+ }
153
+ if (!enabled && fs.existsSync(path.join(directory(env), 'outbox.sqlite'))) {
154
+ const queue = await require('./ordered-upload.cjs').open(env);
155
+ try { await queue.discardPending(); } finally { await queue.close(); }
156
+ }
157
+ });
158
+ }
159
+
160
+ async function retry(env = process.env, fetcher = fetch) {
161
+ return withLock(env, async () => {
162
+ const state = readState(env);
163
+ if (!state.enabled) return '自动上传未开启。';
164
+ return flush(state, readConfig(configPath(env)), env, fetcher);
165
+ });
166
+ }
167
+
168
+ // Manual uploads have their own progress journal. They must never flush another
169
+ // session's automatic queue or change its consent/binding settings.
170
+ class SessionUploadError extends Error {}
171
+ function redactKeys(value) {
172
+ // Keep offsets stable so splitMessage uses the same IDs as automatic upload.
173
+ return String(value).replace(/\bsk[-_][A-Za-z0-9_-]{4,}/gi, match => '*'.repeat(match.length));
174
+ }
175
+ function display(value) { return redactKeys(value).replace(/[\u0000-\u001f\u007f-\u009f]/g, ' '); }
176
+
177
+ function readLocalFile(file, limit) {
178
+ const fd = fs.openSync(file, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0));
179
+ try {
180
+ const stat = fs.fstatSync(fd);
181
+ if (!stat.isFile() || stat.size > MAX_TRANSCRIPT_BYTES) throw new SessionUploadError('会话记录超过 32 MiB 或不是普通文件。');
182
+ const buffer = Buffer.alloc(Math.min(stat.size, limit));
183
+ let offset = 0, count;
184
+ while (offset < buffer.length && (count = fs.readSync(fd, buffer, offset, buffer.length - offset, offset))) offset += count;
185
+ return buffer.subarray(0, offset).toString('utf8');
186
+ } finally { fs.closeSync(fd); }
187
+ }
188
+
189
+ function localMetadata(client, file) {
190
+ const input = readLocalFile(file, 256 * 1024);
191
+ let id, cwd, timestamp, title = '';
192
+ for (const line of input.split('\n')) {
193
+ let row;
194
+ try { row = JSON.parse(line); } catch { continue; }
195
+ if (!row || typeof row !== 'object') continue;
196
+ if (client === 'codex') {
197
+ if (row.type === 'session_meta') {
198
+ // Subagent transcripts must not be offered as main conversations.
199
+ if (row.payload?.source && typeof row.payload.source === 'object') return null;
200
+ id = row.payload?.id; cwd = row.payload?.cwd; timestamp = row.timestamp;
201
+ }
202
+ if (!title && row.type === 'response_item' && row.payload?.role === 'user' && row.metadata?.retained_source?.complete === true) {
203
+ title = (row.payload.content || []).filter(item => item?.type === 'input_text').map(item => item.text || '').join(' ');
204
+ }
205
+ } else {
206
+ if (row.isSidechain === true) return null;
207
+ if (row.isSidechain !== false || row.isMeta === true || !['user', 'assistant'].includes(row.type)) continue;
208
+ if (id && row.sessionId !== id) return null;
209
+ id = row.sessionId; cwd = row.cwd || cwd; timestamp ||= row.timestamp;
210
+ if (!title && row.type === 'user') title = typeof row.message?.content === 'string' ? row.message.content
211
+ : (Array.isArray(row.message?.content) ? row.message.content : []).filter(item => item?.type === 'text').map(item => item.text || '').join(' ');
212
+ }
213
+ if (id && cwd && title) break;
214
+ }
215
+ if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,200}$/.test(id) || typeof cwd !== 'string' || !path.isAbsolute(cwd)) return null;
216
+ return { client, session_id: id, cwd: path.resolve(cwd), file, timestamp: Number.isFinite(Date.parse(timestamp)) ? new Date(timestamp).toISOString() : '未知时间', title: display(title || '(无标题)').slice(0, 100) };
217
+ }
218
+
219
+ function discoverSessions(env = process.env, client) {
220
+ const home = env.HOME || require('node:os').homedir();
221
+ const codex = env.CODEX_HOME || path.join(home, '.codex');
222
+ const roots = [
223
+ ['codex', path.join(codex, 'sessions')], ['codex', path.join(codex, 'archived_sessions')],
224
+ ['claude', path.join(env.CLAUDE_CONFIG_DIR || path.join(home, '.claude'), 'projects')],
225
+ ].filter(([name]) => !client || name === client);
226
+ const sessions = [], warnings = { unreadable: 0, unsupported: 0 };
227
+ let visited = 0;
228
+ function walk(name, dir, depth = 0) {
229
+ if (depth > 12) throw new SessionUploadError('本机会话目录层级超过限制。');
230
+ let entries;
231
+ try {
232
+ if (!fs.lstatSync(dir).isDirectory()) return;
233
+ entries = fs.readdirSync(dir, { withFileTypes: true });
234
+ } catch (error) { if (error.code !== 'ENOENT') warnings.unreadable++; return; }
235
+ for (const entry of entries) {
236
+ if (++visited > 50000) throw new SessionUploadError('本机会话目录条目过多,请用 --client 缩小范围。');
237
+ if (entry.isDirectory() && !['subagents', 'subagent', 'node_modules'].includes(entry.name)) walk(name, path.join(dir, entry.name), depth + 1);
238
+ else if (entry.isFile() && entry.name.endsWith('.jsonl')) {
239
+ try {
240
+ const item = localMetadata(name, path.join(dir, entry.name));
241
+ if (item) sessions.push(item); else warnings.unsupported++;
242
+ } catch { warnings.unreadable++; }
243
+ }
244
+ }
245
+ }
246
+ for (const [name, root] of roots) walk(name, root);
247
+ sessions.sort((a, b) => b.timestamp.localeCompare(a.timestamp) || a.client.localeCompare(b.client) || a.file.localeCompare(b.file));
248
+ return { sessions, warnings };
249
+ }
250
+
251
+ async function chooseNumber(items, label, question, output) {
252
+ items.forEach((item, index) => output.write(`${index + 1}. ${display(label(item))}\n`));
253
+ const answer = await question('输入编号(回车取消):');
254
+ if (!answer.trim()) return null;
255
+ const number = Number(answer);
256
+ if (!/^\d+$/.test(answer) || !Number.isSafeInteger(number) || number < 1 || number > items.length) throw new SessionUploadError('选择编号无效,未上传。');
257
+ return items[number - 1];
258
+ }
259
+
260
+ async function chooseAssets(catalog, question, output) {
261
+ const teams = catalog.filter(team => team.projects.length && team.agents.length);
262
+ if (!teams.length) throw new SessionUploadError('当前账号没有可选择的团队、项目和 Agent,未上传。');
263
+ const team = await chooseNumber(teams, item => item.name, question, output);
264
+ if (!team) return null;
265
+ const project = await chooseNumber(team.projects, item => `项目:${item.name}`, question, output);
266
+ if (!project) return null;
267
+ const agent = await chooseNumber(team.agents, item => `Agent:${item.name}`, question, output);
268
+ if (!agent) return null;
269
+ return validateSelection(catalog, { team_id: team.team_id, company_id: project.company_id, project_id: project.project_id, agent_id: agent.agent_id, task_id: '' });
270
+ }
271
+
272
+ function journalFile(session, env) {
273
+ const id = crypto.createHash('sha256').update(JSON.stringify([session.client, session.session_id, session.file])).digest('hex');
274
+ return path.join(directory(env), 'manual-uploads', `${id}.json`);
275
+ }
276
+ function writeJournal(file, job) {
277
+ fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
278
+ const temp = `${file}.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`;
279
+ try {
280
+ fs.writeFileSync(temp, JSON.stringify(job), { mode: 0o600, flag: 'wx' });
281
+ fs.renameSync(temp, file);
282
+ fs.chmodSync(file, 0o600);
283
+ } finally { fs.rmSync(temp, { force: true }); }
284
+ }
285
+
286
+ async function uploadSession(session, { env = process.env, fetcher = fetch, question, output = process.stdout } = {}) {
287
+ const prepared = await withLock(env, async () => {
288
+ const config = readConfig(configPath(env));
289
+ const fingerprint = configFingerprint(config), file = journalFile(session, env);
290
+ let job;
291
+ try { job = JSON.parse(fs.readFileSync(file, 'utf8')); }
292
+ catch (error) { if (error.code !== 'ENOENT') throw new SessionUploadError('所选会话上传进度无法读取,未上传。'); }
293
+ if (job && (job.version !== 1 || !Array.isArray(job.messages) || !Array.isArray(job.completed) || job.session_id !== session.session_id || (job.fingerprint !== fingerprint && !job.ordered_connection))) {
294
+ throw new SessionUploadError('所选会话的上传进度或 Memory 身份已改变;未向新服务发送旧内容。');
295
+ }
296
+ // Pending snapshots survive transcript changes, process restart and network
297
+ // failures. Once complete, another invocation can include newly added rows.
298
+ const completed = new Set(job?.completed || []);
299
+ const pending = job?.messages?.some(message => !completed.has(message.direct_event_id));
300
+ if (!pending && !job?.ordered_stream) {
301
+ const parsed = parseTranscript(session.client, session.session_id, readLocalFile(session.file, MAX_TRANSCRIPT_BYTES), 0);
302
+ if (parsed.incomplete) throw new SessionUploadError('会话格式不支持、会话 ID 不匹配或记录尚未写完,未上传。');
303
+ if (!parsed.messages.length) throw new SessionUploadError('会话没有支持上传的主会话用户文本或最终回复,未上传。');
304
+ const messages = parsed.messages.flatMap(message => splitMessage({ ...message, content: redactKeys(message.content) }));
305
+ if (messages.length > MAX_SEEN) throw new SessionUploadError('所选会话消息过多,未上传。');
306
+ job = { version: 1, fingerprint, session_id: session.session_id, messages, completed: [...completed], selection: job?.selection };
307
+ }
308
+ const originalSession = readSession(session.client, session.session_id, session.cwd, env);
309
+ if (originalSession?.config_fingerprint && originalSession.config_fingerprint !== fingerprint) throw new SessionUploadError('原会话属于其他 Memory 身份;内容未上传、绑定未更改,请用 config key 恢复原 Key。');
310
+ const saved = job.selection || originalSession;
311
+ const fields = ['team_id', 'company_id', 'project_id', 'agent_id', 'task_id'];
312
+ let selection = saved?.mode !== 'off' && saved && Object.fromEntries(fields.map(key => [key, saved[key] || '']));
313
+ let binding, authorizedContext;
314
+ async function authorize() {
315
+ const context = await requestMemory(config, 'context', { ...selection, session_id: session.session_id, query: 'Authorize selected local conversation upload' }, fetcher);
316
+ if (typeof context.session_binding !== 'string' || !/^[a-f0-9]{64}$/.test(context.session_binding)) throw new SessionUploadError('服务端未返回有效会话绑定,未上传。');
317
+ binding = context.session_binding;
318
+ authorizedContext = context;
319
+ }
320
+ if (selection) {
321
+ try { await authorize(); }
322
+ catch (error) {
323
+ if (![400, 403, 404].includes(error.status)) throw error;
324
+ output.write('原绑定已失效或无权访问,请重新选择。\n');
325
+ selection = null;
326
+ }
327
+ }
328
+ if (!selection) {
329
+ const catalog = normalizeCatalog(await requestMemory(config, 'catalog', {}, fetcher));
330
+ selection = await chooseAssets(catalog, question, output);
331
+ if (!selection) return { cancelled: true };
332
+ selection = Object.fromEntries(fields.map(key => [key, selection[key]]));
333
+ if (job.selection && (completed.size || job.ordered_stream) && JSON.stringify(selection) !== JSON.stringify(job.selection)) {
334
+ throw new SessionUploadError('该会话已有部分内容上传到原项目;为避免拆散历史,进度已保留,请恢复原资产权限后重试。');
335
+ }
336
+ await authorize();
337
+ }
338
+ job.selection = selection;
339
+ if (authorizedContext.conversation_upload?.protocol === 'ordered-v1') {
340
+ if (job.ordered_connection && job.ordered_connection !== require('./ordered-upload.cjs').connectionId(config, authorizedContext.conversation_upload.user_id)) {
341
+ throw new SessionUploadError('所选会话的 Memory 身份已改变;原进度已保留。');
342
+ }
343
+ // Authorize/select outside the SQLite transaction. Release the legacy
344
+ // migration lock before draining the shared durable queue.
345
+ return { ordered: true, selection, context: authorizedContext, priorJob: job, journal: file };
346
+ }
347
+ // Capability failure never consumes a legacy journal or sends a snapshot.
348
+ throw new SessionUploadError(UPGRADE_REQUIRED);
349
+ });
350
+ if (prepared.ordered) {
351
+ try { return await require('./ordered-upload.cjs').manual(session, prepared.selection, prepared.context, { env, fetcher, priorJob: prepared.priorJob, journal: prepared.journal }); }
352
+ catch { throw new SessionUploadError('所选会话尚未全部取得 L0 确认;正文和进度已保留,请检查上传状态后重试。'); }
353
+ }
354
+ return prepared;
355
+ }
356
+
357
+ async function sessionCommand(argv, { env = process.env, fetcher = fetch, question, output = process.stdout } = {}) {
358
+ let client, id, list = false;
359
+ for (let index = 0; index < argv.length; index++) {
360
+ const arg = argv[index];
361
+ if (arg === '--list') list = true;
362
+ else if (arg === '--client') client = argv[++index];
363
+ else if (arg === '--session') id = argv[++index];
364
+ else if (arg === '--help') { output.write('用法:upload-session [--list | --session ID] [--client codex|claude]\n不带参数时交互选择;回车取消。\n'); return; }
365
+ else throw new SessionUploadError('未知参数;使用 upload-session --help 查看用法。');
366
+ if ((arg === '--client' || arg === '--session') && (!argv[index] || argv[index].startsWith('--'))) throw new SessionUploadError('参数缺少值。');
367
+ }
368
+ if (client && !['codex', 'claude'].includes(client)) throw new SessionUploadError('--client 必须是 codex 或 claude。');
369
+ if (list && id) throw new SessionUploadError('--list 和 --session 不能同时使用。');
370
+ let rl;
371
+ const ask = question || (async prompt => {
372
+ if (!process.stdin.isTTY || !process.stdout.isTTY) throw new SessionUploadError('需要在交互式终端选择会话或资产;未上传。');
373
+ rl ||= readline.createInterface({ input: process.stdin, output });
374
+ return rl.question(prompt);
375
+ });
376
+ try {
377
+ const { sessions, warnings } = discoverSessions(env, client);
378
+ if (warnings.unreadable || warnings.unsupported) output.write(`跳过不可读记录 ${warnings.unreadable} 个、不支持或非主会话记录 ${warnings.unsupported} 个。\n`);
379
+ if (!sessions.length) throw new SessionUploadError('未找到可访问且格式受支持的本地主会话。');
380
+ const label = item => display(`${item.client} | ${item.timestamp} | ${item.session_id} | ${item.title}`);
381
+ if (list) { sessions.forEach(item => output.write(`${label(item)}\n`)); return; }
382
+ const matches = id ? sessions.filter(item => item.session_id === id) : sessions;
383
+ if (!matches.length) throw new SessionUploadError('会话 ID 不存在、不可访问或格式不支持。');
384
+ const selected = id && matches.length === 1 ? matches[0] : await chooseNumber(matches, label, ask, output);
385
+ if (!selected) { output.write('已取消,未上传。\n'); return; }
386
+ output.write(`上传所选会话:${label(selected)}\n`);
387
+ const result = await uploadSession(selected, { env, fetcher, question: ask, output });
388
+ output.write(result.cancelled ? '已取消,未上传。\n' : `所选会话上传完成:本次确认 ${result.sent} 条,累计确认 ${result.completed} 条。\n`);
389
+ return result;
390
+ } catch (error) {
391
+ if (error instanceof SessionUploadError) throw error;
392
+ throw new SessionUploadError(publicError(error));
393
+ } finally { rl?.close(); }
394
+ }
395
+
396
+ async function main() {
397
+ const action = process.argv[2];
398
+ if (action === '--sessions') {
399
+ await sessionCommand(process.argv.slice(3));
400
+ } else if (action === '--enable') {
401
+ if (!process.stdin.isTTY || !process.stdout.isTTY) throw new Error('开启上传需要交互式终端。');
402
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
403
+ const answer = await rl.question('将主会话的用户文本和最终助手回复上传至配置的 Memory 服务并保存为项目 L0,供记忆处理使用。不上传工具、子 Agent 和思考过程。输入 YES 明确同意:');
404
+ rl.close();
405
+ if (answer !== 'YES') throw new Error('未开启上传。');
406
+ await setConsent(true);
407
+ if (process.env.RUTTER_DIRECT_MEMORY_RECOVERY !== 'manual') require('./recovery.cjs').configure();
408
+ process.stdout.write('已开启自动上传;请在当前会话重新确认上传授权。\n');
409
+ } else if (action === '--disable') {
410
+ await setConsent(false);
411
+ if (process.env.RUTTER_DIRECT_MEMORY_RECOVERY !== 'manual') require('./recovery.cjs').configure({ remove: true });
412
+ process.stdout.write('已关闭上传并清除本地待传队列;已发出且服务器已接收的请求无法撤回,已上传数据仍保存在 Memory。\n');
413
+ } else if (action === '--status') {
414
+ const state = readState();
415
+ process.stdout.write(`自动上传:${state.enabled ? '开启' : '关闭'};待传:${state.queue.length} 条。\n`);
416
+ const result = ['acknowledged', 'failed', 'revoked', 'identity_changed'].includes(state.last_result) ? state.last_result : 'none';
417
+ const acknowledged = typeof state.last_acknowledged_at === 'string' && Number.isFinite(Date.parse(state.last_acknowledged_at)) ? new Date(state.last_acknowledged_at).toISOString() : '无';
418
+ if (fs.existsSync(path.join(directory(), 'outbox.sqlite'))) {
419
+ const queue = await require('./ordered-upload.cjs').open();
420
+ try {
421
+ const captures = (await queue.all("SELECT value FROM pcu_meta WHERE key LIKE 'capture:%'")).map(row => JSON.parse(row.value));
422
+ for (const item of await queue.status()) {
423
+ const capture = captures.find(value => value.connection === item.connection && require('./outbox.cjs').hash(value.scope) === require('./outbox.cjs').hash(JSON.parse(item.scope)));
424
+ const at = item.last_ack_at ? new Date(item.last_ack_at).toISOString() : '无';
425
+ process.stdout.write(`会话 ${item.id.slice(0, 12)}:待传 ${item.pending} 批/${item.pending_bytes} 字节;L0 已确认 ${item.acknowledged} 批;最近 L0 确认 ${at};状态 ${item.paused || item.last_result || '等待发送'};采集 ${capture?.result === 'blocked' ? '受阻,来源、身份或存储需要检查' : capture ? '已扫描' : '尚未核验来源'}。\n`);
426
+ }
427
+ const blocked = captures.filter(value => value.result === 'blocked').length;
428
+ process.stdout.write(`采集受阻会话:${blocked};待传为 0 仅表示已入队内容,无完整来源确认保证。\n`);
429
+ } finally { await queue.close(); }
430
+ }
431
+ let recovery = '未注册';
432
+ try { const saved = JSON.parse(fs.readFileSync(path.join(directory(), 'recovery-status.json'))); recovery = saved.installed === true ? '已注册' : '未注册或注册失败'; } catch {}
433
+ process.stdout.write(`后台恢复:${recovery};旧格式待迁移 ${state.queue.length} 批。\n`);
434
+ process.stdout.write(`最近上传结果:${result};最近服务端确认:${acknowledged}(L1 提炼状态未知)。\n诊断日志:${diagnosticFile()}\n`);
435
+ } else if (action === '--clear-log') {
436
+ for (const file of [diagnosticFile(), `${diagnosticFile()}.1`]) fs.rmSync(file, { force: true });
437
+ process.stdout.write('已清理客户端诊断日志;上传队列和会话绑定未更改。\n');
438
+ } else if (action === '--retry') {
439
+ await require('./ordered-upload.cjs').recover(process.env, { retry: true });
440
+ const warning = await retry();
441
+ process.stdout.write(`${warning || '待传队列已处理。'}\n`);
442
+ } else throw new Error('用法:node upload.cjs --enable|--disable|--status|--retry|--clear-log');
443
+ }
444
+ if (require.main === module) main().catch(error => { process.stderr.write(`${error.message}\n`); process.exitCode = 1; });
445
+ module.exports = { readState, writeState, setConsent, enableForSession, enableAfterInstall, capture, flush, retry, splitMessage, statePath,
446
+ configFingerprint, writeJournal, redactKeys, discoverSessions, sessionCommand, uploadSession, journalFile, withLock };
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+ const crypto = require('crypto');
3
+ const { request } = require('./compat.cjs');
4
+
5
+ function retryDelay(attempts, random = Math.random) {
6
+ return Math.min(60000, 1000 * 2 ** Math.min(6, attempts)) * (0.75 + random() * 0.5);
7
+ }
8
+
9
+ // The caller owns queue/config lifecycle. Each claim is a SHORT transaction;
10
+ // DNS, HTTP, binding renewal and response handling occur after it commits.
11
+ async function drain(queue, connection, config, { concurrency = 4, fetcher = request,
12
+ maxBatches = 1000, owner = crypto.randomBytes(16).toString('hex'), currentConfig, renewBinding, env = process.env, streamIds } = {}) {
13
+ if (!Number.isInteger(concurrency) || concurrency < 1 || concurrency > 10) throw new Error('Upload concurrency must be 1–10');
14
+ const lease = await queue.acquireWorker(connection, owner);
15
+ if (!lease) return { attempted: 0, acknowledged: 0, failed: 0, busy: true };
16
+ let attempted = 0, acknowledged = 0, failed = 0, identityPaused = false, reservations = 0;
17
+ async function slot() {
18
+ while (!identityPaused) {
19
+ if (reservations >= maxBatches) break;
20
+ reservations++;
21
+ if (!await queue.renewWorker(lease)) break;
22
+ if (currentConfig && !await currentConfig(config)) break;
23
+ const job = await queue.claim(connection, owner, { streamIds });
24
+ if (!job) break;
25
+ attempted++;
26
+ const started = Date.now();
27
+ const diagnostic = (result, reason, status) => require('./hook.cjs').diagnostic(env, 'upload', result, { id: job.stream, duration_ms: Date.now() - started, reason, status });
28
+ try {
29
+ const headers = { 'content-type': 'application/json', 'x-tdai-service-id': config.service_id, 'x-tdai-user-key': config.memory_user_key };
30
+ let response = await fetcher(`${config.service_url}/hierarchy/direct-memory/authorize`, {
31
+ method: 'POST', redirect: 'error', timeoutMs: 10000, maxBytes: 1024 * 1024, headers,
32
+ body: JSON.stringify({ ...Object.fromEntries(['team_id','company_id','project_id','agent_id','task_id','session_id'].map(key => [key, job.payload[key]])), session_binding: job.binding }),
33
+ });
34
+ if (response.ok) {
35
+ const proof = await response.json();
36
+ if (proof?.code !== 0 || proof.data?.authorized !== true) throw new Error('Asset authorization unconfirmed');
37
+ if (currentConfig && !await currentConfig(config)) { await queue.fail(job, { retryAt: 0 }); break; }
38
+ response = await fetcher(`${config.service_url}/hierarchy/direct-memory/conversation/ordered`, {
39
+ method: 'POST', redirect: 'error', timeoutMs: 10000, maxBytes: 1024 * 1024, headers,
40
+ body: JSON.stringify({ ...job.payload, session_binding: job.binding }),
41
+ });
42
+ }
43
+ if (!response.ok) {
44
+ const pause = response.status === 401 ? 'identity' : response.status === 403 ? 'authorization'
45
+ : [400, 409, 413].includes(response.status) ? 'protocol' : null;
46
+ // A 403 is scoped to this stream. Renew only using the same verified
47
+ // identity and scope; never turn a denied response into fresh consent.
48
+ if (response.status === 401) { identityPaused = true; await queue.pauseConnection(connection, 'identity'); }
49
+ if (response.status === 403 && renewBinding && await renewBinding(job)) {
50
+ await queue.fail(job, { retryAt: 0 }); failed++; continue;
51
+ }
52
+ const retryAfter = Number(response.headers?.get('retry-after'));
53
+ const delay = response.status === 429 && Number.isFinite(retryAfter) && retryAfter > 0
54
+ ? Math.min(retryAfter * 1000, 60000) : retryDelay(job.attempts);
55
+ diagnostic(response.status === 401 || response.status === 403 ? 'revoked' : 'failed', 'rejected', response.status);
56
+ await queue.fail(job, { pause, retryAt: Date.now() + delay, result: response.status === 429 ? 'backpressure' : pause || 'failed' }); failed++; continue;
57
+ }
58
+ const envelope = await response.json();
59
+ if (envelope?.code !== 0) throw new Error('Upload response unconfirmed');
60
+ if (await queue.acknowledge(job, envelope.data)) {
61
+ acknowledged++;
62
+ require('./hook.cjs').diagnostic(env, 'upload', 'acknowledged', { id: job.stream, count: job.payload.messages.length, age_ms: Date.now() - job.created_at });
63
+ }
64
+ } catch (error) {
65
+ diagnostic('failed', /acknowledgement|unconfirmed/i.test(error.message) ? 'unconfirmed' : ['TimeoutError','AbortError'].includes(error.name) ? 'timeout' : 'network');
66
+ await queue.fail(job, { retryAt: Date.now() + retryDelay(job.attempts) }); failed++;
67
+ }
68
+ }
69
+ }
70
+ try {
71
+ await Promise.all(Array.from({ length: concurrency }, slot));
72
+ return { attempted, acknowledged, failed };
73
+ } finally { await queue.releaseWorker(lease); }
74
+ }
75
+
76
+ module.exports = { drain, retryDelay };