Skip to content

espennilsen/pi / extensions/pi-a2a

pi-a2a

Flagged

FileKindSize
AGENTS.mdmarkdown12.0 KB
CHANGELOG.mdmarkdown1.3 KB
README.mdmarkdown11.0 KB
SKILL.mdskill md9.7 KB
agent-card-auth.test.tsscript1.7 KB
agent-card.tsscript6.1 KB
agent-executor.tsscript38.7 KB
auth-integration.test.tsscript4.6 KB
auth-selector.test.tsscript4.0 KB
auth-selector.tsscript2.6 KB
auth-types.tsscript3.7 KB
client.test.tsscript3.1 KB
client.tsscript14.9 KB
config-auth.test.tsscript4.0 KB
config.tsscript9.1 KB
cost.test.tsscript1.8 KB
hub.test.tsscript6.2 KB
hub.tsscript39.5 KB
inbound-auth.test.tsscript2.6 KB
inbound-auth.tsscript4.4 KB
index.tsscript136.5 KB
logger.tsscript1.5 KB
long-running-task-store.tsscript6.3 KB
outbound-auth.test.tsscript1.5 KB
outbound-auth.tsscript1.6 KB
package-lock.jsonmanifest149.9 KB
package.jsonmanifest1.5 KB
peer-metadata.test.tsscript3.7 KB
peer-metadata.tsscript4.6 KB
port-finder.tsscript742 B
server.tsscript12.6 KB
static-agents.tsscript5.8 KB
supervisor.test.tsscript6.1 KB
supervisor.tsscript8.4 KB
task-store.tsscript12.5 KB
token-provider.test.tsscript5.3 KB
token-provider.tsscript7.7 KB
tool-telemetry.test.tsscript2.8 KB
tool-telemetry.tsscript1.7 KB
tsconfig.jsonconfig274 B
types.tsscript11.3 KB

hub.test.ts

View on GitHub
/**
 * pi-a2a — Hub RPC tests for task claiming, leases, and telemetry.
 */

import { afterEach, describe, it } from "node:test";
import assert from "node:assert";
import { claimHubTask, heartbeatHubTask, reportTelemetryToHub, HubRpcError } from "./hub.ts";
import type { HubConfig, TelemetrySnapshot } from "./types.ts";

const hubConfig: HubConfig = {
	url: "http://hub.local/api",
	apiKey: "secret",
};

const log = () => {};

let originalFetch: typeof fetch | null = null;

afterEach(() => {
	if (originalFetch) {
		globalThis.fetch = originalFetch;
		originalFetch = null;
	}
});

function mockFetch(handler: (input: unknown, init?: RequestInit) => Promise<Response> | Response): void {
	originalFetch = globalThis.fetch;
	globalThis.fetch = handler as typeof fetch;
}

function rpcResponse(result: unknown): Response {
	return new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, result }), {
		status: 200,
		headers: { "Content-Type": "application/json" },
	});
}

function rpcError(code: number, message: string, data?: unknown): Response {
	return new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, error: { code, message, data } }), {
		status: 200,
		headers: { "Content-Type": "application/json" },
	});
}

describe("claimHubTask", () => {
	it("returns no task when the hub has nothing eligible", async () => {
		mockFetch(async (_input, init) => {
			const body = JSON.parse(init?.body as string) as { method: string; params: Record<string, unknown> };
			assert.strictEqual(body.method, "tasks.claim");
			assert.strictEqual(body.params.agentId, "agent-1");
			assert.strictEqual(body.params.instanceId, "instance-1");
			assert.strictEqual(body.params.leaseDurationSeconds, 900);
			return rpcResponse({ task: null, claimed: false });
		});

		const result = await claimHubTask({ agentId: "agent-1", instanceId: "instance-1" }, hubConfig, log);
		assert.deepStrictEqual(result, { task: null, claimed: false });
	});

	it("throws conflict when a specific task cannot be claimed", async () => {
		mockFetch(() => rpcError(-32003, "Task is already claimed"));

		await assert.rejects(
			() => claimHubTask({ agentId: "agent-1", instanceId: "instance-1", taskId: "task-1" }, hubConfig, log),
			(err: unknown) => err instanceof HubRpcError && err.code === -32003,
		);
	});

	it("throws when the hub is unavailable", async () => {
		mockFetch(() => new Response("", { status: 500 }));

		await assert.rejects(
			() => claimHubTask({ agentId: "agent-1", instanceId: "instance-1" }, hubConfig, log),
			(err: unknown) => err instanceof HubRpcError && err.message === "No response from hub",
		);
	});

	it("throws when the hub claims a task without returning a task object", async () => {
		mockFetch(() => rpcResponse({ task: null, claimed: true }));

		await assert.rejects(
			() => claimHubTask({ agentId: "agent-1", instanceId: "instance-1" }, hubConfig, log),
			(err: unknown) => err instanceof HubRpcError && err.message === "Malformed claim response from hub",
		);
	});
});

describe("heartbeatHubTask", () => {
	it("renews a task lease for the current agent instance", async () => {
		mockFetch(async (_input, init) => {
			const body = JSON.parse(init?.body as string) as { method: string; params: Record<string, unknown> };
			assert.strictEqual(body.method, "tasks.heartbeat");
			assert.strictEqual(body.params.agentId, "agent-1");
			assert.strictEqual(body.params.instanceId, "instance-1");
			assert.strictEqual(body.params.taskId, "task-1");
			assert.strictEqual(body.params.leaseDurationSeconds, 1200);
			return rpcResponse({
				task: {
					id: "task-1",
					title: "Implement leases",
					description: null,
					project: "pi-a2a",
					repo: null,
					state: "planning",
					priority: "normal",
					assignedAgentId: "agent-1",
					createdBy: "system",
					externalTaskId: null,
					branch: null,
					prUrl: null,
					prNumber: null,
					reportPath: null,
					blockedReason: null,
					reviewRound: 0,
					maxReviewRounds: 3,
					metadata: {},
					createdAt: "2026-01-01T00:00:00.000Z",
					updatedAt: "2026-01-01T00:01:00.000Z",
					startedAt: null,
					completedAt: null,
					leaseOwnerAgentId: "agent-1",
					leaseOwnerInstanceId: "instance-1",
					leaseExpiresAt: "2026-01-01T00:21:00.000Z",
				},
				renewed: true,
			});
		});

		const result = await heartbeatHubTask({ agentId: "agent-1", instanceId: "instance-1", taskId: "task-1", leaseDurationSeconds: 1200 }, hubConfig, log);
		assert.strictEqual(result.renewed, true);
		assert.strictEqual(result.task.id, "task-1");
		assert.strictEqual(result.task.leaseOwnerInstanceId, "instance-1");
	});

	it("throws conflict when the lease is invalid", async () => {
		mockFetch(() => rpcError(-32003, "Lease expired"));

		await assert.rejects(
			() => heartbeatHubTask({ agentId: "agent-1", instanceId: "instance-1", taskId: "task-1" }, hubConfig, log),
			(err: unknown) => err instanceof HubRpcError && err.code === -32003,
		);
	});

	it("throws when the hub returns an invalid heartbeat payload", async () => {
		mockFetch(() => rpcResponse({ task: null, renewed: true }));

		await assert.rejects(
			() => heartbeatHubTask({ agentId: "agent-1", instanceId: "instance-1", taskId: "task-1" }, hubConfig, log),
			(err: unknown) => err instanceof HubRpcError && err.message === "Malformed heartbeat response from hub",
		);
	});
});

describe("reportTelemetryToHub", () => {
	it("includes the instanceId in the telemetry payload", async () => {
		mockFetch(async (_input, init) => {
			const body = JSON.parse(init?.body as string) as { method: string; params: Record<string, unknown> };
			assert.strictEqual(body.method, "agents.reportTelemetry");
			assert.strictEqual(body.params.agentId, "agent-1");
			assert.strictEqual(body.params.instanceId, "instance-1");
			assert.strictEqual(body.params.queueDepth, 2);
			assert.strictEqual(body.params.activeTasks, 1);
			assert.strictEqual(body.params.maxConcurrent, 3);
			return rpcResponse({ telemetryUpdatedAt: "2026-01-01T00:00:00.000Z" });
		});

		const telemetry: TelemetrySnapshot = {
			queueDepth: 2,
			activeTasks: 1,
			maxConcurrent: 3,
		};

		const result = await reportTelemetryToHub("agent-1", telemetry, hubConfig, log, "instance-1");
		assert.deepStrictEqual(result, { telemetryUpdatedAt: "2026-01-01T00:00:00.000Z" });
	});
});
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall