(
set -o pipefail
repo=$PWD
scratch=$(mktemp -d ${TMPDIR:-/tmp}/workflow-proof-XXXXXXXX)
export T3CODE_DEV_SEED=0
echo "Linux real server + MCP client; Node $(node --version)"
echo "commit: $(git rev-parse HEAD)"
export T3CODE_HOME="$scratch/state" T3CODE_WORKFLOWS="$scratch/workflows"
mkdir -p "$T3CODE_HOME/userdata" "$T3CODE_WORKFLOWS" "$scratch/project"
echo '{"workflowsEnabled":true}' > "$T3CODE_HOME/userdata/settings.json"
cat > "$T3CODE_WORKFLOWS/approve.ts" <<'TS'
export default T3.workflow("approve", { version: 1, payload: Schema.Struct({}) },
  Effect.fn(function* () {
    yield* T3.ask("review", { title: "Proceed?", choices: ["yes", "no"] });
    return "done";
  }));
TS
# Sends $SIGNAL as a raw t3_workflow signal through the CLI's own MCP client, so
# fields the CLI itself would refuse still reach the server.
cat > "$scratch/signal.mjs" <<'JS'
import { NodeCrypto, NodeServices } from "@effect/platform-node";
import * as Net from "@t3tools/shared/Net";
import { Effect, Layer, Option, Schema } from "effect";
import { Command } from "effect/cli";
import { withThreadTools } from "./src/cli/thread.ts";

const call = { action: "signal", value: { choice: "yes" }, ...JSON.parse(process.env.SIGNAL) };
const probe = Command.make("probe").pipe(Command.withHandler(() =>
  withThreadTools({ baseDir: Option.none(), json: true }, (tools) =>
    tools.call("t3_workflow", call, Schema.Unknown).pipe(
      Effect.tap((value) => Effect.sync(() => console.log(JSON.stringify(value))))))));
await Effect.runPromise(Command.runWith(probe, { version: "1" })([]).pipe(
  Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, Net.layer))))
  .catch((error) => console.log(`refused: ${error.message}`));
JS
t3() { node "$repo/apps/server/src/bin.ts" "$@"; }
signal() { (cd "$repo/apps/server" && SIGNAL=$1 node - < "$scratch/signal.mjs"); }
# The run's status, and whether its question is still listed.
state() {
  echo "run: $(t3 workflow show "$1" --json | jq -r .run.status)," \
    "question pending: $(t3 workflow questions --json | jq --arg r "$1" 'any(.runId == $r)')"
}
t3 serve --port "${PROOF_PORT:-15346}" "$scratch/project" > "$scratch/server.log" 2>&1 &
server_pid=$!
trap 'kill "$server_pid" 2>/dev/null; wait "$server_pid" 2>/dev/null' EXIT
for _ in $(seq 120); do
  [ -f "$T3CODE_HOME/userdata/server-runtime.json" ] && break
  kill -0 "$server_pid" 2>/dev/null || break
  sleep 0.5
done
[ -f "$T3CODE_HOME/userdata/server-runtime.json" ] || { cat "$scratch/server.log"; echo "server did not start" >&2; exit 1; }
# Starts an approve run and waits until its question is listed.
ask() {
  rid=$(t3 workflow start approve --key "$1" --json | jq -r .run.runId) || return 1
  for _ in $(seq 40); do
    t3 workflow questions --json | jq -e --arg r "$rid" 'any(.runId == $r)' > /dev/null && return
    sleep 0.25
  done
  echo "question never listed" >&2; return 1
}
# Waits for the run to leave "waiting", then prints its state.
settle() {
  for _ in $(seq 3); do
    [ "$(t3 workflow show "$1" --json | jq -r .run.status)" = waiting ] || break
    sleep 0.25
  done
  state "$1"
}

# Both destinations at once. main: {"delivery":"delivered"}, then the run completes.
# This fix: refused, the run still waits and its question stays pending.
echo "CASE: both complete destination pairs"
ask mixed || exit 1
signal "{\"runId\":\"$rid\",\"wait\":\"review\",\"workflow\":\"other\",\"channel\":\"other\"}"
settle "$rid" || exit 1

echo "CASE: run pair plus channel only"
ask partial || exit 1
signal "{\"runId\":\"$rid\",\"wait\":\"review\",\"channel\":\"other\"}"
settle "$rid" || exit 1

echo "CASE: control, run pair only"
# Control, one destination: delivered and completed on both.
ask control || exit 1
out=$(signal "{\"runId\":\"$rid\",\"wait\":\"review\"}") && echo "$out"
[ "$out" = '{"delivery":"delivered"}' ] || exit 1
settle "$rid"
)

