Repository navigation
fix(remote): bind client artifact destinations to capture identity #5301
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
Changes from all commits
c3e68fd
6fff8dc
1585ca7
8de1e74
3ee9563
ccb1a2c
ffee06e
e71768d
9f92303
360a058
23411a2
d7278df
f3f5575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { errorResponse, type DaemonResponse } from '@agent-device/kernel/contracts'; | ||
| import { CAPTURE_IDENTITY_REASONS } from '@agent-device/kernel/errors'; | ||
|
|
||
| type CaptureKind = 'trace' | 'native perf'; | ||
| type CaptureRefusal = 'already-active' | 'mismatch'; | ||
|
|
||
| const messages = { | ||
| trace: { | ||
| 'already-active': 'trace already in progress', | ||
| mismatch: 'trace capture identity changed', | ||
| }, | ||
| 'native perf': { | ||
| 'already-active': 'native perf capture already in progress', | ||
| mismatch: 'native perf capture identity changed', | ||
| }, | ||
| } as const; | ||
|
|
||
| export function captureIdentityRefusal( | ||
| kind: CaptureKind, | ||
| refusal: CaptureRefusal, | ||
| clientCaptureId?: string, | ||
| ): DaemonResponse { | ||
| return errorResponse('INVALID_ARGS', messages[kind][refusal], { | ||
| reason: | ||
| refusal === 'already-active' | ||
| ? CAPTURE_IDENTITY_REASONS.alreadyActive.reason | ||
| : CAPTURE_IDENTITY_REASONS.mismatch.reason, | ||
| ...(refusal === 'already-active' ? { clientCaptureId } : {}), | ||
| }); | ||
| } | ||
|
|
||
| let lastCaptureStartOrder = 0; | ||
|
|
||
| /** Orders successful capture starts even when their HTTP replies arrive out of order. */ | ||
| export function captureStartReceipt(clientCaptureId: string | undefined) { | ||
| if (!clientCaptureId) return {}; | ||
| const now = Math.floor((performance.timeOrigin + performance.now()) * 1_000); | ||
| lastCaptureStartOrder = Math.max(lastCaptureStartOrder + 1, now); | ||
| return { clientCaptureId, captureStartOrder: lastCaptureStartOrder }; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,10 @@ import type { SessionRef } from '../../core/session-state.ts'; | |||||||||||||||||||||||||||||||||||||
| import { recordSessionAction } from '../../core/session-action-recorder.ts'; | ||||||||||||||||||||||||||||||||||||||
| import { errorResponse } from '@agent-device/kernel/contracts'; | ||||||||||||||||||||||||||||||||||||||
| import { withCaptureOutputOwnership } from '../../core/capture-output-ownership.ts'; | ||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||
| captureIdentityRefusal, | ||||||||||||||||||||||||||||||||||||||
| captureStartReceipt, | ||||||||||||||||||||||||||||||||||||||
| } from '../../core/capture-identity-refusal.ts'; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export function handleTraceCommand(params: { | ||||||||||||||||||||||||||||||||||||||
| host: HostEnvironment; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,7 +42,8 @@ function startTrace( | |||||||||||||||||||||||||||||||||||||
| ref: SessionRef, | ||||||||||||||||||||||||||||||||||||||
| ): DaemonResponse { | ||||||||||||||||||||||||||||||||||||||
| const session = sessionStore.requireCurrent(ref); | ||||||||||||||||||||||||||||||||||||||
| if (session.trace) return errorResponse('INVALID_ARGS', 'trace already in progress'); | ||||||||||||||||||||||||||||||||||||||
| if (session.trace) | ||||||||||||||||||||||||||||||||||||||
| return captureIdentityRefusal('trace', 'already-active', session.trace.clientCaptureId); | ||||||||||||||||||||||||||||||||||||||
| const outPath = expandSessionPath( | ||||||||||||||||||||||||||||||||||||||
| req.positionals?.[1] ?? sessionStore.defaultTracePath(session), | ||||||||||||||||||||||||||||||||||||||
| requestCwd(req, host), | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -47,15 +52,20 @@ function startTrace( | |||||||||||||||||||||||||||||||||||||
| fs.appendFileSync(outPath, ''); | ||||||||||||||||||||||||||||||||||||||
| session.trace = { | ||||||||||||||||||||||||||||||||||||||
| outPath, | ||||||||||||||||||||||||||||||||||||||
| clientCaptureId: req.meta?.captureId, | ||||||||||||||||||||||||||||||||||||||
| startedAt: Date.now(), | ||||||||||||||||||||||||||||||||||||||
| clientOutPath: req.meta?.clientArtifactPaths?.outPath, | ||||||||||||||||||||||||||||||||||||||
| clientOutPath: req.meta?.captureId ? req.meta.clientArtifactPaths?.outPath : undefined, | ||||||||||||||||||||||||||||||||||||||
| ...(req.internal?.publicNetworkOnly ? { remoteCaptureOutputPath: outPath } : {}), | ||||||||||||||||||||||||||||||||||||||
| remoteCaptureTenantId: session.lease?.tenantId ?? req.meta?.tenantId, | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
52
to
60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer raised on 🟡 Minor · Old client's pathless trace stop now receives the daemon's own path as its destination A released client (no
Suggested change
Suggested change Record Detailspersona: Useful? Reply Reply
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked at
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||||||||
| recordSessionAction(sessionStore, ref, req, req.command, { action: 'start', outPath }); | ||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||
| ok: true, | ||||||||||||||||||||||||||||||||||||||
| data: { trace: 'started', outPath } satisfies TraceCommandResult, | ||||||||||||||||||||||||||||||||||||||
| data: { | ||||||||||||||||||||||||||||||||||||||
| trace: 'started', | ||||||||||||||||||||||||||||||||||||||
| outPath, | ||||||||||||||||||||||||||||||||||||||
| ...captureStartReceipt(req.meta?.captureId), | ||||||||||||||||||||||||||||||||||||||
| } satisfies TraceCommandResult, | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -67,6 +77,10 @@ function stopTrace( | |||||||||||||||||||||||||||||||||||||
| ): DaemonResponse { | ||||||||||||||||||||||||||||||||||||||
| const session = sessionStore.requireCurrent(ref); | ||||||||||||||||||||||||||||||||||||||
| if (!session.trace) return errorResponse('INVALID_ARGS', 'no active trace'); | ||||||||||||||||||||||||||||||||||||||
| const requestedCaptureId = req.meta?.captureId; | ||||||||||||||||||||||||||||||||||||||
| const clientCaptureId = session.trace.clientCaptureId; | ||||||||||||||||||||||||||||||||||||||
| if (requestedCaptureId && requestedCaptureId !== clientCaptureId) | ||||||||||||||||||||||||||||||||||||||
| return captureIdentityRefusal('trace', 'mismatch'); | ||||||||||||||||||||||||||||||||||||||
| const outPath = relocateTraceOutput( | ||||||||||||||||||||||||||||||||||||||
| requestCwd(req, host), | ||||||||||||||||||||||||||||||||||||||
| session.trace.outPath, | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -76,7 +90,7 @@ function stopTrace( | |||||||||||||||||||||||||||||||||||||
| const tenantId = session.trace.remoteCaptureTenantId; | ||||||||||||||||||||||||||||||||||||||
| const clientOutPath = | ||||||||||||||||||||||||||||||||||||||
| req.meta?.clientArtifactPaths?.outPath ?? | ||||||||||||||||||||||||||||||||||||||
| session.trace.clientOutPath ?? | ||||||||||||||||||||||||||||||||||||||
| (requestedCaptureId ? session.trace.clientOutPath : undefined) ?? | ||||||||||||||||||||||||||||||||||||||
| (req.internal?.publicNetworkOnly ? undefined : outPath); | ||||||||||||||||||||||||||||||||||||||
| session.trace = undefined; | ||||||||||||||||||||||||||||||||||||||
| recordSessionAction(sessionStore, ref, req, req.command, { action: 'stop', outPath }); | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -86,6 +100,7 @@ function stopTrace( | |||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| trace: 'stopped', | ||||||||||||||||||||||||||||||||||||||
| outPath, | ||||||||||||||||||||||||||||||||||||||
| ...(clientCaptureId ? { clientCaptureId } : {}), | ||||||||||||||||||||||||||||||||||||||
| artifacts: [ | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| field: 'outPath', | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.