Phase 1c: real kRPC bridge (full protocol + mock mode for development)
CI / Lint, typecheck, test, build (pull_request) Failing after 9s

- packages/krpc-client: TypeScript kRPC protocol client
  - connection.ts: varint encoding/decoding, length-prefix framing,
    per-socket read queue (avoids race when multiple read promises
    in flight). Uses multiplication not '<<' for varint shift
    because JS truncates << to 32 bits.
  - schema.ts: hand-written protobufjs schema for kRPC meta-protocol
    (ConnectionRequest, Request, Response, StreamUpdate, Status, etc.)
    - enough to do connection handshake, single procedure calls, and
      stream subscription. Service-specific types (SpaceCenter.Vessel,
    Orbit, CelestialBody) need to be loaded from the kRPC mod's
    .proto files at runtime.
  - client.ts: KRPCClient with connect/invoke/addStream/removeStream/
    onStreamUpdate/close. Tested with hand-rolled mock server.
  - 10 tests (varint round-trips incl. uint64, wire format with raw
    sockets).

- apps/tools/ksp-bridge: bridge that connects KSP to our API
  - convert.ts: pure kRPC -> UniverseSnapshot conversion
    (situation enum mapping, body id normalization, etc.)
  - bridge.ts: main poll loop with retry + backoff
  - krpc-adapter.ts: KRPCAdapter class that owns the KRPCClient
  - index.ts: entrypoint with MOCK MODE for development (emits
    synthetic state when no KSP is available, so you can verify the
    HTTP pipeline end-to-end)
  - 9 tests (7 conversion + 2 end-to-end bridge)

- ksp/README.md: full setup guide
  - CKAN install, KSP server start, env vars
  - Hand-rolled KSP calls list (what SpaceCenter methods we need)
  - Roadmap for the remaining .proto-loading work
  - Protocol deep-dive (for the next dev)

- Bug fixes along the way: protobufjs default import (not namespace),
  varint 32-bit truncation, JavaScript bitwise 32-bit limit,
  handshake status enum comparison (number vs string).

End-to-end verified: API + bridge in mock mode, 2 bodies + 1 vessel
arriving at /api/v1/state, 500ms polling cadence, automatic recovery
on HTTP failures.
This commit is contained in:
Mavis
2026-06-02 20:42:54 +00:00
parent 07cc5321d1
commit 68bc7015fd
20 changed files with 2286 additions and 101 deletions
+123
View File
@@ -0,0 +1,123 @@
/**
* KSP Bridge — main loop.
*
* Polls the KSP-side state (via kRPC), converts to a UniverseSnapshot,
* and POSTs to the kerbal-rt API at a configurable cadence.
*
* Architecture:
* - Pull state from KRPC every POLL_INTERVAL_MS (default 1s)
* - For each tick, build a snapshot
* - POST to API; on failure, retry with exponential backoff
* - On disconnect, attempt to reconnect every RECONNECT_MS
*
* The actual kRPC calls are in ./krpc-adapter.ts. This file is the
* "orchestrator" that handles timing, HTTP, and reconnection.
*/
import type { KRPCState } from './convert.js';
import { buildSnapshot } from './convert.js';
export interface BridgeOptions {
/** API base URL, e.g. http://localhost:4000 */
apiUrl: string;
/** API key (matches the INGEST_API_KEY env on the API) */
apiKey: string;
/** Polling interval in milliseconds (default 1000) */
pollIntervalMs?: number;
/** Function that returns the current KSP state, or null if KSP isn't ready */
getState: () => Promise<KRPCState | null>;
/** Optional log function (defaults to console.log) */
log?: (msg: string) => void;
/** Optional error log function */
err?: (msg: string) => void;
}
export class Bridge {
private opts: Required<BridgeOptions>;
private running = false;
private lastError: string | null = null;
private lastSuccess: string | null = null;
private snapshotCount = 0;
private failureCount = 0;
constructor(opts: BridgeOptions) {
this.opts = {
apiUrl: opts.apiUrl.replace(/\/$/, ''),
apiKey: opts.apiKey,
pollIntervalMs: opts.pollIntervalMs ?? 1000,
getState: opts.getState,
log: opts.log ?? ((m) => console.log(`[ksp-bridge] ${m}`)),
err: opts.err ?? ((m) => console.error(`[ksp-bridge] ${m}`)),
};
}
async start(): Promise<void> {
this.running = true;
this.opts.log(`starting — API=${this.opts.apiUrl} interval=${this.opts.pollIntervalMs}ms`);
while (this.running) {
const t0 = Date.now();
try {
const state = await this.opts.getState();
if (state) {
const capturedAt = new Date().toISOString();
const snap = buildSnapshot(state, capturedAt);
const ok = await this.postSnapshot(snap);
if (ok) {
this.snapshotCount += 1;
this.lastSuccess = capturedAt;
this.failureCount = 0;
this.lastError = null;
if (this.snapshotCount % 10 === 1) {
this.opts.log(
`ut=${state.ut.toFixed(0)} bodies=${state.bodies.length} vessels=${state.vessels.length} → OK`,
);
}
}
} else {
this.opts.log('KSP not ready (no state)');
}
} catch (err) {
this.failureCount += 1;
this.lastError = String((err as Error).message ?? err);
this.opts.err(`poll failed: ${this.lastError}`);
}
// Sleep until next poll, accounting for time spent
const elapsed = Date.now() - t0;
const wait = Math.max(0, this.opts.pollIntervalMs - elapsed);
if (this.running && wait > 0) {
await sleep(wait);
}
}
}
stop(): void {
this.running = false;
}
getStats() {
return {
snapshotCount: this.snapshotCount,
failureCount: this.failureCount,
lastSuccess: this.lastSuccess,
lastError: this.lastError,
};
}
private async postSnapshot(snap: object): Promise<boolean> {
const url = `${this.opts.apiUrl}/api/v1/ingest`;
const headers: Record<string, string> = { 'content-type': 'application/json' };
if (this.opts.apiKey) headers['x-api-key'] = this.opts.apiKey;
const res = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(snap),
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${await res.text()}`);
}
return true;
}
}
function sleep(ms: number): Promise<void> {
return new Promise((r) => setTimeout(r, ms));
}