Repository navigation
Command timing: signatures, real durations, background commands (0.38.0) #49
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,3 +503,121 @@ _ds_extract_subcommand() { | |
| ;; | ||
| esac | ||
| } | ||
|
|
||
| # A command's signature for timing ("bun run build", "docker compose up", | ||
| # "git push"), so durations can be compared by what ran without sending the | ||
| # command. Only programs that take subcommands (MULTI below) keep words after | ||
| # the program, up to two, a third only after a group word; every other program | ||
| # is the program alone (`cat <file>` is "cat"), since its words are operands. | ||
| # Words stop at the first flag, path, number or `=`. Before splitting the chain, | ||
| # quoted text and heredoc bodies are removed, so nothing quoted can become a | ||
| # program or a word. The main segment is the first one that is not set-up | ||
| # (`cd x && bun test` is "bun test"); env assignments and wrappers (sudo, env, | ||
| # time, timeout, nice, nohup) are skipped with their options. Private mode keeps | ||
| # the program only, the same as toolSubcommand. The server re-checks the same | ||
| # rules (cleanSignature in the backend's commandQueries.ts): change both. | ||
| # Usage: _ds_command_signature "$RAW_TOOL_INPUT_JSON" | ||
| _ds_command_signature() { | ||
| local max_words=3 | ||
| [ "${DEVSCOPE_PRIVACY:-standard}" = "private" ] && max_words=1 | ||
| # Only bounded passes (reduce over the words): this runs on every Bash call. | ||
| printf '%s' "$1" | jq -r --argjson max "$max_words" ' | ||
| def words: [splits("[ \t]+")] | map(select(length > 0)); | ||
| # Everything after the line that starts a heredoc is its body. | ||
| def no_heredoc: | ||
| if test("<<") then | ||
| split("\n") as $l | ($l | map(test("<<")) | index(true)) as $i | $l[:$i + 1] | join("\n") | ||
| else . end; | ||
| # Quoted spans become a lone \u0001 token: never a program, never a word. | ||
| def no_quotes: | ||
| gsub("\"(\\\\.|[^\"\\\\])*\""; " \u0001 ") | gsub("\u0027[^\u0027]*\u0027"; " \u0001 "); | ||
| # Drop leading VAR=val and wrappers with their options (and option values). | ||
| def unwrap: | ||
| reduce .[] as $t ({prefix: true, wrap: false, skip: false, timeout: false, out: []}; | ||
| if .prefix | not then .out += [$t] | ||
| elif .skip then .skip = false | ||
| elif ($t | test("^[A-Za-z_][A-Za-z0-9_]*=")) then . | ||
| elif ($t | IN("sudo", "env", "time", "nice", "nohup", "command", "exec", "builtin", "timeout")) | ||
| then .wrap = true | .timeout = ($t == "timeout") | ||
| elif .wrap and ($t | startswith("-")) then | ||
| (if ($t | IN("-u", "-g", "-n", "-s", "-k", "-C", "-D", "-h", "-p", "-r", "-t", "-U", "-S", | ||
| "--user", "--group", "--signal", "--kill-after", "--adjustment", "--chdir")) | ||
| then .skip = true else . end) | ||
| elif .timeout and ($t | test("^[0-9]")) then .timeout = false | ||
| else .prefix = false | .out = [$t] end) | ||
| | .out; | ||
| def setup: IN("cd", "pushd", "popd", "export", "source", ".", "set", "unset", "shopt", | ||
| "true", ":", "echo", "printf", "trap", "local", "declare", "mkdir"); | ||
| def prog: split("/") | last | ascii_downcase | gsub("[^a-z0-9._+-]"; ""); | ||
| def plain: test("^[a-z][a-z0-9_:-]{0,23}$"); | ||
| # Programs whose next word names what they do (a subcommand, script, target | ||
| # or tool), so it is kept. | ||
| def multi: IN("git", "gh", "glab", "docker", "podman", "kubectl", "helm", "terraform", "tofu", | ||
| "npm", "pnpm", "yarn", "bun", "bunx", "npx", "deno", "cargo", "rustup", | ||
| "go", "uv", "uvx", "pip", "pip3", "poetry", "pdm", "hatch", "conda", | ||
| "brew", "apt", "apt-get", "dnf", "systemctl", "journalctl", "make", "just", "task", | ||
| "claude", "gcloud", "aws", "az", "fly", "flyctl", "vercel", "wrangler", "supabase", | ||
| "firebase", "dotnet", "mvn", "gradle", "gradlew", "mix", "rails", "bundle", "rake", | ||
| "composer", "swift", "xcodebuild", "zig", "nix"); | ||
| # A third word is kept only after a word that names a group of commands | ||
| # ("npm run test:unit", "docker compose up", "gh pr view"); otherwise it is | ||
| # usually an argument ("git checkout <branch>", "docker logs <container>"). | ||
| def group: IN("run", "run-script", "compose", "exec", "x", "workspace", "workspaces", | ||
| "pr", "issue", "repo", "release", "workflow", "api", "auth", | ||
| "container", "image", "volume", "network", "system", "buildx", "builder", | ||
| "stash", "worktree", "submodule", "remote", "bisect", "lfs", | ||
| "plugin", "plugins", "marketplace", "mcp", "config", "cache", "db", "migrate"); | ||
| (.command // "" | tostring | .[:2000] | no_heredoc | no_quotes) | ||
| | [splits("&&|\\|\\||;|\\||\n")] | ||
| | map(gsub("^[\\s({]+"; "") | gsub("[\\s)}]+$"; "") | words | unwrap | select(length > 0)) | ||
| | (map(select((.[0] | prog | setup) | not)) | first) // first // [] | ||
| | if length == 0 or (.[0] | prog) == "" then "" | ||
| else | ||
| (.[0] | prog) as $p | ||
| | ([$p] | ||
| + (if ($p | multi) then | ||
| .[1:] | reduce .[] as $w ({out: [], stop: false}; | ||
| if .stop or ($w | plain | not) then .stop = true else .out += [$w] end) | ||
| | .out | ||
| else [] end)) | ||
| | if length > 2 and (.[1] | group | not) then .[:2] else . end | ||
| | .[:$max] | join(" ") | ||
|
Comment on lines
+552
to
+584
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Limit In standard mode, The program name already goes through Choose one of these fixes:
With either fix, add a standard-mode test where
|
||
| end | ||
| ' 2>/dev/null || true | ||
| } | ||
|
|
||
| # Report background Bash commands that settled since the last call, as | ||
| # `command.background_complete` events ({commands: [...]}, up to 200 each, see | ||
| # bg_commands.py). Runs only for sessions tool-complete.sh marked as having | ||
| # backgrounded a command, so other sessions never read their transcript for | ||
| # this. `final` (SessionEnd) drops the marker; the offset stays (a Stop still | ||
| # running must not rescan from the start) until the sweep below. Needs | ||
| # SCRIPT_DIR (hook scripts set it) and python3; does nothing without them. | ||
| # Usage: _ds_report_bg_commands "$INPUT" [final] | ||
| _ds_report_bg_commands() { | ||
| local input="$1" final="${2:-}" sid transcript dir found count batch f base | ||
| sid=$(printf '%s' "$input" | jq -r '.session_id // ""' 2>/dev/null | tr -cd 'a-zA-Z0-9_-') | ||
| transcript=$(printf '%s' "$input" | jq -r '.transcript_path // ""' 2>/dev/null) | ||
| dir="${XDG_CACHE_HOME:-$HOME/.cache}/devscope/bg" | ||
| [ -n "$sid" ] && [ -f "$dir/$sid.active" ] && [ -n "$transcript" ] || return 0 | ||
| command -v python3 >/dev/null 2>&1 && [ -n "${SCRIPT_DIR:-}" ] || return 0 | ||
| # A full batch means more may follow (the scan stops at 200); at most 5 rounds. | ||
| for batch in 1 2 3 4 5; do | ||
| found=$(python3 "$SCRIPT_DIR/bg_commands.py" scan "$transcript" 2>/dev/null || echo '[]') | ||
| count=$(printf '%s' "$found" | jq 'length' 2>/dev/null || echo 0) | ||
| [ "${count:-0}" -gt 0 ] 2>/dev/null || break | ||
| printf '%s' "$input" | "$SCRIPT_DIR/send-event.sh" "command.background_complete" \ | ||
| "$(jq -nc --argjson c "$found" '{commands: $c}')" >/dev/null 2>&1 || true | ||
| [ "$count" -ge 200 ] || break | ||
| done | ||
| [ "$final" = "final" ] && rm -f "$dir/$sid.active" 2>/dev/null | ||
| # Sweep: markers after 7 days, offsets (and locks) after 7 days once their | ||
| # session's marker is gone. | ||
| find "$dir" -type f -name '*.active' -mtime +7 -delete 2>/dev/null || true | ||
| find "$dir" -type f \( -name '*.offset' -o -name '*.offset.lock' \) -mtime +7 2>/dev/null \ | ||
| | while IFS= read -r f; do | ||
| base="${f%.lock}"; base="${base%.offset}" | ||
| [ -e "$base.active" ] || rm -f "$f" 2>/dev/null | ||
| done | ||
| return 0 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| #!/usr/bin/env python3 | ||
| """When background Bash commands finished, from the Claude Code transcript. | ||
|
|
||
| A Bash command run with `run_in_background` (or moved to the background after | ||
| its timeout, or by the user) reports PostToolUse as soon as it is backgrounded, | ||
| and no hook fires when it settles. Claude Code does log the settling: it | ||
| enqueues a <task-notification> for the model, and the transcript records that | ||
| as a `queue-operation` entry with `operation: "enqueue"` and a timestamp: | ||
|
|
||
| <task-notification> | ||
| <task-id>b5r0fuwka</task-id> | ||
| <tool-use-id>toolu_016AWJ...</tool-use-id> | ||
| <output-file>/tmp/claude-1000/.../tasks/b5r0fuwka.output</output-file> | ||
| <status>completed</status> | ||
| <summary>Background command "..." completed (exit code 0)</summary> | ||
| </task-notification> | ||
|
|
||
| bg_commands.py scan <transcript.jsonl> | ||
| The commands that settled since the last scan, as a JSON array of | ||
| {toolUseId, taskId, status, exitCode?, finishedAt} ([] when none). | ||
| Reads incrementally: the byte offset of the last line reported is kept | ||
| in ~/.cache/devscope/bg/<transcript>.offset, under an exclusive lock so | ||
| a Stop and a SessionEnd running at once never report twice. At most 200 | ||
| per call; the rest is read by the next call. | ||
|
|
||
| Only ids, the status, the exit code and the time leave the machine: never the | ||
| command, its description (inside the summary) or its output path. | ||
| """ | ||
| import fcntl | ||
| import json | ||
| import os | ||
| import re | ||
| import sys | ||
| import tempfile | ||
|
|
||
| MAX_ITEMS = 200 # the backend accepts at most 200 per event | ||
|
|
||
| NOTIFICATION = re.compile(r"<task-notification>(.*?)</task-notification>", re.S) | ||
|
|
||
|
|
||
| def tag(body, name): | ||
| m = re.search(r"<%s>(.*?)</%s>" % (name, name), body, re.S) | ||
| return m.group(1).strip() if m else "" | ||
|
|
||
|
|
||
| def cache_dir(): | ||
| base = os.environ.get("XDG_CACHE_HOME") or os.path.join(os.path.expanduser("~"), ".cache") | ||
| return os.path.join(base, "devscope", "bg") | ||
|
|
||
|
|
||
| def offset_path(transcript): | ||
| name = re.sub(r"[^A-Za-z0-9_-]", "", os.path.basename(transcript).rsplit(".jsonl", 1)[0]) | ||
| return os.path.join(cache_dir(), (name or "transcript") + ".offset") | ||
|
|
||
|
|
||
| def parse_entry(entry): | ||
| """A settled background command from one transcript entry, else None.""" | ||
| if entry.get("type") != "queue-operation" or entry.get("operation") != "enqueue": | ||
| return None | ||
| content = entry.get("content") | ||
| if not isinstance(content, str) or "<task-notification>" not in content: | ||
| return None | ||
| m = NOTIFICATION.search(content) | ||
| if not m: | ||
| return None | ||
| body = m.group(1) | ||
| summary = tag(body, "summary") | ||
| # Agents and monitors send task notifications too; only shells count here. | ||
| if not summary.startswith("Background command"): | ||
| return None | ||
| tool_use_id = tag(body, "tool-use-id") | ||
| status = tag(body, "status") | ||
| finished_at = entry.get("timestamp") | ||
| if not tool_use_id or not status or not isinstance(finished_at, str): | ||
| return None | ||
| item = { | ||
| "toolUseId": tool_use_id[:200], | ||
| "taskId": tag(body, "task-id")[:100], | ||
| "status": status[:20], | ||
| "finishedAt": finished_at[:50], | ||
| } | ||
| # The real "(exit code N)" ends the summary; the description before it | ||
| # could contain the words too. | ||
| codes = re.findall(r"exit code (-?\d+)", summary) | ||
| if codes: | ||
| item["exitCode"] = int(codes[-1]) | ||
| return item | ||
|
|
||
|
|
||
| def scan(transcript): | ||
| if not os.path.isfile(transcript): | ||
| return [] | ||
| os.makedirs(cache_dir(), mode=0o700, exist_ok=True) | ||
| with open(offset_path(transcript) + ".lock", "w") as lock: | ||
| fcntl.flock(lock, fcntl.LOCK_EX) # the offset read-modify-write is one step | ||
| return scan_locked(transcript) | ||
|
|
||
|
|
||
| def scan_locked(transcript): | ||
| state = offset_path(transcript) | ||
| try: | ||
| with open(state) as f: | ||
| offset = int(f.read().strip() or 0) | ||
| except (OSError, ValueError): | ||
| offset = 0 | ||
| size = os.path.getsize(transcript) | ||
| if offset > size: # rewritten transcript: start over | ||
| offset = 0 | ||
| found, seen = [], set() | ||
| with open(transcript, "rb") as f: | ||
| f.seek(offset) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| for raw in f: | ||
| if not raw.endswith(b"\n"): | ||
| break # half-written line: read it next time | ||
| if b'"queue-operation"' in raw: | ||
| try: | ||
| item = parse_entry(json.loads(raw)) | ||
| except (ValueError, AttributeError): | ||
| item = None | ||
| if item and item["toolUseId"] not in seen: | ||
| if len(found) >= MAX_ITEMS: | ||
| break # full: this line is the first of the next call | ||
| seen.add(item["toolUseId"]) | ||
| found.append(item) | ||
| offset += len(raw) | ||
| fd, tmp = tempfile.mkstemp(dir=cache_dir()) | ||
| with os.fdopen(fd, "w") as f: | ||
| f.write(str(offset)) | ||
| os.replace(tmp, state) | ||
| return found | ||
|
|
||
|
|
||
| def main(argv): | ||
| if len(argv) != 3 or argv[1] != "scan": | ||
| print(__doc__, file=sys.stderr) | ||
| return 2 | ||
| try: | ||
| print(json.dumps(scan(argv[2]), separators=(",", ":"))) | ||
| except Exception: # never break a hook | ||
| print("[]") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main(sys.argv)) | ||
Uh oh!
There was an error while loading. Please reload this page.