Skip to content

Commit 4652bed

Browse files
hkaltaiclaude
andcommitted
fix: keep data_diff and the tool daemon within read-only and release rules; address review findings
- data_diff refuses any source, target or where_clause that is not one complete read-only query (classifier read, single statement, balanced parentheses, no side-effect or dynamic query() calls) before connecting, checks every statement it runs, and opens every connector read-only. sql_check keeps its stricter rules (shared read-only-sql helper). - isBlockedStatement classifies the whole pattern, so leading comments and multi-statement patterns stay hard-blocked. - Redshift values in batch references get their backslashes escaped. - Tool daemon: the idle timer waits for a pending file release; only a request that ran a tool leads to a release, and a stop is answered without waiting for one; the socket is owner-only. - Workspace: refresh clears the active adapter before closing it; a snapshot removed mid-read lists no skills; copies into .claude/skills are opt-in with ALTIMATE_CLAUDE_SKILL_SYNC=1 (the Claude Code plugin sets it). - Analyst effort ignores non-object model params and output_config without effort; managed output preferences resolve files against their own directory; warehouse_add shares the registry's project root; mcp --allow-write help names the hard blocks. - Docs for warehouse_add's local writes, Claude Code skill copies, analyst effort and answer_commit; tests for each change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7109869 commit 4652bed

29 files changed

Lines changed: 428 additions & 99 deletions

‎docs/docs/configure/agents.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ The agent for asking questions about your data. Truly read-only mode for safe da
3030
- **Schema/warehouse/finops**: All inspection tools available
3131
- **Workspace knowledge**: Loads workspace and project skills; reads workspace memory and views this checkout's memory audit log. Can read files bundled with skills, including global skills stored outside the project. Reloading memory syncs the local copy with the workspace, which can remove a local copy of a block the workspace archived. Saving memory is done with `builder`.
3232
- **Config overrides**: A permission config such as `"*": "allow"` cannot turn file edits, SQL writes (including data diffs of arbitrary queries), shell, memory saves, warehouse connection or MCP config changes, training imports, sample project setup, feedback submission, batch tool calls or delegation beyond `data-explorer` back on. A global `"*": "allow"` does turn on MCP and custom tools for analyst, including ones that write (for example a SQL MCP server); to keep them from analyst, put the `"*": "allow"` under `agent.builder` instead of the global config, or deny those tools under `agent.analyst.permission`.
33+
- **Reasoning effort**: On adaptive-thinking models the analyst requests `high` effort unless the agent or model options already choose one. Set `ALTIMATE_ANALYST_EFFORT` (`low`, `medium`, `high`, `xhigh`, `max`, or `off` to send none) to change it.
34+
- **Final answers**: `answer_commit` asks the interactive analyst once per turn to re-check any number that did not appear in tool output before the answer is committed.
3335

3436
!!! tip
3537
Use `analyst` when exploring data to ensure no accidental writes. Switch to `builder` when you're ready to create or modify models.

‎docs/docs/configure/mcp-servers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ At startup the server lists each connection's name and type in its instructions,
3737

3838
<!-- altimate_change start — MCP read-only limits and private output lifecycle -->
3939

40-
Source writes are refused by default. Without `--allow-write`, source queries request database-enforced read-only execution on PostgreSQL, SQLite and DuckDB. For other connection types, read-only is enforced by SQL classification only; stored functions with side effects are not blocked — use a read-only database user. The server prints this caveat on stderr at startup and includes it in `warehouse_list` for each affected connection. Add `--allow-write` to enable SQL write permissions; existing hard safety blocks still apply. `sql_materialize` always requires a read-only source query and writes only into the server session's temporary `scratch` warehouse. Scratch tables disappear when the server exits. Large results may include a local spill file path that your client can open with any file reader. Spill files use a private directory and are deleted when the server exits. Local `tool_output.max_lines` and `tool_output.max_bytes` settings control output limits; remote agent configuration is not loaded.
40+
Source writes are refused by default. Without `--allow-write`, source queries request database-enforced read-only execution on PostgreSQL, SQLite and DuckDB. For other connection types, read-only is enforced by SQL classification only; stored functions with side effects are not blocked — use a read-only database user. The server prints this caveat on stderr at startup and includes it in `warehouse_list` for each affected connection. Add `--allow-write` to enable SQL write permissions; existing hard safety blocks still apply (`DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` stay blocked). `warehouse_add` may write local connection configuration (the connections file and the operating system credential store) even in read-only mode. `sql_materialize` always requires a read-only source query and writes only into the server session's temporary `scratch` warehouse. Scratch tables disappear when the server exits. Large results may include a local spill file path that your client can open with any file reader. Spill files use a private directory and are deleted when the server exits. Local `tool_output.max_lines` and `tool_output.max_bytes` settings control output limits; remote agent configuration is not loaded.
4141

4242
<!-- altimate_change end -->
4343

‎docs/docs/configure/skills.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,7 @@ The skill list the agent sees marks each skill with where it was found:
341341
When more than one skill fits a task, the agent is told to load the one from the highest source in that order, and to fall back to a lower source only when nothing higher covers the task. So a skill your team added to the workspace is preferred over a similar skill another team left in the repository. The label comes from the skill's location, not from anything written in the skill.
342342

343343
One exception: a project skill with exactly the same name as a workspace skill replaces it, and is listed as `project`. That keeps a skill you are editing in the repository, and may later publish, from being shadowed by the workspace's older copy.
344+
345+
### Copies for Claude Code
346+
347+
In a linked project that has a `.claude` directory, each workspace sync can also write the workspace skills into `.claude/skills/` so Claude Code finds them. Copies are written only when `ALTIMATE_CLAUDE_SKILL_SYNC=1` is set; the Altimate Code plugin for Claude Code sets it when it refreshes the workspace. The copies are marked as owned by Altimate Code and git-ignored, and they are withdrawn when you unlink the project or sign out (withdrawal runs whether or not the variable is set).

‎packages/opencode/src/altimate/mcp-server/output-settings.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export async function outputLimits() {
4040
const directory = process.cwd()
4141
const limits = { maxLines: MAX_LINES, maxBytes: MAX_BYTES }
4242
// `{env:VAR}`, `${VAR}` and `{file:path}` resolve as in the agent loader; text that cannot be substituted is skipped.
43-
const apply = async (text: string, from: { path: string } | { source: string }) => {
43+
const apply = async (text: string, from: { path: string } | { source: string; dir?: string }) => {
4444
const expanded = await ConfigVariable.substitute(
45-
"path" in from ? { text, type: "path", path: from.path } : { text, type: "virtual", dir: directory, source: from.source },
45+
"path" in from ? { text, type: "path", path: from.path } : { text, type: "virtual", dir: from.dir ?? directory, source: from.source },
4646
).catch(() => undefined)
4747
if (!expanded) return
4848
const output = parse(expanded)?.tool_output
@@ -82,6 +82,6 @@ export async function outputLimits() {
8282
await load(path.join(ConfigManaged.managedConfigDir(), name))
8383
}
8484
const preferences = await ConfigManaged.readManagedPreferences().catch(() => undefined)
85-
if (preferences) await apply(preferences.text, { source: preferences.source })
85+
if (preferences) await apply(preferences.text, { source: preferences.source, dir: path.dirname(preferences.source) })
8686
return limits
8787
}

‎packages/opencode/src/altimate/mcp-server/workspace.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,11 @@ export async function createWorkspaceAdapter(directory: string) {
215215
if (closed) throw new Error("Workspace server closed.")
216216
await WorkspaceClient.assertIdentity(directory, token)
217217
if (!refresh && active?.token === token) return active
218-
if (active) await retire(active)
219-
active = undefined
218+
if (active) {
219+
const previous = active
220+
active = undefined
221+
await retire(previous)
222+
}
220223
const adapter = await createPinnedWorkspaceAdapter(directory)
221224
try {
222225
await WorkspaceClient.assertIdentity(directory, token)

‎packages/opencode/src/altimate/native/connections/data-diff.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import type { DataDiffParams, DataDiffResult, PartitionDiffResult } from "../types"
1111
import * as Registry from "./registry"
12+
import { requireReadOnlyQuery } from "../../tools/read-only-sql"
13+
import { classifyAndCheck } from "../../tools/sql-classify"
1214

1315
// ---------------------------------------------------------------------------
1416
// Dialect mapping — bridge warehouse config types to Rust SqlDialect serde names
@@ -177,7 +179,7 @@ type Rows = (string | null)[][]
177179
/**
178180
* Execute a SQL statement against a named warehouse and return rows as string[][].
179181
*/
180-
async function executeQuery(sql: string, warehouseName: string | undefined): Promise<Rows> {
182+
async function executeQuery(sql: string, warehouseName: string | undefined, metadata = false): Promise<Rows> {
181183
let connector
182184
if (warehouseName) {
183185
connector = await Registry.get(warehouseName)
@@ -190,10 +192,19 @@ async function executeQuery(sql: string, warehouseName: string | undefined): Pro
190192
connector = await Registry.get(warehouseName)
191193
}
192194

195+
// Validate every generated query before any driver executes it, including partition discovery.
196+
const dialect = warehouseTypeToDialect(Registry.getConfig(warehouseName)?.type ?? "generic")
197+
if (metadata) {
198+
const check = classifyAndCheck(sql, dialect)
199+
if (check.blocked || check.queryType !== "read") throw new Error("Data diff metadata must be read-only")
200+
} else {
201+
requireReadOnlyQuery(sql, dialect)
202+
}
203+
193204
// Bypass the driver's default LIMIT — data-diff needs complete result sets.
194205
const result = await connector.execute(sql, undefined, undefined, {
195206
noLimit: true,
196-
...(Registry.canonicalType(Registry.getConfig(warehouseName)?.type) === "postgres" ? { readOnly: true } : {}),
207+
readOnly: true,
197208
})
198209

199210
// Normalise to string[][] — drivers return mixed types
@@ -449,7 +460,7 @@ async function discoverExtraColumns(
449460

450461
try {
451462
const sql = buildColumnDiscoverySQL(tableName, dialect)
452-
const rows = await executeQuery(sql, warehouseName)
463+
const rows = await executeQuery(sql, warehouseName, true)
453464
const columnInfos = parseColumnInfo(rows, dialect)
454465

455466
if (columnInfos.length === 0) return undefined
@@ -881,6 +892,21 @@ async function runPartitionedDiff(params: DataDiffParams): Promise<DataDiffResul
881892
}
882893

883894
export async function runDataDiff(params: DataDiffParams): Promise<DataDiffResult> {
895+
// Validate both caller-controlled relations and the shared filter before discovery or engine startup.
896+
try {
897+
for (const [source, warehouse] of [
898+
[params.source, params.source_warehouse],
899+
[params.target, params.target_warehouse ?? params.source_warehouse],
900+
] as const) {
901+
const type = warehouse ? Registry.getConfig(warehouse)?.type : Registry.list().warehouses[0]?.type
902+
const dialect = warehouseTypeToDialect(type ?? "generic")
903+
requireReadOnlyQuery(isQuery(source) ? source : `SELECT * FROM ${source}`, dialect)
904+
if (params.where_clause) requireReadOnlyQuery(`SELECT 1 WHERE (${params.where_clause})`, dialect)
905+
}
906+
} catch (error) {
907+
return { success: false, error: `Data diff requires read-only inputs: ${error instanceof Error ? error.message : String(error)}`, steps: 0 }
908+
}
909+
884910
// Dispatch to partitioned diff if partition_column is set
885911
if (params.partition_column) {
886912
return runPartitionedDiff(params)

‎packages/opencode/src/altimate/native/connections/registry.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function globalConfigPath(): string {
177177
*
178178
* It is also the key of the per-project registry state (see `state()`).
179179
*/
180-
function projectRoot(): string {
180+
export function projectRoot(): string {
181181
try {
182182
return Instance.directory
183183
} catch {

‎packages/opencode/src/altimate/session/analyst-effort.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ export function analystEffortOptions(input: {
3232
const configured = [input.agent.options ?? {}, input.model.options ?? {}]
3333
// SAP AI Core keeps its reasoning controls under `modelParams`.
3434
const hasEffort = (options: Record<string, any>) =>
35-
[options, options.modelParams ?? {}].some((level) => EFFORT_KEYS.some((key) => key in level) || "output_config" in level)
35+
[options, options.modelParams].some(
36+
(level) =>
37+
level !== null &&
38+
typeof level === "object" &&
39+
(EFFORT_KEYS.some((key) => key in level) ||
40+
(level.output_config !== null &&
41+
typeof level.output_config === "object" &&
42+
"effort" in level.output_config)),
43+
)
3644
if (configured.some(hasEffort)) return {}
3745
const effort = (process.env[ANALYST_EFFORT_ENV] ?? ANALYST_DEFAULT_EFFORT).trim().toLowerCase()
3846
if (DISABLED.has(effort) || !input.model.capabilities.reasoning) return {}

‎packages/opencode/src/altimate/tool-cli/daemon.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createHash } from "node:crypto"
22
import { spawn } from "node:child_process"
3-
import { lstatSync, mkdirSync, realpathSync, rmSync, statSync } from "node:fs"
3+
import { chmodSync, lstatSync, mkdirSync, realpathSync, rmSync, statSync } from "node:fs"
44
import net from "node:net"
55
import os from "node:os"
66
import path from "node:path"
@@ -191,6 +191,8 @@ export async function startDaemon(options: DaemonOptions): Promise<Daemon> {
191191
let inflight = 0
192192
let reloading: Promise<void> | undefined
193193
let releasing: Promise<void> | undefined
194+
// A tool ran (and may have opened file connections) since the last release.
195+
let dirty = false
194196
let stopping: Promise<void> | undefined
195197
let timer: ReturnType<typeof setTimeout> | undefined
196198
const sockets = new Set<net.Socket>()
@@ -223,14 +225,15 @@ export async function startDaemon(options: DaemonOptions): Promise<Daemon> {
223225
reloading = undefined
224226
}
225227
}
228+
dirty = true
226229
const result = await options.runner.run(request.name, request.args ?? {}, { allowWrite: request.allowWrite === true })
227230
return { id: request.id, ...result }
228231
}
229232

230233
const arm = () => {
231234
clearTimeout(timer)
232235
timer = setTimeout(() => {
233-
if (inflight > 0) return arm()
236+
if (inflight > 0 || releasing) return arm()
234237
void stop()
235238
}, idle)
236239
}
@@ -243,8 +246,13 @@ export async function startDaemon(options: DaemonOptions): Promise<Daemon> {
243246
inflight++
244247
arm()
245248
let afterReply: (() => void) | undefined
249+
let stopRequest = false
246250
Promise.resolve()
247-
.then(() => handle(JSON.parse(line) as Request, (action) => (afterReply = action)))
251+
.then(() => {
252+
const request = JSON.parse(line) as Request
253+
stopRequest = request.op === "stop"
254+
return handle(request, (action) => (afterReply = action))
255+
})
248256
.catch((error): Response => ({
249257
id: "",
250258
status: "error",
@@ -255,8 +263,11 @@ export async function startDaemon(options: DaemonOptions): Promise<Daemon> {
255263
.then(async (response) => {
256264
inflight--
257265
// Release DuckDB/SQLite file locks once idle, before replying, so a client running `dbt build` next finds the file free.
258-
// A request that arrives meanwhile waits for the release (see `handle`).
259-
if (inflight === 0 && options.releaseFiles) {
266+
// A request that arrives meanwhile waits for the release (see `handle`). Whichever request
267+
// finishes last releases, so a run that finished while a status was in flight is covered.
268+
// A stop is answered at once; the daemon then shuts down.
269+
if (inflight === 0 && options.releaseFiles && dirty && !stopRequest) {
270+
dirty = false
260271
const release = options.releaseFiles().catch(() => {})
261272
releasing = release
262273
await release
@@ -289,6 +300,14 @@ export async function startDaemon(options: DaemonOptions): Promise<Daemon> {
289300
server.once("error", reject)
290301
server.listen(file, () => {
291302
server.removeListener("error", reject)
303+
// Only the owner may connect to the socket.
304+
if (process.platform !== "win32") {
305+
try {
306+
chmodSync(file, 0o600)
307+
} catch (error) {
308+
return reject(error)
309+
}
310+
}
292311
resolve()
293312
})
294313
})

‎packages/opencode/src/altimate/tool-cli/permissions.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Effect } from "effect"
22
import type { Tool } from "../../tool/tool"
3+
import { classifyAndCheck } from "../tools/sql-classify"
34

45
/** Permission kinds the data tools raise for reads; anything else is a write and needs `--allow-write`. */
56
export const READ_PERMISSIONS: ReadonlySet<string> = new Set([
@@ -9,11 +10,10 @@ export const READ_PERMISSIONS: ReadonlySet<string> = new Set([
910
"answer_commit",
1011
])
1112

12-
// Same statements the agent's `safetyDenials` rules refuse (agent/agent.ts); matched case-insensitively.
13-
const BLOCKED_STATEMENT = /^\s*(drop\s+(database|schema)\b|truncate\b)/i
14-
13+
// Same statements the agent's `safetyDenials` rules refuse (agent/agent.ts), found by the SQL classifier
14+
// so leading comments and later statements of a multi-statement pattern are seen too.
1515
export function isBlockedStatement(pattern: string): boolean {
16-
return BLOCKED_STATEMENT.test(pattern)
16+
return classifyAndCheck(pattern).blocked
1717
}
1818

1919
export function denied(permission: string, retry = "Re-run"): Error {

0 commit comments

Comments
 (0)