Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/acknowledge-sg-trap-notices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@taskless/cli": patch
---

`validate` trap notices can now be acknowledged per rule. Each ast-grep authoring-trap notice has a stable id (`ellipsis-before-comma`, `comma-before-ellipsis`, `sibling-without-stopby`, `descent-without-stopby`, `awaited-generic-callee`), and a rule that lists one under `metadata.taskless.acknowledge` stops drawing that notice while every other notice on it still prints. `validate --json` and `verify --json` report what was silenced in a new, always-present `acknowledged` array, and an unknown id draws a notice of its own. The `create-sg-rule` and `validate-rule` recipes document the ids and when to acknowledge one.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-29
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## Context

`findSgTraps` walks the parsed rule object and returns notice strings. `verifyRule` (`src/rules/verify.ts`) appends them to the schema layer's `notices`, `inspect.ts` carries them to `RuleValidation` and `RuleVerification`, and `commands/verify.ts` prints or serializes them for both `validate` and `verify`. Nothing about a trap is stable except its text, and nothing in a rule can speak to a notice.

`.taskless/rule-metadata/<id>.yml` exists as a path, but `agent rule-meta` documents that no sidecar has ever been written and `rule meta` always exits with `RULE_META_UNAVAILABLE`. ast-grep's rule schema has a top-level `metadata` field (`"type": "object", "additionalProperties": true`, "Additional metadata for the rule"), which ast-grep parses and otherwise ignores.

## Goals / Non-Goals

**Goals:**

- An author can silence one trap notice on one rule, in the rule, durably.
- Acknowledging one trap does not hide another on the same rule.
- An agent reading `--json` can tell a notice was acknowledged rather than never raised.

**Non-Goals:**

- Narrowing any trap's heuristic.
- Per-relation acknowledgement (see D2).
- Acknowledging anything other than the five ast-grep trap notices, such as the `language:` spelling or `files:` scope notices, which already have a fix in the rule.

## Decisions

### D1. The acknowledgement lives in the rule file, under `metadata.taskless.acknowledge`

```yaml
id: prisma-updatemany-where
language: TypeScript
metadata:
taskless:
acknowledge: [awaited-generic-callee]
rule: ...
```

The issue asks for "whichever `validate` already reads". `validate` reads the rule file and parses it to an object; it reads no sidecar, and the sidecar has no writer. `metadata` is ast-grep's own extension point, so the rule stays valid to ast-grep, passes the vendored schema with no change, and travels with the rule when it is copied, moved or delivered. Namespacing under `taskless` leaves the rest of `metadata` to the author.

Rejected: a YAML comment beside the relation. The `yaml` parser `validate` uses drops comments, so reading one means a second, comment-preserving parse of the file, and a comment is invisible to every tool that round-trips the rule (a formatter, a generator rewriting it).

### D2. Per rule and per id, not per relation

An acknowledgement silences every notice with that id on the rule. A rule with two callee relations, one of which genuinely could see `await fn<T>()`, cannot acknowledge only the other. That case is rare, the remedy for it is to add the `await_expression` branch the notice asks for, and a per-path acknowledgement would key on the dotted path in the notice (`rule.any[1].has`), which shifts whenever a branch is added or reordered and would silently stop matching. The issue's acceptance criteria are per rule.

### D3. One id per notice kind, five in all

| id | notice |
| ------------------------ | ------------------------------------------------------------- |
| `ellipsis-before-comma` | `$$$` before a `,` (the leading form) |
| `comma-before-ellipsis` | `,` before `$$$` under smart strictness (the trailing form) |
| `sibling-without-stopby` | `follows` / `precedes` with no `stopBy` |
| `descent-without-stopby` | `has` / `inside` with neither `stopBy` nor `field` |
| `awaited-generic-callee` | callee `regex` under `field: function` with no awaited branch |

The two `$$$` forms get separate ids because they have different remedies, and the existing design already reports them separately so that fixing one still leaves the other audible. The two `stopBy` traps are separate for the same reason: a sibling stopping at its neighbour and a descent looking one level are different decisions.

Every trap is acknowledgeable. Three already have an in-rule spelling that says "I meant this" (`stopBy: neighbor`, `strictness: ast`), and the recipe says to prefer it; the acknowledgement is the only option for the leading `$$$` form and for the awaited callee.

### D4. `--json` carries `acknowledged`, always present

Each per-rule result gains `acknowledged: [{ id, notice }]`, empty rather than absent, following the same reasoning as `findings`: a key that is sometimes absent gets read as optional. `notice` is the text that would have printed, so an agent sees exactly what was silenced. Human output prints nothing for an acknowledged notice; the point is that it stops repeating.

### D5. A malformed or unknown acknowledgement is a notice

An id that names no trap (a typo, or `missing-stopby` from the issue's sketch) draws a notice listing the known ids. A `metadata.taskless.acknowledge` that is not a list of strings draws one too. Neither fails the rule: like every trap notice, it is something the author decides about. An acknowledgement of a known id that currently fires nothing is not reported; the decision is still recorded, and the trap may come back when the rule changes.

## Risks / Trade-offs

- [An author acknowledges a trap that does apply] → The acknowledgement is visible in the rule and in `--json`'s `acknowledged`, and the recipe tells authors to acknowledge only after checking the code base for the shape.
- [Notice ids become a contract] → They are named in the spec and the recipe; renaming one would un-acknowledge it on every rule, so ids are treated like constraint ids and not renamed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Why

`validate` reports the ast-grep authoring traps as notices (`findSgTraps`, `packages/cli/src/rules/sg-traps.ts`). They are structural: the awaited-generic callee notice fires for every TypeScript or Tsx rule that matches a callee by `regex` under `field: function` without an `await_expression` branch, whether or not the code base ever writes `await fn<T>()`. Once an author has decided a trap does not apply, nothing in the rule can record that, so every `validate` run repeats the notice. Two CLI PMF agent survey runs (a 0.13.0 nightly and 0.13.7) reported it as noise, for a member callee (`prisma.x.updateMany`) and for logger calls that are never called with type arguments (issue #713).

Narrowing the heuristic is not sound: `await api.get<Foo>()` is a member callee too, and it is the case the recipe's method workaround exists for. The author is the only one who knows, so the fix is to let the author say so.

## What Changes

- Every trap notice gets a stable id: `ellipsis-before-comma`, `comma-before-ellipsis`, `sibling-without-stopby`, `descent-without-stopby`, `awaited-generic-callee`.
- An `sg` rule acknowledges a trap by listing its id under `metadata.taskless.acknowledge` in the rule file. `metadata` is ast-grep's own free-form field, already accepted by its schema and its parser, and the rule file is the one thing `validate` reads.
- `validate` and `verify` skip a notice the rule acknowledges and still print every other notice, including other traps on the same rule.
- Under `--json`, each rule result carries an `acknowledged` array (always present, empty when nothing was acknowledged) naming each silenced notice's id and text, so an agent can see why a notice went quiet.
- An acknowledgement naming an id that is not a trap id, or a `metadata.taskless.acknowledge` that is not a list of strings, draws a notice of its own.
- `create-sg-rule` documents the acknowledgement next to the traps.

Not breaking. The bump is `patch`.

## Delivery shape

**Single PR.** The change is a few hundred lines across one module, its caller, the output schema, a recipe and their tests. Spec, implementation and the archive land together.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `cli-rule-validation`: adds a requirement that a rule can acknowledge a trap notice by id.

## Impact

- `packages/cli/src/rules/sg-traps.ts`: trap ids, acknowledgement reading and filtering.
- `packages/cli/src/rules/verify.ts`, `src/rules/inspect.ts`, `src/schemas/rule-check.ts`: carry `acknowledged` to the JSON output.
- `packages/cli/src/agent/create-sg-rule.md`: the Quirks section documents the ids and the acknowledgement.
- Tests in `packages/cli/test/sg-traps.test.ts` and a `validate` end-to-end case.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## ADDED Requirements

### Requirement: A rule can acknowledge a trap notice by id

Each ast-grep trap notice SHALL carry a stable id:

| id | Trap |
| ------------------------ | -------------------------------------------------------------------------------- |
| `ellipsis-before-comma` | a pattern places a `$$$` metavariable directly before a `,` |
| `comma-before-ellipsis` | a pattern places a `,` directly before a `$$$` metavariable |
| `sibling-without-stopby` | a `follows` or `precedes` relation declares no `stopBy` |
| `descent-without-stopby` | a `has` or `inside` relation declares neither `stopBy` nor `field` |
| `awaited-generic-callee` | a TypeScript or Tsx callee `regex` under `field: function` has no awaited branch |

An `sg` rule SHALL be able to acknowledge a trap by listing its id in the rule file under `metadata.taskless.acknowledge`. `validate` and `verify` SHALL NOT print a notice whose id the rule acknowledges, and SHALL still print every other notice on the rule. An acknowledgement SHALL apply to the rule that declares it only.

Under `--json`, every per-rule result SHALL carry an `acknowledged` array, empty rather than absent, with one entry per silenced notice giving its `id` and the `notice` text that would have been printed.

An acknowledgement that names an id no trap carries, a `metadata.taskless.acknowledge` that is not a list of strings, or a `metadata.taskless` that holds no `acknowledge` list, SHALL draw a notice naming the known ids. It SHALL NOT fail the rule.

#### Scenario: An acknowledged awaited-generic notice is silent

- **WHEN** a `TypeScript` rule matches `kind: call_expression` with `has: { field: function, regex: '\.updateMany$' }` and declares `metadata: { taskless: { acknowledge: [awaited-generic-callee] } }`
- **THEN** `validate` SHALL report the rule as valid with no awaited-generic notice
- **AND** under `--json` its `acknowledged` SHALL hold one entry with id `awaited-generic-callee`

#### Scenario: The same rule without the acknowledgement still draws the notice

- **WHEN** the rule above omits `metadata`
- **THEN** the rule SHALL carry the awaited-generic notice
- **AND** under `--json` its `acknowledged` SHALL be empty

#### Scenario: Acknowledging one trap does not hide another

- **WHEN** a rule acknowledges `awaited-generic-callee` and also declares a `follows` relation with no `stopBy`
- **THEN** the rule SHALL carry the `follows` notice
- **AND** SHALL NOT carry the awaited-generic notice

#### Scenario: An unknown acknowledgement id draws a notice

- **WHEN** a rule lists `missing-stopby` under `metadata.taskless.acknowledge`
- **THEN** the rule SHALL carry a notice naming `missing-stopby` and the known ids
- **AND** `validate` SHALL report the rule as valid
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## 1. Trap ids and acknowledgement

- [x] 1.1 Give each trap in `sg-traps.ts` a stable id and have `findSgTraps` return the notices to print and the acknowledged ones, reading `metadata.taskless.acknowledge`; report an unknown id or a malformed list as a notice; verify with new cases in `test/sg-traps.test.ts`.
- [x] 1.2 Carry `acknowledged` through `verify.ts`, `inspect.ts` and the `rule-check` output schema, always present; verify with `pnpm typecheck`.
- [x] 1.3 Add a `validate --json` case for the issue's reproduction rule, with and without the acknowledgement, confirming ast-grep accepts the `metadata` block.

## 2. Recipe and release note

- [x] 2.1 Document the ids and `metadata.taskless.acknowledge` in `create-sg-rule`'s Quirks section and bump its topic version.
- [x] 2.2 Add one `patch` changeset.

## 3. Checks and archive

- [x] 3.1 Run `pnpm typecheck`, `pnpm lint` and the CLI test suite.
- [x] 3.2 Archive the change and confirm every prior `cli-rule-validation` scenario survives.
42 changes: 42 additions & 0 deletions openspec/specs/cli-rule-validation/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,45 @@ Under `--json`, each rule result from `verify` SHALL carry a `status` naming its

- **WHEN** `validate --json` reports a rule
- **THEN** the rule's result SHALL NOT carry `status`

### Requirement: A rule can acknowledge a trap notice by id

Each ast-grep trap notice SHALL carry a stable id:

| id | Trap |
| ------------------------ | -------------------------------------------------------------------------------- |
| `ellipsis-before-comma` | a pattern places a `$$$` metavariable directly before a `,` |
| `comma-before-ellipsis` | a pattern places a `,` directly before a `$$$` metavariable |
| `sibling-without-stopby` | a `follows` or `precedes` relation declares no `stopBy` |
| `descent-without-stopby` | a `has` or `inside` relation declares neither `stopBy` nor `field` |
| `awaited-generic-callee` | a TypeScript or Tsx callee `regex` under `field: function` has no awaited branch |

An `sg` rule SHALL be able to acknowledge a trap by listing its id in the rule file under `metadata.taskless.acknowledge`. `validate` and `verify` SHALL NOT print a notice whose id the rule acknowledges, and SHALL still print every other notice on the rule. An acknowledgement SHALL apply to the rule that declares it only.

Under `--json`, every per-rule result SHALL carry an `acknowledged` array, empty rather than absent, with one entry per silenced notice giving its `id` and the `notice` text that would have been printed.

An acknowledgement that names an id no trap carries, a `metadata.taskless.acknowledge` that is not a list of strings, or a `metadata.taskless` that holds no `acknowledge` list, SHALL draw a notice naming the known ids. It SHALL NOT fail the rule.

#### Scenario: An acknowledged awaited-generic notice is silent

- **WHEN** a `TypeScript` rule matches `kind: call_expression` with `has: { field: function, regex: '\.updateMany$' }` and declares `metadata: { taskless: { acknowledge: [awaited-generic-callee] } }`
- **THEN** `validate` SHALL report the rule as valid with no awaited-generic notice
- **AND** under `--json` its `acknowledged` SHALL hold one entry with id `awaited-generic-callee`

#### Scenario: The same rule without the acknowledgement still draws the notice

- **WHEN** the rule above omits `metadata`
- **THEN** the rule SHALL carry the awaited-generic notice
- **AND** under `--json` its `acknowledged` SHALL be empty

#### Scenario: Acknowledging one trap does not hide another

- **WHEN** a rule acknowledges `awaited-generic-callee` and also declares a `follows` relation with no `stopBy`
- **THEN** the rule SHALL carry the `follows` notice
- **AND** SHALL NOT carry the awaited-generic notice

#### Scenario: An unknown acknowledgement id draws a notice

- **WHEN** a rule lists `missing-stopby` under `metadata.taskless.acknowledge`
- **THEN** the rule SHALL carry a notice naming `missing-stopby` and the known ids
- **AND** `validate` SHALL report the rule as valid
40 changes: 39 additions & 1 deletion packages/cli/src/agent/create-sg-rule.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Topic: create-sg-rule (CLI v%(CLI_VERSION)s / topic v18)
# Topic: create-sg-rule (CLI v%(CLI_VERSION)s / topic v19)

%(ONE_COMMAND_PER_CALL)s

Expand Down Expand Up @@ -261,6 +261,39 @@ sometimes what the author meant. Read it and decide.
| `has` or `inside` without `stopBy` | silent | as a notice |
| a `not` containing a bare metavariable | silent | no |

### Acknowledging a notice

A notice repeats on every `validate` run until the rule changes. Once
you have checked that a trap cannot bite this rule, say so in the rule
file and the notice stops:

```yaml
metadata:
taskless:
acknowledge: [awaited-generic-callee]
```

| notice id | trap | say it in the rule instead |
|--------------------------|---------------------------------------------------------|-------------------------------------|
| `ellipsis-before-comma` | `$$$` before a `,` (`foo($$$A, $B)`) | none |
| `comma-before-ellipsis` | `,` before `$$$` (`foo($A, $$$)`) | `strictness: ast` on the pattern |
| `sibling-without-stopby` | `follows` or `precedes` without `stopBy` | `stopBy: neighbor` |
| `descent-without-stopby` | `has` or `inside` without `stopBy` or `field` | `stopBy: neighbor` |
| `awaited-generic-callee` | a callee `regex` with no `await_expression` branch | none |

Where the last column names a spelling, prefer it: it states what the
relation does, and the notice goes away on its own. Acknowledge only
the traps that have no such spelling, and only after checking the
codebase. For `awaited-generic-callee`, that means confirming this
callee is never called as `await f<T>(...)`; a method such as
`await api.get<Foo>()` is a member callee and is affected too.

An acknowledgement covers every notice with that id on the rule and
nothing else, so a second trap on the same rule still reports. An id
that is not in the table draws a notice of its own. `validate --json`
lists what was silenced under each rule's `acknowledged`, so a quiet
rule can be told from one that never drew the notice.

### Every `regex`, at any depth, needs a `kind`

This one is a Taskless rule, wider than ast-grep's own check. ast-grep
Expand Down Expand Up @@ -479,6 +512,11 @@ Add a `unary_expression` branch the same way if `!`, `void` or
`typeof` matter. The wrapped match spans the `await` too, so a `fix:`
on this rule rewrites it along with the call.

`validate` notices this shape whether or not the codebase ever awaits
the callee with type arguments. If it never does (a Prisma
`updateMany`, a logger), acknowledge `awaited-generic-callee`; see
"Acknowledging a notice" under [Quirks](#quirks).

<!-- on-demand -->
## Relational quirks

Expand Down
Loading