@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
98 lines • 4.87 kB
JavaScript
import { compactUpdatesEnabled } from "../../core/engine-socket-flag.js";
import { CONNECTION_ROLE_FIXED, DELEGATION_INCARNATION_STALE } from "./connection-roles.js";
import { COMPACT_EVENTS_CAPABILITY } from "./custom-capability.js";
import { EVENT_RESYNC_CAPABILITY } from "./socket-peer-recovery.js";
const COMPACT_UPDATES_CAPABILITY = "compact_message_updates_v1";
const COMPACT_PAIR = [COMPACT_UPDATES_CAPABILITY, EVENT_RESYNC_CAPABILITY];
/** A declaration belongs to one actual socket registration, never a session or launch profile. */
export class CompactSelection {
constructor() {
this.capabilities = [];
this.selected = false;
this.admitted = false;
this.refused = false;
}
declare(next, supported, roles) {
if (this.refused)
return "compact_selection_refused";
const caps = next ?? this.capabilities;
const compact = compactUpdatesEnabled() && caps.includes(COMPACT_UPDATES_CAPABILITY);
const pair = compact && caps.includes(EVENT_RESYNC_CAPABILITY);
const refusal = compact && !pair
? "compact_pair_required"
: compact && !supported
? "compact_endpoint_unsupported"
: this.selected && !pair
? "compact_selection_fixed"
: this.admitted && pair !== this.selected
? "compact_selection_fixed"
: undefined;
if (refusal) {
if (!this.selected && compact && !this.admitted)
this.refused = true;
return refusal;
}
if (next !== undefined && !roles(caps)) {
if (compact && !this.selected && !this.admitted)
this.refused = true;
return "connection_role_fixed";
}
this.capabilities = caps.filter((cap) => cap !== COMPACT_EVENTS_CAPABILITY && (compactUpdatesEnabled() || cap !== COMPACT_UPDATES_CAPABILITY));
this.selected = pair;
return undefined;
}
wireCapabilities(caps, enabled) {
return !enabled
? caps.filter((cap) => cap !== COMPACT_EVENTS_CAPABILITY)
: this.selected
? [...caps, COMPACT_EVENTS_CAPABILITY]
: caps;
}
ack() {
return this.selected ? { capabilities: [...COMPACT_PAIR] } : undefined;
}
}
/** Unknown/inherited opt-ins cannot stand in for an actual endpoint implementation. */
export function advertisedCompactCapabilities(caps, supported) {
// RULING-111 (LEAK-E): with the flag off the launch capabilities are echoed exactly as on main.
if (!compactUpdatesEnabled())
return [...caps];
const legacy = caps.filter((cap) => cap !== COMPACT_UPDATES_CAPABILITY && cap !== COMPACT_EVENTS_CAPABILITY);
return supported ? [...new Set([...legacy, ...COMPACT_PAIR])] : legacy;
}
export function declarationReply(record, selection) {
const response = record;
if (response.type !== "response" || response.command !== "set_client_info")
return record;
// RULING-111: G1's ID-less silence exists only with the compact negotiation; flag off answers as main does.
if (response.id === undefined)
return compactUpdatesEnabled() ? undefined : record;
return response.success && selection?.selected ? { ...record, data: selection.ack() } : record;
}
/**
* Shared handler applies session flags only after host validation; classic handlers never select compact. `incarnation`
* is the one the declaring command captured (omo-relay-daemon#307); a declaration from a retired one is refused for
* this command only (`delegation_incarnation_stale`), locally or by the host.
*/
export function sessionDeclaration(options, command, current, declare, replace, incarnation) {
const caps = command.capabilities;
if (caps !== undefined) {
if (!options.clientInfo && compactUpdatesEnabled() && caps.includes(COMPACT_UPDATES_CAPABILITY))
return { capabilities: current, refusal: "compact_endpoint_unsupported" };
const verdict = declare(caps, incarnation);
if (verdict === "stale")
return { capabilities: current, refusal: DELEGATION_INCARNATION_STALE };
if (verdict === "fixed")
return { capabilities: current, refusal: CONNECTION_ROLE_FIXED };
if (!options.clientInfo)
replace(caps);
else if (options.clientInfo.setCapabilities(options.clientInfo.connectionId(), caps, incarnation) === "stale")
return { capabilities: current, refusal: DELEGATION_INCARNATION_STALE };
}
return {
capabilities: caps ?? current,
refusal: undefined,
grantChanged: (current?.includes("grant_select") === true) !== ((caps ?? current)?.includes("grant_select") === true),
};
}
//# sourceMappingURL=compact-negotiation.js.map