Repository navigation
Description
Background & Problem
The CEL filter fields (timelineQuery, timelineExclusionQuery, and logQuery) handle severities as plain integers. These integers come from two sources that are not linked:
- The CEL constants
UNKNOWN,INFO,WARNING,ERROR, andFATALare declared asintand bound to the fixed values0to4inpkg/server/workbench/cel/env.go. - The severity of a log is the
Orderfield of its severity definition in the style chunk of the loaded.khifile. The definitions are registered inpkg/task/inspection/inspectioncore/severity.go.
This causes three problems:
- The constants can drift from the file. The hardcoded values match today only because the registered orders are also
0to4. If a severity is added, renamed, or reordered, the CEL constants become wrong without any error. The CEL references (khi://cel/timelineandkhi://cel/log) are generated from the style chunk, so they can also list a constant or a value that the CEL environment does not have. - Severities are raw integers in CEL.
severity,minSeverity(int), andhasSeverity(int | list<int>)accept any integer. Expressions such asseverity == 2orhasSeverity(7)compile, but their meaning depends on internal order values. The review of feat(workbench): add SearchTimelines tree aggregation for MCP (#1043) #1089 raised the same point for the MCP search tools: severities should not be used as integers. hasSeveritycannot match a severity with an order of 8 or more.indexTimelinesParallelstores the severities of each timeline in the 8-bitTimelineData.SeverityMaskand silently skips orders of 8 or more. ThehasSeveritybinding also returnsfalsefor values outside0to7. The current definitions do not reach this limit, but nothing prevents a new definition from reaching it.
The same filter pipeline serves the Web UI (Workbench.FilterTimeline) and the MCP tools (Workbench.ExecuteFilter), so both are affected.
Proposed Solution
Make the severity definitions in the style chunk the single source of truth for CEL:
- Generate the CEL severity constants from the style chunk of the loaded file, one constant per definition, named by its upper-case label. Remove the hardcoded constants and values from
env.go. - Give severities their own CEL type backed by the definitions.
severity,minSeverity, andhasSeverityaccept only severity values. Comparison operators such asseverity >= WARNINGkeep working and compare by theOrderfield of the definition. Integer literals no longer work as severities. - Replace the integer
StyleResolver.ResolveSeveritywith a lookup that returns the*khifilev6.Severitydefinition.buildSeverityMapinpkg/server/workbench/severity_count.go, added in feat(workbench): add SearchTimelines tree aggregation for MCP (#1043) #1089, already builds this lookup for the MCP search tools. - Replace the 8-bit
TimelineData.SeverityMaskwith a set that has no size limit, such as a set of severity IDs. Keep the most severe definition of each timeline forminSeverity. - Generate the CEL references from the same definitions, and describe severities as named constants without integer values.
Design note: CEL expressions are currently compiled without a loaded file in two places. CELValidationServer validates queries for the Web UI with only the query text, and the MCP tools call workbench.ValidateFilter before they acquire the workbench. With per-file constants, these checks need the style chunk of the target inspection, or the check for unknown severity names must move to the point where the file is loaded.
Alternatives Considered
- Generate integer constants from the style chunk but keep the
inttype: This fixes the drift with a small change. However, integer literals still compile as severities, which goes against the review of feat(workbench): add SearchTimelines tree aggregation for MCP (#1043) #1089. - Use severity labels as strings, such as
minSeverity("ERROR"): This needs no custom CEL type. However, a typo in a label is found only at evaluation time unless labels are checked during compilation, and the order still needs a lookup. It also changes the expressions that the Web UI toolbar generates.
Out of Scope
- Severity counts in the MCP
search_timelinesandsearch_logstools. feat(workbench): add SearchTimelines tree aggregation for MCP (#1043) #1089, feat(workbench): add SearchLogs cross-timeline aggregation for MCP (#1043) #1090, and feat(mcp): add search_timelines and search_logs tools (#1043) #1091 already count them by severity definition. - Changes to the
.khifile format or to the registered severity definitions.
Acceptance Criteria
-
pkg/server/workbench/cel/env.gohas no hardcoded severity constants or values. The constants come from the style chunk of the loaded file. -
severity,minSeverity, andhasSeverityuse a severity type backed by the style chunk definitions. Comparing a severity with an integer literal fails at compile time. - Documented expressions such as
severity >= WARNINGandminSeverity(ERROR)keep working, including the expressions that the Web UI toolbar generates. -
minSeverityandhasSeveritywork for any number of definitions and anyOrdervalue. - The CEL references list exactly the severity constants that the CEL environment declares.
- Tests cover a style chunk with a custom severity, such as a new label or an order of 8 or more.
Related Code & Context
pkg/server/workbench/cel/env.go:NewTimelineEvaluatorandNewLogEvaluatordeclare the severity constants ascel.IntType.TimelineEvaluator.EvaluateandLogEvaluator.Evaluatebind them to0to4. TheminSeverityandhasSeveritybindings compare integers.pkg/server/workbench/cel/types.go:TimelineData.MaxSeverity,TimelineData.SeverityMask,StyleResolver.ResolveSeverity, andSimpleStyleResolver.Severitieshold severities as integer orders.pkg/server/workbench/index.go:buildStyleMapsbuilds the map from severity ID to order.indexTimelinesParallelcomputesMaxSeverityandSeverityMaskand skips orders of 8 or more.pkg/server/workbench/cel/reference.go,pkg/server/workbench/cel/templates/cel-timeline.md.tmpl, andpkg/server/workbench/cel/templates/cel-log.md.tmpl: generate the CEL references from the style chunk and document severities as integer constants.pkg/server/api/v1/cel_validation.goandValidateFilterinpkg/server/workbench/read.go: compile CEL expressions without a loaded file.web/src/app/timeline-toolbar/timeline-toolbar-smart.component.ts: buildsminSeverity(<constant>)from the severity selector, so it depends on the constant names.pkg/task/inspection/inspectioncore/severity.go: registersUNKNOWN,INFO,WARNING,ERROR, andFATALwith orders0to4.pkg/server/workbench/severity_count.go, added in feat(workbench): add SearchTimelines tree aggregation for MCP (#1043) #1089: resolves a log severity ID to its*khifilev6.Severitydefinition.- Related PR / Issue: feat(workbench): add SearchTimelines tree aggregation for MCP (#1043) #1089, feat(workbench): add SearchLogs cross-timeline aggregation for MCP (#1043) #1090, feat(mcp): add search_timelines and search_logs tools (#1043) #1091, MCP: Tools to search timelines and logs #1043, [Epic] KHI MCP server initial release #1049
Activity