diff --git a/.github/workflows/ci-tests-e2e-coverage.yaml b/.github/workflows/ci-tests-e2e-coverage.yaml index bf9e77cbb81..bc41ee76548 100644 --- a/.github/workflows/ci-tests-e2e-coverage.yaml +++ b/.github/workflows/ci-tests-e2e-coverage.yaml @@ -17,7 +17,7 @@ on: type: string secrets: CODECOV_TOKEN: - required: true + required: false workflow_run: workflows: ['CI: Tests E2E'] types: @@ -32,41 +32,48 @@ permissions: jobs: merge: - # Not gated on full success: fail-fast:false means one flaky shard flips the whole run to failure, which would skip merging every other shard's real coverage. if: > github.repository == 'Comfy-Org/ComfyUI_frontend' && (github.event_name == 'workflow_call' || - github.event.workflow_run.event != 'pull_request' || - github.event.workflow_run.head_repository.full_name != github.repository || - github.event.workflow_run.actor.login == 'dependabot[bot]') && - github.event.workflow_run.conclusion != 'cancelled' + github.event.workflow_run.conclusion != 'cancelled') runs-on: ubuntu-latest timeout-minutes: 10 outputs: has-coverage: ${{ steps.coverage-shards.outputs.has-coverage }} steps: - - name: Checkout sources for genhtml + - name: Checkout sources for coverage packaging + if: github.event_name == 'workflow_call' uses: actions/checkout@v7 with: - ref: ${{ inputs.source_sha || github.event.workflow_run.head_sha }} + ref: ${{ inputs.source_sha }} sparse-checkout: | src packages - name: Download all shard coverage data + if: github.event_name == 'workflow_call' uses: dawidd6/action-download-artifact@0bd50d53a6d7fb5cb921e607957e9cc12b4ce392 # v12 with: - run_id: ${{ inputs.source_run_id || github.event.workflow_run.id }} + run_id: ${{ inputs.source_run_id }} name: e2e-coverage-shard-.* name_is_regexp: true path: temp/coverage-shards if_no_artifact_found: warn + - name: Download packaged coverage data + if: github.event_name == 'workflow_run' + uses: dawidd6/action-download-artifact@0bd50d53a6d7fb5cb921e607957e9cc12b4ce392 # v12 + with: + run_id: ${{ github.event.workflow_run.id }} + name: e2e-coverage + path: coverage/playwright + if_no_artifact_found: warn + - name: Detect shard coverage data id: coverage-shards run: | - if [ -d temp/coverage-shards ] && find temp/coverage-shards -name 'coverage.lcov' -type f | grep -q .; then + if [ -f coverage/playwright/coverage.lcov ] || { [ -d temp/coverage-shards ] && find temp/coverage-shards -name 'coverage.lcov' -type f | grep -q .; }; then echo "has-coverage=true" >> "$GITHUB_OUTPUT" else echo "has-coverage=false" >> "$GITHUB_OUTPUT" @@ -74,11 +81,11 @@ jobs: fi - name: Install lcov - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_call' && steps.coverage-shards.outputs.has-coverage == 'true' run: sudo apt-get install -y -qq lcov - name: Merge shard coverage into single LCOV - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_call' && steps.coverage-shards.outputs.has-coverage == 'true' run: | mkdir -p coverage/playwright LCOV_FILES=$(find temp/coverage-shards -name 'coverage.lcov' -type f) @@ -88,7 +95,7 @@ jobs: wc -l coverage/playwright/coverage.lcov - name: Validate merged coverage - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_call' && steps.coverage-shards.outputs.has-coverage == 'true' run: | MERGED_SF=$(grep -c '^SF:' coverage/playwright/coverage.lcov || echo 0) MERGED_LH=$(awk -F: '/^LH:/{s+=$2}END{print s+0}' coverage/playwright/coverage.lcov) @@ -110,7 +117,7 @@ jobs: done - name: Assert coverage was mapped back to source - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_call' && steps.coverage-shards.outputs.has-coverage == 'true' run: | MAPPED_SF=$(grep -cE '^SF:(src|packages)/' coverage/playwright/coverage.lcov || true) echo "Source-mapped files: $MAPPED_SF" >> "$GITHUB_STEP_SUMMARY" @@ -120,7 +127,7 @@ jobs: fi - name: Strip non-source entries from coverage - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_call' && steps.coverage-shards.outputs.has-coverage == 'true' run: | lcov --remove coverage/playwright/coverage.lcov \ '*localhost-8188*' \ @@ -139,7 +146,7 @@ jobs: if-no-files-found: warn - name: Upload E2E coverage to Codecov - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_run' && steps.coverage-shards.outputs.has-coverage == 'true' uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: files: coverage/playwright/coverage.lcov @@ -151,7 +158,7 @@ jobs: fail_ci_if_error: false - name: Generate HTML coverage report - if: steps.coverage-shards.outputs.has-coverage == 'true' + if: github.event_name == 'workflow_call' && steps.coverage-shards.outputs.has-coverage == 'true' run: | genhtml coverage/playwright/coverage.lcov \ -o coverage/html \ @@ -161,6 +168,15 @@ jobs: --ignore-errors source,unmapped \ --synthesize-missing + - name: Download packaged HTML report + if: github.event_name == 'workflow_run' && steps.coverage-shards.outputs.has-coverage == 'true' + uses: dawidd6/action-download-artifact@0bd50d53a6d7fb5cb921e607957e9cc12b4ce392 # v12 + with: + run_id: ${{ github.event.workflow_run.id }} + name: e2e-coverage-html + path: coverage/html + if_no_artifact_found: warn + - name: Upload HTML report artifact if: steps.coverage-shards.outputs.has-coverage == 'true' uses: actions/upload-artifact@v6 diff --git a/.github/workflows/ci-tests-e2e.yaml b/.github/workflows/ci-tests-e2e.yaml index 14839851cda..17a1412ace3 100644 --- a/.github/workflows/ci-tests-e2e.yaml +++ b/.github/workflows/ci-tests-e2e.yaml @@ -229,9 +229,6 @@ jobs: ${{ always() && needs.changes.outputs.should-run == 'true' && - github.event_name == 'pull_request' && - github.event.pull_request.head.repo.fork == false && - github.event.pull_request.user.login != 'dependabot[bot]' && needs.playwright-tests-chromium-sharded.result != 'cancelled' }} uses: ./.github/workflows/ci-tests-e2e-coverage.yaml @@ -240,7 +237,6 @@ jobs: source_pr: ${{ format('{0}', github.event.pull_request.number) }} source_run_id: ${{ format('{0}', github.run_id) }} source_sha: ${{ github.event.pull_request.head.sha || github.sha }} - secrets: inherit # Records video only for spec files newly added in this PR, to keep cost bounded. playwright-video-new-tests: diff --git a/scripts/cicd/e2e-coverage-workflow.test.ts b/scripts/cicd/e2e-coverage-workflow.test.ts new file mode 100644 index 00000000000..b74ad90bebe --- /dev/null +++ b/scripts/cicd/e2e-coverage-workflow.test.ts @@ -0,0 +1,62 @@ +import { readFileSync } from 'node:fs' + +import { describe, expect, it } from 'vitest' +import { parse } from 'yaml' + +type WorkflowStep = { + if?: string + uses?: string + with?: Record +} + +type Workflow = { + jobs: Record< + string, + { + if?: string + steps?: WorkflowStep[] + with?: Record + } + > +} + +function readWorkflow(path: string): Workflow { + return parse(readFileSync(path, 'utf8')) as Workflow +} + +describe('E2E coverage workflows', () => { + it('packages fork coverage before entering the trusted workflow_run', () => { + const coverageWorkflow = readWorkflow( + '.github/workflows/ci-tests-e2e-coverage.yaml' + ) + const e2eWorkflow = readWorkflow('.github/workflows/ci-tests-e2e.yaml') + const coverageSteps = coverageWorkflow.jobs.merge.steps ?? [] + const checkout = coverageSteps.find((step) => + step.uses?.startsWith('actions/checkout@') + ) + const download = coverageSteps.find( + (step) => step.with?.name === 'e2e-coverage' + ) + const codecov = coverageSteps.find((step) => + step.uses?.startsWith('codecov/codecov-action@') + ) + const packageJob = e2eWorkflow.jobs['upload-e2e-coverage'] + + expect(checkout?.if).toContain("github.event_name == 'workflow_call'") + expect(download?.if).toContain("github.event_name == 'workflow_run'") + expect(codecov?.if).toContain("github.event_name == 'workflow_run'") + expect(codecov?.with).toMatchObject({ + override_branch: + '${{ inputs.source_branch || github.event.workflow_run.head_branch }}', + override_commit: + '${{ inputs.source_sha || github.event.workflow_run.head_sha }}', + override_pr: + '${{ inputs.source_pr || github.event.workflow_run.pull_requests[0].number }}' + }) + expect(packageJob.if).not.toContain('fork == false') + expect(packageJob.with).toMatchObject({ + source_branch: '${{ github.head_ref || github.ref_name }}', + source_sha: '${{ github.event.pull_request.head.sha || github.sha }}' + }) + }) +}) diff --git a/src/platform/nodeApi/apiSurface.ts b/src/platform/nodeApi/apiSurface.ts index f69311e6f1a..9aa76467a4d 100644 --- a/src/platform/nodeApi/apiSurface.ts +++ b/src/platform/nodeApi/apiSurface.ts @@ -17,6 +17,7 @@ */ export const API_MEMBERS: ReadonlySet = new Set([ 'Record', + 'accelerators', 'add', 'addActionBarButton', 'addBadge', @@ -35,6 +36,7 @@ export const API_MEMBERS: ReadonlySet = new Set([ 'attrs', 'autoHeight', 'autoQueueMode', + 'available', 'backend', 'batch', 'batchCount', @@ -64,6 +66,7 @@ export const API_MEMBERS: ReadonlySet = new Set([ 'connectedType', 'container', 'context', + 'cpu', 'ctrl', 'ctrlKey', 'declare', @@ -170,6 +173,9 @@ export const API_MEMBERS: ReadonlySet = new Set([ 'max', 'maxHeight', 'maxWidth', + 'memory', + 'memory_available', + 'memory_total', 'menuEvent', 'mergeWidgetConfig', 'message', @@ -181,6 +187,7 @@ export const API_MEMBERS: ReadonlySet = new Set([ 'minWidth', 'mode', 'modify', + 'monitor', 'mount', 'move', 'moveLinksTo', @@ -358,16 +365,19 @@ export const API_MEMBERS: ReadonlySet = new Set([ 'supports', 'surface', 'surfaceHovered', + 'system', 'targetIndex', 'targetNodeId', 'targetSlotId', 'targets', + 'temperature_c', 'text', 'textSecondary', 'theme', 'title', 'to', 'tooltip', + 'total', 'type', 'typeColor', 'ui', @@ -375,10 +385,12 @@ export const API_MEMBERS: ReadonlySet = new Set([ 'update', 'url', 'useGrouping', + 'utilization_percent', 'value', 'values', 'variant', 'version', + 'volumes', 'when', 'widget', 'widgetConfig', diff --git a/src/platform/nodeApi/comfyApi.test.ts b/src/platform/nodeApi/comfyApi.test.ts index fabdddd80dc..bed4c490db7 100644 --- a/src/platform/nodeApi/comfyApi.test.ts +++ b/src/platform/nodeApi/comfyApi.test.ts @@ -53,6 +53,7 @@ describe('comfy API root', () => { expect(api().supports('node.fileDrop')).toBe(true) expect(api().supports('queue.disableAutoQueue')).toBe(true) expect(api().supports('queue.settings')).toBe(true) + expect(api().supports('system.monitor')).toBe(true) }) it('returns false for unknown capabilities instead of throwing', () => { diff --git a/src/platform/nodeApi/comfyApi.ts b/src/platform/nodeApi/comfyApi.ts index 92568b46266..1ede6879a15 100644 --- a/src/platform/nodeApi/comfyApi.ts +++ b/src/platform/nodeApi/comfyApi.ts @@ -45,6 +45,8 @@ import { createUiHandle } from './uiHandle' import { createViewportObserver } from './viewport' import type { SettingsHandle } from './settingsHandle' import type { StorageHandle } from './storageHandle' +import { createSystemApi } from './systemHandle' +import type { SystemHandle } from './systemHandle' import type { UiHandle } from './uiHandle' import type { NodeHandle } from './nodeHandle' import { createWorkflowApi } from './workflowHandle' @@ -135,6 +137,7 @@ const CAPABILITIES: ReadonlyMap = new Map([ ['commands', '1.0'], ['backend', '1.0'], ['storage', '1.0'], + ['system.monitor', '1.0'], ['ui.sidebarTab', '1.0'], ['viewport.changed', '1.0'], ['interaction.state', '1.0'], @@ -220,6 +223,8 @@ export interface Comfy { * between machines. */ readonly storage: StorageHandle + /** Bounded, host-sampled hardware metrics. */ + readonly system: SystemHandle /** The sanctioned slice of app chrome — sidebar tabs. */ readonly ui: UiHandle /** Commands, their keybindings, and notifications. */ @@ -346,6 +351,7 @@ function buildMajor( ) const settings = createSettingsApi() const storage = createStorageApi() + const system = createSystemApi() const ui = createUiHandle() const commands = createCommandsApi() const backend = createBackendApi() @@ -425,6 +431,7 @@ function buildMajor( graph, settings, storage, + system, ui, commands, backend, diff --git a/src/platform/nodeApi/systemHandle.test.ts b/src/platform/nodeApi/systemHandle.test.ts new file mode 100644 index 00000000000..9f8f277dfd6 --- /dev/null +++ b/src/platform/nodeApi/systemHandle.test.ts @@ -0,0 +1,67 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { api } from '@/scripts/api' + +import { createSystemApi } from './systemHandle' + +vi.mock('@/scripts/api', () => ({ + api: { fetchApi: vi.fn() } +})) + +const snapshot = { + cpu: { utilization_percent: 37.5 }, + memory: { total: 1000, available: 400 }, + volumes: [{ id: 'volume-0', label: 'Root', total: 2000, available: 500 }], + accelerators: [ + { + id: 'accelerator-0', + name: 'Example GPU', + memory_total: 4000, + memory_available: 1500, + utilization_percent: 61, + temperature_c: 72 + } + ] +} + +describe('system monitor API', () => { + beforeEach(() => vi.mocked(api.fetchApi).mockReset()) + + it('returns the exact bounded host projection', async () => { + vi.mocked(api.fetchApi).mockResolvedValue( + new Response(JSON.stringify(snapshot), { status: 200 }) + ) + await expect(createSystemApi().monitor()).resolves.toEqual(snapshot) + expect(api.fetchApi).toHaveBeenCalledWith('/system_monitor', { + cache: 'no-store' + }) + }) + + it.for([ + { ...snapshot, memory: { total: 1, available: 2 } }, + { + ...snapshot, + volumes: Array.from({ length: 65 }, () => snapshot.volumes[0]) + }, + { + ...snapshot, + accelerators: [{ ...snapshot.accelerators[0], temperature_c: 1001 }] + }, + { + ...snapshot, + volumes: [{ ...snapshot.volumes[0], label: '/Volumes/Private\0' }] + } + ])('rejects malformed or over-broad snapshots', async (value) => { + vi.mocked(api.fetchApi).mockResolvedValue( + new Response(JSON.stringify(value), { status: 200 }) + ) + await expect(createSystemApi().monitor()).rejects.toThrow( + /invalid snapshot/ + ) + }) + + it('reports a failed host request without parsing it', async () => { + vi.mocked(api.fetchApi).mockResolvedValue(new Response('', { status: 503 })) + await expect(createSystemApi().monitor()).rejects.toThrow(/status 503/) + }) +}) diff --git a/src/platform/nodeApi/systemHandle.ts b/src/platform/nodeApi/systemHandle.ts new file mode 100644 index 00000000000..d1dc2b370f4 --- /dev/null +++ b/src/platform/nodeApi/systemHandle.ts @@ -0,0 +1,154 @@ +import { api } from '@/scripts/api' + +import { ComfyApiError } from './errors' + +const MAX_ITEMS = 64 +const MAX_BYTES = Number.MAX_SAFE_INTEGER + +/** @knipIgnoreUnusedButUsedByCustomNodes */ +export interface SystemMonitorCpu { + readonly utilization_percent: number | null +} + +/** @knipIgnoreUnusedButUsedByCustomNodes */ +export interface SystemMonitorMemory { + readonly total: number + readonly available: number +} + +/** @knipIgnoreUnusedButUsedByCustomNodes */ +export interface SystemMonitorVolume extends SystemMonitorMemory { + readonly id: string + readonly label: string +} + +/** @knipIgnoreUnusedButUsedByCustomNodes */ +export interface SystemMonitorAccelerator { + readonly id: string + readonly name: string + readonly memory_total: number + readonly memory_available: number + readonly utilization_percent: number | null + readonly temperature_c: number | null +} + +/** @knipIgnoreUnusedButUsedByCustomNodes */ +export interface SystemMonitorSnapshot { + readonly cpu: SystemMonitorCpu + readonly memory: SystemMonitorMemory + readonly volumes: readonly SystemMonitorVolume[] + readonly accelerators: readonly SystemMonitorAccelerator[] +} + +export interface SystemHandle { + /** + * Returns one host-sampled hardware snapshot. Volume ids are opaque and + * unsupported utilization or temperature sensors are null. + */ + monitor(): Promise +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +function isText(value: unknown, limit: number): value is string { + return ( + typeof value === 'string' && + value.length > 0 && + value.length <= limit && + !value.includes('\0') + ) +} + +function isBytes(value: unknown): value is number { + return ( + typeof value === 'number' && + Number.isSafeInteger(value) && + value >= 0 && + value <= MAX_BYTES + ) +} + +function isPercent(value: unknown): value is number | null { + return ( + value === null || + (typeof value === 'number' && + Number.isFinite(value) && + value >= 0 && + value <= 100) + ) +} + +function isTemperature(value: unknown): value is number | null { + return ( + value === null || + (typeof value === 'number' && + Number.isFinite(value) && + value >= -273.15 && + value <= 1000) + ) +} + +function isMemory( + value: unknown +): value is SystemMonitorMemory & Record { + return ( + isRecord(value) && + isBytes(value.total) && + isBytes(value.available) && + value.available <= value.total + ) +} + +function isVolume(value: unknown): value is SystemMonitorVolume { + return isMemory(value) && isText(value.id, 128) && isText(value.label, 128) +} + +function isAccelerator(value: unknown): value is SystemMonitorAccelerator { + return ( + isRecord(value) && + isText(value.id, 128) && + isText(value.name, 256) && + isBytes(value.memory_total) && + isBytes(value.memory_available) && + value.memory_available <= value.memory_total && + isPercent(value.utilization_percent) && + isTemperature(value.temperature_c) + ) +} + +function isSnapshot(value: unknown): value is SystemMonitorSnapshot { + return ( + isRecord(value) && + isRecord(value.cpu) && + isPercent(value.cpu.utilization_percent) && + isMemory(value.memory) && + Array.isArray(value.volumes) && + value.volumes.length <= MAX_ITEMS && + value.volumes.every(isVolume) && + Array.isArray(value.accelerators) && + value.accelerators.length <= MAX_ITEMS && + value.accelerators.every(isAccelerator) + ) +} + +export function createSystemApi(): SystemHandle { + return Object.freeze({ + async monitor() { + const response = await api.fetchApi('/system_monitor', { + cache: 'no-store' + }) + if (!response.ok) { + throw new ComfyApiError( + `System monitor request failed with status ${response.status}.` + ) + } + const value: unknown = await response.json() + if (!isSnapshot(value)) { + throw new ComfyApiError('System monitor returned an invalid snapshot.') + } + return value + } + }) +}