From 6cba785e2fa55b74af0dfe81eb1c966aeb74db11 Mon Sep 17 00:00:00 2001 From: Andrii Arsenin Date: Thu, 1 Oct 2026 13:26:07 +0300 Subject: [PATCH 1/2] feat(scenario): tag run log entries with their context - scenario_run_logs gains a context column (step:N, snippet:alias, assertion:pass/fail, run), tracked through a call stack in CodeExecutorService so nested runSnippet calls tag their own and inner log lines - automatic step started/passed/failed entries and the Playwright trace upload notice are tagged accordingly - run log export (CSV/Markdown) and the logs search match context as well as message - add a LogOutput UI component: a terminal-style log viewer with level color coding, elapsed-time-since-start timestamps and a context tag, replacing the generic Table on the run detail page - show the full log list instead of paginating it, capped to a scrollable block - fix migrations.spec.ts's revert test, which only undid the last migration instead of all of them --- .../.storybook/stories/LogOutput.stories.tsx | 99 +++++++++++++++++++ client/src/api/types.ts | 2 + client/src/pages/run/RunDetailPage.tsx | 56 +++-------- client/src/ui/LogOutput/LogOutput.module.css | 91 +++++++++++++++++ client/src/ui/LogOutput/LogOutput.tsx | 66 +++++++++++++ client/src/ui/index.ts | 3 + docs/scenario.md | 11 ++- .../code-executor/code-executor.service.ts | 47 ++++++--- .../src/code-executor/exec-context.builder.ts | 5 + .../1790848194828-AddRunLogContext.ts | 31 ++++++ server/src/database/migrations/index.ts | 3 +- .../src/scenario/scenario-run-log.entity.ts | 4 + .../scenario/scenario-scheduler.service.ts | 15 ++- server/src/scenario/scenario.service.ts | 14 ++- server/test/migrations.spec.ts | 6 +- 15 files changed, 383 insertions(+), 70 deletions(-) create mode 100644 client/.storybook/stories/LogOutput.stories.tsx create mode 100644 client/src/ui/LogOutput/LogOutput.module.css create mode 100644 client/src/ui/LogOutput/LogOutput.tsx create mode 100644 server/src/database/migrations/1790848194828-AddRunLogContext.ts diff --git a/client/.storybook/stories/LogOutput.stories.tsx b/client/.storybook/stories/LogOutput.stories.tsx new file mode 100644 index 0000000..067da07 --- /dev/null +++ b/client/.storybook/stories/LogOutput.stories.tsx @@ -0,0 +1,99 @@ +import type { Meta, StoryObj } from '@storybook/react-vite'; +import { LogOutput } from '../../src/ui/LogOutput/LogOutput'; + +const meta: Meta = { + title: 'UI/LogOutput', + component: LogOutput, + tags: ['autodocs'], + parameters: { layout: 'padded' }, +}; +export default meta; + +type Story = StoryObj; + +const secondsAgo = (n: number) => new Date(Date.now() - n * 1000).toISOString(); + +export const Default: Story = { + args: { + startTime: secondsAgo(60), + entries: [ + { + id: 1, + level: 'log', + context: 'step:1', + message: 'Step 1 started: Login with PKCS', + createdAt: secondsAgo(60), + }, + { + id: 2, + level: 'log', + context: 'snippet:loginWithPkcs', + message: 'Snippet "loginWithPkcs" started', + createdAt: secondsAgo(58), + }, + { + id: 3, + level: 'log', + context: 'snippet:loginWithPkcs', + message: 'Navigating to ID login: https://id.example.com', + createdAt: secondsAgo(57), + }, + { + id: 4, + level: 'log', + context: 'assertion:pass', + message: 'Assertion passed: page must have redirected to cabinet URL', + createdAt: secondsAgo(40), + }, + { + id: 5, + level: 'warn', + context: 'snippet:loginWithPkcs', + message: 'Retrying file upload after a transient error', + createdAt: secondsAgo(39), + }, + { + id: 6, + level: 'log', + context: 'step:1', + message: 'Step 1 passed', + createdAt: secondsAgo(30), + }, + { + id: 7, + level: 'error', + context: 'assertion:fail', + message: 'Assertion failed: Document validation must pass (got 422)', + createdAt: secondsAgo(10), + }, + { + id: 8, + level: 'error', + context: 'step:2', + message: 'Step 2 failed: Document validation must pass (got 422)', + createdAt: secondsAgo(9), + }, + ], + }, +}; + +export const Scrollable: Story = { + args: { + maxHeight: 160, + startTime: secondsAgo(60), + entries: Array.from({ length: 30 }, (_, i) => ({ + id: i, + level: (['log', 'log', 'log', 'warn', 'error'] as const)[i % 5], + context: `step:${Math.floor(i / 6) + 1}`, + message: `Log entry ${i + 1}`, + createdAt: secondsAgo(60 - i * 2), + })), + }, +}; + +export const Empty: Story = { + args: { + entries: [], + emptyMessage: 'No logs yet.', + }, +}; diff --git a/client/src/api/types.ts b/client/src/api/types.ts index 80677ae..1a61646 100644 --- a/client/src/api/types.ts +++ b/client/src/api/types.ts @@ -189,6 +189,8 @@ export interface ScenarioRunLog { runId: string; stepRunId: string | null; level: LogLevel; + /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ + context: string | null; message: string; createdAt: string; } diff --git a/client/src/pages/run/RunDetailPage.tsx b/client/src/pages/run/RunDetailPage.tsx index 22e02b8..1f7cb28 100644 --- a/client/src/pages/run/RunDetailPage.tsx +++ b/client/src/pages/run/RunDetailPage.tsx @@ -7,10 +7,8 @@ import type { Scenario, ScenarioRunDetail, ScenarioRunStep, - ScenarioRunLog, ScenarioRunStatus, RunStepStatus, - LogLevel, FileMetadata, } from '../../api'; import { scenarios } from '../../api'; @@ -27,7 +25,7 @@ import { Card, CodeBlock, DescriptionList, - Pagination, + LogOutput, Search, Table, Timestamp, @@ -70,12 +68,6 @@ const STEP_STATUS_VARIANT: Record = { cancelled: 'warning', }; -const LOG_LEVEL_VARIANT: Record = { - log: 'neutral', - warn: 'warning', - error: 'error', -}; - const FINAL: ScenarioRunStatus[] = ['pass', 'fail']; export function RunDetailPage() { @@ -97,7 +89,6 @@ function RunDetail() { const [error, setError] = useState(null); const [polling, setPolling] = useState(false); const [pulseKey, setPulseKey] = useState(0); - const [logPage, setLogPage] = useState(1); const [logSearch, setLogSearch] = useState(''); const [debouncedSearch, setDebouncedSearch] = useState(''); const [expandAll, setExpandAll] = useState(false); @@ -112,7 +103,6 @@ function RunDetail() { const [deletingFile, setDeletingFile] = useState(false); const [repeating, setRepeating] = useState(false); const logSearchRef = useRef(''); - const LOG_PAGE_SIZE = 25; const pollRef = useRef | null>(null); const stopPolling = () => { @@ -221,7 +211,6 @@ function RunDetail() { const t = setTimeout(() => { setDebouncedSearch(logSearch); logSearchRef.current = logSearch; - setLogPage(1); }, 300); return () => clearTimeout(t); }, [logSearch]); @@ -400,26 +389,6 @@ function RunDetail() { }, ]; - const logColumns: TableColumn[] = [ - { - key: 'level', - header: t('runs.log_level'), - width: 70, - render: (l) => {l.level}, - }, - { - key: 'message', - header: t('runs.log_message'), - render: (l) => {l.message}, - }, - { - key: 'time', - header: t('runs.log_time'), - width: 160, - render: (l) => , - }, - ]; - return (
@@ -560,21 +529,18 @@ function RunDetail() {
- l.id} - loading={false} + ({ + id: l.id, + level: l.level, + context: l.context, + message: l.message, + createdAt: l.createdAt, + }))} emptyMessage="" /> - {run.logs.length > LOG_PAGE_SIZE && ( - - )} )} diff --git a/client/src/ui/LogOutput/LogOutput.module.css b/client/src/ui/LogOutput/LogOutput.module.css new file mode 100644 index 0000000..eb3a93d --- /dev/null +++ b/client/src/ui/LogOutput/LogOutput.module.css @@ -0,0 +1,91 @@ +.root { + display: flex; + flex-direction: column; + background: var(--color-bg-subtle); + border: var(--border-width) solid var(--color-border); + border-radius: var(--radius-md); + overflow: auto; + font-family: var(--font-mono, ui-monospace, 'Cascadia Code', 'Fira Code', monospace); + font-size: var(--font-size-xs); +} + +.empty { + padding: var(--space-4); + color: var(--color-text-muted); + font-size: var(--font-size-sm); +} + +.line { + display: flex; + align-items: baseline; + flex-wrap: wrap; + gap: var(--space-2); + padding: var(--space-1) var(--space-3); + border-left: 3px solid transparent; + border-bottom: 1px solid var(--color-border); +} + +.line:last-child { + border-bottom: none; +} + +.timestamp { + flex: 0 0 auto; + min-width: 7em; + text-align: right; + color: var(--color-text-muted); + white-space: nowrap; +} + +.level { + flex: 0 0 auto; + width: 4.5em; + text-transform: uppercase; + font-weight: 700; +} + +.context { + flex: 0 0 auto; + color: var(--color-secondary-fg); +} + +.context::before { + content: '['; +} + +.context::after { + content: ']'; +} + +.message { + flex: 1 1 320px; + color: var(--color-text); + white-space: pre-wrap; + word-break: break-word; +} + +.log { + border-left-color: var(--color-border); +} + +.log .level { + color: var(--color-text-muted); +} + +.warn { + border-left-color: var(--color-warning-fg); + background: var(--color-warning-bg); +} + +.warn .level { + color: var(--color-warning-fg); +} + +.error { + border-left-color: var(--color-error-fg); + background: var(--color-error-bg); +} + +.error .level { + color: var(--color-error-fg); +} diff --git a/client/src/ui/LogOutput/LogOutput.tsx b/client/src/ui/LogOutput/LogOutput.tsx new file mode 100644 index 0000000..acdca32 --- /dev/null +++ b/client/src/ui/LogOutput/LogOutput.tsx @@ -0,0 +1,66 @@ +import styles from './LogOutput.module.css'; + +export type LogOutputLevel = 'log' | 'warn' | 'error'; + +export interface LogOutputEntry { + id: string | number; + level: LogOutputLevel; + /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ + context?: string | null; + message: string; + createdAt: string | number | Date; +} + +export interface LogOutputProps { + entries: LogOutputEntry[]; + /** Reference point for the elapsed-time column; defaults to the first entry's timestamp. */ + startTime?: string | number | Date; + /** Caps the block's height (e.g. "400px"), scrolling once entries overflow it. */ + maxHeight?: string | number; + emptyMessage?: string; + className?: string; +} + +/** "+12.345s" (or "-" if before startTime, e.g. clock skew). */ +function formatElapsed(deltaMs: number): string { + const sign = deltaMs < 0 ? '-' : '+'; + return `${sign}${(Math.abs(deltaMs) / 1000).toFixed(3)}s`; +} + +/** Terminal-style log viewer: one line per entry, color-coded by level. */ +export function LogOutput({ + entries, + startTime, + maxHeight, + emptyMessage = 'No logs.', + className, +}: LogOutputProps) { + if (entries.length === 0) { + return
{emptyMessage}
; + } + + const startMs = new Date(startTime ?? entries[0].createdAt).getTime(); + + return ( +
+ {entries.map((entry) => { + const entryMs = new Date(entry.createdAt).getTime(); + return ( +
+ + {formatElapsed(entryMs - startMs)} + + {entry.level} + {entry.context && {entry.context}} + {entry.message} +
+ ); + })} +
+ ); +} diff --git a/client/src/ui/index.ts b/client/src/ui/index.ts index b57f21d..7e98da9 100644 --- a/client/src/ui/index.ts +++ b/client/src/ui/index.ts @@ -69,3 +69,6 @@ export type { TableProps, TableColumn, SortDirection } from './Table/Table'; export { UuidBadge } from './UuidBadge/UuidBadge'; export type { UuidBadgeProps } from './UuidBadge/UuidBadge'; + +export { LogOutput } from './LogOutput/LogOutput'; +export type { LogOutputProps, LogOutputEntry, LogOutputLevel } from './LogOutput/LogOutput'; diff --git a/docs/scenario.md b/docs/scenario.md index 91ce5cf..e861e2a 100644 --- a/docs/scenario.md +++ b/docs/scenario.md @@ -47,7 +47,16 @@ Also in scope: Code is syntax-checked before it runs, and executes in the server process: it's trusted code, not sandboxed. -Step run logs also include automatic "Step N started / passed / failed" entries. +## Run logs + +Each run log entry (`level`, `message`, `createdAt`) also carries a `context`: where the message was generated. Contexts: + +- `step:`: the step a log entry belongs to, including the automatic "Step N started / passed / failed" entries +- `snippet:`: entries logged while `context.runSnippet(alias, ...)` is running, including its own automatic "started"/"finished"/"failed" entries. A snippet calling another snippet tags entries with the innermost running snippet's alias +- `assertion:pass` / `assertion:fail`: `context.assert()`'s own log entry +- `run`: run-level entries not tied to a step (e.g. a scenario timeout, or the Playwright trace upload notice) + +`GET /scenarios/:id/runs/:runId?q=...` and the exported CSV/Markdown logs (`GET /scenarios/:id/runs/:runId/export`) match `q` against both `message` and `context`. ## Versioning diff --git a/server/src/code-executor/code-executor.service.ts b/server/src/code-executor/code-executor.service.ts index 7c56cde..80c5cd9 100644 --- a/server/src/code-executor/code-executor.service.ts +++ b/server/src/code-executor/code-executor.service.ts @@ -21,6 +21,8 @@ export interface ExecResult { export type ScriptLogger = ( level: "log" | "warn" | "error", message: string, + /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ + context?: string | null, ) => void; /** The `context` object available inside user scripts as the sole argument. */ @@ -70,6 +72,8 @@ export interface ExecContext { browser: BrowserContext; code: string; log?: ScriptLogger; + /** Base log context for this execution, e.g. "step:2". */ + logContext?: string | null; getStepOutput?: (order: number) => Promise; credentials?: Record; environment?: EnvironmentData | null; @@ -110,6 +114,7 @@ export class CodeExecutorService { browser, code, log, + logContext, getStepOutput, credentials, environment, @@ -125,6 +130,17 @@ export class CodeExecutorService { .map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a))) .join(" "); + // Innermost running snippet (if any) tags log entries; falls back to + // logContext (e.g. the current step) outside of any runSnippet call. + const contextStack: string[] = logContext ? [logContext] : []; + const currentContext = (): string | null => + contextStack.length > 0 ? contextStack[contextStack.length - 1] : null; + const emit = ( + level: "log" | "warn" | "error", + message: string, + contextOverride?: string, + ): void => scriptLog(level, message, contextOverride ?? currentContext()); + const credMap: Record = credentials ?? {}; const envData: EnvironmentData = environment ?? {}; const snippetMap: Record = snippets ?? {}; @@ -133,11 +149,11 @@ export class CodeExecutorService { let scriptContext: ScriptContext; const fakeConsole = { - log: (...args: unknown[]) => scriptLog("log", toStr(args)), - warn: (...args: unknown[]) => scriptLog("warn", toStr(args)), - error: (...args: unknown[]) => scriptLog("error", toStr(args)), - info: (...args: unknown[]) => scriptLog("log", toStr(args)), - debug: (...args: unknown[]) => scriptLog("log", toStr(args)), + log: (...args: unknown[]) => emit("log", toStr(args)), + warn: (...args: unknown[]) => emit("warn", toStr(args)), + error: (...args: unknown[]) => emit("error", toStr(args)), + info: (...args: unknown[]) => emit("log", toStr(args)), + debug: (...args: unknown[]) => emit("log", toStr(args)), }; try { @@ -145,9 +161,9 @@ export class CodeExecutorService { page, browser, dumpDom: (selector?: string) => dumpDom(page, selector), - log: (...args: unknown[]) => scriptLog("log", toStr(args)), - warn: (...args: unknown[]) => scriptLog("warn", toStr(args)), - error: (...args: unknown[]) => scriptLog("error", toStr(args)), + log: (...args: unknown[]) => emit("log", toStr(args)), + warn: (...args: unknown[]) => emit("warn", toStr(args)), + error: (...args: unknown[]) => emit("error", toStr(args)), assert: async ( fn: () => boolean | Promise, description: string, @@ -161,13 +177,13 @@ export class CodeExecutorService { failureReason = (err as Error).message; } if (passed) { - scriptLog("log", `Assertion passed: ${description}`); + emit("log", description, "assertion:pass"); return; } const message = failureReason ? `Assertion failed: ${description} (${failureReason})` : `Assertion failed: ${description}`; - scriptLog("error", message); + emit("error", message, "assertion:fail"); throw new Error(message); }, getStepOutput: getStepOutput ?? (() => Promise.resolve(null)), @@ -197,7 +213,9 @@ export class CodeExecutorService { if (snippetCode == null) { throw new Error(`Snippet alias "${alias}" not found`); } - scriptLog("log", `Snippet "${alias}" started`); + const snippetContext = `snippet:${alias}`; + contextStack.push(snippetContext); + emit("log", `Snippet "${alias}" started`, snippetContext); const snippetFn = new Function( "context", "console", @@ -212,14 +230,17 @@ export class CodeExecutorService { args, playwrightExpect, ); - scriptLog("log", `Snippet "${alias}" finished`); + emit("log", `Snippet "${alias}" finished`, snippetContext); return snippetResult; } catch (err) { - scriptLog( + emit( "error", `Snippet "${alias}" failed: ${(err as Error).message}`, + snippetContext, ); throw err; + } finally { + contextStack.pop(); } }, getScenarioFiles: async (opts?: { limit?: number; offset?: number }) => { diff --git a/server/src/code-executor/exec-context.builder.ts b/server/src/code-executor/exec-context.builder.ts index bcb540a..56d4322 100644 --- a/server/src/code-executor/exec-context.builder.ts +++ b/server/src/code-executor/exec-context.builder.ts @@ -26,6 +26,11 @@ export class ExecContextBuilder { return this; } + logContext(logContext: string): this { + this.ctx.logContext = logContext; + return this; + } + getStepOutput(fn: (order: number) => Promise): this { this.ctx.getStepOutput = fn; return this; diff --git a/server/src/database/migrations/1790848194828-AddRunLogContext.ts b/server/src/database/migrations/1790848194828-AddRunLogContext.ts new file mode 100644 index 0000000..ba07440 --- /dev/null +++ b/server/src/database/migrations/1790848194828-AddRunLogContext.ts @@ -0,0 +1,31 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddRunLogContext1790848194828 implements MigrationInterface { + name = "AddRunLogContext1790848194828"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "temporary_scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), "context" text, CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq" FROM "scenario_run_logs"`, + ); + await queryRunner.query(`DROP TABLE "scenario_run_logs"`); + await queryRunner.query( + `ALTER TABLE "temporary_scenario_run_logs" RENAME TO "scenario_run_logs"`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "scenario_run_logs" RENAME TO "temporary_scenario_run_logs"`, + ); + await queryRunner.query( + `CREATE TABLE "scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq" FROM "temporary_scenario_run_logs"`, + ); + await queryRunner.query(`DROP TABLE "temporary_scenario_run_logs"`); + } +} diff --git a/server/src/database/migrations/index.ts b/server/src/database/migrations/index.ts index 1337591..e096d6e 100644 --- a/server/src/database/migrations/index.ts +++ b/server/src/database/migrations/index.ts @@ -1,7 +1,8 @@ import { Initial1790793447827 } from "./1790793447827-Initial"; +import { AddRunLogContext1790848194828 } from "./1790848194828-AddRunLogContext"; /** * All migrations, oldest first. Listed explicitly (instead of a glob) so the * same list works from compiled JS, ts-jest and the TypeORM CLI. */ -export const MIGRATIONS = [Initial1790793447827]; +export const MIGRATIONS = [Initial1790793447827, AddRunLogContext1790848194828]; diff --git a/server/src/scenario/scenario-run-log.entity.ts b/server/src/scenario/scenario-run-log.entity.ts index 75d6e13..a2b4458 100644 --- a/server/src/scenario/scenario-run-log.entity.ts +++ b/server/src/scenario/scenario-run-log.entity.ts @@ -36,6 +36,10 @@ export class ScenarioRunLogEntity { @Column({ type: "text", default: "log" }) level: LogLevel; + // Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass" + @Column({ type: "text", nullable: true }) + context: string | null; + @Column({ type: "text" }) message: string; diff --git a/server/src/scenario/scenario-scheduler.service.ts b/server/src/scenario/scenario-scheduler.service.ts index 0994ca7..56a5c7c 100644 --- a/server/src/scenario/scenario-scheduler.service.ts +++ b/server/src/scenario/scenario-scheduler.service.ts @@ -143,7 +143,7 @@ export class ScenarioSchedulerService implements OnModuleInit { statuses: ["pending", "waiting"], }) .execute(); - this.persistLog(runId, null, "error", reason); + this.persistLog(runId, null, "error", reason, "run"); } private persistLog( @@ -151,18 +151,19 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRunId: string | null, level: "log" | "warn" | "error", message: string, + context: string | null = null, ): void { const seq = (this.runLogSeq.get(runId) ?? 0) + 1; this.runLogSeq.set(runId, seq); void this.runLogRepo.save( - this.runLogRepo.create({ runId, stepRunId, level, message, seq }), + this.runLogRepo.create({ runId, stepRunId, level, message, context, seq }), ); } private stepLogger(stepRunId: string, runId: string): ScriptLogger { - return (level, msg) => { + return (level, msg, context) => { this.logger[level](`StepRun #${stepRunId} script: ${msg}`); - this.persistLog(runId, stepRunId, level, msg); + this.persistLog(runId, stepRunId, level, msg, context ?? null); }; } @@ -384,6 +385,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "log", `Step ${step.order} started${step.title ? `: ${step.title}` : ""}`, + `step:${step.order}`, ); try { @@ -402,6 +404,7 @@ export class ScenarioSchedulerService implements OnModuleInit { .browser(context) .code(step.execCode) .log(this.stepLogger(stepRun.id, stepRun.runId)) + .logContext(`step:${step.order}`) .getStepOutput(getStepOutput) .credentials(creds) .environment(env) @@ -527,6 +530,7 @@ export class ScenarioSchedulerService implements OnModuleInit { null, "log", "Playwright trace uploaded to run files (open with `npx playwright show-trace trace.zip` or https://trace.playwright.dev)", + "run", ); } catch (err) { this.logger.warn( @@ -557,6 +561,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "log", `Step ${stepRun.order} passed`, + `step:${stepRun.order}`, ); // Find the next waiting step in this run (next by order) @@ -599,6 +604,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "error", `Step ${stepRun.order} failed: ${description}`, + `step:${stepRun.order}`, ); await this.dumpPageOnFailure(stepRun); @@ -666,6 +672,7 @@ export class ScenarioSchedulerService implements OnModuleInit { stepRun.id, "log", `Step ${stepRun.order} failed: page dump uploaded to run files`, + `step:${stepRun.order}`, ); } } diff --git a/server/src/scenario/scenario.service.ts b/server/src/scenario/scenario.service.ts index b4e7fd6..2387ded 100644 --- a/server/src/scenario/scenario.service.ts +++ b/server/src/scenario/scenario.service.ts @@ -578,7 +578,12 @@ export class ScenarioService implements OnModuleInit { `Run ${runId} not found in scenario ${scenarioId}`, ); const logs = await this.runLogRepo.find({ - where: q?.trim() ? { runId, message: Like(`%${q.trim()}%`) } : { runId }, + where: q?.trim() + ? [ + { runId, message: Like(`%${q.trim()}%`) }, + { runId, context: Like(`%${q.trim()}%`) }, + ] + : { runId }, order: { seq: "ASC", createdAt: "ASC" }, }); return Object.assign(run, { logs }); @@ -606,9 +611,9 @@ export class ScenarioService implements OnModuleInit { const escapeCsv = (value: string): string => /[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value; const rows = [ - ["Timestamp", "Level", "Message"].map(escapeCsv).join(","), + ["Timestamp", "Level", "Context", "Message"].map(escapeCsv).join(","), ...logs.map((l) => - [l.createdAt.toISOString(), l.level, l.message] + [l.createdAt.toISOString(), l.level, l.context ?? "", l.message] .map((v) => escapeCsv(String(v))) .join(","), ), @@ -617,7 +622,8 @@ export class ScenarioService implements OnModuleInit { } const sections = logs.map( - (l) => `## [${l.level}] ${l.createdAt.toISOString()}\n\n${l.message}`, + (l) => + `## [${l.level}]${l.context ? ` (${l.context})` : ""} ${l.createdAt.toISOString()}\n\n${l.message}`, ); return [`# Run ${runId} logs`, ...sections].join("\n\n"); } diff --git a/server/test/migrations.spec.ts b/server/test/migrations.spec.ts index 369c759..6f5a634 100644 --- a/server/test/migrations.spec.ts +++ b/server/test/migrations.spec.ts @@ -48,12 +48,14 @@ describe("Database migrations", () => { } }); - it("can revert the initial migration", async () => { + it("can revert all migrations", async () => { const dataSource = new DataSource(dataSourceOptions(":memory:")); await dataSource.initialize(); try { await dataSource.runMigrations(); - await dataSource.undoLastMigration(); + for (let i = 0; i < MIGRATIONS.length; i++) { + await dataSource.undoLastMigration(); + } const tables = await dataSource.query( `SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT IN ('migrations', 'sqlite_sequence')`, ); -- 2.54.0 From 1273491eee32971cdd18b0baa46adf06e3d35524 Mon Sep 17 00:00:00 2001 From: Andrii Arsenin Date: Thu, 1 Oct 2026 14:09:45 +0300 Subject: [PATCH 2/2] feat(scenario): split log args into message/data and add debug level - context.log/warn/error and console.* split arguments: strings join into message, object/array args are kept out of it and stored as the log entry's new data column (JSON), so message stays a stable string regardless of an object's shape - added a debug log level alongside log/warn/error, including context.debug() and console.debug() (previously folded into log) - CSV/Markdown run log export include the data column/block - LogOutput renders data pretty-printed in a separate block below the message, and dims debug entries with a dedicated grey token calibrated to the WCAG AA contrast floor --- .../.storybook/stories/LogOutput.stories.tsx | 32 +++++++++-- client/src/api/types.ts | 4 +- client/src/pages/run/RunDetailPage.tsx | 1 + client/src/ui/LogOutput/LogOutput.module.css | 37 +++++++++++- client/src/ui/LogOutput/LogOutput.tsx | 33 ++++++++--- client/src/ui/tokens.css | 2 + docs/scenario.md | 6 +- .../code-executor/code-executor.service.ts | 57 ++++++++++++++----- .../migrations/1790850734763-AddRunLogData.ts | 31 ++++++++++ server/src/database/migrations/index.ts | 7 ++- .../src/scenario/scenario-run-log.entity.ts | 7 ++- .../scenario/scenario-scheduler.service.ts | 17 ++++-- server/src/scenario/scenario.service.ts | 15 +++-- 13 files changed, 204 insertions(+), 45 deletions(-) create mode 100644 server/src/database/migrations/1790850734763-AddRunLogData.ts diff --git a/client/.storybook/stories/LogOutput.stories.tsx b/client/.storybook/stories/LogOutput.stories.tsx index 067da07..dbe88de 100644 --- a/client/.storybook/stories/LogOutput.stories.tsx +++ b/client/.storybook/stories/LogOutput.stories.tsx @@ -17,6 +17,13 @@ export const Default: Story = { args: { startTime: secondsAgo(60), entries: [ + { + id: 0, + level: 'debug', + context: 'step:1', + message: 'Resolved credential alias "sdJwtKey" to a file credential', + createdAt: secondsAgo(61), + }, { id: 1, level: 'log', @@ -35,39 +42,54 @@ export const Default: Story = { id: 3, level: 'log', context: 'snippet:loginWithPkcs', - message: 'Navigating to ID login: https://id.example.com', + message: 'Saved credential as run file:', + data: JSON.stringify({ + id: '181d8ae2-4aee-4f33-a657-210efe7fe668', + name: 'credential.sd-jwt', + mimeType: 'application/octet-stream', + size: 1657, + sha256: '9c4226999c700a39bda30279907e4479a5b3920ccde5c75771ea0e264c638c0', + path: 'data/files/18/181d/181d8ae2-4aee-4f33-a657-210efe7fe668', + }), createdAt: secondsAgo(57), }, { id: 4, level: 'log', + context: 'snippet:loginWithPkcs', + message: 'Navigating to ID login: https://id.example.com', + createdAt: secondsAgo(57), + }, + { + id: 5, + level: 'log', context: 'assertion:pass', message: 'Assertion passed: page must have redirected to cabinet URL', createdAt: secondsAgo(40), }, { - id: 5, + id: 6, level: 'warn', context: 'snippet:loginWithPkcs', message: 'Retrying file upload after a transient error', createdAt: secondsAgo(39), }, { - id: 6, + id: 7, level: 'log', context: 'step:1', message: 'Step 1 passed', createdAt: secondsAgo(30), }, { - id: 7, + id: 8, level: 'error', context: 'assertion:fail', message: 'Assertion failed: Document validation must pass (got 422)', createdAt: secondsAgo(10), }, { - id: 8, + id: 9, level: 'error', context: 'step:2', message: 'Step 2 failed: Document validation must pass (got 422)', diff --git a/client/src/api/types.ts b/client/src/api/types.ts index 1a61646..f88d238 100644 --- a/client/src/api/types.ts +++ b/client/src/api/types.ts @@ -147,7 +147,7 @@ export interface RunDefaults { export type RunStepStatus = 'waiting' | 'pending' | 'in_progress' | 'pass' | 'fail' | 'cancelled'; -export type LogLevel = 'log' | 'warn' | 'error'; +export type LogLevel = 'debug' | 'log' | 'warn' | 'error'; export interface ScenarioRun { id: string; @@ -192,6 +192,8 @@ export interface ScenarioRunLog { /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ context: string | null; message: string; + /** JSON payload attached to the message (e.g. a created file's metadata), as a raw string. */ + data: string | null; createdAt: string; } diff --git a/client/src/pages/run/RunDetailPage.tsx b/client/src/pages/run/RunDetailPage.tsx index 1f7cb28..4178e92 100644 --- a/client/src/pages/run/RunDetailPage.tsx +++ b/client/src/pages/run/RunDetailPage.tsx @@ -537,6 +537,7 @@ function RunDetail() { level: l.level, context: l.context, message: l.message, + data: l.data, createdAt: l.createdAt, }))} emptyMessage="" diff --git a/client/src/ui/LogOutput/LogOutput.module.css b/client/src/ui/LogOutput/LogOutput.module.css index eb3a93d..33bae68 100644 --- a/client/src/ui/LogOutput/LogOutput.module.css +++ b/client/src/ui/LogOutput/LogOutput.module.css @@ -17,9 +17,8 @@ .line { display: flex; - align-items: baseline; - flex-wrap: wrap; - gap: var(--space-2); + flex-direction: column; + gap: var(--space-1); padding: var(--space-1) var(--space-3); border-left: 3px solid transparent; border-bottom: 1px solid var(--color-border); @@ -29,6 +28,27 @@ border-bottom: none; } +.lineHeader { + display: flex; + align-items: baseline; + flex-wrap: wrap; + gap: var(--space-2); +} + +.data { + margin: 0 0 var(--space-1) calc(7em + var(--space-2)); + border: var(--border-width) solid var(--color-border); + border-radius: var(--radius-sm); + background: var(--color-bg); + max-width: 100%; + overflow-x: auto; +} + +.data > * { + margin: 0; + padding: var(--space-2) var(--space-3); +} + .timestamp { flex: 0 0 auto; min-width: 7em; @@ -64,6 +84,17 @@ word-break: break-word; } +.debug { + border-left-color: var(--color-border); +} + +.debug .level, +.debug .message, +.debug .timestamp, +.debug .context { + color: var(--color-text-faint); +} + .log { border-left-color: var(--color-border); } diff --git a/client/src/ui/LogOutput/LogOutput.tsx b/client/src/ui/LogOutput/LogOutput.tsx index acdca32..f53190b 100644 --- a/client/src/ui/LogOutput/LogOutput.tsx +++ b/client/src/ui/LogOutput/LogOutput.tsx @@ -1,6 +1,7 @@ +import { CodeBlock } from '../CodeBlock/CodeBlock'; import styles from './LogOutput.module.css'; -export type LogOutputLevel = 'log' | 'warn' | 'error'; +export type LogOutputLevel = 'debug' | 'log' | 'warn' | 'error'; export interface LogOutputEntry { id: string | number; @@ -8,6 +9,8 @@ export interface LogOutputEntry { /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ context?: string | null; message: string; + /** JSON payload attached to the message, as a raw string; rendered pretty-printed, separately from the message. */ + data?: string | null; createdAt: string | number | Date; } @@ -27,6 +30,15 @@ function formatElapsed(deltaMs: number): string { return `${sign}${(Math.abs(deltaMs) / 1000).toFixed(3)}s`; } +/** Pretty-prints `data` if it's valid JSON, otherwise returns it as-is. */ +function prettyData(data: string): string { + try { + return JSON.stringify(JSON.parse(data), null, 2); + } catch { + return data; + } +} + /** Terminal-style log viewer: one line per entry, color-coded by level. */ export function LogOutput({ entries, @@ -52,12 +64,19 @@ export function LogOutput({ const entryMs = new Date(entry.createdAt).getTime(); return (
- - {formatElapsed(entryMs - startMs)} - - {entry.level} - {entry.context && {entry.context}} - {entry.message} +
+ + {formatElapsed(entryMs - startMs)} + + {entry.level} + {entry.context && {entry.context}} + {entry.message} +
+ {entry.data && ( +
+ +
+ )}
); })} diff --git a/client/src/ui/tokens.css b/client/src/ui/tokens.css index 51eda38..068dd21 100644 --- a/client/src/ui/tokens.css +++ b/client/src/ui/tokens.css @@ -46,6 +46,7 @@ --color-text: #2D3339; --color-text-muted: #634DBD; /* Kimberly, a touch darker for 4.5:1 on bg-subtle */ + --color-text-faint: #5C6063; /* desaturated grey, ~4.5:1 on bg-subtle - as washed out as WCAG AA allows */ --color-border: #AFBFB9; /* Powder Ash */ --color-bg: #E6EDEA; /* Powder Ash tint */ --color-bg-subtle: #D8E2DE; /* Powder Ash tint, 1 step darker */ @@ -158,6 +159,7 @@ a:hover { --color-text: #E2EAE6; --color-text-muted: #A9A3C9; /* lightened Kimberly */ + --color-text-faint: #9A9A9A; /* desaturated grey, ~4.5:1 on dark bg-subtle - as washed out as WCAG AA allows */ --color-border: #3D4850; --color-bg: #1A1F24; /* deeper than Outer Space */ --color-bg-subtle: #2D3339; /* Outer Space */ diff --git a/docs/scenario.md b/docs/scenario.md index e861e2a..7738347 100644 --- a/docs/scenario.md +++ b/docs/scenario.md @@ -34,7 +34,7 @@ Available on `context`: - `context.getCredential(alias)`: payload of the credential assigned to the scenario under `alias` (parsed JSON when the data is JSON; throws if missing) - `context.getStepOutput(order)`: output of a previous step by absolute order (`1`, `2`, ...) or relative (`-1` = previous step); `null` if there is none - `context.dumpDom(selector?)`: simplified DOM snapshot of the page (or of one element) -- `context.log(...args)`, `context.warn(...args)`, `context.error(...args)`: run log entries +- `context.log(...args)`, `context.warn(...args)`, `context.error(...args)`, `context.debug(...args)`: run log entries - `context.assert(() => boolean, description)`: runs the arrow function and requires it to return `true`. Logs `description` (as a `log` entry on success, as an `error` entry on failure or exception), then throws to stop the step if the function returned anything other than `true` or threw - `context.runSnippet(name, ...args)`: runs stored snippet code with the same context; the snippet receives the extra arguments as `...args`. `name` is the snippet alias for its latest version, or `alias@` (e.g. `login@3`) to pin a specific version. Snippet start/finish/failure is logged - `context.getScenarioFiles(opts?)`: list files uploaded to the scenario (`limit`, `offset`), including their path on disk @@ -42,7 +42,7 @@ Available on `context`: Also in scope: -- `console`: proxied to run logs (`log`, `warn`, `error`, etc.) +- `console`: proxied to run logs (`log`/`info` → `log`, `warn`, `error`, `debug`) - `expect`: Playwright's `expect` (e.g. `await expect(context.page.locator("h1")).toHaveText("Done")`) Code is syntax-checked before it runs, and executes in the server process: it's trusted code, not sandboxed. @@ -56,6 +56,8 @@ Each run log entry (`level`, `message`, `createdAt`) also carries a `context`: w - `assertion:pass` / `assertion:fail`: `context.assert()`'s own log entry - `run`: run-level entries not tied to a step (e.g. a scenario timeout, or the Playwright trace upload notice) +`context.log`/`warn`/`error` (and `console.log`/etc.) split their arguments: strings/numbers/booleans are joined into `message`, while object/array arguments are kept out of it and stored as `data` (JSON), so `message` stays a stable string regardless of an object's shape. With one object argument `data` is that object; with more than one, `data` is an array of them. E.g. `context.log("Saved credential as run file:", fileMeta)` logs `message: "Saved credential as run file:"` with `fileMeta` as `data`, shown pretty-printed and separately from the message in the UI. + `GET /scenarios/:id/runs/:runId?q=...` and the exported CSV/Markdown logs (`GET /scenarios/:id/runs/:runId/export`) match `q` against both `message` and `context`. ## Versioning diff --git a/server/src/code-executor/code-executor.service.ts b/server/src/code-executor/code-executor.service.ts index 80c5cd9..d908121 100644 --- a/server/src/code-executor/code-executor.service.ts +++ b/server/src/code-executor/code-executor.service.ts @@ -19,10 +19,12 @@ export interface ExecResult { } export type ScriptLogger = ( - level: "log" | "warn" | "error", + level: "debug" | "log" | "warn" | "error", message: string, /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ context?: string | null, + /** Structured payload attached to the message (e.g. a created file's metadata). */ + data?: unknown, ) => void; /** The `context` object available inside user scripts as the sole argument. */ @@ -44,6 +46,7 @@ export interface ScriptContext { log: (...args: unknown[]) => void; warn: (...args: unknown[]) => void; error: (...args: unknown[]) => void; + debug: (...args: unknown[]) => void; /** * Runs `fn` and requires it to return `true`. Logs the outcome under * `description`, then throws if `fn` returned anything other than `true` @@ -125,10 +128,27 @@ export class CodeExecutorService { } = ctx; const scriptLog: ScriptLogger = log ?? ((level, msg) => this.logger[level](msg)); - const toStr = (args: unknown[]) => - args - .map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a))) - .join(" "); + + // Splits console/log args into a plain-text message (strings, numbers, ...) + // and a structured data payload (objects/arrays), logged separately so the + // message stays a stable string regardless of the object's shape. + const splitLogArgs = ( + args: unknown[], + ): { message: string; data: unknown } => { + const strings: string[] = []; + const objects: unknown[] = []; + for (const a of args) { + if (a !== null && typeof a === "object") { + objects.push(a); + } else { + strings.push(String(a)); + } + } + return { + message: strings.join(" "), + data: objects.length === 0 ? null : objects.length === 1 ? objects[0] : objects, + }; + }; // Innermost running snippet (if any) tags log entries; falls back to // logContext (e.g. the current step) outside of any runSnippet call. @@ -136,10 +156,16 @@ export class CodeExecutorService { const currentContext = (): string | null => contextStack.length > 0 ? contextStack[contextStack.length - 1] : null; const emit = ( - level: "log" | "warn" | "error", + level: "debug" | "log" | "warn" | "error", message: string, contextOverride?: string, - ): void => scriptLog(level, message, contextOverride ?? currentContext()); + data?: unknown, + ): void => + scriptLog(level, message, contextOverride ?? currentContext(), data); + const emitArgs = (level: "debug" | "log" | "warn" | "error", args: unknown[]): void => { + const { message, data } = splitLogArgs(args); + emit(level, message, undefined, data); + }; const credMap: Record = credentials ?? {}; const envData: EnvironmentData = environment ?? {}; @@ -149,11 +175,11 @@ export class CodeExecutorService { let scriptContext: ScriptContext; const fakeConsole = { - log: (...args: unknown[]) => emit("log", toStr(args)), - warn: (...args: unknown[]) => emit("warn", toStr(args)), - error: (...args: unknown[]) => emit("error", toStr(args)), - info: (...args: unknown[]) => emit("log", toStr(args)), - debug: (...args: unknown[]) => emit("log", toStr(args)), + log: (...args: unknown[]) => emitArgs("log", args), + warn: (...args: unknown[]) => emitArgs("warn", args), + error: (...args: unknown[]) => emitArgs("error", args), + info: (...args: unknown[]) => emitArgs("log", args), + debug: (...args: unknown[]) => emitArgs("debug", args), }; try { @@ -161,9 +187,10 @@ export class CodeExecutorService { page, browser, dumpDom: (selector?: string) => dumpDom(page, selector), - log: (...args: unknown[]) => emit("log", toStr(args)), - warn: (...args: unknown[]) => emit("warn", toStr(args)), - error: (...args: unknown[]) => emit("error", toStr(args)), + log: (...args: unknown[]) => emitArgs("log", args), + warn: (...args: unknown[]) => emitArgs("warn", args), + error: (...args: unknown[]) => emitArgs("error", args), + debug: (...args: unknown[]) => emitArgs("debug", args), assert: async ( fn: () => boolean | Promise, description: string, diff --git a/server/src/database/migrations/1790850734763-AddRunLogData.ts b/server/src/database/migrations/1790850734763-AddRunLogData.ts new file mode 100644 index 0000000..4e90aad --- /dev/null +++ b/server/src/database/migrations/1790850734763-AddRunLogData.ts @@ -0,0 +1,31 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddRunLogData1790850734763 implements MigrationInterface { + name = "AddRunLogData1790850734763"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "temporary_scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), "context" text, "data" text, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context" FROM "scenario_run_logs"`, + ); + await queryRunner.query(`DROP TABLE "scenario_run_logs"`); + await queryRunner.query( + `ALTER TABLE "temporary_scenario_run_logs" RENAME TO "scenario_run_logs"`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "scenario_run_logs" RENAME TO "temporary_scenario_run_logs"`, + ); + await queryRunner.query( + `CREATE TABLE "scenario_run_logs" ("id" varchar PRIMARY KEY NOT NULL, "runId" varchar NOT NULL, "stepRunId" varchar, "level" text NOT NULL DEFAULT ('log'), "message" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "seq" integer NOT NULL DEFAULT (0), "context" text, CONSTRAINT "FK_62d82b9fce3a6a2947ed1136c67" FOREIGN KEY ("stepRunId") REFERENCES "scenario_run_steps" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, CONSTRAINT "FK_dde48478a42823e583a360dd468" FOREIGN KEY ("runId") REFERENCES "scenario_runs" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "scenario_run_logs"("id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context") SELECT "id", "runId", "stepRunId", "level", "message", "createdAt", "seq", "context" FROM "temporary_scenario_run_logs"`, + ); + await queryRunner.query(`DROP TABLE "temporary_scenario_run_logs"`); + } +} diff --git a/server/src/database/migrations/index.ts b/server/src/database/migrations/index.ts index e096d6e..9450fc1 100644 --- a/server/src/database/migrations/index.ts +++ b/server/src/database/migrations/index.ts @@ -1,8 +1,13 @@ import { Initial1790793447827 } from "./1790793447827-Initial"; import { AddRunLogContext1790848194828 } from "./1790848194828-AddRunLogContext"; +import { AddRunLogData1790850734763 } from "./1790850734763-AddRunLogData"; /** * All migrations, oldest first. Listed explicitly (instead of a glob) so the * same list works from compiled JS, ts-jest and the TypeORM CLI. */ -export const MIGRATIONS = [Initial1790793447827, AddRunLogContext1790848194828]; +export const MIGRATIONS = [ + Initial1790793447827, + AddRunLogContext1790848194828, + AddRunLogData1790850734763, +]; diff --git a/server/src/scenario/scenario-run-log.entity.ts b/server/src/scenario/scenario-run-log.entity.ts index a2b4458..b839b71 100644 --- a/server/src/scenario/scenario-run-log.entity.ts +++ b/server/src/scenario/scenario-run-log.entity.ts @@ -9,7 +9,7 @@ import { import { ScenarioRunStepEntity } from "./scenario-run-step.entity"; import { ScenarioRunEntity } from "./scenario-run.entity"; -export type LogLevel = "log" | "warn" | "error"; +export type LogLevel = "debug" | "log" | "warn" | "error"; @Entity("scenario_run_logs") export class ScenarioRunLogEntity { @@ -43,6 +43,11 @@ export class ScenarioRunLogEntity { @Column({ type: "text" }) message: string; + // JSON payload attached to the message (e.g. a created file's metadata), + // kept separate so message stays a stable, human-readable string. + @Column({ type: "text", nullable: true }) + data: string | null; + // Assigned in call order per run, since concurrent unawaited writes can // otherwise persist out of order (createdAt alone isn't a reliable tiebreaker). @Column({ type: "int", default: 0 }) diff --git a/server/src/scenario/scenario-scheduler.service.ts b/server/src/scenario/scenario-scheduler.service.ts index 56a5c7c..b4d5c75 100644 --- a/server/src/scenario/scenario-scheduler.service.ts +++ b/server/src/scenario/scenario-scheduler.service.ts @@ -149,21 +149,30 @@ export class ScenarioSchedulerService implements OnModuleInit { private persistLog( runId: string, stepRunId: string | null, - level: "log" | "warn" | "error", + level: "debug" | "log" | "warn" | "error", message: string, context: string | null = null, + data: unknown = null, ): void { const seq = (this.runLogSeq.get(runId) ?? 0) + 1; this.runLogSeq.set(runId, seq); void this.runLogRepo.save( - this.runLogRepo.create({ runId, stepRunId, level, message, context, seq }), + this.runLogRepo.create({ + runId, + stepRunId, + level, + message, + context, + data: data != null ? JSON.stringify(data) : null, + seq, + }), ); } private stepLogger(stepRunId: string, runId: string): ScriptLogger { - return (level, msg, context) => { + return (level, msg, context, data) => { this.logger[level](`StepRun #${stepRunId} script: ${msg}`); - this.persistLog(runId, stepRunId, level, msg, context ?? null); + this.persistLog(runId, stepRunId, level, msg, context ?? null, data ?? null); }; } diff --git a/server/src/scenario/scenario.service.ts b/server/src/scenario/scenario.service.ts index 2387ded..90d0357 100644 --- a/server/src/scenario/scenario.service.ts +++ b/server/src/scenario/scenario.service.ts @@ -611,9 +611,9 @@ export class ScenarioService implements OnModuleInit { const escapeCsv = (value: string): string => /[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value; const rows = [ - ["Timestamp", "Level", "Context", "Message"].map(escapeCsv).join(","), + ["Timestamp", "Level", "Context", "Message", "Data"].map(escapeCsv).join(","), ...logs.map((l) => - [l.createdAt.toISOString(), l.level, l.context ?? "", l.message] + [l.createdAt.toISOString(), l.level, l.context ?? "", l.message, l.data ?? ""] .map((v) => escapeCsv(String(v))) .join(","), ), @@ -621,10 +621,13 @@ export class ScenarioService implements OnModuleInit { return rows.join("\n"); } - const sections = logs.map( - (l) => - `## [${l.level}]${l.context ? ` (${l.context})` : ""} ${l.createdAt.toISOString()}\n\n${l.message}`, - ); + const sections = logs.map((l) => { + const header = `## [${l.level}]${l.context ? ` (${l.context})` : ""} ${l.createdAt.toISOString()}`; + const data = l.data + ? `\n\n\`\`\`json\n${JSON.stringify(JSON.parse(l.data), null, 2)}\n\`\`\`` + : ""; + return `${header}\n\n${l.message}${data}`; + }); return [`# Run ${runId} logs`, ...sections].join("\n\n"); } -- 2.54.0