Files
dsh-facet/test/scope.test.js
T
NotExpectedYet 40574505f2 fix: judge the working directory and the arguments a tool declares
The guard resolved relative paths against the session directory the conversation
started in, but the file tools resolve them against the session's *current*
working directory — which `working_directory` can change with no guard opinion.
In a `deny` facet, `cd /etc` followed by `read` of a relative path read from
/etc. The same mismatch made a shadowed tool invisible: this deployment installs
`dsh-better-edit`, which replaces `read` (argument `path`) and `edit` (argument
`file`), so a table written against the built-in `file_path` left the busiest
read tool unguarded — an outside file could be read with `deny` in force.

Now:

- `working_directory` is in the path table, so a `cd` outside the facet is
  refused (or put to the user, by mode) like any other out-of-facet path;
- a relative argument is judged against the directory the tool will actually
  use, read lazily from `ctx.workingDirectory`, and the implicit-search rule
  judges that same directory;
- path parameter names come from the tool's own declaration when the registry
  can supply one, so a shadowed tool is judged by the arguments it really takes;
- a delegated session started with a `subagent` `cwd` outside the parent's facet
  is clamped to the parent root instead of being scoped to the outside directory.

`undo_last_edit` (write-like) and the delegation tools join the table. Verified
against a profile with dsh-better-edit: in `deny` mode the outside `read`,
`edit`, `cd /etc` and `subagent cwd /etc` are all refused, while an inside read
still works — and the relative-read bypass from the same profile now stays
inside.
2026-11-09 08:30:07 +00:00

170 lines
8.2 KiB
JavaScript

import assert from 'node:assert/strict'
import { test } from 'node:test'
import {
displayPath,
facetContextText,
facetRuleText,
isWideningCall,
outsideReason,
pathArgumentsFor,
widenReason,
} from '../lib/scope.js'
test('pathArgumentsFor reads the path argument of every guarded tool', () => {
assert.deepEqual(pathArgumentsFor('read', { file_path: 'src/a.ts' }), ['src/a.ts'])
assert.deepEqual(pathArgumentsFor('write', { file_path: 'src/a.ts', content: 'x' }), ['src/a.ts'])
assert.deepEqual(pathArgumentsFor('edit', { file_path: 'src/a.ts' }), ['src/a.ts'])
assert.deepEqual(pathArgumentsFor('read_image', { file_path: 'a.png' }), ['a.png'])
assert.deepEqual(pathArgumentsFor('grep', { pattern: 'x', path: 'src' }), ['src'])
assert.deepEqual(pathArgumentsFor('glob', { pattern: '*.ts', path: 'src' }), ['src'])
assert.deepEqual(pathArgumentsFor('str_replace_editor', { path: 'src/a.ts' }), ['src/a.ts'])
assert.deepEqual(pathArgumentsFor('recall', { path: 'src', focus: 'src/controllers' }), [
'src',
'src/controllers',
])
})
test('pathArgumentsFor has no opinion where it cannot have one', () => {
// The deliberate hole: a shell command cannot be judged from argv.
assert.deepEqual(pathArgumentsFor('bash', { command: 'cat /etc/passwd' }), [])
// Unknown tools are not guessed at.
assert.deepEqual(pathArgumentsFor('mcp_something', { path: '/etc' }), [])
// An omitted path means the session directory, which is already in scope.
assert.deepEqual(pathArgumentsFor('grep', { pattern: 'x' }), [])
// Unvalidated JSON must never make the guard throw.
assert.deepEqual(pathArgumentsFor('read', null), [])
assert.deepEqual(pathArgumentsFor('read', 'nonsense'), [])
assert.deepEqual(pathArgumentsFor('read', { file_path: 42 }), [])
assert.deepEqual(pathArgumentsFor('read', { file_path: ' ' }), [])
assert.deepEqual(pathArgumentsFor('grep', { path: { nested: '/etc' } }), [])
})
test('a shadowed tool is judged by the arguments its own schema declares', () => {
// `dsh-better-edit` replaces read/edit on the agent's layer with different
// argument names; trusting the built-in's `file_path` left the busiest read
// tool unguarded.
assert.deepEqual(pathArgumentsFor('read', { path: 'src/a.ts', limit: 10 }, ['path', 'limit']), ['src/a.ts'])
assert.deepEqual(pathArgumentsFor('edit', { file: 'src/a.ts', edits: [] }, ['file', 'edits']), ['src/a.ts'])
// A declared path-shaped name the table does not list is still found.
assert.deepEqual(pathArgumentsFor('write', { path: 'src/a.ts', content: 'x' }, ['path', 'content']), ['src/a.ts'])
// The table is the fallback when the schema cannot be read.
assert.deepEqual(pathArgumentsFor('read', { path: 'src/a.ts' }), ['src/a.ts'])
})
test('the tools that move the scope are judged like any other path', () => {
assert.deepEqual(pathArgumentsFor('working_directory', { cd: '../elsewhere' }, ['cd']), ['../elsewhere'])
assert.deepEqual(
pathArgumentsFor('subagent', { cwd: '../elsewhere', prompt: 'x' }, ['cwd', 'prompt']),
['../elsewhere'],
)
assert.deepEqual(pathArgumentsFor('subagent_fork', { cwd: 'sub', prompt: 'x' }, ['cwd', 'prompt']), ['sub'])
// Reading the directory names nothing, and moves nothing.
assert.deepEqual(pathArgumentsFor('working_directory', {}, ['cd']), [])
// The undo tool writes a file back, so it is a path like any other.
assert.deepEqual(pathArgumentsFor('undo_last_edit', { path: 'src/a.ts' }), ['src/a.ts'])
})
test('isWideningCall is true only for the one act that needs the user', () => {
assert.equal(isWideningCall('facet', { action: 'add', paths: ['x'] }), true)
// Narrowing and reading cannot show the agent anything new.
assert.equal(isWideningCall('facet', { action: 'remove', paths: ['x'] }), false)
assert.equal(isWideningCall('facet', { action: 'reset' }), false)
assert.equal(isWideningCall('facet', { action: 'status' }), false)
assert.equal(isWideningCall('read', { action: 'add' }), false)
assert.equal(isWideningCall('facet', null), false)
})
test('facetRuleText states the rule and never the binding', () => {
const rule = facetRuleText()
// Mode-neutral by construction. A prompt section is registered once as a plain
// string, and a `volatile()` edit updates the config reference without
// re-applying the plugin — so a `mode` interpolated here would freeze at load
// while the guard went on enforcing the new one. The mode-specific sentence
// lives in the binding, which is a callback.
assert.match(rule, /Work inside the facet\./)
assert.doesNotMatch(rule, /refused|put to the user as a question/)
assert.match(rule, /Never widen the scope on your own initiative/)
// The rule must not name a path: a changing system prompt invalidates the
// prompt cache for the whole conversation.
assert.doesNotMatch(rule, /\/|src\//)
assert.equal(facetContextText({ cwd: '/a', root: '/a/b', extras: [], mode: 'off' }), '')
})
test('facetContextText carries the binding that does change', () => {
const ask = facetContextText({ cwd: '/w', root: '/w/src/room', extras: [], mode: 'ask' })
assert.equal(
ask,
'facet: /w/src/room\n' +
'session folder: /w — relative paths in tool calls resolve here\n' +
'outside the facet: put to the user as a question — say in one line what you need and why, then record it with `facet action=add` so the rest of the session does not have to ask again',
)
const deny = facetContextText({ cwd: '/w', root: '/w/src/room', extras: ['/w/src/shared'], mode: 'deny' })
assert.equal(
deny,
'facet: /w/src/room\n' +
'session folder: /w — relative paths in tool calls resolve here\n' +
'granted outside it: src/shared\n' +
'outside the facet: refused — ask the user to widen the facet from its context panel, or with `facet action=add` where that can be granted',
)
// The binding is the only half that can follow a live edit, so the two modes
// have to read differently here — they no longer differ in the rule.
assert.notEqual(ask, deny)
// Unscoped sessions contribute nothing rather than an empty heading.
assert.equal(facetContextText({ cwd: '/w', root: undefined, extras: [], mode: 'ask' }), '')
assert.equal(facetContextText({ cwd: '/w', root: '/w/src/room', extras: [], mode: 'off' }), '')
})
test('a session with no approval channel says so instead of promising a question', () => {
// `approval: never` refuses every ask before an answerer is consulted, so the
// mode stops mattering: `ask` and `deny` both end in a refusal, and
// `facet action=add` cannot be granted either. `canAsk: false` is the one
// input that collapses them, and it has to name the mechanism that still
// works — the context panel, which the user owns.
for (const mode of ['ask', 'deny']) {
const text = facetContextText({ cwd: '/w', root: '/w/src/room', extras: [], mode, canAsk: false })
assert.match(text, /outside the facet: refused — this session has no approval channel/)
assert.match(text, /`facet action=add` cannot be granted/)
assert.match(text, /context panel/)
assert.doesNotMatch(text, /put to the user as a question/)
}
// The default stays a promise: a caller that knows nothing about approval
// channels must not silently change what the binding says.
assert.match(
facetContextText({ cwd: '/w', root: '/w/src/room', extras: [], mode: 'ask' }),
/put to the user as a question/,
)
})
test('displayPath prefers the shortest unambiguous form', () => {
assert.equal(displayPath('/w', '/w/src/a.ts'), 'src/a.ts')
assert.equal(displayPath('/w', '/w'), '.')
assert.equal(displayPath('/w', '/other/a.ts'), '/other/a.ts')
assert.equal(displayPath('', '/w/a.ts'), '/w/a.ts')
})
test('outsideReason names the path, the boundary and the way forward', () => {
const reason = outsideReason({
cwd: '/w/src/controllers/room',
path: '/w/src/shared/types.ts',
root: '/w/src/controllers/room',
mode: 'ask',
})
assert.match(reason, /src\/shared\/types\.ts/)
assert.match(reason, /src\/controllers\/room/)
// A denial that does not say how to proceed is one the agent routes around.
assert.match(reason, /facet action=add/)
})
test('widenReason survives an add with no usable paths', () => {
assert.match(widenReason(['src/shared']), /src\/shared/)
assert.match(widenReason([]), /nothing/)
})