Skip to content
shopnexusPublic

About

ShopNexus agent runtime: runs a model turn in a sandbox and streams events over SSE

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

agent

ShopNexus's agent runtime. The server calls POST /v1/turns once per turn with the whole history; the runtime runs the model in a sandbox with a workspace that lasts for the conversation, streams events back over SSE, and keeps nothing in memory between turns. The design, and why, is in ARCHITECTURE.md.

curl -N -H "Authorization: Bearer $AGENT_KEY" http://agent:8080/v1/turns -d '{
  "conversation_id": "c-123",
  "instructions": "You are ShopNexus'\''s console assistant.",
  "history": [{"role": "user", "context": "staff: Lan", "content": "How many orders were cancelled this week?"}],
  "token": "<turn token>",
  "model": "console-agent", "model_key": "<Bifrost virtual key>"
}'
# event: tool_call ... event: tool_result ... event: step ... event: result

The model gets three tools, bash, write and edit, in a bubblewrap sandbox with no network and no secrets. Server tools are the shopnexus CLI inside it, which reaches the API through a broker that adds the turn's token.

What the model reads

Context that is missing, extra, wrong, stale or contradictory is what confuses models most, so the runtime controls every byte the model reads.

  1. The history only grows. Each request is the one before it plus what is new, so the provider's prompt cache keeps working. Nothing is omitted, cut or rewritten; only the last turn's reasoning drops out when a new turn starts. A user message's context (time, staff member, shop) is stored with it and read in a <context> block.
  2. The harness writes little, and all of it is in one of three places: the <harness> section of the system prompt (runtime/harness.go), the sentences in tool results (messages.go, sandbox/messages.go), and tool descriptions (sandbox/tools.go). Errors reach the model as the tool wrote them, with secrets redacted. Only schema errors carry guidance.
  3. You can see exactly what the model reads. agent.Render prints a request, testdata/view.golden holds the view of a sample conversation, and the eval saves every transcript. Run go test -update after a change that touches what the model reads, and review the diff.

Output over 4KB is saved to /workspace/.agent/out/<call>.txt; the model reads its size, its first and last 20 lines, and the exit code, and searches the file with rg, jq or fzf --filter instead of running the command again.

Layout

agent.go, view.go    the loop, Stop values, the loop check; the history view
messages.go          the loop's sentences for the model
excerpt.go           first and last lines of a long output; redaction
tool.go, validate.go Tool, NewTool; input checked against the JSON Schema
message.go, render.go Message (the stored format), Model; Render
openai/              Model for OpenAI-compatible APIs (Bifrost)
sandbox/             bwrap sandbox, its supervisor, bash/write/edit
workspace/           workspaces in Garage as tar.zst, cached locally
runtime/             POST /v1/turns, SSE, broker, egress, harness section
cmd/agent/   the service, configured by environment
eval/                real-model scenarios

The root package works on its own: agent.New(agent.Config{Model, Instructions, Tools}) and Run give a stateless loop over any tools, with no sandbox.

Tools and schemas

The JSON Schema is the only place shape rules live, as in the server's API. NewTool[T] builds it with jsonschema.For[T], fields in order, and the same schema checks every call before Run. The model gets every problem at once, in the validator's wording and in schema order:

invalid input for search_orders: field "status": value must be one of 'pending', 'shipped', 'cancelled'; additional properties 'limit' not allowed

Tests

go test -race ./...    # bwrap must be installed; the sandbox and runtime tests use it

# the cgroup test needs a delegated cgroup
systemd-run --user --scope -p Delegate=yes env SANDBOX_TEST_CGROUP=1 go test -run Cgroup ./sandbox

# the workspace store against Garage or any S3
WORKSPACE_TEST_S3=host:3900 WORKSPACE_TEST_BUCKET=... WORKSPACE_TEST_KEY=... WORKSPACE_TEST_SECRET=... go test ./workspace

Eval

Golden files show what the model reads, but not how it reads it. The eval in eval/ runs scenarios against real models: schema errors, raw errors, context, and, through the real runtime and sandbox, a saved long output, a failure seen through a pipe, and a background server. A scenario can start with a made-up first call (first) to reach its path, and lists what the model must read (hits), so a run that never reaches its path fails. Checks are code, not a model acting as judge.

EVAL_BASE_URL=http://bifrost:8080/v1 EVAL_API_KEY=... \
  go test ./eval -v -count=1 -timeout 40m -parallel 6 -models deepseek-v4-flash,gpt-5.6-terra -runs 3

Transcripts go to eval/out/<model>/<scenario>-<n>.txt, the score table to eval/out/report.txt.

Running it

docker build -t agent .
# the seccomp profile lives in the umbrella: infra/stack/seccomp/agent.json
docker run --security-opt seccomp=../infra/stack/seccomp/agent.json --security-opt systempaths=unconfined \
  -e AGENT_KEY=... -e MODEL_BASE_URL=http://bifrost:8080/v1 -e MODEL=deepseek-v4-flash -e MODEL_API_KEY=... \
  -e S3_ENDPOINT=garage:3900 -e S3_ACCESS_KEY=... -e S3_SECRET_KEY=... \
  -p 8080:8080 agent

In the umbrella, docker compose --profile dev up -d agent-dev runs it with package-proxy and the stack's Garage and Bifrost. The settings are listed in ARCHITECTURE.md.

About

ShopNexus agent runtime: runs a model turn in a sandbox and streams events over SSE

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages