Compare commits
23 Commits
v0.1.9
...
fd7a98b263
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd7a98b263 | ||
|
|
d0b100fca8 | ||
| da0fa302de | |||
|
|
54aa6419eb | ||
| 98a31070a7 | |||
| 26346235c9 | |||
| 2212fabf22 | |||
|
|
0fa778353b | ||
| 151efadca3 | |||
|
|
379d53cedc | ||
|
|
043542344a | ||
| e763ceb0ad | |||
|
|
61f06f825f | ||
| b76a9b883a | |||
|
|
ac850869fd | ||
|
|
3107dbf1e5 | ||
|
|
b8b97e3c09 | ||
|
|
d8af560e6d | ||
| 5d12f6ca42 | |||
|
|
91505345a2 | ||
| f7fe22de25 | |||
|
|
3ce43ffa65 | ||
|
|
416e8e5757 |
67
.github/ISSUES/fix-queue-daemon-excess-agents.md
vendored
Normal file
67
.github/ISSUES/fix-queue-daemon-excess-agents.md
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
# Fix: Queue daemon spawning excess agents due to race condition
|
||||
|
||||
## Problem
|
||||
|
||||
When enqueueing multiple tasks (e.g., 6 tasks), the queue daemon was spawning many more subagents than expected, eventually exhausting container memory.
|
||||
|
||||
**Root Cause:** The combination of:
|
||||
1. `process_queue()` calling `opencode run` directly instead of `kugetsu start`, bypassing all concurrency logic
|
||||
2. `count_active_dev_sessions()` counting `pm-agent.json` toward `MAX_CONCURRENT_AGENTS`, reducing effective dev agent slots
|
||||
3. No atomic locking around session count check + session file creation (TOCTOU race condition)
|
||||
4. Background spawning of multiple concurrent processes in `process_queue()`
|
||||
|
||||
**Expected behavior:** With `MAX_CONCURRENT_AGENTS=3` and 6 tasks:
|
||||
- Tasks should be processed sequentially via `kugetsu start`
|
||||
- Only 3 dev agents should run at a time
|
||||
- Tasks should queue and wait for slots to free up
|
||||
|
||||
## Solution
|
||||
|
||||
### 1. `count_active_dev_sessions()` - Exclude pm-agent
|
||||
Only count actual dev agent session files (exclude `pm-agent.json`).
|
||||
|
||||
### 2. `process_queue()` - Call `kugetsu start` directly + retry logic
|
||||
- Call `kugetsu start` directly (foreground, sequential) instead of spawning `opencode run` background process
|
||||
- Dynamic batch size = available slots (removes need for `QUEUE_DAEMON_BATCH_SIZE`)
|
||||
- Retry logic (max 3 attempts) on failure
|
||||
- On failure: cleanup worktree/session and revert to `pending` state
|
||||
- Save `fork_pid` to queue item for timeout handling
|
||||
|
||||
### 3. `cmd_start()` - Add flock
|
||||
- Add flock around critical section (count check + fork)
|
||||
- Track `fork_pid` for queue item timeout handling
|
||||
|
||||
### 4. Notification System
|
||||
New notification types:
|
||||
| Event | Type |
|
||||
|-------|------|
|
||||
| Task enqueued | `task_queued` |
|
||||
| Task dequeued | `task_dequeued` |
|
||||
| Task started | `task_started` |
|
||||
| Task completed | `task_completed` |
|
||||
| Task error | `task_error` |
|
||||
|
||||
### 5. Config
|
||||
- Remove `QUEUE_DAEMON_BATCH_SIZE` (no longer needed - batch size is now dynamic)
|
||||
|
||||
## Notification Flow
|
||||
|
||||
| Event | Location | Type |
|
||||
|-------|----------|------|
|
||||
| Task enqueued | `enqueue_task()` | `task_queued` |
|
||||
| Task dequeued | `process_queue()` after state change to `notified` | `task_dequeued` |
|
||||
| Task started | `cmd_start()` after session file created | `task_started` |
|
||||
| Task completed | `update_queue_item_state()` | `task_completed` |
|
||||
| Task error | `update_queue_item_state()` | `task_error` |
|
||||
|
||||
## Out of Scope
|
||||
|
||||
- Re-check loop in cmd_start (checking if session DB is reliable) - deferred to separate research issue
|
||||
- Buffer mechanism for excess forking (safety failsafe only)
|
||||
|
||||
## Status
|
||||
|
||||
- [x] Issue created
|
||||
- [x] Implementation
|
||||
- [x] PR created (#147)
|
||||
- [ ] Merged
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
## Workflow
|
||||
|
||||
1. Create a branch for your work: `git checkout -b fix/issue-N-name` or `git checkout -b docs/topic-name`
|
||||
1. Create a branch for your work: `git checkout -b fix/issue-N-name` or `git checkout -b feat/issue-N-feature-name`
|
||||
2. Make changes and commit with clear messages
|
||||
3. Open a Pull Request for review
|
||||
4. Do not merge directly to `master` for reviewable changes
|
||||
4. Do not merge directly to `main` for reviewable changes
|
||||
5. After approval, squash and merge
|
||||
|
||||
## Guidelines
|
||||
@@ -17,7 +17,10 @@
|
||||
|
||||
## Branches
|
||||
|
||||
- `master` — stable, reviewed content only
|
||||
- `main` — stable, reviewed content only
|
||||
- `develop` — experimental work for 0.2.x
|
||||
- `fix/*` — bug fixes
|
||||
- `feat/*` — new features
|
||||
- `docs/*` — documentation updates
|
||||
- `refactor/*` — refactoring
|
||||
- `research/*` — new research notes
|
||||
|
||||
@@ -49,6 +49,8 @@ A default config file is created during `kugetsu init` with commented examples:
|
||||
| `MAX_CONCURRENT_AGENTS` | 3 | Maximum number of concurrent dev agents |
|
||||
| `KUGETSU_TEMP_DIR` | `~/.local/share/opencode/tool-output` | Temp directory for subagent tool output (useful in headless environments where /tmp is restricted) |
|
||||
| `KUGETSU_VERBOSITY` | `default` | PM agent verbosity level: `verbose`, `default`, or `quiet` |
|
||||
| `QUEUE_DAEMON_INTERVAL_MINUTES` | 5 | How often daemon polls queue (in minutes) |
|
||||
| `QUEUE_CLEANUP_AGE_DAYS` | 7 | Auto-cleanup completed/error items older than N days |
|
||||
|
||||
### Environment Variables for Agents
|
||||
|
||||
@@ -111,6 +113,10 @@ Each issue session gets its own git worktree to prevent conflicts:
|
||||
├── worktrees/
|
||||
│ ├── github.com-shoko-kugetsu-14/ # Isolated workdir for issue #14
|
||||
│ └── github.com-shoko-kugetsu-15/ # Isolated workdir for issue #15
|
||||
├── queue/
|
||||
│ ├── items/ # Queue item JSON files
|
||||
│ ├── daemon.pid # Daemon process ID
|
||||
│ └── daemon.log # Daemon log output
|
||||
└── index.json # Maps session IDs and issue refs to session files
|
||||
```
|
||||
|
||||
@@ -258,16 +264,17 @@ kugetsu destroy --base -y
|
||||
|
||||
### kugetsu delegate `<message>`
|
||||
|
||||
Send a message to the PM agent for task coordination (fire-and-forget):
|
||||
Send a message to the PM agent for task coordination via queue:
|
||||
```bash
|
||||
kugetsu delegate "work on issue #14"
|
||||
kugetsu delegate "review PR #92"
|
||||
```
|
||||
|
||||
- Non-blocking: returns immediately, runs in background
|
||||
- PM agent processes the message asynchronously
|
||||
- Uses `KUGETSU_VERBOSITY` env var to control PM agent output verbosity
|
||||
- Log output stored in `~/.kugetsu/logs/delegate-<timestamp>.log`
|
||||
- **Always enqueues** (fire-and-forget): returns immediately
|
||||
- Queue daemon polls queue and invokes PM when slots available
|
||||
- Tasks are processed FIFO (first-in-first-out)
|
||||
- Use `kugetsu queue list` to see pending tasks
|
||||
- Use `kugetsu queue-daemon logs` to debug queue processing
|
||||
|
||||
### kugetsu logs [n]
|
||||
|
||||
@@ -328,35 +335,79 @@ kugetsu server default github # Set default server
|
||||
kugetsu server get github # Get server URL
|
||||
```
|
||||
|
||||
### kugetsu queue <list|enqueue|dequeue|clear>
|
||||
### kugetsu queue <list|stats|clear>
|
||||
|
||||
Manage task queue for autonomous PM operation:
|
||||
```bash
|
||||
kugetsu queue list # Show queued tasks
|
||||
kugetsu queue enqueue "task" # Add task to queue
|
||||
kugetsu queue dequeue # Remove next task from queue
|
||||
kugetsu queue clear # Clear all queued tasks
|
||||
kugetsu queue list # Show queued tasks with status
|
||||
kugetsu queue stats # Show queue statistics (total, pending, notified, completed, error)
|
||||
kugetsu queue clear # Clean up old completed/error items
|
||||
kugetsu queue enqueue <issue-ref> <message> # Manually enqueue a task
|
||||
```
|
||||
|
||||
- Queue stored in `~/.kugetsu/queue.json`
|
||||
**Queue Item States:**
|
||||
- `pending` - Waiting in queue, daemon can pick up
|
||||
- `notified` - PM agent has picked up the task
|
||||
- `completed` - Dev agent finished, PR created
|
||||
- `error` - Timeout or failure
|
||||
|
||||
### kugetsu queue-daemon <start|stop|restart|status|logs>
|
||||
|
||||
Manage the queue daemon background process:
|
||||
```bash
|
||||
kugetsu queue-daemon start # Start daemon in background
|
||||
kugetsu queue-daemon stop # Stop daemon
|
||||
kugetsu queue-daemon restart # Restart daemon
|
||||
kugetsu queue-daemon status # Check if daemon is running
|
||||
kugetsu queue-daemon logs # Show recent daemon logs
|
||||
```
|
||||
|
||||
**Daemon Behavior:**
|
||||
1. Runs at configurable interval (default: 5 minutes)
|
||||
2. Checks if active agents < MAX_CONCURRENT_AGENTS
|
||||
3. Picks 1-N pending items (configurable batch size)
|
||||
4. Forks PM session for each picked item
|
||||
5. PM decides whether to use `start` or `continue`
|
||||
|
||||
**Queue Directory:**
|
||||
```
|
||||
~/.kugetsu/queue/
|
||||
├── items/ # Queue item JSON files
|
||||
│ ├── q_1234567890.json # One file per queued task
|
||||
│ └── q_1234567891.json
|
||||
├── daemon.pid # Daemon process ID
|
||||
├── daemon.lock # Daemon lock file
|
||||
└── daemon.log # Daemon log output
|
||||
```
|
||||
|
||||
## Workflow Example
|
||||
|
||||
### First-time Setup
|
||||
```bash
|
||||
# First-time setup (requires TTY)
|
||||
# Initialize kugetsu (requires TTY)
|
||||
kugetsu init
|
||||
# Creates: base session + pm-agent session
|
||||
|
||||
# Start work on issue
|
||||
kugetsu start github.com/shoko/kugetsu#14 "implement feature X"
|
||||
# Creates: worktree at ~/.kugetsu/worktrees/github.com-shoko-kugetsu-14/
|
||||
# Start the queue daemon (for autonomous operation)
|
||||
kugetsu queue-daemon start
|
||||
```
|
||||
|
||||
# Continue later
|
||||
### Normal Workflow
|
||||
```bash
|
||||
# Enqueue tasks via delegate - agents will process them automatically
|
||||
kugetsu delegate "work on issue #14"
|
||||
kugetsu delegate "review PR #92"
|
||||
|
||||
# Check queue status
|
||||
kugetsu queue list # See pending tasks
|
||||
kugetsu queue stats # See statistics
|
||||
|
||||
# Debug queue daemon
|
||||
kugetsu queue-daemon status # Is daemon running?
|
||||
kugetsu queue-daemon logs # See daemon logs
|
||||
|
||||
# Continue work on existing issue
|
||||
kugetsu continue github.com/shoko/kugetsu#14 "add tests"
|
||||
|
||||
# Continue again
|
||||
kugetsu continue github.com/shoko/kugetsu#14 "fix failing test"
|
||||
|
||||
# List all sessions
|
||||
kugetsu list
|
||||
|
||||
@@ -367,6 +418,21 @@ kugetsu prune --force
|
||||
kugetsu destroy github.com/shoko/kugetsu#14
|
||||
```
|
||||
|
||||
### Queue Daemon Management
|
||||
```bash
|
||||
# Check if daemon is running
|
||||
kugetsu queue-daemon status
|
||||
|
||||
# View daemon logs for debugging
|
||||
kugetsu queue-daemon logs
|
||||
|
||||
# Restart daemon if needed
|
||||
kugetsu queue-daemon restart
|
||||
|
||||
# Stop daemon
|
||||
kugetsu queue-daemon stop
|
||||
```
|
||||
|
||||
## Headless Operation
|
||||
|
||||
This design solves the headless CLI limitation discovered in Issue #14:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,8 @@
|
||||
set -euo pipefail
|
||||
|
||||
KUGETSU="./skills/kugetsu/scripts/kugetsu"
|
||||
TEST_KUGETSU_DIR="/tmp/test-kugetsu-$$"
|
||||
export KUGETSU_DIR="$TEST_KUGETSU_DIR"
|
||||
TEST_ISSUE_REF="github.com/shoko/kugetsu#14"
|
||||
TEST_DISCUSS_REF="github.com/shoko/kugetsu#-discuss"
|
||||
TEST_BASE_SESSION_ID="ses_test_base_123"
|
||||
@@ -18,28 +20,28 @@ PASS=0
|
||||
FAIL=0
|
||||
|
||||
cleanup() {
|
||||
rm -rf ~/.kugetsu/sessions/* ~/.kugetsu/worktrees/* ~/.kugetsu/index.json 2>/dev/null || true
|
||||
rm -rf "$TEST_KUGETSU_DIR" 2>/dev/null || true
|
||||
}
|
||||
|
||||
setup_mock_base() {
|
||||
mkdir -p ~/.kugetsu/sessions ~/.kugetsu/worktrees
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
mkdir -p "$TEST_KUGETSU_DIR/sessions" "$TEST_KUGETSU_DIR/worktrees"
|
||||
cat > "$TEST_KUGETSU_DIR/index.json" << EOF
|
||||
{
|
||||
"base": "$TEST_BASE_SESSION_ID",
|
||||
"pm_agent": "$TEST_PM_AGENT_SESSION_ID",
|
||||
"issues": {}
|
||||
}
|
||||
EOF
|
||||
cat > ~/.kugetsu/sessions/$TEST_BASE_SESSION_FILE << EOF
|
||||
cat > "$TEST_KUGETSU_DIR/sessions/$TEST_BASE_SESSION_FILE" << EOF
|
||||
{"type": "base", "opencode_session_id": "$TEST_BASE_SESSION_ID", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}
|
||||
EOF
|
||||
cat > ~/.kugetsu/sessions/$TEST_PM_AGENT_SESSION_FILE << EOF
|
||||
cat > "$TEST_KUGETSU_DIR/sessions/$TEST_PM_AGENT_SESSION_FILE" << EOF
|
||||
{"type": "pm_agent", "opencode_session_id": "$TEST_PM_AGENT_SESSION_ID", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}
|
||||
EOF
|
||||
}
|
||||
|
||||
setup_mock_forked() {
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
cat > "$TEST_KUGETSU_DIR/index.json" << EOF
|
||||
{
|
||||
"base": "$TEST_BASE_SESSION_ID",
|
||||
"pm_agent": "$TEST_PM_AGENT_SESSION_ID",
|
||||
@@ -48,7 +50,7 @@ setup_mock_forked() {
|
||||
}
|
||||
}
|
||||
EOF
|
||||
cat > ~/.kugetsu/sessions/$TEST_FORKED_SESSION_FILE << EOF
|
||||
cat > "$TEST_KUGETSU_DIR/sessions/$TEST_FORKED_SESSION_FILE" << EOF
|
||||
{"type": "forked", "issue_ref": "$TEST_ISSUE_REF", "opencode_session_id": "ses_forked_789", "worktree_path": "/tmp/test-worktree", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}
|
||||
EOF
|
||||
}
|
||||
@@ -112,16 +114,16 @@ echo ""
|
||||
|
||||
# Test 3b: start fails without pm-agent
|
||||
echo "--- Test: start without pm-agent session ---"
|
||||
rm -f ~/.kugetsu/index.json ~/.kugetsu/sessions/*
|
||||
mkdir -p ~/.kugetsu/sessions
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
rm -f $TEST_KUGETSU_DIR/index.json $TEST_KUGETSU_DIR/sessions/*
|
||||
mkdir -p $TEST_KUGETSU_DIR/sessions
|
||||
cat > $TEST_KUGETSU_DIR/index.json << EOF
|
||||
{
|
||||
"base": "$TEST_BASE_SESSION_ID",
|
||||
"pm_agent": null,
|
||||
"issues": {}
|
||||
}
|
||||
EOF
|
||||
cat > ~/.kugetsu/sessions/$TEST_BASE_SESSION_FILE << EOF
|
||||
cat > $TEST_KUGETSU_DIR/sessions/$TEST_BASE_SESSION_FILE << EOF
|
||||
{"type": "base", "opencode_session_id": "$TEST_BASE_SESSION_ID", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}
|
||||
EOF
|
||||
OUTPUT=$($KUGETSU start github.com/shoko/kugetsu#14 "test" 2>&1 || true)
|
||||
@@ -176,7 +178,7 @@ echo ""
|
||||
|
||||
# Test 6c: index.json has pm_agent field
|
||||
echo "--- Test: index.json has pm_agent field ---"
|
||||
if grep -q '"pm_agent"' ~/.kugetsu/index.json; then
|
||||
if grep -q '"pm_agent"' $TEST_KUGETSU_DIR/index.json; then
|
||||
pass "index.json has pm_agent field"
|
||||
else
|
||||
fail "index.json missing pm_agent field"
|
||||
@@ -227,12 +229,12 @@ echo ""
|
||||
echo "--- Test: destroy --pm-agent -y ---"
|
||||
setup_mock_base
|
||||
OUTPUT=$($KUGETSU destroy --pm-agent -y 2>&1 || true)
|
||||
if [ -f ~/.kugetsu/sessions/$TEST_PM_AGENT_SESSION_FILE ]; then
|
||||
if [ -f $TEST_KUGETSU_DIR/sessions/$TEST_PM_AGENT_SESSION_FILE ]; then
|
||||
fail "destroy --pm-agent -y removes pm-agent file"
|
||||
else
|
||||
pass "destroy --pm-agent -y removes pm-agent file"
|
||||
fi
|
||||
if grep -q '"pm_agent": null' ~/.kugetsu/index.json; then
|
||||
if grep -q '"pm_agent": null' $TEST_KUGETSU_DIR/index.json; then
|
||||
pass "destroy --pm-agent -y sets pm_agent to null in index"
|
||||
else
|
||||
fail "destroy --pm-agent -y should set pm_agent to null"
|
||||
@@ -243,7 +245,7 @@ echo ""
|
||||
echo "--- Test: destroy --base -y ---"
|
||||
setup_mock_base
|
||||
OUTPUT=$($KUGETSU destroy --base -y 2>&1 || true)
|
||||
if [ -f ~/.kugetsu/sessions/$TEST_BASE_SESSION_FILE ]; then
|
||||
if [ -f $TEST_KUGETSU_DIR/sessions/$TEST_BASE_SESSION_FILE ]; then
|
||||
fail "destroy --base -y removes base file"
|
||||
else
|
||||
pass "destroy --base -y removes base file"
|
||||
@@ -292,7 +294,7 @@ echo ""
|
||||
|
||||
# Test 15: worktree path in session file
|
||||
echo "--- Test: worktree_path in session file ---"
|
||||
if grep -q "worktree_path" ~/.kugetsu/sessions/$TEST_FORKED_SESSION_FILE; then
|
||||
if grep -q "worktree_path" $TEST_KUGETSU_DIR/sessions/$TEST_FORKED_SESSION_FILE; then
|
||||
pass "session file contains worktree_path"
|
||||
else
|
||||
fail "session file missing worktree_path"
|
||||
@@ -303,7 +305,7 @@ echo ""
|
||||
echo "--- Test: prune with orphaned worktree ---"
|
||||
cleanup
|
||||
setup_mock_base
|
||||
mkdir -p ~/.kugetsu/worktrees/orphaned-worktree
|
||||
mkdir -p $TEST_KUGETSU_DIR/worktrees/orphaned-worktree
|
||||
OUTPUT=$($KUGETSU prune 2>&1 || true)
|
||||
if echo "$OUTPUT" | grep -q "orphaned worktree"; then
|
||||
pass "prune detects orphaned worktree"
|
||||
@@ -315,7 +317,7 @@ echo ""
|
||||
# Test 17: prune --force removes orphaned worktrees
|
||||
echo "--- Test: prune --force removes orphaned worktrees ---"
|
||||
OUTPUT=$($KUGETSU prune --force 2>&1 || true)
|
||||
if [ -d ~/.kugetsu/worktrees/orphaned-worktree ]; then
|
||||
if [ -d $TEST_KUGETSU_DIR/worktrees/orphaned-worktree ]; then
|
||||
fail "prune --force should remove orphaned worktree"
|
||||
else
|
||||
pass "prune --force removes orphaned worktree"
|
||||
@@ -332,10 +334,10 @@ echo ""
|
||||
echo "--- Test: destroy removes worktree ---"
|
||||
cleanup
|
||||
setup_mock_forked
|
||||
# remove_worktree_for_issue derives path from issue ref: ~/.kugetsu/worktrees/github.com-shoko-kugetsu-14
|
||||
mkdir -p ~/.kugetsu/worktrees/github.com-shoko-kugetsu-14
|
||||
# remove_worktree_for_issue derives path from issue ref: $TEST_KUGETSU_DIR/worktrees/github.com-shoko-kugetsu-14
|
||||
mkdir -p $TEST_KUGETSU_DIR/worktrees/github.com-shoko-kugetsu-14
|
||||
OUTPUT=$($KUGETSU destroy github.com/shoko/kugetsu#14 -y 2>&1 || true)
|
||||
if [ -d ~/.kugetsu/worktrees/github.com-shoko-kugetsu-14 ]; then
|
||||
if [ -d $TEST_KUGETSU_DIR/worktrees/github.com-shoko-kugetsu-14 ]; then
|
||||
fail "destroy should remove worktree"
|
||||
else
|
||||
pass "destroy removes worktree"
|
||||
@@ -345,7 +347,7 @@ echo ""
|
||||
# Test 20: session file properly formatted for v2.2
|
||||
echo "--- Test: session file format v2.2 ---"
|
||||
setup_mock_forked
|
||||
SESSION_CONTENT=$(cat ~/.kugetsu/sessions/$TEST_FORKED_SESSION_FILE)
|
||||
SESSION_CONTENT=$(cat $TEST_KUGETSU_DIR/sessions/$TEST_FORKED_SESSION_FILE)
|
||||
if echo "$SESSION_CONTENT" | grep -q '"type": "forked"' && \
|
||||
echo "$SESSION_CONTENT" | grep -q '"worktree_path"'; then
|
||||
pass "session file has v2.2 format"
|
||||
@@ -367,8 +369,8 @@ echo ""
|
||||
|
||||
# Test 22: status when base missing
|
||||
echo "--- Test: status (base missing) ---"
|
||||
mkdir -p ~/.kugetsu/sessions
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
mkdir -p $TEST_KUGETSU_DIR/sessions
|
||||
cat > $TEST_KUGETSU_DIR/index.json << EOF
|
||||
{
|
||||
"base": null,
|
||||
"pm_agent": "$TEST_PM_AGENT_SESSION_ID",
|
||||
@@ -385,7 +387,7 @@ echo ""
|
||||
|
||||
# Test 23: status when pm-agent missing
|
||||
echo "--- Test: status (pm-agent missing) ---"
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
cat > $TEST_KUGETSU_DIR/index.json << EOF
|
||||
{
|
||||
"base": "$TEST_BASE_SESSION_ID",
|
||||
"pm_agent": null,
|
||||
@@ -402,7 +404,7 @@ echo ""
|
||||
|
||||
# Test 24: status when pm-agent is "None" (Python None output)
|
||||
echo "--- Test: status (pm-agent is Python None) ---"
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
cat > $TEST_KUGETSU_DIR/index.json << EOF
|
||||
{
|
||||
"base": "$TEST_BASE_SESSION_ID",
|
||||
"pm_agent": "None",
|
||||
@@ -445,8 +447,8 @@ echo ""
|
||||
# Test 27: delegate when pm-agent missing
|
||||
echo "--- Test: delegate (pm-agent missing) ---"
|
||||
cleanup
|
||||
mkdir -p ~/.kugetsu/sessions ~/.kugetsu/worktrees
|
||||
cat > ~/.kugetsu/index.json << EOF
|
||||
mkdir -p $TEST_KUGETSU_DIR/sessions $TEST_KUGETSU_DIR/worktrees
|
||||
cat > $TEST_KUGETSU_DIR/index.json << EOF
|
||||
{
|
||||
"base": "$TEST_BASE_SESSION_ID",
|
||||
"pm_agent": null,
|
||||
@@ -508,7 +510,7 @@ echo ""
|
||||
# Test 32: delegate is fire-and-forget (returns immediately)
|
||||
echo "--- Test: delegate is fire-and-forget ---"
|
||||
setup_mock_base
|
||||
mkdir -p ~/.kugetsu/logs
|
||||
mkdir -p $TEST_KUGETSU_DIR/logs
|
||||
START=$(date +%s)
|
||||
OUTPUT=$($KUGETSU delegate "test fire-and-forget" 2>&1 || true)
|
||||
END=$(date +%s)
|
||||
@@ -527,10 +529,10 @@ echo ""
|
||||
# Test 33: delegate creates log file
|
||||
echo "--- Test: delegate creates log file ---"
|
||||
setup_mock_base
|
||||
LOG_COUNT_BEFORE=$(ls ~/.kugetsu/logs/*.log 2>/dev/null | wc -l)
|
||||
LOG_COUNT_BEFORE=$(ls $TEST_KUGETSU_DIR/logs/*.log 2>/dev/null | wc -l)
|
||||
$KUGETSU delegate "test log file" 2>&1 || true
|
||||
sleep 1
|
||||
LOG_COUNT_AFTER=$(ls ~/.kugetsu/logs/*.log 2>/dev/null | wc -l)
|
||||
LOG_COUNT_AFTER=$(ls $TEST_KUGETSU_DIR/logs/*.log 2>/dev/null | wc -l)
|
||||
if [ $LOG_COUNT_AFTER -gt $LOG_COUNT_BEFORE ]; then
|
||||
pass "delegate creates log file"
|
||||
else
|
||||
@@ -558,10 +560,10 @@ echo ""
|
||||
|
||||
# Test E2: env set creates file
|
||||
echo "--- Test: env set creates env file ---"
|
||||
mkdir -p ~/.kugetsu/env
|
||||
rm -f ~/.kugetsu/env/pm-agent.env
|
||||
mkdir -p $TEST_KUGETSU_DIR/env
|
||||
rm -f $TEST_KUGETSU_DIR/env/pm-agent.env
|
||||
$KUGETSU env set TEST_VAR "test_value" pm-agent 2>&1 || true
|
||||
if [ -f ~/.kugetsu/env/pm-agent.env ]; then
|
||||
if [ -f $TEST_KUGETSU_DIR/env/pm-agent.env ]; then
|
||||
pass "env set creates pm-agent.env file"
|
||||
else
|
||||
fail "env set did not create pm-agent.env"
|
||||
@@ -570,7 +572,7 @@ echo ""
|
||||
|
||||
# Test E3: env show masks sensitive values
|
||||
echo "--- Test: env show masks sensitive values ---"
|
||||
cat > ~/.kugetsu/env/pm-agent.env << 'ENVEOF'
|
||||
cat > $TEST_KUGETSU_DIR/env/pm-agent.env << 'ENVEOF'
|
||||
export GITEA_TOKEN="secret_token_123"
|
||||
export MY_VAR="visible_value"
|
||||
ENVEOF
|
||||
@@ -584,14 +586,14 @@ echo ""
|
||||
|
||||
# Test E4: Variables exported to child processes via set -a
|
||||
echo "--- Test: set -a exports variables to children ---"
|
||||
mkdir -p ~/.kugetsu/env
|
||||
cat > ~/.kugetsu/env/test.env << 'ENVEOF'
|
||||
mkdir -p $TEST_KUGETSU_DIR/env
|
||||
cat > $TEST_KUGETSU_DIR/env/test.env << 'ENVEOF'
|
||||
export EXPORT_TEST="exported_value"
|
||||
SIMPLE_TEST="not_exported"
|
||||
ENVEOF
|
||||
|
||||
# Simulate what cmd_delegate does
|
||||
ENV_FILE="~/.kugetsu/env/test.env"
|
||||
ENV_FILE="$TEST_KUGETSU_DIR/env/test.env"
|
||||
env_sh="set -a; source '$ENV_FILE'; set +a; "
|
||||
result=$(bash -c "${env_sh}bash -c 'echo \$EXPORT_TEST'")
|
||||
|
||||
@@ -604,11 +606,11 @@ echo ""
|
||||
|
||||
# Test E5: pm-agent.env takes precedence
|
||||
echo "--- Test: pm-agent.env takes precedence over default ---"
|
||||
mkdir -p ~/.kugetsu/env
|
||||
cat > ~/.kugetsu/env/default.env << 'ENVEOF'
|
||||
mkdir -p $TEST_KUGETSU_DIR/env
|
||||
cat > $TEST_KUGETSU_DIR/env/default.env << 'ENVEOF'
|
||||
export GITEA_TOKEN="default_token"
|
||||
ENVEOF
|
||||
cat > ~/.kugetsu/env/pm-agent.env << 'ENVEOF'
|
||||
cat > $TEST_KUGETSU_DIR/env/pm-agent.env << 'ENVEOF'
|
||||
export GITEA_TOKEN="pm_agent_token"
|
||||
ENVEOF
|
||||
|
||||
@@ -644,7 +646,7 @@ fi
|
||||
echo ""
|
||||
|
||||
# Cleanup env files
|
||||
rm -rf ~/.kugetsu/env 2>/dev/null || true
|
||||
rm -rf $TEST_KUGETSU_DIR/env 2>/dev/null || true
|
||||
|
||||
# Test E7: fix_session_permissions function exists
|
||||
echo "--- Test: fix_session_permissions function exists ---"
|
||||
@@ -736,7 +738,7 @@ PASS=0
|
||||
FAIL=0
|
||||
|
||||
test_cleanup() {
|
||||
rm -rf ~/.kugetsu/sessions/* ~/.kugetsu/worktrees/* ~/.kugetsu/index.json ~/.kugetsu/logs/* ~/.kugetsu/.agent_count ~/.kugetsu/.agent_lock 2>/dev/null || true
|
||||
rm -rf $TEST_KUGETSU_DIR/sessions/* $TEST_KUGETSU_DIR/worktrees/* $TEST_KUGETSU_DIR/index.json $TEST_KUGETSU_DIR/logs/* $TEST_KUGETSU_DIR/.agent_count $TEST_KUGETSU_DIR/.agent_lock 2>/dev/null || true
|
||||
}
|
||||
|
||||
pass() {
|
||||
@@ -750,25 +752,25 @@ fail() {
|
||||
}
|
||||
|
||||
setup_mock_sessions() {
|
||||
mkdir -p ~/.kugetsu/sessions ~/.kugetsu/worktrees ~/.kugetsu/logs
|
||||
cat > ~/.kugetsu/index.json << INDEX
|
||||
mkdir -p $TEST_KUGETSU_DIR/sessions $TEST_KUGETSU_DIR/worktrees $TEST_KUGETSU_DIR/logs
|
||||
cat > $TEST_KUGETSU_DIR/index.json << INDEX
|
||||
{
|
||||
"base": "ses_test_base_123",
|
||||
"pm_agent": "ses_test_pm_456",
|
||||
"issues": {}
|
||||
}
|
||||
INDEX
|
||||
echo '{"type": "base", "opencode_session_id": "ses_test_base_123", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}' > ~/.kugetsu/sessions/base.json
|
||||
echo '{"type": "pm_agent", "opencode_session_id": "ses_test_pm_456", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}' > ~/.kugetsu/sessions/pm-agent.json
|
||||
echo '{"type": "base", "opencode_session_id": "ses_test_base_123", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}' > $TEST_KUGETSU_DIR/sessions/base.json
|
||||
echo '{"type": "pm_agent", "opencode_session_id": "ses_test_pm_456", "created_at": "2026-03-29T18:00:00+02:00", "state": "idle"}' > $TEST_KUGETSU_DIR/sessions/pm-agent.json
|
||||
}
|
||||
|
||||
# Test C1: Agent count file is initialized to 0
|
||||
echo "--- Test: agent count file initialized ---"
|
||||
test_cleanup
|
||||
mkdir -p ~/.kugetsu/sessions ~/.kugetsu/worktrees
|
||||
mkdir -p $TEST_KUGETSU_DIR/sessions $TEST_KUGETSU_DIR/worktrees
|
||||
$KUGETSU list > /dev/null 2>&1 || true
|
||||
if [ -f ~/.kugetsu/.agent_count ]; then
|
||||
COUNT=$(cat ~/.kugetsu/.agent_count)
|
||||
if [ -f $TEST_KUGETSU_DIR/.agent_count ]; then
|
||||
COUNT=$(cat $TEST_KUGETSU_DIR/.agent_count)
|
||||
if [ "$COUNT" = "0" ]; then
|
||||
pass "agent count file initialized to 0"
|
||||
else
|
||||
@@ -795,10 +797,10 @@ test_cleanup
|
||||
setup_mock_sessions
|
||||
|
||||
# Initialize count to 0
|
||||
echo 0 > ~/.kugetsu/.agent_count
|
||||
echo 0 > $TEST_KUGETSU_DIR/.agent_count
|
||||
|
||||
# Verify initial state
|
||||
INITIAL=$(cat ~/.kugetsu/.agent_count)
|
||||
INITIAL=$(cat $TEST_KUGETSU_DIR/.agent_count)
|
||||
if [ "$INITIAL" = "0" ]; then
|
||||
pass "agent count starts at 0"
|
||||
else
|
||||
@@ -809,7 +811,7 @@ fi
|
||||
$KUGETSU list > /dev/null 2>&1
|
||||
|
||||
# Verify count is still 0 (no slot leak)
|
||||
AFTER=$(cat ~/.kugetsu/.agent_count)
|
||||
AFTER=$(cat $TEST_KUGETSU_DIR/.agent_count)
|
||||
if [ "$AFTER" = "0" ]; then
|
||||
pass "agent count stays 0 after list (no leak)"
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user