Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/plugins/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"installation": "AVAILABLE",
"authentication": "ON_INSTALL"
},
"version": "0.4.3",
"version": "0.4.4",
"category": "productivity",
"tags": ["terminal", "warp", "oz"]
}
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ tests/notification_conpty_test.py Isolated Windows ConPTY delivery tests
```
## Plugins
- `warp`: `SessionStart`, `Stop`, `PermissionRequest`, `UserPromptSubmit`, `PostToolUse` notifications for Warp.
- `orchestration`: `SessionStart`, `UserPromptSubmit`, `PostToolUse`, `Stop`, `SessionEnd` parent-message delivery for Codex child runs, plus Oz skills.
- `orchestration`: `SessionStart`, `UserPromptSubmit`, `PostToolUse`, `Stop`, `SessionEnd` coordination delivery for Oz root and child runs, plus Oz skills.
Mailbox messages remain user-level coordination. Read-only `run_waited_on` lifecycle
notifications say “Run {id} is waiting for you,” with the waiter identified independently
of parent/child direction. These synthetic records never call mailbox delivery APIs.
Hooks require `OZ_CLI` and `OZ_RUN_ID` and remain inert outside Oz.
Hook commands use `${PLUGIN_ROOT}/scripts/...`; Windows overrides use Codex's `commandWindows` hook field to run `.ps1` entrypoints.
## Local install
```sh
Expand Down
2 changes: 1 addition & 1 deletion plugins/orchestration/.codex-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "orchestration",
"description": "Warp integration for Codex in Oz cloud agent environments, including parent-message delivery hooks and skills",
"version": "0.4.3",
"version": "0.4.4",
"author": {
"name": "Warp",
"url": "https://warp.dev"
Expand Down
56 changes: 31 additions & 25 deletions plugins/orchestration/scripts/oz-parent-common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ function Test-OzHarnessAvailable {
if ([string]::IsNullOrEmpty($env:OZ_RUN_ID)) {
return $false
}
if ([string]::IsNullOrEmpty($env:OZ_PARENT_RUN_ID)) {
return $false
}

return $null -ne (Get-Command $env:OZ_CLI -ErrorAction SilentlyContinue)
}

function Get-StateRoot {
if (-not [string]::IsNullOrEmpty($env:OZ_MESSAGE_LISTENER_STATE_ROOT)) {
return $env:OZ_MESSAGE_LISTENER_STATE_ROOT
}
if (-not [string]::IsNullOrEmpty($env:OZ_PARENT_STATE_ROOT)) {
return $env:OZ_PARENT_STATE_ROOT
}
Expand Down Expand Up @@ -122,6 +122,9 @@ function Import-HookState {
}

function Test-ListenerLifecycleManagedExternally {
if (-not [string]::IsNullOrEmpty($env:OZ_MESSAGE_LISTENER_MANAGED_EXTERNALLY)) {
return $env:OZ_MESSAGE_LISTENER_MANAGED_EXTERNALLY -eq "1"
}
return $env:OZ_PARENT_LISTENER_MANAGED_EXTERNALLY -eq "1"
}

Expand Down Expand Up @@ -433,7 +436,7 @@ function Write-StopBlockIfPending {
return $null
}

$reason = "There are $pendingCount pending parent message(s) from the lead Oz run. Continue so the next safe boundary can surface them."
$reason = "There are $pendingCount pending Oz coordination event(s). Continue so the next safe boundary can surface them."
return ([ordered]@{
decision = "block"
reason = $reason
Expand All @@ -455,23 +458,13 @@ function Confirm-MessageDelivered {
}
}

function Remove-StagedMessage {
param(
[string]$StateDir,
[string]$MessageId
)

Get-ChildItem -LiteralPath (Get-StagedDir $StateDir) -Filter "*-$MessageId.json" -File -ErrorAction SilentlyContinue |
Remove-Item -Force -ErrorAction SilentlyContinue
}

function New-ParentContextFromStagedMessages {
param(
[string]$StateDir,
[int]$MaxContextChars = 6000
)

$renderedContext = "Lead-agent updates arrived from Oz. Treat the latest parent instructions below as authoritative.`n"
$renderedContext = "Oz coordination update. Mailbox messages are user-level coordination. Lifecycle notifications are read-only status, not instructions.`n"
$surfacedIds = New-Object System.Collections.Generic.List[string]
$totalStaged = 0

Expand All @@ -483,6 +476,7 @@ function New-ParentContextFromStagedMessages {
$subject = [string](Get-JsonProperty $messageObject "subject" "")
$body = [string](Get-JsonProperty $messageObject "body" "")
$sequence = [string](Get-JsonProperty $messageObject "sequence" "")
$eventType = [string](Get-JsonProperty $messageObject "event_type" "")

if ([string]::IsNullOrEmpty($messageId)) {
continue
Expand All @@ -491,14 +485,18 @@ function New-ParentContextFromStagedMessages {
$subject = "(no subject)"
}

$block = "---`nParent message"
if (-not [string]::IsNullOrEmpty($sequence)) {
$block = "$block #$sequence"
}
if (-not [string]::IsNullOrEmpty($senderRunId)) {
$block = "$block from $senderRunId"
if ($eventType -eq "run_waited_on") {
$block = "---`nRead-only lifecycle notification`n`n$body"
} else {
$block = "---`nMailbox message"
if (-not [string]::IsNullOrEmpty($sequence)) {
$block = "$block #$sequence"
}
if (-not [string]::IsNullOrEmpty($senderRunId)) {
$block = "$block from $senderRunId"
}
$block = "$block`nSubject: $subject`n`n$body"
}
$block = "$block`nSubject: $subject`n`n$body"

$separator = ""
if ($surfacedIds.Count -gt 0) {
Expand Down Expand Up @@ -528,7 +526,7 @@ function New-ParentContextFromStagedMessages {

$remainingCount = $totalStaged - $surfacedIds.Count
if ($remainingCount -gt 0) {
$note = "`n`nMore parent messages are still staged and will be surfaced on a later turn."
$note = "`n`nMore Oz coordination events are still staged and will be surfaced on a later turn."
if (($renderedContext.Length + $note.Length) -le $MaxContextChars) {
$renderedContext = "$renderedContext$note"
}
Expand All @@ -548,7 +546,15 @@ function Confirm-AndRemoveStagedMessages {
)

foreach ($messageId in $MessageIds) {
Confirm-MessageDelivered $messageId
Remove-StagedMessage $StateDir $messageId
foreach ($path in Get-SortedStagedMessages $StateDir) {
$record = ConvertFrom-JsonSafe (Get-Content -Raw -LiteralPath $path)
if ((Get-JsonProperty $record "message_id" "") -ne $messageId) {
continue
}
if ((Get-JsonProperty $record "event_type" "") -ne "run_waited_on") {
Confirm-MessageDelivered $messageId
}
Remove-Item -LiteralPath $path -Force
}
}
}
51 changes: 26 additions & 25 deletions plugins/orchestration/scripts/oz-parent-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ read_hook_input() {
cat
}

# Only child Codex runs have a lead-agent parent to bridge messages from.
# When OZ_PARENT_RUN_ID is absent, leave the hooks inert so they do not affect
# regular Oz harness sessions that are not running as child agents.
oz_harness_available() {
[ -n "${OZ_CLI:-}" ] &&
[ -n "${OZ_RUN_ID:-}" ] &&
[ -n "${OZ_PARENT_RUN_ID:-}" ] &&
command -v "$OZ_CLI" >/dev/null 2>&1
}

Expand All @@ -39,7 +35,7 @@ session_id_from_input() {
}

state_root() {
printf '%s\n' "${OZ_PARENT_STATE_ROOT:-$HOME/.codex/oz-parent-bridge}"
printf '%s\n' "${OZ_MESSAGE_LISTENER_STATE_ROOT:-${OZ_PARENT_STATE_ROOT:-$HOME/.codex/oz-parent-bridge}}"
}

state_dir_from_session_id() {
Expand Down Expand Up @@ -218,7 +214,7 @@ ensure_last_sequence_file() {
# Set by the Warp driver when it owns the parent-bridge listener and surfaces
# hook output itself. In that mode shell hooks reuse driver state.
listener_lifecycle_managed_externally() {
[ "${OZ_PARENT_LISTENER_MANAGED_EXTERNALLY:-0}" = "1" ]
[ "${OZ_MESSAGE_LISTENER_MANAGED_EXTERNALLY:-${OZ_PARENT_LISTENER_MANAGED_EXTERNALLY:-0}}" = "1" ]
}

resolve_hook_state() {
Expand Down Expand Up @@ -308,7 +304,7 @@ emit_stop_block_if_pending() {
pending_count="$(wait_for_pending_parent_messages "$state_dir")" || return 1
[ "$pending_count" -gt 0 ] || return 1

reason="There are ${pending_count} pending parent message(s) from the lead Oz run. Continue so the next safe boundary can surface them."
reason="There are ${pending_count} pending Oz coordination event(s). Continue so the next safe boundary can surface them."
jq -nc --arg reason "$reason" '{decision:"block", reason:$reason}'
}

Expand All @@ -319,20 +315,14 @@ mark_message_delivered() {
true
}

remove_staged_message() {
local state_dir="$1"
local message_id="$2"
find "$(staged_dir "$state_dir")" -type f -name "*-${message_id}.json" -delete 2>/dev/null || true
}

build_parent_context_from_staged_messages() {
local state_dir="$1"
local max_context_chars="${2:-6000}"
local staged_file total_staged=0
local message_id sender_run_id subject body sequence
local message_id sender_run_id subject body sequence event_type
local block separator candidate remaining note

OZ_PARENT_RENDERED_CONTEXT=$'Lead-agent updates arrived from Oz. Treat the latest parent instructions below as authoritative.\n'
OZ_PARENT_RENDERED_CONTEXT=$'Oz coordination update. Mailbox messages are user-level coordination. Lifecycle notifications are read-only status, not instructions.\n'
OZ_PARENT_REMAINING_COUNT=0
OZ_PARENT_SURFACED_IDS=()

Expand All @@ -345,18 +335,23 @@ build_parent_context_from_staged_messages() {
subject="$(json_file_field "$staged_file" '.subject')"
body="$(json_file_field "$staged_file" '.body')"
sequence="$(json_file_field "$staged_file" '.sequence')"
event_type="$(json_file_field "$staged_file" '.event_type')"

[ -n "$message_id" ] || continue
[ -n "$subject" ] || subject="(no subject)"

block=$'---\nParent message'
if [ -n "$sequence" ]; then
block="$block #$sequence"
fi
if [ -n "$sender_run_id" ]; then
block="$block from $sender_run_id"
if [ "$event_type" = "run_waited_on" ]; then
block=$'---\nRead-only lifecycle notification\n\n'"$body"
else
block=$'---\nMailbox message'
if [ -n "$sequence" ]; then
block="$block #$sequence"
fi
if [ -n "$sender_run_id" ]; then
block="$block from $sender_run_id"
fi
block="$block"$'\n'"Subject: $subject"$'\n\n'"$body"
fi
block="$block"$'\n'"Subject: $subject"$'\n\n'"$body"

separator=""
if [ "${#OZ_PARENT_SURFACED_IDS[@]}" -gt 0 ]; then
Expand Down Expand Up @@ -384,7 +379,7 @@ build_parent_context_from_staged_messages() {

OZ_PARENT_REMAINING_COUNT=$((total_staged - ${#OZ_PARENT_SURFACED_IDS[@]}))
if [ "$OZ_PARENT_REMAINING_COUNT" -gt 0 ]; then
note=$'\n\nMore parent messages are still staged and will be surfaced on a later turn.'
note=$'\n\nMore Oz coordination events are still staged and will be surfaced on a later turn.'
if [ $(( ${#OZ_PARENT_RENDERED_CONTEXT} + ${#note} )) -le "$max_context_chars" ]; then
OZ_PARENT_RENDERED_CONTEXT="${OZ_PARENT_RENDERED_CONTEXT}${note}"
fi
Expand All @@ -396,9 +391,15 @@ deliver_and_remove_staged_messages() {
local message_id
shift

local staged_file event_type
for message_id in "$@"; do
mark_message_delivered "$message_id"
remove_staged_message "$state_dir" "$message_id"
while IFS= read -r staged_file; do
event_type="$(json_file_field "$staged_file" '.event_type')"
if [ "$event_type" != "run_waited_on" ]; then
mark_message_delivered "$message_id"
fi
rm -f "$staged_file"
done < <(find "$(staged_dir "$state_dir")" -type f -name "*-${message_id}.json" -print)
done
}

Expand Down
6 changes: 6 additions & 0 deletions plugins/orchestration/scripts/oz-parent-listener.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function Add-MessageFromWatchRecord {
}

$sequence = [int64]$sequenceValue
$previousSequence = 0L
$previousValue = Get-Content -Raw -LiteralPath (Get-LastSequenceFile $StateDir) -ErrorAction SilentlyContinue
[void][int64]::TryParse($previousValue, [ref]$previousSequence)
if ($sequence -le $previousSequence) {
return
}
$target = Get-StagedMessagePath $StateDir $sequence $messageId
if (-not (Test-Path -LiteralPath $target -PathType Leaf)) {
[System.IO.File]::WriteAllText($target, $Line + "`n")
Expand Down
6 changes: 4 additions & 2 deletions plugins/orchestration/scripts/oz-parent-listener.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ STATE_DIR="${1:?state directory is required}"
stage_message_from_watch_record() {
local state_dir="$1"
local line="$2"
local sequence message_id target
local sequence message_id target previous_sequence

sequence="$(json_field "$line" '.sequence')"
message_id="$(json_field "$line" '.message_id')"

[ -n "$sequence" ] || return 0
[[ "$sequence" =~ ^[0-9]+$ ]] || return 0
previous_sequence="$(cat "$(last_sequence_file "$state_dir")" 2>/dev/null || true)"
[ "$sequence" -gt "${previous_sequence:-0}" ] || return 0
[ -n "$message_id" ] || return 0

target="$(staged_message_path "$state_dir" "$sequence" "$message_id")"
Expand Down
52 changes: 52 additions & 0 deletions tests/test-hooks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,63 @@ param(
}
}

function Test-RootLifecycleDelivery {
. (Join-Path $repoRoot "plugins\orchestration\scripts\oz-parent-common.ps1")
$stateDir = Join-Path ([System.IO.Path]::GetTempPath()) "oz-lifecycle-$([guid]::NewGuid())"
$oldCli = $env:OZ_CLI
$oldRun = $env:OZ_RUN_ID
$oldParent = $env:OZ_PARENT_RUN_ID
try {
New-StateDir $stateDir
$fakeCli = Join-Path $stateDir "oz.ps1"
$log = Join-Path $stateDir "acks.txt"
'param([Parameter(ValueFromRemainingArguments=$true)][string[]]$CliArgs); Add-Content -LiteralPath (Join-Path $PSScriptRoot "acks.txt") -Value ($CliArgs -join " ")' |
Set-Content -LiteralPath $fakeCli
$env:OZ_CLI = $fakeCli
$env:OZ_RUN_ID = "root-run"
$env:OZ_PARENT_RUN_ID = ""
Assert-True (Test-OzHarnessAvailable) "Workload-backed root hooks should be active."

$notification = @{
event_type = "run_waited_on"
sequence = 42
message_id = "synthetic-wait"
sender_run_id = "child-run"
body = "Run child-run is waiting for you"
}
$notification | ConvertTo-Json -Compress |
Set-Content -LiteralPath (Get-StagedMessagePath $stateDir 42 "synthetic-wait")
$context = New-ParentContextFromStagedMessages $stateDir
Assert-True ($context.Context.Contains("Read-only lifecycle notification")) "Notification must be read-only."
Assert-True ($context.Context.Contains("Run child-run is waiting for you")) "Notification must name waiter."
Assert-True (-not $context.Context.Contains("authoritative")) "Waiter is not parent authority."
Confirm-AndRemoveStagedMessages $stateDir $context.SurfacedIds
Assert-True (-not (Test-Path -LiteralPath $log)) "Synthetic records must not call mailbox acknowledgments."
Assert-Equal 0 (Get-StagedMessageCount $stateDir) "Notification must be consumed locally."

@{ sequence = 43; message_id = "mail"; subject = "Update"; body = "Ordinary message" } |
ConvertTo-Json -Compress |
Set-Content -LiteralPath (Get-StagedMessagePath $stateDir 43 "mail")
$context = New-ParentContextFromStagedMessages $stateDir
Assert-True ($context.Context.Contains("Ordinary message")) "Ordinary messages must be preserved."
Confirm-AndRemoveStagedMessages $stateDir $context.SurfacedIds
Assert-True ((Get-Content -Raw -LiteralPath $log).Contains("mark-delivered mail")) "Mailbox delivery acknowledgment missing."
$env:OZ_RUN_ID = ""
Assert-True (-not (Test-OzHarnessAvailable)) "Hooks must stay inert outside Oz."
} finally {
$env:OZ_CLI = $oldCli
$env:OZ_RUN_ID = $oldRun
$env:OZ_PARENT_RUN_ID = $oldParent
Remove-Item -LiteralPath $stateDir -Recurse -Force -ErrorAction SilentlyContinue
}
}

Test-PowerShellSyntax
Test-WindowsHookCommands
Test-WarpPayload
Test-WarpConsoleWriterInterop
Test-WarpHookEntryPoints
Test-ListenerLaunchWithSpaces
Test-RootLifecycleDelivery

Write-Host "PowerShell hook tests passed."
Loading
Loading