query-loop-implementation — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited query-loop-implementation (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Build the loop as product infrastructure, not prompt glue:
ConversationManager -> QueryLoop -> ToolRuntimeConversationManager owns durable state: session id, messages, user settings, budget, persistence.QueryLoop owns one task turn: call model, detect tool calls, execute tools, append tool results, repeat.ToolRuntime owns registered tools: schemas, permission checks, execution, error formatting.Use ReAct as the mental model:
Thought -> Action -> Observation -> Thought -> AnswerImplement it as structured API traffic:
model thinking/text -> tool_call -> tool_result -> next model call -> final textFind where messages are built, where the model is called, and whether tool/function calling is already configured.
Keep the first version narrow: one model call function, one tool registry, explicit exit conditions.
Use the provider's structured tool-call format when available. Avoid parsing free-form Action: text unless the provider has no function/tool-calling API.
Validate tool input against a schema, apply permission checks for risky tools, wrap failures as tool results, and log every call.
Always include maxTurns, timeout/cancel support, token/cost budget checks, and a fatal-error path.
Accept messages as loop input and return updated messages, but leave trimming, retrieval, summarization, and compaction to a separate context-management layer.
Adapt this shape to the user's language and SDK:
async function runQueryLoop({
initialMessages,
model,
tools,
maxTurns = 10,
signal,
}: {
initialMessages: Message[]
model: ModelClient
tools: ToolRegistry
maxTurns?: number
signal?: AbortSignal
}) {
let messages = [...initialMessages]
for (let turn = 1; turn <= maxTurns; turn++) {
if (signal?.aborted) return { status: "aborted", messages }
const response = await model.generate({
messages,
tools: tools.definitions(),
signal,
})
messages.push(response.message)
const toolCalls = extractToolCalls(response.message)
if (toolCalls.length === 0) {
return {
status: "completed",
finalMessage: response.message,
messages,
}
}
for (const call of toolCalls) {
const result = await tools.execute(call, { signal, messages })
messages.push(makeToolResultMessage(call.id, result))
}
}
return { status: "max_turns", messages }
}The "model continues judging" step is not a separate function. It happens when the loop calls the model again after appending tool_result messages.
Implement these before shipping:
Prefer returning structured terminal reasons:
type TerminalReason =
| "completed"
| "max_turns"
| "aborted"
| "timeout"
| "permission_denied"
| "budget_exceeded"
| "fatal_tool_error"Each tool should define:
type Tool = {
name: string
description: string
inputSchema: unknown
risk: "read" | "write" | "execute" | "external"
validate(input: unknown): ValidatedInput
canUse(input: ValidatedInput, ctx: ToolContext): Promise<PermissionDecision>
call(input: ValidatedInput, ctx: ToolContext): Promise<ToolResult>
}Execution order:
find tool by name
-> schema validate model input
-> run tool-specific validation
-> check permission
-> call tool
-> format success or error as tool_resultReturn tool errors to the model when it can plausibly recover, for example invalid arguments, file not found, empty search result, or transient API errors. Stop the loop for security violations, repeated failures, missing credentials, or budget exhaustion.
Keep "intelligence" in the model and "reliability" in code:
For simple AI apps, avoid subagents, worktrees, and streaming tool execution at first. Add them only when the product actually needs parallel work, isolation, or long-running tasks.
Read references/query-loop-patterns.md when designing a new query engine, reviewing an existing implementation, or explaining ReAct-to-query-loop architecture to another engineer.
~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.