Repository navigation
feat: send subagent description and model on agent.start (0.31.0) #42
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 |
|---|---|---|
|
|
@@ -88,6 +88,50 @@ _ds_project_hash() { | |
| _ds_sha256 "${email}:${cwd}:${PPID}" | ||
| } | ||
|
|
||
| # Subagent intents. SubagentStart carries only agent_type and agent_id, so the | ||
| # PreToolUse of the Agent tool queues what the subagent was asked to do and | ||
| # agent-start.sh takes the oldest fresh intent of its type. Private mode keeps | ||
| # the model but never the description. | ||
| _ds_agent_intent_file() { | ||
| echo "${HOME}/.cache/devscope/intents/$(printf '%s' "$1" | tr -cd 'a-zA-Z0-9_-').jsonl" | ||
| } | ||
|
|
||
| # Usage: _ds_record_agent_intent "$HOOK_INPUT_JSON" | ||
| _ds_record_agent_intent() { | ||
| local session file | ||
| session=$(printf '%s' "$1" | jq -r '.session_id // ""' 2>/dev/null) || return 0 | ||
| [ -n "$session" ] || return 0 | ||
| file=$(_ds_agent_intent_file "$session") | ||
| mkdir -p -m 0700 "$(dirname "$file")" | ||
| printf '%s' "$1" | jq -c --arg privacy "$DEVSCOPE_PRIVACY" --argjson now "$(date +%s)" ' | ||
| .tool_input // {} | { | ||
| agentType: (.subagent_type // "general-purpose"), | ||
| description: (if $privacy == "private" then null else (.description // null) end), | ||
| model: (.model // null), | ||
| ts: $now | ||
| }' >> "$file" 2>/dev/null || true | ||
|
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. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: rg -n -i 'macos|darwin|flock|linux|supported|agent.start|subagent' README.md CHANGELOG.md CLAUDE.md CONTRIBUTING.md tests/agents/run.sh scripts/_helpers.sh
sed -n '90,141p' scripts/_helpers.shRepository: DowLucas/devscope-plugin Length of output: 8140 🏁 Script executed: #!/bin/bash
set -u
printf '%s\n' '--- base/head availability and diff ---'
git rev-parse --verify 602eb5b6480a5afb96582e98806a2229d99fb02b
git rev-parse --verify 62dad58ebc88edbef40147c46cc7675cb26c4228
git diff --unified=80 602eb5b6480a5afb96582e98806a2229d99fb02b 62dad58ebc88edbef40147c46cc7675cb26c4228 -- scripts/_helpers.sh scripts/tool-use.sh scripts/agent-start.sh tests/agents/run.sh README.md CLAUDE.md CONTRIBUTING.md
printf '%s\n' '--- support and hook-contract references ---'
rg -n -i --glob '!node_modules/**' --glob '!dist/**' --glob '!build/**' 'flock|Linux|macOS|supported|hook.*error|error.*hook|agent\.start|description|model|intent|queue|PreToolUse|SubagentStart|must|require' README.md CLAUDE.md CONTRIBUTING.md .github scripts tests package.json 2>/dev/null || test "$?" -eq 1
printf '%s\n' '--- relevant hook and test files ---'
sed -n '1,90p' scripts/tool-use.sh
sed -n '1,90p' scripts/agent-start.sh
sed -n '1,120p' tests/agents/run.sh
sed -n '1,120p' CLAUDE.md
sed -n '180,215p' CLAUDE.md
sed -n '250,285p' README.md
sed -n '1,35p' CHANGELOG.mdRepository: DowLucas/devscope-plugin Length of output: 42132 Synchronize intent queue operations with a portable lock.
Do not skip queueing whenever Suggested fix+# Acquire the queue lock with flock, or use an atomic mkdir fallback.
+_ds_agent_intent_unlock() {
+ if [ -n "${_DS_AGENT_INTENT_LOCKDIR:-}" ]; then
+ rmdir "$_DS_AGENT_INTENT_LOCKDIR" 2>/dev/null || true
+ _DS_AGENT_INTENT_LOCKDIR=
+ fi
+}
+
+_ds_agent_intent_lock() {
+ local file lockdir deadline
+ file="$1"
+ if command -v flock >/dev/null 2>&1; then
+ flock -w 2 9
+ return
+ fi
+ lockdir="${file}.lock.d"
+ deadline=$(( $(date +%s) + 2 ))
+ while ! mkdir "$lockdir" 2>/dev/null; do
+ [ "$(date +%s)" -ge "$deadline" ] && return 1
+ sleep 0.05
+ done
+ _DS_AGENT_INTENT_LOCKDIR="$lockdir"
+ trap '_ds_agent_intent_unlock' EXIT
+}
+
# Usage: _ds_record_agent_intent "$HOOK_INPUT_JSON"
_ds_record_agent_intent() {
local session file
session=$(printf '%s' "$1" | jq -r '.session_id // ""' 2>/dev/null) || return 0
[ -n "$session" ] || return 0
file=$(_ds_agent_intent_file "$session")
mkdir -p -m 0700 "$(dirname "$file")"
- printf '%s' "$1" | jq -c --arg privacy "$DEVSCOPE_PRIVACY" --argjson now "$(date +%s)" '
- .tool_input // {} | {
- agentType: (.subagent_type // "general-purpose"),
- description: (if $privacy == "private" then null else (.description // null) end),
- model: (.model // null),
- ts: $now
- }' >> "$file" 2>/dev/null || true
+ (
+ _ds_agent_intent_lock "$file" || exit 0
+ printf '%s' "$1" | jq -c --arg privacy "$DEVSCOPE_PRIVACY" --argjson now "$(date +%s)" '
+ .tool_input // {} | {
+ agentType: (.subagent_type // "general-purpose"),
+ description: (if $privacy == "private" then null else (.description // null) end),
+ model: (.model // null),
+ ts: $now
+ }' >> "$file" 2>/dev/null || true
+ ) 9>"${file}.lock"
}
@@
file=$(_ds_agent_intent_file "$1")
[ -f "$file" ] || return 0
(
- command -v flock >/dev/null 2>&1 && flock -w 2 9
+ _ds_agent_intent_lock "$file" || exit 0
picked=$(jq -cs --arg t "$2" --argjson now "$(date +%s)" '🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| # Prints the oldest intent of the type queued in the last 10 minutes and drops | ||
| # it (and any older ones) from the queue. Prints nothing when there is none. | ||
| # Usage: INTENT=$(_ds_take_agent_intent "$SESSION_ID" "$AGENT_TYPE") | ||
| _ds_take_agent_intent() { | ||
| local file | ||
| file=$(_ds_agent_intent_file "$1") | ||
| [ -f "$file" ] || return 0 | ||
| ( | ||
| command -v flock >/dev/null 2>&1 && flock -w 2 9 | ||
| picked=$(jq -cs --arg t "$2" --argjson now "$(date +%s)" ' | ||
| map(select(.ts >= $now - 600)) | ||
| | (map(.agentType == $t) | index(true)) as $i | ||
| | if $i == null then {take: null, rest: .} | ||
| else {take: .[$i], rest: (.[:$i] + .[$i + 1:])} end' "$file" 2>/dev/null) || exit 0 | ||
| printf '%s' "$picked" | jq -c '.rest[]' > "$file" | ||
| [ -s "$file" ] || rm -f "$file" | ||
| printf '%s' "$picked" | jq -c '.take // empty | del(.ts)' | ||
| ) 9>"${file}.lock" | ||
| } | ||
|
|
||
| # Cross-platform reverse file (tac on Linux, tail -r on macOS) | ||
| _ds_tac() { | ||
| if command -v tac >/dev/null 2>&1; then | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ INPUT=$(cat) | |
| AGENT_TYPE=$(echo "$INPUT" | jq -r '.agent_type // "unknown"') | ||
| AGENT_ID=$(echo "$INPUT" | jq -r '.agent_id // ""') | ||
| CWD=$(echo "$INPUT" | jq -r '.cwd // empty') | ||
| SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // ""') | ||
|
|
||
| # Detect parent agent via agent stack | ||
| PARENT_AGENT_ID="" | ||
|
|
@@ -22,8 +23,17 @@ if [ -n "$CWD" ] && [ -n "$AGENT_ID" ]; then | |
| echo "$AGENT_ID" >> "$STACK_FILE" | ||
| fi | ||
|
|
||
| PAYLOAD=$(jq -n --arg at "$AGENT_TYPE" --arg ai "$AGENT_ID" --arg pai "$PARENT_AGENT_ID" \ | ||
| # Description and model, as queued by the Agent tool's PreToolUse (tool-use.sh) | ||
| INTENT="{}" | ||
| if [ -n "$SESSION_ID" ]; then | ||
| INTENT=$(_ds_take_agent_intent "$SESSION_ID" "$AGENT_TYPE") | ||
| [ -n "$INTENT" ] || INTENT="{}" | ||
| fi | ||
|
|
||
| PAYLOAD=$(jq -n --arg at "$AGENT_TYPE" --arg ai "$AGENT_ID" --arg pai "$PARENT_AGENT_ID" --argjson intent "$INTENT" \ | ||
| '{agentType: $at, agentId: $ai} | ||
| | if $pai != "" then . + {parentAgentId: $pai} else . end') | ||
| | if $pai != "" then . + {parentAgentId: $pai} else . end | ||
| | if $intent.description then . + {description: $intent.description} else . end | ||
|
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. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
# Inspect the private-mode handling on the agent.start send path.
rg -n -C 5 'DEVSCOPE_PRIVACY|agent\.start|description|payload' \
scripts/agent-start.sh scripts/send-event.sh scripts/_helpers.shRepository: DowLucas/devscope-plugin Length of output: 12258 Check privacy mode before emitting a queued description. If the queued intent was recorded in standard mode and the current mode is private, 🐛 Suggested fix--- "a/scripts/agent-start.sh"
+++ "b/scripts/agent-start.sh"
@@ -30,10 +30,11 @@
[ -n "$INTENT" ] || INTENT="{}"
fi
-PAYLOAD=$(jq -n --arg at "$AGENT_TYPE" --arg ai "$AGENT_ID" --arg pai "$PARENT_AGENT_ID" --argjson intent "$INTENT" \
+PAYLOAD=$(jq -n --arg at "$AGENT_TYPE" --arg ai "$AGENT_ID" --arg pai "$PARENT_AGENT_ID" \
+ --arg privacy "$DEVSCOPE_PRIVACY" --argjson intent "$INTENT" \
'{agentType: $at, agentId: $ai}
| if $pai != "" then . + {parentAgentId: $pai} else . end
- | if $intent.description then . + {description: $intent.description} else . end
+ | if $privacy != "private" and $intent.description then . + {description: $intent.description} else . end
| if $intent.model then . + {model: $intent.model} else . end')
echo "$INPUT" | "$SCRIPT_DIR/send-event.sh" "agent.start" "$PAYLOAD"🤖 Prompt for AI Agents |
||
| | if $intent.model then . + {model: $intent.model} else . end') | ||
|
|
||
| echo "$INPUT" | "$SCRIPT_DIR/send-event.sh" "agent.start" "$PAYLOAD" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #!/usr/bin/env bash | ||
| # Tests for the subagent intent queue: tool-use.sh (PreToolUse of the Agent | ||
| # tool) queues description and model, agent-start.sh puts them on agent.start. | ||
| # | ||
| # bash tests/agents/run.sh | ||
| set -euo pipefail | ||
| ROOT="$(cd "$(dirname "$0")/../.." && pwd)" | ||
| TMP=$(mktemp -d) | ||
| export HOME="$TMP" XDG_CONFIG_HOME="$TMP/config" | ||
| unset DEVSCOPE_PRIVACY DEVSCOPE_NUDGE_MODE | ||
| # shellcheck disable=SC1091 | ||
| . "$ROOT/tests/lib/stub.sh" | ||
| respond '{}' | ||
|
|
||
| pass=0; fail=0 | ||
| ok() { pass=$((pass + 1)); echo "ok $1"; } | ||
| bad() { fail=$((fail + 1)); echo "FAIL $1: $2"; } | ||
| pre() { # session subagent_type description [model] | ||
| jq -n --arg s "$1" --arg t "$2" --arg d "$3" --arg m "${4:-}" \ | ||
| '{session_id: $s, cwd: "/tmp", hook_event_name: "PreToolUse", tool_name: "Agent", | ||
| tool_input: ({subagent_type: $t, description: $d, prompt: "do it"} | ||
| + (if $m != "" then {model: $m} else {} end))}' \ | ||
| | "$ROOT/scripts/tool-use.sh" >/dev/null | ||
| } | ||
| start() { # session agent_type agent_id | ||
| jq -n --arg s "$1" --arg t "$2" --arg a "$3" \ | ||
| '{session_id: $s, cwd: "/tmp", hook_event_name: "SubagentStart", agent_type: $t, agent_id: $a}' \ | ||
| | "$ROOT/scripts/agent-start.sh" >/dev/null | ||
| last_at /api/events '.body.payload' | ||
| } | ||
| wait_hits() { for _ in $(seq 1 30); do [ "$(hits)" -ge "$1" ] && return; sleep 0.1; done; } | ||
|
|
||
| pre s1 Explore "Map the topology code" haiku | ||
| p=$(start s1 Explore a1) | ||
| [ "$(printf '%s' "$p" | jq -r .description)" = "Map the topology code" ] && ok "description reaches agent.start" || bad "description" "$p" | ||
| [ "$(printf '%s' "$p" | jq -r .model)" = "haiku" ] && ok "model reaches agent.start" || bad "model" "$p" | ||
|
|
||
| pre s2 Explore "first"; pre s2 Plan "the plan"; pre s2 Explore "second" | ||
| [ "$(start s2 Plan a2 | jq -r .description)" = "the plan" ] && ok "matches by agent type" || bad "type match" "" | ||
| [ "$(start s2 Explore a3 | jq -r .description)" = "first" ] && ok "same type: oldest first" || bad "fifo 1" "" | ||
| [ "$(start s2 Explore a4 | jq -r .description)" = "second" ] && ok "same type: then the next" || bad "fifo 2" "" | ||
| p=$(start s2 Explore a5) | ||
| [ "$(printf '%s' "$p" | jq -r '.description // "none"')" = "none" ] && [ "$(printf '%s' "$p" | jq -r .agentId)" = "a5" ] \ | ||
| && ok "empty queue: plain agent.start" || bad "empty" "$p" | ||
|
|
||
| pre s4 general-purpose "secret task" sonnet | ||
| p=$(start s4 general-purpose a7) | ||
| [ "$(printf '%s' "$p" | jq -r .description)" = "secret task" ] && ok "standard mode keeps description" || bad "standard" "$p" | ||
| DEVSCOPE_PRIVACY=private pre s5 general-purpose "secret task" sonnet | ||
| grep -q "secret task" "$HOME/.cache/devscope/intents/s5.jsonl" && bad "private queue" "description written to disk" || ok "private mode never stores the description" | ||
| p=$(start s5 general-purpose a8) | ||
| [ "$(printf '%s' "$p" | jq -r '.description // "none"')" = "none" ] && [ "$(printf '%s' "$p" | jq -r .model)" = "sonnet" ] \ | ||
| && ok "private mode: model only" || bad "private" "$p" | ||
|
|
||
| pre s6 Explore "old one" | ||
| jq -c '.ts = (.ts - 3600)' "$HOME/.cache/devscope/intents/s6.jsonl" > "$TMP/x" && mv "$TMP/x" "$HOME/.cache/devscope/intents/s6.jsonl" | ||
| [ "$(start s6 Explore a9 | jq -r '.description // "none"')" = "none" ] && ok "intents older than 10 min are dropped" || bad "stale" "" | ||
| [ ! -f "$HOME/.cache/devscope/intents/s6.jsonl" ] && ok "empty queue file is removed" || bad "cleanup" "file left" | ||
|
|
||
| echo "---"; echo "$pass passed, $fail failed" | ||
| [ "$fail" -eq 0 ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Suppress directory-creation errors in the hook.
If the cache directory cannot be created,
mkdirwrites an error to stderr on thePreToolUsepath. Return without recording the intent when creation fails. Suppress that expected error. As per coding guidelines, “Hook scripts are non-blocking — they must exit quickly and suppress errors.” (gnu.org)🤖 Prompt for AI Agents
Source: Coding guidelines