mode_state
Skill5 starsChanged 12 days agoMITView on GitHub
独占模式状态管理,支持互斥检查和跨会话恢复
In this repository119 other files it gives its agents.
- AGENTS.mdAGENTS.md
- templates/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- accessibilitySkill
- frontend-designSkill
- python-background-jobsSkill
- python-executorSkill
- python-testing-patternsSkill
- seoSkill
- ai-slop-cleanerSkill
- analyzeSkill
- api-and-interface-designSkill
- ask-claudeSkill
- ask-geminiSkill
- autopilotSkill
- browser-testing-with-devtoolsSkill
- build-fixSkill
- cancelSkill
- ci-cd-and-automationSkill
- clawd-change-workflowSkill
- code-review-and-qualitySkill
- code-reviewSkill
- code-simplificationSkill
- configure-notificationsSkill
- context-engineeringSkill
- debugging-and-error-recoverySkill
- deep-interviewSkill
- deepsearchSkill
- deprecation-and-migrationSkill
- doctorSkill
- documentation-and-adrsSkill
- ecomodeSkill
- frontend-ui-engineeringSkill
- frontend-ui-uxSkill
- git-masterSkill
- git-workflow-and-versioningSkill
- helpSkill
- hudSkill
- idea-refineSkill
- incremental-implementationSkill
- intent_routerSkill
- keyword_registrySkill
- noteSkill
- omx-setupSkill
- performance-optimizationSkill
- pipeline_orchestratorSkill
- pipelineSkill
- planning-and-task-breakdownSkill
- planSkill
- ralph-initSkill
- ralph_loopSkill
- ralphSkill
- ralplanSkill
- android-reverse-engineeringSkill
- api-securitySkill
- apk-reverseSkill
- attack-chainSkill
- binary-diffSkill
- browser-automationSkill
- browser-extension-reverseSkill
- The first 60 shown.
The file
Files
Overview
实现会话间模式状态的持久化与互斥控制,防止多个并行任务冲突,并支持在会话重启后恢复先前的模式选择。
When to Use
- 需要限制同一时间只能执行一种特定模式(如仅允许“build”或“review”)的场景
- 需要在用户切换会话后保持上一次的模式选项,以便继续执行未完成的任务
- 在多代理协作环境中对执行模式进行统一调度
Implementation Details
- 状态持久化
- 使用
pickle或json写入/读出位于~/.claude/memory/mode_state.json的状态文件。 - 状态包括:
mode(字符串标识)、last_active(时间戳)、active_task_ids(进行中的任务 ID 列表)。
- 使用
- 互斥检查
- 在任何技能或代理进入
Run状态前,调用ModeState.acquire(mode_name)检查当前是否已锁定。 - 若锁定冲突模式则抛出
ModeConflictError,阻止执行。 release()用于在任务结束时归还锁。
- 在任何技能或代理进入
- 模式恢复
- 启动新会话时,读取上一次记录的
mode,自动将 UI 或命令行处置为该模式。 - 通过
ModeState.get_active_mode()提供当前模式访问。
- 启动新会话时,读取上一次记录的
- 线程安全
- 使用文件锁 (
portalocker) 保证并发写入安全。
- 使用文件锁 (
- API 设计
acquire(mode: str) -> bool:尝试获取锁,成功返回True。release(mode: str):释放锁。is_locked(mode: str) -> bool:检查指定模式是否已被占用。set_last_active(mode: str):记录最近活跃时间。get_active_mode() -> str | None:获取当前全局锁定的模式。
Tests
- 单元测试验证互斥锁的正确获取与释放。
- 集成测试模拟多会话情境下的模式恢复。
- 边界测试检查并发访问时的文件锁行为。
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

