@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.
- package/LICENSE +27 -0
- package/README.md +80 -2
- package/THIRD_PARTY_LICENSES.txt +111 -0
- package/bin/cli.cjs +94 -0
- package/bin/npm-runtime.cjs +125 -0
- package/client/darwin-arm64/bootstrap.cjs +159 -0
- package/client/darwin-arm64/compat.cjs +61 -0
- package/client/darwin-arm64/hook.cjs +597 -0
- package/client/darwin-arm64/install.cjs +555 -0
- package/client/darwin-arm64/manifest.json +1 -0
- package/client/darwin-arm64/mcp.cjs +5666 -0
- package/client/darwin-arm64/migration.cjs +53 -0
- package/client/darwin-arm64/node_sqlite3.node +0 -0
- package/client/darwin-arm64/ordered-upload.cjs +236 -0
- package/client/darwin-arm64/outbox.cjs +218 -0
- package/client/darwin-arm64/recovery.cjs +61 -0
- package/client/darwin-arm64/sqlite3.cjs +217 -0
- package/client/darwin-arm64/transcript.cjs +138 -0
- package/client/darwin-arm64/upload.cjs +446 -0
- package/client/darwin-arm64/worker.cjs +76 -0
- package/client/darwin-x64/bootstrap.cjs +159 -0
- package/client/darwin-x64/compat.cjs +61 -0
- package/client/darwin-x64/hook.cjs +597 -0
- package/client/darwin-x64/install.cjs +555 -0
- package/client/darwin-x64/manifest.json +1 -0
- package/client/darwin-x64/mcp.cjs +5666 -0
- package/client/darwin-x64/migration.cjs +53 -0
- package/client/darwin-x64/node_sqlite3.node +0 -0
- package/client/darwin-x64/ordered-upload.cjs +236 -0
- package/client/darwin-x64/outbox.cjs +218 -0
- package/client/darwin-x64/recovery.cjs +61 -0
- package/client/darwin-x64/sqlite3.cjs +217 -0
- package/client/darwin-x64/transcript.cjs +138 -0
- package/client/darwin-x64/upload.cjs +446 -0
- package/client/darwin-x64/worker.cjs +76 -0
- package/client/linux-x64-glibc/bootstrap.cjs +159 -0
- package/client/linux-x64-glibc/compat.cjs +61 -0
- package/client/linux-x64-glibc/hook.cjs +597 -0
- package/client/linux-x64-glibc/install.cjs +555 -0
- package/client/linux-x64-glibc/manifest.json +1 -0
- package/client/linux-x64-glibc/mcp.cjs +5666 -0
- package/client/linux-x64-glibc/migration.cjs +53 -0
- package/client/linux-x64-glibc/node_sqlite3.node +0 -0
- package/client/linux-x64-glibc/ordered-upload.cjs +236 -0
- package/client/linux-x64-glibc/outbox.cjs +218 -0
- package/client/linux-x64-glibc/recovery.cjs +61 -0
- package/client/linux-x64-glibc/sqlite3.cjs +217 -0
- package/client/linux-x64-glibc/transcript.cjs +138 -0
- package/client/linux-x64-glibc/upload.cjs +446 -0
- package/client/linux-x64-glibc/worker.cjs +76 -0
- package/client/win32-x64/bootstrap.cjs +159 -0
- package/client/win32-x64/compat.cjs +61 -0
- package/client/win32-x64/hook.cjs +597 -0
- package/client/win32-x64/install.cjs +555 -0
- package/client/win32-x64/manifest.json +1 -0
- package/client/win32-x64/mcp.cjs +5666 -0
- package/client/win32-x64/migration.cjs +53 -0
- package/client/win32-x64/node_sqlite3.node +0 -0
- package/client/win32-x64/ordered-upload.cjs +236 -0
- package/client/win32-x64/outbox.cjs +218 -0
- package/client/win32-x64/recovery.cjs +61 -0
- package/client/win32-x64/sqlite3.cjs +217 -0
- package/client/win32-x64/transcript.cjs +138 -0
- package/client/win32-x64/upload.cjs +446 -0
- package/client/win32-x64/worker.cjs +76 -0
- package/package.json +34 -4
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const { hash } = require('./outbox.cjs');
|
|
6
|
+
function backup(file, env) {
|
|
7
|
+
if (!fs.existsSync(file)) return;
|
|
8
|
+
const body = fs.readFileSync(file);
|
|
9
|
+
const dir = path.join(env.HOME || require('os').homedir(), '.config/rutter-direct-memory/migration-backups');
|
|
10
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
11
|
+
const target = path.join(dir, hash([file, crypto.createHash('sha256').update(body).digest('hex')]) + '.json');
|
|
12
|
+
try { const fd = fs.openSync(target, 'wx', 0o600); try { fs.writeFileSync(fd, body); fs.fsyncSync(fd); } finally { fs.closeSync(fd); } }
|
|
13
|
+
catch (error) { if (error.code !== 'EEXIST') throw error; }
|
|
14
|
+
}
|
|
15
|
+
function mapSnapshot(messages, sourceMessages) {
|
|
16
|
+
const sources = new Map(sourceMessages.map(message => [message.direct_event_id, message]));
|
|
17
|
+
return messages.map(message => {
|
|
18
|
+
const source = sources.get(message.direct_event_id);
|
|
19
|
+
const { redactKeys } = require('./upload.cjs');
|
|
20
|
+
if (!source || redactKeys(message.content) !== source.content || message.role !== source.role || message.timestamp !== source.timestamp) {
|
|
21
|
+
throw new Error('旧队列与来源记录无法对账;已保留备份和原进度,需要检查该会话。');
|
|
22
|
+
}
|
|
23
|
+
// Preserve identity, text snapshot and original position. Never synthesize
|
|
24
|
+
// positions from queue order or a wall clock.
|
|
25
|
+
return source;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async function automatic(item, sources, config, env, withLock) {
|
|
29
|
+
const upload = require('./upload.cjs');
|
|
30
|
+
if (!upload.readState(env).queue.some(entry => entry.session_id === item.session_id)) return;
|
|
31
|
+
await withLock(env, async () => {
|
|
32
|
+
const state = upload.readState(env);
|
|
33
|
+
const matches = entry => entry.session_id === item.session_id && ['team_id','company_id','project_id','agent_id','task_id']
|
|
34
|
+
.every(key => (entry[key] || '') === (item[key] || ''));
|
|
35
|
+
const pending = state.queue.filter(matches);
|
|
36
|
+
if (!pending.length) return;
|
|
37
|
+
backup(upload.statePath(env), env);
|
|
38
|
+
const fingerprint = crypto.createHash('sha256').update(JSON.stringify([config.service_url, config.service_id, config.memory_user_key])).digest('hex');
|
|
39
|
+
if (state.fingerprint !== fingerprint) throw new Error('旧队列身份尚未验证;备份和原队列已保留。');
|
|
40
|
+
const messages = mapSnapshot(pending.flatMap(entry => entry.messages), sources);
|
|
41
|
+
const ordered = require('./ordered-upload.cjs'); const queue = await ordered.open(env);
|
|
42
|
+
try {
|
|
43
|
+
await queue.enqueue({ connection: item.connection, scope: item, binding: item.binding, messages });
|
|
44
|
+
await queue.transaction(() => queue.run('INSERT OR REPLACE INTO pcu_meta VALUES(?,?)', [`migration:${hash([item.connection, item.session_id])}`,
|
|
45
|
+
JSON.stringify({ protocol: 'ordered-v1', imported: messages.length, digest: hash(messages.map(m => m.direct_event_id)) })]));
|
|
46
|
+
} finally { await queue.close(); }
|
|
47
|
+
// SQLite commits before old JSON is changed. A crash between them replays
|
|
48
|
+
// identical IDs into the same stream; the next attempt can safely finish.
|
|
49
|
+
state.queue = state.queue.filter(entry => !matches(entry));
|
|
50
|
+
upload.writeState(env, state);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
module.exports = { backup, mapSnapshot, automatic };
|
|
Binary file
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { spawn } = require('child_process');
|
|
5
|
+
const { Outbox, hash } = require('./outbox.cjs');
|
|
6
|
+
const { parseOrderedTranscript } = require('./transcript.cjs');
|
|
7
|
+
const { drain } = require('./worker.cjs');
|
|
8
|
+
const hook = require('./hook.cjs');
|
|
9
|
+
|
|
10
|
+
function root(env = process.env) { return path.join(env.HOME || require('os').homedir(), '.config', 'rutter-direct-memory'); }
|
|
11
|
+
function connectionId(config, user) { return hash([config.service_url, config.service_id, user]); }
|
|
12
|
+
function fingerprint(config) { return hash([config.service_url, config.service_id, config.memory_user_key]); }
|
|
13
|
+
function open(env) { return Outbox.open(path.join(root(env), 'outbox.sqlite')); }
|
|
14
|
+
function scope(session) { return Object.fromEntries(['team_id', 'company_id', 'project_id', 'agent_id', 'task_id', 'session_id'].map(key => [key, session[key] || ''])); }
|
|
15
|
+
function chunks(messages) {
|
|
16
|
+
const { splitMessage, redactKeys } = require('./upload.cjs');
|
|
17
|
+
return messages.flatMap(message => {
|
|
18
|
+
const parts = splitMessage({ ...message, content: redactKeys(message.content) });
|
|
19
|
+
return parts.map((part, index) => ({ ...part, chunk_index: index, chunk_count: parts.length }));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function register(session, event, config, env = process.env) {
|
|
24
|
+
if (session.conversation_upload?.protocol !== 'ordered-v1' || !session.conversation_upload.user_id) return;
|
|
25
|
+
if (typeof event.transcript_path !== 'string' || !path.isAbsolute(event.transcript_path)) return;
|
|
26
|
+
const item = { ...scope(session), client: session.client, cwd: session.cwd, transcript_path: event.transcript_path,
|
|
27
|
+
binding: session.session_binding, user_id: session.conversation_upload.user_id, consent: session.upload_enabled_at,
|
|
28
|
+
initial_event_id: session.upload_initial_event_id, initial_prompt: session.upload_initial_prompt,
|
|
29
|
+
connection: connectionId(config, session.conversation_upload.user_id),
|
|
30
|
+
fingerprint: session.upload_config_fingerprint || null, consent_revision: session.upload_consent_revision || 1, saved_at: Date.now() };
|
|
31
|
+
const queue = await open(env);
|
|
32
|
+
try { await queue.transaction(async () => {
|
|
33
|
+
// This capability was authenticated by the server. A rotated Key for the
|
|
34
|
+
// same stable user may resume its queues; another user's Key cannot.
|
|
35
|
+
// Other scopes renew their own bindings during recovery. Do not scan and
|
|
36
|
+
// rewrite every registered window while holding this collector's lock.
|
|
37
|
+
await queue.run("UPDATE pcu_stream SET paused=NULL,next_attempt=0 WHERE connection=? AND paused='identity'", [item.connection]);
|
|
38
|
+
await queue.run('INSERT OR REPLACE INTO pcu_meta VALUES(?,?)', [`session:${hash([item.connection, scope(session)])}`, JSON.stringify(item)]);
|
|
39
|
+
}); }
|
|
40
|
+
finally { await queue.close(); }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function collectSnapshot(item, config, env = process.env, { completed = false } = {}) {
|
|
44
|
+
if (item.fingerprint !== fingerprint(config)) return 'Memory 连接凭证已改变;待传数据已保留,请验证原身份后恢复。';
|
|
45
|
+
const upload = require('./upload.cjs');
|
|
46
|
+
const state = upload.readState(env);
|
|
47
|
+
if (!state.enabled || !item.consent) return '';
|
|
48
|
+
if (item.consent < state.since) return 'Memory 上传同意已过期,请重新确认当前会话;尚未采集旧内容。';
|
|
49
|
+
const current = hook.readSession(item.client, item.session_id, item.cwd, env);
|
|
50
|
+
if (!current || current.mode === 'off' || current.upload_enabled_at !== item.consent) return '';
|
|
51
|
+
// Reject replaced symlinks/non-regular files and unbounded transcripts.
|
|
52
|
+
const fd = fs.openSync(item.transcript_path, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0));
|
|
53
|
+
let input;
|
|
54
|
+
try {
|
|
55
|
+
const stat = fs.fstatSync(fd);
|
|
56
|
+
if (!stat.isFile() || stat.size > 32 * 1024 * 1024) throw new Error('Unsupported transcript file');
|
|
57
|
+
input = fs.readFileSync(fd, 'utf8');
|
|
58
|
+
} finally { fs.closeSync(fd); }
|
|
59
|
+
const parsed = parseOrderedTranscript(item.client, item.session_id, input, item.consent, { stopCompleted: completed,
|
|
60
|
+
authorizedEventIds: item.initial_event_id ? [item.initial_event_id] : [], authorizedPrompt: item.initial_prompt });
|
|
61
|
+
if (parsed.incomplete) return 'Memory 会话记录尚未完整,将在后续采集时重试。';
|
|
62
|
+
await require('./migration.cjs').automatic(item, chunks(parseOrderedTranscript(item.client, item.session_id, input, 0, { stopCompleted: completed }).messages), config, env, upload.withLock);
|
|
63
|
+
const queue = await open(env);
|
|
64
|
+
try { await queue.enqueue({ connection: item.connection, scope: scope(item), binding: item.binding, messages: chunks(parsed.messages), checkpoint: parsed.checkpoint, source: { length: Buffer.byteLength(input), prefix: bytes => require('crypto').createHash('sha256').update(Buffer.from(input).subarray(0, bytes)).digest('hex') } }); }
|
|
65
|
+
finally { await queue.close(); }
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// This status also covers messages not yet enqueued. Empty pending is never
|
|
70
|
+
// evidence that a missing/rewritten transcript has been fully confirmed.
|
|
71
|
+
async function collect(item, config, env = process.env, options = {}) {
|
|
72
|
+
let warning, failed = false;
|
|
73
|
+
try { warning = await collectSnapshot(item, config, env, options); return warning; }
|
|
74
|
+
catch (error) { failed = true; throw error; }
|
|
75
|
+
finally {
|
|
76
|
+
const status = { connection: item.connection, scope: scope(item), checked_at: Date.now(),
|
|
77
|
+
result: failed || warning ? 'blocked' : 'scanned',
|
|
78
|
+
reason: failed ? 'source_or_storage' : warning ? 'source_identity_or_consent' : null };
|
|
79
|
+
const queue = await open(env);
|
|
80
|
+
try { await queue.transaction(() => queue.run('INSERT OR REPLACE INTO pcu_meta VALUES(?,?)',
|
|
81
|
+
[`capture:${hash([item.connection, scope(item)])}`, JSON.stringify(status)])); }
|
|
82
|
+
finally { await queue.close(); }
|
|
83
|
+
if (status.result === 'blocked') hook.diagnostic(env, 'capture', 'capture_blocked', { id: hash(scope(item)), reason: failed ? 'source_or_storage' : 'consent' });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function wake(env = process.env) {
|
|
88
|
+
const child = spawn(process.execPath, [__filename, '--recover'], { detached: true, stdio: 'ignore', env });
|
|
89
|
+
child.on('error', () => {}); child.unref();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function capture({ client, event, env = process.env, wakeWorker = wake }) {
|
|
93
|
+
const session = hook.readSession(client, event.session_id, path.resolve(event.cwd), env);
|
|
94
|
+
if (!session || session.mode === 'off') return '';
|
|
95
|
+
const config = hook.readConfig(hook.configPath(env));
|
|
96
|
+
await register(session, event, config, env);
|
|
97
|
+
const item = { ...scope(session), client, cwd: session.cwd, transcript_path: event.transcript_path, binding: session.session_binding,
|
|
98
|
+
consent: session.upload_enabled_at, initial_event_id: session.upload_initial_event_id, initial_prompt: session.upload_initial_prompt,
|
|
99
|
+
connection: connectionId(config, session.conversation_upload.user_id), fingerprint: session.upload_config_fingerprint || null };
|
|
100
|
+
try {
|
|
101
|
+
// Clients may invoke Stop before their asynchronous transcript writer has
|
|
102
|
+
// created or finished the file. Wait without holding any queue transaction.
|
|
103
|
+
for (let attempt = 0; ; attempt++) {
|
|
104
|
+
try {
|
|
105
|
+
const warning = await collect(item, config, env, { completed: true });
|
|
106
|
+
if (!warning?.includes('尚未完整') || attempt >= 19) return warning;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (error.code !== 'ENOENT' || attempt >= 19) throw error;
|
|
109
|
+
}
|
|
110
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
111
|
+
}
|
|
112
|
+
} finally { wakeWorker(env); }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function renew(queue, item, config, env, fetcher) {
|
|
116
|
+
try {
|
|
117
|
+
const context = await hook.requestMemory(config, 'context', { ...scope(item), query: 'Renew existing authorized conversation binding' }, fetcher);
|
|
118
|
+
const identity = context.conversation_upload;
|
|
119
|
+
if (identity?.protocol !== 'ordered-v1' || !identity.user_id
|
|
120
|
+
|| connectionId(config, identity.user_id) !== item.connection || !/^[a-f0-9]{64}$/.test(context.session_binding || '')) return false;
|
|
121
|
+
const next = { ...item, binding: context.session_binding, fingerprint: fingerprint(config), user_id: identity.user_id };
|
|
122
|
+
await queue.transaction(async () => {
|
|
123
|
+
const streams = await queue.all('SELECT id,scope FROM pcu_stream WHERE connection=?', [item.connection]);
|
|
124
|
+
for (const stream of streams) if (hash(JSON.parse(stream.scope)) === hash(scope(item))) await queue.run(
|
|
125
|
+
"UPDATE pcu_stream SET binding=?,paused=CASE WHEN paused IN ('identity','authorization') THEN NULL ELSE paused END,next_attempt=0 WHERE id=?",
|
|
126
|
+
[next.binding, stream.id]);
|
|
127
|
+
if (item.registry_key) await queue.run('UPDATE pcu_meta SET value=? WHERE key=?', [JSON.stringify(next), item.registry_key]);
|
|
128
|
+
});
|
|
129
|
+
if (!item.manual) {
|
|
130
|
+
const saved = hook.readSession(item.client, item.session_id, item.cwd, env);
|
|
131
|
+
if (saved?.conversation_upload?.user_id === identity.user_id && saved.upload_enabled_at === item.consent) {
|
|
132
|
+
hook.writeSession(item.client, item.session_id, item.cwd, { ...saved,
|
|
133
|
+
config_fingerprint: hook.configFingerprint(config), upload_config_fingerprint: fingerprint(config),
|
|
134
|
+
session_binding: next.binding }, env);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return next;
|
|
138
|
+
} catch { return false; }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function recover(env = process.env, { fetcher, retry = false } = {}) {
|
|
142
|
+
if (!fs.existsSync(path.join(root(env), 'outbox.sqlite'))) return;
|
|
143
|
+
const config = hook.readConfig(hook.configPath(env));
|
|
144
|
+
const state = require('./upload.cjs').readState(env);
|
|
145
|
+
const queue = await open(env);
|
|
146
|
+
try {
|
|
147
|
+
const sessions = await queue.all("SELECT key,value FROM pcu_meta WHERE key LIKE 'session:%' OR key LIKE 'manual:%'");
|
|
148
|
+
const connections = new Map();
|
|
149
|
+
for (const row of sessions) {
|
|
150
|
+
let item = { ...JSON.parse(row.value), registry_key: row.key };
|
|
151
|
+
if ((!state.enabled || state.manually_disabled) && !item.manual) continue;
|
|
152
|
+
if (item.fingerprint !== fingerprint(config) || retry) {
|
|
153
|
+
const verified = await renew(queue, item, config, env, fetcher);
|
|
154
|
+
if (!verified) continue;
|
|
155
|
+
item = verified;
|
|
156
|
+
}
|
|
157
|
+
if (item.fingerprint !== fingerprint(config)) continue;
|
|
158
|
+
connections.set(item.connection, [...(connections.get(item.connection) || []), item]);
|
|
159
|
+
if (!item.manual) {
|
|
160
|
+
try { await collect(item, config, env); }
|
|
161
|
+
catch { hook.diagnostic(env, 'upload', 'capture_blocked', { id: hash(scope(item)), reason: 'source_or_storage' }); }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const [connection, items] of connections) {
|
|
165
|
+
if (retry) await queue.resume(connection);
|
|
166
|
+
await drain(queue, connection, config, {
|
|
167
|
+
streamIds: (await queue.all('SELECT id,scope FROM pcu_stream WHERE connection=?', [connection]))
|
|
168
|
+
.filter(stream => items.some(item => hash(scope(item)) === hash(JSON.parse(stream.scope)))).map(stream => stream.id),
|
|
169
|
+
env, fetcher, concurrency: Number(config.upload_concurrency || 4),
|
|
170
|
+
renewBinding: async job => {
|
|
171
|
+
const item = items.find(value => hash(scope(value)) === hash(scope(job.payload)));
|
|
172
|
+
return item ? Boolean(await renew(queue, item, config, env, fetcher)) : false;
|
|
173
|
+
},
|
|
174
|
+
currentConfig: async () => {
|
|
175
|
+
const current = require('./upload.cjs').readState(env);
|
|
176
|
+
const manualKeys = items.filter(item => item.manual).map(item => item.registry_key);
|
|
177
|
+
let manualAllowed = false;
|
|
178
|
+
for (const key of manualKeys) if (await queue.get('SELECT key FROM pcu_meta WHERE key=?', [key])) manualAllowed = true;
|
|
179
|
+
return ((!current.manually_disabled && current.enabled) || manualAllowed)
|
|
180
|
+
&& fingerprint(hook.readConfig(hook.configPath(env))) === fingerprint(config);
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
} finally { await queue.close(); }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function manual(session, selection, context, { env = process.env, fetcher, priorJob, journal } = {}) {
|
|
188
|
+
const upload = require('./upload.cjs');
|
|
189
|
+
const config = hook.readConfig(hook.configPath(env));
|
|
190
|
+
const expected = fingerprint(config);
|
|
191
|
+
const connection = connectionId(config, context.conversation_upload.user_id);
|
|
192
|
+
const selectedScope = scope({ ...selection, session_id: session.session_id });
|
|
193
|
+
const queue = await open(env);
|
|
194
|
+
let result;
|
|
195
|
+
try {
|
|
196
|
+
// An immutable snapshot can drain even if its original file later becomes
|
|
197
|
+
// unreadable/incomplete. Never require rereading it to recover lost ACKs.
|
|
198
|
+
const generation = (await queue.get("SELECT value FROM pcu_meta WHERE key='generation'"))?.value || '0';
|
|
199
|
+
const stream = hash([connection, selectedScope, generation]);
|
|
200
|
+
const prior = (await queue.status()).find(row => row.id === stream);
|
|
201
|
+
if (prior?.pending) result = { stream };
|
|
202
|
+
else {
|
|
203
|
+
const fd = fs.openSync(session.file, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0));
|
|
204
|
+
let input;
|
|
205
|
+
try { const stat = fs.fstatSync(fd); if (!stat.isFile() || stat.size > 32 * 1024 * 1024) throw new Error('Unsupported transcript'); input = fs.readFileSync(fd, 'utf8'); }
|
|
206
|
+
finally { fs.closeSync(fd); }
|
|
207
|
+
const parsed = parseOrderedTranscript(session.client, session.session_id, input, 0);
|
|
208
|
+
if (parsed.incomplete || !parsed.messages.length) throw new Error('会话尚未完整或没有可确认的主会话消息。');
|
|
209
|
+
const sourceMessages = chunks(parsed.messages);
|
|
210
|
+
if (priorJob?.messages?.length && !priorJob.ordered_stream) {
|
|
211
|
+
require('./migration.cjs').backup(journal, env);
|
|
212
|
+
require('./migration.cjs').mapSnapshot(priorJob.messages, sourceMessages);
|
|
213
|
+
}
|
|
214
|
+
result = await queue.enqueue({ connection, scope: selectedScope, binding: context.session_binding,
|
|
215
|
+
messages: sourceMessages, checkpoint: parsed.checkpoint, source: { length: Buffer.byteLength(input), prefix: bytes => require('crypto').createHash('sha256').update(Buffer.from(input).subarray(0, bytes)).digest('hex') } });
|
|
216
|
+
}
|
|
217
|
+
const registryKey = `manual:${result.stream}`;
|
|
218
|
+
await queue.transaction(async () => {
|
|
219
|
+
await queue.run('INSERT OR REPLACE INTO pcu_meta VALUES(?,?)', [registryKey,
|
|
220
|
+
JSON.stringify({ ...selectedScope, connection, fingerprint: expected, binding: context.session_binding, manual: true })]);
|
|
221
|
+
await queue.run("UPDATE pcu_stream SET binding=?,paused=NULL,next_attempt=0 WHERE id=? AND (paused IS NULL OR paused!='disabled')", [context.session_binding, result.stream]);
|
|
222
|
+
});
|
|
223
|
+
const job = { version: 1, fingerprint: upload.configFingerprint(config), session_id: session.session_id,
|
|
224
|
+
selection, messages: [], completed: [], ordered_stream: result.stream, ordered_connection: connection };
|
|
225
|
+
if (journal) upload.writeJournal(journal, job);
|
|
226
|
+
const sent = await drain(queue, connection, config, { env, fetcher, streamIds: [result.stream], currentConfig: async () =>
|
|
227
|
+
fingerprint(hook.readConfig(hook.configPath(env))) === expected && Boolean(await queue.get('SELECT key FROM pcu_meta WHERE key=?', [registryKey])) });
|
|
228
|
+
const status = (await queue.status()).find(row => row.id === result.stream);
|
|
229
|
+
if (status.pending || status.paused === 'disabled') throw new Error('所选会话已入队但尚未全部获得 L0 确认;正文和进度已保留,请重试。');
|
|
230
|
+
return { sent: sent.acknowledged, completed: status.acknowledged };
|
|
231
|
+
} finally { await queue.close(); }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (require.main === module && process.argv.includes('--config')) process.env.RUTTER_DIRECT_MEMORY_CONFIG = process.argv[process.argv.indexOf('--config') + 1];
|
|
235
|
+
if (require.main === module) recover().catch(() => { process.stderr.write('Memory 后台上传未完成;待传数据已保留。\n'); process.exitCode = 1; });
|
|
236
|
+
module.exports = { register, capture, recover, collect, wake, open, connectionId, fingerprint, chunks, manual };
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
const canonical = value => Array.isArray(value) ? value.map(canonical) : value && typeof value === 'object' ? Object.fromEntries(Object.keys(value).sort().map(key => [key, canonical(value[key])])) : value;
|
|
7
|
+
const hash = value => crypto.createHash('sha256').update(JSON.stringify(canonical(value))).digest('hex');
|
|
8
|
+
const PROTOCOL = 'ordered-v1';
|
|
9
|
+
function safeSqliteVersion(version) {
|
|
10
|
+
const [major, minor, patch] = String(version).split('.').map(Number);
|
|
11
|
+
return major > 3 || (major === 3 && (minor > 51 || (minor === 51 && patch >= 3)
|
|
12
|
+
|| (minor === 50 && patch >= 7) || (minor === 44 && patch >= 6)));
|
|
13
|
+
}
|
|
14
|
+
async function retryBusy(operation) {
|
|
15
|
+
const deadline = Date.now() + 5000;
|
|
16
|
+
for (;;) {
|
|
17
|
+
try { return await operation(); }
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (error.code !== 'SQLITE_BUSY' || Date.now() >= deadline) throw error;
|
|
20
|
+
await new Promise(resolve => setTimeout(resolve, 10 + Math.floor(Math.random() * 20)));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function loadDriver() {
|
|
26
|
+
try { return require('./sqlite3.cjs'); }
|
|
27
|
+
catch (error) { if (error.code !== 'MODULE_NOT_FOUND') throw error; return require('sqlite3'); }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class Outbox {
|
|
31
|
+
constructor(db) { this.db = db; this.tail = Promise.resolve(); }
|
|
32
|
+
static async open(file, { driver = loadDriver() } = {}) {
|
|
33
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
34
|
+
// Create privately before SQLite opens WAL/SHM files in the private directory.
|
|
35
|
+
// Never open/close an EXISTING database outside SQLite: on POSIX, closing
|
|
36
|
+
// any descriptor for this inode drops this process's fcntl locks, including
|
|
37
|
+
// a different live SQLite connection's writer lock. Other processes could
|
|
38
|
+
// then enter a second writer and corrupt WAL state.
|
|
39
|
+
try { const fd = fs.openSync(file, 'wx', 0o600); fs.closeSync(fd); }
|
|
40
|
+
catch (error) { if (error.code !== 'EEXIST') throw error; }
|
|
41
|
+
if (!fs.lstatSync(file).isFile()) throw new Error('Outbox must be a regular private file');
|
|
42
|
+
fs.chmodSync(file, 0o600);
|
|
43
|
+
const db = await new Promise((resolve, reject) => { const value = new driver.Database(file, error => error ? reject(error) : resolve(value)); });
|
|
44
|
+
const queue = new Outbox(db);
|
|
45
|
+
try {
|
|
46
|
+
// Even short native waits can starve the writer's queued COMMIT when
|
|
47
|
+
// many Hooks share libuv. All lock retries run outside the thread pool.
|
|
48
|
+
db.configure('busyTimeout', 0);
|
|
49
|
+
if (!safeSqliteVersion((await queue.get('SELECT sqlite_version() AS version')).version)) {
|
|
50
|
+
throw new Error('SQLite engine needs the WAL-reset fix (3.51.3+); rebuild the native client before use');
|
|
51
|
+
}
|
|
52
|
+
if ((await queue.get('PRAGMA journal_mode')).journal_mode !== 'wal') await queue.exec('PRAGMA journal_mode=WAL');
|
|
53
|
+
await queue.exec('PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON;');
|
|
54
|
+
if (!await queue.get("SELECT name FROM sqlite_master WHERE type='table' AND name='pcu_worker'")) await queue.transaction(() => queue.exec(`
|
|
55
|
+
CREATE TABLE IF NOT EXISTS pcu_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
56
|
+
INSERT OR IGNORE INTO pcu_meta VALUES ('schema', '1');
|
|
57
|
+
CREATE TABLE IF NOT EXISTS pcu_stream (
|
|
58
|
+
id TEXT PRIMARY KEY, connection TEXT NOT NULL, scope TEXT NOT NULL, binding TEXT NOT NULL,
|
|
59
|
+
next_seq INTEGER NOT NULL DEFAULT 1, checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
60
|
+
last_ack_at INTEGER NOT NULL DEFAULT 0, last_result TEXT,
|
|
61
|
+
paused TEXT, next_attempt INTEGER NOT NULL DEFAULT 0, last_attempt INTEGER NOT NULL DEFAULT 0,
|
|
62
|
+
lease_owner TEXT, lease_until INTEGER NOT NULL DEFAULT 0, fence INTEGER NOT NULL DEFAULT 0
|
|
63
|
+
);
|
|
64
|
+
CREATE TABLE IF NOT EXISTS pcu_event (
|
|
65
|
+
stream TEXT NOT NULL REFERENCES pcu_stream(id), event TEXT NOT NULL, hash TEXT NOT NULL,
|
|
66
|
+
source_order INTEGER NOT NULL, chunk_index INTEGER NOT NULL, chunk_count INTEGER NOT NULL,
|
|
67
|
+
PRIMARY KEY(stream,event), UNIQUE(stream,source_order,chunk_index)
|
|
68
|
+
);
|
|
69
|
+
CREATE TABLE IF NOT EXISTS pcu_batch (
|
|
70
|
+
stream TEXT NOT NULL REFERENCES pcu_stream(id), seq INTEGER NOT NULL, id TEXT NOT NULL,
|
|
71
|
+
hash TEXT NOT NULL, payload TEXT, bytes INTEGER NOT NULL, attempts INTEGER NOT NULL DEFAULT 0,
|
|
72
|
+
ack TEXT, created_at INTEGER NOT NULL, PRIMARY KEY(stream,seq), UNIQUE(stream,id)
|
|
73
|
+
);
|
|
74
|
+
CREATE INDEX IF NOT EXISTS pcu_ready ON pcu_stream(connection,paused,next_attempt,last_attempt);
|
|
75
|
+
CREATE TABLE IF NOT EXISTS pcu_worker (connection TEXT PRIMARY KEY, owner TEXT NOT NULL, fence INTEGER NOT NULL, until_ms INTEGER NOT NULL);
|
|
76
|
+
`));
|
|
77
|
+
// Most opens need no write transaction. Older schemas are upgraded once,
|
|
78
|
+
// with a second check under the writer lock for competing processes.
|
|
79
|
+
if (!(await queue.all('PRAGMA table_info(pcu_stream)')).some(column => column.name === 'last_ack_at')) {
|
|
80
|
+
await queue.transaction(async () => {
|
|
81
|
+
const columns = await queue.all('PRAGMA table_info(pcu_stream)');
|
|
82
|
+
for (const [name, type] of [['last_ack_at', 'INTEGER NOT NULL DEFAULT 0'], ['last_result', 'TEXT']]) {
|
|
83
|
+
if (!columns.some(column => column.name === name)) await queue.run(`ALTER TABLE pcu_stream ADD COLUMN ${name} ${type}`);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if ((await queue.get("SELECT value FROM pcu_meta WHERE key='schema'")).value !== '1') throw new Error('Unsupported outbox schema');
|
|
88
|
+
return queue;
|
|
89
|
+
} catch (error) { await queue.close(); throw error; }
|
|
90
|
+
}
|
|
91
|
+
exec(sql) { return retryBusy(() => new Promise((resolve, reject) => this.db.exec(sql, error => error ? reject(error) : resolve()))); }
|
|
92
|
+
run(sql, params = []) { return retryBusy(() => new Promise((resolve, reject) => this.db.run(sql, params, function(error) { error ? reject(error) : resolve({ changes: this.changes, lastID: this.lastID }); }))); }
|
|
93
|
+
get(sql, params = []) { return retryBusy(() => new Promise((resolve, reject) => this.db.get(sql, params, (error, row) => error ? reject(error) : resolve(row)))); }
|
|
94
|
+
all(sql, params = []) { return retryBusy(() => new Promise((resolve, reject) => this.db.all(sql, params, (error, rows) => error ? reject(error) : resolve(rows)))); }
|
|
95
|
+
// sqlite.serialize(async () => ...) is NOT a transaction mutex. Await the
|
|
96
|
+
// entire transaction before allowing another operation on this connection.
|
|
97
|
+
transaction(fn) {
|
|
98
|
+
const result = this.tail.then(async () => {
|
|
99
|
+
await this.exec('BEGIN IMMEDIATE');
|
|
100
|
+
try { const value = await fn(); await this.exec('COMMIT'); return value; }
|
|
101
|
+
catch (error) { await this.exec('ROLLBACK'); throw error; }
|
|
102
|
+
});
|
|
103
|
+
this.tail = result.catch(() => {}); return result;
|
|
104
|
+
}
|
|
105
|
+
async close() { await this.tail; await new Promise((resolve, reject) => this.db.close(error => error ? reject(error) : resolve())); }
|
|
106
|
+
|
|
107
|
+
// connection is a hash of endpoint + service + authenticated stable user;
|
|
108
|
+
// scope carries project/Agent/session dimensions, never a raw User_Key.
|
|
109
|
+
enqueue({ connection, scope, binding, messages, checkpoint = 0, source, budget = 128 * 1024 * 1024 }) {
|
|
110
|
+
scope = Object.fromEntries(['team_id','company_id','project_id','agent_id','task_id','session_id'].map(key => [key, scope[key] || '']));
|
|
111
|
+
return this.transaction(async () => {
|
|
112
|
+
const generation = (await this.get("SELECT value FROM pcu_meta WHERE key='generation'"))?.value || '0';
|
|
113
|
+
const id = hash([connection, scope, generation]);
|
|
114
|
+
await this.run('INSERT OR IGNORE INTO pcu_stream(id,connection,scope,binding) VALUES(?,?,?,?)', [id, connection, JSON.stringify(scope), binding]);
|
|
115
|
+
const stream = await this.get('SELECT * FROM pcu_stream WHERE id=?', [id]);
|
|
116
|
+
if (source) {
|
|
117
|
+
const prior = await this.get('SELECT value FROM pcu_meta WHERE key=?', [`source:${id}`]);
|
|
118
|
+
if (prior) {
|
|
119
|
+
const value = JSON.parse(prior.value);
|
|
120
|
+
if (source.length < value.length || source.prefix(value.length) !== value.hash) throw new Error('Source transcript was truncated or rewritten; checkpoint retained');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let seq = stream.next_seq, added = 0;
|
|
124
|
+
for (const message of messages) {
|
|
125
|
+
if (!Number.isSafeInteger(message.source_order) || message.source_order < 0 || !Number.isSafeInteger(message.chunk_index)
|
|
126
|
+
|| !Number.isSafeInteger(message.chunk_count) || message.chunk_count < 1 || message.chunk_index < 0 || message.chunk_index >= message.chunk_count
|
|
127
|
+
|| !/^[a-f0-9]{64}$/.test(message.direct_event_id) || !['user', 'assistant'].includes(message.role) || typeof message.content !== 'string') throw new Error('Invalid ordered message');
|
|
128
|
+
const digest = hash(message);
|
|
129
|
+
const seen = await this.get('SELECT hash FROM pcu_event WHERE stream=? AND event=?', [id, message.direct_event_id]);
|
|
130
|
+
if (seen) { if (seen.hash !== digest) throw new Error('Source message conflict'); continue; }
|
|
131
|
+
await this.run('INSERT INTO pcu_event VALUES(?,?,?,?,?,?)', [id, message.direct_event_id, digest, message.source_order, message.chunk_index, message.chunk_count]);
|
|
132
|
+
const payload = { protocol: PROTOCOL, stream_id: id, session_id: scope.session_id, batch_seq: seq, messages: [message] };
|
|
133
|
+
const batchHash = hash(payload), batchId = hash([id, seq, batchHash]);
|
|
134
|
+
const body = JSON.stringify({ ...scope, ...payload, batch_id: batchId, batch_hash: batchHash });
|
|
135
|
+
await this.run('INSERT INTO pcu_batch(stream,seq,id,hash,payload,bytes,created_at) VALUES(?,?,?,?,?,?,?)', [id, seq++, batchId, batchHash, body, Buffer.byteLength(body), Date.now()]);
|
|
136
|
+
added++;
|
|
137
|
+
}
|
|
138
|
+
const used = await this.get('SELECT COALESCE(SUM(bytes),0) AS bytes FROM pcu_batch WHERE payload IS NOT NULL');
|
|
139
|
+
if (used.bytes > budget) throw new Error('Outbox disk budget exceeded; source checkpoint retained');
|
|
140
|
+
await this.run('UPDATE pcu_stream SET binding=?,next_seq=?,checkpoint=MAX(checkpoint,?) WHERE id=?', [binding, seq, checkpoint, id]);
|
|
141
|
+
if (source) await this.run('INSERT OR REPLACE INTO pcu_meta VALUES(?,?)', [`source:${id}`, JSON.stringify({ length: source.length, hash: source.prefix(source.length) })]);
|
|
142
|
+
return { stream: id, added };
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
claim(connection, owner, { now = Date.now(), leaseMs = 60000, streamIds } = {}) {
|
|
147
|
+
if (streamIds && !streamIds.length) return Promise.resolve(null);
|
|
148
|
+
return this.transaction(async () => {
|
|
149
|
+
const stream = await this.get(`SELECT s.* FROM pcu_stream s WHERE connection=? AND paused IS NULL
|
|
150
|
+
AND next_attempt<=? AND lease_until<=? ${streamIds ? `AND s.id IN (${streamIds.map(() => '?').join(',')})` : ''} AND EXISTS(SELECT 1 FROM pcu_batch b WHERE b.stream=s.id AND b.payload IS NOT NULL)
|
|
151
|
+
ORDER BY last_attempt,id LIMIT 1`, [connection, now, now, ...(streamIds || [])]);
|
|
152
|
+
if (!stream) return null;
|
|
153
|
+
const fence = stream.fence + 1;
|
|
154
|
+
await this.run('UPDATE pcu_stream SET lease_owner=?,lease_until=?,fence=?,last_attempt=? WHERE id=?', [owner, now + leaseMs, fence, now, stream.id]);
|
|
155
|
+
const batch = await this.get('SELECT * FROM pcu_batch WHERE stream=? AND payload IS NOT NULL ORDER BY seq LIMIT 1', [stream.id]);
|
|
156
|
+
await this.run('UPDATE pcu_batch SET attempts=attempts+1 WHERE stream=? AND seq=?', [stream.id, batch.seq]);
|
|
157
|
+
return { ...batch, payload: JSON.parse(batch.payload), binding: stream.binding, owner, fence };
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async acknowledge(job, ack) {
|
|
162
|
+
// A generic 200, accepted count or another batch's ACK cannot delete text.
|
|
163
|
+
if (!ack || ack.protocol !== PROTOCOL || ack.batch_id !== job.id || ack.batch_hash !== job.hash
|
|
164
|
+
|| ack.batch_seq !== job.seq || ack.committed_seq < job.seq || !Array.isArray(ack.accepted_ids)
|
|
165
|
+
|| ack.accepted_ids.length !== job.payload.messages.length || new Set(ack.accepted_ids).size !== ack.accepted_ids.length
|
|
166
|
+
|| ack.accepted_ids.some(value => typeof value !== 'string' || !value)
|
|
167
|
+
|| JSON.stringify(ack.event_ids) !== JSON.stringify(job.payload.messages.map(message => message.direct_event_id))) throw new Error('Unmatched durable L0 acknowledgement');
|
|
168
|
+
return this.transaction(async () => {
|
|
169
|
+
const stream = await this.get('SELECT * FROM pcu_stream WHERE id=?', [job.stream]);
|
|
170
|
+
if (stream.lease_owner !== job.owner || stream.fence !== job.fence) return false;
|
|
171
|
+
await this.run('UPDATE pcu_batch SET payload=NULL,bytes=0,ack=? WHERE stream=? AND seq=?', [JSON.stringify(ack), job.stream, job.seq]);
|
|
172
|
+
await this.run("UPDATE pcu_stream SET lease_owner=NULL,lease_until=0,next_attempt=0,last_ack_at=?,last_result='acknowledged' WHERE id=?", [Date.now(), job.stream]);
|
|
173
|
+
return true;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fail(job, { retryAt = Date.now() + 1000, pause = null, result = 'failed' } = {}) {
|
|
178
|
+
return this.transaction(async () => (await this.run(`UPDATE pcu_stream SET lease_owner=NULL,lease_until=0,next_attempt=?,paused=COALESCE(?,paused),last_result=?
|
|
179
|
+
WHERE id=? AND lease_owner=? AND fence=?`, [retryAt, pause, result, job.stream, job.owner, job.fence])).changes === 1);
|
|
180
|
+
}
|
|
181
|
+
discardPending() {
|
|
182
|
+
return this.transaction(async () => {
|
|
183
|
+
await this.run("UPDATE pcu_batch SET payload=NULL,bytes=0 WHERE payload IS NOT NULL");
|
|
184
|
+
await this.run("UPDATE pcu_stream SET paused='disabled',lease_owner=NULL,lease_until=0,fence=fence+1");
|
|
185
|
+
await this.run("DELETE FROM pcu_meta WHERE key LIKE 'session:%' OR key LIKE 'manual:%' OR key LIKE 'capture:%'");
|
|
186
|
+
await this.run("INSERT OR REPLACE INTO pcu_meta VALUES('generation',?)", [crypto.randomBytes(16).toString('hex')]);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
pauseConnection(connection, reason) { return this.transaction(() => this.run('UPDATE pcu_stream SET paused=? WHERE connection=?', [reason, connection])); }
|
|
190
|
+
resume(connection) { return this.transaction(() => this.run('UPDATE pcu_stream SET paused=NULL,next_attempt=0 WHERE connection=?', [connection])); }
|
|
191
|
+
acquireWorker(connection, owner, now = Date.now()) {
|
|
192
|
+
return this.transaction(async () => {
|
|
193
|
+
const prior = await this.get('SELECT * FROM pcu_worker WHERE connection=?', [connection]);
|
|
194
|
+
if (prior && prior.until_ms > now && prior.owner !== owner) return null;
|
|
195
|
+
const fence = (prior?.fence || 0) + 1;
|
|
196
|
+
await this.run('INSERT OR REPLACE INTO pcu_worker VALUES(?,?,?,?)', [connection, owner, fence, now + 60000]);
|
|
197
|
+
return { connection, owner, fence };
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
renewWorker(lease, now = Date.now()) {
|
|
201
|
+
return this.transaction(async () => (await this.run('UPDATE pcu_worker SET until_ms=? WHERE connection=? AND owner=? AND fence=? AND until_ms>?',
|
|
202
|
+
[now + 60000, lease.connection, lease.owner, lease.fence, now])).changes === 1);
|
|
203
|
+
}
|
|
204
|
+
releaseWorker(lease) {
|
|
205
|
+
return this.transaction(() => this.run('UPDATE pcu_worker SET until_ms=0 WHERE connection=? AND owner=? AND fence=?', [lease.connection, lease.owner, lease.fence]));
|
|
206
|
+
}
|
|
207
|
+
async status() {
|
|
208
|
+
await this.tail;
|
|
209
|
+
return this.all(`SELECT s.id,s.connection,s.scope,s.paused,s.checkpoint,s.last_ack_at,s.last_result,s.last_attempt,s.next_attempt,
|
|
210
|
+
COALESCE(SUM(b.bytes),0) AS pending_bytes,
|
|
211
|
+
MIN(CASE WHEN b.payload IS NOT NULL THEN b.created_at END) AS oldest_pending_at,
|
|
212
|
+
COUNT(CASE WHEN b.payload IS NOT NULL THEN 1 END) AS pending,
|
|
213
|
+
COUNT(CASE WHEN b.ack IS NOT NULL THEN 1 END) AS acknowledged
|
|
214
|
+
FROM pcu_stream s LEFT JOIN pcu_batch b ON b.stream=s.id GROUP BY s.id`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
module.exports = { Outbox, hash, PROTOCOL, safeSqliteVersion };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const { spawnSync } = require('child_process');
|
|
6
|
+
const name = 'rutter-direct-memory';
|
|
7
|
+
const xml = value => String(value).replace(/[&<>"']/g, ch => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ch]));
|
|
8
|
+
const unit = value => '"' + String(value).replace(/%/g, '%%').replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/[\r\n]/g, '') + '"';
|
|
9
|
+
function configure({ remove = false, env = process.env, platform = process.platform, runner = spawnSync,
|
|
10
|
+
node = process.execPath, script = env.RUTTER_DIRECT_MEMORY_RECOVERY_SCRIPT || path.join(__dirname, 'ordered-upload.cjs'), uid = process.getuid?.(), user = env.USERNAME || env.USER } = {}) {
|
|
11
|
+
const home = env.HOME || os.homedir();
|
|
12
|
+
const root = path.join(home, '.config', name);
|
|
13
|
+
const taskName = `RutterDirectMemory-${require('crypto').createHash('sha256').update(home).digest('hex').slice(0,12)}`;
|
|
14
|
+
const config = env.RUTTER_DIRECT_MEMORY_CONFIG || path.join(root, 'config.json');
|
|
15
|
+
fs.mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
16
|
+
const run = (cmd, args, optional = false) => {
|
|
17
|
+
const result = runner(cmd, args, { env, encoding: 'utf8', timeout: 10000, windowsHide: true });
|
|
18
|
+
if (!optional && (result.error || result.status !== 0)) throw new Error(`后台恢复注册失败:${cmd};可运行 upload.cjs --retry 重试上传。`);
|
|
19
|
+
};
|
|
20
|
+
const write = (file, body) => { fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 }); fs.writeFileSync(file, body, { mode: 0o600 }); };
|
|
21
|
+
try {
|
|
22
|
+
if (platform === 'linux') {
|
|
23
|
+
const base = path.join(home, '.config/systemd/user', name);
|
|
24
|
+
if (remove) {
|
|
25
|
+
run('systemctl', ['--user', 'disable', '--now', `${name}.timer`, `${name}.service`], true);
|
|
26
|
+
fs.rmSync(base + '.timer', { force: true }); fs.rmSync(base + '.service', { force: true });
|
|
27
|
+
} else {
|
|
28
|
+
write(base + '.service', `[Unit]\nDescription=Rutter pending conversation upload\n[Service]\nType=oneshot\nEnvironment=${unit('HOME=' + home)}\nEnvironment=${unit('RUTTER_DIRECT_MEMORY_CONFIG=' + config)}\nExecStart=${unit(node)} ${unit(script)} --recover\nNice=10\nTimeoutStartSec=15min\n`);
|
|
29
|
+
write(base + '.timer', `[Unit]\nDescription=Recover pending Rutter conversations\n[Timer]\nOnStartupSec=30\nOnUnitActiveSec=60\nPersistent=true\n[Install]\nWantedBy=timers.target\n`);
|
|
30
|
+
}
|
|
31
|
+
run('systemctl', ['--user', 'daemon-reload']);
|
|
32
|
+
if (!remove) run('systemctl', ['--user', 'enable', '--now', `${name}.timer`]);
|
|
33
|
+
} else if (platform === 'darwin') {
|
|
34
|
+
const file = path.join(home, 'Library/LaunchAgents', `com.rutter.direct-memory.plist`);
|
|
35
|
+
run('launchctl', ['bootout', `gui/${uid}`, file], true);
|
|
36
|
+
if (remove) fs.rmSync(file, { force: true });
|
|
37
|
+
else {
|
|
38
|
+
write(file, `<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>Label</key><string>com.rutter.direct-memory</string><key>ProgramArguments</key><array><string>${xml(node)}</string><string>${xml(script)}</string><string>--recover</string></array><key>EnvironmentVariables</key><dict><key>HOME</key><string>${xml(home)}</string><key>RUTTER_DIRECT_MEMORY_CONFIG</key><string>${xml(config)}</string></dict><key>StartInterval</key><integer>60</integer><key>RunAtLoad</key><true/><key>LowPriorityIO</key><true/></dict></plist>`);
|
|
39
|
+
run('launchctl', ['bootstrap', `gui/${uid}`, file]);
|
|
40
|
+
}
|
|
41
|
+
} else if (platform === 'win32') {
|
|
42
|
+
if (remove) {
|
|
43
|
+
run('schtasks', ['/End', '/TN', taskName], true);
|
|
44
|
+
run('schtasks', ['/Delete', '/TN', taskName, '/F'], true);
|
|
45
|
+
fs.rmSync(path.join(root, 'recovery-task.xml'), { force: true });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (!user) throw new Error('Windows 后台恢复需要当前登录用户名。');
|
|
49
|
+
const file = path.join(root, 'recovery-task.xml');
|
|
50
|
+
write(file, `<?xml version="1.0" encoding="UTF-8"?><Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"><Triggers><LogonTrigger><Enabled>true</Enabled></LogonTrigger><TimeTrigger><Repetition><Interval>PT1M</Interval><StopAtDurationEnd>false</StopAtDurationEnd></Repetition><StartBoundary>2026-01-01T00:00:00</StartBoundary><Enabled>true</Enabled></TimeTrigger></Triggers><Principals><Principal id="Author"><UserId>${xml(user)}</UserId><LogonType>InteractiveToken</LogonType><RunLevel>LeastPrivilege</RunLevel></Principal></Principals><Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><ExecutionTimeLimit>PT15M</ExecutionTimeLimit></Settings><Actions Context="Author"><Exec><Command>${xml(node)}</Command><Arguments>${xml('"' + script + '" --recover --config "' + config + '"')}</Arguments></Exec></Actions></Task>`);
|
|
51
|
+
run('schtasks', ['/Create', '/TN', taskName, '/XML', file, '/F']);
|
|
52
|
+
}
|
|
53
|
+
} else throw new Error('当前平台没有后台恢复适配。');
|
|
54
|
+
const state = { installed: !remove, platform, checked_at: new Date().toISOString() };
|
|
55
|
+
write(path.join(root, 'recovery-status.json'), JSON.stringify(state)); return state;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
write(path.join(root, 'recovery-status.json'), JSON.stringify({ installed: false, platform, reason: 'registration_failed', checked_at: new Date().toISOString() }));
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
module.exports = { configure };
|