Skip to main content

Using agenticspec · 3 of 4

Queries

Find the places in your sessions where something happened, with the sq query language.

docs/queries.md

Queries

sq is a small query language over your sessions. A query finds the places in sessions where something happened: a call a hook refused, an edit followed by a test run, a long wait on you.

$ agenticspec query --text '?- exposure{kind: K, channel: "ToolInput", verdict: "Unk"}.'

This lists the kinds of unconfirmed secrets and personal data that appeared in tool arguments in the last 72 hours. query prints a JSON report with one row per answer and session.

Run a query​

formwhat runs
agenticspec query file.sqthe rules, queries and checks in the file
agenticspec query --text '...'sq given inline
agenticspec query file.sq --text '...'both, the text appended to the file
agenticspec query file.sq --transcript <session>.jsonlone transcript file, read directly, without the store

Without --transcript, a query runs over every session of the window. --since-hours and --root choose the window.

Rules and queries​

A rule gives a name to a combination of facts. A query, starting with ?-, prints every set of values that makes it true.

refused(C, T, R) :- call{node: C, id: I, tool: T},
decided{call: I, decision: "Refuse", reason: R}.
?- refused(C, T, R).

This prints each refused tool call, its tool and the reason.

  • Names starting with an upper-case letter are variables. _ matches anything.
  • rel{field: Value} matches facts of a relation by field name. Fields you leave out match anything.
  • "text" is a literal. ~"^(Edit|Execute)$" matches a regular expression.
  • !p(X) holds when p(X) has no answer.
  • Comparisons: =, !=, <, <=, >, >=.
  • Durations are written 250ms, 30s, 5m or 1h and compare as milliseconds.
  • //, # and % start a comment.

Counting​

count, sum, min and max aggregate over the answers inside the braces:

calls(A, N) :- agent{id: A, role: "Main"}, N = count{ call{agent: A, node: C} }, N >= 50.
?- calls(A, N).

count counts distinct answers. Bind a field that differs per fact, here node: C, or every call counts once.

Order in time: seq​

seq(A, B, ...) holds when the steps happen in this order. Each one is the nearest later step that matches.

edit_then_run(E, X) :-
seq(E, X, scope: agent, within: 5m),
call{node: E, category: "Edit"}, call{node: X, category: "Execute"}.
?- edit_then_run(E, X).
optioneffect
scope: agentthe steps belong to the same agent
scope: sessionthe steps may belong to different agents of the session
within: 10mthe whole sequence lies within this time
gap: 3at most this many other steps between two matched steps
pick: allevery later match, not only the nearest

Checks​

A check turns a rule into a pass or fail result per session:

check refused-then-retried {
about "a call a hook refused was asked for again unchanged"
severity medium
expect none
find retried
}

expect none means the rule should have no answers. expect some means it should have at least one. Each session gets one verdict per check:

verdictmeaning
Okthe expectation holds
Gapit does not
Unkit depends on facts the transcript does not record for certain

The report counts the verdicts of each check under ok, gap and unk.

The checks that ship with agenticspec are in queries/session.sq. sync evaluates them for every session, and the dashboard shows the results.

checkfinds
cache-bust-after-idlea turn after an idle gap over 5 minutes that wrote the whole context again
refused-then-retrieda call a hook refused that the agent asked for again unchanged
fanout-unreada sub-agent of a fan-out of three or more that ended after the parent's last turn, so its result was not read
human-correctiona correction from you within 2 minutes of an edit or a command
waiting-on-humanthe main agent waiting more than 30 minutes for you

Definite and possible answers​

Some facts are not certain: an author read from the opening words of a prompt, a decision the transcript does not record, a sub-agent link that had to be inferred. An answer that rests on such a fact is marked possible, and unknown lists the facts it rests on. Every other answer is definite.

Rows of the building blocks​

queries/blocks.sq lifts a session into rows of the building blocks: the tasks it was given, the runs that worked them, their steps, the questions asked, the messages sent and received, and the harness sessions, turns and tool calls of its agents.

$ agenticspec query queries/blocks.sq --transcript <session>.jsonl
$ agenticspec blocks lift <session>.jsonl

query shows the answers of its rules. blocks lift writes one row per answer, checked against the blocks, in the form of a scenario's given (see Scenarios).

ruleblock entityfrom
block_input(the inputs of a draft scenario)a prompt of the main agent by Human, Delegate or PeerSession; a Harness or Hook prompt is no input
block_taskagentic.work.Taskan input classed Task
block_runagentic.work.Run, with its agentic.work.Assignmentthe main agent working a task from its prompt on; a sub-agent working the task of the run whose call spawned it
block_stepagentic.work.Stepeach model turn and tool call of a run, in order; kind Agent, Child (a spawn), Person (AskUserQuestion) or Program
block_questionagentic.question.Questionan AskUserQuestion call, or an input classed Question
block_messageagentic.channel.Messagea SendMessage call, or an input by a delegate or another session
block_receivedMessage.received_in_turnthe first model turn of the turn an input started
block_sessionagentic.harness.HarnessSessioneach agent of role Main or SubAgent, from its first node; a sub-agent names its parent's session
block_turnagentic.harness.Turneach model turn of such an agent, with its usage and context size
block_callagentic.harness.ToolCalleach tool call of such an agent: Denied when refused, Completed or Failed by its result, Requested when neither a decision nor a result is recorded

Every row names the node it was lifted from in evidence. The session, turn and call rows carry the identifiers the other rows name: a run's and a step's harness_session_id, a sent message's sent_by_call and a read message's received_in_turn each name a written row. A side call has no session, so no session, turn or call row is written for it. A field the session does not record, such as a question's options, the channel of a message, a session's harness, principal and limits, a turn's inference or a call's tool, is listed in not_recorded, never guessed. No relation records a claim on a pool, so agentic.resource.Claim is reported in not_observable and no row of it is written. Identifiers are UUIDs derived from the session's nodes and agents, so two lifts of one session give the same rows.

blocks lift prints JSON:

fieldwhat
session_idthe session
given{ rows: [...] }, each row { entity, id, state, fields, not_recorded, evidence }
entitiesthe number of rows per entity
not_observablethe entities the session cannot show

Relations​

Every step of a session is a node, written <session>#<number>.

relationfields
sessionid, project, harness, opened_at, ended_at
agentid, session, parent, role, depth, spawned_by, started_at, ended_at
cmdnode, session, agent, seq, at, actor, kind, outcome: every step
promptnode, agent, author, attribution, class, bytes, digest, mid_turn
callnode, id, agent, tool, category, bytes, digest, cwd, background
decidednode, call, decision, decider, reason
completednode, call, ok, bytes, digest, dur
turnnode, agent, model, ctx, uncached, cache_read, write_5m, write_1h, output
turn_endnode, agent
compactednode, agent, before
exposurenode, id, agent, channel, class, kind, alias, verdict, call
refused_cmdnode, error: a step agenticspec could not replay
atnode, t: the time of a node
nexta, b: b is the next step of the same agent
turn_ofnode, prompt: the step belongs to the turn this prompt started
intervalagent, state, on, from, to, dur, open: when an agent was Idle or Working, and what it waited on
bustnode, agent, cause, written: a cache bust, see KPIs
spawnedcall, child: the call started this sub-agent

Values you will match on:

fieldvalues
call.categoryRead, Search, Edit, Execute, Network, Spawn, Message, Plan, Other
prompt.classTask, Correction, Approval, Question, Continue, Unknown
prompt.authorHuman, Delegate, PeerSession, ParentAgent, Harness, Hook
decided.decisionAccept, Refuse
agent.roleMain, SubAgent, SideCall (a model call of the tool outside every conversation, such as a title)

bytes and digest stand for content: its length and its SHA-256. The text itself is not stored, so a query never sees it.

Relations of an architecture​

The same language selects in an architecture document, over the relations part, setting, link and marker. agenticstudio uses them in the selectors of its patches; their fields are on the agenticstudio page.