-
Notifications
You must be signed in to change notification settings - Fork 828
add LocalProcessIntrospectTool for incident response #2518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
X1Vi
wants to merge
4
commits into
Tracer-Cloud:main
Choose a base branch
from
X1Vi:feature/#1506-local-process-introspect-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d7d9c97
feat: add LocalProcessIntrospectTool for incident response
X1Vi 17f9416
fix: promote resolve_target to public, cap stdout read at 4 MB to pre…
X1Vi caf8e65
fix: rename error_signals→error_counts, add docs page, add bounded-re…
X1Vi 00b2173
docs: merge local_process_introspect into agents.mdx
X1Vi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """Tool for introspecting a local process during incident response. | ||
|
|
||
| Returns a psutil snapshot and the last 50 stdout lines for a given PID. | ||
| The investigation planner calls this to diagnose stuck or misbehaving | ||
| local agents from the OpenSRE interactive shell. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from datetime import UTC | ||
| from typing import Any | ||
|
|
||
| from app.agents.error_signals import ErrorSignals | ||
| from app.agents.probe import ProcessSnapshot, probe | ||
| from app.agents.tail import DEFAULT_MAX_BYTES, AttachUnsupported, resolve_target | ||
| from app.tools.tool_decorator import tool | ||
|
|
||
|
|
||
| def _snapshot_to_dict(snapshot: ProcessSnapshot) -> dict[str, Any]: | ||
| return { | ||
| "pid": snapshot.pid, | ||
| "cpu_percent": snapshot.cpu_percent, | ||
| "rss_mb": snapshot.rss_mb, | ||
| "num_fds": snapshot.num_fds, | ||
| "num_connections": snapshot.num_connections, | ||
| "status": snapshot.status, | ||
| "started_at": snapshot.started_at.astimezone(UTC).isoformat(), | ||
| } | ||
|
|
||
|
|
||
| def _read_stdout_tail(pid: int, max_lines: int = 50) -> str | None: | ||
| """Read the last ``max_lines`` lines from the process's stdout. | ||
|
|
||
| Linux: resolves ``/proc/<pid>/fd/1``. | ||
| macOS: resolves fd 1 via ``lsof``. | ||
| Returns ``None`` when the pid doesn't exist, stdout is a pipe/socket/tty, | ||
| or we lack permission — the planner treats ``None`` as "unavailable". | ||
| """ | ||
| try: | ||
| target = resolve_target(pid) | ||
| except (AttachUnsupported, OSError): | ||
| return None | ||
| try: | ||
| with open(target.path, "rb") as f: | ||
| offset = max(0, os.fstat(f.fileno()).st_size - DEFAULT_MAX_BYTES) | ||
| if offset > 0: | ||
| f.seek(offset) | ||
| data = f.read() | ||
| except (OSError, PermissionError, FileNotFoundError): | ||
| return None | ||
| lines = data.decode("utf-8", errors="replace").splitlines() | ||
| return "\n".join(lines[-max_lines:]) | ||
|
|
||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| @tool( | ||
| name="local_process_introspect", | ||
| source="knowledge", | ||
| description=( | ||
| "Introspect a local process: return a psutil resource snapshot " | ||
| "(CPU%, RSS MB, fd count, connection count, status, start time) " | ||
| "and the last 50 lines of stdout. Use this when the planner needs " | ||
| "to diagnose a stuck, high-cpu, or misbehaving local agent during " | ||
| "incident response." | ||
| ), | ||
| input_schema={ | ||
| "type": "object", | ||
| "properties": { | ||
| "pid": { | ||
| "type": "integer", | ||
| "description": "Process ID to introspect.", | ||
| }, | ||
| }, | ||
| "required": ["pid"], | ||
| }, | ||
| use_cases=[ | ||
| "Diagnosing a stuck or high-cpu local agent during incident response", | ||
| "Checking whether a process is alive and making forward progress", | ||
| "Reading recent stdout output from a local process", | ||
| "Verifying resource usage of a monitored agent", | ||
| ], | ||
| outputs={ | ||
| "snapshot": "psutil ProcessSnapshot dict, or null if the PID is inaccessible", | ||
| "stdout_tail": "last 50 stdout lines as a string, or null if stdout cannot be read", | ||
| "error_counts": "error/retry counts per category from recent stdout", | ||
| }, | ||
| surfaces=("investigation",), | ||
| ) | ||
| def local_process_introspect(pid: int) -> dict[str, Any]: | ||
| snapshot = probe(pid) | ||
| stdout_tail = _read_stdout_tail(pid) | ||
| error_counts: dict[str, float] = {} | ||
| if stdout_tail: | ||
| signals = ErrorSignals() | ||
| signals.observe(stdout_tail) | ||
| error_counts = signals.rate_per_minute() | ||
| return { | ||
| "snapshot": _snapshot_to_dict(snapshot) if snapshot else None, | ||
| "stdout_tail": stdout_tail, | ||
| "error_counts": error_counts, | ||
| } | ||
|
X1Vi marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.