@fitchmultz/pi-subagents 0.47.0 → 0.49.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/CHANGELOG.md +29 -3
- package/dist/extension/build-info.js +1 -1
- package/dist/pi-intercom/bridge-broadcast.js +60 -0
- package/dist/pi-intercom/bridge-catalog.js +372 -0
- package/dist/pi-intercom/bridge-config.js +15 -3
- package/dist/pi-intercom/bridge-control.js +104 -0
- package/dist/pi-intercom/bridge-groups.js +135 -0
- package/dist/pi-intercom/bridge-http.js +8 -1
- package/dist/pi-intercom/bridge-launch-journal.js +84 -0
- package/dist/pi-intercom/bridge-launch-policy.js +71 -5
- package/dist/pi-intercom/bridge-launch.js +186 -76
- package/dist/pi-intercom/bridge-mailbox.js +3 -55
- package/dist/pi-intercom/bridge-operations.js +127 -0
- package/dist/pi-intercom/bridge-presence.js +5 -1
- package/dist/pi-intercom/bridge-protocol.js +7 -0
- package/dist/pi-intercom/bridge-routes.js +195 -27
- package/dist/pi-intercom/bridge-rpc-process.js +43 -15
- package/dist/pi-intercom/bridge-session.js +83 -66
- package/dist/pi-intercom/bridge-storage.js +73 -0
- package/dist/pi-intercom/bridge-target.js +22 -0
- package/dist/pi-intercom/bridge-wake-owner.js +57 -0
- package/dist/pi-intercom/bridge-wake-state.js +293 -0
- package/dist/pi-intercom/bridge-wake.js +190 -99
- package/dist/pi-intercom/bridge.js +39 -39
- package/dist/pi-intercom/broker/client.js +1 -0
- package/dist/pi-intercom/connection.js +2 -3
- package/dist/pi-intercom/inbound.js +2 -4
- package/dist/pi-intercom/index.js +7 -1
- package/dist/pi-intercom/native-control-auth.js +80 -0
- package/dist/pi-intercom/native-control-protocol.js +111 -0
- package/dist/pi-intercom/native-control.js +130 -0
- package/dist/pi-intercom/saved-transcript.js +135 -0
- package/dist/pi-intercom/session-targets.js +3 -0
- package/dist/pi-intercom/transcript.js +175 -0
- package/docs/intercom-bridge.md +315 -174
- package/docs/intercom.md +1 -1
- package/docs/reference.md +1 -1
- package/package.json +1 -1
- package/scripts/intercom-bridge-wake-preflight.mjs +112 -0
- package/scripts/intercom-remote.mjs +213 -38
- package/src/pi-intercom/bridge-broadcast.ts +70 -0
- package/src/pi-intercom/bridge-catalog.ts +489 -0
- package/src/pi-intercom/bridge-config.ts +20 -3
- package/src/pi-intercom/bridge-control.ts +179 -0
- package/src/pi-intercom/bridge-groups.ts +169 -0
- package/src/pi-intercom/bridge-http.ts +15 -1
- package/src/pi-intercom/bridge-launch-journal.ts +122 -0
- package/src/pi-intercom/bridge-launch-policy.ts +88 -7
- package/src/pi-intercom/bridge-launch.ts +296 -107
- package/src/pi-intercom/bridge-mailbox.ts +3 -59
- package/src/pi-intercom/bridge-operations.ts +169 -0
- package/src/pi-intercom/bridge-presence.ts +7 -1
- package/src/pi-intercom/bridge-protocol.ts +8 -0
- package/src/pi-intercom/bridge-routes.ts +291 -49
- package/src/pi-intercom/bridge-rpc-process.ts +53 -16
- package/src/pi-intercom/bridge-session.ts +98 -96
- package/src/pi-intercom/bridge-storage.ts +83 -0
- package/src/pi-intercom/bridge-target.ts +35 -0
- package/src/pi-intercom/bridge-wake-owner.ts +58 -0
- package/src/pi-intercom/bridge-wake-state.ts +321 -0
- package/src/pi-intercom/bridge-wake.ts +224 -130
- package/src/pi-intercom/bridge.ts +41 -49
- package/src/pi-intercom/broker/client.ts +3 -0
- package/src/pi-intercom/connection.ts +2 -3
- package/src/pi-intercom/inbound.ts +2 -4
- package/src/pi-intercom/index.ts +7 -1
- package/src/pi-intercom/native-control-auth.ts +89 -0
- package/src/pi-intercom/native-control-protocol.ts +180 -0
- package/src/pi-intercom/native-control.ts +160 -0
- package/src/pi-intercom/saved-transcript.ts +175 -0
- package/src/pi-intercom/session-targets.ts +4 -0
- package/src/pi-intercom/transcript.ts +226 -0
- package/src/pi-intercom/types.ts +2 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { X509Certificate } from "node:crypto";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { loadControlPolicy } from "./bridge-config.js";
|
|
6
|
+
import { getPiAgentDir } from "./agent-dir.js";
|
|
7
|
+
import { fingerprint } from "./bridge-protocol.js";
|
|
8
|
+
export function authenticatedCertificateChain(socket) {
|
|
9
|
+
const blocks = [];
|
|
10
|
+
const seen = new Set();
|
|
11
|
+
let cert = socket.getPeerCertificate(true);
|
|
12
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
13
|
+
const parsed = new X509Certificate(cert.raw);
|
|
14
|
+
if (seen.has(parsed.fingerprint256)) {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
seen.add(parsed.fingerprint256);
|
|
18
|
+
blocks.push(parsed.toString());
|
|
19
|
+
cert = cert.issuerCertificate;
|
|
20
|
+
}
|
|
21
|
+
return blocks.join("\n");
|
|
22
|
+
}
|
|
23
|
+
function certificates(pem) {
|
|
24
|
+
const blocks = pem.match(/-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----/g);
|
|
25
|
+
if (!blocks || blocks.length > 8) {
|
|
26
|
+
throw new Error("Invalid bounded certificate chain.");
|
|
27
|
+
}
|
|
28
|
+
return blocks.map((block) => new X509Certificate(block));
|
|
29
|
+
}
|
|
30
|
+
function validNow(cert) {
|
|
31
|
+
return Date.parse(cert.validFrom) <= Date.now() && Date.parse(cert.validTo) > Date.now();
|
|
32
|
+
}
|
|
33
|
+
function trustedChain(chain, roots) {
|
|
34
|
+
let current = chain.at(0);
|
|
35
|
+
if (current === undefined || current.ca || !validNow(current)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
39
|
+
const subject = current;
|
|
40
|
+
const issuer = [...roots, ...chain.slice(1)].find((candidate) => candidate.ca &&
|
|
41
|
+
validNow(candidate) &&
|
|
42
|
+
subject.checkIssued(candidate) &&
|
|
43
|
+
subject.verify(candidate.publicKey));
|
|
44
|
+
if (!issuer) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (roots.some((root) => root.fingerprint256 === issuer.fingerprint256)) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
current = issuer;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
function controlPolicyPath() {
|
|
55
|
+
const configured = process.env.PI_INTERCOM_BRIDGE_CONFIG;
|
|
56
|
+
if (configured !== undefined) {
|
|
57
|
+
return configured;
|
|
58
|
+
}
|
|
59
|
+
const standard = join(getPiAgentDir(), "intercom", "bridge.json");
|
|
60
|
+
return existsSync(standard) ? standard : join(homedir(), ".pi", "intercom-bridge", "config.json");
|
|
61
|
+
}
|
|
62
|
+
/** Unix broker peers are same-user trusted. Names/payload claims grant no authority. */
|
|
63
|
+
export function authorizedControlPeer(peerId, certificate) {
|
|
64
|
+
try {
|
|
65
|
+
const configPath = controlPolicyPath();
|
|
66
|
+
const config = loadControlPolicy(configPath);
|
|
67
|
+
const chain = certificates(certificate);
|
|
68
|
+
const client = chain.at(0);
|
|
69
|
+
if (client === undefined) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const fp = fingerprint(client.fingerprint256);
|
|
73
|
+
return (peerId === `remote-${fp.toLowerCase()}` &&
|
|
74
|
+
config.clients.has(fp) &&
|
|
75
|
+
trustedChain(chain, certificates(config.ca.toString("utf8"))));
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { isRecord, isUnknownArray } from "../shared/unknown.js";
|
|
2
|
+
export const CONTROL_SCHEMA = "pi-intercom/control-v1";
|
|
3
|
+
export const CONTROL_FALLBACK = "Native Intercom metadata exchange (no conversation response needed).";
|
|
4
|
+
export function isControlVerb(value) {
|
|
5
|
+
return (value === "capabilities" || value === "cancel" || value === "stop" || value === "transcript");
|
|
6
|
+
}
|
|
7
|
+
export function isControlRequest(value) {
|
|
8
|
+
return (isRecord(value) &&
|
|
9
|
+
value.schema === CONTROL_SCHEMA &&
|
|
10
|
+
value.kind === "request" &&
|
|
11
|
+
isControlVerb(value.verb) &&
|
|
12
|
+
typeof value.certificate === "string" &&
|
|
13
|
+
value.certificate.length <= 16384 &&
|
|
14
|
+
Object.keys(value).every((key) => ["schema", "kind", "verb", "certificate", "options"].includes(key)));
|
|
15
|
+
}
|
|
16
|
+
export function isControlResult(value) {
|
|
17
|
+
if (!isRecord(value) ||
|
|
18
|
+
value.schema !== CONTROL_SCHEMA ||
|
|
19
|
+
value.kind !== "result" ||
|
|
20
|
+
!isControlVerb(value.verb)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return (validResultFields(value) &&
|
|
24
|
+
validResultPayload(value) &&
|
|
25
|
+
Buffer.byteLength(JSON.stringify(value)) <= 1024 * 1024);
|
|
26
|
+
}
|
|
27
|
+
function optionalText(value, max) {
|
|
28
|
+
return value === undefined || (typeof value === "string" && value.length <= max);
|
|
29
|
+
}
|
|
30
|
+
function validResultFields(value) {
|
|
31
|
+
const fields = new Set([
|
|
32
|
+
"schema",
|
|
33
|
+
"kind",
|
|
34
|
+
"verb",
|
|
35
|
+
"status",
|
|
36
|
+
"mode",
|
|
37
|
+
"idle",
|
|
38
|
+
"pendingInput",
|
|
39
|
+
"queueCleared",
|
|
40
|
+
"processExited",
|
|
41
|
+
"stopSupport",
|
|
42
|
+
"reason",
|
|
43
|
+
"transcript",
|
|
44
|
+
"verbs",
|
|
45
|
+
]);
|
|
46
|
+
return (Object.keys(value).every((key) => fields.has(key)) &&
|
|
47
|
+
typeof value.status === "string" &&
|
|
48
|
+
["ok", "denied", "cancel_requested", "shutdown_requested", "unsupported", "failed"].includes(value.status) &&
|
|
49
|
+
optionalText(value.reason, 2048) &&
|
|
50
|
+
optionalText(value.mode, 64) &&
|
|
51
|
+
[value.idle, value.pendingInput].every((flag) => flag === undefined || typeof flag === "boolean") &&
|
|
52
|
+
[value.queueCleared, value.processExited].every((flag) => flag === undefined || flag === false));
|
|
53
|
+
}
|
|
54
|
+
function validResultPayload(value) {
|
|
55
|
+
if (value.stopSupport !== undefined &&
|
|
56
|
+
(typeof value.stopSupport !== "string" ||
|
|
57
|
+
!["native_shutdown", "requires_activity", "host_dependent"].includes(value.stopSupport))) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if (value.verbs !== undefined &&
|
|
61
|
+
(!isUnknownArray(value.verbs) || !value.verbs.every(isControlVerb))) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return value.transcript === undefined || validTranscript(value.transcript);
|
|
65
|
+
}
|
|
66
|
+
function validTranscript(value) {
|
|
67
|
+
if (!isRecord(value) || !isUnknownArray(value.records) || value.records.length > 100) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
const fields = new Set([
|
|
71
|
+
"sessionId",
|
|
72
|
+
"leafId",
|
|
73
|
+
"records",
|
|
74
|
+
"bytes",
|
|
75
|
+
"truncated",
|
|
76
|
+
"cursor",
|
|
77
|
+
"scanLimitReached",
|
|
78
|
+
"finalizedOnly",
|
|
79
|
+
"streaming",
|
|
80
|
+
]);
|
|
81
|
+
return (Object.keys(value).every((key) => fields.has(key)) &&
|
|
82
|
+
typeof value.sessionId === "string" &&
|
|
83
|
+
value.sessionId.length <= 512 &&
|
|
84
|
+
(value.leafId === null || typeof value.leafId === "string") &&
|
|
85
|
+
value.records.every(validTranscriptRecord) &&
|
|
86
|
+
validTranscriptMetadata(value));
|
|
87
|
+
}
|
|
88
|
+
function validTranscriptMetadata(value) {
|
|
89
|
+
return (typeof value.bytes === "number" &&
|
|
90
|
+
Number.isInteger(value.bytes) &&
|
|
91
|
+
value.bytes >= 0 &&
|
|
92
|
+
value.bytes <= 131072 &&
|
|
93
|
+
typeof value.truncated === "boolean" &&
|
|
94
|
+
optionalText(value.cursor, 512) &&
|
|
95
|
+
typeof value.scanLimitReached === "boolean" &&
|
|
96
|
+
value.finalizedOnly === true &&
|
|
97
|
+
value.streaming === "unavailable");
|
|
98
|
+
}
|
|
99
|
+
function validTranscriptRecord(value) {
|
|
100
|
+
if (!isRecord(value)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const fields = new Set(["id", "timestamp", "role", "text", "truncated"]);
|
|
104
|
+
return (Object.keys(value).every((key) => fields.has(key)) &&
|
|
105
|
+
[value.id, value.timestamp].every((field) => typeof field === "string" && field.length <= 512) &&
|
|
106
|
+
typeof value.role === "string" &&
|
|
107
|
+
["user", "assistant", "toolResult", "custom"].includes(value.role) &&
|
|
108
|
+
typeof value.text === "string" &&
|
|
109
|
+
Buffer.byteLength(value.text) <= 131072 &&
|
|
110
|
+
typeof value.truncated === "boolean");
|
|
111
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { authorizedControlPeer } from "./native-control-auth.js";
|
|
2
|
+
import { CONTROL_FALLBACK, CONTROL_SCHEMA, isControlRequest, } from "./native-control-protocol.js";
|
|
3
|
+
import { readLiveTranscript, transcriptOptions } from "./transcript.js";
|
|
4
|
+
export class NativeControls {
|
|
5
|
+
lifecycle;
|
|
6
|
+
connection;
|
|
7
|
+
constructor(lifecycle, connection) {
|
|
8
|
+
this.lifecycle = lifecycle;
|
|
9
|
+
this.connection = connection;
|
|
10
|
+
}
|
|
11
|
+
result(ctx, request) {
|
|
12
|
+
const base = {
|
|
13
|
+
schema: CONTROL_SCHEMA,
|
|
14
|
+
kind: "result",
|
|
15
|
+
verb: request.verb,
|
|
16
|
+
mode: ctx.mode,
|
|
17
|
+
idle: ctx.isIdle(),
|
|
18
|
+
pendingInput: ctx.hasPendingMessages(),
|
|
19
|
+
queueCleared: false,
|
|
20
|
+
processExited: false,
|
|
21
|
+
};
|
|
22
|
+
if (request.verb !== "transcript" && request.options !== undefined) {
|
|
23
|
+
throw new Error("Only transcript accepts options.");
|
|
24
|
+
}
|
|
25
|
+
const stopSupport = stopCapability(ctx);
|
|
26
|
+
switch (request.verb) {
|
|
27
|
+
case "capabilities":
|
|
28
|
+
return {
|
|
29
|
+
...base,
|
|
30
|
+
status: "ok",
|
|
31
|
+
stopSupport,
|
|
32
|
+
verbs: ["capabilities", "cancel", "stop", "transcript"],
|
|
33
|
+
};
|
|
34
|
+
case "transcript":
|
|
35
|
+
return {
|
|
36
|
+
...base,
|
|
37
|
+
status: "ok",
|
|
38
|
+
transcript: readLiveTranscript(ctx.sessionManager, transcriptOptions(request.options)),
|
|
39
|
+
};
|
|
40
|
+
case "cancel":
|
|
41
|
+
return {
|
|
42
|
+
...base,
|
|
43
|
+
status: "cancel_requested",
|
|
44
|
+
reason: "Native current-operation abort; accepted queued input is not cleared.",
|
|
45
|
+
};
|
|
46
|
+
case "stop":
|
|
47
|
+
return {
|
|
48
|
+
...base,
|
|
49
|
+
status: ctx.mode === "print" || ctx.mode === "json" ? "unsupported" : "shutdown_requested",
|
|
50
|
+
stopSupport,
|
|
51
|
+
reason: "Native shutdown/current-operation abort requested, not process exit. Idle RPC is deferred until a native command/settlement; print/JSON/SDK have no guaranteed shutdown binding.",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async receive(ctx, from, message) {
|
|
56
|
+
if (!Object.hasOwn(message, "control")) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
// Even malformed/response envelopes never enter conversation journaling or inference.
|
|
60
|
+
const request = message.control;
|
|
61
|
+
if (!isControlRequest(request) ||
|
|
62
|
+
message.replyTo !== undefined ||
|
|
63
|
+
message.delivery !== "passive" ||
|
|
64
|
+
message.expectsReply === true) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
const generation = this.lifecycle.generation;
|
|
68
|
+
const active = this.connection.active;
|
|
69
|
+
if (!active || !this.lifecycle.live(ctx, generation)) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
const result = this.admit(ctx, from.id, request);
|
|
73
|
+
// Receipt is acknowledged before shutdown can retire this socket/context.
|
|
74
|
+
await active.send(from.id, {
|
|
75
|
+
text: CONTROL_FALLBACK,
|
|
76
|
+
delivery: "passive",
|
|
77
|
+
replyTo: message.id,
|
|
78
|
+
control: result,
|
|
79
|
+
});
|
|
80
|
+
this.apply(ctx, generation, { active, peer: from.id, request, result });
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
apply(ctx, generation, state) {
|
|
84
|
+
const { active, peer, request, result } = state;
|
|
85
|
+
if (!this.lifecycle.live(ctx, generation) ||
|
|
86
|
+
this.connection.active !== active ||
|
|
87
|
+
!authorizedControlPeer(peer, request.certificate)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (result.status === "shutdown_requested" ||
|
|
91
|
+
(result.status === "unsupported" && request.verb === "stop")) {
|
|
92
|
+
ctx.shutdown();
|
|
93
|
+
if (this.lifecycle.live(ctx, generation)) {
|
|
94
|
+
ctx.abort();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (result.status === "cancel_requested") {
|
|
98
|
+
ctx.abort();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
admit(ctx, peer, request) {
|
|
102
|
+
if (!authorizedControlPeer(peer, request.certificate)) {
|
|
103
|
+
return {
|
|
104
|
+
schema: CONTROL_SCHEMA,
|
|
105
|
+
kind: "result",
|
|
106
|
+
verb: request.verb,
|
|
107
|
+
status: "denied",
|
|
108
|
+
reason: "Sender certificate is not currently authorized by private bridge configuration.",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
return this.result(ctx, request);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return {
|
|
116
|
+
schema: CONTROL_SCHEMA,
|
|
117
|
+
kind: "result",
|
|
118
|
+
verb: request.verb,
|
|
119
|
+
status: "failed",
|
|
120
|
+
reason: "Native control input/state is unavailable.",
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function stopCapability(ctx) {
|
|
126
|
+
if (ctx.mode === "tui") {
|
|
127
|
+
return "native_shutdown";
|
|
128
|
+
}
|
|
129
|
+
return ctx.mode === "rpc" ? "requires_activity" : "host_dependent";
|
|
130
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { constants, openSync, closeSync, fstatSync } from "node:fs";
|
|
2
|
+
import { loadNativeSession } from "../shared/native-session-loader.js";
|
|
3
|
+
import { scanJournal } from "../shared/journal-reader.js";
|
|
4
|
+
import { Fault } from "./bridge-protocol.js";
|
|
5
|
+
import { readLiveTranscript } from "./transcript.js";
|
|
6
|
+
const FILE_BYTES = 64 * 1024 * 1024;
|
|
7
|
+
const ENTRY_COUNT = 50000;
|
|
8
|
+
const TEXT_LIMIT = 131072;
|
|
9
|
+
const fields = new Set([
|
|
10
|
+
"type",
|
|
11
|
+
"id",
|
|
12
|
+
"parentId",
|
|
13
|
+
"timestamp",
|
|
14
|
+
"version",
|
|
15
|
+
"cwd",
|
|
16
|
+
"customType",
|
|
17
|
+
"display",
|
|
18
|
+
"content",
|
|
19
|
+
]);
|
|
20
|
+
/** Only branch structure and public textual content are hydrated, never image/provider/custom data. */
|
|
21
|
+
const transcriptProjection = (path) => {
|
|
22
|
+
if (path.length === 0) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (path[0] === "message") {
|
|
26
|
+
return messageField(path.slice(1));
|
|
27
|
+
}
|
|
28
|
+
if (path[0] === "content") {
|
|
29
|
+
return contentField(path.slice(1));
|
|
30
|
+
}
|
|
31
|
+
return path.length === 1 && fields.has(String(path[0])) ? 4096 : false;
|
|
32
|
+
};
|
|
33
|
+
function messageField(path) {
|
|
34
|
+
if (path.length === 0) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
if (path[0] === "content") {
|
|
38
|
+
return contentField(path.slice(1));
|
|
39
|
+
}
|
|
40
|
+
return path.length === 1 && ["role", "output", "display", "truncated"].includes(String(path[0]))
|
|
41
|
+
? TEXT_LIMIT
|
|
42
|
+
: false;
|
|
43
|
+
}
|
|
44
|
+
function contentField(path) {
|
|
45
|
+
return path.length < 2 || (path.length === 2 && ["type", "text"].includes(String(path[1])))
|
|
46
|
+
? TEXT_LIMIT
|
|
47
|
+
: false;
|
|
48
|
+
}
|
|
49
|
+
function textField(path) {
|
|
50
|
+
if (path[0] === "message") {
|
|
51
|
+
return ((path.length === 2 && ["content", "output"].includes(String(path[1]))) ||
|
|
52
|
+
(path.length === 4 && path[1] === "content" && path[3] === "text"));
|
|
53
|
+
}
|
|
54
|
+
return path[0] === "content" && (path.length === 1 || (path.length === 3 && path[2] === "text"));
|
|
55
|
+
}
|
|
56
|
+
function savedLines(fd, size) {
|
|
57
|
+
const lines = [];
|
|
58
|
+
const cut = new Set();
|
|
59
|
+
const lengths = new Map();
|
|
60
|
+
scanJournal(fd, transcriptProjection, ({ value }) => {
|
|
61
|
+
if (lines.length >= ENTRY_COUNT) {
|
|
62
|
+
throw new Fault(413, "transcript_too_large", "Saved journal exceeds the 50000-entry budget.");
|
|
63
|
+
}
|
|
64
|
+
lines.push(JSON.stringify(value));
|
|
65
|
+
lengths.clear();
|
|
66
|
+
}, {
|
|
67
|
+
policy: "inspect",
|
|
68
|
+
requireNewline: true,
|
|
69
|
+
end: size,
|
|
70
|
+
limits: { depth: 64, nodes: 16384, arrayLength: 4096, keyLength: 512 },
|
|
71
|
+
malformed: (_start, _end, error) => {
|
|
72
|
+
if (error instanceof RangeError) {
|
|
73
|
+
throw new Fault(413, "transcript_too_large", "Saved record exceeds projection limits.");
|
|
74
|
+
}
|
|
75
|
+
lengths.clear();
|
|
76
|
+
cut.delete(lines.length);
|
|
77
|
+
},
|
|
78
|
+
stringChunk: (path, chunk) => {
|
|
79
|
+
if (textField(path)) {
|
|
80
|
+
const key = JSON.stringify(path);
|
|
81
|
+
const count = (lengths.get(key) ?? 0) + chunk.length;
|
|
82
|
+
lengths.set(key, count);
|
|
83
|
+
if (count > TEXT_LIMIT) {
|
|
84
|
+
cut.add(lines.length);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
return { lines, cut };
|
|
90
|
+
}
|
|
91
|
+
/** Internal hook: caller MUST resolve a catalog-approved saved ID/name, never accept an HTTP filepath. */
|
|
92
|
+
export async function readSavedTranscript(packageRoot, approvedFile, options = {}) {
|
|
93
|
+
const sdk = await loadNativeSession(packageRoot);
|
|
94
|
+
const fd = openSync(approvedFile, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
95
|
+
try {
|
|
96
|
+
const before = fstatSync(fd);
|
|
97
|
+
if (!before.isFile() || before.size > FILE_BYTES) {
|
|
98
|
+
throw new Fault(413, "transcript_too_large", "Saved journal exceeds the 64 MiB read budget.");
|
|
99
|
+
}
|
|
100
|
+
const { lines, cut } = savedLines(fd, before.size);
|
|
101
|
+
const after = fstatSync(fd);
|
|
102
|
+
if (after.size !== before.size ||
|
|
103
|
+
after.mtimeMs !== before.mtimeMs ||
|
|
104
|
+
after.ctimeMs !== before.ctimeMs) {
|
|
105
|
+
throw new Fault(409, "session_changed", "Saved journal changed during reading; refresh the catalog.");
|
|
106
|
+
}
|
|
107
|
+
const entries = sdk.parseSessionEntries(lines.join("\n"));
|
|
108
|
+
return savedPage(entries, cut, options, sdk);
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
closeSync(fd);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function savedPage(entries, cut, options, sdk) {
|
|
115
|
+
const header = entries.find((entry) => entry.type === "session");
|
|
116
|
+
if (header === undefined) {
|
|
117
|
+
throw new Fault(409, "invalid_session", "Saved journal has no native session header.");
|
|
118
|
+
}
|
|
119
|
+
const legacy = (header.version ?? 1) < 2;
|
|
120
|
+
if (legacy && options.cursor !== undefined) {
|
|
121
|
+
throw new Fault(409, "cursor_unavailable", "Legacy journals have no stable entry IDs; request a fresh bounded recent transcript.");
|
|
122
|
+
}
|
|
123
|
+
// Native inMemory reconstructs branch state/migrations without ever opening or writing the file.
|
|
124
|
+
const manager = sdk.SessionManager.inMemory(header.cwd, undefined, entries);
|
|
125
|
+
const page = readLiveTranscript(manager, options);
|
|
126
|
+
const cutIds = new Set(entries.flatMap((entry, index) => entry.type !== "session" && cut.has(index) ? [entry.id] : []));
|
|
127
|
+
const records = page.records.map((entry) => cutIds.has(entry.id) ? { ...entry, truncated: true } : entry);
|
|
128
|
+
return {
|
|
129
|
+
...page,
|
|
130
|
+
// ponytail: v1 has no persisted entry IDs; use catalog snapshot cursors if legacy pagination is needed.
|
|
131
|
+
cursor: legacy ? undefined : page.cursor,
|
|
132
|
+
records,
|
|
133
|
+
truncated: page.truncated || records.some((entry) => entry.truncated),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -2,6 +2,9 @@ import { execFile } from "node:child_process";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
export const MIN_SESSION_TARGET_PREFIX_LENGTH = 8;
|
|
5
|
+
export function nativePeerId(sessionId) {
|
|
6
|
+
return `pi-${createHash("sha256").update(sessionId).digest("hex").slice(0, 32)}`;
|
|
7
|
+
}
|
|
5
8
|
export function shortSessionId(sessionId) {
|
|
6
9
|
return sessionId.slice(0, MIN_SESSION_TARGET_PREFIX_LENGTH);
|
|
7
10
|
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { isRecord, isUnknownArray } from "../shared/unknown.js";
|
|
2
|
+
import { Fault, label, object } from "./bridge-protocol.js";
|
|
3
|
+
const SCAN_LIMIT = 1024;
|
|
4
|
+
export function transcriptOptions(value = {}) {
|
|
5
|
+
const input = object(value, ["limit", "maxBytes", "cursor"]);
|
|
6
|
+
const limit = integer(input.limit, 50, 1, 100);
|
|
7
|
+
const maxBytes = integer(input.maxBytes, 65536, 256, 131072);
|
|
8
|
+
const cursor = input.cursor === undefined ? undefined : label(input.cursor, "cursor", 512);
|
|
9
|
+
return { limit, maxBytes, cursor };
|
|
10
|
+
}
|
|
11
|
+
function integer(value, fallback, min, max) {
|
|
12
|
+
const result = value === undefined ? fallback : value;
|
|
13
|
+
if (typeof result !== "number" || !Number.isInteger(result) || result < min || result > max) {
|
|
14
|
+
throw new Fault(400, "invalid_input", `Transcript limit must be an integer from ${min} to ${max}.`);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
function textualContent(content) {
|
|
19
|
+
if (typeof content === "string") {
|
|
20
|
+
return { text: content, truncated: false };
|
|
21
|
+
}
|
|
22
|
+
if (!isUnknownArray(content)) {
|
|
23
|
+
return { text: "", truncated: false };
|
|
24
|
+
}
|
|
25
|
+
const pieces = [];
|
|
26
|
+
let remaining = 131073;
|
|
27
|
+
let truncated = content.length > 4096;
|
|
28
|
+
for (const part of content.slice(0, 4096)) {
|
|
29
|
+
if (remaining <= 0) {
|
|
30
|
+
truncated = true;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
if (isRecord(part) && part.type === "text" && typeof part.text === "string") {
|
|
34
|
+
const text = part.text.slice(0, remaining);
|
|
35
|
+
pieces.push(text);
|
|
36
|
+
truncated ||= text.length < part.text.length;
|
|
37
|
+
remaining -= text.length + 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { text: pieces.join("\n"), truncated };
|
|
41
|
+
}
|
|
42
|
+
function visible(entry) {
|
|
43
|
+
if (entry.type === "custom_message") {
|
|
44
|
+
return entry.display ? { role: "custom", ...textualContent(entry.content) } : undefined;
|
|
45
|
+
}
|
|
46
|
+
if (entry.type !== "message") {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const message = entry.message;
|
|
50
|
+
if (message.role === "bashExecution") {
|
|
51
|
+
return {
|
|
52
|
+
role: "toolResult",
|
|
53
|
+
text: message.output,
|
|
54
|
+
truncated: nativeTruncation(message.truncated),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (!["user", "assistant", "toolResult", "custom"].includes(message.role)) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
if (message.role === "custom" && !message.display) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return "content" in message
|
|
64
|
+
? { role: message.role, ...textualContent(message.content) }
|
|
65
|
+
: undefined;
|
|
66
|
+
}
|
|
67
|
+
function nativeTruncation(value) {
|
|
68
|
+
// Older saved bash entries may predate the native boolean field.
|
|
69
|
+
return value === true;
|
|
70
|
+
}
|
|
71
|
+
function boundedText(text, budget) {
|
|
72
|
+
// Slice before UTF-8 encoding to avoid copying arbitrarily large live tool output.
|
|
73
|
+
const bytes = Buffer.from(text.slice(0, budget));
|
|
74
|
+
if (bytes.length <= budget) {
|
|
75
|
+
return bytes.toString();
|
|
76
|
+
}
|
|
77
|
+
let end = budget;
|
|
78
|
+
while (end > 0 && (bytes[end] & 0xc0) === 0x80) {
|
|
79
|
+
end -= 1;
|
|
80
|
+
}
|
|
81
|
+
return bytes.subarray(0, end).toString();
|
|
82
|
+
}
|
|
83
|
+
function startId(manager, leaf, cursor) {
|
|
84
|
+
if (cursor === undefined) {
|
|
85
|
+
return { id: leaf, scanned: 0 };
|
|
86
|
+
}
|
|
87
|
+
let raw;
|
|
88
|
+
try {
|
|
89
|
+
raw = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
throw new Fault(400, "invalid_cursor", "Invalid transcript cursor.");
|
|
93
|
+
}
|
|
94
|
+
const data = object(raw, ["leaf", "before"]);
|
|
95
|
+
if (data.leaf !== leaf) {
|
|
96
|
+
throw new Fault(409, "branch_changed", "Active branch changed; request a new transcript page.");
|
|
97
|
+
}
|
|
98
|
+
const before = label(data.before, "cursor entry", 128);
|
|
99
|
+
let id = leaf;
|
|
100
|
+
for (let scanned = 0; id !== null && scanned < SCAN_LIMIT; scanned += 1) {
|
|
101
|
+
if (id === before) {
|
|
102
|
+
return { id, scanned };
|
|
103
|
+
}
|
|
104
|
+
id = manager.getEntry(id)?.parentId ?? null;
|
|
105
|
+
}
|
|
106
|
+
throw new Fault(400, "invalid_cursor", "Cursor is outside the bounded active branch.");
|
|
107
|
+
}
|
|
108
|
+
/** Finalized textual output on the selected branch; never builds full history/context. */
|
|
109
|
+
export function readLiveTranscript(manager, input = {}) {
|
|
110
|
+
const options = transcriptOptions(input);
|
|
111
|
+
const leaf = manager.getLeafId();
|
|
112
|
+
const start = startId(manager, leaf, options.cursor);
|
|
113
|
+
let id = start.id;
|
|
114
|
+
let scanned = start.scanned;
|
|
115
|
+
const records = [];
|
|
116
|
+
let bytes = 0;
|
|
117
|
+
let textTruncated = false;
|
|
118
|
+
while (id !== null && scanned < SCAN_LIMIT) {
|
|
119
|
+
scanned += 1;
|
|
120
|
+
const entry = manager.getEntry(id);
|
|
121
|
+
if (!entry) {
|
|
122
|
+
textTruncated = true;
|
|
123
|
+
id = null;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
const record = transcriptRecord(entry, options.maxBytes - bytes);
|
|
127
|
+
if (record !== undefined) {
|
|
128
|
+
records.push(record);
|
|
129
|
+
bytes += Buffer.byteLength(record.text);
|
|
130
|
+
textTruncated ||= record.truncated;
|
|
131
|
+
}
|
|
132
|
+
id = entry.parentId;
|
|
133
|
+
if (records.length >= options.limit || bytes >= options.maxBytes) {
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return transcriptPage(manager, {
|
|
138
|
+
leaf,
|
|
139
|
+
records,
|
|
140
|
+
bytes,
|
|
141
|
+
textTruncated,
|
|
142
|
+
id,
|
|
143
|
+
scanLimitReached: scanned >= SCAN_LIMIT && id !== null,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function transcriptRecord(entry, budget) {
|
|
147
|
+
const output = visible(entry);
|
|
148
|
+
if (output === undefined || output.text.length === 0) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
const text = boundedText(output.text, budget);
|
|
152
|
+
return {
|
|
153
|
+
id: entry.id,
|
|
154
|
+
timestamp: entry.timestamp,
|
|
155
|
+
role: output.role,
|
|
156
|
+
text,
|
|
157
|
+
truncated: text !== output.text || output.truncated,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function transcriptPage(manager, state) {
|
|
161
|
+
const { leaf, records, bytes, textTruncated, id, scanLimitReached } = state;
|
|
162
|
+
return {
|
|
163
|
+
sessionId: manager.getSessionId(),
|
|
164
|
+
leafId: leaf,
|
|
165
|
+
records: records.reverse(),
|
|
166
|
+
bytes,
|
|
167
|
+
truncated: id !== null || textTruncated,
|
|
168
|
+
scanLimitReached,
|
|
169
|
+
...(id !== null && !scanLimitReached
|
|
170
|
+
? { cursor: Buffer.from(JSON.stringify({ leaf, before: id })).toString("base64url") }
|
|
171
|
+
: {}),
|
|
172
|
+
finalizedOnly: true,
|
|
173
|
+
streaming: "unavailable",
|
|
174
|
+
};
|
|
175
|
+
}
|