Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions bridge/mac/imsg-read
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion bridge/mac/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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()
Expand Down
72 changes: 51 additions & 21 deletions collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
123 changes: 123 additions & 0 deletions read-sync.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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({});
});
Loading
Loading