import { describe, expect, spyOn, test, vi } from "bun:test"; import * as crypto from "node:crypto"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { logger } from "@sayknow-cli/utils"; import { Marked } from "marked"; import { Settings } from "../src/config/settings"; import { tokenFingerprint } from "../src/sdk/bus/config"; import { markdownToTelegramHtml, splitTelegramHtml, TELEGRAM_MESSAGE_LIMIT, TELEGRAM_PARSE_MODE, } from "../src/sdk/bus/html-format"; import { acquireDaemonTransitionLock, classifyNotificationEndpoint, type NotificationEndpointFileIdentity, releaseDaemonTransitionLock, } from "../src/sdk/bus/notification-service"; import { RateLimitPool } from "../src/sdk/bus/rate-limit-pool"; import { deliverRichWithFallback } from "../src/sdk/bus/rich-render"; import { acquireDaemonOwnership, type BotApi, BTW_QUESTION_MAX_UNICODE_SCALARS, BTW_QUESTION_MAX_UTF8_BYTES, confirmTelegramDaemonSpawn, DAEMON_GENERATION, DAEMON_VERSION, type DaemonState, daemonPaths, endpointAuthorityDigest, ensureTelegramDaemonRunning, ensureTelegramDaemonRunningDetailed, hasSafeDaemonStateShape, isCurrentCompatibleOwner, isFreshLiveOwner, LEGACY_TOOL_ACTIVITY_CAPABILITY, readAttestedLegacyDaemonOwner, readDaemonState, readOwnerFreshnessSnapshot, registerNotificationRoot, releaseDaemonOwnership, renewDaemonHeartbeat, renewOwnerHeartbeatSidecar, retireProvisionalDaemonOwnership, SERVING_EPOCH, spawnTelegramDaemonOwner, TelegramBotTransport, type TelegramDaemonFs, type TelegramDaemonOptions, TelegramEventDispatchState, TelegramNotificationDaemon, TelegramUpdatePoller, TOOL_ACTIVITY_CAPABILITY, unregisterNotificationRoot, waitForTelegramDaemonReady, withNotificationRootRegistryFence, } from "../src/sdk/bus/telegram-daemon"; import { ownerPidFromOwnerId, runDaemonInternal, runDaemonSmoke } from "../src/sdk/bus/telegram-daemon-cli"; import { NOTIFICATION_PROTOCOL_VERSION } from "../src/sdk/bus/telegram-daemon-contract"; import { TelegramDaemonController } from "../src/sdk/bus/telegram-daemon-control"; import type { InboundAttachment } from "../src/sdk/bus/threaded-inbound"; const THREADED_FALLBACK_NOTICE = "Flat Telegram private chat supports outbound notifications and inline ask buttons only. Enable Threaded Mode in @BotFather > Bot Settings > Threads Settings for free-text replies and session commands."; type AttachmentDownload = { bytes: Buffer } | { failure: "download_failed" | "too_large" }; interface AttachmentTestAccess { downloadTelegramFile(filePath: string, maxBytes?: number): Promise; resolveInboundAttachment( attachment: InboundAttachment, sessionId: string, ): Promise<{ images: Array<{ data: string }>; fileNotes: string[] }>; } function attachmentAccess(daemon: TelegramNotificationDaemon): AttachmentTestAccess { return daemon as unknown as AttachmentTestAccess; } test("steady ownership heartbeat advances only the owner-tagged sidecar", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 1; const ownership = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", now: () => now, }); expect(ownership.acquired).toBe(true); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, }), ).toBe(true); now = 2; expect( await renewOwnerHeartbeatSidecar({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, }), ).toBe(true); expect((await readDaemonState(s))?.heartbeatAt).toBe(1); expect((await readOwnerFreshnessSnapshot({ settings: s })).effectiveHeartbeatAt).toBe(2); }); test("stale-tag sidecars are inert and a stale writer cannot overwrite a successor heartbeat", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "old", }); fs.writeFileSync( paths.heartbeat, JSON.stringify({ pid: process.pid, incarnation: process.platform === "darwin" ? undefined : "stale", ownerId: "other", acquisitionId: "other", heartbeatAt: Date.now() + 60_000, }), ); const snapshot = await readOwnerFreshnessSnapshot({ settings: s }); expect(snapshot.effectiveHeartbeatAt).toBe((await readDaemonState(s))?.heartbeatAt); expect( await renewOwnerHeartbeatSidecar({ settings: s, ownerId: "other", acquisitionId: "other", pid: process.pid }), ).toBe(false); }); test("a stale rename landing after a steal stays inert and the successor self-heals", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 1; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "a", now: () => now, }); await renewDaemonHeartbeat({ settings: s, ownerId: "a", acquisitionId: "a", pid: process.pid, now: () => now }); // Pause owner A's renewal at the exact post-final-lock-read pre-rename window. let releaseRename: (() => void) | undefined; const renamePaused = new Promise(resolve => { releaseRename = resolve; }); let renameStarted: (() => void) | undefined; const started = new Promise(resolve => { renameStarted = resolve; }); const pausedFs: TelegramDaemonFs = { ...(fs.promises as unknown as TelegramDaemonFs), rename: async (from, to) => { renameStarted?.(); await renamePaused; return await fs.promises.rename(from as string, to as string); }, }; now = 10; const inFlight = renewOwnerHeartbeatSidecar({ settings: s, ownerId: "a", acquisitionId: "a", pid: process.pid, now: () => now, fs: pausedFs, }); await started; // Successor B steals on disk while A is paused past its final lock read. const bState = { ...(await readDaemonState(s))!, ownerId: "b", acquisitionId: "b", startedAt: 20, heartbeatAt: 20, }; fs.writeFileSync(paths.state, JSON.stringify(bState)); fs.writeFileSync( paths.lock, JSON.stringify({ pid: bState.pid, incarnation: bState.incarnation, ownerId: "b", acquisitionId: "b", startedAt: 20, }), ); // Resume A: its rename LANDS on the shared pathname. releaseRename?.(); await inFlight; // A's landed sidecar is tag-inert for B: freshness falls back to B's state floor. const snapshot = await readOwnerFreshnessSnapshot({ settings: s }); expect(snapshot.ownerTag?.ownerId).toBe("b"); expect(snapshot.effectiveHeartbeatAt).toBe(20); // B's own next renewal restores a matching fresh sidecar. now = 30; expect( await renewOwnerHeartbeatSidecar({ settings: s, ownerId: "b", acquisitionId: "b", pid: process.pid, now: () => now, }), ).toBe(true); expect((await readOwnerFreshnessSnapshot({ settings: s })).effectiveHeartbeatAt).toBe(30); }); test("modern owners without a matching lock are never fresh or current", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", }); const state = await readDaemonState(s); fs.unlinkSync(paths.lock); const missingLock = await readOwnerFreshnessSnapshot({ settings: s }); expect(missingLock.effectiveHeartbeatAt).toBeUndefined(); expect( isCurrentCompatibleOwner({ state: missingLock.state, now: Date.now(), tokenFingerprint: "fp", chatId: "42", pidAlive: () => true, effectiveHeartbeatAt: missingLock.effectiveHeartbeatAt, }), ).toBe(false); fs.writeFileSync( paths.lock, JSON.stringify({ pid: process.pid, incarnation: state?.incarnation, ownerId: "other", acquisitionId: "other", startedAt: 1, }), ); const mismatchedLock = await readOwnerFreshnessSnapshot({ settings: s }); expect(mismatchedLock.effectiveHeartbeatAt).toBeUndefined(); }); test("sidecar fence skips publication after the ownership lock changes", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", }); const originalReadFile = fs.promises.readFile.bind(fs.promises); let lockReads = 0; const fencedFs: TelegramDaemonFs = { ...(fs.promises as unknown as TelegramDaemonFs), readFile: async (file, encoding) => { if (file === paths.lock && ++lockReads === 2) return JSON.stringify({ pid: process.pid, incarnation: "changed", ownerId: "next", acquisitionId: "next", startedAt: 1, }); return await originalReadFile(file, encoding); }, }; expect( await renewOwnerHeartbeatSidecar({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, fs: fencedFs, }), ).toBe(false); expect(fs.existsSync(paths.heartbeat)).toBe(false); }); test("failed initial sidecar proof rolls ready publication forward to retired and unlinks the ownership lock", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); // Provisional launcher reservation; the child self-binds its real PID on its // first heartbeat renewal. fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "owner", acquisitionId: "owner", tokenFingerprint: "fp", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, generation: DAEMON_GENERATION, ownershipPhase: "provisional", servingEpoch: SERVING_EPOCH, }), ); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "owner", acquisitionId: "owner", startedAt: 100 }), ); const originalWriteFile = fs.promises.writeFile.bind(fs.promises); const fencedFs: TelegramDaemonFs = { ...(fs.promises as unknown as TelegramDaemonFs), // Transition-lock acquisition and exact lock unlink require the // identity-bound capabilities the plain fs.promises surface lacks. ...transitionFsCapabilities(), writeFile: (async (file: string, data: string, options?: { mode?: number }) => { if (String(file).startsWith(paths.heartbeat)) throw new Error("simulated sidecar write failure"); return await originalWriteFile(file, data, options); }) as TelegramDaemonFs["writeFile"], }; expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 222, pidIncarnation: pid => (pid === 111 ? "linux:111" : "linux:222"), fs: fencedFs, now: () => 1_000, }), ).toBe(false); // Failure occurs while the rebound state is still provisional, so no waiter // can observe ready before the rollback advances it to retired and exact-unlinks // the ownership lock. const rolled = JSON.parse(fs.readFileSync(paths.state, "utf8")); expect(rolled).toMatchObject({ pid: 222, launcherPid: 111, ownershipPhase: "retired", stoppedAt: 1_000, }); expect(fs.existsSync(paths.lock)).toBe(false); }); test("initial readiness stays false until the heartbeat sidecar rename is durable", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "owner", acquisitionId: "owner", tokenFingerprint: "fp", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, generation: DAEMON_GENERATION, ownershipPhase: "provisional", servingEpoch: SERVING_EPOCH, }), ); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "owner", acquisitionId: "owner", startedAt: 100 }), ); const originalRename = fs.promises.rename.bind(fs.promises); let readyDuringSidecarRename: boolean | undefined; const fencedFs: TelegramDaemonFs = { ...(fs.promises as unknown as TelegramDaemonFs), ...transitionFsCapabilities(), rename: async (from, to) => { await originalRename(from, to); if (to === paths.heartbeat) { readyDuringSidecarRename = await waitForTelegramDaemonReady({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 222, tokenFingerprint: "fp", chatId: "42", fs: fencedFs, now: () => 1_000, pidAlive: pid => pid === 222, pidIncarnation: pid => (pid === 111 ? "linux:111" : "linux:222"), timeoutMs: 0, }); } }, }; expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 222, pidIncarnation: pid => (pid === 111 ? "linux:111" : "linux:222"), fs: fencedFs, now: () => 1_000, }), ).toBe(true); expect(readyDuringSidecarRename).toBe(false); expect( await waitForTelegramDaemonReady({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 222, tokenFingerprint: "fp", chatId: "42", fs: fencedFs, now: () => 1_000, pidAlive: pid => pid === 222, pidIncarnation: () => "linux:222", timeoutMs: 0, }), ).toBe(true); }); test("post-TTL owner with a fresh sidecar stays accepted while a stalled sidecar goes stale", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 1_000; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", now: () => now, }); await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, }); // Advance far past the TTL: the frozen state.heartbeatAt alone is stale. now += 3 * 20_000; await renewOwnerHeartbeatSidecar({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, }); const state = await readDaemonState(s); const snapshot = await readOwnerFreshnessSnapshot({ settings: s }); expect(now - (state?.heartbeatAt ?? 0)).toBeGreaterThan(20_000); expect(snapshot.effectiveHeartbeatAt).toBe(now); expect( isFreshLiveOwner({ state, now, tokenFingerprint: "fp", chatId: "42", pidAlive: () => true, effectiveHeartbeatAt: snapshot.effectiveHeartbeatAt, }), ).toBe(true); // Without the sidecar-derived freshness the same owner is stale — the // snapshot is load-bearing for post-TTL acceptance. expect(isFreshLiveOwner({ state, now, tokenFingerprint: "fp", chatId: "42", pidAlive: () => true })).toBe(false); }); test("steady sidecar renewal creates no transition markers and calls no exactUnlink", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 1; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", now: () => now, }); await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, }); const leakArtifacts = () => fs .readdirSync(paths.dir) .filter( name => name.startsWith(".skc-delete-daemon-transition-") || name.startsWith(".skc-exact-unlink-placeholder-"), ); const before = leakArtifacts(); let exactUnlinks = 0; let stealTouches = 0; const spyFs: TelegramDaemonFs = { ...(fs.promises as unknown as TelegramDaemonFs), exactUnlink: async () => { exactUnlinks += 1; throw new Error("steady sidecar renewal must never call exactUnlink"); }, writeFile: async (file, data, opts) => { if (String(file).includes(path.basename(paths.steal))) stealTouches += 1; return await fs.promises.writeFile(file as string, data as string, opts as never); }, }; for (let tick = 0; tick < 25; tick++) { now += 5_000; expect( await renewOwnerHeartbeatSidecar({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, fs: spyFs, }), ).toBe(true); } expect(exactUnlinks).toBe(0); expect(stealTouches).toBe(0); expect(leakArtifacts()).toEqual(before); }); test("corrupt or directory sidecar degrades to state-floor freshness and never throws", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const now = 1; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", now: () => now, }); await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: process.pid, now: () => now, }); const floor = (await readDaemonState(s))?.heartbeatAt; for (const corrupt of ["{truncated", "", "[1,2]", "null", '{"heartbeatAt":NaN}']) { fs.rmSync(paths.heartbeat, { force: true, recursive: true }); fs.writeFileSync(paths.heartbeat, corrupt); const snapshot = await readOwnerFreshnessSnapshot({ settings: s }); expect(snapshot.effectiveHeartbeatAt).toBe(floor); } fs.rmSync(paths.heartbeat, { force: true, recursive: true }); fs.mkdirSync(paths.heartbeat); const snapshot = await readOwnerFreshnessSnapshot({ settings: s }); expect(snapshot.effectiveHeartbeatAt).toBe(floor); fs.rmSync(paths.heartbeat, { force: true, recursive: true }); }); test("endpoint authority digest canonicalizes endpoint presentation and binds authenticated identity", () => { const canonical = endpointAuthorityDigest("ws://LOCALHOST:80/sdk?ignored=yes#ignored", "token"); expect(canonical).toBe(endpointAuthorityDigest("ws://localhost/sdk", "token")); expect(endpointAuthorityDigest("ws://localhost/sdk", "token", "native-connection-a")).not.toBe(canonical); expect(endpointAuthorityDigest("ws://localhost/sdk", "token", "native-connection-a")).toBe( endpointAuthorityDigest("ws://localhost/sdk", "token", "native-connection-a"), ); }); test("endpoint classification excludes lifecycle records and fails closed for PID-less and unreadable records", async () => { const agentDir = tempAgentDir(); const identity: NotificationEndpointFileIdentity = { dev: 1n, ino: 1n, size: 1n, mtimeNs: 1n, sha256: "test" }; const classify = async (name: string, value: string) => await classifyNotificationEndpoint( { readEndpointFile: async () => ({ bytes: Buffer.from(value), identity }) }, path.join(agentDir, name), pid => pid === 7, ); const dead = await classify("dead.json", JSON.stringify({ url: "ws://dead", token: "x", pid: 8 })); const pidless = await classify("pidless.json", JSON.stringify({ url: "ws://unknown", token: "x" })); expect(dead.kind === "endpoint" && dead.liveness).toBe("dead"); expect(pidless.kind === "endpoint" && pidless.liveness).toBe("unknown"); expect((await classify("broker.json", JSON.stringify({ url: "ws://broker", token: "x", pid: 8 }))).kind).toBe( "non-endpoint", ); expect( (await classify("owner.lifecycle.json", JSON.stringify({ pid: 8, incarnation: "linux:8", effectMarker: "x" }))) .kind, ).toBe("non-endpoint"); const unreadable = await classifyNotificationEndpoint( { readEndpointFile: async () => Promise.reject(new Error("mid-write")) }, path.join(agentDir, "broken.json"), () => false, ); expect(unreadable.kind).toBe("unreadable"); }); function tempAgentDir(): string { return fs.mkdtempSync(path.join(os.tmpdir(), "skc-telegram-daemon-test-")); } function exactTransitionFs(onExactUnlink?: (file: string) => void) { return { readFile: (file: string, encoding: "utf8") => fs.promises.readFile(file, encoding), writeFile: (file: string, data: string, opts?: Parameters[2]) => fs.promises.writeFile(file, data, opts), stat: async (file: string) => ({ mtimeMs: (await fs.promises.stat(file)).mtimeMs }), readEndpointFile: async (file: string) => { const bytes = await fs.promises.readFile(file); const stat = await fs.promises.lstat(file, { bigint: true }); return { bytes, identity: { dev: stat.dev, ino: stat.ino, size: stat.size, mtimeNs: stat.mtimeNs, sha256: crypto.createHash("sha256").update(bytes).digest("hex"), }, }; }, exactUnlink: async (file: string, identity: NotificationEndpointFileIdentity) => { onExactUnlink?.(file); const bytes = await fs.promises.readFile(file).catch(() => undefined); if (!bytes) return { ok: false, code: "missing" }; const stat = await fs.promises.lstat(file, { bigint: true }); const matches = stat.dev === identity.dev && stat.ino === identity.ino && stat.size === identity.size && stat.mtimeNs === identity.mtimeNs && crypto.createHash("sha256").update(bytes).digest("hex") === identity.sha256; if (!matches) return { ok: false, code: "identity_mismatch" }; await fs.promises.unlink(file); return { ok: true }; }, }; } function transitionFsCapabilities(): Pick { const transitionFs = exactTransitionFs(); return { readEndpointFile: transitionFs.readEndpointFile, exactUnlink: transitionFs.exactUnlink, }; } function settings(agentDir: string, botToken = "123456:secret-token"): Settings { // Isolate getAgentDir() to the temp dir so daemon persistence (aliases, // topics, lock/state/roots) never writes into the real global ~/.skc/agent. return setPrivateAgentDir( Settings.isolated({ "notifications.enabled": true, "notifications.telegram.botToken": botToken, "notifications.telegram.chatId": "42", "notifications.daemon.idleTimeoutMs": 20, }) as Settings, agentDir, ); } function setPrivateAgentDir(s: Settings, agentDir: string) { return new Proxy(s, { get(target, prop) { if (prop === "getAgentDir") return () => agentDir; const value = Reflect.get(target, prop, target); return typeof value === "function" ? value.bind(target) : value; }, }) as Settings; } function readyTelegramSpawnFixture({ settings, firstChildPid, now, onSpawn, onReady, }: { settings: Settings; firstChildPid: number; now?: () => number; onSpawn?: (pid: number, command: string, args: string[]) => void; onReady?: () => void; }) { let nextChildPid = firstChildPid; let pending: { ownerId: string; pid: number } | undefined; return { spawn: (command: string, args: string[]) => { const ownerId = args[args.indexOf("--owner-id") + 1]!; const pid = nextChildPid++; pending = { ownerId, pid }; onSpawn?.(pid, command, args); return { pid, unref() {} }; }, publishReady: async () => { if (!pending) throw new Error("Telegram child was not spawned"); expect( await renewDaemonHeartbeat({ settings, ownerId: pending.ownerId, acquisitionId: pending.ownerId, pid: pending.pid, pidIncarnation: () => "linux:100", now, }), ).toBe(true); onReady?.(); }, sleep: async () => { if (pending) { await renewDaemonHeartbeat({ settings, ownerId: pending.ownerId, acquisitionId: pending.ownerId, pid: pending.pid, pidIncarnation: () => "linux:100", now, }); onReady?.(); } }, }; } function topicStateFs(onTopicStateWrite: () => Promise): TelegramDaemonFs { return { mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: async (file, data, opts) => { if (file.includes("telegram-topics.json")) await onTopicStateWrite(); await fs.promises.writeFile(file, data, opts); }, rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: async (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), }; } class FakeWs extends EventTarget { static OPEN = 1; readyState = 1; setReadyState(readyState: number): void { this.readyState = readyState; } sent: string[] = []; constructor(public url = "") { super(); FakeWs.instances.push(this); } send(data: string) { this.sent.push(data); } close() { this.readyState = 3; this.dispatchEvent(new Event("close")); } emit(data: unknown) { this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(data) })); } static instances: FakeWs[] = []; } async function enableEphemeralTurns(daemon: TelegramNotificationDaemon, sessionId = "S"): Promise { const session = daemon.sessions.get(sessionId)!; if (!session.replayPending) session.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "hello", capabilities: ["ephemeral_turn_v1"] }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); } class FakeBotApi { createdTopicThreadIds: number[] = []; calls: Array<{ method: string; body: any; options?: { noRetry?: boolean; signal?: AbortSignal } }> = []; updates: any[] = []; activeGetUpdates = 0; maxConcurrentGetUpdates = 0; botUsername: string | undefined = undefined; async call(method: string, body: unknown, options?: { noRetry?: boolean; signal?: AbortSignal }): Promise { this.calls.push({ method, body, options }); if (method === "getUpdates") { this.activeGetUpdates++; this.maxConcurrentGetUpdates = Math.max(this.maxConcurrentGetUpdates, this.activeGetUpdates); await Promise.resolve(); this.activeGetUpdates--; const result = this.updates; this.updates = []; return { ok: true, result }; } if (method === "getMe") return { ok: true, result: this.botUsername ? { id: 1, username: this.botUsername } : { id: 1 } }; if (method === "getChat") return { ok: true, result: { id: (body as { chat_id?: unknown }).chat_id, type: "private" } }; if (method === "getFile") return { ok: true, result: { file_path: "docs/file_7.bin" } }; if (method === "createForumTopic") { const threadId = this.calls.length; this.createdTopicThreadIds.push(threadId); return { ok: true, result: { message_thread_id: threadId } }; } if (method === "sendMessage" || method === "sendRichMessage") return { ok: true, result: { message_id: this.calls.length } }; return { ok: true, result: true }; } } class SinglePollerBotApi extends FakeBotApi { inFlightOwner: string | undefined; conflicts: string[] = []; events: Array<{ type: "start" | "conflict" | "end"; owner: string }> = []; #release: (() => void) | undefined; override async call( method: string, body: unknown, options?: { noRetry?: boolean; signal?: AbortSignal }, ): Promise { if (method !== "getUpdates") return await super.call(method, body, options); const owner = (body as { owner: string }).owner; this.calls.push({ method, body, options }); if (this.inFlightOwner) { this.conflicts.push(owner); this.events.push({ type: "conflict", owner }); return { ok: false, error_code: 409, description: "Conflict: terminated by other getUpdates request" }; } this.inFlightOwner = owner; this.events.push({ type: "start", owner }); await new Promise(resolve => (this.#release = resolve)); this.events.push({ type: "end", owner }); this.inFlightOwner = undefined; this.#release = undefined; return { ok: true, result: [] }; } start(owner: string): void { void this.call("getUpdates", { owner }); } async stop(owner: string): Promise { if (this.inFlightOwner !== owner) throw new Error(`Expected ${owner} to be polling`); this.#release?.(); await Promise.resolve(); } } async function unavailableControlHarness(fsImpl?: TelegramDaemonFs) { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, ...(fsImpl ? { fs: fsImpl } : {}), }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const threadId = bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id as number; bot.calls = []; return { agentDir, bot, daemon, threadId }; } type TopicAuthorityState = { topics: Record< string, { topicId?: string; name?: string; nameOwner?: string; nameReconcilePending?: boolean; userNameUpdateId?: number; authorityState?: "active" | "delete_pending"; endpointDigest?: string; endpointGeneration?: number; replayGeneration?: number; replaySeq?: number; } >; closedEndpoints?: Record; }; async function readTopicAuthorityState(agentDir: string): Promise { return JSON.parse( await fs.promises.readFile(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8"), ) as TopicAuthorityState; } function forumTopicEditedUpdate( updateId: number, threadId: number, name: string, { chat = { id: 42 }, from = { id: 42, is_bot: false }, }: { chat?: { id: number | string }; from?: { id: number; is_bot?: boolean } | null; } = {}, ) { return { update_id: updateId, message: { chat, ...(from === null ? {} : { from }), message_thread_id: threadId, forum_topic_edited: { name }, }, }; } async function identityTopicHarness({ agentDir = tempAgentDir(), bot = new FakeBotApi(), fs: fsImpl, ownerId = "owner", title = "Generated title", }: { agentDir?: string; bot?: FakeBotApi; fs?: TelegramDaemonFs; ownerId?: string; title?: string; } = {}) { const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId, botToken: "tok", chatId: "42", botApi: bot, fs: fsImpl, }); const session = { sessionId: "S", token: "tok", endpointKey: "ws://s", endpointDigest: "endpoint-digest", hostGeneration: 1, ws: { readyState: 1, send() {} }, pending: new Map(), }; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title, }); return { agentDir, bot, daemon, session, threadId: bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id as number, }; } class ReplayRenameBotApi extends FakeBotApi { threadId: number | undefined; getChatFailure: (() => unknown) | undefined; override async call(method: string, body: unknown): Promise { if (method === "getUpdates") { this.calls.push({ method, body }); const offset = (body as { offset?: number }).offset ?? 0; if (this.threadId !== undefined && offset <= 50) { return { ok: true, result: [ forumTopicEditedUpdate(50, this.threadId, "First focus"), forumTopicEditedUpdate(51, this.threadId, "Later focus"), ], }; } return { ok: true, result: [] }; } if (method === "getChat" && this.getChatFailure) { const failure = this.getChatFailure; this.getChatFailure = undefined; this.calls.push({ method, body }); return failure(); } return super.call(method, body); } } class FailingCallbackAckBotApi extends FakeBotApi { override async call(method: string, body: unknown): Promise { if (method === "answerCallbackQuery") { this.calls.push({ method, body }); throw new Error("callback ack failed"); } return super.call(method, body); } } describe("telegram daemon", () => { test("N concurrent ensureTelegramDaemonRunning creates exactly one owner", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let spawns = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 211, onSpawn: () => spawns++, }); const results = await Promise.all( Array.from({ length: 8 }, (_, i) => ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, `cwd-${i}`), sessionId: `s${i}` }, { spawn: child.spawn, sleep: child.publishReady, pidAlive: pid => pid === 111 || pid === 211, pidIncarnation: () => "linux:100", pid: 111, }, ), ), ); expect(results.filter(r => r === "owner_spawned")).toHaveLength(1); expect(results.filter(r => r === "attached")).toHaveLength(7); expect(spawns).toBe(1); }); test("ensureTelegramDaemonRunning ignores blank Telegram credentials when another adapter enables notifications", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir( Settings.isolated({ "notifications.enabled": true, "notifications.telegram.botToken": " ", "notifications.telegram.chatId": "\t", "notifications.discord.botToken": "discord-token", "notifications.discord.applicationId": "discord-app", "notifications.discord.guildId": "discord-guild", "notifications.discord.parentChannelId": "discord-parent", }) as Settings, agentDir, ); let spawns = 0; const result = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "cwd"), sessionId: "s1" }, { spawn: () => { spawns++; return { unref() {} }; }, pidAlive: () => true, pid: 111, }, ); expect(result).toBe("disabled"); expect(spawns).toBe(0); expect(fs.existsSync(daemonPaths(agentDir).roots)).toBe(false); }); test("concurrent root registrations persist every root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await Promise.all( Array.from({ length: 12 }, (_, i) => registerNotificationRoot({ settings: s, cwd: path.join(agentDir, `cwd-${i}`), sessionId: `s${i}` }), ), ); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[]; sessions: Record; }; expect(registry.roots).toHaveLength(12); expect(Object.keys(registry.sessions)).toHaveLength(12); for (let i = 0; i < 12; i++) { expect(registry.sessions[`s${i}`]).toBe(path.join(agentDir, `cwd-${i}`, ".skc", "state")); } }); test("unregistering a session removes only its unreferenced root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const sharedCwd = path.join(agentDir, "shared"); const otherCwd = path.join(agentDir, "other"); const sharedA = await registerNotificationRoot({ settings: s, cwd: sharedCwd, sessionId: "shared-a" }); const sharedB = await registerNotificationRoot({ settings: s, cwd: sharedCwd, sessionId: "shared-b" }); await registerNotificationRoot({ settings: s, cwd: otherCwd, sessionId: "other" }); expect( await unregisterNotificationRoot({ settings: s, cwd: sharedCwd, sessionId: "shared-a", registrationToken: sharedA.token, }), ).toMatchObject({ remainingRoots: 2 }); expect( await unregisterNotificationRoot({ settings: s, cwd: sharedCwd, sessionId: "shared-b", registrationToken: sharedB.token, }), ).toMatchObject({ remainingRoots: 1 }); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[]; sessions: Record; }; expect(registry.roots).toEqual([path.join(otherCwd, ".skc", "state")]); expect(registry.sessions).toEqual({ other: path.join(otherCwd, ".skc", "state") }); }); test("stale unregister does not delete a session re-registered to another root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const first = path.join(agentDir, "first"); const second = path.join(agentDir, "second"); await registerNotificationRoot({ settings: s, cwd: first, sessionId: "session" }); await registerNotificationRoot({ settings: s, cwd: second, sessionId: "session" }); await unregisterNotificationRoot({ settings: s, cwd: first, sessionId: "session" }); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[]; sessions: Record; }; const secondRoot = path.join(second, ".skc", "state"); expect(registry.sessions).toEqual({ session: secondRoot }); expect(registry.roots).toContain(secondRoot); }); test("re-registering a session prunes its unreferenced managed root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const first = path.join(agentDir, "first"); const second = path.join(agentDir, "second"); const firstRoot = path.join(first, ".skc", "state"); const secondRoot = path.join(second, ".skc", "state"); await registerNotificationRoot({ settings: s, cwd: first, sessionId: "session" }); const replacement = await registerNotificationRoot({ settings: s, cwd: second, sessionId: "session" }); let registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { version: number; roots: string[]; managedRoots: string[]; sessions: Record; registrationTokens: Record; }; expect(registry).toEqual({ version: 1, roots: [secondRoot], managedRoots: [secondRoot], sessions: { session: secondRoot }, registrationTokens: { session: expect.any(String) }, }); expect(registry.roots).not.toContain(firstRoot); await unregisterNotificationRoot({ settings: s, cwd: second, sessionId: "session", registrationToken: replacement.token, }); registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toEqual({ version: 1, roots: [], managedRoots: [], sessions: {}, registrationTokens: {} }); }); test("stale-token unregister is fenced by a live replacement registration to the same root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "session"); // Same sessionId + same cwd re-registration records an identical root, so // the root match alone cannot distinguish the replacement: the token must. const first = await registerNotificationRoot({ settings: s, cwd, sessionId: "session" }); const second = await registerNotificationRoot({ settings: s, cwd, sessionId: "session" }); expect(second.token).not.toBe(first.token); await unregisterNotificationRoot({ settings: s, cwd, sessionId: "session", registrationToken: first.token }); const root = path.join(cwd, ".skc", "state"); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ roots: [root], managedRoots: [root], sessions: { session: root }, registrationTokens: { session: second.token }, }); }); test("tokenless cleanup is fenced when the live registration has an ownership token", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "session"); const registration = await registerNotificationRoot({ settings: s, cwd, sessionId: "session" }); await unregisterNotificationRoot({ settings: s, cwd, sessionId: "session" }); const root = path.join(cwd, ".skc", "state"); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ roots: [root], managedRoots: [root], sessions: { session: root }, registrationTokens: { session: registration.token }, }); }); test("matching-token unregister removes the replacement registration", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "session"); await registerNotificationRoot({ settings: s, cwd, sessionId: "session" }); const second = await registerNotificationRoot({ settings: s, cwd, sessionId: "session" }); await unregisterNotificationRoot({ settings: s, cwd, sessionId: "session", registrationToken: second.token }); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toEqual({ version: 1, roots: [], managedRoots: [], sessions: {}, registrationTokens: {} }); }); test("legacy token-less registrations keep root-match unregister behavior", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "session"); const root = path.join(cwd, ".skc", "state"); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); // Pre-token-fencing registry shape: no registrationTokens map at all. fs.writeFileSync( paths.roots, JSON.stringify({ version: 1, roots: [root], managedRoots: [root], sessions: { session: root } }), ); await unregisterNotificationRoot({ settings: s, cwd, sessionId: "session" }); const registry = JSON.parse(fs.readFileSync(paths.roots, "utf8")); expect(registry).toEqual({ version: 1, roots: [], managedRoots: [], sessions: {}, registrationTokens: {} }); }); test("reload rollback is fenced by a replacement registration minted mid-reload", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "session"); let now = 1_000; writeLiveOwner(agentDir, { servingEpoch: undefined, heartbeatAt: now }); let replacedToken: string | undefined; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd, sessionId: "session" }, { now: () => now, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: () => undefined, sleep: async () => { now += 8_000; // A newer ensure re-registers the same session to the same root // while the failed reload is still unwinding; its token must fence // the stale rollback from deleting the live registration. replacedToken = (await registerNotificationRoot({ settings: s, cwd, sessionId: "session" })).token; }, waitStepMs: 8_000, }, ), ).rejects.toThrow("Unable to replace stale Telegram daemon"); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ sessions: { session: path.join(cwd, ".skc", "state") }, registrationTokens: { session: replacedToken }, }); }); test("re-registering one session preserves a managed root referenced by another session", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const shared = path.join(agentDir, "shared"); const replacement = path.join(agentDir, "replacement"); const sharedRoot = path.join(shared, ".skc", "state"); const replacementRoot = path.join(replacement, ".skc", "state"); await registerNotificationRoot({ settings: s, cwd: shared, sessionId: "moving" }); await registerNotificationRoot({ settings: s, cwd: shared, sessionId: "staying" }); await registerNotificationRoot({ settings: s, cwd: replacement, sessionId: "moving" }); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { version: number; roots: string[]; managedRoots: string[]; sessions: Record; }; expect(registry.roots).toEqual([replacementRoot, sharedRoot].sort()); expect(registry.managedRoots).toEqual([replacementRoot, sharedRoot].sort()); expect(registry.sessions).toEqual({ moving: replacementRoot, staying: sharedRoot }); }); test("legacy unmanaged roots survive register and unregister", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const legacyRoot = path.join(agentDir, "legacy", ".skc", "state"); fs.mkdirSync(daemonPaths(agentDir).dir, { recursive: true }); fs.writeFileSync(daemonPaths(agentDir).roots, JSON.stringify({ version: 1, roots: [legacyRoot], sessions: {} })); await registerNotificationRoot({ settings: s, cwd: path.join(agentDir, "legacy"), sessionId: "legacy-session" }); await unregisterNotificationRoot({ settings: s, cwd: path.join(agentDir, "legacy"), sessionId: "legacy-session", }); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry.roots).toEqual([legacyRoot]); expect(registry.managedRoots).toEqual([]); }); test("reload rollback preserves legacy unmanaged root provenance through later unregister", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const legacyCwd = path.join(agentDir, "legacy"); const replacementCwd = path.join(agentDir, "replacement"); const legacyRoot = path.join(legacyCwd, ".skc", "state"); fs.mkdirSync(daemonPaths(agentDir).dir, { recursive: true }); fs.writeFileSync( daemonPaths(agentDir).roots, JSON.stringify({ version: 1, roots: [legacyRoot], managedRoots: [], sessions: { session: legacyRoot } }), ); let now = 1_000; writeLiveOwner(agentDir, { heartbeatAt: now, servingEpoch: undefined }); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: replacementCwd, sessionId: "session" }, { now: () => now, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: () => undefined, sleep: async () => { now += 8_000; }, waitStepMs: 8_000, }, ), ).rejects.toThrow("Unable to replace stale Telegram daemon"); let registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ roots: [legacyRoot], managedRoots: [], sessions: { session: legacyRoot } }); await unregisterNotificationRoot({ settings: s, cwd: legacyCwd, sessionId: "session" }); registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ roots: [legacyRoot], managedRoots: [], sessions: {} }); }); test("fake Bot API observes one getUpdates loop", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); await daemon.pollOnce(); await daemon.pollOnce(); expect(bot.maxConcurrentGetUpdates).toBe(1); }); test("TelegramUpdatePoller isolates handler failures and backs off before retrying an update", async () => { const calls: Array<{ method: string; body: any }> = []; const processed: string[] = []; const sleeps: number[] = []; const bot = { async call(method: string, body: unknown): Promise { calls.push({ method, body }); if (calls.length === 1) { return { ok: true, result: [ { update_id: 10, value: "bad" }, { update_id: 11, value: "retry" }, { update_id: 12, value: "later" }, ], }; } return { ok: true, result: [] }; }, }; const poller = new TelegramUpdatePoller({ botApi: bot, runtime: { sleep: async (ms: number) => void sleeps.push(ms) } as any, backoff: { next: () => 500, reset() {} } as any, processUpdate: async update => { const value = (update as { value: string }).value; processed.push(value); if (value === "bad") throw new Error("boom"); return value === "retry" ? "retry" : "consumed"; }, }); expect(await poller.pollOnce()).toBe(3); expect(await poller.pollOnce()).toBe(0); expect(calls.map(call => call.body.offset)).toEqual([0, 11]); expect(processed).toEqual(["bad", "retry"]); expect(sleeps).toEqual([500]); }); test("TelegramBotTransport keeps JSON and multipart Bot API details outside daemon", async () => { const requests: Array<{ url: string; init: RequestInit }> = []; const transport = new TelegramBotTransport({ botToken: "tok", apiBase: "https://telegram.test", fetchImpl: (async (url: string | URL | Request, init?: RequestInit) => { requests.push({ url: String(url), init: init ?? {} }); return new Response(JSON.stringify({ ok: true, result: true }), { status: 200 }); }) as typeof fetch, }); await transport.call("sendMessage", { chat_id: "42", text: "hello" }); await transport.call("sendPhoto", { chat_id: "42", photo: Buffer.from("x").toString("base64") }); expect(requests[0].url).toBe("https://telegram.test/bottok/sendMessage"); expect(requests[0].init.headers).toEqual({ "content-type": "application/json" }); expect(requests[0].init.body).toBe(JSON.stringify({ chat_id: "42", text: "hello" })); expect(requests[1].url).toBe("https://telegram.test/bottok/sendPhoto"); expect(requests[1].init.body).toBeInstanceOf(FormData); }); test("TelegramBotTransport noRetry performs one application request", async () => { let attempts = 0; const transport = new TelegramBotTransport({ botToken: "tok", apiBase: "https://telegram.test", fetchImpl: (async () => { attempts++; throw new Error("connection reset after write"); }) as unknown as typeof fetch, }); await expect( transport.call("sendMessage", { chat_id: "42", text: "Selected!" }, { noRetry: true }), ).rejects.toThrow("connection reset"); expect(attempts).toBe(1); }); test("TelegramEventDispatchState groups dispatch state without changing maps", () => { const state = new TelegramEventDispatchState(); state.busy.add("S"); state.inboundReactions.set(7, { messageId: 70 }); state.seenUpdateIds.add(99); expect([...state.busy]).toEqual(["S"]); expect(state.inboundReactions.get(7)).toEqual({ messageId: 70 }); expect(state.seenUpdateIds.has(99)).toBe(true); }); test("stale dead-pid lock is stolen by exactly one contender", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, JSON.stringify({ pid: 999, incarnation: "linux:100", startedAt: 0 })); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:100", ownerId: "old", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 0, roots: [], version: 1, }), ); const results = await Promise.all( Array.from({ length: 5 }, () => acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pidAlive: pid => pid !== 999, pidIncarnation: () => "linux:100", pid: 222, }), ), ); expect(results.filter(r => r.acquired)).toHaveLength(1); }); test.each([ "{not json", JSON.stringify({ pid: "invalid", startedAt: 0 }), ])("fresh malformed lock metadata remains protected", async lockMetadata => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, lockMetadata); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: () => false, pidIncarnation: () => "linux:101", randomId: () => "successor", }), ).resolves.toEqual({ acquired: false, attached: false, provisional: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe(lockMetadata); }); test("zero-byte release lock without a generation-3 owner remains blocked", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, ""); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: () => false, pidIncarnation: () => "linux:101", }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe(""); }); test("fresh malformed lock remains protected even with malformed state", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, "{not json"); fs.writeFileSync(paths.state, JSON.stringify({ pid: "invalid", tokenFingerprint: "fp", chatId: "42" })); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: () => false, randomId: () => "successor", }), ).resolves.toEqual({ acquired: false, attached: false, provisional: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe("{not json"); }); test("keeps legacy PID-only live initializer artifacts blocked and unchanged", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); const lock = JSON.stringify({ pid: 111, startedAt: 29_999 }); const state = JSON.stringify({ pid: "invalid", tokenFingerprint: "fp", chatId: "42" }); fs.writeFileSync(paths.lock, lock); fs.writeFileSync(paths.state, state); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:102" : "linux:101"), }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe(lock); expect(fs.readFileSync(paths.state, "utf8")).toBe(state); }); test("stopped state with a retained lock is taken over even when its PID is live", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:103", ownerId: "stopped", acquisitionId: "stopped", startedAt: 0, }), ); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, incarnation: "linux:103", ownerId: "stopped", acquisitionId: "stopped", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 0, stoppedAt: 1, roots: [], version: DAEMON_VERSION, generation: DAEMON_GENERATION, }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:103" : "linux:101"), }), ).resolves.toMatchObject({ acquired: true }); }); test.each([ ["generation 3", { generation: 3 }], ["the pre-generation schema", {}], ] as const)("reclaims a pre-incarnation stopped tombstone from %s when its PID is still live", async (_schema, legacyFields) => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, ownerId: "legacy-stopped", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 1, stoppedAt: 2, roots: [], version: DAEMON_VERSION, ...legacyFields, launcherPid: 110, }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "replacement", now: () => 30_000, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:103" : "linux:101"), }), ).resolves.toMatchObject({ acquired: true, ownerId: "replacement" }); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).not.toHaveProperty("stoppedAt"); }); test("reclaims a generation-3 stopped tombstone with its retained zero-byte lock", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, ""); fs.utimesSync(paths.lock, new Date(150), new Date(150)); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, ownerId: "legacy-stopped", tokenFingerprint: "fp", chatId: "42", startedAt: 100, heartbeatAt: 200, stoppedAt: 300, roots: [], version: DAEMON_VERSION, generation: 3, }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "replacement", now: () => 30_000, pidAlive: () => false, pidIncarnation: pid => `linux:${pid}`, }), ).resolves.toMatchObject({ acquired: true, ownerId: "replacement" }); expect(fs.readFileSync(paths.lock, "utf8")).not.toBe(""); }); test.each([ ["recorded PID is still live", 150, true], ["zero-byte lock is newer than the tombstone", 301, false], ] as const)("keeps a generation-3 zero-byte lock when %s", async (_case, lockMtime, ownerAlive) => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, ""); fs.utimesSync(paths.lock, new Date(lockMtime), new Date(lockMtime)); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, ownerId: "legacy-stopped", tokenFingerprint: "fp", chatId: "42", startedAt: 100, heartbeatAt: 200, stoppedAt: 300, roots: [], version: DAEMON_VERSION, generation: 3, }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "replacement", now: () => 30_000, pidAlive: pid => ownerAlive && pid === 111, pidIncarnation: pid => `linux:${pid}`, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe(""); }); test.each([ ["matching", { pid: 111, startedAt: 0 }, { acquired: true, ownerId: "replacement" }], ["newer-start-time", { pid: 111, startedAt: 3 }, { acquired: false, blocked: true }], ["different-pid", { pid: 112, startedAt: 0 }, { acquired: false, blocked: true }], ] as const)("handles a %s retained legacy lock without reclaiming a newer reservation", async (_case, lock, expected) => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, ownerId: "legacy-stopped", tokenFingerprint: "fp", chatId: "42", startedAt: 1, heartbeatAt: 1, stoppedAt: 2, roots: [], version: DAEMON_VERSION, }), ); fs.writeFileSync(paths.lock, JSON.stringify(lock)); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "replacement", pidAlive: pid => pid === lock.pid || pid === process.pid, pidIncarnation: pid => pid === 222 ? "linux:101" : pid === process.pid ? `linux:${process.pid}` : undefined, }), ).resolves.toMatchObject(expected); }); test("keeps a live malformed legacy tombstone blocked when launcherPid is invalid", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, ownerId: "legacy-stopped", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 1, stoppedAt: 2, roots: [], version: DAEMON_VERSION, launcherPid: "corrupt", }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "replacement", pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:103" : "linux:101"), }), ).resolves.toMatchObject({ acquired: false, blocked: true }); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toHaveProperty("launcherPid", "corrupt"); }); test("release removes only the exact old lock before a successor acquires", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const pidAlive = (pid: number) => pid === 111 || pid === 222 || pid === process.pid; const pidIncarnation = (pid: number) => (pid === 111 ? "linux:111" : pid === 222 ? "linux:222" : `linux:${pid}`); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 111, ownerId: "old", pidAlive, pidIncarnation, }); const paths = daemonPaths(agentDir); let stoppedWritten!: () => void; let continueRelease!: () => void; const stoppedWrite = new Promise(resolve => { stoppedWritten = resolve; }); let pausedStoppedWrite = false; const releaseGate = new Promise(resolve => { continueRelease = resolve; }); const pausedFs: TelegramDaemonFs = { ...exactTransitionFs(), mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: async (file, data, opts) => { await fs.promises.writeFile(file, data, opts); if (!pausedStoppedWrite && file.startsWith(`${paths.state}.`) && file.endsWith(".tmp")) { pausedStoppedWrite = true; stoppedWritten(); await releaseGate; } }, rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: async (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), }; const release = releaseDaemonOwnership({ settings: s, ownerId: "old", tokenFingerprint: "fp", chatId: "42", pid: 111, fs: pausedFs, pidIncarnation, }); await stoppedWrite; const successorUnlinks: string[] = []; const successorFsBase = pausedFs; const successorFs: TelegramDaemonFs = { ...successorFsBase, unlink: async file => { successorUnlinks.push(file); await successorFsBase.unlink(file); }, }; let successorSettled = false; const successor = acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "new", pidAlive, pidIncarnation, fs: successorFs, }).finally(() => { successorSettled = true; }); await Bun.sleep(0); expect(successorSettled).toBe(false); continueRelease(); await release; await expect(successor).resolves.toMatchObject({ acquired: true, ownerId: "new" }); expect(successorUnlinks).not.toContain(paths.lock); expect((await readDaemonState(s))?.ownerId).toBe("new"); expect(fs.existsSync(paths.lock)).toBe(true); }); test("malformed acquisition PID fails closed without probing liveness", async () => { const agentDir = tempAgentDir(); const probes: number[] = []; await expect( acquireDaemonOwnership({ settings: setPrivateAgentDir(settings(agentDir), agentDir), tokenFingerprint: "fp", chatId: "42", pid: Number.NaN, pidAlive: pid => { probes.push(pid); return false; }, }), ).resolves.toEqual({ acquired: false, attached: true }); expect(probes).toEqual([]); }); test("malformed persisted PID is not sent to the liveness probe", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, JSON.stringify({ pid: "invalid", startedAt: 0 })); fs.writeFileSync( paths.state, JSON.stringify({ pid: "invalid", ownerId: "old", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 0, roots: [], version: DAEMON_VERSION, }), ); const probes: number[] = []; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: pid => { probes.push(pid); return false; }, }); expect(probes).toEqual([]); }); test("state publication failure leaves recoverable lock metadata", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let failStateWrite = true; const crashingFs: TelegramDaemonFs = { ...transitionFsCapabilities(), mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: async (file, data, opts) => { if (file.startsWith(`${paths.state}.`) && file.endsWith(".tmp") && failStateWrite) { failStateWrite = false; throw new Error("simulated crash between lock and state publication"); } await fs.promises.writeFile(file, data, opts); }, rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: async (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), }; await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 111, ownerId: "crashed", now: () => 0, pidIncarnation: () => "linux:111", fs: crashingFs, }), ).rejects.toThrow("simulated crash"); expect(fs.existsSync(paths.lock)).toBe(true); expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toEqual({ pid: 111, incarnation: "linux:111", ownerId: "crashed", acquisitionId: "crashed", startedAt: 0, }); expect(fs.existsSync(paths.state)).toBe(false); expect( await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: () => false, pidIncarnation: () => "linux:222", }), ).toMatchObject({ acquired: true }); }); test("classifies a canonical live initializer without state as provisional", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "initializer", acquisitionId: "initializer", startedAt: 0, }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:111" : "linux:222"), }), ).resolves.toEqual({ acquired: false, attached: false, provisional: true }); }); test("waits for a canonical initializer to publish ready state before registering its root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const cwd = path.join(agentDir, "concurrent-session"); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "initializer", acquisitionId: "initializer", startedAt: 100, }), ); let published = false; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd, sessionId: "concurrent-session" }, { pid: 222, now: () => 100, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:111" : "linux:222"), readinessTimeoutMs: 25, waitStepMs: 5, sleep: async () => { expect(fs.existsSync(paths.roots)).toBe(false); if (published) return; published = true; fs.writeFileSync( paths.state, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "initializer", acquisitionId: "initializer", ownershipPhase: "ready", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, generation: DAEMON_GENERATION, servingEpoch: SERVING_EPOCH, }), ); }, }, ), ).resolves.toBe("attached"); expect(published).toBe(true); expect(JSON.parse(fs.readFileSync(paths.roots, "utf8"))).toMatchObject({ sessions: { "concurrent-session": expect.any(String) }, }); }); test("concurrent ensure follows a child that self-binds between provisional acquisition and its state snapshot, then reclaims the dead child lock after a crash or forced kill", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const root = path.join(agentDir, "concurrent-rebound"); const fp = tokenFingerprint("123456:secret-token"); let childProvenanceAvailable = false; let childAlive = true; let spawned = 0; const pidIncarnation = (pid: number) => { if (pid === 4242) return "linux:4242"; if (pid === 4243) return childProvenanceAvailable ? "linux:4243" : undefined; if (pid === 4245) return "linux:4245"; return "linux:100"; }; const pidAlive = (pid: number) => pid === 4242 || (pid === 4243 && childAlive) || pid === 4245; const acquisitionId = "launcher-acquisition"; const first = await spawnTelegramDaemonOwner( { settings: s, roots: [root], tokenFingerprint: fp, chatId: "42" }, { pid: 4242, platform: "win32", pidAlive, pidIncarnation, randomId: () => acquisitionId, spawn: () => { spawned++; return { pid: 4243, unref() {} }; }, }, ); expect(first).toMatchObject({ result: "owner_spawned", acquisition: { launcherPid: 4242, pid: 4243 } }); if (first.result !== "owner_spawned") throw new Error(`Expected owner_spawned, received ${first.result}`); const boundOwnerId = first.acquisition.ownerId; const boundAcquisitionId = first.acquisition.acquisitionId; expect(await readDaemonState(s)).toMatchObject({ pid: 4242, ownerId: boundOwnerId, acquisitionId: boundAcquisitionId, ownershipPhase: "provisional", }); let published = false; let stateReads = 0; let boundAtStateRead: number | undefined; const bindingFs: TelegramDaemonFs = { ...transitionFsCapabilities(), mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: async (file, encoding) => { if (file === paths.state && ++stateReads === 6) { boundAtStateRead = stateReads; published = true; childProvenanceAvailable = true; const renewed = await renewDaemonHeartbeat({ settings: s, ownerId: boundOwnerId, acquisitionId: boundAcquisitionId, pid: 4243, pidIncarnation, }); if (!renewed) throw new Error( `child binding rejected: state=${await fs.promises.readFile(paths.state, "utf8")} lock=${await fs.promises.readFile(paths.lock, "utf8")}`, ); } return await fs.promises.readFile(file, encoding); }, writeFile: (file, data, opts) => fs.promises.writeFile(file, data, opts).then(() => undefined), rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: async (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), }; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: root, sessionId: "concurrent-rebound" }, { pid: 4244, platform: "win32", pidAlive, pidIncarnation, fs: bindingFs, spawn: () => { spawned++; return { pid: 4245, unref() {} }; }, readinessTimeoutMs: 25, waitStepMs: 5, sleep: async () => undefined, }, ), ).resolves.toBe("attached"); expect(boundAtStateRead).toBe(6); expect(spawned).toBe(1); expect(published).toBe(true); expect(JSON.parse(fs.readFileSync(paths.roots, "utf8"))).toMatchObject({ sessions: { "concurrent-rebound": expect.any(String) }, }); expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toMatchObject({ pid: 4243, incarnation: "linux:4243", ownerId: boundOwnerId, acquisitionId: boundAcquisitionId, }); const state = await readDaemonState(s); expect(state).toMatchObject({ launcherPid: 4242, pid: 4243, ownershipPhase: "ready" }); await expect( waitForTelegramDaemonReady({ settings: s, ownerId: state?.ownerId, acquisitionId: `${boundAcquisitionId}-foreign`, tokenFingerprint: fp, chatId: "42", pidAlive, pidIncarnation, timeoutMs: 0, }), ).resolves.toBe(false); await expect( waitForTelegramDaemonReady({ settings: s, ownerId: state?.ownerId, acquisitionId: boundAcquisitionId, tokenFingerprint: "foreign-token", chatId: "42", pidAlive, pidIncarnation, timeoutMs: 0, }), ).resolves.toBe(false); // A child crash (including an ungraceful forced kill) must leave its own, // dead lease reclaimable even while the source launcher remains live. childAlive = false; let replacementPublished = false; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "replacement"), sessionId: "replacement" }, { pid: 4244, pidAlive, pidIncarnation, randomId: () => "replacement-acquisition", spawn: () => { spawned++; return { pid: 4245, unref() {} }; }, readinessTimeoutMs: 25, waitStepMs: 5, sleep: async () => { if (replacementPublished) return; replacementPublished = true; const replacement = await readDaemonState(s); expect(replacement).toBeDefined(); expect( await renewDaemonHeartbeat({ settings: s, ownerId: replacement?.ownerId ?? "", acquisitionId: replacement?.acquisitionId, pid: 4245, pidIncarnation, }), ).toBe(true); }, }, ), ).resolves.toBe("spawned"); expect(spawned).toBe(2); expect(replacementPublished).toBe(true); const replacementLock = JSON.parse(fs.readFileSync(paths.lock, "utf8")) as { pid: number; incarnation: string; ownerId: string; acquisitionId: string; }; expect(replacementLock).toMatchObject({ pid: 4245, incarnation: "linux:4245" }); expect(replacementLock.ownerId).toBe(replacementLock.acquisitionId); }); test("times out a canonical initializer that never publishes ready state without registering its root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:111", ownerId: "initializer", acquisitionId: "initializer", startedAt: 100, }), ); let sleeps = 0; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "timeout-session"), sessionId: "timeout-session" }, { pid: 222, now: () => 100, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:111" : "linux:222"), readinessTimeoutMs: 10, waitStepMs: 5, sleep: async () => { sleeps++; }, }, ), ).resolves.toBe("blocked_identity"); expect(sleeps).toBe(2); expect(fs.existsSync(paths.roots)).toBe(false); }); test("blocks a live initializer lock when canonical provenance is unavailable", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); const lock = JSON.stringify({ pid: 111, incarnation: "linux:111", startedAt: 0 }); fs.writeFileSync(paths.lock, lock); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? undefined : "linux:222"), }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe(lock); }); test("reclaims a canonical initializer lock after PID reuse", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.lock, JSON.stringify({ pid: 111, incarnation: "linux:444", startedAt: 0 })); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, now: () => 30_000, pidAlive: pid => pid === 111, pidIncarnation: pid => (pid === 111 ? "linux:333" : "linux:222"), ownerId: "successor", }), ).resolves.toMatchObject({ acquired: true, ownerId: "successor" }); expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toMatchObject({ pid: 222, incarnation: "linux:222" }); }); test("serializes provisional lock publication and reclaims only a proven-dead owner", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:old", ownerId: "old", acquisitionId: "old", ownershipPhase: "ready", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 0, roots: [], version: DAEMON_VERSION, generation: DAEMON_GENERATION, }), ); let publicationStarted!: () => void; const publicationReached = new Promise(resolve => { publicationStarted = resolve; }); let releasePublication!: () => void; const publicationReleased = new Promise(resolve => { releasePublication = resolve; }); let paused = true; const pausedFs: TelegramDaemonFs = { ...transitionFsCapabilities(), mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: async (file, data, opts) => { if (paused && file.startsWith(`${paths.state}.`) && file.endsWith(".tmp")) { publicationStarted(); await publicationReleased; } await fs.promises.writeFile(file, data, opts); }, rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: async (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), stat: file => fs.promises.stat(file), }; const provenance = (pid: number) => `linux:${pid}`; const first = acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 111, ownerId: "first", now: () => 30_000, pidAlive: pid => pid === 111 || pid === process.pid, pidIncarnation: provenance, fs: pausedFs, }); await publicationReached; expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toMatchObject({ pid: 111, ownerId: "first", acquisitionId: "first", }); const second = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "second", now: () => 30_000, pidAlive: pid => pid === 111 || pid === process.pid, pidIncarnation: provenance, fs: pausedFs, }); expect(second).toEqual({ acquired: false, attached: false, provisional: true }); expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toMatchObject({ ownerId: "first" }); paused = false; releasePublication(); expect(await first).toMatchObject({ acquired: true, ownerId: "first" }); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ ownerId: "first" }); expect( await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "recovered", now: () => 60_000, pidAlive: () => false, pidIncarnation: provenance, }), ).toMatchObject({ acquired: true, ownerId: "recovered" }); }); test.each([ "valid", "partial", ] as const)("keeps a new %s initializer protected from an older stopped tombstone past transition TTL", async publication => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 999, incarnation: "linux:old", ownerId: "old", acquisitionId: "old", startedAt: 0 }), ); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:old", ownerId: "old", acquisitionId: "old", tokenFingerprint: "fp", chatId: "42", startedAt: 0, heartbeatAt: 0, stoppedAt: 1, roots: [], version: DAEMON_VERSION, generation: DAEMON_GENERATION, }), ); let reached!: () => void; const reachedPublication = new Promise(resolve => { reached = resolve; }); let resume!: () => void; const publicationGate = new Promise(resolve => { resume = resolve; }); let paused = true; const pausedFs: TelegramDaemonFs = { ...transitionFsCapabilities(), mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: async (file, data, opts) => { const pauseOnLock = publication === "partial" && file === paths.lock && typeof opts === "object" && opts !== null && "flag" in opts && opts.flag === "wx"; const pauseOnState = publication === "valid" && file.startsWith(`${paths.state}.`) && file.endsWith(".tmp"); if (paused && (pauseOnLock || pauseOnState)) { if (pauseOnLock) await fs.promises.writeFile(file, "{", { mode: 0o600 }); reached(); await publicationGate; if (pauseOnLock) await fs.promises.writeFile(file, data, { mode: 0o600 }); else await fs.promises.writeFile(file, data, opts); return; } await fs.promises.writeFile(file, data, opts); }, rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: async (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), stat: file => fs.promises.stat(file), }; const provenance = (pid: number) => `linux:${pid}`; const first = acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 111, ownerId: "first", now: () => 30_000, pidAlive: pid => pid === 111 || pid === process.pid, pidIncarnation: provenance, fs: pausedFs, }); await reachedPublication; const second = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 222, ownerId: "second", now: () => 60_000, pidAlive: pid => pid === 111 || pid === process.pid, pidIncarnation: provenance, fs: pausedFs, }); expect(second).toEqual({ acquired: false, attached: false, provisional: true }); paused = false; resume(); expect(await first).toMatchObject({ acquired: true, ownerId: "first" }); const lock = JSON.parse(fs.readFileSync(paths.lock, "utf8")); const state = JSON.parse(fs.readFileSync(paths.state, "utf8")); expect(lock).toMatchObject({ ownerId: "first", acquisitionId: "first", pid: 111 }); expect(state).toMatchObject({ ownerId: "first", acquisitionId: "first", pid: 111 }); }); test("fresh heartbeat is not stolen", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:100", ownerId: "old", tokenFingerprint: "fp", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, generation: DAEMON_GENERATION, acquisitionId: "old", ownershipPhase: "ready", servingEpoch: SERVING_EPOCH, }), ); fs.writeFileSync( paths.lock, JSON.stringify({ pid: 999, incarnation: "linux:100", ownerId: "old", acquisitionId: "old", startedAt: 100 }), ); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pidAlive: () => true, pidIncarnation: () => "linux:100", now: () => 101, }); expect(result).toEqual({ acquired: false, attached: true }); }); test("live owner token/chat mismatch blocks attach without registering a root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:100", ownerId: "old", tokenFingerprint: "old-fp", chatId: "old-chat", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, }), ); fs.writeFileSync(paths.lock, ""); const beforeState = fs.readFileSync(paths.state, "utf8"); const signals: Array<[number, string]> = []; const unlinked: string[] = []; const baseFs = topicStateFs(async () => undefined); const recordingFs: TelegramDaemonFs = { ...baseFs, unlink: async file => { unlinked.push(file); await fs.promises.unlink(file); }, }; let spawns = 0; const result = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { now: () => 101, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: (pid, signal) => signals.push([pid, signal]), fs: recordingFs, spawn: () => { spawns++; return { unref() {} }; }, }, ); expect(result).toBe("blocked"); expect(spawns).toBe(0); expect(signals).toEqual([]); expect(unlinked).toEqual([]); expect(fs.existsSync(paths.lock)).toBe(true); expect(fs.readFileSync(paths.state, "utf8")).toBe(beforeState); expect(fs.existsSync(paths.roots)).toBe(false); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ ownerId: "old", tokenFingerprint: "old-fp", chatId: "old-chat", }); }); test.each([ ["unavailable current provenance", "linux:100", undefined, "missing"], ["non-canonical current provenance", "linux:100", "linux:not-a-start-time", "aged-malformed"], ["non-canonical persisted provenance", "linux:not-a-start-time", "linux:100", "aged-malformed"], ["missing persisted provenance with no lock", undefined, "linux:100", "missing"], ["missing persisted provenance with an aged malformed lock", undefined, "linux:100", "aged-malformed"], ["non-string persisted provenance with no lock", 100, "linux:100", "missing"], ["non-string persisted provenance with an aged malformed lock", 100, "linux:100", "aged-malformed"], ])( "foreign live owner with %s remains blocked without replacement", async (_name, incarnation, currentIncarnation, lockKind) => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir, `123456:${path.basename(agentDir)}`), agentDir); const paths = daemonPaths(agentDir); const state = { pid: 999, incarnation, ownerId: "foreign-owner", tokenFingerprint: "foreign-fp", chatId: "foreign-chat", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, JSON.stringify(state)); if (lockKind === "aged-malformed") { fs.writeFileSync(paths.lock, "{"); fs.utimesSync(paths.lock, 0, 0); } const beforeState = fs.readFileSync(paths.state, "utf8"); const beforeLock = fs.existsSync(paths.lock) ? fs.readFileSync(paths.lock, "utf8") : undefined; let spawns = 0; const result = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: pid => (pid === 999 ? currentIncarnation : "linux:200"), spawn: () => { spawns++; return { unref() {} }; }, }, ); expect(result).toBe("blocked"); expect(spawns).toBe(0); expect(fs.readFileSync(paths.state, "utf8")).toBe(beforeState); expect(fs.existsSync(paths.lock)).toBe(beforeLock !== undefined); if (beforeLock !== undefined) expect(fs.readFileSync(paths.lock, "utf8")).toBe(beforeLock); expect(fs.existsSync(paths.roots)).toBe(false); }, 15_000, ); test.each([ "missing", "aged-malformed", ])("same-identity live owner with a non-string incarnation and a %s lock remains unchanged", async lockKind => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: 100, ownerId: "same-owner", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, }), ); if (lockKind === "aged-malformed") { fs.writeFileSync(paths.lock, "{"); fs.utimesSync(paths.lock, 0, 0); } const beforeState = fs.readFileSync(paths.state, "utf8"); const beforeLock = fs.existsSync(paths.lock) ? fs.readFileSync(paths.lock, "utf8") : undefined; let spawns = 0; const result = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: pid => (pid === 999 ? "linux:100" : "linux:200"), spawn: () => { spawns++; return { unref() {} }; }, }, ); expect(result).toBe("blocked"); expect(spawns).toBe(0); expect(fs.readFileSync(paths.state, "utf8")).toBe(beforeState); expect(fs.existsSync(paths.lock)).toBe(beforeLock !== undefined); if (beforeLock !== undefined) expect(fs.readFileSync(paths.lock, "utf8")).toBe(beforeLock); expect(fs.existsSync(paths.roots)).toBe(false); }); test.each([ "missing", "aged-malformed", ])("foreign live owner with malformed stoppedAt and a %s lock remains unchanged", async lockKind => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:100", ownerId: "foreign-owner", tokenFingerprint: "foreign-fp", chatId: "foreign-chat", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, stoppedAt: "not-a-time", }), ); if (lockKind === "aged-malformed") { fs.writeFileSync(paths.lock, "{"); fs.utimesSync(paths.lock, 0, 0); } const beforeState = fs.readFileSync(paths.state, "utf8"); const beforeLock = fs.existsSync(paths.lock) ? fs.readFileSync(paths.lock, "utf8") : undefined; let spawns = 0; const result = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: pid => (pid === 999 ? "linux:100" : "linux:200"), spawn: () => { spawns++; return { unref() {} }; }, }, ); expect(result).toBe("blocked"); expect(spawns).toBe(0); expect(fs.readFileSync(paths.state, "utf8")).toBe(beforeState); expect(fs.existsSync(paths.lock)).toBe(beforeLock !== undefined); if (beforeLock !== undefined) expect(fs.readFileSync(paths.lock, "utf8")).toBe(beforeLock); expect(fs.existsSync(paths.roots)).toBe(false); }); test("foreign live owner with a canonical mismatched incarnation is reclaimed", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const state = { pid: 999, incarnation: "linux:100", ownerId: "foreign-owner", tokenFingerprint: "foreign-fp", chatId: "foreign-chat", startedAt: 100, heartbeatAt: 100, roots: [], version: DAEMON_VERSION, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, JSON.stringify(state)); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 222, randomId: () => "replacement", pidAlive: pid => pid === 999, pidIncarnation: pid => (pid === 999 ? "linux:101" : "linux:200"), now: () => 101, }), ).resolves.toMatchObject({ acquired: true, ownerId: "replacement" }); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 222, ownerId: "replacement", tokenFingerprint: "e60b05c186ca", chatId: "42", }); }); // ----------------------------------------------------------------------- // A rolling serving-epoch upgrade can leave a still-live predecessor owning // the lock. Its persisted schema `version` is unchanged (1), so a freshly // upgraded host must converge an earlier serving epoch instead of silently // attaching. Generation is retained for guarded behavior inventory only; // same-epoch live owners attach across generation changes. // ----------------------------------------------------------------------- function liveOwnerState(extra: Partial = {}): DaemonState { return { pid: 999, incarnation: "linux:100", ownerId: "old", tokenFingerprint: "e60b05c186ca", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, acquisitionId: "old", ownershipPhase: "ready", servingEpoch: SERVING_EPOCH, ...extra, }; } function writeLiveOwner(agentDir: string, extra: Partial = {}): void { const paths = daemonPaths(agentDir); const state = liveOwnerState(extra); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, JSON.stringify(state)); fs.writeFileSync( paths.lock, JSON.stringify({ pid: state.pid, incarnation: state.incarnation, ownerId: state.ownerId, acquisitionId: state.acquisitionId ?? state.ownerId, startedAt: state.startedAt, }), ); } test("keeps wire protocol 3 while generation 32 adds the rejected-token poll pause", () => { expect(NOTIFICATION_PROTOCOL_VERSION).toBe(3); // Generation 29 adds structural serving safeguards; generation 30 adds // capability-versioned tool activity; generation 31 adds the Windows // cooperative control stop; generation 32 pauses polling on a rejected // bot token, all without changing the wire protocol. expect(DAEMON_GENERATION).toBe(32); }); test.each([ "1", 0, -1, 1.5, 2 ** 53, Number.NaN, ])("servingEpoch %p is malformed and has no lifecycle authority", async servingEpoch => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); // Intentionally malformed runtime JSON: cross the type boundary explicitly. const state = { ...liveOwnerState(), servingEpoch } as unknown as Partial & ReturnType; writeLiveOwner(agentDir, state); expect(hasSafeDaemonStateShape(state)).toBe(false); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: state.tokenFingerprint, chatId: state.chatId, now: () => 101, pidAlive: pid => pid === state.pid, pidIncarnation: () => state.incarnation, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); }); test("servingEpoch future owner is safe but attaches without signals or artifact changes", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const state = liveOwnerState({ generation: DAEMON_GENERATION, servingEpoch: 99, tokenFingerprint: tokenFingerprint("123456:secret-token"), }); writeLiveOwner(agentDir, state); const paths = daemonPaths(agentDir); const stateBefore = fs.readFileSync(paths.state, "utf8"); const lockBefore = fs.readFileSync(paths.lock, "utf8"); const signals: Array<[number, string]> = []; expect( isCurrentCompatibleOwner({ state, now: 101, tokenFingerprint: state.tokenFingerprint, chatId: state.chatId, pidAlive: () => true, pidIncarnation: () => state.incarnation, }), ).toBe(true); expect(hasSafeDaemonStateShape(state)).toBe(true); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "future-epoch"), sessionId: "future-epoch" }, { now: () => 101, pid: 4242, pidAlive: () => true, pidIncarnation: () => state.incarnation, sendSignal: (pid, signal) => signals.push([pid, signal]), spawn: () => { throw new Error("future owner must not be replaced"); }, }, ), ).resolves.toBe("attached"); expect(signals).toEqual([]); expect(fs.readFileSync(paths.state, "utf8")).toBe(stateBefore); expect(fs.readFileSync(paths.lock, "utf8")).toBe(lockBefore); }); test("servingEpoch absent is epoch 1 and requests convergence, while equal epoch attaches", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION, servingEpoch: undefined }); const ownershipInput = { settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", now: () => 101, pidAlive: (pid: number) => pid === 999, pidIncarnation: () => "linux:100", }; await expect(acquireDaemonOwnership(ownershipInput)).resolves.toEqual({ acquired: false, attached: false, reloadRequired: true, }); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION, servingEpoch: SERVING_EPOCH }); await expect(acquireDaemonOwnership(ownershipInput)).resolves.toEqual({ acquired: false, attached: true }); }); test("servingEpoch predecessor without a generation requests convergence", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { servingEpoch: undefined }); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: () => true, pidIncarnation: () => "linux:100", now: () => 101, }); expect(result).toEqual({ acquired: false, attached: false, reloadRequired: true }); }); test("same servingEpoch attaches across an immediately preceding generation", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION - 1, heartbeatAt: Date.now() }); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: () => true, pidIncarnation: () => "linux:100", now: () => Date.now(), }); expect(result).toEqual({ acquired: false, attached: true }); await expect( new TelegramDaemonController(s, { now: () => 101, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", }).status(), ).resolves.toMatchObject({ health: "running", pid: 999 }); const signals: Array<[number, string]> = []; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "same-epoch-successor"), sessionId: "same-epoch-successor" }, { now: () => 101, pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: (pid, signal) => signals.push([pid, signal]), spawn: () => { throw new Error("same-epoch predecessor must attach"); }, }, ), ).resolves.toBe("attached"); expect(signals).toEqual([]); }); test("readiness attaches a same-epoch cross-generation owner but rejects a malformed generation", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const now = 1_000; const readiness = { settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", now: () => now, pidAlive: () => true, pidIncarnation: () => "linux:100", sleep: async () => undefined, waitStepMs: 5, timeoutMs: 50, }; // Serving-epoch gated, not generation gated: a same-epoch owner from the // immediately preceding generation is a compatible ready owner. writeLiveOwner(agentDir, { generation: DAEMON_GENERATION - 1, servingEpoch: SERVING_EPOCH, heartbeatAt: now }); await expect(waitForTelegramDaemonReady(readiness)).resolves.toBe(true); // A fractional generation has no lifecycle authority and never publishes readiness. writeLiveOwner(agentDir, { generation: DAEMON_GENERATION - 0.5, servingEpoch: SERVING_EPOCH, heartbeatAt: now, }); await expect(waitForTelegramDaemonReady(readiness)).resolves.toBe(false); }); test("generation-less parent-format live owner remains blocked", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); const parent = { pid: 999, ownerId: "parent", tokenFingerprint: "e60b05c186ca", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, }; fs.writeFileSync(paths.state, JSON.stringify(parent)); const input = { settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: (pid: number) => pid === 999, pidIncarnation: () => "linux:100", now: () => 101, }; expect(await acquireDaemonOwnership(input)).toEqual({ acquired: false, attached: false, blocked: true }); fs.writeFileSync(paths.state, JSON.stringify({ ...parent, heartbeatAt: 102 })); expect(await acquireDaemonOwnership({ ...input, now: () => 103 })).toEqual({ acquired: false, attached: false, blocked: true, }); }); test("legacy generation-absent parent owner makes detailed ensure blocked_identity without signaling", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const parent = { pid: 999, ownerId: "parent", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, `${JSON.stringify(parent, null, 2)}\n`); const signals: Array<[number, string]> = []; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "legacy-parent"), sessionId: "legacy-parent" }, { pid: 4242, now: () => 101, pidAlive: () => true, pidIncarnation: () => "linux:100", sendSignal: (pid, signal) => signals.push([pid, signal]), readinessTimeoutMs: 0, spawn: () => { throw new Error("generation-absent parent must not be replaced"); }, }, ), ).resolves.toBe("blocked_identity"); expect(signals).toEqual([]); }); test("parent-format dead owner is reclaimed, while reused and static live owners remain blocked", async () => { const parent = { pid: 999, ownerId: "parent", tokenFingerprint: "e60b05c186ca", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, }; for (const [name, pidAlive, pidIncarnation, expected] of [ ["dead", () => false, () => "linux:100", true], ["reused", () => true, () => undefined, false], ["static", () => true, () => "linux:100", false], ] as const) { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, JSON.stringify(parent)); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: parent.tokenFingerprint, chatId: parent.chatId, pid: 222, randomId: () => name, pidAlive, pidIncarnation, now: () => 101, }); expect(result.acquired).toBe(expected); } }); test("parent-format dead owner is reclaimed before changed token and chat identity are checked", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, ownerId: "parent", tokenFingerprint: "previous-token", chatId: "previous-chat", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, }), ); expect( await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 222, randomId: () => "reclaimed", pidAlive: () => false, pidIncarnation: () => "linux:100", now: () => 101, }), ).toMatchObject({ acquired: true }); }); test.each([ "", "{", ])("malformed legacy transition reservation %p remains blocked after TTL for manual cleanup", async legacyReservation => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const state = path.join(agentDir, "telegram-daemon.json"); const stateBefore = JSON.stringify({ ownerId: "existing-owner", pid: 111 }); fs.writeFileSync(marker, legacyReservation); fs.writeFileSync(state, stateBefore); fs.utimesSync(marker, 0, 0); await expect( acquireDaemonTransitionLock({ fs: exactTransitionFs(), path: marker, pid: 222, pidAlive: () => false, pidIncarnation: () => "linux:100", now: () => 100_000, randomToken: () => "transition-token", retries: 1, retryDelayMs: 0, sleep: async () => undefined, }), ).resolves.toBeUndefined(); expect(fs.readFileSync(marker, "utf8")).toBe(legacyReservation); expect(fs.readFileSync(state, "utf8")).toBe(stateBefore); }); test("a paused generation-6 writer cannot overwrite generation-7 authority after blocked recovery", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const state = path.join(agentDir, "telegram-daemon.json"); const legacyReservation = ""; const legacyPublication = JSON.stringify({ pid: 111, startedAt: 1 }); const stateBefore = JSON.stringify({ ownerId: "existing-owner", pid: 111 }); fs.writeFileSync(marker, legacyReservation); fs.writeFileSync(state, stateBefore); fs.utimesSync(marker, 0, 0); const transition = await acquireDaemonTransitionLock({ fs: exactTransitionFs(), path: marker, pid: 222, pidAlive: () => false, pidIncarnation: () => "linux:222", now: () => 100_000, randomToken: () => "generation-7-token", retries: 1, retryDelayMs: 0, sleep: async () => undefined, }); expect(transition).toBeUndefined(); expect(fs.readFileSync(marker, "utf8")).toBe(legacyReservation); // This models the old second pathname write resuming after generation 7 was // blocked. No generation-7 authority was published for it to overwrite. fs.writeFileSync(marker, legacyPublication); expect(fs.readFileSync(marker, "utf8")).toBe(legacyPublication); expect(fs.readFileSync(state, "utf8")).toBe(stateBefore); }); test("transition stale reclaim preserves a successor installed after exact validation", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const stale = JSON.stringify({ pid: 999, incarnation: "linux:old", createdAt: 0, token: "stale-token" }); const fresh = JSON.stringify({ pid: 111, incarnation: "linux:fresh", createdAt: 1, token: "fresh-token" }); fs.writeFileSync(marker, stale); fs.utimesSync(marker, 0, 0); const transitionFs = exactTransitionFs(file => { fs.unlinkSync(file); fs.writeFileSync(file, fresh); }); await expect( acquireDaemonTransitionLock({ fs: transitionFs, path: marker, pid: 222, pidAlive: pid => pid === 222, pidIncarnation: pid => (pid === 222 ? "linux:222" : undefined), now: () => 100_000, retries: 0, }), ).resolves.toBeUndefined(); expect(fs.readFileSync(marker, "utf8")).toBe(fresh); }); test("transition reclaim retains a live marker when a later probe would be unavailable", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const live = JSON.stringify({ pid: 999, incarnation: "linux:live", createdAt: 0, token: "live-token" }); fs.writeFileSync(marker, live); let liveProbeCount = 0; let liveCheckCount = 0; await expect( acquireDaemonTransitionLock({ fs: exactTransitionFs(), path: marker, pid: 222, pidAlive: pid => { liveCheckCount++; return pid === 999; }, pidIncarnation: pid => { if (pid === 222) return "linux:222"; liveProbeCount++; return liveProbeCount === 1 ? "linux:live" : undefined; }, retries: 0, }), ).resolves.toBeUndefined(); expect(liveProbeCount).toBe(1); expect(liveCheckCount).toBe(1); expect(fs.readFileSync(marker, "utf8")).toBe(live); }); test("atomic transition publication cannot overwrite a successor installed while a creator is stalled", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const fresh = JSON.stringify({ pid: 111, incarnation: "linux:fresh", createdAt: 1, token: "fresh-token" }); const transitionFs = exactTransitionFs(); let stalled = true; const writeFile = transitionFs.writeFile; transitionFs.writeFile = async (file, data, opts) => { if (stalled) { stalled = false; fs.writeFileSync(file, fresh, { flag: "wx" }); } await writeFile(file, data, opts); }; await expect( acquireDaemonTransitionLock({ fs: transitionFs, path: marker, pid: 222, pidAlive: pid => pid === 111, pidIncarnation: () => "linux:222", retries: 0, }), ).resolves.toBeUndefined(); expect(fs.readFileSync(marker, "utf8")).toBe(fresh); }); test("transition acquisition requires an identity-capable releaser", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); await expect( acquireDaemonTransitionLock({ fs: { readFile: (file: string, encoding: "utf8") => fs.promises.readFile(file, encoding), writeFile: (file: string, data: string, opts?: Parameters[2]) => fs.promises.writeFile(file, data, opts), }, path: marker, pid: 222, pidAlive: () => false, pidIncarnation: () => "linux:222", }), ).resolves.toBeUndefined(); expect(fs.existsSync(marker)).toBe(false); }); test("exact-token release cannot remove a successor", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const lock = { pid: 222, incarnation: "linux:222", createdAt: 1, token: "owner-token" }; const fresh = JSON.stringify({ pid: 111, incarnation: "linux:fresh", createdAt: 2, token: "fresh-token" }); fs.writeFileSync(marker, `${JSON.stringify(lock)}\n`); const transitionFs = exactTransitionFs(file => { fs.unlinkSync(file); fs.writeFileSync(file, fresh); }); await expect(releaseDaemonTransitionLock({ fs: transitionFs, path: marker, lock })).resolves.toBe(false); expect(fs.readFileSync(marker, "utf8")).toBe(fresh); }); test("transition lock retains a live non-canonical owner", async () => { const agentDir = tempAgentDir(); const marker = path.join(agentDir, "transition.steal"); const legacy = JSON.stringify({ pid: 999, incarnation: "darwin:Thu Jul 17 10:00:00 2025", createdAt: 1 }); fs.writeFileSync(marker, legacy); const transitionFs = exactTransitionFs(); await expect( acquireDaemonTransitionLock({ fs: transitionFs, path: marker, pid: 222, pidAlive: () => true, pidIncarnation: () => "linux:222", now: () => 100_000, retries: 0, }), ).resolves.toBeUndefined(); expect(fs.readFileSync(marker, "utf8")).toBe(legacy); }); test("#2028 acquire attaches to a current-generation live owner (no reload)", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION }); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: () => true, pidIncarnation: () => "linux:100", now: () => 101, }); expect(result).toEqual({ acquired: false, attached: true }); }); test("#2028 refuses a phase-less current-generation owner instead of attaching", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION, ownershipPhase: undefined }); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: () => true, pidIncarnation: () => "linux:100", now: () => 101, }); expect(result).toEqual({ acquired: false, attached: false, provisional: true }); }); test("reclaims same-identity stale ownership after authoritative PID reuse without signaling the replacement", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let spawns = 0; const signals: Array<[number, string]> = []; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, onSpawn: () => spawns++, }); writeLiveOwner(agentDir, { ownerId: "stale-owner", acquisitionId: "stale-owner", pid: 999, incarnation: "linux:104", generation: DAEMON_GENERATION, }); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "reused-pid" }, { pid: 4242, pidAlive: pid => pid === 999 || pid === 4243, pidIncarnation: pid => (pid === 999 ? "linux:105" : "linux:100"), sendSignal: (pid, signal) => signals.push([pid, signal]), spawn: child.spawn, sleep: child.sleep, waitStepMs: 1, readinessTimeoutMs: 10, }, ), ).resolves.toBe("spawned"); expect(signals).toEqual([]); expect(spawns).toBe(1); expect(fs.existsSync(paths.lock)).toBe(true); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4243, incarnation: "linux:100", generation: DAEMON_GENERATION, ownershipPhase: "ready", }); }); test("does not reclaim or attach a same-identity owner when PID provenance is unavailable", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION }); const stateBefore = fs.readFileSync(paths.state, "utf8"); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pid: 4242, pidAlive: () => true, pidIncarnation: pid => (pid === 999 ? undefined : "linux:100"), }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.existsSync(paths.lock)).toBe(true); expect(fs.readFileSync(paths.state, "utf8")).toBe(stateBefore); }); test("leaves complete live legacy ownership artifacts unchanged when provenance is non-canonical", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); writeLiveOwner(agentDir, { incarnation: "darwin:Thu Jul 17 10:00:00 2025" as unknown as string, generation: undefined, }); fs.writeFileSync(paths.lock, "legacy-owner-lock"); fs.writeFileSync( paths.steal, JSON.stringify({ pid: 999, incarnation: "darwin:Thu Jul 17 10:00:00 2025", createdAt: 1 }), ); const stateBefore = fs.readFileSync(paths.state, "utf8"); const lockBefore = fs.readFileSync(paths.lock, "utf8"); const transitionLockBefore = fs.readFileSync(paths.steal, "utf8"); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pid: 4242, pidAlive: () => true, pidIncarnation: () => "linux:4242", }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.state, "utf8")).toBe(stateBefore); expect(fs.readFileSync(paths.lock, "utf8")).toBe(lockBefore); expect(fs.readFileSync(paths.steal, "utf8")).toBe(transitionLockBefore); }); test("#2028 acquire does not downgrade a NEWER-generation live owner (attaches)", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION + 1 }); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: () => true, pidIncarnation: () => "linux:100", now: () => 101, }); expect(result).toEqual({ acquired: false, attached: true }); }); test("#2028 acquiring ownership stamps the current daemon generation into state", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const result = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 111, pidIncarnation: () => "linux:100", randomId: () => "owner", }); expect(result).toMatchObject({ acquired: true, ownerId: "owner", acquisitionId: "owner" }); const state = JSON.parse(fs.readFileSync(daemonPaths(agentDir).state, "utf8")); expect(state).toMatchObject({ generation: DAEMON_GENERATION, acquisitionId: "owner", ownershipPhase: "provisional", }); }); test("#2028 heartbeat promotes only the exact provisional acquisition to ready", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 111, pidIncarnation: () => "linux:100", randomId: () => "owner", }); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "other", pid: 111, pidIncarnation: () => "linux:100", }), ).toBe(false); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 111, pidIncarnation: () => "linux:100", }), ).toBe(true); expect(JSON.parse(fs.readFileSync(daemonPaths(agentDir).state, "utf8"))).toMatchObject({ ownerId: "owner", acquisitionId: "owner", ownershipPhase: "ready", }); }); test("#2028 heartbeat contention never reports a provisional owner ready", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 111, pidIncarnation: () => "linux:100", randomId: () => "owner", }); const paths = daemonPaths(agentDir); fs.writeFileSync(paths.steal, "held"); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 111, pidIncarnation: () => "linux:100", stealRetries: 2, stealRetryDelayMs: 0, sleep: async () => undefined, }), ).toBe(false); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ ownershipPhase: "provisional" }); }); test("transition lock keeps an aged malformed crash marker blocked pending manual cleanup", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 111, pidIncarnation: () => "linux:100", randomId: () => "owner", }); const paths = daemonPaths(agentDir); fs.writeFileSync(paths.steal, ""); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", pid: 111, pidIncarnation: () => "linux:100", stealRetries: 0, }), ).toBe(false); fs.utimesSync(paths.steal, 0, 0); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", pid: 111, pidIncarnation: () => "linux:100", stealRetries: 1, stealRetryDelayMs: 0, }), ).toBe(false); expect(fs.readFileSync(paths.steal, "utf8")).toBe(""); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ ownershipPhase: "provisional" }); }); test("#2028 heartbeat retries through a released transition lock before publishing ready", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 111, pidIncarnation: () => "linux:100", randomId: () => "owner", }); const paths = daemonPaths(agentDir); fs.writeFileSync(paths.steal, "held"); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", acquisitionId: "owner", pid: 111, pidIncarnation: () => "linux:100", stealRetries: 1, stealRetryDelayMs: 0, sleep: async () => { fs.unlinkSync(paths.steal); }, }), ).toBe(true); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ ownershipPhase: "ready", pid: 111 }); }); test("#2028 malformed live schema or generation blocks without signaling or retaining a root", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: 0, roots: [123] as unknown as string[] }); const paths = daemonPaths(agentDir); const signals: Array<[number, string]> = []; let spawns = 0; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, pidAlive: () => true, pidIncarnation: () => "linux:100", sendSignal: (pid, signal) => signals.push([pid, signal]), spawn: () => { spawns++; return { unref() {} }; }, }, ), ).resolves.toBe("blocked_identity"); expect(signals).toEqual([]); expect(spawns).toBe(0); expect(fs.existsSync(paths.roots)).toBe(false); }); test("#2028 readiness is bounded when injected time does not advance", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir); const sleeps: number[] = []; await expect( waitForTelegramDaemonReady({ settings: s, ownerId: "fresh", tokenFingerprint: "e60b05c186ca", chatId: "42", now: () => 101, pidAlive: () => false, sleep: async ms => void sleeps.push(ms), waitStepMs: 1, timeoutMs: 3, }), ).resolves.toBe(false); expect(sleeps).toEqual([1, 1, 1]); }); test("servingEpoch predecessor reloads via a safe SIGTERM handoff", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); // An epoch-1 owner is otherwise fresh and live, but must converge before // this host can attach to it. writeLiveOwner(agentDir, { generation: 5, servingEpoch: undefined, heartbeatAt: Date.now() }); const paths = daemonPaths(agentDir); const alive = new Set([999, 4242]); const signals: Array<[number, string]> = []; let oldAliveAtSpawn: boolean | undefined; const spawns: Array<{ command: string; args: string[] }> = []; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, onSpawn: (pid, command, args) => { alive.add(pid); oldAliveAtSpawn = alive.has(999); spawns.push({ command, args }); }, }); const cwd = path.join(agentDir, "new-session"); const result = await ensureTelegramDaemonRunning( { settings: s, cwd, sessionId: "new-session" }, { pid: 4242, pidAlive: pid => alive.has(pid), pidIncarnation: () => "linux:100", processReference: pid => pid === 999 ? { incarnation: "linux:100", termination: "cooperative", signalRoot: sig => { signals.push([pid, sig]); if (sig === "SIGTERM") alive.delete(pid); }, } : undefined, sleep: child.sleep, spawn: child.spawn, }, ); expect(result).toBe("owner_spawned"); // Cooperative handoff: the old poller is SIGTERM'd and must be dead before a // replacement poller spawns (no Telegram getUpdates 409 overlap). expect(signals).toContainEqual([999, "SIGTERM"]); expect(signals.some(([, sig]) => sig === "SIGKILL")).toBe(false); expect(spawns).toHaveLength(1); expect(oldAliveAtSpawn).toBe(false); const after = JSON.parse(fs.readFileSync(paths.state, "utf8")); expect(after.ownerId).not.toBe("old"); expect(after.generation).toBe(DAEMON_GENERATION); // The new session's root is persisted so the replacement daemon serves it. expect(after.roots).toContain(path.join(cwd, ".skc", "state")); }); test("D6a: lower-generation predecessor stops polling before the epoch-2 successor is ready", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION - 1, servingEpoch: undefined, heartbeatAt: Date.now() }); const bot = new SinglePollerBotApi(); const alive = new Set([999, 4242]); const signals: Array<[number, string]> = []; let spawns = 0; let oldAliveAtSpawn: boolean | undefined; let successorPolling = false; bot.start("predecessor"); await Promise.resolve(); const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4244, onSpawn: () => { spawns++; oldAliveAtSpawn = alive.has(999); alive.add(4244); }, onReady: () => { if (!successorPolling) { successorPolling = true; bot.start("successor"); } }, }); const result = await ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, pidAlive: pid => alive.has(pid), pidIncarnation: () => "linux:100", processReference: pid => pid === 999 ? { incarnation: "linux:100", termination: "cooperative", signalRoot: sig => { signals.push([pid, sig]); if (sig === "SIGTERM") { alive.delete(pid); void bot.stop("predecessor"); } }, } : undefined, sleep: child.sleep, spawn: child.spawn, }, ); await Promise.resolve(); expect(result).toBe("reloaded"); expect(signals).toContainEqual([999, "SIGTERM"]); expect(spawns).toBe(1); expect(oldAliveAtSpawn).toBe(false); expect(bot.events).toEqual([ { type: "start", owner: "predecessor" }, { type: "end", owner: "predecessor" }, { type: "start", owner: "successor" }, ]); expect(bot.conflicts).toEqual([]); const after = JSON.parse(fs.readFileSync(daemonPaths(agentDir).state, "utf8")); expect(after).toMatchObject({ generation: DAEMON_GENERATION, servingEpoch: SERVING_EPOCH, ownershipPhase: "ready", }); await bot.stop("successor"); }); test("D6b: SIGKILL fences the poller before the successor becomes ready", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 101; const bot = new SinglePollerBotApi(); const alive = new Set([999]); const signals: Array<[number, NodeJS.Signals]> = []; let oldAliveAtSpawn: boolean | undefined; let successorPolling = false; writeLiveOwner(agentDir, { generation: DAEMON_GENERATION, heartbeatAt: 100 }); bot.start("predecessor"); await Promise.resolve(); const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4244, now: () => now, onSpawn: () => { oldAliveAtSpawn = alive.has(999); alive.add(4244); }, onReady: () => { if (!successorPolling) { successorPolling = true; bot.start("successor"); } }, }); const result = await new TelegramDaemonController(s, { platform: "linux", now: () => now, ownerPid: 4242, pidAlive: pid => alive.has(pid), pidIncarnation: () => "linux:100", processReference: pid => pid === 999 ? { incarnation: "linux:100", termination: "cooperative", signalRoot: signal => { signals.push([pid, signal]); if (signal === "SIGKILL") { alive.delete(pid); void bot.stop("predecessor"); } }, } : undefined, spawn: child.spawn, sleep: async ms => { now += ms; if (!alive.has(999)) await child.sleep(); }, waitStepMs: 1, readinessTimeoutMs: 2, }).reload({ force: true, gracefulTimeoutMs: 1, killTimeoutMs: 1 }); await Promise.resolve(); expect(result.ok).toBe(true); expect(signals).toEqual([ [999, "SIGTERM"], [999, "SIGKILL"], ]); expect(oldAliveAtSpawn).toBe(false); const predecessorStoppedAt = bot.events.findIndex(event => event.type === "end" && event.owner === "predecessor"); const successorStartedAt = bot.events.findIndex(event => event.type === "start" && event.owner === "successor"); expect(predecessorStoppedAt).toBeGreaterThanOrEqual(0); expect(successorStartedAt).toBeGreaterThan(predecessorStoppedAt); expect(bot.conflicts.length).toBeLessThanOrEqual(1); // A SIGKILL handoff may have bounded transient 409s before death, but the // single-poller bridge must see no overlap once the captured owner is dead. expect(bot.events.slice(predecessorStoppedAt + 1).some(event => event.type === "conflict")).toBe(false); await bot.stop("successor"); }); test("cooldown waits for a provisional successor's ready provenance before attaching", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const now = 1_000; const paths = daemonPaths(agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION - 1, servingEpoch: undefined, heartbeatAt: now }); fs.writeFileSync( path.join(paths.dir, "telegram-daemon.reload-attempt.json"), JSON.stringify({ lastReloadAt: now - 1, ownerId: "old", targetGeneration: DAEMON_GENERATION }), ); const provisionalSuccessor = liveOwnerState({ ownerId: "successor", acquisitionId: "successor-acquisition", ownershipPhase: "provisional", generation: DAEMON_GENERATION, servingEpoch: SERVING_EPOCH, heartbeatAt: now, }); let nowCalls = 0; const cooldownNow = () => { nowCalls++; // The fourth clock read is reloadNow, after the cooldown callback has read // the reload-required predecessor and before its first cooldown poll. if (nowCalls === 4) { fs.writeFileSync(paths.state, JSON.stringify(provisionalSuccessor)); fs.writeFileSync( paths.lock, JSON.stringify({ pid: provisionalSuccessor.pid, incarnation: provisionalSuccessor.incarnation, ownerId: provisionalSuccessor.ownerId, acquisitionId: provisionalSuccessor.acquisitionId, startedAt: provisionalSuccessor.startedAt, }), ); } return now; }; const sleeps: number[] = []; const result = await ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "cooldown-successor"), sessionId: "cooldown-successor" }, { now: cooldownNow, pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", readinessTimeoutMs: 1, waitStepMs: 1, sleep: async ms => { sleeps.push(ms); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ ownershipPhase: "provisional", }); fs.writeFileSync(paths.state, JSON.stringify({ ...provisionalSuccessor, ownershipPhase: "ready" })); fs.writeFileSync( paths.lock, JSON.stringify({ pid: provisionalSuccessor.pid, incarnation: provisionalSuccessor.incarnation, ownerId: provisionalSuccessor.ownerId, acquisitionId: provisionalSuccessor.acquisitionId, startedAt: provisionalSuccessor.startedAt, }), ); }, spawn: () => { throw new Error("ready successor should attach during cooldown"); }, }, ); expect(result).toBe("attached"); // Platform-dependent code paths read the injected clock a different // number of times (6 on POSIX, 5 on Windows). The load-bearing facts are: // the successor swap ran (read #4 happened), exactly one cooldown poll // slept, and the provisional owner was only attached after turning ready. expect(nowCalls).toBeGreaterThanOrEqual(4); expect(sleeps).toEqual([1]); }); test("cooldown blocks a ready epoch-1 owner with matching provenance", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const now = 1_000; const paths = daemonPaths(agentDir); const attemptPath = path.join(paths.dir, "telegram-daemon.reload-attempt.json"); const signals: Array<[number, NodeJS.Signals]> = []; writeLiveOwner(agentDir, { generation: DAEMON_GENERATION - 1, servingEpoch: undefined, heartbeatAt: now }); fs.writeFileSync( attemptPath, JSON.stringify({ lastReloadAt: now - 1, ownerId: "old", targetGeneration: DAEMON_GENERATION }), ); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "cooldown-ready"), sessionId: "cooldown-ready" }, { now: () => now, pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: (pid, signal) => signals.push([pid, signal]), sleep: async () => undefined, waitStepMs: 1, readinessTimeoutMs: 1, spawn: () => { throw new Error("cooldown must not replace the epoch-1 predecessor"); }, }, ), ).resolves.toBe("blocked_identity"); expect(signals).toEqual([]); expect(JSON.parse(fs.readFileSync(attemptPath, "utf8"))).toMatchObject({ lastReloadAt: now - 1, targetGeneration: DAEMON_GENERATION, }); }); test("servingEpoch reload fails when the stale owner never exits", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: 5, servingEpoch: undefined, heartbeatAt: 1_000 }); let nowMs = 1_000; let spawns = 0; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, now: () => nowMs, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: () => {}, sleep: async ms => { nowMs += ms; }, waitStepMs: 100, spawn: () => { spawns++; return { unref() {} }; }, }, ), ).rejects.toThrow("Unable to replace stale Telegram daemon"); expect(spawns).toBe(0); }); test("reload failure restores a new session root registration", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 1_000; writeLiveOwner(agentDir, { servingEpoch: undefined, heartbeatAt: now }); const cwd = path.join(agentDir, "new-session"); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd, sessionId: "new" }, { now: () => now, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: () => undefined, sleep: async () => { now += 8_000; }, waitStepMs: 8_000, }, ), ).rejects.toThrow("Unable to replace stale Telegram daemon"); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ roots: [], sessions: {} }); }); test("reload failure restores a preexisting session root registration", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const oldCwd = path.join(agentDir, "old-session"); const newCwd = path.join(agentDir, "new-session"); await registerNotificationRoot({ settings: s, cwd: oldCwd, sessionId: "session" }); let now = 1_000; writeLiveOwner(agentDir, { servingEpoch: undefined, heartbeatAt: now }); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: newCwd, sessionId: "session" }, { now: () => now, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", sendSignal: () => undefined, sleep: async () => { now += 8_000; }, waitStepMs: 8_000, }, ), ).rejects.toThrow("Unable to replace stale Telegram daemon"); const registry = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")); expect(registry).toMatchObject({ roots: [path.join(oldCwd, ".skc", "state")], managedRoots: [path.join(oldCwd, ".skc", "state")], sessions: { session: path.join(oldCwd, ".skc", "state") }, }); }); test("detailed ensure refuses a stale-heartbeat live owner instead of attaching", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { heartbeatAt: 100 }); const signals: Array<[number, string]> = []; let spawns = 0; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, now: () => 100_000, pidAlive: () => true, pidIncarnation: () => "linux:100", sendSignal: (pid, signal) => signals.push([pid, signal]), readinessTimeoutMs: 0, spawn: () => { spawns++; return { unref() {} }; }, }, ), ).resolves.toBe("blocked_identity"); expect(signals).toEqual([]); expect(spawns).toBe(0); }); test("v0.10.2 generation 3 is physically live but requires a current-generation handoff", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const legacyGeneration = 3; const state = liveOwnerState({ generation: legacyGeneration, heartbeatAt: 100 }); writeLiveOwner(agentDir, { generation: legacyGeneration, heartbeatAt: 100 }); expect(legacyGeneration).toBe(3); expect( isFreshLiveOwner({ state, now: 101, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", }), ).toBe(true); expect( isCurrentCompatibleOwner({ state, now: 101, tokenFingerprint: "e60b05c186ca", chatId: "42", pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", }), ).toBe(true); const ownership = await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", now: () => 101, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", }); expect(ownership).toEqual({ acquired: false, attached: false, blocked: true }); }); test("attests the literal v0.10.2 generation 3 owner before requiring reload", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const fingerprint = tokenFingerprint("123456:secret-token"); const legacyState = { pid: 999, ownerId: "999-v010-owner", tokenFingerprint: fingerprint, chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, `${JSON.stringify(legacyState, null, 2)}\n`); fs.writeFileSync(paths.lock, ""); fs.utimesSync(paths.lock, new Date(0), new Date(0)); const ownershipInput = { settings: s, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, ownerId: "current-owner", pidAlive: (pid: number) => pid === 999, pidIncarnation: (pid: number) => `linux:${pid}`, }; await expect(acquireDaemonOwnership({ ...ownershipInput, now: () => 100_000 })).resolves.toEqual({ acquired: false, attached: false, provisional: true, }); await expect( readAttestedLegacyDaemonOwner({ settings: s, now: () => 100_000, pidIncarnation: ownershipInput.pidIncarnation, tokenFingerprint: fingerprint, chatId: "42", }), ).resolves.toBeUndefined(); const advancedState = `${JSON.stringify({ ...legacyState, heartbeatAt: 201 }, null, 2)}\n`; fs.writeFileSync(paths.state, advancedState); await expect(acquireDaemonOwnership({ ...ownershipInput, now: () => 100_001 })).resolves.toEqual({ acquired: false, attached: false, legacyReloadRequired: true, reloadRequired: true, }); await expect( readAttestedLegacyDaemonOwner({ settings: s, now: () => 100_001, pidIncarnation: ownershipInput.pidIncarnation, tokenFingerprint: fingerprint, chatId: "42", }), ).resolves.toMatchObject({ state: { pid: 999, heartbeatAt: 201 }, incarnation: "linux:999" }); fs.utimesSync(paths.lock, new Date(1), new Date(1)); await expect( readAttestedLegacyDaemonOwner({ settings: s, now: () => 100_001, pidIncarnation: ownershipInput.pidIncarnation, tokenFingerprint: fingerprint, chatId: "42", }), ).resolves.toBeUndefined(); expect(fs.readFileSync(paths.state, "utf8")).toBe(advancedState); expect(fs.readFileSync(paths.lock, "utf8")).toBe(""); }); test("attested legacy reload aborts when configuration changes while opening process authority", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const fingerprint = tokenFingerprint("123456:secret-token"); const legacyState = { pid: 999, ownerId: "999-v010-owner", tokenFingerprint: fingerprint, chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, `${JSON.stringify(legacyState, null, 2)}\n`); fs.writeFileSync(paths.lock, ""); fs.utimesSync(paths.lock, new Date(0), new Date(0)); const ownershipInput = { settings: s, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, ownerId: "current-owner", pidAlive: (pid: number) => pid === 999, pidIncarnation: (pid: number) => `linux:${pid}`, }; await acquireDaemonOwnership({ ...ownershipInput, now: () => 100_000 }); fs.writeFileSync(paths.state, `${JSON.stringify({ ...legacyState, heartbeatAt: 201 }, null, 2)}\n`); await expect(acquireDaemonOwnership({ ...ownershipInput, now: () => 100_001 })).resolves.toMatchObject({ reloadRequired: true, legacyReloadRequired: true, }); const signals: NodeJS.Signals[] = []; const result = await new TelegramDaemonController(s, { platform: "linux", now: () => 100_001, pidAlive: pid => pid === 999, pidIncarnation: pid => `linux:${pid}`, processReference: () => { s.set("notifications.telegram.chatId", "43"); return { incarnation: "linux:999", termination: "cooperative", signalRoot: signal => signals.push(signal), }; }, }).reloadForGenerationUpgrade({}, true); expect(result.outcome).toBe("failed"); expect(result.operation.message).toContain("ownership changed"); expect(signals).toEqual([]); }); test("accepts only canonical historical parent bytes for legacy attestation", async () => { const fingerprint = tokenFingerprint("123456:secret-token"); const parent = { pid: 999, ownerId: "999-v010-owner", tokenFingerprint: fingerprint, chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, }; const canonical = `${JSON.stringify(parent, null, 2)}\n`; const { ownerId, ...parentWithoutOwner } = parent; const rejected = [ ["extra key", `${JSON.stringify({ ...parent, unexpected: true }, null, 2)}\n`], ["reordered keys", `${JSON.stringify({ ownerId, ...parentWithoutOwner }, null, 2)}\n`], ["compact JSON", JSON.stringify(parent)], ["modern hybrid", `${JSON.stringify({ ...parent, incarnation: "linux:100" }, null, 2)}\n`], ] as const; for (const [name, stateBytes] of rejected) { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, stateBytes); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: pid => `linux:${pid}`, now: () => 100_000, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.state, "utf8"), name).toBe(stateBytes); expect(fs.existsSync(`${paths.state}.legacy-migration.json`), name).toBe(false); } const zeroLockDir = tempAgentDir(); const zeroLockSettings = setPrivateAgentDir(settings(zeroLockDir), zeroLockDir); const zeroLockPaths = daemonPaths(zeroLockDir); fs.mkdirSync(zeroLockPaths.dir, { recursive: true }); fs.writeFileSync(zeroLockPaths.state, canonical); fs.writeFileSync(zeroLockPaths.lock, ""); await expect( acquireDaemonOwnership({ settings: zeroLockSettings, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: pid => `linux:${pid}`, now: () => 100_000, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.existsSync(`${zeroLockPaths.state}.legacy-migration.json`)).toBe(false); const validDir = tempAgentDir(); const validSettings = setPrivateAgentDir(settings(validDir), validDir); const validPaths = daemonPaths(validDir); fs.mkdirSync(validPaths.dir, { recursive: true }); fs.writeFileSync(validPaths.state, canonical); await expect( acquireDaemonOwnership({ settings: validSettings, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: pid => `linux:${pid}`, now: () => 100_000, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.existsSync(`${validPaths.state}.legacy-migration.json`)).toBe(false); }); test("keeps a live v0.10.2 owner blocked when its legacy lock does not match", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const fingerprint = tokenFingerprint("123456:secret-token"); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, ownerId: "999-v010-owner", tokenFingerprint: fingerprint, chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }), ); fs.writeFileSync(paths.lock, "not-a-release-lock"); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, ownerId: "current-owner", now: () => 100_000, pidAlive: pid => pid === 999, pidIncarnation: pid => `linux:${pid}`, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); }); test("detailed ensure reloads a live v0.10.2 owner after attestation", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const fingerprint = tokenFingerprint("123456:secret-token"); const legacyState = { pid: 999, ownerId: "999-v010-owner", tokenFingerprint: fingerprint, chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, `${JSON.stringify(legacyState, null, 2)}\n`); fs.writeFileSync(paths.lock, ""); fs.utimesSync(paths.lock, new Date(0), new Date(0)); let now = 100_000; let legacyHeartbeatAdvanced = false; const alive = new Set([999, 4242]); const signals: Array<[number, string]> = []; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4244, now: () => now, onSpawn: pid => alive.add(pid), }); const result = await ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { platform: "linux", pid: 4242, now: () => now, pidAlive: pid => alive.has(pid), pidIncarnation: () => "linux:100", processReference: pid => pid === 999 ? { incarnation: "linux:100", termination: "cooperative", signalRoot: signal => { signals.push([pid, signal]); if (signal === "SIGTERM") alive.delete(pid); }, } : undefined, sleep: async ms => { now += ms; if (!legacyHeartbeatAdvanced) { legacyHeartbeatAdvanced = true; fs.writeFileSync(paths.state, `${JSON.stringify({ ...legacyState, heartbeatAt: 201 }, null, 2)}\n`); return; } await child.sleep(); }, spawn: child.spawn, readinessTimeoutMs: 2, waitStepMs: 1, }, ); expect(result).toBe("reloaded"); expect(signals).toContainEqual([999, "SIGTERM"]); }); test("Windows legacy cooperative handoff remains closed without spawning", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const state = { pid: 999, ownerId: "v010-owner", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, `${JSON.stringify(state, null, 2)}\n`); fs.writeFileSync(paths.lock, ""); const stateBytes = fs.readFileSync(paths.state, "utf8"); const lockBytes = fs.readFileSync(paths.lock, "utf8"); let spawns = 0; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "windows-v010" }, { platform: "win32", pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", spawn: () => { spawns++; return { pid: 4243, unref() {} }; }, }, ), ).resolves.toBe("blocked_identity"); expect(spawns).toBe(0); expect(fs.readFileSync(paths.state, "utf8")).toBe(stateBytes); expect(fs.readFileSync(paths.lock, "utf8")).toBe(lockBytes); }); test("Windows replaces a dead v0.10 parent owner instead of preserving its stale fence", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const state = { pid: 999, ownerId: "dead-v010-owner", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }; fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync(paths.state, `${JSON.stringify(state, null, 2)}\n`); fs.writeFileSync(paths.lock, ""); let now = 1_000; let spawns = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, now: () => now, onSpawn: () => spawns++, }); const result = await ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "windows-dead-v010" }, { platform: "win32", pid: 4242, now: () => now, pidAlive: pid => pid === 4243, pidIncarnation: () => "linux:100", spawn: child.spawn, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { now++; await child.sleep(); }, }, ); expect(result).toBe("spawned"); expect(spawns).toBe(1); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4243, ownershipPhase: "ready", }); }); test("blocks a promoted generation-3 hybrid with a zero-byte lock before controller routing", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, incarnation: "linux:100", ownerId: "promoted-generation-3", acquisitionId: "promoted-generation-3", ownershipPhase: "ready", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 3, }), ); fs.writeFileSync(paths.lock, ""); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); expect(fs.readFileSync(paths.lock, "utf8")).toBe(""); }); test("rejects numeric and hybrid pre-incarnation parent records", async () => { for (const state of [{ generation: 2 }, { generation: 19 }] as const) { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, `${JSON.stringify({ pid: 999, ownerId: "invalid-parent", tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, ...state, })}\n`, ); fs.writeFileSync(paths.lock, ""); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pid: 4242, pidAlive: pid => pid === 999, pidIncarnation: () => "linux:100", }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); } }); test("keeps post-incarnation generation records without provenance blocked", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const fingerprint = tokenFingerprint("123456:secret-token"); fs.mkdirSync(paths.dir, { recursive: true }); fs.writeFileSync( paths.state, JSON.stringify({ pid: 999, ownerId: "999-incomplete-owner", tokenFingerprint: fingerprint, chatId: "42", startedAt: 100, heartbeatAt: 200, roots: [], version: DAEMON_VERSION, generation: 5, }), ); await expect( acquireDaemonOwnership({ settings: s, tokenFingerprint: fingerprint, chatId: "42", pid: 4242, ownerId: "current-owner", now: () => 100_000, pidAlive: pid => pid === 999, pidIncarnation: pid => `linux:${pid}`, }), ).resolves.toEqual({ acquired: false, attached: false, blocked: true }); }); test("#2028 binds a provisional launcher PID after a briefly contended transition lock", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, now: () => now, onSpawn: () => fs.writeFileSync(paths.steal, "held"), }); let sleeps = 0; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "bind-after-contention" }, { pid: 4242, now: () => now, pidAlive: pid => pid === 4243, pidIncarnation: () => "linux:100", spawn: child.spawn, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { if (++sleeps === 1) fs.unlinkSync(paths.steal); else { now++; await child.sleep(); } }, }, ), ).resolves.toBe("spawned"); expect(sleeps).toBeGreaterThanOrEqual(2); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4243, ownershipPhase: "ready", }); }); test("auto-reconciles a stale dead-owner lock and spawns a fresh owner without manual recovery", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); // A prior owner (pid 111) crashed but left its state + lock behind. On Linux its // PID is briefly reused, so the first ownership probe sees it alive and refuses // with an ownership block; it then dies. This is exactly the case that used to // hard-block SDK startup until a manual `skc notify recovery`. writeLiveOwner(agentDir, { pid: 111, ownerId: "crashed", acquisitionId: "crashed", tokenFingerprint: "stale-token", generation: DAEMON_GENERATION, }); const endpoint = path.join(agentDir, ".skc", "state", "sdk", "reconciled.json"); fs.mkdirSync(path.dirname(endpoint), { recursive: true }); fs.writeFileSync(endpoint, JSON.stringify({ url: "ws://dead", token: "dead", pid: 111 })); let now = 1_000; let staleProbes = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, now: () => now }); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "reconciled" }, { pid: 4242, now: () => now, pidAlive: pid => (pid === 111 ? staleProbes++ === 0 : pid === 4243), pidIncarnation: () => "linux:100", spawn: child.spawn, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { now++; await child.sleep(); }, }, ), ).resolves.toBe("spawned"); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4243, ownershipPhase: "ready", }); expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toMatchObject({ pid: 4243 }); expect(fs.existsSync(endpoint)).toBe(false); }); test("Windows production preflight clears a lockless dead owner and dead endpoints before one ready replacement", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); writeLiveOwner(agentDir, { pid: 111, ownerId: "crashed", acquisitionId: "crashed", tokenFingerprint: "stale-token", generation: DAEMON_GENERATION, }); fs.unlinkSync(paths.lock); const endpointDir = path.join(agentDir, ".skc", "state", "sdk"); const endpoints = [path.join(endpointDir, "dead-a.json"), path.join(endpointDir, "dead-b.json")]; fs.mkdirSync(endpointDir, { recursive: true }); for (const endpoint of endpoints) fs.writeFileSync(endpoint, JSON.stringify({ url: "ws://dead", token: "dead", pid: 111 })); let now = 1_000; let spawns = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, now: () => now, onSpawn: () => spawns++, }); await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "windows-lockless-recovery" }, { platform: "win32", pid: 4242, now: () => now, pidAlive: pid => pid === 4243, pidIncarnation: () => "linux:100", spawn: child.spawn, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { now++; await child.sleep(); }, }, ), ).resolves.toBe("spawned"); expect(spawns).toBe(1); expect(endpoints.every(endpoint => !fs.existsSync(endpoint))).toBe(true); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4243, ownershipPhase: "ready", }); }); test("Windows production preflight fails closed on unsafe lockless-owner endpoint evidence", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); writeLiveOwner(agentDir, { pid: 111, ownerId: "crashed", acquisitionId: "crashed", generation: DAEMON_GENERATION, }); fs.unlinkSync(paths.lock); const endpoint = path.join(agentDir, ".skc", "state", "sdk", "unproven.json"); fs.mkdirSync(path.dirname(endpoint), { recursive: true }); fs.writeFileSync(endpoint, JSON.stringify({ url: "ws://unproven", token: "unproven" })); let spawns = 0; const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {}); try { await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "windows-unsafe-recovery" }, { platform: "win32", pid: 4242, pidAlive: () => false, pidIncarnation: () => "linux:100", spawn: () => { spawns++; return { pid: 4243, unref() {} }; }, }, ), ).resolves.toBe("blocked_identity"); expect(spawns).toBe(0); expect(fs.existsSync(endpoint)).toBe(true); expect(warnSpy).toHaveBeenCalledWith( expect.stringContaining("startup recovery unsafe (unsafe-endpoint); run `skc notify recovery`"), ); } finally { warnSpy.mockRestore(); } }); test("startup recovery retains an endpoint successor installed during exact stale cleanup", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); const endpoint = path.join(agentDir, ".skc", "state", "sdk", "raced.json"); const successor = JSON.stringify({ url: "ws://live", token: "live", pid: 4243 }); fs.mkdirSync(path.dirname(endpoint), { recursive: true }); fs.writeFileSync(endpoint, JSON.stringify({ url: "ws://dead", token: "dead", pid: 111 })); writeLiveOwner(agentDir, { pid: 111, ownerId: "crashed", acquisitionId: "crashed", tokenFingerprint: "stale-token", generation: DAEMON_GENERATION, }); let now = 1_000; let staleProbes = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, now: () => now }); const recoveryFs: TelegramDaemonFs = { ...(fs.promises as unknown as TelegramDaemonFs), ...exactTransitionFs(file => { if (file === endpoint) { fs.unlinkSync(file); fs.writeFileSync(file, successor); } }), }; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "raced" }, { fs: recoveryFs, pid: 4242, now: () => now, pidAlive: pid => (pid === 111 ? staleProbes++ === 0 : pid === 4243), pidIncarnation: () => "linux:100", spawn: child.spawn, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { now++; await child.sleep(); }, }, ), ).resolves.toBe("blocked_identity"); expect(fs.readFileSync(endpoint, "utf8")).toBe(successor); expect(JSON.parse(fs.readFileSync(paths.lock, "utf8"))).toMatchObject({ pid: 111, ownerId: "crashed" }); }); test("#2028 retires an unbound launcher reservation after bounded bind contention so ensure can recover", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 0; let spawns = 0; let ownerId = ""; let sleeps = 0; const deps = { pid: 4242, now: () => now, pidAlive: (pid: number) => pid === 4244, pidIncarnation: () => "linux:100", spawn: (_command: string, args: string[]) => { spawns++; ownerId = args[args.indexOf("--owner-id") + 1]!; if (spawns === 1) fs.writeFileSync(paths.steal, "held"); return { pid: spawns === 1 ? 4243 : 4244, unref() {} }; }, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { now++; if (++sleeps === 6) { fs.unlinkSync(paths.steal); return; } if (spawns === 2) { await renewDaemonHeartbeat({ settings: s, ownerId, acquisitionId: ownerId, pid: 4244, pidIncarnation: () => "linux:100", now: () => now, }); } }, }; await expect( ensureTelegramDaemonRunningDetailed({ settings: s, cwd: agentDir, sessionId: "failed-bind" }, deps), ).rejects.toThrow("Telegram daemon did not become ready after spawning"); expect(sleeps).toBe(6); expect(fs.existsSync(paths.lock)).toBe(false); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4242, ownershipPhase: "retired", }); await expect( ensureTelegramDaemonRunningDetailed({ settings: s, cwd: agentDir, sessionId: "recovered-bind" }, deps), ).resolves.toBe("spawned"); expect(spawns).toBe(2); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4244, ownershipPhase: "ready", }); }); test("#2028 accepts a ready child that publishes before the delayed bind takes the transition lock", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 0; let ownerId = ""; await expect( ensureTelegramDaemonRunningDetailed( { settings: s, cwd: agentDir, sessionId: "ready-before-bind" }, { pid: 4242, now: () => now, pidAlive: pid => pid === 4243, pidIncarnation: () => "linux:100", spawn: (_command, args) => { ownerId = args[args.indexOf("--owner-id") + 1]!; fs.writeFileSync(paths.steal, "held"); return { pid: 4243, unref() {} }; }, readinessTimeoutMs: 1, waitStepMs: 1, sleep: async () => { fs.unlinkSync(paths.steal); now++; await renewDaemonHeartbeat({ settings: s, ownerId, acquisitionId: ownerId, pid: 4243, pidIncarnation: () => "linux:100", now: () => now, }); }, }, ), ).resolves.toBe("spawned"); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4243, ownershipPhase: "ready", }); }); test("child ready publication wins the readiness-versus-retire race", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 0; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 4242, pidIncarnation: () => "linux:100", randomId: () => "race-child", now: () => now, }); const ready = await confirmTelegramDaemonSpawn({ settings: s, spawned: { result: "owner_spawned", acquisition: Object.freeze({ ownerId: "race-child", acquisitionId: "race-child", launcherPid: 4242, pid: 4243, }), runtime: { mode: "compiled", execPath: process.execPath, reloadPicksUpSourceEdits: false }, warnings: [], }, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 4242, now: () => now, pidAlive: pid => pid === 4243, pidIncarnation: () => "linux:100", waitStepMs: 1, timeoutMs: 1, sleep: async () => { now++; await renewDaemonHeartbeat({ settings: s, ownerId: "race-child", acquisitionId: "race-child", pid: 4243, pidIncarnation: () => "linux:100", now: () => now, }); }, }); expect(ready).toBe(true); expect(JSON.parse(fs.readFileSync(daemonPaths(agentDir).state, "utf8"))).toMatchObject({ pid: 4243, ownershipPhase: "ready", }); }); test("no-PID spawn never accepts a ready-like launcher publication and retires its reservation", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 0; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 4242, pidIncarnation: () => "linux:100", randomId: () => "no-child-pid", now: () => now, }); const ready = await confirmTelegramDaemonSpawn({ settings: s, spawned: { result: "owner_spawned", acquisition: Object.freeze({ ownerId: "no-child-pid", acquisitionId: "no-child-pid", launcherPid: 4242 }), runtime: { mode: "compiled", execPath: process.execPath, reloadPicksUpSourceEdits: false }, warnings: [], }, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 4242, now: () => now, pidAlive: pid => pid === 4242, pidIncarnation: () => "linux:100", waitStepMs: 1, timeoutMs: 1, sleep: async () => { now++; await renewDaemonHeartbeat({ settings: s, ownerId: "no-child-pid", acquisitionId: "no-child-pid", pid: 4242, pidIncarnation: () => "linux:100", now: () => now, }); }, }); expect(ready).toBe(false); expect(fs.existsSync(paths.lock)).toBe(false); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4242, ownershipPhase: "retired", }); }); test("no-caller-PID readiness follows only the exact child owner, acquisition, and incarnation", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 0; await acquireDaemonOwnership({ settings: s, tokenFingerprint: "e60b05c186ca", chatId: "42", pid: 4242, pidIncarnation: () => "linux:100", randomId: () => "exact-owner", now: () => now, }); const ready = await waitForTelegramDaemonReady({ settings: s, ownerId: "exact-owner", acquisitionId: "exact-owner", tokenFingerprint: "e60b05c186ca", chatId: "42", now: () => now, pidAlive: pid => pid === 4243, pidIncarnation: pid => (pid === 4243 ? "linux:106" : "linux:100"), timeoutMs: 4, waitStepMs: 1, sleep: async () => { now++; expect( await renewDaemonHeartbeat({ settings: s, ownerId: "wrong-owner", acquisitionId: "exact-owner", pid: 4243, pidIncarnation: () => "linux:100", now: () => now, }), ).toBe(false); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "exact-owner", acquisitionId: "wrong-acquisition", pid: 4243, pidIncarnation: () => "linux:100", now: () => now, }), ).toBe(false); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "exact-owner", acquisitionId: "exact-owner", pid: 4243, pidIncarnation: () => "linux:106", now: () => now, }), ).toBe(true); }, }); expect(ready).toBe(true); expect(JSON.parse(fs.readFileSync(daemonPaths(agentDir).state, "utf8"))).toMatchObject({ ownerId: "exact-owner", acquisitionId: "exact-owner", pid: 4243, incarnation: "linux:106", generation: DAEMON_GENERATION, ownershipPhase: "ready", }); }); test("provisional retirement cannot release a successor that wins the ownership race", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { ownerId: "successor", pid: 4343, generation: DAEMON_GENERATION, heartbeatAt: 100, }); const paths = daemonPaths(agentDir); await expect( retireProvisionalDaemonOwnership({ settings: s, ownerId: "provisional", pid: 4242, pidIncarnation: () => "linux:100", now: () => 101, }), ).resolves.toBe(false); expect(fs.existsSync(paths.lock)).toBe(true); const successor = JSON.parse(fs.readFileSync(paths.state, "utf8")); expect(successor).toMatchObject({ ownerId: "successor", pid: 4343 }); expect(successor).not.toHaveProperty("stoppedAt"); }); test("failed provisional startup is retired before a later current owner can become ready", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); let now = 0; let provisionalAlive = false; let replacementAlive = false; let spawns = 0; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, onSpawn: () => (replacementAlive = true), }); const deps = { pid: 4242, now: () => now, pidAlive: (pid: number) => (pid === 4242 && provisionalAlive) || (pid === 4243 && replacementAlive), pidIncarnation: () => "linux:100", spawn: (...args: Parameters) => { spawns++; return spawns === 1 ? { unref() {} } : child.spawn(...args); }, sleep: async () => { now += 8_000; await child.sleep(); }, waitStepMs: 8_000, }; const first = ensureTelegramDaemonRunningDetailed({ settings: s, cwd: agentDir, sessionId: "first" }, deps); const failure = await first.catch(error => error); expect(failure).toBeInstanceOf(Error); expect((failure as Error).message).toBe("Telegram daemon did not become ready after spawning"); expect((failure as Error).message).not.toContain("secret-token"); expect(spawns).toBe(1); expect(fs.existsSync(paths.lock)).toBe(false); expect(JSON.parse(fs.readFileSync(paths.state, "utf8"))).toMatchObject({ pid: 4242, ownershipPhase: "retired", stoppedAt: expect.any(Number), }); provisionalAlive = true; await expect( ensureTelegramDaemonRunningDetailed({ settings: s, cwd: agentDir, sessionId: "second" }, deps), ).resolves.toBe("spawned"); expect(spawns).toBe(2); const replacement = JSON.parse(fs.readFileSync(paths.state, "utf8")); expect(replacement).toMatchObject({ generation: DAEMON_GENERATION, pid: 4243 }); expect(replacement).not.toHaveProperty("stoppedAt"); }); test("#2028 ensureTelegramDaemonRunning reuses a current-generation live owner without a reload", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION, heartbeatAt: Date.now() }); const paths = daemonPaths(agentDir); const signals: Array<[number, string]> = []; let spawns = 0; const result = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "new-session"), sessionId: "new-session" }, { pid: 4242, pidAlive: () => true, pidIncarnation: () => "linux:100", sendSignal: (pid, sig) => signals.push([pid, sig]), sleep: async () => undefined, spawn: () => { spawns++; return { unref() {} }; }, }, ); expect(result).toBe("attached"); expect(signals).toHaveLength(0); expect(spawns).toBe(0); // Still the original owner; the new session's root is registered onto it. expect(JSON.parse(fs.readFileSync(paths.state, "utf8")).ownerId).toBe("old"); expect(fs.existsSync(paths.roots)).toBe(true); }); test("ensure converges after a fenced last-root stop races attach-before-registration", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); writeLiveOwner(agentDir, { generation: DAEMON_GENERATION, heartbeatAt: Date.now() }); const stoppedRegistration = await registerNotificationRoot({ settings: s, cwd: path.join(agentDir, "stopped-session"), sessionId: "stopped-session", }); const unregistered = await unregisterNotificationRoot({ settings: s, cwd: path.join(agentDir, "stopped-session"), sessionId: "stopped-session", registrationToken: stoppedRegistration.token, }); if (!unregistered.registryFingerprint) throw new Error("Expected empty root registry fingerprint"); let oldOwnerAlive = true; let replacementAlive = false; const stopOwnsRoots = Promise.withResolvers(); const resumeStop = Promise.withResolvers(); const registrationBlocked = Promise.withResolvers(); const resumeRegistration = Promise.withResolvers(); const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 4243, onSpawn: () => (replacementAlive = true), }); const fencedStop = withNotificationRootRegistryFence({ settings: s, registryFingerprint: unregistered.registryFingerprint, action: async () => { stopOwnsRoots.resolve(); await resumeStop.promise; oldOwnerAlive = false; await releaseDaemonOwnership({ settings: s, ownerId: "old", acquisitionId: "old", pid: 999, generation: DAEMON_GENERATION, pidIncarnation: () => "linux:100", }); }, }); await stopOwnsRoots.promise; const realSleep = Bun.sleep.bind(Bun); const sleepSpy = vi.spyOn(Bun, "sleep").mockImplementation((duration?: number | Date) => { if (duration === 100) { registrationBlocked.resolve(); return resumeRegistration.promise; } return realSleep(duration ?? 0); }); try { const ensuring = ensureTelegramDaemonRunningDetailed( { settings: s, cwd: path.join(agentDir, "replacement-session"), sessionId: "replacement-session" }, { pid: 4242, pidAlive: pid => (pid === 999 && oldOwnerAlive) || pid === 4242 || (pid === 4243 && replacementAlive), pidIncarnation: () => "linux:100", spawn: child.spawn, sleep: child.sleep, }, ); await registrationBlocked.promise; resumeStop.resolve(); expect(await fencedStop).toBe(true); resumeRegistration.resolve(); expect(await ensuring).toBe("spawned"); const replacement = await readDaemonState(s); expect(replacement).toMatchObject({ pid: 4243, ownershipPhase: "ready", generation: DAEMON_GENERATION }); expect( isCurrentCompatibleOwner({ state: replacement, now: Date.now(), tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pidAlive: pid => pid === 4243 && replacementAlive, pidIncarnation: () => "linux:100", }), ).toBe(true); expect(JSON.parse(fs.readFileSync(paths.roots, "utf8"))).toMatchObject({ sessions: { "replacement-session": path.join(agentDir, "replacement-session", ".skc", "state") }, }); } finally { sleepSpy.mockRestore(); } }); test("idle self-exit after timeout releases ownership", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); let now = 0; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), idleTimeoutMs: 10, now: () => (now += 11), setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, }); await daemon.run(); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(false); }); test("runDaemonInternal rewrites persisted owner pid to daemon process pid", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pid: 111, pidIncarnation: () => "linux:100", randomId: () => "owner", allowPidRebind: true, }); class OneShotDaemon extends TelegramNotificationDaemon { #options: TelegramDaemonOptions; constructor(options: TelegramDaemonOptions) { super(options); this.#options = options; } override async run(): Promise { await renewDaemonHeartbeat({ settings: this.#options.settings, ownerId: this.#options.ownerId, tokenFingerprint: tokenFingerprint("123456:secret-token"), chatId: "42", pid: this.#options.pid!, pidIncarnation: () => "linux:100", }); } } await runDaemonInternal(["--agent-dir", agentDir, "--owner-id", "owner"], { SettingsImpl: { init: async () => s }, DaemonImpl: OneShotDaemon, processPid: 222, readDaemonState: async () => undefined, }); const state = JSON.parse(fs.readFileSync(daemonPaths(agentDir).state, "utf8")) as { pid: number; ownerId: string; }; expect(state.pid).toBe(222); expect(state.ownerId).toBe("owner"); }); test("Windows source-linked spawn uses an opaque owner id and the daemon rebinds its PID", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let spawnArgs: string[] | undefined; const spawned = await spawnTelegramDaemonOwner( { settings: s, tokenFingerprint: "fp", chatId: "42" }, { execPath: "/usr/local/bin/bun", platform: "win32", pid: 7132, pidIncarnation: () => "linux:107", randomId: () => "launch-token", spawn: (_command, args) => { spawnArgs = args; return { unref() {} }; }, }, ); expect(spawned).toMatchObject({ result: "owner_spawned", acquisition: { ownerId: "daemon-launch-token" } }); expect(spawnArgs).toEqual(expect.arrayContaining(["--owner-id", "daemon-launch-token"])); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "42", pid: 8123, pidIncarnation: () => "linux:108", }), ).toBe(true); expect((await readDaemonState(s))?.pid).toBe(8123); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "42", pid: 9123, pidIncarnation: () => "linux:109", }), ).toBe(false); expect((await readDaemonState(s))?.pid).toBe(8123); }); test("serializes concurrent launcher PID handoffs so exactly one daemon binds", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 7132, ownerId: "daemon-launch-token", allowPidRebind: true, pidIncarnation: () => "linux:107", }); const [firstBound, secondBound] = await Promise.all([ renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "42", pid: 8123, pidIncarnation: () => "linux:108", }), renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "42", pid: 9123, pidIncarnation: () => "linux:109", }), ]); expect([firstBound, secondBound].filter(Boolean)).toHaveLength(1); expect((await readDaemonState(s))?.pid).toBe(firstBound ? 8123 : 9123); const boundPid = firstBound ? 8123 : 9123; expect(JSON.parse(fs.readFileSync(daemonPaths(agentDir).lock, "utf8"))).toMatchObject({ pid: boundPid, incarnation: firstBound ? "linux:108" : "linux:109", ownerId: "daemon-launch-token", acquisitionId: "daemon-launch-token", }); }); test("serializes release with launcher PID handoff", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 7132, ownerId: "daemon-launch-token", allowPidRebind: true, pidIncarnation: () => "linux:100", }); const [bound] = await Promise.all([ renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "42", pid: 8123, pidIncarnation: () => "linux:100", }), releaseDaemonOwnership({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "42", pid: 7132, pidIncarnation: () => "linux:100", }), ]); const state = await readDaemonState(s); if (bound) { expect(state).toMatchObject({ pid: 8123 }); expect(state?.stoppedAt).toBeUndefined(); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(true); } else { expect(state).toMatchObject({ pid: 7132, stoppedAt: expect.any(Number) }); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(false); } }); test("Unix source and compiled launches use PID-prefixed owner IDs and recover dead launchers", async () => { const sourceAgentDir = tempAgentDir(); const source = await spawnTelegramDaemonOwner( { settings: settings(sourceAgentDir), tokenFingerprint: "fp", chatId: "42" }, { execPath: "/usr/local/bin/bun", platform: "linux", pid: 4242, pidIncarnation: () => "linux:110", randomId: () => "4242-nonce", spawn: () => ({ unref() {} }), }, ); const compiledAgentDir = tempAgentDir(); const compiled = await spawnTelegramDaemonOwner( { settings: settings(compiledAgentDir), tokenFingerprint: "fp", chatId: "42" }, { execPath: "/opt/skc/skc", platform: "win32", pid: 5252, pidIncarnation: () => "linux:111", randomId: () => "5252-nonce", spawn: () => ({ unref() {} }), }, ); expect(source).toMatchObject({ result: "owner_spawned", acquisition: { ownerId: "4242-nonce" } }); expect(compiled).toMatchObject({ result: "owner_spawned", acquisition: { ownerId: "5252-nonce" } }); if (source.result !== "owner_spawned" || compiled.result !== "owner_spawned") { throw new Error("expected both daemon launchers to acquire ownership"); } expect(ownerPidFromOwnerId(source.acquisition.ownerId)).toBe(4242); expect(ownerPidFromOwnerId(compiled.acquisition.ownerId)).toBe(5252); expect( await acquireDaemonOwnership({ settings: settings(sourceAgentDir), tokenFingerprint: "fp", chatId: "42", pid: 4343, randomId: () => "4343-replacement", pidAlive: pid => pid !== 4242, pidIncarnation: () => "linux:105", }), ).toMatchObject({ acquired: true, ownerId: "4343-replacement" }); }); test("daemon heartbeat rejects omitted identity or PID at runtime", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 7132, ownerId: "owner", pidIncarnation: () => "linux:100", }); // Runtime callers that omit identity or PID cannot renew ownership. expect(await renewDaemonHeartbeat({ settings: s, ownerId: "owner" })).toBe(false); expect(await readDaemonState(s)).toMatchObject({ pid: 7132, heartbeatAt: expect.any(Number) }); }); test("daemon heartbeat rejects a foreign Telegram identity even with the owner id", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 7132, ownerId: "daemon-launch-token", pidIncarnation: () => "linux:100", }); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "foreign-fp", chatId: "42", pid: 8123, pidIncarnation: () => "linux:100", }), ).toBe(false); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "daemon-launch-token", tokenFingerprint: "fp", chatId: "foreign-chat", pid: 8123, pidIncarnation: () => "linux:100", }), ).toBe(false); expect((await readDaemonState(s))?.pid).toBe(7132); }); test("release rejects a matching owner and Telegram identity with the wrong PID", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: 7132, ownerId: "owner", pidIncarnation: () => "linux:100", }); await releaseDaemonOwnership({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "42", pid: 8123, pidIncarnation: () => "linux:100", }); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(true); expect((await readDaemonState(s))?.stoppedAt).toBeUndefined(); }); test("runDaemonInternal stops when persisted ownership moves to another owner", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let tick: (() => void) | undefined; let stopReason: string | undefined; let resolveRun!: () => void; class StubDaemon { requestStop(reason?: string): void { stopReason = reason; resolveRun(); } run(): Promise { return new Promise(resolve => { resolveRun = resolve; }); } } const run = runDaemonInternal(["--agent-dir", agentDir, "--owner-id", "owner"], { SettingsImpl: { init: async () => s }, DaemonImpl: StubDaemon, readDaemonState: async () => ({ ownerId: "replacement", heartbeatAt: 1 }) as never, setInterval: callback => { tick = callback; return 1 as unknown as Timer; }, clearInterval: () => {}, }); for (let attempt = 0; attempt < 100 && !tick; attempt++) await Bun.sleep(1); expect(tick).toBeDefined(); tick!(); await run; expect(stopReason).toBe("stop"); }); test("runDaemonInternal stops after persisted heartbeat remains stalled", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let tick: (() => void) | undefined; let now = 0; let stopReason: string | undefined; let resolveRun!: () => void; class StubDaemon { requestStop(reason?: string): void { stopReason = reason; resolveRun(); } run(): Promise { return new Promise(resolve => { resolveRun = resolve; }); } } const run = runDaemonInternal(["--agent-dir", agentDir, "--owner-id", "owner"], { SettingsImpl: { init: async () => s }, DaemonImpl: StubDaemon, now: () => now, readDaemonState: async () => ({ ownerId: "owner", heartbeatAt: 1 }) as never, setInterval: callback => { tick = callback; return 1 as unknown as Timer; }, clearInterval: () => {}, }); for (let attempt = 0; attempt < 100 && !tick; attempt++) await Bun.sleep(1); expect(tick).toBeDefined(); tick!(); await Bun.sleep(0); now = Number.MAX_SAFE_INTEGER; tick!(); await run; expect(stopReason).toBe("stop"); }); test("runDaemonInternal exits before constructing daemon for blank Telegram credentials with another adapter configured", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir( Settings.isolated({ "notifications.enabled": true, "notifications.telegram.botToken": " ", "notifications.telegram.chatId": "\t", "notifications.discord.botToken": "discord-token", "notifications.discord.applicationId": "discord-app", "notifications.discord.guildId": "discord-guild", "notifications.discord.parentChannelId": "discord-parent", }) as Settings, agentDir, ); let daemonConstructed = false; class StubDaemon { constructor() { daemonConstructed = true; } async run(): Promise {} requestStop(): void {} } await runDaemonInternal(["--agent-dir", agentDir, "--owner-id", "owner"], { SettingsImpl: { init: async () => s }, DaemonImpl: StubDaemon, pidAlive: () => true, }); expect(daemonConstructed).toBe(false); }); test("requests startup replay and restores identity from replay envelopes", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const socket = FakeWs.instances[0]!; socket.dispatchEvent(new Event("open")); const session = daemon.sessions.get("S")!; const liveDuringReplay = { type: "action_needed", kind: "ask", id: "live", question: "Now?", options: ["Yes"], }; await daemon.handleSessionMessage(session, liveDuringReplay); expect(session.pending.has("live")).toBe(false); expect(socket.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "event_replay", id: "telegram-startup-replay:S", sinceGeneration: 1, sinceSeq: 0, }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: "telegram-startup-replay:S", generation: 1, lastSeq: 4, events: [ { type: "event", name: "action_needed", payload: { type: "action_needed", kind: "ask", id: "stale", question: "Old?", options: ["No"] }, }, { type: "event", name: "identity_header", payload: { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev" }, }, { type: "event", name: "action_needed", payload: liveDuringReplay, }, { type: "event", name: "turn_stream", payload: { type: "turn_stream", sessionId: "S", phase: "finalized", text: "already-delivered-history", }, }, ], }); expect(bot.calls.some(call => call.method === "createForumTopic")).toBe(true); expect(session.pending.has("stale")).toBe(false); expect(session.pending.has("live")).toBe(true); expect( bot.calls.filter(call => call.method === "sendMessage" && String(call.body.text).includes("Now?")).length, ).toBe(1); expect(bot.calls.some(call => String(call.body.text).includes("already-delivered-history"))).toBe(false); daemon.connectSession("S", "ws://s-reconnected", "ts-2"); const replacementSocket = FakeWs.instances[1]!; replacementSocket.dispatchEvent(new Event("open")); expect(replacementSocket.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "event_replay", id: "telegram-startup-replay:S", sinceGeneration: 1, sinceSeq: 0, }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "event_replay_result", ok: true, id: "telegram-startup-replay:S", generation: 1, lastSeq: 5, events: [ { type: "event", generation: 1, seq: 5, name: "turn_stream", payload: { type: "turn_stream", sessionId: "S", phase: "finalized", text: "delivered-before-reconnect", }, }, ], }); expect(bot.calls.some(call => String(call.body.text).includes("delivered-before-reconnect"))).toBe(false); }); test("callback alias from session B routes only to session B", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("A", "ws://a", "ta"); daemon.connectSession("B", "ws://b", "tb"); await daemon.handleSessionMessage(daemon.sessions.get("B")!, { type: "action_needed", kind: "ask", id: "askB", question: "Q", options: ["Y"], }); const alias = bot.calls.find(c => c.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; await daemon.handleTelegramUpdate({ update_id: 1, callback_query: { id: "cb", data: alias, message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(JSON.parse(FakeWs.instances[1]!.sent[0]!)).toEqual({ type: "reply", id: "askB", answer: 0, token: "tb" }); expect(bot.calls.some(c => c.method === "answerCallbackQuery")).toBe(true); }); test("callback alias reply is delivered when Telegram callback ack fails", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FailingCallbackAckBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y"], }); const alias = bot.calls.find(c => c.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; await daemon.handleTelegramUpdate({ update_id: 1, callback_query: { id: "cb", data: alias, message: { chat: { id: 42 } } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toEqual({ type: "reply", id: "ask", answer: 0, token: "ts" }); expect(bot.calls.some(c => c.method === "answerCallbackQuery")).toBe(true); }); test("successful model lists render bounded labels and short aliases in the owning topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:8", status: "ok", message: "Select a model.", modelChoices: [ { selector: "provider/gpt-5", label: "GPT\n5" }, { selector: "provider/long", label: "x".repeat(100) }, { selector: "provider/private", label: "https://private.example.invalid/model" }, ], }); const sent = bot.calls.find(call => call.method === "sendMessage")!.body; const buttons = sent.reply_markup.inline_keyboard.flat(); expect(buttons).toHaveLength(2); expect(sent.message_thread_id).toBeDefined(); expect(sent.text).toBe("✅ Select a model."); expect(JSON.stringify(sent)).not.toContain("provider/gpt-5"); expect(buttons[0].text).toBe("1. GPT 5"); expect(Buffer.byteLength(buttons[1].text, "utf8")).toBeLessThanOrEqual(52); expect( buttons.every((button: { callback_data: string }) => Buffer.byteLength(button.callback_data, "utf8") <= 64), ).toBe(true); expect(buttons.every((button: { callback_data: string }) => button.callback_data.startsWith("m:"))).toBe(true); }); test("model choice callbacks are chat-authorized and forward one session-bound control command", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:8", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const alias = bot.calls.find(call => call.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; await daemon.handleTelegramUpdate({ update_id: 9, callback_query: { id: "wrong", data: alias, message: { chat: { id: 99 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect( bot.calls.some( call => call.method === "answerCallbackQuery" && call.body.callback_query_id === "wrong" && call.body.text === "Not authorized", ), ).toBe(true); await daemon.handleTelegramUpdate({ update_id: 9, callback_query: { id: "right", data: alias, message: { chat: { id: 42 } } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toEqual({ type: "control_command", sessionId: "S", token: "ts", requestId: "tg:model:9", updateId: 9, command: { name: "model", action: "set", selector: "provider/gpt-5" }, }); expect( bot.calls.some(call => call.method === "answerCallbackQuery" && call.body.callback_query_id === "right"), ).toBe(true); }); test("untrusted config updates cannot rekey a transport session", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("transport", "ws://transport", "tt"); const session = daemon.sessions.get("transport")!; await daemon.handleSessionMessage(session, { type: "control_command_result", sessionId: "old-logical", requestId: "tg:model:old", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/old", label: "Old" }], }); await daemon.handleSessionMessage(session, { type: "config_update", sessionId: "new-logical", }); const threadId = bot.calls.find(call => call.method === "sendMessage" && call.body.reply_markup)!.body .message_thread_id; await daemon.handleTelegramUpdate({ update_id: 19, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 19 }, }); expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toMatchObject({ type: "control_command", sessionId: "transport", command: { name: "usage" }, }); }); test.each([ ["malformed", { generation: 0, lastSeq: 0, events: [] }], [ "conflicting identity", { generation: 1, lastSeq: 0, events: [ { payload: { type: "identity_header", sessionId: "canonical" } }, { payload: { type: "identity_header", sessionId: "conflict" } }, ], }, ], ] as const)("%s replay rejects queued config rekeys before draining", async (_name, replay) => { FakeWs.instances = []; const agentDir = tempAgentDir(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, }); daemon.connectSession("transport", "ws://transport", "tt"); const session = daemon.sessions.get("transport")!; (session.ws as unknown as FakeWs).dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "config_update", sessionId: "untrusted" }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, ...replay, }); expect(daemon.sessions.has("transport")).toBe(false); expect((daemon as any).topics.get("untrusted")).toBeUndefined(); }); test("model aliases expire after ten minutes using the injected daemon clock", async () => { FakeWs.instances = []; let now = 0; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, now: () => now, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:model:ttl", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const alias = bot.calls.find(call => call.method === "sendMessage" && call.body.reply_markup)!.body.reply_markup .inline_keyboard[0][0].callback_data; now = 10 * 60 * 1_000; await daemon.handleTelegramUpdate({ update_id: 22, callback_query: { id: "expired", data: alias, message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect( bot.calls.some( call => call.method === "answerCallbackQuery" && call.body.text === "Button is stale. Run /model again.", ), ).toBe(true); }); test("a fresh model menu replaces every prior alias for its logical session", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "control_command_result", sessionId: "S", requestId: "tg:model:one", status: "ok", message: "Select a model.", modelChoices: [ { selector: "provider/old-one", label: "Old one" }, { selector: "provider/old-two", label: "Old two" }, ], }); const oldAliases = bot.calls .find(call => call.method === "sendMessage" && call.body.reply_markup)! .body.reply_markup.inline_keyboard.flat() .map((button: { callback_data: string }) => button.callback_data); await daemon.handleSessionMessage(session, { type: "control_command_result", sessionId: "S", requestId: "tg:model:two", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/fresh", label: "Fresh" }], }); const freshAlias = bot.calls.filter(call => call.method === "sendMessage" && call.body.reply_markup).at(-1)!.body .reply_markup.inline_keyboard[0][0].callback_data; for (const [index, alias] of oldAliases.entries()) { await daemon.handleTelegramUpdate({ update_id: 30 + index, callback_query: { id: `old-${index}`, data: alias, message: { chat: { id: 42 } } }, }); } expect(FakeWs.instances[0]!.sent).toHaveLength(0); await daemon.handleTelegramUpdate({ update_id: 32, callback_query: { id: "fresh", data: freshAlias, message: { chat: { id: 42 } } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toMatchObject({ command: { name: "model", action: "set", selector: "provider/fresh" }, }); }); test("a superseded model-menu creation sends neither a menu nor a threaded delivery", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); createStarted.resolve(); return releaseCreate.promise; } return originalCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://predecessor", "old"); const predecessor = daemon.sessions.get("S")!; const rendering = daemon.handleSessionMessage(predecessor, { type: "control_command_result", sessionId: "S", requestId: "tg:model:race", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/race", label: "Race" }], }); await createStarted.promise; daemon.connectSession("S", "ws://successor", "new"); releaseCreate.resolve({ ok: true, result: { message_thread_id: 79 } }); await expect(rendering).rejects.toThrow("topic authority was revoked during creation"); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); expect( bot.calls.filter(call => call.method === "deleteForumTopic").map(call => call.body.message_thread_id), ).toEqual([79]); expect((daemon as any).topics.get("S")).toBeUndefined(); }); test("a recovered model menu creates a bound topic and delivers its choices after public held creation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); createStarted.resolve(); return releaseCreate.promise; } return originalCall(method, body, options); }; const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("TRANSPORT", "ws://recovered", "trusted"); const session = daemon.sessions.get("TRANSPORT")!; session.ws.dispatchEvent(new Event("open")); const replay = daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 4, lastSeq: 1, events: [ { payload: { type: "identity_header", sessionId: "LOGICAL", repo: "sayknow-cli", branch: "dev", title: "Recovered session", }, }, ], }); await createStarted.promise; const rendering = daemon.handleSessionMessage(session, { type: "control_command_result", sessionId: "LOGICAL", requestId: "tg:model:recovered", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/recovered", label: "Recovered" }], }); await Promise.resolve(); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.reply_markup)).toHaveLength(0); releaseCreate.resolve({ ok: true, result: { message_thread_id: 79 } }); await Promise.all([replay, rendering]); await new Promise(resolve => setTimeout(resolve, 0)); const menus = bot.calls.filter(call => call.method === "sendMessage" && call.body.reply_markup); const persisted = await readTopicAuthorityState(agentDir); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(1); expect(menus).toHaveLength(1); expect(persisted.topics.LOGICAL).toMatchObject({ chatId: "42", endpointKey: session.endpointKey, endpointDigest: endpointAuthorityDigest("ws://recovered", "trusted"), endpointGeneration: 4, }); expect(menus[0]!.body.message_thread_id).toBe(Number(persisted.topics.LOGICAL.topicId)); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 3, lastSeq: 1, events: [{ payload: { type: "identity_header", sessionId: "STALE" } }], }); expect(session.hostGeneration).toBe(4); expect( bot.calls.filter( call => call.method === "createForumTopic" || (call.method === "sendMessage" && call.body.reply_markup), ), ).toHaveLength(2); }); test("failed model keyboard delivery falls back to the generic control result", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const originalCall = bot.call.bind(bot); bot.call = async (method: string, body: unknown): Promise => { if (method === "sendMessage" && (body as { reply_markup?: unknown }).reply_markup) { bot.calls.push({ method, body }); throw new Error("keyboard rejected"); } return originalCall(method, body); }; const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:model:fallback", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const attemptedKeyboard = bot.calls.find(call => call.method === "sendMessage" && call.body.reply_markup)!; const alias = attemptedKeyboard.body.reply_markup.inline_keyboard[0][0].callback_data; expect( bot.calls.some( call => call.method === "sendMessage" && !call.body.reply_markup && call.body.text === "✅ Select a model.", ), ).toBe(true); await daemon.handleTelegramUpdate({ update_id: 40, callback_query: { id: "failed-menu", data: alias, message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect( bot.calls.some( call => call.method === "answerCallbackQuery" && call.body.text === "Button is stale. Run /model again.", ), ).toBe(true); }); test("model choices from a prior daemon are stale after restart", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const firstBot = new FakeBotApi(); const first = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: firstBot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); first.connectSession("S", "ws://s", "ts"); await first.handleSessionMessage(first.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:8", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const alias = firstBot.calls.find(call => call.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; const secondBot = new FakeBotApi(); const secondAgentDir = tempAgentDir(); const second = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(secondAgentDir), secondAgentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: secondBot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); second.connectSession("S", "ws://s", "ts"); await second.handleTelegramUpdate({ update_id: 10, callback_query: { id: "stale", data: alias, message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[1]!.sent).toHaveLength(0); expect( secondBot.calls.some( call => call.method === "answerCallbackQuery" && call.body.text === "Button is stale. Run /model again.", ), ).toBe(true); }); test("model choice aliases are one-shot and duplicate taps become stale", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:8", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const alias = bot.calls.find(call => call.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; const callback = (updateId: number) => daemon.handleTelegramUpdate({ update_id: updateId, callback_query: { id: `tap-${updateId}`, data: alias, message: { chat: { id: 42 } } }, }); await callback(11); await callback(12); expect(FakeWs.instances[0]!.sent).toHaveLength(1); expect( bot.calls.some( call => call.method === "answerCallbackQuery" && call.body.text === "Button is stale. Run /model again.", ), ).toBe(true); }); test("disconnected model-choice sessions fail closed and consume their aliases", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:8", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const alias = bot.calls.find(call => call.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; FakeWs.instances[0]!.close(); await daemon.handleTelegramUpdate({ update_id: 13, callback_query: { id: "closed", data: alias, message: { chat: { id: 42 } } }, }); await daemon.handleTelegramUpdate({ update_id: 14, callback_query: { id: "duplicate", data: alias, message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect( bot.calls.filter( call => call.method === "answerCallbackQuery" && call.body.text === "Button is stale. Run /model again.", ), ).toHaveLength(2); }); test("invalid model results keep generic control rendering and create no keyboard", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:model:9", status: "error", message: "Model is unavailable.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const sent = bot.calls.find(call => call.method === "sendMessage")!.body; expect(sent.text).toBe("❌ Model is unavailable."); expect(sent.reply_markup).toBeUndefined(); }); test("model choice delivery survives a callback acknowledgement failure", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FailingCallbackAckBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "control_command_result", sessionId: "S", requestId: "tg:8", status: "ok", message: "Select a model.", modelChoices: [{ selector: "provider/gpt-5", label: "GPT 5" }], }); const alias = bot.calls.find(call => call.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; await daemon.handleTelegramUpdate({ update_id: 15, callback_query: { id: "ack-fails", data: alias, message: { chat: { id: 42 } } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toMatchObject({ type: "control_command", command: { name: "model", action: "set", selector: "provider/gpt-5" }, }); expect(bot.calls.some(call => call.method === "answerCallbackQuery")).toBe(true); }); test("unknown and expired aliases are stale guidance with zero frames", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("A", "ws://a", "ta"); await daemon.handleTelegramUpdate({ callback_query: { id: "cb", data: "missing", message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("stale"))).toBe(true); await daemon.handleTelegramUpdate({ callback_query: { id: "cb2", data: "expired", message: { chat: { id: 42 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); }); test("stale callbacks in non-private chats do not send guidance messages", async () => { FakeWs.instances = []; const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { id: body.chat_id, type: "supergroup" } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const agentDir = tempAgentDir(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "-10042", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("A", "ws://a", "ta"); await daemon.handleTelegramUpdate({ callback_query: { id: "cb", data: "missing", message: { chat: { id: -10042 } } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls.some(c => c.method === "answerCallbackQuery" && c.body.text === "Button is stale")).toBe(true); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); }); test("known alias with dead target is stale guidance with zero frames", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y"], }); const alias = bot.calls.find(c => c.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; FakeWs.instances[0]!.setReadyState(3); await daemon.handleTelegramUpdate({ callback_query: { id: "cb", data: alias, message: { chat: { id: 42 } } } }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls.some(c => c.method === "answerCallbackQuery" && c.body.text === "Button is stale")).toBe(true); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("stale"))).toBe(true); bot.calls = []; daemon.sessions.delete("S"); await daemon.handleTelegramUpdate({ callback_query: { id: "cb2", data: alias, message: { chat: { id: 42 } } } }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("stale"))).toBe(true); }); test("known alias with non-pending target is stale guidance with zero frames", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y"], }); const alias = bot.calls.find(c => c.method === "sendMessage")!.body.reply_markup.inline_keyboard[0][0] .callback_data; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_resolved", id: "ask" }); await daemon.handleTelegramUpdate({ callback_query: { id: "cb", data: alias, message: { chat: { id: 42 } } } }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("stale"))).toBe(true); }); test("action_resolved clears reply message routes for that ask", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y"], }); const askMessageId = [...daemon.messageRoutes.entries()].find( ([, route]) => route.sessionId === "S" && route.actionId === "ask", )?.[0]; expect(askMessageId).toBeDefined(); daemon.messageRoutes.set("same-session-other", { sessionId: "S", actionId: "other" }); daemon.messageRoutes.set("other-session", { sessionId: "T", actionId: "ask" }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_resolved", id: "ask" }); expect(daemon.messageRoutes.has(askMessageId!)).toBe(false); expect(daemon.messageRoutes.get("same-session-other")).toEqual({ sessionId: "S", actionId: "other" }); expect(daemon.messageRoutes.get("other-session")).toEqual({ sessionId: "T", actionId: "ask" }); }); test("delayed close from replaced socket preserves fresh reply message routes", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://old", "old-token"); const oldSocket = FakeWs.instances[0]!; daemon.connectSession("S", "ws://new", "new-token"); const newSocket = FakeWs.instances[1]!; const newSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(newSession, { type: "action_needed", kind: "ask", id: "ask-new", question: "Q", options: ["Y"], }); const askMessageId = [...daemon.messageRoutes.entries()].find( ([, route]) => route.sessionId === "S" && route.actionId === "ask-new", )?.[0]; expect(askMessageId).toBeDefined(); oldSocket.close(); expect(daemon.sessions.get("S")).toBe(newSession); expect(daemon.messageRoutes.has(askMessageId!)).toBe(true); await daemon.handleTelegramUpdate({ message: { chat: { id: 42 }, text: "ok", reply_to_message: { message_id: Number(askMessageId) } }, }); expect(JSON.parse(newSocket.sent.at(-1)!)).toEqual({ type: "reply", id: "ask-new", answer: "ok", token: "new-token", }); }); test("reply_to_message routes and non-paired chat leaks nothing", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); daemon.messageRoutes.set("55", { sessionId: "S", actionId: "A" }); daemon.sessions.get("S")!.pending.set("A", { sessionId: "S", actionId: "A" }); await daemon.handleTelegramUpdate({ message: { chat: { id: "bad" }, text: "x", reply_to_message: { message_id: 55 } }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls).toHaveLength(0); await daemon.handleTelegramUpdate({ message: { chat: { id: 42 }, text: "ok", reply_to_message: { message_id: 55 } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toEqual({ type: "reply", id: "A", answer: "ok", token: "ts" }); }); test("plain text answers a pending ask as free-input instead of injecting a new turn", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); // Emit an ask: creates the forum topic, registers the pending ask, sends the message. await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const askSend = bot.calls.find(c => c.method === "sendMessage"); const threadId = askSend!.body.message_thread_id; // A plain free-text message in that topic answers the pending ask... await daemon.handleTelegramUpdate({ update_id: 1, message: { chat: { id: 42 }, message_thread_id: threadId, text: "my typed answer", message_id: 99 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent).toContainEqual({ type: "reply", id: "ask1", answer: "my typed answer", token: "ts" }); // ...and must NOT be injected as a new user turn. expect(sent.some(frame => frame.type === "user_message")).toBe(false); const visibleReceipt = bot.calls.find( c => c.method === "sendMessage" && c.body.message_thread_id === threadId && c.body.text === "Selected!", ); expect(visibleReceipt).toBeUndefined(); }); test("deduplicates pending ask text before semantic acknowledgement completes", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let markAckStarted: () => void = () => {}; const ackStarted = new Promise(resolve => { markAckStarted = resolve; }); let releaseAck: () => void = () => {}; const ackGate = new Promise(resolve => { releaseAck = () => resolve({ ok: true, result: { message_id: 999 } }); }); class BlockingAckBotApi extends FakeBotApi { override async call(method: string, body: unknown): Promise { if (method === "sendMessage" && (body as { text?: unknown }).text === "Selected!") { this.calls.push({ method, body }); markAckStarted(); return ackGate; } return super.call(method, body); } } const bot = new BlockingAckBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.createdTopicThreadIds.at(-1)!; const update = { update_id: 1, message: { chat: { id: 42 }, message_thread_id: threadId, text: "my typed answer", message_id: 99 }, }; await daemon.handleTelegramUpdate(update); const acknowledgement = daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-1", commitKey: "commit-1", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await ackStarted; await daemon.handleTelegramUpdate(update); const repliesBeforeAck = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).filter( frame => frame.type === "reply" && frame.id === "ask1", ); expect(repliesBeforeAck).toHaveLength(1); releaseAck(); await acknowledgement; expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-1", commitKey: "commit-1", outcome: { status: "delivered", messageId: 999 }, }); }); test("cooldown-suppressed selected acknowledgement stays pending and retries", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as never, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes", "no"], }); const internal = daemon as unknown as { botCooldownUntil: number; pool: { pending: number }; flushPool(): Promise; }; internal.botCooldownUntil = Date.now() + 5_000; const callsBefore = bot.calls.length; await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-cooldown", commitKey: "commit-cooldown", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); expect(bot.calls).toHaveLength(callsBefore); expect(internal.pool.pending).toBe(1); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).not.toContainEqual( expect.objectContaining({ type: "ask_selected_ack_result", requestId: "ack-cooldown" }), ); internal.botCooldownUntil = 0; await internal.flushPool(); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-cooldown", commitKey: "commit-cooldown", outcome: expect.objectContaining({ status: "delivered" }), }); }); test("deduplicates a semantic acknowledgement and sends Selected without HTML or retry metadata", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes", "no"], }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-1", commitKey: "commit-1", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-2", commitKey: "commit-1", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); const selectedCalls = bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!"); expect(selectedCalls).toHaveLength(1); expect(selectedCalls[0]?.body.parse_mode).toBeUndefined(); const results = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).filter( frame => frame.type === "ask_selected_ack_result", ); expect(results).toEqual([ { type: "ask_selected_ack_result", requestId: "ack-1", commitKey: "commit-1", outcome: { status: "delivered", messageId: selectedCalls[0] ? bot.calls.indexOf(selectedCalls[0]) + 1 : 0 }, }, { type: "ask_selected_ack_result", requestId: "ack-2", commitKey: "commit-1", outcome: { status: "delivered", messageId: selectedCalls[0] ? bot.calls.indexOf(selectedCalls[0]) + 1 : 0 }, }, ]); }); test("reports an ambiguous Selected send failure after one application attempt", async () => { FakeWs.instances = []; class FailingSelectedBotApi extends FakeBotApi { override async call(method: string, body: unknown): Promise { if (method === "sendMessage" && (body as { text?: unknown }).text === "Selected!") { this.calls.push({ method, body }); throw new Error("connection reset after write"); } return super.call(method, body); } } const agentDir = tempAgentDir(); const bot = new FailingSelectedBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-1", commitKey: "commit-1", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-2", commitKey: "commit-1", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!")).toHaveLength(1); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-1", commitKey: "commit-1", outcome: { status: "unknown", reason: "transport_ambiguous" }, }); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-2", commitKey: "commit-1", outcome: { status: "unknown", reason: "transport_ambiguous" }, }); }); test("rejects an expired acknowledgement without a Telegram send", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-expired", commitKey: "commit-expired", actionId: "ask1", deadlineAt: Date.now() - 1, }); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!")).toHaveLength(0); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-expired", commitKey: "commit-expired", outcome: { status: "failed", reason: "expired" }, }); }); test("sends a live acknowledgement in flat private chat without a topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); (daemon as unknown as { topics: Map }).topics.delete("S"); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-flat", commitKey: "commit-flat", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); const selected = bot.calls.find(call => call.method === "sendMessage" && call.body.text === "Selected!"); expect(selected?.body).toEqual({ chat_id: "42", text: "Selected!" }); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-flat", commitKey: "commit-flat", outcome: expect.objectContaining({ status: "delivered" }), }); }); test("uses persisted topic authority for recovery without reopening an accepted ask", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); await daemon.handleSessionMessage(session, { type: "action_resolved", id: "ask1" }); expect(session.pending.has("ask1")).toBe(false); const restarted = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner-restarted", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); await restarted.loadTopics(); restarted.connectSession("S", "ws://s-restarted", "ts-restarted"); const recoveredSession = restarted.sessions.get("S")!; await restarted.handleSessionMessage(recoveredSession, { type: "ask_selected_ack_request", mode: "recovery", requestId: "recovery-1", commitKey: "commit-recovery", sessionId: "S", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); expect(recoveredSession.pending.has("ask1")).toBe(false); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!")).toHaveLength(1); const sendsBeforeWrongSession = bot.calls.length; await restarted.handleSessionMessage(recoveredSession, { type: "ask_selected_ack_request", mode: "recovery", requestId: "recovery-2", commitKey: "commit-wrong-session", sessionId: "other", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); expect(bot.calls).toHaveLength(sendsBeforeWrongSession); expect(FakeWs.instances[1]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "recovery-2", commitKey: "commit-wrong-session", outcome: { status: "failed", reason: "route_missing" }, }); }); test("coalesces overlapping acknowledgement requests for the same commit", async () => { FakeWs.instances = []; const selectedStarted = Promise.withResolvers(); const releaseSelected = Promise.withResolvers(); class BlockingSelectedBotApi extends FakeBotApi { override async call(method: string, body: unknown): Promise { if (method === "sendMessage" && (body as { text?: unknown }).text === "Selected!") { this.calls.push({ method, body }); selectedStarted.resolve(); return releaseSelected.promise; } return super.call(method, body); } } const agentDir = tempAgentDir(); const bot = new BlockingSelectedBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); const first = daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-1", commitKey: "same-commit", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await selectedStarted.promise; await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-2", commitKey: "same-commit", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_cancel", requestId: "ack-1", commitKey: "same-commit", }); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!")).toHaveLength(1); releaseSelected.resolve({ ok: true, result: { message_id: 88 } }); await first; const results = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).filter( frame => frame.type === "ask_selected_ack_result", ); expect(results).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-1", commitKey: "same-commit", outcome: { status: "failed", reason: "cancelled" }, }); expect(results).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-2", commitKey: "same-commit", outcome: { status: "delivered", messageId: 88 }, }); }); test("endpoint replacement reuses the in-flight acknowledgement tombstone without a second POST", async () => { FakeWs.instances = []; const selectedStarted = Promise.withResolvers(); const releaseSelected = Promise.withResolvers(); class BlockingSelectedBotApi extends FakeBotApi { override async call(method: string, body: unknown): Promise { if (method === "sendMessage" && (body as { text?: unknown }).text === "Selected!") { this.calls.push({ method, body }); selectedStarted.resolve(); return releaseSelected.promise; } return super.call(method, body); } } const agentDir = tempAgentDir(); const bot = new BlockingSelectedBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as never, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); const request = daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-drop", commitKey: "commit-drop", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await selectedStarted.promise; (daemon as unknown as { dropSession(session: unknown, reason: string): void }).dropSession( session, "session_closed", ); daemon.connectSession("S", "ws://replacement", "replacement-token"); const replacement = daemon.sessions.get("S")!; await daemon.handleSessionMessage(replacement, { type: "ask_selected_ack_request", mode: "recovery", requestId: "ack-replacement", commitKey: "commit-drop", sessionId: "S", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!")).toHaveLength(1); expect(FakeWs.instances[1]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-replacement", commitKey: "commit-drop", outcome: { status: "unknown", reason: "transport_ambiguous" }, }); releaseSelected.resolve({ ok: true, result: { message_id: 88 } }); await request; // The dropped socket's immutable lease is revoked immediately, so completion // cannot emit a stale WebSocket acknowledgement after its replacement wins. const results = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).filter( frame => frame.type === "ask_selected_ack_result" && frame.requestId === "ack-drop", ); expect(results).toEqual([]); }); test("cancels a drained acknowledgement before the Telegram application attempt", async () => { FakeWs.instances = []; const routeCheckStarted = Promise.withResolvers(); const releaseRouteCheck = Promise.withResolvers(); const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask1", question: "Proceed?", options: ["yes"], }); const routeCheckedDaemon = daemon as unknown as { pairedChatIsPrivate(): Promise }; routeCheckedDaemon.pairedChatIsPrivate = async () => { routeCheckStarted.resolve(); await releaseRouteCheck.promise; return true; }; const request = daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack-cancel", commitKey: "commit-cancel", actionId: "ask1", deadlineAt: Date.now() + 8_000, }); await routeCheckStarted.promise; await daemon.handleSessionMessage(session, { type: "ask_selected_ack_cancel", requestId: "ack-cancel", commitKey: "commit-cancel", }); releaseRouteCheck.resolve(); await request; expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "Selected!")).toHaveLength(0); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack-cancel", commitKey: "commit-cancel", outcome: { status: "failed", reason: "cancelled" }, }); }); test("no-topic plain text does not answer the only pending ask", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); await daemon.handleTelegramUpdate({ update_id: 2, message: { chat: { id: 42 }, text: "my typed answer", message_id: 100 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "reply")).toBe(false); expect(sent.some(frame => frame.type === "user_message")).toBe(false); }); test("plain text injects a user turn when no ask is pending", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); // Create the topic + pending via an ask, then resolve it so nothing is pending. await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_resolved", id: "ask1" }); await daemon.handleTelegramUpdate({ update_id: 7, message: { chat: { id: 42 }, message_thread_id: threadId, text: "start a new task", message_id: 100 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "user_message" && frame.text === "start a new task")).toBe(true); expect(sent.some(frame => frame.type === "reply")).toBe(false); }); test("routes /btw as an ephemeral frame and returns its reply to the original topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await enableEphemeralTurns(daemon); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.createdTopicThreadIds.at(-1)!; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_resolved", id: "ask1" }); await daemon.handleTelegramUpdate({ update_id: 8, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw what changed?", message_id: 101 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent).toContainEqual( expect.objectContaining({ type: "ephemeral_turn", sessionId: "S", question: "what changed?", token: "ts", updateId: 8, threadId: String(threadId), messageId: 101, requestId: expect.stringMatching(/^btw:/), }), ); expect(sent.some(frame => frame.type === "user_message")).toBe(false); const requestId = sent.find(frame => frame.type === "ephemeral_turn")!.requestId; const replyCountBefore = bot.calls.filter(call => call.method === "sendMessage").length; const richCountBefore = bot.calls.filter(call => call.method === "sendRichMessage").length; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "ephemeral_turn_result", sessionId: "S", requestId: "btw:stale", threadId: String(threadId), updateId: 8, messageId: 101, status: "ok", text: "Stale answer", }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(replyCountBefore); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "ephemeral_turn_result", sessionId: "S", requestId, threadId: String(threadId), updateId: 8, messageId: 101, status: "ok", text: "# Reply\n\n| Name | Score |\n| --- | ---: |\n| Ada | 10 |", }); const richCountAfterResult = bot.calls.filter(call => call.method === "sendRichMessage").length; expect(richCountAfterResult).toBe(richCountBefore + 1); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "ephemeral_turn_result", sessionId: "S", requestId, threadId: String(threadId), updateId: 8, messageId: 101, status: "ok", text: "Duplicate answer", }); expect(bot.calls.filter(call => call.method === "sendRichMessage")).toHaveLength(richCountAfterResult); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(replyCountBefore); const rich = bot.calls.find( call => call.method === "sendRichMessage" && call.body.reply_parameters?.message_id === 101, )!; expect(rich.body).toEqual({ chat_id: "42", message_thread_id: threadId, reply_parameters: { message_id: 101 }, rich_message: { markdown: "# Reply\n\n| Name | Score |\n| --- | ---: |\n| Ada | 10 |", skip_entity_detection: true, }, }); expect(rich.body.rich_message.blocks).toBeUndefined(); expect(rich.body.rich_message.media).toBeUndefined(); expect(rich.options).toEqual(expect.objectContaining({ noRetry: true, signal: expect.any(AbortSignal) })); bot.calls = []; const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendRichMessage") { bot.calls.push({ method, body, options }); throw new Error("rich transport outcome unknown"); } return call(method, body, options); }; await daemon.handleTelegramUpdate({ update_id: 9, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw uncertain", message_id: 102 }, }); const uncertainRequest = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).find( frame => frame.type === "ephemeral_turn" && frame.messageId === 102, )!; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "ephemeral_turn_result", sessionId: "S", requestId: uncertainRequest.requestId, threadId: String(threadId), updateId: 9, messageId: 102, status: "ok", text: "# Uncertain\n\n| Name |\n| --- |\n| Ada |", }); expect(bot.calls.map(call => call.method)).toEqual(["sendRichMessage"]); }); test("/btw addressed to another bot is consumed without injecting a user message", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.createdTopicThreadIds.at(-1)!; await daemon.handleTelegramUpdate({ update_id: 81, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw@OtherBot secret", message_id: 181 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "ephemeral_turn" || frame.type === "user_message")).toBe(false); }); test("routes /btw results through recovered logical topic ownership after a session rekey", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); await replayResumedIdentity(daemon, "S", "S", { url: "ws://s", token: "ts" }); await replayResumedIdentity(daemon, "S", "rekeyed", { url: "ws://s", token: "ts", generation: 2 }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "hello", capabilities: ["ephemeral_turn_v1"], }); const threadId = bot.createdTopicThreadIds.at(-1)!; const recovered = daemon.sessions.get("S")!; await daemon.handleTelegramUpdate({ update_id: 82, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw rekey", message_id: 182 }, }); const request = (recovered.ws as unknown as FakeWs).sent .map(frame => JSON.parse(frame)) .find(frame => frame.type === "ephemeral_turn")!; expect(request.sessionId).toBe("rekeyed"); const rekeyMarkdown = "| Value |\n| --- |\n| still routed |"; await daemon.handleSessionMessage(recovered, { type: "ephemeral_turn_result", sessionId: "rekeyed", requestId: request.requestId, threadId: String(threadId), updateId: 82, messageId: 182, status: "ok", text: rekeyMarkdown, }); const rich = bot.calls.find( call => call.method === "sendRichMessage" && call.body.reply_parameters?.message_id === 182, )!; expect(rich.body).toEqual({ chat_id: "42", message_thread_id: threadId, reply_parameters: { message_id: 182 }, rich_message: { markdown: rekeyMarkdown, skip_entity_detection: true }, }); expect(rich.options).toEqual(expect.objectContaining({ noRetry: true, signal: expect.any(AbortSignal) })); }); test("routes /btw rich delivery at exact 32,768 boundary and preserves HTML when bypassed or disabled", async () => { const tablePrefix = "| Value |\n| --- |\n| "; const tableSuffix = " |"; const lexerSpy = vi.spyOn(Marked.prototype, "lexer"); try { for (const { label, rich, length, richCalls, lexerCalls } of [ { label: "default omitted at 32,768", rich: undefined, length: 32_768, richCalls: 1, lexerCalls: 1 }, { label: "explicit on at 32,768", rich: { enabled: true }, length: 32_768, richCalls: 1, lexerCalls: 1 }, { label: "explicit off at 32,768", rich: { enabled: false }, length: 32_768, richCalls: 0, lexerCalls: 0 }, { label: "explicit on at 32,769", rich: { enabled: true }, length: 32_769, richCalls: 0, lexerCalls: 0 }, ] as const) { FakeWs.instances = []; const bot = new RichFakeBotApi(); const agentDir = tempAgentDir(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, WebSocketImpl: FakeWs as any, ...(rich ? { rich } : {}), }); daemon.connectSession("S", "ws://s", "ts"); await enableEphemeralTurns(daemon); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.richThreadId; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_resolved", id: "ask1" }); bot.calls.length = 0; lexerSpy.mockClear(); await daemon.handleTelegramUpdate({ update_id: 80, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw boundary", message_id: 180 }, }); const requestId = JSON.parse(FakeWs.instances[0]!.sent.at(-1)!).requestId; const markdown = `${tablePrefix}${"x".repeat(length - tablePrefix.length - tableSuffix.length)}${tableSuffix}`; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "ephemeral_turn_result", sessionId: "S", requestId, threadId: String(threadId), updateId: 80, messageId: 180, status: "ok", text: markdown, }); expect(countMethod(bot, "sendRichMessage"), label).toBe(richCalls); expect(lexerSpy, label).toHaveBeenCalledTimes(lexerCalls); const html = bot.calls.filter(call => call.method === "sendMessage"); if (richCalls === 1) { expect(html, label).toHaveLength(0); const rich = findMethod(bot, "sendRichMessage")!; expect(rich.body).toEqual({ chat_id: "42", message_thread_id: threadId, reply_parameters: { message_id: 180 }, rich_message: { markdown, skip_entity_detection: true }, }); expect(rich.body.rich_message.blocks).toBeUndefined(); expect(rich.body.rich_message.media).toBeUndefined(); expect(rich.options).toEqual( expect.objectContaining({ noRetry: true, signal: expect.any(AbortSignal) }), ); } else { expect(html, label).toHaveLength(splitTelegramHtml(markdownToTelegramHtml(markdown)).length); expect( html.map(call => call.body.text), label, ).toEqual(splitTelegramHtml(markdownToTelegramHtml(markdown))); expect(html[0]!.body.reply_parameters, label).toEqual({ message_id: 180 }); expect( html.slice(1).every(call => call.body.reply_parameters === undefined), label, ).toBe(true); expect( html.every(call => call.body.parse_mode === TELEGRAM_PARSE_MODE), label, ).toBe(true); expect( html.every(call => call.options?.noRetry === true && call.options.signal instanceof AbortSignal), label, ).toBe(true); } } } finally { lexerSpy.mockRestore(); } }); test("shows /btw help for an empty question without injecting a user turn", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_resolved", id: "ask1" }); await daemon.handleTelegramUpdate({ update_id: 9, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw", message_id: 102 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "ephemeral_turn" || frame.type === "user_message")).toBe(false); const reply = bot.calls.find( call => call.method === "sendMessage" && call.body.text === "Usage: /btw ", )!; expect(reply).toMatchObject({ body: { message_thread_id: threadId, reply_parameters: { message_id: 102 }, text: "Usage: /btw ", }, options: { noRetry: true, signal: expect.any(AbortSignal) }, }); }); test("telegram control command forwards control_command instead of user_message", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleTelegramUpdate({ update_id: 8, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/context", message_id: 101 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent).toContainEqual({ type: "control_command", sessionId: "S", token: "ts", requestId: "tg:8", updateId: 8, threadId: String(threadId), command: { name: "context" }, }); expect(sent.some(frame => frame.type === "user_message")).toBe(false); }); test("unavailable private threaded controls publish seen state before one exact notice", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const threadId = bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id; daemon.sessions.delete("S"); bot.calls = []; const update = { update_id: 81, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 101 }, }; await daemon.handleTelegramUpdate(update); await daemon.handleTelegramUpdate(update); expect(bot.calls.filter(call => call.method === "sendMessage").map(call => call.body.text)).toEqual([ "Session control unavailable: this local SKC session is disconnected.", ]); }); test.each([ "writeFile", "rename", ] as const)("unavailable control %s failure is silent and does not retain the update in memory", async failingMethod => { let fail = true; const fsImpl: TelegramDaemonFs = { mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: (file, data, opts) => fail && failingMethod === "writeFile" && file.includes("telegram-seen-updates") ? Promise.reject(new Error("disk failure")) : fs.promises.writeFile(file, data, opts), rename: (oldPath, newPath) => fail && failingMethod === "rename" && newPath.includes("telegram-seen-updates") ? Promise.reject(new Error("rename failure")) : fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), }; const { bot, daemon, threadId } = await unavailableControlHarness(fsImpl); daemon.sessions.delete("S"); const update = { update_id: 82, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 102 }, }; await daemon.handleTelegramUpdate(update); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); fail = false; await daemon.handleTelegramUpdate(update); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); }); test("unavailable control publishes state before notice and restart suppresses it", async () => { const { agentDir, bot, daemon, threadId } = await unavailableControlHarness(); daemon.sessions.delete("S"); let persistedBeforeSend = false; const originalCall = bot.call.bind(bot); bot.call = async (method, body) => { if (method === "sendMessage") persistedBeforeSend = fs.existsSync(daemonPaths(agentDir).seenUpdates); return originalCall(method, body); }; const update = { update_id: 83, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 103 }, }; await daemon.handleTelegramUpdate(update); expect(persistedBeforeSend).toBe(true); const restarted = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "other", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); await restarted.loadTopics(); await restarted.loadSeenUpdateIds(); bot.calls = []; await restarted.handleTelegramUpdate(update); expect(bot.calls).toEqual([]); }); test("private and media boundaries do not emit unavailable control notices", async () => { const { agentDir, bot, daemon, threadId } = await unavailableControlHarness(); daemon.sessions.delete("S"); bot.calls = []; Reflect.set(daemon, "pairedChatPrivate", undefined); const originalCall = bot.call.bind(bot); bot.call = async (method, body) => { if (method === "getChat") return { ok: true, result: { id: 42, type: "group" } }; return originalCall(method, body); }; await daemon.handleTelegramUpdate({ update_id: 84, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 104 }, }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); expect(fs.existsSync(daemonPaths(agentDir).seenUpdates)).toBe(false); bot.call = originalCall; bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 85, message: { chat: { id: 42 }, message_thread_id: threadId, caption: "/usage", photo: [{ file_id: "photo" }], message_id: 105, }, }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); }); test("non-open sockets publish once even when the unavailable notice send fails", async () => { const { bot, daemon, threadId } = await unavailableControlHarness(); FakeWs.instances[0]!.setReadyState(3); let sendAttempts = 0; const originalCall = bot.call.bind(bot); bot.call = async (method, body) => { if (method === "sendMessage") { sendAttempts += 1; throw new Error("network failure"); } return originalCall(method, body); }; const update = { update_id: 86, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 106 }, }; await daemon.handleTelegramUpdate(update); await daemon.handleTelegramUpdate(update); expect(sendAttempts).toBe(1); }); test("session cleanup leaves its former topic unknown", async () => { const { bot, daemon, threadId } = await unavailableControlHarness(); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "session_closed", sessionId: "S" }); bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 87, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/usage", message_id: 107 }, }); expect(bot.calls).toEqual([]); }); test("invalid telegram control command returns usage once without answering pending ask", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; const askMessageId = bot.calls.findIndex(c => c.method === "sendMessage") + 1; await daemon.handleTelegramUpdate({ update_id: 9, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/reasoning impossible", message_id: 102, reply_to_message: { message_id: askMessageId }, }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "reply")).toBe(false); expect(sent.some(frame => frame.type === "user_message")).toBe(false); const usageMessages = () => bot.calls.filter(c => c.method === "sendMessage" && String(c.body.text).startsWith("Usage: /reasoning")); expect(usageMessages()).toHaveLength(1); await daemon.handleTelegramUpdate({ update_id: 9, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/reasoning impossible", message_id: 102 }, }); expect(usageMessages()).toHaveLength(1); }); test("wrong-suffix telegram control command is consumed, not injected or ask-answered", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, }); Object.assign(daemon, { botUsername: "SayknowCliBot" }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 10, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/context@OtherBot", message_id: 103 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "reply")).toBe(false); expect(sent.some(frame => frame.type === "user_message")).toBe(false); expect(sent.some(frame => frame.type === "control_command")).toBe(false); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); }); test("persisted seen update ids suppress duplicate threaded injection after restart", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleTelegramUpdate({ update_id: 77, message: { chat: { id: 42 }, message_thread_id: threadId, text: "repeat once", message_id: 100 }, }); const sent = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)); expect(sent.some(frame => frame.type === "user_message" && frame.text === "repeat once")).toBe(true); const seenState = JSON.parse(fs.readFileSync(daemonPaths(agentDir).seenUpdates, "utf8")) as { updateIds: number[]; }; expect(seenState.updateIds).toContain(77); FakeWs.instances = []; const restarted = new TelegramNotificationDaemon({ settings: s, ownerId: "owner-2", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, }); await restarted.loadTopics(); await restarted.loadSeenUpdateIds(); restarted.connectSession("S", "ws://s2", "ts2"); await restarted.handleTelegramUpdate({ update_id: 77, message: { chat: { id: 42 }, message_thread_id: threadId, text: "repeat once", message_id: 101 }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); }); test("runDaemonSmoke exits without polling and emits no token", async () => { const agentDir = tempAgentDir(); await runDaemonSmoke({ agentDir }); expect(fs.readdirSync(daemonPaths(agentDir).dir).join("\n")).not.toContain("secret-token"); }); test("release requires the owner identity as well as its opaque owner id", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", }); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "other", tokenFingerprint: "fp", chatId: "42", pid: process.pid, }), ).toBe(false); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "42", pid: process.pid, }), ).toBe(true); await releaseDaemonOwnership({ settings: s, ownerId: "owner", tokenFingerprint: "foreign-fp", chatId: "42", pid: process.pid, }); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(true); expect( await acquireDaemonOwnership({ settings: s, tokenFingerprint: "foreign-fp", chatId: "42", pid: process.pid, randomId: () => "owner", }), ).toMatchObject({ acquired: false, blocked: true }); await releaseDaemonOwnership({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "99", pid: process.pid, }); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(true); await releaseDaemonOwnership({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "42", pid: process.pid, }); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(false); }); test("stopped ownership records do not block immediate reacquisition", async () => { for (const token of ["fp", "foreign-fp"]) { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", }); await releaseDaemonOwnership({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "42", pid: process.pid, }); expect( await acquireDaemonOwnership({ settings: s, tokenFingerprint: token, chatId: "42", pid: process.pid, pidAlive: () => true, randomId: () => "replacement", }), ).toMatchObject({ acquired: true, ownerId: "replacement" }); } }); test("heartbeat rejects malformed PIDs without changing ownership state", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", }); const before = await readDaemonState(s); for (const pid of [Number.NaN, Number.POSITIVE_INFINITY, 0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1]) { expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "42", pid, }), ).toBe(false); } expect(await readDaemonState(s)).toEqual(before); }); test("heartbeat rejects malformed persisted handoff PIDs without rewriting state", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const paths = daemonPaths(agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", allowPidRebind: true, }); const malformed = { ...(await readDaemonState(s))!, pid: -1, launcherPid: -1, }; fs.writeFileSync(paths.state, JSON.stringify(malformed)); expect( await renewDaemonHeartbeat({ settings: s, ownerId: "owner", tokenFingerprint: "fp", chatId: "42", pid: process.pid + 1, }), ).toBe(false); expect(await readDaemonState(s)).toEqual(malformed); }); test("ownership heartbeat remains fresh during a pending long poll and is cleaned up on exit", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let now = 0; await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, now: () => now, randomId: () => "owner", }); const timers = new Map void }>(); let nextTimerId = 1; let pollStarted!: () => void; const pollStartedPromise = new Promise(resolve => { pollStarted = resolve; }); let releasePoll!: () => void; const pollGate = new Promise(resolve => { releasePoll = resolve; }); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", now: () => now, idleTimeoutMs: 60_000, createLifecycleControlServer: null, botApi: { async call(method: string): Promise { if (method === "getUpdates") { pollStarted(); await pollGate; } return { ok: true, result: [] }; }, }, setIntervalImpl: ((callback: () => void, ms: number) => { const id = nextTimerId++; timers.set(id, { ms, callback }); return id as unknown as ReturnType; }) as typeof setInterval, clearIntervalImpl: ((id: number) => { timers.delete(id); }) as unknown as typeof clearInterval, }); const runPromise = daemon.run(); await pollStartedPromise; now = 10_000; [...timers.values()].find(timer => timer.ms === 5_000)?.callback(); for (let attempts = 0; attempts < 20; attempts++) { const heartbeat = JSON.parse(await fs.promises.readFile(daemonPaths(agentDir).heartbeat, "utf8")) as { heartbeatAt: number; }; if (heartbeat.heartbeatAt === now) break; await Bun.sleep(5); } expect((await readDaemonState(s))?.heartbeatAt).toBe(0); expect(JSON.parse(await fs.promises.readFile(daemonPaths(agentDir).heartbeat, "utf8"))).toMatchObject({ heartbeatAt: now, }); daemon.requestStop(); releasePoll(); await runPromise; expect(timers.size).toBe(0); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(false); }); test("heartbeat fails closed without recreating a removed daemon directory", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: "fp", chatId: "42", pid: process.pid, randomId: () => "owner", }); const paths = daemonPaths(agentDir); fs.rmSync(paths.dir, { recursive: true, force: true }); await expect(renewDaemonHeartbeat({ settings: s, ownerId: "owner" })).resolves.toBe(false); expect(fs.existsSync(paths.dir)).toBe(false); }); test("scan timer connects new sessions while a getUpdates long-poll is in flight", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); // Endpoint discovery files live at /.skc/state/sdk/.json. const writeEndpoint = async (cwd: string, sessionId: string, url: string) => { await registerNotificationRoot({ settings: s, cwd, sessionId }); const dir = path.join(cwd, ".skc", "state", "sdk"); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, `${sessionId}.json`), JSON.stringify({ url, token: "tok" })); }; // Session A exists from the start so the run loop reaches the long-poll branch. await writeEndpoint(path.join(agentDir, "cwd-a"), "A", "ws://a"); // getUpdates blocks (simulating the 25s long-poll) until released, so the // run loop's own scanRoots call cannot pick up session B. let releasePoll: () => void = () => {}; const pollGate = new Promise(resolve => { releasePoll = resolve; }); const inner = new FakeBotApi(); const gatedBot = { get calls() { return inner.calls; }, async call(method: string, body: unknown): Promise { if (method === "getUpdates") { await pollGate; return { ok: true, result: [] }; } return inner.call(method, body); }, }; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: gatedBot, WebSocketImpl: FakeWs as any, scanIntervalMs: 5, idleTimeoutMs: 60_000, createLifecycleControlServer: null, }); const until = async (pred: () => boolean, ms = 2000) => { const start = Date.now(); while (!pred()) { if (Date.now() - start > ms) throw new Error("condition not met in time"); await new Promise(r => setTimeout(r, 5)); } }; const runPromise = daemon.run(); await until(() => daemon.sessions.has("A")); // Session B starts AFTER the loop is blocked in the long-poll. The scan timer // (not the long-poll-gated loop scan) must connect it promptly. await writeEndpoint(path.join(agentDir, "cwd-b"), "B", "ws://b"); await until(() => daemon.sessions.has("B")); expect(daemon.sessions.has("B")).toBe(true); // Stop: hand ownership to another owner so the next heartbeat renew fails, // then release the long-poll so the loop can observe it and exit. fs.writeFileSync( daemonPaths(agentDir).state, JSON.stringify({ version: DAEMON_VERSION, ownerId: "other", pid: 1, heartbeatAt: 0 }), ); releasePoll(); await runPromise; }); // Ported from upstream gajae 37e3e84d1d (#6174): a Windows controller cannot // signal the owner, so the owner watches its control request during a long poll. test("control watcher aborts a blocked getUpdates long-poll when a stop is requested", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); await registerNotificationRoot({ settings: s, cwd: path.join(agentDir, "cwd-a"), sessionId: "A" }); const endpointDir = path.join(agentDir, "cwd-a", ".skc", "state", "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync(path.join(endpointDir, "A.json"), JSON.stringify({ url: "ws://a", token: "tok" })); const watchTimers = new Map void>(); let nextWatchTimer = 1; const pollEntered = Promise.withResolvers(); let shouldStop = false; let aborted = false; const inner = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: { async call(method: string, body: unknown, options?: { signal?: AbortSignal }): Promise { if (method !== "getUpdates") return inner.call(method, body); pollEntered.resolve(); await new Promise(resolve => { const onAbort = () => { aborted = true; resolve(); }; if (options?.signal?.aborted) onAbort(); else options?.signal?.addEventListener("abort", onAbort, { once: true }); }); return { ok: true, result: [] }; }, }, WebSocketImpl: FakeWs as any, scanIntervalMs: 5, idleTimeoutMs: 60_000, createLifecycleControlServer: null, control: { shouldStop: async () => shouldStop }, setTimeoutImpl: ((callback: () => void, ms = 0) => { if (ms !== 750) return setTimeout(callback, ms); const id = nextWatchTimer++; watchTimers.set(id, callback); return id as unknown as NodeJS.Timeout; }) as typeof setTimeout, clearTimeoutImpl: ((timer: NodeJS.Timeout) => { if (!watchTimers.delete(timer as unknown as number)) clearTimeout(timer); }) as typeof clearTimeout, }); const runPromise = daemon.run(); await pollEntered.promise; shouldStop = true; const [watchId, watch] = [...watchTimers.entries()][0] ?? []; expect(watch).toBeDefined(); if (watchId !== undefined) watchTimers.delete(watchId); watch?.(); await runPromise; expect(aborted).toBe(true); expect(watchTimers.size).toBe(0); }); test("pollOnce survives a transient getUpdates failure instead of crashing", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let calls = 0; const bot = { async call(method: string): Promise { if (method === "getUpdates") { calls++; const err = new Error("The socket connection was closed unexpectedly.") as Error & { code?: string }; err.code = "ECONNRESET"; throw err; } return { ok: true, result: [] }; }, }; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, }); // Must resolve (not reject): the run loop relies on this never throwing. await expect(daemon.pollOnce()).resolves.toBe(0); expect(calls).toBe(1); }); test("default botApi retries transient network failures before delivering", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let attempts = 0; const fetchImpl = (async () => { attempts++; if (attempts < 3) { const err = new Error("socket reset") as Error & { code?: string }; err.code = "ECONNRESET"; throw err; } return new Response(JSON.stringify({ ok: true, result: { message_id: 7 } }), { headers: { "content-type": "application/json" }, }); }) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", fetchImpl, setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, }); const res = (await (daemon as any).botApi.call("sendMessage", { chat_id: 42, text: "hi" })) as { result?: { message_id?: number }; }; expect(attempts).toBe(3); expect(res.result?.message_id).toBe(7); }); }); describe("telegram daemon connection-drop resilience", () => { // AC-1/AC-2: model a half-open daemon-to-session WebSocket that stays OPEN, // accepts send(), and never dispatches close. The liveness fence must evict it // and let scanRoots() reconnect the current endpoint. test("AC-1/AC-2: half-open session socket is detected and reconnected", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "sess-cwd"); await registerNotificationRoot({ settings: s, cwd, sessionId: "S" }); const roots = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[] }; const endpointDir = path.join(roots.roots[0]!, "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync(path.join(endpointDir, "S.json"), JSON.stringify({ url: "ws://s", token: "ts" })); let now = 0; const liveness: Array<() => void> = []; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, now: () => now, setIntervalImpl: ((cb: () => void) => { liveness.push(cb); return 0; }) as any, clearIntervalImpl: (() => {}) as any, }); await daemon.scanRoots(); expect(FakeWs.instances).toHaveLength(1); expect(daemon.sessions.has("S")).toBe(true); FakeWs.instances[0]!.dispatchEvent(new Event("open")); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).find(frame => frame.type === "hello")).toEqual({ type: "hello", protocolVersion: 3, capabilities: [ "client_ping_pong", "ask_controls_v1", "ask_selected_ack_v1", "tool_activity_v2", "tool_activity_v1", "ephemeral_turn_v1", ], }); // The native server advertises the ping/pong capability so ack-based // liveness can start; then the link goes half-open (no further frames, // socket never closes, no pong will arrive). FakeWs.instances[0]!.emit({ type: "hello", protocolVersion: 2, capabilities: ["client_ping_pong"] }); FakeWs.instances[0]!.emit({ type: "event_replay_result", ok: true, id: "telegram-startup-replay:S", generation: 1, lastSeq: 0, events: [], }); for (let attempts = 0; attempts < 20 && liveness.length === 0; attempts++) { await Bun.sleep(1); } expect(liveness).toHaveLength(1); // Advance past the heartbeat TTL and fire any liveness probe. Post-fix this // detects the missing pong, drops the stale session, and reconnects. now += 25_000; for (const cb of liveness) cb(); await Promise.resolve(); await daemon.scanRoots(); expect(FakeWs.instances).toHaveLength(2); expect(daemon.sessions.get("S")?.ws).toBe(FakeWs.instances[1] as unknown as WebSocket); expect(FakeWs.instances[1]!.readyState).toBe(FakeWs.OPEN); }); test("D6c: connect-only empty replay creates no topic until one identity frame arrives", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "connecting-cwd"); await registerNotificationRoot({ settings: s, cwd, sessionId: "S" }); const roots = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[] }; const endpointDir = path.join(roots.roots[0]!, "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync(path.join(endpointDir, "S.json"), JSON.stringify({ url: "ws://s", token: "ts" })); let now = 0; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, now: () => now, }); await daemon.scanRoots(); const predecessor = FakeWs.instances[0]!; predecessor.setReadyState(0); now = 1_001; await daemon.scanRoots(); const successor = FakeWs.instances[1]!; expect(daemon.sessions.get("S")?.ws).toBe(successor as unknown as WebSocket); successor.dispatchEvent(new Event("open")); await Bun.sleep(1); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); predecessor.setReadyState(FakeWs.OPEN); predecessor.dispatchEvent(new Event("open")); predecessor.emit({ type: "turn_stream", sessionId: "S", text: "predecessor must not route" }); await Bun.sleep(1); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); expect( bot.calls.some(call => call.method === "sendMessage" && call.body.text === "predecessor must not route"), ).toBe(false); const active = daemon.sessions.get("S")!; await daemon.handleSessionMessage(active, { type: "event_replay_result", ok: true, id: active.replayId, generation: 1, lastSeq: 0, events: [], }); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); await daemon.handleSessionMessage(active, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(1); await daemon.handleSessionMessage(active, { type: "turn_stream", sessionId: "S", text: "successor routes" }); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "successor routes"), ).toHaveLength(1); expect((await readTopicAuthorityState(agentDir)).topics.S).toMatchObject({ endpointGeneration: 1, topicId: expect.any(String), }); }); // Phase 1 / AC-7: a getUpdates rejection during an internet outage must not // kill the daemon. On current code run() awaits pollOnce() with no try/catch, // so the rejection unwinds run() and releases ownership. This asserts the // DESIRED survival (run resolves) and is RED on current code (run rejects). test("AC-7: getUpdates rejection during outage does not terminate the daemon", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); let now = 0; let getUpdatesCalls = 0; const bot = { calls: [] as Array<{ method: string; body: any }>, async call(method: string, body: unknown): Promise { this.calls.push({ method, body }); if (method === "getUpdates") { getUpdatesCalls++; if (getUpdatesCalls === 1) { // Drop the only session so the next loop iteration idle-exits // once the daemon survives the rejection (post-fix path). daemon.sessions.get("S")?.ws.close(); throw new Error("network down: getUpdates rejected"); } return { ok: true, result: [] }; } return { ok: true, result: true }; }, }; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, idleTimeoutMs: 10, now: () => (now += 1000), setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, setIntervalImpl: (() => 0) as any, clearIntervalImpl: (() => {}) as any, }); daemon.connectSession("S", "ws://s", "ts"); await expect(daemon.run()).resolves.toBeUndefined(); expect(getUpdatesCalls).toBeGreaterThanOrEqual(1); }); }); test("daemon registers in-thread config and lifecycle commands and drops stale rpc/answer commands", async () => { const s = settings(tempAgentDir()); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); await daemon.registerBotCommands(); const call = bot.calls.find((c: any) => c.method === "setMyCommands"); expect(call).toBeTruthy(); const cmds = (call!.body.commands as Array<{ command: string }>).map(c => c.command); expect(cmds).toContain("verbose"); expect(cmds).toContain("lean"); expect(cmds).toContain("redact"); expect(cmds).toContain("toolactivity"); expect(cmds).not.toContain("tools"); const verbose = (call!.body.commands as Array<{ command: string; description: string }>).find( command => command.command === "verbose", ); expect(verbose?.description).toBe("Mirror bounded tool-owned summaries + provider-displayable reasoning summaries"); expect(cmds).toContain("btw"); expect(cmds).toContain("session_create"); expect(cmds).toContain("session_recent"); expect(cmds).toContain("session_close"); expect(cmds).toContain("model"); expect(cmds).toContain("session_resume"); expect(cmds).not.toContain("answer"); expect(cmds).not.toContain("attach"); expect(cmds).not.toContain("detach"); }); test("forum lifecycle commands fail closed even when addressed to this bot username", async () => { const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { id: body.chat_id, type: "supergroup" } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "-10042", botApi: bot, }); Object.assign(daemon, { lifecycleControlActive: true, botUsername: "SayknowCliBot" }); await daemon.handleTelegramUpdate({ update_id: 101, message: { chat: { id: -10042, type: "supergroup" }, text: "/session_recent", message_id: 1 }, }); await daemon.handleTelegramUpdate({ update_id: 102, message: { chat: { id: -10042, type: "supergroup" }, text: "/session_recent@OtherBot", message_id: 2 }, }); await daemon.handleTelegramUpdate({ update_id: 103, message: { chat: { id: -10042, type: "supergroup" }, text: "/session_recent@SayknowCliBot", message_id: 3 }, }); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); expect(bot.calls.filter(c => c.method === "getChat")).toHaveLength(0); }); test("forum lifecycle commands fail closed when bot username is unavailable", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "-10042", botApi: bot, }); Object.assign(daemon, { lifecycleControlActive: true }); await daemon.handleTelegramUpdate({ update_id: 104, message: { chat: { id: -10042, type: "supergroup" }, text: "/session_recent@SayknowCliBot", message_id: 4 }, }); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); }); test("non-addressable forum lifecycle commands in known topics are dropped", async () => { let updateId = 200; const exercise = async (text: string, botUsername?: string): Promise => { FakeWs.instances = []; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "-10042", botApi: bot, WebSocketImpl: FakeWs as any, }); Object.assign(daemon, { lifecycleControlActive: true, botUsername }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; bot.calls = []; await daemon.handleTelegramUpdate({ update_id: updateId++, message: { chat: { id: -10042, type: "supergroup" }, message_thread_id: threadId, message_id: updateId, text, }, }); expect(FakeWs.instances[0]!.sent).toHaveLength(0); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); }; await exercise("/session_recent", "SayknowCliBot"); await exercise("/session_recent@OtherBot", "SayknowCliBot"); await exercise("/session_recent@SayknowCliBot"); }); test("ensureTelegramDaemonRunning spawns the daemon subcommand with owner-id and agent-dir", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let captured: { command: string; args: string[] } | undefined; const child = readyTelegramSpawnFixture({ settings: s, firstChildPid: 211, onSpawn: (_pid, command, args) => { captured = { command, args }; }, }); const res = await ensureTelegramDaemonRunning( { settings: s, cwd: path.join(agentDir, "cwd"), sessionId: "s1" }, { spawn: child.spawn, sleep: child.sleep, pidAlive: () => true, pidIncarnation: () => "linux:100", pid: 111, }, ); expect(res).toBe("owner_spawned"); expect(captured).toBeTruthy(); expect(captured!.args).toContain("notify"); expect(captured!.args).toContain("daemon-internal"); expect(captured!.args).toContain("--owner-id"); const ai = captured!.args.indexOf("--agent-dir"); expect(ai).toBeGreaterThanOrEqual(0); expect(captured!.args[ai + 1]).toBe(agentDir); }); test("image_attachment frame uploads via sendPhoto into an identified session topic", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map(), }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", }); await daemon.handleSessionMessage(session as any, { type: "image_attachment", sessionId: "S", source: "computer", mime: "image/png", data: "AAAA", }); const createTopic = bot.calls.find(c => c.method === "createForumTopic"); const photo = bot.calls.find(c => c.method === "sendPhoto"); expect(createTopic).toBeTruthy(); expect(createTopic!.body.name).toBe("sayknow-cli/dev"); expect(photo).toBeTruthy(); expect(photo!.body.photo).toBe("AAAA"); expect(Number(photo!.body.message_thread_id)).toBeGreaterThan(0); }); describe("telegram topic name template (#1909)", () => { async function createdTopicName( identity: { repo?: string; branch?: string; title?: string }, topics?: { nameTemplate?: string }, ): Promise { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, topics, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", ...identity, }); return bot.calls.find(c => c.method === "createForumTopic")?.body.name as string | undefined; } test("unset template preserves the built-in {repo}/{branch} - {title} composition", async () => { expect(await createdTopicName({ repo: "sayknow-cli", branch: "dev", title: "Fix flaky retries" })).toBe( "sayknow-cli/dev - Fix flaky retries", ); }); test("configured template renders the session title first", async () => { expect( await createdTopicName( { repo: "sayknow-cli", branch: "dev", title: "Fix flaky retries" }, { nameTemplate: "{title} · {repo}/{branch}" }, ), ).toBe("Fix flaky retries · sayknow-cli/dev"); }); test("a referenced placeholder with no value falls back to the default composition (no dangling separator)", async () => { // Before a session title exists, the title-first template must NOT render // "· sayknow-cli/dev"; it falls back to the built-in composition instead. expect( await createdTopicName({ repo: "sayknow-cli", branch: "dev" }, { nameTemplate: "{title} · {repo}/{branch}" }), ).toBe("sayknow-cli/dev"); }); test("a repo-less session with a template falls back to the session-id default", async () => { expect(await createdTopicName({}, { nameTemplate: "{title} · {repo}/{branch}" })).toBe("SKC S"); }); test("a blank template is ignored so behavior is unchanged", async () => { expect( await createdTopicName({ repo: "sayknow-cli", branch: "dev", title: "Ship it" }, { nameTemplate: " " }), ).toBe("sayknow-cli/dev - Ship it"); }); test("a title-only template renders just the title, falling back when the title is absent", async () => { expect( await createdTopicName({ repo: "sayknow-cli", branch: "dev", title: "Ship it" }, { nameTemplate: "{title}" }), ).toBe("Ship it"); expect(await createdTopicName({ repo: "sayknow-cli", branch: "dev" }, { nameTemplate: "{title}" })).toBe( "sayknow-cli/dev", ); }); test("unknown placeholders are left verbatim", async () => { expect( await createdTopicName( { repo: "sayknow-cli", branch: "dev", title: "Ship it" }, { nameTemplate: "{title} [{env}]" }, ), ).toBe("Ship it [{env}]"); }); test("a later title header renames the topic using the configured template", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, topics: { nameTemplate: "{title} · {repo}/{branch}" }, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", }); await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Second title", }); expect(bot.calls.filter(c => c.method === "createForumTopic").map(c => c.body.name)).toEqual(["sayknow-cli/dev"]); expect(bot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual([ "Second title · sayknow-cli/dev", ]); }); }); test("identity-less threaded frames wait for identity instead of creating fallback topics", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "image_attachment", sessionId: "S", source: "computer", mime: "image/png", data: "AAAA", }); expect(bot.calls.find(c => c.method === "createForumTopic")).toBeUndefined(); expect(bot.calls.find(c => c.method === "sendPhoto")).toBeUndefined(); await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", }); const createTopic = bot.calls.find(c => c.method === "createForumTopic"); const photo = bot.calls.find(c => c.method === "sendPhoto"); expect(createTopic).toBeTruthy(); expect(createTopic!.body.name).toBe("sayknow-cli/dev"); expect(photo).toBeTruthy(); expect(photo!.body.message_thread_id).toBeGreaterThan(0); }); test("transient topic rename failure is retried on the next identity header", async () => { class RetryRenameBotApi extends FakeBotApi { editAttempts = 0; override async call(method: string, body: unknown): Promise { if (method === "editForumTopic") { this.editAttempts++; this.calls.push({ method, body }); if (this.editAttempts === 1) throw new Error("temporary rename failure"); return { ok: true, result: true }; } return super.call(method, body); } } const agentDir = tempAgentDir(); const bot = new RetryRenameBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "action_needed", kind: "ask", id: "ask1", question: "Name it?", options: ["a", "b"], }); expect(bot.calls.find(c => c.method === "createForumTopic")!.body.name).toBe("SKC S"); await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Readable title", }); await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Readable title", }); const edits = bot.calls.filter(c => c.method === "editForumTopic"); expect(edits).toHaveLength(2); expect(edits.map(c => c.body.name)).toEqual([ "sayknow-cli/dev - Readable title", "sayknow-cli/dev - Readable title", ]); }); test("a delayed user topic rename is immediately restored after a completed daemon edit", async () => { const { agentDir, bot, daemon, session, threadId } = await identityTopicHarness({ title: "First title" }); await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Generated title", }); bot.calls = []; await daemon.handleTelegramUpdate(forumTopicEditedUpdate(41, threadId, "My focus")); const persisted = await readTopicAuthorityState(agentDir); expect(bot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual(["My focus"]); expect(persisted.topics.S).toMatchObject({ name: "My focus", nameOwner: "user", nameReconcilePending: false }); bot.calls = []; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Later generated title", }); expect(bot.calls.filter(c => c.method === "editForumTopic")).toHaveLength(0); }); test("bot-originated topic edit service messages do not claim user ownership", async () => { const { bot, daemon, session, threadId } = await identityTopicHarness({ title: "First title" }); bot.calls = []; for (const [updateId, name, overrides] of [ [42, "Bot echo", { from: { id: 42, is_bot: true } }], [43, "Wrong user", { from: { id: 7, is_bot: false } }], [44, "String chat", { chat: { id: "42" } }], [45, "Unknown bot status", { from: { id: 42 } }], [46, "Missing sender", { from: null }], ] as const) { await daemon.handleTelegramUpdate(forumTopicEditedUpdate(updateId, threadId, name, overrides)); } expect(bot.calls.filter(c => c.method === "editForumTopic")).toHaveLength(0); await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Second title", }); expect(bot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual([ "sayknow-cli/dev - Second title", ]); }); test("a user rename racing an in-flight daemon edit is restored after the daemon edit", async () => { class RacingRenameBotApi extends FakeBotApi { readonly daemonEditStarted = Promise.withResolvers(); readonly releaseDaemonEdit = Promise.withResolvers(); override async call(method: string, body: unknown): Promise { const name = (body as { name?: unknown } | null)?.name; if (method === "editForumTopic" && name === "sayknow-cli/dev - Generated title") { this.calls.push({ method, body }); this.daemonEditStarted.resolve(); await this.releaseDaemonEdit.promise; return { ok: true, result: true }; } return super.call(method, body); } } const bot = new RacingRenameBotApi(); const { daemon, session, threadId } = await identityTopicHarness({ bot, title: "First title" }); bot.calls = []; const identityUpdate = daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Generated title", }); await bot.daemonEditStarted.promise; await daemon.handleTelegramUpdate(forumTopicEditedUpdate(43, threadId, "My focus")); const laterIdentity = daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Later generated title", }); await laterIdentity; expect(bot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual([ "sayknow-cli/dev - Generated title", ]); bot.releaseDaemonEdit.resolve(); await identityUpdate; expect(bot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual([ "sayknow-cli/dev - Generated title", "My focus", ]); bot.calls = []; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Another generated title", }); expect(bot.calls.some(c => c.method === "editForumTopic")).toBe(false); }); test("concurrent user renames persist the newest pending ownership state", async () => { const firstWriteStarted = Promise.withResolvers(); const releaseFirstWrite = Promise.withResolvers(); let blockNextTopicWrite = false; const fsImpl = topicStateFs(async () => { if (blockNextTopicWrite) { blockNextTopicWrite = false; firstWriteStarted.resolve(); await releaseFirstWrite.promise; } }); const { agentDir, daemon, threadId } = await identityTopicHarness({ fs: fsImpl }); blockNextTopicWrite = true; const firstRename = daemon.handleTelegramUpdate(forumTopicEditedUpdate(45, threadId, "First focus")); await firstWriteStarted.promise; const secondRename = daemon.handleTelegramUpdate(forumTopicEditedUpdate(46, threadId, "Latest focus")); releaseFirstWrite.resolve(); await Promise.all([firstRename, secondRename]); const state = await readTopicAuthorityState(agentDir); expect(state.topics.S).toMatchObject({ name: "Latest focus", nameOwner: "user", nameReconcilePending: false, }); }); test("topic-state write failure retries the rename before later Telegram updates", async () => { let failNextTopicWrite = false; const fsImpl = topicStateFs(async () => { if (failNextTopicWrite) { failNextTopicWrite = false; throw new Error("injected topic-state write failure"); } }); const bot = new ReplayRenameBotApi(); const { agentDir, daemon, threadId } = await identityTopicHarness({ bot, fs: fsImpl }); bot.threadId = threadId; failNextTopicWrite = true; await daemon.pollOnce(); const failedState = await readTopicAuthorityState(agentDir); expect(failedState.topics.S).not.toMatchObject({ name: "First focus", nameOwner: "user" }); await daemon.pollOnce(); await daemon.pollOnce(); const persistedState = await readTopicAuthorityState(agentDir); expect(persistedState.topics.S).toMatchObject({ name: "Later focus", nameOwner: "user", nameReconcilePending: false, userNameUpdateId: 51, }); expect(bot.calls.filter(call => call.method === "getUpdates").map(call => call.body.offset)).toEqual([0, 0, 52]); expect(bot.calls.filter(call => call.method === "editForumTopic").map(call => call.body.name)).toEqual([ "First focus", "Later focus", ]); }); test.each([ [ "thrown getChat", () => { throw new Error("getChat unavailable"); }, ], ["getChat ok:false", () => ({ ok: false, description: "temporary failure" })], ["malformed getChat", () => ({ ok: true, result: {} })], ])("indeterminate %s retries a forum rename before dispatching later updates", async (_name, getChatFailure) => { const bot = new ReplayRenameBotApi(); const { agentDir } = await identityTopicHarness({ bot, ownerId: "creator" }); bot.threadId = bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id; bot.calls = []; bot.getChatFailure = getChatFailure; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "retry", botToken: "tok", chatId: "42", botApi: bot, }); await daemon.loadTopics(); await daemon.pollOnce(); const beforePrivacySucceeds = await readTopicAuthorityState(agentDir); expect(beforePrivacySucceeds.topics.S).not.toMatchObject({ nameOwner: "user" }); expect(bot.calls.filter(call => call.method === "getUpdates").map(call => call.body.offset)).toEqual([0]); await daemon.pollOnce(); await daemon.pollOnce(); const durableState = await readTopicAuthorityState(agentDir); expect(durableState.topics.S).toMatchObject({ name: "Later focus", nameOwner: "user", userNameUpdateId: 51, }); expect(bot.calls.filter(call => call.method === "getUpdates").map(call => call.body.offset)).toEqual([0, 0, 52]); }); test("remote-success user-name reconciliation retries after its pending-clear write fails", async () => { let successfulTopicWritesBeforeFailure: number | undefined; const fsImpl = topicStateFs(async () => { if (successfulTopicWritesBeforeFailure === undefined) return; if (successfulTopicWritesBeforeFailure > 0) { successfulTopicWritesBeforeFailure--; return; } successfulTopicWritesBeforeFailure = undefined; throw new Error("injected pending-clear write failure"); }); const firstBot = new FakeBotApi(); const { agentDir, daemon: firstDaemon, session, threadId, } = await identityTopicHarness({ bot: firstBot, fs: fsImpl }); successfulTopicWritesBeforeFailure = 1; await firstDaemon.handleTelegramUpdate(forumTopicEditedUpdate(44, threadId, "My focus")); expect(firstBot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual(["My focus"]); const persistedState = await readTopicAuthorityState(agentDir); expect(persistedState.topics.S).toMatchObject({ name: "My focus", nameOwner: "user", nameReconcilePending: true, }); const retryBot = new FakeBotApi(); const restarted = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner-2", botToken: "tok", chatId: "42", botApi: retryBot, }); await restarted.loadTopics(); await restarted.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Later generated title", }); expect(retryBot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual(["My focus"]); }); test.each([ [ "throw", () => { throw new Error("temporary reconciliation failure"); }, ], ["ok:false", () => ({ ok: false, description: "temporary reconciliation failure" })], ])("user-name reconciliation %s remains retryable after restart", async (_name, failure) => { class FailingUserRenameBotApi extends FakeBotApi { override async call(method: string, body: unknown): Promise { if (method === "editForumTopic" && (body as { name?: unknown }).name === "My focus") { this.calls.push({ method, body }); return failure(); } return super.call(method, body); } } const failingBot = new FailingUserRenameBotApi(); const { agentDir, daemon: firstDaemon, session, threadId } = await identityTopicHarness({ bot: failingBot }); await firstDaemon.handleTelegramUpdate(forumTopicEditedUpdate(47, threadId, "My focus")); await firstDaemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Later generated title", }); const retryBot = new FakeBotApi(); const restarted = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner-2", botToken: "tok", chatId: "42", botApi: retryBot, }); await restarted.loadTopics(); await restarted.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Later generated title", }); expect(retryBot.calls.filter(c => c.method === "editForumTopic").map(c => c.body.name)).toEqual(["My focus"]); }); test("live sessions with the same repo branch create distinct topics", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S1", "ws://s1", "t1"); daemon.connectSession("S2", "ws://s2", "t2"); const first = daemon.sessions.get("S1")!; const second = daemon.sessions.get("S2")!; await daemon.handleSessionMessage(first, { type: "identity_header", sessionId: "S1", repo: "sayknow-cli", branch: "dev", }); await daemon.handleSessionMessage(second, { type: "identity_header", sessionId: "S2", repo: "sayknow-cli", branch: "dev", }); await daemon.handleSessionMessage(second, { type: "turn_stream", sessionId: "S2", text: "second session output", }); const creates = bot.calls.filter(c => c.method === "createForumTopic"); const sends = bot.calls.filter(c => c.method === "sendMessage"); expect(creates).toHaveLength(2); expect(creates.map(c => c.body.name)).toEqual(["sayknow-cli/dev", "sayknow-cli/dev"]); expect(sends).toHaveLength(3); expect(sends[0]!.body.message_thread_id).not.toBe(sends[1]!.body.message_thread_id); expect(sends[2]!.body.message_thread_id).toBe(sends[1]!.body.message_thread_id); }); test("transient identity for an existing repo branch does not create a duplicate topic", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const live = { sessionId: "LIVE", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; const transient = { sessionId: "DEAD", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(live as any, { type: "identity_header", sessionId: "LIVE", repo: "sayknow-cli", branch: "dev", }); await daemon.handleSessionMessage(transient as any, { type: "identity_header", sessionId: "DEAD", repo: "sayknow-cli", branch: "dev", }); expect(bot.calls.filter(c => c.method === "createForumTopic")).toHaveLength(1); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(1); }); test("stale identity after loadTopics reuses the persisted repo branch owner", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const firstDaemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const live = { sessionId: "LIVE", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await firstDaemon.handleSessionMessage(live as any, { type: "identity_header", sessionId: "LIVE", repo: "sayknow-cli", branch: "dev", }); const restartedDaemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); await restartedDaemon.loadTopics(); const stale = { sessionId: "DEAD", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await restartedDaemon.handleSessionMessage(stale as any, { type: "identity_header", sessionId: "DEAD", repo: "sayknow-cli", branch: "dev", }); expect(bot.calls.filter(c => c.method === "createForumTopic").map(c => c.body.name)).toEqual(["sayknow-cli/dev"]); const threadedSends = bot.calls.filter(c => c.method === "sendMessage").map(c => c.body.message_thread_id); expect(threadedSends).toHaveLength(1); expect(Number(threadedSends[0])).toBeGreaterThan(0); }); async function replayResumedIdentity( daemon: TelegramNotificationDaemon, transportSessionId: string, logicalSessionId: string, { url = "ws://canonical", token = "canonical-token", generation = 1 } = {}, ): Promise { daemon.connectSession(transportSessionId, url, token); const session = daemon.sessions.get(transportSessionId)!; session.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation, lastSeq: 1, events: [ { payload: { type: "identity_header", sessionId: logicalSessionId, repo: "sayknow-cli", branch: "dev", title: "Recovered session", }, }, ], }); } function recoveryDaemon( agentDir: string, bot: FakeBotApi, chatId = "42", fsImpl?: TelegramDaemonFs, ): TelegramNotificationDaemon { const daemonSettings = chatId === "42" ? settings(agentDir) : setPrivateAgentDir( Settings.isolated({ "notifications.enabled": true, "notifications.telegram.botToken": "123456:secret-token", "notifications.telegram.chatId": chatId, "notifications.daemon.idleTimeoutMs": 20, }) as Settings, agentDir, ); return new TelegramNotificationDaemon({ settings: daemonSettings, ownerId: "owner", botToken: "tok", chatId, botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, fs: fsImpl, }); } test("authenticated resume resets a persisted generation-one replay cursor for a fresh endpoint incarnation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "CANONICAL", "CANONICAL", { url: "ws://old-runtime", token: "old-token", generation: 1, }); const originalTopicId = bot.createdTopicThreadIds[0]!; const firstState = await readTopicAuthorityState(agentDir); expect(firstState.topics.CANONICAL).toMatchObject({ replayGeneration: 1, replaySeq: 1 }); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(1); bot.calls = []; const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); restarted.connectSession("CANONICAL", "ws://fresh-runtime", "fresh-token"); const fresh = restarted.sessions.get("CANONICAL")!; fresh.ws.dispatchEvent(new Event("open")); expect((fresh.ws as unknown as FakeWs).sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "event_replay", id: fresh.replayId, sinceGeneration: 1, sinceSeq: 0, }); await restarted.handleSessionMessage(fresh, { type: "event_replay_result", ok: true, id: fresh.replayId, generation: 1, lastSeq: 2, events: [ { payload: { type: "identity_header", sessionId: "CANONICAL", repo: "sayknow-cli", branch: "dev", title: "Recovered session", }, }, ], }); await restarted.handleSessionMessage(fresh, { type: "turn_stream", sessionId: "CANONICAL", text: "Recovered output", }); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); const sends = bot.calls.filter(call => call.method === "sendMessage"); expect(sends).toHaveLength(1); expect(sends[0]!.body.message_thread_id).toBe(originalTopicId); const persisted = await readTopicAuthorityState(agentDir); expect(persisted.topics.CANONICAL).toMatchObject({ endpointKey: expect.any(String), endpointGeneration: 1, endpointIncarnation: 1, replayGeneration: 1, replaySeq: 2, }); }); test("persisted A and B topics keep A fail-closed through A-to-B recovery until A resumes", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "A", "A", { url: "ws://a", token: "a" }); await replayResumedIdentity(initial, "B", "B", { url: "ws://b", token: "b" }); const [aTopic, bTopic] = bot.createdTopicThreadIds; expect(aTopic).not.toBe(bTopic); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); await replayResumedIdentity(restarted, "A", "B", { url: "ws://a-rekey", token: "rekey" }); const rekeyed = restarted.sessions.get("A")!; const rekeyedWs = rekeyed.ws as unknown as FakeWs; rekeyedWs.sent.length = 0; bot.calls = []; await restarted.handleTelegramUpdate({ update_id: 280_101, message: { chat: { id: 42 }, message_thread_id: aTopic, message_id: 1, text: "must not hijack A" }, }); expect(rekeyedWs.sent).toEqual([]); expect(bot.calls.filter(call => call.method === "setMessageReaction")).toHaveLength(0); await replayResumedIdentity(restarted, "A-RETURN", "A", { url: "ws://a-return", token: "return" }); const returned = restarted.sessions.get("A-RETURN")!; const returnedWs = returned.ws as unknown as FakeWs; returnedWs.sent.length = 0; await restarted.handleTelegramUpdate({ update_id: 280_102, message: { chat: { id: 42 }, message_thread_id: aTopic, message_id: 2, text: "A routes after resume" }, }); expect(returnedWs.sent.map(frame => JSON.parse(frame))).toContainEqual( expect.objectContaining({ type: "user_message", text: "A routes after resume" }), ); const persisted = await readTopicAuthorityState(agentDir); expect(persisted.topics).toMatchObject({ A: { topicId: String(aTopic) }, B: { topicId: String(bTopic) } }); }); test("authenticated fresh endpoint recovery rejects a concurrent active incumbent", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); await replayResumedIdentity(daemon, "OLD", "CANONICAL", { url: "ws://old-runtime", token: "old-token" }); const originalTopicId = bot.createdTopicThreadIds[0]!; bot.calls = []; await replayResumedIdentity(daemon, "CONTENDER", "CANONICAL", { url: "ws://fresh-runtime", token: "fresh-token", generation: 1, }); expect(daemon.sessions.has("CONTENDER")).toBe(false); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); expect(await readTopicAuthorityState(agentDir)).toMatchObject({ topics: { CANONICAL: { topicId: String(originalTopicId) } }, }); }); test("serializes concurrent endpoint recovery claims before durable publication", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "INITIAL", "CANONICAL", { url: "ws://old", token: "old" }); let releaseWrite!: () => void; const writeReleased = new Promise(resolve => (releaseWrite = resolve)); let writeReached!: () => void; const writeBlocked = new Promise(resolve => (writeReached = resolve)); let paused = true; const restarted = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!paused) return; writeReached(); await writeReleased; }), ); await restarted.loadTopics(); const winner = replayResumedIdentity(restarted, "WINNER", "CANONICAL", { url: "ws://winner", token: "winner-token", }); await writeBlocked; const loser = replayResumedIdentity(restarted, "LOSER", "CANONICAL", { url: "ws://loser", token: "loser-token", }); paused = false; releaseWrite(); await Promise.all([winner, loser]); bot.calls = []; await restarted.handleSessionMessage(restarted.sessions.get("WINNER")!, { type: "turn_stream", sessionId: "CANONICAL", text: "winner only", }); expect(restarted.sessions.has("LOSER")).toBe(false); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); expect((await readTopicAuthorityState(agentDir)).topics.CANONICAL).toMatchObject({ endpointDigest: endpointAuthorityDigest("ws://winner", "winner-token"), }); const afterRestart = recoveryDaemon(agentDir, bot); await afterRestart.loadTopics(); await replayResumedIdentity(afterRestart, "RESTART", "CANONICAL", { url: "ws://winner", token: "winner-token" }); bot.calls = []; await afterRestart.handleSessionMessage(afterRestart.sessions.get("RESTART")!, { type: "turn_stream", sessionId: "CANONICAL", text: "durably winner only", }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); }); test("retries a failed recovery claim without publishing its endpoint", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "INITIAL", "CANONICAL", { url: "ws://old", token: "old" }); let failWrite = true; const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (failWrite) throw new Error("simulated recovery persistence failure"); }), ); await daemon.loadTopics(); await replayResumedIdentity(daemon, "FAILED", "CANONICAL", { url: "ws://failed", token: "failed-token" }); bot.calls = []; expect(daemon.sessions.has("FAILED")).toBe(false); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); failWrite = false; await replayResumedIdentity(daemon, "RETRY", "CANONICAL", { url: "ws://retry", token: "retry-token" }); bot.calls = []; await daemon.handleSessionMessage(daemon.sessions.get("RETRY")!, { type: "turn_stream", sessionId: "CANONICAL", text: "retry routes", }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); expect((await readTopicAuthorityState(agentDir)).topics.CANONICAL).toMatchObject({ endpointDigest: endpointAuthorityDigest("ws://retry", "retry-token"), }); const afterRestart = recoveryDaemon(agentDir, bot); await afterRestart.loadTopics(); await replayResumedIdentity(afterRestart, "RESTART", "CANONICAL", { url: "ws://retry", token: "retry-token" }); bot.calls = []; await afterRestart.handleSessionMessage(afterRestart.sessions.get("RESTART")!, { type: "turn_stream", sessionId: "CANONICAL", text: "only durable retry routes", }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); }); test("a held successful recovery claim keeps live frames behind the replay barrier until durable authorization", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "INITIAL", "CANONICAL", { url: "ws://old", token: "old" }); let releaseWrite!: () => void; const writeReleased = new Promise(resolve => (releaseWrite = resolve)); let writeStarted!: () => void; const writeBlocked = new Promise(resolve => (writeStarted = resolve)); const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { writeStarted(); await writeReleased; }), ); await daemon.loadTopics(); daemon.connectSession("HELD", "ws://new", "new"); const held = daemon.sessions.get("HELD")!; held.ws.dispatchEvent(new Event("open")); const recovery = daemon.handleSessionMessage(held, { type: "event_replay_result", ok: true, id: held.replayId, generation: 2, lastSeq: 7, events: [{ payload: { type: "identity_header", sessionId: "CANONICAL", repo: "sayknow-cli", branch: "dev" } }], }); await writeBlocked; await daemon.handleSessionMessage(held, { type: "turn_stream", sessionId: "CANONICAL", text: "live after claim" }); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "live after claim"), ).toHaveLength(0); releaseWrite(); await recovery; expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "live after claim"), ).toHaveLength(1); expect((await readTopicAuthorityState(agentDir)).topics.CANONICAL).toMatchObject({ endpointDigest: endpointAuthorityDigest("ws://new", "new"), }); }); test("a failed recovery write rolls back its binding before a concurrent rename persists", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "INITIAL", "CANONICAL", { url: "ws://old", token: "old" }); const originalTopicId = bot.createdTopicThreadIds[0]!; let failRecoveryWrite = true; let releaseRecoveryWrite!: () => void; const recoveryWriteReleased = new Promise(resolve => (releaseRecoveryWrite = resolve)); let recoveryWriteReached!: () => void; const recoveryWriteBlocked = new Promise(resolve => (recoveryWriteReached = resolve)); const restarted = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!failRecoveryWrite) return; recoveryWriteReached(); await recoveryWriteReleased; throw new Error("simulated recovery persistence failure"); }), ); await restarted.loadTopics(); const failedRecovery = replayResumedIdentity(restarted, "FAILED", "CANONICAL", { url: "ws://failed", token: "failed-token", }); await recoveryWriteBlocked; const rename = restarted.handleTelegramUpdate(forumTopicEditedUpdate(903, originalTopicId, "Concurrent user name")); failRecoveryWrite = false; releaseRecoveryWrite(); await Promise.all([failedRecovery, rename]); const persisted = await readTopicAuthorityState(agentDir); expect(persisted.topics.CANONICAL).toMatchObject({ endpointDigest: endpointAuthorityDigest("ws://old", "old"), name: "Concurrent user name", nameOwner: "user", }); bot.calls = []; expect(restarted.sessions.has("FAILED")).toBe(false); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); }); test("resume recovery rejects unsafe durable topic bindings without creating or reusing a topic", async () => { const cases: Array<{ name: string; mutate: (state: any) => void; chatId?: string; url?: string; generation?: number; }> = [ { name: "cross chat", mutate: () => {}, chatId: "43" }, { name: "delete pending", mutate: state => (state.topics.CANONICAL.authorityState = "delete_pending") }, { name: "incomplete binding", mutate: state => delete state.topics.CANONICAL.endpointDigest }, { name: "persisted malformed binding marker", mutate: state => (state.topics.CANONICAL.bindingMalformed = true) }, { name: "ambiguous topic", mutate: state => (state.topics.OTHER = { ...state.topics.CANONICAL, identitySent: false }), }, ]; for (const testCase of cases) { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "INITIATOR", "CANONICAL"); const state = await readTopicAuthorityState(agentDir); testCase.mutate(state); await fs.promises.writeFile(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), JSON.stringify(state)); bot.calls = []; const resumed = recoveryDaemon(agentDir, bot, testCase.chatId); await resumed.loadTopics(); await replayResumedIdentity(resumed, "INITIATOR", "CANONICAL", { url: testCase.url, generation: testCase.generation, }); expect( bot.calls.filter(call => call.method === "createForumTopic"), testCase.name, ).toHaveLength(0); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.message_thread_id !== undefined), testCase.name, ).toHaveLength(0); } }); test("resume recovery preserves a user-owned durable topic name", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "INITIATOR", "CANONICAL"); const state = await readTopicAuthorityState(agentDir); Object.assign(state.topics.CANONICAL, { name: "My topic", nameOwner: "user", userNameUpdateId: 9 }); await fs.promises.writeFile(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), JSON.stringify(state)); bot.calls = []; const resumed = recoveryDaemon(agentDir, bot); await resumed.loadTopics(); await replayResumedIdentity(resumed, "INITIATOR", "CANONICAL"); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); expect(bot.calls.filter(call => call.method === "editForumTopic")).toHaveLength(0); expect((await readTopicAuthorityState(agentDir)).topics.CANONICAL).toMatchObject({ name: "My topic", nameOwner: "user", userNameUpdateId: 9, }); }); test("threaded mode off: frames fall back to the flat paired chat with a one-time notice", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); // Telegram explicitly confirms that Threaded Mode is unavailable, so flat // delivery is permitted for the paired private chat. bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "createForumTopic") return { ok: false, description: "Bad Request: Threaded Mode is disabled" }; if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); await daemon.handleSessionMessage(session as any, { type: "context_update", sessionId: "S", lastMessage: "hello world", }); await daemon.handleSessionMessage(session as any, { type: "action_needed", sessionId: "S", id: "ask1", kind: "ask", question: "Proceed?", options: ["Yes", "No"], }); const sends = bot.calls.filter(c => c.method === "sendMessage"); // Everything is delivered flat (no message_thread_id) since topics are unavailable. expect(sends.length).toBeGreaterThan(0); expect(sends.every(c => c.body.message_thread_id === undefined)).toBe(true); // The nudge is sent exactly once with the requested copy. const notices = sends.filter(c => String(c.body.text).includes(THREADED_FALLBACK_NOTICE)); expect(notices).toHaveLength(1); // The ask still carries its inline keyboard in flat mode. const ask = sends.find(c => String(c.body.text).includes("Proceed?")); expect(ask).toBeTruthy(); expect(ask!.body.reply_markup?.inline_keyboard?.length).toBeGreaterThan(0); }); test("topic creation transport failures fail closed without flat delivery", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = async (method, body) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "createForumTopic") throw new Error("connection reset"); return { ok: true, result: true }; }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await expect( daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }), ).rejects.toThrow("connection reset"); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); }); test("malformed topic creation success is attempted once per endpoint and fails closed", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = async (method, body) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "createForumTopic") return { ok: true, result: {} }; return { ok: true, result: true }; }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", endpointDigest: "endpoint-1", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map(), }; for (let attempt = 0; attempt < 2; attempt++) { await expect( daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }), ).rejects.toThrow("invalid message_thread_id"); } expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(1); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); await expect( daemon.handleSessionMessage({ ...session, endpointDigest: "endpoint-2" } as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }), ).rejects.toThrow("invalid message_thread_id"); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(2); }); test("cooldown-suppressed topic creation is a quiet per-attempt refusal", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = async (method, body) => { bot.calls.push({ method, body }); if (method === "createForumTopic") return undefined; return { ok: true, result: true }; }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); Object.assign(daemon, { pairedChatPrivate: true }); const warning = spyOn(logger, "warn").mockImplementation(() => {}); try { await expect((daemon as any).ensureTopic("S", "topic")).resolves.toBeUndefined(); await expect((daemon as any).ensureTopic("S", "topic")).resolves.toBeUndefined(); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(2); expect(warning).not.toHaveBeenCalled(); } finally { warning.mockRestore(); } }); test("malformed topic creation success fails closed without flat delivery", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = async (method, body) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "createForumTopic") return { ok: true, result: {} }; return { ok: true, result: true }; }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await expect( daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }), ).rejects.toThrow("invalid message_thread_id"); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); }); test("topic persistence failures fail closed without flat delivery", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); let failFirstTopicWrite = true; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, fs: topicStateFs(async () => { if (!failFirstTopicWrite) return; failFirstTopicWrite = false; throw new Error("topic persistence failed"); }), }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await expect( daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }), ).rejects.toThrow("topic persistence failed"); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); expect(bot.calls.filter(call => call.method === "deleteForumTopic")).toHaveLength(1); expect((daemon as any).topics.get("S")?.authorityState).not.toBe("active"); }); test("threaded mode off: multiple sessions share a single fallback notice", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "createForumTopic") return { ok: false, description: "Bad Request: Threaded Mode is disabled" }; if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); for (const sessionId of ["A", "B", "C"]) { await daemon.handleSessionMessage( { sessionId, token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() } as any, { type: "identity_header", sessionId, repo: "r", branch: sessionId }, ); } const sends = bot.calls.filter(c => c.method === "sendMessage"); expect(sends.every(c => c.body.message_thread_id === undefined)).toBe(true); expect(sends.filter(c => String(c.body.text).includes(THREADED_FALLBACK_NOTICE))).toHaveLength(1); }); test("threaded mode off: image_attachment uploads flat without message_thread_id", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "createForumTopic") return { ok: false, description: "Bad Request: Threaded Mode is disabled" }; if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "sendPhoto") return { ok: true, result: { message_id: bot.calls.length } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); await daemon.handleSessionMessage( { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() } as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }, ); await daemon.handleSessionMessage( { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() } as any, { type: "image_attachment", sessionId: "S", source: "computer", mime: "image/png", data: "AAAA" }, ); const photo = bot.calls.find(c => c.method === "sendPhoto"); expect(photo).toBeTruthy(); expect(photo!.body.photo).toBe("AAAA"); expect(photo!.body.message_thread_id).toBeUndefined(); const notice = bot.calls.filter( c => c.method === "sendMessage" && String(c.body.text).includes(THREADED_FALLBACK_NOTICE), ); expect(notice).toHaveLength(1); }); test("non-private chat: fails closed before topic creation or flat delivery", async () => { for (const chatType of ["supergroup", "group", "channel"]) { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); // Even if the target chat would accept forum topic creation, the paired chat // contract is private-only, so the daemon must fail closed before creating // topics or sending session content into a shared chat. bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "createForumTopic") return { ok: true, result: { message_thread_id: 777 } }; if (method === "getChat") return { ok: true, result: { type: chatType } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); await daemon.handleSessionMessage(session as any, { type: "context_update", sessionId: "S", lastMessage: "secret", }); await daemon.handleSessionMessage(session as any, { type: "action_needed", sessionId: "S", id: "ask1", kind: "ask", question: "Proceed?", options: ["Yes"], }); expect(bot.calls.filter(c => c.method === "createForumTopic")).toHaveLength(0); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); } }); test("threaded off + unresolvable getChat: fails closed", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "createForumTopic") return { ok: true, result: {} }; if (method === "getChat") throw new Error("getChat failed"); if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "context_update", sessionId: "S", lastMessage: "secret" }); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); }); test("rejected getChat backs off and warns once instead of on every delivery", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: false, error_code: 401, description: "Unauthorized" }; return { ok: true, result: true }; }) as any; let now = 1_000_000; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, now: () => now, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; const warnSpy = spyOn(logger, "warn"); const deliver = () => daemon.handleSessionMessage(session as any, { type: "context_update", sessionId: "S", lastMessage: "x" }); const getChatCalls = () => bot.calls.filter(c => c.method === "getChat").length; try { await deliver(); await deliver(); // one failure retries right away expect(getChatCalls()).toBe(2); for (let tick = 0; tick < 20; tick++) await deliver(); // repeated failure: backing off expect(getChatCalls()).toBe(2); now += 15_000; await deliver(); expect(getChatCalls()).toBe(3); now += 15_000; // backoff doubled to 30s await deliver(); expect(getChatCalls()).toBe(3); const privacyWarnings = warnSpy.mock.calls.filter(call => String(call[0]).includes("getChat privacy check")); expect(privacyWarnings).toHaveLength(1); expect(String(privacyWarnings[0]?.[0])).toContain("401"); expect(bot.calls.filter(c => c.method === "sendMessage")).toHaveLength(0); } finally { warnSpy.mockRestore(); } }); test("identity_header without a title names the topic repo/branch", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map(), }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", }); const createTopic = bot.calls.find(c => c.method === "createForumTopic"); expect(createTopic).toBeTruthy(); expect(createTopic!.body.name).toBe("sayknow-cli/dev"); }); test("identity_header with repo/branch and a title composes repo/branch - title", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map(), }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "sayknow-cli", branch: "dev", title: "Rebuild notifications", }); const createTopic = bot.calls.find(c => c.method === "createForumTopic"); expect(createTopic).toBeTruthy(); expect(createTopic!.body.name).toBe("sayknow-cli/dev - Rebuild notifications"); }); test("identity_header without title or repo falls back to the SKC session label", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "abcdef123456", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map(), }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "abcdef123456", }); const createTopic = bot.calls.find(c => c.method === "createForumTopic"); expect(createTopic).toBeTruthy(); expect(createTopic!.body.name).toBe("SKC 123456"); }); test("activity busy frame sends a typing chat action into the session topic", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; // Create the topic first so the typing action has somewhere to go. await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); await daemon.handleSessionMessage(session as any, { type: "activity", sessionId: "S", state: "busy" }); const typing = bot.calls.find(c => c.method === "sendChatAction"); expect(typing).toBeTruthy(); expect(typing!.body.action).toBe("typing"); expect(Number(typing!.body.message_thread_id)).toBeGreaterThan(0); // Idle clears busy; activity idle itself sends no chat action. bot.calls = []; await daemon.handleSessionMessage(session as any, { type: "activity", sessionId: "S", state: "idle" }); expect(bot.calls.some(c => c.method === "sendChatAction")).toBe(false); }); test("session_closed deletes the topic and resume creates a fresh visible topic", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); let now = 0; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, now: () => now, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; for (let i = 0; i < 25; i++) { await daemon.handleSessionMessage(session as any, { type: "turn_stream", sessionId: "S", phase: "finalized", text: `queued-before-delete-${i}`, }); } bot.calls = []; await daemon.handleSessionMessage(session as any, { type: "session_closed", sessionId: "S" }); const deleted = bot.calls.find(c => c.method === "deleteForumTopic"); expect(deleted).toBeTruthy(); expect(deleted!.body.message_thread_id).toBe(threadId); now = 10_000; bot.calls = []; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", title: "resumed", }); const create = bot.calls.find(c => c.method === "createForumTopic"); const send = bot.calls.find(c => c.method === "sendMessage"); expect(create).toBeTruthy(); expect(create!.body.name).toBe("r/b - resumed"); expect(send).toBeTruthy(); expect(send!.body.message_thread_id).toBeTruthy(); expect(bot.calls.some(c => c.method === "reopenForumTopic")).toBe(false); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("queued-before-delete"))).toBe( false, ); }); test("delete-pending topics fence model choices and threaded frames while active topics still deliver", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://active", "token"); const activeSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(activeSession, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const topicId = bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id; bot.calls = []; await daemon.handleSessionMessage(activeSession, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "active threaded output", }); expect(bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id).toBe(topicId); bot.calls = []; await daemon.handleSessionMessage(activeSession, { type: "control_command_result", status: "ok", sessionId: "S", message: "Select a model.", modelChoices: [{ selector: "provider/model", label: "Model" }], }); expect(bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id).toBe(topicId); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "deleteForumTopic") { bot.calls.push({ method, body, options }); return { ok: false, description: "delete outcome unknown" }; } return call(method, body, options); }; await daemon.handleSessionMessage(activeSession, { type: "session_closed", sessionId: "S" }); expect(bot.calls.find(call => call.method === "deleteForumTopic")!.body.message_thread_id).toBe(topicId); bot.calls = []; daemon.connectSession("S", "ws://resumed", "replacement-token"); const resumedSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(resumedSession, { type: "control_command_result", status: "ok", sessionId: "S", message: "Select a model.", modelChoices: [{ selector: "provider/model", label: "Model" }], }); await daemon.handleSessionMessage(resumedSession, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "must not target the delete-pending topic", }); expect(bot.calls.some(call => call.method === "createForumTopic")).toBe(false); expect(bot.calls.some(call => call.method === "sendMessage")).toBe(false); }); test("queued selected acknowledgement is rejected after its topic lease is fenced", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask", question: "Proceed?", options: ["yes"], }); const topicId = bot.createdTopicThreadIds.at(-1)!; const internals = daemon as unknown as { flushPool(): Promise; topics: { beginDelete(sessionId: string): unknown }; }; const flushPool = internals.flushPool.bind(daemon); internals.flushPool = async () => {}; bot.calls = []; await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack", commitKey: "commit", actionId: "ask", deadlineAt: Date.now() + 8_000, }); internals.topics.beginDelete("S"); internals.flushPool = flushPool; await flushPool(); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.message_thread_id === topicId)).toBe(false); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual({ type: "ask_selected_ack_result", requestId: "ack", commitKey: "commit", outcome: { status: "failed", reason: "route_missing" }, }); }); test("held threaded frame is rejected after its topic lease is fenced", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const topicId = bot.createdTopicThreadIds.at(-1)!; const internals = daemon as unknown as { flushPool(): Promise; topics: { beginDelete(sessionId: string): unknown }; }; const flushPool = internals.flushPool.bind(daemon); internals.flushPool = async () => {}; bot.calls = []; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "held output", }); internals.topics.beginDelete("S"); internals.flushPool = flushPool; await flushPool(); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.message_thread_id === topicId)).toBe(false); }); test("held rich fallback cannot send to a fenced topic", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: true }, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const topicId = bot.createdTopicThreadIds.at(-1)!; const richStarted = Promise.withResolvers(); const releaseRich = Promise.withResolvers(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendRichMessage") { bot.calls.push({ method, body, options }); richStarted.resolve(); await releaseRich.promise; return { ok: false, description: "rich unavailable" }; } return call(method, body, options); }; bot.calls = []; const delivery = daemon.handleSessionMessage(session as never, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: "# held rich fallback", }); await richStarted.promise; (daemon as unknown as { topics: { beginDelete(sessionId: string): unknown } }).topics.beginDelete("S"); releaseRich.resolve(); await delivery; expect(bot.calls.some(call => call.method === "sendMessage" && call.body.message_thread_id === topicId)).toBe(false); }); test("held edit fallback cannot send to a fenced topic", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); await daemon.handleSessionMessage(session as never, { type: "turn_stream", sessionId: "S", phase: "live", text: "live", messageRef: "held-edit", }); const topicId = bot.createdTopicThreadIds.at(-1)!; const editStarted = Promise.withResolvers(); const releaseEdit = Promise.withResolvers(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "editMessageText") { bot.calls.push({ method, body, options }); editStarted.resolve(); await releaseEdit.promise; throw new Error("edit rejected"); } return call(method, body, options); }; bot.calls = []; const delivery = daemon.handleSessionMessage(session as never, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: "final", messageRef: "held-edit", }); await editStarted.promise; (daemon as unknown as { topics: { beginDelete(sessionId: string): unknown } }).topics.beginDelete("S"); releaseEdit.resolve(); await delivery; expect(bot.calls.some(call => call.method === "sendMessage" && call.body.message_thread_id === topicId)).toBe(false); }); test("held draft delivery cannot continue to a fenced topic", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: true }, richDraft: { enabled: true }, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const topicId = bot.createdTopicThreadIds.at(-1)!; const draftStarted = Promise.withResolvers(); const releaseDraft = Promise.withResolvers(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendRichMessageDraft") { bot.calls.push({ method, body, options }); draftStarted.resolve(); await releaseDraft.promise; return { ok: true, result: true }; } return call(method, body, options); }; bot.calls = []; const delivery = daemon.handleSessionMessage(session as never, { type: "turn_stream", sessionId: "S", phase: "live", text: "held draft", }); await draftStarted.promise; (daemon as unknown as { topics: { beginDelete(sessionId: string): unknown } }).topics.beginDelete("S"); releaseDraft.resolve(); await delivery; expect(bot.calls.some(call => call.method === "sendMessage" && call.body.message_thread_id === topicId)).toBe(false); }); test("held topic-name reconciliation cannot edit a fenced topic", async () => { const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let holdTopicWrite = false; const fsImpl = topicStateFs(async () => { if (!holdTopicWrite) return; writeStarted.resolve(); await releaseWrite.promise; }); const { bot, daemon, threadId } = await identityTopicHarness({ fs: fsImpl }); bot.calls = []; holdTopicWrite = true; const update = daemon.handleTelegramUpdate(forumTopicEditedUpdate(1, threadId, "held name")); await writeStarted.promise; (daemon as unknown as { topics: { beginDelete(sessionId: string): unknown } }).topics.beginDelete("S"); releaseWrite.resolve(); await update; expect(bot.calls.some(call => call.method === "editForumTopic" && call.body.message_thread_id === threadId)).toBe( false, ); }); test("delete-pending identity owners are not selected for forwarding", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const owner = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(owner as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); const internals = daemon as unknown as { topics: { beginDelete(sessionId: string): unknown }; topicOwnerForIdentity(msg: { repo: string; branch: string }): string | undefined; }; internals.topics.beginDelete("S"); expect(internals.topicOwnerForIdentity({ repo: "r", branch: "b" })).toBeUndefined(); }); test("active action controls still target their active topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, rich: { enabled: false }, }); daemon.connectSession("S", "ws://s", "token"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Continue?", options: ["yes"], controls: [{ id: "navigation_forward", kind: "navigation", label: "Next", enabled: true }], }); const sent = bot.calls.find(call => call.method === "sendMessage")!; expect(sent.body.message_thread_id).toBe(bot.createdTopicThreadIds.at(-1)); expect(sent.body.reply_markup.inline_keyboard.flat().map((button: { text: string }) => button.text)).toContain( "Next", ); }); test("session_closed clears reply message routes for the closed session", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); daemon.connectSession("T", "ws://t", "tt"); daemon.messageRoutes.set("s-ask", { sessionId: "S", actionId: "ask" }); daemon.messageRoutes.set("s-other", { sessionId: "S", actionId: "other" }); daemon.messageRoutes.set("t-ask", { sessionId: "T", actionId: "ask" }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "session_closed", sessionId: "S" }); expect([...daemon.messageRoutes.values()].some(route => route.sessionId === "S")).toBe(false); expect(daemon.messageRoutes.get("t-ask")).toEqual({ sessionId: "T", actionId: "ask" }); }); test("session_closed revokes persisted ask aliases and pending replies before settling its topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const deleteStarted = Promise.withResolvers(); const releaseDelete = Promise.withResolvers(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "deleteForumTopic") { bot.calls.push({ method, body, options }); deleteStarted.resolve(); await releaseDelete.promise; return { ok: true, result: true }; } return call(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, rich: { enabled: false }, }); daemon.connectSession("LOGICAL", "ws://logical", "token"); const session = daemon.sessions.get("LOGICAL")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask", question: "Continue?", options: ["yes"], }); const sent = bot.calls.find(call => call.method === "sendMessage" && call.body.reply_markup)!; const alias = sent.body.reply_markup.inline_keyboard[0][0].callback_data; const close = daemon.handleSessionMessage(session, { type: "session_closed", sessionId: "LOGICAL" }); await deleteStarted.promise; const aliases = JSON.parse(fs.readFileSync(daemonPaths(agentDir).aliases, "utf8")); expect(Object.values(aliases.routes).some((route: any) => route.sessionId === "LOGICAL")).toBe(false); expect(session.pending.has("ask")).toBe(false); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadAliases(); await restarted.handleTelegramUpdate({ update_id: 900_001, callback_query: { id: "closed-ask", data: alias, message: { chat: { id: 42 } } }, }); expect((restarted as any).aliasTable.get(alias)).toBeUndefined(); releaseDelete.resolve(); await close; }); test("closing endpoint stays fenced after delete settlement until final persistence and teardown", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const finalWriteStarted = Promise.withResolvers(); const releaseFinalWrite = Promise.withResolvers(); let holdFinalWrite = false; const fsImpl = topicStateFs(async () => { if (!holdFinalWrite) return; finalWriteStarted.resolve(); await releaseFinalWrite.promise; }); const bot = new FakeBotApi(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "deleteForumTopic") holdFinalWrite = true; return call(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, fs: fsImpl, rich: { enabled: false }, }); daemon.connectSession("S", "ws://closing", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask", question: "Continue?", options: ["yes"], }); const ask = bot.calls.find(call => call.method === "sendMessage" && call.body.reply_markup)!; const threadId = ask.body.message_thread_id; const alias = ask.body.reply_markup.inline_keyboard[0][0].callback_data; bot.calls = []; const close = daemon.handleSessionMessage(session, { type: "session_closed", sessionId: "S" }); await finalWriteStarted.promise; const socket = session.ws as unknown as FakeWs; socket.sent.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "must not recreate after settlement", }); await daemon.handleTelegramUpdate({ update_id: 900_002, message: { chat: { id: 42 }, message_thread_id: threadId, text: "old reply", message_id: 10 }, }); await daemon.handleTelegramUpdate({ update_id: 900_003, callback_query: { id: "old-callback", data: alias, message: { chat: { id: 42 } } }, }); expect(socket.sent).toEqual([]); expect(bot.calls.some(call => call.method === "createForumTopic")).toBe(false); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.message_thread_id === threadId)).toBe( false, ); releaseFinalWrite.resolve(); await close; }); test("session_closed tombstones its endpoint generation so scans do not recreate an empty topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "repo"); await registerNotificationRoot({ settings: s, cwd, sessionId: "S" }); const endpointDir = path.join(cwd, ".skc", "state", "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync(path.join(endpointDir, "S.json"), JSON.stringify({ url: "ws://live", token: "ts", pid: 4242 })); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, pidAlive: (pid: number) => pid === 4242, }); const waitForCreate = async () => { for (let i = 0; i < 20; i++) { const create = bot.calls.find(c => c.method === "createForumTopic"); if (create) return create; await new Promise(resolve => setTimeout(resolve, 1)); } throw new Error("createForumTopic was not called"); }; const waitForTopicRecord = async () => { const topicsFile = path.join(daemonPaths(agentDir).dir, "telegram-topics.json"); for (let i = 0; i < 20; i++) { if (fs.existsSync(topicsFile) && fs.readFileSync(topicsFile, "utf8").includes('"S"')) return; await new Promise(resolve => setTimeout(resolve, 1)); } throw new Error("topic registry was not persisted"); }; await daemon.scanRoots(); expect(FakeWs.instances).toHaveLength(1); FakeWs.instances[0]!.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "event_replay_result", ok: true, id: "telegram-startup-replay:S", generation: 1, lastSeq: 0, events: [], }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", }); await waitForCreate(); await waitForTopicRecord(); bot.calls = []; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "session_closed", sessionId: "S" }); expect(bot.calls.some(c => c.method === "deleteForumTopic")).toBe(true); expect(daemon.sessions.has("S")).toBe(false); bot.calls = []; await daemon.scanRoots(); expect(FakeWs.instances).toHaveLength(1); expect(bot.calls.some(c => c.method === "createForumTopic")).toBe(false); fs.writeFileSync(path.join(endpointDir, "S.json"), JSON.stringify({ url: "ws://resumed", token: "ts2", pid: 4242 })); await daemon.scanRoots(); expect(FakeWs.instances).toHaveLength(2); FakeWs.instances[1]!.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "event_replay_result", ok: true, id: "telegram-startup-replay:S", generation: 1, lastSeq: 0, events: [], }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S" }); const resumedCreate = await waitForCreate(); expect(resumedCreate.body.name).toBe("SKC S"); }); test("inbound thread message gets a queued reaction, flipped to consumed on ack", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; // Create the topic and learn its thread id from the pinned identity message. await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 7, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 555, text: "steer me" }, }); // The user turn is forwarded to the session… expect(JSON.parse(FakeWs.instances[0]!.sent[0]!)).toMatchObject({ type: "user_message", text: "steer me", updateId: 7, }); // …and the originating message gets the queued reaction. const queued = bot.calls.find(c => c.method === "setMessageReaction"); expect(queued).toBeTruthy(); expect(queued!.body.message_id).toBe(555); expect(queued!.body.reaction[0].emoji).toBe("👀"); bot.calls = []; await daemon.handleSessionMessage(session, { type: "inbound_ack", sessionId: "S", updateId: 7, state: "consumed" }); const consumed = bot.calls.find(c => c.method === "setMessageReaction"); expect(consumed).toBeTruthy(); expect(consumed!.body.message_id).toBe(555); expect(consumed!.body.reaction[0].emoji).toBe("✅"); }); test("inbound photo is downloaded and forwarded as an image in the user_message", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const fetchImpl = (async () => new Response(new Uint8Array([1, 2, 3, 4]))) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, fetchImpl, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleTelegramUpdate({ update_id: 11, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 100, photo: [{ file_id: "small" }, { file_id: "large" }], }, }); const frame = JSON.parse(FakeWs.instances[0]!.sent[0]!); expect(frame.type).toBe("user_message"); expect(frame.images).toHaveLength(1); expect(frame.images[0].mime).toBe("image/jpeg"); expect(Buffer.from(frame.images[0].data, "base64")).toEqual(Buffer.from([1, 2, 3, 4])); // The largest photo size is the one resolved/downloaded. expect(bot.calls.some(c => c.method === "getFile" && c.body.file_id === "large")).toBe(true); }); test("inbound attachment download enforces declared and streamed byte ceilings", async () => { let requests = 0; let nonSuccessCancelled = false; let oversizedCancelled = false; let nonSuccessSignal: AbortSignal | undefined; const nonSuccessBody = new ReadableStream({ cancel() { nonSuccessCancelled = true; }, }); const oversizedBody = new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array(4)); controller.enqueue(new Uint8Array([1])); }, cancel() { oversizedCancelled = true; }, }); const responses = [ new Response(nonSuccessBody, { status: 503 }), new Response(null, { headers: { "content-length": "5" } }), new Response(oversizedBody), new Response(new Uint8Array([1, 2, 3, 4]), { headers: { "content-length": "4" } }), ]; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", fetchImpl: (async (_url, init) => { if (requests === 0) nonSuccessSignal = init?.signal ?? undefined; return responses[requests++]!; }) as typeof fetch, }); const download = attachmentAccess(daemon).downloadTelegramFile.bind(daemon); await expect(download("docs/unavailable.bin", 4)).resolves.toEqual({ failure: "download_failed" }); expect(nonSuccessCancelled).toBe(true); expect(nonSuccessSignal?.aborted).toBe(true); await expect(download("docs/declared.bin", 4)).resolves.toEqual({ failure: "too_large" }); await expect(download("docs/chunked.bin", 4)).resolves.toEqual({ failure: "too_large" }); expect(oversizedCancelled).toBe(true); await expect(download("docs/exact.bin", 4)).resolves.toEqual({ bytes: Buffer.from([1, 2, 3, 4]) }); }); test("inbound attachment download aborts when its single deadline expires", async () => { let expire: (() => void) | undefined; let observedSignal: AbortSignal | undefined; const fetchImpl = ((_url: string | URL | Request, init?: RequestInit) => { observedSignal = init?.signal ?? undefined; const pending = Promise.withResolvers(); observedSignal?.addEventListener("abort", () => pending.reject(observedSignal?.reason), { once: true }); return pending.promise; }) as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", fetchImpl, setTimeoutImpl: ((callback: () => void) => { expire = callback; return 1; }) as unknown as typeof setTimeout, clearTimeoutImpl: (() => undefined) as unknown as typeof clearTimeout, }); const pending = attachmentAccess(daemon).downloadTelegramFile("docs/slow.bin"); await Promise.resolve(); expire?.(); await expect(pending).resolves.toEqual({ failure: "download_failed" }); expect(observedSignal?.aborted).toBe(true); }); test("one inbound attachment deadline covers getFile and a stalled response body read", async () => { let expire: (() => void) | undefined; let cancelled = false; let getFileSignal: AbortSignal | undefined; let fetchSignal: AbortSignal | undefined; let timerCount = 0; const body = new ReadableStream({ cancel() { cancelled = true; }, }); const botApi: BotApi = { async call(method, _body, opts): Promise { if (method === "getFile") { getFileSignal = opts?.signal; return { ok: true, result: { file_path: "docs/stalled-body.bin", file_size: 1 } }; } return { ok: true, result: true }; }, }; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi, fetchImpl: (async (_url, init) => { fetchSignal = init?.signal ?? undefined; return new Response(body); }) as typeof fetch, setTimeoutImpl: ((callback: () => void) => { timerCount++; expire = callback; return 1; }) as unknown as typeof setTimeout, clearTimeoutImpl: (() => undefined) as unknown as typeof clearTimeout, }); const pending = attachmentAccess(daemon).resolveInboundAttachment( { fileId: "slow", kind: "photo", mime: "image/jpeg" }, "S", ); while (!fetchSignal) await Promise.resolve(); expire?.(); const result = await pending; expect(result.images).toHaveLength(0); expect(result.fileNotes[0]).toContain("attachment download failed"); expect(timerCount).toBe(1); expect(getFileSignal?.aborted).toBe(true); expect(fetchSignal?.aborted).toBe(true); expect(cancelled).toBe(true); }); test("inbound attachments enforce per-session count and cumulative byte budgets", async () => { const resolve = (daemon: TelegramNotificationDaemon, fileId: string, kind: "photo" | "document" = "photo") => attachmentAccess(daemon).resolveInboundAttachment( { fileId, kind, mime: kind === "photo" ? "image/jpeg" : "application/octet-stream", fileName: kind === "document" ? "file.bin" : undefined, }, "S", ); const harness = (sizes: number[]) => { let getFileCalls = 0; let fetchCalls = 0; const botApi: BotApi = { async call(method: string): Promise { if (method === "getFile") return { ok: true, result: { file_path: "photos/file.jpg", file_size: sizes[getFileCalls++] } }; return { ok: true, result: true }; }, }; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi, fetchImpl: (async () => { fetchCalls++; return new Response(new Uint8Array([1])); }) as unknown as typeof fetch, }); return { daemon, calls: () => ({ getFileCalls, fetchCalls }) }; }; const count = harness([1, 20 * 1024 * 1024 + 1, ...Array(20).fill(1)]); const writeFile = fs.promises.writeFile.bind(fs.promises); let partialPath: string | undefined; const writeSpy = vi.spyOn(fs.promises, "writeFile").mockImplementationOnce(async (file, data, options) => { partialPath = String(file); await writeFile(file, data, options); throw new Error("disk full after partial write"); }); const writeFailed = await resolve(count.daemon, "write-fails", "document"); writeSpy.mockRestore(); expect(writeFailed.images).toHaveLength(0); expect(writeFailed.fileNotes[0]).toContain("attachment error"); expect(partialPath).toBeDefined(); expect(fs.existsSync(partialPath!)).toBe(false); expect((await resolve(count.daemon, "oversize")).images).toHaveLength(0); for (let i = 0; i < 20; i++) expect((await resolve(count.daemon, `ok-${i}`)).images).toHaveLength(1); const countRejected = await resolve(count.daemon, "count-exhausted"); expect(countRejected.images).toHaveLength(0); expect(countRejected.fileNotes[0]).toContain("session attachment limit"); expect(count.calls()).toEqual({ getFileCalls: 22, fetchCalls: 21 }); const mib = 1024 * 1024; const total = harness([20 * mib, 20 * mib, 10 * mib]); const results = await Promise.all(["a", "b", "c", "total-exhausted"].map(id => resolve(total.daemon, id))); expect(results.slice(0, 3).every(result => result.images.length === 1)).toBe(true); expect(results[3]!.images).toHaveLength(0); expect(results[3]!.fileNotes[0]).toContain("session attachment limit"); expect(total.calls()).toEqual({ getFileCalls: 3, fetchCalls: 3 }); }); test("redacts token-shaped download URLs from attachment failure logs", async () => { const botToken = "123456789:ABCDEF_ghijklmnopqrstuvwxyz012345"; let downloadedUrl = ""; const fetchImpl = (async (url: string | URL | Request) => { downloadedUrl = String(url); throw new Error(`fetch failed: ${downloadedUrl}`); }) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken, chatId: "42", fetchImpl, }); const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {}); const errorSpy = vi.spyOn(logger, "error").mockImplementation(() => {}); try { await expect(attachmentAccess(daemon).downloadTelegramFile("photos/file.jpg")).resolves.toEqual({ failure: "download_failed", }); const logged = JSON.stringify([...warnSpy.mock.calls, ...errorSpy.mock.calls]); expect(downloadedUrl).toBe(`https://api.telegram.org/file/bot${botToken}/photos/file.jpg`); expect(warnSpy).toHaveBeenCalledTimes(1); expect(errorSpy).not.toHaveBeenCalled(); expect(logged).toContain(""); expect(logged).not.toContain(botToken); expect(logged).not.toMatch(/\d{6,}:[A-Za-z0-9_-]{20,}/); } finally { warnSpy.mockRestore(); errorSpy.mockRestore(); } }); test("redacts token-shaped URLs from getUpdates poll failure logs", async () => { const botToken = "123456789:ABCDEF_ghijklmnopqrstuvwxyz012345"; const botApi: BotApi = { async call(method: string): Promise { if (method === "getUpdates") { throw new Error(`fetch failed: https://api.telegram.org/bot${botToken}/getUpdates`); } return { ok: true, result: [] }; }, }; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken, chatId: "42", botApi, setTimeoutImpl: ((callback: () => void) => { callback(); return 0; }) as unknown as typeof setTimeout, }); const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {}); const errorSpy = vi.spyOn(logger, "error").mockImplementation(() => {}); try { await expect(daemon.pollOnce()).resolves.toBe(0); const logged = JSON.stringify([...warnSpy.mock.calls, ...errorSpy.mock.calls]); expect(warnSpy).not.toHaveBeenCalled(); expect(errorSpy).toHaveBeenCalledTimes(1); expect(logged).toContain(""); expect(logged).not.toContain(botToken); expect(logged).not.toMatch(/\d{6,}:[A-Za-z0-9_-]{20,}/); } finally { warnSpy.mockRestore(); errorSpy.mockRestore(); } }); test("inbound document is saved to a tmp file and its path injected into the text", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const fetchImpl = (async () => new Response(new Uint8Array([9, 9, 9]))) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, fetchImpl, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleTelegramUpdate({ update_id: 12, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 101, caption: "look", document: { file_id: "doc-1", mime_type: "application/pdf", file_name: "report.pdf" }, }, }); const frame = JSON.parse(FakeWs.instances[0]!.sent[0]!); expect(frame.type).toBe("user_message"); expect(frame.images).toHaveLength(0); expect(frame.text).toContain("look"); const match = String(frame.text).match(/saved to (\S+report\.pdf)/); expect(match).toBeTruthy(); expect(fs.existsSync(match![1]!)).toBe(true); expect(fs.readFileSync(match![1]!)).toEqual(Buffer.from([9, 9, 9])); // Security: every platform uses a private, unguessable per-session directory // and an exclusive random file name. POSIX additionally exposes the requested // 0700/0600 ownership bits; Windows does not represent ACLs in stat().mode. const dest = match![1]!; const attachmentDir = path.dirname(dest); expect(attachmentDir).not.toBe(os.tmpdir()); expect(path.basename(attachmentDir)).toMatch(/^skc-telegram-[A-Za-z0-9_-]+$/); expect(path.basename(dest)).toMatch(/^[0-9a-f]{16}-report\.pdf$/); if (process.platform !== "win32") { const fileMode = fs.statSync(dest).mode & 0o777; const dirMode = fs.statSync(attachmentDir).mode & 0o777; expect(fileMode).toBe(0o600); expect(fileMode & 0o077).toBe(0); expect(dirMode & 0o077).toBe(0); } expect(dest.startsWith(os.tmpdir())).toBe(true); }); test("inbound document with a path-traversal filename stays sandboxed in the private temp dir", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const fetchImpl = (async () => new Response(new Uint8Array([7]))) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, fetchImpl, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleTelegramUpdate({ update_id: 21, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 200, document: { file_id: "doc-evil", mime_type: "application/octet-stream", file_name: "../../../etc/passwd" }, }, }); const frame = JSON.parse(FakeWs.instances[0]!.sent[0]!); const match = String(frame.text).match(/saved to (\S+)/); expect(match).toBeTruthy(); const dest = match![1]!; const base = path.basename(dest); const dir = path.dirname(dest); // The attacker-controlled name must be sanitized so it cannot traverse: // no path separators and no ".." segments survive. expect(base.includes("/")).toBe(false); expect(base.includes("\\")).toBe(false); expect(base).not.toContain(".."); // The real saved file lives directly inside the private per-session temp dir // (under the system temp root), not at the attacker-referenced location. expect(path.dirname(fs.realpathSync(dest))).toBe(fs.realpathSync(dir)); expect(dir.startsWith(os.tmpdir())).toBe(true); expect(fs.realpathSync(dest)).not.toBe("/etc/passwd"); }); test("daemon attachment temp dirs are removed by the shutdown cleanup path", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const fetchImpl = (async () => new Response(new Uint8Array([1, 1]))) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, fetchImpl, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); const threadId = bot.calls.find(c => c.method === "sendMessage")!.body.message_thread_id; await daemon.handleTelegramUpdate({ update_id: 22, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 201, document: { file_id: "doc-x", mime_type: "application/pdf", file_name: "keep.pdf" }, }, }); const frame = JSON.parse(FakeWs.instances[0]!.sent[0]!); const dir = path.dirname(String(frame.text).match(/saved to (\S+)/)![1]!); expect(fs.existsSync(dir)).toBe(true); // run()'s `finally` invokes cleanupAllAttachmentDirs() on daemon shutdown; // exercise that exact cleanup path here. await (daemon as any).cleanupAllAttachmentDirs(); expect(fs.existsSync(dir)).toBe(false); }); test("outbound file_attachment frame triggers a sendDocument upload to the topic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); bot.calls = []; const data = Buffer.from([5, 6, 7]).toString("base64"); await daemon.handleSessionMessage(session, { type: "file_attachment", sessionId: "S", name: "out.pdf", mime: "application/pdf", data, caption: "here", }); const doc = bot.calls.find(c => c.method === "sendDocument"); expect(doc).toBeTruthy(); expect(doc!.body.document).toBe(data); expect(doc!.body.fileName).toBe("out.pdf"); expect(doc!.body.mime).toBe("application/pdf"); expect(Number(doc!.body.message_thread_id)).toBeGreaterThan(0); }); describe("telegram daemon reconnect reconciliation", () => { function endpointFor(agentDir: string, cwd: string, s: Settings, sessionId: string) { return (async () => { await registerNotificationRoot({ settings: s, cwd, sessionId }); const roots = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[] }; const dir = path.join(roots.roots[0]!, "sdk"); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, `${sessionId}.json`), JSON.stringify({ url: "ws://s", token: "ts" })); })(); } test("identity-guarded replacement survives delayed old close and old message", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await endpointFor(agentDir, path.join(agentDir, "cwd"), s, "S"); let now = 0; const liveness: Array<() => void> = []; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, now: () => now, setIntervalImpl: ((cb: () => void) => { liveness.push(cb); return 0; }) as any, clearIntervalImpl: (() => {}) as any, }); await daemon.scanRoots(); FakeWs.instances[0]!.emit({ type: "hello", protocolVersion: 2, capabilities: ["client_ping_pong"] }); now += 25_000; for (const cb of liveness) cb(); await daemon.scanRoots(); const replacement = daemon.sessions.get("S"); expect(FakeWs.instances).toHaveLength(2); expect(replacement?.ws).toBe(FakeWs.instances[1] as unknown as WebSocket); // Delayed old close from the superseded socket must not delete the replacement. FakeWs.instances[0]!.dispatchEvent(new Event("close")); expect(daemon.sessions.get("S")).toBe(replacement); // Delayed old message from the superseded socket must not produce a send. bot.calls = []; FakeWs.instances[0]!.emit({ type: "action_needed", kind: "ask", id: "old", question: "Q", options: ["Y"] }); await Promise.resolve(); expect(bot.calls).toHaveLength(0); }); test("legacy server without ping/pong capability does not start ack liveness", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await endpointFor(agentDir, path.join(agentDir, "cwd"), s, "S"); let now = 0; const liveness: Array<() => void> = []; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, now: () => now, setIntervalImpl: ((cb: () => void) => { liveness.push(cb); return 0; }) as any, clearIntervalImpl: (() => {}) as any, }); await daemon.scanRoots(); // Legacy server: advertises capabilities WITHOUT client_ping_pong. FakeWs.instances[0]!.emit({ type: "hello", protocolVersion: 1, capabilities: ["threaded"] }); expect(liveness).toHaveLength(0); now += 100_000; for (const cb of liveness) cb(); await daemon.scanRoots(); // No ack-based force-drop: the single original socket remains; half-open ack // recovery is simply unavailable for a non-capable server. expect(FakeWs.instances).toHaveLength(1); expect(daemon.sessions.has("S")).toBe(true); }); test("AC-3/AC-5: after reconnect a replayed ask renders one re-ask and future frames flow", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await endpointFor(agentDir, path.join(agentDir, "cwd"), s, "S"); let now = 0; const liveness: Array<() => void> = []; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, now: () => now, setIntervalImpl: ((cb: () => void) => { liveness.push(cb); return 0; }) as any, clearIntervalImpl: (() => {}) as any, }); await daemon.scanRoots(); FakeWs.instances[0]!.emit({ type: "hello", protocolVersion: 2, capabilities: ["client_ping_pong"] }); now += 25_000; for (const cb of liveness) cb(); await daemon.scanRoots(); const replacement = daemon.sessions.get("S")!; expect(replacement.ws).toBe(FakeWs.instances[1] as unknown as WebSocket); // AC-3: the native server replays its single buffered ask to the fresh // client; the daemon renders exactly one fresh re-ask in the topic. bot.calls = []; await daemon.handleSessionMessage(replacement, { type: "action_needed", kind: "ask", id: "ask1", question: "Resume?", options: ["Yes", "No"], }); const reAsks = bot.calls.filter(c => c.method === "sendMessage" && c.body.reply_markup?.inline_keyboard); expect(reAsks).toHaveLength(1); // AC-5: future streamed frames after reconnect are delivered to the topic. bot.calls = []; await daemon.handleSessionMessage(replacement, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "post-reconnect output", }); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("post-reconnect"))).toBe( true, ); }); }); describe("telegram daemon reconnect answer routing", () => { test("AC-4: a button tap after reconnect routes a reply through the replacement socket", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "cwd"); await registerNotificationRoot({ settings: s, cwd, sessionId: "S" }); const roots = JSON.parse(fs.readFileSync(daemonPaths(agentDir).roots, "utf8")) as { roots: string[] }; const dir = path.join(roots.roots[0]!, "sdk"); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, "S.json"), JSON.stringify({ url: "ws://s", token: "ts" })); let now = 0; const liveness: Array<() => void> = []; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, now: () => now, setIntervalImpl: ((cb: () => void) => { liveness.push(cb); return 0; }) as any, clearIntervalImpl: (() => {}) as any, }); await daemon.scanRoots(); FakeWs.instances[0]!.emit({ type: "hello", protocolVersion: 2, capabilities: ["client_ping_pong"] }); now += 25_000; for (const cb of liveness) cb(); await daemon.scanRoots(); const replacement = daemon.sessions.get("S")!; // The native server replays the buffered ask to the reconnected client. await daemon.handleSessionMessage(replacement, { type: "action_needed", kind: "ask", id: "ask1", question: "Resume?", options: ["Yes", "No"], }); const alias = bot.calls.find(c => c.method === "sendMessage" && c.body.reply_markup)!.body.reply_markup .inline_keyboard[0][0].callback_data; // A button tap on the fresh re-ask must route a reply over the new socket. await daemon.handleTelegramUpdate({ update_id: 1, callback_query: { id: "cb", data: alias, message: { chat: { id: 42 } } }, }); const sent = (replacement.ws as unknown as { sent: string[] }).sent; const replyFrame = sent.map(x => JSON.parse(x)).find(m => m.type === "reply"); expect(replyFrame).toEqual({ type: "reply", id: "ask1", answer: 0, token: "ts" }); }); }); test("pollOnce resolves to 0 when the in-flight getUpdates is aborted", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = { call: (_method: string, _body: unknown, opts?: { signal?: AbortSignal }) => new Promise((_resolve, reject) => { opts?.signal?.addEventListener("abort", () => reject(Object.assign(new Error("The operation was aborted"), { name: "AbortError" })), ); }), }; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const ac = new AbortController(); const pending = daemon.pollOnce(ac.signal); ac.abort(); expect(await pending).toBe(0); }); test("pollOnce backs off on a Telegram 409 conflict instead of processing updates", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const sleeps: number[] = []; const bot = { call: async () => ({ ok: false, error_code: 409, description: "Conflict: terminated by other getUpdates request", }), }; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, setTimeoutImpl: ((cb: () => void, ms?: number) => { sleeps.push(ms ?? 0); cb(); return 0; }) as unknown as typeof setTimeout, }); expect(await daemon.pollOnce()).toBe(0); expect(await daemon.pollOnce()).toBe(0); expect(await daemon.pollOnce()).toBe(0); expect(await daemon.pollOnce()).toBe(0); expect(await daemon.pollOnce()).toBe(0); expect(await daemon.pollOnce()).toBe(0); expect(sleeps).toHaveLength(6); expect(sleeps[0]).toBe(1_000); for (let index = 1; index < sleeps.length; index++) { if (sleeps[index] < 10_000) expect(sleeps[index]).toBeGreaterThanOrEqual(sleeps[index - 1] * 1.5); expect(sleeps[index]).toBeLessThanOrEqual(Math.min(sleeps[index - 1] * 2, 10_000)); } }); test("TelegramUpdatePoller logs getUpdates failures only on transition and reports suppressed recovery", async () => { const sleeps: number[] = []; let calls = 0; const bot = { async call() { calls += 1; if (calls <= 3) throw new Error(`network-${calls}`); return { ok: true, result: [] }; }, }; const errorSpy = spyOn(logger, "error").mockImplementation(() => {}); const infoSpy = spyOn(logger, "info").mockImplementation(() => {}); try { const poller = new TelegramUpdatePoller({ botApi: bot, runtime: { sleep: async (ms: number) => void sleeps.push(ms) } as any, backoff: { next: () => 500, reset() {} } as any, processUpdate: async () => "consumed", }); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); const getUpdatesFailures = errorSpy.mock.calls.filter( call => call[0] === "notifications daemon: getUpdates failed", ); expect(getUpdatesFailures).toHaveLength(1); expect(getUpdatesFailures[0]?.[1]).toMatchObject({ error: "Error: network-1", suppressedCount: 0 }); const recoveries = infoSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates recovered", ); expect(recoveries).toHaveLength(1); expect(recoveries[0]?.[1]).toMatchObject({ from: "getUpdates_failed", suppressedCount: 2, updateCount: 0 }); expect(sleeps).toEqual([1_000, 1_000, 1_000]); } finally { errorSpy.mockRestore(); infoSpy.mockRestore(); } }); test("TelegramUpdatePoller treats non-conflict API failures as unhealthy until a verified success", async () => { const sleeps: number[] = []; let calls = 0; const bot = { async call() { calls += 1; if (calls <= 2) return { ok: false, error_code: 502, description: "Bad Gateway" }; return { ok: true, result: [] }; }, }; const errorSpy = spyOn(logger, "error").mockImplementation(() => {}); const infoSpy = spyOn(logger, "info").mockImplementation(() => {}); try { const poller = new TelegramUpdatePoller({ botApi: bot, runtime: { sleep: async (ms: number) => void sleeps.push(ms) } as any, backoff: { next: () => 500, reset() {} } as any, processUpdate: async () => "consumed", }); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); const apiFailures = errorSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates API failed", ); expect(apiFailures).toHaveLength(1); expect(apiFailures[0]?.[1]).toMatchObject({ errorCode: 502, description: "Bad Gateway", suppressedCount: 0, }); const recoveries = infoSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates recovered", ); expect(recoveries).toHaveLength(1); expect(recoveries[0]?.[1]).toMatchObject({ from: "api_failure", suppressedCount: 1, updateCount: 0 }); expect(sleeps).toEqual([1_000, 1_000]); } finally { errorSpy.mockRestore(); infoSpy.mockRestore(); } }); test("TelegramUpdatePoller pauses polling with exponential backoff while Telegram rejects the bot token", async () => { let now = 0; const sleeps: number[] = []; let calls = 0; let rejectToken = true; const bot = { async call() { calls += 1; return rejectToken ? { ok: false, error_code: 401, description: "Unauthorized" } : { ok: true, result: [] }; }, }; const transitions: Array = []; const errorSpy = spyOn(logger, "error").mockImplementation(() => {}); const infoSpy = spyOn(logger, "info").mockImplementation(() => {}); try { const poller = new TelegramUpdatePoller({ botApi: bot, runtime: { now: () => now, sleep: async (ms: number) => { sleeps.push(ms); now += ms; }, } as any, backoff: { next: () => 500, reset() {} } as any, processUpdate: async () => "consumed", onAuthRejected: rejection => void transitions.push(rejection), }); const pollUntilCall = async (target: number) => { for (let i = 0; i < 1_000 && calls < target; i++) await poller.pollOnce(); expect(calls).toBe(target); }; expect(await poller.pollOnce()).toBe(0); expect(calls).toBe(1); // Paused: no Bot API request until the 60s window elapses, in bounded wait steps. await pollUntilCall(2); expect(sleeps).toEqual([25_000, 25_000, 10_000]); expect(now).toBe(60_000); // Second rejection doubles the window. sleeps.length = 0; await pollUntilCall(3); expect(now).toBe(180_000); expect(sleeps.reduce((a, b) => a + b, 0)).toBe(120_000); rejectToken = false; sleeps.length = 0; await pollUntilCall(4); expect(now).toBe(420_000); // Healthy again: the next poll calls Telegram immediately. await poller.pollOnce(); expect(calls).toBe(5); expect(transitions).toEqual([{ errorCode: 401, description: "Unauthorized" }, undefined]); const rejections = errorSpy.mock.calls.filter(call => String(call[0]).includes("rejected the bot token")); expect(rejections).toHaveLength(1); expect(rejections[0]?.[1]).toMatchObject({ errorCode: 401, retryInMs: 60_000 }); const recoveries = infoSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates recovered", ); expect(recoveries[0]?.[1]).toMatchObject({ from: "auth_rejected", suppressedCount: 2 }); } finally { errorSpy.mockRestore(); infoSpy.mockRestore(); } }); test("TelegramUpdatePoller treats malformed update items as unhealthy and does not advance the offset", async () => { const calls: Array<{ offset: unknown }> = []; const sleeps: number[] = []; let attempt = 0; const bot = { async call(_method: string, body: { offset?: unknown }) { calls.push({ offset: body.offset }); attempt += 1; if (attempt <= 2) return { ok: true, result: [{}] }; return { ok: true, result: [] }; }, }; const errorSpy = spyOn(logger, "error").mockImplementation(() => {}); const infoSpy = spyOn(logger, "info").mockImplementation(() => {}); try { const poller = new TelegramUpdatePoller({ botApi: bot, runtime: { sleep: async (ms: number) => void sleeps.push(ms) } as any, backoff: { next: () => 500, reset() {} } as any, processUpdate: async () => { throw new Error("malformed update should not be processed"); }, }); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); expect(await poller.pollOnce()).toBe(0); const apiFailures = errorSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates API failed", ); expect(apiFailures).toHaveLength(1); expect(apiFailures[0]?.[1]).toMatchObject({ description: "Malformed getUpdates response", suppressedCount: 0, }); const recoveries = infoSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates recovered", ); expect(recoveries).toHaveLength(1); expect(recoveries[0]?.[1]).toMatchObject({ from: "api_failure", suppressedCount: 1, updateCount: 0 }); expect(calls.map(call => call.offset)).toEqual([0, 0, 0]); expect(sleeps).toEqual([1_000, 1_000]); } finally { errorSpy.mockRestore(); infoSpy.mockRestore(); } }); test("TelegramUpdatePoller skips a poisoned update but still advances past valid updates in the same batch", async () => { const calls: Array<{ offset: unknown }> = []; const sleeps: number[] = []; const processed: number[] = []; let attempt = 0; const bot = { async call(_method: string, body: { offset?: unknown }) { calls.push({ offset: body.offset }); attempt += 1; if (attempt === 1) { return { ok: true, result: [{ update_id: 5 }, {}, { update_id: 7 }] }; } return { ok: true, result: [] }; }, }; const errorSpy = spyOn(logger, "error").mockImplementation(() => {}); const infoSpy = spyOn(logger, "info").mockImplementation(() => {}); try { const poller = new TelegramUpdatePoller({ botApi: bot, runtime: { sleep: async (ms: number) => void sleeps.push(ms) } as any, backoff: { next: () => 500, reset() {} } as any, processUpdate: async (update: any) => { processed.push(update.update_id); return "consumed"; }, }); // A poisoned middle entry must not wedge the offset: the valid updates on // either side are still processed and the offset advances past them. expect(await poller.pollOnce()).toBe(0); // The second poll resumes AFTER the highest valid update_id (7 -> offset 8), // proving the poison did not stall the stream. expect(await poller.pollOnce()).toBe(0); expect(processed).toEqual([5, 7]); expect(calls.map(call => call.offset)).toEqual([0, 8]); const apiFailures = errorSpy.mock.calls.filter( call => call[0] === "notifications daemon: Telegram getUpdates API failed", ); expect(apiFailures).toHaveLength(1); expect(apiFailures[0]?.[1]).toMatchObject({ description: "Malformed getUpdates response" }); } finally { errorSpy.mockRestore(); infoSpy.mockRestore(); } }); test("requestStop aborts the active long poll and run() exits, releasing ownership", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); let markPollStarted!: () => void; const pollStarted = new Promise(resolve => { markPollStarted = resolve; }); const bot = { call: (method: string, _body: unknown, opts?: { signal?: AbortSignal }) => { if (method === "getUpdates") { return new Promise((_resolve, reject) => { opts?.signal?.addEventListener("abort", () => reject(Object.assign(new Error("aborted"), { name: "AbortError" })), ); markPollStarted(); }); } return Promise.resolve({ ok: true, result: true }); }, }; class NoScan extends TelegramNotificationDaemon { override async scanRoots(): Promise {} } const daemon = new NoScan({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, }); daemon.connectSession("S", "ws://s", "t"); const runPromise = daemon.run(); await pollStarted; daemon.requestStop("signal"); await runPromise; expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(false); }); test("graceful stop cancels a pending /btw and suppresses delayed successful delivery", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); const pollStarted = Promise.withResolvers(); const bot = new FakeBotApi(); const call = bot.call.bind(bot); bot.call = async (method, body, opts) => { if (method === "getUpdates") { pollStarted.resolve(); const pending = Promise.withResolvers(); opts?.signal?.addEventListener("abort", () => pending.reject(Object.assign(new Error("aborted"), { name: "AbortError" })), ); return pending.promise; } return call(method, body); }; class NoScan extends TelegramNotificationDaemon { override async scanRoots(): Promise {} } const daemon = new NoScan({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, createLifecycleControlServer: null, }); daemon.connectSession("S", "ws://s", "t"); await enableEphemeralTurns(daemon); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); const threadId = bot.createdTopicThreadIds.at(-1)!; await daemon.handleTelegramUpdate({ update_id: 901, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw stop now", message_id: 1901 }, }); const request = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).find( frame => frame.type === "ephemeral_turn", )!; const socket = FakeWs.instances[0]!; const send = socket.send.bind(socket); socket.send = data => { send(data); const frame = JSON.parse(data); if (frame.type === "ephemeral_turn_cancel") { queueMicrotask(() => socket.emit({ type: "ephemeral_turn_result", sessionId: frame.sessionId, requestId: frame.requestId, updateId: frame.updateId, messageId: frame.messageId, threadId: frame.threadId, status: "ok", text: "late answer", }), ); } }; bot.calls = []; const runPromise = daemon.run(); await pollStarted.promise; daemon.requestStop("signal"); await runPromise; const cancels = socket.sent.map(frame => JSON.parse(frame)).filter(frame => frame.type === "ephemeral_turn_cancel"); expect(cancels).toHaveLength(1); const cancel = cancels[0]!; expect(cancel).toEqual({ type: "ephemeral_turn_cancel", sessionId: "S", token: "t", requestId: request.requestId, updateId: 901, messageId: 1901, threadId: String(threadId), reason: "daemon_shutdown", }); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ), ).toEqual([ expect.objectContaining({ body: expect.objectContaining({ message_thread_id: threadId, reply_parameters: { message_id: 1901 }, text: "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", }), }), ]); }); test("graceful stop drains active and idle /btw terminal deliveries concurrently under one deadline", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); const pollStarted = Promise.withResolvers(); const activeDeliveryStarted = Promise.withResolvers(); const idleDeliveryStarted = Promise.withResolvers(); const bot = new FakeBotApi(); const call = bot.call.bind(bot); let activeAttempts = 0; let idleAttempts = 0; let activeSignal: AbortSignal | undefined; let idleSignal: AbortSignal | undefined; let idleStartedAt = 0; bot.call = async (method, body, options) => { if (method === "getUpdates") { pollStarted.resolve(); const pending = Promise.withResolvers(); options?.signal?.addEventListener( "abort", () => pending.reject(Object.assign(new Error("aborted"), { name: "AbortError" })), { once: true }, ); return pending.promise; } const text = (body as { text?: unknown }).text; if (method === "sendMessage" && text === "active answer") { bot.calls.push({ method, body, options }); activeAttempts += 1; activeSignal = options?.signal; activeDeliveryStarted.resolve(); const pending = Promise.withResolvers(); options?.signal?.addEventListener( "abort", () => pending.reject(Object.assign(new Error("aborted"), { name: "AbortError" })), { once: true }, ); return pending.promise; } if ( method === "sendMessage" && text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again." ) { bot.calls.push({ method, body, options }); idleAttempts += 1; idleSignal = options?.signal; idleStartedAt = Date.now(); idleDeliveryStarted.resolve(); const pending = Promise.withResolvers(); options?.signal?.addEventListener( "abort", () => pending.reject(Object.assign(new Error("aborted"), { name: "AbortError" })), { once: true }, ); return pending.promise; } return call(method, body, options); }; class NoScan extends TelegramNotificationDaemon { override async scanRoots(): Promise {} } const daemon = new NoScan({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: false }, WebSocketImpl: FakeWs as any, createLifecycleControlServer: null, }); daemon.connectSession("S", "ws://s", "t"); await enableEphemeralTurns(daemon); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); const threadId = bot.createdTopicThreadIds.at(-1)!; await daemon.handleTelegramUpdate({ update_id: 902, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw active", message_id: 1902 }, }); await daemon.handleTelegramUpdate({ update_id: 903, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw idle", message_id: 1903 }, }); const requests = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).filter( frame => frame.type === "ephemeral_turn", ); expect(requests).toHaveLength(2); const activeHandling = daemon.handleSessionMessage(daemon.sessions.get("S")!, { ...requests[0], type: "ephemeral_turn_result", status: "ok", text: "active answer", }); await activeDeliveryStarted.promise; const runPromise = daemon.run(); await pollStarted.promise; const stopStartedAt = Date.now(); daemon.requestStop("signal"); await idleDeliveryStarted.promise; await Promise.all([activeHandling, runPromise]); expect(idleStartedAt - stopStartedAt).toBeLessThan(500); expect(activeSignal?.aborted).toBe(true); expect(idleSignal?.aborted).toBe(true); expect(activeAttempts).toBe(1); expect(idleAttempts).toBe(1); const terminalCalls = bot.calls.filter( call => call.method === "sendMessage" && (call.body.text === "active answer" || call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again."), ); expect(terminalCalls).toHaveLength(2); expect(terminalCalls.every(call => call.options?.noRetry === true)).toBe(true); }, 5_000); test("run() loop exits when an owner-scoped control request asks it to stop", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); let cleared = false; class NoScan extends TelegramNotificationDaemon { override async scanRoots(): Promise {} } const daemon = new NoScan({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), control: { shouldStop: async owner => owner === "owner", clear: async () => { cleared = true; }, }, setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, }); await daemon.run(); expect(cleared).toBe(true); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(false); }); test("run() persists aliases before releasing ownership on exit", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); let now = 0; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), idleTimeoutMs: 10, now: () => (now += 11), setTimeoutImpl: ((cb: () => void) => { cb(); return 0; }) as any, }); daemon.aliasTable.put({ sessionId: "S", actionId: "ask", answer: 0 }); await daemon.run(); expect(fs.existsSync(daemonPaths(agentDir).aliases)).toBe(true); }); test("a fresh daemon scanRoots reconnects an existing session endpoint", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "repo"); await registerNotificationRoot({ settings: s, cwd, sessionId: "live-session" }); const endpointDir = path.join(cwd, ".skc", "state", "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync(path.join(endpointDir, "live-session.json"), JSON.stringify({ url: "ws://live", token: "tok" })); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, }); await daemon.scanRoots(); expect(daemon.sessions.has("live-session")).toBe(true); expect(FakeWs.instances.some(ws => ws.url.startsWith("ws://live"))).toBe(true); }); test("connectSession does not create a Telegram topic before an outbound frame", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("sess-abc123", "ws://x", "tok"); FakeWs.instances[0]!.dispatchEvent(new Event("open")); await new Promise(r => setTimeout(r, 10)); expect(bot.calls.find(c => c.method === "createForumTopic")).toBeUndefined(); }); test("identity_header lazily creates a topic and applies its resolved name", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let releaseCreate: (tid: string) => void = () => {}; const createGate = new Promise(r => { releaseCreate = r; }); const bot = new FakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "createForumTopic") { const tid = await createGate; return { ok: true, result: { message_thread_id: Number(tid) } }; } if (method === "getChat") return { ok: true, result: { type: "private" } }; if (method === "editForumTopic") return { ok: true, result: true }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, }); daemon.connectSession("sess-xyz999", "ws://x", "tok"); FakeWs.instances[0]!.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(daemon.sessions.get("sess-xyz999")!, { type: "event_replay_result", ok: true, id: "telegram-startup-replay:sess-xyz999", generation: 1, lastSeq: 0, events: [], }); expect(bot.calls.filter(c => c.method === "createForumTopic")).toHaveLength(0); const session = daemon.sessions.get("sess-xyz999")!; const identity = { type: "identity_header", sessionId: "sess-xyz999", repo: "myrepo", branch: "mybranch", }; const identityP = daemon.handleSessionMessage(session, identity); await Promise.resolve(); releaseCreate("777"); await identityP; expect(bot.calls.filter(c => c.method === "createForumTopic")).toHaveLength(1); expect(bot.calls.find(c => c.method === "createForumTopic")!.body.name).toBe("myrepo/mybranch"); expect(bot.calls.find(c => c.method === "editForumTopic")).toBeUndefined(); }); test("scanRoots connects only live endpoints (skips stale + dead-PID records)", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "repo"); await registerNotificationRoot({ settings: s, cwd, sessionId: "live" }); await registerNotificationRoot({ settings: s, cwd, sessionId: "stale" }); await registerNotificationRoot({ settings: s, cwd, sessionId: "dead" }); const endpointDir = path.join(cwd, ".skc", "state", "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync(path.join(endpointDir, "live.json"), JSON.stringify({ url: "ws://live", token: "t", pid: 4242 })); fs.writeFileSync( path.join(endpointDir, "stale.json"), JSON.stringify({ url: "ws://stale", token: "t", pid: 4242, stale: true }), ); fs.writeFileSync(path.join(endpointDir, "dead.json"), JSON.stringify({ url: "ws://dead", token: "t", pid: 999999 })); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, // Only pid 4242 is "alive"; 999999 is dead. pidAlive: (pid: number) => pid === 4242, }); await daemon.scanRoots(); expect(daemon.sessions.has("live")).toBe(true); expect(daemon.sessions.has("stale")).toBe(false); expect(daemon.sessions.has("dead")).toBe(false); expect(FakeWs.instances.every(ws => ws.url.startsWith("ws://live"))).toBe(true); }); test("scanRoots reaps stale and dead-PID session topics after the orphan grace window", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "repo"); await registerNotificationRoot({ settings: s, cwd, sessionId: "stale" }); await registerNotificationRoot({ settings: s, cwd, sessionId: "dead" }); const endpointDir = path.join(cwd, ".skc", "state", "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync( path.join(endpointDir, "stale.json"), JSON.stringify({ url: "ws://stale", token: "t", stale: true }), ); fs.writeFileSync(path.join(endpointDir, "dead.json"), JSON.stringify({ url: "ws://dead", token: "t", pid: 999999 })); fs.mkdirSync(daemonPaths(agentDir).dir, { recursive: true }); fs.writeFileSync( path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), JSON.stringify({ topics: { stale: { topicId: "101", identitySent: true, createdAt: 0, name: "stale", chatId: "42", endpointKey: "ws://stale", endpointDigest: endpointAuthorityDigest("ws://stale", "t"), endpointGeneration: 1, }, dead: { topicId: "102", identitySent: true, createdAt: 0, name: "dead", chatId: "42", endpointKey: "ws://dead", endpointDigest: endpointAuthorityDigest("ws://dead", "t"), endpointGeneration: 1, }, }, }), ); const bot = new FakeBotApi(); const originalBotCall = bot.call.bind(bot); bot.call = async (method: string, body: unknown): Promise => { if (method === "deleteForumTopic" && (body as { message_thread_id?: unknown }).message_thread_id === 101) { bot.calls.push({ method, body }); return { ok: false, description: "Bad Request: TOPIC_ID_INVALID" }; } return await originalBotCall(method, body); }; let now = 120_000; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, pidAlive: () => false, now: () => now, }); await daemon.loadTopics(); daemon.connectSession("stale", "ws://stale", "t"); daemon.connectSession("dead", "ws://dead", "t"); await daemon.scanRoots(); expect(bot.calls.some(c => c.method === "deleteForumTopic")).toBe(false); expect(daemon.sessions.size).toBe(0); let persisted = JSON.parse(fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8")); expect(persisted.topics.stale.orphanedAt).toBe(120_000); expect(persisted.topics.dead.orphanedAt).toBe(120_000); now += 60_000; await daemon.scanRoots(); expect( bot.calls .filter(c => c.method === "deleteForumTopic") .map(c => c.body.message_thread_id) .sort(), ).toEqual([101, 102]); persisted = JSON.parse(fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8")); expect(persisted.topics).toEqual({}); }); test("scanRoots reaps missing endpoint topics only when all roots are readable and grace has elapsed", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "repo"); await registerNotificationRoot({ settings: s, cwd, sessionId: "missing" }); fs.mkdirSync(path.join(cwd, ".skc", "state", "sdk"), { recursive: true }); fs.mkdirSync(daemonPaths(agentDir).dir, { recursive: true }); fs.writeFileSync( path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), JSON.stringify({ topics: { missing: { topicId: "201", identitySent: true, createdAt: 0, name: "missing", chatId: "42", endpointKey: "ws://missing", endpointDigest: endpointAuthorityDigest("ws://missing", "missing"), endpointGeneration: 1, }, }, }), ); const bot = new FakeBotApi(); let now = 120_000; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, now: () => now, }); await daemon.loadTopics(); await daemon.scanRoots(); expect(bot.calls.some(c => c.method === "deleteForumTopic")).toBe(false); let persisted = JSON.parse(fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8")); expect(persisted.topics.missing.orphanedAt).toBe(120_000); now += 60_000; await daemon.scanRoots(); expect(bot.calls.filter(c => c.method === "deleteForumTopic").map(c => c.body.message_thread_id)).toEqual([201]); persisted = JSON.parse(fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8")); expect(persisted.topics).toEqual({}); const blockedAgentDir = tempAgentDir(); const blockedSettings = setPrivateAgentDir(settings(blockedAgentDir), blockedAgentDir); await registerNotificationRoot({ settings: blockedSettings, cwd: path.join(blockedAgentDir, "unreadable"), sessionId: "kept", }); fs.mkdirSync(daemonPaths(blockedAgentDir).dir, { recursive: true }); fs.writeFileSync( path.join(daemonPaths(blockedAgentDir).dir, "telegram-topics.json"), JSON.stringify({ topics: { kept: { topicId: "202", identitySent: true, createdAt: 0, name: "kept", chatId: "42", endpointKey: "ws://kept", endpointDigest: endpointAuthorityDigest("ws://kept", "kept"), endpointGeneration: 1, }, }, }), ); const blockedBot = new FakeBotApi(); const blockedDaemon = new TelegramNotificationDaemon({ settings: blockedSettings, ownerId: "owner", botToken: "tok", chatId: "42", botApi: blockedBot, now: () => 120_000, }); await blockedDaemon.loadTopics(); await blockedDaemon.scanRoots(); expect(blockedBot.calls.some(c => c.method === "deleteForumTopic")).toBe(false); }); test("scanRoots preserves a logical owner on another transport through the orphan grace window and restart", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "B", "B", { url: "ws://b", token: "b" }); const topicId = bot.createdTopicThreadIds[0]!; let now = 1_000; const rekeyed = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, now: () => now, }); await rekeyed.loadTopics(); await replayResumedIdentity(rekeyed, "A", "B", { url: "ws://a", token: "a" }); bot.calls = []; await rekeyed.scanRoots(); now += 60_000; await rekeyed.scanRoots(); expect(bot.calls.filter(call => call.method === "deleteForumTopic")).toHaveLength(0); expect((await readTopicAuthorityState(agentDir)).topics.B).toMatchObject({ topicId: String(topicId) }); const afterRestart = recoveryDaemon(agentDir, bot); await afterRestart.loadTopics(); await replayResumedIdentity(afterRestart, "B-RETURN", "B", { url: "ws://b-return", token: "return" }); bot.calls = []; await afterRestart.handleSessionMessage(afterRestart.sessions.get("B-RETURN")!, { type: "turn_stream", sessionId: "B", text: "B survives scan and restart", }); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.message_thread_id === topicId), ).toHaveLength(1); }); test("runDaemonInternal wires SIGTERM to the daemon stop method", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); let stopped = false; let resolveRun: (() => void) | undefined; class StubDaemon { constructor(public opts: unknown) {} requestStop(): void { stopped = true; resolveRun?.(); } run(): Promise { return new Promise(resolve => { resolveRun = resolve; }); } } const originalOnce = process.once.bind(process); const originalOff = process.off.bind(process); let sigtermHandler: (() => void) | undefined; (process as any).once = (event: string, handler: () => void) => { if (event === "SIGTERM") sigtermHandler = handler; // Do not register real signal handlers in-process; just capture them. return process; }; (process as any).off = () => process; try { const runPromise = runDaemonInternal(["--agent-dir", agentDir, "--owner-id", "owner"], { SettingsImpl: { init: async () => s }, DaemonImpl: StubDaemon as any, }); await new Promise(resolve => setTimeout(resolve, 5)); expect(sigtermHandler).toBeTruthy(); sigtermHandler?.(); await runPromise; expect(stopped).toBe(true); } finally { (process as any).once = originalOnce; (process as any).off = originalOff; } }); test("a long finalized turn is scheduled through the pool, not burst in one grant", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const now = 0; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, now: () => now, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; await daemon.handleSessionMessage(session as any, { type: "identity_header", sessionId: "S", repo: "r", branch: "b", }); bot.calls = []; // A finalized turn with NO messageRef renders keyless (legacy fresh-send). // Its rendered HTML spans multiple Telegram chunks. const raw = "가".repeat(9000); const expectedChunks = splitTelegramHtml(markdownToTelegramHtml(raw)); expect(expectedChunks.length).toBeGreaterThan(1); // sanity: the turn really splits await daemon.handleSessionMessage(session as any, { type: "turn_stream", sessionId: "S", phase: "finalized", text: raw, }); // One granted slot -> exactly one Telegram send; the remaining chunks are // re-submitted to the pool rather than fanned out against the single token. const firstFlush = bot.calls.filter(c => c.method === "sendMessage"); expect(firstFlush).toHaveLength(1); expect(firstFlush[0]!.body.text).toBe(expectedChunks[0]); // A follow-up flush drains the queued continuation chunks (each consumed a // token) ahead of the newer frame, preserving order and dropping nothing. bot.calls = []; await daemon.handleSessionMessage(session as any, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "tail", }); const rest = bot.calls.filter(c => c.method === "sendMessage").map(c => c.body.text); expect(rest).toEqual([...expectedChunks.slice(1), markdownToTelegramHtml("tail")]); }); // --------------------------------------------------------------------------- // Rev 3 rich final-answer promotion verification (Slice 1). Proves that the // off state is byte-identical (transport golden + daemon HTML body), documents // the multipart FormData contract, and pins the exact rich-promotion counts, // topic-match matrix, transport-level ok:false fallback, version-skew // tolerance, and the long-string split seam. Config reachability lives in // notifications-daemon-config-reachability.test.ts. // --------------------------------------------------------------------------- /** FakeBotApi with a deterministic topic id and a switchable sendRichMessage outcome. */ class RichFakeBotApi extends FakeBotApi { richBehavior: "ok" | "ok_false" | "throw" = "ok"; richThreadId = 555; /** When true, createForumTopic returns a confirmed capability refusal for flat delivery. */ threadedOff = false; override async call( method: string, body: unknown, options?: { noRetry?: boolean; signal?: AbortSignal }, ): Promise { if (method === "createForumTopic") { this.calls.push({ method, body, options }); return this.threadedOff ? { ok: false, description: "Bad Request: forum topics are disabled" } : { ok: true, result: { message_thread_id: this.richThreadId } }; } if (method === "sendRichMessage") { this.calls.push({ method, body, options }); if (this.richBehavior === "throw") throw new Error("rich transport down"); if (this.richBehavior === "ok_false") return { ok: false, description: "rich unavailable" }; return { ok: true, result: { message_id: 4242 } }; } return super.call(method, body, options); } } function richSession(id = "S"): any { return { sessionId: id, token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; } function makeRichDaemon(bot: FakeBotApi, rich?: { enabled: boolean }): TelegramNotificationDaemon { return new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, ...(rich ? { rich } : {}), }); } /** * Drive an identity_header (creates the topic) then a finalized turn_stream, * resetting call history in between so only the finalized turn's calls remain. */ async function driveFinalizedTurn( daemon: TelegramNotificationDaemon, bot: FakeBotApi, session: any, raw: string, finalAnswer = true, ): Promise { await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: session.sessionId, repo: "r", branch: "b", }); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: session.sessionId, phase: "finalized", finalAnswer, text: raw, }); } const countMethod = (bot: FakeBotApi, method: string): number => bot.calls.filter(c => c.method === method).length; const findMethod = (bot: FakeBotApi, method: string) => bot.calls.find(c => c.method === method); describe("telegram daemon rich final-answer promotion (Rev 3 verification)", () => { // (a) TRANSPORT GOLDEN -------------------------------------------------- test("(a) transport serializes off-state sendMessage variants byte-identically", async () => { const requests: Array<{ url: string; init: RequestInit }> = []; const transport = new TelegramBotTransport({ botToken: "tok", apiBase: "https://telegram.test", fetchImpl: (async (url: string | URL | Request, init?: RequestInit) => { requests.push({ url: String(url), init: init ?? {} }); return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 }); }) as typeof fetch, }); // The exact bodies flushPool / the ask path hand to botApi.call in the off state. const single = { chat_id: "42", message_thread_id: 100, text: "Hello world", parse_mode: "HTML" }; const longText = "x".repeat(4200); const longChunk = { chat_id: "42", message_thread_id: 100, text: longText, parse_mode: "HTML" }; const ask = { chat_id: "42", message_thread_id: 100, text: "Pick one option", parse_mode: "HTML", reply_markup: { inline_keyboard: [ [ { text: "1", callback_data: "a:1" }, { text: "2", callback_data: "a:2" }, ], ], }, }; await transport.call("sendMessage", single); await transport.call("sendMessage", longChunk); await transport.call("sendMessage", ask); // Byte-identical JSON with the exact pre-rich field order. expect(requests[0].init.body).toBe( '{"chat_id":"42","message_thread_id":100,"text":"Hello world","parse_mode":"HTML"}', ); expect(requests[1].init.body).toBe( `{"chat_id":"42","message_thread_id":100,"text":"${longText}","parse_mode":"HTML"}`, ); expect(requests[2].init.body).toBe( '{"chat_id":"42","message_thread_id":100,"text":"Pick one option","parse_mode":"HTML",' + '"reply_markup":{"inline_keyboard":[[{"text":"1","callback_data":"a:1"},{"text":"2","callback_data":"a:2"}]]}}', ); for (const r of requests) { expect(r.url).toBe("https://telegram.test/bottok/sendMessage"); expect(r.init.headers).toEqual({ "content-type": "application/json" }); } }); // (b) FormData semantics ------------------------------------------------ test("(b) sendPhoto/sendDocument multipart carries fields, file names, and blob types", async () => { const requests: Array<{ url: string; init: RequestInit }> = []; const transport = new TelegramBotTransport({ botToken: "tok", apiBase: "https://telegram.test", fetchImpl: (async (url: string | URL | Request, init?: RequestInit) => { requests.push({ url: String(url), init: init ?? {} }); return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 }); }) as typeof fetch, }); await transport.call("sendPhoto", { chat_id: "42", message_thread_id: 7, photo: Buffer.from("PNGDATA").toString("base64"), mime: "image/png", caption: "shot", parse_mode: "HTML", }); await transport.call("sendDocument", { chat_id: "42", message_thread_id: 9, document: Buffer.from("DOCDATA").toString("base64"), mime: "application/pdf", fileName: "notes.pdf", caption: "doc", parse_mode: "HTML", }); // Defaults: no mime -> image/png; no mime/fileName -> octet-stream/"file"; no thread -> field omitted. await transport.call("sendPhoto", { chat_id: "42", photo: Buffer.from("Z").toString("base64") }); await transport.call("sendDocument", { chat_id: "42", document: Buffer.from("Z").toString("base64") }); expect(requests[0].url).toBe("https://telegram.test/bottok/sendPhoto"); const photo = requests[0].init.body as FormData; expect(photo).toBeInstanceOf(FormData); expect(photo.get("chat_id")).toBe("42"); expect(photo.get("message_thread_id")).toBe("7"); expect(photo.get("caption")).toBe("shot"); expect(photo.get("parse_mode")).toBe("HTML"); const photoFile = photo.get("photo") as File; expect(photoFile).toBeInstanceOf(Blob); expect(photoFile.name).toBe("image"); expect(photoFile.type).toBe("image/png"); expect(await photoFile.text()).toBe("PNGDATA"); expect(requests[1].url).toBe("https://telegram.test/bottok/sendDocument"); const doc = requests[1].init.body as FormData; expect(doc.get("chat_id")).toBe("42"); expect(doc.get("message_thread_id")).toBe("9"); expect(doc.get("caption")).toBe("doc"); expect(doc.get("parse_mode")).toBe("HTML"); const docFile = doc.get("document") as File; expect(docFile).toBeInstanceOf(Blob); expect(docFile.name).toBe("notes.pdf"); expect(docFile.type).toBe("application/pdf"); expect(await docFile.text()).toBe("DOCDATA"); const photoDefaults = requests[2].init.body as FormData; expect((photoDefaults.get("photo") as File).type).toBe("image/png"); const docDefaults = requests[3].init.body as FormData; expect(docDefaults.has("message_thread_id")).toBe(false); expect(docDefaults.has("caption")).toBe(false); expect(docDefaults.has("parse_mode")).toBe(false); const docDefaultFile = docDefaults.get("document") as File; expect(docDefaultFile.name).toBe("file"); expect(docDefaultFile.type).toBe("application/octet-stream"); }); // (c) off-state fake BotApi: no rich, byte-identical HTML body ----------- test("(c) off states never call sendRichMessage and emit a byte-identical HTML body", async () => { const raw = "Just plain final answer text"; const goldenBody = '{"chat_id":"42","message_thread_id":555,"text":"Just plain final answer text","parse_mode":"HTML"}'; const offStates: Array<{ label: string; rich?: { enabled: boolean } }> = [ { label: "enabled false", rich: { enabled: false } }, ]; for (const state of offStates) { const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, state.rich); await driveFinalizedTurn(daemon, bot, richSession(), raw); expect(countMethod(bot, "sendRichMessage")).toBe(0); expect(countMethod(bot, "sendMessage")).toBe(1); const body = findMethod(bot, "sendMessage")!.body; // Byte-identical to the pre-rich HTML path (field order included). expect(JSON.stringify(body)).toBe(goldenBody); expect(body.text).toBe(markdownToTelegramHtml(raw)); expect(body.parse_mode).toBe(TELEGRAM_PARSE_MODE); } }); // (d) exact counts ------------------------------------------------------ test("(d) on+matching topic promotes exactly one sendRichMessage and no sendMessage", async () => { const raw = "# Final\n\nThe answer."; const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: true }); await driveFinalizedTurn(daemon, bot, richSession(), raw); expect(countMethod(bot, "sendRichMessage")).toBe(1); expect(countMethod(bot, "sendMessage")).toBe(0); expect(findMethod(bot, "sendRichMessage")!.body).toEqual({ chat_id: "42", message_thread_id: 555, rich_message: { markdown: raw, skip_entity_detection: true }, }); expect(findMethod(bot, "sendRichMessage")!.options).toEqual({ noRetry: true, signal: expect.any(AbortSignal), }); }); test("(d) editable finalized messageRef stays on HTML edit path and never promotes rich", async () => { const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: true }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); expect(bot.calls.length).toBeGreaterThan(0); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "live", text: "Hello", messageRef: "1", }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: "Hello world!", messageRef: "1", }); expect(countMethod(bot, "sendRichMessage")).toBe(0); expect(countMethod(bot, "editMessageText")).toBeGreaterThanOrEqual(1); expect(countMethod(bot, "sendMessage")).toBe(1); expect(findMethod(bot, "sendMessage")!.body.text).toBe(markdownToTelegramHtml("Hello")); }); test("(d) editable multi-chunk finalized continuations strip rich markers and never re-promote", async () => { const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: true }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); expect(bot.calls.length).toBeGreaterThan(0); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "live", text: "seed", messageRef: "1", }); const raw = "가".repeat(9000); const chunks = splitTelegramHtml(markdownToTelegramHtml(raw)); expect(chunks.length).toBeGreaterThan(1); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: raw, messageRef: "1", }); expect(countMethod(bot, "sendRichMessage")).toBe(0); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: "tail", messageRef: "1", }); expect(countMethod(bot, "sendRichMessage")).toBe(0); const sends = bot.calls.filter(c => c.method === "sendMessage"); expect(sends.length).toBeGreaterThan(0); expect(sends.every(c => c.body.parse_mode === TELEGRAM_PARSE_MODE)).toBe(true); }); test("(d) oversized rich finals skip promotion and drain HTML chunks through the pool", async () => { const raw = "B".repeat(9000); const chunks = splitTelegramHtml(markdownToTelegramHtml(raw)); expect(chunks.length).toBeGreaterThan(1); const bot = new RichFakeBotApi(); bot.richBehavior = "throw"; const daemon = makeRichDaemon(bot, { enabled: true }); const session = richSession(); await driveFinalizedTurn(daemon, bot, session, raw); expect(countMethod(bot, "sendRichMessage")).toBe(0); // Fairness: oversized rich payloads stay on the HTML path, where only the // first chunk is delivered on this token and the rest are requeued. const first = bot.calls.filter(c => c.method === "sendMessage"); expect(first).toHaveLength(1); expect(first[0]!.body.text).toBe(chunks[0]); // A follow-up flush drains the requeued continuations (ahead of the newer frame). bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: session.sessionId, phase: "finalized", text: "tail", }); const rest = bot.calls.filter(c => c.method === "sendMessage"); expect(rest.map(c => c.body.text)).toEqual([...chunks.slice(1), markdownToTelegramHtml("tail")]); expect(rest.every(c => c.body.message_thread_id === 555 && c.body.parse_mode === TELEGRAM_PARSE_MODE)).toBe(true); }); test("(d) sendRichMessage ok:false falls back to a single HTML chunk", async () => { const raw = "One short final answer"; const bot = new RichFakeBotApi(); bot.richBehavior = "ok_false"; const daemon = makeRichDaemon(bot, { enabled: true }); await driveFinalizedTurn(daemon, bot, richSession(), raw); expect(countMethod(bot, "sendRichMessage")).toBe(1); expect(countMethod(bot, "sendMessage")).toBe(1); expect(findMethod(bot, "sendMessage")!.body.text).toBe(markdownToTelegramHtml(raw)); }); test("(d) deliverRichWithFallback warns exactly once per explicit rejection and never on success", async () => { const send = { method: "sendMessage", lane: "finalized", text: "x", richMarkdown: "raw md" } as any; const base = { chat_id: "42", message_thread_id: 555 }; const okBot = new RichFakeBotApi(); const okWarns: string[] = []; let okFallbacks = 0; await deliverRichWithFallback( okBot as any, base, send, AbortSignal.timeout(30_000), async () => { okFallbacks++; }, { warn: m => okWarns.push(m) }, ); expect(countMethod(okBot, "sendRichMessage")).toBe(1); expect(okFallbacks).toBe(0); expect(okWarns).toHaveLength(0); const okFalseBot = new RichFakeBotApi(); okFalseBot.richBehavior = "ok_false"; const okFalseWarns: string[] = []; let okFalseFallbacks = 0; await deliverRichWithFallback( okFalseBot as any, base, send, AbortSignal.timeout(30_000), async () => { okFalseFallbacks++; }, { warn: m => okFalseWarns.push(m) }, ); expect(okFalseFallbacks).toBe(1); expect(okFalseWarns).toHaveLength(1); expect(okFalseWarns[0]).toContain("rich unavailable"); }); // (e) global rich matrix --------------------------------------------------- test("(e) global rich promotes in threaded and flat delivery", async () => { const raw = "Matrix final answer"; const threadedBot = new RichFakeBotApi(); await driveFinalizedTurn(makeRichDaemon(threadedBot, { enabled: true }), threadedBot, richSession(), raw); expect(countMethod(threadedBot, "sendRichMessage")).toBe(1); expect(findMethod(threadedBot, "sendRichMessage")!.body.message_thread_id).toBe(555); const flatBot = new RichFakeBotApi(); flatBot.threadedOff = true; await driveFinalizedTurn(makeRichDaemon(flatBot, { enabled: true }), flatBot, richSession(), raw); expect(countMethod(flatBot, "sendRichMessage")).toBe(1); expect(findMethod(flatBot, "sendRichMessage")!.body.message_thread_id).toBeUndefined(); }); // (f) transport-level ok:false -> HTML fallback ------------------------- test("(f) transport-level {ok:false} sendRichMessage response falls back to HTML", async () => { const fetchCalls: Array<{ method: string; body: any }> = []; const jsonResponse = (obj: unknown) => new Response(JSON.stringify(obj), { status: 200, headers: { "content-type": "application/json" } }); const fetchImpl = (async (url: string | URL | Request, init?: RequestInit) => { const method = String(url).split("/").pop() ?? ""; const body = typeof init?.body === "string" ? JSON.parse(init.body) : undefined; fetchCalls.push({ method, body }); if (method === "getChat") return jsonResponse({ ok: true, result: { id: 42, type: "private" } }); if (method === "getMe") return jsonResponse({ ok: true, result: { id: 1 } }); if (method === "createForumTopic") return jsonResponse({ ok: true, result: { message_thread_id: 555 } }); if (method === "sendRichMessage") return jsonResponse({ ok: false, description: "rich off at transport" }); return jsonResponse({ ok: true, result: { message_id: fetchCalls.length } }); }) as typeof fetch; const transport = new TelegramBotTransport({ botToken: "tok", apiBase: "https://telegram.test", fetchImpl }); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: transport, rich: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "r", branch: "b" }); fetchCalls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: "Transport ok false answer", }); expect(fetchCalls.filter(c => c.method === "sendRichMessage")).toHaveLength(1); const htmlSends = fetchCalls.filter(c => c.method === "sendMessage"); expect(htmlSends.length).toBeGreaterThan(0); expect(htmlSends[0].body.text).toBe(markdownToTelegramHtml("Transport ok false answer")); }); // default-on: a finalAnswer-bearing frame reaching a rich-unset daemon promotes rich test("default-on: finalAnswer frame on a rich-unset daemon promotes rich", async () => { const raw = "# Heading here"; const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot); // no rich option defaults on await driveFinalizedTurn(daemon, bot, richSession(), raw, true); expect(countMethod(bot, "sendRichMessage")).toBe(1); expect(countMethod(bot, "sendMessage")).toBe(0); expect(findMethod(bot, "sendRichMessage")!.body.rich_message.markdown).toBe(raw); }); // long-string seam: a 4096+ finalized message splits identically in the off state test("long-string seam: 4096+ off-state finalized message keeps the existing HTML split", async () => { const raw = "C".repeat(9000); const chunks = splitTelegramHtml(markdownToTelegramHtml(raw)); expect(chunks.length).toBeGreaterThan(1); const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: false }); // off await driveFinalizedTurn(daemon, bot, richSession(), raw); expect(countMethod(bot, "sendRichMessage")).toBe(0); const sends = bot.calls.filter(c => c.method === "sendMessage"); // Off state uses the unchanged HTML path. Upstream fans the split across // pool drains (first chunk now, the rest re-queued), so this flush leads // with chunk[0]; the full multi-chunk drain is covered by the pool test // above. The point here: no rich promotion, and chunks stay limit-bounded. expect(sends.length).toBeGreaterThanOrEqual(1); expect(sends[0]!.body.text).toBe(chunks[0]); expect(sends.every(c => c.body.text.length <= TELEGRAM_MESSAGE_LIMIT)).toBe(true); }); }); // --------------------------------------------------------------------------- // G006: rich overflow boundary. Oversized finalized answers stay on the HTML // chunk path instead of probing rich and then falling back in the same pool // drain. The lower-level fallback helper still degrades oversized rich failures // to split HTML when called directly; the daemon-level contract is that normal // routing avoids the predictable over-limit rich miss entirely. // --------------------------------------------------------------------------- describe("telegram daemon rich overflow boundary (G006)", () => { test("(g) 4096+ finalized payload skips rich and drains HTML chunks through the pool", async () => { const raw = "D".repeat(9000); const html = markdownToTelegramHtml(raw); const chunks = splitTelegramHtml(html); expect(html.length).toBeGreaterThan(TELEGRAM_MESSAGE_LIMIT); // genuine 4096+ overflow expect(chunks.length).toBeGreaterThan(1); // must actually split into multiple messages const bot = new RichFakeBotApi(); bot.richBehavior = "ok_false"; const daemon = makeRichDaemon(bot, { enabled: true }); const session = richSession(); await driveFinalizedTurn(daemon, bot, session, raw); // Oversized rich payloads are not promoted, so the granted pool slot maps to // one HTML sendMessage and continuations are drained on later slots. expect(countMethod(bot, "sendRichMessage")).toBe(0); const first = bot.calls.filter(c => c.method === "sendMessage"); expect(first).toHaveLength(1); expect(first[0]!.body.text).toBe(chunks[0]); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: session.sessionId, phase: "finalized", text: "tail", }); const rest = bot.calls.filter(c => c.method === "sendMessage"); const allChunkTexts = [chunks[0], ...rest.map(c => c.body.text)]; expect(allChunkTexts).toEqual([...chunks, markdownToTelegramHtml("tail")]); // no content dropped expect(rest.every(c => c.body.text.length <= TELEGRAM_MESSAGE_LIMIT)).toBe(true); expect(rest.every(c => c.body.message_thread_id === 555 && c.body.parse_mode === TELEGRAM_PARSE_MODE)).toBe(true); }); test("(g) rich {ok:false} at 4096+ overflow warns exactly once and runs the chunked HTML fallback once", async () => { const raw = "E".repeat(9000); const html = markdownToTelegramHtml(raw); const chunks = splitTelegramHtml(html); expect(html.length).toBeGreaterThan(TELEGRAM_MESSAGE_LIMIT); expect(chunks.length).toBeGreaterThan(1); const bot = new RichFakeBotApi(); bot.richBehavior = "ok_false"; const send = { method: "sendMessage", lane: "finalized", text: html, richMarkdown: raw } as any; const base = { chat_id: "42", message_thread_id: 555 }; const warns: string[] = []; let fallbacks = 0; // Mirror the daemon's sendHtmlFallback closure (telegram-daemon.ts) verbatim. const sendHtmlFallback = async () => { fallbacks++; for (const text of splitTelegramHtml(send.text)) { await bot.call("sendMessage", { chat_id: base.chat_id, message_thread_id: base.message_thread_id, text, parse_mode: TELEGRAM_PARSE_MODE, }); } }; await deliverRichWithFallback(bot as any, base, send, AbortSignal.timeout(30_000), sendHtmlFallback, { warn: m => warns.push(m), }); expect(countMethod(bot, "sendRichMessage")).toBe(1); expect(fallbacks).toBe(1); // fallback invoked exactly once for the whole overflow expect(warns).toHaveLength(1); // one diagnostic, NOT one per chunk expect(warns[0]).toContain("sendRichMessage rejected"); const sends = bot.calls.filter(c => c.method === "sendMessage"); expect(sends).toHaveLength(chunks.length); expect(sends.map(c => c.body.text)).toEqual(chunks); expect(sends.every(c => c.body.text.length <= TELEGRAM_MESSAGE_LIMIT)).toBe(true); }); test("(g) normal-length (<= limit) traffic is unchanged: promoted -> one rich send; off -> one HTML send", async () => { const raw = "A concise final answer well under the Telegram limit."; expect(markdownToTelegramHtml(raw).length).toBeLessThanOrEqual(TELEGRAM_MESSAGE_LIMIT); // no overflow // Promoted + ok:true -> exactly one sendRichMessage, and NO HTML fallback/split. const onBot = new RichFakeBotApi(); await driveFinalizedTurn(makeRichDaemon(onBot, { enabled: true }), onBot, richSession(), raw); expect(countMethod(onBot, "sendRichMessage")).toBe(1); expect(countMethod(onBot, "sendMessage")).toBe(0); // Off -> the unchanged single HTML sendMessage (no rich, no split). const offBot = new RichFakeBotApi(); await driveFinalizedTurn(makeRichDaemon(offBot, { enabled: false }), offBot, richSession(), raw); expect(countMethod(offBot, "sendRichMessage")).toBe(0); expect(countMethod(offBot, "sendMessage")).toBe(1); expect(findMethod(offBot, "sendMessage")!.body.text).toBe(markdownToTelegramHtml(raw)); }); }); describe("telegram daemon action-needed rich delivery (G004)", () => { function makeAskDaemon(bot: FakeBotApi, rich: { enabled: boolean }) { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, WebSocketImpl: FakeWs as any, rich, }); daemon.connectSession("S", "ws://s", "ts"); return daemon; } test("ask rich success: one sendRichMessage with reply_markup, route registered, callback + free-text reply route", async () => { FakeWs.instances = []; const bot = new RichFakeBotApi(); const daemon = makeAskDaemon(bot, { enabled: true }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y", "N"], recommendedIndex: 1, }); const rich = bot.calls.filter(c => c.method === "sendRichMessage"); expect(rich).toHaveLength(1); expect(rich[0]!.options).toEqual({ noRetry: true, signal: expect.any(AbortSignal), }); expect(countMethod(bot, "sendMessage")).toBe(0); expect(rich[0]!.body.rich_message.markdown).toContain("Q"); expect(rich[0]!.body.rich_message.markdown).toContain("2. N (Recommended)"); expect(rich[0]!.body.reply_markup.inline_keyboard.flat().map((button: { text: string }) => button.text)).toEqual([ "1", "2", ]); expect(rich[0]!.body.reply_markup.inline_keyboard).toBeTruthy(); expect(rich[0]!.body.message_thread_id).toBe(555); expect(daemon.messageRoutes.get("4242")).toEqual({ sessionId: "S", actionId: "ask" }); const alias = rich[0]!.body.reply_markup.inline_keyboard[0][1].callback_data; await daemon.handleTelegramUpdate({ update_id: 1, callback_query: { id: "cb", data: alias, message: { chat: { id: 42 } } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent.at(-1)!)).toEqual({ type: "reply", id: "ask", answer: 1, token: "ts", }); await daemon.handleTelegramUpdate({ update_id: 2, message: { chat: { id: 42 }, text: "typed answer", reply_to_message: { message_id: 4242 } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent.at(-1)!)).toEqual({ type: "reply", id: "ask", answer: "typed answer", token: "ts", }); }); for (const behavior of ["ok_false"] as const) { test(`ask rich ${behavior}: HTML fallback registers last-chunk id and routes replies`, async () => { FakeWs.instances = []; const bot = new RichFakeBotApi(); bot.richBehavior = behavior; const daemon = makeAskDaemon(bot, { enabled: true }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y", "N"], }); expect(countMethod(bot, "sendRichMessage")).toBe(1); const htmlSends = bot.calls.filter(c => c.method === "sendMessage"); expect(htmlSends.length).toBeGreaterThanOrEqual(1); expect(htmlSends.at(-1)!.body.reply_markup.inline_keyboard).toBeTruthy(); const askEntry = [...daemon.messageRoutes.entries()].find( ([, route]) => route.sessionId === "S" && route.actionId === "ask", ); expect(askEntry).toBeDefined(); await daemon.handleTelegramUpdate({ update_id: 1, message: { chat: { id: 42 }, text: "typed", reply_to_message: { message_id: Number(askEntry![0]) } }, }); expect(JSON.parse(FakeWs.instances[0]!.sent.at(-1)!)).toEqual({ type: "reply", id: "ask", answer: "typed", token: "ts", }); }); } test("idle rich: one sendRichMessage without reply_markup and no message route", async () => { FakeWs.instances = []; const bot = new RichFakeBotApi(); const daemon = makeAskDaemon(bot, { enabled: true }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "idle", id: "idle1", summary: "all done", }); expect(countMethod(bot, "sendRichMessage")).toBe(1); expect(countMethod(bot, "sendMessage")).toBe(0); const rich = findMethod(bot, "sendRichMessage")!; expect(rich.body.rich_message.markdown).toContain("Agent idle"); expect(rich.body.reply_markup).toBeUndefined(); expect(daemon.messageRoutes.size).toBe(0); }); test("off (rich.enabled=false) ask: byte-identical HTML, zero sendRichMessage", async () => { FakeWs.instances = []; const bot = new RichFakeBotApi(); const daemon = makeAskDaemon(bot, { enabled: false }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y", "N"], }); expect(countMethod(bot, "sendRichMessage")).toBe(0); const sends = bot.calls.filter(c => c.method === "sendMessage"); expect(sends).toHaveLength(1); expect(sends[0]!.body.parse_mode).toBe(TELEGRAM_PARSE_MODE); expect(sends[0]!.body.reply_markup.inline_keyboard).toBeTruthy(); const askEntry = [...daemon.messageRoutes.entries()].find( ([, route]) => route.sessionId === "S" && route.actionId === "ask", ); expect(askEntry).toBeDefined(); }); test("off (rich.enabled=false) ask stops HTML chunks on a cooldown-suppressed send", async () => { FakeWs.instances = []; const bot = new RichFakeBotApi(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => method === "sendMessage" ? undefined : await call(method, body, options); const daemon = makeAskDaemon(bot, { enabled: false }); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y", "N"], }); expect(countMethod(bot, "sendMessage")).toBe(0); expect(daemon.messageRoutes.size).toBe(0); }); }); describe("telegram daemon /rich toggle (G005)", () => { function richDaemonWithSettings(bot: FakeBotApi, s: Settings, enabled: boolean) { return new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, rich: { enabled }, }); } test("/rich off persists the setting, updates runtime, and confirms in-topic", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new RichFakeBotApi(); const daemon = richDaemonWithSettings(bot, s, true); await daemon.handleTelegramUpdate({ update_id: 501, message: { chat: { id: 42 }, message_thread_id: 555, text: "/rich off", message_id: 1 }, }); const confirm = bot.calls.find(c => c.method === "sendMessage" && c.body.text === "Rich messages: off"); expect(confirm).toBeDefined(); expect(confirm!.body.message_thread_id).toBe(555); expect(s.get("notifications.telegram.rich.enabled")).toBe(false); // Runtime is toggled immediately: the next finalized final is HTML, not rich. bot.calls.length = 0; await driveFinalizedTurn(daemon, bot, richSession(), "answer after off"); expect(countMethod(bot, "sendRichMessage")).toBe(0); expect(countMethod(bot, "sendMessage")).toBe(1); }); test("/rich on re-enables rich at runtime and persists true", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new RichFakeBotApi(); const daemon = richDaemonWithSettings(bot, s, false); await daemon.handleTelegramUpdate({ update_id: 502, message: { chat: { id: 42 }, text: "/rich on", message_id: 1 }, }); expect(bot.calls.some(c => c.method === "sendMessage" && c.body.text === "Rich messages: on")).toBe(true); expect(s.get("notifications.telegram.rich.enabled")).toBe(true); bot.calls.length = 0; await driveFinalizedTurn(daemon, bot, richSession(), "answer after on"); expect(countMethod(bot, "sendRichMessage")).toBe(1); }); test("/rich works with no connected session (handled before WS-dependent injection)", async () => { const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: true }); // no connectSession await daemon.handleTelegramUpdate({ update_id: 601, message: { chat: { id: 42 }, text: "/rich off", message_id: 1 }, }); expect(bot.calls.some(c => c.method === "sendMessage" && c.body.text === "Rich messages: off")).toBe(true); }); test("invalid /rich arg returns usage guidance and is not injected", async () => { const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: true }); await daemon.handleTelegramUpdate({ update_id: 602, message: { chat: { id: 42 }, text: "/rich maybe", message_id: 1 }, }); expect(bot.calls.some(c => c.method === "sendMessage" && c.body.text === "Usage: /rich on|off")).toBe(true); }); test("/rich off while an ask is pending is a config command, not an ask answer", async () => { FakeWs.instances = []; const bot = new RichFakeBotApi(); const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, WebSocketImpl: FakeWs as any, rich: { enabled: true }, }); daemon.connectSession("S", "ws://s", "ts"); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "action_needed", kind: "ask", id: "ask", question: "Q", options: ["Y", "N"], }); const threadId = bot.calls.find(c => c.method === "sendRichMessage")?.body.message_thread_id; const before = FakeWs.instances[0]!.sent.length; bot.calls.length = 0; await daemon.handleTelegramUpdate({ update_id: 701, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/rich off", message_id: 9 }, }); const afterFrames = FakeWs.instances[0]!.sent.slice(before).map(f => JSON.parse(f)); expect(afterFrames.some(f => f.type === "reply")).toBe(false); // ask NOT answered expect(bot.calls.some(c => c.method === "sendMessage" && c.body.text === "Rich messages: off")).toBe(true); }); test("settings write failure leaves runtime unchanged and warns the user", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); (s as any).set = () => { throw new Error("disk full"); }; const bot = new RichFakeBotApi(); const daemon = richDaemonWithSettings(bot, s, true); await daemon.handleTelegramUpdate({ update_id: 801, message: { chat: { id: 42 }, text: "/rich off", message_id: 1 }, }); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).includes("unchanged"))).toBe(true); // Runtime stays rich because the persistence failed. bot.calls.length = 0; await driveFinalizedTurn(daemon, bot, richSession(), "still rich"); expect(countMethod(bot, "sendRichMessage")).toBe(1); }); test("a non-paired chat cannot toggle rich", async () => { const bot = new RichFakeBotApi(); const daemon = makeRichDaemon(bot, { enabled: true }); await daemon.handleTelegramUpdate({ update_id: 901, message: { chat: { id: -99999 }, text: "/rich off", message_id: 1 }, }); expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).startsWith("Rich messages"))).toBe( false, ); }); test("/rich in a non-private paired chat is rejected (fail-closed, no toggle)", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new RichFakeBotApi(); // Paired chat resolves to a group/supergroup, not a private DM. bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { id: body.chat_id, type: "supergroup" } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, rich: { enabled: true }, }); await daemon.handleTelegramUpdate({ update_id: 951, message: { chat: { id: 42, type: "supergroup" }, text: "/rich off", message_id: 1 }, }); // No toggle confirmation is sent and the persisted setting is untouched. expect(bot.calls.some(c => c.method === "sendMessage" && String(c.body.text).startsWith("Rich messages"))).toBe( false, ); expect(s.get("notifications.telegram.rich.enabled")).toBe(true); }); test("/rich confirms success only after a durable flushOrThrow (swallowed background save)", async () => { const agentDir = tempAgentDir(); // The real Settings.set is a synchronous fire-and-forget whose background // #saveNow swallows write errors, so flushOrThrow() is the only signal of a // failed config.yml write. Simulate set() succeeding while flushOrThrow() // rejects, and assert /rich does NOT confirm success. const base = setPrivateAgentDir(settings(agentDir), agentDir); const s = new Proxy(base, { get(target, prop) { if (prop === "set") return async () => undefined; if (prop === "flushOrThrow") return async () => { throw new Error("config.yml write failed"); }; const value = Reflect.get(target, prop, target); return typeof value === "function" ? value.bind(target) : value; }, }) as Settings; const bot = new RichFakeBotApi(); bot.call = (async (method: string, body: any) => { bot.calls.push({ method, body }); if (method === "getChat") return { ok: true, result: { id: body.chat_id, type: "private" } }; if (method === "sendMessage") return { ok: true, result: { message_id: bot.calls.length } }; return { ok: true, result: true }; }) as any; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot as any, rich: { enabled: true }, }); await daemon.handleTelegramUpdate({ update_id: 952, message: { chat: { id: 42, type: "private" }, text: "/rich off", message_id: 1 }, }); // A durable-write failure is reported as "unchanged"; success is never confirmed. expect( bot.calls.some( c => c.method === "sendMessage" && c.body.text === "Rich messages: unchanged (settings write failed)", ), ).toBe(true); expect(bot.calls.some(c => c.method === "sendMessage" && c.body.text === "Rich messages: off")).toBe(false); }); }); describe("Telegram tool activity capability and routing", () => { test("advertises tool_activity_v2 with receive-only legacy-v1 compatibility", () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://s", "ts"); FakeWs.instances[0]!.dispatchEvent(new Event("open")); const hello = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).find(frame => frame.type === "hello"); expect(hello.capabilities).toContain(TOOL_ACTIVITY_CAPABILITY); expect(hello.capabilities).toContain(LEGACY_TOOL_ACTIVITY_CAPABILITY); const threadedFrames = (TelegramNotificationDaemon as any).THREADED_FRAMES as Set; expect(threadedFrames.has("tool_activity")).toBe(true); expect(threadedFrames.has("reasoning_summary")).toBe(true); }); test("omitted daemon option is fail-closed for tool activity", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "important-session", branch: "main", }); expect(bot.calls.some(call => JSON.stringify(call.body).includes("important-session"))).toBe(true); const importantCallCount = bot.calls.length; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "omitted-option", toolName: "bash", phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "omitted-option", toolName: "bash", phase: "completed", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "malformed-omitted-option", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "unknown-phase-omitted-option", toolName: "bash", phase: "surprised", }); expect(bot.calls).toHaveLength(importantCallCount); }); test("rejects malformed tool activity even when activity is enabled", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "important-session", branch: "main", }); const importantCallCount = bot.calls.length; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "malformed-enabled", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "unknown-phase-enabled", toolName: "read", phase: "surprised", }); expect(bot.calls).toHaveLength(importantCallCount); }); test("legacy-v1 normal terminal retires its exact settlement before the tool call id is reused", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "reused-v1", toolName: "read", phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "reused-v1", toolName: "read", phase: "completed", }); const runtime = daemon as unknown as { legacyToolStarts: Map }; expect(runtime.legacyToolStarts.has("S:tool:reused-v1")).toBe(false); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(1); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "reused-v1", toolName: "read", phase: "started", }); expect(runtime.legacyToolStarts.has("S:tool:reused-v1")).toBe(true); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(2); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(1); }); test.each([ "threaded", "flat", ] as const)("legacy-v1 %s terminal submission rejection retires exact settlement before reuse", async route => { const bot = new FakeBotApi(); if (route === "flat") { const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); return { ok: false, error_code: 400, description: "forum topics are disabled" }; } return await originalCall(method, body, options); }; } const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY], }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const toolCallId = `terminal-rejected-${route}`; const key = `S:tool:${toolCallId}`; const startFrame = { type: "tool_activity", sessionId: "S", toolCallId, toolName: "read", phase: "started", }; await daemon.handleSessionMessage(session, startFrame); const runtime = daemon as unknown as { legacyToolStarts: Map; liveMessages: Map; toolActivityOwners: Map; submitPool: (item: unknown) => boolean; }; expect(runtime.legacyToolStarts.has(key)).toBe(true); expect(runtime.liveMessages.has(key)).toBe(true); const originalSubmitPool = runtime.submitPool.bind(daemon); runtime.submitPool = () => false; try { await daemon.handleSessionMessage(session, { ...startFrame, phase: "completed" }); } finally { runtime.submitPool = originalSubmitPool; } expect(runtime.legacyToolStarts.has(key)).toBe(false); expect(runtime.liveMessages.has(key)).toBe(false); expect(runtime.toolActivityOwners.has(key)).toBe(false); const sendsBeforeReuse = bot.calls.filter(call => call.method === "sendMessage").length; const editsBeforeReuse = bot.calls.filter(call => call.method === "editMessageText").length; await daemon.handleSessionMessage(session, startFrame); expect(runtime.legacyToolStarts.has(key)).toBe(true); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(sendsBeforeReuse + 1); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(editsBeforeReuse); }); test("legacy-v1 unknown closes only an already-visible start as summary-free cancelled", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "legacy-visible", toolName: "read", phase: "started", }); const unknown = { type: "tool_activity", sessionId: "S", toolCallId: "legacy-visible", toolName: "read", phase: "unknown", argsSummary: "secret args", resultSummary: "secret result", }; await Promise.all([daemon.handleSessionMessage(session, unknown), daemon.handleSessionMessage(session, unknown)]); const edit = bot.calls.find(call => call.method === "editMessageText"); expect(String(edit?.body.text)).toContain("read — cancelled"); expect(String(edit?.body.text)).not.toContain("secret"); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(1); }); test("legacy-v1 unknown waits for an ordered in-flight start and settles it exactly once", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const startSendEntered = Promise.withResolvers(); const releaseStartSend = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { startSendEntered.resolve(); await releaseStartSend.promise; } return await originalCall(method, body, options); }; const started = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "legacy-race", toolName: "read", phase: "started", }); await startSendEntered.promise; const runtime = daemon as unknown as { liveMessages: Map; toolActivityOwners: Map; }; expect(runtime.toolActivityOwners.has("S:tool:legacy-race")).toBe(true); expect(runtime.liveMessages.has("S:tool:legacy-race")).toBe(false); expect(bot.calls).toHaveLength(0); let unknownSettled = false; const unknown = daemon .handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "legacy-race", toolName: "read", phase: "unknown", argsSummary: "secret race args", resultSummary: "secret race result", }) .finally(() => { unknownSettled = true; }); await Promise.resolve(); expect(unknownSettled).toBe(false); releaseStartSend.resolve(); await Promise.all([started, unknown]); const starts = bot.calls.filter( call => call.method === "sendMessage" && String(call.body.text).includes("read — started"), ); const cancelledEdits = bot.calls.filter( call => call.method === "editMessageText" && String(call.body.text).includes("read — cancelled"), ); expect(starts).toHaveLength(1); expect(cancelledEdits).toHaveLength(1); expect(String(cancelledEdits[0]!.body.text)).not.toContain("secret race"); expect(cancelledEdits[0]!.body.message_id).toBe(1); expect(runtime.liveMessages.has("S:tool:legacy-race")).toBe(false); expect(runtime.toolActivityOwners.has("S:tool:legacy-race")).toBe(false); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "legacy-race", toolName: "read", phase: "unknown", }); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(1); expect(bot.calls).toHaveLength(2); }); test("legacy-v1 unknown atomically cancels a start pending before identity", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "pending-legacy", toolName: "read", phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "pending-legacy", toolName: "read", phase: "unknown", argsSummary: "must not leak", }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); expect(bot.calls.some(call => String(call.body.text).includes("pending-legacy"))).toBe(false); const runtime = daemon as unknown as { pendingThreadedFrames: Map; legacyToolStarts: Map; toolActivityOwners: Map; }; expect(runtime.pendingThreadedFrames.get("S") ?? []).toHaveLength(0); expect(runtime.legacyToolStarts.has("S:tool:pending-legacy")).toBe(false); expect(runtime.toolActivityOwners.has("S:tool:pending-legacy")).toBe(false); }); test("legacy-v1 unknown removes the exact rate-limit-deferred start", async () => { const now = () => 4_000; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, now, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); const runtime = daemon as unknown as { pool: { availableTokens(): number; pending: number; submit(item: object): void }; flushPool(): Promise; legacyToolStarts: Map; }; const available = Math.floor(runtime.pool.availableTokens()); for (let index = 0; index < available; index++) { runtime.pool.submit({ sessionId: "drain", lane: "idle", itemId: `legacy-drain:${index}`, payload: { send: { method: "sendMessage", lane: "idle", text: `drain ${index}` } }, }); } await runtime.flushPool(); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "rate-deferred", toolName: "bash", phase: "started", }); expect(runtime.pool.pending).toBe(1); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "rate-deferred", toolName: "bash", phase: "unknown", resultSummary: "must not leak", }); expect(runtime.pool.pending).toBe(0); expect(runtime.legacyToolStarts.has("S:tool:rate-deferred")).toBe(false); expect(bot.calls).toHaveLength(0); }); test("legacy settlement rechecks a capability upgrade before accepting unknown", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "upgrade-authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { entered.resolve(); await release.promise; } return await originalCall(method, body, options); }; const start = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "upgrade-race", toolName: "read", phase: "started", }); await entered.promise; const upgrade = daemon.handleSessionMessage(session, { type: "hello", capabilities: [TOOL_ACTIVITY_CAPABILITY], }); let unknown!: Promise; try { unknown = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "upgrade-race", toolName: "read", phase: "unknown", resultSummary: "must not leak", }); } finally { release.resolve(); } await Promise.all([start, upgrade, unknown]); await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; expect(session.toolActivityCapability).toBe("v2"); expect(bot.calls.filter(call => String(call.body.text).includes("read — started"))).toHaveLength(1); expect(bot.calls.filter(call => String(call.body.text).includes("read — cancelled"))).toHaveLength(1); expect(bot.calls.every(call => !String(call.body.text).includes("must not leak"))).toBe(true); }); test("policy epoch transition settles the exact dispatch before unknown can act", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "policy-authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { entered.resolve(); await release.promise; } return await originalCall(method, body, options); }; const start = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "policy-race", toolName: "read", phase: "started", }); await entered.promise; const runtime = daemon as unknown as { toolActivityPolicyEpoch: number; opts: { toolActivity?: { enabled: boolean } }; cancelLegacyToolStartsForPolicyTransition(): Promise; legacyToolStarts: Map; }; let policyTransition!: Promise; let unknown!: Promise; try { runtime.toolActivityPolicyEpoch++; runtime.opts.toolActivity = { enabled: false }; policyTransition = runtime.cancelLegacyToolStartsForPolicyTransition(); unknown = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "policy-race", toolName: "read", phase: "unknown", argsSummary: "must not leak", }); } finally { release.resolve(); } await Promise.all([start, unknown, policyTransition]); await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; expect(bot.calls.filter(call => String(call.body.text).includes("read — started"))).toHaveLength(1); expect(bot.calls.filter(call => String(call.body.text).includes("read — cancelled"))).toHaveLength(1); expect(bot.calls.every(call => !String(call.body.text).includes("must not leak"))).toBe(true); expect(runtime.legacyToolStarts.has("S:tool:policy-race")).toBe(false); }); test("socket replacement cancels the exact dispatch and duplicate unknown stays inert", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("bash — started")) { entered.resolve(); await release.promise; } return await originalCall(method, body, options); }; const start = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "replaced", toolName: "bash", phase: "started", }); await entered.promise; const replacement = richSession(); try { (daemon as unknown as { dropSession(sessionToDrop: typeof session, reason: string): void }).dropSession( session, "same_authority_replaced", ); replacement.endpointDigest = "authority"; daemon.sessions.set("S", replacement); await daemon.handleSessionMessage(replacement, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY], }); } finally { release.resolve(); } await start; await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; for (let index = 0; index < 2; index++) { await daemon.handleSessionMessage(replacement, { type: "tool_activity", sessionId: "S", toolCallId: "replaced", toolName: "bash", phase: "unknown", }); } expect(bot.calls.filter(call => String(call.body.text).includes("bash — started"))).toHaveLength(1); expect(bot.calls.filter(call => String(call.body.text).includes("bash — cancelled"))).toHaveLength(1); const runtime = daemon as unknown as { legacyToolStarts: Map }; expect(runtime.legacyToolStarts.has("S:tool:replaced")).toBe(false); }); test("legacy start rejection settles failed and later unknown cannot emit", async () => { const bot = new FakeBotApi(); let rejectedAttempts = 0; const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { rejectedAttempts++; return { ok: false, description: "rejected" }; } return await originalCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "rejected", toolName: "read", phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "rejected", toolName: "read", phase: "unknown", }); expect(rejectedAttempts).toBe(1); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(0); const runtime = daemon as unknown as { legacyToolStarts: Map; toolActivityOwners: Map; }; expect(runtime.legacyToolStarts.has("S:tool:rejected")).toBe(false); expect(runtime.toolActivityOwners.has("S:tool:rejected")).toBe(false); }); test("v2 remains strict after later v1 and capability-absent hello frames", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "v2-visible", toolName: "read", phase: "started", }); expect(bot.calls.length).toBeGreaterThan(0); bot.calls = []; await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY], }); await daemon.handleSessionMessage(session, { type: "hello" }); expect(session.toolActivityCapability).toBe("v2"); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "v2-visible", toolName: "read", phase: "unknown", }); expect(bot.calls).toHaveLength(0); }); test("v1 upgrades to v2 and then strictly rejects unknown for a visible start", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY], }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "upgraded-visible", toolName: "read", phase: "started", }); expect(bot.calls.length).toBeGreaterThan(0); bot.calls = []; await daemon.handleSessionMessage(session, { type: "hello", capabilities: [TOOL_ACTIVITY_CAPABILITY] }); expect(session.toolActivityCapability).toBe("v2"); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "upgraded-visible", toolName: "read", phase: "unknown", }); expect(bot.calls).toHaveLength(0); }); test("daemon renderer rejects unsupported tool phases instead of producing an unknown terminal", () => { const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), toolActivity: { enabled: true }, }); const render = ( daemon as unknown as { renderThreadedFrame(frame: Record): { text: string } | undefined; } ).renderThreadedFrame.bind(daemon); expect( render({ type: "tool_activity", toolCallId: "unsupported", toolName: "read", phase: "surprised", }), ).toBeUndefined(); }); test("explicit off suppresses all noisy tool bubbles without suppressing session, assistant, or ask notifications", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: false }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "important-session", branch: "main", }); expect( bot.calls.some(call => call.method === "sendMessage" && String(call.body.text).includes("important-session")), ).toBe(true); const callsBeforeTools = bot.calls.length; for (const toolName of ["bash", "read", "task", "subagent"]) { await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: `disabled-${toolName}`, toolName, phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: `disabled-${toolName}`, toolName, phase: "completed", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: `disabled-failed-${toolName}`, toolName, phase: "failed", }); } expect(bot.calls).toHaveLength(callsBeforeTools); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "live", text: "Important assistant", messageRef: "important-answer", }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: "Important assistant answer", messageRef: "important-answer", }); await daemon.handleSessionMessage(session, { type: "action_needed", sessionId: "S", kind: "ask", id: "important-ask", question: "Important choice?", options: ["Continue"], }); expect( bot.calls.some( call => (call.method === "sendMessage" || call.method === "editMessageText") && String(call.body.text).includes("Important assistant"), ), ).toBe(true); expect(bot.calls.some(call => JSON.stringify(call.body).includes("Important choice?"))).toBe(true); }); test("/toolactivity off persists, suppresses new tools, and still terminalizes a visible start", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "visible", toolName: "read", phase: "started", }); await daemon.handleTelegramUpdate({ update_id: 960, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 1 }, }); expect(s.get("notifications.telegram.toolActivity.enabled")).toBe(false); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "Tool activity: off")).toBe( true, ); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "visible", toolName: "read", phase: "completed", argsSummary: "secret opt-out args", resultSummary: "secret opt-out result", }); const terminalEdit = bot.calls.find( call => call.method === "editMessageText" && String(call.body.text).includes("read — ok"), ); expect(terminalEdit).toBeDefined(); expect(String(terminalEdit?.body.text)).not.toContain("secret opt-out args"); expect(String(terminalEdit?.body.text)).not.toContain("secret opt-out result"); const deliveredCount = bot.calls.length; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "hidden", toolName: "task", phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "hidden", toolName: "task", phase: "completed", }); expect(bot.calls).toHaveLength(deliveredCount); }); test("/toolactivity off preserves a queued terminal edit for an already-visible tool", async () => { let nowMs = 3_000; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, now: () => nowMs, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "visible-terminal", toolName: "read", phase: "started", }); const internal = daemon as unknown as { pool: { availableTokens(nowMs?: number): number; pending: number; submit(item: object): void; }; flushPool(): Promise; }; const available = Math.floor(internal.pool.availableTokens()); for (let index = 0; index < available; index++) { internal.pool.submit({ sessionId: "token-drain", lane: "idle", itemId: `terminal-drain:${index}`, payload: { send: { method: "sendMessage", lane: "idle", text: `drain ${index}` } }, }); } await internal.flushPool(); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "visible-terminal", toolName: "read", phase: "completed", argsSummary: "stale policy args", resultSummary: "stale policy result", }); expect(internal.pool.pending).toBe(1); await daemon.handleTelegramUpdate({ update_id: 966, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 6 }, }); expect(internal.pool.pending).toBe(1); nowMs += 1_000; await internal.flushPool(); const terminalEdit = bot.calls.find( call => call.method === "editMessageText" && String(call.body.text).includes("read — ok"), ); expect(terminalEdit).toBeDefined(); expect(String(terminalEdit?.body.text)).not.toContain("stale policy args"); expect(String(terminalEdit?.body.text)).not.toContain("stale policy result"); }); test("/toolactivity rejects invalid input and foreign suffixes while addressed on/off persists and reports status", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); s.set("notifications.telegram.toolActivity.enabled", true); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); (daemon as unknown as { botUsername?: string }).botUsername = "SayknowCliBot"; await daemon.handleTelegramUpdate({ update_id: 961, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off accidental", message_id: 1 }, }); expect(s.get("notifications.telegram.toolActivity.enabled")).toBe(true); expect(bot.calls.some(call => call.body.text === "Usage: /toolactivity on|off")).toBe(true); bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 962, message: { chat: { id: 42, type: "private" }, text: "/toolactivity@OtherBot off", message_id: 2 }, }); expect(s.get("notifications.telegram.toolActivity.enabled")).toBe(true); expect(bot.calls).toHaveLength(0); await daemon.handleTelegramUpdate({ update_id: 963, message: { chat: { id: 42, type: "private" }, text: "/toolactivity@SayknowCliBot off", message_id: 3, }, }); expect(s.get("notifications.telegram.toolActivity.enabled")).toBe(false); expect(bot.calls.some(call => call.body.text === "Tool activity: off")).toBe(true); bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 964, message: { chat: { id: 42, type: "private" }, text: "/TOOLACTIVITY@SAYKNOWCLIBOT ON", message_id: 4, }, }); expect(s.get("notifications.telegram.toolActivity.enabled")).toBe(true); expect(bot.calls.some(call => call.body.text === "Tool activity: on")).toBe(true); const runtime = daemon as unknown as { toolActivityPolicyEpoch: number }; const epoch = runtime.toolActivityPolicyEpoch; await daemon.handleTelegramUpdate({ update_id: 965, message: { chat: { id: 42, type: "private" }, text: "/toolactivity on", message_id: 5, }, }); expect(runtime.toolActivityPolicyEpoch).toBe(epoch); }); test("/toolactivity off removes pending-topic and rate-limited tool starts", async () => { const now = () => 3_000; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, now, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "pending-topic", toolName: "read", phase: "started", }); await daemon.handleTelegramUpdate({ update_id: 963, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 3 }, }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); expect(bot.calls.some(call => String(call.body.text).includes("pending-topic — started"))).toBe(false); await daemon.handleTelegramUpdate({ update_id: 964, message: { chat: { id: 42, type: "private" }, text: "/toolactivity on", message_id: 4 }, }); const internal = daemon as unknown as { pool: { availableTokens(nowMs?: number): number; pending: number; submit(item: object): void; }; flushPool(): Promise; }; const available = Math.floor(internal.pool.availableTokens()); for (let index = 0; index < available; index++) { internal.pool.submit({ sessionId: "token-drain", lane: "idle", itemId: `token-drain:${index}`, payload: { send: { method: "sendMessage", lane: "idle", text: `drain ${index}` } }, }); } await internal.flushPool(); bot.calls = []; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "queued", toolName: "subagent", phase: "started", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "queued", toolName: "subagent", phase: "completed", }); expect(internal.pool.pending).toBe(2); await daemon.handleTelegramUpdate({ update_id: 965, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 5 }, }); expect(internal.pool.pending).toBe(0); expect(bot.calls.some(call => String(call.body.text).includes("subagent — started"))).toBe(false); expect(bot.calls.some(call => String(call.body.text).includes("subagent — ok"))).toBe(false); }); test("/toolactivity off preserves a queued terminal for an already-granted start", async () => { const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { entered.resolve(); await release.promise; } return await originalCall(method, body, options); }; const toolSend = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "granted", toolName: "read", phase: "started", }); await entered.promise; const terminal = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "granted", toolName: "read", phase: "completed", }); const toggle = daemon.handleTelegramUpdate({ update_id: 964, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 4 }, }); let acknowledgedBeforeRelease = false; let disabledBeforeRelease = false; try { await Bun.sleep(0); acknowledgedBeforeRelease = bot.calls.some(call => call.body.text === "Tool activity: off"); const runtime = daemon as unknown as { opts: { toolActivity?: { enabled: boolean } } }; for (let attempt = 0; attempt < 100 && runtime.opts.toolActivity?.enabled !== false; attempt++) { await Bun.sleep(5); } disabledBeforeRelease = runtime.opts.toolActivity?.enabled === false; } finally { release.resolve(); } expect(acknowledgedBeforeRelease).toBe(false); expect(disabledBeforeRelease).toBe(true); await Promise.all([toolSend, toggle, terminal]); const toolIndex = bot.calls.findIndex(call => String(call.body.text).includes("read — started")); const ackIndex = bot.calls.findIndex(call => call.body.text === "Tool activity: off"); expect(toolIndex).toBeGreaterThanOrEqual(0); expect(ackIndex).toBeGreaterThan(toolIndex); expect( bot.calls.some(call => call.method === "editMessageText" && String(call.body.text).includes("read — ok")), ).toBe(true); }); test("parallel tool bubbles retain in-flight entries and evict completed entries", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, WebSocketImpl: FakeWs as any, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; const sendToolActivity = async (toolCallId: "A" | "B", phase: "started" | "completed") => daemon.handleSessionMessage(session as never, { type: "tool_activity", sessionId: "S", toolCallId, toolName: `tool-${toolCallId}`, phase, }); await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; const liveMessages = (daemon as unknown as { liveMessages: Map }).liveMessages; await sendToolActivity("A", "started"); await sendToolActivity("B", "started"); expect(liveMessages.get("S:tool:A")).toBe(1); expect(liveMessages.get("S:tool:B")).toBe(2); await sendToolActivity("A", "completed"); expect(bot.calls.filter(call => call.method === "editMessageText")).toEqual([ expect.objectContaining({ body: expect.objectContaining({ message_id: 1, text: expect.stringContaining("tool-A — ok") }), }), ]); expect(liveMessages.has("S:tool:A")).toBe(false); expect(liveMessages.get("S:tool:B")).toBe(2); await sendToolActivity("B", "completed"); const sends = bot.calls.filter(call => call.method === "sendMessage"); const edits = bot.calls.filter(call => call.method === "editMessageText"); expect(sends).toHaveLength(2); expect(edits).toHaveLength(2); expect(edits.map(call => call.body.message_id)).toEqual([1, 2]); expect(edits.map(call => call.body.text)).toEqual([ expect.stringContaining("tool-A — ok"), expect.stringContaining("tool-B — ok"), ]); expect(liveMessages.has("S:tool:A")).toBe(false); expect(liveMessages.has("S:tool:B")).toBe(false); ( daemon as unknown as { recordLiveMessage(sessionId: string, coalesceKey: string, messageId: number): void } ).recordLiveMessage("S", "turn:one", 101); ( daemon as unknown as { recordLiveMessage(sessionId: string, coalesceKey: string, messageId: number): void } ).recordLiveMessage("S", "turn:two", 102); expect(liveMessages.has("S:turn:one")).toBe(false); expect(liveMessages.get("S:turn:two")).toBe(102); }); test("recovered tool activity uses the delivered logical session for its start and terminal", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); await replayResumedIdentity(daemon, "TRANSPORT-A", "LOGICAL-B", { url: "ws://a", token: "a-token" }); const session = daemon.sessions.get("TRANSPORT-A")!; bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "LOGICAL-B", toolCallId: "recovered-tool", toolName: "read", phase: "started", }); const liveMessages = (daemon as unknown as { liveMessages: Map }).liveMessages; expect(liveMessages.get("LOGICAL-B:tool:recovered-tool")).toBe(1); expect(liveMessages.has("TRANSPORT-A:tool:recovered-tool")).toBe(false); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "LOGICAL-B", toolCallId: "recovered-tool", toolName: "read", phase: "completed", }); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); expect(bot.calls.filter(call => call.method === "editMessageText")).toEqual([ expect.objectContaining({ body: expect.objectContaining({ message_id: 1, text: expect.stringContaining("read — ok") }), }), ]); expect(liveMessages.has("LOGICAL-B:tool:recovered-tool")).toBe(false); expect( (daemon as unknown as { toolActivityOwners: Map }).toolActivityOwners.has( "LOGICAL-B:tool:recovered-tool", ), ).toBe(false); }); test("failed terminal tool delivery evicts its key before a later reuse", async () => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, WebSocketImpl: FakeWs as any, }); const session = { sessionId: "S", token: "tok", ws: { readyState: 1, send() {} }, pending: new Map() }; const sendToolActivity = async (phase: "started" | "completed") => daemon.handleSessionMessage(session as never, { type: "tool_activity", sessionId: "S", toolCallId: "A", toolName: "tool-A", phase, }); await daemon.handleSessionMessage(session as never, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await sendToolActivity("started"); const liveMessages = (daemon as unknown as { liveMessages: Map }).liveMessages; expect(liveMessages.get("S:tool:A")).toBe(1); const normalCall = bot.call.bind(bot); bot.call = (async (method: string, body: unknown) => { bot.calls.push({ method, body }); if (method === "editMessageText" || method === "sendMessage") throw new Error("terminal delivery failed"); return normalCall(method, body); }) as typeof bot.call; await sendToolActivity("completed"); expect(liveMessages.has("S:tool:A")).toBe(false); bot.call = normalCall; bot.calls = []; await sendToolActivity("started"); expect(bot.calls.some(call => call.method === "editMessageText")).toBe(false); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(1); expect(liveMessages.has("S:tool:A")).toBe(true); }); test("delivers a tool start queued before identity without changing its policy epoch", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "before-identity", toolName: "read", phase: "started", }); expect(bot.calls.some(call => String(call.body.text).includes("read — started"))).toBe(false); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); expect(bot.calls.some(call => String(call.body.text).includes("read — started"))).toBe(true); }); test("drops a queued tool start when endpoint authority changes during topic creation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const topicStarted = Promise.withResolvers(); const releaseTopic = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { topicStarted.resolve(); await releaseTopic.promise; } return await originalCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://old", "old-token"); const oldSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(oldSession, { type: "tool_activity", sessionId: "S", toolCallId: "old-authority", toolName: "read", phase: "started", }); const identity = daemon.handleSessionMessage(oldSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await topicStarted.promise; daemon.connectSession("S", "ws://new", "new-token"); releaseTopic.resolve(); await identity; await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; expect(bot.calls.some(call => String(call.body.text).includes("read — started"))).toBe(false); }); test("drops a queued tool start after close and same-authority reconnect during topic creation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const topicStarted = Promise.withResolvers(); const releaseTopic = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { topicStarted.resolve(); await releaseTopic.promise; } return await originalCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://old", "old-token"); const oldSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(oldSession, { type: "tool_activity", sessionId: "S", toolCallId: "closed-authority", toolName: "read", phase: "started", }); const identity = daemon.handleSessionMessage(oldSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await topicStarted.promise; oldSession.ws.close(); daemon.connectSession("S", "ws://old", "old-token"); releaseTopic.resolve(); await identity; await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; expect(daemon.sessions.get("S")).not.toBe(oldSession); expect(bot.calls.some(call => String(call.body.text).includes("read — started"))).toBe(false); }); test("settings-driven daemon replacement terminalizes visible tools before disabled successor starts", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const oldDaemon = new TelegramNotificationDaemon({ settings: s, ownerId: "old-owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); oldDaemon.connectSession("S", "ws://old", "old-token"); const oldSession = oldDaemon.sessions.get("S")!; await oldDaemon.handleSessionMessage(oldSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await oldDaemon.handleSessionMessage(oldSession, { type: "tool_activity", sessionId: "S", toolCallId: "reload-visible", toolName: "read", phase: "started", }); expect(bot.calls.some(call => String(call.body.text).includes("read — started"))).toBe(true); expect( ( oldDaemon as unknown as { toolActivityOwners: Map; } ).toolActivityOwners.has("S:tool:reload-visible"), ).toBe(true); expect( (oldDaemon as unknown as { liveMessages: Map }).liveMessages.has("S:tool:reload-visible"), ).toBe(true); oldDaemon.requestStop("reload"); await (oldDaemon as unknown as { toolShutdownBarrier: Promise }).toolShutdownBarrier; expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(1); expect( bot.calls.some( call => call.method === "editMessageText" && String(call.body.text).includes("read — cancelled"), ), ).toBe(true); expect(bot.calls.some(call => String(call.body.text).includes("read — unknown"))).toBe(false); const callsAfterCleanup = bot.calls.length; const successor = new TelegramNotificationDaemon({ settings: s, ownerId: "new-owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: false }, }); successor.connectSession("S", "ws://new", "new-token"); await successor.handleSessionMessage(successor.sessions.get("S")!, { type: "tool_activity", sessionId: "S", toolCallId: "reload-visible", toolName: "read", phase: "completed", }); expect(bot.calls).toHaveLength(callsAfterCleanup); }); test("off then on cannot revive a tool start held behind the reconnect replay barrier", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; FakeWs.instances[0]!.dispatchEvent(new Event("open")); expect(session.replayPending).toBe(true); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "replay-held", toolName: "subagent", phase: "started", }); expect(session.replayQueue).toHaveLength(1); await daemon.handleTelegramUpdate({ update_id: 970, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 70 }, }); expect(session.replayQueue).toHaveLength(0); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "admitted-while-off", toolName: "read", phase: "started", }); expect(session.replayQueue).toHaveLength(0); await daemon.handleTelegramUpdate({ update_id: 971, message: { chat: { id: 42, type: "private" }, text: "/toolactivity on", message_id: 71 }, }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); expect(bot.calls.some(call => String(call.body.text).includes("subagent — started"))).toBe(false); }); test("disabled terminals cannot cross endpoint authority when toolCallId is reused", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://old", "old-token"); const oldSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(oldSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; await daemon.handleSessionMessage(oldSession, { type: "tool_activity", sessionId: "S", toolCallId: "reused", toolName: "read", phase: "started", }); daemon.connectSession("S", "ws://replacement", "replacement-token"); await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; const editsAfterReplacement = bot.calls.filter(call => call.method === "editMessageText").length; expect(editsAfterReplacement).toBe(1); (daemon as unknown as { opts: { toolActivity?: { enabled: boolean } } }).opts.toolActivity = { enabled: false, }; await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "tool_activity", sessionId: "S", toolCallId: "reused", toolName: "read", phase: "completed", }); expect(bot.calls.filter(call => call.method === "editMessageText")).toHaveLength(editsAfterReplacement); }); test("off/on epoch fences a replayed start already awaiting topic creation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); await (daemon as unknown as { pairedChatIsPrivate(): Promise }).pairedChatIsPrivate(); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; FakeWs.instances[0]!.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "awaiting-topic", toolName: "read", phase: "started", }); const topicStarted = Promise.withResolvers(); const releaseTopic = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { topicStarted.resolve(); await releaseTopic.promise; } return await originalCall(method, body, options); }; const replay = daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); await replay; const identity = daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await topicStarted.promise; await daemon.handleTelegramUpdate({ update_id: 972, message: { chat: { id: 42, type: "private" }, text: "/toolactivity off", message_id: 72 }, }); await daemon.handleTelegramUpdate({ update_id: 973, message: { chat: { id: 42, type: "private" }, text: "/toolactivity on", message_id: 73 }, }); releaseTopic.resolve(); await identity; expect(bot.calls.some(call => String(call.body.text).includes("read — started"))).toBe(false); }); test("shutdown waits for a granted start and rejects ambiguous delivery", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "known-visible", toolName: "subagent", phase: "started", }); const started = Promise.withResolvers(); const release = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { started.resolve(); await release.promise; throw new Error("ambiguous transport"); } return await originalCall(method, body, options); }; const delivery = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "ambiguous", toolName: "read", phase: "started", }); await started.promise; daemon.requestStop("reload"); release.resolve(); await delivery; await expect((daemon as unknown as { toolShutdownBarrier: Promise }).toolShutdownBarrier).rejects.toThrow( "ambiguous", ); expect( bot.calls.some( call => call.method === "editMessageText" && String(call.body.text).includes("subagent — cancelled"), ), ).toBe(true); expect(bot.calls.some(call => String(call.body.text).includes("subagent — unknown"))).toBe(false); }); test("strict shutdown sees a delayed best-effort cleanup failure", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://old", "old-token"); const oldSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(oldSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await daemon.handleSessionMessage(oldSession, { type: "tool_activity", sessionId: "S", toolCallId: "overlapping-cleanup", toolName: "read", phase: "started", }); const firstEditStarted = Promise.withResolvers(); const releaseFirstEdit = Promise.withResolvers(); const originalCall = bot.call.bind(bot); let editAttempt = 0; bot.call = async (method, body, options) => { if (method === "editMessageText" && ++editAttempt === 1) { firstEditStarted.resolve(); await releaseFirstEdit.promise; return { ok: false, description: "best effort rejected" }; } return await originalCall(method, body, options); }; daemon.connectSession("S", "ws://new", "new-token"); await firstEditStarted.promise; daemon.requestStop("reload"); releaseFirstEdit.resolve(); await (daemon as unknown as { toolShutdownBarrier: Promise }).toolShutdownBarrier; expect(editAttempt).toBe(2); expect( (daemon as unknown as { unresolvedToolTerminalizations: Map }).unresolvedToolTerminalizations .size, ).toBe(0); }); test("retains malformed best-effort terminal failures for strict shutdown cleanup", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://old", "old-token"); const oldSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(oldSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await daemon.handleSessionMessage(oldSession, { type: "tool_activity", sessionId: "S", toolCallId: "malformed-terminal", toolName: "read", phase: "started", }); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "editMessageText") { bot.calls.push({ method, body, options }); return undefined; } return await originalCall(method, body, options); }; daemon.connectSession("S", "ws://new", "new-token"); await (daemon as unknown as { toolTerminalizationChain: Promise }).toolTerminalizationChain; expect( (daemon as unknown as { unresolvedToolTerminalizations: Map }).unresolvedToolTerminalizations .size, ).toBe(1); bot.call = originalCall; daemon.requestStop("reload"); await (daemon as unknown as { toolShutdownBarrier: Promise }).toolShutdownBarrier; expect(bot.calls.filter(call => call.method === "editMessageText").length).toBeGreaterThanOrEqual(2); expect( (daemon as unknown as { unresolvedToolTerminalizations: Map }).unresolvedToolTerminalizations .size, ).toBe(0); }); test("strict shutdown attempts every visible tool when an earlier cleanup fails", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; for (const toolCallId of ["first", "second"]) { await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId, toolName: "read", phase: "started", }); } const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "editMessageText" && (body as { message_id?: number }).message_id === 1) { bot.calls.push({ method, body, options }); return { ok: false, description: "first cleanup rejected" }; } return await originalCall(method, body, options); }; daemon.requestStop("reload"); await expect((daemon as unknown as { toolShutdownBarrier: Promise }).toolShutdownBarrier).rejects.toThrow( "first cleanup rejected", ); const edits = bot.calls.filter(call => call.method === "editMessageText"); expect(edits.filter(call => call.body.message_id === 1)).toHaveLength(5); expect(edits.some(call => call.body.message_id === 2)).toBe(true); const liveMessages = (daemon as unknown as { liveMessages: Map }).liveMessages; expect(liveMessages.has("S:tool:first")).toBe(true); expect(liveMessages.has("S:tool:second")).toBe(false); }); test("shutdown retries rejected terminal edits and restores visible ownership", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, toolActivity: { enabled: true }, }); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "retry-terminal", toolName: "read", phase: "started", }); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => method === "editMessageText" ? { ok: false, description: "temporary rejection" } : await originalCall(method, body, options); daemon.requestStop("reload"); await expect((daemon as unknown as { toolShutdownBarrier: Promise }).toolShutdownBarrier).rejects.toThrow( "temporary rejection", ); expect( (daemon as unknown as { liveMessages: Map }).liveMessages.has("S:tool:retry-terminal"), ).toBe(true); }); }); describe("telegram daemon /btw reservation and capability boundaries", () => { async function daemonWithTopic( input: { enabled?: boolean; capability?: boolean; rich?: boolean; now?: () => number; fetchImpl?: typeof fetch; } = {}, ) { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: setPrivateAgentDir(settings(agentDir), agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, rich: { enabled: input.rich ?? true }, now: input.now, WebSocketImpl: FakeWs as any, fetchImpl: input.fetchImpl, btw: { enabled: input.enabled ?? true }, }); daemon.connectSession("S", "ws://s", "ts"); if (input.capability ?? true) await enableEphemeralTurns(daemon); await daemon.handleSessionMessage(daemon.sessions.get("S")!, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); return { agentDir, bot, daemon, threadId: bot.createdTopicThreadIds.at(-1), }; } test("reserves every /btw form before outstanding-ask reply routing", async () => { const { bot, daemon, threadId } = await daemonWithTopic(); daemon.messageRoutes.set("1799", { sessionId: "S", actionId: "outstanding" }); daemon.sessions.get("S")!.pending.set("outstanding", { sessionId: "S", actionId: "outstanding" }); for (const [updateId, messageId, text] of [ [801, 1801, "/btw"], [802, 1802, "/btw@OtherBot question"], [803, 1803, "/btw-not-a-command"], [805, 1805, "/btw@OwnBot@malformed question"], ] as const) { await daemon.handleTelegramUpdate({ update_id: updateId, message: { chat: { id: 42 }, message_thread_id: threadId, reply_to_message: { message_id: 1799 }, text, message_id: messageId, }, }); } const originalCall = bot.call.bind(bot); bot.call = async (method, body, opts) => method === "getFile" ? { ok: true, result: {} } : originalCall(method, body, opts); await daemon.handleTelegramUpdate({ update_id: 804, message: { chat: { id: 42 }, message_thread_id: threadId, reply_to_message: { message_id: 1799 }, text: "/btw with attachment", message_id: 1804, photo: [{ file_id: "attachment" }], }, }); const frames = FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame) as { type?: string }); expect( frames.some( frame => frame.type === "ephemeral_turn" || frame.type === "user_message" || frame.type === "reply", ), ).toBe(false); const replies = bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "Usage: /btw ", ); expect(replies).toHaveLength(4); expect(replies).toEqual( expect.arrayContaining( [1801, 1803, 1804, 1805].map(messageId => expect.objectContaining({ body: expect.objectContaining({ message_thread_id: threadId, reply_parameters: { message_id: messageId }, }), options: expect.objectContaining({ noRetry: true, signal: expect.any(AbortSignal) }), }), ), ), ); }); test.each([ ["exact scalar boundary", "x".repeat(BTW_QUESTION_MAX_UNICODE_SCALARS)], ["exact UTF-8 byte boundary with multibyte scalars", "😀".repeat(BTW_QUESTION_MAX_UTF8_BYTES / 4)], ])("accepts /btw at the %s", async (_name, question) => { const { bot, daemon, threadId } = await daemonWithTopic(); await daemon.handleTelegramUpdate({ update_id: 1900, message: { chat: { id: 42 }, message_thread_id: threadId, text: `/btw ${question}`, message_id: 1900 }, }); expect(FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame))).toContainEqual( expect.objectContaining({ type: "ephemeral_turn", question }), ); expect( bot.calls.some( call => call.method === "sendMessage" && call.body.text === "Question must be at most 4096 Unicode scalar values and 16384 UTF-8 bytes.", ), ).toBe(false); }); test.each([ ["one-over scalar boundary", "x".repeat(BTW_QUESTION_MAX_UNICODE_SCALARS + 1)], ["one-over UTF-8 byte boundary", `${"😀".repeat(BTW_QUESTION_MAX_UTF8_BYTES / 4)}x`], ])("rejects /btw at the %s before creating pending state", async (_name, question) => { const { bot, daemon, threadId } = await daemonWithTopic(); await daemon.handleTelegramUpdate({ update_id: 1901, message: { chat: { id: 42 }, message_thread_id: threadId, text: `/btw ${question}`, message_id: 1901 }, }); expect( FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).some(frame => frame.type === "ephemeral_turn"), ).toBe(false); expect(bot.calls.at(-1)).toMatchObject({ method: "sendMessage", body: { message_thread_id: threadId, reply_parameters: { message_id: 1901 }, text: "Question must be at most 4096 Unicode scalar values and 16384 UTF-8 bytes.", }, options: { noRetry: true, signal: expect.any(AbortSignal) }, }); }); test("reserves an oversized /btw update after an ambiguous reply so duplicate replay is exactly once", async () => { const { bot, daemon, threadId } = await daemonWithTopic(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage") { bot.calls.push({ method, body, options }); throw new Error("transport outcome unknown"); } return call(method, body, options); }; const update = { update_id: 1902, message: { chat: { id: 42 }, message_thread_id: threadId, text: `/btw ${"x".repeat(BTW_QUESTION_MAX_UNICODE_SCALARS + 1)}`, message_id: 1902, }, }; await daemon.handleTelegramUpdate(update); await daemon.handleTelegramUpdate(update); const replies = bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "Question must be at most 4096 Unicode scalar values and 16384 UTF-8 bytes.", ); expect(replies).toHaveLength(1); expect(replies[0]).toMatchObject({ body: { reply_parameters: { message_id: 1902 }, text: "Question must be at most 4096 Unicode scalar values and 16384 UTF-8 bytes.", }, options: { noRetry: true, signal: expect.any(AbortSignal) }, }); expect( FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).some(frame => frame.type === "ephemeral_turn"), ).toBe(false); }); test.each([ ["disabled", { enabled: false }, 1806], ["disconnected", {}, 1807], ])("returns /btw attachment usage before %s branches", async (_name, options, messageId) => { const { bot, daemon, threadId } = await daemonWithTopic(options); if (_name === "disconnected") (daemon.sessions.get("S")!.ws as unknown as FakeWs).dispatchEvent(new Event("close")); await daemon.handleTelegramUpdate({ update_id: messageId, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw attachment", message_id: messageId, photo: [{ file_id: "attachment" }], }, }); expect(bot.calls.at(-1)).toMatchObject({ method: "sendMessage", body: { message_thread_id: threadId, reply_parameters: { message_id: messageId }, text: "Usage: /btw ", }, options: { noRetry: true, signal: expect.any(AbortSignal) }, }); expect( FakeWs.instances .flatMap(socket => socket.sent.map(frame => JSON.parse(frame))) .some(frame => frame.type === "ephemeral_turn"), ).toBe(false); }); test("returns the exact disabled and unsupported replies without emitting an ephemeral frame", async () => { const disabled = await daemonWithTopic({ enabled: false }); await disabled.daemon.handleTelegramUpdate({ update_id: 804, message: { chat: { id: 42 }, message_thread_id: disabled.threadId, text: "/btw status?", message_id: 1804 }, }); expect(disabled.bot.calls.at(-1)).toMatchObject({ method: "sendMessage", body: { message_thread_id: disabled.threadId, reply_parameters: { message_id: 1804 }, text: "Telegram /btw is disabled in local settings.", }, options: { noRetry: true, signal: expect.any(AbortSignal) }, }); expect( FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).some(frame => frame.type === "ephemeral_turn"), ).toBe(false); const unsupported = await daemonWithTopic({ capability: false }); const unsupportedSession = unsupported.daemon.sessions.get("S")!; (unsupportedSession.ws as unknown as FakeWs).dispatchEvent(new Event("open")); await unsupported.daemon.handleSessionMessage(unsupportedSession, { type: "hello", capabilities: ["event_replay_v1"], }); await unsupported.daemon.handleSessionMessage(unsupportedSession, { type: "event_replay_result", ok: true, id: unsupportedSession.replayId, generation: 1, lastSeq: 0, events: [], }); expect(unsupportedSession.hostGeneration).toBe(1); expect(unsupportedSession.ephemeralCapable).toBe(false); await unsupported.daemon.handleTelegramUpdate({ update_id: 805, message: { chat: { id: 42 }, message_thread_id: unsupported.threadId, text: "/btw status?", message_id: 1805 }, }); expect(unsupported.bot.calls.at(-1)).toMatchObject({ method: "sendMessage", body: { message_thread_id: unsupported.threadId, reply_parameters: { message_id: 1805 }, text: "Restart this SKC session to enable /btw.", }, options: { noRetry: true, signal: expect.any(AbortSignal) }, }); expect( FakeWs.instances[0]!.sent.map(frame => JSON.parse(frame)).some(frame => frame.type === "ephemeral_turn"), ).toBe(false); }); test("queues split /btw HTML fallback chunks through the shared rate limiter in order", async () => { const now = () => 1_000; const { bot, daemon, threadId } = await daemonWithTopic({ rich: false, now }); await daemon.handleTelegramUpdate({ update_id: 806, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw split", message_id: 1806 }, }); const session = daemon.sessions.get("S")!; const request = (session.ws as unknown as FakeWs).sent .map(frame => JSON.parse(frame) as Record) .find(frame => frame.type === "ephemeral_turn")!; const markdown = `# Split answer\n\n${"가".repeat(9_000)}`; const expectedChunks = splitTelegramHtml(markdownToTelegramHtml(markdown)); expect(expectedChunks.length).toBeGreaterThan(1); const pool = (daemon as unknown as { pool: { availableTokens(nowMs?: number): number } }).pool; const tokensBefore = pool.availableTokens(); bot.calls = []; await daemon.handleSessionMessage(session, { ...request, type: "ephemeral_turn_result", status: "ok", text: markdown, }); const sends = bot.calls.filter(call => call.method === "sendMessage"); expect(sends.map(call => call.body.text)).toEqual(expectedChunks); expect(sends[0]?.body.reply_parameters).toEqual({ message_id: 1806 }); expect(sends.slice(1).every(call => call.body.reply_parameters === undefined)).toBe(true); expect(sends.every(call => call.options?.noRetry === true)).toBe(true); expect(tokensBefore - pool.availableTokens()).toBe(expectedChunks.length); }); test("partial accepted /btw fallback tombstones the turn without a replacement notice", async () => { const now = () => 2_000; const { bot, daemon, threadId } = await daemonWithTopic({ rich: false, now }); await daemon.handleTelegramUpdate({ update_id: 807, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw partial", message_id: 1807 }, }); const session = daemon.sessions.get("S")!; const request = (session.ws as unknown as FakeWs).sent .map(frame => JSON.parse(frame) as Record) .find(frame => frame.type === "ephemeral_turn")!; const markdown = "나".repeat(12_000); const expectedChunks = splitTelegramHtml(markdownToTelegramHtml(markdown)); expect(expectedChunks.length).toBeGreaterThan(2); const normalCall = bot.call.bind(bot); let attempts = 0; bot.calls = []; bot.call = async (method, body, options) => { if (method === "sendMessage" && expectedChunks.includes((body as { text?: string }).text ?? "")) { bot.calls.push({ method, body, options }); attempts += 1; return attempts === 2 ? { ok: false, description: "rejected" } : { ok: true, result: { message_id: 1 } }; } return normalCall(method, body, options); }; await daemon.handleSessionMessage(session, { ...request, type: "ephemeral_turn_result", status: "ok", text: markdown, }); const sends = bot.calls.filter(call => call.method === "sendMessage"); expect(sends.map(call => call.body.text)).toEqual(expectedChunks.slice(0, 2)); expect(sends.every(call => call.options?.noRetry === true)).toBe(true); expect(attempts).toBe(2); await daemon.handleSessionMessage(session, { type: "session_closed", sessionId: "S" }); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ), ).toHaveLength(0); }); test("removes a queued stale /btw fallback before reconnect replay", async () => { const now = () => 3_000; const { bot, daemon, threadId } = await daemonWithTopic({ rich: false, now }); const internal = daemon as unknown as { pool: { availableTokens(nowMs?: number): number; pending: number; submit(item: object): void; }; flushPool(): Promise; }; const available = Math.floor(internal.pool.availableTokens()); expect(available).toBeGreaterThan(0); for (let index = 0; index < available; index++) { internal.pool.submit({ sessionId: "token-drain", lane: "idle", itemId: `token-drain:${index}`, payload: { send: { method: "sendMessage", lane: "idle", text: `drain ${index}` }, }, }); } await internal.flushPool(); expect(internal.pool.availableTokens()).toBe(0); bot.calls = []; await daemon.handleTelegramUpdate({ update_id: 808, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw queued", message_id: 1808 }, }); const session = daemon.sessions.get("S")!; const socket = session.ws as unknown as FakeWs; const request = socket.sent .map(frame => JSON.parse(frame) as Record) .find(frame => frame.type === "ephemeral_turn")!; const handling = daemon.handleSessionMessage(session, { ...request, type: "ephemeral_turn_result", status: "ok", text: "queued answer", }); await Bun.sleep(0); expect(internal.pool.pending).toBe(1); socket.close(); await handling; expect(internal.pool.pending).toBe(0); expect(bot.calls.filter(call => call.method === "sendMessage")).toEqual([]); daemon.connectSession("S", "ws://s", "ts"); await enableEphemeralTurns(daemon); const replacement = daemon.sessions.get("S")!.ws as unknown as FakeWs; expect( replacement.sent .map(frame => JSON.parse(frame) as Record) .find(frame => frame.type === "ephemeral_turn"), ).toBeUndefined(); }); test("reports true /btw capacity once and reuses completed tombstone capacity", async () => { const { bot, daemon, threadId } = await daemonWithTopic(); const session = daemon.sessions.get("S")!; const socket = session.ws as unknown as FakeWs; const requests = () => socket.sent .map(frame => JSON.parse(frame) as Record) .filter(frame => frame.type === "ephemeral_turn"); bot.calls = []; for (let index = 0; index < 256; index++) { await daemon.handleTelegramUpdate({ update_id: 10_000 + index, message: { chat: { id: 42 }, message_thread_id: threadId, text: `/btw pending ${index}`, message_id: 20_000 + index, }, }); } expect(requests()).toHaveLength(256); const capacityUpdate = { update_id: 10_256, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw over capacity", message_id: 20_256, }, }; await daemon.handleTelegramUpdate(capacityUpdate); await daemon.handleTelegramUpdate(capacityUpdate); const capacityReplies = () => bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "Too many /btw questions are pending. Wait for one to finish and try again.", ); expect(capacityReplies()).toEqual([ expect.objectContaining({ body: expect.objectContaining({ message_thread_id: threadId, reply_parameters: { message_id: 20_256 }, }), options: expect.objectContaining({ noRetry: true }), }), ]); expect(requests()).toHaveLength(256); const normalCall = bot.call.bind(bot); let unknownCapacityAttempts = 0; bot.call = async (method, body, options) => { if ( method === "sendMessage" && (body as { reply_parameters?: { message_id?: unknown } }).reply_parameters?.message_id === 20_258 ) { bot.calls.push({ method, body, options }); unknownCapacityAttempts += 1; throw new Error("capacity delivery outcome unknown"); } return normalCall(method, body, options); }; const unknownCapacityUpdate = { update_id: 10_258, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw unknown capacity delivery", message_id: 20_258, }, }; await expect(daemon.handleTelegramUpdate(unknownCapacityUpdate)).rejects.toThrow( "capacity delivery outcome unknown", ); await daemon.handleTelegramUpdate(unknownCapacityUpdate); expect(unknownCapacityAttempts).toBe(1); expect(capacityReplies().at(-1)).toMatchObject({ body: expect.objectContaining({ reply_parameters: { message_id: 20_258 } }), options: expect.objectContaining({ noRetry: true }), }); expect(requests()).toHaveLength(256); const capacityReplyCountBeforeReuse = capacityReplies().length; bot.call = normalCall; const completed = requests()[0]!; await daemon.handleSessionMessage(session, { ...completed, type: "ephemeral_turn_result", status: "failed", }); await daemon.handleTelegramUpdate({ update_id: 10_257, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw after completion", message_id: 20_257, }, }); expect(requests()).toHaveLength(257); expect(requests().at(-1)).toMatchObject({ question: "after completion", messageId: 20_257 }); expect(capacityReplies()).toHaveLength(capacityReplyCountBeforeReuse); }, 30_000); test("reserves /btw updates before visible replies and host execution", async () => { const { agentDir, bot, daemon, threadId } = await unavailableControlHarness(); await enableEphemeralTurns(daemon); let persistedBeforeSend = false; const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage" && (body as { text?: unknown }).text === "Usage: /btw ") persistedBeforeSend = fs.existsSync(daemonPaths(agentDir).seenUpdates); return originalCall(method, body, options); }; await daemon.handleTelegramUpdate({ update_id: 1806, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw", message_id: 2806 }, }); expect(persistedBeforeSend).toBe(true); const socket = FakeWs.instances[0]!; let persistedBeforeExecution = false; const send = socket.send.bind(socket); socket.send = data => { if (JSON.parse(data).type === "ephemeral_turn") { const state = JSON.parse(fs.readFileSync(daemonPaths(agentDir).seenUpdates, "utf8")) as { updateIds: number[]; }; persistedBeforeExecution = state.updateIds.includes(1807); } send(data); }; await daemon.handleTelegramUpdate({ update_id: 1807, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw reserve", message_id: 2807 }, }); expect(persistedBeforeExecution).toBe(true); }); test("does not send or execute /btw when update reservation cannot persist", async () => { let fail = false; const fsImpl: TelegramDaemonFs = { mkdir: (file, opts) => fs.promises.mkdir(file, opts).then(() => undefined), readFile: (file, encoding) => fs.promises.readFile(file, encoding), writeFile: (file, data, opts) => fail && file.includes("telegram-seen-updates") ? Promise.reject(new Error("disk failure")) : fs.promises.writeFile(file, data, opts), rename: (oldPath, newPath) => fs.promises.rename(oldPath, newPath).then(() => undefined), unlink: file => fs.promises.unlink(file), open: (file, flags, mode) => fs.promises.open(file, flags, mode), readdir: file => fs.promises.readdir(file), chmod: (file, mode) => fs.promises.chmod(file, mode), }; const { bot, daemon, threadId } = await unavailableControlHarness(fsImpl); await enableEphemeralTurns(daemon); fail = true; bot.calls = []; const socket = FakeWs.instances[0]!; const sentBefore = socket.sent.length; await daemon.handleTelegramUpdate({ update_id: 1808, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw no replay", message_id: 2808 }, }); expect(bot.calls).toEqual([]); expect( socket.sent .slice(sentBefore) .map(frame => JSON.parse(frame)) .some(frame => frame.type === "ephemeral_turn"), ).toBe(false); }); test("keeps endpoint tuples distinct and rejects a different one from reusing a durable same-ID topic", async () => { const { daemon, threadId } = await daemonWithTopic(); expect(endpointAuthorityDigest("ws://x/", "abc")).not.toBe(endpointAuthorityDigest("ws://x/a", "bc")); daemon.connectSession("S", "ws://x/", "abc"); const session = daemon.sessions.get("S")!; const socket = session.ws as unknown as FakeWs; socket.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "hello", capabilities: ["ephemeral_turn_v1"] }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 4, lastSeq: 0, events: [], }); await daemon.handleTelegramUpdate({ update_id: 906, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw reconnect?", message_id: 1906 }, }); expect(socket.sent.map(frame => JSON.parse(frame)).some(frame => frame.type === "ephemeral_turn")).toBe(false); }); test("does not resume a /btw request onto a same-authority replacement socket", async () => { const { bot, daemon, threadId } = await daemonWithTopic(); await daemon.handleTelegramUpdate({ update_id: 989, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw reconnect", message_id: 1989 }, }); const initial = daemon.sessions.get("S")!; const request = JSON.parse((initial.ws as unknown as FakeWs).sent.at(-1)!) as { requestId: string; sessionId: string; updateId: number; messageId: number; threadId: string; }; (initial.ws as unknown as FakeWs).dispatchEvent(new Event("close")); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ), ).toHaveLength(0); daemon.connectSession("S", "ws://s", "ts"); const replacement = daemon.sessions.get("S")!; await enableEphemeralTurns(daemon); const replayedRequest = (replacement.ws as unknown as FakeWs).sent .map(frame => JSON.parse(frame) as Record) .find(frame => frame.type === "ephemeral_turn"); expect(replayedRequest).toBeUndefined(); await daemon.handleSessionMessage(replacement, { ...request, type: "ephemeral_turn_result", status: "ok", text: "reconnected answer", }); await daemon.handleSessionMessage(replacement, { ...request, type: "ephemeral_turn_result", status: "ok", text: "reconnected answer", }); expect( bot.calls.filter( call => (call.method === "sendMessage" && call.body.text === "reconnected answer") || (call.method === "sendRichMessage" && call.body.rich_message.markdown === "reconnected answer"), ), ).toHaveLength(0); }); test.each([ "session_closed", "liveness_timeout", "authority_replaced", ] as const)("terminalizes a pending /btw exactly once when %s loses its transport session", async loss => { const { bot, daemon, threadId } = await daemonWithTopic(); await daemon.handleTelegramUpdate({ update_id: 990, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw lost?", message_id: 1990 }, }); const session = daemon.sessions.get("S")!; const request = JSON.parse((session.ws as unknown as FakeWs).sent.at(-1)!) as { requestId: string; sessionId: string; updateId: number; messageId: number; threadId: string; }; if (loss === "session_closed") { await daemon.handleSessionMessage(session, { type: "session_closed", sessionId: "S" }); } else if (loss === "liveness_timeout") { (daemon as unknown as { dropSession(session: unknown, reason: string): void }).dropSession( session, "liveness_timeout", ); } else if (loss === "authority_replaced") { daemon.connectSession("S", "ws://replacement", "replacement-token"); } await Promise.resolve(); const unavailable = bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ); expect(unavailable).toHaveLength(1); expect(unavailable[0]).toMatchObject({ body: { chat_id: "42", message_thread_id: threadId, reply_parameters: { message_id: 1990 }, }, }); const deliveriesAfterLoss = bot.calls.filter( call => call.method === "sendMessage" || call.method === "sendRichMessage", ).length; await daemon.handleSessionMessage(session, { ...request, type: "ephemeral_turn_result", status: "ok", text: "late answer", }); expect(bot.calls.filter(call => call.method === "sendMessage" || call.method === "sendRichMessage")).toHaveLength( deliveriesAfterLoss, ); }); test("terminalizes the prior logical session before a threaded identity rekey", async () => { const { bot, daemon, threadId } = await daemonWithTopic(); await daemon.handleTelegramUpdate({ update_id: 991, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw old identity?", message_id: 1991 }, }); const session = daemon.sessions.get("S")!; const request = JSON.parse((session.ws as unknown as FakeWs).sent.at(-1)!) as { requestId: string; sessionId: string; updateId: number; messageId: number; threadId: string; }; await daemon.handleSessionMessage(session, { type: "config_update", sessionId: "replacement-logical" }); await Promise.resolve(); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ), ).toHaveLength(1); const deliveriesAfterRekey = bot.calls.filter( call => call.method === "sendMessage" || call.method === "sendRichMessage", ).length; await daemon.handleSessionMessage(session, { ...request, type: "ephemeral_turn_result", status: "ok", text: "late old-identity answer", }); expect(bot.calls.filter(call => call.method === "sendMessage" || call.method === "sendRichMessage")).toHaveLength( deliveriesAfterRekey, ); }); test("joins a rejected result and sends session loss before deleting the topic", async () => { const { bot, daemon, threadId } = await daemonWithTopic(); await daemon.handleTelegramUpdate({ update_id: 992, message: { chat: { id: 42 }, message_thread_id: threadId, text: "/btw delivery race?", message_id: 1992 }, }); const session = daemon.sessions.get("S")!; const request = JSON.parse((session.ws as unknown as FakeWs).sent.at(-1)!) as { requestId: string; sessionId: string; updateId: number; messageId: number; threadId: string; }; const answerStarted = Promise.withResolvers(); const answerResponse = Promise.withResolvers(); const normalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if ( method === "sendRichMessage" || (method === "sendMessage" && (body as { text?: unknown }).text === "delivery result") ) { bot.calls.push({ method, body, options }); answerStarted.resolve(); return await answerResponse.promise; } return await normalCall(method, body, options); }; const handling = daemon.handleSessionMessage(session, { ...request, type: "ephemeral_turn_result", status: "ok", text: "delivery result", }); await Promise.race([ answerStarted.promise, Bun.sleep(1_000).then(() => { throw new Error( `Expected answer delivery to start; request=${JSON.stringify(request)} calls=${JSON.stringify(bot.calls.slice(-3))}`, ); }), ]); const closing = daemon.handleSessionMessage(session, { type: "session_closed", sessionId: "S" }); await Bun.sleep(0); expect(bot.calls.some(call => call.method === "deleteForumTopic")).toBe(false); answerResponse.resolve({ ok: false, description: "delivery unavailable" }); await Promise.all([handling, closing]); expect( bot.calls.filter( call => call.method === "sendRichMessage" || (call.method === "sendMessage" && call.body.text === "delivery result"), ), ).toHaveLength(1); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ), ).toHaveLength(1); const unavailableIndex = bot.calls.findIndex( call => call.method === "sendMessage" && call.body.text === "This /btw question stopped because the SKC session closed or changed. Reopen it and try again.", ); const deleteIndex = bot.calls.findIndex(call => call.method === "deleteForumTopic"); expect(unavailableIndex).toBeGreaterThanOrEqual(0); expect(deleteIndex).toBeGreaterThan(unavailableIndex); }); test("retires a granted pool settlement so an item id can be safely reused", async () => { const pool = new RateLimitPool({ capacity: 1, refillPerSec: 0, now: () => 0 }); const first = pool.submit({ sessionId: "S", lane: "ask", itemId: "receipt", payload: "first" }); expect(pool.drain()).toEqual([{ sessionId: "S", lane: "ask", itemId: "receipt", payload: "first" }]); pool.settle(first.itemId, "accepted"); await expect(first.settled).resolves.toBe("accepted"); const second = pool.submit({ sessionId: "S", lane: "ask", itemId: "receipt", payload: "second" }); pool.settle(second.itemId, "removed"); await expect(second.settled).resolves.toBe("removed"); }); test.each([ ["accepted", async () => ({ ok: true, result: true }), false], ["rejected", async () => ({ ok: false, description: "TOPIC_ID_INVALID" }), false], ["ambiguous", async () => Promise.reject(new Error("network lost")), true], ])("close during an accepted create performs a compensating delete (%s)", async (_outcome, deleteResult, retained) => { const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const createGate = Promise.withResolvers(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); createStarted.resolve(); return createGate.promise; } if (method === "deleteForumTopic") { bot.calls.push({ method, body, options }); return deleteResult(); } return call(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, }); const creating = (daemon as any).ensureTopic("S", "topic"); await createStarted.promise; const closing = (daemon as any).deleteTopic("S"); createGate.resolve({ ok: true, result: { message_thread_id: 77 } }); await expect(creating).rejects.toThrow("topic authority was revoked during creation"); await closing; expect( bot.calls.filter(call => call.method === "deleteForumTopic").map(call => call.body.message_thread_id), ).toEqual([77]); const persisted = JSON.parse( fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8"), ); if (retained) expect(persisted.topics.S).toMatchObject({ topicId: "77", authorityState: "delete_pending" }); else expect(persisted.topics.S).toBeUndefined(); }); test("an accepted stale create persists its fence across restart before remote compensation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const deleteStarted = Promise.withResolvers(); const releaseDelete = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); createStarted.resolve(); return releaseCreate.promise; } if (method === "deleteForumTopic") { bot.calls.push({ method, body, options }); deleteStarted.resolve(); return releaseDelete.promise; } return originalCall(method, body, options); }; const daemon = recoveryDaemon(agentDir, bot); const predecessorReplay = replayResumedIdentity(daemon, "CANONICAL", "CANONICAL", { url: "ws://predecessor", token: "old", }); await createStarted.promise; daemon.connectSession("CANONICAL", "ws://successor", "new"); releaseCreate.resolve({ ok: true, result: { message_thread_id: 78 } }); await deleteStarted.promise; const fencedBeforeCompensation = await readTopicAuthorityState(agentDir); expect(fencedBeforeCompensation.topics.CANONICAL).toMatchObject({ topicId: "78", authorityState: "delete_pending", }); expect((fencedBeforeCompensation as { fences?: Record }).fences?.CANONICAL).toBeGreaterThan(0); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); bot.calls = []; await replayResumedIdentity(restarted, "RESTART", "CANONICAL", { url: "ws://successor", token: "new" }); expect(restarted.sessions.has("RESTART")).toBe(false); expect( bot.calls.filter(call => call.method === "createForumTopic" || call.method === "sendMessage"), ).toHaveLength(0); expect((await readTopicAuthorityState(agentDir)).topics.CANONICAL).toMatchObject({ authorityState: "delete_pending", }); releaseDelete.resolve({ ok: true, result: true }); await predecessorReplay; }); test("fresh-endpoint rotation compensates a held predecessor create and only successor delivery routes", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const deleteStarted = Promise.withResolvers(); const originalCall = bot.call.bind(bot); let heldCreate = true; bot.call = async (method, body, options) => { if (method === "createForumTopic" && heldCreate) { bot.calls.push({ method, body, options }); createStarted.resolve(); return releaseCreate.promise; } if (method === "deleteForumTopic") deleteStarted.resolve(); return originalCall(method, body, options); }; const daemon = recoveryDaemon(agentDir, bot); const predecessorReplay = replayResumedIdentity(daemon, "CANONICAL", "CANONICAL", { url: "ws://predecessor", token: "old", }); await createStarted.promise; void predecessorReplay.catch(() => undefined); // Connecting a fresh endpoint revokes the predecessor's immutable creation // lease before Telegram accepts its held create. daemon.connectSession("CANONICAL", "ws://successor", "new"); const successor = daemon.sessions.get("CANONICAL")!; successor.ws.dispatchEvent(new Event("open")); releaseCreate.resolve({ ok: true, result: { message_thread_id: 77 } }); await predecessorReplay; await deleteStarted.promise; for (let i = 0; i < 20 && (daemon as any).topics.get("CANONICAL") !== undefined; i++) await new Promise(resolve => setTimeout(resolve, 1)); expect( bot.calls.filter(call => call.method === "deleteForumTopic").map(call => call.body.message_thread_id), ).toEqual([77]); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.message_thread_id === 77)).toEqual([]); expect((daemon as any).topics.get("CANONICAL")).toBeUndefined(); heldCreate = false; await daemon.handleSessionMessage(successor, { type: "event_replay_result", ok: true, id: successor.replayId, generation: 1, lastSeq: 1, events: [{ payload: { type: "identity_header", sessionId: "CANONICAL", repo: "sayknow-cli", branch: "dev" } }], }); await daemon.handleSessionMessage(successor, { type: "turn_stream", sessionId: "CANONICAL", text: "successor output", }); const routed = bot.calls.filter( call => call.method === "sendMessage" && call.body.message_thread_id !== undefined, ); expect(routed).toHaveLength(2); expect(routed.every(call => call.body.message_thread_id !== 77)).toBe(true); }); test.each([ ["accepted remote delete", async () => ({ ok: true, result: true }), false], ["already absent remote topic", async () => ({ ok: false, description: "message thread not found" }), false], ["ambiguous remote delete", async () => ({ ok: false, description: "transport unavailable" }), true], ] as const)("startup scan reconciles a crash-persisted delete fence after %s", async (_outcome, deleteResult, retained) => { const agentDir = tempAgentDir(); const topicsPath = path.join(daemonPaths(agentDir).dir, "telegram-topics.json"); fs.mkdirSync(path.dirname(topicsPath), { recursive: true }); fs.writeFileSync( topicsPath, JSON.stringify({ topics: { S: { topicId: "77", identitySent: true, createdAt: 1, chatId: "42", endpointKey: "ws://s", endpointDigest: endpointAuthorityDigest("ws://s", "token"), endpointGeneration: 1, authorityState: "delete_pending", authorityEpoch: 2, }, }, fences: { S: 2 }, }), ); const bot = new FakeBotApi(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method !== "deleteForumTopic") return call(method, body, options); bot.calls.push({ method, body, options }); return deleteResult(); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "restarted-owner", botToken: "tok", chatId: "42", botApi: bot, }); await daemon.loadTopics(); await daemon.scanRoots(); expect( bot.calls.filter(call => call.method === "deleteForumTopic").map(call => call.body.message_thread_id), ).toEqual([77]); const persisted = JSON.parse(fs.readFileSync(topicsPath, "utf8")); if (retained) expect(persisted.topics.S).toMatchObject({ topicId: "77", authorityState: "delete_pending" }); else expect(persisted.topics.S).toBeUndefined(); }); test("failed close publication restores only close authority while retaining a concurrent user rename across restart", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const renameWriteStarted = Promise.withResolvers(); const releaseRenameWrite = Promise.withResolvers(); let holdRenameWrite = false; let failCloseWrite = false; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, fs: topicStateFs(async () => { if (holdRenameWrite) { holdRenameWrite = false; renameWriteStarted.resolve(); await releaseRenameWrite.promise; return; } if (!failCloseWrite) return; failCloseWrite = false; throw new Error("first close publication fails"); }), }); daemon.connectSession("S", "ws://s", "ts"); const closingSession = daemon.sessions.get("S")!; await daemon.handleSessionMessage(closingSession, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); await (daemon as any).topicsPersistQueue; const threadId = bot.createdTopicThreadIds[0]!; holdRenameWrite = true; expect((daemon as any).topics.markUserName("S", "Still mine", 2990)).toBe("updated"); const rename = (daemon as any).persistTopics(); await Promise.race([ renameWriteStarted.promise, Bun.sleep(1_000).then(() => Promise.reject(new Error("rename publication did not begin"))), ]); failCloseWrite = true; const closing = daemon.handleSessionMessage(closingSession, { type: "session_closed", sessionId: "S" }); const closeFailure = closing.then( () => undefined, error => error, ); releaseRenameWrite.resolve(); await rename; expect(await closeFailure).toMatchObject({ message: "first close publication fails" }); const persisted = await readTopicAuthorityState(agentDir); expect(persisted.topics.S).toMatchObject({ topicId: String(threadId), name: "Still mine", nameOwner: "user" }); expect(persisted.topics.S.authorityState).not.toBe("delete_pending"); expect(persisted.closedEndpoints?.S).toBeUndefined(); const restartedBot = new FakeBotApi(); const restarted = recoveryDaemon(agentDir, restartedBot); await restarted.loadTopics(); restarted.connectSession("S", "ws://s", "ts"); const restartedSession = restarted.sessions.get("S")!; await restarted.handleSessionMessage(restartedSession, { type: "turn_stream", sessionId: "S", text: "still routes", }); expect( restartedBot.calls.filter(call => call.method === "sendMessage" && call.body.text === "still routes"), ).toHaveLength(1); }); test("a superseded recovered close retries a failed tombstone clear before returning", async () => { FakeWs.instances = []; const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let holdCloseWrite = false; let failClearWrite = false; let trackCloseWrites = false; let closeWrites = 0; const agentDir = tempAgentDir(); const daemon = recoveryDaemon( agentDir, new FakeBotApi(), "42", topicStateFs(async () => { if (!trackCloseWrites) return; if (holdCloseWrite) { holdCloseWrite = false; writeStarted.resolve(); await releaseWrite.promise; } closeWrites++; if (failClearWrite && closeWrites === 3) { failClearWrite = false; throw new Error("fail once while clearing close tombstone"); } }), ); await replayResumedIdentity(daemon, "PREDECESSOR", "S", { url: "ws://old", token: "old-token" }); const oldSession = daemon.sessions.get("PREDECESSOR")!; trackCloseWrites = true; holdCloseWrite = true; const closing = daemon.handleSessionMessage(oldSession, { type: "session_closed", sessionId: "S" }); await writeStarted.promise; daemon.connectSession("SUCCESSOR", "ws://successor", "new-token"); failClearWrite = true; releaseWrite.resolve(); await closing; await replayResumedIdentity(daemon, "SUCCESSOR", "S", { url: "ws://successor", token: "new-token", generation: 2, }); const persisted = JSON.parse( fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8"), ); expect(persisted.closedEndpoints?.S).toBeUndefined(); expect((daemon as any).closedEndpointKeys.has("S")).toBe(false); }); test("a replaced close restores its published fence without a second epoch and survives restart", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let armed = false; let writes = 0; const bot = new FakeBotApi(); const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!armed || ++writes !== 2) return; writeStarted.resolve(); await releaseWrite.promise; }), ); await replayResumedIdentity(daemon, "PREDECESSOR", "S", { url: "ws://old", token: "old-token" }); const predecessor = daemon.sessions.get("PREDECESSOR")!; const topicId = bot.createdTopicThreadIds.at(-1)!; armed = true; const closing = daemon.handleSessionMessage(predecessor, { type: "session_closed", sessionId: "S" }); await writeStarted.promise; daemon.connectSession("PREDECESSOR", "ws://replacement", "replacement-token"); releaseWrite.resolve(); await closing; const persisted = await readTopicAuthorityState(agentDir); expect(persisted.topics.S).toMatchObject({ topicId: String(topicId) }); expect(persisted.topics.S.authorityState).not.toBe("delete_pending"); expect(persisted.closedEndpoints?.PREDECESSOR).toBeUndefined(); await replayResumedIdentity(daemon, "PREDECESSOR", "S", { url: "ws://replacement", token: "replacement-token", generation: 2, }); bot.calls.length = 0; await daemon.handleSessionMessage(daemon.sessions.get("PREDECESSOR")!, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "still active", }); expect(bot.calls.filter(call => call.method === "sendMessage" && call.body.text === "still active")).toHaveLength( 1, ); const restartedBot = new FakeBotApi(); const restarted = recoveryDaemon(agentDir, restartedBot); await restarted.loadTopics(); await replayResumedIdentity(restarted, "RECOVERED", "S", { url: "ws://recovered", token: "recovered-token" }); await restarted.handleSessionMessage(restarted.sessions.get("RECOVERED")!, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "routes after restart", }); expect( restartedBot.calls.filter(call => call.method === "sendMessage" && call.body.text === "routes after restart"), ).toHaveLength(1); }); test("supersession rejects queued HTML continuations from the predecessor lease", async () => { FakeWs.instances = []; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, rich: { enabled: true }, }); daemon.connectSession("S", "ws://predecessor", "old-token"); const predecessor = daemon.sessions.get("S")!; await daemon.handleSessionMessage(predecessor, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); bot.calls.length = 0; const markdown = "x".repeat(9_000); const chunks = splitTelegramHtml(markdownToTelegramHtml(markdown)); expect(chunks.length).toBeGreaterThan(1); await daemon.handleSessionMessage(predecessor, { type: "turn_stream", sessionId: "S", phase: "finalized", finalAnswer: true, text: markdown, }); expect(bot.calls.filter(call => call.method === "sendMessage").map(call => call.body.text)).toEqual([chunks[0]]); daemon.connectSession("S", "ws://successor", "new-token"); const successor = daemon.sessions.get("S")!; await daemon.handleSessionMessage(successor, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); bot.calls.length = 0; await daemon.handleSessionMessage(successor, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "successor tail", }); const sent = bot.calls.filter(call => call.method === "sendMessage").map(call => call.body.text); expect(sent).toEqual([markdownToTelegramHtml("successor tail")]); expect(sent.some(text => chunks.slice(1).includes(text))).toBe(false); }); test("supersession rejects queued flat-fallback continuations from the predecessor lease", async () => { FakeWs.instances = []; const bot = new FakeBotApi(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); return { ok: false, description: "forum topics are disabled" }; } return originalCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, rich: { enabled: false }, }); daemon.connectSession("S", "ws://predecessor", "old-token"); const predecessor = daemon.sessions.get("S")!; await daemon.handleSessionMessage(predecessor, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); bot.calls.length = 0; const markdown = "y".repeat(9_000); const chunks = splitTelegramHtml(markdownToTelegramHtml(markdown)); await daemon.handleSessionMessage(predecessor, { type: "turn_stream", sessionId: "S", phase: "finalized", text: markdown, }); expect(bot.calls.filter(call => call.method === "sendMessage").map(call => call.body.text)).toEqual([chunks[0]]); daemon.connectSession("S", "ws://successor", "new-token"); const successor = daemon.sessions.get("S")!; await daemon.handleSessionMessage(successor, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); bot.calls.length = 0; await daemon.handleSessionMessage(successor, { type: "turn_stream", sessionId: "S", phase: "finalized", text: "successor flat tail", }); const sent = bot.calls.filter(call => call.method === "sendMessage").map(call => call.body.text); expect(sent).toEqual([markdownToTelegramHtml("successor flat tail")]); expect(sent.some(text => chunks.slice(1).includes(text))).toBe(false); }); test("successor commit during inbound attachment preparation sends nothing through the predecessor", async () => { FakeWs.instances = []; const downloadStarted = Promise.withResolvers(); const releaseDownload = Promise.withResolvers(); const bot = new FakeBotApi(); const fetchImpl = (async () => { downloadStarted.resolve(); await releaseDownload.promise; return { ok: true, arrayBuffer: async () => new Uint8Array([1, 2, 3]).buffer }; }) as unknown as typeof fetch; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, fetchImpl, WebSocketImpl: FakeWs as any, }); daemon.connectSession("S", "ws://predecessor", "old-token"); const predecessor = daemon.sessions.get("S")!; const predecessorSocket = predecessor.ws as unknown as FakeWs; await daemon.handleSessionMessage(predecessor, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); const threadId = bot.calls.find(call => call.method === "sendMessage")!.body.message_thread_id; predecessorSocket.sent.length = 0; bot.calls.length = 0; const inbound = daemon.handleTelegramUpdate({ update_id: 2991, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 1991, photo: [{ file_id: "held-photo" }], }, }); await downloadStarted.promise; daemon.connectSession("S", "ws://successor", "new-token"); releaseDownload.resolve(); await inbound; expect(predecessorSocket.sent).toHaveLength(0); expect(bot.calls.some(call => call.method === "setMessageReaction" || call.method === "sendMessage")).toBe(false); expect((daemon as any).inboundReactions.has(2991)).toBe(false); }); test.each([ [2995, 1995, "/btw", "guidance"], [2996, 1996, "/context", "control"], ] as const)("held %s reservation releases a delete-pending topic without durable consumption", async (updateId, messageId, text) => { const { agentDir, bot, daemon, threadId } = await daemonWithTopic(); const reservationStarted = Promise.withResolvers(); const releaseReservation = Promise.withResolvers(); const internals = daemon as unknown as { reserveSeenUpdateId(updateId: number): Promise; topics: { beginDelete(sessionId: string): unknown }; }; const reserve = internals.reserveSeenUpdateId.bind(daemon); internals.reserveSeenUpdateId = async id => { reservationStarted.resolve(); await releaseReservation.promise; return reserve(id); }; const socket = daemon.sessions.get("S")!.ws as unknown as FakeWs; const sentBefore = socket.sent.length; bot.calls.length = 0; const handling = daemon.handleTelegramUpdate({ update_id: updateId, message: { chat: { id: 42 }, message_thread_id: threadId, text, message_id: messageId }, }); await reservationStarted.promise; internals.topics.beginDelete("S"); releaseReservation.resolve(); await handling; expect(socket.sent.slice(sentBefore)).toHaveLength(0); expect(bot.calls).toEqual([]); expect((daemon as any).dispatchState.seenUpdateIds.has(updateId)).toBe(false); const persisted = JSON.parse(fs.readFileSync(daemonPaths(agentDir).seenUpdates, "utf8")) as { updateIds: number[]; }; expect(persisted.updateIds).not.toContain(updateId); }); test("held inbound attachment work stops at a delete-pending topic without effects or update consumption", async () => { FakeWs.instances = []; const downloadStarted = Promise.withResolvers(); const releaseDownload = Promise.withResolvers(); const fetchImpl = (async () => { downloadStarted.resolve(); await releaseDownload.promise; return { ok: true, arrayBuffer: async () => new Uint8Array([1, 2, 3]).buffer }; }) as unknown as typeof fetch; const { agentDir, bot, daemon, threadId } = await daemonWithTopic({ fetchImpl }); const socket = daemon.sessions.get("S")!.ws as unknown as FakeWs; const sentBefore = socket.sent.length; bot.calls.length = 0; const updateId = 2994; const handling = daemon.handleTelegramUpdate({ update_id: updateId, message: { chat: { id: 42 }, message_thread_id: threadId, message_id: 1994, photo: [{ file_id: "held-photo" }], }, }); await downloadStarted.promise; (daemon as any).topics.beginDelete("S"); releaseDownload.resolve(); await handling; expect(socket.sent.slice(sentBefore)).toHaveLength(0); expect(bot.calls.some(call => call.method === "sendMessage" || call.method === "setMessageReaction")).toBe(false); expect((daemon as any).dispatchState.seenUpdateIds.has(updateId)).toBe(false); expect(fs.existsSync(daemonPaths(agentDir).seenUpdates)).toBe(false); }); test("restart before A-to-B replay keeps persisted initiator topic input fail-closed", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "A", "A", { url: "ws://a", token: "a" }); await replayResumedIdentity(initial, "B", "B", { url: "ws://b", token: "b" }); const aTopic = bot.createdTopicThreadIds[0]!; const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); restarted.connectSession("A", "ws://a-rekey", "rekey"); const pending = restarted.sessions.get("A")!; const socket = pending.ws as unknown as FakeWs; socket.sent.length = 0; bot.calls.length = 0; await restarted.handleTelegramUpdate({ update_id: 280_201, message: { chat: { id: 42 }, message_thread_id: aTopic, message_id: 1, text: "cannot claim before replay" }, }); expect(socket.sent).toEqual([]); expect(bot.calls.filter(call => call.method === "setMessageReaction")).toHaveLength(0); await replayResumedIdentity(restarted, "A", "B", { url: "ws://a-rekey", token: "rekey" }); const rekeyed = restarted.sessions.get("A")!.ws as unknown as FakeWs; rekeyed.sent.length = 0; await restarted.handleTelegramUpdate({ update_id: 280_202, message: { chat: { id: 42 }, message_thread_id: aTopic, message_id: 2, text: "still cannot hijack" }, }); expect(rekeyed.sent).toEqual([]); }); test("daemon quarantines a persisted unbound legacy topic for inbound and outbound threaded traffic", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const topicsPath = path.join(daemonPaths(agentDir).dir, "telegram-topics.json"); fs.mkdirSync(path.dirname(topicsPath), { recursive: true }); fs.writeFileSync( topicsPath, JSON.stringify({ topics: { LEGACY: { topicId: "777", identitySent: true, createdAt: 1 } }, }), ); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); await daemon.loadTopics(); daemon.connectSession("LEGACY", "ws://legacy", "token"); const session = daemon.sessions.get("LEGACY")!; const socket = session.ws as unknown as FakeWs; socket.sent.length = 0; await daemon.handleTelegramUpdate({ update_id: 280_203, message: { chat: { id: 42 }, message_thread_id: 777, message_id: 3, text: "legacy inbound" }, }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "LEGACY", text: "legacy outbound" }); expect(socket.sent).toEqual([]); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.message_thread_id !== undefined), ).toHaveLength(0); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); }); test("shutdown retains ownership and returns when control persistence never settles", async () => { const agentDir = tempAgentDir(); const s = settings(agentDir); await acquireDaemonOwnership({ settings: s, tokenFingerprint: tokenFingerprint("tok"), chatId: "42", pid: process.pid, randomId: () => "owner", }); const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: new FakeBotApi(), idleTimeoutMs: 0, control: { shouldStop: async () => true, clear: async () => new Promise(() => undefined), }, }); await expect( Promise.race([daemon.run(), Bun.sleep(2_000).then(() => Promise.reject(new Error("shutdown timed out")))]), ).resolves.toBeUndefined(); expect(fs.existsSync(daemonPaths(agentDir).lock)).toBe(true); }); test("identity-less A-to-B replay remains denied without an exact A durable owner", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "A", "A", { url: "ws://rekey", token: "token" }); const aTopic = bot.createdTopicThreadIds[0]!; await replayResumedIdentity(first, "A", "B", { url: "ws://rekey", token: "token" }); const bTopic = bot.createdTopicThreadIds[1]!; expect(bTopic).not.toBe(aTopic); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); expect(await readTopicAuthorityState(agentDir)).toMatchObject({ topics: { B: { endpointDigest: endpointAuthorityDigest("ws://rekey", "token") } }, }); restarted.connectSession("A", "ws://rekey", "token"); const resumed = restarted.sessions.get("A")!; resumed.ws.dispatchEvent(new Event("open")); await restarted.handleSessionMessage(resumed, { type: "event_replay_result", ok: true, id: resumed.replayId, generation: 1, lastSeq: 2, events: [], }); expect(resumed.replayPending).toBe(true); expect(resumed.logicalSessionId).toBe("A"); bot.calls.length = 0; await restarted.handleSessionMessage(resumed, { type: "turn_stream", sessionId: "B", text: "identity-less resume", }); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "identity-less resume")).toBe( false, ); }); test("identity-less replay denies a matching logical session with a mismatched endpoint binding", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "S", "S", { url: "ws://durable", token: "durable-token" }); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); restarted.connectSession("S", "ws://different", "different-token"); const resumed = restarted.sessions.get("S")!; resumed.ws.dispatchEvent(new Event("open")); await restarted.handleSessionMessage(resumed, { type: "event_replay_result", ok: true, id: resumed.replayId, generation: 1, lastSeq: 2, events: [], }); expect(resumed.replayPending).toBe(true); expect(resumed.logicalSessionId).toBe("S"); bot.calls.length = 0; await restarted.handleSessionMessage(resumed, { type: "turn_stream", sessionId: "S", text: "endpoint-mismatched identity-less replay", }); expect( bot.calls.some( call => call.method === "sendMessage" && call.body.text === "endpoint-mismatched identity-less replay", ), ).toBe(false); }); test("identity-less replay buffers frames until a lazy identity create flushes them", async () => { FakeWs.instances = []; const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); class HeldCreateBotApi extends FakeBotApi { override async call( method: string, body: unknown, options?: { noRetry?: boolean; signal?: AbortSignal }, ): Promise { if (method === "createForumTopic") { this.calls.push({ method, body, options }); createStarted.resolve(); return await releaseCreate.promise; } return super.call(method, body, options); } } const bot = new HeldCreateBotApi(); const daemon = recoveryDaemon(tempAgentDir(), bot); daemon.connectSession("FRESH", "ws://fresh", "fresh-token"); const session = daemon.sessions.get("FRESH")!; session.ws.dispatchEvent(new Event("open")); const replay = daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); await Bun.sleep(1); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "FRESH", text: "fresh bootstrap drains after create", }); expect( bot.calls.some( call => call.method === "sendMessage" && call.body.text === "fresh bootstrap drains after create", ), ).toBe(false); const identity = daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "FRESH", repo: "repo", branch: "branch", }); await createStarted.promise; releaseCreate.resolve({ ok: true, result: { message_thread_id: 101 } }); await replay; await identity; const internals = daemon as unknown as { flushPool(): Promise; pendingThreadedFrames: Map; }; for (let attempt = 0; attempt < 20 && internals.pendingThreadedFrames.has("FRESH"); attempt++) await Bun.sleep(1); await internals.flushPool(); expect(session.replayPending).toBe(false); expect(session.logicalSessionId).toBe("FRESH"); expect(session.recoveryLease?.state).toBe("authorized"); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(1); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.message_thread_id === 101 && call.body.text === "fresh bootstrap drains after create", ), ).toHaveLength(1); expect((daemon as any).topics.get("FRESH")).toMatchObject({ topicId: "101", chatId: "42", endpointKey: session.endpointKey, endpointDigest: session.endpointDigest, endpointGeneration: 1, }); }); test.each([ "createForumTopic", "initial topic-state persistence", ])("valid replay recovers and delivers once after one-shot %s failure", async failure => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const call = bot.call.bind(bot); let failCreate = failure === "createForumTopic"; bot.call = async (method, body, options) => { if (method === "createForumTopic" && failCreate) { failCreate = false; bot.calls.push({ method, body, options }); return { ok: false, description: "one-shot create failure" }; } return call(method, body, options); }; let failInitialTopicWrite = failure === "initial topic-state persistence"; const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!failInitialTopicWrite) return; failInitialTopicWrite = false; throw new Error("one-shot initial topic-state persistence failure"); }), ); await replayResumedIdentity(daemon, "FAILED", "S", { url: "ws://failed", token: "failed-token" }); expect(daemon.sessions.has("FAILED")).toBe(false); await replayResumedIdentity(daemon, "RECOVERED", "S", { url: "ws://recovered", token: "recovered-token", generation: 2, }); await daemon.handleSessionMessage(daemon.sessions.get("RECOVERED")!, { type: "turn_stream", sessionId: "S", phase: "finalized", text: `recovered after ${failure}`, }); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.text === `recovered after ${failure}`), ).toHaveLength(1); }); test("identity-less replay denies ambiguous endpoint authority", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("C", "ws://shared", "token"); const session = daemon.sessions.get("C")!; const binding = { chatId: "42", endpointKey: session.endpointKey, endpointDigest: session.endpointDigest }; await (daemon as any).topics.getOrCreateTopic("A", async () => "101", Date.now, undefined, binding); await (daemon as any).topics.getOrCreateTopic("B", async () => "102", Date.now, undefined, binding); expect((daemon as any).topics.endpointAuthority(binding)).toEqual({ state: "ambiguous" }); session.ws.dispatchEvent(new Event("open")); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "C", text: "ambiguous authority" }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "ambiguous authority")).toBe( false, ); }); test("identity-less replay denies malformed partial endpoint authority", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("C", "ws://partial", "token"); const session = daemon.sessions.get("C")!; const binding = { chatId: "42", endpointKey: session.endpointKey, endpointDigest: session.endpointDigest }; (daemon as any).topics.replace({ topics: { A: { topicId: "101", identitySent: false, createdAt: 1, chatId: binding.chatId, endpointKey: binding.endpointKey, bindingMalformed: true, }, }, }); expect((daemon as any).topics.endpointAuthority(binding)).toEqual({ state: "ambiguous" }); session.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "C", text: "partial authority" }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "partial authority")).toBe( false, ); }); test("identity-less replay rechecks held staged endpoint authority inside its recovery claim", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "A", "A", { url: "ws://old", token: "old" }); const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let holdWrite = true; const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!holdWrite) return; holdWrite = false; writeStarted.resolve(); await releaseWrite.promise; }), ); await daemon.loadTopics(); daemon.connectSession("A", "ws://old", "old"); const incumbent = daemon.sessions.get("A")!; incumbent.ws.dispatchEvent(new Event("open")); const incumbentReplay = daemon.handleSessionMessage(incumbent, { type: "event_replay_result", ok: true, id: incumbent.replayId, generation: 2, lastSeq: 0, events: [{ payload: { type: "identity_header", sessionId: "A" } }], }); await writeStarted.promise; daemon.connectSession("C", "ws://staged", "token"); const session = daemon.sessions.get("C")!; const binding = { chatId: "42", endpointKey: session.endpointKey, endpointDigest: session.endpointDigest }; session.ws.dispatchEvent(new Event("open")); const replay = daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); const publicationStarted = Promise.withResolvers(); const releasePublication = Promise.withResolvers(); const publication = (daemon as any).topics.getOrCreateTopic( "B", async () => "101", Date.now, undefined, binding, async () => { publicationStarted.resolve(); await releasePublication.promise; }, ); await publicationStarted.promise; releaseWrite.resolve(); await incumbentReplay; await replay; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "C", text: "staged authority" }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); releasePublication.resolve(); await publication; }); test("identity-less replay denies a closed endpoint tombstone", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("C", "ws://closed", "token"); const session = daemon.sessions.get("C")!; const binding = { chatId: "42", endpointKey: session.endpointKey, endpointDigest: session.endpointDigest }; (daemon as any).closedEndpointKeys.set("A", binding); expect((daemon as any).topics.endpointAuthority(binding)).toEqual({ state: "none" }); session.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "C", text: "closed authority" }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "closed authority")).toBe( false, ); }); test("identity-less replay denies an exact durable owner when its endpoint is tombstoned", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("A", "ws://tombstoned-owner", "token"); const session = daemon.sessions.get("A")!; const binding = { chatId: "42", endpointKey: session.endpointKey, endpointDigest: session.endpointDigest }; await (daemon as any).topics.getOrCreateTopic("A", async () => "101", Date.now, undefined, binding); (daemon as any).closedEndpointKeys.set("A", binding); session.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "A", text: "must remain queued" }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); expect((daemon as any).topics.endpointAuthority(binding)).toEqual({ state: "unique", sessionId: "A" }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "must remain queued")).toBe( false, ); }); test("identity-less replay denies a held live pre-staged endpoint claim", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("B", "ws://shared-prestage", "token"); const incumbent = daemon.sessions.get("B")!; const incumbentBinding = { chatId: "42", endpointKey: incumbent.endpointKey, endpointDigest: incumbent.endpointDigest, }; const creating = (daemon as any).topics.getOrCreateTopic( "B", async () => { createStarted.resolve(); return await releaseCreate.promise; }, Date.now, undefined, incumbentBinding, ); await createStarted.promise; daemon.connectSession("C", "ws://shared-prestage", "token"); const session = daemon.sessions.get("C")!; session.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "C", text: "must not drain" }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "must not drain")).toBe(false); releaseCreate.resolve("101"); await creating; }); test("identity-less replay resumes an exact durable owner without excluding its registry claim", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "A", "A", { url: "ws://exact-owner", token: "token" }); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); restarted.connectSession("A", "ws://exact-owner", "token"); const session = restarted.sessions.get("A")!; session.ws.dispatchEvent(new Event("open")); await restarted.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 2, lastSeq: 0, events: [], }); expect(session.replayPending).toBe(false); expect(session.logicalSessionId).toBe("A"); expect(session.recoveryLease?.state).toBe("authorized"); }); test("identity-less replacement cannot exclude a same-ID predecessor create claim", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("A", "ws://same-id-predecessor", "token"); const predecessor = daemon.sessions.get("A")!; const binding = { chatId: "42", endpointKey: predecessor.endpointKey, endpointDigest: predecessor.endpointDigest, }; const creating = (daemon as any).topics.getOrCreateTopic( "A", async () => { createStarted.resolve(); return await releaseCreate.promise; }, Date.now, undefined, binding, ); await createStarted.promise; daemon.connectSession("A", "ws://same-id-predecessor", "token"); const replacement = daemon.sessions.get("A")!; replacement.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(replacement, { type: "event_replay_result", ok: true, id: replacement.replayId, generation: 1, lastSeq: 0, events: [], }); expect(replacement.replayPending).toBe(true); expect(replacement.recoveryLease?.state).not.toBe("authorized"); releaseCreate.resolve("101"); await creating; }); test("identity-less replay drains after lazy identity creation", async () => { FakeWs.instances = []; const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); class HeldCreateBotApi extends FakeBotApi { override async call( method: string, body: unknown, options?: { noRetry?: boolean; signal?: AbortSignal }, ): Promise { if (method === "createForumTopic") { this.calls.push({ method, body, options }); createStarted.resolve(); return await releaseCreate.promise; } return super.call(method, body, options); } } const bot = new HeldCreateBotApi(); const daemon = recoveryDaemon(tempAgentDir(), bot); daemon.connectSession("A", "ws://own-lazy-create", "token"); const session = daemon.sessions.get("A")!; session.ws.dispatchEvent(new Event("open")); const replay = daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); await Bun.sleep(1); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "A", text: "drains after lazy creation", }); const internals = daemon as unknown as { flushPool(): Promise; pendingThreadedFrames: Map; }; for (let attempt = 0; attempt < 20 && !internals.pendingThreadedFrames.has("A"); attempt++) await Bun.sleep(1); expect(internals.pendingThreadedFrames.get("A")).toHaveLength(1); const identity = daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "A", repo: "repo", branch: "branch", }); await createStarted.promise; releaseCreate.resolve({ ok: true, result: { message_thread_id: 101 } }); await replay; await identity; for (let attempt = 0; attempt < 20 && internals.pendingThreadedFrames.has("A"); attempt++) await Bun.sleep(1); await internals.flushPool(); expect(session.replayPending).toBe(false); expect(session.recoveryLease?.state).toBe("authorized"); expect(session.replayQueue).toHaveLength(0); expect(internals.pendingThreadedFrames.get("A")).toBeUndefined(); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.message_thread_id === 101 && call.body.text === "drains after lazy creation", ), ).toHaveLength(1); }); test("ok:false replay results cannot bootstrap or rekey authority", async () => { FakeWs.instances = []; const bootstrapDaemon = recoveryDaemon(tempAgentDir(), new FakeBotApi()); bootstrapDaemon.connectSession("FRESH", "ws://false-bootstrap", "token"); const bootstrap = bootstrapDaemon.sessions.get("FRESH")!; bootstrap.ws.dispatchEvent(new Event("open")); await bootstrapDaemon.handleSessionMessage(bootstrap, { type: "event_replay_result", ok: false, id: bootstrap.replayId, generation: 1, lastSeq: 0, events: [], }); expect(bootstrapDaemon.sessions.has("FRESH")).toBe(false); const bot = new FakeBotApi(); const daemon = recoveryDaemon(tempAgentDir(), bot); await replayResumedIdentity(daemon, "A", "A", { url: "ws://false-rekey", token: "token" }); daemon.connectSession("A", "ws://false-rekey", "token"); const rekey = daemon.sessions.get("A")!; rekey.ws.dispatchEvent(new Event("open")); await daemon.handleSessionMessage(rekey, { type: "event_replay_result", ok: false, id: rekey.replayId, generation: 2, lastSeq: 0, events: [{ payload: { type: "identity_header", sessionId: "B" } }], }); expect(daemon.sessions.has("A")).toBe(false); expect((daemon as any).topics.get("B")).toBeUndefined(); }); test("a stale held recovery result cannot reject a newer replay generation", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "A", "A", { url: "ws://concurrent-generations", token: "token" }); const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let held = true; const restarted = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!held) return; held = false; writeStarted.resolve(); await releaseWrite.promise; }), ); await restarted.loadTopics(); restarted.connectSession("A", "ws://concurrent-generations", "token"); const session = restarted.sessions.get("A")!; session.ws.dispatchEvent(new Event("open")); await restarted.handleSessionMessage(session, { type: "turn_stream", sessionId: "A", text: "generation-two output reaches generation-three lease", }); const generationTwo = restarted.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 2, lastSeq: 0, events: [{ payload: { type: "identity_header", sessionId: "A" } }], }); await writeStarted.promise; const generationThree = restarted.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 3, lastSeq: 0, events: [{ payload: { type: "identity_header", sessionId: "A" } }], }); releaseWrite.resolve(); await Promise.all([generationTwo, generationThree]); expect(restarted.sessions.get("A")).toBe(session); expect(session.replayPending).toBe(false); expect(session.hostGeneration).toBe(3); expect(session.recoveryLease).toMatchObject({ state: "authorized", binding: { endpointGeneration: 3 } }); expect( bot.calls.filter( call => call.method === "sendMessage" && call.body.text === "generation-two output reaches generation-three lease", ), ).toHaveLength(1); }); test("recovery rejects a socket that closes while persistence is held", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const first = recoveryDaemon(agentDir, bot); await replayResumedIdentity(first, "S", "S", { url: "ws://held", token: "token" }); const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let holdWrite = true; const restarted = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!holdWrite) return; holdWrite = false; writeStarted.resolve(); await releaseWrite.promise; }), ); await restarted.loadTopics(); restarted.connectSession("S", "ws://held", "token"); const session = restarted.sessions.get("S")!; session.ws.dispatchEvent(new Event("open")); await restarted.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", text: "must not drain" }); const replay = restarted.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 2, lastSeq: 0, events: [{ payload: { type: "identity_header", sessionId: "S" } }], }); await writeStarted.promise; (session.ws as unknown as FakeWs).setReadyState(2); releaseWrite.resolve(); await replay; expect(session.replayPending).toBe(true); expect(session.recoveryLease?.state).toBe("rejected"); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "must not drain")).toBe(false); }); test.each([ ["CONNECTING", 0], ["CLOSING", 2], ])("identity-less replay does not bootstrap or drain queued effects for a %s socket", async (_state, readyState) => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("S", "ws://not-open", "token"); const session = daemon.sessions.get("S")!; const socket = session.ws as unknown as FakeWs; socket.dispatchEvent(new Event("open")); socket.setReadyState(readyState); bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", text: "must remain queued" }); await daemon.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 1, lastSeq: 0, events: [], }); expect(session.replayPending).toBe(true); expect(session.replayQueue).toHaveLength(1); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "must remain queued")).toBe( false, ); }); test("retires fully unbound legacy authority before creating a fresh bound topic for asks", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const topicsPath = path.join(daemonPaths(agentDir).dir, "telegram-topics.json"); fs.mkdirSync(path.dirname(topicsPath), { recursive: true }); fs.writeFileSync( topicsPath, JSON.stringify({ topics: { LEGACY: { topicId: "777", identitySent: true, createdAt: 1 } }, }), ); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); await daemon.loadTopics(); daemon.connectSession("LEGACY", "ws://legacy", "token"); const session = daemon.sessions.get("LEGACY")!; const socket = session.ws as unknown as FakeWs; socket.sent.length = 0; bot.calls.length = 0; await daemon.handleSessionMessage(session, { type: "action_needed", kind: "ask", id: "ask", question: "Must not fall back", options: ["No"], }); await daemon.handleSessionMessage(session, { type: "ask_selected_ack_request", mode: "live", requestId: "ack", commitKey: "commit", actionId: "ask", deadlineAt: Date.now() + 10_000, }); const rendered = bot.calls.filter(call => call.method === "sendMessage" || call.method === "sendRichMessage"); expect(rendered).toHaveLength(2); expect(rendered.every(call => call.body.message_thread_id !== 777)).toBe(true); expect(socket.sent.map(frame => JSON.parse(frame))).not.toContainEqual({ type: "ask_selected_ack_result", requestId: "ack", commitKey: "commit", outcome: { status: "failed", reason: "route_missing" }, }); const persisted = JSON.parse(fs.readFileSync(topicsPath, "utf8")); expect(persisted.topics.LEGACY).toMatchObject({ chatId: "42", endpointKey: endpointAuthorityDigest("ws://legacy", "token"), endpointDigest: endpointAuthorityDigest("ws://legacy", "token"), }); }); test("rejected recovery leaves the durable cursor unchanged and drops queued effects", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "S", "S", { generation: 1 }); const before = JSON.parse(fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8")); expect(before.topics.S).toMatchObject({ replayGeneration: 1, replaySeq: 1 }); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); restarted.connectSession("S", "ws://replacement", "replacement-token"); const session = restarted.sessions.get("S")!; session.ws.dispatchEvent(new Event("open")); bot.calls.length = 0; await restarted.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", text: "queued must not render", }); await restarted.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 2, lastSeq: 9, events: [ { payload: { type: "identity_header", sessionId: "S" } }, { payload: { type: "identity_header", sessionId: "OTHER" } }, ], }); expect(restarted.sessions.has("S")).toBe(false); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "queued must not render")).toBe( false, ); const after = JSON.parse(fs.readFileSync(path.join(daemonPaths(agentDir).dir, "telegram-topics.json"), "utf8")); expect(after.topics.S).toMatchObject({ replayGeneration: 1, replaySeq: 1 }); }); test.each([ [ "generation reset", { gap: { kind: "generation_reset", fromGeneration: 1, toGeneration: 2, resyncQueries: ["Q01"] }, events: [], }, ], [ "sequence gap", { gap: { kind: "sequence_gap", fromSeq: 2, toSeq: 3, resyncQueries: ["Q01"] }, events: [], }, ], ["malformed event entry", { events: [null] }], ] as const)("%s replay cannot authorize, mutate topics or cursors, or drain queued effects", async (_name, proof) => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "S", "S", { generation: 1 }); const topicsPath = path.join(daemonPaths(agentDir).dir, "telegram-topics.json"); const before = fs.readFileSync(topicsPath, "utf8"); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); restarted.connectSession("S", "ws://canonical", "canonical-token"); const session = restarted.sessions.get("S")!; session.ws.dispatchEvent(new Event("open")); bot.calls.length = 0; await restarted.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", text: "must remain queued", }); await restarted.handleSessionMessage(session, { type: "event_replay_result", ok: true, id: session.replayId, generation: 2, lastSeq: 9, ...proof, }); expect(restarted.sessions.has("S")).toBe(false); expect(session.replayPending).toBe(true); expect(session.logicalSessionIdTrusted).toBe(false); expect(session.hostGeneration).toBe(0); expect(session.recoveryLease).toBeUndefined(); expect(bot.calls.filter(call => call.method === "createForumTopic")).toHaveLength(0); expect(bot.calls.some(call => call.method === "sendMessage" && call.body.text === "must remain queued")).toBe( false, ); expect(fs.readFileSync(topicsPath, "utf8")).toBe(before); }); test("scanRoots preserves a pre-aged orphaned topic while its logical owner is live on another transport", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const s = setPrivateAgentDir(settings(agentDir), agentDir); const cwd = path.join(agentDir, "repo"); await registerNotificationRoot({ settings: s, cwd, sessionId: "B" }); const endpointDir = path.join(cwd, ".skc", "state", "sdk"); fs.mkdirSync(endpointDir, { recursive: true }); fs.writeFileSync( path.join(endpointDir, "B.json"), JSON.stringify({ url: "ws://dead-b", token: "dead", stale: true }), ); const bot = new FakeBotApi(); const initial = recoveryDaemon(agentDir, bot); await replayResumedIdentity(initial, "B", "B", { url: "ws://b", token: "b" }); const topicId = bot.createdTopicThreadIds[0]!; const topicsPath = path.join(daemonPaths(agentDir).dir, "telegram-topics.json"); const persisted = JSON.parse(fs.readFileSync(topicsPath, "utf8")); persisted.topics.B.orphanedAt = 0; fs.writeFileSync(topicsPath, JSON.stringify(persisted)); let now = 120_000; const daemon = new TelegramNotificationDaemon({ settings: s, ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, now: () => now, pidAlive: () => false, }); await daemon.loadTopics(); await replayResumedIdentity(daemon, "A", "B", { url: "ws://a", token: "a" }); bot.calls.length = 0; await daemon.scanRoots(); now += 60_000; await daemon.scanRoots(); expect(bot.calls.filter(call => call.method === "deleteForumTopic")).toEqual([]); expect((await readTopicAuthorityState(agentDir)).topics.B).toMatchObject({ topicId: String(topicId) }); }); test("a B-to-A rekey does not strand an independently recovered B owner", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon(agentDir, bot); await replayResumedIdentity(daemon, "B", "B", { url: "ws://b", token: "b" }); const bTopic = bot.createdTopicThreadIds[0]!; await replayResumedIdentity(daemon, "B", "A", { url: "ws://a", token: "a" }); await replayResumedIdentity(daemon, "B-RETURN", "B", { url: "ws://b", token: "b" }); const recovered = daemon.sessions.get("B-RETURN")!; expect(recovered.logicalSessionId).toBe("B"); bot.calls.length = 0; await daemon.handleSessionMessage(recovered, { type: "turn_stream", sessionId: "B", text: "independent B recovery", }); expect( bot.calls.filter(call => call.method === "sendMessage" && call.body.message_thread_id === bTopic), ).toHaveLength(1); }); test("held initial daemon topic publication admits neither inbound nor outbound routing", async () => { FakeWs.instances = []; const writeStarted = Promise.withResolvers(); const releaseWrite = Promise.withResolvers(); let holdWrite = true; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { if (!holdWrite) return; holdWrite = false; writeStarted.resolve(); await releaseWrite.promise; }), ); daemon.connectSession("S", "ws://s", "token"); const session = daemon.sessions.get("S")!; const identity = daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "main", }); await writeStarted.promise; const socket = session.ws as unknown as FakeWs; socket.sent.length = 0; bot.calls.length = 0; await daemon.handleTelegramUpdate({ update_id: 280_301, message: { chat: { id: 42 }, message_thread_id: 1, message_id: 1, text: "must not route" }, }); await daemon.handleSessionMessage(session, { type: "turn_stream", sessionId: "S", text: "must wait for commit" }); expect(socket.sent).toEqual([]); expect(bot.calls.filter(call => call.method === "sendMessage")).toEqual([]); releaseWrite.resolve(); await identity; }); test("stale accepted create cannot fence or delete a successor topic lease", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); const originalCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); createStarted.resolve(); return releaseCreate.promise; } return originalCall(method, body, options); }; const daemon = recoveryDaemon(agentDir, bot); daemon.connectSession("S", "ws://old", "old"); const predecessor = daemon.sessions.get("S")!; const creating = (daemon as any).ensureTopic("S", "old topic", predecessor); await createStarted.promise; daemon.connectSession("S", "ws://successor", "new"); const successor = daemon.sessions.get("S")!; (daemon as any).topics.replace({ topics: { S: { topicId: "888", identitySent: false, createdAt: 1, authorityEpoch: 2, creationLeaseEpoch: 2, chatId: "42", endpointKey: successor.endpointKey, endpointDigest: successor.endpointDigest, endpointGeneration: 1, }, }, fences: { S: 2 }, }); releaseCreate.resolve({ ok: true, result: { message_thread_id: 777 } }); await expect(creating).resolves.toBeUndefined(); expect((daemon as any).topics.get("S")).toMatchObject({ topicId: "888" }); expect((daemon as any).topics.get("S")?.authorityState).not.toBe("delete_pending"); expect(bot.calls.filter(call => call.method === "deleteForumTopic")).toEqual([]); }); test("post-dispatch delete rejection retains the exact tombstone despite a successor and restart", async () => { FakeWs.instances = []; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const deleteStarted = Promise.withResolvers(); const releaseDelete = Promise.withResolvers(); const originalCall = bot.call.bind(bot); let holdDelete = true; bot.call = async (method, body, options) => { if (method === "deleteForumTopic" && holdDelete) { bot.calls.push({ method, body, options }); deleteStarted.resolve(); return releaseDelete.promise; } return originalCall(method, body, options); }; const daemon = recoveryDaemon(agentDir, bot); await replayResumedIdentity(daemon, "PREDECESSOR", "S", { url: "ws://old", token: "old" }); const predecessor = daemon.sessions.get("PREDECESSOR")!; const topicId = bot.createdTopicThreadIds.at(-1)!; const closing = daemon.handleSessionMessage(predecessor, { type: "session_closed", sessionId: "S" }); await deleteStarted.promise; daemon.connectSession("SUCCESSOR", "ws://successor", "new"); releaseDelete.resolve({ ok: false, description: "transport unavailable" }); await closing; const fenced = await readTopicAuthorityState(agentDir); expect(fenced.topics.S).toMatchObject({ topicId: String(topicId), authorityState: "delete_pending" }); expect(fenced.topics.S.authorityState).not.toBe("active"); holdDelete = false; const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); await restarted.scanRoots(); expect((await readTopicAuthorityState(agentDir)).topics.S).toBeUndefined(); }); test("revoked create retains a delete fence after two failed publications and ambiguous deletion across restart", async () => { FakeWs.instances = []; const createStarted = Promise.withResolvers(); const releaseCreate = Promise.withResolvers(); let topicWrites = 0; const agentDir = tempAgentDir(); const bot = new FakeBotApi(); const call = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "createForumTopic") { bot.calls.push({ method, body, options }); createStarted.resolve(); return releaseCreate.promise; } if (method === "deleteForumTopic") { bot.calls.push({ method, body, options }); return { ok: false, description: "transport unavailable" }; } return call(method, body, options); }; const daemon = recoveryDaemon( agentDir, bot, "42", topicStateFs(async () => { topicWrites++; if (topicWrites <= 2) throw new Error("durable fence unavailable"); }), ); daemon.connectSession("S", "ws://old", "old"); const predecessor = daemon.sessions.get("S")!; const creating = (daemon as any).ensureTopic("S", "topic", predecessor); await createStarted.promise; daemon.connectSession("S", "ws://successor", "new"); releaseCreate.resolve({ ok: true, result: { message_thread_id: 909 } }); await expect(creating).rejects.toThrow("durable fence unavailable"); expect(bot.calls.filter(call => call.method === "deleteForumTopic")).toHaveLength(1); const fenced = await readTopicAuthorityState(agentDir); expect(fenced.topics.S).toMatchObject({ topicId: "909", authorityState: "delete_pending" }); const restarted = recoveryDaemon(agentDir, bot); await restarted.loadTopics(); bot.calls.length = 0; await restarted.scanRoots(); expect( bot.calls.filter(call => call.method === "deleteForumTopic").map(call => call.body.message_thread_id), ).toEqual([909]); }); }); test("Telegram Bot API 429 cooldown suppresses outbound calls while polling continues and expires", async () => { const agentDir = tempAgentDir(); let now = 0; const bot = new FakeBotApi(); const call = bot.call.bind(bot); let sendAttempts = 0; bot.call = async (method, body, options) => { if (method === "sendMessage") { sendAttempts++; if (sendAttempts === 1) { bot.calls.push({ method, body, options }); return { ok: false, error_code: 429, parameters: { retry_after: 3 } }; } } return await call(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as any, now: () => now, }); const api = (daemon as any).botApi as BotApi; const warning = spyOn(logger, "warn").mockImplementation(() => {}); try { expect(await api.call("sendMessage", { chat_id: 42, text: "first" })).toMatchObject({ error_code: 429 }); expect(await api.call("sendMessage", { chat_id: 42, text: "suppressed" })).toBeUndefined(); expect(await api.call("createForumTopic", { chat_id: 42, name: "suppressed" })).toBeUndefined(); expect(bot.calls.filter(entry => entry.method === "sendMessage")).toHaveLength(1); expect(bot.calls.filter(entry => entry.method === "createForumTopic")).toHaveLength(0); expect(await api.call("getUpdates", { timeout: 0 })).toMatchObject({ ok: true }); expect(bot.calls.filter(entry => entry.method === "getUpdates")).toHaveLength(1); expect(warning.mock.calls.filter(call => String(call[0]).includes("flood-control cooldown"))).toHaveLength(1); now = 3_000; expect(await api.call("sendMessage", { chat_id: 42, text: "resumed" })).toMatchObject({ ok: true }); expect(sendAttempts).toBe(2); } finally { warning.mockRestore(); } }); test("cooldown-active flush grant requeues untouched and settles ambiguous, then retries after cooldown", async () => { const agentDir = tempAgentDir(); let now = 0; const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as never, now: () => now, }); const internal = daemon as unknown as { botCooldownUntil: number; pool: { pending: number; submit(item: object): void; settle(itemId: string, disposition: "accepted" | "ambiguous" | "rejected" | "removed"): void; }; flushPool(): Promise; }; // A bot-wide 429 cooldown is active: every outbound call would be suppressed // by callBotApi, so the drained grant must never settle "accepted". internal.botCooldownUntil = 5_000; const settle = spyOn(internal.pool, "settle"); try { internal.pool.submit({ sessionId: "S", lane: "finalized", itemId: "cooldown-item", payload: { send: { method: "sendMessage", lane: "finalized", text: "held" } }, }); await internal.flushPool(); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); expect(settle).toHaveBeenCalledWith("cooldown-item", "ambiguous"); // Requeued untouched: exactly-once holds because suppression proves nothing left. expect(internal.pool.pending).toBe(1); now = 5_000; await internal.flushPool(); const sent = bot.calls.filter(call => call.method === "sendMessage"); expect(sent).toHaveLength(1); expect(String(sent[0]!.body.text)).toContain("held"); expect(settle.mock.calls.some(call => call[1] === "accepted")).toBe(true); } finally { settle.mockRestore(); } }); test("initiating 429 flush response requeues and settles ambiguous before retry", async () => { const agentDir = tempAgentDir(); let now = 0; const bot = new FakeBotApi(); const call = bot.call.bind(bot); let attempts = 0; bot.call = async (method, body, options) => { if (method === "sendMessage" && ++attempts === 1) { bot.calls.push({ method, body, options }); return { ok: false, error_code: 429, parameters: { retry_after: 3 } }; } return await call(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as never, now: () => now, }); const internal = daemon as unknown as { pool: { pending: number; submit(item: object): void; settle(itemId: string, disposition: "accepted" | "ambiguous" | "rejected" | "removed"): void; }; flushPool(): Promise; }; const settle = spyOn(internal.pool, "settle"); try { internal.pool.submit({ sessionId: "S", lane: "finalized", itemId: "fresh-429", payload: { send: { method: "sendMessage", lane: "finalized", text: "retry me" } }, }); await internal.flushPool(); expect(settle).toHaveBeenCalledWith("fresh-429", "ambiguous"); expect(internal.pool.pending).toBe(1); now = 3_000; await internal.flushPool(); expect(bot.calls.filter(entry => entry.method === "sendMessage")).toHaveLength(2); expect(settle.mock.calls.some(call => call[1] === "accepted")).toBe(true); } finally { settle.mockRestore(); } }); test("concurrent accepted typing cannot suppress requeue of the current retryable item", async () => { const agentDir = tempAgentDir(); const now = 0; const bot = new FakeBotApi(); const realCall = bot.call.bind(bot); const sendStarted = Promise.withResolvers(); const releaseRetryable = Promise.withResolvers(); bot.call = async (method, body, options) => { if (method === "sendMessage") { bot.calls.push({ method, body, options }); sendStarted.resolve(); await releaseRetryable.promise; return { ok: false, error_code: 429, parameters: { retry_after: 3 } }; } return await realCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as never, now: () => now, }); const internal = daemon as unknown as { pool: { pending: number; submit(item: object): void; settle(itemId: string, disposition: "accepted" | "ambiguous" | "rejected" | "removed"): void; }; flushPool(): Promise; botApi: BotApi; }; const settle = spyOn(internal.pool, "settle"); try { internal.pool.submit({ sessionId: "S", lane: "finalized", itemId: "concurrent-429", payload: { send: { method: "sendMessage", lane: "finalized", text: "retry me" } }, }); const flushing = internal.flushPool(); await sendStarted.promise; await internal.botApi.call("sendChatAction", { chat_id: "42", action: "typing" }); expect(bot.calls.some(call => call.method === "sendChatAction")).toBe(true); releaseRetryable.resolve(); await flushing; expect(settle).toHaveBeenCalledWith("concurrent-429", "ambiguous"); expect(internal.pool.pending).toBe(1); } finally { settle.mockRestore(); } }); test("mid-item cooldown suppression flips an otherwise-accepted grant to ambiguous", async () => { const agentDir = tempAgentDir(); const now = 0; const bot = new FakeBotApi(); const realCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendRichMessageDraft") { bot.calls.push({ method, body, options }); // A concurrent 429 lands after the grant's pre-check: the cooldown // engages mid-item, so the frame's own send is silently suppressed. return { ok: false, error_code: 429, parameters: { retry_after: 3 } }; } return await realCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, WebSocketImpl: FakeWs as never, now: () => now, richDraft: { enabled: true }, }); const internal = daemon as unknown as { botCooldownUntil: number; pool: { pending: number; submit(item: object): void; settle(itemId: string, disposition: "accepted" | "ambiguous" | "rejected" | "removed"): void; }; flushPool(): Promise; }; const warning = spyOn(logger, "warn").mockImplementation(() => {}); const settle = spyOn(internal.pool, "settle"); try { internal.pool.submit({ sessionId: "S", lane: "live", itemId: "draft-item", coalesceKey: "draft", payload: { send: { method: "sendMessage", lane: "live", text: "frame", editable: true, richDraftMarkdown: "frame" }, }, }); await internal.flushPool(); expect(internal.botCooldownUntil).toBeGreaterThan(0); // The suppressed send neither threw nor delivered; the grant must not // record a possibly-undelivered frame as accepted. expect(settle).toHaveBeenCalledWith("draft-item", "ambiguous"); expect(bot.calls.filter(call => call.method === "sendMessage")).toHaveLength(0); } finally { settle.mockRestore(); warning.mockRestore(); } }); test("Telegram Bot API 429 cooldown starts when a delayed response is received", async () => { const agentDir = tempAgentDir(); let now = 0; const calls: string[] = []; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: { async call(method) { calls.push(method); if (method === "sendMessage" && calls.filter(entry => entry === "sendMessage").length === 1) { now = 5_000; return { ok: false, error_code: 429, parameters: { retry_after: 3 } }; } return { ok: true, result: true }; }, }, WebSocketImpl: FakeWs as any, now: () => now, }); const api = (daemon as any).botApi as BotApi; await api.call("sendMessage", { chat_id: 42, text: "delayed 429" }); now = 7_999; expect(await api.call("sendMessage", { chat_id: 42, text: "still suppressed" })).toBeUndefined(); now = 8_000; expect(await api.call("sendMessage", { chat_id: 42, text: "resumed" })).toMatchObject({ ok: true }); expect(calls.filter(method => method === "sendMessage")).toHaveLength(2); }); test("Telegram Bot API 429 cooldown clamps malformed retry_after values and does not hot-loop", async () => { for (const [retryAfter, expectedMs] of [ ["x", 30_000], [-5, 1_000], [1e9, 3_600_000], ] as const) { const agentDir = tempAgentDir(); let now = 0; const calls: string[] = []; const daemon = new TelegramNotificationDaemon({ settings: settings(agentDir), ownerId: "owner", botToken: "tok", chatId: "42", botApi: { async call(method) { calls.push(method); return method === "sendMessage" ? { ok: false, error_code: 429, parameters: { retry_after: retryAfter } } : { ok: true, result: [] }; }, }, WebSocketImpl: FakeWs as any, now: () => now, }); const api = (daemon as any).botApi as BotApi; await api.call("sendMessage", { chat_id: 42, text: "429" }); for (let i = 0; i < 10; i++) await api.call("sendMessage", { chat_id: 42, text: "suppressed" }); expect(calls.filter(method => method === "sendMessage")).toHaveLength(1); now = expectedMs - 1; expect(await api.call("sendMessage", { chat_id: 42, text: "still suppressed" })).toBeUndefined(); now++; await api.call("sendMessage", { chat_id: 42, text: "expired" }); expect(calls.filter(method => method === "sendMessage")).toHaveLength(2); } }); // --------------------------------------------------------------------------- // PR #3186 blockers: serving epoch convergence, visible-start policy orphaning, // 429-retryable settlement retention, and concurrent start/terminal pool IDs. // --------------------------------------------------------------------------- describe("PR #3186 blockers", () => { test("serving epoch 4 replaces earlier-epoch daemons via isCurrentCompatibleOwner", () => { // Epoch 1: legacy daemon states that never published servingEpoch. // Epoch 4 reloads epoch-3 daemons; 0.7.14 and earlier among them poll a // rejected bot token every second until the generation 32 poller replaces them. expect(SERVING_EPOCH).toBe(4); const freshInput = (servingEpoch?: number) => { const state: DaemonState = { pid: 999, incarnation: "linux:100", ownerId: "old", tokenFingerprint: "fp", chatId: "42", startedAt: 100, heartbeatAt: 100, roots: [], version: 1, acquisitionId: "old", ownershipPhase: "ready", generation: DAEMON_GENERATION, servingEpoch, }; return { state, now: state.heartbeatAt, tokenFingerprint: state.tokenFingerprint, chatId: state.chatId, pidAlive: () => true, pidIncarnation: () => state.incarnation, }; }; // Epoch undefined (epoch 1) — not compatible. expect(isCurrentCompatibleOwner(freshInput(undefined))).toBe(false); // Epochs 2 and 3 — not compatible with epoch 4. expect(isCurrentCompatibleOwner(freshInput(2))).toBe(false); expect(isCurrentCompatibleOwner(freshInput(3))).toBe(false); // Epoch 4 — compatible. expect(isCurrentCompatibleOwner(freshInput(4))).toBe(true); // Future epoch 5 — still compatible (>= check). expect(isCurrentCompatibleOwner(freshInput(5))).toBe(true); }); test("visible v1 starts are terminated with terminalization on policy transition", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "policy-vis-authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; // Admit a legacy start and let it become visible (delivered). await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "visible-start-1", toolName: "bash", phase: "started", }); const runtime = daemon as unknown as { legacyToolStarts: Map; toolActivityPolicyEpoch: number; opts: { toolActivity?: { enabled: boolean } }; cancelLegacyToolStartsForPolicyTransition(): Promise; toolTerminalizationChain: Promise; liveMessages: Map; }; const key = "S:tool:visible-start-1"; expect(runtime.legacyToolStarts.has(key)).toBe(true); expect(runtime.legacyToolStarts.get(key)!.phase).toBe("visible"); // Record its liveMessage (the FakeBotApi already recorded it). expect(runtime.liveMessages.has(key)).toBe(true); // Trigger a policy transition (simulating /tool-activity off). const editsBefore = bot.calls.filter(c => c.method === "editMessageText").length; runtime.toolActivityPolicyEpoch++; runtime.opts.toolActivity = { enabled: false }; await runtime.cancelLegacyToolStartsForPolicyTransition(); await runtime.toolTerminalizationChain; // The visible start must be settled as "terminal" and cleaned up. expect(runtime.legacyToolStarts.has(key)).toBe(false); // A terminalization edit should have been enqueued for the visible message. const editsAfter = bot.calls.filter(c => c.method === "editMessageText").length; expect(editsAfter).toBeGreaterThan(editsBefore); }); test("retryable 429 start send retains exact settlement until definitive outcome", async () => { const bot = new FakeBotApi(); let sendCount = 0; const realCall = bot.call.bind(bot); bot.call = async (method, body, options) => { if (method === "sendMessage") { sendCount++; if (sendCount === 1) { // First attempt: retryable 429. bot.calls.push({ method, body, options }); return { ok: false, error_code: 429, parameters: { retry_after: 3 } }; } } return await realCall(method, body, options); }; let now = 0; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, now: () => now, }); const session = richSession(); session.endpointDigest = "retry-authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; sendCount = 0; // Send a legacy v1 tool start. await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "retryable-start", toolName: "bash", phase: "started", }); const runtime = daemon as unknown as { legacyToolStarts: Map; pool: { pending: number }; }; const key = "S:tool:retryable-start"; // After the 429, the legacy start must NOT be failed — it's retryable. // The settlement should still be live (the start is still in-flight via requeue). expect(runtime.legacyToolStarts.has(key)).toBe(true); const state = runtime.legacyToolStarts.get(key)!; // The phase must not be "failed" — it stays admitted/dispatching since the requeue is pending. expect(state.phase).not.toBe("failed"); expect(state.settledOutcome).toBeUndefined(); expect(runtime.pool.pending).toBeGreaterThan(0); // Advance through cooldown/refill windows until the queued start retries. const internal = daemon as unknown as { flushPool(): Promise }; for (let attempt = 0; attempt < 3 && sendCount < 2; attempt++) { now += 3_000; await internal.flushPool(); } // Now the second sendMessage should have succeeded. expect(sendCount).toBeGreaterThan(1); expect(runtime.legacyToolStarts.has(key)).toBe(true); expect(runtime.legacyToolStarts.get(key)!.phase).toBe("visible"); }); test("retryable legacy start without its exact settlement is not requeued", async () => { const bot = new FakeBotApi(); const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const realCall = bot.call.bind(bot); let startAttempts = 0; bot.call = async (method, body, options) => { if (method === "sendMessage" && String((body as { text?: unknown }).text).includes("read — started")) { startAttempts++; entered.resolve(); await release.promise; return { ok: false, error_code: 429, parameters: { retry_after: 1 } }; } return await realCall(method, body, options); }; let now = 0; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, now: () => now, }); const session = richSession(); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); const start = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "lost-settlement", toolName: "read", phase: "started", }); await entered.promise; const runtime = daemon as unknown as { legacyToolStarts: Map; flushPool(): Promise; }; runtime.legacyToolStarts.delete("S:tool:lost-settlement"); release.resolve(); await start; now = 3_000; await runtime.flushPool(); expect(startAttempts).toBe(1); }); test("concurrent start and terminal frames use distinct pool item IDs sharing the same settlement", async () => { const bot = new FakeBotApi(); const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const realCall = bot.call.bind(bot); let startSendSeen = false; bot.call = async (method, body, options) => { const text = String((body as { text?: unknown }).text ?? ""); if (method === "sendMessage" && text.includes("bash — started") && !startSendSeen) { startSendSeen = true; entered.resolve(); await release.promise; } return await realCall(method, body, options); }; const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "pool-id-authority"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; // Send a start frame — it will block on the first sendMessage. const startPromise = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "pool-id-test", toolName: "bash", phase: "started", }); await entered.promise; const runtime = daemon as unknown as { legacyToolStarts: Map; pool: { pending: number; submit(item: object): { itemId: string }; }; nextLegacyToolStartId: number; }; const key = "S:tool:pool-id-test"; expect(runtime.legacyToolStarts.has(key)).toBe(true); const startState = runtime.legacyToolStarts.get(key)!; const startItemId = startState.itemId; expect(startItemId).toBeDefined(); expect(startItemId).toMatch(/^legacy-tool-start:/); // Now send the terminal (completed) while the start is still in-flight. const terminalPromise = daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "pool-id-test", toolName: "bash", phase: "completed", }); // Release the blocked start send. release.resolve(); await Promise.all([startPromise, terminalPromise]); // The terminal must have used a different pool item ID (legacy-tool-terminal:N). // Verify via the bot calls: both start and terminal should have been sent. const sends = bot.calls.filter(c => c.method === "sendMessage"); // The start goes through as a sendMessage, then the terminal edits it. expect(sends.length).toBeGreaterThanOrEqual(1); // After completion, the legacy start settlement should be retired. expect(runtime.legacyToolStarts.has(key)).toBe(false); }); test("terminal frame does not mutate legacy start phase to queued", async () => { const bot = new FakeBotApi(); const daemon = new TelegramNotificationDaemon({ settings: settings(tempAgentDir()), ownerId: "owner", botToken: "tok", chatId: "42", botApi: bot, toolActivity: { enabled: true }, }); const session = richSession(); session.endpointDigest = "phase-guard"; daemon.sessions.set("S", session); await daemon.handleSessionMessage(session, { type: "hello", capabilities: [LEGACY_TOOL_ACTIVITY_CAPABILITY] }); await daemon.handleSessionMessage(session, { type: "identity_header", sessionId: "S", repo: "repo", branch: "branch", }); bot.calls = []; // Admit a start, let it become visible. await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "phase-guard-test", toolName: "read", phase: "started", }); const runtime = daemon as unknown as { legacyToolStarts: Map; }; const key = "S:tool:phase-guard-test"; expect(runtime.legacyToolStarts.has(key)).toBe(true); expect(runtime.legacyToolStarts.get(key)!.phase).toBe("visible"); // Send the terminal (completed) — it must NOT reset the phase to "queued". await daemon.handleSessionMessage(session, { type: "tool_activity", sessionId: "S", toolCallId: "phase-guard-test", toolName: "read", phase: "completed", }); // The settlement must be fully retired after the terminal completes. expect(runtime.legacyToolStarts.has(key)).toBe(false); // Both a start send and a terminal edit must have been produced. const sends = bot.calls.filter(c => c.method === "sendMessage"); const edits = bot.calls.filter(c => c.method === "editMessageText"); expect(sends.length).toBeGreaterThanOrEqual(1); expect(edits.length).toBeGreaterThanOrEqual(1); }); });