Skip to content

feat(syntropy): KG-direct tools — 3 kg-mcp tools + 16 new tests (SYN-33 Phase C+D) - #21

Merged
supmo668 merged 4 commits into
mainfrom
feat/kg-direct-tools
May 15, 2026
Merged

supmo668 merged 4 commits into
mainfrom
feat/kg-direct-tools

Conversation

@supmo668

Copy link
Copy Markdown

Summary

Implements SYN-33 Phase C+D (SH-Apps#64) — the openclaw-extension side of the KG-direct tools plan. ADR-001-aligned: extension uses the same `sj_*` ApiToken (from Supabase Vault via openclaw#13) the existing 9 SJ tools use; kg-mcp does atomic `quota_check_and_debit` server-side per ADR-001 §5; no second token type, no broker.

Built via 4 TDD cycles, one commit per cycle.

TDD cycle log

Cycle File Tests Commit
1 `kg-client.ts` + `kg-client.test.ts` — Bearer-auth HTTP client to kg-mcp 8 (mirrors `client.test.ts` + 1 new paywall-shape test) (cycle 1)
2 `kg-tools.ts` + `kg-tools.test.ts` — 3 MVP tool factories with `scope=diet_kg:read` 4 (cycle 2)
3 `config.ts` + `config.test.ts` + `index.ts` — wire kg-tools into plugin registration behind `kgBaseUrl` + `enableKgDirect` feature flags 4 new config tests; existing 11 still pass `8d051b67e`
4 `kg-mcp-live.test.ts` extended — 4 new LIVE-gated tests (3 tool round-trips + paywall scenario) +4 (all skip cleanly without `LIVE=1`) `28a250690`

Each cycle: write failing test → run vitest (RED on cycles 1 & 2) → implement minimal code → run vitest → GREEN → commit.

3 KG-direct MVP tools

Tool Args τ Use case
`kg_food_to_bioactives` `{ food_name, max_results? }` 1 Bioactive profile of a logged food for mechanism-of-action citations
`kg_compound_lookup` `{ compound_name }` 1 Canonical name, synonyms, pharmacology summary, dosage range
`kg_contraindication_check` `{ supplements[], medications[] }` 3 Multi-pair interaction check before recommending a supplement

All three carry `scope=diet_kg:read` (manifest-driven authz hint per PR #55).

ADR-001 alignment

  • §2.2 — uses existing `sj_*` Bearer from Vault; no new token type
  • §5 — kg-mcp handles atomic `quota_check_and_debit`; extension is metering-unaware
  • §5.2 — paywall responses pass through unchanged via the standard tool-result rendering path; the new client test asserts the shape is not rewritten

Feature-flag behavior

  • `kgBaseUrl` unset → KG tools not registered (silent default)
  • `kgBaseUrl` set + `enableKgDirect=false` → explicit opt-out
  • `kgBaseUrl` set + `enableKgDirect=true` (or omitted) → KG tools merge with SJ tools at registration

The `tools.registerTool` callback now returns `[...sjTools, ...kgTools]` when enabled, so the agent sees both surfaces atomically — no flicker, no half-state.

Test results

```
Test Files 8 passed | 1 skipped (9)
Tests 80 passed | 3 skipped (83)
Duration 1.2s
```

Excluded from local run: `vault.test.ts` (env-side missing `@supabase/supabase-js` in my symlinked node_modules; passes on CI with fresh `pnpm install`).

Coverage matrix vs SYN-33 plan

Phase This PR Status
A — JSON Schema source-of-truth for KG response types Hand-rolled TypeBox inline as transition pattern (same as pre-#14 `MealTypeSchema`); codegen comes from #44 pipeline ⏸ Deferred — separate monorepo PR
B — kg-mcp atomic `quota_check_and_debit` Mocked at `callKgTool` boundary (paywall-shape test); shrine-diet-bioactivity owns the server-side handlers ⏸ Owned by bioactivity repo
C — Extension `kg-client.ts` + feature flag ✅ This PR
D — `kg-tools.ts` + tests ✅ This PR
E — LIVE smoke ✅ This PR (LIVE-gated; run `LIVE=1 KG_MCP_BEARER=$(infisical get …) pnpm test:extensions`)

Follow-ups not in this PR

Test plan for reviewer

  • `pnpm install && pnpm test:extensions` — confirms full suite green
  • `pnpm tsgo` + `pnpm lint` — type + lint clean
  • (Optional, LIVE) `LIVE=1 KG_MCP_BEARER=$(infisical get …) pnpm test:extensions` — exercises the 3 tools against `kg-mcp-test.up.railway.app`
  • (Optional, LIVE+paywall) Add `SJ_TEST_PAYWALL_TOKEN` (test-only Clerk user, 0τ balance) to also exercise the paywall scenario

Cross-references

supmo668 added 4 commits May 14, 2026 17:46
ADR-001-aligned Bearer-auth HTTP client to shrine-diet-bioactivity kg-mcp.
Uses the same sj_* ApiToken (from Supabase Vault via openclaw#13) the
extension already manages for SJ MCP. No new token type, no broker.

Tests (mirror client.test.ts pattern + 1 new):
- JSON-RPC envelope shape + Bearer auth header
- MCP content array unwrap (JSON + plain text)
- JSON-RPC error envelope surfaces error message
- HTTP non-OK returns status + body
- Network failure returns error
- Raw JSON-RPC result fallback (no content array)
- ADR-001 §5 paywall response shape passes through unchanged

Result: 8/8 tests pass.

Part of SYN-33 / SH-Apps#64 Phase C — extension client. Hand-rolled
result type for now; KG response codegen comes from Phase A.
3 KG-direct MVP tool factories per SYN-33 plan-of-record:
- kg_food_to_bioactives (Food → Bioactives lookup)
- kg_compound_lookup (Compound canonical profile + dosage range)
- kg_contraindication_check (Multi-pair supplement × medication check)

Each tool:
- Hand-rolled TypeBox schema (codegen from Phase A schemas will replace)
- scope='diet_kg:read' manifest tag (#55 manifest plan contract)
- Calls callKgTool with the user's sj_* Bearer (ADR-001 §2)
- Renders kg-mcp response (incl. paywall) via standard toAgentResult path

Tests assert: 3 tools registered, canonical names, scope tag wired,
TypeBox object schemas present, non-empty labels + descriptions.

Result: 4/4 tests pass.

Part of SYN-33 / SH-Apps#64 Phase D — manifest yamls land separately
once the manifest-driven generator from PR #55 stabilizes. The hand-rolled
schemas here are the same pattern tools.ts used pre-#14 for MealTypeSchema
— OK to ship as-is per the plan-of-record's deferred-codegen note.
config.ts:
- Add optional kgBaseUrl + enableKgDirect to ConfigSchema (TypeBox)
- Pass through to parseSyntropyConfig return; absent stays absent
  so downstream can distinguish 'not configured' from 'opted out'
- URL validation on kgBaseUrl mirrors syntropyBaseUrl pattern

config.test.ts:
- 4 new tests: optional absence, wired-through value, URL validation,
  explicit enableKgDirect=false preserved

index.ts:
- Import createAllKgTools
- Compute kgEnabled = kgBaseUrl !== undefined && enableKgDirect !== false
- registerTool callback merges sjTools + kgTools (when enabled)
- Startup log shows kg=enabled/disabled state for ops visibility

ADR-001 alignment:
- Same sj_* Bearer passed to both SJ MCP + kg-mcp (no token exchange)
- No metering code in extension; kg-mcp does atomic quota_check_and_debit
  on its side (ADR-001 §5)

Suite total: 80 passed / 3 skipped / 0 failed
(was 64 from #15+#20 → now 64 + 8 kg-client + 4 kg-tools + 4 new config = 80)

Note: index.ts lifecycle has no direct test (pre-existing gap from #15
plan-of-record §What's still open). Tracked as follow-up. The feature
flag (kgEnabled) is computed via config tests; the tool-merge path is
covered transitively via kg-tools.test.ts.
…+ paywall (LIVE-gated)

4 new LIVE-gated tests on top of the existing /health probes:
- kg_food_to_bioactives round-trip
- kg_compound_lookup round-trip
- kg_contraindication_check round-trip
- ADR-001 §5 paywall shape (requires SJ_TEST_PAYWALL_TOKEN — separate
  describe so it skips independently if the test-only 0τ user isn't
  provisioned)

Total: 7 LIVE-gated tests (3 existing + 4 new), all skip cleanly without
LIVE=1. Standard /health probe + improved status-then-body assertion
(skip body fetch on 200 — avoids the small wasted round-trip from
openclaw#20 review).

Per the SYN-33 plan, this Phase E delivery is intentionally a *smoke*
probe — kg-mcp's own test suite (lives in shrine-diet-bioactivity,
Phase B work) owns response-shape correctness. We just verify the
contract surface from the consumer side.

Final suite: 80 passed / 3 skipped (LIVE) → with this commit 80 passed
/ 7 skipped (LIVE). Run: pnpm test:extensions
@supmo668 supmo668 added documentation Improvements or additions to documentation enhancement New feature or request labels May 15, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Formal models conformance drift detected

The formal models extracted constants (generated/*) do not match this openclaw PR.

This check is informational (not blocking merges yet).
See the formal-models-conformance-drift artifact for the diff.

If this change is intentional, follow up by updating the formal models repo or regenerating the extracted artifacts there.

@supmo668
supmo668 merged commit 51562b7 into main May 15, 2026
17 of 29 checks passed
@supmo668
supmo668 deleted the feat/kg-direct-tools branch May 15, 2026 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant