Compare commits

...

3 Commits

Author SHA1 Message Date
rohoswagger
43a59e4d74 chore: trim redundant source-count logging in generate_agents_md.py
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 14:33:17 -08:00
rohoswagger
dc8fc7eefc refactor: simplify to single-step AGENTS.md generation via stdin pipe
Instead of writing AGENTS.md with the placeholder and then calling the
script to replace it, pipe the template directly into the script via
stdin so the final file is written in one step.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 14:07:25 -08:00
rohoswagger
78f7914093 fix: invoke generate_agents_md.py in K8s to populate knowledge sources
The KNOWLEDGE_SOURCES_SECTION placeholder in AGENTS.md was never being
replaced in Kubernetes environments. The script existed in the container
image but was never called during session setup.

Now the K8s setup script calls generate_agents_md.py after writing
AGENTS.md, scanning the files symlink to populate the knowledge sources
section. Also updated the script to accept CLI args instead of env vars
to fit the invocation context.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 13:34:29 -08:00
2 changed files with 42 additions and 45 deletions

View File

@@ -1,15 +1,19 @@
#!/usr/bin/env python3
"""Generate AGENTS.md by scanning the files directory and populating the template.
This script runs at container startup, AFTER the init container has synced files
from S3. It scans the /workspace/files directory to discover what knowledge sources
are available and generates appropriate documentation.
This script runs during session setup, AFTER files have been synced from S3
and the files symlink has been created. It reads the template from stdin,
replaces the {{KNOWLEDGE_SOURCES_SECTION}} placeholder by scanning the
knowledge source directory, and writes the final AGENTS.md to the output path.
Environment variables:
- AGENT_INSTRUCTIONS: The template content with placeholders to replace
Usage:
printf '%s' "$TEMPLATE" | python3 generate_agents_md.py <output_path> <files_path>
Arguments:
output_path: Path to write the final AGENTS.md
files_path: Path to the files directory to scan for knowledge sources
"""
import os
import sys
from pathlib import Path
@@ -189,49 +193,39 @@ def build_knowledge_sources_section(files_path: Path) -> str:
def main() -> None:
"""Main entry point for container startup script.
Is called by the container startup script to scan /workspace/files and populate
the knowledge sources section.
Reads the template from stdin, replaces the {{KNOWLEDGE_SOURCES_SECTION}}
placeholder by scanning the files directory, and writes the result.
Usage:
printf '%s' "$TEMPLATE" | python3 generate_agents_md.py <output_path> <files_path>
"""
# Read template from environment variable
template = os.environ.get("AGENT_INSTRUCTIONS", "")
if len(sys.argv) != 3:
print(
f"Usage: {sys.argv[0]} <output_path> <files_path>",
file=sys.stderr,
)
sys.exit(1)
output_path = Path(sys.argv[1])
files_path = Path(sys.argv[2])
# Read template from stdin
template = sys.stdin.read()
if not template:
print("Warning: No AGENT_INSTRUCTIONS template provided", file=sys.stderr)
template = "# Agent Instructions\n\nNo instructions provided."
print("Error: No template content provided on stdin", file=sys.stderr)
sys.exit(1)
# Scan files directory - check /workspace/files first, then /workspace/demo_data
files_path = Path("/workspace/files")
demo_data_path = Path("/workspace/demo_data")
# Resolve symlinks (handles both direct symlinks and dirs containing symlinks)
resolved_files_path = files_path.resolve()
# Use demo_data if files doesn't exist or is empty
if not files_path.exists() or not any(files_path.iterdir()):
if demo_data_path.exists():
files_path = demo_data_path
knowledge_sources_section = build_knowledge_sources_section(resolved_files_path)
knowledge_sources_section = build_knowledge_sources_section(files_path)
# Replace placeholders
content = template
content = content.replace(
# Replace placeholder and write final file
content = template.replace(
"{{KNOWLEDGE_SOURCES_SECTION}}", knowledge_sources_section
)
# Write AGENTS.md
output_path = Path("/workspace/AGENTS.md")
output_path.write_text(content)
# Log result
source_count = 0
if files_path.exists():
source_count = len(
[
d
for d in files_path.iterdir()
if d.is_dir() and not d.name.startswith(".")
]
)
print(
f"Generated AGENTS.md with {source_count} knowledge sources from {files_path}"
)
print(f"Generated {output_path} (scanned {resolved_files_path})")
if __name__ == "__main__":

View File

@@ -1348,9 +1348,10 @@ if [ -d /workspace/skills ]; then
echo "Linked skills to /workspace/skills"
fi
# Write agent instructions
# Write agent instructions (scans files dir to populate knowledge sources)
echo "Writing AGENTS.md"
printf '%s' '{agent_instructions_escaped}' > {session_path}/AGENTS.md
printf '%s' '{agent_instructions_escaped}' \
| python3 /usr/local/bin/generate_agents_md.py {session_path}/AGENTS.md {session_path}/files
# Write opencode config
echo "Writing opencode.json"
@@ -1776,9 +1777,11 @@ set -e
echo "Creating files symlink to {symlink_target}"
ln -sf {symlink_target} {session_path}/files
# Write agent instructions
# Write agent instructions (scans files dir to populate knowledge sources)
echo "Writing AGENTS.md"
printf '%s' '{agent_instructions_escaped}' > {session_path}/AGENTS.md
printf '%s' '{agent_instructions_escaped}' \
| python3 /usr/local/bin/generate_agents_md.py \
{session_path}/AGENTS.md {session_path}/files
# Write opencode config
echo "Writing opencode.json"