The agent-run and agent-test subcommands are designed to integrate code agents (like Claude, Gemini, etc.) into the git-po-helper workflow for automating localization tasks.
agent-run update-pot:
git-po-helper agent-run update-pot [--agent <agent-name>]This command uses a code agent with a configured prompt to update the po/git.pot template file according to po/README.md.
agent-test update-pot:
git-po-helper agent-test update-pot [--agent <agent-name>] [--runs <n>]This command runs the agent-run update-pot operation multiple times (default: 5, configurable via --runs or config file) and provides an average score where success = 100 points and failure = 0 points.
The commands read from git-po-helper.yaml configuration file. Example:
default_lang_code: "zh_CN"
prompt:
update_pot: "update po/git.pot according to po/README.md"
update_po: "update {{.source}} according to po/README.md"
translate: "translate {{.source}} according to po/README.md"
review: "review and improve {{.source}} according to po/README.md"
agent-test:
runs: 5
pot_entries_before_update: null
pot_entries_after_update: null
po_entries_before_update: null
po_entries_after_update: null
po_new_entries_after_update: null
po_fuzzy_entries_after_update: null
agents:
claude:
cmd: ["claude", "-p", "{{.prompt}}"]
gemini:
cmd: ["gemini", "--prompt", "{{.prompt}}"]-
Agent Selection: If only one agent is configured,
--agentflag is optional. If multiple agents exist,--agentis required. -
Prompt Template: The prompt from
prompt.update_potis used, with placeholders replaced:{{.prompt}}→ the actual prompt text{{.source}}→ po file path (not used in update-pot){commit}→ commit ID (default: HEAD, not used in update-pot)
-
Command Execution: The agent command from
agents.<agent-name>.cmdis executed with placeholders replaced. -
Testing Mode:
agent-testruns the operation--runstimes (default from config) and calculates average success rate. -
Entry Count Validation: If
pot_entries_before_updateandpot_entries_after_updateare configured (not null and not 0), the system will:- Count entries in
po/git.potbefore calling the agent - Verify the count matches
pot_entries_before_update(if configured) - Count entries in
po/git.potafter calling the agent - Verify the count matches
pot_entries_after_update(if configured) - If validation fails, the operation is marked as failed (score = 0)
- If validation passes, the operation is marked as successful (score = 100)
- If these values are not configured (null or 0), no validation is performed
- Count entries in
┌─────────────────────────────────────────────────────────────┐
│ git-po-helper │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ agent-run │ │ agent-test │ │
│ │ │ │ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │update-pot│ │ │ │update-pot│ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │update-po │ │ │ │update-po │ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │translate │ │ │ │translate │ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │review │ │ │ │review │ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │
│ └──────────────┘ └──────────────┘ │
│ │ │ │
│ └──────────────┬──────────────┘ │
│ │ │
│ ┌──────────────▼──────────────┐ │
│ │ util/agent.go │ │
│ │ - LoadConfig() │ │
│ │ - RunAgent() │ │
│ │ - CountPotEntries() │ │
│ └──────────────────────────────┘ │
│ │ │
│ ┌──────────────▼──────────────┐ │
│ │ config/agent.go │ │
│ │ - AgentConfig struct │ │
│ │ - LoadYAML() │ │
│ └──────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
File Location: git-po-helper.yaml in the repository root (same directory as .git/).
Configuration Structure:
type AgentConfig struct {
DefaultLangCode string `yaml:"default_lang_code"`
Prompt PromptConfig `yaml:"prompt"`
AgentTest AgentTestConfig `yaml:"agent-test"`
Agents map[string]Agent `yaml:"agents"`
}
type PromptConfig struct {
UpdatePot string `yaml:"update_pot"`
UpdatePo string `yaml:"update_po"`
Translate string `yaml:"translate"`
Review string `yaml:"review"`
}
type AgentTestConfig struct {
Runs *int `yaml:"runs"`
PotEntriesBeforeUpdate *int `yaml:"pot_entries_before_update"`
PotEntriesAfterUpdate *int `yaml:"pot_entries_after_update"`
PoEntriesBeforeUpdate *int `yaml:"po_entries_before_update"`
PoEntriesAfterUpdate *int `yaml:"po_entries_after_update"`
PoNewEntriesAfterUpdate *int `yaml:"po_new_entries_after_update"`
PoFuzzyEntriesAfterUpdate *int `yaml:"po_fuzzy_entries_after_update"`
}
type Agent struct {
Cmd []string `yaml:"cmd"`
}Configuration Loading:
- Use
gopkg.in/yaml.v3orgithub.com/spf13/viper(already in use) to load YAML - Search for
git-po-helper.yamlin repository root - Validate required fields (at least one agent, prompt.update_pot)
- Merge CLI arguments with config (CLI takes precedence)
Flow:
- Load configuration from
git-po-helper.yaml - Determine agent to use:
- If
--agentprovided, use that agent - If only one agent in config, use it
- Otherwise, return error requiring
--agent
- If
- Pre-validation (if
pot_entries_before_updateis configured and not 0):- Count entries in
po/git.potusingCountPotEntries() - Compare with
pot_entries_before_update - If mismatch, return error and exit (score = 0)
- Count entries in
- Get prompt from
prompt.update_pot - Replace placeholders in agent command:
{{.prompt}}→ prompt text
- Execute agent command
- Post-validation (if
pot_entries_after_updateis configured and not 0):- Count entries in
po/git.potusingCountPotEntries() - Compare with
pot_entries_after_update - If mismatch, return error and exit (score = 0)
- Count entries in
- Validate pot file syntax (using
msgfmtor similar) - Return success/failure
Success Criteria:
- Agent command exits with code 0
po/git.potfile exists and is valid (can be checked withmsgfmt)- Pre-validation (if configured): Entry count before update matches
pot_entries_before_update - Post-validation (if configured): Entry count after update matches
pot_entries_after_update - If validation is not configured (null or 0), validation steps are skipped
Error Handling:
- Configuration file not found: warn but continue with defaults if possible
- Agent not found: return error
- Agent command fails: return error with stderr output
- Invalid pot file: return error
- Pre-validation failure: Return error with message indicating expected vs actual entry count before update (score = 0)
- Post-validation failure: Return error with message indicating expected vs actual entry count after update (score = 0)
Flow:
- Load configuration
- Determine number of runs:
- Use
--runsif provided - Otherwise use
agent-test.runsfrom config - Default to 5 if neither provided
- Use
- For each run:
- Pre-validation (if
pot_entries_before_updateis configured and not 0):- Count entries in
po/git.potbefore update - Verify count matches
pot_entries_before_update - If mismatch, mark as failure (score = 0) and skip agent execution
- Count entries in
- Run
agent-run update-potlogic (if pre-validation passed) - Post-validation (if
pot_entries_after_updateis configured and not 0):- Count entries in
po/git.potafter update - Verify count matches
pot_entries_after_update - If mismatch, mark as failure (score = 0)
- If match, mark as success (score = 100)
- Count entries in
- If validation is not configured, use agent command exit code:
- Success (exit code 0) = 100 points
- Failure (non-zero exit code) = 0 points
- Record score for this run
- Pre-validation (if
- Calculate average score:
(sum of scores) / number of runs - Display results:
- Individual run results (including validation status)
- Average score
- Summary statistics (success count, failure count, validation failures)
Scoring:
- Success = 100 points
- Failure = 0 points
- Average = sum of scores / number of runs
Entry Count Validation:
- If
pot_entries_before_updateis configured (not null and not 0):- Before agent execution, count entries in
po/git.pot - If count does not match
pot_entries_before_update, mark run as failed (score = 0) - Skip agent execution if pre-validation fails
- Before agent execution, count entries in
- If
pot_entries_after_updateis configured (not null and not 0):- After agent execution, count entries in
po/git.pot - If count does not match
pot_entries_after_update, mark run as failed (score = 0) - If count matches, mark run as successful (score = 100)
- After agent execution, count entries in
- If validation is not configured (null or 0):
- No entry count validation is performed
- Scoring is based solely on agent command exit code (0 = success, non-zero = failure)
The entry count validation is a critical feature for ensuring the agent updates the POT file correctly. The validation behavior is determined by the configuration values pot_entries_before_update and pot_entries_after_update.
-
Null or Zero Values: If a validation field is
nullor0, validation is disabled for that stage. The system will not perform any entry count checking. -
Non-Zero Values: If a validation field has a non-zero value, validation is enabled and the system will:
- Count entries in
po/git.potat the specified stage - Compare the actual count with the expected value
- Mark the operation as failed (score = 0) if counts don't match
- Mark the operation as successful (score = 100) if counts match
- Count entries in
When: pot_entries_before_update is configured (not null and not 0)
Process:
- Count entries in
po/git.potusingCountPotEntries() - Compare with
pot_entries_before_update - If mismatch:
- Log error message:
"entry count before update: expected {expected}, got {actual}" - Return error immediately
- Do not execute agent command
- Score = 0
- Log error message:
- If match:
- Continue to agent execution
- Score determined by post-validation or agent exit code
Use Case: Ensures the POT file is in the expected state before the agent runs, useful for testing scenarios where you want to verify the starting condition.
When: pot_entries_after_update is configured (not null and not 0)
Process:
- Execute agent command (if pre-validation passed or was disabled)
- Count entries in
po/git.potusingCountPotEntries() - Compare with
pot_entries_after_update - If mismatch:
- Log error message:
"entry count after update: expected {expected}, got {actual}" - Return error
- Score = 0
- Log error message:
- If match:
- Mark operation as successful
- Score = 100
Use Case: Verifies that the agent correctly updated the POT file with the expected number of entries. This is the primary validation for ensuring agent correctness.
In agent-test mode, validation is performed for each run:
- Pre-validation failure: The run is marked as failed (score = 0) and the agent is not executed for that run
- Post-validation failure: The run is marked as failed (score = 0) even if the agent command succeeded
- Both validations pass: The run is marked as successful (score = 100)
- Validation disabled: Scoring is based on agent command exit code (0 = 100, non-zero = 0)
The average score is calculated as: (sum of all run scores) / number of runs
Scenario 1: Both validations enabled
agent-test:
pot_entries_before_update: 5000
pot_entries_after_update: 5100- Before agent: Verify 5000 entries (fail if not)
- After agent: Verify 5100 entries (fail if not)
- Success only if both match
Scenario 2: Only post-validation enabled
agent-test:
pot_entries_before_update: null
pot_entries_after_update: 5100- Before agent: No validation
- After agent: Verify 5100 entries (fail if not)
Scenario 3: Validation disabled
agent-test:
pot_entries_before_update: null
pot_entries_after_update: null- No entry count validation
- Scoring based on agent exit code only
func CountPotEntries(potFile string) (int, error)Counts msgid entries in a POT file by:
- Opening the file
- Scanning for lines starting with
^msgid - Counting non-empty msgid entries (excluding header)
func RunAgentAndParse(cmd []string, outputFormat, kind string) (stdout, originalStdout, stderr []byte, streamResult AgentStreamResult, err error)Executes the agent command (uses ExecuteAgentCommandStream internally):
- Replaces placeholders in command before calling
- Runs in current working directory
- For json format: streams and parses output in real-time
- For default format: buffers output then parses
- Returns stdout, raw stdout, stderr, and stream result for diagnostics
func ValidatePoFile(potFile string) errorValidates POT file syntax using msgfmt --check-format or similar.
func ValidatePotEntryCount(potFile string, expectedCount *int, stage string) errorValidates entry count in POT file:
- If
expectedCountis nil or 0, returns nil (no validation) - Otherwise, counts entries using
CountPotEntries() - Compares with
expectedCount - Returns error if mismatch, nil if match
stageparameter is used for error messages ("before update" or "after update")
Existing Code Reuse:
util/update.go:UpdatePotFile()- for understanding pot file handlingrepository/repository.go:WorkDir()- for getting repository rootutil/const.go:PoDir,GitPotconstantsflag/flag.go: Pattern for flag handling
New Dependencies:
- YAML parser: Use
gopkg.in/yaml.v3(add togo.mod) - Or extend viper to support YAML config files
Tasks:
- Create
config/agent.gofor configuration structures - Create
util/agent.gofor agent execution logic - Add YAML dependency to
go.modif needed
Files to Create:
config/agent.go- Configuration structures and loadingutil/agent.go- Agent execution utilities
Files to Modify:
go.mod- Add YAML dependency (if not using viper's YAML support)
Validation:
go buildsucceeds- Configuration structures compile
Tasks:
- Implement
LoadAgentConfig()function inconfig/agent.go - Search for
git-po-helper.yamlin repository root - Parse YAML into
AgentConfigstruct - Validate required fields
- Handle missing config file gracefully
Functions to Implement:
func LoadAgentConfig() (*AgentConfig, error)
func (c *AgentConfig) Validate() errorValidation:
- Unit test with sample YAML file
- Test missing config file handling
- Test invalid YAML handling
Tasks:
- Implement
CountPotEntries()inutil/agent.go - Handle file reading errors
- Parse POT file format correctly (skip header, count msgid entries)
Functions to Implement:
func CountPotEntries(potFile string) (int, error)Validation:
- Unit test with sample POT file
- Test with empty file
- Test with invalid file
Tasks:
- Implement placeholder replacement (
{{.prompt}},{{.source}},{{.commit}}) - Implement
RunAgentAndParse()function (or useExecuteAgentCommandStream+ parse) - Handle command execution errors
- Capture stdout/stderr
Functions to Implement:
type PlaceholderVars map[string]string
func ReplacePlaceholders(template string, kv PlaceholderVars) (string, error)
func RunAgentAndParse(cmd []string, outputFormat, kind string) ([]byte, []byte, []byte, AgentStreamResult, error)Validation:
- Unit test with mock commands
- Test placeholder replacement
- Test error handling
Tasks:
- Create
cmd/agent-run.gowithupdate-potsubcommand - Integrate configuration loading
- Implement agent selection logic
- Implement pre-validation:
- Check if
pot_entries_before_updateis configured (not null and not 0) - If configured, count entries and validate
- Return error if validation fails
- Check if
- Execute agent command
- Implement post-validation:
- Check if
pot_entries_after_updateis configured (not null and not 0) - If configured, count entries and validate
- Return error if validation fails
- Check if
- Validate pot file syntax after update
- Return appropriate exit codes
Files to Create:
cmd/agent-run.go- Main command structureutil/agent-run.go- Business logic for agent-run
Functions to Implement:
func CmdAgentRunUpdatePot(agentName string) error
func ValidatePotEntryCount(potFile string, expectedCount *int, stage string) errorValidation:
- Integration test with mock agent
- Test agent selection logic
- Test pre-validation (matching and non-matching counts)
- Test post-validation (matching and non-matching counts)
- Test with validation disabled (null/0 values)
- Test error cases
Tasks:
- Create
cmd/agent-test.gowithupdate-potsubcommand - Implement run loop
- Implement validation logic:
- For each run, perform pre-validation if
pot_entries_before_updateis configured - Skip agent execution if pre-validation fails
- Perform post-validation if
pot_entries_after_updateis configured - Score based on validation results (100 if pass, 0 if fail)
- For each run, perform pre-validation if
- Implement scoring logic:
- If validation is configured: score based on validation results
- If validation is not configured: score based on agent exit code
- Display results in readable format:
- Show validation status for each run
- Show entry counts (expected vs actual) for failed validations
- Handle optional validation assertions
Files to Create:
cmd/agent-test.go- Main command structureutil/agent-test.go- Business logic for agent-test
Functions to Implement:
func CmdAgentTestUpdatePot(agentName string, runs int) error
func RunAgentTestUpdatePot(agentName string, runs int) ([]int, float64, error)
// Returns: scores for each run, average score, errorValidation:
- Unit test scoring logic with validation enabled/disabled
- Integration test with multiple runs
- Test pre-validation failures (should skip agent execution)
- Test post-validation failures (should mark as failed)
- Test with validation disabled (should use agent exit code)
- Test optional assertions
Tasks:
- Add
--agentflag to both commands - Add
--runsflag toagent-test - Register commands in
cmd/root.go - Add help text
Files to Modify:
cmd/agent-run.go- Add flagscmd/agent-test.go- Add flagscmd/root.go- Register commands
Validation:
git-po-helper agent-run -hshows helpgit-po-helper agent-test -hshows help- Flags are parsed correctly
Tasks:
- Add appropriate log messages
- Handle all error cases gracefully
- Provide helpful error messages
- Use existing logging patterns (logrus)
Validation:
- Error messages are clear and actionable
- Logging follows project conventions
Tasks:
- Update
README.mdor createdocs/agent-commands.md - Add example configuration file
- Write integration tests
- Test with real agents (if available)
Files to Create/Modify:
docs/agent-commands.md- User documentationtest/t*agent-run*.sh- Integration tests- Example
git-po-helper.yamlin docs
Validation:
- Documentation is clear and complete
- Integration tests pass
- Example config works
Tasks:
- Code review for Go best practices
- Ensure consistency with existing codebase
- Optimize performance if needed
- Add additional error handling if needed
Validation:
- Code follows project conventions
- No linter errors
- Performance is acceptable
- Configuration loading with various YAML formats
- Placeholder replacement
- Pot entry counting
- Agent command execution (mocked)
- Full
agent-run update-potworkflow with mock agent - Full
agent-test update-potworkflow with multiple runs - Entry count validation scenarios:
- Pre-validation success and failure
- Post-validation success and failure
- Validation disabled (null/0 values)
- Both validations enabled
- Only pre-validation enabled
- Only post-validation enabled
- Error scenarios (missing config, invalid agent, etc.)
- Test with real agent commands (if available)
- Verify pot file updates correctly
- Verify scoring works as expected
- Support for other subcommands (update-po, translate, review)
- Caching of agent responses
- Parallel execution for agent-test
- More sophisticated scoring metrics
- Integration with CI/CD pipelines