78 / 90

第78日: リトライ予算、タイムアウトと正直な実行限界

バックオフとジッターでリトライを制限し、有限タイムアウトを強制し、プロジェクトが提供する分離と提供しない分離を正確に述べる。

エージェント 480 アクティブ分(8 時間)
01 120 分

リトライ予算、指数バックオフとジッター

Agent支援エンジニアリングチェック

前提条件

第76〜77日のリトライカウンタ機構。基礎的な確率。

学習目標

厳格な試行予算・指数バックオフ・フルジッターを備えたリトライポリシーを設計し、無制限で同期したリトライが部分障害を連鎖障害へ増幅する仕組みを説明する。

手順

1. プロジェクトのポリシーは意図的に最小限です: ok=false の観測ごとに retries が増加し、retries > max_retries=2 で実行が失敗します。遅延なしの即時再発行です。
2. 本番ポリシー(AWS Builder's Library): delay = random_between(0, min(cap, base * 2**attempt))。フルジッターはクライアント同士の相関を断ち、競合時の総仕事量を最小化します。
3. 連鎖障害モデル(Google SRE 第22章): リトライは依存先が劣化している時こそ負荷を倍加させます。リトライ予算+ジッター付きバックオフがその増幅に上限を与えます。
4. リトライするのは一過性障害のみ。永続障害(invalid_arguments, not_found)は訂正が必要です。アダプタが引数を直して再発行したからこそ、そのリトライは有効でした。

演習と予測記録

retry_with_backoff(fn, attempts=3, base=0.05, cap=0.5) を実装し、各試行間にフルジッター間隔でスリープし、枯渇後は再送出してください。2回失敗後に成功する不安定スタブと、予算を全消費する永続失敗の両方で駆動します。

独立習得基準

ジッターの数式を使って、プロジェクトのリトライ予算(2)と本番のジッター付きバックオフがともに仕事量を制限する理由と、それぞれが防ぐ障害(予算枯渇 vs 相関リトライストーム)を挙げられる。

公式ソースリソースと権利証跡

linking Amazon Web Services site content / linked reference noncommercial

Exponential Backoff And Jitter

著者・提供機関: Marc Brooker, AWS Architecture Blog | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Adding Backoff; Adding Jitter (Full Jitter, Equal Jitter, Decorrelated Jitter)

検証バージョンと日付: AWS Architecture Blog 2015 (Verified: 2026-10-30)

選定理由:

Detailed practitioner reference proving that full jitter (sleep = random_between(0, min(cap, base*2**attempt))) prevents synchronized retry storms and reduces total work during contention.

ソース前提知識:

Retries, timeouts, and basic probability of correlated load.

安全フォールバック(オフライン回復性):

The backoff exercise is implemented in pure Python with a local flaky stub; the derivation of jitter benefits is fully reproduced in lesson instructions.

比較対象候補リソースと選定評価 (2)
Google Cloud retry strategy documentation ↗ compared_alternative [CC BY 4.0 / documentation terms]

Product-scoped truncated exponential backoff recipe; Brooker's analysis explains why jitter matters, not just what to configure.

Polly resilience library retry policies (.NET) ↗ compared_alternative [BSD-3-Clause]

Library implementation reference; the AWS article supplies the comparative evidence (with/without jitter) that anchors the lesson.

linking CC BY-NC-ND 4.0 noncommercial no-derivatives

Site Reliability Engineering, Chapter 22: Addressing Cascading Failures

著者・提供機関: Mike Ulrich, in Site Reliability Engineering (eds. Betsy Beyer, Chris Jones, Jennifer Petoff, Niall Richard Murphy; Google) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Preventing Server Overload: Retries

検証バージョンと日付: O'Reilly 2016 free online edition (Verified: 2026-10-30)

選定理由:

Primary analysis of how unbounded retries amplify partial outages into cascading failures, motivating hard retry budgets and deadlines in agent runtimes.

ソース前提知識:

Retries, queueing, and load-shedding basics.

安全フォールバック(オフライン回復性):

The retry-amplification scenario and budget arithmetic are fully reproduced in the lesson's worked example.

比較対象候補リソースと選定評価 (2)
Release It! (Michael Nygard) - Stability patterns ↗ compared_alternative [Commercial book]

Broader stability-pattern catalog (circuit breakers, bulkheads); the SRE chapter is free and directly quantifies retry amplification.

Strong companion article; the SRE chapter frames the failure mode (cascading overload) that the retry budget defends against.

02 120 分

タイムアウト、デッドラインと実測されるkill境界

独立習得チェック(AI 支援なし)

前提条件

第77日の証拠パック。サブプロセス基礎。

学習目標

子プロセスに有限タイムアウトを強制し、kill境界を実測する: サブプロセスのタイムアウトは直接の子を終了させるが、脱出した子孫の後始末は保証できない。

手順

1. 本リポジトリのタイムアウト制御: scripts/check_snippets.py は RUN_TIMEOUT=15.0、stdin=DEVNULL、一時作業ディレクトリで実行。lab/run_all_tests.py は subprocess.run(timeout=...) の前にタイムアウトが有限の正数か検証します(bool, 0, 負数, NaN, inf を拒否)。
2. デッドラインとタイムアウト: タイムアウトは1つのブロッキング待機を制限し、デッドラインはエンドツーエンドの作業を制限します。エージェントループはステップ予算でデッドラインを近似します。
3. kill境界: subprocess.run は TimeoutExpired で子プロセスをkillしますが、脱出した子孫(ダブルフォークや独自セッション)は生存します。labランナーのREADMEがこのギャップを明示しています: 直接の子のみを終了させ、子孫ツリーの後始末は未実装です。
4. プロセスグループが欠落した機構です: setpgid/setsid と killpg はグループ全体へシグナルを送れます。これが無ければ脱出子孫の後始末保証はありません。

演習と予測記録

独立した孫プロセス(subprocess.Popen(['sleep','60']) 後に sleep)を起動する子を timeout=1 で実行してください。TimeoutExpired を捕捉後、pgrep -f 'sleep 60' で孫が生存していることを示します。生存 PID を証拠ログに記録します。

独立習得基準

稼働中のプロセス一覧で、タイムアウトがプロセスツリーではなく1プロセスのみをkillすることを実演できる。未検証の子孫後始末を主張しない。

公式ソースリソースと権利証跡

linking PSF License Version 2

Python Standard Library: subprocess - Subprocess Management

著者・提供機関: Python Software Foundation | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Using the subprocess Module (subprocess.run timeout) and Notes: Timeout Behavior

検証バージョンと日付: Python 3.13 documentation (Verified: 2026-10-30)

選定理由:

Normative reference for subprocess.run(timeout=...), TimeoutExpired, DEVNULL stdin, and the documented kill scope: timeout terminates the child process; it does not define process-tree cleanup semantics.

ソース前提知識:

Processes, arguments, exit codes, and context managers.

安全フォールバック(オフライン回復性):

The kill-boundary experiment runs entirely in the standard library, so the documented semantics are exercised offline.

比較対象候補リソースと選定評価 (2)
POSIX standard: process creation and termination ↗ compared_alternative [IEEE/Open Group standard]

Formal POSIX semantics; the Python docs map directly onto the commands learners type.

psutil process-tree utilities ↗ compared_alternative [BSD-3-Clause]

Third-party tree enumeration; the exercise deliberately demonstrates the stdlib boundary before reaching for helpers.

linking Linux man-pages license (freely distributable)

Linux man-pages: credentials(7) and setpgid(2) - Process Groups and Sessions

著者・提供機関: Linux man-pages project (Michael Kerrisk et al.) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: credentials(7) DESCRIPTION: Process group and session

検証バージョンと日付: man-pages 6.x online edition (Verified: 2026-10-30)

選定理由:

Normative reference for process-group semantics (PGID, sessions, killpg): the mechanism a runner must use to signal a whole descendant tree, which the lab runner and subprocess timeout path do not employ.

ソース前提知識:

POSIX processes, signals, and fork/exec model.

安全フォールバック(オフライン回復性):

Process-group behavior is demonstrated live with os.setsid and process inspection; local man pages cover the same text offline.

比較対象候補リソースと選定評価 (2)
POSIX.1-2017 specification: setpgid ↗ compared_alternative [IEEE/Open Group standard]

Formal standard text; the man page presents the same contract with implementation notes learners can test directly.

Detailed textbook treatment of sessions and job control; the man page is the free, always-current operational reference.

03 120 分

分離レイヤー: 検証ルール、リソース制限、OSサンドボックス

Agent支援エンジニアリングチェック

前提条件

第78日アクティビティ1〜2。プロジェクトの全制御目録。

学習目標

プロジェクトの各制御を正しい防御層(ASTゲート、引数検証、ペイロード/ステップ/リトライ制限、使い捨てワークスペース、名前空間/cgroup、microVM分離)に配置し、各層が防げない脅威を述べる。

手順

1. 層1 - 静的ASTゲート: scripts/check_snippets.py は全スニペットに ast.parse を実行(構文エラーを拒否)し、classify() が torch インポート・対話入力・訓練コードをスキップします。ASTルールは実行時挙動ではなくソース構造を検査します。
2. 層2 - 引数検証: _tool_call は未知ツール、非マッピング引数、Path(filename).name != filename を満たすファイル名を拒否します('../outside.txt' トラバーサルを阻止)。
3. 層3 - リソース制限: tool_payload_bytes=4096(size_limit)、max_steps=6、max_retries=2。消費を制限しますが分離はしません。
4. 層4 - 使い捨てワークスペース: 実行ごとの TemporaryDirectory は衛生上の利便であり、セキュリティ境界ではありません。絶対パスは依然として書き込み可能です。
5. プロジェクトが到達しない層: カーネル分離(名前空間、cgroup、seccomp)とハードウェア仮想化境界(Firecracker microVM)。信頼できないコードを実行するマルチテナントランタイムが要求する水準です。

演習と予測記録

summarize-notes 実行の5層ラダーを描き、各層に「防ぐ脅威1つ」と「防げない脅威1つ」を注記してください(例: basename チェックは read_note トラバーサルを阻止。ループするアダプタは止まらず、ステップ予算は浪費を制限するだけ)。

独立習得基準

プロジェクトの全制御について「防ぐ脅威1つ」と「防げない脅威1つ」を述べられる。OSレベル封じ込めが明示的に存在しない点を含む。

公式ソースリソースと権利証跡

linking Linux man-pages license (freely distributable)

Linux man-pages: namespaces(7) - Overview of Linux Namespaces

著者・提供機関: Linux man-pages project (Michael Kerrisk et al.) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: namespaces(7) DESCRIPTION: Namespace types

検証バージョンと日付: man-pages 6.x online edition (Verified: 2026-10-30)

選定理由:

Normative reference enumerating the eight Linux namespace types; establishes what genuine OS-level isolation requires (mount, PID, network, user namespaces plus seccomp and cgroup limits), all of which the learning project deliberately does not provide.

ソース前提知識:

Processes, mounts, network interfaces, and cgroups basics.

安全フォールバック(オフライン回復性):

Namespace concepts are analyzed against project code, not exercised; unshare(1) examples are optional enrichment.

比較対象候補リソースと選定評価 (2)
Docker Engine security documentation ↗ compared_alternative [Apache-2.0 / documentation terms]

Product view assembling namespaces and cgroups into containers; namespaces(7) supplies the primitive inventory the lesson enumerates.

LWN.net series: Namespaces in operation ↗ compared_alternative [LWN site content]

Strong narrative walkthrough; the man page is the maintained reference surface.

linking USENIX open access

Firecracker: Lightweight Virtualization for Serverless Applications

著者・提供機関: Alexandru Agache, Marc Brooker, Alexandra Iordache, Anthony Liguori, Rolf Neugebauer, Phil Piwonka, Diana-Maria Popa | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3.4 Security and Section 3.4.1 Jailer (nsdi20-paper-agache.pdf)

検証バージョンと日付: NSDI 2020 / USENIX open access (Verified: 2026-10-30)

選定理由:

Primary literature on production multi-tenant code execution: Firecracker wraps workloads in hardware-virtualized microVMs because containers alone are not a sufficient security boundary, the reference standard for what genuine isolation means.

ソース前提知識:

Virtualization basics, KVM, process isolation, and threat models.

安全フォールバック(オフライン回復性):

The isolation-layer analysis compares documented properties only; no virtualization is launched in the lesson.

比較対象候補リソースと選定評価 (2)
gVisor architecture documentation ↗ compared_alternative [Apache-2.0]

User-space kernel interception alternative; Firecracker's hardware boundary provides the stronger reference point for the guarantee matrix.

Kata Containers architecture ↗ compared_alternative [Apache-2.0]

VM-backed container runtime; Firecracker's NSDI paper supplies the peer-reviewed threat-model justification.

04 120 分

評価: 実行制限の保証マトリクス監査

独立習得チェック(AI 支援なし)

前提条件

第78日アクティビティ1〜3。

学習目標

文書化された全制御を「主張する防御」「主張しない防御」で監査する保証マトリクスを作成し、各セルを稼働証拠か明示的文書記述で検証する。

手順

1. 列: control | 機構と場所 | 防ぐ | 防がない。
2. 行: ツール許可リスト(ALLOWED_TOOLS frozenset)。basename チェック(_tool_call)。4096バイトペイロード上限。max_steps=6。max_retries=2。TemporaryDirectory ワークスペース。アクション/観測のみのトレース。サブプロセスタイムアウト(check_snippets.py、labランナー)。プロセスグループツリーkill(欠如)。OSサンドボックス/VM境界(欠如)。
3. 証拠規則: 「防ぐ」の主張は稼働デモかユニットテストが必要。「防がない」の主張は稼働反例(生存する sleep PID)か文書記述(README: リソース制限は制御でありOSサンドボックスではない。project.json の evidence.resource_limit_note)が必要です。
4. 証拠に結び付かない主張の行は却下します。正直なマトリクスは未検証の約束をゼロで出荷します。

演習と予測記録

guarantee_matrix.md の10行マトリクスを記入してください。行レベルの機構(例: agent.py の 'Path(filename).name != filename')とアクティビティ2の脱出子孫 PID を引用します。

独立習得基準

全ての否定的主張が証拠か文書で裏付けられた保証マトリクスを提出できる。未検証の約束ゼロ。

公式ソースリソースと権利証跡

linking PSF License Version 2

Python Standard Library: ast - Abstract Syntax Trees

著者・提供機関: Python Software Foundation | オリジナル出所URL ↗

該当セクション・タイムスタンプ: ast helpers (ast.parse, ast.walk)

検証バージョンと日付: Python 3.13 documentation (Verified: 2026-10-30)

選定理由:

Normative reference for ast.parse used by scripts/check_snippets.py as the static admission gate: AST rules inspect source structure before any execution and cannot evaluate runtime behavior.

ソース前提知識:

Python syntax, parse trees, and compile pipeline basics.

安全フォールバック(オフライン回復性):

Every AST check in the lesson runs in the standard library; no external validator is fetched.

比較対象候補リソースと選定評価 (2)

Production AST linter; the stdlib ast module is the minimal sufficient tool for the admission-gate exercise.

Bandit security linter ↗ compared_alternative [Apache-2.0]

Security-oriented AST rules; deliberately cited as a comparison to show AST gating is necessary but never sufficient for safety.

linking CC BY 4.0

Kubernetes Documentation: Resource Management for Pods and Containers

著者・提供機関: Kubernetes Documentation Contributors | オリジナル出所URL ↗

該当セクション・タイムスタンプ: How Kubernetes applies resource requests and limits

検証バージョンと日付: Kubernetes documentation (Verified: 2026-10-30)

選定理由:

Primary reference for cgroup-enforced resource limits (CPU throttling, memory OOM-kill): demonstrates that production limits are kernel-enforced accounting, contrasted with the project's application-level counters.

ソース前提知識:

Containers, CPU/memory accounting, and QoS classes.

安全フォールバック(オフライン回復性):

The lesson contrasts documented cgroup semantics with the project's counters; no cluster is required.

比較対象候補リソースと選定評価 (2)
Linux man-pages: cgroups(7) ↗ compared_alternative [Linux man-pages license]

Kernel-level primitive reference; Kubernetes docs present the enforcement outcome (throttle vs OOM-kill) learners recognize from operations.

Docker resource constraints documentation ↗ compared_alternative [Apache-2.0 / documentation terms]

Single-host flag surface; Kubernetes documentation covers limit semantics plus QoS classification.