78 / 90

第 78 天:重試預算、逾時與誠實的執行邊界

以退避與抖動約束重試,強制有限逾時,並準確說明專案提供與不提供的隔離能力。

Agent 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)與正式環境抖動退避都約束了工作量,並指出各自防禦的故障(預算耗盡 vs 相關重試風暴)。

權威來源資源與權利證明

linking Amazon Web Services site content / linked reference noncommercial

Exponential Backoff And Jitter

作者 / 機構: Marc Brooker, AWS Architecture Blog | 原始出處連結 ↗

對應章節 / 時間戳: 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) | 原始出處連結 ↗

對應章節 / 時間戳: 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 分鐘

逾時、截止期限與實測終止邊界

獨立掌握檢查(無 AI 輔助)

前置要求

第 77 天的證據包;子行程基礎。

學習目標

對子行程強制有限逾時,然後實測終止邊界:subprocess 逾時會終結直接子行程,但無法承諾清理逃逸的後代行程。

操作指引

1. 本儲存庫的逾時控制:scripts/check_snippets.py 設定 RUN_TIMEOUT=15.0,stdin=DEVNULL,並在暫時工作目錄中執行;lab/run_all_tests.py 在 subprocess.run(timeout=...) 之前校驗逾時必須是有限正數(拒絕 bool、0、負數、NaN、inf)。
2. 截止期限 vs 逾時:逾時約束單次阻塞等待;截止期限約束端到端工作——Agent 迴圈用步數預算近似截止期限。
3. 終止邊界:subprocess.run 在 TimeoutExpired 時終結子行程;逃逸的後代(雙重 fork 或獨立工作階段)仍然存活——lab 執行器 README 明確寫出此缺口:它只終止直接子行程,後代樹清理仍未實作。
4. 行程群組是缺失的機制:setpgid/setsid 加 killpg 可讓執行器向整組發訊號;沒有它就沒有對逃逸後代的清理保證。

自主實作與記錄

執行一個會啟動獨立孫行程的子行程(先 subprocess.Popen(['sleep','60']) 再睡眠),設定 timeout=1。捕捉 TimeoutExpired 後執行 pgrep -f 'sleep 60' 展示孫行程存活。把存活的 PID 記入證據日誌。

獨立掌握標準

用即時的行程表展示逾時只終結一個行程而非行程樹;不宣稱未經驗證的後代清理。

權威來源資源與權利證明

linking PSF License Version 2

Python Standard Library: subprocess - Subprocess Management

作者 / 機構: Python Software Foundation | 原始出處連結 ↗

對應章節 / 時間戳: 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.) | 原始出處連結 ↗

對應章節 / 時間戳: 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 執行畫出五層階梯;每層標註一個它擋住的威脅和一個擋不住的威脅(例如 basename 檢查擋住 read_note 穿越;沒有機制阻止空轉的配接器——步數預算只是限制了浪費)。

獨立掌握標準

對專案中每項控制,說出一個它擋住的威脅和一個擋不住的威脅——包括明確承認缺少 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.) | 原始出處連結 ↗

對應章節 / 時間戳: 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 | 原始出處連結 ↗

對應章節 / 時間戳: 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 工作區;僅動作/觀測軌跡;subprocess 逾時(check_snippets.py、lab 執行器);行程群組樹終止(缺失);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 | 原始出處連結 ↗

對應章節 / 時間戳: 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 | 原始出處連結 ↗

對應章節 / 時間戳: 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.