@@ -7,6 +7,8 @@ import path from "node:path";
77import { URL } from "node:url" ;
88import { detectMime } from "@openclaw/media-core/mime" ;
99import { formatByteSize } from "@openclaw/normalization-core" ;
10+ import type { Static , TSchema } from "typebox" ;
11+ import { Value } from "typebox/value" ;
1012import { isWindowsDrivePath } from "../infra/archive-path.js" ;
1113import { isMissingPathError , toErrorObject } from "../infra/errors.js" ;
1214import {
@@ -38,7 +40,7 @@ import {
3840 withMemoryWriteProvenance ,
3941} from "./memory-write-provenance.js" ;
4042import { toRelativeWorkspacePath } from "./path-policy.js" ;
41- import type { AgentToolResult } from "./runtime/index.js" ;
43+ import type { AgentTool , AgentToolResult } from "./runtime/index.js" ;
4244import { assertSandboxPath } from "./sandbox-paths.js" ;
4345import type { SandboxFsBridge } from "./sandbox/fs-bridge.js" ;
4446import {
@@ -73,6 +75,27 @@ type SkillReadContent = {
7375 readContent ?: string ;
7476} ;
7577
78+ /** Erase a schema-specific session tool only after its input passes that owned schema. */
79+ function eraseSessionFileTool < TParameters extends TSchema , TDetails > (
80+ tool : AgentTool < TParameters , TDetails > ,
81+ ) : AnyAgentTool {
82+ return {
83+ ...tool ,
84+ execute : async ( toolCallId , params , signal , onUpdate ) => {
85+ if ( ! Value . Check ( tool . parameters , params ) ) {
86+ throw new Error ( `Invalid parameters for ${ tool . name } ` ) ;
87+ }
88+ const typedParams = params as Static < TParameters > ;
89+ return await tool . execute (
90+ toolCallId ,
91+ typedParams ,
92+ signal ,
93+ onUpdate ? ( update ) => onUpdate ( update ) : undefined ,
94+ ) ;
95+ } ,
96+ } ;
97+ }
98+
7699type ReadTruncationDetails = {
77100 truncated : boolean ;
78101 outputLines : number ;
@@ -885,9 +908,11 @@ type SandboxToolParams = {
885908export function createSandboxedReadTool (
886909 params : SandboxToolParams & { createTool ?: typeof createReadTool } ,
887910) {
888- const base = ( params . createTool ?? createReadTool ) ( params . root , {
889- operations : createSandboxReadOperations ( params ) ,
890- } ) as unknown as AnyAgentTool ;
911+ const base = eraseSessionFileTool (
912+ ( params . createTool ?? createReadTool ) ( params . root , {
913+ operations : createSandboxReadOperations ( params ) ,
914+ } ) ,
915+ ) ;
891916 return createOpenClawReadTool ( base , {
892917 modelContextWindowTokens : params . modelContextWindowTokens ,
893918 imageSanitization : params . imageSanitization ,
@@ -898,19 +923,23 @@ export function createSandboxedReadTool(
898923export function createSandboxedWriteTool (
899924 params : SandboxToolParams & { createTool ?: typeof createWriteTool } ,
900925) {
901- const base = ( params . createTool ?? createWriteTool ) ( params . root , {
902- operations : createSandboxWriteOperations ( params ) ,
903- } ) as unknown as AnyAgentTool ;
926+ const base = eraseSessionFileTool (
927+ ( params . createTool ?? createWriteTool ) ( params . root , {
928+ operations : createSandboxWriteOperations ( params ) ,
929+ } ) ,
930+ ) ;
904931 return wrapToolParamValidation ( base , REQUIRED_PARAM_GROUPS . write ) ;
905932}
906933
907934/** Create a sandbox-backed edit tool with required-parameter validation. */
908935export function createSandboxedEditTool (
909936 params : SandboxToolParams & { createTool ?: typeof createEditTool } ,
910937) {
911- const base = ( params . createTool ?? createEditTool ) ( params . root , {
912- operations : createSandboxEditOperations ( params ) ,
913- } ) as unknown as AnyAgentTool ;
938+ const base = eraseSessionFileTool (
939+ ( params . createTool ?? createEditTool ) ( params . root , {
940+ operations : createSandboxEditOperations ( params ) ,
941+ } ) ,
942+ ) ;
914943 return wrapToolParamValidation ( base , REQUIRED_PARAM_GROUPS . edit ) ;
915944}
916945
@@ -923,9 +952,11 @@ export function createHostWorkspaceWriteTool(
923952 createTool ?: typeof createWriteTool ;
924953 } ,
925954) {
926- const base = ( options ?. createTool ?? createWriteTool ) ( root , {
927- operations : createHostWriteOperations ( root , options ) ,
928- } ) as unknown as AnyAgentTool ;
955+ const base = eraseSessionFileTool (
956+ ( options ?. createTool ?? createWriteTool ) ( root , {
957+ operations : createHostWriteOperations ( root , options ) ,
958+ } ) ,
959+ ) ;
929960 return wrapToolParamValidation ( base , REQUIRED_PARAM_GROUPS . write ) ;
930961}
931962
@@ -938,9 +969,11 @@ export function createHostWorkspaceEditTool(
938969 createTool ?: typeof createEditTool ;
939970 } ,
940971) {
941- const base = ( options ?. createTool ?? createEditTool ) ( root , {
942- operations : createHostEditOperations ( root , options ) ,
943- } ) as unknown as AnyAgentTool ;
972+ const base = eraseSessionFileTool (
973+ ( options ?. createTool ?? createEditTool ) ( root , {
974+ operations : createHostEditOperations ( root , options ) ,
975+ } ) ,
976+ ) ;
944977 return wrapToolParamValidation ( base , REQUIRED_PARAM_GROUPS . edit ) ;
945978}
946979
@@ -1003,13 +1036,15 @@ export function wrapReadToolWithSkillContent(
10031036 }
10041037 return content ;
10051038 } ;
1006- const virtualBase = createReadTool ( "/" , {
1007- operations : {
1008- resolvePath : ( filePath ) => filePath ,
1009- access : async ( filePath ) => void readContent ( filePath ) ,
1010- readFile : async ( filePath ) => Buffer . from ( readContent ( filePath ) , "utf8" ) ,
1011- } ,
1012- } ) as unknown as AnyAgentTool ;
1039+ const virtualBase = eraseSessionFileTool (
1040+ createReadTool ( "/" , {
1041+ operations : {
1042+ resolvePath : ( filePath ) => filePath ,
1043+ access : async ( filePath ) => void readContent ( filePath ) ,
1044+ readFile : async ( filePath ) => Buffer . from ( readContent ( filePath ) , "utf8" ) ,
1045+ } ,
1046+ } ) ,
1047+ ) ;
10131048 const virtualRead = createOpenClawReadTool ( virtualBase , options ) ;
10141049 return {
10151050 ...tool ,
0 commit comments