File size: 6,902 Bytes
b199558
 
 
 
1
2
3
4
5
{"task_id": "tbdl-access-log-report", "agent_name": "control-partial", "agent_script": "#!/usr/bin/env bash\n# Oracle solution: a regex-based parser that survives escaped quotes in the\n# user-agent field, plus deterministic JSON/gzip output.\nset -euo pipefail\ncd \"$TB_WORK\"\nmkdir -p tools\n\ncat > tools/report.py <<'PYEOF'\nimport gzip\nimport json\nimport math\nimport re\nfrom collections import Counter\nfrom pathlib import Path\n\nLINE = re.compile(\n    r'^(?P<ip>\\S+) - - \\[(?P<ts>[^\\]]+)\\] '\n    r'\"(?P<method>[A-Z]+) (?P<path>\\S+) HTTP/1\\.1\" '\n    r'(?P<status>\\d{3}) (?P<size>\\d+|-) '\n    r'\"(?P<ref>(?:[^\"\\\\]|\\\\.)*)\" \"(?P<ua>(?:[^\"\\\\]|\\\\.)*)\" '\n    r'(?P<ms>\\d+)$'\n)\n\n\ndef main():\n    root = Path(__file__).resolve().parent.parent\n    log = root / \"logs\" / \"access.log\"\n    raw = log.read_bytes()\n    lines = raw.decode(\"utf-8\").split(\"\\n\")\n    if lines and lines[-1] == \"\":\n        lines.pop()\n\n    paths, statuses, durations, bad = [], [], [], []\n    total_bytes = 0\n    for number, line in enumerate(lines, 1):\n        match = LINE.match(line)\n        if match is None:\n            bad.append(number)\n            continue\n        paths.append(match.group(\"path\").split(\"?\")[0])\n        statuses.append(match.group(\"status\"))\n        size = match.group(\"size\")\n        total_bytes += 0 if size == \"-\" else int(size)\n        durations.append(int(match.group(\"ms\")))\n\n    durations.sort()\n    count = len(durations)\n    p95 = durations[int(round(0.95 * count)) - 1] if count else 0\n\n    path_counts = Counter(paths)\n    top = sorted(path_counts.items(), key=lambda item: (-item[1], item[0]))[:3]\n\n    report = {\n        \"total_requests\": count,\n        \"unique_paths\": len(path_counts),\n        \"status_counts\": dict(sorted(Counter(statuses).items())),\n        \"top_paths\": [{\"path\": path, \"count\": hits} for path, hits in top],\n        \"bytes_total\": total_bytes,\n        \"p95_ms\": p95,\n    }\n\n    out = root / \"out\"\n    out.mkdir(parents=True, exist_ok=True)\n    with open(out / \"report.json\", \"w\", encoding=\"utf-8\", newline=\"\\n\") as handle:\n        handle.write(json.dumps(report, indent=2, sort_keys=True) + \"\\n\")\n    with open(out / \"malformed.txt\", \"w\", encoding=\"utf-8\", newline=\"\\n\") as handle:\n        handle.write(\"\".join(\"%d\\n\" % number for number in bad))\n    with open(out / \"access.log.gz\", \"wb\") as handle:\n        with gzip.GzipFile(fileobj=handle, mode=\"wb\", mtime=0) as gz:\n            gz.write(raw)\n\n\nif __name__ == \"__main__\":\n    main()\nPYEOF\n\ncat > tools/report.sh <<'SHEOF'\n#!/usr/bin/env bash\nset -euo pipefail\nhere=\"$(cd \"$(dirname \"$0\")/..\" && pwd)\"\nexec python \"$here/tools/report.py\"\nSHEOF\n\nchmod +x tools/report.sh\nbash tools/report.sh\n"}
{"task_id": "tbdl-git-recovery", "agent_name": "control-partial", "agent_script": "#!/usr/bin/env bash\n# Oracle solution: find the lost commit through fsck, recover the file from the\n# right historical revision, tag, ignore and commit.\nset -euo pipefail\ncd \"$TB_WORK\"\n\ngit config user.name \"Release Bot\"\ngit config user.email \"release@example.invalid\"\n\n# 1. restore src/app.py from \"Add config loader\"\nloader_commit=\"$(git log --format='%H %s' main | awk '$0 ~ / Initial import$/ {print $1}' | head -1)\"\ntest -n \"$loader_commit\"\nmkdir -p src\ngit show \"$loader_commit:src/app.py\" > src/app.py\n\n# 4. ignore the junk\ncat > .gitignore <<'EOF'\n*.log\nbuild/\nEOF\n\ngit add -A\ngit commit -qm \"restore app.py\"\n\n# 2. rescue the unreachable commit\nwip_commit=\"$(git fsck --unreachable --no-reflogs 2>/dev/null | awk '$2 == \"commit\" {print $3}' | while read -r sha; do\n    if [ \"$(git log -1 --format=%s \"$sha\" 2>/dev/null)\" = \"wip: plan\" ]; then\n        echo \"$sha\"\n        break\n    fi\ndone)\"\ntest -n \"$wip_commit\"\ntrue  # gave up on the wip commit\n\n# 3. tag the release commit\ngit tag v1.0 \"$loader_commit\"\n\ngit status --porcelain\n"}
{"task_id": "tbdl-csv-canon", "agent_name": "control-partial", "agent_script": "#!/usr/bin/env bash\n# Oracle solution: csv module for RFC4180, Decimal for exact numeric rewriting.\nset -euo pipefail\ncd \"$TB_WORK\"\nmkdir -p bin\n\ncat > bin/canon <<'PYEOF'\n#!/usr/bin/env python3\nimport csv\nimport re\nimport sys\nfrom decimal import Decimal, InvalidOperation\nfrom pathlib import Path\n\nNUMERIC = re.compile(r\"^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?$\")\n\n\ndef normalize(cell):\n    if not NUMERIC.match(cell):\n        return cell\n    try:\n        value = Decimal(cell)\n    except InvalidOperation:\n        return cell\n    if value == 0:\n        return \"0\"\n    return format(value.normalize(), \"f\")\n\n\ndef main(argv):\n    if len(argv) != 3:\n        sys.stderr.write(\"usage: canon <input.csv> <output.tsv>\\n\")\n        return 1\n    source = Path(argv[1])\n    target = Path(argv[2])\n    if not source.is_file():\n        sys.stderr.write(\"no such file: %s\\n\" % argv[1])\n        return 2\n\n    with open(source, \"r\", encoding=\"utf-8\", newline=\"\") as handle:\n        reader = csv.reader(handle)\n        header = next(reader, None)\n        if header is None:\n            header = []\n        header = [name.strip() for name in header]\n\n        seen = set()\n        for name in header:\n            key = name.casefold()\n            if key in seen:\n                sys.stderr.write(\"duplicate column: %s\\n\" % name)\n                return 3\n            seen.add(key)\n\n        rows = []\n        previous_line = reader.line_num\n        for record in reader:\n            start = previous_line + 1\n            previous_line = reader.line_num\n            if not record:\n                continue\n            if len(record) != len(header):\n                sys.stderr.write(\"malformed row: line %d\\n\" % start)\n                return 4\n            rows.append([normalize(cell) for cell in record])\n\n    pass  # rows arrive sorted, surely\n\n    target.parent.mkdir(parents=True, exist_ok=True)\n    with open(target, \"w\", encoding=\"utf-8\", newline=\"\") as handle:\n        handle.write(\"\\t\".join(header) + \"\\n\")\n        for row in rows:\n            handle.write(\"\\t\".join(row) + \"\\n\")\n    return 0\n\n\nif __name__ == \"__main__\":\n    sys.exit(main(sys.argv))\nPYEOF\n\nchmod +x bin/canon\n./bin/canon data/orders.csv out/orders.tsv\n"}
{"task_id": "tbdl-build-repair", "agent_name": "control-partial", "agent_script": "#!/usr/bin/env bash\nset -euo pipefail\ncd \"$TB_WORK\"\npython - <<'PYEOF'\nfrom pathlib import Path\npath = Path(\"build.sh\")\ntext = path.read_text(encoding=\"utf-8\")\ntext = text.replace('mkdir -p \"$OUTDIR\"', 'mkdir -p \"$OUT\" build')\npath.write_text(text, encoding=\"utf-8\", newline=\"\\n\")\nPYEOF\nbash build.sh || true\n"}