Repository navigation
fix(telemetry): deduplicate session_started and model switch events #3499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d55283c
fix(telemetry): deduplicate session_started and model switch events
7Sageer 88b4705
fix(telemetry): keep engine session_started for direct v2 SDK clients…
7Sageer 3d92150
fix(telemetry): preserve model_switch when activation rebinds the sam…
7Sageer 002556c
fix(telemetry): route TUI reload through harness
7Sageer 280633d
test: update /reload message-flow test for harness reload route
7Sageer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { | ||
| AuthFlowController, | ||
| type AuthFlowHost, | ||
| } from '#/tui/controllers/auth-flow'; | ||
|
|
||
| function makeHost( | ||
| options: { | ||
| engineV2?: boolean; | ||
| withSession?: boolean; | ||
| defaultModel?: string; | ||
| } = {}, | ||
| ) { | ||
| const appState = { | ||
| workDir: '/tmp/work', | ||
| additionalDirs: [] as string[], | ||
| planMode: false, | ||
| model: 'old-model', | ||
| thinkingEffort: 'off', | ||
| }; | ||
| const session = | ||
| options.withSession === true | ||
| ? { | ||
| id: 'ses-live', | ||
| setModel: vi.fn(async () => ({ model: 'k2', providerName: 'managed' })), | ||
| setThinking: vi.fn(async () => {}), | ||
| } | ||
| : undefined; | ||
| const host = { | ||
| state: { appState }, | ||
| session, | ||
| engineV2: options.engineV2 === true, | ||
| harness: { | ||
| createSession: vi.fn(async () => ({ id: 'ses-new', summary: { title: null } })), | ||
| getConfig: vi.fn(async () => ({ | ||
| defaultModel: options.defaultModel, | ||
| models: { k2: { provider: 'managed:kimi-code', model: 'kimi-k2', maxContextSize: 200_000 } }, | ||
| providers: {}, | ||
| })), | ||
| }, | ||
| options: { startup: {} }, | ||
| setAppState: vi.fn((patch: Record<string, unknown>) => Object.assign(appState, patch)), | ||
| setStartupReady: vi.fn(), | ||
| resetSessionRuntime: vi.fn(), | ||
| setSession: vi.fn(async (next: unknown) => { | ||
| (host as { session: unknown }).session = next; | ||
| }), | ||
| syncRuntimeState: vi.fn(async () => {}), | ||
| appendStartupNotice: vi.fn(), | ||
| hydrateLazyConfigDefaults: vi.fn(async () => {}), | ||
| sessionEventHandler: { startSubscription: vi.fn() }, | ||
| fetchSessions: vi.fn(async () => {}), | ||
| updateTerminalTitle: vi.fn(), | ||
| refreshSkillCommands: vi.fn(async () => {}), | ||
| refreshPluginCommands: vi.fn(async () => {}), | ||
| } as unknown as AuthFlowHost & { | ||
| session: unknown; | ||
| harness: { | ||
| createSession: ReturnType<typeof vi.fn>; | ||
| getConfig: ReturnType<typeof vi.fn>; | ||
| }; | ||
| setAppState: ReturnType<typeof vi.fn>; | ||
| }; | ||
| return { host, appState, session }; | ||
| } | ||
|
|
||
| describe('activateModelAfterLogin', () => { | ||
| it('applies through setModel and reports a live session when one exists', async () => { | ||
| const { host, session } = makeHost({ withSession: true }); | ||
| const authFlow = new AuthFlowController(host); | ||
|
|
||
| const reachedLiveSession = await authFlow.activateModelAfterLogin('k2', 'high'); | ||
|
|
||
| expect(reachedLiveSession).toBe(true); | ||
| expect(session!.setModel).toHaveBeenCalledWith('k2'); | ||
| expect(session!.setThinking).toHaveBeenCalledWith('high'); | ||
| }); | ||
|
|
||
| it('only patches app state and reports no live session on the session-less v2 path', async () => { | ||
| const { host, appState } = makeHost({ engineV2: true }); | ||
| const authFlow = new AuthFlowController(host); | ||
|
|
||
| const reachedLiveSession = await authFlow.activateModelAfterLogin('k2', 'high'); | ||
|
|
||
| expect(reachedLiveSession).toBe(false); | ||
| expect(host.harness.createSession).not.toHaveBeenCalled(); | ||
| expect(appState.model).toBe('k2'); | ||
| expect(appState).toMatchObject({ lazySessionThinking: 'high' }); | ||
| }); | ||
|
|
||
| it('creates the session on the session-less v1 path and still reports no live session', async () => { | ||
| const { host } = makeHost(); | ||
| const authFlow = new AuthFlowController(host); | ||
|
|
||
| // The v1 creation binds the model without an engine model_switch event, | ||
| // so callers must treat this path as "no live session reached" even | ||
| // though host.session is defined afterwards. | ||
| const reachedLiveSession = await authFlow.activateModelAfterLogin('k2', 'high'); | ||
|
|
||
| expect(reachedLiveSession).toBe(false); | ||
| expect(host.harness.createSession).toHaveBeenCalledWith( | ||
| expect.objectContaining({ model: 'k2', thinking: 'high' }), | ||
| ); | ||
| expect(host.session).toMatchObject({ id: 'ses-new' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('refreshConfigAfterLogin', () => { | ||
| it('reports false without activating when no default model is configured', async () => { | ||
| const { host } = makeHost({ withSession: true }); | ||
| const authFlow = new AuthFlowController(host); | ||
|
|
||
| const reachedLiveSession = await authFlow.refreshConfigAfterLogin(); | ||
|
|
||
| expect(reachedLiveSession).toBe(false); | ||
| }); | ||
|
|
||
| it('propagates the activation result for the persisted default model', async () => { | ||
| const live = makeHost({ withSession: true, defaultModel: 'k2' }); | ||
| const reachedLive = await new AuthFlowController(live.host).refreshConfigAfterLogin(); | ||
| expect(reachedLive).toBe(true); | ||
| expect(live.session!.setModel).toHaveBeenCalledWith('k2'); | ||
|
|
||
| const lazy = makeHost({ engineV2: true, defaultModel: 'k2' }); | ||
| const reachedLazy = await new AuthFlowController(lazy.host).refreshConfigAfterLogin(); | ||
| expect(reachedLazy).toBe(false); | ||
| expect(lazy.host.harness.createSession).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a session-less v1 user selects a model with a session-only effort such as
xhigh,refreshConfigAfterLogin()creates the session with that model and correctly returnsfalsebecause creation emits nomodel_switch; the second activation then sees the newly created session and returnstrue, but itssetModel(alias)is a no-op because that alias is already bound, so the engine emits nothing. OR-ing that result suppresses the TUI event as well, leaving this selection with zeromodel_switchrecords; preserve the creation-path result unless the second activation actually changed the model.Useful? React with 👍 / 👎.