Product
Platform overview Tomosu engine (open source) Tomosu Cloud Reliability rules
Solutions
Prevent outages before merge Reliability Radar Fix safety For developers For SRE and platform teams
Developers
Quickstart Documentation Contribute a rule
Resources
Blog Case studies Learn FAQ News
Company
Pricing About Contact Join the Waitlist
Theme

Write a rule

A rule is a folder with a check.yaml and examples of code it must and must not flag. tomosu rules new scaffolds one, rules test runs it against its examples, and rules try runs it on your code. A rule may only read facts the indexer really gives.

  • rules new
  • rules test
  • rules create
  • Updated

Before you start, check Community rules: the failure you have in mind may already have a check, which you can customize instead.

Which way should I write it?

WayGood for
From a few answers: Community Rules → New rule, or tomosu rules create“Flag calls to these functions when…”: a missing argument, a call in a loop, an ignored result, a call outside try
By hand: tomosu rules new, then edit check.yamlAnything a CEL condition over the code facts can say

Either way, a new rule starts in shadow: it runs and is counted, but its findings are not shown until you set status: active.

How do I make a rule from a few answers?

Say what to look at, when it is a problem and how to fix it, and give one example of bad code (and, optionally, good code). The rule is written in the usual format, tested on your examples and saved.

$ tomosu rules create --name Redis client without a socket timeout --category 01 --calls redis.Redis --when missing-arg --arg socket_timeout --value 2 --risk A Redis call can hang when the server stops answering. --fix Pass socket_timeout so a stuck call fails fast. --bad bad.py --good good.py --out myrules
# Made with the rule builder. Edit it like any rule; it stays in shadow
# (evaluated, never shown) until you set status: active.
checks:
    - id: 01.redis-client-without-a-socket-timeout
      category: "01"
      kind: pre-merge
      name: Redis client without a socket timeout
      version: 1
      status: shadow
      tier: unassigned
      contributor: local
      weight: 0.75
      scope:
        languages:
            - python
      detect:
        analyzer: cel
        condition: |-
            !site.is_decorator && site.callee.matches(params.calls) && !site.has_kwargs_splat && !("socket_timeout" in site.kwargs)
        params:
            calls: ^(redis\.Redis)$
        target: call
      risk: A Redis call can hang when the server stops answering.
      remediation:
        fix:
            steps:
                - action: add-kwarg
                  kwarg: socket_timeout
                  values:
                    - value: "2"
                      when: .
        summary: Pass socket_timeout so a stuck call fails fast.
---
Created 01.redis-client-without-a-socket-timeout in myrules/01.redis-client-without-a-socket-timeout (status: shadow). It flags the bad example on line(s) [4].
Test it: tomosu rules test myrules/01.redis-client-without-a-socket-timeout · try it on code: tomosu rules try myrules/01.redis-client-without-a-socket-timeout <path>

The console’s New rule asks the same questions:

The New rule page: what is it (name, category, severity), what should it catch (functions to flag, when, the argument that must be there, the value to add as a fix), and an optional custom CEL condition.
Community Rules → New rule.

What is in a rule’s folder?

01.redis-client-without-a-socket-timeout/
  check.yaml               the rule
  tests/positive/          code it must flag
  tests/negative/          code it must leave alone
  tests/fixes/             for a rule with a fix template: the code, and the same code fixed (.fixed)

The condition reads facts the indexer gives for one site, such as a call’s name (site.callee) and keyword arguments (site.kwargs). A rule may only read facts that really exist; list them with tomosu rules facts:

$ tomosu rules facts --lang python --target call
python (from 250 files)
  call: arg_values, arg_vars, args, assigned_to, awaited, callee, chain, context_manager, discarded, effects, first_string, fn_error_checks, function, has_args_splat, has_kwargs_splat, in_async, in_comprehension, in_except, in_format, in_function, in_lock, in_route, in_route_path, in_span, in_threaded_route_path, in_transaction, in_try, is_decorator, kwarg, kwarg_values, kwarg_vars, kwargs, line, loop_depth, loop_infinite, loop_retry, name, nargs, receiver, receiver_ctor_known, receiver_kwarg_values, receiver_kwargs, receiver_type, resolved, resource, sql_verb, target, target_effects, target_reaches
(? = only when the repository has a manifest; test it with has())

How do I test it?

rules test runs the rule on its examples: every positive example must be flagged on the marked lines, no negative example may be, and every fix must turn the code into its .fixed version. It also checks the facts contract: no branch of the condition may read a fact the language never gives.

$ tomosu rules test myrules/01.redis-client-without-a-socket-timeout
1 checks, 3 examples, 0 failed
facts contract: 4 fact references, 0 never given

A rule with a fix template also needs an example under tests/fixes, with the fixed version beside it. rules create writes it for you from your bad example, and uses the fixed code as the negative example when you give no good one.

How do I try it on my own code?

rules try scans a folder with only that rule:

$ tomosu rules try myrules/01.redis-client-without-a-socket-timeout app
note: 1 rule(s) in shadow are reported for this run; set status: active to report them in every scan
1. High · [Pre-merge review] Redis client without a socket timeout (01.redis-client-without-a-socket-timeout)
   How:  Rule Passed with Code Fix
   cache.py:4  in cache
   Risk: A Redis call can hang when the server stops answering.
   Fix (drop-in, unverified until your tests run):
     @@ -1,4 +1,4 @@
      import redis
      
      def cache():
     -    return redis.Redis(host="cache", port=6379)
     +    return redis.Redis(host="cache", port=6379, socket_timeout=2)  # tomosu: 01.redis-client-without-a-socket-timeout
   id f-4e748a3fff80 · weight 0.75

1 finding in 1 file · 1 check · rules local:01.redis-client-without-a-socket-timeout

rules try reports the rule even while it is in shadow, and says so; a normal scan shows its findings only once you set status: active in check.yaml.

How do I start one by hand?

$ tomosu rules new 01.my-new-check --dir myrules
created myrules/01.my-new-check/check.yaml
add examples under myrules/01.my-new-check/tests/positive and myrules/01.my-new-check/tests/negative, then run: tomosu rules test myrules/01.my-new-check

The full format, every target and fix action, is in the rules format referenceComing soon. Rules you make are yours and stay on your engine; to offer one to everyone, contribute it.