feat(FN-3710): retry overlap-class cluster create failures in distributed t

Adds retry logic for distributed task ID overlap conflicts across the cluster, hardening the ID allocator with exponential-backoff recovery, wiring the retry into task workflow registration, and documenting the behavior in architecture docs. Minor CSS tokenization on the Mailbox and Plugin Manager m

Fusion-Task-Id: FN-3710
This commit is contained in:
Fusion
2026-05-07 11:44:42 -07:00
committed by gsxdsm
parent a98e5aac1a
commit 24325cd3c1
8 changed files with 264 additions and 138 deletions

View File

@@ -0,0 +1,5 @@
---
"@runfusion/fusion": patch
---
Fix cluster-backed task creation to automatically recover when a reserved `FN-*` ID overlaps an existing task. The allocator and task-create route now advance to the next available ID with bounded retry behavior while preserving reservation commit/abort safety guarantees.

View File

@@ -531,6 +531,7 @@ Implemented in `agent-heartbeat.ts`:
- Mesh allocator write routes (`/api/mesh/task-ids/reserve|commit|abort`) return `503` when the coordinator node is unreachable; they never fall back to local-only cluster ID issuance. - Mesh allocator write routes (`/api/mesh/task-ids/reserve|commit|abort`) return `503` when the coordinator node is unreachable; they never fall back to local-only cluster ID issuance.
- Cluster task creation now uses a strong-write reserve → create → replicate → commit/abort sequence. - Cluster task creation now uses a strong-write reserve → create → replicate → commit/abort sequence.
- `POST /api/tasks` reserves a distributed ID, creates the authoritative local task with that reserved ID, then POSTs authenticated replication payloads to peer nodes. - `POST /api/tasks` reserves a distributed ID, creates the authoritative local task with that reserved ID, then POSTs authenticated replication payloads to peer nodes.
- Creation self-heals stale ID overlap state: if a reserved `FN-*` collides with an existing task (`Task ID already exists...` or replicated-create collision), the route aborts that reservation, cleans up partial local state, reserves the next ID, and retries up to a bounded limit.
- Replica apply uses `TaskStore.applyReplicatedTaskCreate(...)`, which is idempotent by task ID: replaying the same payload returns the existing task without creating duplicates. - Replica apply uses `TaskStore.applyReplicatedTaskCreate(...)`, which is idempotent by task ID: replaying the same payload returns the existing task without creating duplicates.
- If an incoming replicated payload conflicts with a different existing task record for the same ID, the apply path returns a deterministic collision error instead of overwriting data. - If an incoming replicated payload conflicts with a different existing task record for the same ID, the apply path returns a deterministic collision error instead of overwriting data.
- Any replication/coordinator failure aborts the reservation and returns write failure (`503`), so this path does not report success for local-only partial writes. - Any replication/coordinator failure aborts the reservation and returns write failure (`503`), so this path does not report success for local-only partial writes.

View File

@@ -84,6 +84,28 @@ describe("distributed-task-id allocator", () => {
expect(state.nextSequence).toBe(3702); expect(state.nextSequence).toBe(3702);
}); });
it("skips stale overlapping nextSequence values and reserves the next free id", async () => {
const db = new Database("/tmp/fusion-test", { inMemory: true });
db.init();
const now = new Date().toISOString();
db.prepare(
"INSERT INTO tasks (id, description, \"column\", createdAt, updatedAt) VALUES (?, '', 'todo', ?, ?)",
).run("FN-002", now, now);
db.prepare(
"INSERT INTO distributed_task_id_state (prefix, nextSequence, committedClusterTaskCount, lastCommittedTaskId, updatedAt) VALUES (?, ?, ?, ?, ?)",
).run("FN", 2, 1, "FN-001", now);
const allocator = createDistributedTaskIdAllocator(db);
const reservation = await allocator.reserveDistributedTaskId({ prefix: "FN", nodeId: "node-a" });
expect(reservation.taskId).toBe("FN-003");
expect(reservation.sequence).toBe(3);
const state = await allocator.getDistributedTaskIdState({ prefix: "FN" });
expect(state.nextSequence).toBe(4);
expect(state.committedClusterTaskCount).toBe(1);
});
it("state reports committed count independently from nextSequence", async () => { it("state reports committed count independently from nextSequence", async () => {
const { allocator } = createAllocator(); const { allocator } = createAllocator();
const first = await allocator.reserveDistributedTaskId({ prefix: "FN", nodeId: "node-a" }); const first = await allocator.reserveDistributedTaskId({ prefix: "FN", nodeId: "node-a" });

View File

@@ -81,6 +81,22 @@ export function createDistributedTaskIdAllocator(db: Database): DistributedTaskI
return result.changes ?? 0; return result.changes ?? 0;
}; };
const taskIdExists = (prefix: string, sequence: number): boolean => {
const taskId = formatDistributedTaskId(prefix, sequence);
const existsInTable = (table: string): boolean => {
try {
const row = db
.prepare(`SELECT 1 as found FROM ${table} WHERE id = ? LIMIT 1`)
.get(taskId) as { found?: number } | undefined;
return row?.found === 1;
} catch {
return false;
}
};
return existsInTable("tasks") || existsInTable("archivedTasks");
};
const ensureStateRow = (prefix: string): void => { const ensureStateRow = (prefix: string): void => {
// Seed nextSequence past any pre-existing task ID for this prefix. Without // Seed nextSequence past any pre-existing task ID for this prefix. Without
// this, projects whose tasks were originally allocated through // this, projects whose tasks were originally allocated through
@@ -161,7 +177,11 @@ export function createDistributedTaskIdAllocator(db: Database): DistributedTaskI
) )
.get(prefix) as { nextSequence: number; committedClusterTaskCount: number }; .get(prefix) as { nextSequence: number; committedClusterTaskCount: number };
const sequence = state.nextSequence; let sequence = state.nextSequence;
while (taskIdExists(prefix, sequence)) {
sequence += 1;
}
const taskId = formatDistributedTaskId(prefix, sequence); const taskId = formatDistributedTaskId(prefix, sequence);
const reservationId = randomUUID(); const reservationId = randomUUID();

View File

@@ -27,11 +27,11 @@
justify-content: center; justify-content: center;
min-width: 20px; min-width: 20px;
height: 20px; height: 20px;
padding: 0 6px; padding: 0 var(--space-xs);
border-radius: 10px; border-radius: 10px;
background: var(--color-error); background: var(--color-error);
color: var(--fab-text); color: var(--fab-text);
font-size: 0.7rem; font-size: var(--font-size-3xs, 0.7rem);
font-weight: 700; font-weight: 700;
} }
@@ -46,7 +46,7 @@
flex-wrap: wrap; flex-wrap: wrap;
gap: var(--space-sm); gap: var(--space-sm);
padding: 0 var(--space-lg) var(--space-sm); padding: 0 var(--space-lg) var(--space-sm);
border-bottom: 1px solid var(--border); border-bottom: var(--btn-border-width) solid var(--border);
} }
.mailbox-tab { .mailbox-tab {
@@ -76,11 +76,11 @@
justify-content: center; justify-content: center;
min-width: 16px; min-width: 16px;
height: 16px; height: 16px;
padding: 0 4px; padding: 0 var(--btn-border-width);
border-radius: var(--radius-md); border-radius: var(--radius-md);
background: var(--color-error); background: var(--color-error);
color: var(--fab-text); color: var(--fab-text);
font-size: 0.65rem; font-size: var(--font-size-4xs, 0.65rem);
font-weight: 700; font-weight: 700;
} }
@@ -96,7 +96,7 @@
.mailbox-list { .mailbox-list {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 2px; gap: var(--btn-border-width);
} }
.mailbox-empty { .mailbox-empty {
@@ -112,7 +112,7 @@
.mailbox-empty p { .mailbox-empty p {
margin: 0; margin: 0;
font-size: 0.9rem; font-size: var(--font-size-base, 0.9rem);
} }
.mailbox-item { .mailbox-item {
@@ -156,12 +156,12 @@
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
gap: var(--space-sm); gap: var(--space-sm);
margin-bottom: 2px; margin-bottom: var(--btn-border-width);
} }
.mailbox-item-from, .mailbox-item-from,
.mailbox-item-to { .mailbox-item-to {
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text); color: var(--text);
white-space: nowrap; white-space: nowrap;
overflow: hidden; overflow: hidden;
@@ -169,14 +169,14 @@
} }
.mailbox-item-time { .mailbox-item-time {
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
color: var(--text-muted); color: var(--text-muted);
white-space: nowrap; white-space: nowrap;
flex-shrink: 0; flex-shrink: 0;
} }
.mailbox-item-preview { .mailbox-item-preview {
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
color: var(--text-muted); color: var(--text-muted);
white-space: nowrap; white-space: nowrap;
overflow: hidden; overflow: hidden;
@@ -189,14 +189,14 @@
border-radius: 50%; border-radius: 50%;
background: var(--todo); background: var(--todo);
flex-shrink: 0; flex-shrink: 0;
margin-top: 6px; margin-top: var(--space-xs);
} }
/* Conversation grouping */ /* Conversation grouping */
.mailbox-conversations { .mailbox-conversations {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 2px; gap: var(--btn-border-width);
} }
.mailbox-conversation-group { .mailbox-conversation-group {
@@ -225,14 +225,14 @@
justify-content: center; justify-content: center;
min-width: 20px; min-width: 20px;
height: 20px; height: 20px;
padding: 0 6px; padding: 0 var(--space-xs);
font-size: 11px; font-size: var(--font-size-xs, 0.8rem);
font-weight: 600; font-weight: 600;
background: var(--todo); background: var(--todo);
color: var(--fab-text); color: var(--fab-text);
border-radius: 10px; border-radius: 10px;
flex-shrink: 0; flex-shrink: 0;
margin-top: 6px; margin-top: var(--space-xs);
} }
/* Message detail view */ /* Message detail view */
@@ -254,7 +254,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: var(--space-sm); gap: var(--space-sm);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
color: var(--text-muted); color: var(--text-muted);
} }
@@ -266,21 +266,21 @@
.mailbox-message-type { .mailbox-message-type {
display: inline-flex; display: inline-flex;
padding: 2px 8px; padding: var(--btn-border-width) var(--space-sm);
border-radius: 4px; border-radius: var(--radius-sm);
background: var(--bg-tertiary); background: var(--bg-tertiary);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
color: var(--text-muted); color: var(--text-muted);
} }
.mailbox-message-time { .mailbox-message-time {
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
} }
.mailbox-message-participants { .mailbox-message-participants {
display: flex; display: flex;
gap: 24px; gap: var(--space-xl);
padding: 12px; padding: var(--space-md);
background: var(--bg-tertiary); background: var(--bg-tertiary);
border-radius: var(--radius-md); border-radius: var(--radius-md);
} }
@@ -288,8 +288,8 @@
.mailbox-participant { .mailbox-participant {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 6px; gap: var(--space-xs);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
} }
.mailbox-participant-label { .mailbox-participant-label {
@@ -299,7 +299,7 @@
.mailbox-participant-value { .mailbox-participant-value {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 4px; gap: var(--btn-border-width);
color: var(--text); color: var(--text);
} }
@@ -307,7 +307,7 @@
padding: var(--space-lg); padding: var(--space-lg);
background: var(--bg-secondary); background: var(--bg-secondary);
border-radius: var(--radius-md); border-radius: var(--radius-md);
font-size: 0.9rem; font-size: var(--font-size-base, 0.9rem);
line-height: 1.5; line-height: 1.5;
white-space: pre-wrap; white-space: pre-wrap;
word-break: break-word; word-break: break-word;
@@ -319,7 +319,7 @@
border-radius: var(--radius-md); border-radius: var(--radius-md);
background: color-mix(in srgb, var(--text-muted) 12%, transparent); background: color-mix(in srgb, var(--text-muted) 12%, transparent);
color: var(--text-muted); color: var(--text-muted);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
line-height: 1.4; line-height: 1.4;
white-space: pre-wrap; white-space: pre-wrap;
word-break: break-word; word-break: break-word;
@@ -333,16 +333,16 @@
} }
.mailbox-conversation-label { .mailbox-conversation-label {
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
color: var(--text-muted); color: var(--text-muted);
font-weight: 600; font-weight: 600;
} }
.mailbox-conversation-msg { .mailbox-conversation-msg {
padding: 8px 12px; padding: var(--space-sm) var(--space-md);
border-radius: var(--radius-md); border-radius: var(--radius-md);
background: var(--bg-tertiary); background: var(--bg-tertiary);
border-left: 3px solid transparent; border-left: calc(var(--btn-border-width) * 3) solid transparent;
} }
.mailbox-conversation-msg.current { .mailbox-conversation-msg.current {
@@ -355,13 +355,13 @@
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
gap: var(--space-sm); gap: var(--space-sm);
margin-bottom: 4px; margin-bottom: var(--btn-border-width);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
color: var(--text-muted); color: var(--text-muted);
} }
.mailbox-conversation-msg-body { .mailbox-conversation-msg-body {
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
line-height: 1.4; line-height: 1.4;
white-space: pre-wrap; white-space: pre-wrap;
word-break: break-word; word-break: break-word;
@@ -372,7 +372,7 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: var(--space-md); gap: var(--space-md);
min-height: 300px; min-height: calc(var(--space-xl) * 2 + var(--space-lg));
} }
.mailbox-agents-dropdown { .mailbox-agents-dropdown {
@@ -399,7 +399,7 @@
flex: 1; flex: 1;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 2px; gap: var(--btn-border-width);
} }
/* === Mailbox Agent Sub-Tabs === */ /* === Mailbox Agent Sub-Tabs === */
@@ -408,14 +408,14 @@
flex-direction: row; flex-direction: row;
gap: var(--space-sm); gap: var(--space-sm);
padding: var(--space-sm) 0; padding: var(--space-sm) 0;
border-bottom: 1px solid var(--border); border-bottom: var(--btn-border-width) solid var(--border);
} }
.mailbox-agent-subtab { .mailbox-agent-subtab {
justify-content: center; justify-content: center;
min-height: 30px; min-height: calc(var(--space-lg) + var(--space-sm) - var(--btn-border-width));
padding: var(--space-xs) var(--space-md); padding: var(--space-xs) var(--space-md);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
color: var(--text-muted); color: var(--text-muted);
border-color: var(--border); border-color: var(--border);
background: var(--surface); background: var(--surface);
@@ -433,8 +433,8 @@
} }
.mailbox-agent-subtab .mailbox-tab-badge { .mailbox-agent-subtab .mailbox-tab-badge {
margin-left: 4px; margin-left: var(--btn-border-width);
font-size: 0.7rem; font-size: var(--font-size-3xs, 0.7rem);
} }
/* Skeleton loading */ /* Skeleton loading */
@@ -448,7 +448,7 @@
display: flex; display: flex;
align-items: flex-start; align-items: flex-start;
gap: var(--space-md); gap: var(--space-md);
padding: 12px; padding: var(--space-md);
} }
.mailbox-skeleton-avatar { .mailbox-skeleton-avatar {
@@ -463,12 +463,12 @@
flex: 1; flex: 1;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 6px; gap: var(--space-xs);
} }
.mailbox-skeleton-line { .mailbox-skeleton-line {
height: 12px; height: 12px;
border-radius: 4px; border-radius: var(--radius-sm);
background: var(--bg-tertiary); background: var(--bg-tertiary);
animation: pulse 1.5s ease-in-out infinite; animation: pulse 1.5s ease-in-out infinite;
} }
@@ -493,7 +493,7 @@
.mailbox-view .mailbox-header { .mailbox-view .mailbox-header {
padding: var(--space-lg) var(--space-xl); padding: var(--space-lg) var(--space-xl);
border-bottom: 1px solid var(--border); border-bottom: var(--btn-border-width) solid var(--border);
background: var(--surface); background: var(--surface);
} }
@@ -520,7 +520,7 @@
.mailbox-view .mailbox-split-detail-pane { .mailbox-view .mailbox-split-detail-pane {
min-height: 0; min-height: 0;
overflow-y: auto; overflow-y: auto;
border: 1px solid var(--border); border: var(--btn-border-width) solid var(--border);
border-radius: var(--radius-md); border-radius: var(--radius-md);
background: var(--surface); background: var(--surface);
padding: var(--space-md); padding: var(--space-md);
@@ -606,7 +606,7 @@
.mailbox-modal .mailbox-tab { .mailbox-modal .mailbox-tab {
flex-shrink: 0; flex-shrink: 0;
padding: var(--space-sm) var(--space-md); padding: var(--space-sm) var(--space-md);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
} }
.mailbox-modal .mailbox-content { .mailbox-modal .mailbox-content {
@@ -679,7 +679,7 @@
.mailbox-view .mailbox-tab { .mailbox-view .mailbox-tab {
flex-shrink: 0; flex-shrink: 0;
padding: var(--space-sm) var(--space-md); padding: var(--space-sm) var(--space-md);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
} }
.mailbox-view .mailbox-content { .mailbox-view .mailbox-content {
@@ -747,7 +747,7 @@
/* Message Composer mobile overrides */ /* Message Composer mobile overrides */
.message-composer-textarea { .message-composer-textarea {
min-height: 80px; min-height: calc(var(--space-2xl) + var(--space-lg));
} }
.message-composer-footer { .message-composer-footer {
@@ -761,7 +761,7 @@
.message-composer-field { .message-composer-field {
flex-direction: column; flex-direction: column;
gap: 4px; gap: var(--btn-border-width);
} }
.message-composer-label { .message-composer-label {
@@ -775,7 +775,7 @@
.message-composer { .message-composer {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 16px; gap: var(--space-lg);
} }
.message-composer-header { .message-composer-header {
@@ -783,7 +783,7 @@
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
font-weight: 600; font-weight: 600;
font-size: 0.95rem; font-size: var(--font-size-md, 0.95rem);
} }
.message-composer-body { .message-composer-body {
@@ -803,21 +803,21 @@
} }
.message-composer-label { .message-composer-label {
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text-muted); color: var(--text-muted);
min-width: 60px; min-width: calc(var(--space-xl) + var(--space-lg) + var(--space-sm) + var(--btn-border-width));
padding-top: 6px; padding-top: var(--space-xs);
flex-shrink: 0; flex-shrink: 0;
} }
.message-composer-select { .message-composer-select {
flex: 1; flex: 1;
padding: 6px 10px; padding: var(--space-xs) calc(var(--space-sm) + var(--btn-border-width));
border: 1px solid var(--border); border: var(--btn-border-width) solid var(--border);
border-radius: var(--radius-sm); border-radius: var(--radius-sm);
background: var(--bg-secondary); background: var(--bg-secondary);
color: var(--text); color: var(--text);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
font-family: var(--font-primary); font-family: var(--font-primary);
outline: none; outline: none;
transition: border-color var(--transition-fast), box-shadow var(--transition-fast); transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
@@ -836,25 +836,25 @@
.message-composer-recipient-fixed { .message-composer-recipient-fixed {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 6px; gap: var(--space-xs);
padding: 8px 12px; padding: var(--space-sm) var(--space-md);
background: var(--bg-tertiary); background: var(--bg-tertiary);
border-radius: var(--radius-sm); border-radius: var(--radius-sm);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text); color: var(--text);
} }
.message-composer-textarea { .message-composer-textarea {
width: 100%; width: 100%;
padding: 6px 10px; padding: var(--space-xs) calc(var(--space-sm) + var(--btn-border-width));
border: 1px solid var(--border); border: var(--btn-border-width) solid var(--border);
border-radius: var(--radius-sm); border-radius: var(--radius-sm);
background: var(--bg-secondary); background: var(--bg-secondary);
color: var(--text); color: var(--text);
font-size: 0.9rem; font-size: var(--font-size-base, 0.9rem);
font-family: var(--font-primary); font-family: var(--font-primary);
resize: vertical; resize: vertical;
min-height: 100px; min-height: calc(var(--space-2xl) + var(--space-xl) + var(--space-md));
line-height: 1.5; line-height: 1.5;
outline: none; outline: none;
transition: border-color var(--transition-fast), box-shadow var(--transition-fast); transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
@@ -872,7 +872,7 @@
.message-composer-charcount { .message-composer-charcount {
text-align: right; text-align: right;
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
color: var(--text-muted); color: var(--text-muted);
} }
@@ -888,7 +888,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: var(--space-sm); gap: var(--space-sm);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text-muted); color: var(--text-muted);
cursor: pointer; cursor: pointer;
} }
@@ -903,7 +903,7 @@
.message-composer-wake-hint { .message-composer-wake-hint {
margin-left: var(--space-xs); margin-left: var(--space-xs);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
color: var(--text-muted); color: var(--text-muted);
} }
@@ -915,7 +915,7 @@
background: color-mix(in srgb, var(--color-error) 10%, transparent); background: color-mix(in srgb, var(--color-error) 10%, transparent);
border-radius: var(--radius-sm); border-radius: var(--radius-sm);
color: var(--color-error); color: var(--color-error);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
} }
.message-composer-footer { .message-composer-footer {

View File

@@ -19,7 +19,7 @@
} }
.plugin-manager-header-title { .plugin-manager-header-title {
font-size: 13px; font-size: var(--font-size-sm, 0.85rem);
font-weight: 600; font-weight: 600;
color: var(--text); color: var(--text);
flex: 1; flex: 1;
@@ -43,7 +43,7 @@
.plugin-install-hint { .plugin-install-hint {
margin: 0; margin: 0;
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text-secondary, var(--text-muted)); color: var(--text-secondary, var(--text-muted));
line-height: 1.45; line-height: 1.45;
} }
@@ -97,7 +97,7 @@
} }
.plugin-version { .plugin-version {
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
} }
.plugin-state-badge { .plugin-state-badge {
@@ -105,7 +105,7 @@
align-items: center; align-items: center;
padding: var(--space-xs) var(--space-sm); padding: var(--space-xs) var(--space-sm);
border-radius: var(--radius-pill); border-radius: var(--radius-pill);
font-size: 0.7rem; font-size: var(--font-size-3xs, 0.7rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
letter-spacing: 0.04em; letter-spacing: 0.04em;
@@ -157,7 +157,7 @@
} }
.plugin-description { .plugin-description {
font-size: 0.95rem; font-size: var(--font-size-md, 0.95rem);
color: var(--text-secondary, var(--text-muted)); color: var(--text-secondary, var(--text-muted));
line-height: 1.5; line-height: 1.5;
} }
@@ -166,7 +166,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: var(--space-xs); gap: var(--space-xs);
font-size: 0.9rem; font-size: var(--font-size-base, 0.9rem);
color: var(--text-muted); color: var(--text-muted);
} }
@@ -180,7 +180,7 @@
align-items: center; align-items: center;
gap: var(--space-xs); gap: var(--space-xs);
color: var(--color-info); color: var(--color-info);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
flex-wrap: wrap; flex-wrap: wrap;
overflow-wrap: anywhere; overflow-wrap: anywhere;
} }
@@ -189,7 +189,7 @@
margin: 0; margin: 0;
padding: 0; padding: 0;
border: 0; border: 0;
font-size: 0.95rem; font-size: var(--font-size-md, 0.95rem);
} }
.plugin-settings-form { .plugin-settings-form {
@@ -212,7 +212,7 @@
.plugin-settings-group-heading { .plugin-settings-group-heading {
margin: 0; margin: 0;
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
font-weight: 600; font-weight: 600;
letter-spacing: 0.04em; letter-spacing: 0.04em;
text-transform: uppercase; text-transform: uppercase;
@@ -317,12 +317,12 @@
.plugin-builtins-heading { .plugin-builtins-heading {
margin: 0; margin: 0;
font-size: 0.95rem; font-size: var(--font-size-md, 0.95rem);
} }
.plugin-builtins-description { .plugin-builtins-description {
margin: 0; margin: 0;
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text-muted); color: var(--text-muted);
} }
@@ -356,7 +356,7 @@
.plugin-builtins-name { .plugin-builtins-name {
color: var(--text); color: var(--text);
font-size: 0.9rem; font-size: var(--font-size-base, 0.9rem);
font-weight: 500; font-weight: 500;
} }
@@ -367,7 +367,7 @@
border-radius: var(--radius-pill); border-radius: var(--radius-pill);
background: var(--status-in-review-bg); background: var(--status-in-review-bg);
color: var(--in-review); color: var(--in-review);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
} }
@@ -377,7 +377,7 @@
align-items: center; align-items: center;
padding: var(--btn-border-width) var(--space-xs); padding: var(--btn-border-width) var(--space-xs);
border-radius: var(--radius-pill); border-radius: var(--radius-pill);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
} }
@@ -397,7 +397,7 @@
align-items: center; align-items: center;
padding: var(--btn-border-width) var(--space-xs); padding: var(--btn-border-width) var(--space-xs);
border-radius: var(--radius-pill); border-radius: var(--radius-pill);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
} }
@@ -425,12 +425,12 @@
.plugin-builtins-description-text { .plugin-builtins-description-text {
flex: 1 1 100%; flex: 1 1 100%;
color: var(--text-muted); color: var(--text-muted);
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
} }
.plugin-builtins-metadata-only { .plugin-builtins-metadata-only {
color: var(--text-muted); color: var(--text-muted);
font-size: 0.8rem; font-size: var(--font-size-xs, 0.8rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
} }
@@ -465,7 +465,7 @@
.plugin-bundled-runtime-name { .plugin-bundled-runtime-name {
color: var(--text); color: var(--text);
font-size: 0.9rem; font-size: var(--font-size-base, 0.9rem);
font-weight: 500; font-weight: 500;
} }
@@ -476,7 +476,7 @@
border-radius: var(--radius-pill); border-radius: var(--radius-pill);
background: var(--status-in-review-bg); background: var(--status-in-review-bg);
color: var(--in-review); color: var(--in-review);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
} }
@@ -495,12 +495,12 @@
.plugin-bundled-runtime-heading { .plugin-bundled-runtime-heading {
margin: 0; margin: 0;
font-size: 0.95rem; font-size: var(--font-size-md, 0.95rem);
} }
.plugin-bundled-runtime-description { .plugin-bundled-runtime-description {
margin: 0; margin: 0;
font-size: 0.85rem; font-size: var(--font-size-sm, 0.85rem);
color: var(--text-muted); color: var(--text-muted);
} }
@@ -509,7 +509,7 @@
align-items: center; align-items: center;
padding: var(--btn-border-width) var(--space-xs); padding: var(--btn-border-width) var(--space-xs);
border-radius: var(--radius-pill); border-radius: var(--radius-pill);
font-size: 0.75rem; font-size: var(--font-size-2xs, 0.75rem);
font-weight: 600; font-weight: 600;
text-transform: uppercase; text-transform: uppercase;
} }

View File

@@ -727,6 +727,66 @@ describe("POST /tasks", () => {
expect(res.body.error).toContain("nodeId must be a string"); expect(res.body.error).toContain("nodeId must be a string");
}); });
it("retries reserved-id create when first reservation overlaps an existing task id", async () => {
const reserveDistributedTaskId = vi
.fn()
.mockResolvedValueOnce({ reservationId: "res-1", taskId: "FN-7001" })
.mockResolvedValueOnce({ reservationId: "res-2", taskId: "FN-7002" });
const commitDistributedTaskIdReservation = vi.fn().mockResolvedValue({});
const abortDistributedTaskIdReservation = vi.fn().mockResolvedValue({});
const createTaskWithReservedId = vi
.fn()
.mockRejectedValueOnce(new Error("Task ID already exists: FN-7001"))
.mockResolvedValueOnce({
...FAKE_TASK_DETAIL,
id: "FN-7002",
column: "triage",
createdAt: "2026-05-05T00:00:00.000Z",
updatedAt: "2026-05-05T00:00:00.000Z",
});
const deleteTask = vi.fn().mockResolvedValue(undefined);
const getTask = vi.fn().mockResolvedValue({ ...FAKE_TASK_DETAIL, prompt: "# FN-7002\n\nBig initiative\n" });
const storeWithReservedCreate = createMockStore({
createTaskWithReservedId,
deleteTask,
getTask,
getDistributedTaskIdAllocator: vi.fn().mockReturnValue({
reserveDistributedTaskId,
commitDistributedTaskIdReservation,
abortDistributedTaskIdReservation,
}),
});
const app = express();
app.use(express.json());
app.use("/api", createApiRoutes(storeWithReservedCreate));
const res = await REQUEST(
app,
"POST",
"/api/tasks",
JSON.stringify({ description: "Big initiative" }),
{ "Content-Type": "application/json" },
);
expect(res.status).toBe(201);
expect(createTaskWithReservedId).toHaveBeenCalledTimes(2);
expect(createTaskWithReservedId).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ description: "Big initiative" }),
expect.objectContaining({ taskId: "FN-7001" }),
);
expect(createTaskWithReservedId).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ description: "Big initiative" }),
expect.objectContaining({ taskId: "FN-7002" }),
);
expect(abortDistributedTaskIdReservation).toHaveBeenCalledTimes(1);
expect(abortDistributedTaskIdReservation).toHaveBeenCalledWith(expect.objectContaining({ reservationId: "res-1", reason: "failed-create" }));
expect(commitDistributedTaskIdReservation).toHaveBeenCalledWith(expect.objectContaining({ reservationId: "res-2" }));
expect(deleteTask).not.toHaveBeenCalled();
});
it("aborts reservation and deletes local task on replication failure", async () => { it("aborts reservation and deletes local task on replication failure", async () => {
const reserveDistributedTaskId = vi.fn().mockResolvedValue({ reservationId: "res-1", taskId: "FN-7002" }); const reserveDistributedTaskId = vi.fn().mockResolvedValue({ reservationId: "res-1", taskId: "FN-7002" });
const commitDistributedTaskIdReservation = vi.fn().mockResolvedValue({}); const commitDistributedTaskIdReservation = vi.fn().mockResolvedValue({});
@@ -768,6 +828,7 @@ describe("POST /tasks", () => {
expect(abortDistributedTaskIdReservation).toHaveBeenCalledWith(expect.objectContaining({ reservationId: "res-1", reason: "failed-create" })); expect(abortDistributedTaskIdReservation).toHaveBeenCalledWith(expect.objectContaining({ reservationId: "res-1", reason: "failed-create" }));
expect(deleteTask).toHaveBeenCalledWith("FN-7002"); expect(deleteTask).toHaveBeenCalledWith("FN-7002");
expect(commitDistributedTaskIdReservation).not.toHaveBeenCalled(); expect(commitDistributedTaskIdReservation).not.toHaveBeenCalled();
expect(reserveDistributedTaskId).toHaveBeenCalledTimes(1);
vi.unstubAllGlobals(); vi.unstubAllGlobals();
mockCentralListNodes.mockResolvedValue([]); mockCentralListNodes.mockResolvedValue([]);
}); });

View File

@@ -249,53 +249,70 @@ export function registerTaskWorkflowRoutes(ctx: ApiRoutesContext, deps: TaskWork
const remoteNodes = nodes.filter((node) => node.type === "remote" && node.url && node.apiKey); const remoteNodes = nodes.filter((node) => node.type === "remote" && node.url && node.apiKey);
await central.close(); await central.close();
const reservation = await allocator.reserveDistributedTaskId({ const nodeIdForReservation = localNode?.id ?? "local";
prefix: "FN", const isOverlapClassFailure = (err: unknown): boolean => {
nodeId: localNode?.id ?? "local",
});
let createdTask: Task | null = null;
try {
createdTask = await scopedStore.createTaskWithReservedId(createInput, {
taskId: reservation.taskId,
});
const replicatedPayload = buildMeshReplicatedTaskCreatePayload({
taskId: createdTask.id,
reservationId: reservation.reservationId,
sourceNodeId: localNode?.id ?? "local",
createdAt: createdTask.createdAt,
updatedAt: createdTask.updatedAt,
prompt: (await scopedStore.getTask(createdTask.id)).prompt,
createInput: toReplicatedCreateInput(createdTask),
});
for (const peer of remoteNodes) {
await fetchFromRemoteNode(peer, "/api/mesh/tasks/create", {
method: "POST",
body: replicatedPayload,
});
}
await allocator.commitDistributedTaskIdReservation({
reservationId: reservation.reservationId,
nodeId: localNode?.id ?? "local",
});
res.status(201).json(createdTask);
} catch (err: unknown) {
await allocator.abortDistributedTaskIdReservation({
reservationId: reservation.reservationId,
nodeId: localNode?.id ?? "local",
reason: "failed-create",
}).catch(() => undefined);
if (createdTask) {
await scopedStore.deleteTask(createdTask.id).catch(() => undefined);
}
const message = err instanceof Error ? err.message : String(err); const message = err instanceof Error ? err.message : String(err);
throw new ApiError(503, `Cluster task create failed: ${message}`); return (
message.includes("Task ID already exists:") ||
message.includes("Replicated task payload collision for existing task")
);
};
const maxCreateAttempts = 3;
for (let attempt = 1; attempt <= maxCreateAttempts; attempt += 1) {
const reservation = await allocator.reserveDistributedTaskId({
prefix: "FN",
nodeId: nodeIdForReservation,
});
let createdTask: Task | null = null;
try {
createdTask = await scopedStore.createTaskWithReservedId(createInput, {
taskId: reservation.taskId,
});
const replicatedPayload = buildMeshReplicatedTaskCreatePayload({
taskId: createdTask.id,
reservationId: reservation.reservationId,
sourceNodeId: nodeIdForReservation,
createdAt: createdTask.createdAt,
updatedAt: createdTask.updatedAt,
prompt: (await scopedStore.getTask(createdTask.id)).prompt,
createInput: toReplicatedCreateInput(createdTask),
});
for (const peer of remoteNodes) {
await fetchFromRemoteNode(peer, "/api/mesh/tasks/create", {
method: "POST",
body: replicatedPayload,
});
}
await allocator.commitDistributedTaskIdReservation({
reservationId: reservation.reservationId,
nodeId: nodeIdForReservation,
});
res.status(201).json(createdTask);
return;
} catch (err: unknown) {
await allocator.abortDistributedTaskIdReservation({
reservationId: reservation.reservationId,
nodeId: nodeIdForReservation,
reason: "failed-create",
}).catch(() => undefined);
if (createdTask) {
await scopedStore.deleteTask(createdTask.id).catch(() => undefined);
}
if (attempt < maxCreateAttempts && isOverlapClassFailure(err)) {
continue;
}
const message = err instanceof Error ? err.message : String(err);
throw new ApiError(503, `Cluster task create failed: ${message}`);
}
} }
} catch (err: unknown) { } catch (err: unknown) {
if (err instanceof ApiError) { if (err instanceof ApiError) {