Skip to content

Commit 56724ae

Browse files
committed
docs: document query owner context
1 parent b995eb6 commit 56724ae

3 files changed

Lines changed: 127 additions & 1 deletion

File tree

docs/probe-agent/sdk/tools-reference.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ AST-based structural queries using tree-sitter patterns.
163163
| `path` | string | No | Directory to search |
164164
| `language` | string | No | Programming language |
165165
| `limit` | number | No | Maximum results |
166+
| `with_context` | boolean | No | Include owning source-block context in JSON output |
166167

167168
**Example AI Usage:**
168169
```xml
@@ -173,6 +174,17 @@ AST-based structural queries using tree-sitter patterns.
173174
</query>
174175
```
175176

177+
Use `with_context` when the exact AST match needs its enclosing function, method, class, attached comments, or call context for downstream analysis:
178+
179+
```xml
180+
<query>
181+
<pattern>fetch($$$ARGS)</pattern>
182+
<language>typescript</language>
183+
<path>./src</path>
184+
<with_context>true</with_context>
185+
</query>
186+
```
187+
176188
**Common Patterns:**
177189
- Functions: `fn $NAME() { $$$BODY }`
178190
- Classes: `class $NAME { $$$BODY }`

docs/probe-cli/query.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ probe query "go $FUNC($$$ARGS)" ./src -l go
119119
| `--allow-tests` | Boolean | false | Include test files |
120120
| `-i`, `--ignore` | String[] | - | Patterns to ignore |
121121
| `--no-gitignore` | Boolean | false | Don't respect .gitignore |
122+
| `--with-context`, `--owner-context` | Boolean | false | Include owning source-block context in JSON output |
122123

123124
### Language Options
124125

@@ -156,10 +157,68 @@ probe query "fn $NAME()" ./src --format markdown
156157
# JSON for tooling
157158
probe query "fn $NAME()" ./src --format json
158159

160+
# JSON with owning source-block context
161+
probe query "fetch($$$ARGS)" ./src --language typescript --format json --with-context
162+
159163
# Plain text
160164
probe query "fn $NAME()" ./src --format plain
161165
```
162166

167+
### Owner Context JSON
168+
169+
Use `--with-context` when a structural match is not enough by itself and the caller also needs the source block a human would inspect. This keeps `query` precise while adding neutral source facts such as the owning function, method, class, attached comments, and enclosing calls.
170+
171+
```bash
172+
probe query "fetch($$$ARGS)" ./src --language typescript --format json --with-context
173+
```
174+
175+
The default JSON fields remain available. With context enabled, each result can also include:
176+
177+
```json
178+
{
179+
"schema_version": "probe.query.context.v1",
180+
"results": [
181+
{
182+
"file": "src/api.ts",
183+
"lines": [4, 4],
184+
"node_type": "match",
185+
"content": "fetch(url)",
186+
"column_start": 10,
187+
"column_end": 20,
188+
"language": "typescript",
189+
"pattern": {
190+
"source": "fetch($$$ARGS)",
191+
"id": null
192+
},
193+
"match": {
194+
"node_type": "call_expression",
195+
"content": "fetch(url)",
196+
"lines": [4, 4],
197+
"columns": [10, 20]
198+
},
199+
"owner": {
200+
"symbol": "loadProfile",
201+
"qualified_symbol": "loadProfile",
202+
"node_type": "function_declaration",
203+
"scope": "function",
204+
"lines": [2, 5],
205+
"columns": [1, 2],
206+
"comments": [
207+
{
208+
"kind": "leading",
209+
"start_line": 1,
210+
"end_line": 1,
211+
"text": "// Network boundary: user profile API client."
212+
}
213+
]
214+
}
215+
}
216+
]
217+
}
218+
```
219+
220+
Probe does not interpret comment contents or apply domain policy. Downstream tools decide whether a comment is a requirement, security annotation, checklist marker, or ordinary text.
221+
163222
---
164223

165224
## Common Patterns

docs/reference/output-formats.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,61 @@ The `query` command's JSON output is similar to the search command but includes
125125
}
126126
```
127127

128+
When `--with-context` or `--owner-context` is used with `--format json`, Probe keeps the compatible result fields and adds source-block context for each structural match:
129+
130+
```json
131+
{
132+
"schema_version": "probe.query.context.v1",
133+
"results": [
134+
{
135+
"file": "/path/to/api.ts",
136+
"lines": [4, 4],
137+
"node_type": "match",
138+
"content": "fetch(url)",
139+
"column_start": 10,
140+
"column_end": 20,
141+
"language": "typescript",
142+
"pattern": {
143+
"source": "fetch($$$ARGS)",
144+
"id": null
145+
},
146+
"match": {
147+
"node_type": "call_expression",
148+
"content": "fetch(url)",
149+
"lines": [4, 4],
150+
"columns": [10, 20]
151+
},
152+
"owner": {
153+
"symbol": "requestJSON",
154+
"qualified_symbol": "requestJSON",
155+
"node_type": "function_declaration",
156+
"scope": "function",
157+
"lines": [1, 5],
158+
"columns": [1, 2],
159+
"comments": [
160+
{
161+
"kind": "leading",
162+
"start_line": 1,
163+
"end_line": 1,
164+
"text": "// Handles outbound API calls."
165+
}
166+
],
167+
"enclosing_symbols": [],
168+
"enclosing_calls": []
169+
}
170+
}
171+
],
172+
"summary": {
173+
"count": 1,
174+
"total_bytes": 10,
175+
"total_tokens": 3
176+
},
177+
"version": "0.0.0"
178+
}
179+
```
180+
181+
The context fields are intentionally source facts only. Probe reports owner, match, comment, symbol, and enclosing-call metadata; it does not interpret requirements, policies, test frameworks, or annotation formats.
182+
128183
#### Example: Query JSON Output
129184

130185
```bash
@@ -622,4 +677,4 @@ Both JSON and XML formats handle special characters appropriately:
622677
- **JSON**: Special characters in code are properly escaped according to JSON rules
623678
- **XML**: Code blocks are wrapped in CDATA sections to preserve formatting and special characters
624679

625-
This ensures that the output can be reliably parsed regardless of the content of the code.
680+
This ensures that the output can be reliably parsed regardless of the content of the code.

0 commit comments

Comments
 (0)