gstack icon indicating copy to clipboard operation
gstack copied to clipboard

gbrain-local-status: 5s probe timeout misclassifies healthy-but-slow engines as broken-config; /sync-gbrain skips code+memory

Open jbarol opened this issue 5 months ago • 4 comments

Symptom

On a brain whose Postgres sits behind a Supabase transaction-mode pooler (port 6543), /sync-gbrain permanently skips the code + memory stages:

gstack-gbrain-sync (incremental):
  SKIP  code         skipped — local engine broken-config — config at ~/.gbrain/config.json is malformed; see /setup-gbrain Step 1.5
  SKIP  memory       skipped — local engine broken-config — ...
  OK    brain-sync   curated artifacts pushed

…while the engine is in fact healthy: gbrain doctor is green and gbrain sources list --json returns valid JSON with exit 0. The config file is well-formed.

Root cause

lib/gbrain-local-status.ts classifies the engine by probing gbrain sources list --json with PROBE_TIMEOUT_MS = 5_000. On this setup a fresh CLI connection through the pooler takes 6.9–10.7s (measured across runs), so the probe times out every time. The timeout error's stderr matches neither "Cannot connect to database" nor "config.json", so the defensive default (codex #8) classifies it as broken-config — and the 60s cache then propagates the misdiagnosis to the orchestrator and every skill preamble.

The user-facing message ("config at ~/.gbrain/config.json is malformed") points at a file that is fine, so the remediation steps in /setup-gbrain Step 1.5 can't fix it.

Reproduce

Any gbrain on a connection where time gbrain sources list --json > 5s (e.g. Supabase transaction pooler from a residential connection). Then run /sync-gbrain → code+memory SKIP with broken-config.

Suggested fix (any of)

  1. Raise PROBE_TIMEOUT_MS (20s covered my worst case), and/or
  2. Honor an env override (e.g. GSTACK_GBRAIN_PROBE_TIMEOUT_MS) so slow-pooler users can tune it without patching the lib, and/or
  3. Classify the timeout case distinctly (e.g. slow-engine) instead of falling into broken-config, with a message that names the real condition ("probe exceeded Ns — engine may be reachable but slow; raise the timeout").

Locally patching PROBE_TIMEOUT_MS 5000→20000 fully resolves it (sync runs all stages), but the patch is reverted by every git reset --hard upgrade.

jbarol avatar Jun 10 '26 20:06 jbarol

Another data point from a session-mode Supabase pooler (so not pooler-specific — plain network latency triggers it too):

  • gbrain sources list --json succeeds (exit 0, valid JSON) but takes 5.3–5.8s across 3 runs (gbrain 0.42.40.0, postgres engine, gbrain doctor green, gbrain_doctor_ok: true in the same detect output).
  • PROBE_TIMEOUT_MS = 5_000 (lib/gbrain-local-status.ts:76) kills the probe, and the defensive default classifies the timeout as broken-config → brain-aware blocks suppressed despite a healthy brain.

An env override (e.g. GSTACK_GBRAIN_PROBE_TIMEOUT_MS) or distinguishing ETIMEDOUT/SIGTERM-on-timeout from real config errors before falling through to the defensive default would fix both this and the original report.

jakedwyer avatar Jun 12 '26 18:06 jakedwyer

This is a health-check classification contract issue.

A 5s probe timeout should not automatically mean broken config.

It may mean:

healthy but cold
healthy but slow
engine warming
network slow
temporary load
broken config
auth failure
unreachable endpoint

Those states should be separated because /sync-gbrain should not skip code+memory just because a healthy engine was slow to respond once.

A standard AISP-style contract could express this:

[
  {
    "role": "system",
    "content": {
      "protocol": "AISP V1.0.0",
      "axiom_0": "Human_Sovereignty_and_Wellbeing",
      "id": "gstack_gbrain_health_probe_contract_aisp",
      "name": "gstack gbrain Health Probe Contract",
      "version": "1.0.0",
      "license": "MIT",
      "summary": "Machine-checkable contract for classifying gbrain health probes without mislabeling slow engines as broken config.",
      "flow_format": "mermaid",
      "loading_mode": "node",
      "tools": ["shell"],
      "params": {
        "probe_timeout_seconds": "integer",
        "engine_url": "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": "health_check_policy",
          "when_to_use": [
            "before sync-gbrain skips memory/code sync",
            "when local status probe times out",
            "when classifying gbrain config health"
          ],
          "when_not_to_use": [
            "manual force sync",
            "explicit user override"
          ]
        },
        "non_negotiable": [
          {
            "rule": "A single timeout must not be classified as broken config without retry or reason.",
            "enforced_by": "classify.step2:sys.assert"
          },
          {
            "rule": "sync-gbrain skip decision must distinguish slow/unknown from broken config.",
            "enforced_by": "decision.step2:sys.assert"
          }
        ],
        "discovery": {
          "category": "health_check",
          "tags": ["gstack", "gbrain", "health", "timeout", "sync", "aisp"]
        },
        "risk_level": "medium",
        "resources": []
      },
      "aisop": {
        "main": "graph TD\n    probe[Run health probe] --> classify[Classify result]\n    classify --> decision[Decide sync behavior]\n    decision --> trace[Record probe trace]\n    trace --> end_node((End))"
      },
      "functions": {
        "probe": {
          "step1": "Run health probe with configured timeout and optional retry/backoff.",
          "output_mapping": "probe_result"
        },
        "classify": {
          "step1": "Classify result as healthy, slow_unknown, warming, auth_failure, unreachable, broken_config, or unknown.",
          "step2": "sys.assert('timeout alone is not broken_config', 'Timeout must not be classified as broken config without additional evidence')",
          "output_mapping": "health_classification"
        },
        "decision": {
          "step1": "Choose sync behavior: proceed, retry, warn, skip, or fail.",
          "step2": "sys.assert('skip decision distinguishes slow from broken config', 'sync-gbrain skip must not treat slow as broken config')",
          "output_mapping": "sync_decision"
        },
        "trace": {
          "step1": "Record timeout, retries, classification, and sync decision.",
          "output_mapping": "health_probe_trace"
        },
        "end_node": {
          "step1": "Return health_probe_trace."
        }
      }
    }
  }
]

Minimal implementation:

1. Add retry/backoff or longer timeout for cold local engines.
2. Classify timeout as slow_unknown, not broken_config.
3. Let sync-gbrain choose warn/retry instead of skip where appropriate.
4. Emit trace showing why sync was skipped.

optimization2026 avatar Jun 30 '26 06:06 optimization2026

@16francej Please close this issue as resolved. #2077 added a separate timeout state, a longer configurable probe timeout, and proceed-with-warning behavior, so a slow healthy GBrain is no longer mislabeled as broken configuration.

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

Verified already fixed on main, in two parts:

  1. The probe timeout is overridable: GSTACK_GBRAIN_PROBE_TIMEOUT_MS (default now 15s, was 5s) via probeTimeoutMs() in lib/gbrain-local-status.ts.
  2. A timed-out probe classifies as timeout (slow-but-healthy), never broken-config, and every consumer (--is-ok, gen-skill-docs, gstack-config gbrain-refresh, the sync stages) treats timeout as usable — brain-aware blocks are no longer suppressed for slow poolers.

Regression test: test/gbrain-local-status.test.ts ("returns 'timeout' (not broken-config) when the probe exceeds the deadline (#1964)").

The remaining sibling gap (thin-client/remote-MCP setups misclassified as broken-config) is #2051, fixed in PR #2472. Closing this one.

garrytan avatar Aug 07 '26 21:08 garrytan