fix: allow paired_session_task router_state keys and guard new keys at write time

Commit c858b6e started writing paired_session_task:<folder> keys into
router_state, but the startup check only allowed last_seq/last_agent_seq.
The first restart after those keys existed (2026-10-03 05:11) exited with
code 78 and crash-looped ejclaw.service for 11 minutes.

- Keep the allowed keys/prefixes in one list in src/db/router-state.ts and
  use it for both the startup check and setRouterStateInDatabase, so an
  unregistered key fails on its first write and in tests instead of at the
  next startup.
- paired-session-binding imports the prefix constant instead of redefining it.
- Regression tests: app-written keys survive a re-init; unregistered keys are
  rejected on write.
- Add the incident report (docs/fix-2026-10-03-router-state-startup.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-03 05:28:40 +09:00
parent 2e285c5d78
commit 11c179c565
4 changed files with 324 additions and 15 deletions

View File

@@ -70,6 +70,7 @@ import {
updateTask,
} from './db.js';
import { initializeDatabaseSchema } from './db/bootstrap.js';
import { bindPairedSessionToTask } from './paired-session-binding.js';
import {
buildPairedTurnAttemptId,
buildPairedTurnAttemptParentId,
@@ -1990,6 +1991,31 @@ describe('room assignment writes', () => {
rawDb.close();
});
it('starts with the router_state keys the app itself writes', () => {
// Regression: paired_session_task:* keys written at runtime made the next
// startup fail with exit 78 and crash-loop the service (2026-10-03).
const tempDir = fs.mkdtempSync('/tmp/ejclaw-router-state-app-keys-');
const dbPath = path.join(tempDir, 'messages.db');
_initTestDatabaseFromFile(dbPath);
setRouterState('last_seq', '42');
setRouterState('last_agent_seq', '{"dc:room":"7"}');
bindPairedSessionToTask('room', 'task-1');
bindPairedSessionToTask('room:reviewer', 'task-1');
expect(() => _initTestDatabaseFromFile(dbPath)).not.toThrow();
});
it('rejects writing an unregistered router_state key', () => {
_initTestDatabase();
expect(() => setRouterState('some_new_feature:room', 'x')).toThrow(
/Unregistered router_state key "some_new_feature:room"/,
);
expect(() => setRouterState('last_timestamp', '1')).toThrow(
/Unregistered router_state key/,
);
});
it('ignores stale registered_groups capability rows once room_settings exists', () => {
const tempDir = fs.mkdtempSync('/tmp/ejclaw-room-ssot-');
const dbPath = path.join(tempDir, 'messages.db');

View File

@@ -26,11 +26,42 @@ export function getRouterStateFromDatabase(
return prefixedRow.value;
}
/**
* Every router_state key the app writes must be registered here.
*
* Startup refuses to run while an unregistered key is in the table (it guards
* against legacy keys such as `last_timestamp`). A feature that wrote a new key
* without registering it passed until the next restart, then crash-looped the
* service (2026-10-03, `paired_session_task:*`). Writes are now checked against
* the same list, so an unregistered key fails on its first write and in tests
* instead of at the next startup.
*/
const SUPPORTED_ROUTER_STATE_KEYS = new Set(['last_seq', 'last_agent_seq']);
export const PAIRED_SESSION_TASK_KEY_PREFIX = 'paired_session_task:';
const SUPPORTED_ROUTER_STATE_KEY_PREFIXES = [PAIRED_SESSION_TASK_KEY_PREFIX];
export function isSupportedRouterStateKey(key: string): boolean {
if (
SUPPORTED_ROUTER_STATE_KEY_PREFIXES.some((prefix) => key.startsWith(prefix))
) {
return true;
}
// Legacy service-prefixed form (`<serviceId>:last_seq`) is migrated on read.
const separator = key.indexOf(':');
const baseKey = separator >= 0 ? key.slice(separator + 1) : key;
return SUPPORTED_ROUTER_STATE_KEYS.has(baseKey);
}
export function setRouterStateInDatabase(
database: Database,
key: string,
value: string,
): void {
if (!isSupportedRouterStateKey(key)) {
throw new Error(
`Unregistered router_state key "${key}": add it to src/db/router-state.ts or startup will reject the DB`,
);
}
database
.prepare('INSERT OR REPLACE INTO router_state (key, value) VALUES (?, ?)')
.run(key, value);
@@ -40,20 +71,12 @@ export function getUnsupportedRouterStateKeysFromDatabase(
database: Database,
): string[] {
const rows = database
.prepare(
`SELECT key
FROM router_state
WHERE (
CASE
WHEN instr(key, ':') > 0 THEN substr(key, instr(key, ':') + 1)
ELSE key
END
) NOT IN ('last_seq', 'last_agent_seq')
ORDER BY key`,
)
.prepare('SELECT key FROM router_state ORDER BY key')
.all() as Array<{ key: string }>;
return rows.map((row) => row.key);
return rows
.map((row) => row.key)
.filter((key) => !isSupportedRouterStateKey(key));
}
export function getLastRespondingAgentTypeFromDatabase(

View File

@@ -1,4 +1,5 @@
import { getRouterState, setRouterState } from './db.js';
import { PAIRED_SESSION_TASK_KEY_PREFIX } from './db/router-state.js';
/**
* Paired-room agent sessions are scoped to one paired task.
@@ -12,10 +13,8 @@ import { getRouterState, setRouterState } from './db.js';
* The task a session belongs to is kept in router_state, keyed by the session
* folder, so the scope survives a restart.
*/
const SESSION_TASK_KEY_PREFIX = 'paired_session_task:';
function bindingKey(sessionFolder: string): string {
return `${SESSION_TASK_KEY_PREFIX}${sessionFolder}`;
return `${PAIRED_SESSION_TASK_KEY_PREFIX}${sessionFolder}`;
}
export function getPairedSessionTaskId(