@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
158 lines • 6.46 kB
JavaScript
import { detachedLabel, sessionKey } from "./rpc-monitor-render.js";
const REMOTE_INSTANCE = "remote";
/** Turn ids remembered per session: enough to drop a resnapshot's repeats, bounded for a long watch. */
const SETTLED_KEPT = 16;
export class RpcRemoteFeed {
#target;
#options;
#host;
#settled = new Map();
#cursor;
#opened = [];
#subscription;
#lostReported = false;
#stopped = false;
constructor(target, options, host) {
this.#target = target;
this.#options = options;
this.#host = host;
for (const [key, turns] of Object.entries(options.saved?.settled ?? {}))
this.#settled.set(key, [...turns]);
this.#cursor = options.saved?.cursor;
}
/** Opens the subscription; resolves to the arm-time rows. Callbacks before the open resolves wait for it. */
async start() {
const pending = [];
let ready = false;
const run = (step) => {
if (this.#stopped)
return;
if (ready)
step();
else
pending.push(step);
};
const { events, exclude, source } = this.#options;
const subscription = await source.open(this.#target, { events, exclude, ...(this.#cursor === undefined ? {} : { resumeFrom: this.#cursor }) }, {
record: (record) => run(() => this.#record(record)),
resynced: (state) => run(() => this.#resync(state)),
cursor: (cursor) => run(() => {
this.#cursor = cursor;
this.#host.checkpoint();
}),
lost: (reason) => run(() => this.#lost(reason)),
ended: (reason) => run(() => this.#ended(reason)),
});
if (this.#stopped) {
subscription.stop();
return [];
}
this.#subscription = subscription;
const { initial } = subscription;
const device = this.#target.device;
this.#host.hosts.set(device, { instanceId: REMOTE_INSTANCE, generation: initial.generation ?? "" });
this.#opened = initial.settledTurns ?? [];
for (const turn of this.#opened)
this.#remember(turnKey(device, turn), turn.turnId);
if (initial.cursor !== undefined)
this.#cursor = initial.cursor;
const rows = this.#host.renderer.seed(device, initial.rows);
this.#host.checkpoint();
ready = true;
for (const step of pending.splice(0))
step();
return rows;
}
stop() {
this.#stopped = true;
this.#subscription?.stop();
this.#subscription = undefined;
}
checkpoint() {
const settled = Object.fromEntries([...this.#settled].map(([key, turns]) => [key, [...turns]]));
return this.#cursor === undefined ? { settled } : { cursor: this.#cursor, settled };
}
/** After a restore: the turns that settled while nothing watched, which the saved checkpoint had not seen. */
diffSettled(saved) {
return this.#opened
.filter((turn) => !saved?.settled[turnKey(this.#target.device, turn)]?.includes(turn.turnId))
.map((turn) => settledLine(turnKey(this.#target.device, turn), turn.turnId));
}
#record(record) {
const device = this.#target.device;
if (record.type !== "agent_settled" || typeof record.turnId !== "string") {
this.#host.record(device, record);
return;
}
const key = sessionKey(device, record);
if (this.#seen(key, record.turnId))
return;
this.#remember(key, record.turnId);
this.#host.record(device, record);
this.#host.checkpoint();
}
#resync(state) {
const device = this.#target.device;
const lines = [];
const host = this.#host.hosts.get(device);
if (state.generation !== undefined) {
if (host !== undefined && host.generation !== state.generation)
lines.push(`rpc host ${device} restarted (generation ${host.generation} -> ${state.generation})`);
this.#host.hosts.set(device, { instanceId: REMOTE_INSTANCE, generation: state.generation });
}
const before = new Map([...this.#host.renderer.statuses()].filter(([key]) => key.startsWith(`${device}#`)));
const now = new Map(state.rows.map((row) => [sessionKey(device, row), String(row.status)]));
for (const [key, status] of now) {
const was = before.get(key) ?? "unknown";
if (was !== status)
lines.push(`rpc ${detachedLabel(key)} status ${was} -> ${status} (changed while detached)`);
}
for (const key of before.keys())
if (!now.has(key))
lines.push(`rpc ${detachedLabel(key)} closed (gone while detached)`);
for (const turn of state.settledTurns ?? []) {
const key = turnKey(device, turn);
if (this.#seen(key, turn.turnId))
continue;
this.#remember(key, turn.turnId);
lines.push(settledLine(key, turn.turnId));
}
this.#host.renderer.forgetSocket(device);
this.#host.renderer.seed(device, state.rows);
if (state.cursor !== undefined)
this.#cursor = state.cursor;
this.#lostReported = false;
for (const line of lines)
this.#host.line(line);
this.#host.checkpoint();
}
#lost(reason) {
if (this.#lostReported)
return;
this.#lostReported = true;
this.#host.line(`rpc device ${this.#target.device} unreachable (${reason}); re-watching`);
}
#ended(reason) {
this.#host.line(`rpc ${this.#target.device}/${this.#target.session} access ended (${reason})`);
this.#host.ended(`access ended (${reason})`);
}
#seen(key, turnId) {
return this.#settled.get(key)?.includes(turnId) === true;
}
#remember(key, turnId) {
const turns = this.#settled.get(key) ?? [];
if (turns.includes(turnId))
return;
turns.push(turnId);
if (turns.length > SETTLED_KEPT)
turns.shift();
this.#settled.set(key, turns);
}
}
function turnKey(device, turn) {
return `${device}#${turn.sessionId}`;
}
function settledLine(key, turnId) {
return `rpc ${detachedLabel(key)} turn ${turnId} settled while detached`;
}
//# sourceMappingURL=rpc-monitor-remote.js.map