Skip to content

実装: guard-shadow log writer の TOCTOU 耐性を canonical writer 化で強化する #1587

Closed as not planned

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "PR #1572 の post-merge cleanup で分離された、repo-root `.guard_shadow_log.jsonl` の全 producer を TOCTOU 耐性・bounded size・partial-write handling を備える canonical writer へ統合する"
change_kind: code
dedupe_key: "follow-up:squne121/loop-protocol:pr/1572:guard-shadow-log-canonical-writer-toctou"
origin_skill: post-merge-cleanup
source_pr: "#1572"

Parent Issue

なし(PR #1572 の post-merge cleanup から抽出した単独 follow-up)

Parent Goal Ref

Current Validated Scope

Remaining Parent Gaps

Outcome

.guard_shadow_log.jsonl の Python/Node producer が共通 canonical writer を通じて、trusted directory FD に束縛された regular file へ 0600・サイズ上限付き・完全 record write の JSONL append のみを行い、symlink/FIFO/directory/identity 不一致/size 超過/partial write は記録前に fail-close する実装と回帰テストが揃う。

In Scope

  • scripts/agent-guards/shadow_log_canonical_writer.py を追加し、repo root を trusted directory として FD で開き、.guard_shadow_log.jsonl を O_NOFOLLOW と regular-file fstat で検査してから append する共通 writer を実装する。
  • writer に共通 lock、0600 file mode、1つの明示的な size limit、partial-write loop、strict JSON object record serialization(allow_nan=False)を実装し、失敗時は既存 file を truncate/replace せず非0で終了する。
  • .claude/hooks/shadow_log.py を canonical writer の呼出し口へ更新し、既存2 shell hook を変更せず同じ経路に収束させる。
  • scripts/check-codex-agents.mjs の direct fs.appendFileSync() writer を廃止し、同じ canonical writer 経路へ更新する。
  • 新規 Python integration test で Python helper、両 shell hook 経路、Node caller の append、同時 producer、non-regular substitution、inode identity 異常、size limit、partial-write failure を検証する。
  • SSOT に writer の owner、許容する file kind/permission/size、失敗時の fail-close、及び rotation/retention が本 Issue の対象外であることを追記する。

Out of Scope

Acceptance Criteria

  • AC1: Python helper と Node caller が、同一 canonical writer を通じて .guard_shadow_log.jsonl へ strict JSON object の改行終端 record を append する。
  • AC2: canonical writer は trusted directory FD と O_NOFOLLOW を用い、final path が symlink/directory/FIFO/socket/device、または open 前後で identity 不一致のとき file を変更せず非0で fail-close する。
  • AC3: canonical writer は regular file を 0600 として扱い、明示された size limit 超過と partial-write failure で truncate/replace せず非0で fail-close する。
  • AC4: 複数 OS process を barrier で同時起動しても、Python helper・両 shell hook 経路・Node caller 由来の全 record が strict JSONL として欠落・部分混在なく保存される。
  • AC5: skill_runtime_exec.py の 実装: 契約実行ラッパーの unauthorized_write_path 誤検知を .guard_shadow_log.jsonl 専用の型付き exact-file ポリシーで解消する #1572 typed postcondition は変更せず、SSOT は writer 側 hardening と事後検査の責務境界を明記する。

Runtime Verification Applicability

decision: immediate
reason: "実 producer、並行 process、FD/path kind、partial write と size limit を実ファイルシステムで検証する必要がある"
applicable_acs: [AC1, AC2, AC3, AC4]
execution_environment: "Linux/WSL、uv-managed Python、Node.js、POSIX file descriptor と flock を利用可能な専用 worktree"
skip_conditions: "必要な Python/Node/POSIX 機能が欠ける場合だけ exit 77 で停止し、それ以外の writer/test failure は FAIL とする"
fallback_policy: "fallback 実装・direct append・mock のみの成功は PASS としない"
artifact_requirements: "artifacts/runtime-verification-AC<N>-<timestamp>.log に producer 別 record count、file mode、inode、size、executor exit code を保存する"

Verification Commands

# AC1
$ uv run --locked pytest scripts/agent-guards/tests/test_guard_shadow_log_canonical_writer.py::test_python_and_node_callers_share_canonical_writer -q

# AC2
$ uv run --locked pytest scripts/agent-guards/tests/test_guard_shadow_log_canonical_writer.py::test_nonregular_and_identity_change_fail_closed_without_mutation -q

# AC3
$ uv run --locked pytest scripts/agent-guards/tests/test_guard_shadow_log_canonical_writer.py::test_size_limit_and_partial_write_fail_closed_without_replacement -q

# AC4
$ uv run --locked pytest scripts/agent-guards/tests/test_guard_shadow_log_canonical_writer.py::test_barrier_synchronized_real_producers_preserve_all_records -q

# AC5
# baseline-expect: pass
$ git diff --exit-code origin/main...HEAD -- scripts/agent-guards/skill_runtime_exec.py

Allowed Paths

  • .claude/hooks/shadow_log.py
  • scripts/check-codex-agents.mjs
  • scripts/agent-guards/shadow_log_canonical_writer.py(新規)
  • scripts/agent-guards/tests/test_guard_shadow_log_canonical_writer.py(新規)
  • docs/dev/agent-skill-boundaries.md

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • openat2() 相当が必須で、O_NOFOLLOW と trusted directory FD による代替では受容できないと判明した場合
  • log rotation 又は record schema 再設計が size limit 導入に必須と判明した場合
  • 動作検証 AC の VC が exit 77 を返した場合

Required Skills

  • Python(POSIX file descriptor / pytest 統合テスト)
  • Node.js(child process integration)
  • POSIX ファイルシステムの race-condition 耐性設計

Required Design References

Scope Delta

Delivery Rule

  • 1 Issue = 1 PR とし、専用 worktree で実装して Draft PR を作成する。

Notes for Reviewer

Activity

  1. added
    phase/implementation
    agent/implementer
    enhancement
    triage-required
    on Jul 18, 2026
  2. squne121 commented on Aug 19, 2026

    @squne121
    OwnerAuthor

    Disposition: superseded / not_planned

    2026-08-20 OWNER decision により、.guard_shadow_log.jsonl は security authority / canonical audit ledger ではなく shadow rollout / false-positive 観測用の local telemetry であることが確認され、subsystem 自体(persistent shadow logging)を廃止する方針が確定した。

    削除する telemetry artifact の writer を TOCTOU 耐性・canonical writer 化で harden することは、この decision と直接矛盾する(廃止対象のセキュリティサーフェスをむしろ維持・拡大することになるため)。したがって本 Issue(canonical writer / common lock / 0600 / size limit / partial-write hardening の実装)は着手しない。

    本 Issue は close する。過去の contract 内容自体は歴史的事実として書き換えない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent/implementerenhancementphase/implementationtriage-required

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions