Skip to content

Commit 8060b34

Browse files
authored
Merge pull request #3 from warpdotdev/harry/app-3556-add-two-way-handshake-for-plugin-warp-versions
Add two way handshake for claude code plugin and Warp version
2 parents ecf7c7a + f0ac3a1 commit 8060b34

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

plugins/warp/scripts/build-payload.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,28 @@
1313
# The function extracts common fields (session_id, cwd, project) from the
1414
# hook's stdin JSON (passed as $1), then merges any extra jq args you pass.
1515

16+
# The current protocol version this plugin knows how to produce.
17+
PLUGIN_CURRENT_PROTOCOL_VERSION=1
18+
19+
# Negotiate the protocol version with Warp.
20+
# Uses min(plugin_current, warp_declared), falling back to 1 if Warp doesn't advertise a version.
21+
negotiate_protocol_version() {
22+
local warp_version="${WARP_CLI_AGENT_PROTOCOL_VERSION:-1}"
23+
if [ "$warp_version" -lt "$PLUGIN_CURRENT_PROTOCOL_VERSION" ] 2>/dev/null; then
24+
echo "$warp_version"
25+
else
26+
echo "$PLUGIN_CURRENT_PROTOCOL_VERSION"
27+
fi
28+
}
29+
1630
build_payload() {
1731
local input="$1"
1832
local event="$2"
1933
shift 2
2034

35+
local protocol_version
36+
protocol_version=$(negotiate_protocol_version)
37+
2138
# Extract common fields from the hook input
2239
local session_id cwd project
2340
session_id=$(echo "$input" | jq -r '.session_id // empty' 2>/dev/null)
@@ -30,11 +47,12 @@ build_payload() {
3047
# Build the payload: common fields + any extra args passed by the caller.
3148
# Extra args should be jq flag pairs like: --arg key "value" or --argjson key '{"a":1}'
3249
jq -nc \
50+
--argjson v "$protocol_version" \
3351
--arg agent "claude" \
3452
--arg event "$event" \
3553
--arg session_id "$session_id" \
3654
--arg cwd "$cwd" \
3755
--arg project "$project" \
3856
"$@" \
39-
'{v:1, agent:$agent, event:$event, session_id:$session_id, cwd:$cwd, project:$project} + $ARGS.named'
57+
'{v:$v, agent:$agent, event:$event, session_id:$session_id, cwd:$cwd, project:$project} + $ARGS.named'
4058
}

plugins/warp/tests/test-hooks.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp/proj"}' "stop" \
112112
assert_json_field "quotes in query preserved" "$PAYLOAD" ".query" 'what does "hello world" mean?'
113113
assert_json_field "parens in response preserved" "$PAYLOAD" ".response" 'It means greeting. Use: printf("hello")'
114114

115+
echo ""
116+
echo "--- Protocol version negotiation ---"
117+
118+
# Default: no env var set → falls back to plugin max (1)
119+
unset WARP_CLI_AGENT_PROTOCOL_VERSION
120+
PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp"}' "stop")
121+
assert_json_field "defaults to v1 when env var absent" "$PAYLOAD" ".v" "1"
122+
123+
# Warp declares v1 → use 1
124+
export WARP_CLI_AGENT_PROTOCOL_VERSION=1
125+
PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp"}' "stop")
126+
assert_json_field "v1 when warp declares 1" "$PAYLOAD" ".v" "1"
127+
128+
# Warp declares a higher version than the plugin knows → capped to plugin current
129+
export WARP_CLI_AGENT_PROTOCOL_VERSION=99
130+
PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp"}' "stop")
131+
assert_json_field "capped to plugin current when warp is ahead" "$PAYLOAD" ".v" "1"
132+
133+
# Warp declares a lower version than the plugin knows → use warp's version
134+
# (not testable with PLUGIN_MAX=1 since there's no v0, but we verify the min logic
135+
# by temporarily overriding the variable)
136+
PLUGIN_CURRENT_PROTOCOL_VERSION=5
137+
export WARP_CLI_AGENT_PROTOCOL_VERSION=3
138+
PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp"}' "stop")
139+
assert_json_field "uses warp version when plugin is ahead" "$PAYLOAD" ".v" "3"
140+
PLUGIN_CURRENT_PROTOCOL_VERSION=1
141+
142+
# Clean up
143+
unset WARP_CLI_AGENT_PROTOCOL_VERSION
144+
115145
# --- Summary ---
116146

117147
echo ""

0 commit comments

Comments
 (0)