forked from molecule-ai/molecule-core
Compare commits
331 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 184ce7ae4e | |||
| ff75aeb43e | |||
| 81cf0cbf98 | |||
| 412dec0d87 | |||
| 39931acd9c | |||
| 6f19b88fa7 | |||
| 83454e5efd | |||
| 8254bedf30 | |||
| ec72f199e6 | |||
| ae22a55675 | |||
| 08648bf4b1 | |||
| eec4ea2e7d | |||
| 6201d12533 | |||
| 81e83c05b7 | |||
| 5b5eacbb29 | |||
| c8fca1467e | |||
| 7c8b81c6eb | |||
| fc1c45789e | |||
| e3a18ed8e8 | |||
| 9f551319d2 | |||
| 1052f8bdb0 | |||
| 30fb507165 | |||
| 77e9a965ac | |||
| 5334d60de4 | |||
| d6c0227e3f | |||
| 27db090d3d | |||
| 0f25f6de97 | |||
| 9991057ad1 | |||
| b89a49ec93 | |||
| f5613bf099 | |||
| 9bd2a2c45f | |||
| a489ee1a7c | |||
| c79ba05ed5 | |||
| 6470e5f41b | |||
| aa560c0314 | |||
| 7644e82f2f | |||
| 33fabdf483 | |||
| abba16beb4 | |||
| 9c752e0673 | |||
| be18b9c8f9 | |||
| 2cb1b26512 | |||
| 48d1945269 | |||
| a04a49f7aa | |||
| bbec4cfcfb | |||
| 19c25a9278 | |||
| e50799bc29 | |||
| 07839580a0 | |||
| 2227a14b1e | |||
| e72f9ad107 | |||
| 17aec22f9b | |||
| 8388144098 | |||
| a327d207da | |||
| afe5a0cfe9 | |||
| 529c3f3922 | |||
| c778b62202 | |||
| d80bffe3e3 | |||
| 0c461eb9f1 | |||
| 86015412eb | |||
| f81813f708 | |||
| 58253f0673 | |||
| 28ef75d25e | |||
| 243f9bc2b1 | |||
| 43bf94a07c | |||
| 55f5c0b0ff | |||
| 86fdaad111 | |||
| 6125700c39 | |||
| 89ee8e4d04 | |||
| db14191bc9 | |||
| 26e2e97006 | |||
| ec574f3d4b | |||
| 42f2ea3f4f | |||
| e0e9201142 | |||
| 90d202c80a | |||
| 1e8d7ae17c | |||
| ecf5f6fbf3 | |||
| fcdf79774d | |||
| d6337a1ae9 | |||
| 471dff25e9 | |||
| 3d2a50e2a2 | |||
| 9e678ccd5e | |||
| 191ef3be91 | |||
| 25fd6b021d | |||
| a959feae84 | |||
| c661ea4cd3 | |||
| 49027af419 | |||
| 4c9f12258d | |||
| da46bdeded | |||
| d890fd9a3f | |||
| ec1f21922c | |||
| ca61213578 | |||
| 118b8e47ad | |||
| ab164c1967 | |||
| b5f530e27a | |||
| 44bb35a926 | |||
| 024ef260db | |||
| d175d0c4c1 | |||
| d21ac991c1 | |||
| c85783fbee | |||
| b375252dc8 | |||
| 3d226a2c68 | |||
| da6d319c48 | |||
| 76e9656a7b | |||
| 35017c5452 | |||
| d10c1a1a36 | |||
| 61b7755c3c | |||
| 21a7e7b0e7 | |||
| 9a772bf946 | |||
| 0a90d7ae1a | |||
| 5b7f4d260b | |||
| f0fd7b4d9e | |||
| 7993693cf1 | |||
| 789d705866 | |||
| cb820acbd6 | |||
| 52915268b2 | |||
| 82e7059e0e | |||
| 5950d4cd81 | |||
| 1e12ed7e9f | |||
| 4f67fe59fb | |||
| 410275e5af | |||
| 1557743ef9 | |||
| e727b31246 | |||
| ae05f91bd8 | |||
| c89f17a2aa | |||
| cbe48c2225 | |||
| b0bcd97781 | |||
| 56149f8a24 | |||
| 0134353a48 | |||
| aca7d99152 | |||
| aec0fb35d2 | |||
| b5c0b4d371 | |||
| 2ed4f4fb41 | |||
| 02b325063b | |||
| 43caac911a | |||
| 2e505e7748 | |||
| ae79b9e9fe | |||
| b3b9a242d6 | |||
| ed6dfe01e5 | |||
| 4c9309e801 | |||
| 20f76c4fdf | |||
| ca6e7c39cf | |||
| ba63f76e10 | |||
| b037d555fa | |||
| 62fc25757c | |||
| a345adacad | |||
| 7cc1c39c49 | |||
| 8152cfc81e | |||
| 111c3d2c01 | |||
| 46d79a3e3b | |||
| 2198f92dcb | |||
| beab899501 | |||
| b851cfc813 | |||
| 3cb72b1df0 | |||
| 11c9ed2a46 | |||
| c0bfd19b9e | |||
| e0f9434eaf | |||
| 80e4b9ac9a | |||
| daefdd21c5 | |||
| 8df8487bbe | |||
| 9a835ef631 | |||
| 174e594690 | |||
| 856c967950 | |||
| 73f7e0c03b | |||
| 31f9a5e85e | |||
| c5dd14d8db | |||
| 7e1fdf5847 | |||
| d084d7e61a | |||
| 9c9be4cf12 | |||
| f256bfa9c6 | |||
| 463316772b | |||
| dfd0bc528c | |||
| 4ea6f437e9 | |||
| a872202fe7 | |||
| 2b862f65f9 | |||
| 53760a8a2f | |||
| 0f389ba325 | |||
| 472862bc50 | |||
| 461e5dcad0 | |||
| b5435b4732 | |||
| 4b16c95450 | |||
| f1b72af97e | |||
| 31facfc5c4 | |||
| 19e7acdc22 | |||
| 1ce51abea4 | |||
| 0ec226e119 | |||
| 872b781f64 | |||
| 0dd1244510 | |||
| 26fa220bef | |||
| 5559e96400 | |||
| 3bc7749e84 | |||
| 6d7a7fc86f | |||
| ecb3c75d74 | |||
| 2f7beb9bce | |||
| bd881f8756 | |||
| e39d818ac4 | |||
| ed4d24fb8c | |||
| 3a5544a9e6 | |||
| 095171f163 | |||
| 9c7b34cb7f | |||
| 8514ff1a96 | |||
| 1785732bbb | |||
| 066a0772ee | |||
| 3f2cc8cdd6 | |||
| 5c80b9c3d6 | |||
| a8850bac55 | |||
| adfa34c4ae | |||
| 7692dd4975 | |||
| 28f22609d9 | |||
| e67a854a33 | |||
| 3e7d483b8c | |||
| 4f4b6c4f90 | |||
| fc10386a78 | |||
| 1282c1c8ff | |||
| a242ca8b01 | |||
| ac9b07b7ad | |||
| 41ae4ec50b | |||
| 02960209a0 | |||
| d866d3aa5f | |||
| 61d5908817 | |||
| 89bdf29d6f | |||
| 700d44ec3d | |||
| f70071e1e1 | |||
| 63ac99788b | |||
| 28472f0d2d | |||
| f42feb4ed7 | |||
| 99e7f13149 | |||
| 6488ba09e7 | |||
| 8176b5142d | |||
| 314277769e | |||
| e0b567e992 | |||
| 707e4d7342 | |||
| 4f9e3feece | |||
| 10752fe330 | |||
| 8f7122a9b6 | |||
| b3982035b3 | |||
| d1122f8d28 | |||
| 4b35d25d86 | |||
| 46731729d4 | |||
| 6dc2d907a2 | |||
| 849bc97349 | |||
| e13dcab5e0 | |||
| 721010307c | |||
| 9f47ecf86e | |||
| ebc20794f3 | |||
| 73a949bb5c | |||
| 281cb04163 | |||
| fe7ff5440d | |||
| 5b0a75ab73 | |||
| a6dadc7ee0 | |||
| 5e52a0fdad | |||
| 6b445aae2d | |||
| 4f3d51bd61 | |||
| 9a64aeaa2c | |||
| 2d783b5ca6 | |||
| 6fc328ef44 | |||
| bb3212ad37 | |||
| 1986260603 | |||
| d297e75fc9 | |||
| 3ae0513209 | |||
| 4b6373861c | |||
| 3886e8fb9f | |||
| d48693144b | |||
| 1b207b214d | |||
| 1e97fb9a16 | |||
| 7cffff844b | |||
| 4a0d7cd545 | |||
| 35b3ea598a | |||
| 1161b97faf | |||
| 059962a0a3 | |||
| b07575c710 | |||
| 586fa5f84e | |||
| b937415e1e | |||
| 0f46c7eefe | |||
| 8aea1f008c | |||
| 8417bce50d | |||
| 3195657837 | |||
| 7b0bd32957 | |||
| 6fb9bc9bcd | |||
| 9cd2c02f14 | |||
| 9929f73e80 | |||
| 829ab66462 | |||
| 3b3e821a60 | |||
| a08eaa6ca2 | |||
| c5322f318a | |||
| 290e6dfdc3 | |||
| f74fff6ae4 | |||
| 5bfa4b1d80 | |||
| 51e7d94605 | |||
| f2397bf138 | |||
| ff5f4cbf7c | |||
| c53b2b104f | |||
| 01b653d6b0 | |||
| f05633f5b0 | |||
| ff1003e5f6 | |||
| d9fb57092c | |||
| c1cff3169f | |||
| f52de74b7b | |||
| 53d823e719 | |||
| 4511659a9e | |||
| 032c011b37 | |||
| c0997a5703 | |||
| 1d3d18fd66 | |||
| be997883c9 | |||
| 3f4c5f8076 | |||
| e1c99cd24c | |||
| 26b5b21238 | |||
| 25cb17c906 | |||
| 238f4d45df | |||
| bcea8ac822 | |||
| 87ae691e67 | |||
| 99f6481acc | |||
| 2c4bfd83e4 | |||
| 9e8aa39692 | |||
| b7f0b279eb | |||
| fa3353a3ca | |||
| 1187a66d2e | |||
| d360c34a30 | |||
| 287961375f | |||
| 98f883cb99 | |||
| f1840d467c | |||
| 5596cb52ef | |||
| 563e58a835 | |||
| eaee113416 | |||
| 170e037ad1 | |||
| 6f8f978975 | |||
| 034350f823 | |||
| a6b4758f5d | |||
| b4a2c990fb | |||
| ffd90dcf1e | |||
| 44df1befef | |||
| 32fc77bad4 | |||
| ead920ac09 |
@@ -186,7 +186,7 @@ jobs:
|
||||
echo "proceed=true" >> "$GITHUB_OUTPUT"
|
||||
echo "::notice::E2E green for this SHA — proceeding with promote"
|
||||
;;
|
||||
completed/failure|completed/cancelled|completed/timed_out)
|
||||
completed/failure|completed/timed_out)
|
||||
echo "proceed=false" >> "$GITHUB_OUTPUT"
|
||||
{
|
||||
echo "## ❌ Auto-promote aborted — E2E Staging SaaS failed"
|
||||
@@ -198,6 +198,27 @@ jobs:
|
||||
} >> "$GITHUB_STEP_SUMMARY"
|
||||
exit 1
|
||||
;;
|
||||
completed/cancelled)
|
||||
# cancelled ≠ failure. Per-SHA concurrency cancels older E2E
|
||||
# runs when a newer push lands (memory:
|
||||
# feedback_concurrency_group_per_sha) — the newer SHA will
|
||||
# have its own E2E + promote chain. Treat the same as
|
||||
# in_progress: defer without aborting, let the next E2E run
|
||||
# promote when it lands.
|
||||
#
|
||||
# Caught 2026-05-05 02:03 on sha 31f9a5e — auto-promote
|
||||
# blocked the whole chain because this case fell through to
|
||||
# exit 1 instead of clean defer.
|
||||
echo "proceed=false" >> "$GITHUB_OUTPUT"
|
||||
{
|
||||
echo "## ⏭ Auto-promote deferred — E2E Staging SaaS was cancelled"
|
||||
echo
|
||||
echo "E2E Staging SaaS for \`${SHA:0:7}\`: \`$RESULT\`"
|
||||
echo "Likely per-SHA concurrency (newer push superseded this E2E run)."
|
||||
echo "The newer SHA's E2E will fire its own promote when it lands."
|
||||
echo "If you need this specific SHA promoted, manually dispatch."
|
||||
} >> "$GITHUB_STEP_SUMMARY"
|
||||
;;
|
||||
in_progress/*|queued/*|requested/*|waiting/*|pending/*)
|
||||
echo "proceed=false" >> "$GITHUB_OUTPUT"
|
||||
{
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
name: branch-protection drift check
|
||||
|
||||
# Catches out-of-band edits to branch protection (UI clicks, manual gh
|
||||
# api PATCH from a one-off ops session) by comparing live state against
|
||||
# tools/branch-protection/apply.sh's desired state every day. Fails the
|
||||
# workflow when they drift; the failure is the signal.
|
||||
#
|
||||
# When it fails: re-run apply.sh to put the live state back to the
|
||||
# script's intent, OR update apply.sh to encode the new intent and
|
||||
# commit. Either way the script is the source of truth.
|
||||
|
||||
on:
|
||||
schedule:
|
||||
# 14:00 UTC daily. Off-hours for most teams; gives a fresh signal
|
||||
# at the start of every working day.
|
||||
- cron: '0 14 * * *'
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
branches: [staging, main]
|
||||
paths:
|
||||
- 'tools/branch-protection/**'
|
||||
- '.github/workflows/branch-protection-drift.yml'
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
drift:
|
||||
name: Branch protection drift
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
|
||||
# Token strategy by trigger:
|
||||
#
|
||||
# - schedule (daily canary): hard-fail when the admin token is
|
||||
# missing. This is the *only* trigger where silent soft-skip is
|
||||
# dangerous — a missing secret on the cron run means the drift
|
||||
# gate has effectively disappeared with no human in the loop to
|
||||
# notice. Per feedback_schedule_vs_dispatch_secrets_hardening.md
|
||||
# the rule is "schedule/automated triggers must hard-fail".
|
||||
#
|
||||
# - pull_request (touching tools/branch-protection/**): soft-skip
|
||||
# with a prominent warning. A PR cannot retroactively drift the
|
||||
# live state — drift happens *between* PRs (UI clicks, manual
|
||||
# gh api PATCH) and is the schedule's job to catch. The PR-time
|
||||
# gate would only catch typos in apply.sh, which the apply.sh
|
||||
# *_payload unit tests catch better. A human is reviewing the
|
||||
# PR and will see the warning in the workflow log.
|
||||
#
|
||||
# - workflow_dispatch (operator one-off): soft-skip with warning,
|
||||
# so an operator can run a diagnostic without configuring the
|
||||
# secret first.
|
||||
- name: Verify admin token present (hard-fail on schedule only)
|
||||
env:
|
||||
GH_TOKEN_FOR_ADMIN_API: ${{ secrets.GH_TOKEN_FOR_ADMIN_API }}
|
||||
run: |
|
||||
if [[ -n "$GH_TOKEN_FOR_ADMIN_API" ]]; then
|
||||
echo "GH_TOKEN_FOR_ADMIN_API present — drift_check will run with admin scope."
|
||||
exit 0
|
||||
fi
|
||||
if [[ "${{ github.event_name }}" == "schedule" ]]; then
|
||||
echo "::error::GH_TOKEN_FOR_ADMIN_API secret missing on the daily canary." >&2
|
||||
echo "" >&2
|
||||
echo "The schedule run is the SoT for branch-protection drift detection." >&2
|
||||
echo "Without admin scope it silently passes, hiding any out-of-band edits." >&2
|
||||
echo "Set GH_TOKEN_FOR_ADMIN_API at Settings → Secrets and variables → Actions." >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "::warning::GH_TOKEN_FOR_ADMIN_API secret missing — drift_check will be SKIPPED."
|
||||
echo "::warning::PR drift checks need repo-admin scope to read /branches/:b/protection."
|
||||
echo "::warning::This is non-fatal: the daily schedule run is the canonical drift gate."
|
||||
echo "SKIP_DRIFT_CHECK=1" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Run drift check
|
||||
if: env.SKIP_DRIFT_CHECK != '1'
|
||||
env:
|
||||
# Repo-admin scope, needed for /branches/:b/protection.
|
||||
GH_TOKEN: ${{ secrets.GH_TOKEN_FOR_ADMIN_API }}
|
||||
run: bash tools/branch-protection/drift_check.sh
|
||||
@@ -50,19 +50,35 @@ jobs:
|
||||
env:
|
||||
MOLECULE_CP_URL: https://staging-api.moleculesai.app
|
||||
MOLECULE_ADMIN_TOKEN: ${{ secrets.MOLECULE_STAGING_ADMIN_TOKEN }}
|
||||
# Without an LLM key the test_staging_full_saas.sh script provisions
|
||||
# the workspace with empty secrets, hermes derive-provider.sh resolves
|
||||
# `openai/gpt-4o` to PROVIDER=openrouter, no OPENROUTER_API_KEY is
|
||||
# found in env, and A2A returns "No LLM provider configured" at
|
||||
# request time (canary step 8/11). The full-lifecycle workflow
|
||||
# (e2e-staging-saas.yml) has carried this secret since launch — the
|
||||
# canary regressed when it was first split out and lost the env
|
||||
# block. Issue #1500 had ~30 consecutive failures before this was
|
||||
# spotted; do NOT remove without re-reading the script's secrets-
|
||||
# injection block.
|
||||
# MiniMax is the canary's PRIMARY LLM auth path post-2026-05-04.
|
||||
# Switched from hermes+OpenAI after #2578 (the staging OpenAI key
|
||||
# account went over quota and stayed dead for 36+ hours, taking
|
||||
# the canary red the entire time). claude-code template's
|
||||
# `minimax` provider routes ANTHROPIC_BASE_URL to
|
||||
# api.minimax.io/anthropic and reads MINIMAX_API_KEY at boot —
|
||||
# ~5-10x cheaper per token than gpt-4.1-mini AND on a separate
|
||||
# billing account, so OpenAI quota collapse no longer wedges the
|
||||
# canary. Mirrors the migration continuous-synth-e2e.yml made on
|
||||
# 2026-05-03 (#265) for the same reason. tests/e2e/test_staging_
|
||||
# full_saas.sh branches SECRETS_JSON on which key is present —
|
||||
# MiniMax wins when set.
|
||||
E2E_MINIMAX_API_KEY: ${{ secrets.MOLECULE_STAGING_MINIMAX_API_KEY }}
|
||||
# Direct-Anthropic alternative for operators who don't want to
|
||||
# set up a MiniMax account (priority below MiniMax — first
|
||||
# non-empty wins in test_staging_full_saas.sh's secrets-injection
|
||||
# block). See #2578 PR comment for the rationale.
|
||||
E2E_ANTHROPIC_API_KEY: ${{ secrets.MOLECULE_STAGING_ANTHROPIC_API_KEY }}
|
||||
# OpenAI fallback — kept wired so an operator-dispatched run with
|
||||
# E2E_RUNTIME=hermes overridden via workflow_dispatch can still
|
||||
# exercise the OpenAI path without re-editing the workflow.
|
||||
E2E_OPENAI_API_KEY: ${{ secrets.MOLECULE_STAGING_OPENAI_KEY }}
|
||||
E2E_MODE: canary
|
||||
E2E_RUNTIME: hermes
|
||||
E2E_RUNTIME: claude-code
|
||||
# Pin the canary to a specific MiniMax model rather than relying
|
||||
# on the per-runtime default (which could resolve to "sonnet" →
|
||||
# direct Anthropic and defeat the cost saving). M2.7-highspeed
|
||||
# is "Token Plan only" but cheap-per-token and fast.
|
||||
E2E_MODEL_SLUG: MiniMax-M2.7-highspeed
|
||||
E2E_RUN_ID: "canary-${{ github.run_id }}"
|
||||
|
||||
steps:
|
||||
@@ -75,13 +91,47 @@ jobs:
|
||||
exit 2
|
||||
fi
|
||||
|
||||
- name: Verify OpenAI key present
|
||||
- name: Verify LLM key present
|
||||
run: |
|
||||
if [ -z "$E2E_OPENAI_API_KEY" ]; then
|
||||
echo "::error::MOLECULE_STAGING_OPENAI_KEY secret not set — A2A will fail at request time with 'No LLM provider configured'"
|
||||
# Per-runtime key check — claude-code uses MiniMax; hermes /
|
||||
# langgraph (operator-dispatched only) use OpenAI. Hard-fail
|
||||
# rather than soft-skip per the lesson from synth E2E #2578:
|
||||
# an empty key silently falls through to the wrong
|
||||
# SECRETS_JSON branch and the canary fails 5 min later with
|
||||
# a confusing auth error instead of the clean "secret
|
||||
# missing" message at the top.
|
||||
case "${E2E_RUNTIME}" in
|
||||
claude-code)
|
||||
# Either MiniMax OR direct-Anthropic works — first
|
||||
# non-empty wins in the test script's secrets-injection
|
||||
# priority chain. Operators only need to set ONE of these
|
||||
# secrets; we don't force a choice between them.
|
||||
if [ -n "${E2E_MINIMAX_API_KEY:-}" ]; then
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY"
|
||||
required_secret_value="${E2E_MINIMAX_API_KEY}"
|
||||
elif [ -n "${E2E_ANTHROPIC_API_KEY:-}" ]; then
|
||||
required_secret_name="MOLECULE_STAGING_ANTHROPIC_API_KEY"
|
||||
required_secret_value="${E2E_ANTHROPIC_API_KEY}"
|
||||
else
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY or MOLECULE_STAGING_ANTHROPIC_API_KEY"
|
||||
required_secret_value=""
|
||||
fi
|
||||
;;
|
||||
langgraph|hermes)
|
||||
required_secret_name="MOLECULE_STAGING_OPENAI_KEY"
|
||||
required_secret_value="${E2E_OPENAI_API_KEY:-}"
|
||||
;;
|
||||
*)
|
||||
echo "::warning::Unknown E2E_RUNTIME='${E2E_RUNTIME}' — skipping LLM-key check"
|
||||
required_secret_name=""
|
||||
required_secret_value="present"
|
||||
;;
|
||||
esac
|
||||
if [ -n "$required_secret_name" ] && [ -z "$required_secret_value" ]; then
|
||||
echo "::error::${required_secret_name} secret not set for runtime=${E2E_RUNTIME} — A2A will fail at request time with 'No LLM provider configured'"
|
||||
exit 2
|
||||
fi
|
||||
echo "OpenAI key present ✓ (len=${#E2E_OPENAI_API_KEY})"
|
||||
echo "LLM key present ✓ (runtime=${E2E_RUNTIME}, key=${required_secret_name}, len=${#required_secret_value})"
|
||||
|
||||
- name: Canary run
|
||||
id: canary
|
||||
@@ -245,12 +295,16 @@ jobs:
|
||||
# See molecule-controlplane#420.
|
||||
leaks=()
|
||||
for slug in $orgs; do
|
||||
code=$(curl -sS -o /tmp/canary-cleanup.out -w "%{http_code}" \
|
||||
# Tempfile-routed -w + set +e/-e prevents curl-exit-code
|
||||
# pollution of the captured status (lint-curl-status-capture.yml).
|
||||
set +e
|
||||
curl -sS -o /tmp/canary-cleanup.out -w "%{http_code}" \
|
||||
-X DELETE "$MOLECULE_CP_URL/cp/admin/tenants/$slug" \
|
||||
-H "Authorization: Bearer $ADMIN_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"confirm\":\"$slug\"}" \
|
||||
|| echo "000")
|
||||
-d "{\"confirm\":\"$slug\"}" >/tmp/canary-cleanup.code
|
||||
set -e
|
||||
code=$(cat /tmp/canary-cleanup.code 2>/dev/null || echo "000")
|
||||
if [ "$code" = "200" ] || [ "$code" = "204" ]; then
|
||||
echo "[teardown] deleted $slug (HTTP $code)"
|
||||
else
|
||||
|
||||
@@ -272,6 +272,14 @@ jobs:
|
||||
find tests/e2e infra/scripts -type f -name '*.sh' -print0 \
|
||||
| xargs -0 shellcheck --severity=warning
|
||||
|
||||
- if: needs.changes.outputs.scripts == 'true'
|
||||
name: Lint cleanup-trap hygiene (RFC #2873)
|
||||
# Asserts every shell E2E test that calls `mktemp` also installs
|
||||
# an EXIT trap. Catches the /tmp-leak class — a missing trap
|
||||
# silently leaks scratch into CI runners (~10-100KB per run).
|
||||
# See tests/e2e/lint_cleanup_traps.sh for the rule + fix pattern.
|
||||
run: bash tests/e2e/lint_cleanup_traps.sh
|
||||
|
||||
- if: needs.changes.outputs.scripts == 'true'
|
||||
name: Run E2E bash unit tests (no live infra)
|
||||
# Pure-bash unit tests for E2E helper libs (lib/*.sh). These pin
|
||||
@@ -358,6 +366,72 @@ jobs:
|
||||
- if: needs.changes.outputs.python == 'true'
|
||||
run: python -m pytest --tb=short
|
||||
|
||||
- if: needs.changes.outputs.python == 'true'
|
||||
name: Per-file critical-path coverage (MCP / inbox / auth)
|
||||
# MCP-critical Python files have a per-file floor on top of the
|
||||
# 86% total floor in pytest.ini. Rationale (issue #2790, after
|
||||
# the PR #2766 → PR #2771 cycle): the total floor averages ~6000
|
||||
# lines, so a single MCP file could regress to ~50% with no
|
||||
# complaint as long as other modules compensate. These five
|
||||
# files handle multi-tenant routing + auth + inbox dispatch —
|
||||
# a coverage drop here is the same risk shape as a Go-side
|
||||
# workspace-server token/secrets file dropping below 10%.
|
||||
#
|
||||
# Floor 75% sits below current actuals (80-96%) so this gate is
|
||||
# strictly additive — no existing PR fails. Ratchet plan in
|
||||
# COVERAGE_FLOOR.md.
|
||||
run: |
|
||||
set -e
|
||||
PER_FILE_FLOOR=75
|
||||
CRITICAL_FILES=(
|
||||
"a2a_mcp_server.py"
|
||||
"mcp_cli.py"
|
||||
"a2a_tools.py"
|
||||
"inbox.py"
|
||||
"platform_auth.py"
|
||||
)
|
||||
|
||||
# pytest already wrote .coverage; emit a JSON view scoped to
|
||||
# the critical files so jq/python can read the per-file pct
|
||||
# without parsing tabular text. --include uses fnmatch, and
|
||||
# the leading "*" allows the file to live anywhere under the
|
||||
# workspace root (today they sit at workspace/<name>.py).
|
||||
INCLUDES=$(printf '*%s,' "${CRITICAL_FILES[@]}")
|
||||
INCLUDES="${INCLUDES%,}"
|
||||
python -m coverage json -o /tmp/critical-cov.json --include="$INCLUDES"
|
||||
|
||||
FAILED=0
|
||||
for f in "${CRITICAL_FILES[@]}"; do
|
||||
# Match by top-level path key (e.g. "a2a_tools.py", not
|
||||
# "builtin_tools/a2a_tools.py" — different file at 100%).
|
||||
# The keys in coverage.json are paths relative to the run
|
||||
# cwd (workspace/), so the critical-path entry sits at the
|
||||
# bare basename.
|
||||
pct=$(jq -r --arg f "$f" '.files | to_entries | map(select(.key == $f)) | .[0].value.summary.percent_covered // "MISSING"' /tmp/critical-cov.json)
|
||||
if [ "$pct" = "MISSING" ]; then
|
||||
echo "::error file=workspace/$f::No coverage data — file may have moved or test exclusion mis-set."
|
||||
FAILED=$((FAILED+1))
|
||||
continue
|
||||
fi
|
||||
echo "$f: ${pct}%"
|
||||
if awk "BEGIN{exit !($pct < $PER_FILE_FLOOR)}"; then
|
||||
echo "::error file=workspace/$f::${pct}% < ${PER_FILE_FLOOR}% per-file floor (MCP critical path). See COVERAGE_FLOOR.md."
|
||||
FAILED=$((FAILED+1))
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$FAILED" -gt 0 ]; then
|
||||
echo ""
|
||||
echo "$FAILED MCP critical-path file(s) below the ${PER_FILE_FLOOR}% per-file floor."
|
||||
echo "These paths handle multi-tenant routing, auth tokens, and inbox dispatch."
|
||||
echo "A coverage drop here is the same risk shape as Go-side tokens/secrets files"
|
||||
echo "dropping below 10% (see COVERAGE_FLOOR.md). Either:"
|
||||
echo " (a) add tests to raise coverage back above ${PER_FILE_FLOOR}%, or"
|
||||
echo " (b) if this is unavoidable historical debt, file an issue and propose"
|
||||
echo " adjusting the floor with rationale in COVERAGE_FLOOR.md."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# SDK + plugin validation moved to standalone repo:
|
||||
# github.com/Molecule-AI/molecule-sdk-python
|
||||
|
||||
|
||||
@@ -32,20 +32,30 @@ name: Continuous synthetic E2E (staging)
|
||||
|
||||
on:
|
||||
schedule:
|
||||
# Every 20 minutes, on :10 :30 :50. Two constraints:
|
||||
# Every 10 minutes, on :02 :12 :22 :32 :42 :52. Three constraints:
|
||||
# 1. Stay off the top-of-hour. GitHub Actions scheduler drops
|
||||
# :00 firings under high load (own docs:
|
||||
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule).
|
||||
# Empirical 2026-05-03: cron was '0,20,40 * * * *' but actual
|
||||
# firings landed at :08, :03, :01, :03 with :20 + :40 silently
|
||||
# dropped — only the :00-region run survived. Detection
|
||||
# latency degraded from claimed 20 min to actual ~60 min.
|
||||
# :10/:30/:50 sit far enough from :00 that GH-load skips
|
||||
# stop dropping us.
|
||||
# Prior history: cron was '0,20,40' (2026-05-02) — only :00
|
||||
# ever survived. Bumped to '10,30,50' (2026-05-03) on the
|
||||
# theory that further-from-:00 wins. Empirically 2026-05-04
|
||||
# that ALSO dropped to ~60 min effective cadence (only ~1
|
||||
# schedule fire per hour — see molecule-core#2726). Detection
|
||||
# latency was claimed 20 min, actual 60 min.
|
||||
# 2. Avoid colliding with the existing :15 sweep-cf-orphans
|
||||
# and :45 sweep-cf-tunnels — both hit the CF API and we
|
||||
# don't want to fight for rate-limit tokens.
|
||||
- cron: '10,30,50 * * * *'
|
||||
# 3. Avoid the :30 heavy slot (canary-staging /30, sweep-aws-
|
||||
# secrets, sweep-stale-e2e-orgs every :15) — multiple
|
||||
# overlapping cron registrations on the same minute is part
|
||||
# of what GH drops under load.
|
||||
# Solution: bump fires-per-hour 3 → 6 AND keep all slots in clean
|
||||
# lanes (1-3 min away from any other cron). Even with empirically-
|
||||
# observed ~67% GH drop ratio, 6 attempts/hour yields ~2 effective
|
||||
# fires = ~30 min cadence; closer to the 20-min target than the
|
||||
# current shape and provides a real degradation alarm if drops
|
||||
# get worse.
|
||||
- cron: '2,12,22,32,42,52 * * * *'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
runtime:
|
||||
@@ -83,7 +93,18 @@ jobs:
|
||||
synth:
|
||||
name: Synthetic E2E against staging
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 12
|
||||
# Bumped from 12 → 20 (2026-05-04). Tenant user-data install phase
|
||||
# (apt-get update + install docker.io/jq/awscli/caddy + snap install
|
||||
# ssm-agent) runs from raw Ubuntu on every boot — none of it is
|
||||
# pre-baked into the tenant AMI. Empirical fetch_secrets/ok timing
|
||||
# across today's canaries: 51s → 82s → 143s → 625s. apt-mirror tail
|
||||
# latency drives the boot-to-fetch_secrets phase from ~1min to >10min.
|
||||
# A 12min budget leaves only ~2min for the workspace (which needs
|
||||
# ~3.5min for claude-code cold boot) on slow-apt days, blowing the
|
||||
# budget. 20min absorbs the worst tenant tail so the workspace probe
|
||||
# gets the full ~7min it needs even on a slow apt day. Real fix:
|
||||
# pre-bake caddy + ssm-agent into the tenant AMI (controlplane#TBD).
|
||||
timeout-minutes: 20
|
||||
env:
|
||||
# claude-code default: cold-start ~5 min (comparable to langgraph),
|
||||
# but uses MiniMax-M2.7-highspeed via the template's third-party-
|
||||
@@ -119,6 +140,11 @@ jobs:
|
||||
# tests/e2e/test_staging_full_saas.sh branches SECRETS_JSON on
|
||||
# which key is present — MiniMax wins when set.
|
||||
E2E_MINIMAX_API_KEY: ${{ secrets.MOLECULE_STAGING_MINIMAX_API_KEY }}
|
||||
# Direct-Anthropic alternative for operators who don't want to
|
||||
# set up a MiniMax account (priority below MiniMax — first
|
||||
# non-empty wins in test_staging_full_saas.sh's secrets-injection
|
||||
# block). See #2578 PR comment for the rationale.
|
||||
E2E_ANTHROPIC_API_KEY: ${{ secrets.MOLECULE_STAGING_ANTHROPIC_API_KEY }}
|
||||
# OpenAI fallback — kept wired so operators can dispatch with
|
||||
# E2E_RUNTIME=langgraph or =hermes and still have a working
|
||||
# canary path. The script picks the right blob shape based on
|
||||
@@ -149,13 +175,21 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# LLM-key requirement is per-runtime: claude-code uses MiniMax
|
||||
# (MOLECULE_STAGING_MINIMAX_API_KEY), langgraph + hermes use
|
||||
# OpenAI (MOLECULE_STAGING_OPENAI_KEY).
|
||||
# LLM-key requirement is per-runtime: claude-code accepts
|
||||
# EITHER MiniMax OR direct-Anthropic (whichever is set first),
|
||||
# langgraph + hermes use OpenAI (MOLECULE_STAGING_OPENAI_KEY).
|
||||
case "${E2E_RUNTIME}" in
|
||||
claude-code)
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY"
|
||||
required_secret_value="${E2E_MINIMAX_API_KEY:-}"
|
||||
if [ -n "${E2E_MINIMAX_API_KEY:-}" ]; then
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY"
|
||||
required_secret_value="${E2E_MINIMAX_API_KEY}"
|
||||
elif [ -n "${E2E_ANTHROPIC_API_KEY:-}" ]; then
|
||||
required_secret_name="MOLECULE_STAGING_ANTHROPIC_API_KEY"
|
||||
required_secret_value="${E2E_ANTHROPIC_API_KEY}"
|
||||
else
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY or MOLECULE_STAGING_ANTHROPIC_API_KEY"
|
||||
required_secret_value=""
|
||||
fi
|
||||
;;
|
||||
langgraph|hermes)
|
||||
required_secret_name="MOLECULE_STAGING_OPENAI_KEY"
|
||||
|
||||
@@ -192,12 +192,16 @@ jobs:
|
||||
# cleanup miss shouldn't fail-flag the canvas test when the
|
||||
# actual smoke check passed; the sweeper is the safety net.
|
||||
# See molecule-controlplane#420.
|
||||
code=$(curl -sS -o /tmp/canvas-cleanup.out -w "%{http_code}" \
|
||||
# Tempfile-routed -w + set +e/-e prevents curl-exit-code
|
||||
# pollution of the captured status (lint-curl-status-capture.yml).
|
||||
set +e
|
||||
curl -sS -o /tmp/canvas-cleanup.out -w "%{http_code}" \
|
||||
-X DELETE "$MOLECULE_CP_URL/cp/admin/tenants/$slug" \
|
||||
-H "Authorization: Bearer $ADMIN_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"confirm\":\"$slug\"}" \
|
||||
|| echo "000")
|
||||
-d "{\"confirm\":\"$slug\"}" >/tmp/canvas-cleanup.code
|
||||
set -e
|
||||
code=$(cat /tmp/canvas-cleanup.code 2>/dev/null || echo "000")
|
||||
if [ "$code" = "200" ] || [ "$code" = "204" ]; then
|
||||
echo "[teardown] deleted $slug (HTTP $code)"
|
||||
else
|
||||
|
||||
@@ -159,12 +159,16 @@ jobs:
|
||||
# leaked. Sweeper catches the rest within ~45 min.
|
||||
leaks=()
|
||||
for slug in $orgs; do
|
||||
code=$(curl -sS -o /tmp/external-cleanup.out -w "%{http_code}" \
|
||||
# Tempfile-routed -w + set +e/-e prevents curl-exit-code
|
||||
# pollution of the captured status (lint-curl-status-capture.yml).
|
||||
set +e
|
||||
curl -sS -o /tmp/external-cleanup.out -w "%{http_code}" \
|
||||
-X DELETE "$MOLECULE_CP_URL/cp/admin/tenants/$slug" \
|
||||
-H "Authorization: Bearer $ADMIN_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"confirm\":\"$slug\"}" \
|
||||
|| echo "000")
|
||||
-d "{\"confirm\":\"$slug\"}" >/tmp/external-cleanup.code
|
||||
set -e
|
||||
code=$(cat /tmp/external-cleanup.code 2>/dev/null || echo "000")
|
||||
if [ "$code" = "200" ] || [ "$code" = "204" ]; then
|
||||
echo "[teardown] deleted $slug (HTTP $code)"
|
||||
else
|
||||
|
||||
@@ -48,9 +48,9 @@ on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
runtime:
|
||||
description: "Runtime to test (hermes | claude-code | langgraph)"
|
||||
description: "Runtime to test (claude-code [default, MiniMax] | hermes [OpenAI] | langgraph [OpenAI])"
|
||||
required: false
|
||||
default: "hermes"
|
||||
default: "claude-code"
|
||||
keep_org:
|
||||
description: "Skip teardown for debugging (only use via manual dispatch!)"
|
||||
required: false
|
||||
@@ -83,11 +83,32 @@ jobs:
|
||||
# retrieval + teardown. Configure in
|
||||
# Settings → Secrets and variables → Actions → Repository secrets.
|
||||
MOLECULE_ADMIN_TOKEN: ${{ secrets.MOLECULE_STAGING_ADMIN_TOKEN }}
|
||||
# OpenAI key for workspace LLM calls (section 8 A2A). Without it,
|
||||
# Hermes runtime crashes at boot with "No provider API key found".
|
||||
# Configure at Settings → Secrets → Actions → MOLECULE_STAGING_OPENAI_KEY.
|
||||
# MiniMax is the PRIMARY LLM auth path post-2026-05-04. Switched
|
||||
# from hermes+OpenAI default after #2578 (the staging OpenAI key
|
||||
# account went over quota and stayed dead for 36+ hours, taking
|
||||
# the full-lifecycle E2E red on every provisioning-critical push).
|
||||
# claude-code template's `minimax` provider routes
|
||||
# ANTHROPIC_BASE_URL to api.minimax.io/anthropic and reads
|
||||
# MINIMAX_API_KEY at boot — separate billing account so an
|
||||
# OpenAI quota collapse no longer wedges the gate. Mirrors the
|
||||
# canary-staging.yml + continuous-synth-e2e.yml migrations.
|
||||
E2E_MINIMAX_API_KEY: ${{ secrets.MOLECULE_STAGING_MINIMAX_API_KEY }}
|
||||
# Direct-Anthropic alternative for operators who don't want to
|
||||
# set up a MiniMax account (priority below MiniMax — first
|
||||
# non-empty wins in test_staging_full_saas.sh's secrets-injection
|
||||
# block). See #2578 PR comment for the rationale.
|
||||
E2E_ANTHROPIC_API_KEY: ${{ secrets.MOLECULE_STAGING_ANTHROPIC_API_KEY }}
|
||||
# OpenAI fallback — kept wired so an operator-dispatched run with
|
||||
# E2E_RUNTIME=hermes or =langgraph via workflow_dispatch can still
|
||||
# exercise the OpenAI path.
|
||||
E2E_OPENAI_API_KEY: ${{ secrets.MOLECULE_STAGING_OPENAI_KEY }}
|
||||
E2E_RUNTIME: ${{ github.event.inputs.runtime || 'hermes' }}
|
||||
E2E_RUNTIME: ${{ github.event.inputs.runtime || 'claude-code' }}
|
||||
# Pin the model when running on the default claude-code path —
|
||||
# the per-runtime default ("sonnet") routes to direct Anthropic
|
||||
# and defeats the cost saving. Operators can override via the
|
||||
# workflow_dispatch flow (no input wired here yet — runtime
|
||||
# override is enough for ad-hoc).
|
||||
E2E_MODEL_SLUG: ${{ github.event.inputs.runtime == 'hermes' && 'openai/gpt-4o' || github.event.inputs.runtime == 'langgraph' && 'openai:gpt-4o' || 'MiniMax-M2.7-highspeed' }}
|
||||
E2E_RUN_ID: "${{ github.run_id }}-${{ github.run_attempt }}"
|
||||
E2E_KEEP_ORG: ${{ github.event.inputs.keep_org && '1' || '0' }}
|
||||
|
||||
@@ -102,13 +123,45 @@ jobs:
|
||||
fi
|
||||
echo "Admin token present ✓"
|
||||
|
||||
- name: Verify OpenAI key present
|
||||
- name: Verify LLM key present
|
||||
run: |
|
||||
if [ -z "$E2E_OPENAI_API_KEY" ]; then
|
||||
echo "::error::MOLECULE_STAGING_OPENAI_KEY secret not set — workspaces will fail at boot with 'No provider API key found'"
|
||||
# Per-runtime key check — claude-code uses MiniMax; hermes /
|
||||
# langgraph (operator-dispatched only) use OpenAI. Hard-fail
|
||||
# rather than soft-skip per #2578's lesson — empty key
|
||||
# silently falls through to the wrong SECRETS_JSON branch and
|
||||
# produces a confusing auth error 5 min later instead of the
|
||||
# clean "secret missing" message at the top.
|
||||
case "${E2E_RUNTIME}" in
|
||||
claude-code)
|
||||
# Either MiniMax OR direct-Anthropic works — first
|
||||
# non-empty wins in the test script's secrets-injection
|
||||
# priority chain.
|
||||
if [ -n "${E2E_MINIMAX_API_KEY:-}" ]; then
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY"
|
||||
required_secret_value="${E2E_MINIMAX_API_KEY}"
|
||||
elif [ -n "${E2E_ANTHROPIC_API_KEY:-}" ]; then
|
||||
required_secret_name="MOLECULE_STAGING_ANTHROPIC_API_KEY"
|
||||
required_secret_value="${E2E_ANTHROPIC_API_KEY}"
|
||||
else
|
||||
required_secret_name="MOLECULE_STAGING_MINIMAX_API_KEY or MOLECULE_STAGING_ANTHROPIC_API_KEY"
|
||||
required_secret_value=""
|
||||
fi
|
||||
;;
|
||||
langgraph|hermes)
|
||||
required_secret_name="MOLECULE_STAGING_OPENAI_KEY"
|
||||
required_secret_value="${E2E_OPENAI_API_KEY:-}"
|
||||
;;
|
||||
*)
|
||||
echo "::warning::Unknown E2E_RUNTIME='${E2E_RUNTIME}' — skipping LLM-key check"
|
||||
required_secret_name=""
|
||||
required_secret_value="present"
|
||||
;;
|
||||
esac
|
||||
if [ -n "$required_secret_name" ] && [ -z "$required_secret_value" ]; then
|
||||
echo "::error::${required_secret_name} secret not set for runtime=${E2E_RUNTIME} — workspaces will fail at boot with 'No provider API key found'"
|
||||
exit 2
|
||||
fi
|
||||
echo "OpenAI key present ✓ (len=${#E2E_OPENAI_API_KEY})"
|
||||
echo "LLM key present ✓ (runtime=${E2E_RUNTIME}, key=${required_secret_name}, len=${#required_secret_value})"
|
||||
|
||||
- name: CP staging health preflight
|
||||
run: |
|
||||
@@ -171,12 +224,16 @@ jobs:
|
||||
leaks=()
|
||||
for slug in $orgs; do
|
||||
echo "Safety-net teardown: $slug"
|
||||
code=$(curl -sS -o /tmp/saas-cleanup.out -w "%{http_code}" \
|
||||
# Tempfile-routed -w + set +e/-e prevents curl-exit-code
|
||||
# pollution of the captured status (lint-curl-status-capture.yml).
|
||||
set +e
|
||||
curl -sS -o /tmp/saas-cleanup.out -w "%{http_code}" \
|
||||
-X DELETE "$MOLECULE_CP_URL/cp/admin/tenants/$slug" \
|
||||
-H "Authorization: Bearer $ADMIN_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"confirm\":\"$slug\"}" \
|
||||
|| echo "000")
|
||||
-d "{\"confirm\":\"$slug\"}" >/tmp/saas-cleanup.code
|
||||
set -e
|
||||
code=$(cat /tmp/saas-cleanup.code 2>/dev/null || echo "000")
|
||||
if [ "$code" = "200" ] || [ "$code" = "204" ]; then
|
||||
echo "[teardown] deleted $slug (HTTP $code)"
|
||||
else
|
||||
|
||||
@@ -148,12 +148,16 @@ jobs:
|
||||
# safety net within ~45 min.
|
||||
leaks=()
|
||||
for slug in $orgs; do
|
||||
code=$(curl -sS -o /tmp/sanity-cleanup.out -w "%{http_code}" \
|
||||
# Tempfile-routed -w + set +e/-e prevents curl-exit-code
|
||||
# pollution of the captured status (lint-curl-status-capture.yml).
|
||||
set +e
|
||||
curl -sS -o /tmp/sanity-cleanup.out -w "%{http_code}" \
|
||||
-X DELETE "$MOLECULE_CP_URL/cp/admin/tenants/$slug" \
|
||||
-H "Authorization: Bearer $ADMIN_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"confirm\":\"$slug\"}" \
|
||||
|| echo "000")
|
||||
-d "{\"confirm\":\"$slug\"}" >/tmp/sanity-cleanup.code
|
||||
set -e
|
||||
code=$(cat /tmp/sanity-cleanup.code 2>/dev/null || echo "000")
|
||||
if [ "$code" = "200" ] || [ "$code" = "204" ]; then
|
||||
echo "[teardown] deleted $slug (HTTP $code)"
|
||||
else
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
name: Handlers Postgres Integration
|
||||
|
||||
# Real-Postgres integration tests for workspace-server/internal/handlers/.
|
||||
# Triggered on every PR/push that touches the handlers package.
|
||||
#
|
||||
# Why this workflow exists
|
||||
# ------------------------
|
||||
# Strict-sqlmock unit tests pin which SQL statements fire — they're fast
|
||||
# and let us iterate without a DB. But sqlmock CANNOT detect bugs that
|
||||
# depend on the row state AFTER the SQL runs. The result_preview-lost
|
||||
# bug shipped to staging in PR #2854 because every unit test was
|
||||
# satisfied with "an UPDATE statement fired" — none verified the row's
|
||||
# preview field actually landed. The local-postgres E2E that retrofit
|
||||
# self-review caught it took 2 minutes to set up and would have caught
|
||||
# the bug at PR-time.
|
||||
#
|
||||
# This job spins a Postgres service container, applies the migration,
|
||||
# and runs `go test -tags=integration` against a live DB. Required
|
||||
# check on staging branch protection — backend handler PRs cannot
|
||||
# merge without a real-DB regression gate.
|
||||
#
|
||||
# Cost: ~30s job (postgres pull from GH cache + go build + 4 tests).
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, staging]
|
||||
pull_request:
|
||||
branches: [main, staging]
|
||||
merge_group:
|
||||
types: [checks_requested]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: handlers-pg-integ-${{ github.event.pull_request.head.sha || github.sha }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
detect-changes:
|
||||
name: detect-changes
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
handlers: ${{ steps.filter.outputs.handlers }}
|
||||
steps:
|
||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
- uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
|
||||
id: filter
|
||||
with:
|
||||
filters: |
|
||||
handlers:
|
||||
- 'workspace-server/internal/handlers/**'
|
||||
- 'workspace-server/internal/wsauth/**'
|
||||
- 'workspace-server/migrations/**'
|
||||
- '.github/workflows/handlers-postgres-integration.yml'
|
||||
|
||||
# Single-job-with-per-step-if pattern: always runs to satisfy the
|
||||
# required-check name on branch protection; real work gates on the
|
||||
# paths filter. See ci.yml's Platform (Go) for the same shape.
|
||||
integration:
|
||||
name: Handlers Postgres Integration
|
||||
needs: detect-changes
|
||||
runs-on: ubuntu-latest
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:15-alpine
|
||||
env:
|
||||
POSTGRES_PASSWORD: test
|
||||
POSTGRES_DB: molecule
|
||||
ports:
|
||||
- 5432:5432
|
||||
# GHA spins this with --health-cmd built in for postgres images.
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 5s
|
||||
--health-timeout 5s
|
||||
--health-retries 10
|
||||
defaults:
|
||||
run:
|
||||
working-directory: workspace-server
|
||||
steps:
|
||||
- if: needs.detect-changes.outputs.handlers != 'true'
|
||||
working-directory: .
|
||||
run: echo "No handlers/migrations changes — skipping; this job always runs to satisfy the required-check name."
|
||||
|
||||
- if: needs.detect-changes.outputs.handlers == 'true'
|
||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
|
||||
- if: needs.detect-changes.outputs.handlers == 'true'
|
||||
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
|
||||
with:
|
||||
go-version: 'stable'
|
||||
|
||||
- if: needs.detect-changes.outputs.handlers == 'true'
|
||||
name: Apply migrations to Postgres service
|
||||
env:
|
||||
PGPASSWORD: test
|
||||
run: |
|
||||
# Wait for postgres to actually accept connections (the
|
||||
# GHA --health-cmd is best-effort but psql can still race).
|
||||
for i in {1..15}; do
|
||||
if pg_isready -h localhost -p 5432 -U postgres -q; then break; fi
|
||||
echo "waiting for postgres..."; sleep 2
|
||||
done
|
||||
|
||||
# Apply every .up.sql in lexicographic order with
|
||||
# ON_ERROR_STOP=0 — failing migrations are SKIPPED rather than
|
||||
# blocking the suite. This handles the current schema state
|
||||
# where a few historical migrations (e.g. 017_memories_fts_*)
|
||||
# depend on tables that were later renamed/dropped and so
|
||||
# cannot replay from scratch. The migrations that DO succeed
|
||||
# land their tables, which is sufficient for the integration
|
||||
# tests in handlers/.
|
||||
#
|
||||
# Why not maintain a curated allowlist: every new migration
|
||||
# touching a handlers/-tested table would have to update this
|
||||
# workflow. With apply-all-or-skip, a future migration that
|
||||
# adds a column to delegations runs automatically (its base
|
||||
# table 049_delegations.up.sql already succeeded above it in
|
||||
# the order). Operators only need to revisit this if the
|
||||
# migration chain becomes legitimately replayable end-to-end.
|
||||
#
|
||||
# Per-migration result is logged so a failed migration that
|
||||
# SHOULD have been replayable surfaces in the CI log instead
|
||||
# of silently failing.
|
||||
set +e
|
||||
for migration in migrations/*.up.sql; do
|
||||
if psql -h localhost -U postgres -d molecule -v ON_ERROR_STOP=1 \
|
||||
-f "$migration" >/dev/null 2>&1; then
|
||||
echo "✓ $(basename "$migration")"
|
||||
else
|
||||
echo "⊘ $(basename "$migration") (skipped — see comment in workflow)"
|
||||
fi
|
||||
done
|
||||
set -e
|
||||
|
||||
# Sanity: the delegations table MUST exist for the integration
|
||||
# tests to be meaningful. Hard-fail if 049 didn't land — that
|
||||
# would be a real regression we want loud.
|
||||
if ! psql -h localhost -U postgres -d molecule -tA \
|
||||
-c "SELECT 1 FROM information_schema.tables WHERE table_name = 'delegations'" \
|
||||
| grep -q 1; then
|
||||
echo "::error::delegations table missing after migration replay — handler integration tests would be meaningless"
|
||||
exit 1
|
||||
fi
|
||||
echo "✓ delegations table present"
|
||||
|
||||
- if: needs.detect-changes.outputs.handlers == 'true'
|
||||
name: Run integration tests
|
||||
env:
|
||||
INTEGRATION_DB_URL: postgres://postgres:test@localhost:5432/molecule?sslmode=disable
|
||||
run: |
|
||||
go test -tags=integration -timeout 5m -v ./internal/handlers/ -run "^TestIntegration_"
|
||||
|
||||
- if: needs.detect-changes.outputs.handlers == 'true' && failure()
|
||||
name: Diagnostic dump on failure
|
||||
env:
|
||||
PGPASSWORD: test
|
||||
run: |
|
||||
echo "::group::delegations table state"
|
||||
psql -h localhost -U postgres -d molecule -c "SELECT * FROM delegations LIMIT 50;" || true
|
||||
echo "::endgroup::"
|
||||
@@ -0,0 +1,94 @@
|
||||
name: Lint curl status-code capture
|
||||
|
||||
# Pins the workflow-bash anti-pattern that produced "HTTP 000000" on the
|
||||
# 2026-05-04 redeploy-tenants-on-main run for sha 2b862f6:
|
||||
#
|
||||
# HTTP_CODE=$(curl ... -w '%{http_code}' ... || echo "000")
|
||||
#
|
||||
# When curl exits non-zero (connection reset → 56, --fail-with-body 4xx/5xx
|
||||
# → 22), the `-w '%{http_code}'` already wrote a status to stdout — usually
|
||||
# "000" for connection failures or the actual code for HTTP errors. The
|
||||
# `|| echo "000"` then fires AND appends ANOTHER "000" to the captured
|
||||
# stdout, producing values like "000000" or "409000" that fail string
|
||||
# comparisons against "200" while looking superficially right.
|
||||
#
|
||||
# Same class of bug the synth-E2E §7c gate hit twice (PRs #2779/#2783 +
|
||||
# #2797). Memory: feedback_curl_status_capture_pollution.md.
|
||||
#
|
||||
# Fix shape (route -w into a tempfile so curl's exit code can't pollute):
|
||||
#
|
||||
# set +e
|
||||
# curl ... -w '%{http_code}' >code.txt 2>/dev/null
|
||||
# set -e
|
||||
# HTTP_CODE=$(cat code.txt 2>/dev/null)
|
||||
# [ -z "$HTTP_CODE" ] && HTTP_CODE="000"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths: ['.github/workflows/**']
|
||||
push:
|
||||
branches: [main, staging]
|
||||
paths: ['.github/workflows/**']
|
||||
merge_group:
|
||||
types: [checks_requested]
|
||||
|
||||
jobs:
|
||||
scan:
|
||||
name: Scan workflows for curl status-capture pollution
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
- name: Find curl ... -w '%{http_code}' ... || echo "000" subshells
|
||||
run: |
|
||||
set -uo pipefail
|
||||
# Multi-line aware: look for `$(curl ... -w '%{http_code}' ... || echo "000")`
|
||||
# subshell where the entire command-substitution wraps a curl that
|
||||
# ends with `|| echo "000"`. Must distinguish from the SAFE shape
|
||||
# `$(cat tempfile 2>/dev/null || echo "000")` — `cat` with a missing
|
||||
# tempfile produces empty stdout, no pollution.
|
||||
python3 <<'PY'
|
||||
import os, re, sys, glob
|
||||
|
||||
BAD_FILES = []
|
||||
|
||||
# Match the buggy substitution across newlines: $(curl ... -w '%{http_code}' ... || echo "000")
|
||||
# The `\\n` is the bash line-continuation that lets curl flags span lines.
|
||||
# We collapse continuation lines first, then look for the single-line bad pattern.
|
||||
PATTERN = re.compile(
|
||||
r'\$\(\s*curl\b[^)]*-w\s*[\'"]%\{http_code\}[\'"][^)]*\|\|\s*echo\s+"000"\s*\)',
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
# Self-skip: this lint workflow contains the literal anti-pattern in
|
||||
# its own docstring — that's intentional, not a bug.
|
||||
SELF = ".github/workflows/lint-curl-status-capture.yml"
|
||||
|
||||
for f in sorted(glob.glob(".github/workflows/*.yml")):
|
||||
if f == SELF:
|
||||
continue
|
||||
with open(f) as fh:
|
||||
content = fh.read()
|
||||
# Collapse bash line-continuations (\\\n + leading whitespace)
|
||||
# into a single logical line so the regex can see the full
|
||||
# curl invocation as one chunk.
|
||||
flat = re.sub(r'\\\s*\n\s*', ' ', content)
|
||||
for m in PATTERN.finditer(flat):
|
||||
BAD_FILES.append((f, m.group(0)[:120]))
|
||||
|
||||
if not BAD_FILES:
|
||||
print("✓ No curl-status-capture pollution patterns detected")
|
||||
sys.exit(0)
|
||||
|
||||
print(f"::error::Found {len(BAD_FILES)} curl-status-capture pollution site(s):")
|
||||
for f, snippet in BAD_FILES:
|
||||
print(f"::error file={f}::Curl status-capture pollution: '|| echo \"000\"' inside a $(curl ... -w '%{{http_code}}' ...) subshell. On non-2xx or connection failure, curl's -w writes a status, then exits non-zero, then the || echo appends another '000' — producing 'HTTP 000000' or '409000' that fails comparisons silently. Fix: route -w into a tempfile so the exit code can't pollute stdout. See memory feedback_curl_status_capture_pollution.md.")
|
||||
print(f" matched: {snippet}…")
|
||||
print()
|
||||
print("Fix template:")
|
||||
print(' set +e')
|
||||
print(' curl ... -w \'%{http_code}\' >code.txt 2>/dev/null')
|
||||
print(' set -e')
|
||||
print(' HTTP_CODE=$(cat code.txt 2>/dev/null)')
|
||||
print(' [ -z "$HTTP_CODE" ] && HTTP_CODE="000"')
|
||||
sys.exit(1)
|
||||
PY
|
||||
@@ -184,12 +184,29 @@ jobs:
|
||||
echo " body: $BODY"
|
||||
|
||||
HTTP_RESPONSE=$(mktemp)
|
||||
HTTP_CODE=$(curl -sS -o "$HTTP_RESPONSE" -w '%{http_code}' \
|
||||
HTTP_CODE_FILE=$(mktemp)
|
||||
# Route -w into its own tempfile so curl's exit code (e.g. 56
|
||||
# on connection-reset, 22 on --fail-with-body 4xx/5xx) can't
|
||||
# pollute the captured stdout. The previous inline-substitution
|
||||
# shape produced "000000" on connection reset (curl wrote
|
||||
# "000" via -w, then the inline echo-fallback appended another
|
||||
# "000") — caught on the 2026-05-04 redeploy of sha 2b862f6.
|
||||
# set +e/-e keeps the non-zero curl exit from tripping the
|
||||
# outer pipeline. See lint-curl-status-capture.yml for the
|
||||
# CI gate that pins this fix shape.
|
||||
set +e
|
||||
curl -sS -o "$HTTP_RESPONSE" -w '%{http_code}' \
|
||||
-m 1200 \
|
||||
-H "Authorization: Bearer $CP_ADMIN_API_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-X POST "$CP_URL/cp/admin/tenants/redeploy-fleet" \
|
||||
-d "$BODY" || echo "000")
|
||||
-d "$BODY" >"$HTTP_CODE_FILE"
|
||||
set -e
|
||||
# Stderr from curl (e.g. dial errors with -sS) goes to the runner
|
||||
# log so operators can see WHY a connection failed. Stdout is
|
||||
# captured to $HTTP_CODE_FILE because that's where -w writes.
|
||||
HTTP_CODE=$(cat "$HTTP_CODE_FILE" 2>/dev/null || echo "000")
|
||||
[ -z "$HTTP_CODE" ] && HTTP_CODE="000"
|
||||
|
||||
echo "HTTP $HTTP_CODE"
|
||||
cat "$HTTP_RESPONSE" | jq . || cat "$HTTP_RESPONSE"
|
||||
|
||||
@@ -146,12 +146,26 @@ jobs:
|
||||
echo " body: $BODY"
|
||||
|
||||
HTTP_RESPONSE=$(mktemp)
|
||||
HTTP_CODE=$(curl -sS -o "$HTTP_RESPONSE" -w '%{http_code}' \
|
||||
HTTP_CODE_FILE=$(mktemp)
|
||||
# Route -w into its own tempfile so curl's exit code (e.g. 56
|
||||
# on connection-reset) can't pollute the captured stdout. The
|
||||
# previous inline-substitution shape produced "000000" on
|
||||
# connection reset — caught on main variant 2026-05-04
|
||||
# redeploying sha 2b862f6. Same fix shape as the synth-E2E
|
||||
# §9c gate (PR #2797). See lint-curl-status-capture.yml for
|
||||
# the CI gate that pins this fix shape.
|
||||
set +e
|
||||
curl -sS -o "$HTTP_RESPONSE" -w '%{http_code}' \
|
||||
-m 1200 \
|
||||
-H "Authorization: Bearer $CP_STAGING_ADMIN_API_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-X POST "$CP_URL/cp/admin/tenants/redeploy-fleet" \
|
||||
-d "$BODY" || echo "000")
|
||||
-d "$BODY" >"$HTTP_CODE_FILE"
|
||||
set -e
|
||||
# Stderr from curl (-sS shows dial errors etc.) goes to the
|
||||
# runner log so operators can see WHY a connection failed.
|
||||
HTTP_CODE=$(cat "$HTTP_CODE_FILE" 2>/dev/null || echo "000")
|
||||
[ -z "$HTTP_CODE" ] && HTTP_CODE="000"
|
||||
|
||||
echo "HTTP $HTTP_CODE"
|
||||
cat "$HTTP_RESPONSE" | jq . || cat "$HTTP_RESPONSE"
|
||||
|
||||
@@ -43,7 +43,20 @@ on:
|
||||
types: [checks_requested]
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.head.sha || github.sha }}
|
||||
# Include event_name so a PR sync (event=pull_request) and the
|
||||
# subsequent staging push (event=push) on the SAME merge SHA don't
|
||||
# collide in one group. Without event_name, both runs hashed to
|
||||
# the same key and cancel-in-progress=true cancelled whichever
|
||||
# arrived second — usually the push run, which staging branch-
|
||||
# protection then sees as a CANCELLED required check and refuses
|
||||
# to mark merged. Caught 2026-05-05 across PR #2869's runs (run
|
||||
# ids 25371863455 / 25371811486 / 25371078157 / 25370403142 — every
|
||||
# staging push run cancelled, every matching PR run green).
|
||||
#
|
||||
# Per memory `feedback_concurrency_group_per_sha.md` — same drift
|
||||
# class that broke auto-promote-staging on 2026-04-28. Pin invariant:
|
||||
# event_name + sha is the minimum unique key for these workflows.
|
||||
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.head.sha || github.sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -159,12 +159,18 @@ jobs:
|
||||
# The DELETE handler requires {"confirm": "<slug>"} matching
|
||||
# the URL slug — fat-finger guard. Idempotent: re-issuing
|
||||
# picks up via org_purges.last_step.
|
||||
http_code=$(curl -sS -o /tmp/del_resp -w "%{http_code}" \
|
||||
# Tempfile-routed -w + set +e/-e prevents curl-exit-code
|
||||
# pollution of the captured status (lint-curl-status-capture.yml).
|
||||
set +e
|
||||
curl -sS -o /tmp/del_resp -w "%{http_code}" \
|
||||
--max-time 60 \
|
||||
-X DELETE "$MOLECULE_CP_URL/cp/admin/tenants/$slug" \
|
||||
-H "Authorization: Bearer $ADMIN_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"confirm\":\"$slug\"}" || echo "000")
|
||||
-d "{\"confirm\":\"$slug\"}" >/tmp/del_code
|
||||
set -e
|
||||
# Stderr from curl (-sS shows dial errors etc.) goes to runner log.
|
||||
http_code=$(cat /tmp/del_code 2>/dev/null || echo "000")
|
||||
if [ "$http_code" = "200" ] || [ "$http_code" = "204" ]; then
|
||||
deleted=$((deleted+1))
|
||||
echo " deleted: $slug"
|
||||
|
||||
+50
-2
@@ -1,7 +1,7 @@
|
||||
# Coverage Floor
|
||||
|
||||
CI enforces three coverage gates on `workspace-server` (Go). All defined in
|
||||
`.github/workflows/ci.yml` → `platform-build` job.
|
||||
CI enforces coverage gates on two surfaces — `workspace-server` (Go) and
|
||||
`workspace/` (Python). All defined in `.github/workflows/ci.yml`.
|
||||
|
||||
## Current floors (2026-04-23)
|
||||
|
||||
@@ -76,3 +76,51 @@ This gate makes "no untested critical paths merged" a mechanical property of
|
||||
the CI, not a behavioural property of QA agents or individual reviewers —
|
||||
which is the only way to make it survive fleet outages, agent rotations, or
|
||||
QA process changes.
|
||||
|
||||
## Python (workspace/) — added 2026-05-04 from #2790
|
||||
|
||||
The Python side has its own gates in the `python-lint` job:
|
||||
|
||||
| Gate | Threshold | Where |
|
||||
|---|---|---|
|
||||
| **Total floor** | `86%` | `workspace/pytest.ini` `--cov-fail-under=86` (issue #1817) |
|
||||
| **Critical-path per-file floor** | `75%` | Inline shell step after the pytest run |
|
||||
|
||||
### Critical-path Python files
|
||||
|
||||
These handle multi-tenant routing, auth tokens, and inbox dispatch. A
|
||||
coverage drop here is the same risk shape as a Go-side `tokens*` /
|
||||
`secrets*` file regressing below 10%.
|
||||
|
||||
- `workspace/a2a_mcp_server.py` — MCP dispatcher (PR #2766 / #2771)
|
||||
- `workspace/mcp_cli.py` — molecule-mcp standalone CLI entry
|
||||
- `workspace/a2a_tools.py` — workspace-scoped tool implementations
|
||||
- `workspace/inbox.py` — multi-workspace inbox + per-workspace cursors
|
||||
- `workspace/platform_auth.py` — per-workspace token resolver
|
||||
|
||||
### Why 75% (vs 86% total)
|
||||
|
||||
The total floor averages ~6000 lines across `workspace/`. A single MCP
|
||||
file could drop to ~50% with no CI complaint as long as other modules
|
||||
compensate. The per-file floor closes that distribution gap. 75% sits
|
||||
below current actuals (80–96% as of 2026-05-04) — strictly additive,
|
||||
no existing PR fails.
|
||||
|
||||
### Python ratchet plan
|
||||
|
||||
| Date | Total | Per-file critical | Notes |
|
||||
|---|---|---|---|
|
||||
| 2026-05-04 | 86% | 75% | Initial gate (this file). |
|
||||
| 2026-06-04 | 86% | 80% | First ratchet — at-floor files must catch up. |
|
||||
| 2026-07-04 | 88% | 85% | |
|
||||
| 2026-08-04 | 90% | 90% | Target steady-state. |
|
||||
|
||||
### Why this Python gate exists
|
||||
|
||||
Issue #2790, after the PR #2766 → PR #2771 cycle. PR #2766 added
|
||||
multi-workspace routing through `a2a_tools.py` + `a2a_mcp_server.py`,
|
||||
shipped to main with green CI, but the dispatcher silently dropped a
|
||||
load-bearing kwarg for 4 of 9 tools — caught only by post-merge code
|
||||
review. The structural drift gate (`test_dispatcher_schema_drift.py`,
|
||||
PR #2791) catches the schema↔dispatcher mismatch class; this floor
|
||||
catches the broader "MCP-critical file regressed" class.
|
||||
|
||||
@@ -138,14 +138,37 @@ export function A2ATopologyOverlay() {
|
||||
// Stable Zustand action reference — safe to call inside effects
|
||||
const setA2AEdges = useCanvasStore((s) => s.setA2AEdges);
|
||||
|
||||
// Read the nodes array as a primitive ref; derive visible IDs outside the selector
|
||||
const nodes = useCanvasStore((s) => s.nodes);
|
||||
// Subscribe to a STABLE STRING KEY of visible workspace IDs, not the
|
||||
// nodes array itself. Zustand returns a new array reference on every
|
||||
// store update (status flips, position drags, peer-discovery writes,
|
||||
// workspace-tab opens, etc.) — even when the set of visible IDs is
|
||||
// unchanged. Selecting a sorted-CSV string makes Zustand's default
|
||||
// shallow-equal short-circuit the re-render unless the actual ID set
|
||||
// changes.
|
||||
//
|
||||
// Why this matters: previously visibleIds was useMemo'd on `nodes`, so
|
||||
// the array reference recreated on every store mutation. fetchAndUpdate
|
||||
// (useCallback'd on visibleIds) then recreated, the useEffect re-fired,
|
||||
// it tore down the 60s setInterval and immediately re-ran the fan-out.
|
||||
// With ~5 store updates/second from heartbeats + polling, the canvas
|
||||
// hammered /workspaces/<id>/activity?type=delegation 5×N requests/sec
|
||||
// until edge rate-limit kicked in with HTTP 429. The recursive React
|
||||
// render trace in the original bug report (uE → ux → uE → ux ...) is
|
||||
// the symptom of this re-render storm.
|
||||
//
|
||||
// The fix is purely the dependency-stability change here; the fetch
|
||||
// logic is unchanged.
|
||||
const visibleIdsKey = useCanvasStore((s) =>
|
||||
s.nodes
|
||||
.filter((n) => !n.hidden)
|
||||
.map((n) => n.id)
|
||||
.sort()
|
||||
.join(",")
|
||||
);
|
||||
|
||||
// IDs of visible (non-nested, non-hidden) workspace nodes.
|
||||
// Recomputed only when the nodes array reference changes.
|
||||
const visibleIds = useMemo(
|
||||
() => nodes.filter((n) => !n.hidden).map((n) => n.id),
|
||||
[nodes]
|
||||
() => (visibleIdsKey ? visibleIdsKey.split(",") : []),
|
||||
[visibleIdsKey]
|
||||
);
|
||||
|
||||
// Fetch delegation activity for all visible workspaces and rebuild overlay edges.
|
||||
|
||||
@@ -32,11 +32,18 @@ export function CommunicationOverlay() {
|
||||
|
||||
const fetchComms = useCallback(async () => {
|
||||
try {
|
||||
// Fetch activity from all online workspaces
|
||||
// Fan-out cap: each polled workspace = 1 round-trip. The platform
|
||||
// rate limits at 600 req/min/IP; combined with heartbeats + other
|
||||
// canvas polling, every workspace polled here costs ~6 req/min
|
||||
// (1 every 30s × 1 per workspace). Capping at 3 keeps this
|
||||
// overlay's footprint at 18 req/min worst case — well under
|
||||
// budget even with 8+ workspaces visible. Caught 2026-05-04 when
|
||||
// a user with 8+ workspaces (Design Director + 6 sub-agents +
|
||||
// 3 standalones) saw sustained 429s in canvas console.
|
||||
const onlineNodes = nodesRef.current.filter((n) => n.data.status === "online");
|
||||
const allComms: Communication[] = [];
|
||||
|
||||
for (const node of onlineNodes.slice(0, 6)) {
|
||||
for (const node of onlineNodes.slice(0, 3)) {
|
||||
try {
|
||||
const activities = await api.get<Array<{
|
||||
id: string;
|
||||
@@ -91,10 +98,20 @@ export function CommunicationOverlay() {
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
// Gate polling on visibility — when the user collapses the overlay
|
||||
// the data isn't being read, so the per-workspace fan-out becomes
|
||||
// pure rate-limit overhead. Pre-fix this overlay polled regardless
|
||||
// of whether the panel was shown, costing ~36 req/min from a
|
||||
// hidden surface.
|
||||
if (!visible) return;
|
||||
fetchComms();
|
||||
const interval = setInterval(fetchComms, 10000);
|
||||
// 30s cadence (was 10s). At 3-workspace fan-out that's 6 req/min
|
||||
// worst case from this overlay. Combined with heartbeats (~30/min)
|
||||
// and other canvas polling, leaves ample headroom under the 600/
|
||||
// min/IP server-side rate limit even at 8+ workspace tenants.
|
||||
const interval = setInterval(fetchComms, 30000);
|
||||
return () => clearInterval(interval);
|
||||
}, [fetchComms]);
|
||||
}, [fetchComms, visible]);
|
||||
|
||||
if (!visible || comms.length === 0) {
|
||||
return (
|
||||
|
||||
@@ -215,16 +215,6 @@ export function ContextMenu() {
|
||||
closeContextMenu();
|
||||
}, [contextMenu, selectNode, setPanelTab, closeContextMenu]);
|
||||
|
||||
const handleExpand = useCallback(async () => {
|
||||
if (!contextMenu) return;
|
||||
try {
|
||||
await api.post(`/workspaces/${contextMenu.nodeId}/expand`, {});
|
||||
} catch (e) {
|
||||
showToast("Expand failed", "error");
|
||||
}
|
||||
closeContextMenu();
|
||||
}, [contextMenu, closeContextMenu]);
|
||||
|
||||
const setCollapsed = useCanvasStore((s) => s.setCollapsed);
|
||||
const handleCollapse = useCallback(async () => {
|
||||
if (!contextMenu) return;
|
||||
@@ -295,7 +285,7 @@ export function ContextMenu() {
|
||||
},
|
||||
{ label: "Zoom to Team", icon: "⊕", action: handleZoomToTeam },
|
||||
]
|
||||
: [{ label: "Expand to Team", icon: "▷", action: handleExpand }]),
|
||||
: []),
|
||||
{ label: "", icon: "", action: () => {}, divider: true },
|
||||
...(isPaused
|
||||
? [{ label: "Resume", icon: "▶", action: handleResume }]
|
||||
|
||||
@@ -48,16 +48,21 @@ export function EmptyState() {
|
||||
});
|
||||
|
||||
// "Create blank" bypasses templates entirely — no preflight, no
|
||||
// modal, just POST /workspaces with a default name and tier.
|
||||
// Deliberately NOT routed through useTemplateDeploy because it
|
||||
// has no `template.id` to deploy against.
|
||||
// modal, just POST /workspaces with a default name. Deliberately
|
||||
// NOT routed through useTemplateDeploy because it has no
|
||||
// `template.id` to deploy against.
|
||||
//
|
||||
// tier is omitted so the backend picks a SaaS-aware default
|
||||
// (T4 on SaaS, T3 on self-hosted — see WorkspaceHandler.DefaultTier).
|
||||
// The previous hardcoded `tier: 2` shipped every fresh-tenant agent
|
||||
// at Standard regardless of host, which surprised SaaS users whose
|
||||
// CreateWorkspaceDialog already defaults to T4.
|
||||
const createBlank = async () => {
|
||||
setBlankCreating(true);
|
||||
setBlankError(null);
|
||||
try {
|
||||
const ws = await api.post<{ id: string }>("/workspaces", {
|
||||
name: "My First Agent",
|
||||
tier: 2,
|
||||
canvas: firstDeployCoords(),
|
||||
});
|
||||
handleDeployed(ws.id);
|
||||
|
||||
@@ -132,6 +132,11 @@ const TAB_HELP: Record<
|
||||
check:
|
||||
"TOML rejects duplicate `[mcp_servers.molecule]` tables. Open ~/.codex/config.toml and remove the old block before pasting the new one.",
|
||||
},
|
||||
{
|
||||
symptom: "Canvas messages don't wake codex",
|
||||
check:
|
||||
"Step 3 (codex-channel-molecule bridge daemon) is required for inbound push. Check `pgrep -f codex-channel-molecule` and `tail ~/.codex-channel-molecule/daemon.log`.",
|
||||
},
|
||||
],
|
||||
},
|
||||
openclaw: {
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { api } from "@/lib/api";
|
||||
import type { MemoryEntry } from "@/components/MemoryInspectorPanel";
|
||||
|
||||
type Scope = "LOCAL" | "TEAM" | "GLOBAL";
|
||||
const SCOPES: Scope[] = ["LOCAL", "TEAM", "GLOBAL"];
|
||||
|
||||
interface AddProps {
|
||||
open: boolean;
|
||||
mode: "add";
|
||||
workspaceId: string;
|
||||
defaultScope: Scope;
|
||||
defaultNamespace?: string;
|
||||
entry?: undefined;
|
||||
onClose: () => void;
|
||||
onSaved: () => void;
|
||||
}
|
||||
|
||||
interface EditProps {
|
||||
open: boolean;
|
||||
mode: "edit";
|
||||
workspaceId: string;
|
||||
entry: MemoryEntry;
|
||||
defaultScope?: undefined;
|
||||
defaultNamespace?: undefined;
|
||||
onClose: () => void;
|
||||
onSaved: () => void;
|
||||
}
|
||||
|
||||
type Props = AddProps | EditProps;
|
||||
|
||||
export function MemoryEditorDialog(props: Props) {
|
||||
const { open, mode, workspaceId, onClose, onSaved } = props;
|
||||
const dialogRef = useRef<HTMLDivElement>(null);
|
||||
const [mounted, setMounted] = useState(false);
|
||||
const [scope, setScope] = useState<Scope>("LOCAL");
|
||||
const [namespace, setNamespace] = useState("general");
|
||||
const [content, setContent] = useState("");
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setMounted(true);
|
||||
}, []);
|
||||
|
||||
// Reset form whenever the dialog opens.
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
setError(null);
|
||||
setSaving(false);
|
||||
if (mode === "edit" && props.entry) {
|
||||
setScope(props.entry.scope);
|
||||
setNamespace(props.entry.namespace || "general");
|
||||
setContent(props.entry.content);
|
||||
} else if (mode === "add") {
|
||||
setScope(props.defaultScope);
|
||||
setNamespace(props.defaultNamespace || "general");
|
||||
setContent("");
|
||||
}
|
||||
// mode/props are stable per-open; intentional shallow deps.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [open]);
|
||||
|
||||
// Move focus into the dialog when it opens (WCAG SC 2.4.3).
|
||||
useEffect(() => {
|
||||
if (!open || !mounted) return;
|
||||
const raf = requestAnimationFrame(() => {
|
||||
dialogRef.current?.querySelector<HTMLElement>("textarea, input, select")?.focus();
|
||||
});
|
||||
return () => cancelAnimationFrame(raf);
|
||||
}, [open, mounted]);
|
||||
|
||||
// Escape closes; Cmd/Ctrl-Enter saves.
|
||||
const onCloseRef = useRef(onClose);
|
||||
onCloseRef.current = onClose;
|
||||
const handleSaveRef = useRef<() => void>(() => {});
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const handler = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") {
|
||||
e.preventDefault();
|
||||
onCloseRef.current();
|
||||
} else if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
||||
e.preventDefault();
|
||||
handleSaveRef.current();
|
||||
}
|
||||
};
|
||||
window.addEventListener("keydown", handler);
|
||||
return () => window.removeEventListener("keydown", handler);
|
||||
}, [open]);
|
||||
|
||||
const handleSave = async () => {
|
||||
if (saving) return;
|
||||
const trimmed = content.trim();
|
||||
if (!trimmed) {
|
||||
setError("Content cannot be empty");
|
||||
return;
|
||||
}
|
||||
setError(null);
|
||||
setSaving(true);
|
||||
try {
|
||||
if (mode === "add") {
|
||||
await api.post(`/workspaces/${workspaceId}/memories`, {
|
||||
content: trimmed,
|
||||
scope,
|
||||
namespace: namespace.trim() || "general",
|
||||
});
|
||||
} else {
|
||||
// PATCH only sends fields that changed. Content always changeable;
|
||||
// namespace only sent if it differs from the original (saves a
|
||||
// no-op write through redactSecrets + re-embed).
|
||||
const original = props.entry;
|
||||
const body: Record<string, string> = {};
|
||||
if (trimmed !== original.content) body.content = trimmed;
|
||||
const ns = namespace.trim() || "general";
|
||||
if (ns !== original.namespace) body.namespace = ns;
|
||||
if (Object.keys(body).length === 0) {
|
||||
// No-op edit — close without an HTTP round-trip.
|
||||
onSaved();
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
await api.patch(
|
||||
`/workspaces/${workspaceId}/memories/${encodeURIComponent(original.id)}`,
|
||||
body,
|
||||
);
|
||||
}
|
||||
onSaved();
|
||||
onClose();
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Save failed");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
handleSaveRef.current = handleSave;
|
||||
|
||||
if (!open || !mounted) return null;
|
||||
|
||||
const titleId = "memory-editor-title";
|
||||
const isEdit = mode === "edit";
|
||||
|
||||
return createPortal(
|
||||
<div className="fixed inset-0 z-[9999] flex items-center justify-center">
|
||||
<div className="absolute inset-0 bg-black/60 backdrop-blur-sm" onClick={onClose} />
|
||||
|
||||
<div
|
||||
ref={dialogRef}
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby={titleId}
|
||||
className="relative bg-surface-sunken border border-line rounded-xl shadow-2xl shadow-black/50 max-w-[480px] w-full mx-4 overflow-hidden"
|
||||
>
|
||||
<div className="px-5 py-4 space-y-3">
|
||||
<h3 id={titleId} className="text-sm font-semibold text-ink">
|
||||
{isEdit ? "Edit memory" : "Add memory"}
|
||||
</h3>
|
||||
|
||||
{/* Scope */}
|
||||
<div className="space-y-1">
|
||||
<label className="text-[10px] text-ink-soft block" htmlFor="memory-editor-scope">
|
||||
Scope
|
||||
</label>
|
||||
{isEdit ? (
|
||||
<div
|
||||
id="memory-editor-scope"
|
||||
className="text-[12px] font-mono text-ink-mid bg-surface rounded px-2 py-1.5 border border-line/50"
|
||||
title="Scope is fixed on edit. To move a memory across scopes, delete and re-create it."
|
||||
>
|
||||
{scope}
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center gap-1" id="memory-editor-scope" role="radiogroup" aria-label="Scope">
|
||||
{SCOPES.map((s) => (
|
||||
<button
|
||||
key={s}
|
||||
type="button"
|
||||
role="radio"
|
||||
aria-checked={scope === s}
|
||||
onClick={() => setScope(s)}
|
||||
className={[
|
||||
"px-3 py-1 text-[11px] rounded transition-colors",
|
||||
scope === s
|
||||
? "bg-accent-strong text-white"
|
||||
: "bg-surface-card text-ink-mid hover:text-ink",
|
||||
].join(" ")}
|
||||
>
|
||||
{s}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Namespace */}
|
||||
<div className="space-y-1">
|
||||
<label htmlFor="memory-editor-namespace" className="text-[10px] text-ink-soft block">
|
||||
Namespace
|
||||
</label>
|
||||
<input
|
||||
id="memory-editor-namespace"
|
||||
type="text"
|
||||
value={namespace}
|
||||
onChange={(e) => setNamespace(e.target.value)}
|
||||
placeholder="general"
|
||||
className="w-full bg-surface border border-line/60 focus:border-accent/60 rounded px-2 py-1.5 text-[12px] text-ink placeholder-zinc-600 focus:outline-none transition-colors"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="space-y-1">
|
||||
<label htmlFor="memory-editor-content" className="text-[10px] text-ink-soft block">
|
||||
Content
|
||||
</label>
|
||||
<textarea
|
||||
id="memory-editor-content"
|
||||
value={content}
|
||||
onChange={(e) => setContent(e.target.value)}
|
||||
rows={6}
|
||||
placeholder="What should the agent remember?"
|
||||
className="w-full bg-surface border border-line/60 focus:border-accent/60 rounded px-2 py-1.5 text-[12px] font-mono text-ink placeholder-zinc-600 focus:outline-none transition-colors resize-y min-h-[100px] max-h-[300px]"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div
|
||||
role="alert"
|
||||
aria-live="assertive"
|
||||
className="px-2 py-1.5 bg-red-950/30 border border-red-800/40 rounded text-[11px] text-bad"
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-end gap-2 px-5 py-3 border-t border-line bg-surface/50">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
disabled={saving}
|
||||
className="px-3.5 py-1.5 text-[13px] text-ink-mid hover:text-ink bg-surface-card hover:bg-surface-elevated border border-line hover:border-line-soft rounded-lg transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-accent/40 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSave}
|
||||
disabled={saving}
|
||||
className="px-3.5 py-1.5 text-[13px] rounded-lg transition-colors bg-accent hover:bg-accent-strong text-white focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-offset-surface-sunken focus-visible:ring-accent/60 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{saving ? "Saving…" : isEdit ? "Save changes" : "Add memory"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { api } from "@/lib/api";
|
||||
import { ConfirmDialog } from "@/components/ConfirmDialog";
|
||||
import { MemoryEditorDialog } from "@/components/MemoryEditorDialog";
|
||||
|
||||
// ── Types ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -92,6 +93,13 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
// ── Delete state ─────────────────────────────────────────────────────────────
|
||||
const [pendingDeleteId, setPendingDeleteId] = useState<string | null>(null);
|
||||
|
||||
// ── Editor state (Add + Edit share one modal) ───────────────────────────────
|
||||
type EditorState =
|
||||
| { mode: "add" }
|
||||
| { mode: "edit"; entry: MemoryEntry }
|
||||
| null;
|
||||
const [editorState, setEditorState] = useState<EditorState>(null);
|
||||
|
||||
// ── Data loading ────────────────────────────────────────────────────────────
|
||||
|
||||
const loadEntries = useCallback(async () => {
|
||||
@@ -241,14 +249,24 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
? "1 memory"
|
||||
: `${entries.length} memories`}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadEntries}
|
||||
className="px-2 py-1 text-[11px] bg-surface-card hover:bg-surface-card text-ink-mid rounded transition-colors"
|
||||
aria-label="Refresh memories"
|
||||
>
|
||||
↻ Refresh
|
||||
</button>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setEditorState({ mode: "add" })}
|
||||
className="px-2 py-1 text-[11px] bg-accent hover:bg-accent-strong text-white rounded transition-colors"
|
||||
aria-label="Add memory"
|
||||
>
|
||||
+ Add
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadEntries}
|
||||
className="px-2 py-1 text-[11px] bg-surface-card hover:bg-surface-card text-ink-mid rounded transition-colors"
|
||||
aria-label="Refresh memories"
|
||||
>
|
||||
↻ Refresh
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Error banner */}
|
||||
@@ -307,6 +325,7 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
<MemoryEntryRow
|
||||
key={entry.id}
|
||||
entry={entry}
|
||||
onEdit={() => setEditorState({ mode: "edit", entry })}
|
||||
onDelete={() => setPendingDeleteId(entry.id)}
|
||||
/>
|
||||
))}
|
||||
@@ -324,6 +343,29 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setPendingDeleteId(null)}
|
||||
/>
|
||||
|
||||
{/* Add / Edit dialog */}
|
||||
{editorState?.mode === "add" && (
|
||||
<MemoryEditorDialog
|
||||
open={true}
|
||||
mode="add"
|
||||
workspaceId={workspaceId}
|
||||
defaultScope={activeScope}
|
||||
defaultNamespace={activeNamespace || "general"}
|
||||
onClose={() => setEditorState(null)}
|
||||
onSaved={loadEntries}
|
||||
/>
|
||||
)}
|
||||
{editorState?.mode === "edit" && (
|
||||
<MemoryEditorDialog
|
||||
open={true}
|
||||
mode="edit"
|
||||
workspaceId={workspaceId}
|
||||
entry={editorState.entry}
|
||||
onClose={() => setEditorState(null)}
|
||||
onSaved={loadEntries}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -332,10 +374,11 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
|
||||
interface MemoryEntryRowProps {
|
||||
entry: MemoryEntry;
|
||||
onEdit: () => void;
|
||||
onDelete: () => void;
|
||||
}
|
||||
|
||||
function MemoryEntryRow({ entry, onDelete }: MemoryEntryRowProps) {
|
||||
function MemoryEntryRow({ entry, onEdit, onDelete }: MemoryEntryRowProps) {
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const bodyId = `mem-body-${sanitizeId(entry.id)}`;
|
||||
|
||||
@@ -413,17 +456,30 @@ function MemoryEntryRow({ entry, onDelete }: MemoryEntryRowProps) {
|
||||
<span className="text-[9px] text-ink-soft">
|
||||
Created: {new Date(entry.created_at).toLocaleString()}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onDelete();
|
||||
}}
|
||||
aria-label="Delete memory"
|
||||
className="text-[10px] px-2 py-0.5 bg-red-950/40 hover:bg-red-900/50 border border-red-900/30 rounded text-bad transition-colors shrink-0"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
<div className="flex items-center gap-1.5 shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onEdit();
|
||||
}}
|
||||
aria-label="Edit memory"
|
||||
className="text-[10px] px-2 py-0.5 bg-surface-card hover:bg-surface-elevated border border-line/40 rounded text-ink-mid hover:text-ink transition-colors"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onDelete();
|
||||
}}
|
||||
aria-label="Delete memory"
|
||||
className="text-[10px] px-2 py-0.5 bg-red-950/40 hover:bg-red-900/50 border border-red-900/30 rounded text-bad transition-colors"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -283,7 +283,7 @@ export function SidePanel() {
|
||||
{panelTab === "skills" && <SkillsTab key={selectedNodeId} workspaceId={selectedNodeId} data={node.data} />}
|
||||
{panelTab === "activity" && <ActivityTab key={selectedNodeId} workspaceId={selectedNodeId} />}
|
||||
{panelTab === "chat" && <ChatTab key={selectedNodeId} workspaceId={selectedNodeId} data={node.data} />}
|
||||
{panelTab === "terminal" && <TerminalTab key={selectedNodeId} workspaceId={selectedNodeId} />}
|
||||
{panelTab === "terminal" && <TerminalTab key={selectedNodeId} workspaceId={selectedNodeId} data={node.data} />}
|
||||
{panelTab === "config" && <ConfigTab key={selectedNodeId} workspaceId={selectedNodeId} />}
|
||||
{panelTab === "schedule" && <ScheduleTab key={selectedNodeId} workspaceId={selectedNodeId} />}
|
||||
{panelTab === "channels" && <ChannelsTab key={selectedNodeId} workspaceId={selectedNodeId} />}
|
||||
|
||||
@@ -316,7 +316,7 @@ export function Toolbar() {
|
||||
<div className="space-y-2">
|
||||
<HelpRow shortcut="⌘K" text="Search workspaces and jump straight into Details or Chat." />
|
||||
<HelpRow shortcut="Palette" text="Open the template palette to deploy a new workspace." />
|
||||
<HelpRow shortcut="Right-click" text="Use node actions for expand, duplicate, export, restart, or delete." />
|
||||
<HelpRow shortcut="Right-click" text="Use node actions for duplicate, export, restart, or delete." />
|
||||
<HelpRow shortcut="Chat" text="If a task is still running, the chat tab resumes that session automatically." />
|
||||
<HelpRow shortcut="Config" text="Use the Config tab for skills, model, secrets, and runtime settings." />
|
||||
<HelpRow shortcut="Dbl-click / Z" text="Zoom canvas to fit a team node and all its sub-workspaces." />
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { useCallback, useMemo } from "react";
|
||||
import { Handle, NodeResizer, Position, type NodeProps, type Node } from "@xyflow/react";
|
||||
import { useCanvasStore, type WorkspaceNodeData } from "@/store/canvas";
|
||||
import { getConfigurationError, getConfigurationStatus } from "@/store/canvas-topology";
|
||||
import { showToast } from "@/components/Toaster";
|
||||
import { Tooltip } from "@/components/Tooltip";
|
||||
import { STATUS_CONFIG, TIER_CONFIG } from "@/lib/design-tokens";
|
||||
@@ -35,8 +36,28 @@ function EjectIcon(props: React.SVGProps<SVGSVGElement>) {
|
||||
}
|
||||
|
||||
export function WorkspaceNode({ id, data }: NodeProps<Node<WorkspaceNodeData>>) {
|
||||
const statusCfg = STATUS_CONFIG[data.status] || STATUS_CONFIG.offline;
|
||||
// Configuration-status overlay (PR #2756 / #467 chain). When the
|
||||
// workspace is reachable but adapter.setup() failed (typically a
|
||||
// missing/rotated LLM credential), the agent_card carries
|
||||
// configuration_status: "not_configured". Surface this as a distinct
|
||||
// tile state so the operator sees a useful error instead of an
|
||||
// ambiguous "online but silent" workspace.
|
||||
//
|
||||
// The override only applies when the underlying status is "online" —
|
||||
// a workspace that's actually offline / failed / provisioning gets
|
||||
// its own treatment. "online + not_configured" is the gap PR #2756
|
||||
// introduced; everything else was already covered.
|
||||
const isMisconfigured =
|
||||
data.status === "online" &&
|
||||
getConfigurationStatus(data.agentCard) === "not_configured";
|
||||
const configurationError = getConfigurationError(data.agentCard);
|
||||
const effectiveStatus = isMisconfigured ? "not_configured" : data.status;
|
||||
const statusCfg = STATUS_CONFIG[effectiveStatus] || STATUS_CONFIG.offline;
|
||||
const tierCfg = TIER_CONFIG[data.tier] || { label: `T${data.tier}`, color: "text-ink-mid bg-surface-card border border-line" };
|
||||
const tooltipExtra = isMisconfigured && configurationError
|
||||
? `Agent not configured: ${configurationError}`
|
||||
: null;
|
||||
void tooltipExtra; // wired in via aria-label below; reserved here for future tooltip surface.
|
||||
// Org-deploy context — four derived flags off one store subscription.
|
||||
// Drives the shimmer while provisioning, the dimmed/non-draggable
|
||||
// treatment on locked descendants, and the Cancel pill on the root.
|
||||
@@ -75,7 +96,12 @@ export function WorkspaceNode({ id, data }: NodeProps<Node<WorkspaceNodeData>>)
|
||||
<div
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={`${data.name} workspace — ${data.status}`}
|
||||
aria-label={
|
||||
isMisconfigured && configurationError
|
||||
? `${data.name} workspace — agent not configured: ${configurationError}`
|
||||
: `${data.name} workspace — ${data.status}`
|
||||
}
|
||||
title={isMisconfigured && configurationError ? `Agent not configured: ${configurationError}` : undefined}
|
||||
aria-pressed={isSelected}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
@@ -283,11 +309,12 @@ export function WorkspaceNode({ id, data }: NodeProps<Node<WorkspaceNodeData>>)
|
||||
|
||||
{/* Bottom row: status / active tasks */}
|
||||
<div className="flex items-center justify-between mt-0.5">
|
||||
{data.status !== "online" ? (
|
||||
{effectiveStatus !== "online" ? (
|
||||
<div className={`text-[10px] uppercase tracking-widest font-medium ${
|
||||
data.status === "failed" ? "text-bad" :
|
||||
data.status === "degraded" ? "text-warm" :
|
||||
data.status === "provisioning" ? "text-accent" :
|
||||
effectiveStatus === "failed" ? "text-bad" :
|
||||
effectiveStatus === "degraded" ? "text-warm" :
|
||||
effectiveStatus === "not_configured" ? "text-warm" :
|
||||
effectiveStatus === "provisioning" ? "text-accent" :
|
||||
"text-ink-mid"
|
||||
}`}>
|
||||
{statusCfg.label}
|
||||
@@ -313,6 +340,19 @@ export function WorkspaceNode({ id, data }: NodeProps<Node<WorkspaceNodeData>>)
|
||||
{data.lastSampleError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Configuration error preview — same visual as the degraded
|
||||
* error preview but keyed off the agent_card's configuration_status.
|
||||
* Tells the operator which env var is missing so they can fix it
|
||||
* without having to dig into the workspace logs. */}
|
||||
{isMisconfigured && configurationError && (
|
||||
<div
|
||||
className="text-[10px] text-warm truncate mt-1 bg-warm/10 px-1.5 py-0.5 rounded border border-warm/40"
|
||||
title={configurationError}
|
||||
>
|
||||
{configurationError}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Handle
|
||||
|
||||
@@ -296,4 +296,75 @@ describe("A2ATopologyOverlay component", () => {
|
||||
// setA2AEdges should still be called with an empty array
|
||||
expect(mockStoreState.setA2AEdges).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// Regression for the 2026-05-04 render-loop incident:
|
||||
// tenant heartbeats / status flips / peer-discovery writes mutated
|
||||
// canvas store .nodes ~5x/sec. Previously visibleIds was useMemo'd on
|
||||
// [nodes] so the array reference recreated on every store mutation,
|
||||
// causing fetchAndUpdate to recreate, the useEffect to re-fire, and
|
||||
// the 60-second polling fan-out to fire on EVERY store update. With
|
||||
// 5 visible workspaces and 5 store updates/sec, the canvas hammered
|
||||
// /workspaces/<id>/activity?type=delegation 25×/sec until edge rate
|
||||
// -limit returned 429 (per browser console captured by user).
|
||||
//
|
||||
// Fix: select a stable string key (sorted CSV of IDs) from Zustand
|
||||
// so the selector's shallow-equal short-circuit prevents re-renders
|
||||
// when the actual ID set hasn't changed.
|
||||
//
|
||||
// This test verifies the fetch fires ONCE on mount + only re-fires
|
||||
// when the visible ID set actually changes, NOT on every nodes[]
|
||||
// reference change.
|
||||
it("does not re-fetch when nodes[] reference changes but visible IDs are the same", async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
mockGet.mockResolvedValue([] as any);
|
||||
const { rerender } = render(<A2ATopologyOverlay />);
|
||||
await act(async () => { await Promise.resolve(); await Promise.resolve(); });
|
||||
|
||||
const callsAfterMount = mockGet.mock.calls.length;
|
||||
// Sanity: 2 visible nodes (ws-a, ws-b) → 2 fan-out requests on mount
|
||||
expect(callsAfterMount).toBe(2);
|
||||
|
||||
// Simulate a store mutation that changes the nodes array reference
|
||||
// (e.g. status flip on a node) WITHOUT changing the set of visible
|
||||
// IDs. Pre-fix: this triggered a re-fetch storm. Post-fix: the
|
||||
// sorted-CSV selector returns the same key, Zustand's shallow-equal
|
||||
// short-circuits, useMemo keeps the same visibleIds, fetchAndUpdate
|
||||
// keeps the same identity, useEffect does NOT re-fire.
|
||||
mockStoreState.nodes = [
|
||||
{ id: "ws-a", hidden: false, data: { newStatus: "online" } }, // mutated
|
||||
{ id: "ws-b", hidden: false, data: {} },
|
||||
{ id: "ws-hidden", hidden: true, data: {} },
|
||||
];
|
||||
rerender(<A2ATopologyOverlay />);
|
||||
await act(async () => { await Promise.resolve(); await Promise.resolve(); });
|
||||
|
||||
// No additional fetches should have fired.
|
||||
expect(mockGet.mock.calls.length).toBe(callsAfterMount);
|
||||
});
|
||||
|
||||
it("re-fetches when the visible ID set actually changes", async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
mockGet.mockResolvedValue([] as any);
|
||||
const { rerender } = render(<A2ATopologyOverlay />);
|
||||
await act(async () => { await Promise.resolve(); await Promise.resolve(); });
|
||||
|
||||
const callsAfterMount = mockGet.mock.calls.length;
|
||||
expect(callsAfterMount).toBe(2);
|
||||
|
||||
// Add a new visible workspace — the visible-ID-set actually changed.
|
||||
mockStoreState.nodes = [
|
||||
{ id: "ws-a", hidden: false, data: {} },
|
||||
{ id: "ws-b", hidden: false, data: {} },
|
||||
{ id: "ws-c", hidden: false, data: {} }, // NEW
|
||||
{ id: "ws-hidden", hidden: true, data: {} },
|
||||
];
|
||||
rerender(<A2ATopologyOverlay />);
|
||||
await act(async () => { await Promise.resolve(); await Promise.resolve(); });
|
||||
|
||||
// Should have fetched the additional workspace + the existing two
|
||||
// (the effect re-fires once with the new ID set). Total: 2 + 3 = 5.
|
||||
expect(mockGet.mock.calls.length).toBe(callsAfterMount + 3);
|
||||
const allPaths = mockGet.mock.calls.map(([p]) => p as string);
|
||||
expect(allPaths.some((p) => p.includes("ws-c"))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* CommunicationOverlay tests — pin the rate-limit fix shipped 2026-05-04.
|
||||
*
|
||||
* The overlay polls /workspaces/:id/activity?limit=5 for each online
|
||||
* workspace. Pre-fix it (a) polled regardless of visibility and (b)
|
||||
* fanned out to 6 workspaces every 10s. With 8+ workspaces a user
|
||||
* triggered sustained 429s (server-side rate limit is 600 req/min/IP).
|
||||
*
|
||||
* These tests pin:
|
||||
* 1. Fan-out cap of 3 — even with 6 online nodes, only 3 fetches
|
||||
* 2. Visibility gate — when collapsed, no polling
|
||||
*
|
||||
* If a future refactor pushes either dial back up, CI fails before
|
||||
* the regression hits a paying tenant.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { render, cleanup, act, fireEvent } from "@testing-library/react";
|
||||
|
||||
// ── Mocks (hoisted before imports) ────────────────────────────────────────────
|
||||
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: { get: vi.fn() },
|
||||
}));
|
||||
|
||||
// Six online nodes — enough to verify the cap of 3.
|
||||
const mockStoreState = {
|
||||
selectedNodeId: null as string | null,
|
||||
nodes: [
|
||||
{ id: "ws-1", data: { status: "online", name: "ws-1" } },
|
||||
{ id: "ws-2", data: { status: "online", name: "ws-2" } },
|
||||
{ id: "ws-3", data: { status: "online", name: "ws-3" } },
|
||||
{ id: "ws-4", data: { status: "online", name: "ws-4" } },
|
||||
{ id: "ws-5", data: { status: "online", name: "ws-5" } },
|
||||
{ id: "ws-6", data: { status: "online", name: "ws-6" } },
|
||||
{ id: "ws-offline", data: { status: "offline", name: "off" } },
|
||||
],
|
||||
};
|
||||
|
||||
vi.mock("@/store/canvas", () => ({
|
||||
useCanvasStore: vi.fn(
|
||||
(selector: (s: typeof mockStoreState) => unknown) =>
|
||||
selector(mockStoreState)
|
||||
),
|
||||
}));
|
||||
|
||||
// design-tokens has named exports — keep the shape minimal.
|
||||
vi.mock("@/lib/design-tokens", () => ({
|
||||
COMM_TYPE_LABELS: {
|
||||
a2a_send: "→",
|
||||
a2a_receive: "←",
|
||||
task_update: "✓",
|
||||
},
|
||||
}));
|
||||
|
||||
// ── Imports (after mocks) ─────────────────────────────────────────────────────
|
||||
|
||||
import { api } from "@/lib/api";
|
||||
import { CommunicationOverlay } from "../CommunicationOverlay";
|
||||
|
||||
const mockGet = vi.mocked(api.get);
|
||||
|
||||
// ── Setup ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
mockGet.mockReset();
|
||||
mockGet.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("CommunicationOverlay — fan-out cap", () => {
|
||||
it("polls at most 3 of 6 online workspaces (rate-limit floor)", async () => {
|
||||
await act(async () => {
|
||||
render(<CommunicationOverlay />);
|
||||
});
|
||||
// Mount fires the first poll synchronously (no interval tick yet).
|
||||
// Pre-fix: 6 calls. Post-fix: 3.
|
||||
expect(mockGet).toHaveBeenCalledTimes(3);
|
||||
// Verify the calls are for the FIRST 3 online nodes (slice order).
|
||||
expect(mockGet).toHaveBeenCalledWith("/workspaces/ws-1/activity?limit=5");
|
||||
expect(mockGet).toHaveBeenCalledWith("/workspaces/ws-2/activity?limit=5");
|
||||
expect(mockGet).toHaveBeenCalledWith("/workspaces/ws-3/activity?limit=5");
|
||||
});
|
||||
|
||||
it("never polls offline workspaces", async () => {
|
||||
await act(async () => {
|
||||
render(<CommunicationOverlay />);
|
||||
});
|
||||
expect(mockGet).not.toHaveBeenCalledWith(
|
||||
"/workspaces/ws-offline/activity?limit=5",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("CommunicationOverlay — cadence", () => {
|
||||
it("uses 30s interval cadence (was 10s pre-fix)", async () => {
|
||||
await act(async () => {
|
||||
render(<CommunicationOverlay />);
|
||||
});
|
||||
expect(mockGet).toHaveBeenCalledTimes(3); // initial mount poll
|
||||
|
||||
// Advance 10s — pre-fix this would fire another poll. Post-fix: silent.
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(10_000);
|
||||
});
|
||||
expect(mockGet).toHaveBeenCalledTimes(3);
|
||||
|
||||
// Advance to 30s — interval fires.
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(20_000);
|
||||
});
|
||||
expect(mockGet).toHaveBeenCalledTimes(6); // +3 from second tick
|
||||
});
|
||||
});
|
||||
|
||||
describe("CommunicationOverlay — visibility gate", () => {
|
||||
// The visibility gate is the dial that drops collapsed-panel polling
|
||||
// to ZERO. The cadence test above can't catch its removal — if a
|
||||
// refactor dropped `if (!visible) return`, the cadence test would
|
||||
// still pass because the effect would still fire every 30s.
|
||||
//
|
||||
// Direct probe: render with comms-returning mock so the panel
|
||||
// actually renders (close button only exists in the expanded panel,
|
||||
// not the collapsed button-state). Click close, advance the clock,
|
||||
// assert no further fetches.
|
||||
it("stops polling after the user collapses the panel", async () => {
|
||||
// Mock returns one a2a_send so comms.length > 0 → panel renders →
|
||||
// close button accessible.
|
||||
mockGet.mockResolvedValue([
|
||||
{
|
||||
id: "act-1",
|
||||
workspace_id: "ws-1",
|
||||
activity_type: "a2a_send",
|
||||
source_id: "ws-1",
|
||||
target_id: "ws-2",
|
||||
summary: "test",
|
||||
status: "completed",
|
||||
duration_ms: 100,
|
||||
created_at: new Date().toISOString(),
|
||||
},
|
||||
]);
|
||||
|
||||
const { getByLabelText } = await act(async () => {
|
||||
return render(<CommunicationOverlay />);
|
||||
});
|
||||
// Drain pending microtasks (resolves the await in fetchComms) so
|
||||
// setComms lands and the panel renders. Don't advance time — that
|
||||
// would fire the next interval tick and pollute the assertion.
|
||||
await act(async () => {
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
});
|
||||
// Initial mount polled 3 workspaces.
|
||||
expect(mockGet).toHaveBeenCalledTimes(3);
|
||||
mockGet.mockClear();
|
||||
|
||||
// Click the close button. Synchronous getByLabelText avoids
|
||||
// findBy's internal setTimeout (deadlocks under useFakeTimers).
|
||||
const closeBtn = getByLabelText("Close communications panel");
|
||||
await act(async () => {
|
||||
fireEvent.click(closeBtn);
|
||||
});
|
||||
|
||||
// Advance well past the 30s cadence — gate should suppress the tick.
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(60_000);
|
||||
});
|
||||
expect(mockGet).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -228,4 +228,38 @@ describe("ContextMenu — keyboard accessibility", () => {
|
||||
);
|
||||
expect(closeContextMenu).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// The "Expand to Team" right-click action was removed in Phase 2 of
|
||||
// RFC #2857 — every workspace can already have children via the
|
||||
// regular CreateWorkspace flow with parent_id, so a separate
|
||||
// backend bulk-create handler (which was non-idempotent and leaked
|
||||
// EC2s on every duplicate call) was deleted in PR #2856 and the
|
||||
// canvas affordance is gone with it.
|
||||
it("'Expand to Team' menu item is gone (childless workspace)", () => {
|
||||
// Default mockStore.nodes = [] → no children → workspace is childless.
|
||||
render(<ContextMenu />);
|
||||
const items = screen.getAllByRole("menuitem");
|
||||
const labels = items.map((el) => el.textContent?.trim() ?? "");
|
||||
// Literal absence — vitest's toContain uses Object.is/===, so the
|
||||
// earlier `.not.toContain(expect.stringMatching(...))` shape passed
|
||||
// for ANY string array (asymmetric matchers only work with toEqual /
|
||||
// arrayContaining). Pin the production string verbatim.
|
||||
expect(labels.some((l) => l.includes("Expand to Team"))).toBe(false);
|
||||
// Sanity: childless menu still has the regular actions.
|
||||
expect(labels.some((l) => l.includes("Delete"))).toBe(true);
|
||||
expect(labels.some((l) => l.includes("Restart"))).toBe(true);
|
||||
});
|
||||
|
||||
it("'Collapse Team' is still present when the workspace HAS children", () => {
|
||||
// Mark a child belonging to ws-1 so hasChildren() returns true.
|
||||
mockStore.nodes = [{ id: "child-1", data: { parentId: "ws-1" } }];
|
||||
render(<ContextMenu />);
|
||||
const items = screen.getAllByRole("menuitem");
|
||||
const labels = items.map((el) => el.textContent?.trim() ?? "");
|
||||
expect(labels.some((l) => /Collapse Team|Expand Team/.test(l))).toBe(true);
|
||||
expect(labels.some((l) => l.includes("Arrange Children"))).toBe(true);
|
||||
expect(labels.some((l) => l.includes("Zoom to Team"))).toBe(true);
|
||||
// Cleanup for other tests.
|
||||
mockStore.nodes = [];
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* MemoryEditorDialog tests — covers Add (POST /memories) and Edit
|
||||
* (PATCH /memories/:id) flows. Pins:
|
||||
* - Add posts {content, scope, namespace} with the trimmed defaults
|
||||
* - Edit only sends fields that changed (no-op edit short-circuits, no PATCH fires)
|
||||
* - Empty content blocks save
|
||||
* - Save error surfaces in the dialog and keeps the modal open
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { render, screen, fireEvent, waitFor, cleanup } from "@testing-library/react";
|
||||
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: {
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
patch: vi.fn(),
|
||||
del: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import { api } from "@/lib/api";
|
||||
import { MemoryEditorDialog } from "../MemoryEditorDialog";
|
||||
import type { MemoryEntry } from "../MemoryInspectorPanel";
|
||||
|
||||
const mockPost = vi.mocked(api.post);
|
||||
const mockPatch = vi.mocked(api.patch);
|
||||
|
||||
const SAMPLE: MemoryEntry = {
|
||||
id: "mem-x",
|
||||
workspace_id: "ws-1",
|
||||
content: "original content",
|
||||
scope: "TEAM",
|
||||
namespace: "procedures",
|
||||
created_at: "2026-04-17T12:00:00.000Z",
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockPost.mockResolvedValue({} as never);
|
||||
mockPatch.mockResolvedValue({} as never);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
describe("Add mode", () => {
|
||||
it("POSTs scope+namespace+trimmed-content and calls onSaved+onClose", async () => {
|
||||
const onClose = vi.fn();
|
||||
const onSaved = vi.fn();
|
||||
render(
|
||||
<MemoryEditorDialog
|
||||
open
|
||||
mode="add"
|
||||
workspaceId="ws-1"
|
||||
defaultScope="GLOBAL"
|
||||
defaultNamespace="facts"
|
||||
onClose={onClose}
|
||||
onSaved={onSaved}
|
||||
/>,
|
||||
);
|
||||
|
||||
const textarea = screen.getByLabelText(/Content/i) as HTMLTextAreaElement;
|
||||
fireEvent.change(textarea, { target: { value: " new fact " } });
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /Add memory$/i }));
|
||||
|
||||
await waitFor(() => expect(mockPost).toHaveBeenCalledTimes(1));
|
||||
expect(mockPost).toHaveBeenCalledWith("/workspaces/ws-1/memories", {
|
||||
content: "new fact",
|
||||
scope: "GLOBAL",
|
||||
namespace: "facts",
|
||||
});
|
||||
expect(onSaved).toHaveBeenCalledTimes(1);
|
||||
expect(onClose).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("blocks save when content is empty (whitespace-only)", () => {
|
||||
const onClose = vi.fn();
|
||||
const onSaved = vi.fn();
|
||||
render(
|
||||
<MemoryEditorDialog
|
||||
open
|
||||
mode="add"
|
||||
workspaceId="ws-1"
|
||||
defaultScope="LOCAL"
|
||||
onClose={onClose}
|
||||
onSaved={onSaved}
|
||||
/>,
|
||||
);
|
||||
const textarea = screen.getByLabelText(/Content/i) as HTMLTextAreaElement;
|
||||
fireEvent.change(textarea, { target: { value: " " } });
|
||||
fireEvent.click(screen.getByRole("button", { name: /Add memory$/i }));
|
||||
expect(mockPost).not.toHaveBeenCalled();
|
||||
expect(screen.getByRole("alert").textContent).toMatch(/empty/i);
|
||||
expect(onSaved).not.toHaveBeenCalled();
|
||||
expect(onClose).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Edit mode", () => {
|
||||
it("PATCHes only changed fields", async () => {
|
||||
const onClose = vi.fn();
|
||||
const onSaved = vi.fn();
|
||||
render(
|
||||
<MemoryEditorDialog
|
||||
open
|
||||
mode="edit"
|
||||
workspaceId="ws-1"
|
||||
entry={SAMPLE}
|
||||
onClose={onClose}
|
||||
onSaved={onSaved}
|
||||
/>,
|
||||
);
|
||||
|
||||
const textarea = screen.getByLabelText(/Content/i) as HTMLTextAreaElement;
|
||||
fireEvent.change(textarea, { target: { value: "rewritten content" } });
|
||||
// namespace untouched
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /Save changes/i }));
|
||||
|
||||
await waitFor(() => expect(mockPatch).toHaveBeenCalledTimes(1));
|
||||
expect(mockPatch).toHaveBeenCalledWith(
|
||||
"/workspaces/ws-1/memories/mem-x",
|
||||
{ content: "rewritten content" },
|
||||
);
|
||||
expect(onSaved).toHaveBeenCalledTimes(1);
|
||||
expect(onClose).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("no-op edit short-circuits (no PATCH fires) and still closes", async () => {
|
||||
const onClose = vi.fn();
|
||||
const onSaved = vi.fn();
|
||||
render(
|
||||
<MemoryEditorDialog
|
||||
open
|
||||
mode="edit"
|
||||
workspaceId="ws-1"
|
||||
entry={SAMPLE}
|
||||
onClose={onClose}
|
||||
onSaved={onSaved}
|
||||
/>,
|
||||
);
|
||||
fireEvent.click(screen.getByRole("button", { name: /Save changes/i }));
|
||||
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
||||
expect(mockPatch).not.toHaveBeenCalled();
|
||||
expect(onSaved).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("sends namespace too when both content and namespace changed", async () => {
|
||||
const onClose = vi.fn();
|
||||
const onSaved = vi.fn();
|
||||
render(
|
||||
<MemoryEditorDialog
|
||||
open
|
||||
mode="edit"
|
||||
workspaceId="ws-1"
|
||||
entry={SAMPLE}
|
||||
onClose={onClose}
|
||||
onSaved={onSaved}
|
||||
/>,
|
||||
);
|
||||
fireEvent.change(screen.getByLabelText(/Content/i), {
|
||||
target: { value: "newer content" },
|
||||
});
|
||||
fireEvent.change(screen.getByLabelText(/Namespace/i), {
|
||||
target: { value: "blockers" },
|
||||
});
|
||||
fireEvent.click(screen.getByRole("button", { name: /Save changes/i }));
|
||||
await waitFor(() => expect(mockPatch).toHaveBeenCalledTimes(1));
|
||||
expect(mockPatch).toHaveBeenCalledWith(
|
||||
"/workspaces/ws-1/memories/mem-x",
|
||||
{ content: "newer content", namespace: "blockers" },
|
||||
);
|
||||
});
|
||||
|
||||
it("surfaces save error and keeps the modal open", async () => {
|
||||
const onClose = vi.fn();
|
||||
const onSaved = vi.fn();
|
||||
mockPatch.mockRejectedValueOnce(new Error("boom"));
|
||||
render(
|
||||
<MemoryEditorDialog
|
||||
open
|
||||
mode="edit"
|
||||
workspaceId="ws-1"
|
||||
entry={SAMPLE}
|
||||
onClose={onClose}
|
||||
onSaved={onSaved}
|
||||
/>,
|
||||
);
|
||||
fireEvent.change(screen.getByLabelText(/Content/i), {
|
||||
target: { value: "rewritten content" },
|
||||
});
|
||||
fireEvent.click(screen.getByRole("button", { name: /Save changes/i }));
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole("alert").textContent).toMatch(/boom/),
|
||||
);
|
||||
expect(onClose).not.toHaveBeenCalled();
|
||||
expect(onSaved).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useRef, useEffect, useCallback } from "react";
|
||||
import { useState, useRef, useEffect, useCallback, useLayoutEffect } from "react";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
import remarkGfm from "remark-gfm";
|
||||
import { api } from "@/lib/api";
|
||||
@@ -124,14 +124,43 @@ function extractReplyText(resp: A2AResponse): string {
|
||||
// doesn't). Single source of truth for file-part parsing across
|
||||
// live chat, activity log replay, and any future consumers.
|
||||
|
||||
/** Initial chat history page size. The newest N messages are rendered
|
||||
* on first paint; older history is fetched on demand via loadOlder()
|
||||
* when the user scrolls the top sentinel into view. */
|
||||
const INITIAL_HISTORY_LIMIT = 10;
|
||||
/** Subsequent older-history batch size. Larger than INITIAL so a long
|
||||
* scroll-back doesn't fan out into many round-trips. */
|
||||
const OLDER_HISTORY_BATCH = 20;
|
||||
|
||||
/**
|
||||
* Load chat history from the activity_logs database via the platform API.
|
||||
* Uses source=canvas to only get user-initiated messages (not agent-to-agent).
|
||||
*
|
||||
* Pagination:
|
||||
* - Pass `limit` to bound the page size (newest-first from server).
|
||||
* - Pass `beforeTs` (RFC3339) to fetch rows STRICTLY OLDER than that
|
||||
* timestamp. Combined with limit, this yields the next-older page
|
||||
* when scrolling backward through history.
|
||||
*
|
||||
* `reachedEnd` is true when the server returned fewer rows than asked
|
||||
* for — caller uses this to disable further older-batch fetches.
|
||||
* (Counts row-level returns, not chat-bubble count: each row may
|
||||
* produce 1-2 bubbles.)
|
||||
*/
|
||||
async function loadMessagesFromDB(workspaceId: string): Promise<{ messages: ChatMessage[]; error: string | null }> {
|
||||
async function loadMessagesFromDB(
|
||||
workspaceId: string,
|
||||
limit: number,
|
||||
beforeTs?: string,
|
||||
): Promise<{ messages: ChatMessage[]; error: string | null; reachedEnd: boolean }> {
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
type: "a2a_receive",
|
||||
source: "canvas",
|
||||
limit: String(limit),
|
||||
});
|
||||
if (beforeTs) params.set("before_ts", beforeTs);
|
||||
const activities = await api.get<ActivityRowForHydration[]>(
|
||||
`/workspaces/${workspaceId}/activity?type=a2a_receive&source=canvas&limit=50`,
|
||||
`/workspaces/${workspaceId}/activity?${params.toString()}`,
|
||||
);
|
||||
|
||||
const messages: ChatMessage[] = [];
|
||||
@@ -142,11 +171,12 @@ async function loadMessagesFromDB(workspaceId: string): Promise<{ messages: Chat
|
||||
for (const a of [...activities].reverse()) {
|
||||
messages.push(...activityRowToMessages(a, isInternalSelfMessage));
|
||||
}
|
||||
return { messages, error: null };
|
||||
return { messages, error: null, reachedEnd: activities.length < limit };
|
||||
} catch (err) {
|
||||
return {
|
||||
messages: [],
|
||||
error: err instanceof Error ? err.message : "Failed to load chat history",
|
||||
reachedEnd: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -256,6 +286,60 @@ function MyChatPanel({ workspaceId, data }: Props) {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [confirmRestart, setConfirmRestart] = useState(false);
|
||||
const bottomRef = useRef<HTMLDivElement>(null);
|
||||
// First-mount scroll-to-bottom needs `behavior: "instant"` — long
|
||||
// conversations smooth-animate for ~300ms which any concurrent
|
||||
// re-render can interrupt, leaving the user stuck mid-conversation
|
||||
// when the chat tab opens. Subsequent appends (new agent messages)
|
||||
// keep `smooth` for the visual "landing" feel. Flipped the first
|
||||
// time messages.length goes positive, so a workspace switch (which
|
||||
// remounts ChatTab) gets a fresh instant jump too.
|
||||
const hasInitialScrollRef = useRef(false);
|
||||
// Lazy-load older history on scroll-up.
|
||||
// - containerRef = the scrollable messages viewport
|
||||
// - topRef = sentinel above the messages list; IO observes it
|
||||
// and triggers loadOlder() when it enters view
|
||||
// - hasMore = false once a fetch returns < limit rows; stops IO
|
||||
// - loadingOlder = drives the "Loading older messages…" UI label
|
||||
// - inflightRef = synchronous guard against double-entry of loadOlder
|
||||
// when the IO callback fires twice in the same
|
||||
// microtask (state-based guard would be stale until
|
||||
// the next React commit)
|
||||
// - scrollAnchorRef = saves distance-from-bottom before a prepend
|
||||
// so the useLayoutEffect below can restore the
|
||||
// user's exact viewport position. Without this,
|
||||
// prepending older messages would jump the scroll
|
||||
// position by the height of the new content.
|
||||
// - oldestMessageRef / hasMoreRef = let the loadOlder closure read
|
||||
// the latest values without taking them as deps —
|
||||
// every live agent push mutates `messages`, and
|
||||
// having loadOlder depend on `messages` would tear
|
||||
// down + re-arm the IntersectionObserver on every
|
||||
// push. Refs decouple the observer lifecycle from
|
||||
// message-list updates.
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const topRef = useRef<HTMLDivElement>(null);
|
||||
const [hasMore, setHasMore] = useState(true);
|
||||
const [loadingOlder, setLoadingOlder] = useState(false);
|
||||
const inflightRef = useRef(false);
|
||||
// The scroll anchor includes the first-message id as it was BEFORE
|
||||
// the prepend — see useLayoutEffect below for why. Without this tag,
|
||||
// a live agent push that appends WHILE loadOlder is in flight would
|
||||
// run useLayoutEffect against the append (anchor still set), the
|
||||
// "restore" math would scroll the user to a stale offset, AND the
|
||||
// append's normal scroll-to-bottom would be swallowed.
|
||||
const scrollAnchorRef = useRef<
|
||||
{ savedDistanceFromBottom: number; expectFirstIdNotEqual: string | null } | null
|
||||
>(null);
|
||||
const oldestMessageRef = useRef<ChatMessage | null>(null);
|
||||
const hasMoreRef = useRef(true);
|
||||
// Monotonic token bumped on workspace switch + on every loadOlder
|
||||
// entry. Each fetch's .then() captures its own token; if the token
|
||||
// has moved, the resolved messages belong to a stale workspace or a
|
||||
// superseded fetch and we silently drop them. Without this guard, a
|
||||
// workspace switch mid-fetch would have the in-flight promise
|
||||
// resolve into the new workspace's setMessages — the user sees
|
||||
// someone else's history briefly.
|
||||
const fetchTokenRef = useRef(0);
|
||||
// Files the user has picked but not yet sent. Cleared on send
|
||||
// (upload success) or by the × on each pill.
|
||||
const [pendingFiles, setPendingFiles] = useState<File[]>([]);
|
||||
@@ -294,17 +378,144 @@ function MyChatPanel({ workspaceId, data }: Props) {
|
||||
sendInFlightRef.current = false;
|
||||
}, []);
|
||||
|
||||
// Load chat history from database on mount
|
||||
useEffect(() => {
|
||||
// Initial-load fetch — used by the mount effect and the "Retry"
|
||||
// button below. Single source of truth so the two paths can't drift
|
||||
// (e.g. INITIAL_HISTORY_LIMIT bumped in the effect but not the
|
||||
// retry, leading to inconsistent first-paint sizes).
|
||||
const loadInitial = useCallback(() => {
|
||||
setLoading(true);
|
||||
setLoadError(null);
|
||||
loadMessagesFromDB(workspaceId).then(({ messages: msgs, error: fetchErr }) => {
|
||||
setMessages(msgs);
|
||||
setLoadError(fetchErr);
|
||||
setLoading(false);
|
||||
});
|
||||
setHasMore(true);
|
||||
// Bump the token; any in-flight fetch from the previous workspace
|
||||
// (or a previous retry) will see token != myToken in its .then()
|
||||
// and silently bail — the late response can't clobber the new
|
||||
// workspace's state.
|
||||
fetchTokenRef.current += 1;
|
||||
const myToken = fetchTokenRef.current;
|
||||
loadMessagesFromDB(workspaceId, INITIAL_HISTORY_LIMIT).then(
|
||||
({ messages: msgs, error: fetchErr, reachedEnd }) => {
|
||||
if (fetchTokenRef.current !== myToken) return;
|
||||
setMessages(msgs);
|
||||
setLoadError(fetchErr);
|
||||
setHasMore(!reachedEnd);
|
||||
setLoading(false);
|
||||
},
|
||||
);
|
||||
}, [workspaceId]);
|
||||
|
||||
// Load chat history on mount / workspace switch.
|
||||
// Initial load is bounded to INITIAL_HISTORY_LIMIT (newest 10) — the
|
||||
// rest streams in as the user scrolls up via loadOlder() below. Pre-
|
||||
// 2026-05-05 this fetched the newest 50 in one shot; on a long-running
|
||||
// workspace that meant 50× message-bubble paint + DOM cost on every
|
||||
// tab-open even when the user only wanted to read the last few.
|
||||
useEffect(() => {
|
||||
loadInitial();
|
||||
}, [loadInitial]);
|
||||
|
||||
// Mirror the latest oldest-message + hasMore into refs so loadOlder
|
||||
// can read them without taking `messages` as a dep. Every live push
|
||||
// through agentMessages would otherwise recreate loadOlder and tear
|
||||
// down the IO observer.
|
||||
useEffect(() => {
|
||||
oldestMessageRef.current = messages[0] ?? null;
|
||||
}, [messages]);
|
||||
useEffect(() => {
|
||||
hasMoreRef.current = hasMore;
|
||||
}, [hasMore]);
|
||||
|
||||
// Fetch the next-older batch and prepend. Stable identity (deps =
|
||||
// [workspaceId]) so the IntersectionObserver effect below doesn't
|
||||
// re-arm on every messages update.
|
||||
const loadOlder = useCallback(async () => {
|
||||
// inflightRef is the load-bearing guard — synchronous, set BEFORE
|
||||
// any await, so two IO callbacks dispatched in the same microtask
|
||||
// can't both pass. The state checks are defensive secondary
|
||||
// gates for the slow-scroll case.
|
||||
if (inflightRef.current || !hasMoreRef.current) return;
|
||||
const oldest = oldestMessageRef.current;
|
||||
if (!oldest) return;
|
||||
const container = containerRef.current;
|
||||
if (!container) return;
|
||||
inflightRef.current = true;
|
||||
// Capture the user's distance-from-bottom BEFORE we prepend so the
|
||||
// useLayoutEffect can restore it after the new DOM lands. The
|
||||
// expectFirstIdNotEqual tag is what the layout effect checks
|
||||
// against `messages[0].id` to disambiguate prepend (id changed) vs
|
||||
// append (id unchanged → live message landed mid-fetch). Without
|
||||
// it, an agent push during loadOlder runs the "restore" against a
|
||||
// stale anchor — user gets yanked + the append's bottom-pin is
|
||||
// swallowed.
|
||||
scrollAnchorRef.current = {
|
||||
savedDistanceFromBottom: container.scrollHeight - container.scrollTop,
|
||||
expectFirstIdNotEqual: oldest.id,
|
||||
};
|
||||
fetchTokenRef.current += 1;
|
||||
const myToken = fetchTokenRef.current;
|
||||
setLoadingOlder(true);
|
||||
try {
|
||||
const { messages: older, reachedEnd } = await loadMessagesFromDB(
|
||||
workspaceId,
|
||||
OLDER_HISTORY_BATCH,
|
||||
oldest.timestamp,
|
||||
);
|
||||
// Workspace switched (or another loadOlder bumped the token)
|
||||
// mid-fetch — drop these results, they belong to a stale tab.
|
||||
if (fetchTokenRef.current !== myToken) {
|
||||
scrollAnchorRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (older.length > 0) {
|
||||
setMessages((prev) => [...older, ...prev]);
|
||||
} else {
|
||||
// Nothing came back — clear the anchor so the next paint doesn't
|
||||
// try to "restore" against a no-op prepend.
|
||||
scrollAnchorRef.current = null;
|
||||
}
|
||||
setHasMore(!reachedEnd);
|
||||
} finally {
|
||||
setLoadingOlder(false);
|
||||
inflightRef.current = false;
|
||||
}
|
||||
}, [workspaceId]);
|
||||
|
||||
// IntersectionObserver on the top sentinel. Fires loadOlder() the
|
||||
// moment the user scrolls within 200px of the top. AbortController
|
||||
// unwires cleanly on workspace switch / unmount; root is the
|
||||
// scrollable container so we observe only what's visible inside it.
|
||||
//
|
||||
// Dependencies:
|
||||
// - loadOlder — stable per workspaceId (refs decouple it from
|
||||
// message updates), so this dep is here for the
|
||||
// workspace-switch case only
|
||||
// - hasMore — re-run when older history runs out so we
|
||||
// disconnect cleanly
|
||||
// - hasMessages — load-bearing: the sentinel JSX is gated on
|
||||
// `messages.length > 0`, so topRef.current is null
|
||||
// on the empty-messages render. We re-arm exactly
|
||||
// once when messages first land. NOT depending on
|
||||
// `messages.length` (or `messages`) directly so
|
||||
// each subsequent message append doesn't tear down
|
||||
// + re-arm the observer.
|
||||
const hasMessages = messages.length > 0;
|
||||
useEffect(() => {
|
||||
const top = topRef.current;
|
||||
const container = containerRef.current;
|
||||
if (!top || !container) return;
|
||||
if (!hasMore) return; // stop observing when no older history exists
|
||||
const ac = new AbortController();
|
||||
const io = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (ac.signal.aborted) return;
|
||||
if (entries[0]?.isIntersecting) loadOlder();
|
||||
},
|
||||
{ root: container, rootMargin: "200px 0px 0px 0px", threshold: 0 },
|
||||
);
|
||||
io.observe(top);
|
||||
ac.signal.addEventListener("abort", () => io.disconnect());
|
||||
return () => ac.abort();
|
||||
}, [loadOlder, hasMore, hasMessages]);
|
||||
|
||||
// Agent reachability
|
||||
useEffect(() => {
|
||||
const reachable = data.status === "online" || data.status === "degraded";
|
||||
@@ -316,7 +527,41 @@ function MyChatPanel({ workspaceId, data }: Props) {
|
||||
currentTaskRef.current = data.currentTask;
|
||||
}, [data.currentTask]);
|
||||
|
||||
useEffect(() => {
|
||||
// Scroll behavior across messages updates:
|
||||
// - Prepend (loadOlder landed) → restore the user's saved
|
||||
// distance-from-bottom so their reading position is unchanged.
|
||||
// - Append / initial → pin to latest bubble.
|
||||
// useLayoutEffect (not useEffect) so scroll restoration runs BEFORE
|
||||
// paint — otherwise the user sees the page jump for one frame.
|
||||
useLayoutEffect(() => {
|
||||
const container = containerRef.current;
|
||||
const anchor = scrollAnchorRef.current;
|
||||
// Only honor the anchor when this messages-update is the prepend
|
||||
// we expected. messages[0].id is the test:
|
||||
// - prepend → messages[0] is one of the older rows → id !== expectFirstIdNotEqual
|
||||
// - append → messages[0] unchanged → id === expectFirstIdNotEqual → fall through
|
||||
// Without this check, an agent push that lands mid-loadOlder would
|
||||
// run the restore against the append's update, yank the user's
|
||||
// scroll, AND swallow the append's bottom-pin.
|
||||
if (
|
||||
anchor &&
|
||||
container &&
|
||||
messages.length > 0 &&
|
||||
messages[0].id !== anchor.expectFirstIdNotEqual
|
||||
) {
|
||||
container.scrollTop = container.scrollHeight - anchor.savedDistanceFromBottom;
|
||||
scrollAnchorRef.current = null;
|
||||
return;
|
||||
}
|
||||
// Instant on first arrival of messages — smooth-scroll on a long
|
||||
// conversation gets interrupted by concurrent renders and leaves
|
||||
// the user stuck in the middle. After the first jump, subsequent
|
||||
// appends animate as before.
|
||||
if (!hasInitialScrollRef.current && messages.length > 0) {
|
||||
hasInitialScrollRef.current = true;
|
||||
bottomRef.current?.scrollIntoView({ behavior: "instant" as ScrollBehavior });
|
||||
return;
|
||||
}
|
||||
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||
}, [messages]);
|
||||
|
||||
@@ -735,7 +980,7 @@ function MyChatPanel({ workspaceId, data }: Props) {
|
||||
</div>
|
||||
)}
|
||||
{/* Messages */}
|
||||
<div className="flex-1 overflow-y-auto p-3 space-y-3">
|
||||
<div ref={containerRef} className="flex-1 overflow-y-auto p-3 space-y-3">
|
||||
{loading && (
|
||||
<div className="text-xs text-ink-soft text-center py-4">Loading chat history...</div>
|
||||
)}
|
||||
@@ -748,15 +993,7 @@ function MyChatPanel({ workspaceId, data }: Props) {
|
||||
Failed to load chat history: {loadError}
|
||||
</p>
|
||||
<button
|
||||
onClick={() => {
|
||||
setLoading(true);
|
||||
setLoadError(null);
|
||||
loadMessagesFromDB(workspaceId).then(({ messages: msgs, error: fetchErr }) => {
|
||||
setMessages(msgs);
|
||||
setLoadError(fetchErr);
|
||||
setLoading(false);
|
||||
});
|
||||
}}
|
||||
onClick={loadInitial}
|
||||
className="text-[10px] px-2 py-0.5 rounded bg-red-800/40 text-bad hover:bg-red-700/50 transition-colors"
|
||||
>
|
||||
Retry
|
||||
@@ -768,6 +1005,24 @@ function MyChatPanel({ workspaceId, data }: Props) {
|
||||
No messages yet. Send a message to start chatting with this agent.
|
||||
</div>
|
||||
)}
|
||||
{/* Top sentinel for lazy-loading older history. The IO observer
|
||||
in the effect above watches this; entering view triggers the
|
||||
next-older batch fetch. Sits ABOVE messages.map so it's the
|
||||
first thing the user reaches when scrolling up.
|
||||
|
||||
Only mounted when there might be more history (hasMore) so a
|
||||
short conversation doesn't pay an idle observer. The
|
||||
"Loading older messages…" line replaces the sentinel during
|
||||
the fetch so the user sees feedback for the scroll-up
|
||||
gesture. Once we hit the end, we drop the sentinel entirely
|
||||
instead of showing a "no more messages" footer — the user's
|
||||
scroll resting against the top of the conversation IS the
|
||||
signal. */}
|
||||
{hasMore && messages.length > 0 && (
|
||||
<div ref={topRef} className="text-xs text-ink-soft text-center py-1">
|
||||
{loadingOlder ? "Loading older messages…" : " "}
|
||||
</div>
|
||||
)}
|
||||
{messages.map((msg) => (
|
||||
<div key={msg.id} className={`flex ${msg.role === "user" ? "justify-end" : "justify-start"}`}>
|
||||
<div
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useCanvasStore } from "@/store/canvas";
|
||||
import { type ConfigData, DEFAULT_CONFIG, TextInput, NumberInput, Toggle, TagList, Section } from "./config/form-inputs";
|
||||
import { parseYaml, toYaml } from "./config/yaml-utils";
|
||||
import { SecretsSection } from "./config/secrets-section";
|
||||
import { ExternalConnectionSection } from "./ExternalConnectionSection";
|
||||
import {
|
||||
ProviderModelSelector,
|
||||
buildProviderCatalog,
|
||||
@@ -886,11 +887,24 @@ export function ConfigTab({ workspaceId }: Props) {
|
||||
</Section>
|
||||
)}
|
||||
|
||||
<Section title="Skills & Tools" defaultOpen={false}>
|
||||
<TagList label="Skills" values={config.skills || []} onChange={(v) => update("skills", v)} placeholder="e.g. code-review" />
|
||||
<TagList label="Tools" values={config.tools || []} onChange={(v) => update("tools", v)} placeholder="e.g. web_search, filesystem" />
|
||||
<TagList label="Prompt Files" values={config.prompt_files || []} onChange={(v) => update("prompt_files", v)} placeholder="e.g. system-prompt.md" />
|
||||
<TagList label="Shared Context" values={config.shared_context || []} onChange={(v) => update("shared_context", v)} placeholder="e.g. architecture.md" />
|
||||
{/* Skills + Tools used to live here as TagList inputs. They were
|
||||
redundant with their dedicated tabs:
|
||||
- Skills → managed via SkillsTab (per-workspace skill folders)
|
||||
- Tools → managed via the Plugins tab (install/uninstall)
|
||||
Editing them here only set the config.yaml field; the
|
||||
actual install/load happened elsewhere. Removed to stop
|
||||
showing the misnamed list-input affordance. */}
|
||||
|
||||
<Section title="Prompt Files" defaultOpen={false}>
|
||||
<p className="text-[10px] text-ink-soft px-1 pb-1">
|
||||
Markdown files that compose this workspace's system prompt.
|
||||
Loaded in order at boot from the workspace config dir
|
||||
(e.g. <code className="font-mono">system-prompt.md</code>,{' '}
|
||||
<code className="font-mono">CLAUDE.md</code>,{' '}
|
||||
<code className="font-mono">AGENTS.md</code>). Edit the file
|
||||
contents directly via the Files tab.
|
||||
</p>
|
||||
<TagList label="Files (load order)" values={config.prompt_files || []} onChange={(v) => update("prompt_files", v)} placeholder="e.g. system-prompt.md" />
|
||||
</Section>
|
||||
|
||||
<Section title="A2A Protocol" defaultOpen={false}>
|
||||
@@ -947,6 +961,9 @@ export function ConfigTab({ workspaceId }: Props) {
|
||||
: "This runtime manages its own config outside the platform template."}
|
||||
</div>
|
||||
)}
|
||||
{!error && config.runtime === "external" && (
|
||||
<ExternalConnectionSection workspaceId={workspaceId} />
|
||||
)}
|
||||
{success && (
|
||||
<div className="mx-3 mb-2 px-3 py-1.5 bg-green-900/30 border border-green-800 rounded text-xs text-good">Saved</div>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
'use client';
|
||||
|
||||
// ExternalConnectionSection — credential lifecycle controls for runtime=external
|
||||
// workspaces. Surfaced inside ConfigTab when the workspace's runtime is
|
||||
// "external"; ignored for hermes/claude-code/etc. (those have their own
|
||||
// restart-mints-token path).
|
||||
//
|
||||
// Two affordances:
|
||||
//
|
||||
// 1. "Show connection info" (read-only)
|
||||
// Fetches GET /workspaces/:id/external/connection. Returns the
|
||||
// connect block (PLATFORM_URL, WORKSPACE_ID, all 7 snippets) WITH
|
||||
// auth_token="". The modal masks the token field and labels it
|
||||
// "rotate to reveal a new token — current token is unrecoverable".
|
||||
//
|
||||
// 2. "Rotate credentials" (destructive)
|
||||
// POST /workspaces/:id/external/rotate. Revokes any prior live
|
||||
// tokens, mints a fresh one, returns the same connect block with
|
||||
// auth_token populated. Old credentials stop working IMMEDIATELY —
|
||||
// the previously-paired agent will fail auth on its next heartbeat.
|
||||
// Confirm dialog explains this before firing.
|
||||
//
|
||||
// Reuses the existing ExternalConnectModal so the snippet UX is the
|
||||
// same as on Create — operators don't have to learn a second modal.
|
||||
|
||||
import { useState } from "react";
|
||||
import * as Dialog from "@radix-ui/react-dialog";
|
||||
|
||||
import { api } from "@/lib/api";
|
||||
import {
|
||||
ExternalConnectModal,
|
||||
type ExternalConnectionInfo,
|
||||
} from "../ExternalConnectModal";
|
||||
|
||||
interface Props {
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
export function ExternalConnectionSection({ workspaceId }: Props) {
|
||||
const [info, setInfo] = useState<ExternalConnectionInfo | null>(null);
|
||||
const [busy, setBusy] = useState<"show" | "rotate" | null>(null);
|
||||
const [confirmRotate, setConfirmRotate] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
async function showConnection() {
|
||||
setError(null);
|
||||
setBusy("show");
|
||||
try {
|
||||
const resp = await api.get<{ connection: ExternalConnectionInfo }>(
|
||||
`/workspaces/${workspaceId}/external/connection`,
|
||||
);
|
||||
setInfo(resp.connection);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
|
||||
async function doRotate() {
|
||||
setError(null);
|
||||
setBusy("rotate");
|
||||
setConfirmRotate(false);
|
||||
try {
|
||||
const resp = await api.post<{ connection: ExternalConnectionInfo }>(
|
||||
`/workspaces/${workspaceId}/external/rotate`,
|
||||
{},
|
||||
);
|
||||
setInfo(resp.connection);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-3 mt-3 p-3 bg-surface-sunken/50 border border-line rounded">
|
||||
<h3 className="text-xs text-ink-mid font-medium mb-1">External Connection</h3>
|
||||
<p className="text-[10px] text-ink-soft mb-2">
|
||||
This workspace runs an external agent. Use these controls to
|
||||
re-show the setup snippets or rotate the workspace token.
|
||||
</p>
|
||||
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
<button
|
||||
type="button"
|
||||
onClick={showConnection}
|
||||
disabled={busy !== null}
|
||||
className="px-3 py-1.5 bg-surface-card hover:bg-surface-card text-xs rounded text-ink-mid disabled:opacity-30 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-accent/60"
|
||||
>
|
||||
{busy === "show" ? "Loading…" : "Show connection info"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setConfirmRotate(true)}
|
||||
disabled={busy !== null}
|
||||
className="px-3 py-1.5 bg-red-900/30 hover:bg-red-900/50 border border-red-800/60 text-xs rounded text-bad disabled:opacity-30 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-red-600/60"
|
||||
>
|
||||
{busy === "rotate" ? "Rotating…" : "Rotate credentials"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mt-2 px-2 py-1 bg-red-900/30 border border-red-800 rounded text-[10px] text-bad">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Dialog.Root open={confirmRotate} onOpenChange={setConfirmRotate}>
|
||||
<Dialog.Portal>
|
||||
<Dialog.Overlay className="fixed inset-0 bg-black/60 z-50" />
|
||||
<Dialog.Content className="fixed left-1/2 top-1/2 z-50 w-[min(440px,92vw)] -translate-x-1/2 -translate-y-1/2 rounded-xl bg-surface-sunken border border-line p-5 shadow-2xl">
|
||||
<Dialog.Title className="text-sm font-medium text-ink mb-2">
|
||||
Rotate workspace credentials?
|
||||
</Dialog.Title>
|
||||
<Dialog.Description className="text-xs text-ink-mid mb-4 leading-relaxed">
|
||||
This will mint a new <code className="font-mono">workspace_auth_token</code> and{' '}
|
||||
<strong>immediately invalidate the current one</strong>. Your external
|
||||
agent will start failing authentication on its next heartbeat
|
||||
until you redeploy it with the new token.
|
||||
</Dialog.Description>
|
||||
<div className="flex justify-end gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setConfirmRotate(false)}
|
||||
className="px-3 py-1.5 bg-surface-card text-xs rounded text-ink-mid"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={doRotate}
|
||||
className="px-3 py-1.5 bg-red-700 hover:bg-red-600 text-xs rounded text-white"
|
||||
>
|
||||
Rotate
|
||||
</button>
|
||||
</div>
|
||||
</Dialog.Content>
|
||||
</Dialog.Portal>
|
||||
</Dialog.Root>
|
||||
|
||||
<ExternalConnectModal info={info} onClose={() => setInfo(null)} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -10,6 +10,7 @@ interface Props {
|
||||
interface MemoryEntry {
|
||||
key: string;
|
||||
value: unknown;
|
||||
version?: number;
|
||||
expires_at: string | null;
|
||||
updated_at: string;
|
||||
}
|
||||
@@ -28,6 +29,10 @@ export function MemoryTab({ workspaceId }: Props) {
|
||||
const [newValue, setNewValue] = useState("");
|
||||
const [newTTL, setNewTTL] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [editingKey, setEditingKey] = useState<string | null>(null);
|
||||
const [editValue, setEditValue] = useState("");
|
||||
const [editTTL, setEditTTL] = useState("");
|
||||
const [editError, setEditError] = useState<string | null>(null);
|
||||
|
||||
const awarenessUrl = useMemo(() => {
|
||||
try {
|
||||
@@ -109,6 +114,69 @@ export function MemoryTab({ workspaceId }: Props) {
|
||||
}
|
||||
};
|
||||
|
||||
const beginEdit = (entry: MemoryEntry) => {
|
||||
setEditError(null);
|
||||
setEditingKey(entry.key);
|
||||
// Stringify objects/arrays as pretty JSON; render plain strings raw so the
|
||||
// editor doesn't surprise users with surrounding quotes.
|
||||
setEditValue(
|
||||
typeof entry.value === "string"
|
||||
? entry.value
|
||||
: JSON.stringify(entry.value, null, 2),
|
||||
);
|
||||
if (entry.expires_at) {
|
||||
const remainingMs = new Date(entry.expires_at).getTime() - Date.now();
|
||||
const ttl = Math.max(0, Math.floor(remainingMs / 1000));
|
||||
setEditTTL(ttl > 0 ? String(ttl) : "");
|
||||
} else {
|
||||
setEditTTL("");
|
||||
}
|
||||
};
|
||||
|
||||
const cancelEdit = () => {
|
||||
setEditingKey(null);
|
||||
setEditValue("");
|
||||
setEditTTL("");
|
||||
setEditError(null);
|
||||
};
|
||||
|
||||
const handleEditSave = async (entry: MemoryEntry) => {
|
||||
setEditError(null);
|
||||
|
||||
let parsedValue: unknown;
|
||||
try {
|
||||
parsedValue = JSON.parse(editValue);
|
||||
} catch {
|
||||
parsedValue = editValue;
|
||||
}
|
||||
|
||||
// if_match_version closes the silent-overwrite hole when two writers
|
||||
// race. The handler returns 409 with the current version on mismatch
|
||||
// — surface that as a retry hint and reload to pick up the new state.
|
||||
const body: Record<string, unknown> = { key: entry.key, value: parsedValue };
|
||||
if (typeof entry.version === "number") {
|
||||
body.if_match_version = entry.version;
|
||||
}
|
||||
if (editTTL) {
|
||||
const ttl = parseInt(editTTL);
|
||||
if (!Number.isNaN(ttl) && ttl > 0) body.ttl_seconds = ttl;
|
||||
}
|
||||
|
||||
try {
|
||||
await api.post(`/workspaces/${workspaceId}/memory`, body);
|
||||
cancelEdit();
|
||||
loadMemory();
|
||||
} catch (e) {
|
||||
const message = e instanceof Error ? e.message : "Failed to save";
|
||||
if (message.includes("409") || /if_match_version mismatch/i.test(message)) {
|
||||
setEditError("This entry changed since you opened it. Reloading.");
|
||||
loadMemory();
|
||||
} else {
|
||||
setEditError(message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const openAwareness = () => {
|
||||
window.open(awarenessUrl, "_blank", "noopener,noreferrer");
|
||||
};
|
||||
@@ -308,24 +376,71 @@ export function MemoryTab({ workspaceId }: Props) {
|
||||
|
||||
{expanded === entry.key && (
|
||||
<div className="px-3 pb-2 space-y-2">
|
||||
<pre className="text-[10px] text-ink-mid bg-surface-sunken rounded p-2 overflow-x-auto max-h-40">
|
||||
{JSON.stringify(entry.value, null, 2)}
|
||||
</pre>
|
||||
{editingKey === entry.key ? (
|
||||
<div className="space-y-2">
|
||||
<textarea
|
||||
value={editValue}
|
||||
onChange={(e) => setEditValue(e.target.value)}
|
||||
rows={4}
|
||||
aria-label={`Edit value for ${entry.key}`}
|
||||
className="w-full bg-surface-sunken border border-line rounded px-2 py-1 text-xs font-mono text-ink focus:outline-none focus:border-accent resize-none"
|
||||
/>
|
||||
<input
|
||||
value={editTTL}
|
||||
onChange={(e) => setEditTTL(e.target.value)}
|
||||
placeholder="TTL in seconds (blank = no expiry)"
|
||||
aria-label={`Edit TTL for ${entry.key}`}
|
||||
className="w-full bg-surface-sunken border border-line rounded px-2 py-1 text-xs text-ink focus:outline-none focus:border-accent"
|
||||
/>
|
||||
{editError && (
|
||||
<div role="alert" className="text-[10px] text-bad">
|
||||
{editError}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleEditSave(entry)}
|
||||
className="px-3 py-1 bg-accent hover:bg-accent-strong text-xs rounded text-white"
|
||||
>
|
||||
Save
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={cancelEdit}
|
||||
className="px-3 py-1 bg-surface-card hover:bg-surface-elevated text-xs rounded text-ink-mid"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<pre className="text-[10px] text-ink-mid bg-surface-sunken rounded p-2 overflow-x-auto max-h-40">
|
||||
{JSON.stringify(entry.value, null, 2)}
|
||||
</pre>
|
||||
)}
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-[9px] text-ink-soft">
|
||||
Updated: {new Date(entry.updated_at).toLocaleString()}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDelete(entry.key)}
|
||||
// hover:text-bad on top of text-bad was a no-op.
|
||||
// Switch to a hover bg + focus-visible ring so
|
||||
// the destructive button visibly responds and
|
||||
// keyboard users see focus.
|
||||
className="text-[10px] text-bad hover:bg-red-950/40 rounded px-1 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-red-500/60"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
<div className="flex items-center gap-2">
|
||||
{editingKey !== entry.key && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => beginEdit(entry)}
|
||||
className="text-[10px] text-ink-mid hover:bg-surface-elevated rounded px-1 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-accent/60"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDelete(entry.key)}
|
||||
className="text-[10px] text-bad hover:bg-red-950/40 rounded px-1 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-red-500/60"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -1,16 +1,105 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState, useCallback } from "react";
|
||||
import type { WorkspaceNodeData } from "@/store/canvas";
|
||||
|
||||
interface Props {
|
||||
workspaceId: string;
|
||||
/** Workspace metadata from the canvas store. Optional for back-compat
|
||||
* with any caller that still mounts <TerminalTab workspaceId=... />
|
||||
* without threading data through (e.g. tests). When present, the
|
||||
* runtime field gates the early-return below. */
|
||||
data?: WorkspaceNodeData;
|
||||
}
|
||||
|
||||
import { deriveWsBaseUrl } from "@/lib/ws-url";
|
||||
|
||||
const WS_URL = deriveWsBaseUrl();
|
||||
|
||||
export function TerminalTab({ workspaceId }: Props) {
|
||||
/**
|
||||
* NotAvailablePanel — full-tab placeholder with a big terminal-off icon
|
||||
* for runtimes that don't expose a TTY (e.g. external workspaces, where
|
||||
* the platform doesn't own the process). Pre-fix the tab tried to open
|
||||
* a WebSocket against /ws/terminal/<id> for these workspaces, the server
|
||||
* 404'd, and the user saw "Connection failed" — which reads as a bug,
|
||||
* not as "this runtime intentionally has no shell". This banner makes
|
||||
* the absence intentional.
|
||||
*/
|
||||
function NotAvailablePanel({ runtime }: { runtime: string }) {
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center h-full p-8 text-center bg-surface-sunken/30">
|
||||
{/* Big terminal-off icon — bracket "[_]" with a slash through it.
|
||||
Custom inline SVG so we don't depend on an icon set being
|
||||
present at canvas build-time. */}
|
||||
<svg
|
||||
width="72"
|
||||
height="72"
|
||||
viewBox="0 0 72 72"
|
||||
fill="none"
|
||||
aria-hidden="true"
|
||||
className="text-ink-soft mb-4"
|
||||
>
|
||||
<rect
|
||||
x="10"
|
||||
y="14"
|
||||
width="52"
|
||||
height="44"
|
||||
rx="4"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
fill="none"
|
||||
opacity="0.6"
|
||||
/>
|
||||
<path
|
||||
d="M22 30 L30 36 L22 42"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
opacity="0.7"
|
||||
/>
|
||||
<path
|
||||
d="M34 44 L44 44"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
opacity="0.7"
|
||||
/>
|
||||
{/* Diagonal cancel slash */}
|
||||
<path
|
||||
d="M14 14 L58 58"
|
||||
stroke="currentColor"
|
||||
strokeWidth="3"
|
||||
strokeLinecap="round"
|
||||
/>
|
||||
</svg>
|
||||
<h3 className="text-sm font-medium text-ink mb-1.5">Terminal not available</h3>
|
||||
<p className="text-[11px] text-ink-soft max-w-xs leading-relaxed">
|
||||
This workspace runs the{" "}
|
||||
<span className="font-mono text-ink-mid">{runtime}</span> runtime,
|
||||
which doesn't expose a shell. Use the Chat tab to interact with the
|
||||
agent directly.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Runtimes that don't expose a TTY. Keep narrow — only add a runtime
|
||||
* here when its provisioner genuinely has no shell endpoint, otherwise
|
||||
* the user loses access to a real debugging surface. */
|
||||
const RUNTIMES_WITHOUT_TERMINAL = new Set(["external"]);
|
||||
|
||||
export function TerminalTab({ workspaceId, data }: Props) {
|
||||
// Early-return for runtimes that have no shell. Skips the entire
|
||||
// xterm + WebSocket dance below — without this, mounting the tab
|
||||
// for an external workspace pops the WS, gets a 404 from the
|
||||
// workspace-server (no /ws/terminal/<id> route registered for it),
|
||||
// and shows "Connection failed" with a Reconnect button — confusing
|
||||
// because the workspace IS healthy, just doesn't have a TTY.
|
||||
if (data && RUNTIMES_WITHOUT_TERMINAL.has(data.runtime)) {
|
||||
return <NotAvailablePanel runtime={data.runtime} />;
|
||||
}
|
||||
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const termRef = useRef<{ dispose: () => void } | null>(null);
|
||||
const wsRef = useRef<WebSocket | null>(null);
|
||||
|
||||
@@ -0,0 +1,340 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// Pins the lazy-loading chat-history pagination added 2026-05-05.
|
||||
//
|
||||
// Pre-fix: ChatTab fetched the newest 50 messages on every mount and
|
||||
// scrolled to bottom, paying full DOM cost up-front even when the user
|
||||
// only wanted to read the last few bubbles. Post-fix: initial load is
|
||||
// bounded to 10 newest, and an IntersectionObserver on a top sentinel
|
||||
// triggers loadOlder() (batch of 20 with `before_ts` cursor) when the
|
||||
// user scrolls up.
|
||||
//
|
||||
// Pinned branches:
|
||||
// 1. Initial fetch carries `limit=10` and NO before_ts (newest-first
|
||||
// slice). Pre-fix this was limit=50.
|
||||
// 2. Server returning fewer than `limit` rows clears `hasMore` so the
|
||||
// top sentinel is removed and the IO observer disconnects — no
|
||||
// "Loading older messages…" spinner on a short conversation.
|
||||
// 3. Server returning exactly `limit` rows on the first batch keeps
|
||||
// hasMore=true so the sentinel mounts (verified indirectly by
|
||||
// asserting the rendered bubble count matches the full page).
|
||||
// 4. The retry button after a failed initial load uses the same
|
||||
// INITIAL_HISTORY_LIMIT (10), not the legacy 50.
|
||||
//
|
||||
// IntersectionObserver / scroll-anchor restoration is exercised by the
|
||||
// E2E synth-canary suite — pinning it in jsdom would require mocking
|
||||
// the observer and faking layout, which is brittler than trusting a
|
||||
// live-DOM canary against the staging tenant.
|
||||
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
|
||||
import { render, screen, cleanup, waitFor, fireEvent } from "@testing-library/react";
|
||||
import React from "react";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
// Both ChatTab sub-panels (MyChat + AgentComms) mount simultaneously so
|
||||
// keyboard tab order and aria-controls land on a real DOM. Both fire
|
||||
// /activity GETs on mount: MyChat's hits `type=a2a_receive&source=canvas`,
|
||||
// AgentComms's hits a different filter. Route the mock by URL so each
|
||||
// gets a sensible default and only MyChat's call is what the assertions
|
||||
// scrutinise.
|
||||
const myChatActivityCalls: string[] = [];
|
||||
let myChatNextResponse: { ok: true; rows: unknown[] } | { ok: false; err: Error } = {
|
||||
ok: true,
|
||||
rows: [],
|
||||
};
|
||||
const apiGet = vi.fn((path: string): Promise<unknown> => {
|
||||
if (path.includes("type=a2a_receive") && path.includes("source=canvas")) {
|
||||
myChatActivityCalls.push(path);
|
||||
if (myChatNextResponse.ok) return Promise.resolve(myChatNextResponse.rows);
|
||||
return Promise.reject(myChatNextResponse.err);
|
||||
}
|
||||
// AgentComms / heartbeat / anything else — empty array is a safe
|
||||
// default that won't blow up the corresponding component's .then().
|
||||
return Promise.resolve([]);
|
||||
});
|
||||
const apiPost = vi.fn();
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: {
|
||||
get: (path: string) => apiGet(path),
|
||||
post: (path: string, body: unknown) => apiPost(path, body),
|
||||
del: vi.fn(),
|
||||
patch: vi.fn(),
|
||||
put: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/store/canvas", () => ({
|
||||
useCanvasStore: vi.fn((selector?: (s: unknown) => unknown) =>
|
||||
selector ? selector({ agentMessages: {}, consumeAgentMessages: () => [] }) : {},
|
||||
),
|
||||
}));
|
||||
|
||||
// Capture IntersectionObserver instances so tests can drive callbacks
|
||||
// directly (jsdom has no layout, so nothing crosses thresholds on its
|
||||
// own) AND assert observer-instance count to pin the perf invariant
|
||||
// that live-message churn doesn't tear down + re-arm the observer.
|
||||
type IOInstance = {
|
||||
callback: IntersectionObserverCallback;
|
||||
observed: Element[];
|
||||
disconnected: boolean;
|
||||
};
|
||||
const ioInstances: IOInstance[] = [];
|
||||
|
||||
beforeEach(() => {
|
||||
apiGet.mockClear();
|
||||
apiPost.mockReset();
|
||||
myChatActivityCalls.length = 0;
|
||||
myChatNextResponse = { ok: true, rows: [] };
|
||||
ioInstances.length = 0;
|
||||
class FakeIO {
|
||||
private inst: IOInstance;
|
||||
constructor(cb: IntersectionObserverCallback) {
|
||||
this.inst = { callback: cb, observed: [], disconnected: false };
|
||||
ioInstances.push(this.inst);
|
||||
}
|
||||
observe(el: Element) {
|
||||
this.inst.observed.push(el);
|
||||
}
|
||||
unobserve() {}
|
||||
disconnect() {
|
||||
this.inst.disconnected = true;
|
||||
}
|
||||
}
|
||||
// Install on every reachable global — different bundlers / module
|
||||
// graphs can resolve `IntersectionObserver` via `window`, `globalThis`,
|
||||
// or the bare global. Without all three, jsdom's own (pre-existing)
|
||||
// stub silently wins and ioInstances stays empty.
|
||||
(window as unknown as { IntersectionObserver: unknown }).IntersectionObserver = FakeIO;
|
||||
(globalThis as unknown as { IntersectionObserver: unknown }).IntersectionObserver = FakeIO;
|
||||
// jsdom doesn't implement scrollIntoView; ChatTab calls it after every
|
||||
// messages update.
|
||||
Element.prototype.scrollIntoView = vi.fn();
|
||||
});
|
||||
|
||||
function triggerIntersection(instanceIdx = -1) {
|
||||
// -1 → the latest observer (the live one). Tests targeting an old
|
||||
// (disconnected) instance pass a positive index.
|
||||
const inst = ioInstances.at(instanceIdx);
|
||||
if (!inst) throw new Error(`no IO instance at ${instanceIdx}`);
|
||||
inst.callback(
|
||||
[{ isIntersecting: true, target: inst.observed[0] } as IntersectionObserverEntry],
|
||||
inst as unknown as IntersectionObserver,
|
||||
);
|
||||
}
|
||||
|
||||
import { ChatTab } from "../ChatTab";
|
||||
|
||||
function makeActivityRow(seq: number): Record<string, unknown> {
|
||||
// Zero-pad seq into the minute slot so "seq=10" doesn't produce
|
||||
// the invalid timestamp "00:010:00Z" (caught by the loadOlder URL
|
||||
// assertion below — first version of the helper used `0${seq}` and
|
||||
// the test failed on `before_ts` having an extra digit).
|
||||
const mm = String(seq).padStart(2, "0");
|
||||
return {
|
||||
activity_type: "a2a_receive",
|
||||
status: "ok",
|
||||
created_at: `2026-05-05T00:${mm}:00Z`,
|
||||
request_body: { params: { message: { parts: [{ kind: "text", text: `user msg ${seq}` }] } } },
|
||||
response_body: { result: `agent reply ${seq}` },
|
||||
};
|
||||
}
|
||||
|
||||
// Server returns newest-first; the helper builds a server-shape page
|
||||
// so the order in the rendered messages array matches production.
|
||||
function newestFirstPage(start: number, count: number): unknown[] {
|
||||
return Array.from({ length: count }, (_, i) => makeActivityRow(start + count - 1 - i));
|
||||
}
|
||||
|
||||
const minimalData = {
|
||||
status: "online" as const,
|
||||
runtime: "claude-code",
|
||||
currentTask: null,
|
||||
} as unknown as Parameters<typeof ChatTab>[0]["data"];
|
||||
|
||||
describe("ChatTab lazy history pagination", () => {
|
||||
it("initial fetch carries limit=10 (not the legacy 50)", async () => {
|
||||
myChatNextResponse = { ok: true, rows: [makeActivityRow(1)] };
|
||||
render(<ChatTab workspaceId="ws-1" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
const url = myChatActivityCalls[0];
|
||||
expect(url).toContain("limit=10");
|
||||
expect(url).not.toContain("limit=50");
|
||||
// before_ts should NOT be set on the initial fetch — that's the
|
||||
// newest-first slice the user lands on.
|
||||
expect(url).not.toContain("before_ts");
|
||||
});
|
||||
|
||||
it("hides the top sentinel when initial fetch returns fewer than the limit", async () => {
|
||||
// 3 < 10 → server says "no more older history exists"; sentinel
|
||||
// should NOT mount and the "Loading older messages…" line should
|
||||
// never appear (it can't, since the sentinel is what triggers it).
|
||||
myChatNextResponse = {
|
||||
ok: true,
|
||||
rows: [makeActivityRow(1), makeActivityRow(2), makeActivityRow(3)],
|
||||
};
|
||||
render(<ChatTab workspaceId="ws-2" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/Loading chat history/i)).toBeNull();
|
||||
});
|
||||
expect(screen.queryByText(/Loading older messages/i)).toBeNull();
|
||||
});
|
||||
|
||||
it("renders all messages when initial fetch returns exactly the limit", async () => {
|
||||
// 10 == limit → server might have more older rows; sentinel SHOULD
|
||||
// mount so the IO observer can fire loadOlder() on scroll-up. We
|
||||
// verify by checking the rendered bubble count — if hasMore stayed
|
||||
// true the sentinel render path doesn't crash and all 10 rows
|
||||
// produced their pair of bubbles.
|
||||
const fullPage = Array.from({ length: 10 }, (_, i) => makeActivityRow(i + 1));
|
||||
myChatNextResponse = { ok: true, rows: fullPage };
|
||||
render(<ChatTab workspaceId="ws-3" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/Loading chat history/i)).toBeNull();
|
||||
});
|
||||
expect(screen.getAllByText(/user msg/).length).toBe(10);
|
||||
expect(screen.getAllByText(/agent reply/).length).toBe(10);
|
||||
});
|
||||
|
||||
it("retry-after-failure uses limit=10, not the legacy 50", async () => {
|
||||
myChatNextResponse = { ok: false, err: new Error("network down") };
|
||||
render(<ChatTab workspaceId="ws-4" data={minimalData} />);
|
||||
const retry = await screen.findByText(/Retry/);
|
||||
myChatNextResponse = { ok: true, rows: [makeActivityRow(1)] };
|
||||
fireEvent.click(retry);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(2));
|
||||
const retryUrl = myChatActivityCalls[1];
|
||||
expect(retryUrl).toContain("limit=10");
|
||||
expect(retryUrl).not.toContain("limit=50");
|
||||
});
|
||||
|
||||
it("loadOlder fetches limit=20 with before_ts=oldest.timestamp", async () => {
|
||||
// Initial page = 10 rows in newest-first order (seq 10..1). After
|
||||
// the component reverses to oldest-first for display, messages[0]
|
||||
// is built from seq=1 — the oldest — and its timestamp is what
|
||||
// before_ts should carry.
|
||||
myChatNextResponse = { ok: true, rows: newestFirstPage(1, 10) };
|
||||
render(<ChatTab workspaceId="ws-load-older" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
await waitFor(() => expect(ioInstances.length).toBeGreaterThan(0));
|
||||
|
||||
// Stage the older-batch response, then fire the IO callback.
|
||||
myChatNextResponse = { ok: true, rows: newestFirstPage(0, 1) };
|
||||
triggerIntersection();
|
||||
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(2));
|
||||
const olderUrl = myChatActivityCalls[1];
|
||||
expect(olderUrl).toContain("limit=20");
|
||||
expect(olderUrl).toContain("before_ts=");
|
||||
expect(decodeURIComponent(olderUrl)).toContain("before_ts=2026-05-05T00:01:00Z");
|
||||
});
|
||||
|
||||
it("inflight guard rejects a second IO trigger while first loadOlder is in flight", async () => {
|
||||
myChatNextResponse = { ok: true, rows: newestFirstPage(1, 10) };
|
||||
render(<ChatTab workspaceId="ws-inflight" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
await waitFor(() => expect(ioInstances.length).toBeGreaterThan(0));
|
||||
|
||||
// Hold the next loadOlder fetch open with a manual deferred so we
|
||||
// can fire the second trigger while the first is in-flight.
|
||||
let release!: (rows: unknown[]) => void;
|
||||
const deferred = new Promise<unknown[]>((res) => {
|
||||
release = res;
|
||||
});
|
||||
apiGet.mockImplementationOnce((path: string): Promise<unknown> => {
|
||||
myChatActivityCalls.push(path);
|
||||
return deferred;
|
||||
});
|
||||
|
||||
triggerIntersection(); // start loadOlder #1
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(2));
|
||||
|
||||
// Second IO trigger lands while #1 is still pending.
|
||||
triggerIntersection();
|
||||
triggerIntersection();
|
||||
triggerIntersection();
|
||||
// Without the inflight guard, each of these would have started a
|
||||
// new fetch. With the guard, none of them do — call count stays 2.
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
expect(myChatActivityCalls.length).toBe(2);
|
||||
|
||||
// Release the first fetch. Inflight clears in the finally block;
|
||||
// a subsequent IO trigger is permitted again (verified by checking
|
||||
// we can fire a follow-up after release without hanging the test).
|
||||
release([]);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(2));
|
||||
});
|
||||
|
||||
it("empty older response clears the scroll anchor and unmounts the sentinel", async () => {
|
||||
// The bug we're pinning: if loadOlder returns 0 rows, the
|
||||
// scrollAnchorRef must be cleared so the next paint doesn't try to
|
||||
// restore against a no-op prepend (which would fight the natural
|
||||
// bottom-pin for any subsequent live message). hasMore flipping to
|
||||
// false is the same flag-flip path; sentinel disappearing is the
|
||||
// observable proxy.
|
||||
myChatNextResponse = { ok: true, rows: newestFirstPage(1, 10) };
|
||||
render(<ChatTab workspaceId="ws-anchor" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
await waitFor(() => expect(ioInstances.length).toBeGreaterThan(0));
|
||||
|
||||
myChatNextResponse = { ok: true, rows: [] }; // empty → reachedEnd
|
||||
triggerIntersection();
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(2));
|
||||
|
||||
// After reachedEnd the sentinel unmounts (hasMore=false). We can't
|
||||
// peek scrollAnchorRef directly, but we can assert the consequence:
|
||||
// scrollIntoView (the bottom-pin for live appends) is not blocked
|
||||
// by a stale anchor. Trigger a re-render via an unrelated state
|
||||
// change… in practice the safest assertion here is that the
|
||||
// sentinel disappeared (proving the empty response propagated to
|
||||
// hasMore correctly, which is the same flag-flip path as anchor
|
||||
// clearing).
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/Loading older messages/i)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it("IntersectionObserver does not churn when older messages prepend", async () => {
|
||||
// Whole-PR perf invariant: prepending older history (the load-bearing
|
||||
// user gesture) must NOT tear down + re-arm the IO observer.
|
||||
// Triggering loadOlder is the cleanest way to drive a messages
|
||||
// mutation from inside the test, since live agent push goes through
|
||||
// a Zustand store that's harder to drive reliably from jsdom.
|
||||
//
|
||||
// Pre-fix, loadOlder depended on `messages`, so every prepend
|
||||
// recreated loadOlder → re-ran the IO effect → new observer. Each
|
||||
// call to triggerIntersection() produced a fresh disconnected
|
||||
// observer + a new live one. Post-fix, the observer survives.
|
||||
myChatNextResponse = { ok: true, rows: newestFirstPage(1, 10) };
|
||||
render(<ChatTab workspaceId="ws-stable-io" data={minimalData} />);
|
||||
await waitFor(() => expect(myChatActivityCalls.length).toBe(1));
|
||||
await waitFor(() => expect(ioInstances.length).toBeGreaterThan(0));
|
||||
|
||||
// Snapshot the observer instance after first paint stabilises.
|
||||
const observerBefore = ioInstances.at(-1);
|
||||
expect(observerBefore).toBeDefined();
|
||||
expect(observerBefore!.disconnected).toBe(false);
|
||||
|
||||
// Trigger three older-batch prepends. Each batch returns the full
|
||||
// OLDER_HISTORY_BATCH (20 rows) so reachedEnd stays false and the
|
||||
// sentinel keeps mounting. Pre-fix, each prepend mutated `messages`
|
||||
// → recreated loadOlder → re-ran the IO effect → new observer.
|
||||
for (let batch = 0; batch < 3; batch++) {
|
||||
myChatNextResponse = {
|
||||
ok: true,
|
||||
rows: newestFirstPage(-(batch + 1) * 20, 20),
|
||||
};
|
||||
const callsBefore = myChatActivityCalls.length;
|
||||
triggerIntersection();
|
||||
await waitFor(() =>
|
||||
expect(myChatActivityCalls.length).toBe(callsBefore + 1),
|
||||
);
|
||||
}
|
||||
|
||||
// The original observer is still the live one — no churn.
|
||||
expect(observerBefore!.disconnected).toBe(false);
|
||||
expect(ioInstances.at(-1)).toBe(observerBefore);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// Regression tests for the ConfigTab section restructure (user feedback
|
||||
// 2026-05-04: "Skills and Tools are having their own tab as plugin, and
|
||||
// Prompt Files are in the file system which can be directly edited. Am
|
||||
// I missing something?" + "Tools should be merged into plugin then, and
|
||||
// for prompt files... should be in another section than in skill& tools").
|
||||
//
|
||||
// What this pins:
|
||||
// 1. The "Skills & Tools" section title is gone.
|
||||
// 2. Editable Skills + Tools tag inputs are gone (managed elsewhere).
|
||||
// 3. A dedicated "Prompt Files" section exists with explanatory text.
|
||||
//
|
||||
// If a future PR re-adds the Skills/Tools tag inputs to ConfigTab, this
|
||||
// suite catches it.
|
||||
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
|
||||
import { render, screen, cleanup, waitFor, fireEvent } from "@testing-library/react";
|
||||
import React from "react";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
const apiGet = vi.fn();
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: {
|
||||
get: (path: string) => apiGet(path),
|
||||
patch: vi.fn(),
|
||||
put: vi.fn(),
|
||||
post: vi.fn(),
|
||||
del: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
const storeUpdateNodeData = vi.fn();
|
||||
const storeRestartWorkspace = vi.fn();
|
||||
vi.mock("@/store/canvas", () => ({
|
||||
useCanvasStore: Object.assign(
|
||||
(selector: (s: unknown) => unknown) =>
|
||||
selector({ restartWorkspace: storeRestartWorkspace, updateNodeData: storeUpdateNodeData }),
|
||||
{
|
||||
getState: () => ({
|
||||
restartWorkspace: storeRestartWorkspace,
|
||||
updateNodeData: storeUpdateNodeData,
|
||||
}),
|
||||
},
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("../AgentCardSection", () => ({
|
||||
AgentCardSection: () => <div data-testid="agent-card-stub" />,
|
||||
}));
|
||||
|
||||
import { ConfigTab } from "../ConfigTab";
|
||||
|
||||
beforeEach(() => {
|
||||
apiGet.mockReset();
|
||||
apiGet.mockImplementation((path: string) => {
|
||||
if (path === `/workspaces/ws-test`) {
|
||||
return Promise.resolve({ runtime: "claude-code" });
|
||||
}
|
||||
if (path === `/workspaces/ws-test/model`) {
|
||||
return Promise.resolve({ model: "claude-opus-4-7" });
|
||||
}
|
||||
if (path === `/workspaces/ws-test/provider`) {
|
||||
return Promise.resolve({ provider: "anthropic-oauth", source: "default" });
|
||||
}
|
||||
if (path === `/workspaces/ws-test/files/config.yaml`) {
|
||||
return Promise.resolve({ content: "name: test\nruntime: claude-code\n" });
|
||||
}
|
||||
if (path === "/templates") {
|
||||
return Promise.resolve([
|
||||
{ id: "claude-code", name: "Claude Code", runtime: "claude-code", providers: [] },
|
||||
]);
|
||||
}
|
||||
return Promise.reject(new Error(`unmocked api.get: ${path}`));
|
||||
});
|
||||
});
|
||||
|
||||
describe("ConfigTab section restructure", () => {
|
||||
it("does not render a 'Skills & Tools' section title", async () => {
|
||||
render(<ConfigTab workspaceId="ws-test" />);
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalled());
|
||||
// Section button uses the title as its accessible name; should be absent.
|
||||
expect(screen.queryByRole("button", { name: /Skills\s*&\s*Tools/i })).toBeNull();
|
||||
});
|
||||
|
||||
it("does not render an editable Skills tag input", async () => {
|
||||
render(<ConfigTab workspaceId="ws-test" />);
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalled());
|
||||
// TagList renders its label; check no input labelled "Skills" in the form.
|
||||
// (Skills are managed via the dedicated Skills tab.)
|
||||
const skillsLabels = screen
|
||||
.queryAllByText(/^Skills$/)
|
||||
.filter((el) => el.tagName.toLowerCase() === "label");
|
||||
expect(skillsLabels).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("does not render an editable Tools tag input", async () => {
|
||||
render(<ConfigTab workspaceId="ws-test" />);
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalled());
|
||||
// Tools are managed via the Plugins tab — install a plugin → its tools
|
||||
// become available. No reason to type tool names here.
|
||||
const toolsLabels = screen
|
||||
.queryAllByText(/^Tools$/)
|
||||
.filter((el) => el.tagName.toLowerCase() === "label");
|
||||
expect(toolsLabels).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("renders a dedicated 'Prompt Files' section with explanatory copy", async () => {
|
||||
render(<ConfigTab workspaceId="ws-test" />);
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalled());
|
||||
// Section is collapsed by default — find + expand first.
|
||||
const sectionButton = screen.getByRole("button", { name: /Prompt Files/i });
|
||||
expect(sectionButton).toBeTruthy();
|
||||
fireEvent.click(sectionButton);
|
||||
// Explanatory copy mentions system-prompt.md (split across <code> tags
|
||||
// so use textContent on any element rather than the default text matcher).
|
||||
await waitFor(() => {
|
||||
const matches = screen.queryAllByText((_, el) =>
|
||||
(el?.textContent || "").includes("system-prompt.md"),
|
||||
);
|
||||
expect(matches.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// ExternalConnectionSection — coverage for the credential-rotate +
|
||||
// re-show-instructions UI on the Config tab.
|
||||
//
|
||||
// What this pins:
|
||||
// 1. "Show connection info" → GET /external/connection, opens modal
|
||||
// with auth_token=""
|
||||
// 2. "Rotate credentials" → confirm dialog → POST /external/rotate,
|
||||
// opens modal with the returned auth_token
|
||||
// 3. Confirm dialog cancels without firing the POST
|
||||
// 4. API failure surfaces an error chip (no silent loss)
|
||||
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
|
||||
import {
|
||||
render,
|
||||
screen,
|
||||
cleanup,
|
||||
fireEvent,
|
||||
waitFor,
|
||||
} from "@testing-library/react";
|
||||
import React from "react";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
const apiGet = vi.fn();
|
||||
const apiPost = vi.fn();
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: {
|
||||
get: (path: string) => apiGet(path),
|
||||
post: (path: string, body?: unknown) => apiPost(path, body),
|
||||
patch: vi.fn(),
|
||||
put: vi.fn(),
|
||||
del: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import { ExternalConnectionSection } from "../ExternalConnectionSection";
|
||||
|
||||
beforeEach(() => {
|
||||
apiGet.mockReset();
|
||||
apiPost.mockReset();
|
||||
});
|
||||
|
||||
const SAMPLE_INFO = {
|
||||
workspace_id: "ws-test",
|
||||
platform_url: "https://platform.example.test",
|
||||
auth_token: "",
|
||||
registry_endpoint: "https://platform.example.test/registry/register",
|
||||
heartbeat_endpoint: "https://platform.example.test/registry/heartbeat",
|
||||
// The modal stamps these snippets server-side; for the test we
|
||||
// bake workspace_id into one so the rendered DOM contains a
|
||||
// findable token after the modal mounts.
|
||||
curl_register_template: "# curl ws=ws-test",
|
||||
python_snippet: "# py ws=ws-test",
|
||||
claude_code_channel_snippet: "# claude ws=ws-test",
|
||||
universal_mcp_snippet: "# mcp ws=ws-test",
|
||||
hermes_channel_snippet: "# hermes ws=ws-test",
|
||||
codex_snippet: "# codex ws=ws-test",
|
||||
openclaw_snippet: "# openclaw ws=ws-test",
|
||||
};
|
||||
|
||||
describe("ExternalConnectionSection", () => {
|
||||
it("renders both action buttons", () => {
|
||||
render(<ExternalConnectionSection workspaceId="ws-test" />);
|
||||
expect(screen.getByRole("button", { name: /show connection info/i })).toBeTruthy();
|
||||
expect(screen.getByRole("button", { name: /rotate credentials/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("'Show connection info' calls GET /external/connection and opens modal with blank token", async () => {
|
||||
apiGet.mockResolvedValue({ connection: { ...SAMPLE_INFO, auth_token: "" } });
|
||||
render(<ExternalConnectionSection workspaceId="ws-test" />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /show connection info/i }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(apiGet).toHaveBeenCalledWith("/workspaces/ws-test/external/connection"),
|
||||
);
|
||||
// The ExternalConnectModal renders the workspace_id field in its
|
||||
// copy-block. document.body covers Radix's portal mount point.
|
||||
await waitFor(() => {
|
||||
expect(document.body.textContent || "").toContain("ws-test");
|
||||
});
|
||||
});
|
||||
|
||||
it("'Rotate credentials' opens confirm dialog before firing POST", async () => {
|
||||
render(<ExternalConnectionSection workspaceId="ws-test" />);
|
||||
fireEvent.click(screen.getByRole("button", { name: /rotate credentials/i }));
|
||||
|
||||
// Confirm dialog appears with the destructive copy.
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByText(/Rotate workspace credentials\?/i),
|
||||
).toBeTruthy();
|
||||
});
|
||||
expect(screen.getByText(/immediately invalidate the current one/i)).toBeTruthy();
|
||||
|
||||
// POST must NOT have fired yet — only on confirm.
|
||||
expect(apiPost).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("Cancel in confirm dialog dismisses without rotating", async () => {
|
||||
render(<ExternalConnectionSection workspaceId="ws-test" />);
|
||||
fireEvent.click(screen.getByRole("button", { name: /rotate credentials/i }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText(/Rotate workspace credentials\?/i)).toBeTruthy(),
|
||||
);
|
||||
fireEvent.click(screen.getByRole("button", { name: /^cancel$/i }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.queryByText(/Rotate workspace credentials\?/i)).toBeNull(),
|
||||
);
|
||||
expect(apiPost).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("Confirm in dialog POSTs to /external/rotate and opens modal with returned token", async () => {
|
||||
apiPost.mockResolvedValue({
|
||||
connection: { ...SAMPLE_INFO, auth_token: "fresh-tok-123" },
|
||||
});
|
||||
render(<ExternalConnectionSection workspaceId="ws-test" />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /rotate credentials/i }));
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText(/Rotate workspace credentials\?/i)).toBeTruthy(),
|
||||
);
|
||||
// Click the dialog's Rotate button (NOT the section's — the section's
|
||||
// "Rotate credentials" stays mounted; the dialog's "Rotate" is the
|
||||
// commit button. getAllByRole returns both; pick the one inside the
|
||||
// dialog by name "Rotate" exact-match).
|
||||
const rotateBtns = screen.getAllByRole("button", { name: /^rotate$/i });
|
||||
expect(rotateBtns.length).toBeGreaterThanOrEqual(1);
|
||||
fireEvent.click(rotateBtns[rotateBtns.length - 1]);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(apiPost).toHaveBeenCalledWith(
|
||||
"/workspaces/ws-test/external/rotate",
|
||||
{},
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("Surfaces API errors as a visible chip, not silent loss", async () => {
|
||||
apiGet.mockRejectedValue(new Error("forbidden"));
|
||||
render(<ExternalConnectionSection workspaceId="ws-test" />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /show connection info/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
const matches = screen.queryAllByText((_, el) =>
|
||||
(el?.textContent || "").toLowerCase().includes("forbidden"),
|
||||
);
|
||||
expect(matches.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,220 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// Pins the Edit affordance added to MemoryTab. Until this PR the Memory tab
|
||||
// was Add+Delete only; an entry that needed correction had to be deleted and
|
||||
// re-added — losing the version-counter and any in-flight optimistic-locking
|
||||
// invariants other writers depend on.
|
||||
//
|
||||
// Each test pins one branch of the new flow. If any fails, the bug is back.
|
||||
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
|
||||
import { render, screen, cleanup, waitFor, fireEvent } from "@testing-library/react";
|
||||
import React from "react";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
const apiGet = vi.fn();
|
||||
const apiPost = vi.fn();
|
||||
const apiDel = vi.fn();
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: {
|
||||
get: (path: string) => apiGet(path),
|
||||
post: (path: string, body: unknown) => apiPost(path, body),
|
||||
del: (path: string) => apiDel(path),
|
||||
patch: vi.fn(),
|
||||
put: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import { MemoryTab } from "../MemoryTab";
|
||||
|
||||
const sampleEntries = [
|
||||
{
|
||||
key: "team_brief",
|
||||
value: { goal: "ship v2" },
|
||||
version: 3,
|
||||
expires_at: null,
|
||||
updated_at: "2026-05-04T10:00:00Z",
|
||||
},
|
||||
{
|
||||
key: "plain_note",
|
||||
value: "raw text note",
|
||||
version: 1,
|
||||
expires_at: "2099-01-01T00:00:00Z",
|
||||
updated_at: "2026-05-04T10:01:00Z",
|
||||
},
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
apiGet.mockReset();
|
||||
apiPost.mockReset();
|
||||
apiDel.mockReset();
|
||||
apiGet.mockImplementation((path: string) => {
|
||||
if (path === "/workspaces/ws-test/memory") {
|
||||
return Promise.resolve(sampleEntries);
|
||||
}
|
||||
return Promise.reject(new Error(`unmocked api.get: ${path}`));
|
||||
});
|
||||
});
|
||||
|
||||
async function renderAndExpand(key: string) {
|
||||
render(<MemoryTab workspaceId="ws-test" />);
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalled());
|
||||
// Reveal the Advanced section that hosts the entry list.
|
||||
const showAdvanced = await screen.findByRole("button", { name: "Show" });
|
||||
fireEvent.click(showAdvanced);
|
||||
// Expand the row.
|
||||
const row = await screen.findByRole("button", { name: new RegExp(key) });
|
||||
fireEvent.click(row);
|
||||
}
|
||||
|
||||
describe("MemoryTab Edit affordance", () => {
|
||||
it("Edit button appears once a row is expanded", async () => {
|
||||
await renderAndExpand("team_brief");
|
||||
expect(screen.getAllByRole("button", { name: "Edit" }).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("clicking Edit on a JSON-valued entry pre-fills the textarea with pretty JSON", async () => {
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = (await screen.findByLabelText(
|
||||
"Edit value for team_brief",
|
||||
)) as HTMLTextAreaElement;
|
||||
expect(textarea.value).toBe('{\n "goal": "ship v2"\n}');
|
||||
});
|
||||
|
||||
it("clicking Edit on a string-valued entry pre-fills raw (no surrounding quotes)", async () => {
|
||||
await renderAndExpand("plain_note");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = (await screen.findByLabelText(
|
||||
"Edit value for plain_note",
|
||||
)) as HTMLTextAreaElement;
|
||||
expect(textarea.value).toBe("raw text note");
|
||||
});
|
||||
|
||||
it("Save POSTs with if_match_version + parsed value, then reloads", async () => {
|
||||
apiPost.mockResolvedValue({ status: "ok", key: "team_brief", version: 4 });
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = await screen.findByLabelText("Edit value for team_brief");
|
||||
fireEvent.change(textarea, { target: { value: '{"goal":"ship v3"}' } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
|
||||
await waitFor(() => expect(apiPost).toHaveBeenCalledTimes(1));
|
||||
expect(apiPost).toHaveBeenCalledWith("/workspaces/ws-test/memory", {
|
||||
key: "team_brief",
|
||||
value: { goal: "ship v3" },
|
||||
if_match_version: 3,
|
||||
});
|
||||
// Reload after save → second GET.
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it("Save with non-JSON text falls back to plain string", async () => {
|
||||
apiPost.mockResolvedValue({ status: "ok" });
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = await screen.findByLabelText("Edit value for team_brief");
|
||||
fireEvent.change(textarea, { target: { value: "free-form note" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
|
||||
await waitFor(() => expect(apiPost).toHaveBeenCalledTimes(1));
|
||||
expect(apiPost.mock.calls[0][1].value).toBe("free-form note");
|
||||
});
|
||||
|
||||
it("TTL field is forwarded as ttl_seconds when set", async () => {
|
||||
apiPost.mockResolvedValue({ status: "ok" });
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const ttlInput = await screen.findByLabelText("Edit TTL for team_brief");
|
||||
fireEvent.change(ttlInput, { target: { value: "3600" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
|
||||
await waitFor(() => expect(apiPost).toHaveBeenCalledTimes(1));
|
||||
expect(apiPost.mock.calls[0][1].ttl_seconds).toBe(3600);
|
||||
});
|
||||
|
||||
it("blank/zero/non-numeric TTL is omitted from the payload", async () => {
|
||||
apiPost.mockResolvedValue({ status: "ok" });
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const ttlInput = await screen.findByLabelText("Edit TTL for team_brief");
|
||||
// Junk + zero both must drop out — payload must not contain ttl_seconds.
|
||||
fireEvent.change(ttlInput, { target: { value: "abc" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
await waitFor(() => expect(apiPost).toHaveBeenCalledTimes(1));
|
||||
expect(apiPost.mock.calls[0][1]).not.toHaveProperty("ttl_seconds");
|
||||
});
|
||||
|
||||
it("Cancel discards edits and restores the rendered value", async () => {
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = await screen.findByLabelText("Edit value for team_brief");
|
||||
fireEvent.change(textarea, { target: { value: '{"goal":"discarded"}' } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
||||
|
||||
expect(apiPost).not.toHaveBeenCalled();
|
||||
// Editor is gone; the JSON pre-block is back.
|
||||
expect(screen.queryByLabelText("Edit value for team_brief")).toBeNull();
|
||||
expect(screen.getAllByText(/"goal": "ship v2"/i).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("409 response surfaces a retry hint and reloads", async () => {
|
||||
apiPost.mockRejectedValueOnce(
|
||||
new Error("HTTP 409: if_match_version mismatch"),
|
||||
);
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = await screen.findByLabelText("Edit value for team_brief");
|
||||
fireEvent.change(textarea, { target: { value: '{"goal":"ship v3"}' } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
|
||||
await waitFor(() => expect(apiPost).toHaveBeenCalledTimes(1));
|
||||
const alert = await screen.findByRole("alert");
|
||||
expect(alert.textContent).toMatch(/changed since you opened it/i);
|
||||
// Initial mount load + post-conflict reload.
|
||||
await waitFor(() => expect(apiGet).toHaveBeenCalledTimes(2));
|
||||
});
|
||||
|
||||
it("non-409 error surfaces the message and does not reload", async () => {
|
||||
apiPost.mockRejectedValueOnce(new Error("boom"));
|
||||
await renderAndExpand("team_brief");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
|
||||
const alert = await screen.findByRole("alert");
|
||||
expect(alert.textContent).toBe("boom");
|
||||
// Only the initial mount load — no retry reload.
|
||||
expect(apiGet).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("entry with no version omits if_match_version (back-compat with older shape)", async () => {
|
||||
// Pre-version-counter shape: drop the `version` field from the row.
|
||||
apiGet.mockReset();
|
||||
apiGet.mockImplementation((path: string) => {
|
||||
if (path === "/workspaces/ws-test/memory") {
|
||||
return Promise.resolve([
|
||||
{
|
||||
key: "old_entry",
|
||||
value: "legacy",
|
||||
expires_at: null,
|
||||
updated_at: "2026-05-04T10:00:00Z",
|
||||
},
|
||||
]);
|
||||
}
|
||||
return Promise.reject(new Error(`unmocked: ${path}`));
|
||||
});
|
||||
apiPost.mockResolvedValue({ status: "ok" });
|
||||
|
||||
await renderAndExpand("old_entry");
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit" })[0]);
|
||||
const textarea = await screen.findByLabelText("Edit value for old_entry");
|
||||
fireEvent.change(textarea, { target: { value: "updated" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||||
|
||||
await waitFor(() => expect(apiPost).toHaveBeenCalledTimes(1));
|
||||
const payload = apiPost.mock.calls[0][1];
|
||||
expect(payload).not.toHaveProperty("if_match_version");
|
||||
expect(payload.value).toBe("updated");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,107 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// Pins the "Terminal not available" early-return added 2026-05-05.
|
||||
//
|
||||
// Pre-fix: TerminalTab tried to open /ws/terminal/<id> for every
|
||||
// workspace including external runtimes (which have no shell endpoint).
|
||||
// The server returned 404, status flipped to "error", user saw
|
||||
// "Connection failed" with a Reconnect button — reading as a bug
|
||||
// when really the runtime intentionally has no TTY. Now: when
|
||||
// data.runtime is in RUNTIMES_WITHOUT_TERMINAL, render a banner +
|
||||
// big icon instead of mounting xterm/WS.
|
||||
//
|
||||
// Pinned branches:
|
||||
// 1. external runtime → "Terminal not available" banner renders,
|
||||
// runtime name surfaces in the body so the user knows WHY.
|
||||
// 2. external runtime → xterm + WebSocket are NOT initialised.
|
||||
// Verified by checking the global WebSocket constructor isn't
|
||||
// called.
|
||||
// 3. claude-code (or any other runtime) → no banner, normal mount
|
||||
// proceeds. Pre-fix regression cover.
|
||||
// 4. data prop omitted (back-compat with any caller that doesn't
|
||||
// thread it through) → no early-return, falls through to normal
|
||||
// mount. Tested via the absence of the banner.
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { render, screen, cleanup } from "@testing-library/react";
|
||||
import React from "react";
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
// xterm + addon-fit are dynamically imported by TerminalTab. Stub them
|
||||
// so the tests don't pull a 200KB+ dependency just to verify the
|
||||
// not-available banner. The stubs only matter for the non-banner
|
||||
// branches; the banner returns BEFORE the dynamic import.
|
||||
vi.mock("xterm", () => ({
|
||||
Terminal: vi.fn().mockImplementation(() => ({
|
||||
loadAddon: vi.fn(),
|
||||
open: vi.fn(),
|
||||
onData: vi.fn(),
|
||||
write: vi.fn(),
|
||||
dispose: vi.fn(),
|
||||
onResize: vi.fn(),
|
||||
cols: 80,
|
||||
rows: 24,
|
||||
})),
|
||||
}));
|
||||
vi.mock("@xterm/addon-fit", () => ({
|
||||
FitAddon: vi.fn().mockImplementation(() => ({
|
||||
fit: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Track WebSocket constructor calls — this is the load-bearing
|
||||
// assertion for "external doesn't even try to connect".
|
||||
let wsConstructed = 0;
|
||||
beforeEach(() => {
|
||||
wsConstructed = 0;
|
||||
(globalThis as unknown as { WebSocket: unknown }).WebSocket = vi
|
||||
.fn()
|
||||
.mockImplementation(() => {
|
||||
wsConstructed++;
|
||||
return {
|
||||
addEventListener: vi.fn(),
|
||||
removeEventListener: vi.fn(),
|
||||
send: vi.fn(),
|
||||
close: vi.fn(),
|
||||
readyState: 0,
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
import { TerminalTab } from "../TerminalTab";
|
||||
|
||||
const externalData = { runtime: "external", status: "online" } as unknown as Parameters<
|
||||
typeof TerminalTab
|
||||
>[0]["data"];
|
||||
|
||||
const claudeData = { runtime: "claude-code", status: "online" } as unknown as Parameters<
|
||||
typeof TerminalTab
|
||||
>[0]["data"];
|
||||
|
||||
describe("TerminalTab not-available early-return for runtimes without TTY", () => {
|
||||
it("external runtime renders the not-available banner with runtime name", () => {
|
||||
render(<TerminalTab workspaceId="ws-ext" data={externalData} />);
|
||||
expect(screen.getByText(/Terminal not available/i)).not.toBeNull();
|
||||
// Runtime name surfaces so user knows WHY there's no terminal.
|
||||
expect(screen.getByText(/external/)).not.toBeNull();
|
||||
});
|
||||
|
||||
it("external runtime does NOT open a WebSocket", async () => {
|
||||
render(<TerminalTab workspaceId="ws-ext" data={externalData} />);
|
||||
// Wait a tick for any deferred init (there shouldn't be any, but
|
||||
// tolerate a microtask boundary).
|
||||
await new Promise((r) => setTimeout(r, 0));
|
||||
expect(wsConstructed).toBe(0);
|
||||
});
|
||||
|
||||
it("claude-code runtime does NOT render the banner (normal mount)", () => {
|
||||
render(<TerminalTab workspaceId="ws-claude" data={claudeData} />);
|
||||
expect(screen.queryByText(/Terminal not available/i)).toBeNull();
|
||||
});
|
||||
|
||||
it("data prop omitted falls through to normal mount (back-compat)", () => {
|
||||
render(<TerminalTab workspaceId="ws-no-data" />);
|
||||
expect(screen.queryByText(/Terminal not available/i)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -22,7 +22,6 @@ export interface ConfigData {
|
||||
// task_budget maps to output_config.task_budget.total (requires beta header task-budgets-2026-03-13)
|
||||
task_budget?: number;
|
||||
prompt_files: string[];
|
||||
shared_context: string[];
|
||||
skills: string[];
|
||||
tools: string[];
|
||||
a2a: { port: number; streaming: boolean; push_notifications: boolean };
|
||||
@@ -40,7 +39,6 @@ export const DEFAULT_CONFIG: ConfigData = {
|
||||
effort: "",
|
||||
task_budget: 0,
|
||||
prompt_files: [],
|
||||
shared_context: [],
|
||||
skills: [],
|
||||
tools: [],
|
||||
a2a: { port: 8000, streaming: true, push_notifications: true },
|
||||
|
||||
@@ -120,7 +120,6 @@ export function toYaml(config: ConfigData): string {
|
||||
if (config.effort) { lines.push(""); simple("effort", config.effort); }
|
||||
if (config.task_budget && config.task_budget > 0) { simple("task_budget", config.task_budget); }
|
||||
if (config.prompt_files?.length) { lines.push(""); list("prompt_files", config.prompt_files); }
|
||||
if (config.shared_context?.length) { lines.push(""); list("shared_context", config.shared_context); }
|
||||
lines.push(""); list("skills", config.skills);
|
||||
if (config.tools?.length) { list("tools", config.tools); }
|
||||
lines.push(""); obj("a2a", config.a2a as unknown as Record<string, unknown>);
|
||||
|
||||
@@ -5,6 +5,13 @@ export const STATUS_CONFIG: Record<string, { dot: string; glow: string; label: s
|
||||
degraded: { dot: "bg-amber-400", glow: "shadow-amber-400/50", label: "Degraded", bar: "from-amber-500/20 to-transparent" },
|
||||
failed: { dot: "bg-red-400", glow: "shadow-red-400/50", label: "Failed", bar: "from-red-500/20 to-transparent" },
|
||||
provisioning: { dot: "bg-sky-400 motion-safe:animate-pulse", glow: "shadow-sky-400/50", label: "Starting", bar: "from-sky-500/20 to-transparent" },
|
||||
// not_configured: derived state from agent_card.configuration_status (PR #2756 chain).
|
||||
// Workspace is reachable (heartbeating, /agent-card serves) but adapter.setup()
|
||||
// failed — typically a missing/rotated LLM credential. Amber to differentiate from
|
||||
// online (green) and failed (red) — the workspace itself is healthy, just needs
|
||||
// configuration. Hover renders agent_card.configuration_error in the tooltip so
|
||||
// the operator sees the exact env var to set.
|
||||
not_configured: { dot: "bg-amber-300", glow: "shadow-amber-300/50", label: "Not configured", bar: "from-amber-400/20 to-transparent" },
|
||||
};
|
||||
|
||||
export function statusDotClass(status: string): string {
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
getConfigurationStatus,
|
||||
getConfigurationError,
|
||||
} from "../canvas-topology";
|
||||
|
||||
// Tests for the getConfigurationStatus / getConfigurationError helpers
|
||||
// (issue #467 / PR #2756 chain). Surfacing the workspace's
|
||||
// `agent_card.configuration_status` is the user-visible payoff of
|
||||
// PR #2756's decoupling — without it, a misconfigured workspace looks
|
||||
// identical to a healthy one in the canvas tile.
|
||||
|
||||
describe("getConfigurationStatus", () => {
|
||||
it("returns null when agentCard is null", () => {
|
||||
expect(getConfigurationStatus(null)).toBe(null);
|
||||
});
|
||||
|
||||
it("returns null when agentCard has no configuration_status", () => {
|
||||
expect(getConfigurationStatus({ name: "x" })).toBe(null);
|
||||
});
|
||||
|
||||
it("returns 'ready' when agent reports configuration ok", () => {
|
||||
expect(
|
||||
getConfigurationStatus({ configuration_status: "ready" }),
|
||||
).toBe("ready");
|
||||
});
|
||||
|
||||
it("returns 'not_configured' when agent reports setup failed", () => {
|
||||
expect(
|
||||
getConfigurationStatus({ configuration_status: "not_configured" }),
|
||||
).toBe("not_configured");
|
||||
});
|
||||
|
||||
it("ignores unknown values defensively", () => {
|
||||
// A future agent reporting a status string we don't yet recognise
|
||||
// shouldn't crash the canvas — we treat it as 'no info' (null).
|
||||
expect(
|
||||
getConfigurationStatus({ configuration_status: "starting" }),
|
||||
).toBe(null);
|
||||
expect(
|
||||
getConfigurationStatus({ configuration_status: 42 }),
|
||||
).toBe(null);
|
||||
expect(
|
||||
getConfigurationStatus({ configuration_status: null }),
|
||||
).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getConfigurationError", () => {
|
||||
it("returns null when agentCard is null", () => {
|
||||
expect(getConfigurationError(null)).toBe(null);
|
||||
});
|
||||
|
||||
it("returns null when status is 'ready' even if error string present", () => {
|
||||
// Defensive: if the agent somehow ships configuration_status=ready
|
||||
// alongside a stale configuration_error from a previous boot, we
|
||||
// trust the live status flag and don't surface the stale error.
|
||||
expect(
|
||||
getConfigurationError({
|
||||
configuration_status: "ready",
|
||||
configuration_error: "stale: was unset",
|
||||
}),
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it("returns the error string when status is 'not_configured'", () => {
|
||||
expect(
|
||||
getConfigurationError({
|
||||
configuration_status: "not_configured",
|
||||
configuration_error:
|
||||
"RuntimeError: Neither OPENAI_API_KEY nor MINIMAX_API_KEY is set",
|
||||
}),
|
||||
).toBe(
|
||||
"RuntimeError: Neither OPENAI_API_KEY nor MINIMAX_API_KEY is set",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns null when status is 'not_configured' but error is missing", () => {
|
||||
expect(
|
||||
getConfigurationError({ configuration_status: "not_configured" }),
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it("returns null when error is empty string", () => {
|
||||
// Empty string isn't actionable for the operator — treat same as
|
||||
// missing.
|
||||
expect(
|
||||
getConfigurationError({
|
||||
configuration_status: "not_configured",
|
||||
configuration_error: "",
|
||||
}),
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it("returns null when error is non-string", () => {
|
||||
expect(
|
||||
getConfigurationError({
|
||||
configuration_status: "not_configured",
|
||||
configuration_error: { reason: "object" },
|
||||
}),
|
||||
).toBe(null);
|
||||
});
|
||||
});
|
||||
@@ -564,3 +564,42 @@ export function extractSkillNames(agentCard: Record<string, unknown> | null): st
|
||||
.map((skill: Record<string, unknown>) => String(skill.name || skill.id || ""))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configuration status reported by the workspace, or null
|
||||
* when the agent card doesn't carry one (older runtime, or pre-PR #2756
|
||||
* worker).
|
||||
*
|
||||
* Pairs with molecule-core PR #2756: when adapter.setup() fails, the
|
||||
* runtime mounts a not-configured handler AND advertises the failure
|
||||
* via agent_card.configuration_status = "not_configured" +
|
||||
* configuration_error = "<reason>". Canvas reads both to render a
|
||||
* "needs config" tile instead of a confused "online but silent" state.
|
||||
*
|
||||
* Returns null (not undefined) so callers can distinguish "no info"
|
||||
* from explicit values via a strict equality check.
|
||||
*/
|
||||
export function getConfigurationStatus(
|
||||
agentCard: Record<string, unknown> | null,
|
||||
): "ready" | "not_configured" | null {
|
||||
if (!agentCard) return null;
|
||||
const raw = agentCard.configuration_status;
|
||||
if (raw === "ready" || raw === "not_configured") return raw;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configuration error string from the agent card when
|
||||
* configuration_status is "not_configured", or null otherwise.
|
||||
*
|
||||
* Already redacted server-side via secret_redactor (PR #2778) — safe to
|
||||
* render in the UI verbatim.
|
||||
*/
|
||||
export function getConfigurationError(
|
||||
agentCard: Record<string, unknown> | null,
|
||||
): string | null {
|
||||
if (!agentCard) return null;
|
||||
if (getConfigurationStatus(agentCard) !== "not_configured") return null;
|
||||
const raw = agentCard.configuration_error;
|
||||
return typeof raw === "string" && raw.length > 0 ? raw : null;
|
||||
}
|
||||
|
||||
@@ -27,11 +27,11 @@ prompt_files:
|
||||
# AGENTS.md-style example:
|
||||
# prompt_files: [AGENTS.md]
|
||||
|
||||
# Files to share with direct children (1-level inheritance)
|
||||
# Children fetch these at startup via GET /workspaces/:id/shared-context
|
||||
shared_context:
|
||||
- architecture.md
|
||||
- conventions.md
|
||||
# NOTE: `shared_context` (parent → child file injection at boot) was removed.
|
||||
# To share knowledge across a team, use memory v2's team:<id> namespace via
|
||||
# the recall_memory MCP tool — the agent pulls it on demand instead of
|
||||
# paying for it at every boot. For large blob-shaped artefacts, see RFC
|
||||
# #2789 (platform-owned shared file storage).
|
||||
|
||||
# Skills to load -- folder names under skills/
|
||||
skills:
|
||||
@@ -123,7 +123,6 @@ env:
|
||||
| `runtime` | No | Adapter to use: `langgraph` (default), `claude-code`, `crewai`, `autogen`, `deepagents`, `openclaw`. See [Agent Runtime Adapters](./cli-runtime.md). |
|
||||
| `model` | Yes | LangChain-compatible provider string (e.g. `anthropic:claude-sonnet-4-6`). Overridden by `MODEL_PROVIDER` env var if set. |
|
||||
| `prompt_files` | No | Ordered list of markdown files to load as system prompt. Defaults to `["system-prompt.md"]` if omitted. `MEMORY.md` and `USER.md` are auto-appended when present so frozen memory snapshots do not need to be duplicated here. Supports any agent framework's file structure (OpenClaw, Claude Code, etc.) |
|
||||
| `shared_context` | No | Files from this workspace's config dir to share with direct children. Children fetch these at startup and inject into their system prompt as `## Parent Context`. 1-level inheritance only (grandchildren don't see grandparent's context). |
|
||||
| `skills` | Yes | List of skill folder names to load from `skills/` |
|
||||
| `tools` | No | Built-in tools from workspace-template |
|
||||
| `memory` | No | Memory backend config (defaults to filesystem) |
|
||||
@@ -157,7 +156,6 @@ The file watcher monitors the entire config directory. When `config.yaml` change
|
||||
| `name`, `description`, `version` | Yes | Rebuild Agent Card with new metadata |
|
||||
| `a2a` | **No** | Port and protocol changes require container restart |
|
||||
| `delegation` | Yes | Retry/timeout defaults take effect on next delegation call |
|
||||
| `shared_context` | Yes | Children fetch on next prompt rebuild; no restart needed |
|
||||
| `sub_workspaces` | **No** | Team structure changes go through `POST /workspaces/:id/expand` |
|
||||
|
||||
See [Skills — Live Reload](./skills.md#live-reload) for the full file watcher flow.
|
||||
|
||||
@@ -24,21 +24,19 @@ When you receive a task, break it into sub-tasks and delegate to your team.
|
||||
Always review work before reporting completion to the caller.
|
||||
```
|
||||
|
||||
### 2. Parent Context (if child workspace)
|
||||
### 2. Team-shared knowledge (on demand)
|
||||
|
||||
If this workspace was created via team expansion (has a `PARENT_ID` env var), it fetches its parent's shared context files at startup via `GET /workspaces/{parent_id}/shared-context`. The parent declares which files to share in its `config.yaml`:
|
||||
Team-scoped knowledge is no longer injected at boot. The previous
|
||||
`shared_context` field + `GET /workspaces/{parent_id}/shared-context`
|
||||
fetch was removed; agents now pull team-shared knowledge on demand via
|
||||
memory v2's `team:<id>` namespace using the `recall_memory` MCP tool.
|
||||
|
||||
```yaml
|
||||
shared_context:
|
||||
- architecture.md
|
||||
- conventions.md
|
||||
```
|
||||
|
||||
These files are injected as a `## Parent Context` section, with each file rendered under a `### {filename}` heading. This gives children the parent's project knowledge (architecture, conventions, API schemas) without exposing the parent's system prompt or full config.
|
||||
|
||||
**1-level inheritance only:** A grandchild sees its direct parent's shared context, not its grandparent's. This mirrors the L2 Team Memory scope.
|
||||
|
||||
**Graceful degradation:** If the parent is offline or the endpoint returns an error, the child starts normally without parent context.
|
||||
This shifts cost from "every boot, always" to "only when the agent
|
||||
asks", and lets team members write to the shared store from anywhere
|
||||
that can resolve the namespace (canvas Memory tab, agent
|
||||
`commit_memory`, admin import). For large blob-shaped artefacts (full
|
||||
architecture docs, brand assets, PDFs) see RFC #2789 (platform-owned
|
||||
shared file storage).
|
||||
|
||||
### 3. Skill Instructions
|
||||
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
# Team Expansion (Recursive Workspaces)
|
||||
|
||||
When a workspace is expanded into a team, it gains sub-workspaces while its own agent remains as the **team lead** (coordinator). This is recursive — sub-workspaces can themselves be expanded into teams, infinitely deep.
|
||||
|
||||
## How It Works
|
||||
|
||||
When Developer PM is expanded into a team:
|
||||
|
||||
```
|
||||
Business Core
|
||||
|
|
||||
+-- Developer PM (agent stays, becomes coordinator)
|
||||
|
|
||||
+-- Frontend Agent (sub-workspace, private scope)
|
||||
+-- Backend Agent (sub-workspace, private scope)
|
||||
+-- QA Agent (sub-workspace, private scope)
|
||||
```
|
||||
|
||||
- Developer PM's agent **still exists** and acts as coordinator
|
||||
- Developer PM receives incoming A2A messages from Business Core
|
||||
- Developer PM's agent decides how to delegate to sub-workspaces
|
||||
- Sub-workspaces talk to Developer PM and to each other (same level)
|
||||
- Sub-workspaces **cannot** talk to Business Core or any workspace outside the team
|
||||
|
||||
## Communication Rules
|
||||
|
||||
| Direction | Allowed? | Example |
|
||||
|-----------|----------|---------|
|
||||
| Parent level -> team lead | Yes | Business Core -> Developer PM |
|
||||
| Team lead -> sub-workspaces | Yes | Developer PM -> Frontend Agent |
|
||||
| Sub-workspace -> team lead | Yes | Frontend Agent -> Developer PM |
|
||||
| Sub-workspace <-> sibling | Yes | Frontend Agent <-> Backend Agent |
|
||||
| Outside -> sub-workspace directly | No (403) | Business Core -> Frontend Agent |
|
||||
| Sub-workspace -> outside directly | No | Frontend Agent -> Business Core |
|
||||
|
||||
The team lead (Developer PM) is the **only** bridge between the team's internal world and the outside.
|
||||
|
||||
## Scoped Registry
|
||||
|
||||
Sub-workspaces register in the platform registry but with a **private scope**. The registry knows about them but enforces access control.
|
||||
|
||||
```
|
||||
Registry:
|
||||
Business Core :8001 scope: public
|
||||
Developer PM :8002 scope: public
|
||||
Frontend Agent :8010 scope: private, parent=Developer PM
|
||||
Backend Agent :8011 scope: private, parent=Developer PM
|
||||
QA Agent :8012 scope: private, parent=Developer PM
|
||||
```
|
||||
|
||||
- The platform can always discover any workspace (for provisioning, monitoring)
|
||||
- The parent workspace can discover its sub-workspaces
|
||||
- Sub-workspaces can discover their siblings (same parent)
|
||||
- Outside workspaces get a **403 Forbidden** if they try to discover a private sub-workspace
|
||||
|
||||
## How to Expand
|
||||
|
||||
Expansion is triggered via `POST /workspaces/:id/expand`. The platform reads the `sub_workspaces` list from the workspace's config and provisions each one. On the canvas, users right-click a workspace node and select "Expand into team."
|
||||
|
||||
Collapsing is the inverse: `POST /workspaces/:id/collapse`. Sub-workspaces are stopped and removed.
|
||||
|
||||
## What Happens on Expansion
|
||||
|
||||
When Developer PM is expanded into a team, the hierarchy changes but the outside view doesn't. Business Core's parent/child relationship to Developer PM is unaffected — Developer PM still responds to the same A2A endpoint.
|
||||
|
||||
The events fired:
|
||||
- `WORKSPACE_EXPANDED` with the new `sub_workspace_ids` in the payload
|
||||
- `WORKSPACE_PROVISIONING` for each new sub-workspace
|
||||
- `WORKSPACE_ONLINE` for each sub-workspace as they come up
|
||||
|
||||
Communication rules are automatically derived from the new hierarchy — no manual wiring needed.
|
||||
|
||||
## Canvas Behavior
|
||||
|
||||
- Children render as embedded mini-cards (`TeamMemberChip`) inside the parent node, not as separate canvas nodes
|
||||
- Each mini-card shows full status: gradient bar, name, tier badge, skills pills, active tasks, descendant count
|
||||
- **Recursive rendering** up to 3 levels deep (`MAX_NESTING_DEPTH = 3`) — sub-cards can contain their own "Team" sections
|
||||
- Parent node dynamically resizes: 210-280px (no children), 320-450px (children), 400-560px (grandchildren)
|
||||
- Eject button (sky-blue arrow icon) on hover extracts a child from the team
|
||||
- "Extract from Team" also available in the right-click context menu
|
||||
- Double-click a team node to zoom/fit to the parent area
|
||||
- The parent workspace node shows a badge with total descendant count
|
||||
|
||||
## Collapsing a Team
|
||||
|
||||
The inverse of expansion, triggered via `POST /workspaces/:id/collapse`:
|
||||
|
||||
1. Each sub-workspace agent wraps up current work and writes a handoff document to memory
|
||||
2. Sub-workspaces are stopped and removed
|
||||
3. The team lead's agent goes back to handling everything directly
|
||||
4. A `WORKSPACE_COLLAPSED` event fires
|
||||
|
||||
Sub-workspace memory is cleaned up based on backend (see [Memory — Cleanup](../architecture/memory.md#cleanup-on-workspace-deletion)).
|
||||
|
||||
## Deleting a Team Workspace
|
||||
|
||||
When a team workspace is deleted:
|
||||
1. Platform shows a warning listing all sub-workspaces that will be deleted
|
||||
2. User can **drag sub-workspaces out** of the team before confirming (promotes them to the parent level)
|
||||
3. On confirmation, cascade delete removes the parent and all remaining sub-workspaces
|
||||
4. `WORKSPACE_REMOVED` events fire for each deleted workspace
|
||||
|
||||
## Related Docs
|
||||
|
||||
- [Communication Rules](../api-protocol/communication-rules.md) — Full access control model
|
||||
- [Core Concepts](../product/core-concepts.md) — Workspace fundamentals
|
||||
- [System Prompt Structure](./system-prompt-structure.md) — How peer capabilities are injected
|
||||
- [Provisioner](../architecture/provisioner.md) — How sub-workspaces are deployed
|
||||
- [Registry & Heartbeat](../api-protocol/registry-and-heartbeat.md) — How registration works
|
||||
- [Event Log](../architecture/event-log.md) — Events fired during expansion
|
||||
- [Canvas UI](../frontend/canvas.md) — Visual behavior of teams
|
||||
@@ -0,0 +1,358 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: Molecule Memory Plugin v1
|
||||
version: 1.0.0
|
||||
description: |
|
||||
Contract between workspace-server and a memory backend plugin. The
|
||||
plugin owns its own storage; workspace-server is the security
|
||||
perimeter (secret redaction, namespace ACL, GLOBAL audit/wrap).
|
||||
|
||||
Defined in RFC #2728. See docs/rfc/memory-v2-rationale.md for design
|
||||
rationale.
|
||||
|
||||
Auth: none. Plugins MUST be reachable only on a private network or
|
||||
unix socket — workspace-server is the only sanctioned client.
|
||||
servers:
|
||||
- url: http://localhost:9100
|
||||
description: Built-in postgres-backed plugin (default)
|
||||
|
||||
paths:
|
||||
/v1/health:
|
||||
get:
|
||||
summary: Liveness + capability probe
|
||||
operationId: getHealth
|
||||
responses:
|
||||
'200':
|
||||
description: Plugin healthy
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/HealthResponse' }
|
||||
'503':
|
||||
description: Plugin unhealthy (e.g., backing store down)
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/Error' }
|
||||
|
||||
/v1/namespaces/{name}:
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/NamespaceName'
|
||||
put:
|
||||
summary: Upsert a namespace (idempotent)
|
||||
operationId: upsertNamespace
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/NamespaceUpsert' }
|
||||
responses:
|
||||
'200': { $ref: '#/components/responses/Namespace' }
|
||||
'400': { $ref: '#/components/responses/BadRequest' }
|
||||
patch:
|
||||
summary: Update namespace metadata or TTL
|
||||
operationId: patchNamespace
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/NamespacePatch' }
|
||||
responses:
|
||||
'200': { $ref: '#/components/responses/Namespace' }
|
||||
'404': { $ref: '#/components/responses/NotFound' }
|
||||
delete:
|
||||
summary: Delete namespace and all its memories (operator action)
|
||||
operationId: deleteNamespace
|
||||
responses:
|
||||
'204':
|
||||
description: Deleted
|
||||
'404': { $ref: '#/components/responses/NotFound' }
|
||||
|
||||
/v1/namespaces/{name}/memories:
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/NamespaceName'
|
||||
post:
|
||||
summary: Write a memory to a namespace
|
||||
description: |
|
||||
`content` MUST already be secret-redacted by the workspace-server.
|
||||
Plugin does not run additional redaction.
|
||||
operationId: commitMemory
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/MemoryWrite' }
|
||||
responses:
|
||||
'201':
|
||||
description: Memory persisted
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/MemoryWriteResponse' }
|
||||
'400': { $ref: '#/components/responses/BadRequest' }
|
||||
'404': { $ref: '#/components/responses/NotFound' }
|
||||
|
||||
/v1/search:
|
||||
post:
|
||||
summary: Search memories across one or more namespaces
|
||||
description: |
|
||||
workspace-server MUST intersect the requested `namespaces` with
|
||||
the caller's currently-readable set BEFORE invoking this
|
||||
endpoint. The plugin treats the list as authoritative.
|
||||
operationId: searchMemories
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/SearchRequest' }
|
||||
responses:
|
||||
'200':
|
||||
description: Search results
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/SearchResponse' }
|
||||
'400': { $ref: '#/components/responses/BadRequest' }
|
||||
|
||||
/v1/memories/{id}:
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
required: true
|
||||
schema: { type: string, format: uuid }
|
||||
delete:
|
||||
summary: Forget a memory by id
|
||||
description: |
|
||||
`requested_by_namespace` is the namespace the caller has write
|
||||
access to; the plugin SHOULD reject if the memory doesn't belong
|
||||
to that namespace.
|
||||
operationId: forgetMemory
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/ForgetRequest' }
|
||||
responses:
|
||||
'204':
|
||||
description: Forgotten
|
||||
'403': { $ref: '#/components/responses/Forbidden' }
|
||||
'404': { $ref: '#/components/responses/NotFound' }
|
||||
|
||||
components:
|
||||
parameters:
|
||||
NamespaceName:
|
||||
in: path
|
||||
name: name
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
minLength: 1
|
||||
maxLength: 256
|
||||
pattern: '^[a-z]+:[A-Za-z0-9_:.\-]+$'
|
||||
example: 'workspace:550e8400-e29b-41d4-a716-446655440000'
|
||||
|
||||
responses:
|
||||
Namespace:
|
||||
description: Namespace state
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/Namespace' }
|
||||
BadRequest:
|
||||
description: Invalid input
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/Error' }
|
||||
NotFound:
|
||||
description: Resource not found
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/Error' }
|
||||
Forbidden:
|
||||
description: Caller lacks write access to the requested namespace
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: '#/components/schemas/Error' }
|
||||
|
||||
schemas:
|
||||
HealthResponse:
|
||||
type: object
|
||||
required: [status, version, capabilities]
|
||||
properties:
|
||||
status: { type: string, enum: [ok, degraded] }
|
||||
version: { type: string, example: "1.0.0" }
|
||||
capabilities:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
enum: [embedding, fts, ttl, pin, propagation]
|
||||
description: |
|
||||
Optional features this plugin supports. workspace-server
|
||||
adapts MCP responses based on this list (e.g., agents can
|
||||
request semantic search only when `embedding` is present).
|
||||
|
||||
NamespaceKind:
|
||||
type: string
|
||||
enum: [workspace, team, org, custom]
|
||||
|
||||
Namespace:
|
||||
type: object
|
||||
required: [name, kind, created_at]
|
||||
properties:
|
||||
name: { type: string }
|
||||
kind: { $ref: '#/components/schemas/NamespaceKind' }
|
||||
expires_at:
|
||||
type: string
|
||||
format: date-time
|
||||
nullable: true
|
||||
metadata:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
nullable: true
|
||||
created_at: { type: string, format: date-time }
|
||||
|
||||
NamespaceUpsert:
|
||||
type: object
|
||||
required: [kind]
|
||||
properties:
|
||||
kind: { $ref: '#/components/schemas/NamespaceKind' }
|
||||
expires_at: { type: string, format: date-time, nullable: true }
|
||||
metadata:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
nullable: true
|
||||
|
||||
NamespacePatch:
|
||||
type: object
|
||||
properties:
|
||||
expires_at: { type: string, format: date-time, nullable: true }
|
||||
metadata:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
nullable: true
|
||||
|
||||
MemoryKind:
|
||||
type: string
|
||||
enum: [fact, summary, checkpoint]
|
||||
|
||||
MemorySource:
|
||||
type: string
|
||||
enum: [agent, runtime, user]
|
||||
|
||||
MemoryWrite:
|
||||
type: object
|
||||
required: [content, kind, source]
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
format: uuid
|
||||
nullable: true
|
||||
description: |
|
||||
Optional idempotency key. When supplied, the plugin MUST
|
||||
treat the write as upsert keyed on this id (re-running
|
||||
the same write does not duplicate). When omitted, the
|
||||
plugin generates a fresh UUID. Used by the backfill CLI.
|
||||
content:
|
||||
type: string
|
||||
minLength: 1
|
||||
description: Already secret-redacted by workspace-server.
|
||||
kind: { $ref: '#/components/schemas/MemoryKind' }
|
||||
source: { $ref: '#/components/schemas/MemorySource' }
|
||||
expires_at: { type: string, format: date-time, nullable: true }
|
||||
propagation:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
nullable: true
|
||||
description: |
|
||||
Opaque metadata the plugin stores and returns. Reserved for
|
||||
future cross-namespace propagation semantics.
|
||||
pin: { type: boolean, default: false }
|
||||
embedding:
|
||||
type: array
|
||||
items: { type: number }
|
||||
nullable: true
|
||||
description: |
|
||||
Optional pre-computed embedding. Plugins reporting the
|
||||
`embedding` capability MAY ignore this and recompute.
|
||||
|
||||
MemoryWriteResponse:
|
||||
type: object
|
||||
required: [id, namespace]
|
||||
properties:
|
||||
id: { type: string, format: uuid }
|
||||
namespace: { type: string }
|
||||
|
||||
Memory:
|
||||
type: object
|
||||
required: [id, namespace, content, kind, source, created_at]
|
||||
properties:
|
||||
id: { type: string, format: uuid }
|
||||
namespace: { type: string }
|
||||
content: { type: string }
|
||||
kind: { $ref: '#/components/schemas/MemoryKind' }
|
||||
source: { $ref: '#/components/schemas/MemorySource' }
|
||||
expires_at: { type: string, format: date-time, nullable: true }
|
||||
propagation:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
nullable: true
|
||||
pin: { type: boolean }
|
||||
created_at: { type: string, format: date-time }
|
||||
score:
|
||||
type: number
|
||||
nullable: true
|
||||
description: Relevance score from search (semantic + FTS).
|
||||
|
||||
SearchRequest:
|
||||
type: object
|
||||
required: [namespaces]
|
||||
properties:
|
||||
namespaces:
|
||||
type: array
|
||||
items: { type: string }
|
||||
minItems: 1
|
||||
description: |
|
||||
Already intersected with the caller's readable set by
|
||||
workspace-server.
|
||||
query: { type: string }
|
||||
kinds:
|
||||
type: array
|
||||
items: { $ref: '#/components/schemas/MemoryKind' }
|
||||
limit:
|
||||
type: integer
|
||||
minimum: 1
|
||||
maximum: 100
|
||||
default: 20
|
||||
embedding:
|
||||
type: array
|
||||
items: { type: number }
|
||||
nullable: true
|
||||
|
||||
SearchResponse:
|
||||
type: object
|
||||
required: [memories]
|
||||
properties:
|
||||
memories:
|
||||
type: array
|
||||
items: { $ref: '#/components/schemas/Memory' }
|
||||
|
||||
ForgetRequest:
|
||||
type: object
|
||||
required: [requested_by_namespace]
|
||||
properties:
|
||||
requested_by_namespace:
|
||||
type: string
|
||||
description: Namespace the caller has write access to.
|
||||
|
||||
Error:
|
||||
type: object
|
||||
required: [code, message]
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
enum:
|
||||
- bad_request
|
||||
- not_found
|
||||
- forbidden
|
||||
- internal
|
||||
- unavailable
|
||||
message: { type: string }
|
||||
details:
|
||||
type: object
|
||||
additionalProperties: true
|
||||
nullable: true
|
||||
@@ -199,7 +199,6 @@ Install safeguards bound the cost of a single install (env-tunable via `PLUGIN_I
|
||||
| `GET` | `/templates` | List available templates. **Requires AdminAuth** (PR #701). |
|
||||
| `GET` | `/org/templates` | List available org templates. **Requires AdminAuth** (PR #701). |
|
||||
| `POST` | `/templates/import` | Import an agent folder as a new template |
|
||||
| `GET` | `/workspaces/:id/shared-context` | Read parent shared-context files |
|
||||
| `GET` | `/workspaces/:id/files` | List files under an allowed root |
|
||||
| `GET` | `/workspaces/:id/files/*path` | Read a file |
|
||||
| `PUT` | `/workspaces/:id/files/*path` | Write a file |
|
||||
|
||||
@@ -41,8 +41,6 @@ Full contract: `docs/runbooks/admin-auth.md`.
|
||||
| GET | /admin/workspaces/:id/test-token | admin_test_token.go — mint a fresh bearer token for E2E scripts; returns 404 unless `MOLECULE_ENV != production` or `MOLECULE_ENABLE_TEST_TOKENS=1` |
|
||||
| GET/POST/DELETE | /admin/secrets[/:key] | secrets.go — legacy aliases for /settings/secrets |
|
||||
| WS | /workspaces/:id/terminal | terminal.go |
|
||||
| POST | /workspaces/:id/expand | team.go |
|
||||
| POST | /workspaces/:id/collapse | team.go |
|
||||
| POST/GET | /workspaces/:id/approvals | approvals.go |
|
||||
| POST | /workspaces/:id/approvals/:id/decide | approvals.go |
|
||||
| GET | /approvals/pending | approvals.go |
|
||||
@@ -68,7 +66,6 @@ Full contract: `docs/runbooks/admin-auth.md`.
|
||||
| GET | /channels/adapters | channels.go (list available platforms) |
|
||||
| POST | /channels/discover | channels.go (auto-detect chats for a bot token) |
|
||||
| POST | /webhooks/:type | channels.go (incoming social webhook) |
|
||||
| GET | /workspaces/:id/shared-context | templates.go |
|
||||
| GET/PUT/DELETE | /workspaces/:id/files[/*path] | templates.go |
|
||||
| GET | /canvas/viewport | viewport.go — open, no auth required (cosmetic, bootstrap-friendly) |
|
||||
| PUT | /canvas/viewport | viewport.go — `CanvasOrBearer` middleware; accepts bearer OR Origin matching `CORS_ORIGINS`. Cosmetic-only route — worst case viewport corruption, recovered by page refresh. |
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
**Status:** living document — update when you ship a feature that touches one backend.
|
||||
**Owner:** workspace-server + controlplane teams.
|
||||
**Last audit:** 2026-05-02 (Claude agent, PR #TBD).
|
||||
**Last audit:** 2026-05-05 (Claude agent — `provisionWorkspaceAuto` / `StopWorkspaceAuto` / `HasProvisioner` SoT pattern landed in PRs #2811 + #2824).
|
||||
|
||||
## Why this exists
|
||||
|
||||
@@ -15,16 +15,39 @@ Every user-visible workspace feature should work on both backends unless it is f
|
||||
|
||||
This document is the canonical matrix. If you are landing a workspace-facing feature, update the row before you merge.
|
||||
|
||||
## How to dispatch (the SoT pattern)
|
||||
|
||||
When a handler needs to start, stop, or check whether-something-can-run a workspace, it MUST go through the centralized dispatcher on `WorkspaceHandler`:
|
||||
|
||||
| Need | Use | Source |
|
||||
|---|---|---|
|
||||
| Start a workspace | `provisionWorkspaceAuto(ctx, ...)` | `workspace.go:130` |
|
||||
| Stop a workspace | `StopWorkspaceAuto(ctx, wsID)` | `workspace.go:172` |
|
||||
| Gate "do we have any backend wired?" | `HasProvisioner()` | `workspace.go:115` |
|
||||
|
||||
Each dispatcher routes to `cpProv.X()` when the SaaS backend is wired, then `provisioner.X()` when the Docker backend is wired, then a defined fallback (`provisionWorkspaceAuto` self-marks-failed; `StopWorkspaceAuto` no-ops; `HasProvisioner` returns false).
|
||||
|
||||
**Rule: do not call `h.cpProv.Stop`, `h.provisioner.Stop`, `h.cpProv.Start`, or `h.provisioner.Start` directly from a handler.** Source-level pins (`TestNoCallSiteCallsDirectProvisionerExceptAuto`, `TestNoCallSiteCallsBareStop`) gate this at CI; they exist because the same drift class shipped twice — TeamHandler.Expand (#2367) bypassed routing on Start, then `team.go:208` + `workspace_crud.go:432` bypassed it on Stop (#2813, #2814) for ~6 months.
|
||||
|
||||
Allowed exceptions (in the source-pin allowlists):
|
||||
- `workspace.go` and `workspace_provision.go` — define the per-backend bodies the dispatcher routes between.
|
||||
- `workspace_restart.go` — pre-dates the dispatchers and uses manual if-cpProv-else dispatch with retry semantics tuned for the restart hot path. Consolidation tracked in #2799.
|
||||
- `container_files.go` — drives the Docker daemon directly for short-lived file-copy containers; no workspace-level Stop semantics involved.
|
||||
|
||||
For "do we have any backend?", use `HasProvisioner()`, never bare `h.provisioner == nil && h.cpProv == nil`. Source-level pin `TestNoBareBothNilCheck` enforces this — added 2026-05-05 after the hongming org-import incident showed the bare check shape was a recurring drift target.
|
||||
|
||||
## The matrix
|
||||
|
||||
| Feature | File(s) | Docker | EC2 | Verdict |
|
||||
|---|---|---|---|---|
|
||||
| **Lifecycle** | | | | |
|
||||
| Create | `workspace_provision.go:19-214` | `provisionWorkspace()` → `provisioner.Start()` | `provisionWorkspaceCP()` → `cpProv.Start()` | ✅ parity |
|
||||
| Create | `workspace.go:130` `provisionWorkspaceAuto` → `provisionWorkspace()` (Docker) / `provisionWorkspaceCP()` (CP) | dispatched | dispatched | ✅ parity (single source of truth, PR #2811) |
|
||||
| Start | `provisioner.go:140-325` | container create + image pull | EC2 `RunInstance` via CP | ✅ parity |
|
||||
| Stop | `provisioner.go:772-785` | `ContainerRemove(force=true)` + optional volume rm | `DELETE /cp/workspaces/:id` | ✅ parity |
|
||||
| Stop | `workspace.go:172` `StopWorkspaceAuto` → `provisioner.Stop()` (Docker) / `cpProv.Stop()` (CP) | dispatched | dispatched | ✅ parity (single source of truth, PR #2824) |
|
||||
| Restart | `workspace_restart.go:45-210` | reads runtime from live container before stop | reads runtime from DB only | ⚠️ divergent — config-change + crash window can boot old runtime on EC2 |
|
||||
| Delete | `workspace_crud.go` | stop + volume rm | stop only (stateless) | ✅ parity (expected divergence on volume cleanup) |
|
||||
| Delete | `workspace_crud.go` `stopAndRemove` → `StopWorkspaceAuto` + Docker-only `RemoveVolume` | stop + volume rm | stop only (stateless — CP has no volumes) | ✅ parity (PR #2824 closed the SaaS-leak gap) |
|
||||
| Org-import (bulk Create) | `org_import.go:178` gates on `h.workspace.HasProvisioner()`; routes through `provisionWorkspaceAuto` per workspace | dispatched | dispatched | ✅ parity (PR #2811 closed the SaaS-skip gate) |
|
||||
| Team-collapse (bulk Stop) | `team.go:206` calls `StopWorkspaceAuto` for each child | dispatched | dispatched | ✅ parity (PR #2824 closed the SaaS-leak gap) |
|
||||
| **Secrets** | | | | |
|
||||
| Create / update | `secrets.go` | DB insert, injected at container start | DB insert, injected via user-data at boot | ✅ parity |
|
||||
| Redaction | `workspace_provision.go:251` | applied at memory-seed time | applied at agent runtime | ⚠️ divergent — timing differs |
|
||||
@@ -76,7 +99,23 @@ This document is the canonical matrix. If you are landing a workspace-facing fea
|
||||
|
||||
- **`tools/check-template-parity.sh`** (this repo) — ensures `install.sh` and `start.sh` in a template repo forward identical sets of provider keys. Wire into each template repo's CI as `bash $MONOREPO/tools/check-template-parity.sh install.sh start.sh`.
|
||||
- **Contract tests** (stub) — `workspace-server/internal/provisioner/backend_contract_test.go` defines the behaviors every `provisioner.Provisioner` implementation must satisfy. Fails compile when a method drifts between `Docker` and `CPProvisioner`. Scenario-level runs are `t.Skip`'d today pending drift risk #6 (see above) — compile-time assertions still catch method drift.
|
||||
- **Source-level dispatcher pins** — `workspace_provision_auto_test.go` enforces the SoT pattern documented above:
|
||||
- `TestNoCallSiteCallsDirectProvisionerExceptAuto` — no handler calls `.provisionWorkspace(` or `.provisionWorkspaceCP(` directly outside the dispatcher's allowlist.
|
||||
- `TestNoCallSiteCallsBareStop` — no handler calls `.provisioner.Stop(` or `.cpProv.Stop(` directly outside the dispatcher's allowlist (strips Go comments before substring match so archaeology in code comments doesn't trip the gate).
|
||||
- `TestNoBareBothNilCheck` — no production code uses `h.provisioner == nil && h.cpProv == nil`; must use `!h.HasProvisioner()`.
|
||||
- `TestOrgImportGate_UsesHasProvisionerNotBareField` — pins the org-import provisioning gate against the bare-Docker-check shape that caused the 2026-05-05 hongming incident.
|
||||
|
||||
## How to update this doc
|
||||
|
||||
When you land a feature that touches a handler dispatch on `h.cpProv != nil`, add or update the matching row. If you can't implement both backends in the same PR, mark the row `docker-only` or `ec2-only` and file an issue tracking the gap.
|
||||
|
||||
### When you add a NEW dispatch site
|
||||
|
||||
If you find yourself writing `if h.cpProv != nil { ... } else if h.provisioner != nil { ... }` for a new operation (Pause, Hibernate, Snapshot, etc.):
|
||||
|
||||
1. Add a `<Op>WorkspaceAuto` method on `WorkspaceHandler` next to the existing dispatchers. Mirror the docstring shape: routing, no-backend fallback, ordering rationale.
|
||||
2. Add a source-level pin in `workspace_provision_auto_test.go` — the bare-call shape your dispatcher replaces, fail when a handler reintroduces it.
|
||||
3. Add a row to the matrix above with the dispatcher reference.
|
||||
4. If your operation has retry semantics specific to a hot path, leave them in the original location for now and file a follow-up under #2799 — don't bake retry into the generic dispatcher unless every caller benefits.
|
||||
|
||||
The pattern is "one dispatcher per verb." Don't fold every operation into `provisionWorkspaceAuto` — different verbs have different no-backend fallbacks (mark-failed for Start, no-op for Stop, false for Has).
|
||||
|
||||
@@ -336,8 +336,6 @@ This same logic governs: A2A delegation, memory scope enforcement, activity visi
|
||||
|
||||
| Method | Endpoint | Purpose |
|
||||
|--------|----------|---------|
|
||||
| `POST` | `/workspaces/:id/expand` | Expand workspace into team (become coordinator) |
|
||||
| `POST` | `/workspaces/:id/collapse` | Collapse team back to single workspace |
|
||||
|
||||
### Files, Terminal, Templates, Bundles (8 endpoints)
|
||||
|
||||
@@ -523,7 +521,8 @@ runtime_config: # Runtime-specific settings
|
||||
skills: ["skill1", "skill2"] # Folder names under skills/
|
||||
tools: ["web_search", "filesystem"] # Built-in tool names
|
||||
prompt_files: ["system-prompt.md"] # Additional prompt text files
|
||||
shared_context: [] # Files from parent workspace
|
||||
# `shared_context` was removed; team-shared knowledge now lives in memory v2's
|
||||
# team:<id> namespace (recall_memory MCP tool). See RFC #2789 for shared files.
|
||||
|
||||
a2a:
|
||||
port: 8000
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# E2E coverage matrix
|
||||
|
||||
This document is the source of truth for which E2E suites guard which surfaces and which gates are wired up where. Read this before adding a new E2E or moving a check between branches.
|
||||
|
||||
## Suites
|
||||
|
||||
| Workflow file | Job (= required-check name) | What it covers | Cron |
|
||||
|---|---|---|---|
|
||||
| `e2e-api.yml` | `E2E API Smoke Test` | A2A handshake, registry/register, /workspaces/:id/a2a forward, structured-event emission. Lightweight enough to run on every PR. | — |
|
||||
| `e2e-staging-canvas.yml` | `Canvas tabs E2E` | Canvas-tab Playwright UX checks against staging — config tab, secrets tab, agent-card tab, Activity hydration. | weekly Sun 08:00 UTC |
|
||||
| `e2e-staging-saas.yml` | `E2E Staging SaaS` | Full lifecycle: org creation → workspace provision (CP path) → A2A delegation → status/heartbeat → workspace delete → EC2 termination. The integration test that catches the silent-drop bug class (#2486 / #2811 / #2813 / #2814). | daily 07:00 UTC |
|
||||
| `e2e-staging-external.yml` | `E2E Staging External Runtime` | External-runtime registration + heartbeat staleness sweep + `/registry/peers` resolution. Validates the OSS-templated workspace path. | daily 07:30 UTC |
|
||||
| `e2e-staging-sanity.yml` | `Intentional-failure teardown sanity` | Inverted assertion — the run MUST fail. Validates the leak-detection self-check itself; not for general gating. | weekly Mon 06:00 UTC |
|
||||
| `continuous-synth-e2e.yml` | `Synthetic E2E against staging` | Standing background coverage between PR runs. Catches drift in production-like staging that PR-time E2Es miss. | every 15 min |
|
||||
|
||||
## Required-check status (branch protection)
|
||||
|
||||
| Suite | staging required | main required |
|
||||
|---|---|---|
|
||||
| `E2E API Smoke Test` | ✅ this PR | ✅ |
|
||||
| `Canvas tabs E2E` | ✅ this PR | (see follow-up) |
|
||||
| `E2E Staging SaaS` | ❌ — needs always-emit refactor | ❌ |
|
||||
| `E2E Staging External Runtime` | ❌ — needs always-emit refactor | ❌ |
|
||||
| `Intentional-failure teardown sanity` | ❌ inverted assertion, never required | ❌ |
|
||||
| `Synthetic E2E against staging` | ❌ cron-only, not a per-PR gate | ❌ |
|
||||
|
||||
## Why the always-emit pattern matters
|
||||
|
||||
Branch protection requires a *check name* to land at SUCCESS for every PR. Workflows with `paths:` filters that exclude a PR never run, so the check name never appears, and the PR sits BLOCKED forever.
|
||||
|
||||
The pattern that supports being required is:
|
||||
|
||||
1. Workflow always triggers on push/PR to the protected branch.
|
||||
2. A `detect-changes` job uses `dorny/paths-filter` to decide if real work runs.
|
||||
3. The protected job runs unconditionally and either (a) does real work when paths matched, or (b) emits a no-op SUCCESS step when paths skipped.
|
||||
|
||||
`e2e-api.yml` and `e2e-staging-canvas.yml` already have this shape. `e2e-staging-saas.yml` and `e2e-staging-external.yml` use plain `paths:` filters and need the refactor before they can be required (filed as follow-up).
|
||||
|
||||
## Adding a new E2E suite
|
||||
|
||||
1. Pick a verb: smoke test, full lifecycle, fault-injection, drift detection. Pre-existing suites split along these lines.
|
||||
2. Use the always-emit shape so the check name can be made required.
|
||||
3. Add a row to the matrix above.
|
||||
4. Decide cron cadence based on cost + how fast drift would otherwise be caught.
|
||||
5. If you want it required, add to the relevant branch protection via `tools/branch-protection/apply.sh` (this PR adds the script).
|
||||
|
||||
## When to break glass — temporarily skip a required E2E
|
||||
|
||||
Don't. If an E2E is intermittently flaky, fix the test or move it out of required. The point of a required check is that it's load-bearing; bypassing one with admin override teaches the next operator the gate is optional.
|
||||
|
||||
If a Production incident requires bypassing, document the override in the incident postmortem with a same-week followup to either fix the test or rip the check out of required.
|
||||
|
||||
## Related issues / PRs
|
||||
|
||||
- #2486 — silent-drop bug class that the SaaS E2E now catches
|
||||
- PR #2811 — `provisionWorkspaceAuto` consolidation (org-import SaaS gate)
|
||||
- PR #2824 — `StopWorkspaceAuto` mirror (closes #2813 + #2814)
|
||||
- Follow-up: refactor `e2e-staging-saas` + `e2e-staging-external` to always-emit (so they can be required)
|
||||
@@ -186,4 +186,3 @@ So the UI now exposes more operational failure state directly instead of silentl
|
||||
- [Quickstart](../quickstart.md)
|
||||
- [Platform API](../api-protocol/platform-api.md)
|
||||
- [Workspace Runtime](../agent-runtime/workspace-runtime.md)
|
||||
- [Team Expansion](../agent-runtime/team-expansion.md)
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ lands in the watch list with a colliding term, add a row here.
|
||||
| **plugin** | A directory under `plugins/` packaging one or more skills or an MCP server wrapper, installable per-workspace via `POST /workspaces/:id/plugins`. Governed by `plugin.yaml`. | **Langflow**: a visual UI node / component in a flowchart. **CrewAI**: a Python-importable callable registered as a capability. |
|
||||
| **agent** | A persistent containerized workspace running continuously — an identity with memory, a role, and a schedule. Not a one-shot invocation. | Most frameworks (AutoGPT, LangChain agents, OpenAI Assistants): a stateless function-call loop. No persistence between invocations unless explicitly checkpointed. |
|
||||
| **flow** | A task execution within a workspace — a request enters, the agent runs tools, emits a response, logs activity. No explicit graph abstraction. | **Langflow**: a directed graph of nodes you author visually. **LangGraph**: a stateful graph of callable nodes. Our "flow" is an imperative timeline, not a graph. |
|
||||
| **team** | A named cluster of workspaces under a PM (org template `expand_team`). Used for role grouping in Canvas. | **CrewAI**: a "crew" is a sequence of agents that pass a task through a declared order. Our "team" is an org-chart abstraction, not an execution order. |
|
||||
| **team** | A named cluster of workspaces under a PM . Used for role grouping in Canvas. | **CrewAI**: a "crew" is a sequence of agents that pass a task through a declared order. Our "team" is an org-chart abstraction, not an execution order. |
|
||||
| **skill** | A directory with `SKILL.md` that an agent invokes via the `Skill` tool. Skills are documentation + optional scripts that teach an agent a recipe. | **Anthropic Skills API**: nearly identical. **CrewAI tool**: closer to our plugin's MCP tool, not our skill. |
|
||||
| **channel** | An outbound/inbound social integration (Telegram, Slack, …) per-workspace, wired in `workspace_channels`. | Slack's "channel": the container for messages. We use "channel" for the adapter + credentials, not the conversation itself. |
|
||||
| **runtime** | The execution engine image tag for a workspace: one of `langgraph`, `claude-code`, `openclaw`, `crewai`, `autogen`, `deepagents`, `hermes`. | **LangGraph runtime**: the Python process running the graph. We use "runtime" for the Docker image + adapter pairing, not the inner process. |
|
||||
|
||||
@@ -166,8 +166,6 @@ list_workspaces
|
||||
|
||||
| MCP Tool | API Route | Method | Description |
|
||||
|----------|-----------|--------|-------------|
|
||||
| `expand_team` | `/workspaces/:id/expand` | POST | Expand team node |
|
||||
| `collapse_team` | `/workspaces/:id/collapse` | POST | Collapse team node |
|
||||
|
||||
### Templates & Bundles
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
# Memory Plugin Contract — Changelog
|
||||
|
||||
Every breaking or operationally-relevant change to the v1 plugin
|
||||
contract or the workspace-server-side wiring lands here. Plugin
|
||||
authors should subscribe to PRs touching this file.
|
||||
|
||||
## [Unreleased] — fixup wave 1 (post-RFC-#2728 self-review)
|
||||
|
||||
A self-review of the initial 11-PR rollout (PRs #2729-#2742) flagged
|
||||
two correctness bugs and three operational hazards. This wave fixes
|
||||
all of them. Order matches operator-impact severity.
|
||||
|
||||
### Critical: backfill idempotency via `MemoryWrite.id` (#2744)
|
||||
|
||||
**The bug.** The backfill CLI claimed idempotent on re-run, but
|
||||
`gen_random_uuid()` in the plugin's INSERT meant every retry created
|
||||
a fresh row. Operators retrying a failed `-apply` would silently
|
||||
double their memory count.
|
||||
|
||||
**The fix.** Optional `id` field on `MemoryWrite`. When supplied,
|
||||
plugins MUST upsert. The backfill now forwards `agent_memories.id`
|
||||
to `MemoryWrite.id`, so retries update in place.
|
||||
|
||||
**Plugin author action.** If your plugin uses
|
||||
`INSERT INTO ... DEFAULT gen_random_uuid()`, switch to
|
||||
`INSERT ... ON CONFLICT (id) DO UPDATE` when `id` is set. The wire
|
||||
contract is forward-compatible — plugins that ignore the field still
|
||||
work for production agent commits (which leave `id` empty), but they
|
||||
will silently corrupt backfill retries.
|
||||
|
||||
### Critical: `memory-backfill -verify` mode (#2747)
|
||||
|
||||
**The miss.** The original PR-7 task spec called for a parity-check
|
||||
mode but it never landed. Operators had no way to confirm a
|
||||
migration succeeded short of "no errors logged."
|
||||
|
||||
**The fix.** New `-verify` flag samples N workspaces, queries
|
||||
`agent_memories` direct, runs an equivalent plugin search via the
|
||||
namespace resolver, multiset-compares contents. Reports mismatches
|
||||
to stdout and exits non-zero so CI can gate the cutover.
|
||||
|
||||
```bash
|
||||
memory-backfill -verify # default sample 50
|
||||
memory-backfill -verify -verify-sample=200 # bigger
|
||||
memory-backfill -verify -workspace=<uuid> # one workspace
|
||||
```
|
||||
|
||||
### Important: `expires_at` validation (#2746)
|
||||
|
||||
**The bug.** `commit_memory_v2` silently dropped malformed
|
||||
`expires_at` strings. Agent passes `expires_at: "tomorrow"`, gets a
|
||||
200, memory has no TTL — agent thinks it set a TTL, didn't.
|
||||
|
||||
**The fix.** Returns
|
||||
`fmt.Errorf("invalid expires_at: must be RFC3339")` on parse
|
||||
failure. Plugin is not called in this case.
|
||||
|
||||
**Plugin author action.** None — this is a workspace-server-side
|
||||
fix. But: if your plugin advertises the `ttl` capability, make sure
|
||||
you actually evict expired rows on read (not just on a janitor cron
|
||||
that runs once a day). The harness in `testing-your-plugin.md` has
|
||||
a TTL-eviction test you should run.
|
||||
|
||||
### Important: audit log JSON via `json.Marshal` (#2746)
|
||||
|
||||
**The bug.** `auditOrgWrite` built `activity_logs.metadata` via
|
||||
`fmt.Sprintf` with `%q`. For ASCII (today's UUID + hex digest) this
|
||||
coincidentally produces valid JSON; for unicode or control bytes it
|
||||
silently produces non-JSON.
|
||||
|
||||
**The fix.** Replaced with `json.Marshal(map[string]string{...})`.
|
||||
Same wire shape today, won't regress when metadata grows.
|
||||
|
||||
**Plugin author action.** None — workspace-server-internal.
|
||||
|
||||
### Operator action: staging verification (#292)
|
||||
|
||||
**Status.** Tracked as task #292. PR-merged ≠ verified. Operator
|
||||
must:
|
||||
1. Provision a staging tenant, set `MEMORY_PLUGIN_URL`
|
||||
2. Run real `commit_memory_v2` from a workspace
|
||||
3. `memory-backfill -dry-run` against staging data
|
||||
4. `memory-backfill -apply`, then `-verify`
|
||||
5. Set `MEMORY_V2_CUTOVER=true`, verify admin export still works
|
||||
6. Run a legacy `commit_memory` from a workspace, verify it lands
|
||||
in plugin storage via the PR-6 shim
|
||||
|
||||
### Other follow-ups still open
|
||||
|
||||
- **#289**: admin export O(workspaces) → O(namespaces) — N+1 pattern
|
||||
in `exportViaPlugin` (1000-workspace tenants run 1000× resolver
|
||||
CTEs + 1000× plugin searches today).
|
||||
- **#291**: workspace deletion must call `DELETE
|
||||
/v1/namespaces/{name}` — orphans accumulate today.
|
||||
- **#293**: real-subprocess boot E2E — current PR-11 is integration
|
||||
(httptest + sqlmock), not E2E.
|
||||
|
||||
These are tracked but deferred; they're operationally annoying, not
|
||||
incident-shaped.
|
||||
|
||||
## [v1.0.0] — initial release (RFC #2728, PRs #2729-#2742)
|
||||
|
||||
Initial plugin contract + 11-PR rollout. See
|
||||
[issue #2728](https://github.com/Molecule-AI/molecule-core/issues/2728)
|
||||
for the full RFC.
|
||||
|
||||
Endpoints: `/v1/health`, `/v1/namespaces/{name}` (PUT/PATCH/DELETE),
|
||||
`/v1/namespaces/{name}/memories` (POST), `/v1/search` (POST),
|
||||
`/v1/memories/{id}` (DELETE).
|
||||
|
||||
Capabilities: `embedding`, `fts`, `ttl`, `pin`, `propagation`.
|
||||
|
||||
Operator runbook: see [README.md § Replacing the built-in plugin](README.md#replacing-the-built-in-plugin).
|
||||
@@ -0,0 +1,191 @@
|
||||
# Writing a Memory Plugin
|
||||
|
||||
This document is for operators and ecosystem authors who want to
|
||||
replace the built-in postgres-backed memory plugin (the default
|
||||
implementation that ships with workspace-server) with their own.
|
||||
|
||||
The contract was introduced by RFC #2728. The shipped binary is
|
||||
`cmd/memory-plugin-postgres/`; reading its source is the fastest way
|
||||
to see a complete reference implementation.
|
||||
|
||||
## What the contract is
|
||||
|
||||
The plugin is an HTTP server that workspace-server talks to via the
|
||||
OpenAPI v1 spec at [`docs/api-protocol/memory-plugin-v1.yaml`](../api-protocol/memory-plugin-v1.yaml).
|
||||
|
||||
Six endpoints:
|
||||
|
||||
| Endpoint | Method | Purpose |
|
||||
|---|---|---|
|
||||
| `/v1/health` | GET | Liveness probe + capability list |
|
||||
| `/v1/namespaces/{name}` | PUT | Idempotent upsert |
|
||||
| `/v1/namespaces/{name}` | PATCH | Update TTL or metadata |
|
||||
| `/v1/namespaces/{name}` | DELETE | Remove namespace and its memories |
|
||||
| `/v1/namespaces/{name}/memories` | POST | Write a memory |
|
||||
| `/v1/search` | POST | Multi-namespace search |
|
||||
| `/v1/memories/{id}` | DELETE | Forget a memory |
|
||||
|
||||
The wire types are defined in
|
||||
`workspace-server/internal/memory/contract/contract.go`. Run-time
|
||||
validation is built into the Go bindings via `Validate()` methods —
|
||||
your plugin SHOULD perform equivalent validation.
|
||||
|
||||
## What workspace-server takes care of
|
||||
|
||||
You do **not** implement these in the plugin; workspace-server is the
|
||||
security perimeter:
|
||||
|
||||
- **Secret redaction** (SAFE-T1201). All `content` you receive is
|
||||
already scrubbed. Don't run additional redaction; it's pointless.
|
||||
- **Namespace ACL**. workspace-server intersects the caller's
|
||||
readable namespaces against the requested list before sending you
|
||||
the search request. The list you receive is authoritative.
|
||||
- **GLOBAL audit**. Org-namespace writes are recorded in
|
||||
`activity_logs` server-side; you don't see them.
|
||||
- **Prompt-injection wrap**. Org memories returned to agents get a
|
||||
`[MEMORY id=... scope=ORG ns=...]:` prefix added at the
|
||||
workspace-server layer. Your `content` field is plain text.
|
||||
|
||||
## What you implement
|
||||
|
||||
- Storage of `memory_namespaces` and `memory_records` (or whatever
|
||||
shape you want — Pinecone vectors, an in-memory map, etc.)
|
||||
- The 7 endpoints above with the request/response shapes the spec
|
||||
defines
|
||||
- `/v1/health` reporting your supported capabilities (see below)
|
||||
- Idempotency on namespace upsert (PUT semantics, not POST)
|
||||
- Idempotency on memory commit when `MemoryWrite.id` is supplied
|
||||
(see "Memory idempotency" below)
|
||||
|
||||
## Memory idempotency
|
||||
|
||||
`MemoryWrite.id` is optional. Two contracts to honor:
|
||||
|
||||
| Caller passes | Plugin MUST |
|
||||
|---|---|
|
||||
| `id` omitted | Generate a fresh UUID, return it in the response |
|
||||
| `id` set | Upsert keyed on this id — if a row with that id already exists, UPDATE it in place rather than inserting a duplicate |
|
||||
|
||||
The backfill CLI (`memory-backfill`) relies on the upsert behavior
|
||||
so retries don't duplicate rows. Production agent commits leave `id`
|
||||
empty and rely on the plugin's UUID generator — the hot path is
|
||||
unchanged.
|
||||
|
||||
The built-in postgres plugin implements this with `INSERT ... ON
|
||||
CONFLICT (id) DO UPDATE`. A vector-DB plugin (e.g., Pinecone) would
|
||||
use the database's native upsert primitive on the same id.
|
||||
|
||||
## Capability negotiation
|
||||
|
||||
Your `/v1/health` response declares what features you support:
|
||||
|
||||
```json
|
||||
{
|
||||
"status": "ok",
|
||||
"version": "1.0.0",
|
||||
"capabilities": ["embedding", "fts", "ttl", "pin", "propagation"]
|
||||
}
|
||||
```
|
||||
|
||||
| Capability | What it gates |
|
||||
|---|---|
|
||||
| `embedding` | Agents may ask for semantic search; you receive `embedding: [...]` in search bodies |
|
||||
| `fts` | Agents may pass a query string; you decide how to match (FTS, ILIKE, regex) |
|
||||
| `ttl` | Agents may set `expires_at`; you must not return expired rows |
|
||||
| `pin` | Agents may set `pin: true`; you should rank pinned rows first |
|
||||
| `propagation` | Agents may set `propagation: {...}`; you must store it as opaque JSON and return it on read |
|
||||
|
||||
A capability you DON'T list is fine — workspace-server adapts the MCP
|
||||
tool surface to match. E.g., a Pinecone-only plugin that lists only
|
||||
`embedding` will silently ignore agents' `query` strings.
|
||||
|
||||
## Deployment models
|
||||
|
||||
Three common shapes:
|
||||
|
||||
1. **Same machine, different process**: workspace-server boots, then
|
||||
`MEMORY_PLUGIN_URL=http://localhost:9100` points at your plugin
|
||||
running on a unix socket or localhost port. This is what the
|
||||
built-in postgres plugin does.
|
||||
|
||||
2. **Separate container**: deploy your plugin as its own service on
|
||||
the private network. Set `MEMORY_PLUGIN_URL` to its DNS name.
|
||||
|
||||
3. **Self-managed**: customer-owned plugin running on customer-owned
|
||||
infrastructure, accessed over a tunnel. Same env-var wiring.
|
||||
|
||||
Auth is **none** — the plugin must be reachable only on a private
|
||||
network. workspace-server is the only sanctioned client.
|
||||
|
||||
## Replacing the built-in plugin
|
||||
|
||||
This is the canonical operator runbook for swapping the default
|
||||
plugin out. The same sequence applies whether you're swapping for
|
||||
another postgres plugin variant, Pinecone, Letta, or a custom
|
||||
implementation.
|
||||
|
||||
1. **Stand up the new plugin.** Deploy the binary/container, confirm
|
||||
it boots, confirm `/v1/health` returns `ok` with the capability
|
||||
list you expect.
|
||||
|
||||
2. **Run the backfill in dry-run mode** to scope the migration:
|
||||
```bash
|
||||
DATABASE_URL=postgres://... \
|
||||
MEMORY_PLUGIN_URL=http://your-plugin:9100 \
|
||||
memory-backfill -dry-run
|
||||
```
|
||||
Reports row count + namespace mapping per workspace, no writes.
|
||||
|
||||
3. **Apply the backfill:**
|
||||
```bash
|
||||
memory-backfill -apply
|
||||
```
|
||||
Idempotent on retry — the backfill passes each `agent_memories.id`
|
||||
to `MemoryWrite.id`, so partial-then-full re-runs upsert in place.
|
||||
|
||||
4. **Verify parity** before flipping the cutover flag:
|
||||
```bash
|
||||
memory-backfill -verify -verify-sample=200
|
||||
```
|
||||
Random-samples N workspaces, diffs `agent_memories` direct query
|
||||
against plugin search via the workspace's readable namespaces.
|
||||
Reports mismatches and exits non-zero if any are found — wire
|
||||
into your CI to gate the cutover.
|
||||
|
||||
5. **Flip the cutover flag.** Set `MEMORY_V2_CUTOVER=true` on
|
||||
workspace-server and restart. Admin export/import now route
|
||||
through the plugin; legacy `agent_memories` becomes read-only.
|
||||
|
||||
6. **Existing data in the old plugin's tables is NOT auto-dropped.**
|
||||
Deliberate safety property — operator drops manually after the
|
||||
~60-day grace window. If you switch back later, old data comes
|
||||
back into use (no loss).
|
||||
|
||||
If `-verify` reports mismatches, do NOT set `MEMORY_V2_CUTOVER` —
|
||||
inspect the output, re-run `-apply` to backfill missing rows (it
|
||||
upserts, so this is safe), and re-verify.
|
||||
|
||||
## Worked examples
|
||||
|
||||
- [`pinecone-example/`](pinecone-example/) — full Pinecone-backed plugin
|
||||
- [`testing-your-plugin.md`](testing-your-plugin.md) — running the
|
||||
contract test harness against your implementation
|
||||
|
||||
## When to write one vs. fork the default
|
||||
|
||||
Fork the default postgres plugin if:
|
||||
- You want different SQL (Materialized views? Different vector index?)
|
||||
- You want extra auth on top
|
||||
- You want server-side metrics emission
|
||||
|
||||
Write a fresh plugin if:
|
||||
- The storage backend is fundamentally different (vector DB, KV store,
|
||||
in-memory, file-based)
|
||||
- You're integrating an existing memory service (Letta, Mem0, etc.)
|
||||
|
||||
## See also
|
||||
|
||||
- [`CHANGELOG.md`](CHANGELOG.md) — contract revisions and fixup waves
|
||||
- RFC #2728 — design rationale
|
||||
- [`cmd/memory-plugin-postgres/`](../../workspace-server/cmd/memory-plugin-postgres/) — reference implementation
|
||||
- [`docs/api-protocol/memory-plugin-v1.yaml`](../api-protocol/memory-plugin-v1.yaml) — full OpenAPI spec
|
||||
@@ -0,0 +1,124 @@
|
||||
# Pinecone-backed Memory Plugin (worked example)
|
||||
|
||||
A working sketch of a memory plugin that delegates storage to
|
||||
[Pinecone](https://www.pinecone.io/) instead of postgres.
|
||||
|
||||
This is **example code, not a production binary**. It demonstrates
|
||||
how to map the v1 contract onto a vector database. Operators who
|
||||
want to ship this would harden auth, add retries, batch the
|
||||
commit path, etc.
|
||||
|
||||
## Why Pinecone is interesting
|
||||
|
||||
The default postgres plugin's pgvector index works for ~10M memories
|
||||
on a single node. Beyond that, semantic search becomes painful. A
|
||||
managed vector database can handle 1B+ memories, but the trade-offs
|
||||
are different:
|
||||
|
||||
- **Capabilities**: Pinecone is great at `embedding` (its core
|
||||
feature) but has no first-class FTS. So the plugin reports
|
||||
`["embedding"]` and ignores the `query` field.
|
||||
- **TTL**: Pinecone supports per-vector metadata with deletion via
|
||||
metadata filter — TTL becomes a periodic janitor task, not a
|
||||
per-row property.
|
||||
- **Cost**: per-vector billing, so the plugin should batch writes
|
||||
and dedup before posting.
|
||||
|
||||
## Wire mapping
|
||||
|
||||
| Contract field | Pinecone shape |
|
||||
|---|---|
|
||||
| `namespace` | `namespace` (Pinecone's first-class concept) |
|
||||
| `id` (caller-supplied) | `id` (Pinecone vector id; plugin upserts on this) |
|
||||
| `id` (omitted) | Plugin generates `uuid.NewString()` before upsert |
|
||||
| `content` | metadata.text |
|
||||
| `embedding` | `values` |
|
||||
| `kind` / `source` / `pin` / `expires_at` | `metadata.{kind, source, pin, expires_at}` |
|
||||
| `propagation` (opaque JSON) | `metadata.propagation` (also opaque) |
|
||||
|
||||
The contract's `expires_at` becomes a metadata field; a separate
|
||||
janitor cron periodically queries `expires_at < now` and deletes.
|
||||
|
||||
Pinecone's native upsert is the right fit for the idempotency-key
|
||||
contract: passing the same `id` twice updates in place. So a
|
||||
Pinecone plugin gets idempotent backfill retries "for free" if it
|
||||
just forwards `MemoryWrite.id` (or its generated UUID) to the
|
||||
upsert call.
|
||||
|
||||
## Skeleton
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/pinecone-io/go-pinecone/pinecone"
|
||||
)
|
||||
|
||||
type pineconePlugin struct {
|
||||
client *pinecone.Client
|
||||
index string
|
||||
}
|
||||
|
||||
func main() {
|
||||
apiKey := os.Getenv("PINECONE_API_KEY")
|
||||
if apiKey == "" {
|
||||
log.Fatal("PINECONE_API_KEY required")
|
||||
}
|
||||
client, err := pinecone.NewClient(pinecone.NewClientParams{ApiKey: apiKey})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
p := &pineconePlugin{client: client, index: os.Getenv("PINECONE_INDEX")}
|
||||
|
||||
http.HandleFunc("/v1/health", p.health)
|
||||
http.HandleFunc("/v1/search", p.search)
|
||||
// ... rest of the routes ...
|
||||
|
||||
log.Fatal(http.ListenAndServe(":9100", nil))
|
||||
}
|
||||
|
||||
func (p *pineconePlugin) health(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "ok",
|
||||
"version": "1.0.0",
|
||||
"capabilities": []string{"embedding"}, // no FTS, no TTL out-of-box
|
||||
})
|
||||
}
|
||||
|
||||
func (p *pineconePlugin) search(w http.ResponseWriter, r *http.Request) {
|
||||
// Parse contract.SearchRequest
|
||||
// Build Pinecone QueryByVectorValuesRequest with body.Embedding
|
||||
// For each Pinecone namespace in body.Namespaces, call Query
|
||||
// Map results to contract.Memory
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## What's missing from this sketch
|
||||
|
||||
A production-ready Pinecone plugin would add:
|
||||
|
||||
- **Batch commits**: bulk upsert N memories in a single Pinecone call
|
||||
- **TTL janitor**: periodic deletion of expired vectors
|
||||
- **Connection pooling**: keep one Pinecone client alive across requests
|
||||
- **Retry + circuit breaker**: Pinecone occasionally returns 5xx
|
||||
- **Metrics**: latency histograms per endpoint, write/read counters
|
||||
- **Idempotency-key handling**: when `MemoryWrite.id` is supplied,
|
||||
forward it as the Pinecone vector id verbatim; otherwise generate
|
||||
one. Pinecone's `Upsert` is naturally idempotent on id match.
|
||||
|
||||
But the mapping above is the load-bearing part — the rest is
|
||||
operational hardening, not contract-specific.
|
||||
|
||||
## See also
|
||||
|
||||
- [Pinecone Go SDK docs](https://docs.pinecone.io/reference/go-sdk)
|
||||
- [Memory plugin contract spec](../../api-protocol/memory-plugin-v1.yaml)
|
||||
- [Default postgres plugin source](../../../workspace-server/cmd/memory-plugin-postgres/) — for comparison
|
||||
@@ -0,0 +1,181 @@
|
||||
# Testing Your Memory Plugin
|
||||
|
||||
Once you have a plugin implementing the v1 contract, you can validate
|
||||
it against the spec without booting workspace-server.
|
||||
|
||||
## The contract test harness
|
||||
|
||||
Workspace-server ships typed Go bindings + round-trip tests in
|
||||
`workspace-server/internal/memory/contract/`. The simplest way to
|
||||
gain confidence in your plugin's wire compatibility is to point those
|
||||
tests at it.
|
||||
|
||||
A minimal contract suite:
|
||||
|
||||
```go
|
||||
package myplugin_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
mclient "github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/client"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
)
|
||||
|
||||
func TestMyPlugin_FullRoundTrip(t *testing.T) {
|
||||
// Start your plugin somehow (subprocess, in-process, etc.)
|
||||
pluginURL := startMyPlugin(t)
|
||||
cl := mclient.New(mclient.Config{BaseURL: pluginURL})
|
||||
|
||||
// 1. Health
|
||||
hr, err := cl.Boot(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Boot: %v", err)
|
||||
}
|
||||
if hr.Status != "ok" {
|
||||
t.Errorf("status = %q", hr.Status)
|
||||
}
|
||||
|
||||
// 2. Namespace upsert
|
||||
if _, err := cl.UpsertNamespace(context.Background(), "workspace:test-1",
|
||||
contract.NamespaceUpsert{Kind: contract.NamespaceKindWorkspace}); err != nil {
|
||||
t.Fatalf("UpsertNamespace: %v", err)
|
||||
}
|
||||
|
||||
// 3. Commit memory
|
||||
resp, err := cl.CommitMemory(context.Background(), "workspace:test-1",
|
||||
contract.MemoryWrite{
|
||||
Content: "hello",
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CommitMemory: %v", err)
|
||||
}
|
||||
if resp.ID == "" {
|
||||
t.Errorf("plugin must return a non-empty memory id")
|
||||
}
|
||||
|
||||
// 4. Search
|
||||
sresp, err := cl.Search(context.Background(), contract.SearchRequest{
|
||||
Namespaces: []string{"workspace:test-1"},
|
||||
Query: "hello",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Search: %v", err)
|
||||
}
|
||||
if len(sresp.Memories) == 0 {
|
||||
t.Errorf("plugin returned no memories for the query we just wrote")
|
||||
}
|
||||
|
||||
// 5. Forget
|
||||
if err := cl.ForgetMemory(context.Background(), resp.ID,
|
||||
contract.ForgetRequest{RequestedByNamespace: "workspace:test-1"}); err != nil {
|
||||
t.Errorf("ForgetMemory: %v", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Testing idempotency
|
||||
|
||||
The contract requires that `MemoryWrite.id`, when supplied, behaves
|
||||
as an upsert key. The backfill CLI relies on this — without it,
|
||||
operator retries silently duplicate every memory.
|
||||
|
||||
```go
|
||||
func TestMyPlugin_IDIsIdempotencyKey(t *testing.T) {
|
||||
pluginURL := startMyPlugin(t)
|
||||
cl := mclient.New(mclient.Config{BaseURL: pluginURL})
|
||||
if _, err := cl.UpsertNamespace(context.Background(), "workspace:test-1",
|
||||
contract.NamespaceUpsert{Kind: contract.NamespaceKindWorkspace}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fixedID := "11111111-2222-3333-4444-555555555555"
|
||||
|
||||
// First write with a specific id.
|
||||
resp1, err := cl.CommitMemory(context.Background(), "workspace:test-1",
|
||||
contract.MemoryWrite{
|
||||
ID: fixedID,
|
||||
Content: "first version",
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("first commit: %v", err)
|
||||
}
|
||||
if resp1.ID != fixedID {
|
||||
t.Errorf("plugin must echo the supplied id, got %q", resp1.ID)
|
||||
}
|
||||
|
||||
// Second write with the same id — must update, not insert.
|
||||
if _, err := cl.CommitMemory(context.Background(), "workspace:test-1",
|
||||
contract.MemoryWrite{
|
||||
ID: fixedID,
|
||||
Content: "second version (updated)",
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
}); err != nil {
|
||||
t.Fatalf("second commit: %v", err)
|
||||
}
|
||||
|
||||
// Search must return exactly one row, with the updated content.
|
||||
sresp, _ := cl.Search(context.Background(), contract.SearchRequest{
|
||||
Namespaces: []string{"workspace:test-1"},
|
||||
})
|
||||
matches := 0
|
||||
for _, m := range sresp.Memories {
|
||||
if m.ID == fixedID {
|
||||
matches++
|
||||
if m.Content != "second version (updated)" {
|
||||
t.Errorf("upsert didn't update content: got %q", m.Content)
|
||||
}
|
||||
}
|
||||
}
|
||||
if matches != 1 {
|
||||
t.Errorf("upsert produced %d rows for id=%s, want 1", matches, fixedID)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## What the harness does NOT cover
|
||||
|
||||
- **Capability accuracy**: if you list `embedding` you must actually
|
||||
do semantic search. The harness can't tell you whether ranking is
|
||||
meaningful — only that you don't crash.
|
||||
- **TTL eviction**: write a memory with `expires_at` 1 second in the
|
||||
future, sleep 2 seconds, search — assert the memory is gone.
|
||||
- **Concurrency**: hit your plugin with 100 parallel writes; assert
|
||||
no IDs collide.
|
||||
- **Recovery**: kill your plugin's storage backend, send a request,
|
||||
assert your plugin returns 503 (not 200 with stale data).
|
||||
- **Backfill compatibility**: run the operator backfill against your
|
||||
plugin twice in a row (`memory-backfill -apply`); assert the row
|
||||
count doesn't double. The idempotency test above verifies the unit
|
||||
contract; this checks the operational integration.
|
||||
- **Verify-mode parity**: after a backfill, run `memory-backfill
|
||||
-verify`; assert it reports zero mismatches against
|
||||
`agent_memories`.
|
||||
|
||||
## Smoke test against workspace-server
|
||||
|
||||
Once unit-level wire tests pass, run a real workspace-server with your
|
||||
plugin URL:
|
||||
|
||||
```bash
|
||||
DATABASE_URL=postgres://... \
|
||||
MEMORY_PLUGIN_URL=http://localhost:9100 \
|
||||
./workspace-server
|
||||
```
|
||||
|
||||
Then ask an agent to call `commit_memory_v2` and `search_memory`. If
|
||||
both round-trip cleanly, you're done.
|
||||
|
||||
For the full E2E flow (including the namespace resolver, MCP layer,
|
||||
and security perimeter), see [PR-11's plugin-swap test](../../workspace-server/test/e2e/memory_plugin_swap_test.go).
|
||||
|
||||
## Reporting bugs
|
||||
|
||||
If you find a contract ambiguity or missing edge case, file an issue
|
||||
against `Molecule-AI/molecule-core` referencing RFC #2728.
|
||||
@@ -55,9 +55,13 @@ TOP_LEVEL_MODULES = {
|
||||
"a2a_executor",
|
||||
"a2a_mcp_server",
|
||||
"a2a_tools",
|
||||
"a2a_tools_delegation",
|
||||
"a2a_tools_rbac",
|
||||
"adapter_base",
|
||||
"agent",
|
||||
"agents_md",
|
||||
"boot_routes",
|
||||
"card_helpers",
|
||||
"config",
|
||||
"configs_dir",
|
||||
"consolidation",
|
||||
@@ -67,18 +71,24 @@ TOP_LEVEL_MODULES = {
|
||||
"executor_helpers",
|
||||
"heartbeat",
|
||||
"inbox",
|
||||
"inbox_uploads",
|
||||
"initial_prompt",
|
||||
"internal_chat_uploads",
|
||||
"internal_file_read",
|
||||
"main",
|
||||
"mcp_cli",
|
||||
"mcp_heartbeat",
|
||||
"mcp_inbox_pollers",
|
||||
"mcp_workspace_resolver",
|
||||
"molecule_ai_status",
|
||||
"not_configured_handler",
|
||||
"platform_auth",
|
||||
"platform_inbound_auth",
|
||||
"plugins",
|
||||
"preflight",
|
||||
"prompt",
|
||||
"runtime_wedge",
|
||||
"secret_redactor",
|
||||
"shared_runtime",
|
||||
"smoke_mode",
|
||||
"transcript_auth",
|
||||
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
# lint_cleanup_traps.sh — regression gate for the OSS-shape program's
|
||||
# "all E2E tests must have proper cleanup" bar (RFC #2873).
|
||||
#
|
||||
# Asserts: every shell file under tests/e2e/ that calls `mktemp` ALSO
|
||||
# installs an `EXIT` trap somewhere in the file. The trap is the
|
||||
# minimum-viable guarantee that scratch files won't leak when an
|
||||
# assertion or curl exits the script non-zero.
|
||||
#
|
||||
# Why this lints (instead of the test runner enforcing): shell scripts
|
||||
# can't easily be wrapped by an outer harness without breaking the
|
||||
# `WSID=… ./test_x.sh` invocation contract. Static gate is the cheap
|
||||
# defense.
|
||||
#
|
||||
# Usage:
|
||||
# tests/e2e/lint_cleanup_traps.sh
|
||||
#
|
||||
# Exits non-zero if any test_*.sh has unmatched mktemp/trap. CI invokes
|
||||
# it from the existing Shellcheck (E2E scripts) workflow.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
violations=0
|
||||
for f in test_*.sh; do
|
||||
if grep -qE '\bmktemp\b' "$f"; then
|
||||
if ! grep -qE 'trap[[:space:]]+.*EXIT' "$f"; then
|
||||
echo "::error file=tests/e2e/$f::has 'mktemp' but no 'trap … EXIT' — scratch will leak when test exits non-zero. Pattern: TMPDIR_E2E=\$(mktemp -d -t prefix-XXX); trap 'rm -rf \"\$TMPDIR_E2E\"' EXIT INT TERM"
|
||||
violations=$((violations + 1))
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$violations" -gt 0 ]; then
|
||||
echo "::error::$violations shell E2E file(s) leak scratch on early exit. See above."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✓ all $(grep -lE '\bmktemp\b' test_*.sh | wc -l | tr -d ' ') shell E2E files with mktemp also install an EXIT trap"
|
||||
@@ -22,6 +22,13 @@ set -euo pipefail
|
||||
WSID="${WSID:?WSID=<workspace-id> required}"
|
||||
BASE="${BASE:-http://localhost:8080}"
|
||||
|
||||
# Per-run scratch dir collected under one trap so every mktemp leak path
|
||||
# (assertion failure, SIGINT, exit non-zero) is plugged. Pre-fix this test
|
||||
# created a /tmp/hermes-e2e-XXXXXX.txt and never deleted it — ~10 KB ×
|
||||
# every CI run leaked into the runner. RFC #2873 cleanup-hygiene PR.
|
||||
TMPDIR_E2E=$(mktemp -d -t chat-attachments-e2e-XXXXXX)
|
||||
trap 'rm -rf "$TMPDIR_E2E"' EXIT INT TERM
|
||||
|
||||
log() { printf "\n=== %s ===\n" "$*"; }
|
||||
|
||||
log "Preflight: workspace online?"
|
||||
@@ -29,7 +36,9 @@ STATUS=$(curl -s "$BASE/workspaces/$WSID" | python3 -c 'import json,sys;print(js
|
||||
[ "$STATUS" = "online" ] || { echo "workspace not online ($STATUS)"; exit 1; }
|
||||
|
||||
log "Step 1 — Upload a text file via /chat/uploads"
|
||||
TEST_FILE=$(mktemp -t hermes-e2e-XXXXXX.txt)
|
||||
# `mktemp <full-template>` is portable across BSD (macOS) + GNU; -p is
|
||||
# GNU-only and breaks local dev runs on Mac.
|
||||
TEST_FILE=$(mktemp "$TMPDIR_E2E/hermes-e2e-XXXXXX.txt")
|
||||
echo "secret code: $(openssl rand -hex 4)-$(openssl rand -hex 4)" > "$TEST_FILE"
|
||||
EXPECTED=$(cat "$TEST_FILE" | awk '{print $NF}')
|
||||
UPLOAD=$(curl -s -X POST "$BASE/workspaces/$WSID/chat/uploads" -F "files=@$TEST_FILE")
|
||||
|
||||
@@ -24,6 +24,15 @@ set -uo pipefail
|
||||
BASE="${BASE:-http://localhost:8080}"
|
||||
fails=0
|
||||
|
||||
# Per-run scratch dir collected under one trap so every per-runtime
|
||||
# round_trip mktemp leak path (assertion failure, SIGINT, exit
|
||||
# non-zero, function early-return between mktemp and rm) is plugged.
|
||||
# Pre-fix, round_trip's `rm -f "$test_file"` only fired on the success
|
||||
# path inside the function — every test_failure path before the rm
|
||||
# leaked the scratch into /tmp permanently. RFC #2873 cleanup-hygiene PR.
|
||||
TMPDIR_E2E=$(mktemp -d -t mr-attachments-e2e-XXXXXX)
|
||||
trap 'rm -rf "$TMPDIR_E2E"' EXIT INT TERM
|
||||
|
||||
has_patch_in_container() {
|
||||
local container="$1"
|
||||
# Signal that platform helpers are available AND wired into the
|
||||
@@ -74,12 +83,16 @@ print(f"executor: claude-code monkey-patch active ({name})")
|
||||
round_trip() {
|
||||
local label="$1" wsid="$2"
|
||||
local test_file expected upload uri payload reply reply_text
|
||||
test_file=$(mktemp -t e2e-mr-XXXX.txt)
|
||||
# Scratch goes under TMPDIR_E2E; the script-level trap rm -rf's the
|
||||
# whole dir on exit, so per-file rm calls are unnecessary AND make
|
||||
# error paths leak when forgotten.
|
||||
# `mktemp <full-template>` is portable across BSD (macOS) + GNU; -p is GNU-only.
|
||||
test_file=$(mktemp "$TMPDIR_E2E/e2e-mr-${label}-XXXX.txt")
|
||||
expected="secret-$(openssl rand -hex 6)"
|
||||
echo "$expected" > "$test_file"
|
||||
upload=$(curl -s -X POST "$BASE/workspaces/$wsid/chat/uploads" -F "files=@$test_file")
|
||||
uri=$(echo "$upload" | python3 -c 'import json,sys;print(json.load(sys.stdin)["files"][0]["uri"])' 2>/dev/null)
|
||||
[ -z "$uri" ] && { echo "FAIL $label: upload returned no URI: $upload"; rm -f "$test_file"; return 1; }
|
||||
[ -z "$uri" ] && { echo "FAIL $label: upload returned no URI: $upload"; return 1; }
|
||||
payload=$(URI="$uri" python3 -c '
|
||||
import json, os
|
||||
uri = os.environ["URI"]
|
||||
@@ -103,7 +116,8 @@ try:
|
||||
except Exception as exc:
|
||||
print(f"(parse failed: {exc})")
|
||||
' 2>&1)
|
||||
rm -f "$test_file"
|
||||
# $test_file lives under TMPDIR_E2E; the script-level trap rm -rf's
|
||||
# the dir on exit, covering every return path including SIGINT.
|
||||
|
||||
if echo "$reply_text" | grep -qF "$expected"; then
|
||||
echo "PASS $label round-trip: agent quoted $expected"
|
||||
|
||||
@@ -29,11 +29,20 @@ FAIL=0
|
||||
WSID=""
|
||||
|
||||
cleanup() {
|
||||
# Workspace teardown — best-effort, ignore errors so an unrelated CP
|
||||
# outage doesn't shadow a real test failure.
|
||||
if [ -n "$WSID" ]; then
|
||||
curl -s -X DELETE "$BASE/workspaces/$WSID?confirm=true" > /dev/null || true
|
||||
fi
|
||||
# /tmp scratch — pre-fix only ran on success path (the unconditional
|
||||
# rm at the bottom of the script). Trap-based path lets the file leak
|
||||
# whenever the script exits non-zero before reaching the rm. RFC #2873
|
||||
# cleanup-hygiene PR.
|
||||
if [ -n "${TMPF:-}" ]; then
|
||||
rm -f "$TMPF"
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
assert() {
|
||||
local label="$1"
|
||||
@@ -230,7 +239,8 @@ for r in rows:
|
||||
assert "stored URI matches uploaded URI" "$STORED_URI" "$URI"
|
||||
fi
|
||||
|
||||
rm -f "$TMPF"
|
||||
# $TMPF cleanup happens via the trap-cleanup function above — covers
|
||||
# both the success path and any early exit / SIGINT.
|
||||
|
||||
echo ""
|
||||
echo "=== Results: $PASS passed, $FAIL failed ==="
|
||||
|
||||
@@ -321,8 +321,9 @@ tenant_call() {
|
||||
|
||||
# ─── 5. Provision parent workspace ─────────────────────────────────────
|
||||
# Inject the LLM provider key so the runtime can authenticate at boot.
|
||||
# Branch by which secret is set so the script supports both paths
|
||||
# without forcing every dispatch to ship both keys:
|
||||
# Branch by which secret is set so the script supports multiple paths
|
||||
# without forcing every dispatch to ship them all. Priority order
|
||||
# matters — first non-empty wins:
|
||||
#
|
||||
# E2E_MINIMAX_API_KEY → claude-code MiniMax path. Cheapest, default
|
||||
# for the cron canary post-2026-05-03. Routes via the claude-code
|
||||
@@ -334,6 +335,15 @@ tenant_call() {
|
||||
# collisions when a user runs MiniMax + Z.ai workspaces side-by-
|
||||
# side).
|
||||
#
|
||||
# E2E_ANTHROPIC_API_KEY → claude-code direct-Anthropic path (added
|
||||
# 2026-05-04 after #2578 left the operator with an awkward choice
|
||||
# between paying OpenAI's billing top-up and registering a new
|
||||
# MiniMax account). Lower friction than MiniMax for operators
|
||||
# who already have an Anthropic API key for their own Claude
|
||||
# Code session. Pricier per-token than MiniMax but billing is
|
||||
# still independent of MOLECULE_STAGING_OPENAI_KEY. Pinned to the
|
||||
# claude-code runtime — hermes/langgraph use OpenAI-shaped envs.
|
||||
#
|
||||
# E2E_OPENAI_API_KEY → langgraph + hermes paths. Kept as fallback
|
||||
# for operator dispatches that explicitly want to exercise the
|
||||
# OpenAI path. The HERMES_* fields pin hermes-agent's bridge to
|
||||
@@ -341,7 +351,7 @@ tenant_call() {
|
||||
# resolves openai/* → openrouter.ai and 401s). MODEL_PROVIDER
|
||||
# follows workspace/config.py:258's 'provider:model' format.
|
||||
#
|
||||
# Both empty → '{}' (workspace will fail at first turn with an
|
||||
# All empty → '{}' (workspace will fail at first turn with an
|
||||
# expected, actionable auth error rather than masking the test).
|
||||
SECRETS_JSON='{}'
|
||||
if [ -n "${E2E_MINIMAX_API_KEY:-}" ]; then
|
||||
@@ -352,6 +362,25 @@ print(json.dumps({
|
||||
'MINIMAX_API_KEY': k,
|
||||
}))
|
||||
")
|
||||
elif [ -n "${E2E_ANTHROPIC_API_KEY:-}" ]; then
|
||||
# Direct Anthropic path — claude-code adapter reads ANTHROPIC_API_KEY
|
||||
# natively when ANTHROPIC_BASE_URL is unset. Useful for operators
|
||||
# who already have an Anthropic API key (e.g. for their own Claude
|
||||
# Code session) and want to avoid setting up a separate MiniMax
|
||||
# account just for E2E. Pricier per-token than MiniMax but billing
|
||||
# is still independent of MOLECULE_STAGING_OPENAI_KEY, so an OpenAI
|
||||
# quota collapse doesn't wedge this path. Pinned to the claude-code
|
||||
# runtime: hermes/langgraph use OpenAI-shaped envs and won't honour
|
||||
# ANTHROPIC_API_KEY without further wiring (out of scope for this
|
||||
# branch; if you need a hermes/Anthropic path, dispatch with
|
||||
# E2E_RUNTIME=hermes + E2E_OPENAI_API_KEY pointing at a working key).
|
||||
SECRETS_JSON=$(python3 -c "
|
||||
import json, os
|
||||
k = os.environ['E2E_ANTHROPIC_API_KEY']
|
||||
print(json.dumps({
|
||||
'ANTHROPIC_API_KEY': k,
|
||||
}))
|
||||
")
|
||||
elif [ -n "${E2E_OPENAI_API_KEY:-}" ]; then
|
||||
SECRETS_JSON=$(python3 -c "
|
||||
import json, os
|
||||
@@ -475,6 +504,63 @@ for wid in $WS_TO_CHECK; do
|
||||
fi
|
||||
done
|
||||
|
||||
# ─── 7c. Workspace files API config.yaml round-trip ────────────────────
|
||||
# Pin the config-save path that drives the Canvas Config tab's Save &
|
||||
# Restart. Two failure classes this gate catches in one shot:
|
||||
#
|
||||
# 1. Path map drift (PR #2769). Runtime falls through to the wrong
|
||||
# base path (e.g. /opt/configs when user-data only created /configs)
|
||||
# → SSH `install -D` fails with EACCES on a parent dir that doesn't
|
||||
# exist. The user-visible 500 was unobservable without exercising
|
||||
# this code path on a fresh workspace.
|
||||
# 2. Permission drift on /configs. The path is root-owned by cloud-init,
|
||||
# so the SSH-as-ubuntu install needs `sudo -n`. Any future change
|
||||
# that drops the sudo, switches to a non-passwordless-sudo OS user,
|
||||
# or moves the path to a non-ubuntu-writable dir without sudo will
|
||||
# regress this gate.
|
||||
#
|
||||
# Round-trip: PUT a known marker, GET it back, assert content matches.
|
||||
# Marker shape includes the run id so a stale file from a prior canary
|
||||
# can't false-pass.
|
||||
log "7c/11 Files API config.yaml round-trip..."
|
||||
CONFIG_MARKER="# molecule-synth-e2e: ${E2E_RUN_ID:-unknown} ${RUNTIME} $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
CONFIG_PAYLOAD="${CONFIG_MARKER}
|
||||
name: synth-canary
|
||||
runtime: ${RUNTIME}
|
||||
"
|
||||
for wid in $WS_TO_CHECK; do
|
||||
PUT_BODY=$(python3 -c "import json,sys; print(json.dumps({'content': sys.stdin.read()}))" <<< "$CONFIG_PAYLOAD")
|
||||
# Capture body to a tempfile so curl's -w '%{http_code}' is the only
|
||||
# thing on stdout. The first version used `-w '\n%{http_code}\n'` and
|
||||
# parsed via `tail -n 2 | head -n 1`, which broke because bash $(...)
|
||||
# strips the trailing newline → only 2 lines remain in the captured
|
||||
# value → head -n 1 returned the body, not the status code. Caught
|
||||
# post-merge by E2E Staging SaaS at 22:06 UTC: a 200-with-body got
|
||||
# misreported as "PUT returned <body>".
|
||||
PUT_TMP=$(mktemp -t synth_put.XXXXXX)
|
||||
PUT_CODE=$(tenant_call PUT "/workspaces/$wid/files/config.yaml" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PUT_BODY" \
|
||||
-o "$PUT_TMP" \
|
||||
-w '%{http_code}' \
|
||||
2>/dev/null || echo "000")
|
||||
PUT_BODY_OUT=$(cat "$PUT_TMP" 2>/dev/null || echo "")
|
||||
rm -f "$PUT_TMP"
|
||||
if [ "$PUT_CODE" != "200" ] && [ "$PUT_CODE" != "204" ]; then
|
||||
fail "Workspace $wid Files API PUT config.yaml returned $PUT_CODE: $PUT_BODY_OUT — likely a path-map or permission regression in workspace-server template_files_eic.go"
|
||||
fi
|
||||
# PUT-only check; the GET-back round-trip assertion was dropped
|
||||
# 2026-05-04 because PUT (template_files_eic.go SSH-via-EIC →
|
||||
# workspace EC2) and GET (templates.go ReadFile → docker exec on
|
||||
# platform-tenant-local container) hit DIFFERENT paths and DIFFERENT
|
||||
# hosts. The asymmetry is a separate latent bug — Canvas Config tab
|
||||
# rendering reads workspace state via other endpoints, not via this
|
||||
# GET, so the user-facing Save & Restart works (container reads
|
||||
# /configs/config.yaml directly via bind-mount). When the read/write
|
||||
# paths are unified, restore the GET-back marker check here.
|
||||
ok " $wid config.yaml PUT OK (HTTP $PUT_CODE)"
|
||||
done
|
||||
|
||||
# ─── 8. A2A round-trip on parent ───────────────────────────────────────
|
||||
log "8/11 Sending A2A message to parent — expecting agent response..."
|
||||
# Smoke prompt phrasing — DO NOT trim back to the bare "Reply with exactly: PONG"
|
||||
@@ -505,7 +591,17 @@ print(json.dumps({
|
||||
}
|
||||
}))
|
||||
")
|
||||
# Override CURL_COMMON's --max-time 30 for THIS call only. Each canary
|
||||
# creates a fresh org → workspace, so the A2A POST hits a cold model:
|
||||
# claude-code adapter starts its event loop, opens TLS to the LLM
|
||||
# endpoint, ships the first prompt, waits for first token. With MiniMax
|
||||
# (which is the canary default since #2710) cold-call latency
|
||||
# routinely exceeds 30s on the first request after workspace boot.
|
||||
# 90s gives ~3x headroom over observed cold-call P95 (~25-30s).
|
||||
# Subsequent A2A turns hit the same workspace and are sub-second, so
|
||||
# this only widens the window for step 8/11 of the canary's first turn.
|
||||
A2A_RESP=$(tenant_call POST "/workspaces/$PARENT_ID/a2a" \
|
||||
--max-time 90 \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$A2A_PAYLOAD")
|
||||
AGENT_TEXT=$(echo "$A2A_RESP" | python3 -c "
|
||||
@@ -610,8 +706,80 @@ print(json.dumps({
|
||||
d=json.load(sys.stdin)
|
||||
print(len(d if isinstance(d, list) else d.get('events', [])))" 2>/dev/null || echo 0)
|
||||
log " Activity events observed: $ACTIVITY_COUNT"
|
||||
|
||||
# ─── 9c. Workspace KV memory Edit round-trip ─────────────────────────
|
||||
# Pins the Edit affordance added to the canvas Memory tab. The UI calls
|
||||
# POST /workspaces/:id/memory with if_match_version, so the contract is:
|
||||
# 1. initial POST creates row at version 1
|
||||
# 2. GET returns version 1 + value
|
||||
# 3. POST with if_match_version=1 updates → version 2
|
||||
# 4. POST with if_match_version=1 again → 409 (optimistic-lock enforcement)
|
||||
# Without (3) there is no Edit; without (4) two concurrent writers can
|
||||
# silently overwrite each other and the agent loses delegation-ledger state.
|
||||
log "9c. Memory KV Edit round-trip (Edit affordance + 409 gate)"
|
||||
EDIT_KEY="e2e_edit_gate_$SLUG"
|
||||
|
||||
# 1. seed
|
||||
tenant_call POST "/workspaces/$PARENT_ID/memory" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"key\":\"$EDIT_KEY\",\"value\":{\"step\":1}}" >/dev/null \
|
||||
|| fail "memory KV seed POST failed"
|
||||
|
||||
# 2. read back, capture version
|
||||
EDIT_GET=$(tenant_call GET "/workspaces/$PARENT_ID/memory/$EDIT_KEY")
|
||||
EDIT_VER=$(echo "$EDIT_GET" | python3 -c "import json,sys; print(json.load(sys.stdin)['version'])" 2>/dev/null || echo "")
|
||||
[ -z "$EDIT_VER" ] && fail "memory KV GET missing version field. Body: ${EDIT_GET:0:200}"
|
||||
|
||||
# 3. conditional update with matching version
|
||||
tenant_call POST "/workspaces/$PARENT_ID/memory" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"key\":\"$EDIT_KEY\",\"value\":{\"step\":2},\"if_match_version\":$EDIT_VER}" >/dev/null \
|
||||
|| fail "memory KV conditional Edit failed (if_match_version=$EDIT_VER)"
|
||||
|
||||
# 4. value flipped + version incremented?
|
||||
EDIT_GET2=$(tenant_call GET "/workspaces/$PARENT_ID/memory/$EDIT_KEY")
|
||||
EDIT_VAL2=$(echo "$EDIT_GET2" | python3 -c "import json,sys; print(json.load(sys.stdin)['value'].get('step'))" 2>/dev/null || echo "")
|
||||
[ "$EDIT_VAL2" = "2" ] || fail "memory KV Edit did not persist new value. Body: ${EDIT_GET2:0:200}"
|
||||
|
||||
# 5. stale-version POST must 409 — pin the optimistic-lock contract.
|
||||
#
|
||||
# tenant_call uses CURL_COMMON which carries --fail-with-body, so an
|
||||
# expected-409 makes curl exit 22. The previous shape
|
||||
# $(tenant_call ... -w "%{http_code}" || echo "000")
|
||||
# concatenated the captured "409" with the fallback "000" giving a
|
||||
# bogus "409000" value (caught on PR #2792's first E2E run, which is
|
||||
# also why staging-saas E2E has been silent-failing this gate since
|
||||
# PR #2787 merged). Fix: route the status code into its own tempfile
|
||||
# so curl's exit code can't pollute the captured stdout. set +e/-e
|
||||
# keeps the 22 from tripping the outer `set -e` pipeline.
|
||||
set +e
|
||||
tenant_call POST "/workspaces/$PARENT_ID/memory" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"key\":\"$EDIT_KEY\",\"value\":{\"step\":3},\"if_match_version\":$EDIT_VER}" \
|
||||
-o /tmp/memory_stale_resp.txt -w "%{http_code}" >/tmp/memory_stale_code.txt 2>/dev/null
|
||||
set -e
|
||||
EDIT_STALE_CODE=$(cat /tmp/memory_stale_code.txt 2>/dev/null || echo "000")
|
||||
[ "$EDIT_STALE_CODE" = "409" ] || fail "memory KV stale Edit must 409 (optimistic-lock). Got '$EDIT_STALE_CODE': $(cat /tmp/memory_stale_resp.txt 2>/dev/null | head -c 200)"
|
||||
|
||||
# cleanup
|
||||
tenant_call DELETE "/workspaces/$PARENT_ID/memory/$EDIT_KEY" >/dev/null 2>&1 || true
|
||||
ok "Memory KV Edit round-trip + 409 gate passed"
|
||||
|
||||
# ─── 9d. shared_context removal gate ─────────────────────────────────
|
||||
# Pin the deletion of GET /workspaces/:id/shared-context. The route + handler
|
||||
# were removed; team-shared knowledge now flows through memory v2's
|
||||
# team:<id> namespace. If anyone re-introduces a shared-context endpoint
|
||||
# without going through RFC #2789, this gate fires.
|
||||
set +e
|
||||
SC_CODE=$(tenant_call GET "/workspaces/$PARENT_ID/shared-context" \
|
||||
-o /dev/null -w "%{http_code}" 2>/dev/null || echo "000")
|
||||
set -e
|
||||
if [ "$SC_CODE" = "200" ]; then
|
||||
fail "shared-context route should be gone but returned 200 — regression. See task #304."
|
||||
fi
|
||||
ok "shared-context route confirmed removed (HTTP $SC_CODE)"
|
||||
else
|
||||
log "9/11 Canary mode — skipping HMA / peers / activity"
|
||||
log "9/11 Canary mode — skipping HMA / peers / activity / memory-edit / shared-context-gone"
|
||||
fi
|
||||
|
||||
# ─── 10. Delegation mechanics (full mode + child) ──────────────────────
|
||||
|
||||
@@ -94,6 +94,13 @@ services:
|
||||
CP_UPSTREAM_URL: "http://cp-stub:9090"
|
||||
RATE_LIMIT: "1000"
|
||||
CANVAS_PROXY_URL: "http://localhost:3000"
|
||||
# Memory v2 sidecar (PR #2906) bundles the plugin into the
|
||||
# tenant image and starts it before the main server. The plugin
|
||||
# runs `CREATE EXTENSION vector` on first boot, which fails on
|
||||
# the harness's plain postgres:15-alpine (no pgvector). The
|
||||
# harness doesn't exercise memory features, so disable the
|
||||
# sidecar via the entrypoint's documented escape hatch.
|
||||
MEMORY_PLUGIN_DISABLE: "1"
|
||||
networks: [harness-net]
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O- http://localhost:8080/health || exit 1"]
|
||||
@@ -142,6 +149,13 @@ services:
|
||||
CP_UPSTREAM_URL: "http://cp-stub:9090"
|
||||
RATE_LIMIT: "1000"
|
||||
CANVAS_PROXY_URL: "http://localhost:3000"
|
||||
# Memory v2 sidecar (PR #2906) bundles the plugin into the
|
||||
# tenant image and starts it before the main server. The plugin
|
||||
# runs `CREATE EXTENSION vector` on first boot, which fails on
|
||||
# the harness's plain postgres:15-alpine (no pgvector). The
|
||||
# harness doesn't exercise memory features, so disable the
|
||||
# sidecar via the entrypoint's documented escape hatch.
|
||||
MEMORY_PLUGIN_DISABLE: "1"
|
||||
networks: [harness-net]
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O- http://localhost:8080/health || exit 1"]
|
||||
|
||||
@@ -75,9 +75,14 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
# Stub platform_auth so a2a_client imports cleanly without requiring a
|
||||
# real workspace token file. The helper's auth_headers() only matters
|
||||
# when going through the network; we're feeding it a mock response.
|
||||
#
|
||||
# Both stubs accept *args, **kwargs because the multi-workspace work
|
||||
# (#2739, #2743) added optional ``workspace_id`` parameters to
|
||||
# ``auth_headers`` and made ``self_source_headers`` 1-arg-required.
|
||||
# The stubs need to accept whatever the helpers pass without caring.
|
||||
_pa = types.ModuleType("platform_auth")
|
||||
_pa.auth_headers = lambda: {}
|
||||
_pa.self_source_headers = lambda: {}
|
||||
_pa.auth_headers = lambda *a, **kw: {}
|
||||
_pa.self_source_headers = lambda *a, **kw: {}
|
||||
sys.modules.setdefault("platform_auth", _pa)
|
||||
|
||||
sys.path.insert(0, sys.argv[1])
|
||||
|
||||
Executable
+238
@@ -0,0 +1,238 @@
|
||||
#!/usr/bin/env bash
|
||||
# tools/branch-protection/apply.sh — idempotently apply branch
|
||||
# protection to molecule-core's `staging` and `main` branches.
|
||||
#
|
||||
# Single source of truth for the protection settings. Diff this file
|
||||
# against the live state (drift_check.sh handles that nightly + on
|
||||
# every PR that touches this directory).
|
||||
#
|
||||
# Why each branch has its OWN payload section instead of a shared
|
||||
# template: pre-2026-05-05 the script generated both branches from a
|
||||
# shared template that hard-coded enforce_admins=false,
|
||||
# dismiss_stale_reviews=true, strict=false, allow_fork_syncing=true,
|
||||
# and dropped bypass_pull_request_allowances. Live staging had
|
||||
# enforce_admins=true, dismiss_stale_reviews=false, strict=true,
|
||||
# allow_fork_syncing=false, and a bypass list. Running the script
|
||||
# would have silently weakened protection on every dimension at once.
|
||||
# Per-branch payloads codify the deliberate per-branch policy that
|
||||
# already lives on the repo, with the script's net contribution
|
||||
# being ONLY the explicit additions to required_status_checks.
|
||||
#
|
||||
# Per memory feedback_dismiss_stale_reviews_blocks_promote.md,
|
||||
# dismiss_stale_reviews=true silently re-blocks every auto-promote PR
|
||||
# (cost the user 2.5h once already on staging — confirming we keep
|
||||
# this OFF on staging is load-bearing for the auto-promote chain).
|
||||
#
|
||||
# Usage:
|
||||
# tools/branch-protection/apply.sh # apply both branches
|
||||
# tools/branch-protection/apply.sh --dry-run # show payload only
|
||||
# tools/branch-protection/apply.sh --branch staging
|
||||
# tools/branch-protection/apply.sh --skip-preflight # skip check-name validation
|
||||
#
|
||||
# Requires: gh CLI authenticated as a repo admin. The script uses gh's
|
||||
# token (no separate PAT needed).
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
REPO="Molecule-AI/molecule-core"
|
||||
DRY_RUN=0
|
||||
ONLY_BRANCH=""
|
||||
SKIP_PREFLIGHT=0
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--dry-run) DRY_RUN=1; shift ;;
|
||||
--branch) ONLY_BRANCH="$2"; shift 2 ;;
|
||||
--skip-preflight) SKIP_PREFLIGHT=1; shift ;;
|
||||
-h|--help)
|
||||
echo "Usage: $0 [--dry-run] [--branch <name>] [--skip-preflight]"
|
||||
exit 0
|
||||
;;
|
||||
*) echo "Unknown arg: $1" >&2; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# ─── Required-check matrices ──────────────────────────────────────
|
||||
# Each branch's set is the canonical list of check NAMES (from each
|
||||
# workflow's job-name). Adding/removing a check here is the place to
|
||||
# do it. Match docs/e2e-coverage.md.
|
||||
|
||||
read -r -d '' STAGING_CHECKS <<'EOF' || true
|
||||
Analyze (go)
|
||||
Analyze (javascript-typescript)
|
||||
Analyze (python)
|
||||
Canvas (Next.js)
|
||||
Canvas tabs E2E
|
||||
Detect changes
|
||||
E2E API Smoke Test
|
||||
Platform (Go)
|
||||
Python Lint & Test
|
||||
Scan diff for credential-shaped strings
|
||||
Shellcheck (E2E scripts)
|
||||
EOF
|
||||
|
||||
read -r -d '' MAIN_CHECKS <<'EOF' || true
|
||||
Analyze (go)
|
||||
Analyze (javascript-typescript)
|
||||
Analyze (python)
|
||||
Canvas (Next.js)
|
||||
Canvas tabs E2E
|
||||
Detect changes
|
||||
E2E API Smoke Test
|
||||
PR-built wheel + import smoke
|
||||
Platform (Go)
|
||||
Python Lint & Test
|
||||
Scan diff for credential-shaped strings
|
||||
Shellcheck (E2E scripts)
|
||||
EOF
|
||||
|
||||
checks_to_json() {
|
||||
printf '%s\n' "$1" | jq -Rs '
|
||||
split("\n")
|
||||
| map(select(length > 0))
|
||||
| map({context: ., app_id: -1})
|
||||
'
|
||||
}
|
||||
|
||||
# ─── Per-branch payloads (each preserves live-state policy) ───────
|
||||
# Staging payload — preserves the live values that pre-2026-05-05's
|
||||
# apply.sh would have silently rewritten:
|
||||
# enforce_admins=true, dismiss_stale_reviews=false, strict=true,
|
||||
# allow_fork_syncing=false, bypass list = HongmingWang-Rabbit + molecule-ai app.
|
||||
build_staging_payload() {
|
||||
local checks_json
|
||||
checks_json=$(checks_to_json "$STAGING_CHECKS")
|
||||
jq -n \
|
||||
--argjson checks "$checks_json" \
|
||||
'{
|
||||
required_status_checks: {
|
||||
strict: true,
|
||||
checks: $checks
|
||||
},
|
||||
enforce_admins: true,
|
||||
required_pull_request_reviews: {
|
||||
required_approving_review_count: 1,
|
||||
dismiss_stale_reviews: false,
|
||||
require_code_owner_reviews: false,
|
||||
require_last_push_approval: false,
|
||||
bypass_pull_request_allowances: {
|
||||
users: ["HongmingWang-Rabbit"],
|
||||
teams: [],
|
||||
apps: ["molecule-ai"]
|
||||
}
|
||||
},
|
||||
restrictions: null,
|
||||
allow_deletions: false,
|
||||
allow_force_pushes: false,
|
||||
block_creations: false,
|
||||
required_conversation_resolution: true,
|
||||
required_linear_history: false,
|
||||
lock_branch: false,
|
||||
allow_fork_syncing: false
|
||||
}'
|
||||
}
|
||||
|
||||
# Main payload — preserves the live values:
|
||||
# enforce_admins=false, dismiss_stale_reviews=true, strict=true,
|
||||
# allow_fork_syncing=false, NO bypass list.
|
||||
# main intentionally has different settings than staging because main
|
||||
# is the deploy target — the auto-promote app pushes to main without
|
||||
# the friction of an admin-bypass list, and stale-review dismissal
|
||||
# is acceptable here because every change has already cleared
|
||||
# staging review.
|
||||
build_main_payload() {
|
||||
local checks_json
|
||||
checks_json=$(checks_to_json "$MAIN_CHECKS")
|
||||
jq -n \
|
||||
--argjson checks "$checks_json" \
|
||||
'{
|
||||
required_status_checks: {
|
||||
strict: true,
|
||||
checks: $checks
|
||||
},
|
||||
enforce_admins: false,
|
||||
required_pull_request_reviews: {
|
||||
required_approving_review_count: 1,
|
||||
dismiss_stale_reviews: true,
|
||||
require_code_owner_reviews: false,
|
||||
require_last_push_approval: false
|
||||
},
|
||||
restrictions: null,
|
||||
allow_deletions: false,
|
||||
allow_force_pushes: false,
|
||||
block_creations: false,
|
||||
required_conversation_resolution: true,
|
||||
required_linear_history: false,
|
||||
lock_branch: false,
|
||||
allow_fork_syncing: false
|
||||
}'
|
||||
}
|
||||
|
||||
# ─── R3 preflight: validate every desired check name has at least
|
||||
# one historical run ──────────────────────────────────────────────
|
||||
# Pre-fix the script accepted arbitrary strings into
|
||||
# required_status_checks.checks. A typo like "Canvas Tabs E2E" vs
|
||||
# "Canvas tabs E2E" → GH accepts → every PR is blocked forever
|
||||
# waiting for a context that never emits. The preflight hits the
|
||||
# /commits/{sha}/check-runs endpoint and asserts each desired name
|
||||
# has at least one matching run. Skippable via --skip-preflight for
|
||||
# the case where you're adding a brand-new workflow whose first run
|
||||
# hasn't fired yet.
|
||||
preflight_check_names() {
|
||||
local branch="$1"
|
||||
local checks="$2"
|
||||
local sha
|
||||
sha=$(gh api "repos/$REPO/commits/$branch" --jq '.sha' 2>/dev/null || echo "")
|
||||
if [[ -z "$sha" ]]; then
|
||||
echo "preflight: WARN cannot resolve $branch tip SHA, skipping check-name validation" >&2
|
||||
return 0
|
||||
fi
|
||||
local known_names
|
||||
known_names=$(gh api "repos/$REPO/commits/$sha/check-runs?per_page=100" \
|
||||
--jq '.check_runs | map(.name)' 2>/dev/null || echo "[]")
|
||||
local missing=()
|
||||
while IFS= read -r name; do
|
||||
[[ -z "$name" ]] && continue
|
||||
if ! echo "$known_names" | jq -e --arg n "$name" 'index($n) != null' >/dev/null; then
|
||||
missing+=("$name")
|
||||
fi
|
||||
done <<< "$checks"
|
||||
if [[ ${#missing[@]} -gt 0 ]]; then
|
||||
echo "preflight: $branch — these check names are NOT in the historical check-runs for the tip SHA:" >&2
|
||||
printf ' - %s\n' "${missing[@]}" >&2
|
||||
echo "If they're truly new (workflow added but never run), re-run with --skip-preflight." >&2
|
||||
echo "Otherwise typos here will permanently block every PR — fix the names." >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
apply_branch() {
|
||||
local branch="$1"
|
||||
local checks="$2"
|
||||
local payload_fn="$3"
|
||||
local payload
|
||||
payload=$($payload_fn)
|
||||
if [[ "$DRY_RUN" -eq 1 ]]; then
|
||||
echo "=== branch: $branch ==="
|
||||
echo "$payload" | jq .
|
||||
return
|
||||
fi
|
||||
if [[ "$SKIP_PREFLIGHT" -eq 0 ]]; then
|
||||
if ! preflight_check_names "$branch" "$checks"; then
|
||||
echo "FAIL: preflight on $branch caught typos or missing workflows. Aborting." >&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
echo "Applying branch protection on $branch..."
|
||||
printf '%s' "$payload" | gh api -X PUT \
|
||||
"repos/$REPO/branches/$branch/protection" \
|
||||
--input -
|
||||
echo "Applied: $branch"
|
||||
}
|
||||
|
||||
if [[ -z "$ONLY_BRANCH" || "$ONLY_BRANCH" == "staging" ]]; then
|
||||
apply_branch staging "$STAGING_CHECKS" build_staging_payload
|
||||
fi
|
||||
if [[ -z "$ONLY_BRANCH" || "$ONLY_BRANCH" == "main" ]]; then
|
||||
apply_branch main "$MAIN_CHECKS" build_main_payload
|
||||
fi
|
||||
Executable
+157
@@ -0,0 +1,157 @@
|
||||
#!/usr/bin/env bash
|
||||
# tools/branch-protection/drift_check.sh — compare the live branch
|
||||
# protection on staging + main against what apply.sh would set. Used
|
||||
# by branch-protection-drift.yml (cron) to catch out-of-band UI edits.
|
||||
#
|
||||
# Pre-2026-05-05 version diffed only required_status_checks.checks —
|
||||
# would have missed a UI click that flipped enforce_admins or
|
||||
# dismiss_stale_reviews. Now compares the full normalized payload so
|
||||
# any silent rewrite of admin/review/lock/deletion settings trips the
|
||||
# drift gate.
|
||||
#
|
||||
# Exit codes:
|
||||
# 0 — live state matches the script
|
||||
# 1 — drift detected (output shows the diff)
|
||||
# 2 — gh API call failed
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
REPO="Molecule-AI/molecule-core"
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
EXIT_CODE=0
|
||||
|
||||
# Normalise the GET /branches/:b/protection response so we can compare
|
||||
# against apply.sh's payload. The GET response inflates booleans into
|
||||
# {url, enabled} sub-objects and bypass list users/apps into full
|
||||
# user/app objects with avatar_url etc — strip those down to match
|
||||
# the input shape.
|
||||
NORMALISE_LIVE='{
|
||||
required_status_checks: (
|
||||
.required_status_checks
|
||||
| { strict: .strict,
|
||||
checks: (.checks | map({context}) | sort_by(.context)) }
|
||||
),
|
||||
enforce_admins: (
|
||||
if (.enforce_admins | type) == "object"
|
||||
then .enforce_admins.enabled
|
||||
else .enforce_admins end
|
||||
),
|
||||
required_pull_request_reviews: (
|
||||
.required_pull_request_reviews
|
||||
| if . == null then null else
|
||||
{ required_approving_review_count,
|
||||
dismiss_stale_reviews,
|
||||
require_code_owner_reviews,
|
||||
require_last_push_approval,
|
||||
bypass_pull_request_allowances: (
|
||||
if .bypass_pull_request_allowances == null then null
|
||||
else {
|
||||
users: (.bypass_pull_request_allowances.users // [] | map(.login) | sort),
|
||||
teams: (.bypass_pull_request_allowances.teams // [] | map(.slug) | sort),
|
||||
apps: (.bypass_pull_request_allowances.apps // [] | map(.slug) | sort)
|
||||
} end
|
||||
)
|
||||
}
|
||||
end
|
||||
),
|
||||
restrictions: (
|
||||
if .restrictions == null then null
|
||||
else { users: (.restrictions.users | map(.login) | sort),
|
||||
teams: (.restrictions.teams | map(.slug) | sort),
|
||||
apps: (.restrictions.apps | map(.slug) | sort) }
|
||||
end
|
||||
),
|
||||
allow_deletions: (
|
||||
if (.allow_deletions | type) == "object" then .allow_deletions.enabled
|
||||
else (.allow_deletions // false) end
|
||||
),
|
||||
allow_force_pushes: (
|
||||
if (.allow_force_pushes | type) == "object" then .allow_force_pushes.enabled
|
||||
else (.allow_force_pushes // false) end
|
||||
),
|
||||
block_creations: (
|
||||
if (.block_creations | type) == "object" then .block_creations.enabled
|
||||
else (.block_creations // false) end
|
||||
),
|
||||
required_conversation_resolution: (
|
||||
if (.required_conversation_resolution | type) == "object"
|
||||
then .required_conversation_resolution.enabled
|
||||
else (.required_conversation_resolution // false) end
|
||||
),
|
||||
required_linear_history: (
|
||||
if (.required_linear_history | type) == "object" then .required_linear_history.enabled
|
||||
else (.required_linear_history // false) end
|
||||
),
|
||||
lock_branch: (
|
||||
if (.lock_branch | type) == "object" then .lock_branch.enabled
|
||||
else (.lock_branch // false) end
|
||||
),
|
||||
allow_fork_syncing: (
|
||||
if (.allow_fork_syncing | type) == "object" then .allow_fork_syncing.enabled
|
||||
else (.allow_fork_syncing // false) end
|
||||
)
|
||||
}'
|
||||
|
||||
# Apply.sh's payload is already in the input shape; we just need to
|
||||
# canonicalise the checks order and fill in optional fields with their
|
||||
# defaults so the comparison aligns.
|
||||
NORMALISE_SCRIPT='{
|
||||
required_status_checks: {
|
||||
strict: .required_status_checks.strict,
|
||||
checks: (.required_status_checks.checks | map({context}) | sort_by(.context))
|
||||
},
|
||||
enforce_admins: .enforce_admins,
|
||||
required_pull_request_reviews: (
|
||||
if .required_pull_request_reviews == null then null else
|
||||
{ required_approving_review_count: .required_pull_request_reviews.required_approving_review_count,
|
||||
dismiss_stale_reviews: .required_pull_request_reviews.dismiss_stale_reviews,
|
||||
require_code_owner_reviews: (.required_pull_request_reviews.require_code_owner_reviews // false),
|
||||
require_last_push_approval: (.required_pull_request_reviews.require_last_push_approval // false),
|
||||
bypass_pull_request_allowances: (
|
||||
if .required_pull_request_reviews.bypass_pull_request_allowances == null then null
|
||||
else {
|
||||
users: (.required_pull_request_reviews.bypass_pull_request_allowances.users // [] | sort),
|
||||
teams: (.required_pull_request_reviews.bypass_pull_request_allowances.teams // [] | sort),
|
||||
apps: (.required_pull_request_reviews.bypass_pull_request_allowances.apps // [] | sort)
|
||||
} end
|
||||
)
|
||||
}
|
||||
end
|
||||
),
|
||||
restrictions: .restrictions,
|
||||
allow_deletions: (.allow_deletions // false),
|
||||
allow_force_pushes: (.allow_force_pushes // false),
|
||||
block_creations: (.block_creations // false),
|
||||
required_conversation_resolution: (.required_conversation_resolution // false),
|
||||
required_linear_history: (.required_linear_history // false),
|
||||
lock_branch: (.lock_branch // false),
|
||||
allow_fork_syncing: (.allow_fork_syncing // false)
|
||||
}'
|
||||
|
||||
check_branch() {
|
||||
local branch="$1"
|
||||
local want
|
||||
want=$(bash "$SCRIPT_DIR/apply.sh" --dry-run --branch "$branch" 2>&1 |
|
||||
sed -n '/^{$/,/^}$/p' |
|
||||
jq -S "$NORMALISE_SCRIPT")
|
||||
local have_raw
|
||||
if ! have_raw=$(gh api "repos/$REPO/branches/$branch/protection" 2>/dev/null); then
|
||||
echo "drift_check: FAIL to fetch $branch protection (gh API error)"
|
||||
return 2
|
||||
fi
|
||||
local have
|
||||
have=$(echo "$have_raw" | jq -S "$NORMALISE_LIVE")
|
||||
if [[ "$want" != "$have" ]]; then
|
||||
echo "=== DRIFT on $branch ==="
|
||||
diff <(echo "$want") <(echo "$have") || true
|
||||
return 1
|
||||
fi
|
||||
echo "OK: $branch matches desired state"
|
||||
}
|
||||
|
||||
for b in staging main; do
|
||||
if ! check_branch "$b"; then
|
||||
EXIT_CODE=1
|
||||
fi
|
||||
done
|
||||
exit "$EXIT_CODE"
|
||||
@@ -21,6 +21,14 @@ ARG GIT_SHA=dev
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-ldflags "-X github.com/Molecule-AI/molecule-monorepo/platform/internal/buildinfo.GitSHA=${GIT_SHA}" \
|
||||
-o /platform ./cmd/server
|
||||
# Bundle the built-in memory-plugin-postgres binary so an operator can
|
||||
# activate Memory v2 by setting MEMORY_V2_CUTOVER=true + (default)
|
||||
# MEMORY_PLUGIN_URL=http://localhost:9100. The entrypoint starts this
|
||||
# binary in the background; main /platform talks to it over loopback.
|
||||
# Stays inert until the operator flips the cutover env var.
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-ldflags "-X github.com/Molecule-AI/molecule-monorepo/platform/internal/buildinfo.GitSHA=${GIT_SHA}" \
|
||||
-o /memory-plugin ./cmd/memory-plugin-postgres
|
||||
|
||||
# Clone templates + plugins at build time from manifest.json
|
||||
FROM alpine:3.20 AS templates
|
||||
@@ -30,8 +38,9 @@ COPY scripts/clone-manifest.sh /scripts/clone-manifest.sh
|
||||
RUN chmod +x /scripts/clone-manifest.sh && /scripts/clone-manifest.sh /manifest.json /workspace-configs-templates /org-templates /plugins
|
||||
|
||||
FROM alpine:3.20
|
||||
RUN apk add --no-cache ca-certificates git tzdata
|
||||
RUN apk add --no-cache ca-certificates git tzdata wget
|
||||
COPY --from=builder /platform /platform
|
||||
COPY --from=builder /memory-plugin /memory-plugin
|
||||
COPY workspace-server/migrations /migrations
|
||||
COPY --from=templates /workspace-configs-templates /workspace-configs-templates
|
||||
COPY --from=templates /org-templates /org-templates
|
||||
@@ -41,6 +50,7 @@ RUN addgroup -g 1000 platform && adduser -u 1000 -G platform -s /bin/sh -D platf
|
||||
EXPOSE 8080
|
||||
COPY <<'ENTRY' /entrypoint.sh
|
||||
#!/bin/sh
|
||||
# Set up docker-socket group (unchanged from pre-sidecar entrypoint).
|
||||
if [ -S /var/run/docker.sock ]; then
|
||||
SOCK_GID=$(stat -c '%g' /var/run/docker.sock 2>/dev/null || stat -f '%g' /var/run/docker.sock 2>/dev/null)
|
||||
if [ -n "$SOCK_GID" ] && [ "$SOCK_GID" != "0" ]; then
|
||||
@@ -50,6 +60,61 @@ if [ -S /var/run/docker.sock ]; then
|
||||
addgroup platform root 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
|
||||
# Memory v2 sidecar (built-in postgres plugin). Co-located with the
|
||||
# main server so operators flipping MEMORY_V2_CUTOVER=true don't need
|
||||
# to provision a separate service.
|
||||
#
|
||||
# Spawn-gating: only start the sidecar when the operator has indicated
|
||||
# they want it — either MEMORY_V2_CUTOVER=true OR MEMORY_PLUGIN_URL set.
|
||||
# Without that signal, the sidecar adds zero value (the platform's
|
||||
# wiring.go skips building the client too) but pays a real cost: the
|
||||
# plugin's first migration runs `CREATE EXTENSION vector`, which fails
|
||||
# on tenant Postgres without pgvector preinstalled and aborts container
|
||||
# boot via the 30s health gate. Caught on staging redeploy 2026-05-05.
|
||||
#
|
||||
# Env defaults (when sidecar IS spawned):
|
||||
# MEMORY_PLUGIN_DATABASE_URL = $DATABASE_URL (share existing Postgres;
|
||||
# plugin's `memory_namespaces` / `memory_records` tables coexist
|
||||
# with `agent_memories` and the rest of the platform schema —
|
||||
# no conflicts. Operator can override with a separate URL.)
|
||||
# MEMORY_PLUGIN_LISTEN_ADDR = 127.0.0.1:9100
|
||||
#
|
||||
# Set MEMORY_PLUGIN_DISABLE=1 to force-skip the sidecar even with
|
||||
# cutover env set (e.g. running the plugin externally on a separate host).
|
||||
memory_plugin_wanted=""
|
||||
if [ "$MEMORY_V2_CUTOVER" = "true" ] || [ -n "$MEMORY_PLUGIN_URL" ]; then
|
||||
memory_plugin_wanted=1
|
||||
fi
|
||||
if [ -z "$MEMORY_PLUGIN_DISABLE" ] && [ -n "$memory_plugin_wanted" ] && [ -n "$DATABASE_URL" ]; then
|
||||
: "${MEMORY_PLUGIN_DATABASE_URL:=$DATABASE_URL}"
|
||||
: "${MEMORY_PLUGIN_LISTEN_ADDR:=:9100}"
|
||||
export MEMORY_PLUGIN_DATABASE_URL MEMORY_PLUGIN_LISTEN_ADDR
|
||||
echo "memory-plugin: starting sidecar on $MEMORY_PLUGIN_LISTEN_ADDR" >&2
|
||||
# Drop privs to the platform user — the plugin doesn't need root and
|
||||
# runs unprivileged elsewhere (tenant image already starts as canvas).
|
||||
su-exec platform /memory-plugin &
|
||||
MEMORY_PLUGIN_PID=$!
|
||||
# Wait up to 30s for the plugin's /v1/health to return 200. Boot
|
||||
# failure here is fatal — better to crash-loop than to silently
|
||||
# serve cutover traffic against a dead plugin.
|
||||
health_port=${MEMORY_PLUGIN_LISTEN_ADDR#:}
|
||||
ready=0
|
||||
for _ in $(seq 1 30); do
|
||||
if wget -qO- --timeout=2 "http://localhost:${health_port}/v1/health" >/dev/null 2>&1; then
|
||||
ready=1
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$ready" != "1" ]; then
|
||||
echo "memory-plugin: ❌ /v1/health never returned 200 after 30s — aborting boot. Check that DATABASE_URL is reachable, has the pgvector extension, and the plugin's migrations applied." >&2
|
||||
kill "$MEMORY_PLUGIN_PID" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
echo "memory-plugin: ✅ sidecar healthy on :$health_port" >&2
|
||||
fi
|
||||
|
||||
exec su-exec platform /platform "$@"
|
||||
ENTRY
|
||||
RUN chmod +x /entrypoint.sh && apk add --no-cache su-exec
|
||||
|
||||
@@ -34,6 +34,13 @@ ARG GIT_SHA=dev
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-ldflags "-X github.com/Molecule-AI/molecule-monorepo/platform/internal/buildinfo.GitSHA=${GIT_SHA}" \
|
||||
-o /platform ./cmd/server
|
||||
# Memory v2 sidecar binary (Memory v2 #2728). Bundled so an operator
|
||||
# can activate cutover by flipping MEMORY_V2_CUTOVER=true without
|
||||
# provisioning a separate service. See entrypoint-tenant.sh for the
|
||||
# launch logic.
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-ldflags "-X github.com/Molecule-AI/molecule-monorepo/platform/internal/buildinfo.GitSHA=${GIT_SHA}" \
|
||||
-o /memory-plugin ./cmd/memory-plugin-postgres
|
||||
|
||||
# ── Stage 2: Canvas Next.js standalone ────────────────────────────────
|
||||
FROM node:20-alpine AS canvas-builder
|
||||
@@ -74,8 +81,9 @@ RUN deluser --remove-home node 2>/dev/null || true; \
|
||||
delgroup node 2>/dev/null || true; \
|
||||
addgroup -g 1000 canvas && adduser -u 1000 -G canvas -s /bin/sh -D canvas
|
||||
|
||||
# Go platform binary
|
||||
# Go platform binary + Memory v2 sidecar
|
||||
COPY --from=go-builder /platform /platform
|
||||
COPY --from=go-builder /memory-plugin /memory-plugin
|
||||
COPY workspace-server/migrations /migrations
|
||||
|
||||
# Templates + plugins (cloned from GitHub in stage 3)
|
||||
@@ -91,7 +99,7 @@ COPY --from=canvas-builder /canvas/public ./public
|
||||
|
||||
COPY workspace-server/entrypoint-tenant.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh && \
|
||||
chown -R canvas:canvas /canvas /platform /migrations
|
||||
chown -R canvas:canvas /canvas /platform /memory-plugin /migrations
|
||||
|
||||
EXPOSE 8080
|
||||
# entrypoint.sh starts as root to fix volume perms, then drops to
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
// memory-backfill is a one-shot CLI that copies rows from the legacy
|
||||
// agent_memories table into the v2 plugin via its HTTP API.
|
||||
//
|
||||
// Idempotent on re-run: the backfill passes each source row's UUID
|
||||
// to the plugin's MemoryWrite.ID field, and the plugin upserts on
|
||||
// conflict. Re-running the backfill (whole or partial) updates rows
|
||||
// in place rather than duplicating.
|
||||
//
|
||||
// Usage:
|
||||
// memory-backfill -dry-run # count + diff
|
||||
// memory-backfill -apply # actually copy
|
||||
// memory-backfill -apply -limit=10000 # cap rows per run
|
||||
// memory-backfill -apply -workspace=<uuid> # one workspace only
|
||||
//
|
||||
// Required env:
|
||||
// DATABASE_URL — workspace-server DB (read agent_memories)
|
||||
// MEMORY_PLUGIN_URL — target plugin (write memory_records)
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
mclient "github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/client"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/namespace"
|
||||
)
|
||||
|
||||
const defaultLimit = 1000000 // effectively unlimited; cap keeps SQL pageable
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:], os.Stdout, os.Stderr); err != nil {
|
||||
log.Fatalf("memory-backfill: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// run is extracted so tests can drive it with synthesized argv +
|
||||
// captured stdout/stderr. Returns nil on success.
|
||||
func run(argv []string, stdout, stderr *os.File) error {
|
||||
fs := flag.NewFlagSet("memory-backfill", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
dryRun := fs.Bool("dry-run", false, "count + diff only, no writes")
|
||||
apply := fs.Bool("apply", false, "actually copy rows to the plugin")
|
||||
verify := fs.Bool("verify", false, "post-apply parity check: random-sample N workspaces, diff agent_memories vs plugin search")
|
||||
verifySample := fs.Int("verify-sample", 50, "number of workspaces to sample in -verify mode")
|
||||
workspace := fs.String("workspace", "", "limit to a single workspace UUID (empty = all)")
|
||||
limit := fs.Int("limit", defaultLimit, "max rows to process this run")
|
||||
if err := fs.Parse(argv); err != nil {
|
||||
return err
|
||||
}
|
||||
modesPicked := 0
|
||||
if *dryRun {
|
||||
modesPicked++
|
||||
}
|
||||
if *apply {
|
||||
modesPicked++
|
||||
}
|
||||
if *verify {
|
||||
modesPicked++
|
||||
}
|
||||
if modesPicked != 1 {
|
||||
return errors.New("specify exactly one of -dry-run, -apply, or -verify")
|
||||
}
|
||||
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
return errors.New("DATABASE_URL is required")
|
||||
}
|
||||
pluginURL := os.Getenv("MEMORY_PLUGIN_URL")
|
||||
if pluginURL == "" {
|
||||
return errors.New("MEMORY_PLUGIN_URL is required")
|
||||
}
|
||||
|
||||
db, err := sql.Open("postgres", dbURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open db: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return fmt.Errorf("ping db: %w", err)
|
||||
}
|
||||
|
||||
plugin := mclient.New(mclient.Config{BaseURL: pluginURL})
|
||||
resolver := namespace.New(db)
|
||||
|
||||
if *verify {
|
||||
vcfg := verifyConfig{
|
||||
DB: db,
|
||||
Plugin: plugin,
|
||||
Resolver: namespaceResolverAdapter{resolver},
|
||||
SampleSize: *verifySample,
|
||||
WorkspaceID: *workspace,
|
||||
}
|
||||
report, err := verifyParity(context.Background(), vcfg, stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(stdout, "\nVerify complete: workspaces_sampled=%d matches=%d mismatches=%d errors=%d\n",
|
||||
report.WorkspacesSampled, report.Matches, report.Mismatches, report.Errors)
|
||||
if report.Mismatches > 0 || report.Errors > 0 {
|
||||
return fmt.Errorf("verify found %d mismatches and %d errors", report.Mismatches, report.Errors)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := backfillConfig{
|
||||
DB: db,
|
||||
Plugin: plugin,
|
||||
Resolver: resolver,
|
||||
WorkspaceID: *workspace,
|
||||
Limit: *limit,
|
||||
DryRun: *dryRun,
|
||||
}
|
||||
stats, err := backfill(context.Background(), cfg, stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(stdout, "\nBackfill complete: scanned=%d copied=%d skipped=%d errors=%d\n",
|
||||
stats.Scanned, stats.Copied, stats.Skipped, stats.Errors)
|
||||
return nil
|
||||
}
|
||||
|
||||
// backfillStats accumulates the counters the CLI reports.
|
||||
type backfillStats struct {
|
||||
Scanned int
|
||||
Copied int
|
||||
Skipped int
|
||||
Errors int
|
||||
}
|
||||
|
||||
// backfillConfig is the typed dependency bundle. Tests inject stubs
|
||||
// for Plugin and Resolver; production wires real client + resolver.
|
||||
type backfillConfig struct {
|
||||
DB *sql.DB
|
||||
Plugin backfillPlugin
|
||||
Resolver backfillResolver
|
||||
WorkspaceID string
|
||||
Limit int
|
||||
DryRun bool
|
||||
}
|
||||
|
||||
// backfillPlugin is the slice of memory-plugin client we call.
|
||||
type backfillPlugin interface {
|
||||
UpsertNamespace(ctx context.Context, name string, body contract.NamespaceUpsert) (*contract.Namespace, error)
|
||||
CommitMemory(ctx context.Context, namespace string, body contract.MemoryWrite) (*contract.MemoryWriteResponse, error)
|
||||
}
|
||||
|
||||
// backfillResolver lets the backfill compute namespace strings the
|
||||
// same way the live MCP layer does.
|
||||
type backfillResolver interface {
|
||||
WritableNamespaces(ctx context.Context, workspaceID string) ([]namespace.Namespace, error)
|
||||
}
|
||||
|
||||
// backfill is the workhorse. Iterates agent_memories, maps each row's
|
||||
// scope to a v2 namespace via the resolver, and POSTs to the plugin.
|
||||
// Returns final stats. Stops after Limit rows.
|
||||
func backfill(ctx context.Context, cfg backfillConfig, stdout *os.File) (*backfillStats, error) {
|
||||
stats := &backfillStats{}
|
||||
|
||||
query := `
|
||||
SELECT id, workspace_id, content, scope, created_at
|
||||
FROM agent_memories
|
||||
`
|
||||
args := []interface{}{}
|
||||
if cfg.WorkspaceID != "" {
|
||||
query += ` WHERE workspace_id = $1`
|
||||
args = append(args, cfg.WorkspaceID)
|
||||
}
|
||||
query += ` ORDER BY created_at ASC LIMIT $` + fmt.Sprintf("%d", len(args)+1)
|
||||
args = append(args, cfg.Limit)
|
||||
|
||||
rows, err := cfg.DB.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("query agent_memories: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
stats.Scanned++
|
||||
var (
|
||||
id, workspaceID, content, scope string
|
||||
createdAt time.Time
|
||||
)
|
||||
if err := rows.Scan(&id, &workspaceID, &content, &scope, &createdAt); err != nil {
|
||||
fmt.Fprintf(stdout, "scan: %v\n", err)
|
||||
stats.Errors++
|
||||
continue
|
||||
}
|
||||
|
||||
ns, err := mapScopeToNamespace(ctx, cfg.Resolver, workspaceID, scope)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stdout, "[skip] id=%s workspace=%s: %v\n", id, workspaceID, err)
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
if cfg.DryRun {
|
||||
fmt.Fprintf(stdout, "[dry] id=%s scope=%s → ns=%s\n", id, scope, ns)
|
||||
stats.Copied++ // would-have-copied
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure the namespace exists before posting memories. Plugin's
|
||||
// UpsertNamespace is idempotent so calling per-row is wasteful
|
||||
// but safe; for v1 we accept the chattiness.
|
||||
if _, err := cfg.Plugin.UpsertNamespace(ctx, ns, contract.NamespaceUpsert{
|
||||
Kind: namespaceKindFromString(scope),
|
||||
}); err != nil {
|
||||
fmt.Fprintf(stdout, "[err-ns] id=%s ns=%s: %v\n", id, ns, err)
|
||||
stats.Errors++
|
||||
continue
|
||||
}
|
||||
|
||||
// Pass the source row's UUID as the idempotency key so re-runs
|
||||
// upsert in place. Without this, retries would duplicate every
|
||||
// memory.
|
||||
if _, err := cfg.Plugin.CommitMemory(ctx, ns, contract.MemoryWrite{
|
||||
ID: id,
|
||||
Content: content,
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
}); err != nil {
|
||||
fmt.Fprintf(stdout, "[err-mem] id=%s ns=%s: %v\n", id, ns, err)
|
||||
stats.Errors++
|
||||
continue
|
||||
}
|
||||
stats.Copied++
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return stats, fmt.Errorf("iterate rows: %w", err)
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// mapScopeToNamespace mirrors the legacy-shim translation. The
|
||||
// backfill needs the SAME mapping the runtime uses so reads work
|
||||
// after cutover.
|
||||
func mapScopeToNamespace(ctx context.Context, r backfillResolver, workspaceID, scope string) (string, error) {
|
||||
writable, err := r.WritableNamespaces(ctx, workspaceID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve writable: %w", err)
|
||||
}
|
||||
wantKind := contract.NamespaceKindWorkspace
|
||||
switch scope {
|
||||
case "LOCAL":
|
||||
wantKind = contract.NamespaceKindWorkspace
|
||||
case "TEAM":
|
||||
wantKind = contract.NamespaceKindTeam
|
||||
case "GLOBAL":
|
||||
wantKind = contract.NamespaceKindOrg
|
||||
default:
|
||||
return "", fmt.Errorf("unknown scope %q", scope)
|
||||
}
|
||||
for _, ns := range writable {
|
||||
if ns.Kind == wantKind {
|
||||
return ns.Name, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("no writable namespace of kind %s for workspace %s", wantKind, workspaceID)
|
||||
}
|
||||
|
||||
// namespaceKindFromString returns the contract.NamespaceKind for a
|
||||
// legacy scope value. Unknown scopes default to "workspace" so the
|
||||
// backfill never aborts on an unexpected row.
|
||||
func namespaceKindFromString(scope string) contract.NamespaceKind {
|
||||
switch strings.ToUpper(scope) {
|
||||
case "TEAM":
|
||||
return contract.NamespaceKindTeam
|
||||
case "GLOBAL":
|
||||
return contract.NamespaceKindOrg
|
||||
default:
|
||||
return contract.NamespaceKindWorkspace
|
||||
}
|
||||
}
|
||||
|
||||
// namespaceResolverAdapter bridges *namespace.Resolver (which returns
|
||||
// []namespace.Namespace) to verify.go's verifyResolver interface
|
||||
// (which wants []ResolvedNamespace). Keeps verify.go independent of
|
||||
// the namespace-package dependency so its tests can stub easily.
|
||||
type namespaceResolverAdapter struct {
|
||||
r *namespace.Resolver
|
||||
}
|
||||
|
||||
func (a namespaceResolverAdapter) ReadableNamespaces(ctx context.Context, workspaceID string) ([]ResolvedNamespace, error) {
|
||||
src, err := a.r.ReadableNamespaces(ctx, workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]ResolvedNamespace, len(src))
|
||||
for i, ns := range src {
|
||||
out[i] = ResolvedNamespace{Name: ns.Name}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/namespace"
|
||||
)
|
||||
|
||||
// stubBackfillPlugin records calls for assertions.
|
||||
type stubBackfillPlugin struct {
|
||||
upsertedNamespaces []string
|
||||
committedNamespaces []string
|
||||
committedIDs []string // captures MemoryWrite.ID per call
|
||||
upsertErr error
|
||||
commitErr error
|
||||
}
|
||||
|
||||
func (s *stubBackfillPlugin) UpsertNamespace(_ context.Context, name string, _ contract.NamespaceUpsert) (*contract.Namespace, error) {
|
||||
s.upsertedNamespaces = append(s.upsertedNamespaces, name)
|
||||
if s.upsertErr != nil {
|
||||
return nil, s.upsertErr
|
||||
}
|
||||
return &contract.Namespace{Name: name, Kind: contract.NamespaceKindWorkspace}, nil
|
||||
}
|
||||
func (s *stubBackfillPlugin) CommitMemory(_ context.Context, ns string, body contract.MemoryWrite) (*contract.MemoryWriteResponse, error) {
|
||||
s.committedNamespaces = append(s.committedNamespaces, ns)
|
||||
s.committedIDs = append(s.committedIDs, body.ID)
|
||||
if s.commitErr != nil {
|
||||
return nil, s.commitErr
|
||||
}
|
||||
id := body.ID
|
||||
if id == "" {
|
||||
id = "out-1"
|
||||
}
|
||||
return &contract.MemoryWriteResponse{ID: id, Namespace: ns}, nil
|
||||
}
|
||||
|
||||
type stubBackfillResolver struct {
|
||||
writable []namespace.Namespace
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubBackfillResolver) WritableNamespaces(_ context.Context, _ string) ([]namespace.Namespace, error) {
|
||||
return s.writable, s.err
|
||||
}
|
||||
|
||||
func rootBackfillResolver() *stubBackfillResolver {
|
||||
return &stubBackfillResolver{
|
||||
writable: []namespace.Namespace{
|
||||
{Name: "workspace:root-1", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
{Name: "team:root-1", Kind: contract.NamespaceKindTeam, Writable: true},
|
||||
{Name: "org:root-1", Kind: contract.NamespaceKindOrg, Writable: true},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// --- mapScopeToNamespace ---
|
||||
|
||||
func TestMapScopeToNamespace(t *testing.T) {
|
||||
cases := []struct {
|
||||
scope string
|
||||
want string
|
||||
wantErr string
|
||||
}{
|
||||
{"LOCAL", "workspace:root-1", ""},
|
||||
{"TEAM", "team:root-1", ""},
|
||||
{"GLOBAL", "org:root-1", ""},
|
||||
{"WEIRD", "", "unknown scope"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.scope, func(t *testing.T) {
|
||||
got, err := mapScopeToNamespace(context.Background(), rootBackfillResolver(), "root-1", tc.scope)
|
||||
if tc.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
||||
t.Errorf("err = %v, want %q", err, tc.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("got %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapScopeToNamespace_ResolverError(t *testing.T) {
|
||||
r := &stubBackfillResolver{err: errors.New("dead")}
|
||||
_, err := mapScopeToNamespace(context.Background(), r, "root-1", "LOCAL")
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapScopeToNamespace_NoMatchingKind(t *testing.T) {
|
||||
r := &stubBackfillResolver{writable: []namespace.Namespace{
|
||||
{Name: "workspace:x", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
}}
|
||||
_, err := mapScopeToNamespace(context.Background(), r, "root-1", "TEAM")
|
||||
if err == nil || !strings.Contains(err.Error(), "no writable namespace") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- namespaceKindFromString ---
|
||||
|
||||
func TestNamespaceKindFromString(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want contract.NamespaceKind
|
||||
}{
|
||||
{"LOCAL", contract.NamespaceKindWorkspace},
|
||||
{"local", contract.NamespaceKindWorkspace},
|
||||
{"TEAM", contract.NamespaceKindTeam},
|
||||
{"team", contract.NamespaceKindTeam},
|
||||
{"GLOBAL", contract.NamespaceKindOrg},
|
||||
{"global", contract.NamespaceKindOrg},
|
||||
{"weird", contract.NamespaceKindWorkspace}, // safe default
|
||||
{"", contract.NamespaceKindWorkspace},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := namespaceKindFromString(tc.in); got != tc.want {
|
||||
t.Errorf("namespaceKindFromString(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- backfill (the workhorse) ---
|
||||
|
||||
// TestBackfill_PassesSourceUUIDAsIdempotencyKey pins the Critical-1
|
||||
// fix: backfill must forward agent_memories.id to MemoryWrite.ID so
|
||||
// re-runs upsert in place.
|
||||
func TestBackfill_PassesSourceUUIDAsIdempotencyKey(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
now := time.Now().UTC()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("source-uuid-A", "root-1", "fact 1", "LOCAL", now).
|
||||
AddRow("source-uuid-B", "root-1", "fact 2", "LOCAL", now))
|
||||
|
||||
plugin := &stubBackfillPlugin{}
|
||||
cfg := backfillConfig{DB: db, Plugin: plugin, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
if _, err := backfill(context.Background(), cfg, devnull); err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
}
|
||||
if len(plugin.committedIDs) != 2 {
|
||||
t.Fatalf("commits = %d", len(plugin.committedIDs))
|
||||
}
|
||||
if plugin.committedIDs[0] != "source-uuid-A" || plugin.committedIDs[1] != "source-uuid-B" {
|
||||
t.Errorf("committedIDs = %v; idempotency key not forwarded", plugin.committedIDs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackfill_RerunIsIdempotent: same agent_memories rows backfilled
|
||||
// twice. Plugin sees the same UUIDs both times; without the fix the
|
||||
// plugin would generate fresh UUIDs and duplicate.
|
||||
func TestBackfill_RerunIsIdempotent(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
now := time.Now().UTC()
|
||||
rows1 := sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("uuid-1", "root-1", "fact", "LOCAL", now)
|
||||
rows2 := sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("uuid-1", "root-1", "fact", "LOCAL", now)
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").WillReturnRows(rows1)
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").WillReturnRows(rows2)
|
||||
|
||||
plugin := &stubBackfillPlugin{}
|
||||
cfg := backfillConfig{DB: db, Plugin: plugin, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
|
||||
if _, err := backfill(context.Background(), cfg, devnull); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := backfill(context.Background(), cfg, devnull); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugin.committedIDs) != 2 {
|
||||
t.Errorf("commits = %d, want 2", len(plugin.committedIDs))
|
||||
}
|
||||
if plugin.committedIDs[0] != "uuid-1" || plugin.committedIDs[1] != "uuid-1" {
|
||||
t.Errorf("ids = %v; both runs must pass uuid-1 (relies on plugin upsert for actual de-dup)", plugin.committedIDs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_HappyPath_Apply(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
now := time.Now().UTC()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("mem-1", "root-1", "fact x", "LOCAL", now).
|
||||
AddRow("mem-2", "root-1", "team y", "TEAM", now).
|
||||
AddRow("mem-3", "root-1", "org z", "GLOBAL", now))
|
||||
|
||||
plugin := &stubBackfillPlugin{}
|
||||
cfg := backfillConfig{
|
||||
DB: db,
|
||||
Plugin: plugin,
|
||||
Resolver: rootBackfillResolver(),
|
||||
Limit: 100,
|
||||
DryRun: false,
|
||||
}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
stats, err := backfill(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if stats.Scanned != 3 || stats.Copied != 3 || stats.Errors != 0 {
|
||||
t.Errorf("stats = %+v", stats)
|
||||
}
|
||||
if len(plugin.committedNamespaces) != 3 {
|
||||
t.Errorf("commits = %v", plugin.committedNamespaces)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_DryRun_DoesNotCallPlugin(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
now := time.Now().UTC()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("mem-1", "root-1", "fact x", "LOCAL", now))
|
||||
|
||||
plugin := &stubBackfillPlugin{}
|
||||
cfg := backfillConfig{DB: db, Plugin: plugin, Resolver: rootBackfillResolver(), Limit: 100, DryRun: true}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
stats, err := backfill(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if stats.Copied != 1 {
|
||||
t.Errorf("copied = %d", stats.Copied)
|
||||
}
|
||||
if len(plugin.committedNamespaces) != 0 {
|
||||
t.Errorf("plugin must not be called in dry-run mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_WorkspaceFilter(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WithArgs("specific-ws", 100).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{}, Resolver: rootBackfillResolver(), Limit: 100, WorkspaceID: "specific-ws"}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
if _, err := backfill(context.Background(), cfg, devnull); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("workspace filter not applied: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_QueryError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnError(errors.New("dead"))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{}, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
_, err := backfill(context.Background(), cfg, devnull)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_ScanError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}). // wrong shape
|
||||
AddRow("mem-1"))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{}, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
stats, err := backfill(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if stats.Errors != 1 {
|
||||
t.Errorf("errors = %d, want 1", stats.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_RowsErr(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("mem-1", "root-1", "x", "LOCAL", time.Now().UTC()).
|
||||
RowError(0, errors.New("mid-iter")))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{}, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
_, err := backfill(context.Background(), cfg, devnull)
|
||||
if err == nil || !strings.Contains(err.Error(), "iterate") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_SkipsUnmappableRow(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("mem-1", "root-1", "x", "WEIRD", time.Now().UTC()))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{}, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
stats, err := backfill(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if stats.Skipped != 1 || stats.Copied != 0 {
|
||||
t.Errorf("stats = %+v", stats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_PluginUpsertNamespaceError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("mem-1", "root-1", "x", "LOCAL", time.Now().UTC()))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{upsertErr: errors.New("ns dead")}, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
stats, err := backfill(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if stats.Errors != 1 || stats.Copied != 0 {
|
||||
t.Errorf("stats = %+v", stats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfill_PluginCommitMemoryError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id, workspace_id, content, scope, created_at").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "workspace_id", "content", "scope", "created_at"}).
|
||||
AddRow("mem-1", "root-1", "x", "LOCAL", time.Now().UTC()))
|
||||
cfg := backfillConfig{DB: db, Plugin: &stubBackfillPlugin{commitErr: errors.New("mem dead")}, Resolver: rootBackfillResolver(), Limit: 100}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
stats, err := backfill(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if stats.Errors != 1 || stats.Copied != 0 {
|
||||
t.Errorf("stats = %+v", stats)
|
||||
}
|
||||
}
|
||||
|
||||
// --- run (CLI driver) ---
|
||||
|
||||
func TestRun_RejectsBothModes(t *testing.T) {
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{"-dry-run", "-apply"}, stdout, stderr)
|
||||
if err == nil || !strings.Contains(err.Error(), "exactly one") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RejectsNeitherMode(t *testing.T) {
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{}, stdout, stderr)
|
||||
if err == nil || !strings.Contains(err.Error(), "exactly one") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RejectsMissingDatabaseURL(t *testing.T) {
|
||||
t.Setenv("DATABASE_URL", "")
|
||||
t.Setenv("MEMORY_PLUGIN_URL", "http://x")
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{"-dry-run"}, stdout, stderr)
|
||||
if err == nil || !strings.Contains(err.Error(), "DATABASE_URL") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RejectsMissingPluginURL(t *testing.T) {
|
||||
t.Setenv("DATABASE_URL", "postgres://invalid")
|
||||
t.Setenv("MEMORY_PLUGIN_URL", "")
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{"-dry-run"}, stdout, stderr)
|
||||
if err == nil || !strings.Contains(err.Error(), "MEMORY_PLUGIN_URL") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_BadFlags(t *testing.T) {
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{"-not-a-flag"}, stdout, stderr)
|
||||
if err == nil {
|
||||
t.Error("expected flag parse error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package main
|
||||
|
||||
// verify.go — post-apply parity check.
|
||||
//
|
||||
// After a backfill -apply, run with -verify to confirm the migration
|
||||
// actually produced equivalent data. Picks `SampleSize` random
|
||||
// workspaces, queries agent_memories direct + plugin search via the
|
||||
// caller's namespaces, and diffs the result sets by content.
|
||||
//
|
||||
// The diff is best-effort: pg's recent-first ordering and the plugin's
|
||||
// internal ordering may differ, so we compare as sets, not lists.
|
||||
// We do require strict 1:1 multiset equality (every legacy row maps
|
||||
// to exactly one plugin row, ignoring id since the backfill preserves
|
||||
// it via the C1 idempotency key).
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
)
|
||||
|
||||
// verifyConfig is the typed dependency bundle for verifyParity.
|
||||
type verifyConfig struct {
|
||||
DB *sql.DB
|
||||
Plugin verifyPlugin
|
||||
Resolver verifyResolver
|
||||
SampleSize int
|
||||
WorkspaceID string // optional: limit to one workspace
|
||||
Rand *rand.Rand
|
||||
}
|
||||
|
||||
// verifyPlugin is the slice of memory-plugin client we call.
|
||||
type verifyPlugin interface {
|
||||
Search(ctx context.Context, body contract.SearchRequest) (*contract.SearchResponse, error)
|
||||
}
|
||||
|
||||
// verifyResolver mirrors namespace.Resolver. Same shape as
|
||||
// backfillResolver but kept distinct so verify isn't tied to
|
||||
// backfill's interface.
|
||||
type verifyResolver interface {
|
||||
ReadableNamespaces(ctx context.Context, workspaceID string) ([]ResolvedNamespace, error)
|
||||
}
|
||||
|
||||
// ResolvedNamespace is the minimum we need from the resolver — kept
|
||||
// separate so the verify code doesn't depend on the namespace package
|
||||
// (the live tests inject stubs, the binary uses an adapter).
|
||||
type ResolvedNamespace struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// verifyReport accumulates the per-workspace results.
|
||||
type verifyReport struct {
|
||||
WorkspacesSampled int
|
||||
Matches int
|
||||
Mismatches int
|
||||
Errors int
|
||||
}
|
||||
|
||||
// verifyParity is the workhorse. Returns a report; the CLI converts
|
||||
// any non-zero mismatches/errors into a non-zero exit so CI can gate
|
||||
// the cutover.
|
||||
func verifyParity(ctx context.Context, cfg verifyConfig, stdout *os.File) (*verifyReport, error) {
|
||||
report := &verifyReport{}
|
||||
rng := cfg.Rand
|
||||
if rng == nil {
|
||||
rng = rand.New(rand.NewSource(42)) //nolint:gosec // determinism > unpredictability for ops
|
||||
}
|
||||
|
||||
wsIDs, err := pickWorkspaceSample(ctx, cfg.DB, cfg.WorkspaceID, cfg.SampleSize, rng)
|
||||
if err != nil {
|
||||
return report, fmt.Errorf("pick sample: %w", err)
|
||||
}
|
||||
|
||||
for _, wsID := range wsIDs {
|
||||
report.WorkspacesSampled++
|
||||
legacy, err := queryLegacyMemories(ctx, cfg.DB, wsID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stdout, "[err] workspace=%s legacy query: %v\n", wsID, err)
|
||||
report.Errors++
|
||||
continue
|
||||
}
|
||||
readable, err := cfg.Resolver.ReadableNamespaces(ctx, wsID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stdout, "[err] workspace=%s resolve: %v\n", wsID, err)
|
||||
report.Errors++
|
||||
continue
|
||||
}
|
||||
nsList := make([]string, len(readable))
|
||||
for i, ns := range readable {
|
||||
nsList[i] = ns.Name
|
||||
}
|
||||
if len(nsList) == 0 {
|
||||
// No readable namespaces — empty plugin result expected.
|
||||
if len(legacy) == 0 {
|
||||
report.Matches++
|
||||
} else {
|
||||
fmt.Fprintf(stdout, "[mismatch] workspace=%s legacy=%d plugin=0 (no readable namespaces)\n", wsID, len(legacy))
|
||||
report.Mismatches++
|
||||
}
|
||||
continue
|
||||
}
|
||||
resp, err := cfg.Plugin.Search(ctx, contract.SearchRequest{Namespaces: nsList, Limit: 100})
|
||||
if err != nil {
|
||||
fmt.Fprintf(stdout, "[err] workspace=%s plugin search: %v\n", wsID, err)
|
||||
report.Errors++
|
||||
continue
|
||||
}
|
||||
pluginContents := make(map[string]int, len(resp.Memories))
|
||||
for _, m := range resp.Memories {
|
||||
pluginContents[m.Content]++
|
||||
}
|
||||
// Compare as multisets: each legacy content appears at least
|
||||
// once in plugin output. We deliberately tolerate plugin
|
||||
// having MORE rows (the namespace might include team-shared
|
||||
// memories from sibling workspaces that aren't in this
|
||||
// workspace's agent_memories rows).
|
||||
matched := true
|
||||
for _, c := range legacy {
|
||||
if pluginContents[c] == 0 {
|
||||
fmt.Fprintf(stdout, "[mismatch] workspace=%s missing-from-plugin content=%q\n", wsID, truncate(c, 80))
|
||||
matched = false
|
||||
break
|
||||
}
|
||||
pluginContents[c]--
|
||||
}
|
||||
if matched {
|
||||
report.Matches++
|
||||
} else {
|
||||
report.Mismatches++
|
||||
}
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// pickWorkspaceSample returns up to N workspace UUIDs. If
|
||||
// WorkspaceID is set, returns only that one. Otherwise selects N
|
||||
// random workspaces from the workspaces table (TABLESAMPLE would be
|
||||
// nicer but SYSTEM/BERNOULLI sampling has surprising distribution
|
||||
// properties for small populations; we just ORDER BY random() LIMIT).
|
||||
func pickWorkspaceSample(ctx context.Context, db *sql.DB, workspaceID string, n int, _ *rand.Rand) ([]string, error) {
|
||||
if workspaceID != "" {
|
||||
return []string{workspaceID}, nil
|
||||
}
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT id::text
|
||||
FROM workspaces
|
||||
WHERE status != 'removed'
|
||||
ORDER BY random()
|
||||
LIMIT $1
|
||||
`, n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]string, 0, n)
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, id)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// queryLegacyMemories pulls all agent_memories rows for a workspace
|
||||
// (LOCAL + TEAM scopes — what the plugin search would return through
|
||||
// the resolver's readable list, mapped via PR-6 shim semantics).
|
||||
func queryLegacyMemories(ctx context.Context, db *sql.DB, workspaceID string) ([]string, error) {
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT content
|
||||
FROM agent_memories
|
||||
WHERE workspace_id = $1
|
||||
ORDER BY created_at DESC
|
||||
`, workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []string{}
|
||||
for rows.Next() {
|
||||
var c string
|
||||
if err := rows.Scan(&c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "…"
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
)
|
||||
|
||||
// stubVerifyPlugin records search calls and returns canned results.
|
||||
type stubVerifyPlugin struct {
|
||||
searchFn func(ctx context.Context, body contract.SearchRequest) (*contract.SearchResponse, error)
|
||||
}
|
||||
|
||||
func (s *stubVerifyPlugin) Search(ctx context.Context, body contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
if s.searchFn != nil {
|
||||
return s.searchFn(ctx, body)
|
||||
}
|
||||
return &contract.SearchResponse{}, nil
|
||||
}
|
||||
|
||||
// stubVerifyResolver returns a canned readable namespace list.
|
||||
type stubVerifyResolver struct {
|
||||
namespaces []ResolvedNamespace
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubVerifyResolver) ReadableNamespaces(_ context.Context, _ string) ([]ResolvedNamespace, error) {
|
||||
return s.namespaces, s.err
|
||||
}
|
||||
|
||||
// --- pickWorkspaceSample ---
|
||||
|
||||
func TestPickWorkspaceSample_SingleWorkspaceShortCircuit(t *testing.T) {
|
||||
db, _, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
got, err := pickWorkspaceSample(context.Background(), db, "specific-ws", 50, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0] != "specific-ws" {
|
||||
t.Errorf("got %v, want [specific-ws]", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickWorkspaceSample_RandomSample(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WithArgs(50).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).
|
||||
AddRow("ws-1").
|
||||
AddRow("ws-2").
|
||||
AddRow("ws-3"))
|
||||
got, err := pickWorkspaceSample(context.Background(), db, "", 50, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Errorf("got len %d, want 3", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickWorkspaceSample_QueryError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnError(errors.New("dead"))
|
||||
_, err := pickWorkspaceSample(context.Background(), db, "", 50, nil)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickWorkspaceSample_ScanError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "extra"}). // wrong shape
|
||||
AddRow("ws-1", "extra"))
|
||||
_, err := pickWorkspaceSample(context.Background(), db, "", 50, nil)
|
||||
if err == nil {
|
||||
t.Error("expected scan error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- queryLegacyMemories ---
|
||||
|
||||
func TestQueryLegacyMemories_HappyPath(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WithArgs("ws-1").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).
|
||||
AddRow("fact 1").
|
||||
AddRow("fact 2"))
|
||||
got, err := queryLegacyMemories(context.Background(), db, "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if len(got) != 2 || got[0] != "fact 1" {
|
||||
t.Errorf("got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryLegacyMemories_QueryError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnError(errors.New("dead"))
|
||||
_, err := queryLegacyMemories(context.Background(), db, "ws-1")
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- verifyParity (the workhorse) ---
|
||||
|
||||
func TestVerifyParity_AllMatch(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WithArgs("ws-1").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).
|
||||
AddRow("fact A").
|
||||
AddRow("fact B"))
|
||||
|
||||
plugin := &stubVerifyPlugin{
|
||||
searchFn: func(_ context.Context, _ contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "id-A", Content: "fact A"},
|
||||
{ID: "id-B", Content: "fact B"},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
resolver := &stubVerifyResolver{
|
||||
namespaces: []ResolvedNamespace{{Name: "workspace:ws-1"}},
|
||||
}
|
||||
cfg := verifyConfig{DB: db, Plugin: plugin, Resolver: resolver, SampleSize: 50}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, err := verifyParity(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if report.Matches != 1 || report.Mismatches != 0 || report.Errors != 0 {
|
||||
t.Errorf("report = %+v, want 1 match", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_MismatchDetectsMissingFromPlugin(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).
|
||||
AddRow("fact A").
|
||||
AddRow("fact-missing-from-plugin"))
|
||||
|
||||
plugin := &stubVerifyPlugin{
|
||||
searchFn: func(_ context.Context, _ contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "id-A", Content: "fact A"},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
resolver := &stubVerifyResolver{
|
||||
namespaces: []ResolvedNamespace{{Name: "workspace:ws-1"}},
|
||||
}
|
||||
cfg := verifyConfig{DB: db, Plugin: plugin, Resolver: resolver, SampleSize: 50}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, err := verifyParity(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if report.Mismatches != 1 {
|
||||
t.Errorf("report = %+v, want 1 mismatch", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_PluginExtraRowsTolerated(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).
|
||||
AddRow("fact A"))
|
||||
|
||||
// Plugin returns more rows (e.g., team-shared from a sibling).
|
||||
// Verify treats this as a match — legacy is a subset of plugin.
|
||||
plugin := &stubVerifyPlugin{
|
||||
searchFn: func(_ context.Context, _ contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "id-A", Content: "fact A"},
|
||||
{ID: "id-team-1", Content: "team-shared content from sibling"},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
resolver := &stubVerifyResolver{
|
||||
namespaces: []ResolvedNamespace{{Name: "workspace:ws-1"}, {Name: "team:root"}},
|
||||
}
|
||||
cfg := verifyConfig{DB: db, Plugin: plugin, Resolver: resolver, SampleSize: 50}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, err := verifyParity(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if report.Matches != 1 || report.Mismatches != 0 {
|
||||
t.Errorf("report = %+v, want 1 match (plugin-extra is OK)", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_LegacyQueryError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnError(errors.New("dead"))
|
||||
|
||||
cfg := verifyConfig{
|
||||
DB: db,
|
||||
Plugin: &stubVerifyPlugin{},
|
||||
Resolver: &stubVerifyResolver{namespaces: []ResolvedNamespace{{Name: "workspace:ws-1"}}},
|
||||
}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, err := verifyParity(context.Background(), cfg, devnull)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if report.Errors != 1 {
|
||||
t.Errorf("report = %+v, want 1 error", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_ResolverError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).AddRow("x"))
|
||||
|
||||
cfg := verifyConfig{
|
||||
DB: db,
|
||||
Plugin: &stubVerifyPlugin{},
|
||||
Resolver: &stubVerifyResolver{err: errors.New("dead")},
|
||||
}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, _ := verifyParity(context.Background(), cfg, devnull)
|
||||
if report.Errors != 1 {
|
||||
t.Errorf("report = %+v, want 1 error", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_PluginSearchError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).AddRow("x"))
|
||||
|
||||
cfg := verifyConfig{
|
||||
DB: db,
|
||||
Plugin: &stubVerifyPlugin{
|
||||
searchFn: func(_ context.Context, _ contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return nil, errors.New("plugin dead")
|
||||
},
|
||||
},
|
||||
Resolver: &stubVerifyResolver{namespaces: []ResolvedNamespace{{Name: "workspace:ws-1"}}},
|
||||
}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, _ := verifyParity(context.Background(), cfg, devnull)
|
||||
if report.Errors != 1 {
|
||||
t.Errorf("report = %+v, want 1 error", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_NoReadableNamespacesEmptyLegacy(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"})) // empty
|
||||
|
||||
cfg := verifyConfig{
|
||||
DB: db,
|
||||
Plugin: &stubVerifyPlugin{},
|
||||
Resolver: &stubVerifyResolver{namespaces: []ResolvedNamespace{}}, // empty
|
||||
}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, _ := verifyParity(context.Background(), cfg, devnull)
|
||||
// Empty legacy + empty namespaces → match.
|
||||
if report.Matches != 1 {
|
||||
t.Errorf("report = %+v, want 1 match (both empty)", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_NoReadableNamespacesNonEmptyLegacy(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("ws-1"))
|
||||
mock.ExpectQuery("SELECT content FROM agent_memories").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"content"}).AddRow("orphan-fact"))
|
||||
|
||||
cfg := verifyConfig{
|
||||
DB: db,
|
||||
Plugin: &stubVerifyPlugin{},
|
||||
Resolver: &stubVerifyResolver{namespaces: []ResolvedNamespace{}},
|
||||
}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
report, _ := verifyParity(context.Background(), cfg, devnull)
|
||||
// Legacy has rows but plugin can't see any → mismatch.
|
||||
if report.Mismatches != 1 {
|
||||
t.Errorf("report = %+v, want 1 mismatch", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyParity_PickSampleError(t *testing.T) {
|
||||
db, mock, _ := sqlmock.New()
|
||||
defer db.Close()
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnError(errors.New("dead"))
|
||||
cfg := verifyConfig{DB: db, Plugin: &stubVerifyPlugin{}, Resolver: &stubVerifyResolver{}}
|
||||
devnull, _ := os.Open(os.DevNull)
|
||||
defer devnull.Close()
|
||||
_, err := verifyParity(context.Background(), cfg, devnull)
|
||||
if err == nil || !strings.Contains(err.Error(), "pick sample") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Truncate ---
|
||||
|
||||
func TestVerifyTruncate(t *testing.T) {
|
||||
if got := truncate("short", 10); got != "short" {
|
||||
t.Errorf("got %q", got)
|
||||
}
|
||||
if got := truncate(strings.Repeat("a", 200), 10); !strings.HasSuffix(got, "…") {
|
||||
t.Errorf("expected ellipsis: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// --- CLI: -verify mode ---
|
||||
|
||||
func TestRun_VerifyVsApplyMutuallyExclusive(t *testing.T) {
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{"-verify", "-apply"}, stdout, stderr)
|
||||
if err == nil || !strings.Contains(err.Error(), "exactly one") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_VerifyAloneIsValid(t *testing.T) {
|
||||
t.Setenv("DATABASE_URL", "")
|
||||
t.Setenv("MEMORY_PLUGIN_URL", "http://x")
|
||||
stderr, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stderr.Close()
|
||||
stdout, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
||||
defer stdout.Close()
|
||||
err := run([]string{"-verify"}, stdout, stderr)
|
||||
// Will fail later on missing DATABASE_URL, NOT on the
|
||||
// mutually-exclusive-modes check. Asserts that -verify is
|
||||
// recognized as a valid mode.
|
||||
if err == nil || !strings.Contains(err.Error(), "DATABASE_URL") {
|
||||
t.Errorf("err = %v, want DATABASE_URL error (-verify alone is a valid mode)", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
# Real-subprocess E2E for memory-plugin-postgres
|
||||
|
||||
The default `go test ./...` suite covers the plugin via in-process
|
||||
sqlmock tests (PR-3). This directory ALSO ships build-tag-gated tests
|
||||
that spawn the real binary against a live postgres — to catch
|
||||
classes of bug in-process tests can't see:
|
||||
|
||||
- Boot-path regressions (env var typos, panic-on-startup)
|
||||
- Wire-format bugs sqlmock smooths over (the `pq.Array` issue we
|
||||
hit during PR-3 development)
|
||||
- HTTP/socket encoding edge cases
|
||||
- C1 idempotency (real upsert against real postgres)
|
||||
|
||||
## Running
|
||||
|
||||
The tests skip silently unless an operator opts in with both:
|
||||
- The `memory_plugin_e2e` build tag
|
||||
- `MEMORY_PLUGIN_E2E_DB` env var pointing at a writable postgres
|
||||
|
||||
### Quick local run (with docker)
|
||||
|
||||
```bash
|
||||
docker run --rm -d --name memory-plugin-e2e-pg \
|
||||
-e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -e POSTGRES_DB=test \
|
||||
-p 5432:5432 \
|
||||
pgvector/pgvector:pg16
|
||||
|
||||
# Wait a few seconds for postgres to accept connections
|
||||
until docker exec memory-plugin-e2e-pg pg_isready -U test >/dev/null 2>&1; do sleep 0.5; done
|
||||
|
||||
MEMORY_PLUGIN_E2E_DB=postgres://test:test@localhost:5432/test?sslmode=disable \
|
||||
go test -tags memory_plugin_e2e -v -count=1 ./cmd/memory-plugin-postgres/
|
||||
|
||||
docker stop memory-plugin-e2e-pg
|
||||
```
|
||||
|
||||
### CI integration
|
||||
|
||||
These tests are NOT in the default required-checks set. Operators
|
||||
gating cutover on the suite should add a separate workflow step:
|
||||
|
||||
```yaml
|
||||
- name: Memory plugin E2E
|
||||
if: ${{ contains(github.event.pull_request.labels.*.name, 'memory-v2') }}
|
||||
run: |
|
||||
MEMORY_PLUGIN_E2E_DB=${{ secrets.MEMORY_PLUGIN_TEST_DSN }} \
|
||||
go test -tags memory_plugin_e2e -v -count=1 ./cmd/memory-plugin-postgres/
|
||||
```
|
||||
|
||||
## What each test pins
|
||||
|
||||
| Test | Covers |
|
||||
|---|---|
|
||||
| `TestE2E_BootAndHealth` | Binary builds, starts, advertises all 5 capabilities |
|
||||
| `TestE2E_FullCommitSearchForgetRoundTrip` | Real wire encoding (no sqlmock), full agent flow |
|
||||
| `TestE2E_IdempotencyKey` | C1 fix end-to-end — upserts against real postgres |
|
||||
|
||||
## What's still NOT covered
|
||||
|
||||
- Migration drift (assumes the migrations dir is at the conventional
|
||||
path; operator-customized layouts need their own test)
|
||||
- Plugin-internal recovery (kill backing store mid-request, etc.)
|
||||
- Concurrent commits with id collisions across processes
|
||||
- TTL eviction (would need to extend test runtime past `expires_at`)
|
||||
|
||||
These gaps apply equally to forks of this binary; they're listed in
|
||||
[`testing-your-plugin.md`](../../../docs/memory-plugins/testing-your-plugin.md)
|
||||
under "what the harness does NOT cover".
|
||||
@@ -0,0 +1,289 @@
|
||||
//go:build memory_plugin_e2e
|
||||
|
||||
// Package main's real-subprocess boot test (#293 fixup, RFC #2728).
|
||||
//
|
||||
// Build-tag gated so it only runs when an operator explicitly opts in:
|
||||
//
|
||||
// MEMORY_PLUGIN_E2E_DB=postgres://test:test@localhost:5432/test?sslmode=disable \
|
||||
// go test -tags memory_plugin_e2e -v ./cmd/memory-plugin-postgres/
|
||||
//
|
||||
// Why a separate build tag:
|
||||
// - The default `go test ./...` run shouldn't require docker or a
|
||||
// live postgres
|
||||
// - CI gates that DO want to run this can set the env var + tag
|
||||
// - Operators verifying a custom plugin against the contract can
|
||||
// copy this file as the template (replace the binary build step
|
||||
// with their own)
|
||||
//
|
||||
// What this exercises that PR-11's swap test doesn't:
|
||||
// - Real `go build` of cmd/memory-plugin-postgres/
|
||||
// - Real binary boot via os/exec — catches mixed-key panics, missing
|
||||
// env vars, crash-on-startup issues that in-process tests skip
|
||||
// - Real postgres connection — catches wire-format bugs (e.g. the
|
||||
// pq.Array regression we hit during PR-3)
|
||||
// - Real HTTP round-trip with a TCP socket — catches encoding edge
|
||||
// cases sqlmock + httptest can't see
|
||||
//
|
||||
// What this does NOT cover:
|
||||
// - Schema migration drift (assumes the migrations dir is at the
|
||||
// conventional path; operator-customized layouts need their own
|
||||
// test)
|
||||
// - Plugin-internal recovery (kill backing store mid-request, etc.)
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
mclient "github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/client"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
)
|
||||
|
||||
const (
|
||||
bootProbeTimeout = 30 * time.Second
|
||||
bootProbeStep = 500 * time.Millisecond
|
||||
)
|
||||
|
||||
// requireE2EDB returns the test DSN. Skips the test (not fails) when
|
||||
// the env var is unset — keeps `-tags memory_plugin_e2e` runs from
|
||||
// crashing on dev machines without postgres.
|
||||
func requireE2EDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("MEMORY_PLUGIN_E2E_DB")
|
||||
if dsn == "" {
|
||||
t.Skip("MEMORY_PLUGIN_E2E_DB not set — skipping real-subprocess boot test")
|
||||
}
|
||||
return dsn
|
||||
}
|
||||
|
||||
// buildBinary compiles cmd/memory-plugin-postgres/ to a temp dir.
|
||||
// Returns the path of the built binary. Test cleanup deletes it.
|
||||
func buildBinary(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
out := filepath.Join(dir, "memory-plugin-postgres")
|
||||
if runtime.GOOS == "windows" {
|
||||
out += ".exe"
|
||||
}
|
||||
// Find the cmd dir relative to this file.
|
||||
_, thisFile, _, _ := runtime.Caller(0)
|
||||
cmdDir := filepath.Dir(thisFile)
|
||||
build := exec.Command("go", "build", "-o", out, ".")
|
||||
build.Dir = cmdDir
|
||||
build.Env = os.Environ()
|
||||
if outErr, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("go build failed: %v\n%s", err, outErr)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// startBinary launches the built binary with the supplied env. Returns
|
||||
// the *exec.Cmd (test cleanup kills it) and the http URL it's listening
|
||||
// on. Polls /v1/health until ready or times out.
|
||||
func startBinary(t *testing.T, binary, dsn, listen string) (*exec.Cmd, string) {
|
||||
t.Helper()
|
||||
url := "http://" + listen
|
||||
cmd := exec.Command(binary)
|
||||
cmd.Env = append(os.Environ(),
|
||||
"MEMORY_PLUGIN_DATABASE_URL="+dsn,
|
||||
"MEMORY_PLUGIN_LISTEN_ADDR="+listen,
|
||||
// Migrations dir lives next to the cmd source. The binary
|
||||
// reads it relative to cwd by default; we set the env var
|
||||
// override so the test doesn't depend on cwd.
|
||||
"MEMORY_PLUGIN_MIGRATIONS_DIR="+migrationsDirForTest(t),
|
||||
)
|
||||
stdout := &bytes.Buffer{}
|
||||
stderr := &bytes.Buffer{}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatalf("start binary: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if cmd.Process != nil {
|
||||
_ = cmd.Process.Kill()
|
||||
_ = cmd.Wait()
|
||||
}
|
||||
if t.Failed() {
|
||||
t.Logf("binary stdout:\n%s", stdout.String())
|
||||
t.Logf("binary stderr:\n%s", stderr.String())
|
||||
}
|
||||
})
|
||||
|
||||
deadline := time.Now().Add(bootProbeTimeout)
|
||||
for time.Now().Before(deadline) {
|
||||
resp, err := http.Get(url + "/v1/health")
|
||||
if err == nil {
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode == 200 {
|
||||
return cmd, url
|
||||
}
|
||||
}
|
||||
// Bail early if the binary already exited.
|
||||
if cmd.ProcessState != nil && cmd.ProcessState.Exited() {
|
||||
t.Fatalf("binary exited during boot: stderr:\n%s", stderr.String())
|
||||
}
|
||||
time.Sleep(bootProbeStep)
|
||||
}
|
||||
t.Fatalf("binary did not become ready within %v", bootProbeTimeout)
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func migrationsDirForTest(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, thisFile, _, _ := runtime.Caller(0)
|
||||
return filepath.Join(filepath.Dir(thisFile), "migrations")
|
||||
}
|
||||
|
||||
// TestE2E_BootAndHealth: build + start the real binary, hit /v1/health,
|
||||
// confirm capabilities match what the built-in plugin declares. Catches
|
||||
// "binary doesn't start" / "wrong env var name" / "panics on first
|
||||
// request" classes that in-process tests miss.
|
||||
func TestE2E_BootAndHealth(t *testing.T) {
|
||||
dsn := requireE2EDB(t)
|
||||
binary := buildBinary(t)
|
||||
_, url := startBinary(t, binary, dsn, "127.0.0.1:19100")
|
||||
cl := mclient.New(mclient.Config{BaseURL: url})
|
||||
|
||||
hr, err := cl.Boot(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Boot: %v", err)
|
||||
}
|
||||
if hr.Status != "ok" {
|
||||
t.Errorf("status = %q", hr.Status)
|
||||
}
|
||||
wantCaps := map[string]bool{"fts": true, "embedding": true, "ttl": true, "pin": true, "propagation": true}
|
||||
gotCaps := map[string]bool{}
|
||||
for _, c := range hr.Capabilities {
|
||||
gotCaps[c] = true
|
||||
}
|
||||
for c := range wantCaps {
|
||||
if !gotCaps[c] {
|
||||
t.Errorf("capability %q missing — built-in plugin should declare all 5", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestE2E_FullCommitSearchForgetRoundTrip: the full agent flow against
|
||||
// real postgres + real HTTP. Catches wire-format regressions (the
|
||||
// pq.Array bug we hit during PR-3 development) and contract-level
|
||||
// drift between Go bindings and the spec.
|
||||
func TestE2E_FullCommitSearchForgetRoundTrip(t *testing.T) {
|
||||
dsn := requireE2EDB(t)
|
||||
binary := buildBinary(t)
|
||||
_, url := startBinary(t, binary, dsn, "127.0.0.1:19101")
|
||||
cl := mclient.New(mclient.Config{BaseURL: url})
|
||||
|
||||
ctx := context.Background()
|
||||
ns := fmt.Sprintf("workspace:e2e-%d", time.Now().UnixNano())
|
||||
|
||||
// 1. Upsert namespace.
|
||||
if _, err := cl.UpsertNamespace(ctx, ns, contract.NamespaceUpsert{Kind: contract.NamespaceKindWorkspace}); err != nil {
|
||||
t.Fatalf("UpsertNamespace: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = cl.DeleteNamespace(context.Background(), ns) })
|
||||
|
||||
// 2. Commit a memory.
|
||||
resp, err := cl.CommitMemory(ctx, ns, contract.MemoryWrite{
|
||||
Content: "user prefers tabs over spaces",
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CommitMemory: %v", err)
|
||||
}
|
||||
if resp.ID == "" {
|
||||
t.Fatal("plugin returned empty memory id")
|
||||
}
|
||||
|
||||
// 3. Search and find the memory we just wrote.
|
||||
sresp, err := cl.Search(ctx, contract.SearchRequest{Namespaces: []string{ns}, Query: "tabs"})
|
||||
if err != nil {
|
||||
t.Fatalf("Search: %v", err)
|
||||
}
|
||||
if len(sresp.Memories) == 0 {
|
||||
t.Errorf("Search returned 0 memories, want at least 1")
|
||||
}
|
||||
found := false
|
||||
for _, m := range sresp.Memories {
|
||||
if m.ID == resp.ID && m.Content == "user prefers tabs over spaces" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
got, _ := json.Marshal(sresp.Memories)
|
||||
t.Errorf("committed memory not found in search results: %s", got)
|
||||
}
|
||||
|
||||
// 4. Forget the memory.
|
||||
if err := cl.ForgetMemory(ctx, resp.ID, contract.ForgetRequest{RequestedByNamespace: ns}); err != nil {
|
||||
t.Fatalf("ForgetMemory: %v", err)
|
||||
}
|
||||
|
||||
// 5. Search again — gone.
|
||||
sresp, err = cl.Search(ctx, contract.SearchRequest{Namespaces: []string{ns}, Query: "tabs"})
|
||||
if err != nil {
|
||||
t.Fatalf("Search after forget: %v", err)
|
||||
}
|
||||
for _, m := range sresp.Memories {
|
||||
if m.ID == resp.ID {
|
||||
t.Errorf("forgotten memory still in search results")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestE2E_IdempotencyKey covers the C1 fix end-to-end: same id passed
|
||||
// twice should upsert (one row, updated content), not duplicate.
|
||||
func TestE2E_IdempotencyKey(t *testing.T) {
|
||||
dsn := requireE2EDB(t)
|
||||
binary := buildBinary(t)
|
||||
_, url := startBinary(t, binary, dsn, "127.0.0.1:19102")
|
||||
cl := mclient.New(mclient.Config{BaseURL: url})
|
||||
|
||||
ctx := context.Background()
|
||||
ns := fmt.Sprintf("workspace:e2e-idem-%d", time.Now().UnixNano())
|
||||
if _, err := cl.UpsertNamespace(ctx, ns, contract.NamespaceUpsert{Kind: contract.NamespaceKindWorkspace}); err != nil {
|
||||
t.Fatalf("UpsertNamespace: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = cl.DeleteNamespace(context.Background(), ns) })
|
||||
|
||||
fixedID := "11111111-2222-3333-4444-555555555555"
|
||||
for i, content := range []string{"first version", "second version (updated)"} {
|
||||
if _, err := cl.CommitMemory(ctx, ns, contract.MemoryWrite{
|
||||
ID: fixedID,
|
||||
Content: content,
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
}); err != nil {
|
||||
t.Fatalf("commit %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
sresp, err := cl.Search(ctx, contract.SearchRequest{Namespaces: []string{ns}})
|
||||
if err != nil {
|
||||
t.Fatalf("Search: %v", err)
|
||||
}
|
||||
matches := 0
|
||||
for _, m := range sresp.Memories {
|
||||
if m.ID == fixedID {
|
||||
matches++
|
||||
if m.Content != "second version (updated)" {
|
||||
t.Errorf("upsert did not update content: got %q", m.Content)
|
||||
}
|
||||
}
|
||||
}
|
||||
if matches != 1 {
|
||||
t.Errorf("upsert produced %d rows for id=%s, want 1", matches, fixedID)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestLoadConfig_DefaultListenAddrIsLoopback pins the default-bind contract.
|
||||
//
|
||||
// Why this matters: with the prior `:9100` default, the plugin listened on
|
||||
// every interface. Inside the container it didn't matter (no host port
|
||||
// mapping today), but a future change that publishes 9100 OR a cross-host
|
||||
// sidecar deploy would have exposed an unauth'd memory store. Loopback by
|
||||
// default is the least-privilege baseline; operators with a multi-host
|
||||
// topology override via MEMORY_PLUGIN_LISTEN_ADDR.
|
||||
func TestLoadConfig_DefaultListenAddrIsLoopback(t *testing.T) {
|
||||
t.Setenv("MEMORY_PLUGIN_DATABASE_URL", "postgres://stub")
|
||||
t.Setenv("MEMORY_PLUGIN_LISTEN_ADDR", "")
|
||||
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("loadConfig: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(cfg.ListenAddr, "127.0.0.1:") {
|
||||
t.Errorf("default ListenAddr must bind loopback-only, got %q "+
|
||||
"(security regression — would expose plugin on every interface)",
|
||||
cfg.ListenAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_ListenAddrEnvOverride(t *testing.T) {
|
||||
t.Setenv("MEMORY_PLUGIN_DATABASE_URL", "postgres://stub")
|
||||
t.Setenv("MEMORY_PLUGIN_LISTEN_ADDR", ":9100")
|
||||
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("loadConfig: %v", err)
|
||||
}
|
||||
if cfg.ListenAddr != ":9100" {
|
||||
t.Errorf("env override ignored: want :9100, got %q", cfg.ListenAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_MissingDatabaseURL(t *testing.T) {
|
||||
t.Setenv("MEMORY_PLUGIN_DATABASE_URL", "")
|
||||
|
||||
if _, err := loadConfig(); err == nil {
|
||||
t.Fatal("loadConfig must error when MEMORY_PLUGIN_DATABASE_URL is empty")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
// memory-plugin-postgres is the built-in implementation of the memory
|
||||
// plugin contract (RFC #2728). Operators run it next to workspace-
|
||||
// server; workspace-server points MEMORY_PLUGIN_URL at it.
|
||||
//
|
||||
// Owns its own postgres tables (see migrations/). When an operator
|
||||
// swaps in a different plugin, this binary's tables become orphaned
|
||||
// — not auto-dropped. Document this in the plugin docs (PR-10).
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/pgplugin"
|
||||
)
|
||||
|
||||
// migrationsFS bundles the .up.sql files into the binary at build time
|
||||
// so the prebuilt image doesn't need the source tree at runtime. The
|
||||
// prior `os.ReadDir("cmd/memory-plugin-postgres/migrations")` path
|
||||
// only resolved during `go test` from the repo root — in the published
|
||||
// image the path didn't exist and boot failed after the 30s health gate
|
||||
// (caught on staging redeploy 2026-05-05 after PR #2906).
|
||||
//
|
||||
//go:embed migrations/*.up.sql
|
||||
var migrationsFS embed.FS
|
||||
|
||||
const (
|
||||
envDatabaseURL = "MEMORY_PLUGIN_DATABASE_URL"
|
||||
envListenAddr = "MEMORY_PLUGIN_LISTEN_ADDR"
|
||||
envSkipMigrate = "MEMORY_PLUGIN_SKIP_MIGRATE"
|
||||
|
||||
// Loopback-only by default (defense in depth). The platform talks to
|
||||
// the plugin over `http://localhost:9100` from the same container, so
|
||||
// binding to all interfaces would only widen the reachable surface
|
||||
// without enabling any in-design caller. Operators running the plugin
|
||||
// on a separate host override via MEMORY_PLUGIN_LISTEN_ADDR=:9100 (or
|
||||
// some other interface).
|
||||
defaultListenAddr = "127.0.0.1:9100"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatalf("memory-plugin-postgres: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// run is the boot path. Extracted from main() so tests can drive it
|
||||
// with synthesized env. Returns nil on graceful shutdown, an error on
|
||||
// failure to bring up.
|
||||
func run() error {
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("config: %w", err)
|
||||
}
|
||||
|
||||
db, err := openDB(cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open db: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if !cfg.SkipMigrate {
|
||||
if err := runMigrations(db); err != nil {
|
||||
return fmt.Errorf("migrate: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
store := pgplugin.NewStore(db)
|
||||
handler := pgplugin.NewHandler(store, func() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
return db.PingContext(ctx)
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.ListenAddr,
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
// Listen separately so we can log the bound port (handy when
|
||||
// :0 is used in tests).
|
||||
ln, err := net.Listen("tcp", cfg.ListenAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen %s: %w", cfg.ListenAddr, err)
|
||||
}
|
||||
log.Printf("memory-plugin-postgres listening on %s", ln.Addr())
|
||||
|
||||
// Run server in a goroutine; main waits on signal.
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
select {
|
||||
case <-sigCh:
|
||||
log.Println("shutdown signal received")
|
||||
case err := <-errCh:
|
||||
return fmt.Errorf("serve: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
return srv.Shutdown(ctx)
|
||||
}
|
||||
|
||||
type config struct {
|
||||
DatabaseURL string
|
||||
ListenAddr string
|
||||
SkipMigrate bool
|
||||
}
|
||||
|
||||
func loadConfig() (*config, error) {
|
||||
dbURL := strings.TrimSpace(os.Getenv(envDatabaseURL))
|
||||
if dbURL == "" {
|
||||
return nil, fmt.Errorf("%s is required", envDatabaseURL)
|
||||
}
|
||||
addr := strings.TrimSpace(os.Getenv(envListenAddr))
|
||||
if addr == "" {
|
||||
addr = defaultListenAddr
|
||||
}
|
||||
return &config{
|
||||
DatabaseURL: dbURL,
|
||||
ListenAddr: addr,
|
||||
SkipMigrate: os.Getenv(envSkipMigrate) == "1",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func openDB(databaseURL string) (*sql.DB, error) {
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(25)
|
||||
db.SetMaxIdleConns(5)
|
||||
db.SetConnMaxLifetime(30 * time.Minute)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return nil, fmt.Errorf("ping: %w", err)
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// runMigrations applies the schema migrations bundled into the binary
|
||||
// via go:embed (see migrationsFS at the top of this file). Idempotent
|
||||
// on repeat boot — every migration file uses CREATE … IF NOT EXISTS.
|
||||
//
|
||||
// The down migrations are deliberately NOT applied here — that's a
|
||||
// manual operator action. This keeps the binary tiny and avoids
|
||||
// dragging in golang-migrate's drivers.
|
||||
//
|
||||
// MEMORY_PLUGIN_MIGRATIONS_DIR (filesystem path) is honored as an
|
||||
// override for operators who need to ship custom migrations alongside
|
||||
// the binary without rebuilding. When unset (the common case) we read
|
||||
// from the embedded FS.
|
||||
func runMigrations(db *sql.DB) error {
|
||||
if dir := strings.TrimSpace(os.Getenv("MEMORY_PLUGIN_MIGRATIONS_DIR")); dir != "" {
|
||||
return runMigrationsFromDisk(db, dir)
|
||||
}
|
||||
return runMigrationsFromEmbed(db)
|
||||
}
|
||||
|
||||
// runMigrationsFromEmbed applies the *.up.sql files bundled into the
|
||||
// binary at build time. Order is alphabetical (matches the on-disk
|
||||
// behavior of os.ReadDir on Linux for the same set of names).
|
||||
func runMigrationsFromEmbed(db *sql.DB) error {
|
||||
entries, err := migrationsFS.ReadDir("migrations")
|
||||
if err != nil {
|
||||
return fmt.Errorf("read embedded migrations: %w", err)
|
||||
}
|
||||
names := make([]string, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".up.sql") {
|
||||
continue
|
||||
}
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
data, err := migrationsFS.ReadFile("migrations/" + name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read embedded %q: %w", name, err)
|
||||
}
|
||||
if _, err := db.Exec(string(data)); err != nil {
|
||||
return fmt.Errorf("apply %q: %w", name, err)
|
||||
}
|
||||
log.Printf("applied embedded migration %s", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runMigrationsFromDisk preserves the legacy filesystem-path mode for
|
||||
// operator-supplied custom migrations.
|
||||
func runMigrationsFromDisk(db *sql.DB, dir string) error {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read migrations dir %q: %w", dir, err)
|
||||
}
|
||||
names := make([]string, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".up.sql") {
|
||||
continue
|
||||
}
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
path := dir + "/" + name
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read %q: %w", path, err)
|
||||
}
|
||||
if _, err := db.Exec(string(data)); err != nil {
|
||||
return fmt.Errorf("apply %q: %w", path, err)
|
||||
}
|
||||
log.Printf("applied disk migration %s (from %s)", name, dir)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Down migration for memory_v2 plugin schema (RFC #2728).
|
||||
DROP TABLE IF EXISTS memory_records;
|
||||
DROP TABLE IF EXISTS memory_namespaces;
|
||||
@@ -0,0 +1,47 @@
|
||||
-- Memory v2 plugin schema (RFC #2728).
|
||||
--
|
||||
-- These tables are owned by the built-in postgres memory plugin, NOT
|
||||
-- by workspace-server. When an operator swaps in a different memory
|
||||
-- plugin (Pinecone, Letta, custom), these tables become orphaned —
|
||||
-- not auto-dropped. Operator drops them when they're confident they
|
||||
-- don't want to switch back.
|
||||
--
|
||||
-- Lives under cmd/memory-plugin-postgres/migrations/ (NOT
|
||||
-- workspace-server/migrations/) to make the ownership boundary
|
||||
-- visible: workspace-server has zero knowledge of these tables.
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS vector;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS memory_namespaces (
|
||||
name TEXT PRIMARY KEY,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('workspace','team','org','custom')),
|
||||
expires_at TIMESTAMPTZ,
|
||||
metadata JSONB,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS memory_records (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
namespace TEXT NOT NULL REFERENCES memory_namespaces(name) ON DELETE CASCADE,
|
||||
content TEXT NOT NULL,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('fact','summary','checkpoint')),
|
||||
source TEXT NOT NULL CHECK (source IN ('agent','runtime','user')),
|
||||
expires_at TIMESTAMPTZ,
|
||||
propagation JSONB,
|
||||
pin BOOLEAN NOT NULL DEFAULT false,
|
||||
embedding vector(1536),
|
||||
content_tsv tsvector GENERATED ALWAYS AS (to_tsvector('english', content)) STORED,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Indexes:
|
||||
-- - namespace: every search filters by namespace list
|
||||
-- - content_tsv: FTS path
|
||||
-- - embedding: semantic search (partial because most rows have no embedding)
|
||||
-- - expires_at: TTL janitor scans
|
||||
CREATE INDEX IF NOT EXISTS idx_memory_records_namespace ON memory_records(namespace);
|
||||
CREATE INDEX IF NOT EXISTS idx_memory_records_fts ON memory_records USING GIN (content_tsv);
|
||||
CREATE INDEX IF NOT EXISTS idx_memory_records_embedding ON memory_records
|
||||
USING ivfflat (embedding) WHERE embedding IS NOT NULL;
|
||||
CREATE INDEX IF NOT EXISTS idx_memory_records_expires ON memory_records (expires_at)
|
||||
WHERE expires_at IS NOT NULL;
|
||||
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMigrationsEmbedded_ContainsCreateTable pins that the migrations
|
||||
// are bundled into the binary at build time, NOT loaded from a
|
||||
// filesystem path that doesn't exist at runtime in the published image.
|
||||
//
|
||||
// Pre-fix: PR #2906 shipped the binary without the migrations dir;
|
||||
// `os.ReadDir("cmd/memory-plugin-postgres/migrations")` errored on every
|
||||
// tenant boot, the 30s health gate aborted the container, and the
|
||||
// staging redeploy fleet job marked all tenants as failed. Embedding
|
||||
// the migrations into the binary removes the runtime path entirely.
|
||||
func TestMigrationsEmbedded_ContainsCreateTable(t *testing.T) {
|
||||
entries, err := migrationsFS.ReadDir("migrations")
|
||||
if err != nil {
|
||||
t.Fatalf("embedded migrations dir unreadable: %v", err)
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
t.Fatal("embedded migrations dir is empty — go:embed pattern matched no files")
|
||||
}
|
||||
|
||||
var seenUp bool
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".up.sql") {
|
||||
continue
|
||||
}
|
||||
seenUp = true
|
||||
data, err := migrationsFS.ReadFile("migrations/" + e.Name())
|
||||
if err != nil {
|
||||
t.Errorf("read embedded %q: %v", e.Name(), err)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(string(data), "CREATE TABLE") {
|
||||
t.Errorf("embedded %q has no CREATE TABLE — wrong file embedded?", e.Name())
|
||||
}
|
||||
}
|
||||
if !seenUp {
|
||||
t.Fatal("no *.up.sql in embedded migrations — runtime would have no schema to apply")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunMigrationsFromEmbed_OrderingIsAlphabetic pins that we apply
|
||||
// migrations in deterministic alphabetical order, not in whatever
|
||||
// arbitrary order migrationsFS.ReadDir happens to return. With one
|
||||
// migration today this is moot, but a future second migration ('002_…')
|
||||
// MUST run after '001_…' or the schema is broken.
|
||||
//
|
||||
// We can't easily exercise db.Exec here (no test DB); instead pin the
|
||||
// sort step on the directory listing itself.
|
||||
func TestRunMigrationsFromEmbed_OrderingIsAlphabetic(t *testing.T) {
|
||||
entries, err := migrationsFS.ReadDir("migrations")
|
||||
if err != nil {
|
||||
t.Fatalf("embedded migrations dir unreadable: %v", err)
|
||||
}
|
||||
var names []string
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".up.sql") {
|
||||
continue
|
||||
}
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
for i := 1; i < len(names); i++ {
|
||||
if names[i-1] > names[i] {
|
||||
t.Errorf("ReadDir returned non-sorted names; runMigrationsFromEmbed must sort. "+
|
||||
"Got %q before %q", names[i-1], names[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,8 @@ import (
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/events"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/handlers"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/imagewatch"
|
||||
memwiring "github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/wiring"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/pendinguploads"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/provisioner"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/registry"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/router"
|
||||
@@ -166,6 +168,16 @@ func main() {
|
||||
wh.SetCPProvisioner(cpProv)
|
||||
}
|
||||
|
||||
// Memory v2 plugin (RFC #2728): build the dependency bundle once
|
||||
// here so all three handlers (MCPHandler, AdminMemoriesHandler,
|
||||
// WorkspaceHandler) get the same plugin/resolver pair. memBundle
|
||||
// is nil when MEMORY_PLUGIN_URL is unset — every consumer
|
||||
// nil-checks before using.
|
||||
memBundle := memwiring.Build(db.DB)
|
||||
if memBundle != nil {
|
||||
wh.WithNamespaceCleanup(memBundle.NamespaceCleanupFn())
|
||||
}
|
||||
|
||||
// External-plugin env mutators — each plugin contributes 0+ mutators
|
||||
// onto a shared registry. Order matters: gh-identity populates
|
||||
// MOLECULE_AGENT_ROLE-derived attribution env vars that downstream
|
||||
@@ -254,6 +266,14 @@ func main() {
|
||||
})
|
||||
}
|
||||
|
||||
// Pending-uploads GC sweep — deletes acked rows past their retention
|
||||
// window plus unacked rows past expires_at. Without this the
|
||||
// pending_uploads table grows unbounded; even with the 24h hard TTL,
|
||||
// nothing actually deletes a row, just makes it un-fetchable.
|
||||
go supervised.RunWithRecover(ctx, "pending-uploads-sweeper", func(c context.Context) {
|
||||
pendinguploads.StartSweeper(c, pendinguploads.NewPostgres(db.DB), 0)
|
||||
})
|
||||
|
||||
// Provision-timeout sweep — flips workspaces that have been stuck in
|
||||
// status='provisioning' past the timeout window to 'failed' and emits
|
||||
// WORKSPACE_PROVISION_TIMEOUT. Without this the UI banner is cosmetic
|
||||
@@ -286,6 +306,15 @@ func main() {
|
||||
registry.StartHibernationMonitor(c, wh.HibernateWorkspace)
|
||||
})
|
||||
|
||||
// RFC #2829 PR-3: stuck-task sweeper for the durable delegations
|
||||
// ledger. Marks deadline-exceeded rows as failed and heartbeat-stale
|
||||
// in-flight rows as stuck. Both transitions go through the ledger's
|
||||
// terminal forward-only protection so concurrent UpdateStatus calls
|
||||
// are not clobbered. Defaults: 5min interval, 10min stale threshold;
|
||||
// override via DELEGATION_SWEEPER_INTERVAL_S / DELEGATION_STUCK_THRESHOLD_S.
|
||||
delegSweeper := handlers.NewDelegationSweeper(nil, nil)
|
||||
go supervised.RunWithRecover(ctx, "delegation-sweeper", delegSweeper.Start)
|
||||
|
||||
// Channel Manager — social channel integrations (Telegram, Slack, etc.)
|
||||
channelMgr := channels.NewManager(wh, broadcaster)
|
||||
go supervised.RunWithRecover(ctx, "channel-manager", channelMgr.Start)
|
||||
@@ -306,7 +335,7 @@ func main() {
|
||||
cronSched.SetChannels(channelMgr)
|
||||
|
||||
// Router
|
||||
r := router.Setup(hub, broadcaster, prov, platformURL, configsDir, wh, channelMgr)
|
||||
r := router.Setup(hub, broadcaster, prov, platformURL, configsDir, wh, channelMgr, memBundle)
|
||||
|
||||
// HTTP server with graceful shutdown
|
||||
srv := &http.Server{
|
||||
|
||||
@@ -20,6 +20,51 @@ cd /canvas
|
||||
PORT=3000 HOSTNAME=0.0.0.0 node server.js &
|
||||
CANVAS_PID=$!
|
||||
|
||||
# Memory v2 sidecar (built-in postgres plugin). See Dockerfile entrypoint
|
||||
# comment for rationale.
|
||||
#
|
||||
# Spawn-gating: only start the sidecar when the operator has indicated
|
||||
# they want it (MEMORY_V2_CUTOVER=true OR MEMORY_PLUGIN_URL set).
|
||||
# Without that signal, the sidecar adds zero value and risks aborting
|
||||
# tenant boot via the 30s health gate when the tenant Postgres lacks
|
||||
# pgvector. Caught on staging redeploy 2026-05-05:
|
||||
# pq: extension "vector" is not available
|
||||
#
|
||||
# Defaults (when sidecar IS spawned): MEMORY_PLUGIN_DATABASE_URL
|
||||
# falls back to the tenant's DATABASE_URL.
|
||||
MEMORY_PLUGIN_PID=""
|
||||
memory_plugin_wanted=""
|
||||
if [ "$MEMORY_V2_CUTOVER" = "true" ] || [ -n "$MEMORY_PLUGIN_URL" ]; then
|
||||
memory_plugin_wanted=1
|
||||
fi
|
||||
if [ -z "$MEMORY_PLUGIN_DISABLE" ] && [ -n "$memory_plugin_wanted" ] && [ -n "$DATABASE_URL" ]; then
|
||||
: "${MEMORY_PLUGIN_DATABASE_URL:=$DATABASE_URL}"
|
||||
: "${MEMORY_PLUGIN_LISTEN_ADDR:=:9100}"
|
||||
export MEMORY_PLUGIN_DATABASE_URL MEMORY_PLUGIN_LISTEN_ADDR
|
||||
echo "memory-plugin: starting sidecar on $MEMORY_PLUGIN_LISTEN_ADDR" >&2
|
||||
/memory-plugin &
|
||||
MEMORY_PLUGIN_PID=$!
|
||||
# Wait up to 30s for /v1/health. Boot failure is fatal so a misconfigured
|
||||
# tenant crash-loops instead of silently serving cutover traffic against
|
||||
# a dead plugin.
|
||||
health_port=${MEMORY_PLUGIN_LISTEN_ADDR#:}
|
||||
ready=0
|
||||
for _ in $(seq 1 30); do
|
||||
if wget -qO- --timeout=2 "http://localhost:${health_port}/v1/health" >/dev/null 2>&1; then
|
||||
ready=1
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$ready" != "1" ]; then
|
||||
echo "memory-plugin: ❌ /v1/health never returned 200 after 30s — aborting boot. Check DATABASE_URL reachability + pgvector extension + migrations." >&2
|
||||
kill "$MEMORY_PLUGIN_PID" 2>/dev/null || true
|
||||
kill "$CANVAS_PID" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
echo "memory-plugin: ✅ sidecar healthy on :$health_port" >&2
|
||||
fi
|
||||
|
||||
# Start Go platform in foreground-ish (we trap signals)
|
||||
# CANVAS_PROXY_URL tells the platform to proxy unmatched routes to Canvas.
|
||||
# CONTAINER_BACKEND: empty = Docker (default for self-hosted/local).
|
||||
@@ -29,15 +74,20 @@ cd /
|
||||
/platform &
|
||||
PLATFORM_PID=$!
|
||||
|
||||
# If either process exits, kill the other
|
||||
# If any process exits, kill the others
|
||||
cleanup() {
|
||||
kill $CANVAS_PID 2>/dev/null || true
|
||||
kill $PLATFORM_PID 2>/dev/null || true
|
||||
[ -n "$MEMORY_PLUGIN_PID" ] && kill $MEMORY_PLUGIN_PID 2>/dev/null || true
|
||||
}
|
||||
trap cleanup EXIT SIGTERM SIGINT
|
||||
|
||||
# Wait for either to exit — whichever exits first triggers cleanup
|
||||
wait -n $CANVAS_PID $PLATFORM_PID
|
||||
# Wait for any to exit — whichever exits first triggers cleanup
|
||||
if [ -n "$MEMORY_PLUGIN_PID" ]; then
|
||||
wait -n $CANVAS_PID $PLATFORM_PID $MEMORY_PLUGIN_PID
|
||||
else
|
||||
wait -n $CANVAS_PID $PLATFORM_PID
|
||||
fi
|
||||
EXIT_CODE=$?
|
||||
cleanup
|
||||
exit $EXIT_CODE
|
||||
|
||||
@@ -131,11 +131,19 @@ func buildBundleConfigFiles(b *Bundle) map[string][]byte {
|
||||
}
|
||||
|
||||
func markFailed(ctx context.Context, wsID string, broadcaster *events.Broadcaster, err error) {
|
||||
// Set last_sample_error along with status so operators (and the
|
||||
// Canvas E2E + GET /workspaces/:id callers) get a non-null reason
|
||||
// in the row. Pre-2026-05-05 this UPDATE only set status, leaving
|
||||
// last_sample_error NULL — Canvas E2E #2632 surfaced the gap with
|
||||
// `Workspace failed: (no last_sample_error)`. Same UPDATE shape as
|
||||
// markProvisionFailed in workspace-server/internal/handlers/
|
||||
// workspace_provision_shared.go.
|
||||
msg := err.Error()
|
||||
db.DB.ExecContext(ctx,
|
||||
`UPDATE workspaces SET status = $1, updated_at = now() WHERE id = $2`,
|
||||
models.StatusFailed, wsID)
|
||||
`UPDATE workspaces SET status = $1, last_sample_error = $2, updated_at = now() WHERE id = $3`,
|
||||
models.StatusFailed, msg, wsID)
|
||||
broadcaster.RecordAndBroadcast(ctx, "WORKSPACE_PROVISION_FAILED", wsID, map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"error": msg,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
package db_test
|
||||
|
||||
// Static drift gate: every UPDATE that sets status to a "failed" value
|
||||
// must also set last_sample_error in the same statement. Otherwise the
|
||||
// row ends up with status='failed' + last_sample_error=NULL — operators
|
||||
// see "workspace failed" with no reason, and the Canvas E2E reports the
|
||||
// useless `Workspace failed: (no last_sample_error)` from #2632.
|
||||
//
|
||||
// Why a static gate: pre-2026-05-05 we had at least two writers
|
||||
// (markProvisionFailed in workspace_provision_shared.go set the
|
||||
// message; bundle/importer.go's markFailed didn't). The provision-
|
||||
// timeout sweep also sets the message. Code review missed the
|
||||
// importer drift for ~6 months until the Canvas E2E surfaced it.
|
||||
//
|
||||
// Rule:
|
||||
// - If a Go string literal in this repo contains both
|
||||
// `UPDATE workspaces` and a clause setting `status` to a value
|
||||
// resembling "failed" — either via a `$N` placeholder later bound
|
||||
// to StatusFailed, or via an inline `'failed'` literal — that same
|
||||
// literal MUST also contain `last_sample_error`.
|
||||
// - Allowed: an UPDATE that only sets status to a non-failed value
|
||||
// (online, hibernating, removed, etc.). Those don't need the
|
||||
// message column, and clearing it would lose forensic context.
|
||||
//
|
||||
// Caveats:
|
||||
// - The test reads source as text. Multi-line UPDATEs split across
|
||||
// concatenated string fragments will slip past — that's an
|
||||
// accepted limitation for now; the parameterized-write refactor
|
||||
// (#2799) will let us replace this textual gate with a typed-call
|
||||
// gate eventually.
|
||||
// - "last_sample_error" appearing anywhere in the same literal is
|
||||
// enough to satisfy the rule. We don't try to verify the column
|
||||
// receives a non-empty value at runtime — that's the
|
||||
// parameterized-write refactor's territory too.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestWorkspaceStatusFailed_MustSetLastSampleError uses Go's AST to find
|
||||
// every ExecContext call whose argument list includes the
|
||||
// `models.StatusFailed` constant. For each such call, the SQL literal
|
||||
// (the second argument) must also contain `last_sample_error`. This
|
||||
// catches the bug class without false-positive matches on UPDATEs that
|
||||
// set status to a non-failed value (online/hibernating/removed/etc.)
|
||||
// because those don't pass StatusFailed as an arg.
|
||||
func TestWorkspaceStatusFailed_MustSetLastSampleError(t *testing.T) {
|
||||
root := findRepoRoot(t)
|
||||
violations := []string{}
|
||||
|
||||
walkErr := filepath.Walk(filepath.Join(root, "workspace-server", "internal"), func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if filepath.Ext(path) != ".go" {
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(path, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ast.Inspect(f, func(n ast.Node) bool {
|
||||
call, ok := n.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
sel, ok := call.Fun.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
// Match db.DB.ExecContext / db.DB.QueryContext / db.DB.QueryRowContext
|
||||
// — the three SQL execution surfaces this codebase uses.
|
||||
methodName := sel.Sel.Name
|
||||
if methodName != "ExecContext" && methodName != "QueryContext" && methodName != "QueryRowContext" {
|
||||
return true
|
||||
}
|
||||
// Args: 0=ctx, 1=sql-literal, 2..=bind vars.
|
||||
if len(call.Args) < 3 {
|
||||
return true
|
||||
}
|
||||
passesStatusFailed := false
|
||||
for _, a := range call.Args[2:] {
|
||||
if isStatusFailedRef(a) {
|
||||
passesStatusFailed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !passesStatusFailed {
|
||||
return true
|
||||
}
|
||||
// SQL literal — usually `*ast.BasicLit` for a single-line
|
||||
// string or a back-tick string. May also be a const ref.
|
||||
sqlText := extractStringLit(call.Args[1])
|
||||
if sqlText == "" {
|
||||
// SQL is a name reference, not a literal — can't check.
|
||||
return true
|
||||
}
|
||||
if strings.Contains(sqlText, "last_sample_error") {
|
||||
return true
|
||||
}
|
||||
// Skip non-UPDATE statements that happen to pass StatusFailed
|
||||
// (e.g. SELECT … WHERE status = $1). The drift target is
|
||||
// specifically writes that mark the row failed.
|
||||
if !regexp.MustCompile(`(?i)\bUPDATE\s+workspaces\b`).MatchString(sqlText) {
|
||||
return true
|
||||
}
|
||||
rel, _ := filepath.Rel(root, path)
|
||||
pos := fset.Position(call.Pos())
|
||||
snippet := strings.TrimSpace(sqlText)
|
||||
if len(snippet) > 120 {
|
||||
snippet = snippet[:120] + "..."
|
||||
}
|
||||
violations = append(violations,
|
||||
fmt.Sprintf("%s:%d: %s", rel, pos.Line, snippet))
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
if walkErr != nil {
|
||||
t.Fatalf("walk: %v", walkErr)
|
||||
}
|
||||
|
||||
if len(violations) > 0 {
|
||||
t.Errorf("UPDATE workspaces SET status = ... binds models.StatusFailed but the SQL literal does not write last_sample_error — every code path that marks a workspace failed must also write the reason, or operators see `Workspace failed: (no last_sample_error)` (incident: Canvas E2E #2632). Add `, last_sample_error = $N` to the SET clause.\n\nViolations:\n - %s",
|
||||
strings.Join(violations, "\n - "))
|
||||
}
|
||||
}
|
||||
|
||||
// isStatusFailedRef returns true if expr resolves to models.StatusFailed
|
||||
// (selector StatusFailed off the models package). Catches both
|
||||
// `models.StatusFailed` directly and `models.StatusFailed.String()`
|
||||
// style usages — anything that names the constant.
|
||||
func isStatusFailedRef(expr ast.Expr) bool {
|
||||
if sel, ok := expr.(*ast.SelectorExpr); ok {
|
||||
if sel.Sel.Name == "StatusFailed" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// extractStringLit returns the unquoted contents of a string literal
|
||||
// expression, or "" if expr is not a literal we can read statically
|
||||
// (e.g. concatenation, function-call argument, named const reference).
|
||||
func extractStringLit(expr ast.Expr) string {
|
||||
lit, ok := expr.(*ast.BasicLit)
|
||||
if !ok || lit.Kind != token.STRING {
|
||||
return ""
|
||||
}
|
||||
val := lit.Value
|
||||
if len(val) >= 2 {
|
||||
first, last := val[0], val[len(val)-1]
|
||||
if (first == '`' && last == '`') || (first == '"' && last == '"') {
|
||||
return val[1 : len(val)-1]
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
|
||||
@@ -163,7 +163,7 @@ func (h *WorkspaceHandler) maybeMarkContainerDead(ctx context.Context, workspace
|
||||
if wsRuntime == "external" {
|
||||
return false
|
||||
}
|
||||
if h.provisioner == nil && h.cpProv == nil {
|
||||
if !h.HasProvisioner() {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// admin_delegations.go — RFC #2829 PR-4: operator dashboard endpoint
|
||||
// over the durable delegations ledger (PR-1 schema, PR-3 sweeper).
|
||||
//
|
||||
// What this endpoint serves
|
||||
// -------------------------
|
||||
//
|
||||
// GET /admin/delegations[?status=in_flight|stuck|failed&limit=N]
|
||||
//
|
||||
// Returns the rows the operator needs to triage delegation health:
|
||||
// - in_flight : status IN (queued, dispatched, in_progress) — the
|
||||
// things actively churning right now. Default view.
|
||||
// - stuck : status='stuck' — sweeper found these wedged. Operator
|
||||
// can investigate the callee + decide whether to retry
|
||||
// (RFC #2829 PR-5 plan).
|
||||
// - failed : status='failed' — terminal failures, recent. Useful
|
||||
// for spotting trends like "callee X is failing 50% of
|
||||
// delegations since 14:00".
|
||||
//
|
||||
// Why an admin endpoint at all
|
||||
// ----------------------------
|
||||
// Without this, post-incident investigation requires direct DB access —
|
||||
// only the on-call SRE can answer "is workspace X delegating to a wedged
|
||||
// callee?". The dashboard endpoint moves that visibility into the same
|
||||
// surface as /admin/queue, /admin/schedules-health, /admin/memories etc.
|
||||
//
|
||||
// Out of scope (deferred to a follow-up PR per RFC #2829)
|
||||
// -------------------------------------------------------
|
||||
// - "retry this stuck task" mutation: needs careful interaction with
|
||||
// the agent-side cutover (PR-5) before it can be safely re-fired
|
||||
// - p95 / p99 duration aggregates: separate metric exposure, not a
|
||||
// row-level read
|
||||
// - Canvas UI: this is the JSON contract; the canvas operator panel
|
||||
// consumes it in a follow-up canvas PR
|
||||
|
||||
// AdminDelegationsHandler serves the operator dashboard read endpoint.
|
||||
type AdminDelegationsHandler struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewAdminDelegationsHandler(handle *sql.DB) *AdminDelegationsHandler {
|
||||
if handle == nil {
|
||||
handle = db.DB
|
||||
}
|
||||
return &AdminDelegationsHandler{db: handle}
|
||||
}
|
||||
|
||||
// delegationRow mirrors the row shape of the `delegations` table that the
|
||||
// operator dashboard cares about. Order matches the SELECT below — keep
|
||||
// the two in sync if you add a column.
|
||||
type delegationRow struct {
|
||||
DelegationID string `json:"delegation_id"`
|
||||
CallerID string `json:"caller_id"`
|
||||
CalleeID string `json:"callee_id"`
|
||||
TaskPreview string `json:"task_preview"`
|
||||
Status string `json:"status"`
|
||||
LastHeartbeat *time.Time `json:"last_heartbeat,omitempty"`
|
||||
Deadline time.Time `json:"deadline"`
|
||||
ResultPreview *string `json:"result_preview,omitempty"`
|
||||
ErrorDetail *string `json:"error_detail,omitempty"`
|
||||
RetryCount int `json:"retry_count"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// statusFilters maps the query-string `status` value to the SQL set.
|
||||
// Keep tight — operators don't get to query arbitrary status — so a
|
||||
// new status name added to the schema needs an explicit allowlist
|
||||
// entry here. Caught when a future status name doesn't pin to a UI
|
||||
// expectation (forward-defense).
|
||||
var statusFilters = map[string][]string{
|
||||
"in_flight": {"queued", "dispatched", "in_progress"},
|
||||
"stuck": {"stuck"},
|
||||
"failed": {"failed"},
|
||||
"completed": {"completed"},
|
||||
}
|
||||
|
||||
const defaultListLimit = 100
|
||||
const maxListLimit = 1000
|
||||
|
||||
// List handles GET /admin/delegations
|
||||
//
|
||||
// Query params:
|
||||
// - status — one of `in_flight` (default) / `stuck` / `failed` / `completed`
|
||||
// - limit — int, 1..1000 (default 100)
|
||||
//
|
||||
// Returns 200 with `{"delegations": [...], "count": N}`.
|
||||
func (h *AdminDelegationsHandler) List(c *gin.Context) {
|
||||
statusKey := c.DefaultQuery("status", "in_flight")
|
||||
statuses, ok := statusFilters[statusKey]
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "unknown status filter",
|
||||
"allowed": []string{"in_flight", "stuck", "failed", "completed"},
|
||||
"requested_status": statusKey,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
limit := defaultListLimit
|
||||
if v := c.Query("limit"); v != "" {
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n < 1 || n > maxListLimit {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "limit must be 1..1000",
|
||||
"requested": v,
|
||||
})
|
||||
return
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
|
||||
// Build the IN list as a parameterized expression — never string-
|
||||
// concatenate user-controlled values into the SQL. statusKey came
|
||||
// from the allowlist above so the slice is fully bounded.
|
||||
args := make([]any, 0, len(statuses)+1)
|
||||
placeholders := ""
|
||||
for i, s := range statuses {
|
||||
if i > 0 {
|
||||
placeholders += ","
|
||||
}
|
||||
args = append(args, s)
|
||||
placeholders += "$" + strconv.Itoa(i+1)
|
||||
}
|
||||
args = append(args, limit)
|
||||
limitPlaceholder := "$" + strconv.Itoa(len(statuses)+1)
|
||||
|
||||
rows, err := h.db.QueryContext(c.Request.Context(), `
|
||||
SELECT delegation_id, caller_id::text, callee_id::text, task_preview,
|
||||
status, last_heartbeat, deadline, result_preview, error_detail,
|
||||
retry_count, created_at, updated_at
|
||||
FROM delegations
|
||||
WHERE status IN (`+placeholders+`)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT `+limitPlaceholder, args...)
|
||||
if err != nil {
|
||||
log.Printf("AdminDelegations.List: query failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]delegationRow, 0)
|
||||
for rows.Next() {
|
||||
var r delegationRow
|
||||
var lastBeat sql.NullTime
|
||||
var resultPreview, errorDetail sql.NullString
|
||||
if err := rows.Scan(
|
||||
&r.DelegationID, &r.CallerID, &r.CalleeID, &r.TaskPreview,
|
||||
&r.Status, &lastBeat, &r.Deadline, &resultPreview, &errorDetail,
|
||||
&r.RetryCount, &r.CreatedAt, &r.UpdatedAt,
|
||||
); err != nil {
|
||||
log.Printf("AdminDelegations.List: scan failed: %v", err)
|
||||
continue
|
||||
}
|
||||
if lastBeat.Valid {
|
||||
t := lastBeat.Time
|
||||
r.LastHeartbeat = &t
|
||||
}
|
||||
if resultPreview.Valid {
|
||||
s := resultPreview.String
|
||||
r.ResultPreview = &s
|
||||
}
|
||||
if errorDetail.Valid {
|
||||
s := errorDetail.String
|
||||
r.ErrorDetail = &s
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Printf("AdminDelegations.List: rows.Err: %v", err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"delegations": out,
|
||||
"count": len(out),
|
||||
"status": statusKey,
|
||||
"limit": limit,
|
||||
})
|
||||
}
|
||||
|
||||
// Stats handles GET /admin/delegations/stats — at-a-glance counts per
|
||||
// status. Useful for the dashboard summary card at the top of the
|
||||
// operator panel without paying for a row-level fetch.
|
||||
//
|
||||
// Returns 200 with `{"queued": N, "dispatched": N, "in_progress": N,
|
||||
// "completed": N, "failed": N, "stuck": N}`.
|
||||
func (h *AdminDelegationsHandler) Stats(c *gin.Context) {
|
||||
rows, err := h.db.QueryContext(c.Request.Context(), `
|
||||
SELECT status, COUNT(*) FROM delegations GROUP BY status
|
||||
`)
|
||||
if err != nil {
|
||||
log.Printf("AdminDelegations.Stats: query failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// Initialise to zero so the response always has every known status
|
||||
// key — the dashboard card doesn't need to handle "missing key vs
|
||||
// zero" branching.
|
||||
stats := map[string]int{
|
||||
"queued": 0,
|
||||
"dispatched": 0,
|
||||
"in_progress": 0,
|
||||
"completed": 0,
|
||||
"failed": 0,
|
||||
"stuck": 0,
|
||||
}
|
||||
for rows.Next() {
|
||||
var status string
|
||||
var count int
|
||||
if err := rows.Scan(&status, &count); err != nil {
|
||||
log.Printf("AdminDelegations.Stats: scan failed: %v", err)
|
||||
continue
|
||||
}
|
||||
stats[status] = count
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Printf("AdminDelegations.Stats: rows.Err: %v", err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, stats)
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// admin_delegations_test.go — RFC #2829 PR-4 dashboard endpoint coverage.
|
||||
//
|
||||
// - List: status filter + limit defaults + bad-input rejection
|
||||
// - Stats: per-status counts + zero-fill for missing statuses
|
||||
|
||||
// ---------- List ----------
|
||||
|
||||
func TestAdminDelegations_List_DefaultStatusInFlight(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
now := time.Now()
|
||||
mock.ExpectQuery(`SELECT delegation_id, caller_id::text, callee_id::text, task_preview,\s+status, last_heartbeat, deadline, result_preview, error_detail,\s+retry_count, created_at, updated_at\s+FROM delegations\s+WHERE status IN \(\$1,\$2,\$3\)\s+ORDER BY created_at DESC\s+LIMIT \$4`).
|
||||
WithArgs("queued", "dispatched", "in_progress", 100).
|
||||
WillReturnRows(sqlmock.NewRows([]string{
|
||||
"delegation_id", "caller_id", "callee_id", "task_preview",
|
||||
"status", "last_heartbeat", "deadline", "result_preview", "error_detail",
|
||||
"retry_count", "created_at", "updated_at",
|
||||
}).AddRow(
|
||||
"deleg-1", "caller-uuid", "callee-uuid", "task body",
|
||||
"in_progress", now, now.Add(2*time.Hour), nil, nil,
|
||||
0, now.Add(-5*time.Minute), now.Add(-1*time.Minute),
|
||||
))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("body parse: %v", err)
|
||||
}
|
||||
if got := body["count"]; got != float64(1) {
|
||||
t.Errorf("count: expected 1, got %v", got)
|
||||
}
|
||||
if got := body["status"]; got != "in_flight" {
|
||||
t.Errorf("status: expected in_flight, got %v", got)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_StatusStuck(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
mock.ExpectQuery(`SELECT delegation_id`).
|
||||
WithArgs("stuck", 100).
|
||||
WillReturnRows(sqlmock.NewRows([]string{
|
||||
"delegation_id", "caller_id", "callee_id", "task_preview",
|
||||
"status", "last_heartbeat", "deadline", "result_preview", "error_detail",
|
||||
"retry_count", "created_at", "updated_at",
|
||||
}))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?status=stuck", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d", w.Code)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_StatusFailed(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
mock.ExpectQuery(`SELECT delegation_id`).
|
||||
WithArgs("failed", 100).
|
||||
WillReturnRows(sqlmock.NewRows([]string{
|
||||
"delegation_id", "caller_id", "callee_id", "task_preview",
|
||||
"status", "last_heartbeat", "deadline", "result_preview", "error_detail",
|
||||
"retry_count", "created_at", "updated_at",
|
||||
}))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?status=failed", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d", w.Code)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_RejectsUnknownStatus(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?status=garbage", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_RejectsNegativeLimit(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?limit=-5", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_RejectsLimitOverCap(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?limit=99999", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_AcceptsCustomLimit(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
mock.ExpectQuery(`SELECT delegation_id`).
|
||||
WithArgs("queued", "dispatched", "in_progress", 25).
|
||||
WillReturnRows(sqlmock.NewRows([]string{
|
||||
"delegation_id", "caller_id", "callee_id", "task_preview",
|
||||
"status", "last_heartbeat", "deadline", "result_preview", "error_detail",
|
||||
"retry_count", "created_at", "updated_at",
|
||||
}))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?limit=25", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &body)
|
||||
if body["limit"] != float64(25) {
|
||||
t.Errorf("expected limit=25 echo, got %v", body["limit"])
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_List_PopulatesNullableFields(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
now := time.Now()
|
||||
resultStr := "all done"
|
||||
mock.ExpectQuery(`SELECT delegation_id`).
|
||||
WithArgs("completed", 100).
|
||||
WillReturnRows(sqlmock.NewRows([]string{
|
||||
"delegation_id", "caller_id", "callee_id", "task_preview",
|
||||
"status", "last_heartbeat", "deadline", "result_preview", "error_detail",
|
||||
"retry_count", "created_at", "updated_at",
|
||||
}).AddRow(
|
||||
"deleg-2", "c", "ca", "t",
|
||||
"completed", now, now.Add(2*time.Hour), resultStr, nil,
|
||||
0, now, now,
|
||||
))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations?status=completed", nil)
|
||||
h.List(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
var body struct {
|
||||
Delegations []struct {
|
||||
ResultPreview *string `json:"result_preview"`
|
||||
ErrorDetail *string `json:"error_detail"`
|
||||
LastHeartbeat *string `json:"last_heartbeat"`
|
||||
} `json:"delegations"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if len(body.Delegations) != 1 {
|
||||
t.Fatalf("expected 1 row, got %d", len(body.Delegations))
|
||||
}
|
||||
row := body.Delegations[0]
|
||||
if row.ResultPreview == nil || *row.ResultPreview != "all done" {
|
||||
t.Errorf("result_preview not populated correctly: %+v", row.ResultPreview)
|
||||
}
|
||||
if row.ErrorDetail != nil {
|
||||
t.Errorf("error_detail should be nil for completed-no-error: %+v", row.ErrorDetail)
|
||||
}
|
||||
if row.LastHeartbeat == nil {
|
||||
t.Errorf("last_heartbeat should be present (non-NULL); got nil")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Stats ----------
|
||||
|
||||
func TestAdminDelegations_Stats_ZeroFillsMissingStatuses(t *testing.T) {
|
||||
// Stats response must always include every status key. If no rows
|
||||
// exist for status='stuck', the response still shows "stuck": 0.
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
mock.ExpectQuery(`SELECT status, COUNT\(\*\) FROM delegations GROUP BY status`).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"status", "count"}).
|
||||
AddRow("in_progress", 7).
|
||||
AddRow("completed", 130))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations/stats", nil)
|
||||
h.Stats(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
var stats map[string]int
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &stats); err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
|
||||
expectedKeys := []string{"queued", "dispatched", "in_progress", "completed", "failed", "stuck"}
|
||||
for _, k := range expectedKeys {
|
||||
if _, ok := stats[k]; !ok {
|
||||
t.Errorf("stats missing key %q (zero-fill contract broken)", k)
|
||||
}
|
||||
}
|
||||
if stats["in_progress"] != 7 {
|
||||
t.Errorf("in_progress count: expected 7, got %d", stats["in_progress"])
|
||||
}
|
||||
if stats["completed"] != 130 {
|
||||
t.Errorf("completed count: expected 130, got %d", stats["completed"])
|
||||
}
|
||||
if stats["stuck"] != 0 {
|
||||
t.Errorf("stuck must be zero-filled: got %d", stats["stuck"])
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminDelegations_Stats_EmptyTable(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
h := NewAdminDelegationsHandler(nil)
|
||||
|
||||
mock.ExpectQuery(`SELECT status, COUNT\(\*\) FROM delegations GROUP BY status`).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"status", "count"}))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/delegations/stats", nil)
|
||||
h.Stats(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
var stats map[string]int
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &stats)
|
||||
for k, v := range stats {
|
||||
if v != 0 {
|
||||
t.Errorf("empty table → all counts zero; %s=%d", k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// statusFilters is a contract surface — every key here is documented in
|
||||
// the endpoint comment + accepted by the validator. Pin it.
|
||||
func TestStatusFiltersTableShape(t *testing.T) {
|
||||
expected := map[string][]string{
|
||||
"in_flight": {"queued", "dispatched", "in_progress"},
|
||||
"stuck": {"stuck"},
|
||||
"failed": {"failed"},
|
||||
"completed": {"completed"},
|
||||
}
|
||||
for k, want := range expected {
|
||||
got, ok := statusFilters[k]
|
||||
if !ok {
|
||||
t.Errorf("statusFilters missing key %q", k)
|
||||
continue
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("statusFilters[%q]: want %v, got %v", k, want, got)
|
||||
continue
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("statusFilters[%q][%d]: want %q, got %q", k, i, want[i], got[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,83 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
mclient "github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/client"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/namespace"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// envMemoryV2Cutover gates whether admin export/import routes through
|
||||
// the v2 plugin (PR-8 / RFC #2728). When unset, the legacy direct-DB
|
||||
// path runs unchanged so operators who haven't enabled the plugin
|
||||
// keep working.
|
||||
const envMemoryV2Cutover = "MEMORY_V2_CUTOVER"
|
||||
|
||||
// AdminMemoriesHandler provides bulk export/import of agent memories for
|
||||
// backup and restore across Docker rebuilds (issue #1051).
|
||||
type AdminMemoriesHandler struct{}
|
||||
//
|
||||
// PR-8 (RFC #2728): when wired with the v2 plugin via WithMemoryV2 AND
|
||||
// MEMORY_V2_CUTOVER is true, export reads from the plugin's namespaces
|
||||
// and import writes through the plugin. Both paths preserve the
|
||||
// SAFE-T1201 redaction shipped in F1084 + F1085.
|
||||
type AdminMemoriesHandler struct {
|
||||
plugin adminMemoriesPlugin
|
||||
resolver adminMemoriesResolver
|
||||
}
|
||||
|
||||
// adminMemoriesPlugin is the slice of the memory plugin client we
|
||||
// call from this handler.
|
||||
type adminMemoriesPlugin interface {
|
||||
CommitMemory(ctx context.Context, namespace string, body contract.MemoryWrite) (*contract.MemoryWriteResponse, error)
|
||||
Search(ctx context.Context, body contract.SearchRequest) (*contract.SearchResponse, error)
|
||||
UpsertNamespace(ctx context.Context, name string, body contract.NamespaceUpsert) (*contract.Namespace, error)
|
||||
}
|
||||
|
||||
// adminMemoriesResolver mirrors the namespace resolver methods this
|
||||
// handler calls.
|
||||
type adminMemoriesResolver interface {
|
||||
WritableNamespaces(ctx context.Context, workspaceID string) ([]namespace.Namespace, error)
|
||||
ReadableNamespaces(ctx context.Context, workspaceID string) ([]namespace.Namespace, error)
|
||||
}
|
||||
|
||||
// NewAdminMemoriesHandler constructs the handler.
|
||||
func NewAdminMemoriesHandler() *AdminMemoriesHandler {
|
||||
return &AdminMemoriesHandler{}
|
||||
}
|
||||
|
||||
// WithMemoryV2 attaches the v2 plugin + resolver. Production wiring
|
||||
// path; main.go calls this after Boot()-ing the plugin client.
|
||||
func (h *AdminMemoriesHandler) WithMemoryV2(plugin *mclient.Client, resolver *namespace.Resolver) *AdminMemoriesHandler {
|
||||
h.plugin = plugin
|
||||
h.resolver = resolver
|
||||
return h
|
||||
}
|
||||
|
||||
// withMemoryV2APIs is the test-only wiring that takes interfaces.
|
||||
func (h *AdminMemoriesHandler) withMemoryV2APIs(plugin adminMemoriesPlugin, resolver adminMemoriesResolver) *AdminMemoriesHandler {
|
||||
h.plugin = plugin
|
||||
h.resolver = resolver
|
||||
return h
|
||||
}
|
||||
|
||||
// cutoverActive reports whether the export/import path should route
|
||||
// through the v2 plugin.
|
||||
func (h *AdminMemoriesHandler) cutoverActive() bool {
|
||||
if os.Getenv(envMemoryV2Cutover) != "true" {
|
||||
return false
|
||||
}
|
||||
return h.plugin != nil && h.resolver != nil
|
||||
}
|
||||
|
||||
// memoryExportEntry is the JSON shape for a single exported memory.
|
||||
type memoryExportEntry struct {
|
||||
ID string `json:"id"`
|
||||
@@ -36,9 +96,17 @@ type memoryExportEntry struct {
|
||||
// SECURITY (F1084 / #1131): applies redactSecrets to each content field
|
||||
// before returning so that any credentials stored before SAFE-T1201 (#838)
|
||||
// was applied do not leak out via the admin export endpoint.
|
||||
//
|
||||
// CUTOVER (PR-8 / RFC #2728): when MEMORY_V2_CUTOVER=true and the v2
|
||||
// plugin is wired, reads from the plugin instead of agent_memories.
|
||||
func (h *AdminMemoriesHandler) Export(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
if h.cutoverActive() {
|
||||
h.exportViaPlugin(c, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.DB.QueryContext(ctx, `
|
||||
SELECT am.id, am.content, am.scope, am.namespace, am.created_at,
|
||||
w.name AS workspace_name
|
||||
@@ -91,6 +159,9 @@ type memoryImportEntry struct {
|
||||
// before both the deduplication check and the INSERT so that imported memories
|
||||
// with embedded credentials cannot land unredacted in agent_memories (SAFE-T1201
|
||||
// parity with the commit_memory MCP bridge path).
|
||||
//
|
||||
// CUTOVER (PR-8 / RFC #2728): when MEMORY_V2_CUTOVER=true and the v2
|
||||
// plugin is wired, writes through the plugin instead of agent_memories.
|
||||
func (h *AdminMemoriesHandler) Import(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
@@ -100,6 +171,11 @@ func (h *AdminMemoriesHandler) Import(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if h.cutoverActive() {
|
||||
h.importViaPlugin(c, ctx, entries)
|
||||
return
|
||||
}
|
||||
|
||||
imported := 0
|
||||
skipped := 0
|
||||
errors := 0
|
||||
@@ -175,3 +251,310 @@ func (h *AdminMemoriesHandler) Import(c *gin.Context) {
|
||||
"total": len(entries),
|
||||
})
|
||||
}
|
||||
|
||||
// exportViaPlugin reads memories from the v2 plugin and emits them in
|
||||
// the legacy memoryExportEntry shape so existing tooling that consumes
|
||||
// the export keeps working.
|
||||
//
|
||||
// Optimization (#289 fix): the previous implementation was O(workspaces)
|
||||
// in BOTH resolver CTE walks AND plugin search calls. For a 1000-tenant
|
||||
// org, that's 1000 × resolver + 1000 × HTTP, where most are redundant
|
||||
// because workspaces sharing a team/org root see identical namespaces.
|
||||
//
|
||||
// New strategy:
|
||||
// 1. Single SQL pass walks parent_id chains, returning each
|
||||
// workspace's root_id alongside its name.
|
||||
// 2. Group workspaces by root → unique tree count is typically <<
|
||||
// workspace count.
|
||||
// 3. Resolve namespaces ONCE per root (any workspace under that
|
||||
// root produces the same readable list).
|
||||
// 4. Build a UNION of namespaces across all roots; single plugin
|
||||
// search call.
|
||||
// 5. Map each memory back to a workspace_name via a namespace→ws
|
||||
// lookup table built up from step 3.
|
||||
//
|
||||
// Net cost: 1 SQL + N_roots resolver calls + 1 plugin call (vs
|
||||
// N_workspaces resolver + N_workspaces plugin in the old code).
|
||||
func (h *AdminMemoriesHandler) exportViaPlugin(c *gin.Context, ctx context.Context) {
|
||||
// 1. One SQL pass: every workspace + its root id.
|
||||
wsRows, err := loadWorkspacesWithRoots(ctx, db.DB)
|
||||
if err != nil {
|
||||
log.Printf("admin/memories/export (cutover): workspaces query: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "export query failed"})
|
||||
return
|
||||
}
|
||||
|
||||
// 2. Group by root → list of workspaces.
|
||||
rootToWorkspaces := make(map[string][]workspaceRow, len(wsRows))
|
||||
for _, w := range wsRows {
|
||||
rootToWorkspaces[w.RootID] = append(rootToWorkspaces[w.RootID], w)
|
||||
}
|
||||
|
||||
// 3. Resolve team/org namespaces once per root, then add each
|
||||
// member's private workspace:<id> namespace explicitly.
|
||||
//
|
||||
// IMPORTANT: ReadableNamespaces(rootID) returns
|
||||
// {workspace:rootID, team:rootID, org:rootID}. Calling it once
|
||||
// per root is enough for team:/org:/custom: (those are shared by
|
||||
// every member of the root group), but the workspace: namespace
|
||||
// it returns is rootID's only — child members' private
|
||||
// workspace:<childID> namespaces would be silently dropped from
|
||||
// the export. Inject each member's workspace:<id> below to keep
|
||||
// coverage parity with the legacy per-workspace iteration.
|
||||
nsToOwner := make(map[string]string) // namespace → workspace_name (first matching wins)
|
||||
allNamespaces := make(map[string]struct{}) // union for plugin search
|
||||
for rootID, members := range rootToWorkspaces {
|
||||
readable, err := h.resolver.ReadableNamespaces(ctx, rootID)
|
||||
if err != nil {
|
||||
log.Printf("admin/memories/export (cutover) root=%s: resolve: %v", rootID, err)
|
||||
continue
|
||||
}
|
||||
// Collect non-workspace namespaces (team:/org:/custom:/...) from
|
||||
// the root view; these are identical across every member.
|
||||
for _, ns := range readable {
|
||||
if strings.HasPrefix(ns.Name, "workspace:") {
|
||||
continue
|
||||
}
|
||||
allNamespaces[ns.Name] = struct{}{}
|
||||
if _, alreadyMapped := nsToOwner[ns.Name]; alreadyMapped {
|
||||
continue
|
||||
}
|
||||
if owner := pickOwnerForNamespace(ns.Name, members); owner != "" {
|
||||
nsToOwner[ns.Name] = owner
|
||||
}
|
||||
}
|
||||
// Inject each member's private workspace:<id> namespace + its
|
||||
// owner. Children's private memories live in workspace:<childID>
|
||||
// which the root-only resolve doesn't surface.
|
||||
for _, m := range members {
|
||||
ns := "workspace:" + m.ID
|
||||
allNamespaces[ns] = struct{}{}
|
||||
nsToOwner[ns] = m.Name
|
||||
}
|
||||
}
|
||||
|
||||
if len(allNamespaces) == 0 {
|
||||
c.JSON(http.StatusOK, []memoryExportEntry{})
|
||||
return
|
||||
}
|
||||
|
||||
// 4. Single plugin search across the union.
|
||||
nsList := make([]string, 0, len(allNamespaces))
|
||||
for ns := range allNamespaces {
|
||||
nsList = append(nsList, ns)
|
||||
}
|
||||
resp, err := h.plugin.Search(ctx, contract.SearchRequest{Namespaces: nsList, Limit: 100})
|
||||
if err != nil {
|
||||
log.Printf("admin/memories/export (cutover): plugin search: %v", err)
|
||||
c.JSON(http.StatusOK, []memoryExportEntry{})
|
||||
return
|
||||
}
|
||||
|
||||
// 5. Map each memory to a workspace_name, redact, emit.
|
||||
seen := make(map[string]struct{})
|
||||
memories := make([]memoryExportEntry, 0, len(resp.Memories))
|
||||
for _, m := range resp.Memories {
|
||||
if _, dup := seen[m.ID]; dup {
|
||||
continue
|
||||
}
|
||||
seen[m.ID] = struct{}{}
|
||||
owner := nsToOwner[m.Namespace]
|
||||
redacted, _ := redactSecrets(owner, m.Content)
|
||||
memories = append(memories, memoryExportEntry{
|
||||
ID: m.ID,
|
||||
Content: redacted,
|
||||
Scope: legacyScopeFromNamespace(m.Namespace),
|
||||
Namespace: m.Namespace,
|
||||
CreatedAt: m.CreatedAt,
|
||||
WorkspaceName: owner,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, memories)
|
||||
}
|
||||
|
||||
// workspaceRow bundles the per-workspace fields the optimized export
|
||||
// needs (id + name + root for grouping).
|
||||
type workspaceRow struct {
|
||||
ID string
|
||||
Name string
|
||||
RootID string
|
||||
}
|
||||
|
||||
// loadWorkspacesWithRoots returns one row per workspace with its root
|
||||
// id computed via a recursive CTE. Single SQL pass — replaces the
|
||||
// previous N×ReadableNamespaces pattern that walked each tree
|
||||
// independently.
|
||||
func loadWorkspacesWithRoots(ctx context.Context, conn *sql.DB) ([]workspaceRow, error) {
|
||||
rows, err := conn.QueryContext(ctx, `
|
||||
WITH RECURSIVE chain AS (
|
||||
SELECT id, parent_id, name, id AS root_id, 0 AS depth
|
||||
FROM workspaces
|
||||
WHERE parent_id IS NULL
|
||||
UNION ALL
|
||||
SELECT w.id, w.parent_id, w.name, c.root_id, c.depth + 1
|
||||
FROM workspaces w
|
||||
JOIN chain c ON w.parent_id = c.id
|
||||
WHERE c.depth < 50
|
||||
)
|
||||
SELECT id::text, name, root_id::text FROM chain ORDER BY name
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]workspaceRow, 0)
|
||||
for rows.Next() {
|
||||
var w workspaceRow
|
||||
if err := rows.Scan(&w.ID, &w.Name, &w.RootID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, w)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// pickOwnerForNamespace returns the workspace_name to attribute a
|
||||
// namespace to in the export. workspace:<id> namespaces map to the
|
||||
// matching member; team:* / org:* / custom:* fall back to the first
|
||||
// member of the root group (canonical owner).
|
||||
func pickOwnerForNamespace(ns string, members []workspaceRow) string {
|
||||
if strings.HasPrefix(ns, "workspace:") {
|
||||
wantID := strings.TrimPrefix(ns, "workspace:")
|
||||
for _, m := range members {
|
||||
if m.ID == wantID {
|
||||
return m.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
// Non-workspace namespaces: attribute to first member of the root
|
||||
// group. Stable because loadWorkspacesWithRoots returns ORDER BY
|
||||
// name, so the same root group always picks the same owner.
|
||||
if len(members) > 0 {
|
||||
return members[0].Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// importViaPlugin writes the entries through the plugin instead of
|
||||
// directly to agent_memories. Workspaces are resolved by name like
|
||||
// the legacy path. Scope→namespace mapping mirrors the PR-6 shim.
|
||||
func (h *AdminMemoriesHandler) importViaPlugin(c *gin.Context, ctx context.Context, entries []memoryImportEntry) {
|
||||
imported := 0
|
||||
skipped := 0
|
||||
errs := 0
|
||||
|
||||
for _, entry := range entries {
|
||||
var workspaceID string
|
||||
if err := db.DB.QueryRowContext(ctx,
|
||||
`SELECT id::text FROM workspaces WHERE name = $1 LIMIT 1`,
|
||||
entry.WorkspaceName,
|
||||
).Scan(&workspaceID); err != nil {
|
||||
log.Printf("admin/memories/import (cutover): workspace %q not found, skipping", entry.WorkspaceName)
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Redact BEFORE the plugin sees it (SAFE-T1201 parity).
|
||||
content, _ := redactSecrets(workspaceID, entry.Content)
|
||||
|
||||
ns, err := h.scopeToWritableNamespaceForImport(ctx, workspaceID, entry.Scope)
|
||||
if err != nil {
|
||||
log.Printf("admin/memories/import (cutover): %v", err)
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Idempotent namespace upsert before commit.
|
||||
if _, err := h.plugin.UpsertNamespace(ctx, ns, contract.NamespaceUpsert{
|
||||
Kind: namespaceKindFromLegacyScope(entry.Scope),
|
||||
}); err != nil {
|
||||
log.Printf("admin/memories/import (cutover): upsert ns %s: %v", ns, err)
|
||||
errs++
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := h.plugin.CommitMemory(ctx, ns, contract.MemoryWrite{
|
||||
Content: content,
|
||||
Kind: contract.MemoryKindFact,
|
||||
Source: contract.MemorySourceAgent,
|
||||
}); err != nil {
|
||||
log.Printf("admin/memories/import (cutover): commit %s: %v", ns, err)
|
||||
errs++
|
||||
continue
|
||||
}
|
||||
imported++
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"imported": imported,
|
||||
"skipped": skipped,
|
||||
"errors": errs,
|
||||
"total": len(entries),
|
||||
})
|
||||
}
|
||||
|
||||
// scopeToWritableNamespaceForImport mirrors the PR-6 shim translation.
|
||||
// Returns the namespace string the resolver picks for the requested
|
||||
// scope; errors out cleanly on GLOBAL or unmapped values so importing
|
||||
// a malformed entry doesn't crash the run.
|
||||
func (h *AdminMemoriesHandler) scopeToWritableNamespaceForImport(ctx context.Context, workspaceID, scope string) (string, error) {
|
||||
writable, err := h.resolver.WritableNamespaces(ctx, workspaceID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
wantKind := contract.NamespaceKindWorkspace
|
||||
switch strings.ToUpper(scope) {
|
||||
case "", "LOCAL":
|
||||
wantKind = contract.NamespaceKindWorkspace
|
||||
case "TEAM":
|
||||
wantKind = contract.NamespaceKindTeam
|
||||
case "GLOBAL":
|
||||
wantKind = contract.NamespaceKindOrg
|
||||
default:
|
||||
return "", &skipImport{reason: "unknown scope: " + scope}
|
||||
}
|
||||
for _, ns := range writable {
|
||||
if ns.Kind == wantKind {
|
||||
return ns.Name, nil
|
||||
}
|
||||
}
|
||||
return "", &skipImport{reason: "no writable namespace of kind " + string(wantKind)}
|
||||
}
|
||||
|
||||
// skipImport is a typed error so the caller can distinguish "skip
|
||||
// this entry" from a hard failure.
|
||||
type skipImport struct{ reason string }
|
||||
|
||||
func (e *skipImport) Error() string { return "skip: " + e.reason }
|
||||
|
||||
// legacyScopeFromNamespace reverses the namespace→scope mapping for
|
||||
// the export shape. Mirrors namespaceKindToLegacyScope from the PR-6
|
||||
// shim but is lifted out so admin_memories doesn't depend on the MCP
|
||||
// handler's helpers.
|
||||
func legacyScopeFromNamespace(ns string) string {
|
||||
switch {
|
||||
case strings.HasPrefix(ns, "workspace:"):
|
||||
return "LOCAL"
|
||||
case strings.HasPrefix(ns, "team:"):
|
||||
return "TEAM"
|
||||
case strings.HasPrefix(ns, "org:"):
|
||||
return "GLOBAL"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// namespaceKindFromLegacyScope returns the contract.NamespaceKind for
|
||||
// a legacy scope value. Unknown defaults to workspace so importing
|
||||
// an unexpected row still produces a typed namespace.
|
||||
func namespaceKindFromLegacyScope(scope string) contract.NamespaceKind {
|
||||
switch strings.ToUpper(scope) {
|
||||
case "TEAM":
|
||||
return contract.NamespaceKindTeam
|
||||
case "GLOBAL":
|
||||
return contract.NamespaceKindOrg
|
||||
default:
|
||||
return contract.NamespaceKindWorkspace
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,800 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
platformdb "github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/contract"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/memory/namespace"
|
||||
)
|
||||
|
||||
// --- stubs ---
|
||||
|
||||
type stubAdminPlugin struct {
|
||||
upserts []string
|
||||
commits []commitRecord
|
||||
searches []contract.SearchRequest
|
||||
commitFn func(ctx context.Context, ns string, body contract.MemoryWrite) (*contract.MemoryWriteResponse, error)
|
||||
searchFn func(ctx context.Context, body contract.SearchRequest) (*contract.SearchResponse, error)
|
||||
upsertFn func(ctx context.Context, name string, body contract.NamespaceUpsert) (*contract.Namespace, error)
|
||||
}
|
||||
|
||||
type commitRecord struct {
|
||||
NS string
|
||||
Content string
|
||||
}
|
||||
|
||||
func (s *stubAdminPlugin) UpsertNamespace(ctx context.Context, name string, body contract.NamespaceUpsert) (*contract.Namespace, error) {
|
||||
s.upserts = append(s.upserts, name)
|
||||
if s.upsertFn != nil {
|
||||
return s.upsertFn(ctx, name, body)
|
||||
}
|
||||
return &contract.Namespace{Name: name, Kind: body.Kind, CreatedAt: time.Now().UTC()}, nil
|
||||
}
|
||||
func (s *stubAdminPlugin) CommitMemory(ctx context.Context, ns string, body contract.MemoryWrite) (*contract.MemoryWriteResponse, error) {
|
||||
s.commits = append(s.commits, commitRecord{NS: ns, Content: body.Content})
|
||||
if s.commitFn != nil {
|
||||
return s.commitFn(ctx, ns, body)
|
||||
}
|
||||
return &contract.MemoryWriteResponse{ID: "out-1", Namespace: ns}, nil
|
||||
}
|
||||
func (s *stubAdminPlugin) Search(ctx context.Context, body contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
s.searches = append(s.searches, body)
|
||||
if s.searchFn != nil {
|
||||
return s.searchFn(ctx, body)
|
||||
}
|
||||
return &contract.SearchResponse{}, nil
|
||||
}
|
||||
|
||||
type stubAdminResolver struct {
|
||||
readable []namespace.Namespace
|
||||
writable []namespace.Namespace
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubAdminResolver) ReadableNamespaces(_ context.Context, _ string) ([]namespace.Namespace, error) {
|
||||
return s.readable, s.err
|
||||
}
|
||||
func (s *stubAdminResolver) WritableNamespaces(_ context.Context, _ string) ([]namespace.Namespace, error) {
|
||||
return s.writable, s.err
|
||||
}
|
||||
|
||||
func adminRootResolver() *stubAdminResolver {
|
||||
return &stubAdminResolver{
|
||||
readable: []namespace.Namespace{
|
||||
{Name: "workspace:root-1", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
{Name: "team:root-1", Kind: contract.NamespaceKindTeam, Writable: true},
|
||||
{Name: "org:root-1", Kind: contract.NamespaceKindOrg, Writable: true},
|
||||
},
|
||||
writable: []namespace.Namespace{
|
||||
{Name: "workspace:root-1", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
{Name: "team:root-1", Kind: contract.NamespaceKindTeam, Writable: true},
|
||||
{Name: "org:root-1", Kind: contract.NamespaceKindOrg, Writable: true},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// installMockDB swaps platformdb.DB with a sqlmock for a test.
|
||||
func installMockDB(t *testing.T) sqlmock.Sqlmock {
|
||||
t.Helper()
|
||||
mockDB, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("sqlmock new: %v", err)
|
||||
}
|
||||
prev := platformdb.DB
|
||||
platformdb.DB = mockDB
|
||||
t.Cleanup(func() {
|
||||
_ = mockDB.Close()
|
||||
platformdb.DB = prev
|
||||
})
|
||||
return mock
|
||||
}
|
||||
|
||||
// --- cutoverActive ---
|
||||
|
||||
func TestCutoverActive(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
envVal string
|
||||
plugin adminMemoriesPlugin
|
||||
resolver adminMemoriesResolver
|
||||
want bool
|
||||
}{
|
||||
{"env unset", "", &stubAdminPlugin{}, adminRootResolver(), false},
|
||||
{"env true but unwired", "true", nil, nil, false},
|
||||
{"env false", "false", &stubAdminPlugin{}, adminRootResolver(), false},
|
||||
{"env true wired", "true", &stubAdminPlugin{}, adminRootResolver(), true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, tc.envVal)
|
||||
h := &AdminMemoriesHandler{plugin: tc.plugin, resolver: tc.resolver}
|
||||
if got := h.cutoverActive(); got != tc.want {
|
||||
t.Errorf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// --- WithMemoryV2 wiring ---
|
||||
|
||||
func TestWithMemoryV2_AttachesDeps(t *testing.T) {
|
||||
h := NewAdminMemoriesHandler().WithMemoryV2(nil, nil)
|
||||
// Both nil pointers — wiring still attaches them; cutoverActive
|
||||
// reports false because the interface values are nil.
|
||||
if h.plugin == nil && h.resolver == nil {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithMemoryV2APIs_AttachesDeps(t *testing.T) {
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(&stubAdminPlugin{}, adminRootResolver())
|
||||
if h.plugin == nil || h.resolver == nil {
|
||||
t.Error("withMemoryV2APIs must attach both interfaces")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Export via plugin ---
|
||||
|
||||
func TestExport_RoutesThroughPluginWhenCutoverActive(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("ws-1", "alpha", "ws-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{
|
||||
searchFn: func(_ context.Context, body contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "mem-1", Namespace: "workspace:root-1", Content: "fact x", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
{ID: "mem-2", Namespace: "team:root-1", Content: "team y", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("code = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var entries []memoryExportEntry
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &entries); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Errorf("entries = %d", len(entries))
|
||||
}
|
||||
// Legacy scope label must be in the export
|
||||
scopes := map[string]bool{}
|
||||
for _, e := range entries {
|
||||
scopes[e.Scope] = true
|
||||
}
|
||||
if !scopes["LOCAL"] || !scopes["TEAM"] {
|
||||
t.Errorf("expected LOCAL+TEAM scopes, got %v", scopes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExport_DeduplicatesByMemoryID(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
|
||||
// Two workspaces, both will see the same team-shared memory.
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("ws-1", "alpha", "ws-1").
|
||||
AddRow("ws-2", "beta", "ws-2"))
|
||||
|
||||
plugin := &stubAdminPlugin{
|
||||
searchFn: func(_ context.Context, body contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "mem-shared", Namespace: "team:root-1", Content: "team-fact", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
var entries []memoryExportEntry
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &entries)
|
||||
if len(entries) != 1 {
|
||||
t.Errorf("dedup failed; got %d entries, want 1", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestExport_SkipsWorkspaceWhenResolverFails(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("ws-1", "alpha", "ws-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
resolver := &stubAdminResolver{err: errors.New("resolver dead")}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, resolver)
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
// Should still 200 with empty memories — failure is per-workspace.
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("code = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExport_SkipsWorkspaceWhenPluginSearchFails(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("ws-1", "alpha", "ws-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{
|
||||
searchFn: func(_ context.Context, _ contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return nil, errors.New("plugin dead")
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("code = %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExport_WorkspacesQueryFails(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnError(errors.New("db dead"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("code = %d, want 500", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExport_EmptyReadable(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("ws-1", "alpha", "ws-1"))
|
||||
|
||||
resolver := &stubAdminResolver{readable: []namespace.Namespace{}}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(&stubAdminPlugin{}, resolver)
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("code = %d", w.Code)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "[]") {
|
||||
t.Errorf("expected empty array, got %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExport_RedactsSecretsInPluginPath(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("ws-1", "alpha", "ws-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{
|
||||
searchFn: func(_ context.Context, _ contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "mem-1", Namespace: "workspace:root-1", Content: "API_KEY=sk-1234567890abcdefghijk0123456789", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if strings.Contains(w.Body.String(), "sk-1234567890abcdef") {
|
||||
t.Errorf("export leaked unredacted secret: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// --- Import via plugin ---
|
||||
|
||||
func TestImport_RoutesThroughPluginWhenCutoverActive(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WithArgs("alpha").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("root-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "fact x", Scope: "LOCAL", WorkspaceName: "alpha"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("code = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(plugin.commits) != 1 {
|
||||
t.Errorf("commits = %d, want 1", len(plugin.commits))
|
||||
}
|
||||
if plugin.commits[0].NS != "workspace:root-1" {
|
||||
t.Errorf("ns = %q", plugin.commits[0].NS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImport_SkipsUnknownWorkspace(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WithArgs("ghost").
|
||||
WillReturnError(errors.New("no rows"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "x", Scope: "LOCAL", WorkspaceName: "ghost"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
var resp map[string]int
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["skipped"] != 1 || resp["imported"] != 0 {
|
||||
t.Errorf("resp = %v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImport_PluginUpsertNamespaceError(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("root-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{
|
||||
upsertFn: func(_ context.Context, _ string, _ contract.NamespaceUpsert) (*contract.Namespace, error) {
|
||||
return nil, errors.New("upsert dead")
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "x", Scope: "LOCAL", WorkspaceName: "alpha"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
var resp map[string]int
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["errors"] != 1 || resp["imported"] != 0 {
|
||||
t.Errorf("resp = %v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImport_PluginCommitError(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("root-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{
|
||||
commitFn: func(_ context.Context, _ string, _ contract.MemoryWrite) (*contract.MemoryWriteResponse, error) {
|
||||
return nil, errors.New("commit dead")
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "x", Scope: "LOCAL", WorkspaceName: "alpha"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
var resp map[string]int
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["errors"] != 1 {
|
||||
t.Errorf("resp = %v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImport_RedactsBeforePluginSeesContent(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("root-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "API_KEY=sk-1234567890abcdefghijk0123456789", Scope: "LOCAL", WorkspaceName: "alpha"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
if len(plugin.commits) != 1 {
|
||||
t.Fatalf("commits = %d", len(plugin.commits))
|
||||
}
|
||||
if strings.Contains(plugin.commits[0].Content, "sk-1234567890") {
|
||||
t.Errorf("plugin received unredacted content: %q", plugin.commits[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImport_SkipsUnknownScope(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("root-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "x", Scope: "WEIRD", WorkspaceName: "alpha"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
var resp map[string]int
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["skipped"] != 1 {
|
||||
t.Errorf("resp = %v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImport_SkipsWhenResolverErrors(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT id::text FROM workspaces").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("root-1"))
|
||||
|
||||
plugin := &stubAdminPlugin{}
|
||||
resolver := &stubAdminResolver{err: errors.New("dead")}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, resolver)
|
||||
|
||||
body, _ := json.Marshal([]memoryImportEntry{
|
||||
{Content: "x", Scope: "LOCAL", WorkspaceName: "alpha"},
|
||||
})
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/admin/memories/import", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
h.Import(c)
|
||||
|
||||
var resp map[string]int
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["skipped"] != 1 {
|
||||
t.Errorf("resp = %v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExport_BatchesPluginCallsByRoot pins the I3 fix: previously the
|
||||
// export ran one resolver + one plugin search per workspace (N+1 in
|
||||
// both); now it groups by root and runs one resolver + one plugin
|
||||
// search per UNIQUE root.
|
||||
//
|
||||
// Setup: 3 workspaces under 1 root → 1 resolver call + 1 plugin call
|
||||
// (was: 3 resolver + 3 plugin in the old code). The plugin search
|
||||
// receives 5 namespaces: each member's workspace:<id> + team:root-1
|
||||
// + org:root-1. (Children's workspace:<id> namespaces must be
|
||||
// included or admin export silently drops their private memories.)
|
||||
func TestExport_BatchesPluginCallsByRoot(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("root-1", "alpha", "root-1").
|
||||
AddRow("child-1", "alpha-child", "root-1").
|
||||
AddRow("child-2", "alpha-grandchild", "root-1"))
|
||||
|
||||
pluginSearchCount := 0
|
||||
plugin := &stubAdminPlugin{
|
||||
searchFn: func(_ context.Context, body contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
pluginSearchCount++
|
||||
if len(body.Namespaces) != 5 {
|
||||
t.Errorf("plugin search call %d: namespaces len = %d, want 5 (3 workspace + team + org); got %v", pluginSearchCount, len(body.Namespaces), body.Namespaces)
|
||||
}
|
||||
return &contract.SearchResponse{}, nil
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, adminRootResolver())
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("code = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if pluginSearchCount != 1 {
|
||||
t.Errorf("plugin search called %d times, want 1 (was 3 with the old N+1 code)", pluginSearchCount)
|
||||
}
|
||||
}
|
||||
|
||||
// perWorkspaceResolver mimics the real resolver: ReadableNamespaces
|
||||
// returns the SPECIFIC workspace's view (workspace:<that ID> +
|
||||
// team:<root> + org:<root>), not a constant set. The legacy
|
||||
// stubAdminResolver hides the I3 silent-drop bug by ignoring its
|
||||
// workspace-id argument.
|
||||
type perWorkspaceResolver map[string][]namespace.Namespace
|
||||
|
||||
func (r perWorkspaceResolver) ReadableNamespaces(_ context.Context, ws string) ([]namespace.Namespace, error) {
|
||||
v, ok := r[ws]
|
||||
if !ok {
|
||||
return nil, errors.New("perWorkspaceResolver: unknown ws " + ws)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
func (r perWorkspaceResolver) WritableNamespaces(_ context.Context, ws string) ([]namespace.Namespace, error) {
|
||||
return r.ReadableNamespaces(nil, ws)
|
||||
}
|
||||
|
||||
// TestExport_IncludesEveryMembersPrivateNamespace pins the I3 follow-up
|
||||
// fix: when a root group has multiple members, the export must surface
|
||||
// each member's workspace:<id> namespace, not just the root's. Before
|
||||
// the fix, calling ReadableNamespaces(rootID) returned only
|
||||
// workspace:rootID + team:rootID + org:rootID — every child workspace's
|
||||
// private memories were silently dropped from admin export.
|
||||
func TestExport_IncludesEveryMembersPrivateNamespace(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "true")
|
||||
mock := installMockDB(t)
|
||||
|
||||
mock.ExpectQuery("WITH RECURSIVE chain").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "root_id"}).
|
||||
AddRow("root-1", "alpha", "root-1").
|
||||
AddRow("child-1", "alpha-child", "root-1").
|
||||
AddRow("child-2", "alpha-grandchild", "root-1"))
|
||||
|
||||
resolver := perWorkspaceResolver{
|
||||
"root-1": {
|
||||
{Name: "workspace:root-1", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
{Name: "team:root-1", Kind: contract.NamespaceKindTeam, Writable: true},
|
||||
{Name: "org:root-1", Kind: contract.NamespaceKindOrg, Writable: true},
|
||||
},
|
||||
"child-1": {
|
||||
{Name: "workspace:child-1", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
{Name: "team:root-1", Kind: contract.NamespaceKindTeam, Writable: true},
|
||||
{Name: "org:root-1", Kind: contract.NamespaceKindOrg, Writable: true},
|
||||
},
|
||||
"child-2": {
|
||||
{Name: "workspace:child-2", Kind: contract.NamespaceKindWorkspace, Writable: true},
|
||||
{Name: "team:root-1", Kind: contract.NamespaceKindTeam, Writable: true},
|
||||
{Name: "org:root-1", Kind: contract.NamespaceKindOrg, Writable: true},
|
||||
},
|
||||
}
|
||||
|
||||
var passedNamespaces []string
|
||||
plugin := &stubAdminPlugin{
|
||||
searchFn: func(_ context.Context, body contract.SearchRequest) (*contract.SearchResponse, error) {
|
||||
passedNamespaces = append(passedNamespaces, body.Namespaces...)
|
||||
return &contract.SearchResponse{Memories: []contract.Memory{
|
||||
{ID: "m-root", Namespace: "workspace:root-1", Content: "root private", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
{ID: "m-child1", Namespace: "workspace:child-1", Content: "child-1 private", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
{ID: "m-child2", Namespace: "workspace:child-2", Content: "child-2 private", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
{ID: "m-team", Namespace: "team:root-1", Content: "shared team", Kind: contract.MemoryKindFact, Source: contract.MemorySourceAgent, CreatedAt: time.Now().UTC()},
|
||||
}}, nil
|
||||
},
|
||||
}
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(plugin, resolver)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("code = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Every member's private namespace must reach the plugin search.
|
||||
want := []string{"workspace:root-1", "workspace:child-1", "workspace:child-2", "team:root-1", "org:root-1"}
|
||||
got := make(map[string]bool, len(passedNamespaces))
|
||||
for _, ns := range passedNamespaces {
|
||||
got[ns] = true
|
||||
}
|
||||
for _, w := range want {
|
||||
if !got[w] {
|
||||
t.Errorf("plugin search missing namespace %q (got %v)", w, passedNamespaces)
|
||||
}
|
||||
}
|
||||
if len(passedNamespaces) != 5 {
|
||||
t.Errorf("plugin search namespace count = %d, want 5 (3 workspace + team + org)", len(passedNamespaces))
|
||||
}
|
||||
|
||||
// Children's private memories must appear in the export, attributed
|
||||
// to the right workspace_name.
|
||||
var entries []memoryExportEntry
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &entries); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
byID := map[string]memoryExportEntry{}
|
||||
for _, e := range entries {
|
||||
byID[e.ID] = e
|
||||
}
|
||||
for _, exp := range []struct{ id, ns, owner string }{
|
||||
{"m-root", "workspace:root-1", "alpha"},
|
||||
{"m-child1", "workspace:child-1", "alpha-child"},
|
||||
{"m-child2", "workspace:child-2", "alpha-grandchild"},
|
||||
} {
|
||||
e, ok := byID[exp.id]
|
||||
if !ok {
|
||||
t.Errorf("export missing memory %s — children's private memories silently dropped", exp.id)
|
||||
continue
|
||||
}
|
||||
if e.Namespace != exp.ns {
|
||||
t.Errorf("memory %s namespace = %q, want %q", exp.id, e.Namespace, exp.ns)
|
||||
}
|
||||
if e.WorkspaceName != exp.owner {
|
||||
t.Errorf("memory %s owner = %q, want %q", exp.id, e.WorkspaceName, exp.owner)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestPickOwnerForNamespace covers the namespace→workspace_name
|
||||
// attribution helper introduced in I3.
|
||||
func TestPickOwnerForNamespace(t *testing.T) {
|
||||
members := []workspaceRow{
|
||||
{ID: "root-1", Name: "alpha", RootID: "root-1"},
|
||||
{ID: "child-1", Name: "alpha-child", RootID: "root-1"},
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
ns string
|
||||
want string
|
||||
}{
|
||||
{"workspace ns matches member id", "workspace:child-1", "alpha-child"},
|
||||
{"workspace ns no match → first", "workspace:foreign", "alpha"},
|
||||
{"team ns → first member of root group", "team:root-1", "alpha"},
|
||||
{"org ns → first member", "org:root-1", "alpha"},
|
||||
{"custom ns → first member", "custom:foo", "alpha"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := pickOwnerForNamespace(tc.ns, members); got != tc.want {
|
||||
t.Errorf("pickOwnerForNamespace(%q) = %q, want %q", tc.ns, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
if got := pickOwnerForNamespace("workspace:abc", nil); got != "" {
|
||||
t.Errorf("empty members must return \"\", got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Helper functions ---
|
||||
|
||||
func TestLegacyScopeFromNamespace(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{"workspace:abc", "LOCAL"},
|
||||
{"team:abc", "TEAM"},
|
||||
{"org:abc", "GLOBAL"},
|
||||
{"custom:abc", ""},
|
||||
{"", ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := legacyScopeFromNamespace(tc.in); got != tc.want {
|
||||
t.Errorf("legacyScopeFromNamespace(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNamespaceKindFromLegacyScope(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want contract.NamespaceKind
|
||||
}{
|
||||
{"LOCAL", contract.NamespaceKindWorkspace},
|
||||
{"local", contract.NamespaceKindWorkspace},
|
||||
{"TEAM", contract.NamespaceKindTeam},
|
||||
{"GLOBAL", contract.NamespaceKindOrg},
|
||||
{"weird", contract.NamespaceKindWorkspace},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := namespaceKindFromLegacyScope(tc.in); got != tc.want {
|
||||
t.Errorf("namespaceKindFromLegacyScope(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkipImport_ErrorMessage(t *testing.T) {
|
||||
e := &skipImport{reason: "unknown scope: WEIRD"}
|
||||
if !strings.Contains(e.Error(), "unknown scope: WEIRD") {
|
||||
t.Errorf("Error() = %q", e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// --- Confirm legacy paths still work when env is unset ---
|
||||
|
||||
func TestExport_LegacyPathWhenCutoverInactive(t *testing.T) {
|
||||
t.Setenv(envMemoryV2Cutover, "")
|
||||
mock := installMockDB(t)
|
||||
mock.ExpectQuery("SELECT am.id, am.content, am.scope, am.namespace").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "content", "scope", "namespace", "created_at", "workspace_name"}))
|
||||
|
||||
h := NewAdminMemoriesHandler().withMemoryV2APIs(&stubAdminPlugin{}, adminRootResolver())
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/admin/memories/export", nil)
|
||||
h.Export(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("code = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("legacy SQL path not exercised: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -30,23 +30,38 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/events"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/pendinguploads"
|
||||
)
|
||||
|
||||
// ChatFilesHandler serves file upload + download for chat. Holds a
|
||||
// reference to TemplatesHandler so the (still docker-exec) Download
|
||||
// path keeps using the shared findContainer/CopyFromContainer helpers
|
||||
// without duplicating them. Upload no longer reaches into Docker.
|
||||
//
|
||||
// pendingUploads + broadcaster are wired only when the platform's
|
||||
// migration 20260505100000 has run; nil values fall back to the
|
||||
// pre-poll-mode behavior (422 on poll-mode upload, same as before).
|
||||
// This lets the binary keep booting in environments where the
|
||||
// migration hasn't run yet — the poll branch is gated by a not-nil
|
||||
// check at the call site.
|
||||
type ChatFilesHandler struct {
|
||||
templates *TemplatesHandler
|
||||
|
||||
@@ -55,6 +70,19 @@ type ChatFilesHandler struct {
|
||||
// the 50 MB worst case on a slow EC2 link without leaving a
|
||||
// connection hanging forever on a sick workspace.
|
||||
httpClient *http.Client
|
||||
|
||||
// pendingUploads is the platform-side staging layer for poll-mode
|
||||
// uploads. nil → poll branch returns 422 unchanged (the pre-feature
|
||||
// behavior); non-nil → poll branch parses multipart, persists each
|
||||
// file via storage.Put, logs a chat_upload_receive activity row,
|
||||
// and returns 200 with synthetic platform-pending: URIs.
|
||||
pendingUploads pendinguploads.Storage
|
||||
|
||||
// broadcaster is the events.EventEmitter used to notify the canvas
|
||||
// when an activity row lands (so the Agent Comms panel updates
|
||||
// live). Same emitter the rest of the platform uses; nil = no
|
||||
// broadcast (tests).
|
||||
broadcaster events.EventEmitter
|
||||
}
|
||||
|
||||
func NewChatFilesHandler(t *TemplatesHandler) *ChatFilesHandler {
|
||||
@@ -68,6 +96,16 @@ func NewChatFilesHandler(t *TemplatesHandler) *ChatFilesHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// WithPendingUploads enables the poll-mode upload branch by wiring a
|
||||
// Storage + broadcaster. Call site (router.go) does this at
|
||||
// construction; tests set the fields directly when they want the
|
||||
// poll path exercised. Returns the handler for chained construction.
|
||||
func (h *ChatFilesHandler) WithPendingUploads(storage pendinguploads.Storage, broadcaster events.EventEmitter) *ChatFilesHandler {
|
||||
h.pendingUploads = storage
|
||||
h.broadcaster = broadcaster
|
||||
return h
|
||||
}
|
||||
|
||||
// chatUploadMaxBytes caps the full multipart request body so a
|
||||
// malicious / runaway client can't OOM the proxy hop. 50 MB matches
|
||||
// the workspace-side limit; anything larger is rejected at the
|
||||
@@ -102,14 +140,45 @@ const chatUploadDir = "/workspace/.molecule/chat-uploads"
|
||||
// of bug as the original SaaS provision drift fixed in #2366; this
|
||||
// extraction prevents that class on the consumer side.
|
||||
func resolveWorkspaceForwardCreds(c *gin.Context, ctx context.Context, workspaceID, op string) (wsURL, secret string, ok bool) {
|
||||
var deliveryMode sql.NullString
|
||||
if err := db.DB.QueryRowContext(ctx,
|
||||
`SELECT COALESCE(url, '') FROM workspaces WHERE id = $1`, workspaceID,
|
||||
).Scan(&wsURL); err != nil {
|
||||
`SELECT COALESCE(url, ''), delivery_mode FROM workspaces WHERE id = $1`, workspaceID,
|
||||
).Scan(&wsURL, &deliveryMode); err != nil {
|
||||
log.Printf("chat_files %s: workspace lookup failed for %s: %v", op, workspaceID, err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "workspace not found"})
|
||||
return "", "", false
|
||||
}
|
||||
if wsURL == "" {
|
||||
// Distinguish the two empty-URL classes so the user sees an
|
||||
// actionable error rather than a misleading "not registered yet"
|
||||
// (which implies waiting will help):
|
||||
//
|
||||
// push-mode → URL just isn't on the row yet (workspace
|
||||
// restart in progress, or first /registry/register hasn't
|
||||
// landed). 503 + "not registered yet" is correct — retry
|
||||
// after the next heartbeat (~30s) will likely succeed.
|
||||
//
|
||||
// anything else (poll-mode, NULL, empty string) → URL is
|
||||
// structurally absent. The platform never dispatches to a
|
||||
// non-push workspace, so chat upload (which is HTTP-forward
|
||||
// by design) cannot proceed by waiting. Returning 503 here
|
||||
// would loop the canvas client forever. 422 signals "this
|
||||
// request can't succeed against THIS workspace's
|
||||
// configuration" — the only fix is to re-register the
|
||||
// workspace with a publicly-reachable URL.
|
||||
//
|
||||
// Live-observed 2026-05-04: external runtime workspaces (e.g.
|
||||
// molecule-sdk-python on a mac laptop) register with
|
||||
// delivery_mode=NULL. The narrow "poll" check missed them; the
|
||||
// invariant we actually want is "URL empty + not-push = no
|
||||
// dispatch path, ever".
|
||||
if !deliveryMode.Valid || deliveryMode.String != "push" {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"error": "workspace has no callback URL — chat " + op + " requires push-mode + public URL",
|
||||
"detail": "This workspace registered without a publicly-reachable URL (delivery_mode is not 'push'). The platform cannot dispatch chat uploads to it. Re-register the workspace with a public URL in push mode (e.g. via ngrok / Cloudflare tunnel) to enable chat file " + op + ".",
|
||||
})
|
||||
return "", "", false
|
||||
}
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "workspace url not registered yet"})
|
||||
return "", "", false
|
||||
}
|
||||
@@ -230,6 +299,24 @@ func (h *ChatFilesHandler) Upload(c *gin.Context) {
|
||||
|
||||
ctx := c.Request.Context()
|
||||
|
||||
// Branch on delivery_mode BEFORE attempting the HTTP forward.
|
||||
// Push-mode workspaces continue to do the streaming forward
|
||||
// unchanged. Poll-mode workspaces (typically external runtimes
|
||||
// on a laptop, no public callback URL) get the platform-side
|
||||
// staging path — the file lands in pending_uploads, an activity
|
||||
// row goes into the inbox queue, and the workspace pulls on its
|
||||
// next poll cycle.
|
||||
if h.pendingUploads != nil {
|
||||
mode, modeOK := lookupUploadDeliveryMode(c, ctx, workspaceID)
|
||||
if !modeOK {
|
||||
return
|
||||
}
|
||||
if mode == "poll" {
|
||||
h.uploadPollMode(c, ctx, workspaceID)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
wsURL, secret, ok := resolveWorkspaceForwardCreds(c, ctx, workspaceID, "upload")
|
||||
if !ok {
|
||||
return
|
||||
@@ -373,3 +460,317 @@ func (h *ChatFilesHandler) streamWorkspaceResponse(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// lookupUploadDeliveryMode returns the workspace's delivery_mode
|
||||
// for the chat upload branch. Returns ("", false) and writes the
|
||||
// HTTP error response on lookup failure (caller stops). NULL or
|
||||
// empty delivery_mode is treated as "push" — that's the schema
|
||||
// default and matches the legacy pre-#2339 behavior. Only the
|
||||
// explicit string "poll" routes the upload through the poll-mode
|
||||
// branch.
|
||||
//
|
||||
// Why a dedicated helper instead of reusing lookupDeliveryMode
|
||||
// from a2a_proxy_helpers.go: that one swallows errors and falls
|
||||
// back to "push" so the proxy keeps working on a transient DB
|
||||
// hiccup. For upload we want to surface the not-found case as 404
|
||||
// (which the workspace-poll branch wouldn't otherwise hit, since
|
||||
// the workspace-side row IS the source of truth for the mode).
|
||||
func lookupUploadDeliveryMode(c *gin.Context, ctx context.Context, workspaceID string) (string, bool) {
|
||||
var mode sql.NullString
|
||||
err := db.DB.QueryRowContext(ctx,
|
||||
`SELECT delivery_mode FROM workspaces WHERE id = $1`, workspaceID,
|
||||
).Scan(&mode)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "workspace not found"})
|
||||
return "", false
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("chat_files Upload: delivery_mode lookup failed for %s: %v", workspaceID, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "delivery_mode lookup failed"})
|
||||
return "", false
|
||||
}
|
||||
if !mode.Valid || mode.String == "" {
|
||||
return "push", true
|
||||
}
|
||||
return mode.String, true
|
||||
}
|
||||
|
||||
// unsafeFilenameChars matches every character that isn't in the safe
|
||||
// alphanumeric + dot/dash/underscore set. Mirrors the Python regex
|
||||
// _UNSAFE_FILENAME_CHARS in workspace/internal_chat_uploads.py — drift
|
||||
// here would mean canvas-emitted URIs differ between push and poll
|
||||
// paths for the same upload.
|
||||
var unsafeFilenameChars = regexp.MustCompile(`[^a-zA-Z0-9._\-]`)
|
||||
|
||||
// SanitizeFilename reduces a user-supplied filename to a safe form.
|
||||
// Behaviorally identical to sanitize_filename in workspace/
|
||||
// internal_chat_uploads.py. Exported so tests in other packages can
|
||||
// pin behavior parity, and so a future shared library can move both
|
||||
// implementations behind one source of truth.
|
||||
func SanitizeFilename(name string) string {
|
||||
base := filepath.Base(name)
|
||||
// filepath.Base on a path-traversal input ("../../etc/passwd")
|
||||
// returns "passwd" (just the last component) — which matches what
|
||||
// Python's os.path.basename does. Tests pin both here and on the
|
||||
// Python side.
|
||||
base = strings.ReplaceAll(base, " ", "_")
|
||||
base = unsafeFilenameChars.ReplaceAllString(base, "_")
|
||||
if len(base) > 100 {
|
||||
ext := ""
|
||||
dot := strings.LastIndex(base, ".")
|
||||
if dot >= 0 && len(base)-dot <= 16 {
|
||||
ext = base[dot:]
|
||||
}
|
||||
base = base[:100-len(ext)] + ext
|
||||
}
|
||||
if base == "" || base == "." || base == ".." {
|
||||
return "file"
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
// uploadedFile is the per-file response shape the workspace-side
|
||||
// /internal/chat/uploads/ingest also produces. Mirroring the schema
|
||||
// keeps the canvas client unaware of which path handled the upload.
|
||||
type uploadedFile struct {
|
||||
URI string `json:"uri"`
|
||||
Name string `json:"name"`
|
||||
Mimetype string `json:"mimeType"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// uploadPollMode handles a chat upload bound for a poll-mode
|
||||
// workspace. Parses the multipart in-place, persists each file via
|
||||
// pendinguploads.Storage, and logs one chat_upload_receive activity
|
||||
// row per file so the workspace's inbox poller picks them up on its
|
||||
// next cycle.
|
||||
//
|
||||
// Why one activity row per file (not one per multipart batch):
|
||||
// - Each row carries one URI; agents that consume the inbox treat
|
||||
// each row as one inbound event. A batch row would force every
|
||||
// consumer to deserialize a list, doubling the field-shape
|
||||
// surface for no UX win.
|
||||
// - At-least-once semantics: a workspace can ack files
|
||||
// individually. Batch ack would leak partial-success state on
|
||||
// a fetcher crash mid-batch.
|
||||
//
|
||||
// Limits enforced here mirror the workspace-side ingest_handler:
|
||||
// - Total body cap: 50 MB (set on c.Request.Body before reaching us)
|
||||
// - Per-file cap: 25 MB (pendinguploads.MaxFileBytes; rejected as 413)
|
||||
// - Filename: sanitized + capped at 100 chars (SanitizeFilename)
|
||||
//
|
||||
// Logging: every persisted file logs an INFO line with workspace_id,
|
||||
// file_id, size, and sanitized name. Failure modes (oversize, missing
|
||||
// files field, malformed multipart) log at WARN with the same fields.
|
||||
// Phase 3 metrics will hook these structured logs.
|
||||
func (h *ChatFilesHandler) uploadPollMode(c *gin.Context, ctx context.Context, workspaceID string) {
|
||||
// Parse multipart with the same per-file/per-form limits the
|
||||
// workspace-side handler uses (workspace/internal_chat_uploads.py:
|
||||
// max_files=64, max_fields=32). gin's MultipartForm does not
|
||||
// expose those limits directly — the underlying ParseMultipartForm
|
||||
// caps memory at 32 MB by default and spills to disk. For poll-
|
||||
// mode we read each file into memory to hand to Storage.Put;
|
||||
// 25 MB-per-file × 64-files ceiling means worst-case is 1.6 GB of
|
||||
// peak memory. Bound the per-file size at the multipart layer so
|
||||
// the spill never gets close.
|
||||
if err := c.Request.ParseMultipartForm(32 << 20); err != nil {
|
||||
log.Printf("chat_files uploadPollMode: parse multipart failed for %s: %v", workspaceID, err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "malformed multipart body"})
|
||||
return
|
||||
}
|
||||
form := c.Request.MultipartForm
|
||||
if form == nil || len(form.File["files"]) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no files field in request"})
|
||||
return
|
||||
}
|
||||
headers := form.File["files"]
|
||||
if len(headers) > 64 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "too many files (limit 64)"})
|
||||
return
|
||||
}
|
||||
|
||||
wsUUID, err := uuid.Parse(workspaceID)
|
||||
if err != nil {
|
||||
// validateWorkspaceID at the top of Upload already gates this;
|
||||
// the re-parse is defence in depth in case validateWorkspaceID
|
||||
// drifts. Keep the error class consistent so a bad-id reaches
|
||||
// the same 400 path. Not separately tested because the gate at
|
||||
// the call site is structurally the same uuid.Parse.
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid workspace ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Phase 1: pre-validate + read every part BEFORE any DB write.
|
||||
// A multi-file upload must commit all-or-nothing; a per-file
|
||||
// failure halfway through used to leave rows 1..K-1 in the table
|
||||
// while the client got a 500 and retried the whole batch — duplicate
|
||||
// rows, orphan activity rows. Validating up-front + atomic PutBatch
|
||||
// closes that gap.
|
||||
type prepped struct {
|
||||
Sanitized string
|
||||
Mimetype string
|
||||
Content []byte
|
||||
Original string // original (unsanitized) filename for error messages
|
||||
}
|
||||
prepReady := make([]prepped, 0, len(headers))
|
||||
items := make([]pendinguploads.PutItem, 0, len(headers))
|
||||
for _, fh := range headers {
|
||||
if fh.Size > pendinguploads.MaxFileBytes {
|
||||
log.Printf("chat_files uploadPollMode: per-file cap exceeded for %s: %s (%d bytes)",
|
||||
workspaceID, fh.Filename, fh.Size)
|
||||
c.JSON(http.StatusRequestEntityTooLarge, gin.H{
|
||||
"error": "file exceeds per-file cap",
|
||||
"filename": fh.Filename,
|
||||
"size": fh.Size,
|
||||
"max": pendinguploads.MaxFileBytes,
|
||||
})
|
||||
return
|
||||
}
|
||||
content, err := readMultipartFile(fh)
|
||||
if err != nil {
|
||||
log.Printf("chat_files uploadPollMode: read part failed for %s/%s: %v",
|
||||
workspaceID, fh.Filename, err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "could not read file part"})
|
||||
return
|
||||
}
|
||||
// Belt-and-braces post-read cap (multipart.FileHeader.Size can lie
|
||||
// on some clients that don't set Content-Length per part).
|
||||
if len(content) > pendinguploads.MaxFileBytes {
|
||||
log.Printf("chat_files uploadPollMode: per-file cap exceeded post-read for %s: %s (%d bytes)",
|
||||
workspaceID, fh.Filename, len(content))
|
||||
c.JSON(http.StatusRequestEntityTooLarge, gin.H{
|
||||
"error": "file exceeds per-file cap",
|
||||
"filename": fh.Filename,
|
||||
"size": len(content),
|
||||
"max": pendinguploads.MaxFileBytes,
|
||||
})
|
||||
return
|
||||
}
|
||||
sanitized := SanitizeFilename(fh.Filename)
|
||||
mimetype := safeMimetype(fh.Header.Get("Content-Type"))
|
||||
prepReady = append(prepReady, prepped{
|
||||
Sanitized: sanitized, Mimetype: mimetype, Content: content, Original: fh.Filename,
|
||||
})
|
||||
items = append(items, pendinguploads.PutItem{
|
||||
Content: content, Filename: sanitized, Mimetype: mimetype,
|
||||
})
|
||||
}
|
||||
|
||||
// Phase 2: atomic batch insert. On failure no rows commit.
|
||||
fileIDs, err := h.pendingUploads.PutBatch(ctx, wsUUID, items)
|
||||
if err != nil {
|
||||
if errors.Is(err, pendinguploads.ErrTooLarge) {
|
||||
// Belt + suspenders: pre-validation above already caught
|
||||
// this; surface a clean 413 if a malformed FileHeader
|
||||
// somehow slipped through.
|
||||
c.JSON(http.StatusRequestEntityTooLarge, gin.H{
|
||||
"error": "one or more files exceed per-file cap",
|
||||
"max": pendinguploads.MaxFileBytes,
|
||||
})
|
||||
return
|
||||
}
|
||||
log.Printf("chat_files uploadPollMode: storage.PutBatch failed for %s: %v",
|
||||
workspaceID, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "could not stage files"})
|
||||
return
|
||||
}
|
||||
|
||||
// Phase 3: write per-file activity rows and build the response. Activity
|
||||
// rows are written individually (not part of the same Tx as PutBatch)
|
||||
// because LogActivity is shared across many handlers and threading the
|
||||
// Tx through would be a bigger refactor. The trade-off: if an activity
|
||||
// write fails after the PutBatch commits, the pending_uploads rows
|
||||
// orphan until the 24h TTL — significantly better than the previous
|
||||
// "every multi-file upload could orphan" behavior, and the workspace's
|
||||
// fetcher handles soft-404 cleanly when activity rows reference a row
|
||||
// the platform later expired.
|
||||
out := make([]uploadedFile, 0, len(prepReady))
|
||||
for i, p := range prepReady {
|
||||
fileID := fileIDs[i]
|
||||
uri := fmt.Sprintf("platform-pending:%s/%s", workspaceID, fileID)
|
||||
summary := "chat_upload_receive: " + p.Sanitized
|
||||
method := "chat_upload_receive"
|
||||
LogActivity(ctx, h.broadcaster, ActivityParams{
|
||||
WorkspaceID: workspaceID,
|
||||
ActivityType: "a2a_receive",
|
||||
TargetID: &workspaceID,
|
||||
Method: &method,
|
||||
Summary: &summary,
|
||||
RequestBody: map[string]interface{}{
|
||||
"file_id": fileID.String(),
|
||||
"name": p.Sanitized,
|
||||
"mimeType": p.Mimetype,
|
||||
"size": len(p.Content),
|
||||
"uri": uri,
|
||||
},
|
||||
Status: "ok",
|
||||
})
|
||||
|
||||
log.Printf("chat_files uploadPollMode: staged %s/%s (file_id=%s size=%d mimetype=%q)",
|
||||
workspaceID, p.Sanitized, fileID, len(p.Content), p.Mimetype)
|
||||
|
||||
out = append(out, uploadedFile{
|
||||
URI: uri,
|
||||
Name: p.Sanitized,
|
||||
Mimetype: p.Mimetype,
|
||||
Size: int64(len(p.Content)),
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"files": out})
|
||||
}
|
||||
|
||||
// safeMimetype validates a multipart-supplied Content-Type header and
|
||||
// returns a sanitized value safe to store + serve back unmodified.
|
||||
//
|
||||
// The platform's GET /content handler reflects the stored mimetype as
|
||||
// the response Content-Type. An attacker-controlled header that
|
||||
// embedded CR/LF could split the response (header injection); a value
|
||||
// containing semicolons could carry an unexpected charset parameter
|
||||
// that confuses a downstream renderer. Strip CR/LF/control chars +
|
||||
// keep only the type/subtype prefix; reject anything that doesn't
|
||||
// match a basic `type/subtype` regex by falling back to the safe
|
||||
// default (application/octet-stream — the workspace-side handler does
|
||||
// the same fallback).
|
||||
func safeMimetype(raw string) string {
|
||||
const fallback = "application/octet-stream"
|
||||
// Trim parameters (`text/html; charset=utf-8` → `text/html`).
|
||||
if i := strings.IndexByte(raw, ';'); i >= 0 {
|
||||
raw = raw[:i]
|
||||
}
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
// Reject if any control char or whitespace is present (header
|
||||
// injection defense). RFC 7231 mimetype grammar forbids whitespace.
|
||||
for _, r := range raw {
|
||||
if r < 0x21 || r > 0x7e {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
// Require exactly one slash separating type and subtype.
|
||||
parts := strings.Split(raw, "/")
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||
return fallback
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// readMultipartFile reads a multipart part fully into memory. Wraps
|
||||
// the open + io.ReadAll + close idiom so the call site stays clean,
|
||||
// and so a future change (chunked reads / hashing) has one place to
|
||||
// land.
|
||||
func readMultipartFile(fh *multipartFileHeader) ([]byte, error) {
|
||||
f, err := fh.Open()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open part: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
return io.ReadAll(f)
|
||||
}
|
||||
|
||||
// multipartFileHeader is a local alias so the readMultipartFile
|
||||
// signature doesn't pull "mime/multipart" into every test that
|
||||
// touches uploadPollMode.
|
||||
type multipartFileHeader = multipart.FileHeader
|
||||
|
||||
@@ -0,0 +1,750 @@
|
||||
package handlers
|
||||
|
||||
// chat_files_poll_test.go — Upload poll-mode branch tests.
|
||||
//
|
||||
// Pinned in their own file so the existing chat_files_test.go stays
|
||||
// focused on the push-mode forward proxy. Same setupTestDB / sqlmock
|
||||
// scaffolding as the rest of the package, plus an in-memory
|
||||
// pendinguploads.Storage so we don't have to mock six SQL statements
|
||||
// per assertion.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/pendinguploads"
|
||||
)
|
||||
|
||||
// inMemStorage is a process-local pendinguploads.Storage for branch
|
||||
// tests. Records every Put for assertion. Failure modes (Put error,
|
||||
// MarkFetched / Ack tested elsewhere) are injected via fields.
|
||||
type inMemStorage struct {
|
||||
mu sync.Mutex
|
||||
rows map[uuid.UUID]pendinguploads.Record
|
||||
puts []putCall
|
||||
putErr error
|
||||
}
|
||||
|
||||
type putCall struct {
|
||||
WorkspaceID uuid.UUID
|
||||
Filename string
|
||||
Mimetype string
|
||||
Size int
|
||||
}
|
||||
|
||||
func newInMemStorage() *inMemStorage {
|
||||
return &inMemStorage{rows: map[uuid.UUID]pendinguploads.Record{}}
|
||||
}
|
||||
|
||||
func (s *inMemStorage) Put(_ context.Context, ws uuid.UUID, content []byte, filename, mimetype string) (uuid.UUID, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.putErr != nil {
|
||||
return uuid.Nil, s.putErr
|
||||
}
|
||||
id := uuid.New()
|
||||
s.rows[id] = pendinguploads.Record{
|
||||
FileID: id, WorkspaceID: ws, Content: content,
|
||||
Filename: filename, Mimetype: mimetype,
|
||||
SizeBytes: int64(len(content)), CreatedAt: time.Now(),
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
}
|
||||
s.puts = append(s.puts, putCall{
|
||||
WorkspaceID: ws, Filename: filename, Mimetype: mimetype, Size: len(content),
|
||||
})
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// PutBatch mirrors the production atomic-batch contract: any per-item
|
||||
// failure leaves the in-memory state unchanged, simulating Tx rollback.
|
||||
// Pre-validation matches PostgresStorage.PutBatch; oversized items
|
||||
// return ErrTooLarge before any row is added.
|
||||
func (s *inMemStorage) PutBatch(_ context.Context, ws uuid.UUID, items []pendinguploads.PutItem) ([]uuid.UUID, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.putErr != nil {
|
||||
return nil, s.putErr
|
||||
}
|
||||
// Pre-validate so an oversized item rejects the whole batch before
|
||||
// any state mutation — matches the Tx-rollback semantics.
|
||||
for _, it := range items {
|
||||
if len(it.Content) > pendinguploads.MaxFileBytes {
|
||||
return nil, pendinguploads.ErrTooLarge
|
||||
}
|
||||
}
|
||||
ids := make([]uuid.UUID, 0, len(items))
|
||||
stagedRows := make(map[uuid.UUID]pendinguploads.Record, len(items))
|
||||
stagedPuts := make([]putCall, 0, len(items))
|
||||
for _, it := range items {
|
||||
id := uuid.New()
|
||||
stagedRows[id] = pendinguploads.Record{
|
||||
FileID: id, WorkspaceID: ws, Content: it.Content,
|
||||
Filename: it.Filename, Mimetype: it.Mimetype,
|
||||
SizeBytes: int64(len(it.Content)), CreatedAt: time.Now(),
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
}
|
||||
stagedPuts = append(stagedPuts, putCall{
|
||||
WorkspaceID: ws, Filename: it.Filename, Mimetype: it.Mimetype, Size: len(it.Content),
|
||||
})
|
||||
ids = append(ids, id)
|
||||
}
|
||||
for id, r := range stagedRows {
|
||||
s.rows[id] = r
|
||||
}
|
||||
s.puts = append(s.puts, stagedPuts...)
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (s *inMemStorage) Get(context.Context, uuid.UUID) (pendinguploads.Record, error) {
|
||||
return pendinguploads.Record{}, pendinguploads.ErrNotFound
|
||||
}
|
||||
func (s *inMemStorage) MarkFetched(context.Context, uuid.UUID) error { return nil }
|
||||
func (s *inMemStorage) Ack(context.Context, uuid.UUID) error { return nil }
|
||||
|
||||
// Sweep is required by the Storage interface (Phase 3 GC). Not
|
||||
// exercised by upload-branch tests — the dedicated sweeper_test.go +
|
||||
// storage_sweep_test.go cover it.
|
||||
func (s *inMemStorage) Sweep(context.Context, time.Duration) (pendinguploads.SweepResult, error) {
|
||||
return pendinguploads.SweepResult{}, nil
|
||||
}
|
||||
|
||||
// expectPollDeliveryMode stubs the SELECT delivery_mode lookup that
|
||||
// uploadPollMode does (separate from the one resolveWorkspaceForwardCreds
|
||||
// does — this is the new helper introduced for the poll branch).
|
||||
func expectPollDeliveryMode(mock sqlmock.Sqlmock, workspaceID, mode string) {
|
||||
rows := sqlmock.NewRows([]string{"delivery_mode"}).AddRow(mode)
|
||||
mock.ExpectQuery(`SELECT delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(workspaceID).
|
||||
WillReturnRows(rows)
|
||||
}
|
||||
|
||||
func expectPollDeliveryModeMissing(mock sqlmock.Sqlmock, workspaceID string) {
|
||||
mock.ExpectQuery(`SELECT delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(workspaceID).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
}
|
||||
|
||||
// expectActivityInsert stubs the LogActivity INSERT so the poll branch's
|
||||
// per-file activity row write doesn't fail the sqlmock expectations.
|
||||
func expectActivityInsert(mock sqlmock.Sqlmock) {
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
}
|
||||
|
||||
// expectActivityInsertWithTypeAndMethod is a strict variant that pins
|
||||
// the activity_type and method positional args. Used in the discriminator
|
||||
// regression test below — the workspace inbox poller filters
|
||||
// `?type=a2a_receive`, so writing any other activity_type silently breaks
|
||||
// poll-mode delivery without a build/test error. Pin the two discriminator
|
||||
// fields so a refactor that flips activity_type back to a custom value is
|
||||
// caught here instead of at runtime by a confused poller.
|
||||
//
|
||||
// Positional args (LogActivity uses ExecContext with 12 positional params):
|
||||
// $1 workspace_id, $2 activity_type, $3 source_id, $4 target_id,
|
||||
// $5 method, $6 summary, $7 request_body, $8 response_body,
|
||||
// $9 tool_trace, $10 duration_ms, $11 status, $12 error_detail.
|
||||
func expectActivityInsertWithTypeAndMethod(mock sqlmock.Sqlmock, workspaceID, activityType, method string) {
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
workspaceID, // $1 workspace_id
|
||||
activityType, // $2 activity_type ← pinned
|
||||
sqlmock.AnyArg(), // $3 source_id
|
||||
sqlmock.AnyArg(), // $4 target_id (workspaceID, but already covered)
|
||||
method, // $5 method ← pinned
|
||||
sqlmock.AnyArg(), // $6 summary
|
||||
sqlmock.AnyArg(), // $7 request_body
|
||||
sqlmock.AnyArg(), // $8 response_body
|
||||
sqlmock.AnyArg(), // $9 tool_trace
|
||||
sqlmock.AnyArg(), // $10 duration_ms
|
||||
sqlmock.AnyArg(), // $11 status
|
||||
sqlmock.AnyArg(), // $12 error_detail
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
}
|
||||
|
||||
// pollUploadFixture builds a multipart body with N named files.
|
||||
func pollUploadFixture(t *testing.T, files map[string][]byte) (*bytes.Buffer, string) {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
for name, data := range files {
|
||||
fw, err := mw.CreateFormFile("files", name)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateFormFile: %v", err)
|
||||
}
|
||||
_, _ = fw.Write(data)
|
||||
}
|
||||
mw.Close()
|
||||
return &buf, mw.FormDataContentType()
|
||||
}
|
||||
|
||||
// ---- happy path ----
|
||||
|
||||
func TestPollUpload_HappyPath_OneFile_StagesAndLogs(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "11111111-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
expectActivityInsert(mock)
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"report.pdf": []byte("PDF-bytes")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(store.puts) != 1 {
|
||||
t.Fatalf("expected 1 storage Put, got %d", len(store.puts))
|
||||
}
|
||||
put := store.puts[0]
|
||||
if put.Filename != "report.pdf" || put.Size != 9 {
|
||||
t.Errorf("unexpected put: %+v", put)
|
||||
}
|
||||
|
||||
// Response shape must match the workspace-side
|
||||
// /internal/chat/uploads/ingest schema so canvas can't tell which
|
||||
// path handled the upload.
|
||||
var resp struct {
|
||||
Files []struct {
|
||||
URI string `json:"uri"`
|
||||
Name string `json:"name"`
|
||||
Mimetype string `json:"mimeType"`
|
||||
Size int `json:"size"`
|
||||
} `json:"files"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("decode response: %v body=%s", err, w.Body.String())
|
||||
}
|
||||
if len(resp.Files) != 1 {
|
||||
t.Fatalf("response files count = %d, want 1", len(resp.Files))
|
||||
}
|
||||
got := resp.Files[0]
|
||||
if got.Name != "report.pdf" || got.Size != 9 {
|
||||
t.Errorf("response file mismatch: %+v", got)
|
||||
}
|
||||
if !strings.HasPrefix(got.URI, "platform-pending:"+wsID+"/") {
|
||||
t.Errorf("URI %q does not start with platform-pending:%s/", got.URI, wsID)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_MultipleFiles_AllStagedAndLogged(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "11111111-aaaa-bbbb-cccc-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
expectActivityInsert(mock)
|
||||
expectActivityInsert(mock)
|
||||
expectActivityInsert(mock)
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{
|
||||
"a.txt": []byte("aaaa"),
|
||||
"b.txt": []byte("bbbbb"),
|
||||
"c.txt": []byte("cccccc"),
|
||||
})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(store.puts) != 3 {
|
||||
t.Fatalf("expected 3 storage Puts, got %d", len(store.puts))
|
||||
}
|
||||
}
|
||||
|
||||
// ---- regression: push-mode unchanged ----
|
||||
|
||||
func TestPollUpload_PushModeFallsThroughToForward(t *testing.T) {
|
||||
// With pendingUploads wired but the workspace's mode is push,
|
||||
// the poll branch must NOT activate — flow falls through to the
|
||||
// existing resolveWorkspaceForwardCreds path. Pinned via the
|
||||
// "delivery_mode lookup happened, then the URL+mode SELECT
|
||||
// happened, then we 503 because no inbound secret" sequence.
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "22222222-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "push")
|
||||
// After the poll branch is bypassed, we hit
|
||||
// resolveWorkspaceForwardCreds which selects url+delivery_mode.
|
||||
expectURL(mock, wsID, "")
|
||||
// URL empty + mode=push → 503 (no inbound secret check needed).
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x": []byte("data")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status=%d body=%s — expected push-mode 503 fall-through", w.Code, w.Body.String())
|
||||
}
|
||||
if len(store.puts) != 0 {
|
||||
t.Errorf("push-mode should NOT have hit storage, got %d puts", len(store.puts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_NotConfigured_FallsThrough(t *testing.T) {
|
||||
// Backwards compat: a binary running without WithPendingUploads
|
||||
// behaves exactly as before — the poll branch is dead code.
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "33333333-2222-3333-4444-555555555555"
|
||||
expectURLAndMode(mock, wsID, "", "poll") // resolveWorkspaceForwardCreds emits 422
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
// No WithPendingUploads — pendingUploads is nil.
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x": []byte("data")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("status=%d, want 422 (legacy poll-mode rejection)", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- error paths ----
|
||||
|
||||
func TestPollUpload_WorkspaceMissing_404(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "44444444-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryModeMissing(mock, wsID)
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(newInMemStorage(), nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x": []byte("d")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status=%d, want 404", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_DeliveryModeLookupDBError_500(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "55555555-2222-3333-4444-555555555555"
|
||||
mock.ExpectQuery(`SELECT delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(wsID).WillReturnError(errors.New("connection lost"))
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(newInMemStorage(), nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x": []byte("d")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("status=%d, want 500", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_NoFilesField_400(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "66666666-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
// Multipart with a non-files field — no actual files.
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
mw.WriteField("not_files", "hi")
|
||||
mw.Close()
|
||||
|
||||
c, w := makeUploadRequest(t, wsID, &buf, mw.FormDataContentType())
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status=%d, want 400 on no files field", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_MalformedMultipart_400(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "77777777-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
// Body that doesn't match the boundary in Content-Type.
|
||||
c, w := makeUploadRequest(t, wsID, bytes.NewBufferString("garbage"), "multipart/form-data; boundary=fake")
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status=%d, want 400 on malformed multipart", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_StorageError_500(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "88888888-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
store.putErr = errors.New("disk full")
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x.bin": []byte("data")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("status=%d, want 500", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_StorageTooLarge_413(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "99999999-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
store.putErr = pendinguploads.ErrTooLarge
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x.bin": []byte("data")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusRequestEntityTooLarge {
|
||||
t.Errorf("status=%d, want 413", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_TooManyFiles_400(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "aaaaaaaa-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
// 65 files — over the per-batch cap.
|
||||
files := map[string][]byte{}
|
||||
for i := 0; i < 65; i++ {
|
||||
files[uuid.New().String()] = []byte("x")
|
||||
}
|
||||
body, ct := pollUploadFixture(t, files)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status=%d, want 400 on too many files", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_NullDeliveryMode_TreatedAsPush(t *testing.T) {
|
||||
// Production-observed 2026-05-04: external runtime workspaces
|
||||
// (molecule-sdk-python on user infra) sometimes register with
|
||||
// delivery_mode = NULL — the schema default for legacy rows from
|
||||
// before #2339. The poll branch must NOT activate on NULL — only
|
||||
// the explicit "poll" string. This is the same defensive posture
|
||||
// resolveWorkspaceForwardCreds takes for legacy rows.
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "cccccccc-2222-3333-4444-555555555555"
|
||||
mock.ExpectQuery(`SELECT delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(wsID).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"delivery_mode"}).AddRow(nil))
|
||||
// Falls through to resolveWorkspaceForwardCreds:
|
||||
expectURLAndMode(mock, wsID, "", "")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x.bin": []byte("data")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
// resolveWorkspaceForwardCreds with empty url + NULL mode = 422
|
||||
// (the legacy "no callback URL" rejection — exactly what we're
|
||||
// fixing for ACTUAL poll-mode rows but want to preserve for
|
||||
// NULL ones until the row gets a real mode value via the next
|
||||
// /registry/register).
|
||||
if w.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("status=%d, want 422 for NULL delivery_mode (legacy fallthrough)", w.Code)
|
||||
}
|
||||
if len(store.puts) != 0 {
|
||||
t.Errorf("NULL mode should NOT have hit storage, got %d puts", len(store.puts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPollUpload_PerFileCapPreStorage_413(t *testing.T) {
|
||||
// Pin the early-reject branch (fh.Size > MaxFileBytes) BEFORE we
|
||||
// read the part into memory. Without this, an oversize file
|
||||
// would hit the storage layer's belt-and-suspenders check, which
|
||||
// works but burns ~25 MB of memory + DB round-trip first. Send
|
||||
// 25 MB + 1 byte → 413 with the file size in the response.
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "dddddddd-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
// 25 MB + 1 byte. Single file, large enough to trip the early
|
||||
// size check.
|
||||
oversize := make([]byte, pendinguploads.MaxFileBytes+1)
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"big.bin": oversize})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("status=%d, want 413 on per-file size cap", w.Code)
|
||||
}
|
||||
if len(store.puts) != 0 {
|
||||
t.Errorf("per-file cap reject should NOT have called storage.Put, got %d puts", len(store.puts))
|
||||
}
|
||||
// Sanity: response carries the size we tried to upload + the cap.
|
||||
var body_ map[string]any
|
||||
json.Unmarshal(w.Body.Bytes(), &body_)
|
||||
if got := body_["max"]; got == nil {
|
||||
t.Errorf("expected max field in response, got %v", body_)
|
||||
}
|
||||
}
|
||||
|
||||
// SanitizeFilename is exercised in the upload chain — pin one
|
||||
// end-to-end case that exercises the URI path through the response.
|
||||
func TestPollUpload_SanitizesFilenameInResponse(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "bbbbbbbb-2222-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
expectActivityInsert(mock)
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"hello world!.pdf": []byte("data")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
Files []struct {
|
||||
Name string `json:"name"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
}
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if len(resp.Files) == 0 || resp.Files[0].Name != "hello_world_.pdf" {
|
||||
t.Errorf("expected sanitized name 'hello_world_.pdf', got: %+v", resp.Files)
|
||||
}
|
||||
if len(store.puts) == 0 || store.puts[0].Filename != "hello_world_.pdf" {
|
||||
t.Errorf("storage Put didn't receive sanitized filename: %+v", store.puts)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPollUpload_AtomicRollbackOnSecondFileTooLarge pins the
|
||||
// transactional contract introduced in phase 5: when one file in a
|
||||
// multi-file batch fails pre-validation (oversize), NONE of the files
|
||||
// in the batch land in storage. Previously a per-file Put loop would
|
||||
// stage rows 1..K-1 before failing on row K, leaving orphan
|
||||
// pending_uploads + activity rows the client would re-create on retry.
|
||||
//
|
||||
// Pinned via inMemStorage's PutBatch (which mirrors PostgresStorage's
|
||||
// Tx-rollback behavior on a per-item validation failure) — but the
|
||||
// real atomicity guarantee is the integration test in
|
||||
// pending_uploads_integration_test.go.
|
||||
func TestPollUpload_AtomicRollbackOnSecondFileTooLarge(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "aaaaaaaa-3333-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
// Two files: first OK, second over the per-file cap. Pre-validation
|
||||
// in uploadPollMode catches it BEFORE any Put — store.puts must
|
||||
// stay empty. (If the test ever sees len=1, the regression is
|
||||
// "first file slipped through into storage on a partial-failure
|
||||
// batch.")
|
||||
tooBig := bytes.Repeat([]byte{0x42}, pendinguploads.MaxFileBytes+1)
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{
|
||||
"ok.txt": []byte("small"),
|
||||
"huge.bin": tooBig,
|
||||
})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusRequestEntityTooLarge {
|
||||
t.Errorf("status=%d body=%s, want 413", w.Code, w.Body.String())
|
||||
}
|
||||
if len(store.puts) != 0 {
|
||||
t.Errorf("expected zero Puts on rollback, got %d: %+v", len(store.puts), store.puts)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPollUpload_AtomicRollbackOnPutBatchError validates that an in-
|
||||
// flight PutBatch failure (e.g. simulated DB error) leaves zero rows
|
||||
// — same guarantee as the pre-validation path, but exercises the
|
||||
// "Tx-Rollback after BEGIN" branch via the fake.
|
||||
func TestPollUpload_AtomicRollbackOnPutBatchError(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "bbbbbbbb-3333-3333-4444-555555555555"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
|
||||
store := newInMemStorage()
|
||||
store.putErr = errors.New("db down mid-batch")
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{
|
||||
"a.txt": []byte("aaa"),
|
||||
"b.txt": []byte("bbb"),
|
||||
"c.txt": []byte("ccc"),
|
||||
})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("status=%d, want 500", w.Code)
|
||||
}
|
||||
if len(store.puts) != 0 {
|
||||
t.Errorf("expected zero Puts after PutBatch error, got %d", len(store.puts))
|
||||
}
|
||||
}
|
||||
|
||||
// TestPollUpload_MimetypeWithCRLFInjectionStripped pins the safeMimetype
|
||||
// hardening: a multipart-supplied Content-Type header with CR/LF is
|
||||
// rewritten to application/octet-stream so the eventual /content
|
||||
// response can't be header-split on the wire.
|
||||
func TestPollUpload_MimetypeWithCRLFInjectionStripped(t *testing.T) {
|
||||
got := safeMimetype("text/html\r\nX-Injected: pwn")
|
||||
if got != "application/octet-stream" {
|
||||
t.Errorf("CRLF mimetype not stripped, got %q", got)
|
||||
}
|
||||
got = safeMimetype("image/png\x00")
|
||||
if got != "application/octet-stream" {
|
||||
t.Errorf("NUL byte mimetype not stripped, got %q", got)
|
||||
}
|
||||
got = safeMimetype("text/plain; charset=utf-8")
|
||||
if got != "text/plain" {
|
||||
t.Errorf("parameter not stripped, got %q", got)
|
||||
}
|
||||
got = safeMimetype("application/pdf")
|
||||
if got != "application/pdf" {
|
||||
t.Errorf("clean mime modified, got %q", got)
|
||||
}
|
||||
got = safeMimetype("")
|
||||
if got != "" {
|
||||
t.Errorf("empty input should pass through, got %q", got)
|
||||
}
|
||||
got = safeMimetype("notamime")
|
||||
if got != "application/octet-stream" {
|
||||
t.Errorf("non-type/subtype not coerced, got %q", got)
|
||||
}
|
||||
got = safeMimetype("/empty-type")
|
||||
if got != "application/octet-stream" {
|
||||
t.Errorf("missing type half not coerced, got %q", got)
|
||||
}
|
||||
got = safeMimetype("type/")
|
||||
if got != "application/octet-stream" {
|
||||
t.Errorf("missing subtype half not coerced, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPollUpload_ActivityRowDiscriminator pins the
|
||||
// activity_type / method shape that the workspace inbox poller depends
|
||||
// on. The poller filters `GET /workspaces/:id/activity?type=a2a_receive`
|
||||
// so the handler MUST write activity_type=a2a_receive (NOT a custom
|
||||
// type), and use method=chat_upload_receive as the
|
||||
// upload-vs-message-vs-task discriminator.
|
||||
//
|
||||
// Why pinned: a previous iteration of this handler used
|
||||
// activity_type="chat_upload_receive" — silently invisible to the
|
||||
// existing poller. The branch passed every push-mode test, every
|
||||
// storage test, and every per-file content test; the bug only
|
||||
// surfaced at runtime when the workspace polled and got nothing.
|
||||
// Encode the contract in a unit test so the next refactor can't
|
||||
// re-break it without a red CI.
|
||||
func TestPollUpload_ActivityRowDiscriminator(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "abc12345-6789-4abc-8def-000000000999"
|
||||
expectPollDeliveryMode(mock, wsID, "poll")
|
||||
expectActivityInsertWithTypeAndMethod(mock, wsID, "a2a_receive", "chat_upload_receive")
|
||||
|
||||
store := newInMemStorage()
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil)).
|
||||
WithPendingUploads(store, nil)
|
||||
|
||||
body, ct := pollUploadFixture(t, map[string][]byte{"x.pdf": []byte("xx")})
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("expectations: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -58,16 +58,38 @@ func uploadFixture(t *testing.T) (*bytes.Buffer, string) {
|
||||
return &buf, mw.FormDataContentType()
|
||||
}
|
||||
|
||||
// expectURL stubs the SELECT that resolves the workspace's url.
|
||||
// expectURL stubs the SELECT that resolves the workspace's url +
|
||||
// delivery_mode. Defaults delivery_mode to "push" — most tests don't
|
||||
// care about the mode and just want a URL to forward to. Use
|
||||
// expectURLAndMode when the test needs a specific mode (e.g. the
|
||||
// poll-mode 422 path).
|
||||
func expectURL(mock sqlmock.Sqlmock, workspaceID, url string) {
|
||||
mock.ExpectQuery(`SELECT COALESCE\(url, ''\) FROM workspaces WHERE id = \$1`).
|
||||
expectURLAndMode(mock, workspaceID, url, "push")
|
||||
}
|
||||
|
||||
// expectURLAndMode is the explicit form for tests that need to
|
||||
// exercise the delivery_mode branch (e.g. poll-mode workspaces get
|
||||
// a 422 instead of a 503 when URL is empty — the platform can't
|
||||
// dispatch to a non-push workspace at all).
|
||||
func expectURLAndMode(mock sqlmock.Sqlmock, workspaceID, url, mode string) {
|
||||
mock.ExpectQuery(`SELECT COALESCE\(url, ''\), delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(workspaceID).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"url"}).AddRow(url))
|
||||
WillReturnRows(sqlmock.NewRows([]string{"url", "delivery_mode"}).AddRow(url, mode))
|
||||
}
|
||||
|
||||
// expectURLNullMode is the production-observed shape: external runtime
|
||||
// workspaces (molecule-sdk-python on user infra) register with
|
||||
// delivery_mode = NULL, not "poll". Caught 2026-05-04 — the narrow
|
||||
// "poll" check missed three of three real workspaces in user reports.
|
||||
func expectURLNullMode(mock sqlmock.Sqlmock, workspaceID, url string) {
|
||||
mock.ExpectQuery(`SELECT COALESCE\(url, ''\), delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(workspaceID).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"url", "delivery_mode"}).AddRow(url, nil))
|
||||
}
|
||||
|
||||
// expectURLMissing stubs the SELECT to return sql.ErrNoRows.
|
||||
func expectURLMissing(mock sqlmock.Sqlmock, workspaceID string) {
|
||||
mock.ExpectQuery(`SELECT COALESCE\(url, ''\) FROM workspaces WHERE id = \$1`).
|
||||
mock.ExpectQuery(`SELECT COALESCE\(url, ''\), delivery_mode FROM workspaces WHERE id = \$1`).
|
||||
WithArgs(workspaceID).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
}
|
||||
@@ -83,7 +105,7 @@ func TestChatUpload_InvalidWorkspaceID(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
|
||||
c, w := makeUploadRequest(t, "not-a-uuid", &bytes.Buffer{}, "")
|
||||
h.Upload(c)
|
||||
@@ -100,7 +122,7 @@ func TestChatUpload_WorkspaceNotInDB(t *testing.T) {
|
||||
wsID := "00000000-0000-0000-0000-000000000099"
|
||||
expectURLMissing(mock, wsID)
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
@@ -144,7 +166,7 @@ func TestChatUpload_NoInboundSecret_LazyHeal(t *testing.T) {
|
||||
WithArgs(sqlmock.AnyArg(), wsID).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
@@ -181,7 +203,7 @@ func TestChatUpload_NoInboundSecret_LazyHealFailure(t *testing.T) {
|
||||
WithArgs(sqlmock.AnyArg(), wsID).
|
||||
WillReturnError(sql.ErrConnDone) // mint fails
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
@@ -201,17 +223,79 @@ func TestChatUpload_NoURL(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
// Workspace registered but URL hasn't been reported yet (mid-boot).
|
||||
// Workspace registered (push-mode) but URL hasn't been reported
|
||||
// yet (mid-boot). 503 + "not registered yet" is the right surface — the
|
||||
// canvas client can retry after the next heartbeat picks up the URL.
|
||||
// Push mode is the only branch that produces 503; everything else
|
||||
// (poll, NULL, empty) gets 422 because no amount of waiting helps.
|
||||
wsID := "00000000-0000-0000-0000-000000000042"
|
||||
expectURL(mock, wsID, "")
|
||||
expectURLAndMode(mock, wsID, "", "push")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Errorf("expected 503 when workspace url empty, got %d: %s", w.Code, w.Body.String())
|
||||
t.Errorf("expected 503 when workspace url empty (push mode), got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "not registered yet") {
|
||||
t.Errorf("expected transient-state error message, got: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatUpload_PollModeEmptyURL pins the 422 distinguisher: a
|
||||
// poll-mode workspace has no URL by design, so chat upload (which is
|
||||
// HTTP-forward to the workspace) cannot succeed by retrying. Returning
|
||||
// 503 here would loop the canvas client forever; 422 + an actionable
|
||||
// message tells the user what to do.
|
||||
func TestChatUpload_PollModeEmptyURL(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "00000000-0000-0000-0000-000000000099"
|
||||
expectURLAndMode(mock, wsID, "", "poll")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("expected 422 for poll-mode upload, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "push") {
|
||||
t.Errorf("expected error to suggest push mode, got: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatUpload_NullModeEmptyURL — production-observed 2026-05-04:
|
||||
// external-runtime workspaces (molecule-sdk-python on user infra)
|
||||
// register with delivery_mode = NULL, not "poll". The earlier narrow
|
||||
// poll-only check fell through to the misleading 503. The fix is the
|
||||
// inverse-of-push test: anything not exactly "push" with empty URL
|
||||
// can't dispatch and gets the actionable 422.
|
||||
//
|
||||
// Three of three external workspaces in the user's tenant had this
|
||||
// shape (home hermes / runner mac mini / mac laptop, all
|
||||
// runtime=external + url='' + delivery_mode=NULL).
|
||||
func TestChatUpload_NullModeEmptyURL(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
wsID := "30ba7f0b-b303-4a20-aefe-3a4a675b8aa4" // user's "mac laptop"
|
||||
expectURLNullMode(mock, wsID, "")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
|
||||
if w.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("expected 422 for null-delivery-mode upload, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "callback URL") {
|
||||
t.Errorf("expected error to mention callback URL, got: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,7 +338,7 @@ func TestChatUpload_ForwardsToWorkspace_HappyPath(t *testing.T) {
|
||||
expectURL(mock, wsID, srv.URL)
|
||||
expectInboundSecret(mock, wsID, "super-secret-123")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
@@ -296,7 +380,7 @@ func TestChatUpload_ForwardsErrorStatusUnchanged(t *testing.T) {
|
||||
expectURL(mock, wsID, srv.URL)
|
||||
expectInboundSecret(mock, wsID, "tok")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
@@ -318,7 +402,7 @@ func TestChatUpload_WorkspaceUnreachable(t *testing.T) {
|
||||
expectURL(mock, wsID, "http://127.0.0.1:1")
|
||||
expectInboundSecret(mock, wsID, "tok")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
body, ct := uploadFixture(t)
|
||||
c, w := makeUploadRequest(t, wsID, body, ct)
|
||||
h.Upload(c)
|
||||
@@ -334,7 +418,7 @@ func TestChatDownload_InvalidPath(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
|
||||
cases := []struct {
|
||||
name, path, wantSubstr string
|
||||
@@ -423,7 +507,7 @@ func TestChatDownload_WorkspaceNotInDB(t *testing.T) {
|
||||
WithArgs(wsID).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
c, w := makeDownloadRequest(t, wsID, "/workspace/foo.txt")
|
||||
h.Download(c)
|
||||
|
||||
@@ -449,7 +533,7 @@ func TestChatDownload_NoInboundSecret_LazyHeal(t *testing.T) {
|
||||
WithArgs(sqlmock.AnyArg(), wsID).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
c, w := makeDownloadRequest(t, wsID, "/workspace/foo.txt")
|
||||
h.Download(c)
|
||||
|
||||
@@ -475,7 +559,7 @@ func TestChatDownload_NoInboundSecret_LazyHealFailure(t *testing.T) {
|
||||
WithArgs(sqlmock.AnyArg(), wsID).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
c, w := makeDownloadRequest(t, wsID, "/workspace/foo.txt")
|
||||
h.Download(c)
|
||||
|
||||
@@ -508,7 +592,7 @@ func TestChatDownload_ForwardsToWorkspace_HappyPath(t *testing.T) {
|
||||
expectURL(mock, wsID, srv.URL)
|
||||
expectInboundSecret(mock, wsID, "the-secret")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
c, w := makeDownloadRequest(t, wsID, "/workspace/report.txt")
|
||||
h.Download(c)
|
||||
|
||||
@@ -550,7 +634,7 @@ func TestChatDownload_404FromWorkspacePropagated(t *testing.T) {
|
||||
expectURL(mock, wsID, srv.URL)
|
||||
expectInboundSecret(mock, wsID, "tok")
|
||||
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil))
|
||||
h := NewChatFilesHandler(NewTemplatesHandler(t.TempDir(), nil, nil))
|
||||
c, w := makeDownloadRequest(t, wsID, "/workspace/missing.txt")
|
||||
h.Download(c)
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
@@ -13,6 +14,68 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// delegationResultInboxPushEnabled gates the RFC #2829 PR-2 result-push
|
||||
// behavior: when callee POSTs `status=completed` (or `failed`) via
|
||||
// /workspaces/:id/delegations/:delegation_id/update, ALSO write an
|
||||
// `activity_type='a2a_receive'` row to the caller's activity_logs.
|
||||
//
|
||||
// Why a flag: the caller's inbox poller (workspace/inbox.py) queries
|
||||
// `?type=a2a_receive` to surface inbound messages to the agent. Adding
|
||||
// a2a_receive rows for delegation results is the universal-sized fix for
|
||||
// the 600s message/send timeout class — long-running delegations no
|
||||
// longer rely on the proxy holding the HTTP connection open. But it is
|
||||
// observable behavior change (existing agents start seeing delegation
|
||||
// results in their inbox where they didn't before), so we flag it for
|
||||
// staging burn-in before flipping default.
|
||||
//
|
||||
// Default: off. Staging-canary first; flip to on after RFC #2829 PR-3
|
||||
// (agent-side cutover) lands and proves the round-trip end-to-end.
|
||||
func delegationResultInboxPushEnabled() bool {
|
||||
return os.Getenv("DELEGATION_RESULT_INBOX_PUSH") == "1"
|
||||
}
|
||||
|
||||
// pushDelegationResultToInbox writes the inbox-visible row for a
|
||||
// completed/failed delegation. Best-effort: a failure logs but does NOT
|
||||
// fail the parent UpdateStatus — the existing delegate_result row in
|
||||
// activity_logs is still authoritative for the dashboard.
|
||||
//
|
||||
// Caller (sourceID) is the workspace that initiated the delegation; the
|
||||
// inbox row lands in their activity_logs so wait_for_message picks it up.
|
||||
//
|
||||
// Body shape mirrors a2a_receive rows produced by the proxy on a
|
||||
// successful synchronous reply: response_body.text carries the agent's
|
||||
// answer, request_body.delegation_id correlates back to the originating
|
||||
// row.
|
||||
func pushDelegationResultToInbox(ctx context.Context, sourceID, delegationID, status, responsePreview, errorDetail string) {
|
||||
if !delegationResultInboxPushEnabled() {
|
||||
return
|
||||
}
|
||||
respPayload := map[string]interface{}{
|
||||
"text": responsePreview,
|
||||
"delegation_id": delegationID,
|
||||
}
|
||||
respJSON, _ := json.Marshal(respPayload)
|
||||
reqJSON, _ := json.Marshal(map[string]interface{}{
|
||||
"delegation_id": delegationID,
|
||||
})
|
||||
logStatus := "ok"
|
||||
if status == "failed" {
|
||||
logStatus = "error"
|
||||
}
|
||||
summary := "Delegation result delivered"
|
||||
if status == "failed" {
|
||||
summary = "Delegation failed"
|
||||
}
|
||||
if _, err := db.DB.ExecContext(ctx, `
|
||||
INSERT INTO activity_logs (
|
||||
workspace_id, activity_type, method, source_id,
|
||||
summary, request_body, response_body, status, error_detail
|
||||
) VALUES ($1, 'a2a_receive', 'delegate_result', $2, $3, $4::jsonb, $5::jsonb, $6, NULLIF($7, ''))
|
||||
`, sourceID, sourceID, summary, string(reqJSON), string(respJSON), logStatus, errorDetail); err != nil {
|
||||
log.Printf("Delegation %s: inbox-push insert failed: %v", delegationID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegation status lifecycle:
|
||||
// pending → dispatched → received → in_progress → completed | failed
|
||||
//
|
||||
@@ -206,6 +269,9 @@ func insertDelegationRow(ctx context.Context, c *gin.Context, sourceID string, b
|
||||
VALUES ($1, 'delegation', 'delegate', $2, $3, $4, $5::jsonb, 'pending', $6)
|
||||
`, sourceID, sourceID, body.TargetID, "Delegating to "+body.TargetID, string(taskJSON), idemArg)
|
||||
if err == nil {
|
||||
// RFC #2829 #318 — mirror to the durable delegations ledger
|
||||
// (gated by DELEGATION_LEDGER_WRITE; default off → no-op).
|
||||
recordLedgerInsert(ctx, sourceID, body.TargetID, delegationID, body.Task, body.IdempotencyKey)
|
||||
return insertOK
|
||||
}
|
||||
// A unique-constraint hit means a concurrent request just took the
|
||||
@@ -289,6 +355,8 @@ func (h *DelegationHandler) executeDelegation(sourceID, targetID, delegationID s
|
||||
h.broadcaster.RecordAndBroadcast(ctx, "DELEGATION_FAILED", sourceID, map[string]interface{}{
|
||||
"delegation_id": delegationID, "target_id": targetID, "error": proxyErr.Error(),
|
||||
})
|
||||
// RFC #2829 PR-2 result-push (see UpdateStatus for rationale).
|
||||
pushDelegationResultToInbox(ctx, sourceID, delegationID, "failed", "", proxyErr.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -343,17 +411,28 @@ func (h *DelegationHandler) executeDelegation(sourceID, targetID, delegationID s
|
||||
log.Printf("Delegation %s: failed to insert success log: %v", delegationID, err)
|
||||
}
|
||||
|
||||
// RFC #2829 #318: write the ledger row with result_preview FIRST,
|
||||
// THEN updateDelegationStatus. Order matters: SetStatus has a
|
||||
// same-status replay no-op — if updateDelegationStatus's nested
|
||||
// recordLedgerStatus(completed, "", "") fires first, the outer call
|
||||
// hits the no-op branch and result_preview is never written.
|
||||
// Caught by the local-Postgres integration test in
|
||||
// delegation_ledger_integration_test.go.
|
||||
recordLedgerStatus(ctx, delegationID, "completed", "", responseText)
|
||||
h.updateDelegationStatus(sourceID, delegationID, "completed", "")
|
||||
h.broadcaster.RecordAndBroadcast(ctx, "DELEGATION_COMPLETE", sourceID, map[string]interface{}{
|
||||
"delegation_id": delegationID,
|
||||
"target_id": targetID,
|
||||
"response_preview": truncate(responseText, 200),
|
||||
})
|
||||
// RFC #2829 PR-2 result-push (see UpdateStatus for rationale).
|
||||
pushDelegationResultToInbox(ctx, sourceID, delegationID, "completed", responseText, "")
|
||||
}
|
||||
|
||||
// updateDelegationStatus updates the status of a delegation record in activity_logs.
|
||||
func (h *DelegationHandler) updateDelegationStatus(workspaceID, delegationID, status, errorDetail string) {
|
||||
if _, err := db.DB.ExecContext(context.Background(), `
|
||||
ctx := context.Background()
|
||||
if _, err := db.DB.ExecContext(ctx, `
|
||||
UPDATE activity_logs
|
||||
SET status = $1, error_detail = CASE WHEN $2 = '' THEN error_detail ELSE $2 END
|
||||
WHERE workspace_id = $3
|
||||
@@ -362,6 +441,14 @@ func (h *DelegationHandler) updateDelegationStatus(workspaceID, delegationID, st
|
||||
`, status, errorDetail, workspaceID, delegationID); err != nil {
|
||||
log.Printf("Delegation %s: status update failed: %v", delegationID, err)
|
||||
}
|
||||
// RFC #2829 #318 — mirror status transition to the durable ledger
|
||||
// (gated). Note: the ledger uses different vocabulary for "pending"
|
||||
// (its initial state is `queued`); map "received" / unknown values
|
||||
// the ledger doesn't accept by skipping them rather than failing.
|
||||
switch status {
|
||||
case "queued", "dispatched", "in_progress", "completed", "failed", "stuck":
|
||||
recordLedgerStatus(ctx, delegationID, status, errorDetail, "")
|
||||
}
|
||||
}
|
||||
|
||||
// Record handles POST /workspaces/:id/delegations/record — the agent-initiated
|
||||
@@ -407,6 +494,15 @@ func (h *DelegationHandler) Record(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// RFC #2829 #318 — mirror to durable ledger (gated). Record always
|
||||
// reflects an A2A request the agent already fired itself, so the
|
||||
// initial activity_logs status is 'dispatched' — but the ledger's
|
||||
// CHECK constraint only accepts 'queued' as the initial state via
|
||||
// Insert. Insert as queued first; the very next SetStatus(...,
|
||||
// dispatched) below promotes it to dispatched on the same row.
|
||||
recordLedgerInsert(ctx, sourceID, body.TargetID, body.DelegationID, body.Task, "")
|
||||
recordLedgerStatus(ctx, body.DelegationID, "dispatched", "", "")
|
||||
|
||||
h.broadcaster.RecordAndBroadcast(ctx, "DELEGATION_SENT", sourceID, map[string]interface{}{
|
||||
"delegation_id": body.DelegationID,
|
||||
"target_id": body.TargetID,
|
||||
@@ -442,6 +538,13 @@ func (h *DelegationHandler) UpdateStatus(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// RFC #2829 #318 — same ordering pin as executeDelegation completion:
|
||||
// write the with-preview ledger row FIRST so updateDelegationStatus's
|
||||
// inner same-status no-op doesn't clobber preview.
|
||||
if body.Status == "completed" {
|
||||
recordLedgerStatus(ctx, delegationID, "completed", "", body.ResponsePreview)
|
||||
}
|
||||
|
||||
h.updateDelegationStatus(sourceID, delegationID, body.Status, body.Error)
|
||||
|
||||
if body.Status == "completed" {
|
||||
@@ -459,11 +562,19 @@ func (h *DelegationHandler) UpdateStatus(c *gin.Context) {
|
||||
"delegation_id": delegationID,
|
||||
"response_preview": truncate(body.ResponsePreview, 200),
|
||||
})
|
||||
// RFC #2829 PR-2 result-push: when the gate is on, also write an
|
||||
// a2a_receive row so the caller's inbox poller surfaces this to
|
||||
// the agent. Foundational for getting rid of the proxy-blocked
|
||||
// sync path that hits the 600s message/send timeout — once the
|
||||
// agent-side cutover lands, the caller polls its own inbox for
|
||||
// the result instead of holding open an HTTP connection.
|
||||
pushDelegationResultToInbox(ctx, sourceID, delegationID, "completed", body.ResponsePreview, "")
|
||||
} else {
|
||||
h.broadcaster.RecordAndBroadcast(ctx, "DELEGATION_FAILED", sourceID, map[string]interface{}{
|
||||
"delegation_id": delegationID,
|
||||
"error": body.Error,
|
||||
})
|
||||
pushDelegationResultToInbox(ctx, sourceID, delegationID, "failed", "", body.Error)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": body.Status, "delegation_id": delegationID})
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// delegation_inbox_push_test.go — coverage for the RFC #2829 PR-2
|
||||
// result-push behavior. The push is feature-flagged via
|
||||
// DELEGATION_RESULT_INBOX_PUSH=1; default off keeps the existing
|
||||
// strict-sqlmock test surface unchanged.
|
||||
//
|
||||
// What we pin:
|
||||
// 1. Flag off (default) → no a2a_receive INSERT fires.
|
||||
// 2. Flag on, status=completed → a2a_receive row written with the
|
||||
// response_preview and no error_detail.
|
||||
// 3. Flag on, status=failed → a2a_receive row written with status=error
|
||||
// and the error_detail set.
|
||||
// 4. INSERT failure on inbox-push does NOT bubble up — UpdateStatus
|
||||
// still returns 200.
|
||||
|
||||
// ---------- pushDelegationResultToInbox in isolation ----------
|
||||
|
||||
func TestPushDelegationResultToInbox_FlagOff_NoSQL(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
t.Setenv("DELEGATION_RESULT_INBOX_PUSH", "")
|
||||
|
||||
pushDelegationResultToInbox(
|
||||
context.Background(),
|
||||
"caller", "deleg-1", "completed", "answer body", "",
|
||||
)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("flag off must not fire SQL: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushDelegationResultToInbox_FlagOn_CompletedInsertsA2AReceiveRow(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
t.Setenv("DELEGATION_RESULT_INBOX_PUSH", "1")
|
||||
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
"caller-ws",
|
||||
"caller-ws", // source_id mirrors workspace_id
|
||||
"Delegation result delivered",
|
||||
sqlmock.AnyArg(), // request_body json
|
||||
sqlmock.AnyArg(), // response_body json
|
||||
"ok",
|
||||
"", // error_detail empty for completed
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
pushDelegationResultToInbox(
|
||||
context.Background(),
|
||||
"caller-ws", "deleg-1", "completed", "answer body", "",
|
||||
)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushDelegationResultToInbox_FlagOn_FailedInsertsErrorRow(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
t.Setenv("DELEGATION_RESULT_INBOX_PUSH", "1")
|
||||
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
"caller-ws",
|
||||
"caller-ws",
|
||||
"Delegation failed",
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
"error",
|
||||
"target unreachable",
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
pushDelegationResultToInbox(
|
||||
context.Background(),
|
||||
"caller-ws", "deleg-2", "failed", "", "target unreachable",
|
||||
)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- UpdateStatus end-to-end ----------
|
||||
|
||||
func TestUpdateStatus_FlagOn_PushesA2AReceiveOnCompleted(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
t.Setenv("DELEGATION_RESULT_INBOX_PUSH", "1")
|
||||
|
||||
broadcaster := newTestBroadcaster()
|
||||
wh := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
dh := NewDelegationHandler(wh, broadcaster)
|
||||
|
||||
// 1. updateDelegationStatus — UPDATE activity_logs SET status='completed'
|
||||
mock.ExpectExec(`UPDATE activity_logs`).
|
||||
WithArgs("completed", "", "ws-source", "deleg-9").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
// 2. existing delegate_result INSERT (caller-side dashboard view)
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
"ws-source", "ws-source",
|
||||
sqlmock.AnyArg(), // summary
|
||||
sqlmock.AnyArg(), // response_body
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
// 3. NEW: PR-2 a2a_receive row for inbox-poller
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
"ws-source", "ws-source",
|
||||
"Delegation result delivered",
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
"ok",
|
||||
"",
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{
|
||||
{Key: "id", Value: "ws-source"},
|
||||
{Key: "delegation_id", Value: "deleg-9"},
|
||||
}
|
||||
body := `{"status":"completed","response_preview":"all done"}`
|
||||
c.Request = httptest.NewRequest("POST",
|
||||
"/workspaces/ws-source/delegations/deleg-9/update",
|
||||
bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
dh.UpdateStatus(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStatus_FlagOn_PushesA2AReceiveOnFailed(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
t.Setenv("DELEGATION_RESULT_INBOX_PUSH", "1")
|
||||
|
||||
broadcaster := newTestBroadcaster()
|
||||
wh := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
dh := NewDelegationHandler(wh, broadcaster)
|
||||
|
||||
// 1. updateDelegationStatus — UPDATE activity_logs
|
||||
mock.ExpectExec(`UPDATE activity_logs`).
|
||||
WithArgs("failed", "boom", "ws-source", "deleg-10").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
// 2. NEW: PR-2 a2a_receive row for inbox-poller (failure path doesn't
|
||||
// have the existing delegate_result INSERT — only the new push).
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
"ws-source", "ws-source",
|
||||
"Delegation failed",
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
"error",
|
||||
"boom",
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{
|
||||
{Key: "id", Value: "ws-source"},
|
||||
{Key: "delegation_id", Value: "deleg-10"},
|
||||
}
|
||||
body := `{"status":"failed","error":"boom"}`
|
||||
c.Request = httptest.NewRequest("POST",
|
||||
"/workspaces/ws-source/delegations/deleg-10/update",
|
||||
bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
dh.UpdateStatus(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d", w.Code)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateStatus_FlagOff_NoNewSQL — sanity check that the existing
|
||||
// behavior is preserved when the flag is off. Critical for safe rollout.
|
||||
func TestUpdateStatus_FlagOff_NoNewSQL(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
// explicitly empty — flag off
|
||||
t.Setenv("DELEGATION_RESULT_INBOX_PUSH", "")
|
||||
|
||||
broadcaster := newTestBroadcaster()
|
||||
wh := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
dh := NewDelegationHandler(wh, broadcaster)
|
||||
|
||||
// Only the two pre-existing queries — no third (a2a_receive) INSERT.
|
||||
mock.ExpectExec(`UPDATE activity_logs`).
|
||||
WithArgs("completed", "", "ws-source", "deleg-11").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectExec(`INSERT INTO activity_logs`).
|
||||
WithArgs(
|
||||
"ws-source", "ws-source",
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{
|
||||
{Key: "id", Value: "ws-source"},
|
||||
{Key: "delegation_id", Value: "deleg-11"},
|
||||
}
|
||||
c.Request = httptest.NewRequest("POST",
|
||||
"/workspaces/ws-source/delegations/deleg-11/update",
|
||||
bytes.NewBufferString(`{"status":"completed","response_preview":"ok"}`))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
dh.UpdateStatus(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d", w.Code)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("flag-off must not fire extra SQL: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
)
|
||||
|
||||
// delegation_ledger.go — durable per-task ledger for A2A delegation
|
||||
// (RFC #2829 PR-1).
|
||||
//
|
||||
// activity_logs is an event stream — one row per state transition. Replaying
|
||||
// the stream gives you history. This file's table (delegations) is the
|
||||
// folded current state — one row per delegation_id with a single status,
|
||||
// last_heartbeat, deadline, and result_preview.
|
||||
//
|
||||
// Why both: PR-3 needs a sweeper that joins on
|
||||
// (status='in_progress' AND last_heartbeat < now() - interval '10 minutes')
|
||||
// which is impossible to express against the event stream without a window
|
||||
// function over every (delegation_id, latest event) pair — a planner-killing
|
||||
// query at scale. The dedicated table makes the sweeper an indexed scan.
|
||||
//
|
||||
// Writes go to BOTH tables. activity_logs remains the audit-grade record
|
||||
// for forensics; delegations is the queryable view for dashboards + sweeper
|
||||
// joins. Symmetric-write pattern — same posture as tenant_resources (PR
|
||||
// #2343), per memory `reference_tenant_resources_audit`.
|
||||
|
||||
// DelegationLedger writes the per-task durable row alongside the existing
|
||||
// activity_logs event-stream writes. All methods are best-effort: a ledger
|
||||
// write failure logs but does NOT propagate up — activity_logs remains the
|
||||
// audit-grade source of truth.
|
||||
//
|
||||
// Same shape as `tenant_resources` reconciler (PR #2343): orchestration
|
||||
// continues even when the ledger write fails, and the next status update
|
||||
// (or PR-3 reconciler) will heal the ledger.
|
||||
type DelegationLedger struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// NewDelegationLedger returns a ledger backed by the package db handle.
|
||||
// Tests can construct one with a sqlmock-backed *sql.DB.
|
||||
func NewDelegationLedger(handle *sql.DB) *DelegationLedger {
|
||||
if handle == nil {
|
||||
handle = db.DB
|
||||
}
|
||||
return &DelegationLedger{db: handle}
|
||||
}
|
||||
|
||||
// truncatePreview caps stored preview at 4KB. The full prompt/response is
|
||||
// already in activity_logs.{request,response}_body — this is the at-a-glance
|
||||
// view for the dashboard, not a forensic record.
|
||||
const previewCap = 4096
|
||||
|
||||
func truncatePreview(s string) string {
|
||||
if len(s) <= previewCap {
|
||||
return s
|
||||
}
|
||||
return s[:previewCap]
|
||||
}
|
||||
|
||||
// InsertOpts is the agent's record-of-intent. Caller, callee, task preview,
|
||||
// and the chosen delegation_id are required; idempotency_key is optional.
|
||||
type InsertOpts struct {
|
||||
DelegationID string
|
||||
CallerID string
|
||||
CalleeID string
|
||||
TaskPreview string
|
||||
IdempotencyKey string // empty → NULL
|
||||
// Deadline defaults to now + 6h when zero. Callers can pass a tighter
|
||||
// per-task deadline (cron, interactive request) by setting it.
|
||||
Deadline time.Time
|
||||
}
|
||||
|
||||
// Insert writes the queued row. ON CONFLICT (delegation_id) DO NOTHING so
|
||||
// the agent's retry-on-restart codepath is naturally idempotent — a duplicate
|
||||
// Insert with the same delegation_id is a no-op. (Idempotency_key dedupe is
|
||||
// a separate UNIQUE index handled by the same DO NOTHING.)
|
||||
func (l *DelegationLedger) Insert(ctx context.Context, opts InsertOpts) {
|
||||
if opts.DelegationID == "" || opts.CallerID == "" || opts.CalleeID == "" {
|
||||
log.Printf("delegation_ledger Insert: missing required field, skipping")
|
||||
return
|
||||
}
|
||||
deadline := opts.Deadline
|
||||
if deadline.IsZero() {
|
||||
deadline = time.Now().Add(6 * time.Hour)
|
||||
}
|
||||
idemArg := sql.NullString{String: opts.IdempotencyKey, Valid: opts.IdempotencyKey != ""}
|
||||
_, err := l.db.ExecContext(ctx, `
|
||||
INSERT INTO delegations (
|
||||
delegation_id, caller_id, callee_id, task_preview,
|
||||
status, deadline, idempotency_key
|
||||
) VALUES ($1, $2, $3, $4, 'queued', $5, $6)
|
||||
ON CONFLICT (delegation_id) DO NOTHING
|
||||
`, opts.DelegationID, opts.CallerID, opts.CalleeID,
|
||||
truncatePreview(opts.TaskPreview), deadline, idemArg)
|
||||
if err != nil {
|
||||
log.Printf("delegation_ledger Insert(%s): %v", opts.DelegationID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// allowedTransitions enforces the lifecycle in code as defense-in-depth on
|
||||
// the schema CHECK. Terminal states (completed, failed, stuck) reject any
|
||||
// further status update — once a delegation is done, it stays done.
|
||||
//
|
||||
// The "queued → in_progress" jump (skipping dispatched) is allowed: lazy
|
||||
// callers that don't ack the dispatched stage shouldn't be penalised,
|
||||
// since the agent ultimately cares about whether work started, not which
|
||||
// HTTP layer happened to ack first.
|
||||
var allowedTransitions = map[string]map[string]bool{
|
||||
"queued": {"dispatched": true, "in_progress": true, "failed": true},
|
||||
"dispatched": {"in_progress": true, "completed": true, "failed": true},
|
||||
"in_progress": {"completed": true, "failed": true, "stuck": true},
|
||||
}
|
||||
|
||||
// ErrInvalidTransition is returned by SetStatus when the transition would
|
||||
// move out of a terminal state. Callers SHOULD ignore (it's a duplicate
|
||||
// terminal write) but they're surfaced for tests.
|
||||
var ErrInvalidTransition = errors.New("delegation ledger: invalid status transition")
|
||||
|
||||
// SetStatus is the catch-all updater. Status MUST be one of the lifecycle
|
||||
// values. errorDetail is non-empty only for failed/stuck. resultPreview is
|
||||
// non-empty only for completed.
|
||||
//
|
||||
// Idempotent: re-applying the same terminal status with the same payload
|
||||
// returns nil; transitioning back out of a terminal state returns
|
||||
// ErrInvalidTransition. (Forward-only protection — once 'completed' you
|
||||
// don't get to revise to 'failed'.)
|
||||
func (l *DelegationLedger) SetStatus(ctx context.Context,
|
||||
delegationID, status, errorDetail, resultPreview string,
|
||||
) error {
|
||||
if delegationID == "" || status == "" {
|
||||
return errors.New("delegation ledger: missing required field")
|
||||
}
|
||||
|
||||
// Read current status to validate the transition. We accept the rare
|
||||
// race where two updaters both observe the same prior status — Postgres
|
||||
// CHECK constraint catches truly-invalid status values; our forward-only
|
||||
// check is best-effort.
|
||||
var current string
|
||||
err := l.db.QueryRowContext(ctx,
|
||||
`SELECT status FROM delegations WHERE delegation_id = $1`,
|
||||
delegationID,
|
||||
).Scan(¤t)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
// Insert was lost or wasn't called. Defensively NO-OP — the next
|
||||
// agent retry will re-Insert and the next SetStatus will land.
|
||||
log.Printf("delegation_ledger SetStatus(%s, %s): row missing, skipping",
|
||||
delegationID, status)
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Same-status replay (e.g. duplicate completion notification): no-op,
|
||||
// don't bump updated_at, no error.
|
||||
if current == status {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Forward-only on terminal states.
|
||||
if next, ok := allowedTransitions[current]; !ok || !next[status] {
|
||||
// Terminal already — refuse to revise.
|
||||
return ErrInvalidTransition
|
||||
}
|
||||
|
||||
_, err = l.db.ExecContext(ctx, `
|
||||
UPDATE delegations
|
||||
SET status = $2,
|
||||
error_detail = NULLIF($3, ''),
|
||||
result_preview = NULLIF($4, ''),
|
||||
updated_at = now()
|
||||
WHERE delegation_id = $1
|
||||
`, delegationID, status, errorDetail, truncatePreview(resultPreview))
|
||||
return err
|
||||
}
|
||||
|
||||
// Heartbeat stamps last_heartbeat = now() for an in-flight delegation. Used
|
||||
// by the callee whenever it makes progress; PR-3's sweeper compares to
|
||||
// NOW() to decide stuckness. No-op on terminal-state delegations.
|
||||
//
|
||||
// Best-effort: failure logs but doesn't propagate.
|
||||
func (l *DelegationLedger) Heartbeat(ctx context.Context, delegationID string) {
|
||||
if delegationID == "" {
|
||||
return
|
||||
}
|
||||
_, err := l.db.ExecContext(ctx, `
|
||||
UPDATE delegations
|
||||
SET last_heartbeat = now(), updated_at = now()
|
||||
WHERE delegation_id = $1
|
||||
AND status NOT IN ('completed','failed','stuck')
|
||||
`, delegationID)
|
||||
if err != nil {
|
||||
log.Printf("delegation_ledger Heartbeat(%s): %v", delegationID, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
// delegation_ledger_integration_test.go — REAL Postgres integration tests
|
||||
// for the RFC #2829 ledger writes.
|
||||
//
|
||||
// Run with:
|
||||
//
|
||||
// docker run --rm -d --name pg-integration \
|
||||
// -e POSTGRES_PASSWORD=test -e POSTGRES_DB=molecule \
|
||||
// -p 55432:5432 postgres:15-alpine
|
||||
// sleep 4
|
||||
// psql ... < workspace-server/migrations/049_delegations.up.sql
|
||||
// cd workspace-server
|
||||
// INTEGRATION_DB_URL="postgres://postgres:test@localhost:55432/molecule?sslmode=disable" \
|
||||
// go test -tags=integration ./internal/handlers/ -run Integration_
|
||||
//
|
||||
// CI (.github/workflows/handlers-postgres-integration.yml) runs this on
|
||||
// every PR that touches workspace-server/internal/handlers/**.
|
||||
//
|
||||
// Why these are NOT plain unit tests
|
||||
// ----------------------------------
|
||||
// The strict-sqlmock unit tests in delegation_ledger_writes_test.go pin
|
||||
// which SQL statements fire — they are fast and let us iterate without
|
||||
// a DB. But sqlmock CANNOT detect bugs that depend on the ROW STATE
|
||||
// after the SQL runs. The result_preview-lost bug shipped to staging in
|
||||
// PR #2854 because every unit test was satisfied with "an UPDATE
|
||||
// statement fired" — none verified the row's preview field landed.
|
||||
//
|
||||
// These integration tests close that gap by booting a real Postgres,
|
||||
// running the production helpers, and SELECTing the row to verify the
|
||||
// observable state matches the expected outcome.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
mdb "github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// integrationDB returns the configured integration-test connection or
|
||||
// skips the test if INTEGRATION_DB_URL is unset. Local devs run the
|
||||
// docker-postgres incantation in the file header; CI's workflow sets the
|
||||
// env var via a service container.
|
||||
//
|
||||
// NOT SAFE FOR `t.Parallel()`. Each call hot-swaps the package-level
|
||||
// `mdb.DB` and restores via `t.Cleanup`. If two tests using this helper
|
||||
// run in parallel they race on the global; tests that need parallelism
|
||||
// should drive a local `*sql.DB` they own and pass it into helpers
|
||||
// directly rather than going through the package global.
|
||||
func integrationDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
url := os.Getenv("INTEGRATION_DB_URL")
|
||||
if url == "" {
|
||||
t.Skip("INTEGRATION_DB_URL not set; skipping (local devs: see file header)")
|
||||
}
|
||||
conn, err := sql.Open("postgres", url)
|
||||
if err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
if err := conn.Ping(); err != nil {
|
||||
t.Fatalf("ping: %v", err)
|
||||
}
|
||||
// Each test gets a fresh table state — fail loud if cleanup fails so
|
||||
// a bad test doesn't pollute the next one.
|
||||
if _, err := conn.ExecContext(context.Background(), `DELETE FROM delegations`); err != nil {
|
||||
t.Fatalf("cleanup: %v", err)
|
||||
}
|
||||
// Wire the package-level db.DB so production helpers (recordLedgerInsert,
|
||||
// recordLedgerStatus) see the same connection.
|
||||
prev := mdb.DB
|
||||
mdb.DB = conn
|
||||
t.Cleanup(func() {
|
||||
mdb.DB = prev
|
||||
conn.Close()
|
||||
})
|
||||
return conn
|
||||
}
|
||||
|
||||
// readLedgerRow returns (status, result_preview, error_detail) for the
|
||||
// given delegation_id, or fails the test on miss.
|
||||
func readLedgerRow(t *testing.T, conn *sql.DB, id string) (status, preview, errorDetail string) {
|
||||
t.Helper()
|
||||
var prev, errDet sql.NullString
|
||||
err := conn.QueryRowContext(context.Background(),
|
||||
`SELECT status, result_preview, error_detail FROM delegations WHERE delegation_id = $1`, id,
|
||||
).Scan(&status, &prev, &errDet)
|
||||
if err != nil {
|
||||
t.Fatalf("readLedgerRow(%s): %v", id, err)
|
||||
}
|
||||
return status, prev.String, errDet.String
|
||||
}
|
||||
|
||||
// TestIntegration_ResultPreviewPreservedThroughCompletion is the
|
||||
// regression gate for the bug that shipped in PR #2854 + was caught in
|
||||
// self-review: when both the inner SetStatus(completed, "", "") (from
|
||||
// updateDelegationStatus) and an outer SetStatus(completed, "", preview)
|
||||
// fire, the SECOND one is a same-status no-op — order matters.
|
||||
//
|
||||
// The fix in delegation.go calls the WITH-PREVIEW SetStatus FIRST so the
|
||||
// outer write lands the preview, and the inner becomes the no-op.
|
||||
//
|
||||
// This test fires the call sequence in the corrected order and asserts
|
||||
// the row's result_preview matches.
|
||||
//
|
||||
// If a future refactor reverses the order, this test fails on a real
|
||||
// Postgres — which sqlmock would have missed.
|
||||
func TestIntegration_ResultPreviewPreservedThroughCompletion(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
|
||||
id := "integ-deleg-preview-1"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
expectedPreview := "the long-running task's final answer"
|
||||
|
||||
// Mirror the production call sequence the FIXED code path uses.
|
||||
// executeDelegation flow:
|
||||
// 1. insertDelegationRow → recordLedgerInsert (status=queued)
|
||||
// 2. updateDelegationStatus("dispatched", "") at the start of execute,
|
||||
// so the row is at status=dispatched by completion time
|
||||
// 3. recordLedgerStatus("completed", "", preview) ← outer FIRST (the fix)
|
||||
// 4. updateDelegationStatus("completed", "") inside, which calls
|
||||
// recordLedgerStatus("completed", "", "") ← inner same-status no-op
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "the question", "")
|
||||
recordLedgerStatus(context.Background(), id, "dispatched", "", "")
|
||||
recordLedgerStatus(context.Background(), id, "completed", "", expectedPreview)
|
||||
recordLedgerStatus(context.Background(), id, "completed", "", "")
|
||||
|
||||
status, preview, errDet := readLedgerRow(t, conn, id)
|
||||
if status != "completed" {
|
||||
t.Errorf("status: want completed, got %q", status)
|
||||
}
|
||||
if preview != expectedPreview {
|
||||
t.Errorf("result_preview lost: want %q, got %q", expectedPreview, preview)
|
||||
}
|
||||
if errDet != "" {
|
||||
t.Errorf("error_detail should be empty: got %q", errDet)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_ResultPreviewBuggyOrderIsLost — DIAGNOSTIC test that
|
||||
// confirms the ORIGINAL buggy order does lose the preview. Useful when
|
||||
// auditing similar wiring elsewhere.
|
||||
//
|
||||
// This is documented behavior: it asserts the same-status replay no-op
|
||||
// works as designed in DelegationLedger.SetStatus. The fix in
|
||||
// delegation.go is to AVOID this order, not to change SetStatus's
|
||||
// same-status semantics (which the operator dashboard relies on for
|
||||
// idempotent completion notifications).
|
||||
func TestIntegration_ResultPreviewBuggyOrderIsLost(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
|
||||
id := "integ-deleg-preview-2"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
|
||||
// BUGGY sequence in production-shape order: queued → dispatched →
|
||||
// completed (no preview) → completed (preview ignored as same-status).
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "the question", "")
|
||||
recordLedgerStatus(context.Background(), id, "dispatched", "", "") // pre-completion stage
|
||||
recordLedgerStatus(context.Background(), id, "completed", "", "") // inner first
|
||||
recordLedgerStatus(context.Background(), id, "completed", "", "the answer") // outer same-status no-op
|
||||
|
||||
_, preview, _ := readLedgerRow(t, conn, id)
|
||||
if preview != "" {
|
||||
t.Errorf("buggy-order preview was unexpectedly non-empty: %q (SetStatus same-status no-op contract may have changed)", preview)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_FailedTransitionCapturesErrorDetail — error_detail is
|
||||
// the failure-path equivalent of result_preview. The legacy path calls
|
||||
// SetStatus(failed, errorDetail, "") via updateDelegationStatus; no
|
||||
// outer call exists today (no observed bug). This test pins that
|
||||
// error_detail lands as expected, so a future refactor adding an outer
|
||||
// call must consciously preserve the field — same lesson as the preview
|
||||
// bug, just on the failure path.
|
||||
func TestIntegration_FailedTransitionCapturesErrorDetail(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
|
||||
id := "integ-deleg-fail-1"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
expectedError := "callee unreachable: connection refused"
|
||||
|
||||
// queued → failed is allowed by allowedTransitions (the failure-on-
|
||||
// dispatch case) so this exercises a real production path.
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "the question", "")
|
||||
recordLedgerStatus(context.Background(), id, "failed", expectedError, "")
|
||||
|
||||
status, preview, errDet := readLedgerRow(t, conn, id)
|
||||
if status != "failed" {
|
||||
t.Errorf("status: want failed, got %q", status)
|
||||
}
|
||||
if errDet != expectedError {
|
||||
t.Errorf("error_detail: want %q, got %q", expectedError, errDet)
|
||||
}
|
||||
if preview != "" {
|
||||
t.Errorf("result_preview should be empty on failure: got %q", preview)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_Sweeper_DeadlineExceededIsMarkedFailed — real-Postgres
|
||||
// gate for the RFC #2829 PR-3 stuck-task sweeper. Inserts a row with a
|
||||
// past deadline, runs Sweep, asserts the row is now `failed` with
|
||||
// `deadline exceeded by sweeper` in error_detail.
|
||||
//
|
||||
// sqlmock unit tests pinned the SQL fired but couldn't observe the
|
||||
// real ON CONFLICT / index-scan behavior on the partial inflight
|
||||
// index. Real Postgres catches:
|
||||
// - deadline timestamp comparison is correct under tz boundaries
|
||||
// - the partial index actually serves the WHERE clause
|
||||
// - SetStatus's terminal forward-only protection holds across the
|
||||
// sweep + concurrent-write race
|
||||
func TestIntegration_Sweeper_DeadlineExceededIsMarkedFailed(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
|
||||
id := "integ-sweeper-deadline-1"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
|
||||
// Insert + transition to dispatched (otherwise queued→failed is
|
||||
// allowed but doesn't exercise the in-flight scan accurately).
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "task", "")
|
||||
recordLedgerStatus(context.Background(), id, "dispatched", "", "")
|
||||
|
||||
// Force the deadline into the past — Insert defaults to now+6h, so
|
||||
// we override. We don't touch last_heartbeat: the sweeper checks
|
||||
// deadline FIRST (it's the stronger statement) and short-circuits
|
||||
// before evaluating heartbeat staleness, so a NULL or stale beat is
|
||||
// irrelevant for the deadline-failure path.
|
||||
if _, err := conn.ExecContext(context.Background(),
|
||||
`UPDATE delegations SET deadline = now() - interval '1 minute' WHERE delegation_id = $1`, id,
|
||||
); err != nil {
|
||||
t.Fatalf("backdate deadline: %v", err)
|
||||
}
|
||||
|
||||
res := NewDelegationSweeper(nil, nil).Sweep(context.Background())
|
||||
if res.DeadlineFailures != 1 {
|
||||
t.Errorf("expected 1 deadline failure, got %+v", res)
|
||||
}
|
||||
status, _, errDet := readLedgerRow(t, conn, id)
|
||||
if status != "failed" {
|
||||
t.Errorf("status: want failed, got %q", status)
|
||||
}
|
||||
if errDet != "deadline exceeded by sweeper" {
|
||||
t.Errorf("error_detail: %q", errDet)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_Sweeper_StaleHeartbeatIsMarkedStuck — heartbeat
|
||||
// staleness path. Insert + dispatch + backdate last_heartbeat past the
|
||||
// 10× threshold; Sweep should mark the row stuck.
|
||||
func TestIntegration_Sweeper_StaleHeartbeatIsMarkedStuck(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
// Tighten threshold so the test is deterministic + fast.
|
||||
t.Setenv("DELEGATION_STUCK_THRESHOLD_S", "10")
|
||||
|
||||
id := "integ-sweeper-stuck-1"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "task", "")
|
||||
recordLedgerStatus(context.Background(), id, "dispatched", "", "")
|
||||
recordLedgerStatus(context.Background(), id, "in_progress", "", "")
|
||||
|
||||
// Backdate last_heartbeat past the 10s threshold; deadline still in
|
||||
// future so deadline check shouldn't fire.
|
||||
if _, err := conn.ExecContext(context.Background(),
|
||||
`UPDATE delegations SET last_heartbeat = now() - interval '60 seconds' WHERE delegation_id = $1`, id,
|
||||
); err != nil {
|
||||
t.Fatalf("backdate heartbeat: %v", err)
|
||||
}
|
||||
|
||||
res := NewDelegationSweeper(nil, nil).Sweep(context.Background())
|
||||
if res.StuckMarked != 1 {
|
||||
t.Errorf("expected 1 stuck mark, got %+v", res)
|
||||
}
|
||||
status, _, errDet := readLedgerRow(t, conn, id)
|
||||
if status != "stuck" {
|
||||
t.Errorf("status: want stuck, got %q", status)
|
||||
}
|
||||
if !strings.Contains(errDet, "no heartbeat for") {
|
||||
t.Errorf("error_detail should contain 'no heartbeat for'; got %q", errDet)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_Sweeper_HealthyRowsNotTouched — sanity: rows with a
|
||||
// fresh heartbeat AND a future deadline are left alone. Confirms the
|
||||
// partial inflight index scan + per-row branching don't false-positive
|
||||
// against well-behaved delegations.
|
||||
func TestIntegration_Sweeper_HealthyRowsNotTouched(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
|
||||
id := "integ-sweeper-healthy-1"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "task", "")
|
||||
recordLedgerStatus(context.Background(), id, "dispatched", "", "")
|
||||
// Fresh heartbeat = now()
|
||||
if _, err := conn.ExecContext(context.Background(),
|
||||
`UPDATE delegations SET last_heartbeat = now() WHERE delegation_id = $1`, id,
|
||||
); err != nil {
|
||||
t.Fatalf("set heartbeat: %v", err)
|
||||
}
|
||||
|
||||
res := NewDelegationSweeper(nil, nil).Sweep(context.Background())
|
||||
if res.DeadlineFailures != 0 || res.StuckMarked != 0 {
|
||||
t.Errorf("healthy row touched; result: %+v", res)
|
||||
}
|
||||
status, _, _ := readLedgerRow(t, conn, id)
|
||||
if status != "dispatched" {
|
||||
t.Errorf("status changed unexpectedly: %q", status)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_FullLifecycle_QueuedToDispatchedToCompleted — pins the
|
||||
// happy-path lifecycle. INSERT lands the row at queued; SetStatus moves
|
||||
// it through dispatched and into completed with preview. After the
|
||||
// terminal transition, no further state change is possible via
|
||||
// SetStatus (forward-only protection).
|
||||
func TestIntegration_FullLifecycle_QueuedToDispatchedToCompleted(t *testing.T) {
|
||||
conn := integrationDB(t)
|
||||
t.Setenv("DELEGATION_LEDGER_WRITE", "1")
|
||||
|
||||
id := "integ-deleg-lifecycle-1"
|
||||
caller := "11111111-1111-1111-1111-111111111111"
|
||||
callee := "22222222-2222-2222-2222-222222222222"
|
||||
|
||||
recordLedgerInsert(context.Background(), caller, callee, id, "task body", "")
|
||||
if status, _, _ := readLedgerRow(t, conn, id); status != "queued" {
|
||||
t.Errorf("after Insert: status want queued, got %q", status)
|
||||
}
|
||||
recordLedgerStatus(context.Background(), id, "dispatched", "", "")
|
||||
if status, _, _ := readLedgerRow(t, conn, id); status != "dispatched" {
|
||||
t.Errorf("after dispatched: status want dispatched, got %q", status)
|
||||
}
|
||||
recordLedgerStatus(context.Background(), id, "completed", "", "the result")
|
||||
status, preview, _ := readLedgerRow(t, conn, id)
|
||||
if status != "completed" {
|
||||
t.Errorf("after completed: status want completed, got %q", status)
|
||||
}
|
||||
if preview != "the result" {
|
||||
t.Errorf("preview after completed: want %q, got %q", "the result", preview)
|
||||
}
|
||||
|
||||
// Forward-only: trying to revise to failed should silently no-op
|
||||
// (recordLedgerStatus swallows ErrInvalidTransition).
|
||||
recordLedgerStatus(context.Background(), id, "failed", "post-hoc revision", "")
|
||||
status, preview, errDet := readLedgerRow(t, conn, id)
|
||||
if status != "completed" {
|
||||
t.Errorf("forward-only broken: status changed to %q", status)
|
||||
}
|
||||
if preview != "the result" {
|
||||
t.Errorf("preview clobbered by failed revision: %q", preview)
|
||||
}
|
||||
if errDet != "" {
|
||||
t.Errorf("error_detail clobbered by failed revision: %q", errDet)
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user