Skip to content

Latest commit

 

History

History
148 lines (103 loc) · 5.15 KB

File metadata and controls

148 lines (103 loc) · 5.15 KB
layout default
title Chapter 4: Terminal and Runtime Tools
nav_order 4
parent Cline Tutorial

Chapter 4: Terminal and Runtime Tools

Welcome to Chapter 4: Terminal and Runtime Tools. In this part of Cline Tutorial: Agentic Coding with Human Control, you will build an intuitive mental model first, then move into concrete implementation details and practical production tradeoffs.

One of Cline's strongest capabilities is command execution with output feedback. This chapter shows how to use that safely and effectively.

Command Loop

flowchart LR
    A[Run Command] --> B[Capture Output]
    B --> C[Interpret Failure or Success]
    C --> D[Patch or Next Step]
    D --> E[Re-run Validation]
    E --> F[Done or Iterate]
Loading

High-Value Command Classes

Command Type Typical Use
lint/static checks quick syntax and style signal
unit tests verify behavior on targeted modules
integration tests validate cross-module contracts
build checks detect bundling/type/runtime issues
diagnostics reproduce and isolate environment failures

Command Approval Policy

Set clear defaults:

  • read-only and low-risk commands can be broadly approved
  • mutating or destructive commands require explicit confirmation
  • commands outside repo scope should be blocked by default

Canonical Command Catalog

Define repo-level canonical commands for Cline to use:

lint: pnpm lint
test: pnpm test
test:target: pnpm test -- <module>
build: pnpm build

This reduces random command attempts and flaky behavior.

Long-Running Process Pattern

For dev servers/watchers:

  1. start one long-running process
  2. allow Cline to proceed while process is running
  3. run separate short validation commands for checks
  4. stop and restart only when environment changes require it

This avoids repeated startup overhead.

Terminal Safety Controls

Control Why It Matters
per-command approval prevents accidental destructive actions
timeout limits avoids runaway loops
retry caps stops endless failing retries
command denylist blocks known-dangerous actions
scoped working directory limits blast radius

Failure Triage Pattern

When command fails:

  1. classify error type (dependency, syntax, environment, flaky test)
  2. ask for minimal fix in known files
  3. rerun only relevant command first
  4. expand to broader checks after targeted pass

This speeds convergence.

Evidence Requirements

Before accepting task completion, require:

  • exact command(s) executed
  • pass/fail status
  • key error lines or success indicators
  • relationship between patch and command outcome

Chapter Summary

You now have a command-execution model that balances:

  • agent autonomy
  • runtime safety
  • deterministic validation
  • fast failure recovery

Next: Chapter 5: Browser Automation

Source Code Walkthrough

src/integrations/terminal/TerminalManager.ts

The TerminalManager in src/integrations/terminal/TerminalManager.ts manages VS Code terminal instances for Cline's command execution. It handles creating terminals, running commands, capturing output streams, and detecting when long-running processes have finished or need user intervention.

This file is the direct implementation of the terminal tool behavior described in this chapter. The runCommand method shows how Cline executes shell commands: it spawns them in a VS Code terminal, monitors output, and signals completion or timeout back to the agent loop.

src/core/Cline.ts (execute_command handler)

Within src/core/Cline.ts, the execute_command tool handler shows the approval flow before any shell command runs: the proposed command is surfaced to the user in the Cline sidebar, and execution only proceeds after explicit approval. This is the human-in-the-loop gate for all terminal operations.

The handler also covers the "background process" pattern: commands that produce a server or watcher are detected by output patterns, and Cline continues without waiting for process exit.

src/services/shell/ShellIntegration.ts

The shell integration in src/services/shell/ShellIntegration.ts hooks into VS Code's terminal shell integration API to detect command boundaries — when a command starts and ends — without relying on fragile output parsing. This is what allows Cline to know when a build or test run has completed and capture the full exit code.

How These Components Connect

flowchart TD
    A[Agent proposes execute_command tool call]
    B[Cline.ts surfaces command to user sidebar]
    C{User approves?}
    D[TerminalManager creates or reuses VS Code terminal]
    E[Command runs with ShellIntegration tracking]
    F[Output streamed to Cline context]
    G[Completion or timeout detected]
    H[Command blocked, not executed]
    A --> B
    B --> C
    C -- yes --> D
    D --> E
    E --> F
    F --> G
    C -- no --> H
Loading