session-peer 0.3.6 → 0.5.0
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/CONTRIBUTING.md +6 -4
- package/PARITY.md +431 -6
- package/README.ja.md +14 -8
- package/README.ko.md +14 -8
- package/README.md +14 -8
- package/README.zh-CN.md +14 -8
- package/RELEASING.md +109 -0
- package/VALIDATION.md +806 -1
- package/dist/app-server.d.ts +207 -0
- package/dist/app-server.js +1044 -0
- package/dist/cli.js +229 -39
- package/dist/handoff-ledger.d.ts +103 -0
- package/dist/handoff-ledger.js +791 -0
- package/dist/handoff-observer-probe.d.ts +1 -0
- package/dist/handoff-observer-probe.js +170 -0
- package/dist/handoff-operations.d.ts +29 -0
- package/dist/handoff-operations.js +496 -0
- package/dist/handoff-writer-probe.d.ts +1 -0
- package/dist/handoff-writer-probe.js +146 -0
- package/dist/handoff.d.ts +112 -0
- package/dist/handoff.js +261 -0
- package/dist/help.js +18 -0
- package/dist/linux-native-probe.d.ts +1 -0
- package/dist/linux-native-probe.js +51 -0
- package/dist/linux-proc.d.ts +55 -0
- package/dist/linux-proc.js +150 -0
- package/dist/metadata-observation.d.ts +49 -0
- package/dist/metadata-observation.js +250 -0
- package/dist/native-profiles.d.ts +25 -0
- package/dist/native-profiles.js +62 -0
- package/dist/output.js +7 -1
- package/dist/process.d.ts +28 -0
- package/dist/process.js +284 -2
- package/dist/protocol.d.ts +2 -2
- package/dist/protocol.js +1 -1
- package/dist/send.d.ts +18 -1
- package/dist/send.js +91 -6
- package/dist/skill-metadata.js +7 -4
- package/dist/ssh-response.d.ts +11 -0
- package/dist/ssh-response.js +78 -2
- package/dist/ssh.d.ts +2 -1
- package/dist/ssh.js +37 -2
- package/dist/windows.d.ts +28 -0
- package/dist/windows.js +149 -1
- package/docs/api.md +255 -4
- package/docs/design/typescript-skill-compatibility.md +55 -2
- package/docs/guide.ja.md +36 -16
- package/docs/guide.ko.md +39 -19
- package/docs/guide.md +46 -20
- package/docs/guide.zh-CN.md +36 -16
- package/docs/shorthand.md +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Owned one-shot worker. node:sqlite is synchronous, so it never runs on the
|
|
2
|
+
// caller's event loop. Its supervisor kills only this process group at deadline.
|
|
3
|
+
// Codex rust-v0.160.1 / d27764b82f7118f674371e6d6e76271d9d606edb:
|
|
4
|
+
// state/thread_history_migrations/0001_thread_history.sql, 0002_thread_items_item_type.sql;
|
|
5
|
+
// app-server-protocol/src/protocol/v2/item.rs (userMessage.clientId);
|
|
6
|
+
// thread-store/src/local/thread_history.rs (turn_status, projection lag).
|
|
7
|
+
// Also checked against rust-v0.162.1 / 092d3acd6bec3e3a14bdc7e7a2810ab628ab759d:
|
|
8
|
+
// the same migrations, camelCase userMessage.clientId mapping and bounded
|
|
9
|
+
// metadata column projection. Exact source hashes/live native evidence are in
|
|
10
|
+
// test/fixtures/metadata-observation-native-evidence.json. No history/body API.
|
|
11
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { lstatSync, realpathSync } from 'node:fs';
|
|
14
|
+
import { basename, dirname, isAbsolute } from 'node:path';
|
|
15
|
+
const fail = (reason) => ({ supported: false, injectionObserved: false, reason });
|
|
16
|
+
const safe = (value, bytes) => typeof value === 'string' && value.length > 0 &&
|
|
17
|
+
Buffer.byteLength(value) <= bytes && !/[\x00-\x1f\x7f-\x9f]/.test(value) &&
|
|
18
|
+
!/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u.test(value);
|
|
19
|
+
function storageStamp(database) {
|
|
20
|
+
const uid = process.getuid?.(), home = lstatSync(dirname(database));
|
|
21
|
+
if (uid === undefined || !home.isDirectory() || home.isSymbolicLink() || home.uid !== uid || (home.mode & 0o022) ||
|
|
22
|
+
realpathSync(database) !== database)
|
|
23
|
+
throw new Error();
|
|
24
|
+
const stamps = [];
|
|
25
|
+
for (const suffix of ['', '-wal', '-shm', '-journal']) {
|
|
26
|
+
try {
|
|
27
|
+
const side = lstatSync(database + suffix, { bigint: true });
|
|
28
|
+
if (suffix === '-journal' || !side.isFile() || side.isSymbolicLink() || side.uid !== BigInt(uid) ||
|
|
29
|
+
side.nlink !== 1n || (side.mode & 18n))
|
|
30
|
+
throw new Error();
|
|
31
|
+
stamps.push({ suffix, dev: side.dev.toString(), ino: side.ino.toString() });
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (!suffix || error.code !== 'ENOENT')
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Never create a WAL shared-memory sidecar or recover a rollback journal.
|
|
39
|
+
// Existing SHM reader locks are transient; DB/WAL bytes remain read-only.
|
|
40
|
+
if (stamps.some(item => item.suffix === '-wal') !== stamps.some(item => item.suffix === '-shm'))
|
|
41
|
+
throw new Error();
|
|
42
|
+
return createHash('sha256').update(JSON.stringify(stamps)).digest('hex');
|
|
43
|
+
}
|
|
44
|
+
function queryDatabase(request) {
|
|
45
|
+
let db;
|
|
46
|
+
try {
|
|
47
|
+
const before = storageStamp(request.database);
|
|
48
|
+
db = new DatabaseSync(request.database, { readOnly: true });
|
|
49
|
+
db.exec('PRAGMA query_only=ON; PRAGMA trusted_schema=OFF; PRAGMA busy_timeout=40; PRAGMA temp_store=MEMORY;');
|
|
50
|
+
if (db.prepare('PRAGMA temp_store').get().temp_store !== 2 ||
|
|
51
|
+
db.prepare('PRAGMA encoding').get().encoding !== 'UTF-8')
|
|
52
|
+
return fail('metadata_probe_failed');
|
|
53
|
+
if (storageStamp(request.database) !== before)
|
|
54
|
+
return fail('metadata_storage_untrusted');
|
|
55
|
+
const tables = db.prepare("SELECT name,type FROM sqlite_schema WHERE name IN ('thread_items','thread_turns','thread_history_projection_state')").all();
|
|
56
|
+
if (tables.length !== 3 || tables.some(row => row.type !== 'table'))
|
|
57
|
+
return fail('metadata_schema_unsupported');
|
|
58
|
+
const columns = {
|
|
59
|
+
thread_items: { thread_id: 'TEXT', turn_id: 'TEXT', item_id: 'TEXT', rollout_ordinal: 'INTEGER', item_json: 'TEXT', item_type: 'TEXT' },
|
|
60
|
+
thread_turns: { thread_id: 'TEXT', turn_id: 'TEXT', status: 'TEXT' },
|
|
61
|
+
thread_history_projection_state: { thread_id: 'TEXT', next_rollout_byte_offset: 'INTEGER', next_rollout_ordinal: 'INTEGER' },
|
|
62
|
+
};
|
|
63
|
+
for (const [table, required] of Object.entries(columns)) {
|
|
64
|
+
const actual = db.prepare('PRAGMA table_info(' + table + ')').all();
|
|
65
|
+
if (Object.entries(required).some(([name, type]) => !actual.some(row => row.name === name && row.type === type)))
|
|
66
|
+
return fail('metadata_schema_unsupported');
|
|
67
|
+
const keys = table === 'thread_items' ? ['thread_id', 'turn_id', 'item_id'] : table === 'thread_turns' ? ['thread_id', 'turn_id'] : ['thread_id'];
|
|
68
|
+
if (actual.filter(row => typeof row.pk === 'number' && row.pk > 0).length !== keys.length ||
|
|
69
|
+
keys.some((name, i) => !actual.some(row => row.name === name && row.pk === i + 1)))
|
|
70
|
+
return fail('metadata_schema_unsupported');
|
|
71
|
+
}
|
|
72
|
+
const indexes = db.prepare('PRAGMA index_list(thread_items)').all();
|
|
73
|
+
const userIndex = indexes.find(row => row.name === 'idx_thread_items_user_messages' && row.partial === 1);
|
|
74
|
+
const indexColumns = db.prepare('PRAGMA index_info(idx_thread_items_user_messages)').all();
|
|
75
|
+
if (!userIndex || indexColumns.length !== 2 || indexColumns[0]?.name !== 'thread_id' || indexColumns[1]?.name !== 'rollout_ordinal')
|
|
76
|
+
return fail('metadata_schema_unsupported');
|
|
77
|
+
const projection = db.prepare('SELECT next_rollout_byte_offset,next_rollout_ordinal FROM thread_history_projection_state WHERE thread_id=?').get(request.threadId);
|
|
78
|
+
if (!projection || !Object.values(projection).every(value => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0))
|
|
79
|
+
return fail('metadata_history_unavailable');
|
|
80
|
+
// Preparing this query also verifies the partial index's actual predicate.
|
|
81
|
+
// Materialize only row keys before JSON parsing. Never copy item_json into
|
|
82
|
+
// a CTE/temp table: even an oversized cell must stay in its original table.
|
|
83
|
+
// Projected temporary rows contain only bounded IDs, never body values.
|
|
84
|
+
const query = db.prepare(`WITH recent AS MATERIALIZED (
|
|
85
|
+
SELECT rowid AS item_rowid FROM thread_items INDEXED BY idx_thread_items_user_messages
|
|
86
|
+
WHERE thread_id=? AND item_type='userMessage' ORDER BY rollout_ordinal DESC LIMIT 256
|
|
87
|
+
), projected AS MATERIALIZED (
|
|
88
|
+
SELECT CASE WHEN typeof(turn_id)='text' AND octet_length(turn_id)<=128 THEN turn_id END AS turn_id,
|
|
89
|
+
CASE WHEN typeof(item_json)='text' THEN
|
|
90
|
+
CASE WHEN octet_length(item_json)<=262144 THEN
|
|
91
|
+
CASE WHEN json_valid(item_json) THEN
|
|
92
|
+
CASE WHEN json_extract(item_json,'$.type')='userMessage' AND json_type(item_json,'$.clientId')='text' THEN
|
|
93
|
+
CASE WHEN length(CAST(json_extract(item_json,'$.clientId') AS BLOB))<=128 THEN json_extract(item_json,'$.clientId') END
|
|
94
|
+
END
|
|
95
|
+
END
|
|
96
|
+
END
|
|
97
|
+
END AS client_id FROM recent JOIN thread_items ON thread_items.rowid=recent.item_rowid
|
|
98
|
+
) SELECT projected.turn_id AS turnId,projected.client_id AS clientUserMessageId,
|
|
99
|
+
CASE WHEN typeof(thread_turns.status)='text' AND octet_length(thread_turns.status)<=11 THEN
|
|
100
|
+
CASE thread_turns.status WHEN 'inProgress' THEN 'running' WHEN 'completed' THEN 'completed'
|
|
101
|
+
WHEN 'failed' THEN 'failed' WHEN 'interrupted' THEN 'interrupted' ELSE 'unknown' END
|
|
102
|
+
ELSE 'unknown' END AS status
|
|
103
|
+
FROM projected LEFT JOIN thread_turns ON thread_turns.thread_id=? AND thread_turns.turn_id=projected.turn_id
|
|
104
|
+
WHERE projected.client_id=? LIMIT 2`);
|
|
105
|
+
if (request.mode === 'capability')
|
|
106
|
+
return { supported: true, injectionObserved: false };
|
|
107
|
+
const rows = query.all(request.threadId, request.threadId, request.clientUserMessageId);
|
|
108
|
+
if (rows.length > 1)
|
|
109
|
+
return fail('metadata_ambiguous');
|
|
110
|
+
if (!rows.length)
|
|
111
|
+
return { supported: true, injectionObserved: false };
|
|
112
|
+
const row = rows[0];
|
|
113
|
+
if (row.clientUserMessageId !== request.clientUserMessageId || !safe(row.turnId, 128) ||
|
|
114
|
+
!['running', 'completed', 'failed', 'interrupted', 'unknown'].includes(row.status))
|
|
115
|
+
return fail('metadata_probe_failed');
|
|
116
|
+
return { supported: true, injectionObserved: true, clientUserMessageId: request.clientUserMessageId, turn: { id: row.turnId, status: row.status } };
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return fail('metadata_probe_failed');
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
try {
|
|
123
|
+
db?.close();
|
|
124
|
+
}
|
|
125
|
+
catch { /* No diagnostics or storage mutation. */ }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function main(raw) {
|
|
129
|
+
let request;
|
|
130
|
+
try {
|
|
131
|
+
request = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(raw));
|
|
132
|
+
if (!request || !['snapshot', 'capability', 'observe'].includes(request.mode) || !safe(request.database, 2200) ||
|
|
133
|
+
!isAbsolute(request.database) || basename(request.database) !== 'thread_history_1.sqlite' ||
|
|
134
|
+
!/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/.test(request.threadId) ||
|
|
135
|
+
Object.keys(request).some(key => !['mode', 'database', 'threadId', 'clientUserMessageId', 'expectedStorageStamp'].includes(key)) ||
|
|
136
|
+
(request.expectedStorageStamp !== undefined && !/^[a-f0-9]{64}$/.test(request.expectedStorageStamp)) ||
|
|
137
|
+
(request.mode === 'observe' && !safe(request.clientUserMessageId, 128)))
|
|
138
|
+
return fail('metadata_probe_failed');
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return fail('metadata_probe_failed');
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
const before = storageStamp(request.database);
|
|
145
|
+
if (request.expectedStorageStamp !== undefined && before !== request.expectedStorageStamp)
|
|
146
|
+
return fail('metadata_storage_untrusted');
|
|
147
|
+
const result = request.mode === 'snapshot' ? { supported: true, injectionObserved: false } : queryDatabase(request);
|
|
148
|
+
if (!result.supported)
|
|
149
|
+
return result;
|
|
150
|
+
if (storageStamp(request.database) !== before)
|
|
151
|
+
return fail('metadata_storage_untrusted');
|
|
152
|
+
return { ...result, storageStamp: before };
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return fail('metadata_storage_untrusted');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
let bytes = 0, oversized = false;
|
|
159
|
+
const parts = [];
|
|
160
|
+
process.stdin.on('data', (part) => { bytes += part.length; if (bytes > 4096) {
|
|
161
|
+
oversized = true;
|
|
162
|
+
parts.length = 0;
|
|
163
|
+
}
|
|
164
|
+
else if (!oversized)
|
|
165
|
+
parts.push(part); });
|
|
166
|
+
process.stdin.once('end', () => {
|
|
167
|
+
const result = oversized ? fail('metadata_probe_failed') : main(Buffer.concat(parts));
|
|
168
|
+
process.stdout.write(JSON.stringify(result) + '\n');
|
|
169
|
+
});
|
|
170
|
+
process.stdin.once('error', () => { process.stdout.write(JSON.stringify(fail('metadata_probe_failed')) + '\n'); });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { HandoffLedger } from './handoff-ledger.js';
|
|
2
|
+
import { HandoffBudget, type WaitFor } from './handoff.js';
|
|
3
|
+
import { type MetadataQualification } from './metadata-observation.js';
|
|
4
|
+
import { type SendOptions } from './send.js';
|
|
5
|
+
import { type NativeProfile } from './native-profiles.js';
|
|
6
|
+
export type HandoffOptions = {
|
|
7
|
+
correlationId?: string;
|
|
8
|
+
requestAck?: boolean;
|
|
9
|
+
observeDelivery?: boolean;
|
|
10
|
+
remote?: boolean;
|
|
11
|
+
waitFor?: Exclude<WaitFor, 'none'>;
|
|
12
|
+
seconds?: number;
|
|
13
|
+
ledgerPath?: string;
|
|
14
|
+
payload?: string;
|
|
15
|
+
budget?: HandoffBudget;
|
|
16
|
+
resultOverhead?: number;
|
|
17
|
+
};
|
|
18
|
+
export type OperationResult = {
|
|
19
|
+
value: Record<string, unknown>;
|
|
20
|
+
exitCode: number;
|
|
21
|
+
};
|
|
22
|
+
export declare function ledgerPath(): string;
|
|
23
|
+
export declare function privateMessage(path: string): string;
|
|
24
|
+
export declare function correlationEnvelope(message: string, id: string): string;
|
|
25
|
+
export declare function observationQualification(nativeVersion: string, platform: string, remote: boolean, table?: readonly NativeProfile[]): MetadataQualification | undefined;
|
|
26
|
+
export declare function prepareHandoff(options: SendOptions, path?: string, correlationId?: string, budget?: HandoffBudget): Promise<OperationResult>;
|
|
27
|
+
export declare function handoffSend(options: SendOptions, handoffOptions: HandoffOptions): Promise<OperationResult>;
|
|
28
|
+
export declare function waitForHandoff(ledger: HandoffLedger, id: string, goal: 'delivered' | 'acknowledged', budget: HandoffBudget): Promise<OperationResult>;
|
|
29
|
+
export declare function handoffCommand(args: string[], readInput: (limit: number) => Promise<string>): Promise<OperationResult>;
|