diff --git a/CLAUDE.md b/CLAUDE.md index 5b61256..bd5c904 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -211,9 +211,14 @@ what it is handed. Keep it that way. retry counters/deadlines and bounded bridge status errors. Persist BEFORE any Mac mutation, then execute one due action per collector run. Never detach it. A failed action survives restarts and retries with backoff (2–60 seconds). - Only `--all` is dropped on failure (it cannot be bounded); a per-chat intent - is NEVER dropped, however many attempts. Dropping one after five tries - resurrected a dot the user had already read through `intent.seen`, after a + `--all` is attempted once; a failure or interruption keeps its click timestamp + and becomes per-chat `--through` retries from the next complete snapshot. + Later arrivals and explicit gestures are preserved; without a snapshot the + bulk intent waits without repeating `--all`. Business/service ids that cannot + be opened individually retain the bounded bulk marker until a snapshot + confirms they are read; the marker never blocks later per-chat gestures. + Attempts are persisted before touching Messages. A per-chat intent is NEVER + dropped, however many attempts. Dropping one after five tries resurrected a dot the user had already read through `intent.seen`, after a night of a locked Mac (441 attempts, 2026-10-06). A kept intent cannot consume a newer inbound: the snapshot compare and `--through` guard it. `imsg read-state` returns the COMPLETE metadata-only unread snapshot; it is diff --git a/README.md b/README.md index 5b475d4..4e40d03 100644 --- a/README.md +++ b/README.md @@ -902,6 +902,14 @@ for conversations outside the recent-message window. Confirmed local overrides are retired, allowing later Mac/iPhone changes to take effect. Both updated Mac tools require the sibling `read_state.py` module. +Mark-all is attempted once. If it fails or is interrupted, its click timestamp +is retained and the next complete read-state snapshot turns it into bounded +per-conversation retries. Messages newer than the click remain unread. Service +conversations whose identifiers cannot be opened individually keep a local +read marker; they require a later successful mark-all in Messages to sync. +A locked Mac refuses UI read actions promptly; unlocking is still required +for those actions to reach Messages. + `push_read` in `bridge.conf` takes three values, and the default surprises people: **`all`** (the default) pushes *only* on the mark-all gesture, so reading one conversation in Blip clears its dot here and leaves your iPhone's diff --git a/bridge/mac/imsg-read b/bridge/mac/imsg-read index d835ee4..3005796 100755 --- a/bridge/mac/imsg-read +++ b/bridge/mac/imsg-read @@ -66,6 +66,10 @@ def messages_ui_lock(): The owner-only lock contains no data. A second read must never change the selected conversation between the first read's URL and its menu click. """ + # No menu action can succeed behind the lock screen. Refuse before + # acquiring the shared lock or starting an AppleEvent that can hang. + if screen_locked(): + raise OSError(SCREEN_LOCKED) folder = os.path.expanduser("~/.blip") os.makedirs(folder, mode=0o700, exist_ok=True) parent = os.open(folder, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) diff --git a/bridge/mac/test_read.py b/bridge/mac/test_read.py index 8156d8f..a066802 100644 --- a/bridge/mac/test_read.py +++ b/bridge/mac/test_read.py @@ -36,6 +36,16 @@ def year_one_props(props: dict) -> bytes: class ReadTests(unittest.TestCase): + def test_locked_screen_refuses_before_waiting_for_ui_lock_or_calling_automation(self): + with patch.object(read, "screen_locked", return_value=True), \ + patch.object(read.os, "makedirs") as mkdir, \ + patch.object(read, "osa") as osa: + with self.assertRaisesRegex(OSError, "screen is locked"): + with read.messages_ui_lock(): + self.fail("locked screen admitted a UI operation") + mkdir.assert_not_called() + osa.assert_not_called() + def test_group_and_direct_urls(self): for identifier, key in [("chat12345", "groupid"), ("a" * 32, "groupid"), ("+15551234567", "address"), ("12345", "address"), @@ -44,7 +54,9 @@ def test_group_and_direct_urls(self): self.assertTrue(read.chat_url(identifier).startswith("imessage:open?")) def test_private_lock_refuses_symlinks(self): - with tempfile.TemporaryDirectory() as folder, patch.object(read.os.path, "expanduser", return_value=folder): + with tempfile.TemporaryDirectory() as folder, \ + patch.object(read.os.path, "expanduser", return_value=folder), \ + patch.object(read, "screen_locked", return_value=False): with read.messages_ui_lock(): self.assertEqual(os.stat(Path(folder) / "messages-ui.lock").st_mode & 0o777, 0o600) (Path(folder) / "messages-ui.lock").unlink() diff --git a/collector.ts b/collector.ts index e90cd53..eb4507c 100644 --- a/collector.ts +++ b/collector.ts @@ -2377,6 +2377,29 @@ export function checkMacSession(runner = spawnSync, path = MAC_SESSION_PATH, now return state; } +/** A failed bulk read may only retry the conversations and timestamps the + * original click covered. Later explicit gestures win, including aliases. */ +export function expandFailedMarkAll(pending: ReadIntents, snapshot: ReadSnapshot): ReadIntents { + const all = pending["*"]; + if (!all || all.attempts === 0 || !all.seen) return pending; + const next = { ...pending }; + delete next["*"]; + const unaddressable: string[] = []; + for (const [chat, row] of Object.entries(snapshot)) { + if ((all.chats && !all.chats.includes(chat)) || !row.unread || row.latest > all.seen || + [chat, ...(row.aliases ?? [])].some(id => next[id])) continue; + if (!canPushChat(chat)) { + // Business/service identifiers cannot be opened by --chat. Keep the + // original bounded suppression until the Mac confirms they are read. + unaddressable.push(chat); + continue; + } + next[chat] = { ...all, seen: row.latest }; + } + if (unaddressable.length) next["*"] = { ...all, chats: unaddressable }; + return next; +} + export function collect(deep: boolean, markRead = false, readChat = "", seenTs = "", unreadChat = "", explicitRead = false): BlipOutput { // When this POLL ran — the output's own metadata, not a message stamp. // Distinct from nowTs() below, which is the clock message stamps are @@ -2388,9 +2411,8 @@ export function collect(deep: boolean, markRead = false, readChat = "", seenTs = // Persist explicit intent before any network call. An offline click survives // the collector exiting and is retried after reconnection. if (readPush !== "off") { - // Stamp the click. A later --all retry is not safe (it would mark new - // arrivals read on the phone), so this intent is attempted once and only - // hides rows that already existed. + // Stamp the click. Attempt --all once; failed/interrupted attempts become + // timestamp-bounded per-chat retries, so newer arrivals stay unread. if (markRead) pendingReads = queueReadIntent(pendingReads, "*", false, nowTs()); if (unreadChat && pushUnreadArgs(unreadChat)) pendingReads = queueReadIntent(pendingReads, unreadChat, true); if ((explicitRead || (readPush === "thread" && (state.unreadCounts[readChat] ?? 0) > 0)) && readChat && readChat !== unreadChat && canPushChat(readChat)) pendingReads = queueReadIntent(pendingReads, readChat, false, seenTs); @@ -2400,7 +2422,10 @@ export function collect(deep: boolean, markRead = false, readChat = "", seenTs = throw new Error("state write failed; read action was not queued"); } const remoteReads = fetchReadSnapshot(); - if (remoteReads) pendingReads = reconcileReadIntents(pendingReads, remoteReads); + if (remoteReads) { + pendingReads = expandFailedMarkAll(pendingReads, remoteReads); + pendingReads = reconcileReadIntents(pendingReads, remoteReads); + } // Per-chat catch-up. A single global oldest-unread cutoff made every poll // walk the whole history; windowCutoff stays at the watermark and stale // chats are fetched on their own below. @@ -2604,7 +2629,9 @@ export function collect(deep: boolean, markRead = false, readChat = "", seenTs = if (reading && readPush === "thread" && row.unread > 0 && seenTs && row.latest <= seenTs && canPushChat(readChat)) { pendingReads = queueReadIntent(pendingReads, readChat, false, seenTs); } - const intent = pendingReads[canonical] ?? pendingReads["*"]; + const bulk = pendingReads["*"]; + const intent = pendingReads[canonical] ?? pendingReads[chat] ?? + (bulk && (!bulk.chats || bulk.chats.includes(chat)) ? bulk : undefined); const localMark = readMarks[chat] || readMarks[canonical] || ""; if (markRead || (reading && seenTs && row.latest <= seenTs) || // Outside per-thread sync, the global mark is what mark-all and the @@ -2689,28 +2716,31 @@ export function collect(deep: boolean, markRead = false, readChat = "", seenTs = // Process one due intent per run so a failed chat cannot starve the rest. let syncError = ""; if (persisted && readPush !== "off") { - const candidates = pendingReads["*"] ? [["*", pendingReads["*"]] as const] : Object.entries(pendingReads); + // An attempted bulk action needs a complete snapshot before it can be + // expanded safely. Never repeat an unbounded --all, even after a restart. + const all = pendingReads["*"]; + const candidates = all && all.attempts === 0 ? [["*", all] as const] + : Object.entries(pendingReads).filter(([chat]) => chat !== "*"); const due = candidates.filter(([, r]) => r.retryAt <= Date.now()) .sort((a, b) => a[1].retryAt - b[1].retryAt)[0]; if (due) { const [chat, intent] = due; - const result = chat === "*" ? runReadAction(["--all"]) - : runReadAction([intent.unread ? "--unread" : "--chat", chat, - ...(!intent.unread && intent.seen ? ["--through", intent.seen] : [])]); - if (result.ok) delete pendingReads[chat]; - else if (chat === "*") { - // --all cannot be bounded, so it is never retried. Per-chat intents - // remain durable through an outage: dropping one resurrects the - // unread dot even though the user already read through intent.seen. - // Retries back off to once per minute, and both the - // snapshot and --through guard against consuming a newer inbound. - delete pendingReads[chat]; - syncError = result.error || "read sync stopped"; + // Persist the attempt before touching Messages. An interrupted --all + // must also resume as bounded per-chat work, never another --all. + pendingReads[chat] = retryReadIntent(intent, Date.now()); + if (!saveState({ ...nextState, pendingReads })) { + syncError = "read attempt could not be saved; action was not started"; } else { - pendingReads[chat] = retryReadIntent(intent, Date.now(), result.error); - syncError = result.error; + const result = chat === "*" ? runReadAction(["--all"]) + : runReadAction([intent.unread ? "--unread" : "--chat", chat, + ...(!intent.unread && intent.seen ? ["--through", intent.seen] : [])]); + if (result.ok) delete pendingReads[chat]; + else { + pendingReads[chat] = retryReadIntent(intent, Date.now(), result.error); + syncError = result.error; + } + if (!saveState({ ...nextState, pendingReads })) syncError = "read acknowledgement could not be saved; will verify again"; } - if (!saveState({ ...nextState, pendingReads })) syncError = "read acknowledgement could not be saved; will verify again"; } } const remaining = Object.keys(pendingReads).length; diff --git a/read-sync.test.ts b/read-sync.test.ts index 34a19b8..cb225bb 100644 --- a/read-sync.test.ts +++ b/read-sync.test.ts @@ -1,4 +1,5 @@ import { describe, expect, test } from "bun:test"; +import { expandFailedMarkAll } from "./collector"; import { mkdtempSync, mkdirSync, writeFileSync, readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -94,6 +95,8 @@ import json,os,sys home=os.environ['HOME']; args=sys.argv[1:] with open(home+'/actions.jsonl','a') as f: f.write(json.dumps(args)+'\\n') ctl=json.load(open(home+'/control.json')) +if ctl.get('inspect_attempt'): + with open(home+'/attempt.json','w') as f: json.dump(json.load(open(home+'/.local/state/blip/state.json'))['pendingReads'],f) if ctl.get('fail'): print('imsg-read: Accessibility is not granted',file=sys.stderr); sys.exit(77) data=json.load(open(home+'/remote.json')) @@ -304,3 +307,123 @@ test("a due retry cannot jump ahead of a fresh action for another chat", () => { f.run(); expect(f.actions()[0]).toEqual(["--chat", other, "--through", old]); }); + + +describe("failed mark-all recovery", () => { + test("failed bulk reads survive polls and retry only bounded conversations", () => { + const f = fixture(); f.run(); f.put("control.json", { fail: true }); + expect(f.run("--mark-read").unread).toBe(0); + expect(f.state().pendingReads["*"].attempts).toBe(1); + expect(f.run().unread).toBe(0); + expect(f.state().pendingReads[chat].seen).toBe(old); + expect(f.state().pendingReads["*"]).toBeUndefined(); + expect(f.actions()).toEqual([["--all"]]); + let st = f.state(); st.pendingReads[chat].retryAt = 0; f.save(st); + expect(f.run().unread).toBe(0); + expect(f.actions()).toEqual([["--all"], ["--chat", chat, "--through", old]]); + f.put("control.json", {}); + st = f.state(); st.pendingReads[chat].retryAt = 0; f.save(st); + expect(f.run().unread).toBe(0); + expect(f.state().pendingReads).toEqual({}); + expect(f.run().unread).toBe(0); + }, 15000); + + test("messages arriving after a failed bulk read stay unread", () => { + const f = fixture(); f.run(); f.put("control.json", { fail: true }); + f.run("--mark-read"); + const later = new Date(Date.now() + 60000).toISOString().replace(/\.\d{3}Z$/, "Z"); + f.put("remote.json", snapshot(1, later)); + expect(f.run().unread).toBe(1); + expect(f.state().pendingReads).toEqual({}); + expect(f.actions()).toEqual([["--all"]]); + }); + + test("without a snapshot a failed bulk action is retained but never repeated", () => { + const f = fixture(); f.run(); + f.put("control.json", { fail: true, snapshot_fail: true }); + f.run("--mark-read"); + const st = f.state(); st.pendingReads["*"].retryAt = 0; f.save(st); + f.run(); + expect(f.state().pendingReads["*"].attempts).toBe(1); + expect(f.actions()).toEqual([["--all"]]); + f.put("control.json", {}); f.run(); + expect(f.actions()).toEqual([["--all"], ["--chat", chat, "--through", old]]); + expect(f.run().unread).toBe(0); + }); + + test("bounded fallback preserves newer explicit gestures and skips new activity", () => { + const other = "+15557654321", alias = "reader@example.com", newer = "chat12345"; + const all = retryReadIntent(queueReadIntent({}, "*", false, recent)["*"]!, 1000, "locked"); + const pending = { "*": all, [alias]: { unread: true, seen: "", attempts: 0, retryAt: 0 } }; + const expanded = expandFailedMarkAll(pending, { + [chat]: { unread: 2, oldest: old, latest: old }, + [other]: { unread: 1, oldest: old, latest: old, aliases: [alias] }, + [newer]: { unread: 1, oldest: recent, latest: "2026-09-01T12:00:00Z" }, + }); + expect(expanded).toEqual({ [chat]: { ...all, seen: old }, [alias]: pending[alias] }); + expect(pending["*"]).toBe(all); + }); +}); + + +test("a business-thread bulk marker neither resurfaces nor blocks later gestures", () => { + const f = fixture(); + const business = "urn:biz:00000000-0000-0000-0000-000000000000"; + const data = snapshot(1); + data.chats.push({ chat: business, unread: 1, oldest: old, latest: old }); + f.put("remote.json", data); f.run(); + f.put("control.json", { fail: true }); f.run("--mark-read"); + expect(f.run().unread).toBe(0); + expect(f.state().pendingReads["*"].attempts).toBe(1); + expect(f.state().pendingReads[business]).toBeUndefined(); + const alreadyRead = snapshot(0); + alreadyRead.chats.push({ chat: business, unread: 1, oldest: old, latest: old }); + f.put("remote.json", alreadyRead); + f.put("control.json", {}); + expect(f.run("--mark-unread", chat).unread).toBe(1); + expect(f.actions()).toEqual([["--all"], ["--unread", chat]]); + expect(f.run().unread).toBe(1); + const later = new Date(Date.now() + 60000).toISOString().replace(/\.\d{3}Z$/, "Z"); + const fresh = snapshot(1); fresh.chats.push({ chat: business, unread: 1, oldest: old, latest: later }); + f.put("remote.json", fresh); + expect(f.run().unread).toBe(2); + expect(f.state().pendingReads["*"]).toBeUndefined(); +}); + + +test("a scoped bulk marker survives reload and cannot override other chats", () => { + const business = "urn:biz:00000000-0000-0000-0000-000000000000"; + const marker = { unread: false, seen: recent, attempts: 1, retryAt: 0, chats: [business] }; + const parsed = parseReadIntents({ "*": marker }); + expect(parsed["*"]).toEqual(marker); + expect(parseReadIntents({ "*": { ...marker, chats: ["bad\nidentifier"] } })).toEqual({}); + expect(parseReadIntents({ [chat]: marker })).toEqual({}); + expect(reconcileReadIntents(parsed, { + [business]: { unread: 0, oldest: "", latest: old }, + [chat]: { unread: 1, oldest: old, latest: old }, + })).toEqual({}); + expect(expandFailedMarkAll(parsed, { + [business]: { unread: 1, oldest: old, latest: old }, + [chat]: { unread: 1, oldest: old, latest: old }, + })).toEqual(parsed); +}); + + +test("the bulk attempt is durable before the Mac action starts", () => { + const f = fixture(); f.run(); + f.put("control.json", { inspect_attempt: true, fail: true }); + f.run("--mark-read"); + const atAction = JSON.parse(readFileSync(join(f.home, "attempt.json"), "utf8")); + expect(atAction["*"].attempts).toBe(1); + expect(atAction["*"].seen).not.toBe(""); +}); + +test("an interrupted bulk action resumes with a bounded read, never another all", () => { + const f = fixture(); f.run(); + const st = f.state(); + st.pendingReads = { "*": { unread: false, seen: recent, attempts: 1, retryAt: 0 } }; + f.save(st); + expect(f.run().unread).toBe(0); + expect(f.actions()).toEqual([["--chat", chat, "--through", old]]); + expect(f.state().pendingReads).toEqual({}); +}); diff --git a/read-sync.ts b/read-sync.ts index 36e84f3..cefd12e 100644 --- a/read-sync.ts +++ b/read-sync.ts @@ -2,7 +2,7 @@ /** Read synchronization metadata. No message bodies or contact names. */ export interface RemoteRead { unread: number; oldest: string; latest: string; aliases?: string[] } export type ReadSnapshot = Record; -export interface ReadIntent { unread: boolean; seen: string; attempts: number; retryAt: number; error?: string } +export interface ReadIntent { unread: boolean; seen: string; attempts: number; retryAt: number; error?: string; chats?: string[] } export type ReadIntents = Record; export function parseReadSnapshot(value: unknown): ReadSnapshot | null { @@ -31,7 +31,10 @@ export function parseReadIntents(value: unknown): ReadIntents { !Number.isFinite(r.retryAt) || r.retryAt < 0) continue; if (chat !== "*" && !/^\+?[0-9]{3,15}$/.test(chat) && !/^[^@\s]+@[^@\s]+$/.test(chat) && !/^(?:chat[0-9]{1,40}|[a-fA-F0-9]{32})$/.test(chat)) continue; + if (r.chats !== undefined && (chat !== "*" || !Array.isArray(r.chats) || r.chats.length > 32768 || + r.chats.some(id => typeof id !== "string" || !id || id.length > 512 || /[\x00-\x1f\x7f]/.test(id)))) continue; out[chat] = { unread: r.unread, seen: r.seen, attempts: r.attempts, retryAt: r.retryAt, + ...(r.chats ? { chats: r.chats } : {}), ...(typeof r.error === "string" ? { error: r.error.slice(0, 240).replace(/[\x00-\x1f\x7f]/g, " ") } : {}) }; } return out; @@ -57,11 +60,12 @@ export function reconcileReadIntents(pending: ReadIntents, snapshot: ReadSnapsho const next = { ...pending }; for (const [chat, intent] of Object.entries(next)) { if (chat === "*") { - const covered = Object.values(snapshot).filter(r => !intent.seen || r.latest <= intent.seen); + const covered = Object.entries(snapshot).filter(([id, r]) => + (!intent.chats || intent.chats.includes(id)) && (!intent.seen || r.latest <= intent.seen)).map(([, r]) => r); if (covered.every(r => r.unread === 0)) delete next[chat]; continue; } - if (next["*"]) continue; // a preceding mark-all must land before this gesture + if (next["*"]?.attempts === 0) continue; // an unattempted mark-all precedes this gesture const remote = snapshot[chat] ?? Object.values(snapshot).find(r => r.aliases?.includes(chat)); if (remote && ((remote.unread > 0) === intent.unread || (!intent.unread && intent.seen && remote.latest > intent.seen))) delete next[chat];