Skip to content

fix(channels): record failed long-connection events and catch up missed group @ messages - #1400

Merged
zhaozhaozz merged 1 commit into
agent-CI-Infra:mainfrom
zhaozhaozz:fix/issue-1399-feishu-missed-events
Nov 26, 2026
Merged

zhaozhaozz merged 1 commit into
agent-CI-Infra:mainfrom
zhaozhaozz:fix/issue-1399-feishu-missed-events

Conversation

@zhaozhaozz

Copy link
Copy Markdown
Collaborator

Fixes #1399

Problem

A group message that @-mentioned the bot could be lost over the Feishu long connection without a trace. An exception from receiveLongConnection → accept → normalize went back to the SDK, whose logger was a no-op, and nothing was stored. History resync only ran when the connection state changed, so while the socket stayed connected (as it did through the 24.7 s stall in production) the lost message was never caught up or answered.

Change

Failures leave a trace (src/channels/service.ts, src/channels/connections.ts)

  • receiveLongConnection catches a throw from anywhere before or during accept and writes a failed inbox row with the error code. The payload holds only the event type, event id, message id and chat id, no message text. The row uses its own failed:<event_id> key, so a redelivery of the same event or a catch-up of the same message still goes through. It also sets the connector's last_error, logs one line with the event type, code and ids, catches up the event's chat when that chat is known, and rethrows, so the SDK still answers Feishu with 500.
  • The SDK logger sends warn/error to the log as text only: errors by their message, other objects as [object], with every credential the deployment knows redacted (publicText + configuredRedactor). debug/info/trace stay silent. At debug level the SDK prints whole events and the socket URL.

Missed messages are caught up (service.ts, ledger.ts, history-sync.ts)

  • ChannelService.syncTick (the existing 1 s timer) calls catchUp for bound groups every 60 s and right after a tick runs ≥ 5 s late (a blocked event loop). After a stall it runs again 30 s later, once messages sent during the stall are old enough to count as missed.
  • ChannelLedger.catchUp re-runs only finished jobs. The chat job re-reads from covered_until - 60 (incremental). Thread jobs re-read in full, but only for threads with a message in the last 15 minutes, because threads have no time bounds. Running, blocked and paused jobs are left alone.
  • ChannelHistorySync passes every page message to a recovery hook inside the page transaction. A message counts as missed when it is from a person in a bound group, @-mentions the bot, is 30 s to 1 h old, and has no message:<id> inbox row:
    • ≤ 5 min old (the live window): queued exactly like a live @ (event_id recovered:<id>, recovered: true), with the processing indicator, merged into a running turn when there is one. The answer starts with (or, for a rich card, has as its footer) a note that the message was recovered late.
    • older: recorded as completed / channel_message_missed only. It is in the ledger already, and nothing is answered.
    • Idempotency rides on the live path's unique message:<id> key: whichever of the live event and a sync comes first wins, and repeated syncs do nothing.
  • Completed sync ranges that overlap merge into one row, and the cursors of a finished generation are deleted, so a catch-up every minute does not grow channel_sync_ranges (read on every ledger.query via diagnostics).

Docs: docs/features/assistants-and-channels.md (behavior), docs/design/assistants-and-channels.md (mechanism).

Tests

New test/st/src/channels/channel-missed-events.test.mjs:

  • a handler throw through the real EventDispatcher writes the failed row (code, ids, no text), sets last_error, logs the line; a repeat failure keeps one row; the redelivery after the fix is accepted, not deduplicated;
  • SDK warn/error lines carry no request config or app secret; debug/info/trace print nothing;
  • a missed @ found by the catch-up starts exactly one turn, the reply says it is late, a second catch-up and the late live event add no turn and no reply;
  • a 10-minute-old missed @ is stored and recorded as channel_message_missed but not answered; > 1 h and < 30 s messages are not taken as missed;
  • stall/period scheduling (deterministic fake ticks), incremental window, a single range row after repeated catch-ups, unbound groups untouched.

test/st/src/channels/channel-ledger.test.mjs: the two "nothing committed before ACK" tests now expect the failed trace row and still assert that no accepted row or ledger message is committed.

Verification

Head c25bfbb0 (based on origin/main 2f2195a6), Node v22.19.0:

Command Exit Result
npm ci 0
(cd runner && npm ci) 0
npm run check 0 tsc + 854 runtime files
npm test 1 353 sets, 2855 tests, 2848 pass, 7 fail in 2 sets unrelated to channels (below)
TMPDIR=$PWD/.tmp/s node --test test/ut/manager/runner-transport.test.mjs 0 4/4: the full-run failure was EADDRINUSE because the Unix socket path under the deep worktree exceeds 108 bytes
node test/framework/test-sets.mjs test/st/src/harness/codex-harness.test.mjs 0 68/68: the full-run failure was a timing assertion with 14 sets in parallel

All channel sets passed in the full run, including channel-missed-events and channel-ledger.

…ed group @ messages

A Feishu long-connection event that threw before or while it was accepted
went back to the SDK and vanished: the SDK logger was a no-op and nothing was
stored. History resync only ran when the connection state changed, so a lost
@ message in a bound group was never answered.

- receiveLongConnection records a failure as a failed inbox row (error code,
  identifiers only, its own failed:<event_id> key so a redelivery or catch-up
  still goes through), sets the connector's last_error, logs the event type
  and code, and catches up the event's chat.
- The SDK logger passes warnings and errors to the log as redacted text;
  objects and debug output (event bodies, socket URL) are dropped.
- Bound groups sync their recent history every minute, at once after the
  event loop was blocked for 5 s or more, and again 30 s later. Chat jobs
  re-read from just before the end they covered; threads with a message in
  the last 15 minutes are re-read whole.
- History sync hands each message to a recovery hook in the page's
  transaction. A person's @ of the bot with no inbox row, 30 s to 5 minutes
  old, is queued as if it had arrived live (processing indicator, one turn,
  idempotent on the message's inbox key) and the reply says it was recovered
  late; one up to an hour old is only recorded (channel_message_missed).
- Completed sync ranges that overlap merge into one row and spent cursors are
  removed, so the periodic catch-up adds no rows per run.

Fixes agent-CI-Infra#1399
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Reviewed head c25bfbb05ed6c8678a59e9241ced78897361892f (base 2f2195a6, one linear commit). No blocking issues found.

What I verified against the source:

  • Failed events leave a trace. receiveLongConnection records a failed inbox row under its own failed:<event_id> key (event type and identifiers only, sealed), sets the connector last_error, logs one line and rethrows, so the SDK still answers 500 while a redelivery or a later catch-up of the same message is not blocked by the failure row. The SDK logger keeps only warn/error text (errors by message, other objects dropped, deployment credentials redacted) and drops debug/info/trace, which at the pinned SDK version carry event bodies and the socket URL.
  • Catch-up. syncTick re-arms finished sync jobs for bound groups every 60 s, at once after a tick that ran ≥ 5 s late, and again 30 s later. The chat job re-reads from covered_until - 60; thread jobs are re-read only for threads with a message in the last 15 min and only when complete, so blocked/permanent failures are not retried each minute. Finished ranges and their cursors merge into one row, so the periodic catch-up does not grow channel_sync_ranges or cursor rows.
  • Recovery. A person's @ of the bot in a bound group with no message:<id> inbox row is queued as if it had just arrived when it is 30 s–5 min old (event_id recovered:<id>, processing indicator, merged into a running turn when there is one) and only recorded as completed/channel_message_missed between 5 min and 1 h. The live message:<id> key makes this idempotent against a late live event, a redelivery and repeated syncs, and the reply (or the rich card footer) says the message was recovered late. p2p chats, unbound groups, non-user senders, non-@ messages and messages outside the windows start nothing.
  • Tests and docs. test/st/src/channels/channel-missed-events.test.mjs is discovered by the default test/st plan and covers the failure row and redelivery, log redaction, one-turn recovery with the late note and indicator, the missed/too-old/too-new boundaries and the stall/period scheduling and range merging; the two channel-ledger ACK assertions now expect only the failure trace. Both docs dimensions (docs/features/, docs/design/assistants-and-channels.md) describe the new behaviour.
  • Checks. All required checks pass on this head (unit shards, e2e, the four image builds; CodeQL too). The first run's unit shard 2 failed only in acp-harness ("timed out waiting for ACP fixture"), unrelated to this diff and green on the re-run.

Non-blocking notes (nothing to change before merge):

  • Identity commands are not filtered out of recovery; they all require a p2p chat, so a recovered group message only gets the usual private-chat-required error, but excluding the command patterns would state the intent explicitly.
  • The issue review suggested an ingest-failure count on the connector health panel. The failed row is visible in 收发记录 and last_error carries the code, while the health counters still count only queued inbox rows and failed outbox rows.

Enqueued into the merge queue with expectedHeadOid set to this reviewed head.

@agent-ci-infra-bot agent-ci-infra-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit: c25bfbb
Coverage: PR #1400; 9 changed file(s)

Comment thread src/channels/service.ts
m.reply_target?revalidateReplyTarget(this.store,chat.id,m.reply_target,{projects:[bindingScope(binding.id)],fence:m.fence}):undefined)});
const replyTarget=result.reply_target?revalidateReplyTarget(this.store,chat.id,result.reply_target,{projects:[bindingScope(binding.id)],fence:result.fence}):undefined;
// A message recovered after its live event was lost (#1399) is answered late: the answer says so.
const late=e.recovered?recoveredNote:undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — the “recovered late” note is lost when the missed message is merged into a turn that is already running.

recover merges a fresh missed message into the active turn (this.turnMessages.merge(...), src/channels/service.ts:307), which sets its inbox row to merged and hands it to the model only as a generic incoming_during_turn entry (src/channels/turn-messages.ts:161-168). Nothing marks that entry as recovered: describe() has no recovered/late field, and the merged message never becomes a turn source, so late here is computed only from the answered turn’s own source event and the final answer carries neither the recoveredNote footer nor the text prefix.

That contradicts the PR body (“merged into a running turn when there is one. The answer starts with … a note that the message was recovered late”) and docs/design/assistants-and-channels.md (“5 分钟内的按实时路径入队 … 最终回复带补收说明”), and it is exactly the #1399 shape: the live event was lost while a long turn was running, so the recovered message is the one that gets merged.

Options: (a) keep the promise — expose the flag on merged inputs (e.g. a recovered: true field in describe() and/or appending recoveredNote when the turn consumed a recovered input); (b) narrow the claim in the PR body and the design doc to “when the recovered message starts its own turn”. (a) preserves the promised UX; (b) is cheapest but leaves the person unaware their message was answered late.

Current coverage: test/st/src/channels/channel-missed-events.test.mjs case 3 only exercises the no-running-turn path, so neither behaviour is pinned today.

@agent-ci-infra-bot

Copy link
Copy Markdown
Contributor

Review — record failed long-connection events and catch up missed group @ messages

Reviewed head c25bfbb0 against base 2f2195a6 (9 files, +308/−22) and the PR discussion (no earlier review threads on this PR).

Claims checked against the diff

  • Failure trace: receiveLongConnection now catches anything thrown by connector/accept, records a failed inbox row (key = failed:<event_id>, payload = event type + ids only), sets the connector last_error, logs one line, catches up the event's chat, and rethrows so the SDK answers 500. The row's own key leaves message:<id>/card:<id> dedup intact, so a redelivery or a later sync of the same message is still accepted — matches the new dispatcher-level test.
  • SDK logging: only warn/error, text-only (objects → [object], errors by message), publicText + configuredRedactor, 500-char cap; debug/info/trace are no-ops, so the debug line that printed whole events and the socket URL is gone.
  • Cadence: syncTick runs catchUp every 60 s and immediately when a tick lands ≥5 s late, then again 30 s later so messages from the stall clear the 30 s grace. ChannelLedger.catchUp resets only complete jobs (chat job from covered_until − 60, threads only when a message exists in the last 15 min); running/blocked/paused jobs keep their state, so a permanent history error is not retried every minute. Overlapping ranges merge and finished-generation cursors are dropped, so a per-minute catch-up does not grow channel_sync_ranges.
  • Recovery filter: group + person + is_self mention + 30 s–1 h + no message:<id> row + active binding + enabled connector. ≤5 min → queued, processing reaction, recovered: true; older → completed/channel_message_missed, nothing answered. One row per message whichever of the live event and a sync arrives first.
  • Docs: docs/features/assistants-and-channels.md and docs/design/assistants-and-channels.md describe what the code does, and static/channels/transfer.js really renders the failed row as failed · <code> in 收发记录.

Verification I ran in this workspace at PR head (node v22.23.5): npm ci exit 0; npx tsc -p tsconfig.json exit 0; node --test test/st/src/channels/channel-missed-events.test.mjs exit 0 (new cases pass); node --test test/st/src/channels/channel-ledger.test.mjs exit 0 (23/23); all test/st/src/channels sets exit 0 (350 tests, 338 pass, 12 skipped, 0 fail); test/ut/src/channels exit 0 (28/28). I did not run the full sharded npm test.

Findings: one suggestion (F1, inline) — the “recovered late” note is dropped when the recovered message is merged into a turn that is already running, which is the shape of the #1399 incident and contradicts the PR body and the design doc. No must-fix, no security or data-loss issue found.

Non-blocking observations (not findings): a lost @ that is the first reply of a thread, or that lands in a thread with no message in the last 15 minutes, is never recovered — the accepted cost of the 15-minute thread filter; and each active thread is re-read from the start every minute, bounded by the existing 10-jobs-per-tick sync budget.

Recommendation: merge once F1 is either implemented (mark merged recovered inputs as recovered / add the note to a turn that consumed one) or the PR body and design sentence are narrowed to “when the recovered message starts its own turn”.

Reviewed commit: c25bfbb
Coverage: PR #1400; 9 changed file(s)

Merged via the queue into agent-CI-Infra:main with commit 0fc5e3a Nov 26, 2026
14 of 16 checks passed
@zhaozhaozz
zhaozhaozz deleted the fix/issue-1399-feishu-missed-events branch November 26, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feishu long connection: a missed group @ message leaves no trace and is never caught up

1 participant