import type { Config } from "../config.js"; import { type DeliveryLog } from "../delivery-log.js"; import { type Secrets } from "../env.js"; import type { Events } from "../events.js"; import { type HealthCache } from "../health.js"; import { type JobStore } from "../jobs.js"; import type { Logger } from "../logger.js"; import type { Paths } from "../paths.js"; import type { ReadinessCache } from "../readiness.js"; import type { SkillRegistry } from "../registry.js"; import type { ScheduleStatus } from "../scheduler.js"; import { type ServerState } from "../server.js"; import { type CommandHandler } from "./commands.js"; import type { ManagedSkills } from "./managed.js"; import { type CommandType, type LinkStatus } from "./protocol.js"; export interface CloudLinkDeps { paths: Paths; /** The live config (`ConfigRef.current`); `cloud.*` is read on every iteration. */ config: Config; secrets: () => Secrets; fileSecrets: () => Secrets; events: Events; logger: Logger; registry: SkillRegistry; store: JobStore; deliveryLog?: DeliveryLog; schedules?: () => ScheduleStatus[]; health?: HealthCache; readiness?: ReadinessCache; serverState: () => ServerState | undefined; /** The local server's base URL for hosted-ingress deliveries. */ localBaseUrl: () => string | undefined; env?: NodeJS.ProcessEnv; fetchImpl?: typeof fetch; /** Control command handlers (skill.run, job.answer, config.patch, …), when this version provides them. */ control?: Partial>; /** Cloud-managed skills: reconciled with the cloud's assignments on connect and at every snapshot, reported in the snapshot. */ managed?: ManagedSkills; /** The queue's numbers for the status line. */ queueStats?: () => { running: number; queued: number; }; runningJobs?: () => string[]; /** For tests: shorter waits. */ timing?: Partial; } export interface LinkTiming { disabledPollMs: number; syncTimeoutMs: number; backoffMinMs: number; backoffMaxMs: number; revokedRetryMs: number; upgradeRetryMs: number; stopSyncTimeoutMs: number; /** The shortest gap between two health reports sent early because a login changed (a burst of fallbacks sends one). */ healthSoonMs: number; } export interface LinkStatusView extends LinkStatus { enabled: boolean; url: string; machine_id: string | null; last_sync_at: string | null; last_error: string | null; connected_since: string | null; syncs: number; ingress_urls: Record; events_seq: number; } export declare class CloudLink { private readonly deps; private state; private reason?; private running; private readonly outbox; private readonly commandLedger; private readonly ingressLedger; private readonly dispatcher; private readonly timing; private unsubscribe?; private wakeUp?; private inflight?; private failures; private maxBatch; /** The events of the request in flight, to know which one to give up on when even one alone is too large. */ private lastBatch; /** Commands received in the last response, acknowledged in the next request. */ private receivedCommands; /** Per job, the last `job.progress` uploaded, to send at most one progress line per job every few seconds. */ private readonly progressSent; /** Actions waiting for the cloud to acknowledge a command's result. */ private readonly afterAcks; /** Jobs whose output streams to the cloud (`job.watch`). */ private readonly watches; private watchTimer?; private transient; private hints; private lastSnapshotAt; private lastHealthAt; /** A login stopped (or started) working: send a fresh health report at the next sync, not at the next interval. */ private healthDue; private lastSyncAt; private lastError; private connectedSince; private syncs; private needsStartEvent; private loopDone?; constructor(deps: CloudLinkDeps); status(): LinkStatusView; start(): void; /** Stops the loop; when connected, one last sync carries `link.stopped` and whatever is pending (bounded by `stopSyncTimeoutMs`). */ stop(reason?: string): Promise; /** Ends the current sleep or idle long-poll: something new is pending. */ wake(): void; private inflightIdle; private afterAck; private runAfterAck; /** `PUT /api/agent/artifacts//` in chunks with `Content-Range` and the whole file's sha256 on each. */ private upload; private watchStart; private watchStop; /** Reads what the watched jobs wrote since last time and queues it as `job.output` (complete lines while the job runs). */ private pumpWatches; private pushTransient; private env; private policy; private uploadPayloads; private uploadArtifacts; private setState; private snapshot; /** Progress lines of one job are coalesced: a change of state always travels, the same state at most every 5 s. */ private coalesced; /** Redacts and spools one bus event, when the link is active. */ private spool; private jobForUpload; private prepare; private sleep; private backoffMs; private loop; private handleFailure; /** One request to the cloud; returns how long to wait before the next. */ private sync; /** Reconciles the cloud-managed skills in the background; when something changed, the next sync carries a snapshot so the dashboard sees it now. */ private pull; private queueStats; private runningJobs; }