@namzu/sdk 69.4.0 → 70.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/dist/authorization/protected-paths.d.ts +24 -6
- package/dist/authorization/protected-paths.d.ts.map +1 -1
- package/dist/authorization/protected-paths.js +37 -9
- package/dist/authorization/protected-paths.js.map +1 -1
- package/dist/authorization/shell-lexer.d.ts.map +1 -1
- package/dist/authorization/shell-lexer.js +263 -0
- package/dist/authorization/shell-lexer.js.map +1 -1
- package/dist/connector/web/guarded-fetch.d.ts +30 -0
- package/dist/connector/web/guarded-fetch.d.ts.map +1 -1
- package/dist/connector/web/guarded-fetch.js +85 -0
- package/dist/connector/web/guarded-fetch.js.map +1 -1
- package/dist/connector/web/index.d.ts +1 -1
- package/dist/connector/web/index.d.ts.map +1 -1
- package/dist/connector/web/index.js +1 -1
- package/dist/connector/web/index.js.map +1 -1
- package/dist/plugin/common-layout/read.js +2 -2
- package/dist/plugin/common-layout/read.js.map +1 -1
- package/dist/plugin/hook-module/capabilities.d.ts +12 -0
- package/dist/plugin/hook-module/capabilities.d.ts.map +1 -1
- package/dist/plugin/hook-module/capabilities.js +82 -10
- package/dist/plugin/hook-module/capabilities.js.map +1 -1
- package/dist/plugin/hook-module/consent.d.ts +11 -0
- package/dist/plugin/hook-module/consent.d.ts.map +1 -1
- package/dist/plugin/hook-module/consent.js +31 -0
- package/dist/plugin/hook-module/consent.js.map +1 -1
- package/dist/plugin/hook-module/host.d.ts.map +1 -1
- package/dist/plugin/hook-module/host.js +69 -23
- package/dist/plugin/hook-module/host.js.map +1 -1
- package/dist/plugin/hook-module/index.d.ts +1 -1
- package/dist/plugin/hook-module/index.d.ts.map +1 -1
- package/dist/plugin/hook-module/index.js +1 -1
- package/dist/plugin/hook-module/index.js.map +1 -1
- package/dist/plugin/hook-module/scan.js +1 -1
- package/dist/plugin/hook-module/scan.js.map +1 -1
- package/dist/plugin/hook-module/worker-source.d.ts +5 -4
- package/dist/plugin/hook-module/worker-source.d.ts.map +1 -1
- package/dist/plugin/hook-module/worker-source.js +198 -28
- package/dist/plugin/hook-module/worker-source.js.map +1 -1
- package/dist/public-runtime.d.ts +3 -3
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +3 -3
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +1 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +1 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +75 -10
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +21 -3
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +35 -19
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/skills/inline-shell-host.d.ts +6 -0
- package/dist/skills/inline-shell-host.d.ts.map +1 -1
- package/dist/skills/inline-shell-host.js +8 -0
- package/dist/skills/inline-shell-host.js.map +1 -1
- package/dist/skills/inline-shell.d.ts +9 -1
- package/dist/skills/inline-shell.d.ts.map +1 -1
- package/dist/skills/inline-shell.js +78 -10
- package/dist/skills/inline-shell.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +2 -0
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/types/permission/index.d.ts +11 -2
- package/dist/types/permission/index.d.ts.map +1 -1
- package/dist/types/permission/index.js +12 -2
- package/dist/types/permission/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +7 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/package.json +1 -1
- package/src/authorization/protected-paths.ts +44 -9
- package/src/authorization/shell-lexer.ts +242 -0
- package/src/connector/web/guarded-fetch.ts +92 -0
- package/src/connector/web/index.ts +6 -1
- package/src/plugin/common-layout/read.ts +2 -2
- package/src/plugin/hook-module/capabilities.ts +94 -10
- package/src/plugin/hook-module/consent.ts +36 -0
- package/src/plugin/hook-module/host.ts +72 -21
- package/src/plugin/hook-module/index.ts +2 -0
- package/src/plugin/hook-module/scan.ts +2 -2
- package/src/plugin/hook-module/worker-source.ts +199 -28
- package/src/public-runtime.ts +6 -0
- package/src/public-tools.ts +4 -1
- package/src/runtime/query/executor.ts +74 -7
- package/src/runtime/query/review-policy.ts +42 -13
- package/src/skills/inline-shell-host.ts +16 -0
- package/src/skills/inline-shell.ts +90 -8
- package/src/tools/builtins/skill.ts +2 -0
- package/src/types/permission/index.ts +20 -3
- package/src/types/tool/index.ts +7 -0
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { homedir } from 'node:os'
|
|
25
25
|
import { dirname, parse } from 'node:path'
|
|
26
26
|
|
|
27
|
+
import { allowRuleScope } from '../../authorization/gate.js'
|
|
27
28
|
import { unknownProgramInLine } from '../../authorization/program.js'
|
|
28
29
|
import type { ShellWord } from '../../authorization/shell-lexer.js'
|
|
29
30
|
import {
|
|
@@ -35,6 +36,7 @@ import { effectiveBashWorkingDirectory } from '../../tools/builtins/bash-cwd.js'
|
|
|
35
36
|
import { BashTool } from '../../tools/builtins/bash.js'
|
|
36
37
|
import { isTrustedReadOnly } from '../../tools/trusted-read-only.js'
|
|
37
38
|
import type { ToolManager } from '../../toolsets/manager.js'
|
|
39
|
+
import type { AuthorizationRule } from '../../types/authorization/index.js'
|
|
38
40
|
import type {
|
|
39
41
|
HITLResumeDecision,
|
|
40
42
|
LaunchedScript,
|
|
@@ -47,11 +49,13 @@ import type {
|
|
|
47
49
|
import type { ApprovalPolicy } from '../../types/hitl/policy.js'
|
|
48
50
|
import type { SessionId, TurnId } from '../../types/ids/index.js'
|
|
49
51
|
import {
|
|
52
|
+
CLASSIFY_NESTED_REFUSAL,
|
|
50
53
|
PLAN_MODE_REFUSAL,
|
|
51
54
|
PLAN_MODE_REFUSAL_WITH_PLAN_FILE,
|
|
52
55
|
STRICT_MODE_REFUSAL,
|
|
53
56
|
nestedCallModeRefusal,
|
|
54
57
|
} from '../../types/permission/index.js'
|
|
58
|
+
import type { ToolDefinition } from '../../types/tool/index.js'
|
|
55
59
|
import type { AutoReviewClassifier, AutoReviewDecision } from './auto-review/classifier.js'
|
|
56
60
|
import {
|
|
57
61
|
autoReviewBlockedFeedback,
|
|
@@ -151,6 +155,7 @@ export function isReviewMode(value: unknown): value is ReviewMode {
|
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
export {
|
|
158
|
+
CLASSIFY_NESTED_REFUSAL,
|
|
154
159
|
PLAN_MODE_REFUSAL,
|
|
155
160
|
PLAN_MODE_REFUSAL_WITH_PLAN_FILE,
|
|
156
161
|
STRICT_MODE_REFUSAL,
|
|
@@ -530,6 +535,32 @@ const CLASSIFIER_DELEGATION_TOOLS: ReadonlySet<string> = new Set([
|
|
|
530
535
|
'continue_task',
|
|
531
536
|
])
|
|
532
537
|
|
|
538
|
+
/**
|
|
539
|
+
* Whether an allow rule is one `classify` should not defer to for this call: the
|
|
540
|
+
* rule covers a whole shell tool, the line starts an interpreter or a launcher
|
|
541
|
+
* that runs whatever it is given, or the tool starts other agents. Judged on the
|
|
542
|
+
* call as well as the rule because a rule that names a program ("python") is a
|
|
543
|
+
* rule about a program that can run anything. A nested call is judged the same
|
|
544
|
+
* way, so a skill's command is held to what a direct one is.
|
|
545
|
+
*/
|
|
546
|
+
export function allowedCallTooWide(call: {
|
|
547
|
+
readonly name: string
|
|
548
|
+
readonly tool: Pick<ToolDefinition, 'commandArgument' | 'commandDialect'> | undefined
|
|
549
|
+
readonly input: unknown
|
|
550
|
+
readonly allowScope?: 'tool' | 'pattern' | undefined
|
|
551
|
+
/** The rule that allowed it, when the caller has the gate's verdict and not the scope. */
|
|
552
|
+
readonly allowRule?: AuthorizationRule | null
|
|
553
|
+
}): boolean {
|
|
554
|
+
if (CLASSIFIER_DELEGATION_TOOLS.has(call.name.toLowerCase())) return true
|
|
555
|
+
const argument = call.tool?.commandArgument
|
|
556
|
+
if (call.tool === undefined || argument === undefined) return false
|
|
557
|
+
if ((call.allowScope ?? allowRuleScope(call.allowRule ?? null)) === 'tool') return true
|
|
558
|
+
const line = (call.input as Record<string, unknown> | null)?.[argument]
|
|
559
|
+
if (typeof line !== 'string') return true
|
|
560
|
+
const reading = readShellLine(line, call.tool.commandDialect?.({ sandboxed: false }) ?? 'sh')
|
|
561
|
+
return !reading.complete || reading.invocations.some(launchesCode)
|
|
562
|
+
}
|
|
563
|
+
|
|
533
564
|
/** The shell commands `accept-edits` also approves, when every path they name is inside the roots. */
|
|
534
565
|
export const ACCEPT_EDITS_SHELL_COMMANDS: ReadonlySet<string> = new Set([
|
|
535
566
|
'mkdir',
|
|
@@ -546,7 +577,7 @@ export const ACCEPT_EDITS_SHELL_COMMANDS: ReadonlySet<string> = new Set([
|
|
|
546
577
|
* the first time in a session and nobody can be asked.
|
|
547
578
|
*/
|
|
548
579
|
export const SCREEN_CONSENT_UNATTENDED_REFUSAL =
|
|
549
|
-
"Refused: this call would send what is on the user's screen to the model provider, which a person agrees to once per session, and nobody can be asked in this session. Nothing in this batch ran. Tell the user computer use needs their consent in an interactive session."
|
|
580
|
+
"Refused: this call would send what is on the user's screen to the model provider, which a person agrees to once per session, and nobody can be asked in this session. Nothing in this batch ran. Do not try again in this run, and do not reach the screen another way. Tell the user computer use needs their consent in an interactive session, or a rule that allows this call by name."
|
|
550
581
|
|
|
551
582
|
/** What the model is told when the operator declines to share the screen. */
|
|
552
583
|
export const SCREEN_CONSENT_DECLINED_FEEDBACK =
|
|
@@ -1161,17 +1192,13 @@ export function createReviewHandler(options: ReviewPolicyOptions = {}): ResumeHa
|
|
|
1161
1192
|
* Judged on the call as well as the rule because a rule that names a
|
|
1162
1193
|
* program ("python") is a rule about a program that can run anything.
|
|
1163
1194
|
*/
|
|
1164
|
-
const allowRuleTooWide = (tc: ToolCallSummary): boolean =>
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
if (typeof line !== 'string') return true
|
|
1172
|
-
const reading = readShellLine(line, tool.commandDialect?.({ sandboxed: false }) ?? 'sh')
|
|
1173
|
-
return !reading.complete || reading.invocations.some(launchesCode)
|
|
1174
|
-
}
|
|
1195
|
+
const allowRuleTooWide = (tc: ToolCallSummary): boolean =>
|
|
1196
|
+
allowedCallTooWide({
|
|
1197
|
+
name: tc.name,
|
|
1198
|
+
tool: toolOf(tc.name),
|
|
1199
|
+
input: tc.input,
|
|
1200
|
+
allowScope: tc.authorization?.allowScope,
|
|
1201
|
+
})
|
|
1175
1202
|
|
|
1176
1203
|
/** Whether `classify` decides this call without the classifier: it would not be asked, or is plainly safe. */
|
|
1177
1204
|
const neverSent = async (tc: ToolCallSummary, sessionId?: string): Promise<boolean> => {
|
|
@@ -1241,7 +1268,9 @@ export function createReviewHandler(options: ReviewPolicyOptions = {}): ResumeHa
|
|
|
1241
1268
|
const sessionKey = String(request.sessionId ?? '')
|
|
1242
1269
|
if (
|
|
1243
1270
|
consent &&
|
|
1244
|
-
|
|
1271
|
+
// `auto` answers for a person who is there; with nobody to ask it cannot
|
|
1272
|
+
// answer for them, so the first look is refused in every mode.
|
|
1273
|
+
(mode !== 'auto' || !prompt) &&
|
|
1245
1274
|
!consent.sessions.has(sessionKey) &&
|
|
1246
1275
|
request.toolCalls.some(
|
|
1247
1276
|
(tc) =>
|
|
@@ -213,6 +213,12 @@ export interface ToolSkillShellHostOptions {
|
|
|
213
213
|
readonly context: ToolContext
|
|
214
214
|
readonly allow?: ToolDispatchOptions['allow']
|
|
215
215
|
readonly toolName?: string
|
|
216
|
+
/**
|
|
217
|
+
* The text is going to a model that can run a command in its own call. A command
|
|
218
|
+
* the turn's auto mode would send to its reviewer then comes back as a hand-off
|
|
219
|
+
* and the load goes on, where otherwise it fails.
|
|
220
|
+
*/
|
|
221
|
+
readonly handOff?: boolean
|
|
216
222
|
}
|
|
217
223
|
|
|
218
224
|
/**
|
|
@@ -240,6 +246,7 @@ export function createToolSkillShellHost(options: ToolSkillShellHostOptions): In
|
|
|
240
246
|
{ command },
|
|
241
247
|
{
|
|
242
248
|
...(options.allow ? { allow: options.allow } : {}),
|
|
249
|
+
...(options.handOff ? { handOff: true } : {}),
|
|
243
250
|
beforeRun: async () => {
|
|
244
251
|
const verdict = await beforeRun()
|
|
245
252
|
if (verdict.kind === 'run') return undefined
|
|
@@ -255,11 +262,20 @@ export function createToolSkillShellHost(options: ToolSkillShellHostOptions): In
|
|
|
255
262
|
throw error
|
|
256
263
|
}
|
|
257
264
|
if (hook !== undefined) return { kind: 'hook', verdict: hook }
|
|
265
|
+
if (!result.success && handedOff(result)) return { kind: 'handoff' }
|
|
258
266
|
return outcomeOf(result)
|
|
259
267
|
},
|
|
260
268
|
}
|
|
261
269
|
}
|
|
262
270
|
|
|
271
|
+
function handedOff(result: { readonly data?: unknown }): boolean {
|
|
272
|
+
return (
|
|
273
|
+
result.data !== null &&
|
|
274
|
+
typeof result.data === 'object' &&
|
|
275
|
+
(result.data as Record<string, unknown>).handedOff === true
|
|
276
|
+
)
|
|
277
|
+
}
|
|
278
|
+
|
|
263
279
|
/** A shell tool result as the host's outcome of one command. */
|
|
264
280
|
export function outcomeOf(result: {
|
|
265
281
|
readonly success: boolean
|
|
@@ -242,7 +242,7 @@ type ShellContext = 'none' | 'single' | 'double' | 'unsafe'
|
|
|
242
242
|
/**
|
|
243
243
|
* How the shell reads the position at the end of `prefix`, the text of one
|
|
244
244
|
* command up to a value: outside any quote, inside single or double quotes, or
|
|
245
|
-
* somewhere a value cannot be made safe (a comment, a dangling backslash
|
|
245
|
+
* somewhere a value cannot be made safe (a comment, a dangling backslash or a bare `$`, a here-document, a `${...}`, an
|
|
246
246
|
* arithmetic or backtick expansion, an ANSI-C string that is not closed).
|
|
247
247
|
*
|
|
248
248
|
* A `$( ... )` starts a fresh reading of its own, so a value inside one is
|
|
@@ -309,7 +309,9 @@ function shellContextAt(prefix: string): ShellContext {
|
|
|
309
309
|
i = end
|
|
310
310
|
wordStart = false
|
|
311
311
|
continue
|
|
312
|
-
}
|
|
312
|
+
} // A `$` right before the value would join its first character: `$` and a
|
|
313
|
+
// quote read as an ANSI-C string, `$` and `(` or a name as an expansion.
|
|
314
|
+
if (i === prefix.length - 1) return 'unsafe'
|
|
313
315
|
}
|
|
314
316
|
if (c === '`') {
|
|
315
317
|
const end = skipTo(i + 1, '`')
|
|
@@ -442,7 +444,7 @@ function quoteForCommand(value: string, prefix: string, dialect: ShellDialect):
|
|
|
442
444
|
if (context === 'unsafe' || value.includes('`')) {
|
|
443
445
|
throw new InlineShellError(
|
|
444
446
|
context === 'unsafe'
|
|
445
|
-
? 'A value given to this skill or command would land inside a here-document
|
|
447
|
+
? 'A value given to this skill or command would land inside a here-document, a ${...} or arithmetic expansion, or right after a bare $ of a shell command, where it cannot be quoted, so it was not loaded.'
|
|
446
448
|
: 'A value given to this skill or command contains a backtick, which cannot be put inside a shell command, so it was not loaded.',
|
|
447
449
|
'injection',
|
|
448
450
|
)
|
|
@@ -470,7 +472,7 @@ interface Occurrence {
|
|
|
470
472
|
* reads it as data ({@link quoteForCommand}), by where it sits: a person's
|
|
471
473
|
* `-n 5` stays two arguments, and `x; touch f #` stays words of an argument
|
|
472
474
|
* list. Throws {@link InlineShellError} when a value cannot be made safe, or the
|
|
473
|
-
* substituted text
|
|
475
|
+
* substituted text does not hold exactly the template's commands, in order.
|
|
474
476
|
*/
|
|
475
477
|
export function substituteEscaped(
|
|
476
478
|
template: string,
|
|
@@ -514,6 +516,8 @@ export function substituteEscaped(
|
|
|
514
516
|
return marker.kind === 'inline' ? marker.start + 2 : neutral.indexOf('\n', marker.start) + 1
|
|
515
517
|
}
|
|
516
518
|
let out = ''
|
|
519
|
+
// Where each value landed: its place in the neutral text (one character wide) and in the output.
|
|
520
|
+
const landed: Array<{ at: number; outAt: number; length: number }> = []
|
|
517
521
|
for (const piece of pieces) {
|
|
518
522
|
if (typeof piece === 'string') {
|
|
519
523
|
out += piece
|
|
@@ -521,7 +525,7 @@ export function substituteEscaped(
|
|
|
521
525
|
}
|
|
522
526
|
const value = values[piece.token] as string
|
|
523
527
|
const from = commandAt(piece.at)
|
|
524
|
-
|
|
528
|
+
const put =
|
|
525
529
|
from === undefined
|
|
526
530
|
? escapeBetween(value, piece.before, piece.after)
|
|
527
531
|
: quoteForCommand(
|
|
@@ -529,8 +533,26 @@ export function substituteEscaped(
|
|
|
529
533
|
neutral.slice(from, piece.at),
|
|
530
534
|
options.dialect ?? hostCommandShell().dialect,
|
|
531
535
|
)
|
|
536
|
+
landed.push({ at: piece.at, outAt: out.length, length: put.length })
|
|
537
|
+
out += put
|
|
538
|
+
}
|
|
539
|
+
// A neutral offset (a marker's edge) in the output text.
|
|
540
|
+
const inOutput = (at: number): number => {
|
|
541
|
+
let shift = 0
|
|
542
|
+
for (const place of landed) {
|
|
543
|
+
if (place.at >= at) break
|
|
544
|
+
shift += place.length - 1
|
|
545
|
+
}
|
|
546
|
+
return at + shift
|
|
532
547
|
}
|
|
533
|
-
|
|
548
|
+
// The commands the author wrote, values quoted in, are what must be there: the same
|
|
549
|
+
// ones, in the same order. A count would let a value that masks one command and
|
|
550
|
+
// creates another through.
|
|
551
|
+
const expected = markers.map(
|
|
552
|
+
(m) => `${m.kind}\u0000${out.slice(inOutput(m.start), inOutput(m.end))}`,
|
|
553
|
+
)
|
|
554
|
+
const found = findInlineShell(out).map((m) => `${m.kind}\u0000${m.raw}`)
|
|
555
|
+
if (expected.length !== found.length || expected.some((raw, index) => raw !== found[index])) {
|
|
534
556
|
throw new InlineShellError(
|
|
535
557
|
'A value given to this skill or command contains text that would start a shell command, so it was not loaded.',
|
|
536
558
|
'injection',
|
|
@@ -571,6 +593,12 @@ export type InlineShellHookVerdict =
|
|
|
571
593
|
export type InlineShellOutcome =
|
|
572
594
|
/** The permission rules said no, or would have asked a person who is not there. */
|
|
573
595
|
| { readonly kind: 'denied'; readonly reason: string }
|
|
596
|
+
/**
|
|
597
|
+
* Not run here: the session's auto-mode reviewer judges only the commands the model
|
|
598
|
+
* runs itself, so the text asks the model to run this one in its own `bash` call and
|
|
599
|
+
* the load goes on. Only a host with a model to hand it to answers this.
|
|
600
|
+
*/
|
|
601
|
+
| { readonly kind: 'handoff' }
|
|
574
602
|
/** The hook, which runs after permission and before the shell, refused or stopped. */
|
|
575
603
|
| { readonly kind: 'hook'; readonly verdict: Exclude<InlineShellHookVerdict, { kind: 'run' }> }
|
|
576
604
|
| {
|
|
@@ -672,7 +700,10 @@ export async function expandInlineShell(
|
|
|
672
700
|
}
|
|
673
701
|
}
|
|
674
702
|
|
|
675
|
-
|
|
703
|
+
// A string is the text for that spot; a number is the place of a hand-off, whose note
|
|
704
|
+
// is final once the count is known. Never a marker inside the text, which could collide.
|
|
705
|
+
const replacements: (string | number)[] = []
|
|
706
|
+
const handedOff: string[] = []
|
|
676
707
|
let total = 0
|
|
677
708
|
for (const marker of markers) {
|
|
678
709
|
options.signal?.throwIfAborted()
|
|
@@ -688,6 +719,11 @@ export async function expandInlineShell(
|
|
|
688
719
|
'permission',
|
|
689
720
|
)
|
|
690
721
|
}
|
|
722
|
+
if (outcome.kind === 'handoff') {
|
|
723
|
+
handedOff.push(marker.command)
|
|
724
|
+
replacements.push(handedOff.length - 1)
|
|
725
|
+
continue
|
|
726
|
+
}
|
|
691
727
|
if (outcome.kind === 'hook') {
|
|
692
728
|
const verdict = outcome.verdict
|
|
693
729
|
if (verdict.kind === 'stop') {
|
|
@@ -731,7 +767,53 @@ export async function expandInlineShell(
|
|
|
731
767
|
replacements.push(shaped)
|
|
732
768
|
}
|
|
733
769
|
let n = 0
|
|
734
|
-
|
|
770
|
+
if (handedOff.length === 0) {
|
|
771
|
+
return replaceMarkers(source, markers, () => replacements[n++] as string)
|
|
772
|
+
}
|
|
773
|
+
const { header, spot } = handOffNotes(handedOff)
|
|
774
|
+
const body = replaceMarkers(source, markers, () => {
|
|
775
|
+
const r = replacements[n++] as string | number
|
|
776
|
+
return typeof r === 'number' ? spot(r) : r
|
|
777
|
+
})
|
|
778
|
+
return header === '' ? body : `${header}\n\n${body}`
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/** A command as it is written in a hand-off note: in backticks, or fenced when it holds a backtick or a newline. */
|
|
782
|
+
function fenced(command: string): string {
|
|
783
|
+
const longest = Math.max(0, ...(command.match(/`+/g) ?? []).map((run) => run.length))
|
|
784
|
+
const fence = '`'.repeat(Math.max(3, longest + 1))
|
|
785
|
+
return `${fence}\n${command}\n${fence}`
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* The hand-off notes for the commands a model is asked to run itself. One command: its
|
|
790
|
+
* note stands where the command was. Several: a short numbered list goes first (the
|
|
791
|
+
* header), and each spot names its number.
|
|
792
|
+
*/
|
|
793
|
+
function handOffNotes(commands: readonly string[]): {
|
|
794
|
+
header: string
|
|
795
|
+
spot: (index: number) => string
|
|
796
|
+
} {
|
|
797
|
+
const plain = (command: string) => !/[`\n]/.test(command)
|
|
798
|
+
if (commands.length === 1) {
|
|
799
|
+
const command = commands[0] as string
|
|
800
|
+
const lead = '[run this first, exactly as written, with the bash tool and use its output:'
|
|
801
|
+
const note = plain(command) ? `${lead} \`${command}\`]` : `${lead}]\n${fenced(command)}`
|
|
802
|
+
return { header: '', spot: () => note }
|
|
803
|
+
}
|
|
804
|
+
const head = `[Run these ${commands.length} commands first, exactly as written, with the bash tool, and use their output where each is named below. Run them one per call.]`
|
|
805
|
+
const list = commands.map((command, i) =>
|
|
806
|
+
plain(command) ? `${i + 1}. \`${command}\`` : `${i + 1}.\n${fenced(command)}`,
|
|
807
|
+
)
|
|
808
|
+
return {
|
|
809
|
+
header: [head, ...list].join('\n'),
|
|
810
|
+
spot: (i) => {
|
|
811
|
+
const command = commands[i] as string
|
|
812
|
+
return plain(command)
|
|
813
|
+
? `[output of command ${i + 1}, \`${command}\`]`
|
|
814
|
+
: `[output of command ${i + 1}]`
|
|
815
|
+
},
|
|
816
|
+
}
|
|
735
817
|
}
|
|
736
818
|
|
|
737
819
|
function replaceMarkers(
|
|
@@ -432,6 +432,8 @@ export function createSkillTool(options: SkillToolOptions = {}) {
|
|
|
432
432
|
host: createToolSkillShellHost({
|
|
433
433
|
context: input.context,
|
|
434
434
|
...(allow ? { allow } : {}),
|
|
435
|
+
// Only a model loaded this, so only a model can run a command in its own call.
|
|
436
|
+
...(input.invokedBy === 'model' ? { handOff: true } : {}),
|
|
435
437
|
}),
|
|
436
438
|
commandName: input.name,
|
|
437
439
|
...(block !== undefined ? { block } : {}),
|
|
@@ -42,13 +42,30 @@ export const PLAN_MODE_REFUSAL_WITH_PLAN_FILE =
|
|
|
42
42
|
* - `strict` runs only calls a rule allowed (or that only read). A skill's
|
|
43
43
|
* grant is not a rule, so it never lifts this.
|
|
44
44
|
*
|
|
45
|
-
*
|
|
46
|
-
* classifier
|
|
45
|
+
* - `classify` (auto) sends every call but a read, or one a narrow rule allows, to
|
|
46
|
+
* the classifier. A nested call has no classifier to go to, so it is refused,
|
|
47
|
+
* as the direct call would not have run unreviewed. A rule that covers a whole
|
|
48
|
+
* shell tool or a program that runs whatever it is given is too wide to stand
|
|
49
|
+
* in for the reviewer (`ruleTooWide`), as it is for a direct call.
|
|
50
|
+
*
|
|
51
|
+
* Every other mode answers a call that no rule decided by asking a person,
|
|
52
|
+
* which a load cannot do; that stays the caller's refusal.
|
|
47
53
|
*/
|
|
54
|
+
export const CLASSIFY_NESTED_REFUSAL =
|
|
55
|
+
'auto mode reviews only the commands you run yourself, so it does not run this one from inside a skill; run it as a direct call so it can be reviewed'
|
|
56
|
+
|
|
48
57
|
export function nestedCallModeRefusal(
|
|
49
58
|
mode: string | undefined,
|
|
50
|
-
call: {
|
|
59
|
+
call: {
|
|
60
|
+
readonly readOnly: boolean
|
|
61
|
+
readonly allowedByRule: boolean
|
|
62
|
+
/** The rule that allowed it covers a whole tool, or a line that launches code. */
|
|
63
|
+
readonly ruleTooWide?: boolean
|
|
64
|
+
},
|
|
51
65
|
): string | undefined {
|
|
66
|
+
if (mode === 'classify' && !call.readOnly && !(call.allowedByRule && call.ruleTooWide !== true)) {
|
|
67
|
+
return CLASSIFY_NESTED_REFUSAL
|
|
68
|
+
}
|
|
52
69
|
if (mode === 'plan' && !call.readOnly) {
|
|
53
70
|
return 'plan mode only runs commands that read, and this one can change something'
|
|
54
71
|
}
|
package/src/types/tool/index.ts
CHANGED
|
@@ -637,6 +637,13 @@ export interface ToolDispatchOptions {
|
|
|
637
637
|
* floor stopped, and never covers a call the tool declares needs approval.
|
|
638
638
|
*/
|
|
639
639
|
readonly allow?: (call: { readonly name: string; readonly input: unknown }) => boolean
|
|
640
|
+
/**
|
|
641
|
+
* For a tool that is loading text for a model: a call that the turn's auto mode
|
|
642
|
+
* would send to its reviewer (and that no rule denies) is not run and not treated
|
|
643
|
+
* as a failure. It comes back unsuccessful with `data.handedOff === true`, so the
|
|
644
|
+
* caller can ask the model to make the call itself, where the reviewer sees it.
|
|
645
|
+
*/
|
|
646
|
+
readonly handOff?: boolean
|
|
640
647
|
/**
|
|
641
648
|
* Asked after the call is allowed and before it starts. Returns `undefined`
|
|
642
649
|
* to let it run, or the reason it is not to run, which comes back as the
|