Skip to content

Commit e48fd27

Browse files
authored
Refactor: use compiled perry binary for container session discovery (#13)
- Add perry worker subcommand with sessions list/messages commands - Compile perry to standalone binary during build (bun build --compile) - Copy perry binary to containers during sync (/usr/local/bin/perry) - Remove shell script approach (perry-session-reader.sh) - Share opencode-storage.ts between host and worker modes - Update AGENTS.md with manual testing instructions for port 7391 - Fix OpenCode sessions not showing up in web UI This replaces the npm-based perry-worker package with a compiled binary that gets synced to containers, eliminating the need to publish a separate package and ensuring the container always has the latest code.
1 parent 193b07d commit e48fd27

File tree

13 files changed

+410
-228
lines changed

13 files changed

+410
-228
lines changed

AGENTS.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ Perry creates isolated Docker-in-Docker development environments. Distributed ar
9191

9292
If modifying Dockerfile/init scripts, run `perry build` first.
9393

94+
### Manual Agent Testing
95+
96+
When manually testing agent changes, use a dedicated test port to avoid conflicts with any running production agent:
97+
98+
```bash
99+
# Kill any existing agents and start test agent on port 7391
100+
pkill -f "perry agent" 2>/dev/null
101+
perry agent run --port 7391 &
102+
103+
# Configure CLI to use test agent
104+
perry config worker localhost:7391
105+
106+
# Test your changes
107+
perry list
108+
perry sync <workspace-name>
109+
110+
# Verify in container (example: testing perry worker binary)
111+
docker exec -u workspace workspace-<name> perry --version
112+
docker exec -u workspace workspace-<name> perry worker sessions list
113+
114+
# Test API directly
115+
curl -s -X POST "http://localhost:7391/rpc/sessions/list" \
116+
-H "Content-Type: application/json" \
117+
-d '{"json":{"workspaceName":"<name>"}}'
118+
```
119+
120+
**Important**: Always kill the test agent when done, or it will conflict with automated tests.
121+
94122
### UI Testing
95123

96124
**Critical**: UI (Web, mobile) MUST have e2e tests. Unit/integration tests miss rendering bugs, event binding issues, and framework regressions.

DESIGN.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,75 @@ Currently none (Tailscale-trusted). Future options if needed:
819819

820820
---
821821

822+
## Appendix: perry worker Pattern
823+
824+
### Overview
825+
826+
The `perry worker` subcommand runs inside containers to handle container-specific operations like session discovery. It's designed to be updated independently of the Docker image - the compiled binary is copied during `perry sync`, avoiding the need to rebuild images for bug fixes.
827+
828+
### Architecture
829+
830+
```
831+
src/
832+
├── index.ts # Main CLI with 'worker' subcommand
833+
├── sessions/agents/
834+
│ ├── opencode-storage.ts # Shared session reading logic
835+
│ └── opencode.ts # Container provider (calls perry worker)
836+
└── workspace/
837+
└── manager.ts # copyPerryWorker() syncs binary to container
838+
839+
dist/
840+
├── index.js # Main perry CLI
841+
└── perry-worker # Compiled standalone binary (bun build --compile)
842+
```
843+
844+
### Key Principle: No Bifurcation
845+
846+
The same code (`opencode-storage.ts`) handles OpenCode session reading everywhere:
847+
- **Host**: Imported directly in `router.ts`
848+
- **Container**: Called via `perry worker sessions list/messages`
849+
850+
This eliminates duplicate implementations and ensures consistent behavior.
851+
852+
### Build & Sync
853+
854+
The worker binary is compiled during build and copied during sync:
855+
856+
```bash
857+
# Build (in package.json scripts)
858+
bun build src/index.ts --compile --outfile dist/perry-worker --target=bun
859+
860+
# Sync copies binary to container (in WorkspaceManager.copyPerryWorker)
861+
docker cp dist/perry-worker container:/usr/local/bin/perry
862+
```
863+
864+
### Usage
865+
866+
```bash
867+
# List all OpenCode sessions (JSON output)
868+
perry worker sessions list
869+
870+
# Get messages for a session (JSON output)
871+
perry worker sessions messages <session_id>
872+
```
873+
874+
### Why This Pattern?
875+
876+
1. **Independent Updates**: Fix bugs in session reading, rebuild, sync - no Docker image rebuild needed
877+
2. **Single Source of Truth**: Same TypeScript code used on host and in container
878+
3. **Instant Propagation**: `perry sync` updates all workspaces immediately
879+
4. **No External Dependencies**: Self-contained binary, no npm install in container
880+
881+
### Adding New Commands
882+
883+
To add new functionality:
884+
885+
1. Add shared logic to `src/sessions/agents/` or appropriate module
886+
2. Add subcommand to `perry worker` in `src/index.ts`
887+
3. Import shared module in both host code and worker command
888+
889+
---
890+
822891
## Appendix: Port Selection
823892

824893
Default port: **7391**

bun.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"dist"
1111
],
1212
"scripts": {
13-
"build": "rm -rf ./dist && bun run build:ts && bun run build:web && bun link",
13+
"build": "rm -rf ./dist && bun run build:ts && bun run build:worker && bun run build:web && bun link",
1414
"build:ts": "tsc && chmod +x dist/index.js",
15+
"build:worker": "bun build src/index.ts --compile --outfile dist/perry-worker --target=bun",
1516
"build:web": "cp src/shared/client-types.ts web/src/lib/types.ts && cd web && bun run build && cp -r dist ../dist/agent/web",
1617
"test": "vitest run",
1718
"test:web": "playwright test",

perry/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ RUN cd /opt/workspace/internal && bun install --production
193193
COPY internal /opt/workspace/internal
194194
RUN chmod +x /opt/workspace/scripts/dockerd-entrypoint.sh \
195195
&& install -m 0755 /opt/workspace/scripts/dockerd-entrypoint.sh /usr/local/bin/dockerd-entrypoint.sh \
196-
&& install -m 0755 /opt/workspace/scripts/perry-session-reader.sh /usr/local/bin/perry-session-reader \
197196
&& chmod +x /opt/workspace/internal/src/index.ts \
198197
&& printf '#!/bin/sh\nexec bun /opt/workspace/internal/src/index.ts "$@"\n' > /usr/local/bin/workspace-internal \
199198
&& chmod +x /usr/local/bin/workspace-internal

perry/scripts/perry-session-reader.sh

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)