The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): prove quick entry prompt acceptance
LeonardoLGDSee82f20474bb
diff --git a/apps/desktop/src/lib/quick-entry-submission.test.ts b/apps/desktop/src/lib/quick-entry-submission.test.tsnew file mode 100644index 00000000000..45e213b54a2--- /dev/null+++ b/apps/desktop/src/lib/quick-entry-submission.test.ts@@ -0,0 +1,204 @@+import { describe, expect, it } from 'vitest'++import {+ captureQuickEntryRequest,+ type QuickEntryAcceptedPromptStatus,+ validateQuickEntryAcceptance+} from './quick-entry-submission'++const CORRELATION_ID = 'qe-123'+const STORED_SESSION_ID = 'stored-session-1'+const RUNTIME_SESSION_ID = 'runtime-session-1'+const AFTER_DISPATCH = { promptDispatched: true }+const BEFORE_DISPATCH = { promptDispatched: false }++function capturedRequest(): ReturnType<typeof captureQuickEntryRequest> {+ return captureQuickEntryRequest({+ correlationId: CORRELATION_ID,+ identity: {+ prompt: 'Ship the exact prompt',+ storedSessionId: STORED_SESSION_ID,+ target: STORED_SESSION_ID+ }+ })+}++function backendResult(status: QuickEntryAcceptedPromptStatus, overrides: Record<string, unknown> = {}) {+ return {+ correlationId: CORRELATION_ID,+ runtimeSessionId: RUNTIME_SESSION_ID,+ sessionId: STORED_SESSION_ID,+ status,+ ...overrides+ }+}++describe('captureQuickEntryRequest', () => {+ it('freezes the request identity before an await can observe caller mutations', () => {+ const original = {+ correlationId: CORRELATION_ID,+ identity: {+ prompt: 'original prompt',+ storedSessionId: STORED_SESSION_ID,+ target: 'current'+ }+ }+ const request = captureQuickEntryRequest(original)++ original.correlationId = 'qe-other'+ original.identity.prompt = 'changed prompt'+ original.identity.storedSessionId = 'other-stored-session'+ original.identity.target = 'other-target'++ expect(request).toEqual({+ correlationId: CORRELATION_ID,+ identity: {+ prompt: 'original prompt',+ storedSessionId: STORED_SESSION_ID,+ target: 'current'+ }+ })+ expect(Object.isFrozen(request)).toBe(true)+ expect(Object.isFrozen(request.identity)).toBe(true)+ })+})++describe('validateQuickEntryAcceptance', () => {+ it.each(['streaming', 'queued', 'steered', 'redirected'] as const)(+ 'accepts a correlated prompt result with status %s',+ status => {+ const outcome = validateQuickEntryAcceptance(capturedRequest(), backendResult(status), AFTER_DISPATCH)++ expect(outcome).toEqual({+ acceptedIdentity: {+ runtimeSessionId: RUNTIME_SESSION_ID,+ storedSessionId: STORED_SESSION_ID+ },+ correlationId: CORRELATION_ID,+ evidence: {+ observed: true,+ status+ },+ ok: true,+ requestedIdentity: {+ prompt: 'Ship the exact prompt',+ storedSessionId: STORED_SESSION_ID,+ target: STORED_SESSION_ID+ },+ status: 'accepted'+ })+ }+ )++ it('rejects an empty object as backend evidence', () => {+ expect(validateQuickEntryAcceptance(capturedRequest(), {}, AFTER_DISPATCH)).toMatchObject({+ ok: false,+ reason: 'invalid-backend-result',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects a request whose identity is missing', () => {+ expect(validateQuickEntryAcceptance({ correlationId: CORRELATION_ID }, {}, AFTER_DISPATCH)).toMatchObject({+ ok: false,+ reason: 'invalid-request',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects a backend result that omits correlation', () => {+ expect(+ validateQuickEntryAcceptance(capturedRequest(), backendResult('streaming', { correlationId: '' }), AFTER_DISPATCH)+ ).toMatchObject({+ ok: false,+ reason: 'invalid-backend-result',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects a malformed backend response', () => {+ expect(validateQuickEntryAcceptance(capturedRequest(), { status: 'streaming' }, AFTER_DISPATCH)).toMatchObject({+ ok: false,+ reason: 'invalid-backend-result',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects a non-prompt backend status', () => {+ expect(+ validateQuickEntryAcceptance(capturedRequest(), backendResult('completed' as never), AFTER_DISPATCH)+ ).toMatchObject({+ ok: false,+ reason: 'unsupported-backend-status',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects acceptance for the wrong stored identity', () => {+ expect(+ validateQuickEntryAcceptance(+ capturedRequest(),+ backendResult('streaming', { sessionId: 'other-stored-session' }),+ AFTER_DISPATCH+ )+ ).toMatchObject({+ ok: false,+ reason: 'stored-identity-mismatch',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects acceptance when the backend result omits a runtime identity', () => {+ expect(+ validateQuickEntryAcceptance(+ capturedRequest(),+ backendResult('streaming', { runtimeSessionId: '' }),+ AFTER_DISPATCH+ )+ ).toMatchObject({+ ok: false,+ reason: 'invalid-backend-result',+ retryable: false,+ status: 'unknown'+ })+ })++ it('rejects acceptance when the result correlation does not match', () => {+ expect(+ validateQuickEntryAcceptance(+ capturedRequest(),+ backendResult('streaming', { correlationId: 'qe-other' }),+ AFTER_DISPATCH+ )+ ).toMatchObject({+ ok: false,+ reason: 'correlation-mismatch',+ retryable: false,+ status: 'unknown'+ })+ })++ it('treats a proven pre-dispatch rejection as retryable', () => {+ expect(validateQuickEntryAcceptance(capturedRequest(), false, BEFORE_DISPATCH)).toEqual({+ ok: false,+ reason: 'no-prompt-dispatched',+ retryable: true,+ status: 'rejected'+ })+ })++ it('treats the same failure after dispatch as non-retryable unknown', () => {+ expect(validateQuickEntryAcceptance(capturedRequest(), false, AFTER_DISPATCH)).toEqual({+ ok: false,+ reason: 'invalid-backend-result',+ retryable: false,+ status: 'unknown'+ })+ })+})diff --git a/apps/desktop/src/lib/quick-entry-submission.ts b/apps/desktop/src/lib/quick-entry-submission.tsnew file mode 100644index 00000000000..1aeb2a41e06--- /dev/null+++ b/apps/desktop/src/lib/quick-entry-submission.ts@@ -0,0 +1,206 @@+export type QuickEntryAcceptedPromptStatus = 'streaming' | 'queued' | 'steered' | 'redirected'++export interface QuickEntryRequestIdentity {+ /** The exact prompt text submitted through Quick Entry. */+ readonly prompt: string+ /** The selected Quick Entry target: current, new, or a stored session id. */+ readonly target: string+ /** The durable session id the caller intends to address. */+ readonly storedSessionId: string | null+}++export interface CapturedQuickEntryRequest {+ readonly correlationId: string+ readonly identity: QuickEntryRequestIdentity+}++export interface QuickEntryBackendPromptResult {+ readonly correlationId: string+ /** The durable session id accepted by the backend. */+ readonly sessionId: string+ readonly runtimeSessionId: string+ readonly status: QuickEntryAcceptedPromptStatus+}++export interface QuickEntryBackendResultEvidence {+ readonly observed: true+ readonly status: QuickEntryAcceptedPromptStatus+}++export interface QuickEntryAcceptedIdentity {+ readonly runtimeSessionId: string+ readonly storedSessionId: string+}++export type QuickEntrySubmitOutcome =+ | {+ readonly acceptedIdentity: QuickEntryAcceptedIdentity+ readonly correlationId: string+ readonly evidence: QuickEntryBackendResultEvidence+ readonly ok: true+ readonly requestedIdentity: QuickEntryRequestIdentity+ readonly status: 'accepted'+ }+ | {+ readonly ok: false+ readonly reason: 'no-prompt-dispatched'+ readonly retryable: true+ readonly status: 'rejected'+ }+ | {+ readonly ok: false+ readonly reason:+ | 'correlation-mismatch'+ | 'invalid-backend-result'+ | 'invalid-request'+ | 'stored-identity-mismatch'+ | 'unsupported-backend-status'+ readonly retryable: false+ readonly status: 'unknown'+ }++export interface QuickEntryDispatchContext {+ /**+ * Only a caller that can prove its failure happened before prompt dispatch may+ * set this to false. Every post-dispatch failure is unknown because retry can+ * duplicate the original prompt.+ */+ readonly promptDispatched: boolean+}++type UnknownRecord = Record<string, unknown>++const ACCEPTED_PROMPT_STATUSES: ReadonlySet<string> = new Set(['streaming', 'queued', 'steered', 'redirected'])++function isRecord(value: unknown): value is UnknownRecord {+ return typeof value === 'object' && value !== null && !Array.isArray(value)+}++function nonEmptyString(value: unknown): value is string {+ return typeof value === 'string' && value.trim().length > 0+}++/**+ * Capture the mutable Quick Entry inputs before any await can interleave with a+ * session switch. The returned request and nested identity are both frozen.+ */+export function captureQuickEntryRequest(input: {+ correlationId: string+ identity: QuickEntryRequestIdentity+}): CapturedQuickEntryRequest {+ const identity: QuickEntryRequestIdentity = { ...input.identity }+ const request: CapturedQuickEntryRequest = {+ correlationId: input.correlationId,+ identity+ }++ Object.freeze(identity)+ Object.freeze(request)++ return request+}++function invalidRequestOutcome(dispatch: QuickEntryDispatchContext): QuickEntrySubmitOutcome {+ return dispatch.promptDispatched+ ? {+ ok: false,+ reason: 'invalid-request',+ retryable: false,+ status: 'unknown'+ }+ : {+ ok: false,+ reason: 'no-prompt-dispatched',+ retryable: true,+ status: 'rejected'+ }+}++function unknownOutcome(+ reason: Extract<QuickEntrySubmitOutcome, { status: 'unknown' }>['reason']+): QuickEntrySubmitOutcome {+ return {+ ok: false,+ reason,+ retryable: false,+ status: 'unknown'+ }+}++/**+ * Turn a backend response into an acceptance receipt. A supported status alone+ * is not enough: the result must also carry this request's correlation and the+ * exact durable/runtime session identities.+ */+export function validateQuickEntryAcceptance(+ request: unknown,+ backendResult: unknown,+ dispatch: QuickEntryDispatchContext+): QuickEntrySubmitOutcome {+ const requestRecord = isRecord(request) ? request : {}+ const identity = requestRecord.identity+ const correlationId = requestRecord.correlationId++ if (+ !isRecord(identity) ||+ !nonEmptyString(identity.prompt) ||+ !nonEmptyString(identity.target) ||+ !nonEmptyString(identity.storedSessionId) ||+ !nonEmptyString(correlationId)+ ) {+ return invalidRequestOutcome(dispatch)+ }++ if (dispatch.promptDispatched === false) {+ return {+ ok: false,+ reason: 'no-prompt-dispatched',+ retryable: true,+ status: 'rejected'+ }+ }++ const result = isRecord(backendResult) && Object.keys(backendResult).length > 0 ? backendResult : null+ if (+ result === null ||+ !nonEmptyString(result.correlationId) ||+ !nonEmptyString(result.sessionId) ||+ !nonEmptyString(result.runtimeSessionId) ||+ !nonEmptyString(result.status)+ ) {+ return unknownOutcome('invalid-backend-result')+ }++ if (result.correlationId !== correlationId) {+ return unknownOutcome('correlation-mismatch')+ }++ if (result.sessionId !== identity.storedSessionId) {+ return unknownOutcome('stored-identity-mismatch')+ }++ if (!ACCEPTED_PROMPT_STATUSES.has(result.status)) {+ return unknownOutcome('unsupported-backend-status')+ }++ const status = result.status as QuickEntryAcceptedPromptStatus++ return {+ acceptedIdentity: {+ runtimeSessionId: result.runtimeSessionId,+ storedSessionId: result.sessionId+ },+ correlationId,+ evidence: {+ observed: true,+ status+ },+ ok: true,+ requestedIdentity: {+ prompt: identity.prompt,+ storedSessionId: identity.storedSessionId,+ target: identity.target+ },+ status: 'accepted'+ }+}