Skip to content

Commit 9415f7e

Browse files
fix(sandbox): preserve multiline skill frontmatter descriptions (#5098)
Co-authored-by: ChiFungHillmanChan <[email protected]>
1 parent 165390c commit 9415f7e

2 files changed

Lines changed: 577 additions & 11 deletions

File tree

‎src/agents/sandbox/capabilities/skills.py‎

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import abc
44
import io
5+
import re
56
import stat
67
from collections.abc import Mapping, Sequence
78
from dataclasses import dataclass, field
@@ -380,36 +381,134 @@ def _get_manifest_entry_by_path(manifest: Manifest, path: Path) -> BaseEntry | N
380381
return None
381382

382383

384+
_BLOCK_SCALAR_HEADER = re.compile(r"[>|](?:[1-9][+-]?|[+-][1-9]?)?(?:[ \t]+#.*)?")
385+
386+
387+
def _indent_of(line: str) -> int:
388+
"""Count indentation spaces, leaving tabs in scalar content."""
389+
390+
return len(line) - len(line.lstrip(" "))
391+
392+
393+
def _fold_lines(lines: list[str]) -> str:
394+
"""Fold ordinary lines, retaining paragraphs and more-indented block content."""
395+
396+
parts: list[str] = []
397+
previous = ""
398+
blank_lines = 0
399+
for line in lines:
400+
if not line:
401+
blank_lines += 1
402+
continue
403+
if parts:
404+
more_indented = previous.startswith((" ", "\t")) or line.startswith((" ", "\t"))
405+
parts.append("\n" * (blank_lines + 1) if more_indented else "\n" * blank_lines or " ")
406+
parts.append(line)
407+
previous = line
408+
blank_lines = 0
409+
return "".join(parts)
410+
411+
412+
def _take_continuation_lines(
413+
lines: list[str], start: int, end: int, key_indent: int, *, block_header: str | None
414+
) -> tuple[list[str], int]:
415+
"""Consume a field's indented body and strip a block scalar's content indentation."""
416+
417+
content_indent: int | None = None
418+
if block_header is not None:
419+
content_indent = next(
420+
(key_indent + int(char) for char in block_header[1:] if char.isdigit()), None
421+
)
422+
body: list[str] = []
423+
index = start
424+
while index < end:
425+
line = lines[index]
426+
stripped = line.strip()
427+
# Plain-scalar comments may have any indentation and are not scalar content.
428+
if block_header is None and stripped.startswith("#"):
429+
index += 1
430+
continue
431+
if stripped:
432+
indent = _indent_of(line)
433+
# A dedented comment ends a block just like any other nonblank line.
434+
if indent < (content_indent if content_indent is not None else key_indent + 1):
435+
break
436+
if block_header is not None and content_indent is None:
437+
content_indent = indent
438+
body.append(line)
439+
index += 1
440+
while body and not body[-1].strip():
441+
body.pop()
442+
if content_indent is not None:
443+
body = [line[content_indent:] for line in body]
444+
return body, index
445+
446+
383447
def _parse_frontmatter(markdown: str) -> dict[str, str]:
384-
"""Parse the simple YAML frontmatter shape used by skill indexes."""
448+
"""Read the string metadata subset used by skill indexes, without a YAML dependency.
449+
450+
Supports single-line values, indented wrapped plain values, and folded/literal blocks
451+
with optional indentation/chomping indicators and header comments. Block values have
452+
outer whitespace removed for the index; chomping indicators are accepted but do not
453+
change that normalization. Internal paragraph breaks and block indentation survive.
454+
455+
This is not a general YAML loader: tags, aliases, collections, multiline quoted values,
456+
and quoted escape decoding are not interpreted. Nested fields are consumed without
457+
promoting their keys. Single-line quote stripping and inline hash text retain their
458+
historical behavior. Supply explicit Skill metadata or a LazySkillSource when a full
459+
YAML loader is needed.
460+
"""
385461

386462
lines = markdown.splitlines()
387463
if not lines or lines[0].strip() != "---":
388464
return {}
389465

390-
end_index: int | None = None
391-
for index, line in enumerate(lines[1:], start=1):
392-
if line.strip() == "---":
393-
end_index = index
394-
break
466+
# Indented document markers inside scalar content do not close the frontmatter.
467+
delimiter_indent = _indent_of(lines[0])
468+
end_index = next(
469+
(
470+
index
471+
for index, line in enumerate(lines[1:], start=1)
472+
if line.strip() == "---" and _indent_of(line) <= delimiter_indent
473+
),
474+
None,
475+
)
395476
if end_index is None:
396477
return {}
397478

398479
metadata: dict[str, str] = {}
399-
for line in lines[1:end_index]:
480+
index = 1
481+
while index < end_index:
482+
line = lines[index]
483+
index += 1
400484
stripped = line.strip()
401-
if stripped == "" or stripped.startswith("#") or ":" not in stripped:
485+
if not stripped or stripped.startswith("#") or ":" not in stripped:
402486
continue
403487
key, value = stripped.split(":", 1)
404-
parsed_key = key.strip()
405488
parsed_value = value.strip()
406-
if (
489+
# Header comments cannot supply an indentation or chomping indicator.
490+
block_header = (
491+
parsed_value.split()[0] if _BLOCK_SCALAR_HEADER.fullmatch(parsed_value) else None
492+
)
493+
key_indent = _indent_of(line)
494+
continuation, index = _take_continuation_lines(
495+
lines, index, end_index, key_indent, block_header=block_header
496+
)
497+
498+
if block_header is not None:
499+
parsed_value = (
500+
"\n".join(continuation) if block_header[0] == "|" else _fold_lines(continuation)
501+
).strip()
502+
elif (
407503
len(parsed_value) >= 2
408504
and parsed_value[0] == parsed_value[-1]
409505
and parsed_value[0] in {"'", '"'}
410506
):
411507
parsed_value = parsed_value[1:-1]
412-
metadata[parsed_key] = parsed_value
508+
elif parsed_value and continuation:
509+
parsed_value = _fold_lines([parsed_value, *(item.strip() for item in continuation)])
510+
511+
metadata[key.strip()] = parsed_value
413512
return metadata
414513

415514

0 commit comments

Comments
 (0)