Skip to content

Commit f8269bd

Browse files
author
Vikhyath Mondreti
committed
fix legacy logs showing up
1 parent 8558c19 commit f8269bd

7 files changed

Lines changed: 157 additions & 6 deletions

File tree

‎apps/sim/lib/logs/execution/logger.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest'
99
import { recordUsage } from '@/lib/billing/core/usage-log'
1010
import { ExecutionLogger } from '@/lib/logs/execution/logger'
11+
import { SECRET_PROJECTION_VERSION } from '@/lib/logs/execution/trace-store'
1112
import type { WorkflowExecutionLog } from '@/lib/logs/types'
1213
import { emitExecutionCompletedEvent } from '@/lib/workspace-events/emitter'
1314
import type { SerializableExecutionState } from '@/executor/execution/types'
@@ -156,6 +157,52 @@ describe('ExecutionLogger', () => {
156157
expect(typeof logger.getWorkflowExecution).toBe('function')
157158
})
158159

160+
test('marks new execution rows as contract-aware before any provenance is available', async () => {
161+
dbChainMockFns.limit.mockResolvedValueOnce([])
162+
dbChainMockFns.returning.mockResolvedValueOnce([
163+
{
164+
id: 'log-1',
165+
workflowId: 'workflow-123',
166+
executionId: 'execution-123',
167+
stateSnapshotId: 'snapshot-123',
168+
level: 'info',
169+
trigger: 'api',
170+
startedAt: new Date('2026-08-04T00:00:00.000Z'),
171+
endedAt: null,
172+
totalDurationMs: null,
173+
executionData: {},
174+
createdAt: new Date('2026-08-04T00:00:00.000Z'),
175+
},
176+
])
177+
178+
await logger.startWorkflowExecution({
179+
workflowId: 'workflow-123',
180+
workspaceId: 'workspace-123',
181+
executionId: 'execution-123',
182+
trigger: {
183+
type: 'api',
184+
source: 'api',
185+
timestamp: '2026-08-04T00:00:00.000Z',
186+
},
187+
environment: {
188+
variables: {},
189+
workflowId: 'workflow-123',
190+
executionId: 'execution-123',
191+
userId: 'user-123',
192+
workspaceId: 'workspace-123',
193+
},
194+
workflowState: { blocks: {}, edges: [], loops: {}, parallels: {} },
195+
})
196+
197+
expect(dbChainMockFns.values).toHaveBeenCalledWith(
198+
expect.objectContaining({
199+
executionData: expect.objectContaining({
200+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
201+
}),
202+
})
203+
)
204+
})
205+
159206
test('preserves a cancellation that wins the completion update race', async () => {
160207
const startedAt = new Date('2026-08-03T12:00:00.000Z')
161208
const createdAt = new Date('2026-08-03T12:00:00.000Z')
@@ -283,6 +330,7 @@ describe('ExecutionLogger', () => {
283330
})
284331
expect(completedData.correlation).toEqual(completedData.trigger?.data?.correlation)
285332
expect(completedData.finalOutput).toEqual({ ok: true })
333+
expect(completedData.secretProjectionVersion).toBe(SECRET_PROJECTION_VERSION)
286334
expect(completedData.lastStartedBlock?.blockId).toBe('block-start')
287335
expect(completedData.lastCompletedBlock?.blockId).toBe('block-end')
288336
expect(completedData.finalizationPath).toBe('completed')
@@ -445,6 +493,7 @@ describe('ExecutionLogger', () => {
445493

446494
expect(storedBytes).toBeLessThanOrEqual(3 * 1024 * 1024)
447495
expect(compacted.executionDataTruncated).toBe(true)
496+
expect(compacted.secretProjectionVersion).toBe(SECRET_PROJECTION_VERSION)
448497
expect(compacted.executionState).toBeUndefined()
449498
expect(compacted.executionStateSummary).toEqual({
450499
executedBlockCount: 1,
@@ -481,13 +530,15 @@ describe('ExecutionLogger', () => {
481530
workspaceId: 'workspace-1',
482531
},
483532
correlation,
533+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
484534
hasTraceSpans: false,
485535
traceSpanCount: 0,
486536
},
487537
'execution-metadata-only'
488538
)
489539

490540
expect(compacted.executionDataTruncated).toBe(true)
541+
expect(compacted.secretProjectionVersion).toBe(SECRET_PROJECTION_VERSION)
491542
expect(compacted.correlation).toEqual(correlation)
492543
expect(compacted).not.toHaveProperty('environment')
493544
})

‎apps/sim/lib/logs/execution/logger.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
copyTraceSpansWithoutCosts,
5555
externalizeExecutionData,
5656
materializeExecutionData,
57+
SECRET_PROJECTION_VERSION,
5758
TRACE_STORE_REF_KEY,
5859
} from '@/lib/logs/execution/trace-store'
5960
import type {
@@ -447,6 +448,7 @@ export class ExecutionLogger implements IExecutionLoggerService {
447448
}
448449

449450
const minimal: ExecutionData = {
451+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
450452
...(executionData.environment ? { environment: executionData.environment } : {}),
451453
...(executionData.trigger ? { trigger: executionData.trigger } : {}),
452454
...(executionData.billingAttribution
@@ -489,6 +491,7 @@ export class ExecutionLogger implements IExecutionLoggerService {
489491
minimalWithSize.storedBytes > MAX_EXECUTION_DATA_BYTES
490492
) {
491493
const metadataOnly: ExecutionData = {
494+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
492495
...(executionData.billingAttribution
493496
? { billingAttribution: executionData.billingAttribution }
494497
: {}),
@@ -582,6 +585,7 @@ export class ExecutionLogger implements IExecutionLoggerService {
582585
)
583586

584587
return {
588+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
585589
...(existingExecutionData?.environment
586590
? { environment: existingExecutionData.environment }
587591
: {}),
@@ -708,6 +712,7 @@ export class ExecutionLogger implements IExecutionLoggerService {
708712
totalDurationMs: null,
709713
executionDeadlineAt: executionDeadlineAt ?? null,
710714
executionData: {
715+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
711716
environment,
712717
trigger,
713718
...(billingAttribution ? { billingAttribution } : {}),

‎apps/sim/lib/logs/execution/logging-session.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@ describe('LoggingSession.markExecutionAsFailed workflowId scoping', () => {
14901490
})
14911491
.join(' ')
14921492
expect(combined).toContain('force_failed')
1493+
expect(combined).toContain('secretprojectionversion')
14931494
})
14941495

14951496
it('does not overwrite a cancellation with a late force-failure', async () => {

‎apps/sim/lib/logs/execution/logging-session.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
projectTraceSpansForSecrets,
3030
} from '@/lib/logs/execution/trace-secret-projection'
3131
import { traceSpansIndicateFailure } from '@/lib/logs/execution/trace-spans/trace-spans'
32+
import { SECRET_PROJECTION_VERSION } from '@/lib/logs/execution/trace-store'
3233
import type {
3334
ExecutionEnvironment,
3435
ExecutionFinalizationPath,
@@ -1395,7 +1396,11 @@ export class LoggingSession {
13951396
let executionData = sql`jsonb_set(
13961397
jsonb_set(
13971398
jsonb_set(
1398-
COALESCE(execution_data, '{}'::jsonb),
1399+
jsonb_set(
1400+
COALESCE(execution_data, '{}'::jsonb),
1401+
ARRAY['secretProjectionVersion'],
1402+
to_jsonb(${SECRET_PROJECTION_VERSION}::integer)
1403+
),
13991404
ARRAY['error'],
14001405
to_jsonb(${message}::text)
14011406
),

‎apps/sim/lib/logs/execution/trace-store.test.ts‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
externalizeExecutionData,
2323
materializeExecutionData,
2424
projectExecutionDataForDisplay,
25+
SECRET_PROJECTION_VERSION,
2526
TRACE_STORE_REF_KEY,
2627
} from '@/lib/logs/execution/trace-store'
2728

@@ -55,6 +56,7 @@ describe('execution data storage', () => {
5556

5657
const slim = await externalizeExecutionData(
5758
{
59+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
5860
correlation,
5961
hasTraceSpans: true,
6062
traceSpanCount: 2,
@@ -73,12 +75,14 @@ describe('execution data storage', () => {
7375
key: 'execution/workspace-1/workflow-1/execution-1/large-value-lv_bbbbbbbbbbbb.json',
7476
executionId: 'execution-1',
7577
},
78+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
7679
correlation,
7780
hasTraceSpans: true,
7881
traceSpanCount: 2,
7982
})
8083

8184
await expect(materializeExecutionData(slim, CONTEXT)).resolves.toEqual({
85+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
8286
correlation,
8387
hasTraceSpans: true,
8488
traceSpanCount: 2,
@@ -140,12 +144,13 @@ describe('projectExecutionDataForDisplay', () => {
140144
expect(JSON.stringify(displayData)).not.toContain('1234')
141145
})
142146

143-
it('omits log content and keeps only structural traces without trusted provenance', async () => {
147+
it('preserves the full display envelope for legacy rows without a projection contract', async () => {
144148
const displayData = await projectExecutionDataForDisplay(
145149
{
146150
finalOutput: { result: 'unknown-secret' },
147151
workflowInput: { token: 'unknown-secret' },
148152
completionFailure: 'unknown-secret',
153+
executionState: { blockStates: { start: { output: 'legacy-input' } } },
149154
traceSpans: [
150155
{
151156
id: 'span-1',
@@ -161,14 +166,62 @@ describe('projectExecutionDataForDisplay', () => {
161166
CONTEXT
162167
)
163168

169+
expect(displayData.finalOutput).toEqual({ result: 'unknown-secret' })
170+
expect(displayData.workflowInput).toEqual({ token: 'unknown-secret' })
171+
expect(displayData.completionFailure).toBe('unknown-secret')
172+
expect(displayData).not.toHaveProperty('executionState')
173+
expect(displayData.traceSpans).toEqual([
174+
expect.objectContaining({ output: { result: 'unknown-secret' } }),
175+
])
176+
})
177+
178+
it('fails closed for contract-aware rows without provenance', async () => {
179+
const displayData = await projectExecutionDataForDisplay(
180+
{
181+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
182+
finalOutput: { result: 'unknown-secret' },
183+
workflowInput: { token: 'unknown-secret' },
184+
traceSpans: [
185+
{
186+
id: 'span-1',
187+
name: 'Function 1',
188+
type: 'function',
189+
duration: 1,
190+
startTime: '2026-07-31T00:00:00.000Z',
191+
endTime: '2026-07-31T00:00:00.001Z',
192+
output: { result: 'unknown-secret' },
193+
},
194+
],
195+
},
196+
CONTEXT
197+
)
198+
199+
expect(displayData).not.toHaveProperty('secretProjectionVersion')
164200
expect(displayData).not.toHaveProperty('finalOutput')
165201
expect(displayData).not.toHaveProperty('workflowInput')
166-
expect(displayData).not.toHaveProperty('completionFailure')
167202
expect(displayData.traceSpans).toEqual([
168203
expect.not.objectContaining({ output: expect.anything() }),
169204
])
170205
})
171206

207+
it('fails closed when persisted provenance is incomplete', async () => {
208+
const displayData = await projectExecutionDataForDisplay(
209+
{
210+
finalOutput: { result: 'unknown-secret' },
211+
executionState: {
212+
resolvedSecretTraceProvenance: {
213+
version: 1,
214+
complete: false,
215+
entries: [],
216+
},
217+
},
218+
},
219+
CONTEXT
220+
)
221+
222+
expect(displayData).not.toHaveProperty('finalOutput')
223+
})
224+
172225
it('preserves direct literals when trusted provenance has no activated secrets', async () => {
173226
const displayData = await projectExecutionDataForDisplay(
174227
{
@@ -191,6 +244,7 @@ describe('projectExecutionDataForDisplay', () => {
191244
it('omits malformed trace content even when it was present on the stored row', async () => {
192245
const displayData = await projectExecutionDataForDisplay(
193246
{
247+
secretProjectionVersion: SECRET_PROJECTION_VERSION,
194248
traceSpans: { output: 'unsafe' },
195249
},
196250
CONTEXT

‎apps/sim/lib/logs/execution/trace-store.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212

1313
const logger = createLogger('TraceStore')
1414

15+
/** Marks execution data written under the resolved-secret display contract. */
16+
export const SECRET_PROJECTION_VERSION = 1 as const
17+
1518
/**
1619
* Key under which the externalized-execution-data pointer (a `__simLargeValueRef`)
1720
* is stored on the slim `execution_data` row.
@@ -26,7 +29,12 @@ export const TRACE_STORE_REF_KEY = 'traceStoreRef'
2629
* (environment, trigger, tokens, models, truncation flags, and of course the
2730
* heavy payloads) are recovered from the stored object.
2831
*/
29-
const INLINE_MARKER_KEYS = ['hasTraceSpans', 'traceSpanCount', 'correlation'] as const
32+
const INLINE_MARKER_KEYS = [
33+
'secretProjectionVersion',
34+
'hasTraceSpans',
35+
'traceSpanCount',
36+
'correlation',
37+
] as const
3038

3139
/**
3240
* Read-path context. Resolves an externalized payload by storage key, authorized
@@ -206,6 +214,21 @@ const LOG_DISPLAY_CONTENT_KEYS = [
206214

207215
const LOG_DISPLAY_PROJECTION_SPAN_ID = 'secret-safe-log-display-projection'
208216

217+
/** Returns historical execution data using the display behavior from before secret provenance. */
218+
function projectLegacyExecutionDataForDisplay(
219+
executionData: Record<string, unknown>
220+
): Record<string, unknown> {
221+
const omittedKeys = [
222+
'executionState',
223+
'secretProjectionVersion',
224+
...(!Object.hasOwn(executionData, 'traceSpans') || Array.isArray(executionData.traceSpans)
225+
? []
226+
: ['traceSpans']),
227+
]
228+
229+
return omit(executionData, omittedKeys) as Record<string, unknown>
230+
}
231+
209232
/**
210233
* Materializes trusted execution data and returns its log-facing projection.
211234
* Functional readers must continue using {@link materializeExecutionData}.
@@ -220,8 +243,10 @@ export async function materializeExecutionDataForDisplay(
220243

221244
/**
222245
* Projects execution-log content with the encrypted provenance saved by the
223-
* trusted executor. Missing or malformed provenance deliberately yields a
224-
* structural-only log instead of returning content that cannot be proven safe.
246+
* trusted executor. Rows predating the projection contract retain their legacy
247+
* display behavior. Contract-aware rows with missing or malformed provenance
248+
* deliberately yield a structural-only log instead of returning content that
249+
* cannot be proven safe.
225250
*/
226251
export async function projectExecutionDataForDisplay(
227252
executionData: Record<string, unknown>,
@@ -234,6 +259,14 @@ export async function projectExecutionDataForDisplay(
234259
? (executionData.executionState as Record<string, unknown>)
235260
: undefined
236261
const provenance = executionState?.resolvedSecretTraceProvenance
262+
const hasProjectionContract =
263+
Object.hasOwn(executionData, 'secretProjectionVersion') ||
264+
(executionState !== undefined && Object.hasOwn(executionState, 'resolvedSecretTraceProvenance'))
265+
266+
if (!hasProjectionContract) {
267+
return projectLegacyExecutionDataForDisplay(executionData)
268+
}
269+
237270
let registry: ResolvedSecretTraceRegistry | undefined
238271

239272
if (isResolvedSecretTraceProvenanceV1(provenance)) {
@@ -274,6 +307,7 @@ export async function projectExecutionDataForDisplay(
274307
const displayData = omit(executionData, [
275308
...LOG_DISPLAY_CONTENT_KEYS,
276309
'executionState',
310+
'secretProjectionVersion',
277311
'traceSpans',
278312
]) as Record<string, unknown>
279313

‎apps/sim/lib/logs/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export interface WorkflowExecutionLog {
131131
}>
132132
// Execution details
133133
executionData: {
134+
secretProjectionVersion?: 1
134135
environment?: ExecutionEnvironment
135136
trigger?: ExecutionTrigger
136137
billingAttribution?: BillingAttributionSnapshot

0 commit comments

Comments
 (0)