Verify the declared Node entry before completing Agent tasks
This commit is contained in:
1 parent
da93d4fa1c
commit
6d55806b4f
6 files changed
+113
-9
No files matched your search
@@ -257,6 +257,9 @@ try:
|
||||
if len(files)<100:files.append({'path':str(file.relative_to(root)),'bytes':size})
|
||||
if not complete:break
|
||||
result={'sourceFiles':source_count,'files':files,'totalBytes':total,'complete':complete}
|
||||
missing_start = workspace_missing_start(root)
|
||||
if missing_start:
|
||||
result['missingStartFile'] = missing_start
|
||||
elif name=='archive_source':
|
||||
import io,zipfile
|
||||
output=io.BytesIO();total=0;count=0
|
||||
|
||||
@@ -71,6 +71,45 @@ def review_export(root, release=False):
|
||||
'complete': True, 'totalBytes': total}
|
||||
|
||||
|
||||
def node_start_entry(scripts):
|
||||
start = scripts.get('start')
|
||||
build = scripts.get('build')
|
||||
if not isinstance(start, str) or (isinstance(build, str) and build.strip()):
|
||||
return None
|
||||
simple = re.fullmatch(r'node\s+(?:\./)?([\w./-]+\.(?:mjs|cjs|js))', start.strip())
|
||||
if not simple:
|
||||
return None
|
||||
entry = pathlib.PurePosixPath(simple.group(1))
|
||||
if entry.is_absolute() or '..' in entry.parts or simple.group(1).startswith('-'):
|
||||
return None
|
||||
return entry.as_posix()
|
||||
|
||||
|
||||
def workspace_missing_start(root):
|
||||
package_file = root / 'package.json'
|
||||
try:
|
||||
if package_file.is_symlink() or not package_file.is_file():
|
||||
return None
|
||||
with package_file.open('rb') as stream:
|
||||
content = stream.read(256_001)
|
||||
if len(content) > 256_000:
|
||||
return None
|
||||
package = json.loads(content)
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
scripts = package.get('scripts') if isinstance(package, dict) else None
|
||||
entry = node_start_entry(scripts) if isinstance(scripts, dict) else None
|
||||
if entry:
|
||||
target = root / entry
|
||||
try:
|
||||
if target.resolve() == target and target.is_file():
|
||||
return None
|
||||
except (OSError, RuntimeError):
|
||||
pass
|
||||
return entry
|
||||
return None
|
||||
|
||||
|
||||
def release_snapshot(root):
|
||||
snapshot = review_export(root, release=True)
|
||||
data = base64.b64decode(snapshot['archive'])
|
||||
@@ -90,9 +129,9 @@ def release_snapshot(root):
|
||||
or package.get('optionalDependencies'))
|
||||
if start:
|
||||
runtime = 'NODE'
|
||||
simple = re.fullmatch(r'node\s+(?:\./)?([\w./-]+\.(?:mjs|cjs|js))', start)
|
||||
if simple and not build and simple.group(1) not in names:
|
||||
raise ValueError('Start file missing: ' + simple.group(1))
|
||||
entry = node_start_entry(scripts)
|
||||
if entry and entry not in names:
|
||||
raise ValueError('Start file missing: ' + entry)
|
||||
elif build:
|
||||
runtime = 'STATIC_BUILD'
|
||||
elif 'index.html' in names:
|
||||
|
||||
@@ -56,6 +56,21 @@ class GatewayTests(unittest.TestCase):
|
||||
with zipfile.ZipFile(io.BytesIO(base64.b64decode(result["archive"]))) as archive:
|
||||
self.assertEqual({"package.json", "server.js"}, set(archive.namelist()))
|
||||
|
||||
def test_workspace_inspection_reports_missing_start_even_with_other_source(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / "index.js").write_text("console.log('other source')")
|
||||
package = root / "package.json"
|
||||
package.write_text('{"scripts":{"start":"node ./src/server.cjs"}}')
|
||||
result = self.execute_file_tool(root, "workspace_inspect", {})
|
||||
self.assertEqual(1, result["sourceFiles"])
|
||||
self.assertEqual("src/server.cjs", result["missingStartFile"])
|
||||
(root / "src").mkdir()
|
||||
(root / "src/server.cjs").write_text("console.log('ready')")
|
||||
self.assertNotIn("missingStartFile", self.execute_file_tool(root, "workspace_inspect", {}))
|
||||
package.write_text('{"scripts":{"start":"node dist/server.js","build":"tsc"}}')
|
||||
self.assertNotIn("missingStartFile", self.execute_file_tool(root, "workspace_inspect", {}))
|
||||
|
||||
def test_agent_file_change_counts_and_manual_edits_are_separate(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
|
||||
Reference in new issue
Block a user