Skip to content

feat(ci): Add ast-grep rules for RLIMIT_NPROC, O_NONBLOCK, os.killpg #1976

Closed

Description

@dan-petty

Friction

Three process-safety rules have no check: never call setrlimit(RLIMIT_NPROC, ...), never set O_NONBLOCK on file descriptors 0 to 2, and never call os.killpg without the PID 1, own-group and caller-group guards. #1961 moved them from AGENTS.md to ARCHITECTURE.md section 7.3, where an agent reads them only when it looks.

Evidence

.ast-grep/rules/ holds the net-construct rules and the test elapsed-time rule; no rule covers these calls.

Existing tool considered

ast-grep, which the gate already runs. Nothing is built beyond rules.

Proposed remedy

Add ast-grep rules that fail on resource.setrlimit(resource.RLIMIT_NPROC, ...), on os.set_blocking(<0|1|2>, False) and fcntl O_NONBLOCK on those descriptors, and on an os.killpg call outside the guarded helper _terminate_process_group, with rule tests.

Alternatives check of 2026-11-01: not-applicable.
Tool: none found.

Proposed design

Problem statement

ARCHITECTURE.md section 7.3 (lines 201-202) holds three process-safety rules with no automated check: never set RLIMIT_NPROC, never set O_NONBLOCK on descriptors 0, 1 or 2, and never signal a process group without the PID 1, own-group and caller-group guards. The constructs row of devops ci runs ast-grep, but no rule covers these calls, so an agent reads the rules only when it opens that section. This issue adds the three rules, their fixtures and their entries in the construct gate. The guarded helpers in sandbox/host.py and k8s/port_forward_daemon.py now delegate to core.process.terminate_process_group, so the only direct os.killpg call in src/ is the guarded primitive _signal_process_group in core/process.py.

Acceptance criteria

Requirement: RLIMIT_NPROC is refused

The constructs gate SHALL report a violation for a call to resource.setrlimit whose first argument is resource.RLIMIT_NPROC.

Scenario: setrlimit with RLIMIT_NPROC

  • WHEN The rule runs on a fixture that calls resource.setrlimit(resource.RLIMIT_NPROC, (0, 0)).
  • THEN ast-grep reports one violation at the call.

Requirement: A standard descriptor is not set non-blocking

The gate SHALL report a violation for an os.set_blocking call with a literal descriptor of 0, 1 or 2 and a value of False.

Scenario: stdin set non-blocking

  • WHEN The rule runs on a fixture that calls os.set_blocking(0, False).
  • THEN ast-grep reports one violation at the call.

Requirement: O_NONBLOCK is refused on a standard descriptor

The gate SHALL report a violation for an fcntl.fcntl call on a literal descriptor of 0, 1 or 2 whose flags argument holds fcntl.O_NONBLOCK.

Scenario: stdout set non-blocking with fcntl

  • WHEN The rule runs on a fixture that calls fcntl.fcntl(1, fcntl.F_SETFL, fcntl.O_NONBLOCK).
  • THEN ast-grep reports one violation at the call.

Requirement: A descriptor above 2 is allowed

The two descriptor rules SHALL report no violation for a literal descriptor above 2.

Scenario: descriptor 3

  • WHEN The rules run on a fixture that calls os.set_blocking(3, False) and fcntl.fcntl(3, fcntl.F_SETFL, fcntl.O_NONBLOCK).
  • THEN ast-grep reports no violation.

Requirement: An unguarded process-group signal is refused

The gate SHALL report a violation for an os.killpg call outside the functions of src/devops_cli/core/process.py that hold the PID 1, own-group and caller-group guards.

Scenario: bare os.killpg

  • WHEN The rule runs on a fixture that calls os.killpg(os.getpgid(pid), signal.SIGTERM) at module level.
  • THEN ast-grep reports one violation at the call.

Requirement: The guarded primitive is allowed

The os.killpg rule SHALL report no violation for a call inside _signal_process_group, terminate_process_group, _terminate_process_group_os or _terminate_process_group_async.

Scenario: guarded primitive

  • WHEN The rule runs on a fixture whose _signal_process_group function calls os.killpg(pgid, sig).
  • THEN ast-grep reports no violation.

Requirement: The source tree passes the new rules

The new rules MUST report no violation on src/.

Scenario: constructs scan over src

  • WHEN The constructs row of devops ci scans src/ with the new rules.
  • THEN ast-grep reports no violation.

Requirement: Each rule has a test

Each new rule MUST have a file in .ast-grep/rule-tests/ with at least one valid and one invalid case.

Scenario: rule test suite

  • WHEN uv run ast-grep test --skip-snapshot-tests runs.
  • THEN The tests of the three new rules pass.

Requirement: Each rule joins the construct gate

Each new rule MUST set severity: error and files: ["src/**"], and MUST have an entry in CONST_AST_GREP_RULE_FILES and one in VIOLATIONS of tests/gate/construct_gate.py.

Scenario: construct gate table

  • WHEN The construct gate check of the constructs row reads the rule files and the violation table.
  • THEN It reports no missing rule, no rule outside the table, and no rule without a valid and an invalid case.

Existing Tool Considered

ast-grep covers the need: it is the open, maintained scanner that the constructs row and the ast-grep-test hook already run, and the change adds three rule files and their fixtures, so it builds no program code and carries no build cost. What it lacks: it matches syntax, not values, so the PID 1, own-group and caller-group guard values stay with tests/core/test_process.py::test_terminate_process_group_guards_and_lifecycle; and it has no relation for "inside this function in this file", so the os.killpg exception is by function name. The alternatives check of 2026-11-01 found no other tool and gave not-applicable.

Key questions

  • Where do the new ast-grep rules and their tests go?: A rule goes in .ast-grep/rules/<id>.yml, beside the net-construct rules and the test elapsed-time rule, and its cases go in .ast-grep/rule-tests/<id>-test.yml. sgconfig.yml names both directories as ruleDirs and testDir. [sources: sgconfig.yml:1, sgconfig.yml:4, .ast-grep/rules/review-cwd-read.yml:1, .ast-grep/rule-tests/review-cwd-read-test.yml:1]
  • Which command runs the ast-grep rule tests, and how do they join the CI gate?: uv run ast-grep test --skip-snapshot-tests runs the cases under .ast-grep/rule-tests/. The ast-grep-test pre-commit hook runs that command, and the constructs row of devops ci runs ast-grep test as one of its also_cmds, so the same cases run in the gate. [sources: .pre-commit-config.yaml:75, .pre-commit-config.yaml:77, docs/TESTING.md:72, tests/commands/test_net_construct_rules.py:22, sgconfig.yml:4]
  • Does the repository set RLIMIT_NPROC anywhere that the rule must allow?: No. src/ only reads the limit: tests/ai/test_tool_executor_contract.py:1304 calls resource.getrlimit(resource.RLIMIT_NPROC), which the rule does not match, and docs/TESTING.md:139 states that no test sets it. ARCHITECTURE.md and the tool executor state why it is never set. The rule flags every setrlimit call with no exemption. [sources: ARCHITECTURE.md:202, src/devops_cli/security/tool_executor.py:45, tests/ai/test_tool_executor_contract.py:1304, docs/TESTING.md:139]
  • Which descriptors and which calls does the O_NONBLOCK rule cover?: Descriptors 0, 1 and 2 only, for os.set_blocking(fd, False) and for fcntl.fcntl(fd, fcntl.F_SETFL, ...) with fcntl.O_NONBLOCK in the flags argument. The rule does not cover os.open. (best practice) [sources: ARCHITECTURE.md:202, tests/ai/test_review_defects.py:565]
    • Chosen: Descriptors 0 to 2 for os.set_blocking and fcntl.fcntl. ARCHITECTURE.md limits the rule to descriptors 0, 1 and 2, and these are the calls that change an existing descriptor.
    • Rejected: Every use of O_NONBLOCK. It flags the legitimate FIFO open at tests/ai/test_review_defects.py:565.
    • Rejected: Every descriptor of os.set_blocking and fcntl.fcntl. A pipe or socket descriptor above 2 needs a non-blocking mode.
    • Rejected: Include os.open with O_NONBLOCK. os.open returns a fresh descriptor and cannot target 0 to 2, and it is the call that opens the FIFO at tests/ai/test_review_defects.py:565.
  • How does the os.killpg rule allow the guarded helper?: The rule allows os.killpg only inside the functions of src/devops_cli/core/process.py that hold the guards: terminate_process_group, _terminate_process_group_os, _terminate_process_group_async and the primitive _signal_process_group. The _terminate_process_group helpers in sandbox/host.py and k8s/port_forward_daemon.py delegate to core.process.terminate_process_group and hold no os.killpg call, and the only direct call in src/ is _signal_process_group at src/devops_cli/core/process.py:952. The rule uses the ast-grep inside relation with a function_definition whose name matches that set. (best practice) [sources: src/devops_cli/core/process.py:947, src/devops_cli/core/process.py:952, src/devops_cli/core/process.py:958, src/devops_cli/core/process.py:965, src/devops_cli/core/process.py:1006, src/devops_cli/core/process.py:1081, src/devops_cli/sandbox/host.py:65, src/devops_cli/sandbox/host.py:69, src/devops_cli/k8s/port_forward_daemon.py:99, src/devops_cli/k8s/port_forward_daemon.py:103, ARCHITECTURE.md:201, .ast-grep/rules/test-elapsed-upper-bound.yml:107]
    • Chosen: Allow inside the guard-holding functions of core/process.py. It covers the one direct call in src/ and the functions that hold the guards, and needs no file list.
    • Rejected: Allow by file path. A new unguarded call in core/process.py would pass.
    • Rejected: Allow the _terminate_process_group helpers in sandbox/host.py and k8s/port_forward_daemon.py. Both helpers delegate to core.process.terminate_process_group and hold no os.killpg call, so this list would leave the real call at core/process.py:952 unallowed and fail the source-tree criterion.
    • Rejected: Allow no call. The guarded primitive itself would fail the gate.
  • Which tree does each new rule hold?: Each of the three rules holds src/** only. The construct gate holds one tree per rule and needs a twin violation in the other tree that the rule does not report, and the test tree holds no violation of these rules today. (best practice) [sources: tests/gate/construct_gate.py:83, tests/gate/construct_gate.py:96, tests/gate/construct_gate.py:175, tests/gate/construct_gate.py:310, src/devops_cli/config/constants.py:4491, docs/TESTING.md:139, tests/ai/test_tool_executor_contract.py:1304]
    • Chosen: src/** for all three rules. It matches the trees of the existing rules, and the gate writes and checks a twin in tests/ that the rule must not report.
    • Rejected: src/** and tests/** for each rule. The construct gate writes a twin in the other tree and requires no report, and _OTHER_TREE has no entry for a rule that holds both trees.
    • Rejected: A second rule set for tests/**. The test tree holds no violation today, and it doubles the rule and fixture count.
  • Do the PID 1, own-group and caller-group guards stay with the existing test?: Yes. ast-grep matches syntax and not values, so the rule confines the call to the guard-holding functions. The values stay with test_terminate_process_group_guards_and_lifecycle in tests/core/test_process.py, which covers pid 0, 1 and a negative pid, the own process group, and the fallback to os.kill. (best practice) [sources: tests/core/test_process.py:199, tests/core/test_process.py:207, tests/core/test_process.py:229, openspec/changes/archive/2026-11-06-3749-sandbox-tests-on-fakes/proposal.md:49]
    • Chosen: Confine the call to the guard-holding functions and keep the guard test. The rule catches a new unguarded call, and the test catches a missing guard.
    • Rejected: Enforce the guard values with an ast-grep rule. ast-grep matches syntax and not the value of a pid or a process group.
  • What must change in the construct gate besides the rule and test files?: Each rule id must join CONST_AST_GREP_RULE_FILES with its files list, and one violation of each rule must join VIOLATIONS. The construct gate reports a rule outside that table, a rule whose files differ, a rule without a valid or an invalid case, and a rule with no violation in its tree. [sources: tests/gate/construct_gate.py:83, tests/gate/construct_gate.py:138, tests/gate/construct_gate.py:164, tests/gate/construct_gate.py:310, tests/gate/construct_gate.py:322]
  • Does this issue depend on docs(agents): AGENTS.md routes each task to the document that owns it, and no agent prompt or instruction carries the legacy process #1961?: No. ARCHITECTURE.md section 7.3 already holds the three rules at lines 201 and 202, so docs(agents): AGENTS.md routes each task to the document that owns it, and no agent prompt or instruction carries the legacy process #1961 is done. This issue only adds the rules, their tests and their gate entries. [sources: ARCHITECTURE.md:201, ARCHITECTURE.md:202]
  • What tool does the CI gate already run for these checks?: ast-grep. The constructs row of devops ci runs uv run ast-grep scan over src and tests with CONST_AST_GREP_SCAN_ARGS, and the ast-grep-test hook runs the rule tests. This issue adds rules to that run and needs no new tool. [sources: .pre-commit-config.yaml:67, .pre-commit-config.yaml:69, .pre-commit-config.yaml:75, src/devops_cli/config/constants.py:4492, docs/TESTING.md:72]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/infra
    on Oct 23, 2026
  2. dan-petty commented on Oct 23, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/infra. The candidate adds ast-grep rules to the CI process.
    • Priority: P2-Medium. The change improves process safety and is not critical.
    • Value: Medium. The change helps prevent process-safety issues in the project.
    • Effort: Medium. Adding new ast-grep rules and tests requires moderate work.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Refine cannot set this item to Ready at bb46660 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

    • Where do the new ast-grep rules go? (missing verified source)
  4. added
    priority/p2-medium
    status/new
    value/medium
    effort/medium
    type/feature
    scope/ci
    and removed
    type/infra
    on Oct 28, 2026
  5. 25 remaining items

  6. added
    status/blocked
    on Nov 14, 2026
  7. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    The runner Job devops-cli-runner-20261114-211850-83e5 ended: succeeded.
    Image digest: sha256:97690e98a3a8531d7114db820dd9859802ae6cb26167f0192f4b9026ed63154e

    The last lines of its log:

    Tool call: run_command, started: grep -rn net-raw-http-client\\|review-cwd-read --include=<masked> --include=<masked> --include=<masked> ...
    Tool call: run_command, program grep, ok.
    Tool call: read_file, ok.
    Tool call: run_command, started: grep -n gate\\|CONST_ tests/invariants/test_source_tree.py | head ...
    Tool call: run_command, program grep, ok.
    Tool call: read_file, ok.
    Tool call: run_command, started: uv run devops ci --only test-tree
    Tool call: run_command, program uv, ok.
    Tool call: edit_file, ok.
    Tool call: read_file, ok.
    Tool call: list_files, ok.
    Tool call: run_command, started: uv run devops openspec check
    Tool call: run_command, program uv, ok.
    Tool call: edit_file, ok.
    Tool call: run_command, started: git status --short
    Tool call: run_command, started: git diff --stat
    Tool call: run_command, program git, ok.
    Tool call: run_command, program git, ok.
    Tool call: run_command, started: git rev-parse --show-toplevel
    Tool call: run_command, started: git status
    Tool call: run_command, program git, ok.
    Tool call: run_command, program git, ok.
    Tool call: run_command, started: git log --oneline -5
    Tool call: run_command, started: git show --stat --oneline HEAD
    Tool call: run_command, program git, ok.
    Tool call: run_command, program git, ok.
    Tool call: run_command, started: other
    Tool call: run_command, program other:ae327604520a, refused.
    Tool call: run_command, started: git clean -f .data/agent/scratch
    Tool call: run_command, program git, ok.
    Tool call: run_command, started: git status --short --ignored .data/agent/scratch
    Tool call: run_command, started: git show --stat --oneline 9a37b8e53
    Tool call: run_command, program git, ok.
    Tool call: run_command, program git, ok.
    Tool call: run_command, started: uv run devops ci test tests/gate/construct_gate.py
    Tool call: run_command, program uv, ok.
    Tool call: run_command, started: uv run ast-grep test --skip-snapshot-tests
    Tool call: run_command, program uv, ok.
    Failure class ci_infra. Action left: retry.
    ✓ Took 0 item(s) to a ready pull request, stopped 1.
    
  8. added
    status/planned
    and removed
    owner/service
    status/blocked
    on Nov 14, 2026
  9. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Cleared the stop at group 1 by the rule digest: the runner image changed since the stop, from sha256:97690e98a3a8531d7114db820dd9859802ae6cb26167f0192f4b9026ed63154e to sha256:30054d111c2d804d0561b96d1f0fa248bb1ff61a2253a4ef8b386d66d01aec37, and the class ci_infra of the stop is one that a new attempt can fix. The item is Planned again, and the Service takes it in the next round. The Service clears at most 1 for each class and 3 for each item by digest. A stop of another class, a new stop on this image, and a stop over a limit wait for a comment implement: retry.

  10. added
    status/in-review
    and removed
    status/planned
    on Nov 15, 2026
  11. removed
    status/in-review
    on Nov 15, 2026
  12. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-15 04:09 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/cisource/agenttype/featurevalue/medium

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions