Copilot commented on code in PR #886: URL: https://github.com/apache/burr/pull/886#discussion_r4420542753
########## examples/coding-agent/tools.py: ########## @@ -0,0 +1,87 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Tools the coding agent can call. Each takes typed args and returns a dict.""" + +import os +import subprocess + +# Everything is confined to this directory. See README on why this is not a sandbox. +WORKSPACE = os.environ.get("CODING_AGENT_WORKSPACE", "./workspace") + +# The agent may run a command before it has written anything, so make sure the +# directory exists rather than letting subprocess fail on a missing cwd. +os.makedirs(WORKSPACE, exist_ok=True) + + +def _resolve(path: str) -> str: + """Resolve a path inside the workspace, refusing anything that escapes it.""" + root = os.path.realpath(WORKSPACE) + full = os.path.realpath(os.path.join(root, path)) + if not full.startswith(root + os.sep) and full != root: + raise ValueError(f"path escapes workspace: {path}") + return full + + +def list_files(directory: str = ".") -> dict: + """Lists the files and directories at the given path inside the workspace.""" + try: + target = _resolve(directory) + return {"entries": sorted(os.listdir(target))} + except (ValueError, OSError) as e: + return {"error": str(e)} + + +def read_file(path: str) -> dict: + """Reads a text file from the workspace and returns its contents.""" + try: + with open(_resolve(path)) as f: + return {"path": path, "contents": f.read()} + except (ValueError, OSError) as e: + return {"error": str(e)} + + +def write_file(path: str, contents: str) -> dict: + """Writes text to a file in the workspace, creating or overwriting it.""" + try: + full = _resolve(path) + os.makedirs(os.path.dirname(full), exist_ok=True) + with open(full, "w") as f: + f.write(contents) Review Comment: If the model supplies `contents: null` or a list, opening the destination in `"w"` mode truncates an existing file before `f.write` raises `TypeError`; `execute_tool` reports bad arguments only after the original contents are lost. Validate that `contents` is a string before opening the destination. Apply the same check in `examples/coding-agent/notebook.ipynb:131–134`. ########## examples/coding-agent/tools.py: ########## @@ -0,0 +1,87 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Tools the coding agent can call. Each takes typed args and returns a dict.""" + +import os +import subprocess + +# Everything is confined to this directory. See README on why this is not a sandbox. +WORKSPACE = os.environ.get("CODING_AGENT_WORKSPACE", "./workspace") + +# The agent may run a command before it has written anything, so make sure the +# directory exists rather than letting subprocess fail on a missing cwd. +os.makedirs(WORKSPACE, exist_ok=True) + + +def _resolve(path: str) -> str: + """Resolve a path inside the workspace, refusing anything that escapes it.""" + root = os.path.realpath(WORKSPACE) + full = os.path.realpath(os.path.join(root, path)) + if not full.startswith(root + os.sep) and full != root: + raise ValueError(f"path escapes workspace: {path}") + return full + + +def list_files(directory: str = ".") -> dict: + """Lists the files and directories at the given path inside the workspace.""" + try: + target = _resolve(directory) + return {"entries": sorted(os.listdir(target))} + except (ValueError, OSError) as e: + return {"error": str(e)} + + +def read_file(path: str) -> dict: + """Reads a text file from the workspace and returns its contents.""" + try: + with open(_resolve(path)) as f: + return {"path": path, "contents": f.read()} + except (ValueError, OSError) as e: + return {"error": str(e)} + + +def write_file(path: str, contents: str) -> dict: + """Writes text to a file in the workspace, creating or overwriting it.""" + try: + full = _resolve(path) + os.makedirs(os.path.dirname(full), exist_ok=True) + with open(full, "w") as f: + f.write(contents) + return {"path": path, "bytes_written": len(contents)} + except (ValueError, OSError) as e: + return {"error": str(e)} + + +def run_bash(command: str) -> dict: + """Runs a shell command in the workspace and returns its output.""" + try: + proc = subprocess.run( + command, + shell=True, + cwd=os.path.abspath(WORKSPACE), + capture_output=True, + text=True, + timeout=30, Review Comment: On POSIX, this timeout kills the shell created by `shell=True`, but child commands such as `python long_job.py; :` can keep running and modifying files after the tool reports a timeout. Run commands in a separate process group and terminate the group on timeout, waiting for cleanup before returning and handling other platforms appropriately. Apply the same cleanup in `examples/coding-agent/notebook.ipynb:143–157`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
