refactor(gateway): close GatewaySession proxy gap; ban reach-through (#570)#571
Merged
Conversation
…570) Adds ConversationId to the GatewaySession proxy surface, migrates every production reach-through (~25 sites) to the proxy, and adds an architecture fence preventing regression. Root cause: GatewaySession proxied 14 fields of the inner Session record but ConversationId was missing. Every caller that needed conversation-id read/ write had to dive through `session.Session.ConversationId`. Once a few sites did that, the same shape spread to fields that DID have proxies (laziness). The damage: reach-through writes bypass GatewaySessionRuntime — its lock, the stream replay buffer, the secret redactor. PR #540 added a fence for the History case; this PR generalises the defence. Changes: - src/domain/BotNexus.Domain/Gateway/Models/GatewaySession.cs: - Added ConversationId get/set proxy (the missing facade). - Deleted dead ToSession() (zero callers). - Rewrote XML doc on FromSession() to be meaningful. - 18 production files migrated from `session.Session.X` to `session.X`: - src/extensions/BotNexus.Extensions.Channels.SignalR/GatewayHub.cs (4) - src/gateway/BotNexus.Gateway.Api/Controllers/ChannelHistoryController.cs (5) - src/gateway/BotNexus.Gateway.Api/Controllers/ConversationsController.cs (1) - src/gateway/BotNexus.Gateway.Api/Controllers/CrossWorldFederationController.cs (2) - src/gateway/BotNexus.Gateway.Api/Controllers/SessionsController.cs (1) - src/gateway/BotNexus.Gateway.Api/Triggers/CronTrigger.cs (3) - src/gateway/BotNexus.Gateway.Api/Triggers/HeartbeatTrigger.cs (1) - src/gateway/BotNexus.Gateway.Conversations/DefaultConversationRouter.cs (3) - src/gateway/BotNexus.Gateway.Sessions/FileSessionStore.cs (1) - src/gateway/BotNexus.Gateway.Sessions/SessionStoreBase.cs (1) - src/gateway/BotNexus.Gateway.Sessions/SqliteSessionStore.cs (1) - src/gateway/BotNexus.Gateway/Agents/AgentExchangeService.cs (2) - src/gateway/BotNexus.Gateway/Agents/DefaultSubAgentManager.cs (1) - src/gateway/BotNexus.Gateway/Agents/WorkspaceContextBuilder.cs (1) - src/gateway/BotNexus.Gateway/Conversations/DefaultConversationResetService.cs (2) - src/gateway/BotNexus.Gateway/GatewayHost.cs (8) - src/gateway/BotNexus.Gateway/Isolation/InProcessIsolationStrategy.cs (1) - src/gateway/BotNexus.Gateway/Tools/ConversationTool.cs (3) - False-positive fix (rubber-duck pre-impl review): - ChannelHistoryController.HistorySlice.Session -> .GatewaySession (record property name collided with the banned shape; 5 callsites migrated) - CrossWorldFederationController.ResolveResult.Session -> .GatewaySession (same shape — caught at the verification grep, not pre-impl review; 3 callsites migrated) Tests: - New tests/architecture/BotNexus.Architecture.Tests/GatewaySessionFacadeArchitectureTests.cs: - Main fence: 1 (no production file matches `\.Session\b\s*!?\s*\.\s*\w+` outside the allowlist of GatewaySession.cs and GatewaySessionRuntime.cs). - Vacuity guard: 1 (synthetic reach-through MUST trip). - Null-forgiving operator pin: 1 (session!.Session!.X must still match). - False-positive guards: 4 (.Session as bare argument, object initializer, Sessions/SessionStore/SessionId word-boundary, comment mentions). - Comment stripper: identical state-machine lexer from SingleShotWireValueArchitectureTests (PR #569) — preserves strings/chars. - tests/gateway/BotNexus.Gateway.Tests/GatewaySessionBehaviorSnapshotTests.cs: - ConversationId_RoundTripsThroughProxy_ReadingInnerRecord (set via proxy, assert visible on inner record for persistence). - ConversationId_ProxyGetter_ReflectsInnerRecordChanges (set on inner, assert proxy getter reads through — defends against cached-field regression). - ConversationId_ProxyAcceptsNull_ForOrphanSessions (null tolerance). Argument-passing of `session.Session` (e.g. _memoryFlusher.FlushAsync(..., session.Session, ...)) is intentionally NOT banned — the receiver takes the Session value record by design (persistence-layer signatures). The fence regex requires `.Session.<identifier>`, so bare-argument shapes don't match. Out of scope (separate PRs): - Thinning the 8 stream-replay proxies (NextSequenceId, StreamEventLog, ReplayBuffer, AllocateSequenceId, AddStreamEvent, GetStreamEventsAfter, GetStreamEventSnapshot, SetStreamReplayState) — only FileSessionStore (3 sites) + tests use them. - Migrating HeartbeatTrigger.ReplaceHistory to TryReplaceHistoryFromSnapshot — behaviour change (race safety), separate PR. Build clean: 0 warnings, 0 errors under TreatWarningsAsErrors. Tests: Gateway.Tests 1886/0/1 (+3 ConversationId proxy pins), Architecture.Tests 76 -> 83 (+7 fence + guards). Closes #570. Refs #523. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #570. Refs #523.
Adds the missing
ConversationIdproxy toGatewaySession, migrates every production reach-through (session.Session.X) to the proxy (25 sites across 18 files), and adds an architecture fence preventing regression.Root cause
GatewaySessionproxied 14 fields of the innerSessionrecord butConversationIdwas missing. Every caller that needed conversation-id read/write had to dive throughsession.Session.ConversationId. Once a few sites did that, the same shape spread to fields that DID have proxies (laziness pattern).The damage: reach-through writes bypass
GatewaySessionRuntime— its lock, the stream replay buffer, the secret redactor. PR #540 added aThreadSafeHistoryArchitectureTestsfence for theHistorycase; this PR generalises the defence to every other proxied field.Changes
Domain — proxy facade
GatewaySession.cs: addedConversationIdget/set proxy (the missing facade). Deleted deadToSession()(zero callers). RewroteFromSession()XML doc to be meaningful.Production — reach-through migration (18 files, 25 sites)
All
session.Session.X→session.X:GatewayHost.csGatewayHub.csChannelHistoryController.cs,ConversationsController.cs,CrossWorldFederationController.cs,SessionsController.csCronTrigger.cs,HeartbeatTrigger.csDefaultConversationRouter.cs,DefaultConversationResetService.csFileSessionStore.cs,SessionStoreBase.cs,SqliteSessionStore.csAgentExchangeService.cs,DefaultSubAgentManager.cs,WorkspaceContextBuilder.csInProcessIsolationStrategy.cs,ConversationTool.csFalse-positive renames (caught pre/at-impl)
ChannelHistoryController.HistorySlice.Session(typeGatewaySession) — the record property name collided with the banned shape. Renamed toHistorySlice.GatewaySession(5 callsites migrated).CrossWorldFederationController.ResolveResult.Sessionhad the same shape. Renamed toResolveResult.GatewaySession(3 callsites migrated). Filed in commit; mention this here because it surfaced after the rubber-duck pass.Architecture fence —
GatewaySessionFacadeArchitectureTests(7 tests)Main fence (1): source-scans
src/**/*.csfor\.Session\b\s*!?\s*\.\s*\w+outside a 2-entry allowlist (GatewaySession.cs,GatewaySessionRuntime.cs— the proxy and its runtime).Vacuity guard (1): synthetic
session.Session.ConversationId = ...MUST trip the fence.Null-forgiving operator pin (1):
session!.Session!.UpdatedAtMUST also trip — realistic shape, regex permits[!]?.False-positive guards (4):
.Sessionas bare argument (FlushAsync(..., session.Session, ...)) — not banned (receiver takes value record by design).new GatewaySession { Session = inner },gs with { Session = updated }) — not banned.Sessions.X/SessionStore.X/SessionId.X—\bword boundary discriminates.//) — comment stripper removes before regex.Comment-stripping lexer: identical state-machine from
SingleShotWireValueArchitectureTests(PR chore(gateway): rename single-shot CompletionReason "objectiveMet" to "singleShot" (#552) #569) — handles regular strings, verbatim strings (@"…"), char literals.Behavior pins —
GatewaySessionBehaviorSnapshotTests(3 tests)ConversationId_RoundTripsThroughProxy_ReadingInnerRecord— set via proxy, assert visible on inner record (persistence sees it).ConversationId_ProxyGetter_ReflectsInnerRecordChanges— set on inner, assert proxy reads through (defends against cached-field regression).ConversationId_ProxyAcceptsNull_ForOrphanSessions— null tolerance for orphan / legacy sessions.Intentional non-bans
Passing
session.Sessionas a bare argument to a method that takesSessionis not banned — the receiver gets the value record by design (e.g._memoryFlusher.FlushAsync(..., session.Session, options, ct)atGatewayHost.cs:454, persistence-layer signatures). The fence regex requires.Session.<identifier>(an access chain after the dot), so bare-argument shapes don't match.Out of scope (separate PRs)
NextSequenceId,StreamEventLog,ReplayBuffer,AllocateSequenceId,AddStreamEvent,GetStreamEventsAfter,GetStreamEventSnapshot,SetStreamReplayState). OnlyFileSessionStore(3 sites) and tests use them — separate cleanup PR.HeartbeatTrigger.ReplaceHistory→TryReplaceHistoryFromSnapshotmigration. Behavior change (race-safety), separate PR.Known fence gaps (documented, not supported v1)
(session.Session).ConversationId— won't match. Realistic but unusual; future PR can extend if needed.var inner = session.Session; inner.X = …— won't match.Neither pattern appears anywhere in current
src/. Adding regex variants for them is scope creep until a real call site emerges.Validation
dotnet build BotNexus.slnx --nologo --tl:off): 0 warnings, 0 errors underTreatWarningsAsErrors.dotnet test BotNexus.slnx --nologo --tl:off --no-build):[Skip]flags unchanged.Multi-model critique sweep (autopilot policy)
Per user directive, 3 critique agents reviewed this PR before merge:
gpt-5.5claude-opus-4.7ToSession()deletion verified, 1 LOW (plan-deviation note, folded into "Out of scope" above)gpt-5.3-codexNo HIGH/MEDIUM findings to fold in.
Verification grep
After all edits,
grep -nE '\.Session\b\s*!?\s*\.\s*\w+' src/**/*.csreturns exactly one hit:GatewaySession.cs:84— inside the XML doc on the newConversationIdproxy, documenting the banned shape. Stripped by the lexer; file is allowlisted anyway.