gstack icon indicating copy to clipboard operation
gstack copied to clipboard

sync-gbrain: capability check exports GBRAIN_PREPARE=true, breaking all writes on transaction-mode poolers (prepared statement does not exist)

Open jbarol opened this issue 5 months ago • 3 comments

Symptom

Following the /sync-gbrain skill's capability-check snippet, which exports GBRAIN_PREPARE=true citing #1435 ("Without it, search silently returns no results"), breaks every write on a brain behind a Supabase transaction-mode pooler (port 6543):

Full sync blocked: 24 file(s) failed:
  UNKNOWN: 24

with each failure being prepared statement "..." does not exist — the classic prepared-statements-vs-transaction-pooling failure. gbrain itself handles this correctly (it auto-disables prepared statements on port 6543 and says so in its banner); the skill's blanket export GBRAIN_PREPARE=true overrides that auto-detection in exactly the environment where it must stay off.

Where

The Step 4 capability check in the sync-gbrain template:

# GBRAIN_PREPARE=true ensures prepared statements stay enabled when
# connecting through a PgBouncer transaction-mode pooler (port 6543).
# Without it, search silently returns no results (#1435).
export GBRAIN_PREPARE=true

The comment has it inverted: per gbrain's own banner, GBRAIN_PREPARE=true is the override for session-mode poolers; on transaction mode (the Supabase 6543 default) prepared statements must stay disabled. Since the export persists for the rest of the skill run, any sync/import after the capability check inherits the broken setting.

(For what it's worth, the "search silently returns no results" symptom that motivated #1435 had a different cause on my box — gbrain-side, fixed separately — so the export may no longer be needed at all.)

Suggested fix

Drop the blanket export and let gbrain's port-based auto-detection decide; if a session-mode escape hatch is still needed, scope it to the single capability-check command rather than export-ing it, and gate it on detecting session mode.

jbarol avatar Jun 10 '26 20:06 jbarol

This looks like a capability detection contract issue.

A capability check should not export a global behavior flag that later breaks writes against transaction-mode poolers.

The contract should separate:

capability probe result
  from
runtime write mode

A standard AISP-style contract could express that:

[
  {
    "role": "system",
    "content": {
      "protocol": "AISP V1.0.0",
      "axiom_0": "Human_Sovereignty_and_Wellbeing",
      "id": "gstack_gbrain_prepare_mode_contract_aisp",
      "name": "gstack gbrain Prepared Statement Mode Contract",
      "version": "1.0.0",
      "license": "MIT",
      "summary": "Machine-checkable contract for selecting prepared-statement mode without breaking transaction poolers.",
      "flow_format": "mermaid",
      "loading_mode": "node",
      "tools": ["shell"],
      "params": {
        "database_url": "string",
        "pooler_mode": "string?"
      },
      "system_prompt": ""
    }
  },
  {
    "role": "user",
    "content": {
      "instruction": "STRICTLY OBEY aisp_contract; its non_negotiable rules are inviolable; then RUN aisop.main",
      "user_input": "{user_input}",
      "aisp_contract": {
        "profile": "aisp.skill.v1",
        "invocation": {
          "mode": "database_capability_policy",
          "when_to_use": [
            "before sync-gbrain writes",
            "after gbrain capability probe",
            "when transaction-mode pooler may be in use"
          ],
          "when_not_to_use": [
            "read-only status check",
            "local DB known to support prepared statements"
          ]
        },
        "non_negotiable": [
          {
            "rule": "Capability probe must not force prepared statements globally when transaction-mode pooler is detected.",
            "enforced_by": "select_mode.step2:sys.assert"
          },
          {
            "rule": "Prepared statement failures must downgrade to safe non-prepared mode or fail clearly, not corrupt sync behavior.",
            "enforced_by": "write.step2:sys.assert"
          }
        ],
        "discovery": {
          "category": "database_capability",
          "tags": ["gstack", "gbrain", "postgres", "prepared-statements", "pooler", "aisp"]
        },
        "risk_level": "high",
        "resources": []
      },
      "aisop": {
        "main": "graph TD\n    probe[Probe database capability] --> detect[Detect pooler mode]\n    detect --> select_mode[Select write mode]\n    select_mode --> write[Perform write]\n    write --> trace[Record db mode trace]\n    trace --> end_node((End))"
      },
      "functions": {
        "probe": {
          "step1": "Run safe capability probe without exporting global write behavior.",
          "output_mapping": "capability_probe"
        },
        "detect": {
          "step1": "Detect transaction-mode pooler or prepared statement incompatibility.",
          "output_mapping": "pooler_detection"
        },
        "select_mode": {
          "step1": "Select prepared or non-prepared write mode based on probe and pooler detection.",
          "step2": "sys.assert('transaction pooler does not force prepared mode', 'GBRAIN_PREPARE must not break transaction-mode poolers')",
          "output_mapping": "write_mode"
        },
        "write": {
          "step1": "Perform sync write using selected write_mode.",
          "step2": "sys.assert('write mode compatible with database session behavior', 'gbrain write mode incompatible with database')",
          "output_mapping": "write_result"
        },
        "trace": {
          "step1": "Record probe result, pooler detection, selected mode, and write result.",
          "output_mapping": "db_mode_trace"
        },
        "end_node": {
          "step1": "Return db_mode_trace."
        }
      }
    }
  }
]

The practical rule:

capability checks should produce scoped facts, not global behavior flags that leak into later writes.

optimization2026 avatar Jun 30 '26 06:06 optimization2026

@16francej Please close this issue as resolved. #2077 removed GStack’s forced prepared-statement setting for transaction-mode poolers, so sync no longer creates the prepared-statement failure described here.

time-attack avatar Jul 15 '26 10:07 time-attack

Verified already fixed on main. lib/gbrain-exec.ts:86 documents that buildGbrainEnv() never sets GBRAIN_PREPARE (gbrain auto-disables prepared statements on transaction-mode poolers itself; a caller-set value passes through untouched), and sync-gbrain/SKILL.md.tmpl explicitly instructs "Do NOT export GBRAIN_PREPARE here (#1965)."

Regression test: test/build-gbrain-env.test.ts:121-142 ("does not set GBRAIN_PREPARE when DATABASE_URL targets port 6543").

If you still hit "prepared statement does not exist" on a pooler, run /gstack-upgrade to pick up the current skill + libs. Closing.

garrytan avatar Aug 07 '26 21:08 garrytan