fix(provenance): address #143 re-review — shared resolver + decoupled badge
Architecture & design: - Arch A: introduce resolveProvenance() as the single source of truth for deriving a write's actor/aiChatId from the SIGNED identity, and wire it into BOTH transport seams — the REST jwt.strategy and the collab authentication.extension. Previously the collab seam derived actor from the token claim alone and ignored user.isAgent, so a flagged service account's page-content edits over the websocket persisted as lastUpdatedSource='user', drifting from REST. The seams now share one resolver and can't diverge. - Arch B: drop AiAgentBadge's page-history coupling. The generic ui/ badge no longer imports historyAtoms; it exposes an onActivate callback fired after the deep-link, and the history row passes onActivate to close its own modal. Suggestions/warnings: - S1: soften the jwt.strategy provenance comment (applies to every REST write). - S2/suggestion-3: drop the redundant comment-list-item null-aiChatId test (covered by ai-agent-badge.test.tsx). - S3: de-duplicate jwt.strategy.spec test #3 (the no-claim→'user' half duplicated test #2); keep only the signed actor='agent' claim assertion. - W2: add keyboard-activation tests for the badge (Enter/Space, unrelated key). - W3: flip the design doc status to "реализовано (#143)". Tests: - new auth-provenance.decorator.spec.ts unit-tests resolveProvenance + agentSourceFields. - new collab-seam test: is_agent user with no claim → actor='agent' (Arch A regression guard). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
resolveProvenance,
|
||||
agentSourceFields,
|
||||
} from './auth-provenance.decorator';
|
||||
|
||||
/**
|
||||
* Unit tests for the shared provenance helpers (#143 review, Arch A & follow-up
|
||||
* 5). resolveProvenance is the single source of truth wired into BOTH transport
|
||||
* seams (REST jwt.strategy + collab authentication.extension) — testing it here
|
||||
* pins the derivation matrix so the seams can't silently drift. agentSourceFields
|
||||
* is the one-place write-stamp idiom reused at every insert/update site.
|
||||
*/
|
||||
describe('resolveProvenance', () => {
|
||||
it("flags an is_agent user as 'agent' even with no claim (the closed collab gap)", () => {
|
||||
expect(resolveProvenance({ isAgent: true }, undefined)).toEqual({
|
||||
actor: 'agent',
|
||||
aiChatId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("an is_agent user keeps the claim's aiChatId when present", () => {
|
||||
expect(
|
||||
resolveProvenance({ isAgent: true }, { aiChatId: 'chat-1' }),
|
||||
).toEqual({ actor: 'agent', aiChatId: 'chat-1' });
|
||||
});
|
||||
|
||||
it("honors a signed actor='agent' claim on a non-agent user (internal AI-chat token)", () => {
|
||||
expect(
|
||||
resolveProvenance(
|
||||
{ isAgent: false },
|
||||
{ actor: 'agent', aiChatId: 'chat-2' },
|
||||
),
|
||||
).toEqual({ actor: 'agent', aiChatId: 'chat-2' });
|
||||
});
|
||||
|
||||
it("a plain user with no claim resolves to 'user' with null chat", () => {
|
||||
expect(resolveProvenance({ isAgent: false }, undefined)).toEqual({
|
||||
actor: 'user',
|
||||
aiChatId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('tolerates a null/undefined user (defaults to the claim, else user)', () => {
|
||||
expect(resolveProvenance(null, null)).toEqual({
|
||||
actor: 'user',
|
||||
aiChatId: null,
|
||||
});
|
||||
expect(resolveProvenance(undefined, { actor: 'agent' })).toEqual({
|
||||
actor: 'agent',
|
||||
aiChatId: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('agentSourceFields', () => {
|
||||
it('stamps the configured source + chat columns for an agent write', () => {
|
||||
expect(
|
||||
agentSourceFields(
|
||||
{ actor: 'agent', aiChatId: 'chat-1' },
|
||||
'createdSource',
|
||||
'aiChatId',
|
||||
),
|
||||
).toEqual({ createdSource: 'agent', aiChatId: 'chat-1' });
|
||||
});
|
||||
|
||||
it('uses the per-table column names passed in (page update variant)', () => {
|
||||
expect(
|
||||
agentSourceFields(
|
||||
{ actor: 'agent', aiChatId: null },
|
||||
'lastUpdatedSource',
|
||||
'lastUpdatedAiChatId',
|
||||
),
|
||||
).toEqual({ lastUpdatedSource: 'agent', lastUpdatedAiChatId: null });
|
||||
});
|
||||
|
||||
it('returns {} for a user write so the column keeps its default', () => {
|
||||
expect(
|
||||
agentSourceFields(
|
||||
{ actor: 'user', aiChatId: null },
|
||||
'createdSource',
|
||||
'aiChatId',
|
||||
),
|
||||
).toEqual({});
|
||||
});
|
||||
|
||||
it('returns {} when provenance is undefined', () => {
|
||||
expect(
|
||||
agentSourceFields(undefined, 'createdSource', 'aiChatId'),
|
||||
).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,30 @@ export interface AuthProvenanceData {
|
||||
aiChatId: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Single source of truth for deriving a write's provenance from the SIGNED
|
||||
* server-side identity (#143 review, Arch A). Used by BOTH transport seams — the
|
||||
* REST access-token strategy and the collab websocket auth — so they can't drift:
|
||||
*
|
||||
* - A `user.isAgent` service account (e.g. the MCP bot) stamps 'agent' on every
|
||||
* write. It has no internal ai_chats row, so aiChatId comes from the claim
|
||||
* (usually null).
|
||||
* - Otherwise honor the actor claim minted into the internal AI agent's token
|
||||
* (actor='agent' + aiChatId); a normal user token carries no claim → 'user'.
|
||||
*
|
||||
* Provenance is NEVER read from a client body field, so a normal user cannot fake
|
||||
* an 'agent' marker.
|
||||
*/
|
||||
export function resolveProvenance(
|
||||
user: { isAgent?: boolean | null } | null | undefined,
|
||||
claim: { actor?: ProvenanceSource; aiChatId?: string | null } | null | undefined,
|
||||
): AuthProvenanceData {
|
||||
const actor: ProvenanceSource = user?.isAgent
|
||||
? 'agent'
|
||||
: (claim?.actor ?? 'user');
|
||||
return { actor, aiChatId: claim?.aiChatId ?? null };
|
||||
}
|
||||
|
||||
/**
|
||||
* Agent-edit write-stamp fields for a repository insert/update (#143 review).
|
||||
* Spread into the row being written: for an agent it stamps the `*Source`
|
||||
|
||||
Reference in New Issue
Block a user