Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/cli/src/cli/agent-relay-mcp.startup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,25 @@ describe('createAgentRelayMcpServer', () => {
},
]);

// Delegation identity rides on the spawn action so the agent record
// carries it from birth. Without this the only durable identity a
// consumer can find is whatever it can guess from the agent's name, and a
// multi-token project slug is not recoverable from a name at all.
const identityMetadata = {
organization: 'AgentWorkforce',
project: 'chief-delegation-governance',
workstream: 'dispatch-contract',
role: 'worker',
reportsTo: 'chief-delegation-governance-dispatch-lead',
};

const spawnResult = await server.tools.get('spawn')?.handler({
name: 'FleetWorker',
cli: 'codex',
task: 'Implement a fix',
channel: 'general',
target_node: 'node-a',
metadata: identityMetadata,
});
expect(spawnResult.structuredContent.invocation).toEqual({
invocationId: 'inv_1',
Expand All @@ -496,6 +509,7 @@ describe('createAgentRelayMcpServer', () => {
cli: 'codex',
task: 'Implement a fix',
target_node: 'node-a',
metadata: identityMetadata,
channels: ['general'],
},
});
Expand Down
180 changes: 180 additions & 0 deletions packages/cli/src/cli/agent-relay-mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,186 @@ describe('registerAgentWithRebind', () => {
});
});

const IDENTITY = {
organization: 'AgentWorkforce',
project: 'chief-delegation-governance',
workstream: 'dispatch-contract',
role: 'lead',
reportsTo: 'chief-khaliq',
};

const strictSession = () => ({
workspaceKey: 'rk_live_test',
agentToken: 'at_live_existing',
agentName: 'WorkerA',
agents: new Map([['WorkerA', { agentName: 'WorkerA', agentToken: 'at_live_existing' }]]),
});

/**
* A relay that actually stores what it is given, so a test can read the
* record back instead of trusting that the call was made. Asserting only
* "registerOrRotate was called with metadata" would repeat the mistake this
* whole change is about: the parameter being passed is not the field
* landing. `persists: false` models the broken platform.
*/
function fakeRelay({ persists = true }: { persists?: boolean } = {}) {
// The platform writes its own block; a verifier must ignore it.
const records = new Map<string, Record<string, unknown>>([['WorkerA', { fleet: { nodeId: 'node_x' } }]]);
const registerOrRotate = vi.fn(async (input: any) => {
if (persists && input.metadata) {
records.set(input.name, { ...records.get(input.name), ...input.metadata });
}
return { id: 'agent_123', name: input.name, token: 'at_live_rotated', status: 'online' };
});
const list = vi.fn(async () => [...records].map(([name, metadata]) => ({ name, metadata })));
return { agents: { registerOrRotate, list }, records };
}

it('writes supplied metadata through and proves it landed on the record', async () => {
// The short-circuit exists to avoid handing back a dead token. It must not
// also swallow a write: a caller supplying metadata is asking for the
// agent record to change, and returning a cached token discarded that
// silently — success, no warnings, record untouched.
const relay = fakeRelay();

const payload = await registerAgentWithRebind({
session: strictSession(),
setSession: vi.fn(),
getRelay: () => relay as never,
name: 'WorkerA',
metadata: IDENTITY,
verifyMetadata: true,
strictAgentName: true,
preferredAgentName: 'WorkerA',
});

expect(relay.agents.registerOrRotate).toHaveBeenCalledOnce();

// The round trip, not just the call: read the record back and assert the
// fields are actually there.
const [record] = (await relay.agents.list()).filter((a) => a.name === 'WorkerA');
expect(record.metadata).toMatchObject(IDENTITY);
expect(record.metadata.fleet).toEqual({ nodeId: 'node_x' }, 'must not clobber platform keys');

expect(payload.metadata_verified).toBe(true);
expect(payload.warnings).toEqual([]);
Comment on lines +101 to +106

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check how custom assertion messages are supplied in existing Vitest tests.
set -euo pipefail

rg -nP --type=ts -C 1 '\.toEqual\([^)]*,\s*['"'"'"]' -g '**/*.test.ts' | head -50

rg -nP --type=ts -C 1 'expect\([^,()]+,\s*['"'"'"]' -g '**/*.test.ts' | head -50

fd -t f -g 'vitest.config.*' --exec cat -n {}
fd -t f -g 'package.json' -d 3 --exec rg -n '"vitest"' {}

Repository: AgentWorkforce/relay

Length of output: 158


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "tracked candidate files:"
git ls-files | rg '(^|/)agent-relay-mcp\.test\.ts$|vitest|package\.json$' || true

echo
echo "target snippet:"
fd -a 'agent-relay-mcp\.test\.ts' . -x sh -c 'echo "--- $1"; sed -n "70,115p" "$1" | cat -n' sh {}

echo
echo "vitest deps from package manifests:"
fd -t f 'package\.json' -d 5 -x sh -c 'echo "--- $1"; rg -n "(\"`@vitest`|\"vitest\")" "$1" || true' sh {}

echo
echo "matcher second-arg and expect message patterns:"
rg -n --glob '*.test.ts' '\.toEqual\([^,)]+,\s*['"'"'"][^)]*\)' || true
rg -n --glob '*.test.ts' 'expect\s*\([^,()]+,\s*['"'"'"][^,)]+\)' || true

Repository: AgentWorkforce/relay

Length of output: 4891


🌐 Web query:

Vitest toEqual signature expects single argument expect value message custom message

💡 Result:

In Vitest, the standard toEqual matcher takes a single argument, which is the expected value to compare against the received value [1][2][3]. Although it is not explicitly highlighted in the primary API documentation, you can provide an optional custom failure message as a second argument to the expect function itself [4][5][6]. When used this way, the message acts as a prefix to the default error message if the assertion fails [5][6]. The signature for an assertion with a custom message is: expect(received, 'your custom message').toEqual(expected); Key points regarding this behavior: - Compatibility: This functionality allows the second argument of expect to serve as a custom message across various matchers [5][6]. - Behavior: If the assertion fails, the test will throw an error that includes your custom message [5][6]. If the message is omitted, empty, or undefined, it defaults to the standard matcher error message [6]. - Caveats: Because this is an expect function feature rather than a specific matcher parameter, it may behave unexpectedly if a specific matcher (like those provided by external libraries) also attempts to interpret a second argument [7]. Always verify that the matcher you are using is designed to receive only one argument before relying on this pattern [7].

Citations:


🌐 Web query:

Jest toEqual optional second argument message expect.toEqual actually accepts message

💡 Result:

No, Jest's standard expect.toEqual matcher does not accept an optional second argument for a custom error message [1][2][3]. The function signature is strictly expect.toEqual(expected) [1][2]. If you provide a second argument to toEqual, Jest will not treat it as a custom error message, and it may lead to unexpected behavior or assertion errors depending on your test implementation. To provide custom error messages in Jest, common approaches include: 1. Use Third-Party Packages: The library jest-expect-message is a widely used community solution that enables support for custom messages in assertions [4][5]. 2. Custom Matchers: You can use expect.extend to create your own matcher that wraps existing Jest matchers and includes custom logic to return a custom failure message [6][7]. 3. Manual Error Handling: For complex logic, developers often wrap the assertion in a try-catch block and manually throw a new Error with a custom message if the assertion fails [4][5].

Citations:


Move the custom message onto expect, not into toEqual.

expect(record.metadata.fleet, 'must not clobber platform keys').toEqual({ nodeId: 'node_x' })); has the expected type and keeps the message with this assertion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/cli/src/cli/agent-relay-mcp.test.ts` around lines 101 - 106, Update
the fleet metadata assertion in the relay agent test so the custom message is
passed to expect for record.metadata.fleet, while keeping toEqual focused only
on the expected value and preserving the existing assertion behavior.

});

it('says so loudly when the platform accepts metadata and does not persist it', async () => {
// This is the exact defect being fixed, reproduced: the write is accepted,
// the response looks like success, and the record is untouched. A
// passthrough that fails this way is worse than none, because it looks
// like it worked. It must never again be reported as a clean success.
const relay = fakeRelay({ persists: false });

const payload = await registerAgentWithRebind({
session: strictSession(),
setSession: vi.fn(),
getRelay: () => relay as never,
name: 'WorkerA',
metadata: IDENTITY,
verifyMetadata: true,
strictAgentName: true,
preferredAgentName: 'WorkerA',
});

expect(payload.metadata_verified).toBe(false);
expect(payload.warnings).toHaveLength(1);
expect(payload.warnings[0]).toContain('was not persisted');
expect(payload.warnings[0]).toContain('organization');
expect(payload.warnings[0]).toContain('Treat this registration as unattributed');
});

it('reports unverified rather than throwing when the record cannot be read back', async () => {
// The registration itself succeeded; claiming it failed would be its own
// kind of lie. But it must not be reported as verified either.
const relay = fakeRelay();
relay.agents.list = vi.fn(async () => {
throw new Error('workspace unreachable');
}) as never;

const payload = await registerAgentWithRebind({
session: strictSession(),
setSession: vi.fn(),
getRelay: () => relay as never,
name: 'WorkerA',
metadata: IDENTITY,
verifyMetadata: true,
strictAgentName: true,
preferredAgentName: 'WorkerA',
});

expect(payload.token).toBe('at_live_rotated');
expect(payload.metadata_verified).toBe(false);
expect(payload.warnings[0]).toContain('could not read the record back');
expect(payload.warnings[0]).toContain('workspace unreachable');
});

it('writes a supplied persona through, and claims no metadata verification', async () => {
const relay = fakeRelay();

const payload = await registerAgentWithRebind({
session: strictSession(),
setSession: vi.fn(),
getRelay: () => relay as never,
name: 'WorkerA',
persona: 'Accountable lead for chief-delegation-governance',
strictAgentName: true,
preferredAgentName: 'WorkerA',
});

expect(relay.agents.registerOrRotate).toHaveBeenCalledOnce();
// No metadata was supplied, so there is nothing to verify and no read-back
// cost is paid.
expect(relay.agents.list).not.toHaveBeenCalled();
expect(payload.metadata_verified).toBeUndefined();
});

it("reports 'unchecked' rather than success when nobody verified the write", async () => {
// Verification costs a workspace listing, so the per-spawn `{model}` hint
// does not pay for it. But "nobody looked" must not be reported as "it is
// there" — collapsing those two is the same error as the silent discard.
const relay = fakeRelay({ persists: false });

const payload = await registerAgentWithRebind({
session: strictSession(),
setSession: vi.fn(),
getRelay: () => relay as never,
name: 'WorkerA',
metadata: { model: 'gpt-5' },
strictAgentName: true,
preferredAgentName: 'WorkerA',
});

expect(payload.metadata_verified).toBe('unchecked');
expect(relay.agents.list).not.toHaveBeenCalled();
// Not a warning: nothing is known to be wrong. The claim is simply scoped.
expect(payload.warnings).toEqual([]);
});

it('still short-circuits when the caller only wants a token', async () => {
// The original behaviour has to survive: a bare re-registration with no
// write to make should not rotate the token for nothing.
const registerOrRotate = vi.fn();

const payload = await registerAgentWithRebind({
session: {
workspaceKey: 'rk_live_test',
agentToken: 'at_live_existing',
agentName: 'WorkerA',
agents: new Map([['WorkerA', { agentName: 'WorkerA', agentToken: 'at_live_existing' }]]),
},
setSession: vi.fn(),
getRelay: () => ({ agents: { registerOrRotate } }) as never,
name: 'WorkerA',
strictAgentName: true,
preferredAgentName: 'WorkerA',
});

expect(registerOrRotate).not.toHaveBeenCalled();
expect(payload).toMatchObject({ token: 'at_live_existing' });
});

it('re-registers when the strict-named identity was dropped from the agents map', async () => {
// After an `agent_token_invalid` recovery, the active token is null and
// the identity is missing from session.agents. The short-circuit must
Expand Down
Loading