03 / 90

第 03: 恢復演練:診斷失敗的環境

恢復演練:診斷失敗的環境。各活動都保留預測、觀察、複盤與恢復證據。

自學方法與 Python 工程基礎 480 分鐘(8 小時專注學習)
01 120 分鐘

恢復演練:診斷失敗的環境

獨立掌握檢查 — 評量作業期間不得使用 AI 輔助。若接受任何輔助必須如實申報,但接受輔助的作業只計為補救練習:在聲稱掌握之前,必須完成一次新的無輔助檢查。

前置要求

完成第 02 學習日各活動。評量路徑需要 Bash;已驗證的執行範圍是 Linux Bash。需要可用的 `python3`;不安裝套件、不做機器級更改。

學習目標

用預測、最小重現和恢復筆記,診斷一個被故意破壞的虛擬環境或路徑假設。

操作指引

本演練在一次性 env-drill 目錄內提供一個故意破壞但無害的裝置。執行前預測症狀,重現它,收集診斷證據,應用可論證的最小修正,然後複查。全程留在裝置目錄內;不做任何機器級更改(不安裝、不編輯 env-drill 以外的內容、不做持久環境改動——下面的修復只作用於單次命令呼叫)。 各程式碼區塊在失敗即停的子 shell 中執行:設定階段先驗證新建的 mktemp 私有根目錄,失敗會在寫入前中止;還會透過一個不執行模組程式碼的 importlib.util.find_spec 檢查來確認環境 Python 設定是否已能解析 helper,若能解析也會中止(預期失敗需要這一前提;該探針不匯入也不執行模組)。最後的程式碼區塊會印出一條字面 rm -r -- <private-root> 清理行,只指向所建立的根目錄。後續程式碼區塊之前先貼上印出的 export SBX_ROOT= 行;並記錄shell 原本 PYTHONPATH 的設定與否及其取值——未設定、空、非空都是有效證據。 請在 Bash 中執行這些程式碼區塊——印出的 export/rm -r -- 行使用 Bash 的 %q 引用,實際執行範圍為 Linux Bash;zsh/dash/Windows 未執行。

自主實作與記錄

建構損壞的裝置並重現失敗(整塊貼上;若設定失敗,或不執行模組的檢查發現 helper 已可解析,會在寫入前中止):

(
set -e
SBX_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/d03env.XXXXXX")
[ -n "$SBX_ROOT" ]
[ -d "$SBX_ROOT" ]
[ -w "$SBX_ROOT" ]
[ -x "$SBX_ROOT" ]
probe_rc=0
python3 -c "import sys, importlib.util
try:
    spec = importlib.util.find_spec('helper')
except Exception:
    sys.exit(4)
sys.exit(3 if spec is not None else 0)" || probe_rc=$?
if [ "$probe_rc" -ne 0 ]; then
  if [ "$probe_rc" -eq 3 ]; then
    echo "abort: ambient Python config already resolves 'helper' - the intended failure cannot be demonstrated"
  else
    echo "abort: helper-resolvability probe failed (rc=$probe_rc) - cannot verify the precondition safely"
  fi
  exit 1
fi
mkdir "$SBX_ROOT/env-drill"
cd "$SBX_ROOT/env-drill"
mkdir tools
printf 'def greet():\n    return "ok"\n' > tools/helper.py
printf 'from helper import greet\nprint(greet())\n' > app.py
rc=0
python3 app.py 2>err.txt || rc=$?
cat err.txt
grep -q "ModuleNotFoundError: No module named 'helper'" err.txt
echo "expected failure observed (rc=$rc)"
printf 'export SBX_ROOT=%q\n' "$SBX_ROOT"
)

執行前預測錯誤。貼上印出的 export SBX_ROOT= 行,然後收集證據:

(
set -e
: "${SBX_ROOT:?run the setup block first, then paste its printed export line}"
[ -d "$SBX_ROOT/env-drill" ]
cd "$SBX_ROOT/env-drill"
python3 -c "import sys, os; print([os.path.abspath(p or '.') for p in sys.path])"
ls tools
)

套用僅限本次呼叫的可論證最小修正並複查,將 PYTHONPATH 與記錄的原始值比較——程式碼區塊會印出明確的 set/unset 標記和取值,因此未設定、空、非空的原始狀態都可區別地證明未改變:

(
set -e
: "${SBX_ROOT:?run the setup block first, then paste its printed export line}"
[ -d "$SBX_ROOT/env-drill" ]
cd "$SBX_ROOT/env-drill"
pp_state() { if [ "${PYTHONPATH+x}" = x ]; then printf 'PYTHONPATH %s: set value=<%s>\n' "$1" "$PYTHONPATH"; else printf 'PYTHONPATH %s: unset\n' "$1"; fi }
pp_state before
PYTHONPATH=tools python3 app.py
pp_state after
printf 'rm -r -- %q\n' "$SBX_ROOT"
)

記錄症狀、證據、修復、為何最小的理由,以及複查結果。

獨立掌握標準

在無輔助條件下滿足以下全部即通過:(a) 執行前預測了 `ModuleNotFoundError` 症狀;(b) 記錄了診斷證據(缺少 `tools/` 的解析後 `sys.path` 列表,以及顯示檔案存在的 `ls tools` 結果);(c) 所應用的修復限定在單次呼叫內,並書面說明了為何最小;(d) 記錄了成功的複查和無持久化檢查——限定修復後的 `PYTHONPATH` 與記錄的原始值(未設定、空、或不變的非空值)一致。輔助只計為補救練習,在聲稱掌握之前必須完成一次新的無輔助檢查。

權威來源資源與權利證明

linking PSF License Version 2

The Python Tutorial: Virtual Environments and Packages

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

對應章節 / 時間戳: Section 12 (creating venvs, pip installs, requirements files)

驗證版本與日期: Python 3.12 (Verified: 2026-10-30)

入選理由:

Primary reference for python -m venv, interpreter isolation, and requirements files used by the wrong-environment drills.

來源前置條件:

Basic Python syntax from Study Days 01-09

安全回退方案(離線彈性):

The drill needs no network: venv creation, sys.executable inspection, and ModuleNotFoundError reproduction all run offline.

對比候選資源與選型分析 (2)
pip documentation ↗ compared_alternative [MIT]

Primary reference for pip commands, but the tutorial presents the create-activate-install loop as one beginner-friendly narrative.

virtualenv documentation ↗ compared_alternative [MIT License]

Mature third-party environment tool with overlapping behavior; the stdlib venv tutorial was selected to avoid an extra dependency for first-time setup.

linking PSF License Version 2

The Python Tutorial: Errors and Exceptions

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

對應章節 / 時間戳: Sections 8.3-8.6 (handling exceptions, else, finally, cleanup actions)

驗證版本與日期: Python 3.12 (Verified: 2026-10-30)

入選理由:

Primary reference for try/except/else/finally semantics and the with statement as predefined cleanup for Day 11.

來源前置條件:

Basic Python syntax from Study Days 01-09

安全回退方案(離線彈性):

Every clause behavior in the lesson is demonstrated by runnable local snippets.

對比候選資源與選型分析 (2)
MIT OCW 6.0001 Lecture on Exceptions ↗ compared_alternative [CC BY-NC-SA 4.0]

Solid lecture treatment but paced for a semester course and requires video viewing; the tutorial section matches the exercise scope directly.

Python Standard Library: Built-in Exceptions ↗ compared_alternative [PSF License Version 2]

Reference-style exception hierarchy listing rather than the tutorial narrative; retained as lookup material for the recovery drills.

02 120 分鐘

Agent 輔助工程審查與修正

Agent 輔助工程檢查 — 須宣告所選軌道、權限和審查邊界;Agent 的產出不構成獨立證據。若無已授權的工具可用,須將實作部分標記為「受阻 — 未執行」:下面預先編寫的壞補丁是離線範例,不是從真實服務商觀察到的輸出。

前置要求

完成 day-03-act-01,以及已選定的 Agent 工具軌道。

學習目標

審查 Agent 產出的改動,執行檢查,發現失敗,並要求修正而不是盲目接受輸出。

操作指引

在新建的 agent-review-sandbox 內,建立包含 def add(a, b): return a + b 的 calc.py,以及下面的獨立檢查 check_clamp.py——這是你自己的檢查,獨立於 Agent 的任何聲稱。然後請選定的 Agent 精確執行此改動:「在 agent-review-sandbox/calc.py 中新增函式 clamp(x, lo, hi):回傳被限制在 [lo, hi] 區間內的 x,當 lo > hi 時拋出 ValueError。不要修改 add 或任何其他檔案。」寫入前必須批准;沙箱之外禁止一切操作。 宣告的權限是約定,不是作業系統沙箱——範圍必須由你自行驗證。 預算:Agent 回合或重試最多 3 次;停止條件:檢查通過、預算耗盡、或發生任何越界操作。若首次執行即通過,這本身就是有效證據——不要編造缺陷;記錄通過輸出並完成實質性評審。帶標籤的操練補丁可另行用於練習修正流程(記為操練證據)。對產出的程式碼執行你的檢查。若沒有已授權工具,用參考解答中帶標籤的預編寫補丁練習該流程——它是範例,不是實作證據。

自主實作與記錄

獨立行為檢查(自己編寫,儲存為 check_clamp.py):

from calc import clamp
assert clamp(5, 0, 10) == 5
assert clamp(-3, 0, 10) == 0
assert clamp(99, 0, 10) == 10
try:
    clamp(2, 10, 0)
except ValueError:
    pass
else:
    raise AssertionError('clamp must reject lo > hi')
print('clamp checks passed')

對產出的程式碼執行 python3 check_clamp.py。若失敗,寫出精確回饋(哪個用例、預期值與實際值、疑似原因),要求修正,捕獲差異(git diff 或對所存副本 diff -u),並重跑檢查。需記錄:失敗輸出、你的回饋、差異、通過的複查。若首次即通過,記錄通過輸出和實質性證據評審(逐條追蹤各 assert、驗證 lo > hi 的 ValueError 路徑、確認 add 及其他檔案未改動);仍可用帶標籤操練補丁練習回饋流程,但記為操練證據,絕不得作為Agent 輸出。

獨立掌握標準

滿足以下全部即通過:(a) 學習者自寫的 `check_clamp.py` 存在並已對產出程式碼執行;(b) 記錄了真實失敗路徑(失敗輸出、點名失敗用例及預期/實際值與疑似原因的回饋、修正差異、通過複查),或記錄了首次通過(通過輸出加逐條追蹤各檢查、`lo > hi` 路徑、範圍合規的實質性證據評審)。不得改動有效結果來編造 Agent 缺陷;帶標籤操練補丁僅作為註明身份的 optional 練習證據。若無已授權工具,實作部分標記為受阻/未執行,流程在帶標籤範例上演示——絕不得作為真實 Agent 輸出出示。

權威來源資源與權利證明

linking Open Access / CC BY 4.0

SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineering

作者 / 機構: John Yang, Carlos E. Jimenez, Alexander Wettig, Kilian Lieret, Shunyu Yao, Karthik Narasimhan, Ofir Press | 原始出處連結 ↗

對應章節 / 時間戳: Section 2 The Agent-Computer Interface and Section 3 SWE-agent: Designing an ACI for Software Engineering

驗證版本與日期: NeurIPS 2024 / arXiv:2405.15793v2 (Verified: 2026-10-30)

入選理由:

Primary literature on agent-computer interfaces showing that bounded tool commands, trajectory inspection, and environment constraints determine whether an agent can carry an engineering task to verifiable completion.

來源前置條件:

Agent loops, tool protocols, and basic software engineering workflow vocabulary.

安全回退方案(離線彈性):

The permission/budget control profile, tool contract schema, and trace audit procedure are fully implemented in the local bounded agent fixture (projects/agent_learning).

對比候選資源與選型分析 (2)

Broader generalist agent platform; SWE-agent isolates the agent-computer interface design principles most relevant to bounded task delivery.

AutoCodeRover: Autonomous Program Improvement ↗ compared_alternative [Open Access / CC BY 4.0]

Focuses on autonomous program repair via code search; SWE-agent better matches the curriculum's bounded-tool and human-verification emphasis.

linking Open Access / CC BY 4.0

Reflexion: Language Agents with Verbal Reinforcement Learning

作者 / 機構: Noah Shinn, Federico Cassano, Ashwin Gopinath, Karthik Narasimhan, Shunyu Yao | 原始出處連結 ↗

對應章節 / 時間戳: Section 2 & 3 (Reflexion Architecture and Memory Buffer)

驗證版本與日期: NeurIPS 2023 / arXiv:2303.11366v4 (Verified: 2026-10-30)

入選理由:

Rigorous framework analyzing dynamic error feedback, memory buffering of execution failures, and controlled self-reflection loops without updating model weights.

來源前置條件:

Reinforcement learning concepts, agent evaluation loops, and error feedback handling.

安全回退方案(離線彈性):

Error recovery state diagrams and retry budget algorithms are detailed in curriculum materials.

對比候選資源與選型分析 (2)

Single-turn generative text refinement; Reflexion operates across multi-turn external environment action-observation failures.

Domain-specific LLM-based agent for open-world Minecraft; Reflexion establishes domain-general verbal feedback control loops.

03 120 分鐘

獨立掌握:檔案系統成果物檢查

獨立掌握檢查 — 評量作業期間不得使用 AI 輔助。若接受任何輔助必須如實申報,但接受輔助的作業只計為補救練習:在聲稱掌握之前,必須完成一次新的無輔助檢查。

前置要求

完成 day-03-act-02。評量路徑需要 Bash;已驗證的執行範圍是 Linux Bash。

學習目標

用一個全新任務和可檢查的成果物,展示終端/檔案系統的獨立掌握。

操作指引

全新的遷移任務——不是第 02 學習日序列的重複。執行每個檢查之前先寫預測,再記錄觀察輸出。一切都在新建的一次性 report-sandbox 內進行,不碰外部任何內容。 程式碼區塊在受控的失敗即停子 shell 中執行:先驗證新建的 mktemp 私有根目錄,mkdir/cd 失敗會在寫入前中止——絕不會寫入當前目錄。末尾會印出一條字面的 rm -r -- <private-root> 清理命令(Bash %q 引用),只指向本次建立的私有根目錄;僅在想刪除該目錄時才執行它。 請在 Bash 中執行該程式碼區塊;實際執行範圍為 Linux Bash,zsh/dash/Windows 未執行。

自主實作與記錄

在新的 Bash shell 中執行下面整個程式碼區塊(作為一整塊),在每個 wc、diff、find 之前寫下預測。設定失敗會中止子 shell,當前目錄不會被寫入任何內容。末尾會印出一條只指向所建立根目錄的字面 rm -r -- <private-root> 清理命令;僅在想刪除時才執行:

(
set -e
SBX_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/d03rpt.XXXXXX")
[ -n "$SBX_ROOT" ]
[ -d "$SBX_ROOT" ]
[ -w "$SBX_ROOT" ]
[ -x "$SBX_ROOT" ]
mkdir "$SBX_ROOT/report-sandbox"
cd "$SBX_ROOT/report-sandbox"
mkdir data archive
printf 'month,total\njan,10\nfeb,20\n' > data/sales-q1.csv
wc -l data/sales-q1.csv
cp data/sales-q1.csv archive/sales-q1.bak
diff data/sales-q1.csv archive/sales-q1.bak
printf 'draft note\n' > data/readme.txt
mv data/readme.txt archive/notes.txt
find . -type f | sort
printf 'rm -r -- %q\n' "$SBX_ROOT"
)

須滿足的成果物檢查:備份與原檔案逐位元組一致;移動後 data/readme.txt 不再存在;最終 find 輸出恰好列出 ./archive/notes.txt、./archive/sales-q1.bak、./data/sales-q1.csv 三項。

獨立掌握標準

在無輔助條件下滿足以下全部即通過:(a) 每個 `wc`/`diff`/`find` 的預測都在執行前寫下且觀察輸出已記錄;(b) 三項成果物檢查全部成立——`diff` 結果一致、`data/` 中無 `readme.txt`、恰好三個檔案的 `find` 列表;(c) 工作未超出 `report-sandbox`。輔助只計為補救練習,在聲稱掌握之前必須完成一次新的無輔助檢查。

權威來源資源與權利證明

linking PSF License Version 2

Python Standard Library: pathlib

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

對應章節 / 時間戳: Path.exists, Path.cwd, and anchoring paths to __file__

驗證版本與日期: Python 3.12 (Verified: 2026-10-30)

入選理由:

Normative reference for the exists()/cwd() evidence that separates an absent file from a wrong working directory.

來源前置條件:

Basic Python syntax from Study Days 01-09

安全回退方案(離線彈性):

Path.exists() and Path.cwd() are exercised directly in the local drill, so no online page is required.

對比候選資源與選型分析 (2)
os.path documentation ↗ compared_alternative [PSF License Version 2]

Functionally equivalent for exists() checks; pathlib keeps the beginner syntax object-oriented and consistent across the block.

shutil — High-level file operations ↗ compared_alternative [PSF License Version 2]

Covers copying and moving whole trees rather than path modeling; pathlib remains the right reference for the filesystem checks these days rehearse.

linking PSF License Version 2

Python Standard Library: tempfile

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

對應章節 / 時間戳: TemporaryDirectory for disposable on-disk fixtures

驗證版本與日期: Python 3.12 (Verified: 2026-10-30)

入選理由:

Normative reference for creating and auto-removing fixture files inside tests without touching real user files.

來源前置條件:

Basic Python syntax from Study Days 01-09

安全回退方案(離線彈性):

TemporaryDirectory is part of the installed standard library and needs no page access.

對比候選資源與選型分析 (2)
pytest tmp_path fixture ↗ compared_alternative [MIT]

Equivalent capability scoped to the pytest runner; deferred until the curriculum adopts pytest.

io — Core tools for working with streams ↗ compared_alternative [PSF License Version 2]

Lower-level file-object interface without automatic cleanup semantics; tempfile was selected for the safe scratch-file pattern the drills require.

04 120 分鐘

間隔複習與下一步計畫

獨立掌握檢查 — 評量作業期間不得使用 AI 輔助。若接受任何輔助必須如實申報,但接受輔助的作業只計為補救練習:在聲稱掌握之前,必須完成一次新的無輔助檢查。

前置要求

完成 day-03-act-03,以及你自己記錄的第 01–03 學習日證據。

學習目標

提取三天方法的記憶,將證據與掌握標準對比,並為每個遺漏概念安排補救。

操作指引

制定一份與你實際的第 01–03 學習日證據掛鉤的間隔提取計畫——不是再寫一份通用量規。對每個概念記錄:證據狀態(已驗證/已陳舊/缺失)、將要執行的下一次無輔助檢查、具體日期、該檢查失敗時的補救行動。明確列出你可執行的證據缺口(例如 Windows 命令從未執行、實作 Agent 證據待補)——或者,若你宣告的第 01–03 學習日範圍內已有完整的當前證據,可記錄一條有依據的「宣告範圍內未發現」,並引用實際證據、附帶帶日期的複查。不得編造缺口或隱藏未執行的聲稱。承諾一次帶日期的無輔助下次檢查。

自主實作與記錄

產出一份覆蓋至少四個第 01–03 學習日概念的補救計畫。每行記錄:概念|證據狀態|下一次無輔助檢查|日期|遺漏時的補救行動。然後列出你記錄中全部可執行的證據缺口(你相信但未驗證的事);若宣告範圍內沒有,只有引用實際當前證據並承諾帶日期的新無輔助複查的明確「宣告範圍內未發現」才可接受——已有缺口必須披露,不得隱藏。最後以一次預定的新檢查收尾:在該日期無輔助預測一個新的修改/同一性程式碼片段並驗證。

獨立掌握標準

計畫包含至少四個第 01–03 學習日概念,每個都有具名證據狀態、可執行的無輔助檢查、具體日期和補救行動;列出至少一個可執行的證據缺口,或有引用宣告範圍內實際當前證據的有依據的明確「未發現」;並承諾了一次帶日期的無輔助下次檢查,即通過。輔助只計為補救練習,在聲稱掌握之前必須完成一次新的無輔助檢查。

權威來源資源與權利證明

linking Journal publisher copyright (all rights reserved) - lawful external linking only

Test-Enhanced Learning: Taking Memory Tests Improves Long-Term Retention

作者 / 機構: Henry L. Roediger III, Jeffrey D. Karpicke | 原始出處連結 ↗

對應章節 / 時間戳: Psychological Science 17(3): 249-255, Experiments 1-2

驗證版本與日期: Psychological Science, 2006 (Verified: 2026-10-30)

入選理由:

Foundational experimental evidence that retrieval practice (testing) outperforms repeated study for long-term retention; grounds the Day 88 AI-free retrieval checks in peer-reviewed learning science rather than folk advice.

來源前置條件:

Basic understanding of study sessions, retention intervals, and experimental design vocabulary.

安全回退方案(離線彈性):

The curriculum restates the experimental design and effect sizes in original prose; every sealed task runs without the paper.

對比候選資源與選型分析 (2)

Strong meta-analytic coverage of spacing effects but addresses scheduling rather than the retrieval effect itself; retained as support for the Day 90 spaced-review schedule.

Equally foundational follow-up demonstrating repeated retrieval vs repeated study; the 2006 paper is cited as the standard first controlled demonstration.

linking Journal publisher copyright (all rights reserved) - lawful external linking only

Improving Students' Learning With Effective Learning Techniques: Promising Directions From Cognitive and Educational Psychology

作者 / 機構: John Dunlosky, Katherine A. Rawson, Elizabeth J. Marsh, Mitchell J. Nathan, Daniel T. Willingham | 原始出處連結 ↗

對應章節 / 時間戳: Psychological Science in the Public Interest 14(1): 4-58; practice testing and distributed practice sections

驗證版本與日期: Psychological Science in the Public Interest, 2013 (Verified: 2026-10-30)

入選理由:

Detailed comparative review rating practice testing and distributed practice as high-utility techniques while rating highlighting and rereading low-utility; anchors the retrieval, correction, and spaced-review design of Days 88-90.

來源前置條件:

None beyond general academic reading.

安全回退方案(離線彈性):

Curriculum restates the utility ratings and application conditions in original prose; every task remains runnable offline.

對比候選資源與選型分析 (2)
Pashler et al. Learning Styles: Concepts and Evidence ↗ compared_alternative [Publisher copyright (originally Wiley; now SAGE Publications)]

Important negative result on learning-styles matching but narrower scope; Dunlosky et al. rates a broader set of techniques directly usable for this curriculum.

Brown, Roediger & McDaniel: Make It Stick: The Science of Successful Learning ↗ compared_alternative [Publisher copyright (Harvard University Press)]

Strong trade-book synthesis of the same evidence base; the peer-reviewed review is preferred for citation precision and technique-by-technique utility ratings.