Skip to content

Commit 5f9410f

Browse files
committed
Refactor: use compiled perry binary for container session discovery
- 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 f2f7f71 commit 5f9410f

13 files changed

Lines changed: 430 additions & 228 deletions

File tree

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+
`perry-worker` is a CLI tool that runs inside containers (and can also run on the host) to handle container-specific operations. It's designed to be updated independently of the Docker image, avoiding the need to rebuild images or recreate containers for bug fixes.
827+
828+
### Architecture
829+
830+
```
831+
packages/
832+
├── opencode-sessions/ # Shared library for OpenCode session reading
833+
│ └── src/index.ts # listOpencodeSessions(), getOpencodeSessionMessages()
834+
835+
├── perry-worker/ # CLI that runs inside containers
836+
│ └── src/
837+
│ ├── index.ts # Entry point
838+
│ ├── cli.ts # Command router
839+
│ └── commands/
840+
│ └── sessions.ts # Uses @gricha/opencode-sessions
841+
```
842+
843+
### Key Principle: No Bifurcation
844+
845+
The same code (`@gricha/opencode-sessions`) handles OpenCode session reading everywhere:
846+
- **Host**: Imported directly in router.ts
847+
- **Container**: Called via `perry-worker sessions list/messages`
848+
849+
This eliminates duplicate implementations and ensures consistent behavior.
850+
851+
### Installation
852+
853+
perry-worker is installed via npm during container initialization (not baked into Docker image):
854+
855+
```typescript
856+
// perry/internal/src/lib/devtools.ts
857+
const ensurePerryWorker = async () => {
858+
const installResult = await runCommand("npm", ["install", "-g", "@gricha/perry-worker"]);
859+
// ...
860+
}
861+
```
862+
863+
### Usage
864+
865+
```bash
866+
# List all OpenCode sessions
867+
perry-worker sessions list
868+
869+
# Get messages for a session
870+
perry-worker sessions messages <session_id>
871+
```
872+
873+
### Why This Pattern?
874+
875+
1. **Independent Updates**: Fix bugs in session reading without rebuilding Docker images
876+
2. **Single Source of Truth**: Same `@gricha/opencode-sessions` code used everywhere
877+
3. **Smaller Docker Images**: Don't bake in tools that change frequently
878+
4. **Testing**: Can test the shared library independently
879+
880+
### Adding New Commands
881+
882+
To add new functionality to perry-worker:
883+
884+
1. Add the core logic to a shared package (like `@gricha/opencode-sessions`)
885+
2. Create a command in `packages/perry-worker/src/commands/`
886+
3. Register the command in `packages/perry-worker/src/cli.ts`
887+
4. Import the shared package in the main perry codebase where needed
888+
889+
---
890+
822891
## Appendix: Port Selection
823892

824893
Default port: **7391**

bun.lock

Lines changed: 27 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)