|
| 1 | +import { createLogger } from '@sim/logger' |
1 | 2 | import { loggerMock } from '@sim/testing' |
2 | 3 | import { maskClientMock, maskClientMockFns } from '@sim/testing/mocks/mask-client.mock' |
3 | 4 | import { permissionCheckMock } from '@sim/testing/mocks/permission-check.mock' |
4 | 5 | import { storageServiceMockFns } from '@sim/testing/mocks/storage-service.mock' |
5 | 6 | import { uploadsMock } from '@sim/testing/mocks/uploads.mock' |
6 | 7 | import { DrizzleQueryError } from 'drizzle-orm/errors' |
7 | 8 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | +import { classifyFailure, logFailureOnce } from '@/lib/core/errors/failure-log' |
8 | 10 | import { clearLargeValueCacheForTests } from '@/lib/execution/payloads/cache' |
9 | 11 | import { createLargeArrayManifest } from '@/lib/execution/payloads/large-array-manifest' |
10 | 12 | import { isLargeValueRef } from '@/lib/execution/payloads/large-value-ref' |
11 | 13 | import { buildTraceSpans } from '@/lib/logs/execution/trace-spans/trace-spans' |
12 | 14 | import { validateBlockType } from '@/ee/access-control/utils/permission-check' |
13 | 15 | import { BlockType, EDGE } from '@/executor/constants' |
14 | 16 | import type { DAGNode } from '@/executor/dag/builder' |
| 17 | +import { ChildWorkflowError } from '@/executor/errors/child-workflow-error' |
15 | 18 | import { BlockExecutor } from '@/executor/execution/block-executor' |
16 | 19 | import { ExecutionState } from '@/executor/execution/state' |
17 | 20 | import type { BlockHandler, ExecutionContext } from '@/executor/types' |
@@ -496,6 +499,9 @@ describe('BlockExecutor', () => { |
496 | 499 | expect.objectContaining({ cause: expect.objectContaining({ code: 'ECONNRESET' }) }) |
497 | 500 | ) |
498 | 501 | expect(JSON.stringify(logged)).not.toContain('owner-secret-id') |
| 502 | + /** Logged here at error, so the engine and the run surfaces must see it as already logged. */ |
| 503 | + expect(classifyFailure(thrown)).toBe('internal') |
| 504 | + expect(logFailureOnce(createLogger('OuterBoundary'), 'probe', thrown)).toBeUndefined() |
499 | 505 | }) |
500 | 506 |
|
501 | 507 | it('fires block completion callbacks for pausing blocks so clients receive pause output', async () => { |
@@ -572,6 +578,116 @@ describe('BlockExecutor', () => { |
572 | 578 | expect(state.getBlockOutput(block.id)).toEqual(output) |
573 | 579 | }) |
574 | 580 |
|
| 581 | + function failBlockWith(thrown: unknown) { |
| 582 | + const block = createBlock() |
| 583 | + const workflow: SerializedWorkflow = { |
| 584 | + version: '1', |
| 585 | + blocks: [block], |
| 586 | + connections: [], |
| 587 | + loops: {}, |
| 588 | + parallels: {}, |
| 589 | + } |
| 590 | + const state = new ExecutionState() |
| 591 | + const resolver = new VariableResolver(workflow, {}, state) |
| 592 | + const handler: BlockHandler = { |
| 593 | + canHandle: () => true, |
| 594 | + execute: async () => { |
| 595 | + throw thrown |
| 596 | + }, |
| 597 | + } |
| 598 | + const executor = new BlockExecutor([handler], resolver, {}, state) |
| 599 | + const ctx = createContext(state) |
| 600 | + ctx.resolvedSecretTraceRegistry = new ResolvedSecretTraceRegistry([]) |
| 601 | + return executor.execute(ctx, createNode(block), block).catch((error) => error) |
| 602 | + } |
| 603 | + |
| 604 | + function blockFailureLogsSince(loggerIndex: number) { |
| 605 | + const loggers = new Set<{ error: { mock: { calls: unknown[][] } } }>( |
| 606 | + blockExecutorBaseLogger.withMetadata.mock.results |
| 607 | + .slice(loggerIndex) |
| 608 | + .map((result: { value: { error: { mock: { calls: unknown[][] } } } }) => result.value) |
| 609 | + ) |
| 610 | + return [...loggers].flatMap((logger) => |
| 611 | + logger.error.mock.calls.filter(([message]) => message === 'Block execution failed') |
| 612 | + ) |
| 613 | + } |
| 614 | + |
| 615 | + it('logs every block failure that rethrows one persistent object, not just the first', async () => { |
| 616 | + /** A rejected dynamic `import()` or memoized rejected promise rethrows the same object. */ |
| 617 | + const persistentFault = new Error('handler module failed to load') |
| 618 | + const loggerIndex = blockExecutorBaseLogger.withMetadata.mock.results.length |
| 619 | + |
| 620 | + await failBlockWith(persistentFault) |
| 621 | + await failBlockWith(persistentFault) |
| 622 | + |
| 623 | + expect(blockFailureLogsSince(loggerIndex)).toHaveLength(2) |
| 624 | + }) |
| 625 | + |
| 626 | + it.each([ |
| 627 | + ['projects secrets and runtime identifiers out of', false], |
| 628 | + ['fails closed to a structural', true], |
| 629 | + ] as const)('%s the block failure line for a provider error', async (_name, incomplete) => { |
| 630 | + const secret = 'block-failure-secret' |
| 631 | + /** The Agent handler hands its provider error registry to the block executor this way. */ |
| 632 | + const errorRegistry = new ResolvedSecretTraceRegistry([ |
| 633 | + { name: 'TOKEN', plaintext: secret, encryptedValue: 'encrypted-block-failure-secret' }, |
| 634 | + ]) |
| 635 | + errorRegistry.recordResolved('TOKEN', secret) |
| 636 | + if (incomplete) errorRegistry.markIncomplete('unspecified') |
| 637 | + const block = createBlock() |
| 638 | + const workflow: SerializedWorkflow = { |
| 639 | + version: '1', |
| 640 | + blocks: [block], |
| 641 | + connections: [], |
| 642 | + loops: {}, |
| 643 | + parallels: {}, |
| 644 | + } |
| 645 | + const loggerIndex = blockExecutorBaseLogger.withMetadata.mock.results.length |
| 646 | + const state = new ExecutionState() |
| 647 | + const handler: BlockHandler = { |
| 648 | + canHandle: () => true, |
| 649 | + execute: async (ctx) => { |
| 650 | + ctx.errorResolvedSecretTraceRegistry = errorRegistry |
| 651 | + throw new Error(`provider failed with ${secret} __var_TOKEN __sim_runtime_test_1`) |
| 652 | + }, |
| 653 | + } |
| 654 | + const executor = new BlockExecutor( |
| 655 | + [handler], |
| 656 | + new VariableResolver(workflow, {}, state), |
| 657 | + {}, |
| 658 | + state |
| 659 | + ) |
| 660 | + |
| 661 | + await executor.execute(createContext(state), createNode(block), block).catch(() => undefined) |
| 662 | + |
| 663 | + const logged = JSON.stringify(blockFailureLogsSince(loggerIndex)) |
| 664 | + expect(logged).toContain('Block execution failed') |
| 665 | + for (const leaked of [secret, '__var_', '__sim_']) expect(logged).not.toContain(leaked) |
| 666 | + }) |
| 667 | + |
| 668 | + it('logs an internal child workflow fault with the block and run identity', async () => { |
| 669 | + const loggerIndex = blockExecutorBaseLogger.withMetadata.mock.results.length |
| 670 | + |
| 671 | + await failBlockWith( |
| 672 | + new ChildWorkflowError({ |
| 673 | + message: '"Child" failed: child load blew up', |
| 674 | + childWorkflowName: 'Child', |
| 675 | + cause: new TypeError('child load blew up'), |
| 676 | + }) |
| 677 | + ) |
| 678 | + |
| 679 | + const [[, logged]] = blockFailureLogsSince(loggerIndex) |
| 680 | + expect(logged).toEqual( |
| 681 | + expect.objectContaining({ |
| 682 | + blockId: 'function-block-1', |
| 683 | + executionId: 'execution-1', |
| 684 | + workflowId: 'workflow-1', |
| 685 | + error: '"Child" failed: child load blew up', |
| 686 | + failureKind: 'internal', |
| 687 | + }) |
| 688 | + ) |
| 689 | + }) |
| 690 | + |
575 | 691 | it('does not soft-succeed non-agent blocks on user AbortError', async () => { |
576 | 692 | const block = createBlock() |
577 | 693 | const workflow: SerializedWorkflow = { |
@@ -614,11 +730,15 @@ describe('BlockExecutor', () => { |
614 | 730 | const ctx = createContext(state) |
615 | 731 | ctx.abortSignal = abortController.signal |
616 | 732 |
|
617 | | - await expect(executor.execute(ctx, createNode(block), block)).rejects.toThrow(/abort/i) |
| 733 | + const thrown = await executor.execute(ctx, createNode(block), block).catch((error) => error) |
| 734 | + expect(thrown).toBeInstanceOf(Error) |
| 735 | + expect(thrown.message).toMatch(/abort/i) |
618 | 736 |
|
619 | 737 | const output = state.getBlockOutput(block.id) |
620 | 738 | expect(output?.error).toBeTruthy() |
621 | 739 | expect(output).not.toEqual({ content: '' }) |
| 740 | + /** A user Stop is not a Sim fault, so it must not page at error. */ |
| 741 | + expect(classifyFailure(thrown)).toBe('user') |
622 | 742 | }) |
623 | 743 |
|
624 | 744 | it('keeps Sim Chat secret policy in runtime inputs and out of trace inputs', async () => { |
|
0 commit comments