Skip to content

Qsp example agent - #167

Open
mkeenan-di wants to merge 5 commits into
mainfrom
qsp-example-agent
Open

mkeenan-di wants to merge 5 commits into
mainfrom
qsp-example-agent

Conversation

@mkeenan-di

Copy link
Copy Markdown
Contributor

Summary

Bumps di.qsp to 0.4.0. qsp now has what an LLM agent needs to query a policed kdb+ database: an MCP server in q, a primer, refusals as structured data, sizing before a query runs, and signed receipts. It also lets a single qsp compile a query and send it out across an estate (RDB/HDB executors). Behaviour changes are marked behaviour in CHANGELOG.md.

What's new

Agents

  • MCP server in q (mcp/mcp.q, mcp/qsp-mcp). Exposes qsp's calls as agent tools over stdio, with no Python. Also adds a Claude Code skill (skill/qsp/SKILL.md).
  • Primer (api/primer.md, (`.qsp.api;`primer)). How to write q that qsp accepts, written on a made-up schema so it never names real columns. examples/primer/check.q runs every example in it.
  • Refusals as data. classify returns code / kind / subject, codes[] lists them all (core/codes.q), and the audit log records the code.
  • Clearer refusals for common agent mistakes. Examples: a column named after a q function, time.minute, unbalanced brackets (the message now names the bracket), aliases that shadow a server name, and keyed joins on a missing key.
  • locate: shows where in a query q's error is, with a caret under the failing part.
  • Sizing: estimate, precheck, largeread, mingroup, maxqueries, and the bytes / description columns in schema.csv.
  • Context for agents: describe gains shape, table descriptions, and parameterised business terms. Descriptions and terms are only shown to users who can read what they mention (behaviour).
  • told[p;u]: shows what each user's brief tells them and what it withholds. bin/policy.q prints it for review.

Estate: plans, executors, receipts

  • compile / finalize. run is split so a qsp holding only the policy can compile a query and seal the plan with HMAC. Executors run the sealed plan without qsp and attest the result (plan/init.q, estate/exec.q).
  • Planner orchestration (executors). The planner answers a gateway's execasr itself, so a client sends one message whatever the estate looks like.
  • Receipts. Signed receipts (HMAC-SHA1, tested against RFC 2202) from a key ring, plus the api calls verify / receipt / reloadkeys.
  • Gateway features: gateway user scope, async requests, and per-request context recorded in the audit log.
  • bin/serve.q: one launcher for a policy on a port. It replaces examples/gateway/policy.q.
  • di.qsp.cache: caches results by the md5 of the rewritten query.

Query rewriting

  • Leading partition constraints are merged into one date in list (behaviour). This is the same change as the qsp-partition-fold PR; see Notes.
  • As-of joins: rewritten to asofsafe, which sorts and attributes the right side as needed. A day's aj over quotes went from 160s to about 1s with the same result (behaviour: an unsorted right side is now sorted).
  • All of qsp's function swaps (notsafe, ascsafe, descsafe, findsafe, asofsafe, wjsafe) are defined in one registry, core/swaps.q.
  • Other rewriting changes:
    • Filters can now use sub-selects.
    • Lookup functions are evaluated during the rewrite (behaviour).
    • xrank is permitted.
    • cross carries the columns of both sides.
    • defs.q is loaded with the policy.

Examples and docs

  • examples/agents/: Claude Code wired to the MCP server (agent.sh and per-role slash commands), an eval harness with recorded results, and the agenticq scorers.
  • examples/ticker/ (compiler, gateway, and RDB/HDB executors), examples/gateway/, and examples/primer/.
  • docs/estate.md and docs/gateway.md, plus updates to concepts.md, internals.md and security-review.md.

Behaviour changes to check

  • validate returns three new keys, and the audit log has eleven new columns. A policy that registers the audit log in its schema.csv needs these added.
  • A gateway's eval handler now receives (eval;expr) instead of (eval;tree).
  • explain and runx show what ran as q-SQL text instead of a parse tree.
  • See CHANGELOG.md, the 0.4.0 section, for the full list.

Notes for reviewers

  • This overlaps with the qsp-partition-fold PR (the partition fold in init.q, ord.partfixed, the tests and the CHANGELOG entry). Merge that one first, then rebase this branch.
  • examples/ticker/keys/ contains demo-only plan and receipt keys. This is deliberate, and the ticker README says so. Real deployments make their keys outside the repo.

Testing

bin/ci.sh: every suite passes.

suite result
unit (di.qsp 1286, plus cache, locate and estate suites) pass
example walkthrough pass
bin/policy.q -test pass
admin UI endpoints pass
hardening campaign pass

🤖 Generated with Claude Code

@DI-Software-Engineering

Copy link
Copy Markdown

The diff is too large to review automatically (estimated 204,862 tokens, limit 90,000).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants