Skip to content

internal: create cloud studio session ID to group telemetry and errors #31833

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 3 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/app/src/runner/SpecRunnerOpenMode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<StudioPanel
v-if="shouldShowStudioPanel"
data-cy="studio-panel"
:cloud-studio-session-id="studioStore.cloudStudioSessionId"
:can-access-studio-a-i="studioStore.canAccessStudioAI"
:on-studio-panel-close="handleStudioPanelClose"
:event-manager="eventManager"
Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,29 @@ export class EventManager {
this.studioStore.setSuiteId(suiteId)
this.studioStore.setShowUrlPrompt(showUrlPrompt)

this.ws.emit('studio:init', ({ canAccessStudioAI, error }) => {
this.ws.emit('studio:init', ({ canAccessStudioAI, cloudStudioSessionId, error }) => {
if (error) {
// eslint-disable-next-line no-console
console.error(error)
}

this.studioStore.setCanAccessStudioAI(canAccessStudioAI)
this.studioStore.setCloudStudioSessionId(cloudStudioSessionId)
studioInit()
})
}

this.reporterBus.on('studio:init:test', (testId) => {
this.studioStore.setTestId(testId)

this.ws.emit('studio:init', ({ canAccessStudioAI, error }) => {
this.ws.emit('studio:init', ({ canAccessStudioAI, cloudStudioSessionId, error }) => {
if (error) {
// eslint-disable-next-line no-console
console.error(error)
}

this.studioStore.setCanAccessStudioAI(canAccessStudioAI)
this.studioStore.setCloudStudioSessionId(cloudStudioSessionId)
studioInit()
})
})
Expand Down
6 changes: 6 additions & 0 deletions packages/app/src/store/studio-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ interface StudioRecorderState {

canAccessStudioAI: boolean
showUrlPrompt: boolean
cloudStudioSessionId?: string
}

export const useStudioStore = defineStore('studioRecorder', {
Expand All @@ -138,6 +139,7 @@ export const useStudioStore = defineStore('studioRecorder', {
_currentId: 1,
canAccessStudioAI: false,
showUrlPrompt: true,
cloudStudioSessionId: undefined,
}
},

Expand All @@ -161,6 +163,10 @@ export const useStudioStore = defineStore('studioRecorder', {
this.canAccessStudioAI = canAccessStudioAI
},

setCloudStudioSessionId (cloudStudioSessionId: string) {
this.cloudStudioSessionId = cloudStudioSessionId
},

clearRunnableIds () {
this.testId = undefined
this.suiteId = undefined
Expand Down
8 changes: 7 additions & 1 deletion packages/app/src/studio/StudioPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const props = defineProps<{
onStudioPanelClose: () => void
eventManager: EventManager
studioStatus: string | null
cloudStudioSessionId?: string
}>()
interface StudioApp { default: StudioAppDefaultShape }
Expand All @@ -71,7 +72,11 @@ const maybeRenderReactComponent = () => {
return
}
const panel = window.UnifiedRunner.React.createElement(ReactStudioPanel.value, { canAccessStudioAI: props.canAccessStudioAI, onStudioPanelClose: props.onStudioPanelClose })
const panel = window.UnifiedRunner.React.createElement(ReactStudioPanel.value, {
canAccessStudioAI: props.canAccessStudioAI,
onStudioPanelClose: props.onStudioPanelClose,
studioSessionId: props.cloudStudioSessionId,
})
if (!reactRoot.value) {
reactRoot.value = window.UnifiedRunner.ReactDOM.createRoot(container.value)
Expand All @@ -81,6 +86,7 @@ const maybeRenderReactComponent = () => {
}
watch(() => props.canAccessStudioAI, maybeRenderReactComponent)
watch(() => props.cloudStudioSessionId, maybeRenderReactComponent)
const unmountReactComponent = () => {
if (!ReactStudioPanel.value || !container.value) {
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/studio/studio-app-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface StudioPanelProps {
canAccessStudioAI: boolean
onStudioPanelClose?: () => void
studioSessionId?: string
useRunnerStatus?: RunnerStatusShape
useTestContentRetriever?: TestContentRetrieverShape
useStudioAIStream?: StudioAIStreamShape
Expand Down
8 changes: 8 additions & 0 deletions packages/server/lib/cloud/studio/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export class StudioManager implements StudioManagerShape {
await this.invokeAsync('initializeStudioAI', { isEssential: true }, options)
}

updateSessionId (sessionId: string): void {
if (this._studioServer && typeof this._studioServer.updateSessionId === 'function') {
this.invokeSync('updateSessionId', { isEssential: false }, sessionId)
} else {
debug('updateSessionId method not available on studio server')
}
}

async destroy (): Promise<void> {
await this.invokeAsync('destroy', { isEssential: true })
}
Expand Down
16 changes: 11 additions & 5 deletions packages/server/lib/project-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ export class ProjectBase extends EE {

onStudioInit: async () => {
const isStudioReady = this.ctx.coreData.studioLifecycleManager?.isStudioReady()
const cloudStudioSessionId = v4()

if (!isStudioReady) {
debug('User entered studio mode before cloud studio was initialized')
Expand All @@ -425,11 +426,16 @@ export class ProjectBase extends EE {
studioMethodArgs: [],
})

return { canAccessStudioAI: false }
return { canAccessStudioAI: false, cloudStudioSessionId }
}

const studio = await this.ctx.coreData.studioLifecycleManager?.getStudio()

// Update the studio session ID for telemetry
if (studio) {
studio.updateSessionId(cloudStudioSessionId)
}

// only capture studio started event if the user is accessing legacy studio
if (!this.ctx.coreData.studioLifecycleManager?.cloudStudioRequested) {
try {
Expand All @@ -454,7 +460,7 @@ export class ProjectBase extends EE {
const canAccessStudioAI = await studio?.canAccessStudioAI(this.browser) ?? false

if (!canAccessStudioAI) {
return { canAccessStudioAI }
return { canAccessStudioAI, cloudStudioSessionId }
}

this.protocolManager = studio.protocolManager
Expand All @@ -469,19 +475,19 @@ export class ProjectBase extends EE {
if (!studio.protocolManager.dbPath) {
debug('Protocol database path is not set after initializing protocol manager')

return { canAccessStudioAI: false }
return { canAccessStudioAI: false, cloudStudioSessionId }
}

await studio.initializeStudioAI({
protocolDbPath: studio.protocolManager.dbPath,
})

return { canAccessStudioAI: true }
return { canAccessStudioAI: true, cloudStudioSessionId }
}

this.protocolManager = undefined

return { canAccessStudioAI: false }
return { canAccessStudioAI: false, cloudStudioSessionId }
},

onStudioDestroy: async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/lib/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ export class SocketBase {

socket.on('studio:init', async (cb) => {
try {
const { canAccessStudioAI } = await options.onStudioInit()
const { canAccessStudioAI, cloudStudioSessionId } = await options.onStudioInit()

cb({ canAccessStudioAI })
cb({ canAccessStudioAI, cloudStudioSessionId })
} catch (error) {
cb({ error: errors.cloneErr(error) })
}
Expand Down
19 changes: 17 additions & 2 deletions packages/server/test/unit/socket_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,27 @@ describe('lib/socket', () => {

context('on(studio:init)', () => {
it('calls onStudioInit', async function () {
this.options.onStudioInit.resolves({ canAccessStudioAI: true })
this.options.onStudioInit.resolves({ canAccessStudioAI: true, cloudStudioSessionId: 'test-session-id' })

await new Promise((resolve) => {
this.client.emit('studio:init', ({ canAccessStudioAI }) => {
this.client.emit('studio:init', ({ canAccessStudioAI, cloudStudioSessionId }) => {
expect(this.options.onStudioInit).to.be.called
expect(canAccessStudioAI).to.be.true
expect(cloudStudioSessionId).to.eq('test-session-id')

resolve()
})
})
})

it('calls onStudioInit and handles undefined cloudStudioSessionId', async function () {
this.options.onStudioInit.resolves({ canAccessStudioAI: false, cloudStudioSessionId: undefined })

await new Promise((resolve) => {
this.client.emit('studio:init', ({ canAccessStudioAI, cloudStudioSessionId }) => {
expect(this.options.onStudioInit).to.be.called
expect(canAccessStudioAI).to.be.false
expect(cloudStudioSessionId).to.be.undefined

resolve()
})
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/studio/studio-server-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface StudioServerShape {
canAccessStudioAI(browser: Cypress.Browser): Promise<boolean>
addSocketListeners(socket: Socket): void
initializeStudioAI(options: StudioAIInitializeOptions): Promise<void>
updateSessionId(sessionId: string): void
reportError(
error: unknown,
studioMethod: string,
Expand Down
Loading