fix(warehouse): write Snowflake reconnects to the log

Merged
SA
2mo ago
+506-8

Issue for this PR

No tracking issue (searched open issues for "reconnect" and "log"). Found by the v0.12.5 post-release E2E. #1395 made Snowflake reopen sessions it had closed, but those reopens never reached opencode.log.

Type of change

  • Bug fix

What does this PR do?

When Snowflake closes a session (idle timeout, VPN drop, laptop sleep), the driver opens a new one and the statement carries on, so the user sees nothing. Until now opencode.log didn't show it either. A session that kept dropping looked the same as one that never did, which is the situation support needed to see for the field reports behind #1395.

  • Driver (packages/drivers/src/reconnect-events.ts):
    • a process-global reconnect event, following the existing browser sign-in notice pattern (Symbol.for listener set, because the driver and its subscriber can load through different module graphs);
    • snowflake.ts emits started, then reconnected or failed;
    • each event carries the reason (connection-down: the SDK reported it before a statement; statement-refused: a statement came back refused), the duration, the session settings restored, and whether temporary objects or a transaction were lost.
  • Log (reconnect-log.ts):
    • writes those events under the existing warehouse-connect service, as reconnecting, reconnected and reconnect failed (a warning);
    • names the connection when its account maps to exactly one connection; otherwise logs the account only;
    • masks the error the same way as connect failed.
  • Registry: installs the log subscriber before each connect, next to the sign-in notice.

No behaviour change to the reconnect itself; the events are emitted around it.

How did you verify your code works?

  • Live, real Snowflake account (key-pair), with a binary built from this branch:
    • In an altimate-code run session the agent ran USE SCHEMA; its session was then terminated from a separate connection with SYSTEM$ABORT_SESSION, and the next sql_execute went through.
    • opencode.log gained:
      message=reconnecting name=eastman_migration_snowflake type=snowflake account=… reason=statement-refused
      message=reconnected  name=eastman_migration_snowflake type=snowflake account=… reason=statement-refused duration_ms=821 settings_restored=1 session_state_lost=false
      
  • New tests:
    • snowflake-reconnect.test.ts: a session reported down, a refused statement, a reconnect that cannot restore the session, and lost temporary objects.
    • warehouse-reconnect-log.test.ts: the log lines, the masked error, and an account shared by two connections.
    • All 8 mutations are caught: no started event, no failed event, settings count dropped, state loss ignored, reasons swapped, subscription removed, masking removed, name always set.
  • Other checks:
    • Typecheck 13/13; marker guard clean; packages/drivers 367 pass.
    • Full packages/opencode suite: 16,798 pass, 8 fail.
      • 6 are the MCP tests that read the machine's ~/.claude.json (#1386).
      • 2 are run-process subprocess flakes: --trace (#1340) failed 1 of 2 reruns on its own; --format json passed both.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

🤖 Generated with Claude Code

https://claude.ai/code/session_018fJ3X7pcGT4R9yzjsJnqsV


Summary by cubic

Writes Snowflake session reconnects to opencode.log so a session that keeps dropping no longer looks identical to one that never does.

  • The reason statement-refused is now closed-during-statement: the SDK can raise the same error while polling an accepted statement, so "refused" could send someone to re-run a write that ran.
  • sessionStateLost is also true while a statement that can create temp objects or open a transaction is still running on the old session, since whether it did is not known yet.
  • A stateful statement is counted against the session it was sent to; one queued behind a reconnect no longer marks it, and one still running on an older session no longer marks a later reconnect.
  • For a failed reconnect, the event carries only the Snowflake error code without the replayed setting; the caller's own error still names the setting.
  • The process-global event absorbs a rejecting async listener instead of leaving an unhandled rejection.
  • registry.remove() drops the connection's name from the reconnect log, unless its connect is still in flight.
  • Reconnect behavior itself is unchanged.

Summary by CodeRabbit

  • New Features
    • Added visibility into Snowflake reconnection activity, including when a reconnect starts, succeeds, or fails, why it occurred, and how long it took.
    • Successful reconnect updates can include the number of restored settings and a notice when session state may have been lost.
    • Reconnect logs show connection details when they can be identified and mask sensitive error information.
    • Added an event listener API for applications to track reconnect activity.