Skip to content

Extract shared constants into package constants.tql - #163

Merged
jachris merged 2 commits into
mainfrom
topic/extract-package-lets
Aug 6, 2026
Merged

Extract shared constants into package constants.tql#163
jachris merged 2 commits into
mainfrom
topic/extract-package-lets

Conversation

@jachris

@jachris jachris commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

🔍 Problem

OCSF mappers across several library packages copy-paste the same constant
lookup tables (hash-algorithm IDs, severity/status/verdict enums, FortiGate
dispositions). The copies drift: FortiGate CIFS/SSH omit the monitored
disposition their sibling mappers have, so a monitored action silently maps
to 0 (Unknown) instead of 17.

🛠️ Solution

Hoist the genuinely duplicated constant maps into a per-package constants.tql
and reference them as pkg::$name (the new package let bindings).

  • constants.tql added to sysmon, microsoft, okta, fortinet, and sophos.
  • Mappers using a differently-shaped map of the same name keep their local let.
  • Routing FortiGate CIFS/SSH through the shared fortinet::$utm_dispositions
    also fixes the missing monitored mapping (separate commit + changelog).

💬 Review

  • No behavior change apart from the FortiGate CIFS/SSH monitored fix.
  • tenzir-test against a local build: sysmon 30/30, fortinet 1/1, okta 7/7,
    sophos 7/7, microsoft 74/75 — the one failure is a pre-existing, unrelated
    ASIM test (reproduces on a clean tree).
  • Depends on the engine PR shipping constants.tql; library CI runs against
    released Tenzir via uvx, so it stays red until that release.
📚 Docs PR: tenzir/docs#413
📎 Related: tenzir/tenzir#6363

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@jachris
jachris force-pushed the topic/extract-package-lets branch from 03171ae to 399455b Compare June 24, 2026 12:46
@jachris jachris changed the title Extract shared constants into package lets.tql Extract shared constants into package constants.tql Jun 24, 2026
@jachris
jachris force-pushed the topic/extract-package-lets branch from 399455b to d1b8b28 Compare June 24, 2026 13:20
@mavam

mavam commented Jun 25, 2026

Copy link
Copy Markdown
Member

I'm surprised there are so few packages we are extracting constants from. Have you tried Codex for the same task once?

@jachris

jachris commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

I'm surprised there are so few packages we are extracting constants from. Have you tried Codex for the same task once?

My instructions were that it should only extract shared constants. If something is only used locally, I think it makes sense to stay there. Or do you see anything that would still be worthwhile to refactor? (Also, feel free to push to this RP if you want)

@mavam

mavam commented Jun 26, 2026

Copy link
Copy Markdown
Member

All good, i just wondered if you went through all packages or hand-picked a few. I trust that this is complete for what we have.

jachris added a commit to tenzir/tenzir that referenced this pull request Jun 26, 2026
## 🔍 Problem

Packages bundle operators, pipelines, and contexts, but offered no way
to
define shared named constants. Authors had to repeat literal values
across a
package's operators and pipelines.

## 🛠️ Solution

A package can define `let` bindings in a `lets.tql` file at its root.
Each
binding is a compile-time constant, referenceable as `pkg::$name` — from
the
package's own operators and pipelines, and from any external pipeline
that uses
the package. Later bindings may reference earlier ones.

```tql
// acme/lets.tql
let $high_severity = 8
let $threshold = $high_severity + 1
```

```tql
where severity >= acme::$threshold
```

References are resolved and const-evaluated lazily at each use site,
where the
full registry is available — mirroring how user-defined operators
resolve. A
binding must be a deterministic constant; `let $r = random()` is
rejected,
since per-reference evaluation would otherwise yield differing values.

## 💬 Review

- New `pkg_dollar_var` AST node and `let` entity namespace; references
resolve
  against the `packages` domain and cache their constant on the node.
- Bindings are stored unevaluated at load and const-evaluated lazily on
first
reference, so sibling and cross-package references resolve against a
complete
  registry.
- Operators and lets register through a shared `build_package_module`.
- `expression::is_deterministic` is made exact and exhaustive so
composite
constants (lists, records, …) are no longer misclassified; it gates the
  determinism check.
- Tests cover internal and external references, sibling references, and
the
error paths (unknown binding, unknown package, forward reference,
non-`let`
statement in `lets.tql`, reference cycle, non-constant and
non-deterministic
  bindings).

<sub>
📎 Plugins PR: tenzir/tenzir-plugins#568<br>
📚 Docs PR: tenzir/docs#413<br>
📎 Related: tenzir/library#163
</sub>
jachris and others added 2 commits August 6, 2026 16:49
Several OCSF mappers copy-pasted the same constant lookup maps (hash
algorithm ids, signature states, severity/status/verdict enums, ...)
across many operator files. Hoist the genuinely duplicated, constant
ones into a per-package `constants.tql` and reference them as
`pkg::$name`, the new package-level `let` bindings.

Mappers that use a differently-shaped map of the same name (e.g. Okta's
extended ALLOW/DENY status map, Microsoft's Defender alert status, the
per-class FortiGate disposition vocabularies) keep their local `let`.

No behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The CIFS and SSH mappers had a two-entry disposition map (blocked,
allowed) while the sibling UTM content-inspection mappers (webfilter,
emailfilter, virus) also map `monitored` to OCSF disposition_id 17.
A `monitored` action on a CIFS/SSH log therefore fell through to 0
(Unknown).

Route both through the shared `fortinet::$utm_dispositions` binding,
which includes `monitored`, fixing the silent misclassification.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jachris
jachris force-pushed the topic/extract-package-lets branch from d1b8b28 to fdd3067 Compare August 6, 2026 14:53
@jachris
jachris merged commit 8e6313b into main Aug 6, 2026
3 checks passed
@jachris
jachris deleted the topic/extract-package-lets branch August 6, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants