Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: show every unread GitHub notification and stop marking the inbox done locally (#13173)

[._.]/dd620380449a
diff --git a/server/services/githubNotifications.db.test.js b/server/services/githubNotifications.db.test.js
index 61ff15a9ac..c8244ccdf3 100644
--- a/server/services/githubNotifications.db.test.js
+++ b/server/services/githubNotifications.db.test.js
@@ -83,6 +83,24 @@ const listing = (threads, headers = {}) => [
JSON.stringify(threads),
].join('\r\n');
+// GitHub's answer to a listing request for an inbox holding `threads`: the
+// unread listing has only unread threads, done threads stay in the `all=true`
+// listing (as read), and a conditional request lists only threads updated
+// since its If-Modified-Since, or is a 304 when there are none.
+const IF_MODIFIED_SINCE = 'If-Modified-Since: ';
+function githubListing(args, threads) {
+ const path = args.find((arg) => /^notifications\?/.test(arg));
+ const since = args.find((arg) => String(arg).startsWith(IF_MODIFIED_SINCE));
+ let listed = path.includes('all=true') ? threads : threads.filter((t) => t.unread);
+ if (since) {
+ const cutoff = Date.parse(since.slice(IF_MODIFIED_SINCE.length));
+ listed = listed.filter((t) => Date.parse(t.updated_at) > cutoff);
+ if (!listed.length) throw Object.assign(new Error('gh: HTTP 304'), { ghExitCode: 1, ghStderr: 'gh: HTTP 304' });
+ }
+ const newest = Math.max(...threads.map((t) => Date.parse(t.updated_at)));
+ return listing(listed, { 'Last-Modified': new Date(newest).toUTCString() });
+}
+
// Routes gh calls: token lookup, the listing (via `respond`), thread actions,
// and triage's read-only metadata lookups (via `forge`, keyed by API path).
function mockGh(respond, forge = {}) {
@@ -131,14 +149,13 @@ describe.skipIf(!runDb)('GitHub notifications inbox against Postgres', () => {
accountIds.push(account.id);
});
- it('reads the whole inbox, sends If-Modified-Since next time, and marks a thread gone from GitHub done', async () => {
+ it('reads the whole inbox, keeps threads a since-cursor listing leaves out, and marks a thread gone from GitHub done', async () => {
const login = account.login;
const seen = [];
+ let inbox = [thread('101', { owner: login }), thread('102', { owner: 'someone-else' })];
mockGh((args) => {
seen.push(args);
- return seen.length === 1
- ? listing([thread('101', { owner: login }), thread('102', { owner: 'someone-else' })])
- : listing([{ ...thread('102', { owner: 'someone-else' }), unread: false }]);
+ return githubListing(args, inbox);
});
expect(await syncAccountNotifications(account.id)).toMatchObject({ changed: true, fetched: 2 });
@@ -148,28 +165,56 @@ describe.skipIf(!runDb)('GitHub notifications inbox against Postgres', () => {
expect.objectContaining({ threadId: '102', yourRepo: false }),
]));
expect(first.sync[account.id]).toMatchObject({ lastError: null, pollIntervalSeconds: 60 });
+ // A whole first read already holds every unread thread.
+ expect(seen.map((args) => args.find((arg) => /^notifications\?/.test(arg)))).toEqual(['notifications?all=true&per_page=50&page=1']);
+ // Both read on GitHub; a new thread arrives. The since-cursor listing
+ // names only the new one, which says nothing about the rest.
+ inbox = [...inbox.map((t) => ({ ...t, unread: false })), thread('103', { updatedAt: '2026-10-01T11:00:00Z' })];
await syncAccountNotifications(account.id);
- expect(seen[0]).toContain('notifications?all=true&per_page=50&page=1');
- expect(seen[1]).toContain('If-Modified-Since: Thu, 01 Oct 2026 10:00:00 GMT');
- // A cursor saved by the older unread-only listing is not sent.
- await query("UPDATE github_notification_sync SET last_modified = 'Thu, 01 Oct 2026 09:00:00 GMT' WHERE account_id = $1", [account.id]);
+ expect(seen[1]).toContain(`${IF_MODIFIED_SINCE}Thu, 01 Oct 2026 10:00:00 GMT`);
+ expect(seen[2]).toContain('notifications?per_page=50&page=1');
+ let rows = (await listNotifications({ accountId: account.id, view: 'all' })).notifications;
+ expect(rows.map((row) => [row.threadId, row.state, row.unread]).sort()).toEqual([
+ ['101', 'open', false], ['102', 'open', false], ['103', 'open', true],
+ ]);
+
+ // A cursor saved by an older sync is not sent, so the whole listing is
+ // read: a thread gone from it is done.
+ inbox = inbox.filter((t) => t.id !== '102');
+ await query("UPDATE github_notification_sync SET last_modified = 'all:Thu, 01 Oct 2026 11:00:00 GMT' WHERE account_id = $1", [account.id]);
+ const before = seen.length;
await syncAccountNotifications(account.id);
- expect(seen[2].some((arg) => String(arg).startsWith('If-Modified-Since'))).toBe(false);
- const rows = (await listNotifications({ accountId: account.id, view: 'all' })).notifications;
- // Read on GitHub but still in its inbox: open here too. Gone from the inbox: done.
- expect(rows.find((row) => row.threadId === '102')).toMatchObject({ unread: false, state: 'open' });
- expect(rows.find((row) => row.threadId === '101')).toMatchObject({ unread: false, state: 'done' });
- expect((await listNotifications({ accountId: account.id })).counts).toEqual({ 'needs-you': 1, suspect: 0, handled: 1, all: 2, 'with-agents': 0 });
+ expect(seen[before].some((arg) => String(arg).startsWith(IF_MODIFIED_SINCE))).toBe(false);
+ rows = (await listNotifications({ accountId: account.id, view: 'all' })).notifications;
+ expect(rows.find((row) => row.threadId === '102')).toMatchObject({ unread: false, state: 'done' });
+ expect(rows.find((row) => row.threadId === '101')).toMatchObject({ unread: false, state: 'open' });
+ });
+
+ it('brings in every unread thread past the inbox page cap and reopens one marked done here while still unread', async () => {
+ const inbox = [thread('151', { reason: 'mention' })];
+ const unreadOnly = [...inbox, thread('152', { reason: 'mention', updatedAt: '2026-09-01T10:00:00Z' })];
+ // The `all=true` listing never ends within the page cap.
+ mockGh((args) => (args.some((arg) => String(arg).includes('all=true'))
+ ? listing(inbox, { Link: '<https://api.github.com/notifications?all=true&page=2>; rel="next"' })
+ : listing(unreadOnly)));
+ expect(await syncAccountNotifications(account.id)).toMatchObject({ truncated: true });
+ const ids = async () => (await listNotifications({ accountId: account.id })).notifications.map((row) => row.threadId).sort();
+ expect(await ids()).toEqual(['151', '152']);
+
+ // What the earlier sync left behind: an unread thread marked done here
+ // without GitHub being told.
+ await query(
+ "UPDATE github_notifications SET state = 'done', unread = FALSE, state_changed_at = NOW() - INTERVAL '1 hour' WHERE account_id = $1 AND thread_id = '152'",
+ [account.id],
+ );
+ expect(await ids()).toEqual(['151']);
+ await syncAccountNotifications(account.id);
+ expect(await ids()).toEqual(['151', '152']);
});
it('treats a 304 as unchanged and keeps rows', async () => {
- let calls = 0;
- mockGh(() => {
- calls += 1;
- if (calls === 1) return listing([thread('201')]);
- throw Object.assign(new Error('gh: HTTP 304'), { ghExitCode: 1, ghStderr: 'gh: HTTP 304' });
- });
+ mockGh((args) => githubListing(args, [thread('201')]));
await syncAccountNotifications(account.id);
expect(await syncAccountNotifications(account.id)).toMatchObject({ changed: false });
expect((await listNotifications({ accountId: account.id })).notifications).toHaveLength(1);
diff --git a/server/services/githubNotifications.js b/server/services/githubNotifications.js
index 104547f154..f0fa064ac3 100644
--- a/server/services/githubNotifications.js
+++ b/server/services/githubNotifications.js
@@ -60,9 +60,9 @@ const syncFlightFor = (accountId) => {
};
// The stored Last-Modified cursor names the listing it came from. A cursor
-// saved by the older unread-only listing is not reused: a 304 against it would
-// skip the first read of the whole inbox until something new arrived.
-const LISTING_CURSOR_PREFIX = 'all:';
+// saved by an older sync format is not reused: a 304 against it would skip
+// the first full read until something new arrived.
+const LISTING_CURSOR_PREFIX = 'inbox:';
const listingCursor = (stored) => (typeof stored === 'string' && stored.startsWith(LISTING_CURSOR_PREFIX)
? stored.slice(LISTING_CURSOR_PREFIX.length) || null
: null);
@@ -82,9 +82,9 @@ async function requireAccountEnv(account) {
return env;
}
-async function fetchPage(account, env, page, ifModifiedSince) {
+async function fetchPage(account, env, { all, page, ifModifiedSince }) {
const args = ['api', '--hostname', account.host, '--include', '--method', 'GET',
- `notifications?all=true&per_page=${PER_PAGE}&page=${page}`];
+ `notifications?${all ? 'all=true&' : ''}per_page=${PER_PAGE}&page=${page}`];
if (ifModifiedSince) args.push('-H', `If-Modified-Since: ${ifModifiedSince}`);
const output = await execGh(args, GH_TIMEOUT_MS, { env }).catch((error) => {
if (ifModifiedSince && isNotModified(error)) return null;
@@ -101,6 +101,28 @@ async function fetchPage(account, env, page, ifModifiedSince) {
return { notModified: false, threads, headers: parsed.headers };
}
+/**
+ * Every page of one listing, up to MAX_PAGES. GitHub answers a conditional
+ * request that is not a 304 with only the threads updated since the cursor,
+ * so `complete` (the whole listing, safe to reconcile against) needs an
+ * unconditional, untruncated read.
+ */
+async function fetchListing(account, env, { all, ifModifiedSince = null }) {
+ let page = await fetchPage(account, env, { all, page: 1, ifModifiedSince });
+ if (page.notModified) return { notModified: true };
+ const { headers } = page;
+ const threads = [...page.threads];
+ let pageNumber = 1;
+ while (linkHeaderHasNext(page.headers.link) && pageNumber < MAX_PAGES) {
+ pageNumber += 1;
+ page = await fetchPage(account, env, { all, page: pageNumber, ifModifiedSince });
+ if (page.notModified) break;
+ threads.push(...page.threads);
+ }
+ const truncated = page.notModified || linkHeaderHasNext(page.headers.link);
+ return { notModified: false, threads, headers, truncated, complete: !ifModifiedSince && !truncated };
+}
+
const UPSERT_SQL = `INSERT INTO github_notifications (
account_id, thread_id, repo_full_name, repo_owner, repo_private, reason, subject_type,
subject_title, subject_api_url, html_url, unread, gh_updated_at, latest_comment_api_url
@@ -118,7 +140,12 @@ const UPSERT_SQL = `INSERT INTO github_notifications (
unread = EXCLUDED.unread,
-- New activity brings back a thread marked done, and an unsubscribed one
-- (GitHub re-subscribes on a fresh mention or review request). Muted stays.
+ -- An unread thread is in GitHub's inbox (marking one done reads it), so it
+ -- is not done here, unless that state was set after this listing was read.
state = CASE WHEN github_notifications.state IN ('done', 'unsubscribed') AND EXCLUDED.gh_updated_at > github_notifications.gh_updated_at
+ THEN 'open'
+ WHEN github_notifications.state = 'done' AND EXCLUDED.unread
+ AND (github_notifications.state_changed_at IS NULL OR github_notifications.state_changed_at < $14)
THEN 'open' ELSE github_notifications.state END,
gh_updated_at = EXCLUDED.gh_updated_at,
updated_at = NOW()`;
@@ -148,11 +175,36 @@ async function runAutomation(account, env, automate) {
return { ...assist, handedOff: autopilot.handedOff, agentRuns: autopilot.agentRuns };
}
+const listedIds = (listing) => listing.threads.map((thread) => String(thread?.id));
+
+// Only a complete listing says anything about the threads it does not name.
+async function reconcileListings(client, accountId, { inbox, unread }) {
+ if (unread.complete) {
+ await client.query(
+ `UPDATE github_notifications SET unread = FALSE, updated_at = NOW()
+ WHERE account_id = $1 AND unread AND NOT (thread_id = ANY($2::text[]))`,
+ [accountId, listedIds(unread)],
+ );
+ }
+ // GitHub's `all=true` listing still includes threads marked done (as
+ // read), so only a thread missing from the whole listing is known gone.
+ if (inbox.complete) {
+ await client.query(
+ `UPDATE github_notifications SET unread = FALSE,
+ state = CASE WHEN state = 'open' THEN 'done' ELSE state END,
+ state_changed_at = CASE WHEN state = 'open' THEN NOW() ELSE state_changed_at END, updated_at = NOW()
+ WHERE account_id = $1 AND (unread OR state = 'open') AND NOT (thread_id = ANY($2::text[]))`,
+ [accountId, listedIds(inbox)],
+ );
+ }
+}
+
async function syncAccountNow(account, automate) {
const env = await requireAccountEnv(account);
const cursor = (await query('SELECT last_modified FROM github_notification_sync WHERE account_id = $1', [account.id])).rows[0];
- const first = await fetchPage(account, env, 1, listingCursor(cursor?.last_modified));
- if (first.notModified) {
+ const listedAt = new Date();
+ const inbox = await fetchListing(account, env, { all: true, ifModifiedSince: listingCursor(cursor?.last_modified) });
+ if (inbox.notModified) {
await query('UPDATE github_notification_sync SET last_sync_at = NOW(), last_error = \'\', updated_at = NOW() WHERE account_id = $1', [account.id]);
// Threads past the last run's triage cap still need a decision.
const triage = await triageAccountNotifications(account, env);
@@ -160,42 +212,34 @@ async function syncAccountNow(account, automate) {
if (triage.triaged || assist.sorted || assist.acted || assist.handedOff) githubNotificationEvents.emit('changed', { accountId: account.id });
return { accountId: account.id, changed: false, fetched: 0, triage, assist };
}
- const threads = [...first.threads];
- let page = first;
- let pageNumber = 1;
- while (linkHeaderHasNext(page.headers.link) && pageNumber < MAX_PAGES) {
- pageNumber += 1;
- page = await fetchPage(account, env, pageNumber, null);
- threads.push(...page.threads);
+ // The `all=true` listing can run past MAX_PAGES and is a delta after the
+ // first read; the unread listing is small and read whole, so every unread
+ // thread reaches the inbox and `unread` mirrors GitHub.
+ const unread = inbox.complete
+ ? { threads: inbox.threads.filter((thread) => thread?.unread === true), truncated: false, complete: true }
+ : await fetchListing(account, env, { all: false });
+ const byId = new Map();
+ for (const thread of [...inbox.threads, ...unread.threads]) {
+ const record = toNotificationRecord(thread, account.host);
+ if (record) byId.set(record.threadId, record);
}
- const truncated = linkHeaderHasNext(page.headers.link);
- const records = threads.map((thread) => toNotificationRecord(thread, account.host)).filter(Boolean);
+ const records = [...byId.values()];
+ const truncated = inbox.truncated || unread.truncated;
await withTransaction(async (client) => {
for (const r of records) {
await client.query(UPSERT_SQL, [
account.id, r.threadId, r.repoFullName, r.repoOwner, r.repoPrivate, r.reason, r.subjectType,
- r.subjectTitle, r.subjectApiUrl, r.htmlUrl, r.unread, r.ghUpdatedAt, r.latestCommentApiUrl,
+ r.subjectTitle, r.subjectApiUrl, r.htmlUrl, r.unread, r.ghUpdatedAt, r.latestCommentApiUrl, listedAt,
]);
}
- // The listing is the whole GitHub inbox, read and unread (`all=true`); a
- // thread that dropped out was marked done on GitHub. A truncated listing
- // proves nothing about the rest.
- if (!truncated) {
- await client.query(
- `UPDATE github_notifications SET unread = FALSE,
- state = CASE WHEN state = 'open' THEN 'done' ELSE state END,
- state_changed_at = CASE WHEN state = 'open' THEN NOW() ELSE state_changed_at END, updated_at = NOW()
- WHERE account_id = $1 AND (unread OR state = 'open') AND NOT (thread_id = ANY($2::text[]))`,
- [account.id, records.map((r) => r.threadId)],
- );
- }
+ await reconcileListings(client, account.id, { inbox, unread });
await client.query(
`INSERT INTO github_notification_sync (account_id, last_modified, poll_interval_seconds, last_sync_at, last_error, updated_at)
VALUES ($1, $2, $3, NOW(), '', NOW())
ON CONFLICT (account_id) DO UPDATE SET last_modified = EXCLUDED.last_modified,
poll_interval_seconds = EXCLUDED.poll_interval_seconds, last_sync_at = NOW(), last_error = '', updated_at = NOW()`,
- [account.id, first.headers['last-modified'] ? `${LISTING_CURSOR_PREFIX}${first.headers['last-modified']}` : null, pollIntervalSeconds(first.headers['x-poll-interval'])],
+ [account.id, inbox.headers['last-modified'] ? `${LISTING_CURSOR_PREFIX}${inbox.headers['last-modified']}` : null, pollIntervalSeconds(inbox.headers['x-poll-interval'])],
);
});
const triage = await triageAccountNotifications(account, env);