@@ -273,15 +273,65 @@ export async function installEngine(): Promise<InstallResult> {
273273 return { ok : false , error : detail || `npm exited with code ${ run . code ?? "unknown" } ` }
274274}
275275
276+ /** What the bus carries with the offer command: the offer's telemetry fields plus the server's
277+ * node major (the install happens where the server runs), so the surface that prints the offer in
278+ * another process (an attached run) reports `shown` with them. */
279+ export type OfferDetail = Pick < EngineOffer , "reason" | "workspaceId" | "declared" | "found" > & {
280+ nodeMajor : number | null
281+ }
282+
283+ export function encodeOfferDetail ( offer : EngineOffer , nodeMajor : number | null ) : string {
284+ const detail : OfferDetail = {
285+ reason : offer . reason ,
286+ workspaceId : offer . workspaceId ,
287+ declared : offer . declared ,
288+ found : offer . found ,
289+ nodeMajor,
290+ }
291+ return JSON . stringify ( detail )
292+ }
293+
294+ /** The detail back from the bus, or undefined for anything that is not one. */
295+ export function decodeOfferDetail ( raw : unknown ) : OfferDetail | undefined {
296+ if ( typeof raw !== "string" ) return undefined
297+ try {
298+ const v = JSON . parse ( raw ) as Partial < Record < keyof OfferDetail , unknown > >
299+ const reason = v . reason === "engine-missing" || v . reason === "engine-too-old" ? v . reason : undefined
300+ const workspaceId = typeof v . workspaceId === "string" ? v . workspaceId : undefined
301+ const declared = typeof v . declared === "number" ? v . declared : v . declared === undefined ? undefined : NaN
302+ const found = typeof v . found === "string" ? v . found : v . found === undefined ? undefined : ""
303+ const nodeMajor = v . nodeMajor === null ? null : typeof v . nodeMajor === "number" ? v . nodeMajor : undefined
304+ if ( ! reason || workspaceId === undefined || Number . isNaN ( declared ) || found === "" || nodeMajor === undefined ) {
305+ return undefined
306+ }
307+ return { reason, workspaceId, declared, found, nodeMajor }
308+ } catch {
309+ return undefined
310+ }
311+ }
312+
313+ /** An attached run printed the offer it read off the bus: that process is the surface that showed
314+ * it, so it reports `shown` (surface `bus`) from the fields the event carried. False when the
315+ * event carried none that decode, and nothing is reported. */
316+ export function trackPrintedOffer ( raw : unknown , sessionID : string ) : boolean {
317+ const detail = decodeOfferDetail ( raw )
318+ if ( ! detail ) return false
319+ trackInstallPrompt ( "shown" , detail , { surface : "bus" , nodeMajor : detail . nodeMajor } , sessionID )
320+ return true
321+ }
322+
276323/** Ask the TUI to raise the offer. False when the bus is unavailable. The
277324 * session is carried so an attached headless run, which reads the same event
278- * stream, prints the offer raised for its own session only. */
279- async function publishOffer ( sessionID : string ) : Promise < boolean > {
280- if ( syncInternals . publishOffer ) return syncInternals . publishOffer ( sessionID )
325+ * stream, prints the offer raised for its own session only; the offer's fields
326+ * ride along so that run reports `shown` from them (the TUI dialog re-derives
327+ * the offer and reports its own). */
328+ async function publishOffer ( offer : EngineOffer , sessionID : string ) : Promise < boolean > {
329+ const detail = encodeOfferDetail ( offer , await nodeMajor ( ) . catch ( ( ) => null ) )
330+ if ( syncInternals . publishOffer ) return syncInternals . publishOffer ( sessionID , detail )
281331 try {
282332 await AppRuntime . runPromise (
283333 EventV2Bridge . Service . use ( ( events ) =>
284- events . publish ( TuiEvent . CommandExecute , { command : OFFER_COMMAND , sessionID } ) ,
334+ events . publish ( TuiEvent . CommandExecute , { command : OFFER_COMMAND , sessionID, arguments : detail } ) ,
285335 ) ,
286336 )
287337 return true
@@ -335,6 +385,6 @@ export async function offerOrNotify(offer: EngineOffer, toast: Toast, sessionID:
335385 // may hold the offer back (its 7-day latch), so dispatch is not display. The toast reports here
336386 // only when it was shown.
337387 if ( offerInstall ( offer ) ) return
338- if ( await publishOffer ( sessionID ) ) return
388+ if ( await publishOffer ( offer , sessionID ) ) return
339389 if ( await notify ( toast ) ) shown ( "toast" )
340390}
0 commit comments