Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): create real identity before stamping, not the unknown sentinel (#1563)

[._.]/ Adam Eivyf6336a7fbcb6
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index dc82b4a707..e59d39b153 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -27,7 +27,7 @@ import { extractCodexAssistantTail } from '../lib/codexAssistantExtract.js';
import { buildTuiSpawnConfig, spawnTuiAgent } from './agentTuiSpawning.js';
import { processAgentCompletion } from './agentCompletion.js';
import { releaseAppReviewMarker } from './appActivity.js';
-import { getInstanceId } from './instances.js';
+import { getInstanceId, ensureSelf, UNKNOWN_INSTANCE_ID } from './instances.js';
import { runnerAgents, pausedAgents, spawningTasks, useRunner, isTruthyMeta } from './agentState.js';
import { v4 as uuidv4 } from '../lib/uuid.js';
@@ -265,9 +265,20 @@ export async function spawnAgentForTask(task) {
// the completed-agent archive's `metadata.json` automatically (completeAgent
// serializes `.metadata`), so once CoS agent history federates across peers a
// node pair can attribute each agent + its worktree branch to the instance
- // that produced it. `getInstanceId()` never throws — it returns the
- // `UNKNOWN_INSTANCE_ID` sentinel before the local identity is initialized.
- const instanceId = await getInstanceId();
+ // that produced it.
+ //
+ // `getInstanceId()` returns the `UNKNOWN_INSTANCE_ID` sentinel (and never
+ // throws) before the local identity has been created — which can happen on a
+ // boot-time always-on auto-start that spawns an agent before the startup
+ // chain's `ensureSelf()` runs. Persisting that sentinel would leave the
+ // archived agent permanently unattributable, so on the cold path we create
+ // (or load) the real identity here before stamping. The warm path stays a
+ // cheap cached read — `ensureSelf()` only runs the once, when identity is
+ // genuinely missing.
+ let instanceId = await getInstanceId();
+ if (instanceId === UNKNOWN_INSTANCE_ID) {
+ instanceId = (await ensureSelf())?.instanceId || instanceId;
+ }
await registerAgent(agentId, task.id, {
instanceId,
workspacePath,
diff --git a/server/services/agentLifecycle.test.js b/server/services/agentLifecycle.test.js
index e9e9674bbe..e7375dbb6b 100644
--- a/server/services/agentLifecycle.test.js
+++ b/server/services/agentLifecycle.test.js
@@ -658,9 +658,9 @@ describe('spawnAgentForTask — cleanupOnError error recovery', () => {
// serializes `.metadata`. These source-level assertions break loudly if a
// future refactor drops the stamp.
describe('agentLifecycle — instance provenance stamping (#1563)', () => {
- it('source: imports getInstanceId from the instances service', () => {
+ it('source: imports the identity helpers from the instances service', () => {
expect(AGENT_LIFECYCLE_SRC).toMatch(
- /import\s*\{\s*getInstanceId\s*\}\s*from\s*'\.\/instances\.js';/
+ /import\s*\{\s*getInstanceId\s*,\s*ensureSelf\s*,\s*UNKNOWN_INSTANCE_ID\s*\}\s*from\s*'\.\/instances\.js';/
);
});
@@ -674,6 +674,20 @@ describe('agentLifecycle — instance provenance stamping (#1563)', () => {
expect(resolveIdx, 'instanceId must be resolved BEFORE registerAgent is called').toBeLessThan(registerIdx);
});
+ it('source: falls back to ensureSelf() when identity is the unknown sentinel (never archives "unknown")', () => {
+ // Boot-time always-on auto-start can spawn an agent before the startup
+ // chain's ensureSelf() runs, so getInstanceId() can return the sentinel.
+ // The cold path must create/load the real identity rather than persist it.
+ const fnStart = AGENT_LIFECYCLE_SRC.indexOf('export async function spawnAgentForTask');
+ const fnBody = AGENT_LIFECYCLE_SRC.slice(fnStart, fnStart + 60_000);
+ const guardIdx = fnBody.indexOf('=== UNKNOWN_INSTANCE_ID');
+ const ensureIdx = fnBody.indexOf('await ensureSelf()');
+ const registerIdx = fnBody.indexOf('registerAgent(agentId, task.id, {');
+ expect(guardIdx, 'must guard on the UNKNOWN_INSTANCE_ID sentinel').toBeGreaterThan(-1);
+ expect(ensureIdx, 'must call ensureSelf() on the cold path').toBeGreaterThan(guardIdx);
+ expect(ensureIdx, 'the sentinel fallback must run BEFORE registerAgent').toBeLessThan(registerIdx);
+ });
+
it('source: stamps instanceId into the registerAgent metadata', () => {
const registerIdx = AGENT_LIFECYCLE_SRC.indexOf('registerAgent(agentId, task.id, {');
// The metadata object literal opens at registerIdx; assert `instanceId,`