Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it, vi } from 'vitest';
import en from '../../../i18n/en.json';
import zh from '../../../i18n/zh.json';
import { FIRST_PAINT_REGION_WHITELIST } from './firstPaintRegions';
import { modelChainKey, type ModelChainIndex, type ModelChainRequest } from './modelRows';
import type { SourceCreated } from './modelsApi';
import {
createContinuationSettlement,
createSourceCreatedDelivery,
readSurfaceLanding,
sourceMutationReadFailed,
sourceMutationReadScope,
SOURCE_MUTATION_OUTCOMES,
SOURCE_MUTATION_TOAST,
type SourceMutationLandingReads,
} from './mutationSettlement';
import { readyRegion, unreadRegion } from './regionRead';
import type { AgentChain, AgentSupply, RuntimeDependency, Source } from './types';
const translated = (bundle: unknown, key: string): unknown =>
key.split('.').reduce<unknown>((node, part) => {
if (!node || typeof node !== 'object') return undefined;
return (node as Record<string, unknown>)[part];
}, bundle);
describe('mutation settlement fences', () => {
it('atomically rejects every effect belonging to an invalidated attempt', () => {
const authority = createContinuationSettlement();
const attempt = authority.begin();
const apply = vi.fn();
authority.invalidate();
expect(authority.settle(attempt, apply)).toBe('stale');
expect(apply).not.toHaveBeenCalled();
});
it('keeps Source-created delivery behind the same attempt fence', () => {
const authority = createContinuationSettlement();
const delivery = createSourceCreatedDelivery();
const onAdded = vi.fn();
const onClose = vi.fn();
const created = { source: { id: 'src_example00' }, added_to: [], adopted_by: [] } as unknown as SourceCreated;
delivery.update(onAdded, onClose);
const attempt = authority.begin();
authority.invalidate();
expect(delivery.settle(authority, attempt, created)).toBe('stale');
expect(onAdded).not.toHaveBeenCalled();
expect(onClose).not.toHaveBeenCalled();
});
it('keeps child components from bypassing the settlement owners', () => {
const here = dirname(fileURLToPath(import.meta.url));
const addDialog = readFileSync(resolve(here, 'AddApiKeyDialog.tsx'), 'utf8');
const detail = readFileSync(resolve(here, 'SourceDetailPanel.tsx'), 'utf8');
expect(addDialog).not.toMatch(/onAddedRef|onCloseRef/);
expect(addDialog).toContain('createdDelivery.settle');
expect(detail).not.toMatch(/\bonMutation\b|\bonGone\b|beginSourceSnapshot/);
expect(detail).toContain('settlement.source');
expect(detail).toContain('settlement.gone');
});
it('reads the whole first-paint surface plus exactly the chains the impact named', async () => {
const impact = {
hops: [{ backend: 'claude' as const, menu_model: 'claude-opus-4-6', position: 1, source_id: 'src', model_id: 'model-a' }],
gaps: [{ backend: 'codex' as const, model_id: 'gpt-5.6-sol', agents: ['release'] }],
};
const affectedChains = sourceMutationReadScope(impact).affectedChains;
const calls: string[] = [];
let requested: readonly ModelChainRequest[] = [];
const reads = await readSurfaceLanding({
sources: async () => { calls.push('sources'); return [] as Source[]; },
supply: async () => { calls.push('supply'); return [] as AgentSupply[]; },
runtime: async () => { calls.push('runtime'); return {} as RuntimeDependency; },
chains: async (requests) => {
calls.push('chains');
requested = requests;
return Object.fromEntries(requests.map(({ backend, modelId }) => [
modelChainKey(backend, modelId),
readyRegion({} as AgentChain),
]));
},
}, affectedChains);
// A region that draws the surface must be read by the mutation that changes
// it, so the whitelist — not a second list kept here — names the coverage.
const expected = new Set([...Object.keys(FIRST_PAINT_REGION_WHITELIST), 'chains']);
expect(new Set(calls)).toEqual(expected);
expect(new Set(Object.keys(reads))).toEqual(expected);
// The impact evidence is the whole chain scope: no route the write did not
// touch is refetched, and every one it did touch is.
expect(requested).toEqual(affectedChains);
expect(affectedChains).toHaveLength(2);
expect(reads.chains.kind).toBe('ready');
});
it('routes every management settlement through one post-await announcement', () => {
const here = dirname(fileURLToPath(import.meta.url));
const page = readFileSync(resolve(here, 'SettingsModelsPage.tsx'), 'utf8');
const detail = readFileSync(resolve(here, 'SourceDetailPanel.tsx'), 'utf8');
const refresh = page.slice(page.indexOf('const refresh = React.useCallback'), page.indexOf('\n\n const trackSourceMutation'));
const settlement = page.slice(page.indexOf('const trackSourceMutation'), page.indexOf('\n\n React.useEffect', page.indexOf('const trackSourceMutation')));
const committed = detail.slice(
detail.indexOf('const commitManagementMutation'),
detail.indexOf('\n const reconcileEditWrite'),
);
expect(refresh).toMatch(/Promise<SourceMutationLanding>/);
// The page-wide refresh line answers for every region this read installs,
// route chains included. What it must never answer for is supersession —
// `refreshAffectedChains` reports no keys there rather than unread ones, so
// a newer read winning cannot arrive here as a page that could not refresh.
expect(refresh).toContain('sourceMutationReadFailed(landing)');
expect(refresh).toContain("t('settings.models.toast.refreshFailed')");
expect(settlement).toMatch(/Promise<SourceMutationLanding>/);
expect(settlement).toContain('return refresh(affectedChains)');
expect((detail.match(/dispatchManageStage\(\{ type: 'settled' \}\)/g) ?? []).length).toBe(1);
expect(committed).toMatch(/await onMutationCommitted[\s\S]*dispatchManageStage\(\{ type: 'settled' \}\)/);
});
});
/**
* The commit envelope is the only thing the page sees of a write, so the word it
* carries is the word the user reads. The finding this block exists for: an edit
* whose Source turned out to be absent still announced 「供应商已更新」 over a panel
* saying the provider is no longer there.
*/
describe('committed mutation outcomes', () => {
it('answers for every outcome a commit can carry', () => {
expect(Object.keys(SOURCE_MUTATION_TOAST).sort()).toEqual([...SOURCE_MUTATION_OUTCOMES].sort());
});
it.each(Object.entries(SOURCE_MUTATION_TOAST))('has copy in both locales for %s', (_outcome, toast) => {
for (const bundle of [en, zh]) expect(typeof translated(bundle, toast.key)).toBe('string');
});
it('never celebrates an outcome that is not the one the user asked for', () => {
// Tone tracks the outcome, not the branch that renders it: `gone` reaches the
// page from the EDIT flow, and green over 「已经不在了」 is the contradiction.
expect(SOURCE_MUTATION_TOAST.updated.tone).toBe('success');
expect(SOURCE_MUTATION_TOAST.removed.tone).toBe('success');
expect(SOURCE_MUTATION_TOAST.gone.tone).toBe('warning');
});
it('says the same thing as the surface under it when a Source is gone', () => {
// One fact, one sentence: the toast reuses the detail surface's own copy
// rather than adding a second wording for the same state.
expect(SOURCE_MUTATION_TOAST.gone.key).toBe('settings.models.sourceDetail.gone');
});
});
/**
* The page's refresh line answers one question — is anything on screen older
* than the server — and the bug that opened this lane was it answering a
* different one, saying a save that had landed could not be confirmed.
*/
describe('post-mutation read failure', () => {
const landing = (chains: ModelChainIndex): SourceMutationLandingReads => ({
sources: readyRegion([] as Source[]),
supply: readyRegion([] as AgentSupply[]),
runtime: readyRegion({} as RuntimeDependency),
chains: readyRegion(chains),
});
const chainKey = modelChainKey('claude', 'claude-opus-4-6');
it('stays silent when every projection came back', () => {
expect(sourceMutationReadFailed(landing({ [chainKey]: readyRegion({} as AgentChain) }))).toBe(false);
});
it('counts a route request that failed inside a readable index', () => {
// The index itself resolves — `refreshAffectedChains` catches per request —
// so a check that stopped at the index would call an unreadable route a
// clean refresh.
expect(sourceMutationReadFailed(landing({ [chainKey]: unreadRegion<AgentChain>() }))).toBe(true);
});
it('never counts a read a newer read took over', () => {
// Supersession reports no keys rather than unread ones. This is the case the
// user hit: the save had landed, and the page said it could not refresh.
expect(sourceMutationReadFailed(landing({}))).toBe(false);
});
it('counts a first-paint region that could not be read', () => {
expect(sourceMutationReadFailed({
...landing({}),
supply: unreadRegion<AgentSupply[]>(),
})).toBe(true);
});
});