Files
orca/src/shared/pty-source-credit-validation.ts
T
JinjingandOrcaWin 5f7807497e feat(ssh): bound relay PTY output end to end (#11005)
* docs: design SSH relay PTY backpressure

* fix(ssh): bound relay frame decoding

* fix(relay): bound PTY output publication

* fix(ssh): bound PTY model admission

* fix(ssh): settle closed model admissions

* feat(ssh): negotiate bounded PTY consumer sessions

* fix(ssh): fence exit on renderer settlement

* feat(ssh): track PTY source credit end to end

* fix(ssh): recover bounded PTY output across reconnect

* feat(ssh): complete relay PTY output backpressure

* fix(ssh): close final PTY source credit races

* docs(ssh): record final backpressure validation

* feat(ssh): complete relay PTY source-credit lifecycle

* test(ssh): complete provider notification fixture

* fix(ssh): preserve terminal source credit across rotation

* fix(ssh): fail closed on recovery cancellation

* fix(ssh): prioritize mux control writes after drain

* fix(ssh): retire canceled relay restore deliveries

* fix(ssh): order exit cancellation cleanup

* fix(ssh): gate provisional source activation

* test(ssh): register mux drain-priority coverage

* fix(ssh): type stale owner recovery mismatches

* fix(ssh): close projection replacement races

* fix(relay): contain streaming edge failures

* fix(ssh): secure relay endpoint credentials

* docs(ssh): reconcile final backpressure lifecycle

* fix(ssh): bound main IPC output lifecycle

* fix(ssh): close recovery ownership gaps

* docs(ssh): record exact artifact validation

* fix(ssh): reject reclaimed snapshot replacements

* fix(ssh): fence model admission across reconnect

* fix(ssh): contain migration failure per PTY

* docs(ssh): record final exact-head validation

* test(ssh): align deploy fixtures with credential publication

* feat(ssh): add per-target bounded output setting

* fix(ssh): close source recovery review gaps

* fix(ssh): latch source credit environment override

* feat(ssh): make PTY source credit the default

* docs(ssh): record always-on relay validation

* docs(ssh): bind validation to current main

* test(ssh): grant source credit in IPC fixture

* test(ssh): grant source credit in fake relay

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-07-29 17:03:15 -07:00

64 lines
2.5 KiB
TypeScript

import type {
PtySourceCreditAck,
PtySourceDeliveryIdentity,
PtySourceSpan
} from './pty-source-credit-contract'
export function assertPositiveSafeInteger(value: number, name: string): void {
if (!Number.isSafeInteger(value) || value <= 0) {
throw new Error(`${name} must be a positive safe integer`)
}
}
export function assertNonNegativeSafeInteger(value: number, name: string): void {
if (!Number.isSafeInteger(value) || value < 0) {
throw new Error(`${name} must be a non-negative safe integer`)
}
}
export function assertPtySourceIdentity(identity: PtySourceDeliveryIdentity): void {
if (!identity.id || !identity.ptyIncarnation || !identity.deliveryToken) {
throw new Error('PTY source delivery identity is incomplete')
}
assertPositiveSafeInteger(identity.providerGeneration, 'providerGeneration')
assertPositiveSafeInteger(identity.clientGeneration, 'clientGeneration')
assertPositiveSafeInteger(identity.ownerGeneration, 'ownerGeneration')
}
export function assertPtySourceSpan(span: PtySourceSpan): void {
assertPtySourceIdentity(span)
if (!span.spanId) {
throw new Error('spanId is required')
}
assertNonNegativeSafeInteger(span.sourceStartSu, 'sourceStartSu')
assertNonNegativeSafeInteger(span.sourceEndSu, 'sourceEndSu')
assertNonNegativeSafeInteger(span.displayStart, 'displayStart')
assertNonNegativeSafeInteger(span.displayEnd, 'displayEnd')
assertNonNegativeSafeInteger(span.transform.rawLengthSu, 'rawLengthSu')
if (span.sourceEndSu < span.sourceStartSu || span.displayEnd < span.displayStart) {
throw new Error('PTY source span ranges must be ordered')
}
if (span.sourceEndSu - span.sourceStartSu !== span.transform.rawLengthSu) {
throw new Error('PTY source span raw length does not match its source range')
}
if (!span.transform.transformed && span.data.length !== span.transform.rawLengthSu) {
throw new Error('Untransformed PTY source span length is invalid')
}
if (
span.splittable !== undefined &&
span.indivisible !== undefined &&
span.splittable === span.indivisible
) {
throw new Error('PTY source span split metadata is contradictory')
}
}
export function assertPtySourceAck(ack: PtySourceCreditAck): void {
if (!ack.id || !ack.deliveryToken) {
throw new Error('PTY source ACK identity is incomplete')
}
assertPositiveSafeInteger(ack.clientGeneration, 'clientGeneration')
assertPositiveSafeInteger(ack.ownerGeneration, 'ownerGeneration')
assertNonNegativeSafeInteger(ack.creditedEndSu, 'creditedEndSu')
}