Skip to content

Agent Mesh

feat(code-reviewer-pool): make lease enforcement the default

Tomas Tormo312ce0b10bc7
diff --git a/development/examples/code-reviewer-pool/README.md b/development/examples/code-reviewer-pool/README.md
index f7e0b90b..8dbc1395 100644
--- a/development/examples/code-reviewer-pool/README.md
+++ b/development/examples/code-reviewer-pool/README.md
@@ -110,12 +110,14 @@ kubectl --context kind-sam-kind -n sam-kind scale deploy/node-d --replicas=1
| `SAM_DISCOVERY_MS` | `3000` | manager |
| `SAM_LEASE_MS` | `60000` | manager |
| `SAM_GRACE_MISSES` | `2` | manager |
-| `SAM_POOL_SECRET` | unset (enforcement off) | manager + reviewer |
+| `SAM_POOL_SECRET` | `sam-dev-pool-secret` (enforcement always on) | manager + reviewer |
-## Lease enforcement (optional)
+## Lease enforcement
-By default any peer can call a worker's `review_code` directly. Set the same
-`SAM_POOL_SECRET` on the manager and every reviewer to require a lease: the
-manager signs a token on `acquire_worker`, the reviewer verifies it offline and
-returns `NO_LEASE` for any call without a valid, unexpired one. Unset = open, as
-before. A secret set on only one side (or a mismatch between them) fails closed: every `review_code` call returns `NO_LEASE`.
+Every reviewer requires a valid lease: the manager signs a token on
+`acquire_worker`, and the reviewer verifies it offline and returns `NO_LEASE`
+for any `review_code` call without a valid, unexpired one. Both sides share a
+hardcoded dev secret (`sam-dev-pool-secret`) so it works out of the box; override
+it by setting the same `SAM_POOL_SECRET` on the manager and every reviewer. A
+mismatch (or setting it on only one side) fails closed: every call returns
+`NO_LEASE`.
diff --git a/development/examples/code-reviewer-pool/manager/server.mjs b/development/examples/code-reviewer-pool/manager/server.mjs
index 473be5c1..2218c03a 100644
--- a/development/examples/code-reviewer-pool/manager/server.mjs
+++ b/development/examples/code-reviewer-pool/manager/server.mjs
@@ -13,7 +13,7 @@ const POOL_SERVICE = process.env.SAM_POOL_SERVICE ?? "code-reviewer";
const DISCOVERY_MS = Number(process.env.SAM_DISCOVERY_MS ?? 3000);
const LEASE_MS = Number(process.env.SAM_LEASE_MS ?? 60000);
const GRACE_MISSES = Number(process.env.SAM_GRACE_MISSES ?? 2);
-const POOL_SECRET = process.env.SAM_POOL_SECRET ?? ""; // set → hand out signed lease tokens
+const POOL_SECRET = process.env.SAM_POOL_SECRET ?? "sam-dev-pool-secret"; // shared dev secret; enforcement always on
const ACQUIRE_POLL_MS = 200;
// roster: peer_id -> { tool, leasedUntil, leaseId, missCount }.
@@ -61,7 +61,7 @@ function leaseFree() {
entry.leasedUntil = now() + LEASE_MS;
entry.leaseId = String(++leaseSeq);
const lease = { peer_id: peer, tool: entry.tool, lease_id: entry.leaseId };
- if (POOL_SECRET) lease.token = mintToken(POOL_SECRET, peer, entry.leasedUntil);
+ lease.token = mintToken(POOL_SECRET, peer, entry.leasedUntil);
return lease;
}
}
@@ -74,8 +74,8 @@ server.registerTool(
"acquire_worker",
{
description:
- `Acquire (lease) a free worker from the '${POOL_SERVICE}' pool. Returns {peer_id, tool, lease_id, token?}: ` +
- "call the tool via call_remote_tool — forward token in the tool arguments when present (the pool may require it) — then release_worker with the same peer_id and lease_id. " +
+ `Acquire (lease) a free worker from the '${POOL_SERVICE}' pool. Returns {peer_id, tool, lease_id, token}: ` +
+ "call the tool via call_remote_tool — forward token in the tool arguments (the pool requires it) — then release_worker with the same peer_id and lease_id. " +
"Blocks up to timeout_secs if all workers are busy; returns {available:false} if none free by then.",
inputSchema: { timeout_secs: z.number().optional().describe("Max seconds to wait for a free worker (default 10).") },
},
diff --git a/development/examples/code-reviewer-pool/reviewer/review_server.mjs b/development/examples/code-reviewer-pool/reviewer/review_server.mjs
index 5ce494df..cb24dd9c 100644
--- a/development/examples/code-reviewer-pool/reviewer/review_server.mjs
+++ b/development/examples/code-reviewer-pool/reviewer/review_server.mjs
@@ -14,7 +14,7 @@ const REVIEW_PROMPT =
// Single-flight backstop: reject a concurrent call so the one-at-a-time pool
// invariant holds even if a manager lease race hands this worker out twice.
const POOL_BUSY = "POOL_BUSY";
-const POOL_SECRET = process.env.SAM_POOL_SECRET ?? ""; // set → require a valid lease token
+const POOL_SECRET = process.env.SAM_POOL_SECRET ?? "sam-dev-pool-secret"; // shared dev secret; enforcement always on
const NO_LEASE = "NO_LEASE";
let busy = false;
@@ -51,11 +51,11 @@ function newServer() {
"grouped by severity (bug / risk / style).",
inputSchema: {
code: z.string().describe("The code snippet to review (any language)."),
- token: z.string().optional().describe("Lease token from acquire_worker (required when the pool enforces leases)."),
+ token: z.string().optional().describe("Lease token from acquire_worker (required)."),
},
},
async ({ code, token }) => {
- if (POOL_SECRET && !verifyToken(POOL_SECRET, token, Date.now()).valid) {
+ if (!verifyToken(POOL_SECRET, token, Date.now()).valid) {
return { content: [{ type: "text", text: NO_LEASE }], isError: true };
}
if (busy) return { content: [{ type: "text", text: POOL_BUSY }], isError: true };