Compare commits

..
Author SHA1 Message Date
Antigravity AI 5155348545 fix(ci): setup .env from example in local dev CI test 2026-05-15 11:12:30 +12:00
Antigravity AI 5fbfe8954b fix: update openid-client api usage to v6 and resolve db import
- Migrated processAuthCodeResponse to authorizationCodeGrant
- Updated db import to export prisma client correctly
- Updated unit tests with v6 openid-client mocks
- Bumped version to 2.13.1
2026-05-15 11:02:40 +12:00
Antigravity AI 1194be67c4 feat(dx): implement worktree DB orchestration and graceful dev teardown
- feat(dx): [Minor] Implement deterministic port binding per git worktree
  to mitigate Docker port conflicts during parallel development.
- feat(dx): [Minor] Implement graceful development environment teardown
  using a custom node orchestrator (scripts/dev.mjs) replacing CLI concurrently,
  enabling cross-platform tree-kill and optional DB stopping on exit.
- build(deps): Add tree-kill for process termination.
- fix(dx): Prevent rm -rf from orphaning Docker volumes by adding
  a db:prune script and tying volume cleanup to git-wt remove.
2026-05-15 10:55:37 +12:00
Antigravity AI b29133fca0 feat: add watcher script for automatic local plugin synchronization 2026-05-14 22:23:52 +12:00
Antigravity AI ac0092c8a8 ci: add workflow to test local development environment startup and predev tasks 2026-05-14 22:19:44 +12:00
Antigravity AI f0daefd53d chore: map postgres port to host for external access in docker-compose 2026-05-14 22:09:35 +12:00
14 changed files with 336 additions and 22 deletions

No files matched your search

+13 -4
View File
@@ -24,20 +24,29 @@ If you create a worktree inside a subdirectory of the current repo (e.g., `.agen
Do not use raw `git worktree add`. Always use the `git-wt` alias provided by Worktrunk. It is pre-configured via `.config/wt.toml` to:
- Automatically place the worktree in the correct sibling path.
- Automatically run `pnpm install`.
- Automatically copy `.env` and `.env.local` files so the environment works immediately.
- Automatically copy `.env` files so the environment works immediately.
> [!IMPORTANT]
> **For AI Agents:** You MUST append the `--yes` flag when creating worktrees to bypass the interactive approval prompt for these hooks, otherwise the command will fail in your non-interactive shell.
### 3. Database Lifecycle & Port Assignment
Each worktree gets a deterministic PostgreSQL port assigned automatically based on its directory hash (to avoid collisions between multiple running worktrees).
> [!CAUTION]
> **Orphaned Volumes:** You MUST use `git-wt remove` to delete worktrees. This triggers a `pre-remove` hook that cleanly tears down the Docker database. If you manually delete the folder (`rm -rf`), the Docker volume is orphaned. If this happens, run `pnpm run db:prune` in the main repository to garbage collect orphaned database volumes.
## Quick Reference
| Action | Command | Details |
| :--- | :--- | :--- |
| **Create Worktree** | `git-wt switch --create <branch>` | Provisions the sibling directory, branch, env files, and dependencies. |
| **Create Worktree** | `git-wt switch --create <branch> --yes` | Provisions the sibling directory, branch, env files, and dependencies. |
| **Delete Worktree** | `git-wt remove` | Deletes the worktree and the associated branch cleanly. Run from within the worktree. |
## Implementation Steps
1. Run the create command from the main repository:
1. Run the create command from the main repository (always include `--yes`):
```bash
git-wt switch --create feature/my-new-idea
git-wt switch --create feature/my-new-idea --yes
```
2. **CRITICAL:** Change your working directory to the newly created worktree before editing files. Worktrunk creates it as a sibling.
```bash
+4 -1
View File
@@ -2,4 +2,7 @@
install_deps = "pnpm install"
[[pre-start]]
copy_env = """powershell -Command "if (Test-Path '../worldwideview/.env') { Copy-Item '../worldwideview/.env' '.env' -Force }; if (Test-Path '../worldwideview/.env.local') { Copy-Item '../worldwideview/.env.local' '.env.local' -Force }""""
copy_env = """powershell -Command "if (Test-Path '../worldwideview/.env') { Copy-Item '../worldwideview/.env' '.env' -Force }""""
[[pre-remove]]
docker_clean = "docker compose down -v"
+5
View File
@@ -55,6 +55,11 @@ NEXT_PUBLIC_DEMO_DEFAULT_PLUGINS=
# Edition: "local" | "cloud" | "demo"
NEXT_PUBLIC_WWV_EDITION=local
# Optional Database Teardown
# If set to true, terminating 'pnpm run dev' (Ctrl+C) will automatically
# stop the local database container (freeing up port 5432).
WWV_TEARDOWN_DB_ON_EXIT=false
# Demo Admin Secret (demo edition only)
# Allows operator plugin management on the demo instance via x-wwv-admin-secret header.
# Generate any random string. Only set this on the demo deployment.
+60
View File
@@ -0,0 +1,60 @@
name: Test Local Dev Environment
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test-dev:
name: Test Local Dev Startup
runs-on: ubuntu-latest
env:
NEXT_PUBLIC_WWV_EDITION: local
AUTH_SECRET: ci-placeholder-secret-not-used-in-tests
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v6
with:
node-version: 22
cache: pnpm
- name: Create coolify docker network
# Needed because docker-compose.yml references external coolify network
run: docker network create coolify || true
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Setup environment
run: cp .env.example .env
- name: Test predev setup
# Tests that Docker DB boots, port mappings work, Prisma pushes schema, and plugins sync
run: pnpm run predev
- name: Test dev server startup
run: |
# Start the dev server in the background
pnpm run dev &
DEV_PID=$!
echo "Waiting for Next.js to respond on port 3000..."
# Wait up to 120 seconds for the dev server to return a response
timeout 120 bash -c 'until curl -s http://localhost:3000 > /dev/null; do sleep 5; done'
# Check if the process is still running
if ! kill -0 $DEV_PID 2>/dev/null; then
echo "Dev server crashed or failed to start within timeout!"
exit 1
fi
echo "Dev server started successfully!"
# Graceful shutdown
kill $DEV_PID
+3 -1
View File
@@ -208,7 +208,9 @@ Refer to these skill documents for specialized tasks:
- **Workflow**: You **MUST** use the `/commit` workflow before every git commit to ensure proper semantic versioning bumps.
- **Required Checks**: Ensure `pnpm test` and `pnpm build` complete successfully before proposing a merge.
- **Review Process**: Use `/pr-review` to conduct a comprehensive multi-role review on any pull request.
- **Git Worktrees**: We use [Worktrunk](https://github.com/max-sixty/worktrunk) to manage parallel branches and avoid relative path breakage in the monorepo. Use `git-wt switch --create <branch>` to provision isolated sibling worktrees automatically configured with `.env` files and `pnpm install`. Use `git-wt remove` to clean up.
- **Git Worktrees & Database Lifecycle**: We use [Worktrunk](https://github.com/max-sixty/worktrunk) to manage parallel branches and avoid relative path breakage. `git-wt switch --create <branch>` provisions isolated sibling worktrees configured with `.env` files and `pnpm install`. The database port is assigned deterministically based on the directory hash.
> [!CAUTION]
> You MUST use `git-wt remove` to clean up worktrees. Manually deleting a worktree folder (`rm -rf`) will orphan the associated PostgreSQL Docker volume. If this happens, you must manually run `pnpm run db:prune` in the main repository to reclaim disk space.
---
+2 -2
View File
@@ -42,8 +42,8 @@ services:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: worldwideview
expose:
- "5432"
ports:
- "${WWV_DB_PORT:-5432}:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
+4 -2
View File
@@ -1,6 +1,6 @@
{
"name": "worldwideview",
"version": "2.11.0",
"version": "2.13.1",
"private": true,
"type": "module",
"packageManager": "[email protected]",
@@ -8,7 +8,7 @@
"setup": "node scripts/setup.mjs",
"test-worktree": "node scripts/test-worktree.mjs",
"clean": "node -e \"const fs=require('fs');fs.rmSync('.next',{recursive:true,force:true})\"",
"dev": "concurrently \"pnpm run dev:plugins\" \"next dev\"",
"dev": "node scripts/dev.mjs",
"dev:all": "concurrently -n \"dev,backends\" -c \"magenta,blue\" \"pnpm dev\" \"pnpm dev:backends\"",
"dev:backends": "docker compose --profile engine up wwv-data-engine wwv-redis",
"dev:plugins": "node scripts/watch-local-plugins.mjs",
@@ -21,6 +21,7 @@
"test:mutate": "stryker run",
"lint": "eslint .",
"db:reset": "npx prisma migrate reset --force",
"db:prune": "node scripts/db-prune.mjs",
"generate": "prisma generate"
},
"dependencies": {
@@ -78,6 +79,7 @@
"fast-check": "^4.8.0",
"jsdom": "^28.1.0",
"rollup-plugin-external-globals": "^0.13.0",
"tree-kill": "^1.2.2",
"tsx": "^4.21.0",
"typescript": "^5",
"vite": "^8.0.12",
+3
View File
@@ -172,6 +172,9 @@ importers:
rollup-plugin-external-globals:
specifier: ^0.13.0
version: 0.13.0([email protected])
tree-kill:
specifier: ^1.2.2
version: 1.2.2
tsx:
specifier: ^4.21.0
version: 4.21.0
+27
View File
@@ -9,6 +9,7 @@
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
// Load environment variables manually since dotenv might not be installed globally
/**
@@ -46,6 +47,32 @@ if (skipLocalDb) {
process.exit(0);
}
// Deterministic Port Assignment
const cwd = process.cwd();
const folderName = path.basename(cwd);
let port = 5432; // Default for main repo
if (folderName !== 'worldwideview') {
const hash = crypto.createHash('sha256').update(cwd).digest('hex');
const portOffset = parseInt(hash.substring(0, 4), 16) % 1000;
port = 5433 + portOffset;
}
process.env.WWV_DB_PORT = port.toString();
console.log(`🔌 Assigned deterministic database port: ${port}`);
// Rewrite DATABASE_URL in .env if it exists
const envPath = path.resolve(cwd, '.env');
if (fs.existsSync(envPath)) {
let envContent = fs.readFileSync(envPath, 'utf8');
const urlRegex = /(DATABASE_URL\s*=\s*["']?postgresql:\/\/[^:]+:[^@]+@localhost:)(\d+)(\/.*)/;
if (urlRegex.test(envContent)) {
envContent = envContent.replace(urlRegex, `$1${port}$3`);
fs.writeFileSync(envPath, envContent, 'utf8');
}
}
console.log('🚀 Checking local PostgreSQL database...');
try {
+72
View File
@@ -0,0 +1,72 @@
/**
* @file db-prune.mjs
* @description Garbage collects orphaned Docker volumes from deleted git worktrees.
* Resolves the issue where manually deleting a worktree folder (rm -rf) leaves
* behind persistent database and data volumes.
*/
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
console.log('🧹 Starting Garbage Collection for orphaned WorldWideView Docker volumes...');
try {
// Get all docker volumes
const volumesOutput = execSync('docker volume ls --format "{{.Name}}"').toString();
const volumes = volumesOutput.split('\n').map(v => v.trim()).filter(Boolean);
// Identify WorldWideView volumes
const wwvVolumes = volumes.filter(v =>
(v.startsWith('worldwideview') && v.endsWith('_postgres-data')) ||
(v.startsWith('worldwideview') && v.endsWith('_wwv-data'))
);
if (wwvVolumes.length === 0) {
console.log('✅ No WorldWideView volumes found.');
process.exit(0);
}
// Get active worktree folders in the parent directory
const parentDir = path.resolve(process.cwd(), '..');
const folders = fs.readdirSync(parentDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory() && dirent.name.startsWith('worldwideview'))
.map(dirent => dirent.name);
// Compute their expected Docker project names (lowercase, alphanumeric only)
// Docker Compose defaults to this sanitization for project names based on directories
const activeProjectNames = new Set(
folders.map(folder => folder.replace(/[^a-zA-Z0-9]/g, '').toLowerCase())
);
let orphanedCount = 0;
for (const volume of wwvVolumes) {
let projectName = '';
if (volume.endsWith('_postgres-data')) {
projectName = volume.slice(0, -'_postgres-data'.length);
} else if (volume.endsWith('_wwv-data')) {
projectName = volume.slice(0, -'_wwv-data'.length);
}
if (!activeProjectNames.has(projectName)) {
console.log(`🗑️ Deleting orphaned volume: ${volume}`);
try {
execSync(`docker volume rm ${volume}`, { stdio: 'inherit' });
orphanedCount++;
} catch (rmError) {
console.warn(`⚠️ Could not remove volume ${volume}. It may still be in use.`);
}
}
}
if (orphanedCount === 0) {
console.log('✅ All existing volumes belong to active worktrees. Nothing to prune.');
} else {
console.log(`✅ Successfully pruned ${orphanedCount} orphaned volume(s).`);
}
} catch (error) {
console.error('❌ Failed to prune volumes:', error.message);
process.exit(1);
}
+86
View File
@@ -0,0 +1,86 @@
/**
* @file dev.mjs
* @description Orchestrator for the local development environment.
* Replaces direct usage of 'concurrently' via CLI to allow for optional,
* cross-platform graceful teardown of the Docker database on exit.
* @module scripts
*/
import concurrently from 'concurrently';
import kill from 'tree-kill';
import { execSync } from 'child_process';
import fs from 'fs';
import process from 'process';
// Manually parse .env to avoid external dependencies early in the boot process
const loadEnv = (file) => {
try {
if (fs.existsSync(file)) {
const content = fs.readFileSync(file, 'utf8');
content.split('\n').forEach(line => {
const match = line.match(/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/);
if (match) {
const key = match[1];
let value = match[2] || '';
if (value.startsWith('"') && value.endsWith('"')) value = value.slice(1, -1);
if (value.startsWith("'") && value.endsWith("'")) value = value.slice(1, -1);
process.env[key] = value;
}
});
}
} catch (e) {
// Ignore read errors
}
};
loadEnv('.env');
const teardownDbOnExit = process.env.WWV_TEARDOWN_DB_ON_EXIT === 'true' || process.env.WWV_TEARDOWN_DB_ON_EXIT === '1';
const { result, commands } = concurrently(
[
{ command: 'pnpm run dev:plugins', name: 'plugins', prefixColor: 'magenta' },
{ command: 'next dev', name: 'next', prefixColor: 'blue' }
],
{
prefix: 'name',
killOthers: ['failure', 'success']
}
);
let isShuttingDown = false;
const shutdown = () => {
if (isShuttingDown) return;
isShuttingDown = true;
console.log('\n🛑 Shutting down dev servers...');
// Forcefully kill the process tree of each command cross-platform
commands.forEach(cmd => {
if (cmd.pid) {
kill(cmd.pid, 'SIGKILL');
}
});
if (teardownDbOnExit) {
console.log('📦 Tearing down local PostgreSQL database...');
try {
execSync('docker compose stop db', { stdio: 'inherit' });
console.log('✅ Local database stopped.');
} catch (e) {
console.error('⚠️ Failed to stop database container.');
}
}
process.exit(0);
};
// Listen for termination signals
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
result.then(
() => process.exit(0),
() => process.exit(1)
);
+35 -6
View File
@@ -13,29 +13,58 @@ if (!fs.existsSync(LOCAL_PLUGINS_DIR)) {
}
let debounceTimeout = null;
let isSyncing = false;
let pendingSync = false;
function handleFileChange(eventType, filename) {
if (filename && (filename.includes("dist") || filename.includes("node_modules") || filename.endsWith(".map"))) return; // ignore build artifacts
if (filename && (
filename.includes("dist") ||
filename.includes("node_modules") ||
filename.endsWith(".map") ||
filename.includes(".git")
)) return; // ignore build artifacts and git internals
if (debounceTimeout) {
clearTimeout(debounceTimeout);
}
debounceTimeout = setTimeout(async () => {
console.log(`\n[watch] Change detected in local plugins (file: ${filename}). Syncing...`);
try {
await syncAll();
} catch (err) {
console.error(`[watch] Sync failed:`, err);
if (isSyncing) {
pendingSync = true;
return;
}
await runSync(filename);
}, 500);
}
async function runSync(filename) {
isSyncing = true;
console.log(`\n[watch] Change detected in local plugins (file: ${filename}). Syncing...`);
try {
await syncAll();
} catch (err) {
console.error(`[watch] Sync failed:`, err);
} finally {
isSyncing = false;
if (pendingSync) {
pendingSync = false;
await runSync("pending changes");
}
}
}
// Initial sync
console.log(`[watch] Starting initial sync...`);
isSyncing = true;
syncAll().then(() => {
isSyncing = false;
if (pendingSync) {
pendingSync = false;
runSync("pending changes").catch(console.error);
}
console.log(`[watch] Watching ${LOCAL_PLUGINS_DIR} for changes...`);
fs.watch(LOCAL_PLUGINS_DIR, { recursive: true }, handleFileChange);
}).catch(err => {
isSyncing = false;
console.error(`[watch] Initial sync failed:`, err);
});
+4 -5
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import * as client from "openid-client";
import { encryptCredential } from "@/lib/auth/encryption";
import db from "@/lib/db";
import { prisma as db } from "@/lib/db";
export async function GET(req: NextRequest) {
const isHttps = req.nextUrl.protocol === "https:";
const cookiePrefix = isHttps ? "__Host-" : "";
@@ -25,12 +25,11 @@ export async function GET(req: NextRequest) {
authorization_endpoint: new URL("/oauth/authorize", issuer).toString(),
token_endpoint: new URL("/api/tickets/exchange", issuer).toString(),
};
const config = { client_id: "local-app" };
const config = { server, clientId: "local-app" };
try {
const tokens = await client.processAuthCodeResponse(
server,
config,
const tokens = await client.authorizationCodeGrant(
config as any,
new URL(req.url),
{ expectedState: stateCookie, pkceCodeVerifier: verifierCookie }
);
+18 -1
View File
@@ -10,7 +10,24 @@ vi.mock("openid-client", () => ({
discoveryRequest: vi.fn(),
processAuthorizationResponse: vi.fn(),
validateAuthResponse: vi.fn(),
processAuthCodeResponse: vi.fn()
authorizationCodeGrant: vi.fn().mockResolvedValue({ access_token: "mock-token" })
}));
vi.mock("@/lib/auth/encryption", () => ({
encryptCredential: vi.fn().mockResolvedValue({
version: 1,
salt: "mock-salt",
nonce: "mock-nonce",
ciphertext: "mock-ciphertext"
})
}));
vi.mock("@/lib/db", () => ({
prisma: {
marketplaceCredential: {
upsert: vi.fn().mockResolvedValue({})
}
}
}));
describe("PKCE Flow", () => {