Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(mcp-host): route workflowService logs through the structured logger
Jose Gabriel Ramirez Encinase83a0a610361
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex a91328d01..7119d9077 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.343",+ "version": "0.1.344","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.343",+ "version": "0.1.344","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex b0099c671..f71804582 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.343",+ "version": "0.1.344","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/workflow/__tests__/resolveMaxStepTimeoutSeconds.test.ts b/mcp-host/src/workflow/__tests__/resolveMaxStepTimeoutSeconds.test.tsindex d3671ebe4..a3fa4028b 100644--- a/mcp-host/src/workflow/__tests__/resolveMaxStepTimeoutSeconds.test.ts+++ b/mcp-host/src/workflow/__tests__/resolveMaxStepTimeoutSeconds.test.ts@@ -1,11 +1,12 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { logger } from '../../logger'import { resolveMaxStepTimeoutSeconds } from '../workflowService'describe('resolveMaxStepTimeoutSeconds', () => {let warnSpy: ReturnType<typeof vi.spyOn>beforeEach(() => {- warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})+ warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {})})afterEach(() => {@@ -33,7 +34,18 @@ describe('resolveMaxStepTimeoutSeconds', () => {it('warns and falls back when env is below the min (60s)', () => {expect(resolveMaxStepTimeoutSeconds('30')).toBe(5400)expect(warnSpy).toHaveBeenCalledOnce()- expect(warnSpy.mock.calls[0][0]).toMatch(/Ignoring .*="30"/)+ expect(warnSpy).toHaveBeenCalledWith(+ expect.objectContaining({+ envVar: 'CLERUM_MAX_STEP_TIMEOUT_SECONDS',+ value: '30',+ min: 60,+ max: 604800,+ fallback: 5400,+ }),+ expect.stringMatching(/^Ignoring invalid CLERUM_MAX_STEP_TIMEOUT_SECONDS/)+ )+ // The raw env value is a structured field only, never interpolated into msg.+ expect(warnSpy.mock.calls[0][1]).not.toContain('30')})it('warns and falls back when env is above the CRD hard ceiling (7 days)', () => {diff --git a/mcp-host/src/workflow/__tests__/workflowServiceBudgetCheck.test.ts b/mcp-host/src/workflow/__tests__/workflowServiceBudgetCheck.test.tsindex ffce149a8..63b0710c1 100644--- a/mcp-host/src/workflow/__tests__/workflowServiceBudgetCheck.test.ts+++ b/mcp-host/src/workflow/__tests__/workflowServiceBudgetCheck.test.ts@@ -4,6 +4,7 @@ import type { BudgetCheckRequest } from '../../budget/types'import { config as appConfig } from '../../config'import { LlmErrorCode } from '../../core/errors'import type { SingleTurnProvider } from '../../llm'+import { logger } from '../../logger'import type { McpClientConnection, McpClientFactory } from '../stepRouter'import type { McpHostRuntimeAuth } from '../userApprovalRequester'import { WorkflowService } from '../workflowService'@@ -202,6 +203,32 @@ describe('WorkflowService — P3 pre-step budget check', () => {expect(llm.completeSingleTurnWithTools).not.toHaveBeenCalled()})+ it('deny log keeps the user-controlled stepId and reason out of the message string', async () => {+ appConfig.budgetsEnabled = true+ const reason = 'global_tokens_exceeded\nforged-line level=info'+ const trace = makeBudgetFetch({ allowed: false, reason })+ const client = new BudgetClient({+ baseUrl: 'http://gw',+ getAccessToken: () => 't',+ fetchImpl: trace.fetchImpl,+ })+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined)+ const { svc } = makeService(client)+ const stepId = 's1\n[WorkflowService] forged'++ const res = await svc.executeStep({+ stepId,+ instruction: 'hello',+ contextVars: { workflowExecutionId, workflowTeamId, workflowUserId },+ })++ expect(res.status).toBe('failed')+ expect(warn).toHaveBeenCalledWith(+ { component: 'WorkflowService', stepId, reason },+ 'Step denied by budget'+ )+ })+it('unpriced usage: warns budget_unpriced_usage with the pairs and still executes the step', async () => {appConfig.budgetsEnabled = trueconst unpriced = [@@ -214,7 +241,7 @@ describe('WorkflowService — P3 pre-step budget check', () => {getAccessToken: () => 't',fetchImpl: trace.fetchImpl,})- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined)const { svc, llm } = makeService(client)const res = await svc.executeStep({@@ -226,8 +253,13 @@ describe('WorkflowService — P3 pre-step budget check', () => {expect(res.status).toBe('completed')expect(llm.completeSingleTurn).toHaveBeenCalledTimes(1)expect(warn).toHaveBeenCalledWith(- '[WorkflowService] budget_unpriced_usage',- expect.objectContaining({ stepId: 's1', recipe_name: 'my-recipe', pairs: unpriced })+ expect.objectContaining({+ component: 'WorkflowService',+ stepId: 's1',+ recipe_name: 'my-recipe',+ pairs: unpriced,+ }),+ 'budget_unpriced_usage')})@@ -239,19 +271,17 @@ describe('WorkflowService — P3 pre-step budget check', () => {getAccessToken: () => 't',fetchImpl: trace.fetchImpl,})- const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined)const { svc } = makeService(client)- await svc.executeStep({+ const res = await svc.executeStep({stepId: 's1',instruction: 'hello',contextVars: { workflowExecutionId, workflowTeamId, workflowUserId },})- expect(warn).not.toHaveBeenCalledWith(- '[WorkflowService] budget_unpriced_usage',- expect.anything()- )+ expect(res.status).toBe('completed')+ expect(warn).not.toHaveBeenCalledWith(expect.anything(), 'budget_unpriced_usage')})it('fail-open: a broken check lets the step proceed', async () => {diff --git a/mcp-host/src/workflow/workflowService.ts b/mcp-host/src/workflow/workflowService.tsindex f232f016d..a3bc15882 100644--- a/mcp-host/src/workflow/workflowService.ts+++ b/mcp-host/src/workflow/workflowService.ts@@ -16,6 +16,7 @@ import { buildGfsReadTools, buildGfsWriteTools } from '../internalTools/gfs'import { createGfscClient, hasGfsRuntimeAccess } from '../internalTools/gfsClient'import { SingleTurnProvider, createLLMProvider } from '../llm'import { type LlmProvider, descriptorFor, isLlmProvider } from '../llm/registryCore'+import { logger } from '../logger'import {configurePluginWorkloadSdkBootstrapIdentity,resolvePluginWorkloadSdkBootstrapCapabilityFamily,@@ -126,6 +127,8 @@ function credentialBagForProvider(provider: LlmProvider, apiKey: string): ApiKeyconst MAX_SOUL_BYTES = 64 * 1024 // 64KBconst DEFAULT_TIMEOUT_SECONDS = 300const MAX_WORKFLOW_OUTPUT_TOKENS_CEILING = 65_536+/** Structured-log component label (preserves the former `[WorkflowService]` prefix). */+const LOG_COMPONENT = 'WorkflowService'const PROVIDER_LENGTH_ERROR ='provider_output_length_exceeded: provider stopped because the configured output token limit was reached; write full reports to /output artifacts'const PROVIDER_LENGTH_CONTINUATION_PROMPT =@@ -133,7 +136,7 @@ const PROVIDER_LENGTH_CONTINUATION_PROMPT =/*** Resolve per-step timeout ceiling from CLERUM_MAX_STEP_TIMEOUT_SECONDS.- * Invalid env values fall back to `fallback` (with console.warn). The+ * Invalid env values fall back to `fallback` (with a structured warning). The* `hardCeiling` clamps egregious env typos so a misconfig cannot pin a step* effectively forever. Exported as a pure function for unit-testability.*/@@ -146,10 +149,16 @@ export function resolveMaxStepTimeoutSeconds(if (env === undefined || env.trim() === '') return fallbackconst n = Number(env)if (!Number.isFinite(n) || !Number.isInteger(n) || n < min || n > hardCeiling) {- // eslint-disable-next-line no-console- console.warn(- `[WorkflowService] Ignoring CLERUM_MAX_STEP_TIMEOUT_SECONDS="${env}" ` +- `(must be an integer between ${min} and ${hardCeiling}); falling back to ${fallback}.`+ logger.warn(+ {+ component: LOG_COMPONENT,+ envVar: 'CLERUM_MAX_STEP_TIMEOUT_SECONDS',+ value: env,+ min,+ max: hardCeiling,+ fallback,+ },+ 'Ignoring invalid CLERUM_MAX_STEP_TIMEOUT_SECONDS (must be an integer within bounds); using fallback')return fallback}@@ -792,9 +801,16 @@ export class WorkflowService {buildFallback: (entry: ResolvedFallbackEntry) =>this.llmFactory(entry.provider, entry.model, entry.apiKey),onSwitch: event => {- console.warn(- `[WorkflowService] LLM failover: ${event.from.provider}/${event.from.model} → ` +- `${event.to.provider}/${event.to.model} (reason=${event.reason})`+ logger.warn(+ {+ component: LOG_COMPONENT,+ fromProvider: event.from.provider,+ fromModel: event.from.model,+ toProvider: event.to.provider,+ toModel: event.to.model,+ reason: event.reason,+ },+ 'LLM failover')},})@@ -992,15 +1008,24 @@ export class WorkflowService {// Logged regardless of the verdict so a missing price row is caught before// the drift grows.if (budgetVerdict.unpriced?.length) {- console.warn('[WorkflowService] budget_unpriced_usage', {- stepId: req.stepId,- recipe_name: budgetRecipeName,- pairs: budgetVerdict.unpriced,- })+ logger.warn(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ recipe_name: budgetRecipeName,+ pairs: budgetVerdict.unpriced,+ },+ 'budget_unpriced_usage'+ )}if (!budgetVerdict.allowed) {- console.warn(- `[WorkflowService] Step "${req.stepId}" denied by budget: ${budgetVerdict.reason ?? 'budget_exceeded'}`+ logger.warn(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ reason: budgetVerdict.reason ?? 'budget_exceeded',+ },+ 'Step denied by budget')return {stepId: req.stepId,@@ -1026,8 +1051,9 @@ export class WorkflowService {// 0. Gate on human approval if the step requires itconst runtimeAuth = this.getRuntimeAuth()if (req.requiresApproval && !runtimeAuth) {- console.error(- `[WorkflowService] Step "${req.stepId}" requires approval but runtime auth is not configured`+ logger.error(+ { component: LOG_COMPONENT, stepId: req.stepId },+ 'Step requires approval but runtime auth is not configured')this.revertSoulToGlobal()return {@@ -1039,7 +1065,10 @@ export class WorkflowService {}}if (req.requiresApproval && runtimeAuth) {- console.log(`[WorkflowService] Step "${req.stepId}" requires approval — gating`)+ logger.info(+ { component: LOG_COMPONENT, stepId: req.stepId },+ 'Step requires approval; gating'+ )try {await gateStep({@@ -1051,11 +1080,21 @@ export class WorkflowService {},runtimeAuth)- console.log(`[WorkflowService] Step "${req.stepId}" approval granted — proceeding`)+ logger.info(+ { component: LOG_COMPONENT, stepId: req.stepId },+ 'Step approval granted; proceeding'+ )} catch (approvalErr) {const approvalMsg =approvalErr instanceof Error ? approvalErr.message : String(approvalErr)- console.error(`[WorkflowService] Step "${req.stepId}" approval denied: ${approvalMsg}`)+ logger.error(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ reason: sanitizeErrorMessage(approvalMsg),+ },+ 'Step approval denied'+ )this.revertSoulToGlobal()return {stepId: req.stepId,@@ -1172,8 +1211,20 @@ export class WorkflowService {}const promptTokenEstimate = messages.reduce((a, m) => a + m.content.length, 0)- console.log(- `[WorkflowService] LLM call #${i + 1}/${MAX_ITERATIONS} for step "${req.stepId}" (${messages.length} msgs, ~${Math.round(promptTokenEstimate / 4)} tokens, timeout: ${timeoutMs}ms)`+ // Field names avoid the logger's sensitive-key pattern (/token/i), which+ // would otherwise redact these non-secret size/usage counters.+ logger.info(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ iteration: i + 1,+ maxIterations: MAX_ITERATIONS,+ messageCount: messages.length,+ promptChars: promptTokenEstimate,+ promptSizeEstimate: Math.round(promptTokenEstimate / 4),+ timeoutMs,+ },+ 'LLM call started')const shouldForceFinalText =remainingIterations <= 1 && (!requiresToolCall || toolsCalled.length > 0)@@ -1193,8 +1244,20 @@ export class WorkflowService {timeoutMs,abortController.signal)- console.log(- `[WorkflowService] LLM response #${i + 1}: tool_calls=${response.tool_calls?.length ?? 0}, content_len=${response.content?.length ?? 0}, tokens=${JSON.stringify(response.usage ?? {})}`+ logger.info(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ iteration: i + 1,+ toolCallCount: response.tool_calls?.length ?? 0,+ contentLength: response.content?.length ?? 0,+ inputUsage: response.usage?.input_tokens,+ outputUsage: response.usage?.output_tokens,+ totalUsage: response.usage?.total_tokens,+ cacheReadUsage: response.usage?.cache_read_tokens,+ cacheWriteUsage: response.usage?.cache_write_tokens,+ },+ 'LLM response received')totalInputTokens += response.usage?.input_tokens ?? 0@@ -1237,8 +1300,9 @@ export class WorkflowService {const candidateOutput = response.content ?? ''if (requiresToolCall && toolsCalled.length === 0) {if (i < MAX_ITERATIONS - 1) {- console.warn(- `[WorkflowService] Step "${req.stepId}" required a tool call but response #${i + 1} returned none; retrying with tool requirement`+ logger.warn(+ { component: LOG_COMPONENT, stepId: req.stepId, iteration: i + 1 },+ 'Step required a tool call but the response returned none; retrying with tool requirement')messages.push({role: 'assistant',@@ -1252,8 +1316,9 @@ export class WorkflowService {})continue}- console.error(- `[WorkflowService] Step "${req.stepId}" required a tool call but the provider returned no structured tool_calls`+ logger.error(+ { component: LOG_COMPONENT, stepId: req.stepId },+ 'Step required a tool call but the provider returned no structured tool_calls')this.revertSoulToGlobal()return {@@ -1271,8 +1336,9 @@ export class WorkflowService {lengthPartialOutput = candidateOutputmessages.push({ role: 'assistant', content: candidateOutput })messages.push({ role: 'user', content: PROVIDER_LENGTH_CONTINUATION_PROMPT })- console.warn(- `[WorkflowService] Step "${req.stepId}" hit provider output token limit; requesting one bounded continuation`+ logger.warn(+ { component: LOG_COMPONENT, stepId: req.stepId, iteration: i + 1 },+ 'Step hit provider output token limit; requesting one bounded continuation')continue}@@ -1289,8 +1355,14 @@ export class WorkflowService {)if (isSuspicious && i < MAX_ITERATIONS - 1) {- console.log(- `[WorkflowService] Step "${req.stepId}" response #${i + 1} looks suspicious (${candidateOutput.length} chars), requesting proper summary`+ logger.info(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ iteration: i + 1,+ outputLength: candidateOutput.length,+ },+ 'Step response looks suspicious; requesting proper summary')messages.push({ role: 'assistant', content: candidateOutput })messages.push({@@ -1335,8 +1407,9 @@ export class WorkflowService {lengthPartialOutput = candidateOutputmessages.push({ role: 'assistant', content: candidateOutput })messages.push({ role: 'user', content: PROVIDER_LENGTH_CONTINUATION_PROMPT })- console.warn(- `[WorkflowService] Step "${req.stepId}" hit provider output token limit; requesting one bounded continuation`+ logger.warn(+ { component: LOG_COMPONENT, stepId: req.stepId, iteration: i + 1 },+ 'Step hit provider output token limit; requesting one bounded continuation')continue}@@ -1385,8 +1458,14 @@ export class WorkflowService {finalOutput =`[Note: Research reached the iteration limit (${MAX_ITERATIONS} iterations, ${toolsCalled.length} tool calls: ${toolSummary}). ` +`The information gathered above represents partial results. A follow-up step can synthesize these findings.]`- console.log(- `[WorkflowService] Step "${req.stepId}" reached max iterations (${MAX_ITERATIONS}), returning partial results with ${toolsCalled.length} tool calls`+ logger.info(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ maxIterations: MAX_ITERATIONS,+ toolCallCount: toolsCalled.length,+ },+ 'Step reached max iterations; returning partial results')}@@ -1408,12 +1487,18 @@ export class WorkflowService {const errStatus = extractHttpStatus(err)const cause =err instanceof Error && err.cause instanceof Error ? err.cause.message : undefined- console.error('[WorkflowService] Step caught error', {- stepId: req.stepId,- error: rawError,- cause,- stack: err instanceof Error ? err.stack?.split('\n').slice(0, 3).join(' ') : undefined,- })+ const stackHead =+ err instanceof Error ? err.stack?.split('\n').slice(0, 3).join(' ') : undefined+ logger.error(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ error: sanitizeErrorMessage(rawError),+ cause: cause === undefined ? undefined : sanitizeErrorMessage(cause),+ stack: stackHead === undefined ? undefined : sanitizeErrorMessage(stackHead),+ },+ 'Step caught error'+ )// F-9 fix: surface timeout errors with canonical error code (matches abort-check at loop top)if (rawError === 'step-timeout') {return {@@ -1441,7 +1526,17 @@ export class WorkflowService {: `LLM authentication failed (${errStatus}): ${providerLabel}/${modelLabel} rejected the API key. ` +`The key may be expired, invalid, or lack permission for model "${modelLabel}". ` +`Fix: verify the API key in the Kubernetes Secret is correct and active.`- console.error(`[WorkflowService] AUTH ERROR: ${diagnostic}`)+ logger.error(+ {+ component: LOG_COMPONENT,+ stepId: req.stepId,+ status: errStatus,+ provider: providerLabel,+ model: modelLabel,+ diagnostic: sanitizeErrorMessage(diagnostic),+ },+ 'LLM authentication error'+ )return {stepId: req.stepId,status: 'failed',