Welcome to the final chapter of the Shared project tutorial!
In the previous chapter, Execution Backend Strategies, we learned how to spawn agents in different environmentsβwhether invisibly in the background or visibly in a split-pane terminal.
Now that our agents are happily running shell commands, we face a new question: How do we know what they are achieving?
Imagine you are watching a sports game. You don't just want to watch the players run back and forth; you want to know the score.
In our agent system:
npm install, git push).Command Telemetry acts as the Scoreboard Keeper. It sits quietly on the sidelines, watching every command the agent types. When it sees something interestingβlike a Git commitβit notes it down.
The Problem:
The Solution: A utility layer that uses "Pattern Matching" (Regex) to read command strings. It extracts useful data (like branch names or PR numbers) and sends it to our analytics system.
We achieve this using three simple concepts.
The telemetry system does not execute code. It receives the command string after or during execution. It reads the text just like a human reading a log file.
We use Regular Expressions (Regex) to identify specific tools.
git?commit?https://github.com/.../pull/123?When a pattern matches, we fire an event.
git push origin maintengu_git_operation: { operation: 'push' }Let's see how we track the "Holy Grail" of agent tasks: Creating a Pull Request.
The agent might type this command:
gh pr create --title "Fix login bug" --body "Fixed the typo."
Our system needs to:
gh pr create was typed.Here is how the data flows from the agent's terminal to our analytics system.
The logic lives in gitOperationTracking.ts. Let's break it down into small pieces.
First, we need to teach the system what a "Git Command" looks like. We use a helper function gitCmdRe (Git Command Regex).
// gitOperationTracking.ts
// Creates a pattern that looks for "git <command>"
// It handles edge cases like flags: "git -c user.name=Bot commit"
function gitCmdRe(subcmd: string): RegExp {
return new RegExp(
`\\bgit(?:\\s+-[cC]\\s+\\S+|\\s+--\\S+=\\S+)*\\s+${subcmd}\\b`
)
}
// Now we create specific detectors
const GIT_COMMIT_RE = gitCmdRe('commit') // Detects commits
const GIT_PUSH_RE = gitCmdRe('push') // Detects pushes
Explanation: We define standard patterns so we don't have to rewrite complex logic every time we want to find a push or a commit.
This is the brain of the operation. The function trackGitOperations takes the command string and the exit code.
Crucial Check: We only track successful commands. If the agent tried to commit but failed (Exit Code 1), we don't count it.
export function trackGitOperations(
command: string,
exitCode: number,
stdout?: string,
): void {
// 1. If the command failed, ignore it.
const success = exitCode === 0
if (!success) return
// 2. Check for Commits
if (GIT_COMMIT_RE.test(command)) {
logEvent('tengu_git_operation', { operation: 'commit' })
// We also increment a counter for stats
getCommitCounter()?.add(1)
}
// ... (Checks for other commands continue below)
}
Explanation: This function acts as a series of "If" statements. If it sees a commit, it logs a commit. Simple and effective.
Detecting a PR is slightly more complex because different tools can create them (GitHub CLI gh, GitLab CLI glab, or even curl).
We define a list of actions to look for:
const GH_PR_ACTIONS = [
{ re: /\bgh\s+pr\s+create\b/, action: 'created', op: 'pr_create' },
{ re: /\bgh\s+pr\s+merge\b/, action: 'merged', op: 'pr_merge' },
// ... other actions like edit, close, etc.
]
Then, inside our main tracking function, we check this list:
// Inside trackGitOperations...
const prHit = GH_PR_ACTIONS.find(a => a.re.test(command))
if (prHit) {
// Log that a PR operation happened
logEvent('tengu_git_operation', { operation: prHit.op })
}
Explanation: This makes the system extensible. If we want to support a new tool later, we just add it to the GH_PR_ACTIONS list.
Merely knowing a PR was created isn't enough. We want the URL so we can show it to the user. We look at the stdout (the text the command printed to the screen).
// Helper to find a URL like https://github.com/owner/repo/pull/123
function findPrInStdout(stdout: string) {
const match = stdout.match(/https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/\d+/)
return match ? parsePrUrl(match[0]) : null
}
// Inside trackGitOperations...
if (prHit?.action === 'created' && stdout) {
const prInfo = findPrInStdout(stdout)
if (prInfo) {
// Advanced: Link this coding session to the PR URL in the database
linkSessionToPR(sessionId, prInfo.prNumber, prInfo.prUrl, ...)
}
}
Explanation: This is powerful. By reading the output text, we automatically link the Agent's workspace to the actual Pull Request on GitHub. The user can click a link in the UI to go straight to the code.
Congratulations! You have completed the Shared project tutorial. Let's recap what we've built together:
You now understand the fundamental infrastructure required to run a robust, multi-agent AI coding team. These utilities ensure that agents are identifiable, manageable, visible, and measurable.
Happy Coding!
Generated by Code IQ