MCPcopy Create free account
hub / github.com/Louisym/MiniCC / demonstrate_auto_background

Function demonstrate_auto_background

tutorials/14_bash_engine_deep_dive.py:641–679  ·  view source on GitHub ↗

自动后台化 — reference/06-bash-engine.md 问题: 模型请求执行一个命令,但这个命令跑了 30 秒还没完。 如果一直等着,用户体验很差。 解决方案: 四条后台化路径: 1. 显式: 模型设置 run_in_background=true 2. 超时: 命令超过默认超时时间 3. 助手模式: 在主代理中阻塞 > 15 秒 4. 用户: 按 Ctrl+B 手动后台化 特殊规则: sleep 命令禁止自动后台化!

()

Source from the content-addressed store, hash-verified

639
640
641def demonstrate_auto_background():
642 """自动后台化 — reference/06-bash-engine.md
643
644 问题: 模型请求执行一个命令,但这个命令跑了 30 秒还没完。
645 如果一直等着,用户体验很差。
646
647 解决方案: 四条后台化路径:
648 1. 显式: 模型设置 run_in_background=true
649 2. 超时: 命令超过默认超时时间
650 3. 助手模式: 在主代理中阻塞 > 15 秒
651 4. 用户: 按 Ctrl+B 手动后台化
652
653 特殊规则: sleep 命令禁止自动后台化!
654 """
655 print("\n=== 自动后台化策略 ===")
656
657 AUTO_BG_THRESHOLD_SECONDS = 15 # 主代理阻塞阈值
658 SLEEP_COMMANDS = {"sleep"} # 禁止自动后台化的命令
659
660 def should_auto_background(command: str, elapsed: float) -> bool:
661 """决定是否自动后台化"""
662 # 检查是否是 sleep 命令
663 first_word = command.strip().split()[0] if command.strip() else ""
664 if first_word in SLEEP_COMMANDS:
665 return False # sleep 不允许自动后台化
666 return elapsed > AUTO_BG_THRESHOLD_SECONDS
667
668 test_cases = [
669 ("npm install", 20.0, True),
670 ("sleep 30", 20.0, False), # sleep 被排除!
671 ("cargo build", 5.0, False), # 还没超时
672 ("make -j8", 16.0, True),
673 ]
674
675 for cmd, elapsed, expected in test_cases:
676 result = should_auto_background(cmd, elapsed)
677 status = "✓" if result == expected else "✗"
678 action = "后台化" if result else "继续等待"
679 print(f" {status} '{cmd}' 已运行 {elapsed}s → {action}")
680
681
682def demonstrate_output_size_watchdog():

Callers 1

Calls 1

should_auto_backgroundFunction · 0.85

Tested by

no test coverage detected